中国剩余定理-——逐一合并大法
题目链接 This is the link
Problem Description
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一群鸭,快来快来 数一数,二四六七八". And then the cashier put the counted coins back morosely and count again...
Hello Kiki is such a lovely girl that she loves doing counting in a different way. For example, when she is counting X coins, she count them N times. Each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note.
One day Kiki's father found her note and he wanted to know how much coins Kiki was counting.
Input
The first line is T indicating the number of test cases.
Each case contains N on the first line, Mi(1 <= i <= N) on the second line, and corresponding Ai(1 <= i <= N) on the third line.
All numbers in the input and output are integers.
1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai < Mi
Output
For each case output the least positive integer X which Kiki was counting in the sample output format. If there is no solution then output -1.
Sample Input
2 2 14 57 5 56 5 19 54 40 24 80 11 2 36 20 76
Sample Output
Case 1: 341 Case 2: 5996
Author
digiter (Special Thanks echo)
Source
2010 ACM-ICPC Multi-University Training Contest(14)——Host by BJTU
Recommend
zhouzeyong | We have carefully selected several similar problems for you: 3573 3574 3575 3576 3577
题目大意&&解题思路:
直接套用中国剩余定理——余数与除数不互质的算法模板
This is the code
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include <iomanip>
#include<list>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x7f7f7f7f //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios.sync_with_stdio(false);
// 那么cin, 就不能跟C的scanf,sscanf,getchar,fgets之类的一起使用了。
LL ext_gcd(LL a,LL b,LL &x,LL &y)//扩展欧几里得算法
{
if(!b)
{
x=1,y=0;
return a;
}
LL gcd=ext_gcd(b,a%b,y,x);//与下面的形式一样
y=y-a/b*x;//注意这里
/*
LL gcd=ext_gcd(b,a%b,x,y);
LL temp=x-a/b*y;
x=y;
y=temp;
*/
return gcd;
}
//逐一合并大法
LL CRT(int w[],int b[],int k)//w为除数,b为余数,k为有多少式子
{
LL Wi=w[0],ret=b[0];
for(int i=1;i<k;++i)
{
LL wi=w[i];
LL bi=b[i];
LL x,y;
LL gcd=ext_gcd(Wi,wi,x,y);
LL c=bi-ret;
if(c%gcd)//表示没有结果
return -1;
LL W=wi/gcd;
ret+=Wi*(((c/gcd*x)%W+W)%W);
Wi*=W;
}
if(!ret)//表示余数全部为零
{
ret=1;
for(int i=0;i<k;++i)
ret=ret*w[i]/__gcd(ret,(LL)w[i]);//使用库函数求最小公倍数
}
return ret;
}
int main()
{
int w[55],b[55];
int T;
scanf("%d",&T);
for(int t=1;t<=T;++t)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;++i)
{
scanf("%d",&w[i]);//除数
}
for(int i=0;i<n;++i)
{
scanf("%d",&b[i]);//余数
}
LL ans=CRT(w,b,n);
printf("Case %d: %lld\n",t,ans);
}
return 0;
}