POJ 1006 Biorhythms

题解:中国剩余定理

通解:ans=a[i]*M[i]*Inv(M[i],m[i])+k*M

注意window与linux下读入输出差异

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long Lint;

int T;
Lint b;
Lint m[4]={0,23,28,33};
Lint a[4];
Lint M;
Lint ans;

Lint Gcd(Lint a,Lint b){
	if(b==0)return a;
	return Gcd(b,a%b);
}
Lint Inv(Lint x,Lint p){
	int k=0;
	for(int i=1;i<=p;++i)if(Gcd(i,p)==1)++k;
	Lint ret=1;
	k--;
	for(;k;k>>=1,x=x*x%p){
		if(k&1)ret=ret*x%p;
	}
	return ret;
}

int main(){
	M=m[1]*m[2]*m[3];
	while(scanf("%lld%lld%lld%lld",&a[1],&a[2],&a[3],&b)){
		++T;
		if(a[1]==-1)return 0;
		ans=0;
		for(int i=1;i<=3;++i){
			ans=(ans+a[i]*(M/m[i])*Inv(M/m[i],m[i]))%M;
		}
		ans=(ans-b+M)%M;
		if(ans==0)ans=M;
		printf("Case %d: the next triple peak occurs in %lld days.\n",T,ans);
	}
}

  

posted @ 2018-02-25 19:05  ws_zzy  阅读(155)  评论(0编辑  收藏  举报