【中国剩余定理-M不互质的情况】HDU Hello Kiki 2579

Hello Kiki

                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                              Total Submission(s): 2692    Accepted Submission(s): 991



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

题意:

一群鸭子,数数数。。。M个数剩余A个。。求最少有多少鸭子。

解题思路:

中国剩余定理。。但是M不一定互质,所以不能像原来那么求,可以合并所有的方程为一个同余方程,求解一个同余式就可以了。

关键是合并,在网上发现一张图,讲的不错。直接看这个吧!

(n1/d)-1  是(n1/d)的逆。


AC代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>

using namespace std;

typedef long long LL;

LL gcd(LL a,LL b)
{
	if(!b)return a;
	return gcd(b,a%b);
}

LL exgcd(LL a,LL b,LL &x,LL &y)
{
	if(!b){
		x=1;y=0;
		return a;
	}
	LL r=exgcd(b,a%b,x,y);
	LL t=x;
	x=y;
	y=t-(a/b)*y;
	return r;
}

LL mod_reverse(LL A,LL B)
{
	LL x,y;
	LL d=exgcd(A,B,x,y);
	if(d==1) return (x%B+B)%B;
	return -1;
}

LL Merge(LL &A,LL B,LL &mod_A,LL mod_B)
{
	LL C=gcd(mod_A,mod_B);
	LL d=(B-A);
	if(d%C) return -1;
	d=((d/C)%((mod_A*mod_B)/C)+(mod_A*mod_B)/C)%((mod_A*mod_B)/C);//必须取成正整数
	LL A1=mod_reverse(mod_A/C,mod_B/C);
	LL K=A1*d;
	A=mod_A*K+A;
	mod_A=(mod_A*mod_B)/C;
}

LL CRT(LL *M,LL *A,int l)
{
	for(LL i=1;i<l;i++){
		if(Merge(A[0],A[i],M[0],M[i])==-1) return -1;
	}
	if(A[0]==0) return M[0];//考虑到整除的情况。
	LL M1;
	LL a=1;
	LL x,y;
	LL d=exgcd(a,M[0],x,y);
	if(d==1) return ((x*A[0])%M[0]+M[0])%M[0];
	return -1;
}

int main()
{
    LL t;
    int xp=1;
    scanf("%d",&t);
    while(t--){
		int N;
		LL M[10],A[10];
		scanf("%d",&N);
		for(LL i=0;i<N;i++)
			scanf("%lld",&M[i]);
		for(LL i=0;i<N;i++)
			scanf("%lld",&A[i]);
		LL res=CRT(M,A,N);
		if(res==-1) printf("Case %d: -1\n",xp++);
		else printf("Case %d: %lld\n",xp++,res);
    }
    return 0;
}


你可能感兴趣的:(【中国剩余定理-M不互质的情况】HDU Hello Kiki 2579)