您的位置:首页 > 其它

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

2017-08-15 20:49 267 查看
题目描述:
点击打开链接

题意:有三个周期,分别是23,28,33天,现给出三个周期的峰值分别是第,p,e,i天,现在是第d天,问距离最近的三个周期同时达到峰值的日期还有多少天。

假设日期为x,要使三个周期同时达到峰值,那么x%23=p%23,x%28=e%28,x%33=i%33,求出x之后(x-d)就是结果,那么这个方程组显然就是用中国剩余定理去解决。关于中国剩余定理:点击打开链接,具体实现直接看代码吧。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
const int MOD=21252;

int p,e,i,d;

int extgcd(int a,int b,int &x,int &y)//扩展欧几里德。
{
int d=a;
if (b!=0)
{
d=extgcd(b,a%b,y,x);
y-=(a/b)*x;
}
else
{
x=1;
y=0;
}
return d;
}
int china(int m0[],int b[])
{
int x,y,n,m=1,a=0;
for (int j=0;j<3;j++) m=m*m0[j];
for (int j=0;j<3;j++)
{
n=m/m0[j];
extgcd(n,m0[j],x,y);
a=a+n*b[j]*x;
}
return a%m;
}
int main()
{
char c;
cin>>c;
int cas=1;
while(scanf("%d%d%d%d",&p,&e,&i,&d)!=EOF)
{
if (p==-1&&e==-1&&i==-1&&d==-1) break;
int m[3]={23,28,33};
int b[3];
b[0]=p,b[1]=e,b[2]=i;
int sum=china(m,b)-d;
if (sum<=0) sum+=MOD;
printf("Case %d: the next triple peak occurs in %d days.\n",cas,sum);
cas++;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: