【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
2016-08-16 23:36
483 查看
题目链接:
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;
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;
// //by coolxxx // #include<iostream> #include<algorithm> #include<string> #include<iomanip> #include<memory.h> #include<time.h> #include<stdio.h> #include<stdlib.h> #include<string.h> //#include<stdbool.h> #include<math.h> #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define abs(a) ((a)>0?(a):(-(a))) #define lowbit(a) (a&(-a)) #define sqr(a) ((a)*(a)) #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) #define eps (1e-8) #define J 10000000 #define MAX 0x7f7f7f7f #define PI 3.1415926535897 #define N 4 using namespace std; typedef long long LL; int cas,cass; int n,m,lll; LL ans; int p ,a ; LL exgcd(LL a,LL b,LL &x,LL &y) { if(!b){x=1,y=0;return a;} LL d=exgcd(b,a%b,y,x); y-=a/b*x; return d; } LL CRT(int nn) { LL sum=0,tot=1,tott,x,y; int i; for(i=1;i<=nn;i++)tot*=p[i]; for(i=1;i<=nn;i++) { tott=tot/p[i]; exgcd(tott,p[i],x,y); x=(x%p[i]+p[i])%p[i]; sum=((sum+a[i]*tott%tot*x)%tot+tot)%tot; } return sum; } int main() { #ifndef ONLINE_JUDGE // freopen("1.txt","r",stdin); // freopen("2.txt","w",stdout); #endif int i,j,k,ii; // for(scanf("%d",&cas);cas;cas--) // for(scanf("%d",&cas),cass=1;cass<=cas;cass++) // while(~scanf("%s",s)) p[1]=23;p[2]=28;p[3]=33; while(~scanf("%d",&n)) { ans=0; a[1]=n; scanf("%d%d%d",&a[2],&a[3],&lll); if(a[1]+a[2]+a[3]+lll==-4)break; ans=CRT(3); ans=(ans-lll+p[1]*p[2]*p[3]-1)%(p[1]*p[2]*p[3])+1; printf("Case %d: the next triple peak occurs in %lld days.\n",++cass,ans); } return 0; } /* // // */
相关文章推荐
- 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
- hdu 1370 || poj 1006 简单的中国剩余定理或者暴力
- 【中国剩余定理 && 扩展欧几里德】 POJ - 1006 Biorhythms
- poj 1006 Biorhythms 中国剩余定理
- POJ 1006 Biorhythms 中国剩余定理/扩展欧几里得
- POJ-1006 Biorhythms 中国剩余定理
- POJ 1006 Biorhythms 中国剩余定理
- poj 1006 Biorhythms 中国剩余定理
- poj 1006 Biorhythms 中国剩余定理
- 中国剩余定理小结 (互质,非互质) (poj 1006,hdu 3579)
- poj 1006 Biorhythms 中国剩余定理
- POJ 1006 && HDU 1370 Biorhythms(水~)
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
- poj 1006+hdu 1788(中国剩余定理求解同余方程组)
- poj 1006 Biorhythms 中国剩余定理
- poj1006 Biorhythms<中国剩余定理>
- 20140714 「初等数论 - 中国剩余定理」 POJ 1006 Biorhythms
- poj 1006 Biorhythms 中国剩余定理
- hdu 1370 Biorhythms 中国剩余定理的应用 and 枚举方法
- Biorhythms(poj1006中国剩余定理)