poj 1006:Biorhythms 中国剩余定理

传送门

这是一道基础的板子题 题意很简单 右上角可以切换成英文

分析:

因为23 = 23

28 = 2*2*7

33 = 3*11

满足两两互质关系,所以直接套模板就好了

#include<iostream>
#include<algorithm>
#include<cstdio>
#define ll long long
using namespace std;
ll ans;
ll ex_gcd(ll a,ll b,ll &x,ll &y)
{
	if(b==0)
	{
		x=1;
		y=0;
		return a;
	}
	int g=ex_gcd(b,a%b,y,x);
	y-=a/b*x;
	return g;
}
ll inv(ll a,ll b)
{
	ll x,y;
	int g=ex_gcd(a,b,x,y);
	int t=b/g;
	return (x%t+t)%t;
}
ll china(ll *a,ll *m)
{
	ll M=1,ret=0;
	for(int i=0;i<3;i++)
	{
		M*=m[i];
	}
	for(int i=0;i<3;i++)
	{
		ll w=M/m[i];
		ret=(ret+w*inv(w,m[i])*a[i])%M;
	}
	return (ret+M)%M;
}
int main()
{
	ll p[3]={23,28,33};
	int mod=21252;
 	ll r[3];
	int d;
	int cas=0;
	while(scanf("%lld %lld %lld %d",&r[0],&r[1],&r[2],&d))
	{
		ans=0;
		if(r[0]==r[1]&&r[1]==r[2]&&r[0]==-1)
		break;
	 	ans=((china(r,p)-d)%mod+mod)%mod;
	 	printf("Case %d: the next triple peak occurs in %lld days.\n",++cas,ans?ans:21252);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/henucm/article/details/89398783