题目
思路
显然的CRT,当你提交后……
Wrong answer on #10
实际上为了不爆long long,我们要用快龟速乘
code:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
long long n,a[20],m[20],M=1,ans,x,y;
void exgcd(long long a,long long b,long long &x,long long &y)
{
if (b==0)
{
x=1;
y=0;
return;
}
exgcd(b,a%b,x,y);
long long t=x;
x=y,y=t-a/b*y;
return;
}
long long gscf(long long x,long long y,long long myd)
{
long long ans=0;
while (y)
{
if (y&1) ans=(ans+x)%myd;
x=(x+x)%myd;
y>>=1;
}
return ans;
}
int main()
{
cin>>n;
for (int i=1;i<=n;i++) cin>>a[i];
for (int i=1;i<=n;i++) cin>>m[i],a[i]=(a[i]%m[i]+m[i])%m[i],M*=m[i];
for (int i=1;i<=n;i++)
{
exgcd(M/m[i],m[i],x,y);
x=(x%m[i]+m[i])%m[i];
ans=(ans+gscf(gscf(x,M/m[i],M),a[i],M))%M;
}
if (ans<0) ans+=M;
cout<<ans;
return 0;
}