跳到正文

题解

题解:P1001 A+B Problem

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

文章信息

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

版本

  1. v1当前

如果不感兴趣,可以直接提交这一行代码:

Python
print(sum((map(int,input().split())))

接下来使用线段树算法解决这个问题。

将问题建模为区间加,区间查询,那么本题简化为:先在区间 [1,1][1,1] 每个数字加上一个数,再在区间 [2,2][2,2] 每个数字加上一个数,并查询区间 [1,2][1,2] 的和。

但是我们发现这样是不够简化的,所以还要加上区间乘法。

我们将操作看作:区间 [1,2][1,2] 全部加上 11,然后区间 [1,1][1,1] 乘上一个数,区间 [2,2][2,2] 乘上一个数,最后输出答案。

但是我们发现逆元没有出现,那么继续简化为题为区间 [1,100000][1,100000] 区间加上 11,然后区间 [1,50000][1,50000] 乘上一个数,区间 [50001,100000][50001,100000] 乘上一个数,最后输出区间 [1,100000][1,100000] 的和除以 5000050000 在模 1000000000003710000000000037 意义下的答案。

而这个时候我们知道上面那一坨数是一个质数,由 NotDeep 小定理可得在模 pp 意义下除以 xx 等于乘上 xp−2x^{p - 2}。

要注意要开 __int128。

AC Code:

C++
#include <iostream>
#include <queue>
using namespace std;
__int128 n, q, mod = 10000000000037;
struct node{
	__int128 l, r, sum, tag_p, tag_m;
};
node t[800100];
void update(int p) {
	t[p].sum = t[p * 2].sum + t[p * 2 + 1].sum;
	t[p].sum %= mod;
}
void push_tag(__int128 p) {
	t[p * 2].sum = (t[p * 2].sum * t[p].tag_m + t[p].tag_p * (t[p * 2].r - t[p * 2].l + 1)) % mod;
	t[p * 2 + 1].sum = (t[p * 2 + 1].sum * t[p].tag_m + t[p].tag_p * (t[p * 2 + 1].r - t[p * 2 + 1].l + 1)) % mod;
	t[p * 2].tag_m = (t[p * 2].tag_m * t[p].tag_m) % mod;
	t[p * 2 + 1].tag_m = (t[p * 2 + 1].tag_m * t[p].tag_m) % mod;
	t[p * 2].tag_p = (t[p * 2].tag_p * t[p].tag_m + t[p].tag_p) % mod;
	t[p * 2 + 1].tag_p = (t[p * 2 + 1].tag_p * t[p].tag_m + t[p].tag_p) % mod;
	t[p].tag_p = 0;
	t[p].tag_m = 1;
}

void make_tree(__int128 l, __int128 r, __int128 p) {
	t[p].l = l;
	t[p].r = r;
	t[p].tag_p = 0, t[p].tag_m = 1;
	if (l < r) {
		make_tree(l, (l + r) / 2, p * 2);
		make_tree((l + r) / 2 + 1, r, p * 2 + 1);
	}
}
void add(__int128 l, __int128 r, __int128 p, __int128 x) {
	if (l > t[p].r || r < t[p].l) return;
	push_tag(p);
	if (l <= t[p].l && t[p].r <= r) {
		t[p].tag_p += x;
		t[p].tag_p %= mod;
		t[p].sum = (t[p].sum + t[p].tag_p * (t[p].r - t[p].l + 1) % mod) % mod;
		return;
	}
	add(l, r, p * 2, x);
	add(l, r, p * 2 + 1, x);
	update(p);
}
void mul(__int128 l, __int128 r, __int128 p, __int128 x) {
	if (l > t[p].r || r < t[p].l) return;
	push_tag(p);
	if (l <= t[p].l && t[p].r <= r) {
		t[p].tag_p *= x;
		t[p].tag_p %= mod;
		t[p].tag_m *= x;
		t[p].tag_m %= mod;
		t[p].sum = (t[p].sum * t[p].tag_m) % mod;
		return;
	}
	mul(l, r, p * 2, x);
	mul(l, r, p * 2 + 1, x);
	update(p);
}
__int128 get(__int128 l, __int128 r, __int128 p) {
	if (l > t[p].r || r < t[p].l) return 0;
	push_tag(p);
	if (l <= t[p].l && t[p].r <= r) {
		return t[p].sum;
	}
	return (get(l, r, p * 2) + get(l, r, p * 2 + 1)) % mod;
}
__int128 fpow(__int128 a, long long b) {
	__int128 ans = 1;
	while (b) {
		if (b & 1) ans = (ans * a) % mod;
		a = a * a % mod, b >>= 1;
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	long long a, b;
	cin >> a >> b;
	int n = 100000;
	make_tree(1, n, 1);
	add(1, n, 1, 1);
	mul(1, n / 2, 1, a);
	mul(n / 2 + 1, n, 1, b);
	__int128 ans = get(1, 100000, 1);
	ans *= fpow(n / 2, mod - 2);
	ans %= mod;
	cout << (long long)ans << '\n';
	return 0;
}
原文
存档评论12 条抓取于