码迷,mamicode.com
首页 > 其他好文 > 详细

中国剩余定理 POJ 1006 Biorhythms

时间:2015-09-16 14:05:17      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

题意:POJ有中文题面

分析:其实就是求一次同余方程组:(n+d)=p(%23), (n+d)=e(%28), (n+d)=i(%33),套用中国剩余定理模板

 

代码:

/************************************************
* Author        :Running_Time
* Created Time  :2015/9/15 星期二 16:53:01
* File Name     :POJ_1006.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
ll f[N];
int n;
 
ll multi_mod(ll a, ll b, ll p)    {     //a * b % p
    a = (a % p + p) % p;
    b = (b % p + p) % p;
    ll ret = 0;
    while (b)   {
        if (b & 1)  {
            ret += a;
            if (ret >= p)   ret -= p;
        }
        b >>= 1;
        a <<= 1;
        if (a >= p) a -= p;
    }
    return ret;
}

ll ex_GCD(ll a, ll b, ll &x, ll &y) {
    if (b == 0) {
        x = 1;  y = 0;  return a;
    }
    ll d = ex_GCD (b, a % b, y, x);
    y -= x * (a / b);
    return d;
}

ll China(int k, int *b, int *m) {
    ll M = 1, x, y, ret = 0;
    for (int i=1; i<=k; ++i)    M *= m[i];
    for (int i=1; i<=k; ++i)    {
        ll w = M / m[i];
        ex_GCD (w, m[i], x, y);
        ret += b[i] * (x * w % M) % M;
    }
    return (ret - n + M) % M;
}

int main(void)    {
    int b[4], m[4];
    m[1] = 23;  m[2] = 28;  m[3] = 33;
    int cas = 0;
    while (scanf ("%d%d%d%d", &b[1], &b[2], &b[3], &n) == 4)  {
        if (b[1] == -1 && b[2] == -1 && b[3] == -1 && n == -1) break;
        ll ans = China (3, b, m);
        if (ans == 0)   ans = 21252;
        printf ("Case %d: the next triple peak occurs in %I64d days.\n", ++cas, ans);
    }

    return 0;
}

  

中国剩余定理 POJ 1006 Biorhythms

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4813040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!