题解:P1001 A+B Problem
smart_stupid · · 题解
如果不感兴趣,可以直接提交这一行代码:
print(sum((map(int,input().split())))
接下来使用线段树算法解决这个问题。
将问题建模为区间加,区间查询,那么本题简化为:先在区间
但是我们发现这样是不够简化的,所以还要加上区间乘法。
我们将操作看作:区间
但是我们发现逆元没有出现,那么继续简化为题为区间
而这个时候我们知道上面那一坨数是一个质数,由 NotDeep 小定理可得在模
要注意要开 __int128。
AC Code:
#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;
}