Luogu P3868 Guess solution to a problem report

Topic Portal

[Title] effect

[Analysis] ideas

Is the Chinese remainder theorem board title wailing QwQ

The middle is going to blow $ long \ long $, simpler to use $ \ _ int128 $. I called turtle speed to practice hand ride, the result of $ T $ QAQ, a long time to change too.

【Code】

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #define g() getchar()
 8 #define rg register
 9 #define go(i,a,b) for(rg int i=a;i<=b;i++)
10 #define back(i,a,b) for(rg int i=a;i>=b;i--)
11 #define db double
12 #define ll long long
13 #define il inline
14 #define pf printf
15 #define mem(a,b) memset(a,b,sizeof(a))
16 using namespace std;
17 int fr(){
18     int w=0,q=1;
19     char ch=g();
20     while(ch<'0'||ch>'9'){
21         if(ch=='-') q=-1;
22         ch=g();
23     }
24     while(ch>='0'&&ch<='9') w=(w<<1)+(w<<3)+ch-'0',ch=g();
25     return w*q;
26 }
27 int n,a[12],b[12];
28 ll B=1,ans,x,y;
29 il ll mul(ll x,ll y){
30     ll as=0;
31     while(y){
32         if(y&1) as=(as+x)%B;
33         x=(x+x)%B;y>>=1;
34     }
35     return (as+B)%B;
36 }
37 il void exgcd(ll aa,ll bb,ll &x,ll &y){
38     if(!bb) {x=1;y=0;return;}
39     exgcd(bb,aa%bb,x,y);
40     ll z=x;x=y;y=z-y*(aa/bb);
41     return;
42 }
43 int main(){
44     //freopen("","r",stdin);
45     //freopen("","w",stdout);
46     n=fr();
47     go(i,1,n) a[i]=fr();
48     go(i,1,n) b[i]=fr(),B*=b[i];
49     go(i,1,n){
50         a[i]=(a[i]%b[i]+b[i])%b[i];
51         ll bb=B/b[i];
52         exgcd(bb,(ll)b[i],x,y);
53         x=(x%b[i]+b[i])%b[i];
54         ans=(ans+mul(bb,mul(x,a[i]))+B)%B;
55     }
56     pf("%lld\n",ans);
57     return 0;
58 }
Code poke here

Guess you like

Origin www.cnblogs.com/THWZF/p/11593780.html