恶补数论(一)中国剩余定理

我果然是个数(bei)学(jing)一片空白的渣渣

问题模型

给出n个同余方程,x≡a[i](mod m[i]),求x的解(可能有很多种)

求解方式

来自百度百科的解说,我稍加改动,简单明了

中国剩余定理说明:假设整数m1,m2, ... ,mn两两互质,则对任意的整数:a1,a2, ... ,an,方程组
有解,并且通解可以用如下方式构造得到:
是整数m1,m2, ... ,mn的乘积,并设
是除了mi以外的n- 1个整数的乘积。
的数论倒数(
意义下的逆元),也即
那么我们有一个结论(证明在后头),方程组
的通解形式为
:在模
的意义下,方程组
只有一个解:
证明:
从假设可知,对任何
,由于
,所以
这说明存在整数
使得
(m两两互质,且M是取出m后的乘积,自然存在整数t咯)
这样的
叫做
的数论倒数。考察乘积
可知:
(当然是在mod mi意义下的)
所以
满足:
(自己意淫一下就知道了吧)
这说明
就是方程组
的一个解。
另外,假设
都是方程组
的解,那么:
两两互质,这说明
整除
. 所以方程组
的任何两个解之间必然相差
的整数倍(理所当然)。而另一方面,
是一个解,同时所有形式为:
(这就给了我们一个通解的形式?公式?)
的整数也是方程组
的解。所以方程组所有的解的集合就是:
 
好了,说了原理,我们上模板(

算法模板

#define L long long

void exgcd(int a,int b,int &x,int &y){
    if(b==0){
        x=1; y=0;
        return;
    }
    exgcd(b,a%b,x,y);
    int tmp=x; x=y; y=tmp-(a/b)*y;
}

//给出n个方程,x≡a[i](mod m[i])

L China_Remainder_Theorem(int n,int *A,int *M){
    int M=1,ans=0,x,y;
    for(int i=0;i<n;i++) M*=a[i];
    for(int i=0;i<n;i++){
        L Mi=M/m[i];
        exgcd(Mi,m[i],x,y);
        ans=(ans+Mi*x*a[i])%M;
    }
    return (ans+M)%M;
}

应用:[poj1006][Biorhythms]

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

题意:人有智商\情商\体力三个周期,在周期日出现高峰,求三周期的同一出现日在什么时候
#include <stdio.h>
#define RG register
#define inline __inline__ __attribute__((always_inline))

int a[4],m[4]={23,28,33},T,d;

void exgcd(RG int a,RG int b,RG int &x,RG int &y){
  if(!b){
    x=1; y=0;
    return;
  }
  exgcd(b,a%b,x,y);
  RG int tmp=x;
  x=y; y=tmp-(a/b)*y;
}

inline void CRT(){
  RG int M=1,ans=0,x,y;
  for(RG int i=0;i<3;i++)
    M*=m[i];
  for(RG int i=0;i<3;i++){
    RG int MI=M/m[i];
    exgcd(MI,m[i],x,y);
    ans=(ans+MI*x*a[i])%M;
  }
  if(ans<0)ans+=M;
  if(ans<=d)ans+=21252;
  printf("Case %d: the next triple peak occurs in %d days.\n",++T,ans-d);
}

int main(){
  while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&d))
    if(~a[0]) CRT();
    else break;
  return 0;
}

 

posted @ 2017-01-22 20:20  keshuqi  阅读(244)  评论(0编辑  收藏  举报