《洛谷P3868 [TJOI2009]猜数字》
将题意转化下,就是求满足$(n-a[i]) \equiv 0 ~mod~(b[i])$的最小的非负整数x。
转化一下$(n-a[i]) \equiv 0 ~mod~(b[i]) \rightarrow n \equiv a[i]~ mod (b[i])$
那么,就是个CRT。
这里的话,中间会爆longlong,就算中间取模还是会爆,所以套个快速乘来防止爆longlong
// Author: levil #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int,int> pii; const int N = 2e3+5; const int M = 2e6+5; const LL Mod = 1e9+7; #define rg register #define pi acos(-1) #define INF 1e9 #define CT0 cin.tie(0),cout.tie(0) #define IO ios::sync_with_stdio(false) #define dbg(ax) cout << "now this num is " << ax << endl; namespace FASTIO{ inline LL read(){ LL x = 0,f = 1;char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();} return x*f; } void print(int x){ if(x < 0){x = -x;putchar('-');} if(x > 9) print(x/10); putchar(x%10+'0'); } } using namespace FASTIO; void FRE(){/*freopen("data1.in","r",stdin); freopen("data1.out","w",stdout);*/} LL a[15],b[15]; LL exgcd(LL a,LL b,LL &x,LL &y) { if(b == 0) { x = 1,y = 0; return a; } LL t = exgcd(b,a%b,y,x); y -= a/b*x; return t; } LL Mul(LL a,LL b,LL p) { LL re = 0; while(b) { if(b&1) re = (re+a)%p; a = (a+a)%p; b >>= 1; } return re; } int main() { int n;n = read(); LL M = 1; for(rg int i = 1;i <= n;++i) a[i] = read(); for(rg int i = 1;i <= n;++i) b[i] = read(),M *= b[i]; LL ans = 0; for(rg int i = 1;i <= n;++i) { LL Mi = M/b[i],x,y; LL gcd = exgcd(Mi,b[i],x,y); x = (x+b[i])%b[i]; LL ma = Mul(x*a[i]%M,Mi,M); ans = (ans+ma)%M; } printf("%lld\n",ans); system("pause"); }