标签:span ios scan can clu else problem tran 拓展
Vjudge
板子题。
拓展\(CRT\)模板题。
#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
#define MAX 111111
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b){x=1,y=0;return a;}
ll d=exgcd(b,a%b,y,x);
y-=a/b*x;return d;
}
int n;
ll m[MAX],a[MAX];
int main()
{
while(scanf("%d",&n)!=EOF)
{
bool fl=true;ll x,y;
for(int i=1;i<=n;++i)scanf("%lld%lld",&m[i],&a[i]);
for(int i=2;i<=n;++i)
{
ll d=exgcd(m[1],m[i],x,y),g=a[i]-a[1],t;
if(g%d){fl=false;break;}
x*=g/d;t=m[i]/d;x=(x%t+t)%t;
a[1]+=x*m[1];m[1]*=t;a[1]%=m[1];
}
if(!fl)puts("-1");
else printf("%lld\n",(a[1]%m[1]+m[1])%m[1]);
}
return 0;
}
【POJ2891】Strange Way to Express Integers(拓展CRT)
标签:span ios scan can clu else problem tran 拓展
原文地址:https://www.cnblogs.com/cjyyb/p/9316034.html