POJ 1006 Biorhythms (中国剩余定理)
2014-09-27 21:28
316 查看
题目链接:POJ 1006 Biorhythms
题意:给定p,e,i,d,分别表示三种状态出现的时间,d表示从第d天开始。求多少天后三个状态同时出现。
x=p mod 23
x=e mod 28
x=i mod 33
中国剩余定理。
AC代码:
题意:给定p,e,i,d,分别表示三种状态出现的时间,d表示从第d天开始。求多少天后三个状态同时出现。
x=p mod 23
x=e mod 28
x=i mod 33
中国剩余定理。
AC代码:
#include<stdio.h> int exgcd(int a,int b,int &x,int &y) { if(b==0) { x=1; y=0; return a; } int ans=exgcd(b,a%b,x,y); int temp=x; x=y; y=temp-a/b*y; return ans; } // ans==ai mod mi .... int crt(int a[],int m[],int n) { int i,M=1; int ans=0; for(i=1;i<=n;i++) M*=m[i]; for(i=1;i<=n;i++) { int x,y; int Mi=M/m[i]; int gcd=exgcd(Mi,m[i],x,y); ans=(ans+Mi*x*a[i])%M; } if(ans<0) ans+=M; return ans; } int main() { int cas=1; int p,e,i,d; int a[10],m[10]; while(scanf("%d %d %d %d",&p,&e,&i,&d)!=EOF) { if(p==-1 && e==-1 && i==-1 && d==-1) break; m[1]=23; m[2]=28; m[3]=33; a[1]=p; a[2]=e; a[3]=i; int ans=crt(a,m,3); ans=ans-d; while(ans<=0) ans+=21252; printf("Case %d: the next triple peak occurs in %d days.\n",cas++,ans); } return 0; }
相关文章推荐
- POJ 1006 Biorhythms(中国剩余定理)
- poj 1006 Biorhythms (中国剩余定理)
- POJ 1006 Biorhythms(中国剩余定理)
- POJ 1006 Biorhythms(中国剩余定理)
- POJ1006 Biorhythms(中国剩余定理)
- poj1006 Biorhythms(中国剩余定理)
- POJ 题目1006 Biorhythms(中国剩余定理)
- POJ - 1006 Biorhythms (中国剩余定理)
- poj 1006 Biorhythms(中国剩余定理)
- poj 1006 Biorhythms(中国剩余定理)
- POJ 1006 Biorhythms (中国剩余定理)
- Biorhythms(poj 1006)(中国剩余定理)
- POJ 1006 - Biorhythms (中国剩余定理)
- POJ_1006_Biorhythms(中国剩余定理)
- 【POJ 1006】【CRT(中国剩余定理)模板题】Biorhythms
- POJ 1006-Biorhythms(中国剩余定理)
- POJ 1006-Biorhythms(中国剩余定理)
- POJ1006——Biorhythms(中国剩余定理)
- POJ 1006 Biorhythms(中国剩余定理)
- POJ 1006 Biorhythms(中国剩余定理)