求助 WA on #7
查看原帖
求助 WA on #7
255169
__LePetitPrince__楼主2024/3/18 23:47

__int128 开了,负数应该也处理了还是 WA 求助大佬 Orz

// g++ P3868.cpp -o 1
#include <iostream>
#include <stack>
using namespace std;
typedef __int128 ll;
const int S = 15;
ll n, a[S], b[S], M = 1, m[S], t[S];
void read(ll &x) {
    ll s = 0, f = 1; char c = getchar();
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    x = s * f;
}
void exgcd(ll a, ll b, ll &x, ll &y) {
    if (a % b == 0) x = 0, y = 1;
    else exgcd(b, a % b, y, x), y -= a / b * x;
}
ll inv(ll a, ll p) {
    ll x, y;
    exgcd(a, p, x, y);
    return (x % p + p) % p;
}
void print(ll x) {
    stack<int> s;
    while (x) s.push(x % 10), x /= 10;
    while (!s.empty()) printf("%d", s.top()), s.pop();
}
int main() {
    read(n);
    for (int i = 1; i <= n; i++) read(a[i]);
    for (int i = 1; i <= n; i++) read(b[i]), M *= b[i];
    for (int i = 1; i <= n; i++) m[i] = M / b[i], t[i] = inv(m[i] % b[i], b[i]);
    ll ans = 0;
    for (int i = 1; i <= n; i++) a[i] = (a[i] % b[i] + b[i]) % b[i];
    for (int i = 1; i <= n; i++) ans += a[i] * t[i] * m[i] % M, ans %= M;
    print(ans);
    return 0;
}
2024/3/18 23:47
加载中...