跳到正文

题解

题解:P1001 A+B Problem

Q1_Only约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年5月10日
收录
2026年5月27日
更新
2026年11月13日
篇幅
约 6 分钟
获赞
11
题目
P1001

版本

  1. v1当前

提示:本题解不是正常写法,萌新勿看!

正解:

C++
#include<iostream>
using namespace std;
int main(){
    int a,b;cin>>a>>b;
    cout<<a+b<<"\n";
    return 0;
}

正式整活内容

不多废话,上代码。

  1. 高精度树状数组变式
C++
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN = 2e5 + 5;
int C[MAXN], m;
unsigned t;
string a, b;
int lowbit(int k) {
	return k & -k;
}
void add(int x, int k) {
	while (x <= 1145) {
		C[x] += k;
		x += lowbit(x);
	}
}
int solve(int a, int b) {
	--a;
	int suma, sumb;
	suma = sumb = 0;
	while (a) {
		suma += C[a];
		a -= lowbit(a);
	}
	while (b) {
		sumb += C[b];
		b -= lowbit(b);
	}
	return sumb - suma;
}
struct node {
	int x, y;
	bool operator < (node b) {
		if (x == b.x)
			return y < b.y;
		return x < b.x;
	}
} s[MAXN];
string VERY_EASY_PLUS(string a, string b) {
	string ans;
	for (int i = 0; i < a.size(); i++) {
		add(i + 1, a[i] - '0');
		if (solve(i + 1, i + 1) >= 10) {
			add(i + 2, 1);
			add(i + 1, -10);
		}
	}
	for (int i = 0; i < b.size(); i++) {
		add(i + 1, b[i] - '0');
		if (solve(i + 1, i + 1) >= 10) {
			add(i + 2, 1);
			add(i + 1, -10);
		}
	}//求和
	bool can = 0;
	for (int i = max(a.size(), b.size()) + 1; i; i--) {
		if (can || solve(i, i))
			ans.push_back(solve(i, i) + '0'), can = 1;
	}//转换
	return ans;
}
int CHANGE(string chen_zhe) {
	unsigned manba = (24, 0);
	for (auto i : chen_zhe) {
		manba = manba * 10 + i - '0';
	}
	return (int)manba;//转换
}
int main() {
	ios::sync_with_stdio(false);
	cin >> t;
	while (t) {
		a.push_back(t % 10 + '0');
		t /= 10;
	}
	cin >> t;
	while (t) {
		b.push_back(t % 10 + '0');
		t /= 10;
	}
	cout << CHANGE(VERY_EASY_PLUS(a, b));
	return 0;
}
  1. 最短路变式
C++
#include<iostream>
#include<algorithm>
#include<queue>
#include<utility>
#include<cstring>
using namespace std;
#define N 100005
struct BI {
	int to, ne, v;
};
int h[N] = {0}, cnt;
BI e[2 * N];
void add(int a, int b, int v) {
	e[++cnt] = {b, h[a], v};
	h[a] = cnt;
}
struct CMP_pii {
	bool operator()(const pair<int, unsigned> &n1, const pair<int, unsigned> &n2)const {
		if (n1.second == n2.second)
			return n1.first < n2.first;
		return n1.second < n2.second;
	}
};
unsigned dis[N];
void unDijkstra(int st) {
	memset(dis, 0xff, sizeof(dis));
	priority_queue<pair<int, unsigned>, vector<pair<int, unsigned> >, CMP_pii > pq;
	pq.push(make_pair(st, 0));
	while (pq.size()) {
		if (pq.top().second > dis[pq.top().first]) {
			pq.pop();
			continue;
		}
		pair<int, unsigned> top = pq.top();
		pq.pop();
		dis[top.first] = top.second;
		for (int i = h[top.first]; i; i = e[i].ne) {
			dis[e[i].to] = top.second * 10 + e[i].v, pq.push(make_pair(e[i].to, top.second * 10 + e[i].v));
		}
	}
}
int main() {
	unsigned a, b;
	cin >> a >> b;
	unsigned m = 0;
	for (int i = 1; i <= 11; i++) {
		unsigned t = a % 10 + b % 10 + m;
		m = t / 10;
		t %= 10;
		add(i + 1, i, t);
		a /= 10;
		b /= 10;
	}
	unDijkstra(11);
	cout << (int)dis[1];
	return 0;
}
原文
存档评论10 条抓取于