设为首页 加入收藏

TOP

uva 756 - Biorhythms(中国剩余定理)
2015-07-24 05:45:20 来源: 作者: 【 】 浏览:4
Tags:uva 756 Biorhythms 中国 剩余 定理

题目链接:uva 756 - Biorhythms

题目大意:三个周期,23,28,33,输入为分别为在新一年中(三个周期均从0开始),出现周期中峰值的一天,以及当前的日子,问说需要经过多少天,才能使得三个峰值的在同一天。

解题思路:裸的中国剩余定理。

#include 
   
     #include 
    
      typedef long long ll; const int maxn = 5; const ll m[maxn] = {23,28,33}; ll M, t[maxn], D; bool init () { bool flag = false; for (int i = 0; i < 3; i++) { scanf("%lld", &t[i]); if (t[i] != -1) flag = true; } scanf("%lld", &D); if (D != -1) flag = true; return flag; } void gcd (ll a, ll b, ll& d, ll& x, ll& y) { if (b == 0) { d = a; x = 1; y = 0; } else { gcd(b, a%b, d, y, x); y -= (a/b) * x; } } ll china (ll* a, int n) { M = 1; for (int i = 0; i < n; i++) M *= m[i]; ll ans = 0; for (int i = 0; i < n; i++) { ll w = M/m[i], d, x, y; gcd(m[i], w, d, x, y); ans = (ans + y*w*a[i]) % M; } ans = (ans + M) % M; return ans; } int main () { int cas = 1; while (init()) { ll ans = china(t,3); ll t = ans; while (ans < D || ans == 0) ans += M; printf("Case %d: the next triple peak occurs in %lld days.\n", cas++, ans - D); } return 0; }
    
   
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇uva 10622 - Perfect P-th Powers.. 下一篇发现C++Builder 2010集合类的一个..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: