您的位置:首页 > 其它

中国剩余定理 /孙子定理

2015-10-10 23:52 337 查看
                                                   
                  中国剩余定理

1.若gcd(m,M)=1,则存在整数t,使得 t*M≡1(mod m)

   存在t使得 t*M=1(mod m) 等价于 X*M+m*Y=1 有解
 
这个在  我写的 拓展欧几里德  里面已经详细证明过。

2. 方程组 X=a[i](mod m[i]) (且对于i!=j,gcd(m[i],m[j])=1)的解为 X=∑(a[i]*t[i]*M[i])+k*M(k∈Z)

    设有方程组 X=a[i](mod m[i]) (i=1,2,3,...,n)
     下面来求它的解
   
  令 M= m[1]*m[2]*...*m
;
      M[i]=M/m[i];
因为  i!=j时,m[i]与m[j]互质,所以i!=j,有 m[i],M[i] 互质(因为 M[i]=m[1]*m[2]*...*m
/m[i])
 
即gcd(m[i],M[i])=1,所以存在整数t[i],使得t[i]*M[i]≡1(mod m[i]);

令小x=∑a[i]*t[i]*M[i],
x%m[i]=  a[i]*t[i]*M[i]%m[i]+∑(j!=i)a[j]*t[j]*M[j]%m[i]
           =      a[i]*1%m[i]+  0  //因为M[i]*t[i]%m[i]=1  ,M[j]中包含m[i]
           =   a[i]%m[i]
所以x=a[i](mod m[i]),是一组满足方程组的解

因为{m[i]}中元素两两互质,所以解集为X=∑a[i]*t[i]*M[i]+k*M(k∈Z)

3.代码

   “今有物不知其数,三三数之余二 ,五五数之余三 ,七七数之余二,问物几何”

#include<cstdio>
#include<iostream>
using namespace std;

const int maxn= 10   ;
int a[maxn],m[maxn];
int cnt;

int exgcd(int a,int b,int &X,int &Y)
{
if(!b)
{
X=1,Y=0;
return a;
}
int gcd=exgcd(b,a%b,X,Y);
int X1=X,Y1=Y;
X=Y1;
Y=X1-a/b*Y1;
return gcd;
}

int Chinese_Remainder()
{
int M=1;int X,Y;
int ans=0;
for(int i=1;i<=cnt;i++)
M*=m[i];
cout<<"M "<<M<<endl;
for(int i=1;i<=cnt;i++)
{
int M0=M/m[i];
cout<<"M0 "<<M0<<endl;
exgcd(M0,m[i],X,Y);
//        X=(X%m[i]+m[i])%m[i];  此时不必取最小,因为ans必然是M的倍数,在后面做了%M+M再%M处理  就可以保证ans最小非负
//        cout<<X<<endl;
ans=(ans+ a[i]*X*M0)%M;
}
return (ans+M)%M;

}
int main()
{
cnt=0;
int a0,m0;
while(scanf("%d%d",&m0,&a0)==2)
{
cnt++;
m[cnt]=m0;
a[cnt]=a0;
}
printf("%d\n",Chinese_Remainder() );

return 0;
}
/*3 2 5 3 7 2*/




    
    

  4.poj 1006  生理周期

      

生理周期

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 122831 Accepted: 38749
Description

人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天的时间(距给定时间的天数)。例如:给定时间为10,下次出现三个高峰同天的时间是12,则输出2(注意这里不是3)。
Input

输入四个整数:p, e, i和d。 p, e, i分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d 是给定的时间,可能小于p, e, 或 i。 所有给定时间是非负的并且小于365, 所求的时间小于21252。 

当p = e = i = d = -1时,输入数据结束。
Output

从给定时间起,下一次三个高峰同天的时间(距离给定时间的天数)。 

采用以下格式: 

Case 1: the next triple peak occurs in 1234 days. 

注意:即使结果是1天,也使用复数形式“days”。
Sample Input
0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

Sample Output
Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

Source

East Central North America 1999
Translator

北京大学程序设计实习2007, Xie Di

Biorhythms

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 122831 Accepted: 38749
Description

Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one
peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier. 

Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will
be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine
the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give
the number of days to the next occurrence of a triple peak. 

Input

You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles
peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by
a line in which p = e = i = d = -1.
Output

For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form: 

Case 1: the next triple peak occurs in 1234 days. 

Use the plural form ``days'' even if the answer is 1.
Sample Input
0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

Sample Output
Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

Source

East Central North America 1999

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second

using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
//const int maxn=    ;
//const int maxm=    ;
//by yskysker123

ll m[4]={0, 23, 28, 33 };
ll a[7];

ll exgcd(ll a,ll b,ll &X ,ll& Y)
{
if(!b)
{
X=1,Y=0;
return a;
}
ll gcd=exgcd(b,a%b,X,Y);
ll X1=X,Y1=Y;
X=Y1;
Y=X1-a/b*Y1;
return gcd;

}

ll Chinese_Remainder()
{
ll M=1,ans=0;ll X,Y;
for(int i=1;i<=3;i++)  M*=m[i];
for(int i=1;i<=3;i++)
{
ll M0=M/m[i];
exgcd(M0,m[i],X,Y);
ans=(ans+X*M0*a[i])%M;

}
if(ans<=0)  ans+=M;
return ans;

}
int main()
{
ll x,y,z,now;
int kase=0;
while(~scanf("%lld%lld%lld%lld",&x,&y,&z,&now)&& (~x) )
{
a[1]=(m[1]-(now-x)%m[1])%m[1];
a[2]=(m[2]-(now-y)%m[2])%m[2];
a[3]=(m[3]-(now-z)%m[3])%m[3];
printf("Case %d: the next triple peak occurs in %lld days.\n",++kase,Chinese_Remainder());
}

return 0;
}


5.对于m[i]不互质的情况处理

 若有ans=a[1](mod m[1])

        ans=a[2](mod m[2])

且(m[1],m[2])!=1

显然有m[1]*X+a[1]=m[2]*Y+a[2];

      所以  m[1]*X-m[2]*Y=a[2]-a[1];

有解  等价于   gcd(m[1],m[2]) |(a[2]-a[1])

     ans=m[1]*X+a[1]

           =m[1]*(X+k*m[2]/gcd(m[1],m[2]) )+a[1]

            =m[1]*X+a[1]+k*m[1]*m[2]/gcd(m[1],m[2]) 

所以两个方程  等价为方程

ans=m[1]*X+a[1](mod m[1]*m[2]/gcd(m[1],m[2])  )

若该方程的mi 与其他方程 的mi 不互质,则可继续合并。

        




hdu 3579   Hello Kiki




Hello Kiki
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2705    Accepted Submission(s): 999


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

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn=  10  ;
//const int maxm=    ;
//by yskysker123
bool vis[maxn];
int n;
ll m[maxn],a[maxn];

ll exgcd(ll A,ll B,ll& X,ll & Y )
{
if(!B)
{
X=1,Y=0;
return A;
}
ll gcd=exgcd(B,A%B,X,Y);//
ll X1=X,Y1=Y;//这两行位置不可写反
X=Y1;
Y=X1-A/B*Y1;
return gcd;
}

bool cope(int i,int j)
{
ll X,Y;
ll gcd=exgcd(m[i],m[j],X,Y  );
if(  (a[j]-a[i])%gcd  )  return false;

ll b=abs(m[j]/gcd) ;
X*=(a[j]-a[i])/gcd;
X=(X % b+b)%b;

a[i]=m[i]*X+a[i];
m[i]=m[i]*m[j]/gcd ;

a[i]= (a[i]%m[i]+m[i])%m[i];

return true;

}

bool Work()
{
while(1)
{
bool has=0;
for(int i=1;i<=n&& !has;i++)
{
if(vis[i])  continue;
for(int j=i+1;j<=n;j++)
{
if(vis[j]||__gcd(m[i],m[j])==1)  continue;
has=1;
if( !cope(i,j) )  return false;
vis[j]=1;
break;
}
}
if(!has)  break;
}
/*
cout<<endl;
for(int i=1;i<=n;i++)
{
if(vis[i])  continue;
cout<<a[i]<<" "<<m[i]<<endl;
}*/

return true;
}

ll Chinese_Remainder()
{
ll M=1,ans=0;ll X,Y;
for(int i=1;i<=n;i++)
{
if(vis[i]) continue;
M*=m[i];
}
//    cout<<M<<endl;
for(int i=1;i<=n;i++)
{
if(vis[i])  continue;
ll M0=M/m[i];
exgcd(M0,m[i],X,Y);
/*      cout<<"M0 "<<M0<<endl;
cout<<"m[i] "<<m[i]<<"a[i] "<<a[i]<<endl;
cout<<X<<endl;
cout<<(X%m[i]+m[i])%m[i]<<endl;*/
ans=(ans+M0*X*a[i])%M;

}
if(ans<=0)  ans+=M;
return ans;
}
int main()
{
int T,kase=0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(vis,0,sizeof vis);
for(int i=1;i<=n;i++)
{
scanf("%lld",&m[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
printf("Case %d: ",++kase);
if(!Work())  puts("-1");
else printf("%lld\n",Chinese_Remainder());

}

return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: