您的位置:首页 > 其它

HDU 1370 Biorhythms 中国剩余定理

2017-04-20 19:42 357 查看

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=1370

题意:

人有体力,情感和智力三个生理周期,分别为23,28和33天。一个周期内有一天为峰值,对应的体力(情感或智力)达到巅峰。现在分别给出当前年内三个生理周期达到峰值的那一天,再给出一个d,问从第d天到下一个三个周期都达到峰值的日期还有多少天

思路:

中国剩余定理模板题啊。有同余方程组ai≡x(modmi),其中i∈[1,k],所有的mi互质,所有的mi乘积为m,那么有x≡∑ki=1aici(modn),ci=tm∗tm−1(modmi),tm=m/mi

#include <bits/stdc++.h>

using namespace std;

const int N = 110;
int cas = 0;

int extgcd(int a, int b, int &x, int &y)
{
int d = a;
if(b)
{
d = extgcd(b, a%b, y, x);
y -= (a/b) * x;
}
else x = 1, y = 0;
return d;
}
int crt(int A[], int M[], int n)
{
int m = 1;
for(int i = 1; i <= n; i++) m *= M[i];
int ans = 0, x, y;
for(int i = 1; i <= n; i++)
{
int tm = m / M[i];
extgcd(tm, M[i], x, y);
ans = (ans + A[i] * tm * x) % m;
}
if(ans < 0) ans += m;
return ans;
}
int main()
{
int t, a
, m
= {0, 23, 28, 33};
int s;
scanf("%d", &t);
while(t--)
{
while(true)
{
for(int i = 1; i <= 3; i++) scanf("%d", &a[i]);
scanf("%d", &s);
if(a[1] == -1 && a[2] == -1 && a[3] == -1 && s == -1) break;
int res = crt(a, m, 3);
if(res <= s) res += 21252;
printf("Case %d: the next triple peak occurs in %d days.\n", ++cas, res - s);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: