题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3579
题意:Kiki有x个硬币,用了m种方法分,每次记录基数和剩余量,他爸爸呢,想要知道他的小金库有多少钱,但不会算,让你算一下;
分析:很显然是一次同余方程组求解,但有一点,没有0解;
具体代码如下:
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <utility>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
long long gcd(long long a,long long b){
if(b==0)return a;
return gcd(b,a%b);
}
void _gcd(long long a,long long b,long long &x,long long &y){
if(b==1){
x=1;
y=1-a;
}
else{
long long x1,y1;
_gcd(b,a%b,x1,y1);
x=y1;
y=x1-(a/b)*x;
}
}//扩展欧几里得算法
long long a[50],b[50];
int main(){
int t;
cin>>t;
int k=1;
while(t--){
long long m;
cin>>m;
for(int i=0;i<m;i++)
cin>>a[i];
for(int i=0;i<m;i++)
cin>>b[i];
long long a1,a2,b1,b2;
a1=a[0],b1=b[0];
int flag=0;
long long x,y;
long long _a,_b;
long long sum1=1;
long long sum2=a1;
long long c;
for(int i=1;i<m;i++){
sum2*=a[i];
a2=a[i],b2=b[i];
// cout<<b2<<endl;
long long g=gcd(a1,a2);
c=b2-b1;
if(c%g!=0){
flag=1;
break;
}
c/=g;
_a=a1/g;
_b=a2/g;
_gcd(_a,_b,x,y);
x=(x*c%_b+_b)%_b;
x=x*a1+b1;
b1=x;
sum1*=g;
a1=_b*a1;
}
cout<<"Case "<<k++<<": ";
if(flag)cout<<-1<<endl;
else{
if(b1!=0)
cout<<b1<<endl;
else cout<<sum2/sum1<<endl;
}
}
return 0;
}