首先:
孙子定理是中国古代求解一次同余式组(见同余)的方法。是数论中一个重要定理。又称中国余数定理。一元线性同余方程组问题最早可见于中国南北朝时期(公元5世纪)的数学著作《孙子算经》卷下第二十六题,叫做“物不知数”问题,原文如下:
有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二。问物几何?即,一个整数除以三余二,除以五余三,除以七余二,求这个整数。《孙子算经》中首次提到了同余方程组问题,以及以上具体问题的解法,因此在中文数学文献中也会将中国剩余定理称为孙子定理。
用现代数学的语言来说明的话,中国剩余定理给出了以下的一元线性同余方程组:
然后求解x的值
样例:
参考自https://blog.csdn.net/tomorrowtodie/article/details/51865496
Hello Kiki(hdu3579)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5095 Accepted Submission(s): 2007
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)
直接做模板了。。。
代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
ll e_gcd (ll a,ll b,ll& x,ll& y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
ll ans = e_gcd (b, a % b, y, x);
y -= a / b * x;
return ans;
}
ll CR (int a[],int m[],int n)
{
ll Mi=m[1],ans=a[1];
for (int i = 2; i <= n; ++i)
{
ll mi = m[i], ai = a[i];
ll x, y;
ll gcd = e_gcd (Mi, mi, x, y);
ll c = ai - ans;
if (c % gcd != 0) return -1;
ll M = mi / gcd;
ans += Mi * ( ( (c /gcd*x) % M + M) % M);
Mi *= M;
}
if (ans == 0) //当余数都为0
{
ans = 1;
for (int i = 1; i <= n; ++i)
{
ans = ans*m[i]/__gcd(ans,(ll)m[i]);
}
}
return ans;
}
int main()
{
int T;cin>>T;int kas = 0;
while (T--)
{
int n,a[100],m[100];
scanf("%d",&n);
for (int i = 1;i <= n;++i) scanf("%d",m+i);
for (int i = 1;i <= n;++i) scanf("%d",a+i);
printf("Case %d: %lld\n",++kas,CR(a,m,n));
}
return 0;
}
推荐的另外一个题目 Biorhythms
另外:
上面的那个不带有a不存在时候的特判,下面给出一个带有的同样是一个题的代码:
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[100005],r[100005];
int n;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==0)
return x=1,y=0,a;
ll tmp=exgcd(b,a%b,y,x);
y-=a/b*x;
return tmp;
}
ll solve()
{
ll M=a[1],R=r[1],x,y,d;
for(int i=2; i<=n; i++)
{
d=exgcd(M,a[i],x,y);
if((R-r[i])%d!=0)
return -1;
x=(R-r[i])/d*x%a[i];
R-=x*M;
M=M/d*a[i];
R%=M;
}
return (R%M+M)%M;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
scanf("%lld%lld",&a[i],&r[i]);
printf("%lld\n",solve());
}
return 0;
}