【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms

题目链接:

  http://poj.org/problem?id=1006

  http://acm.hdu.edu.cn/showproblem.php?pid=1370

题目大意

  (X+d)%23=a1,(X+d)%28=a2,(X+d)%33=a3,给定a1,a2,a3,d,求最小的X。

题目思路:

  【中国剩余定理】

  23,28,33互素,可以套中国剩余定理。

  也可以直接手算逆元。

  33×28×a模23的逆元为8,则33×28×8=5544;

  23×33×b模28的逆元为19,则23×33×19=14421;

  23×28×c模33的逆元为2,  则23×28×2=1288。

  因此有(5544×p+14421×e+1288×i)%lcm(23,28,33)=n+d (lcm(23,28,33)= 21252)

  所以n=(5544×p+14421×e+1288×i-d)%21252

  本题所求的是最小整数解,避免n为负,因此最后结果 n=(n+21252)% 21252

  so n=(5544*p+14421*e+1288*i-d+21252)%21252;

  

 1 //
 2 //by coolxxx
 3 //
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<memory.h>
 9 #include<time.h>
10 #include<stdio.h>
11 #include<stdlib.h>
12 #include<string.h>
13 //#include<stdbool.h>
14 #include<math.h>
15 #define min(a,b) ((a)<(b)?(a):(b))
16 #define max(a,b) ((a)>(b)?(a):(b))
17 #define abs(a) ((a)>0?(a):(-(a)))
18 #define lowbit(a) (a&(-a))
19 #define sqr(a) ((a)*(a))
20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
21 #define eps (1e-8)
22 #define J 10000000
23 #define MAX 0x7f7f7f7f
24 #define PI 3.1415926535897
25 #define N 4
26 using namespace std;
27 typedef long long LL;
28 int cas,cass;
29 int n,m,lll;
30 LL ans;
31 int p[N],a[N];
32 LL exgcd(LL a,LL b,LL &x,LL &y)
33 {
34     if(!b){x=1,y=0;return a;}
35     LL d=exgcd(b,a%b,y,x);
36     y-=a/b*x;
37     return d;
38 }
39 LL CRT(int nn)
40 {
41     LL sum=0,tot=1,tott,x,y;
42     int i;
43     for(i=1;i<=nn;i++)tot*=p[i];
44     for(i=1;i<=nn;i++)
45     {
46         tott=tot/p[i];
47         exgcd(tott,p[i],x,y);
48         x=(x%p[i]+p[i])%p[i];
49         sum=((sum+a[i]*tott%tot*x)%tot+tot)%tot;
50     }
51     return sum;
52 }
53 int main()
54 {
55     #ifndef ONLINE_JUDGE
56 //    freopen("1.txt","r",stdin);
57 //    freopen("2.txt","w",stdout);
58     #endif
59     int i,j,k,ii;
60 //    for(scanf("%d",&cas);cas;cas--)
61 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
62 //    while(~scanf("%s",s))
63     p[1]=23;p[2]=28;p[3]=33;
64     while(~scanf("%d",&n))
65     {
66         ans=0;
67         a[1]=n;
68         scanf("%d%d%d",&a[2],&a[3],&lll);
69         if(a[1]+a[2]+a[3]+lll==-4)break;
70         ans=CRT(3);
71         ans=(ans-lll+p[1]*p[2]*p[3]-1)%(p[1]*p[2]*p[3])+1;
72         printf("Case %d: the next triple peak occurs in %lld days.\n",++cass,ans);
73     }
74     return 0;
75 }
76 /*
77 //
78
79 //
80 */

千万不要点

时间: 2024-10-11 17:23:39

【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms的相关文章

中国剩余定理 POJ 1006 Biorhythms

题目传送门 题意:POJ有中文题面 分析:其实就是求一次同余方程组:(n+d)=p(%23), (n+d)=e(%28), (n+d)=i(%33),套用中国剩余定理模板 代码: /************************************************ * Author :Running_Time * Created Time :2015/9/15 星期二 16:53:01 * File Name :POJ_1006.cpp **********************

中国剩余定理小结 (互质,非互质) (poj 1006,hdu 3579)

先证明下中国剩余定理 条件: x%m_1=a_1 x%m_2=a_2 ... x%m_n=a_n m_1,m_2,...,m_n两两互质 证明: 设M=m_1*m_2*m_3*...*m_n M_i=M/m_i 因为gcd(M_i,m_i)=1,所以M_ix+m_iy=1 (t_i*M_i)%m_i=1 //由Ext_gcd(M_i,m_i,x,y)求出,t_i=x 方程组的解:x=a_1*t_1*M_1+...+a_n*t_n*M_n 题目:poj 1006 http://poj.org/pr

POJ - 1006 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, respec

poj 1006:Biorhythms(水题,经典题,中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 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,

PKU POJ 1006 Biorhythms (中国剩余定理)

中国剩余定理 x = ai (mod mi)  ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk     其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st   Mi*xi + mi*yi = 1 令ei = Mi*xi ,则有: 则e0a0 + e1a1 + e2a2+ - +en-1*an-1是方程一个解 因为n%3=2,n%5=3,n%7=2且3,5,7互质       使5×7被3除余1,用35×2=70:        使3×7被5除余1,用21×1

POJ 1006 Biorhythms (数论-中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 111285   Accepted: 34638 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,

poj 1006 Biorhythms (中国剩余定理学习)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 113517   Accepted: 35597 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,

Biorhythms HDU - 1370 (中国剩余定理)

题目链接:https://cn.vjudge.net/problem/HDU-1370 题目大意:给你三个式子,让你求出满足题目条件的解. (n-d)%23=p,(n-d)%28=e,(n-d)%33=i.给你d,p,e,i,让你求出n的值,其实把(n-d)看成一个整体,求出来之后再减去d就能求出n了. 中国剩余定理模板也存一下. m数组是被除数,下标从0开始 m[0]=23,m[1]=28,m[2]=33. b数组是等号右边的数,下标从0开始,b[0]=p,b[1]=e,b[2]=i. 然后求

HDU 1370(中国剩余定理)

原文章地址:http://m.blog.csdn.net/article/details?id=18414733 Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如, 智力周期的高峰,人会思维敏捷,精力容易高度集中.因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天.对于每个人,我们想知道何时三个高峰落在同一天. 对于每个周期,我们会给出从当前年份的第一