POJ 1006 Biorhythms 中国剩余定理 数论

本文通过一个在线编程题目介绍了如何使用中国剩余定理解决特定的同余方程组问题。给出了详细的解题思路和完整的代码实现,并分享了学习过程中的思考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  题目链接: http://poj.org/problem?id=1006

  题目大意: 给你三个数p e i 让你求一个x 使得 x % 23 == p, x % 28 == e, x % 33 == i

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

  代码: 

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
typedef long long ll;
using namespace std;

//const int INF = 0x3fffffff;
ll gcd( ll a, ll b ) {
    return b == 0 ? a : gcd( b, a % b );
}

ll lcm( ll a, ll b ) {
    return a / gcd(a, b) * b;
}

int main() {
//    cout << lcm( lcm(23, 28), 33 ) << endl;
    ll p, e, i, d;
    int cases = 1;
    while( cin >> p >> e >> i >> d ) {
        if( p == -1 && e == -1 && i == -1 && d == -1 ) break;
        ll ans = 5544 * p + 14421 * e + 1288 * i - d;
        ans %= 21252;
        if( ans <= 0 ) ans += 21252;
        cout << "Case " << cases++ << ": the next triple peak occurs in " << ans << " days." << endl;
    }
    return 0;
}
View Code

  思考: 今天看了看推理过程, 觉得挺有意思的, 好好学数学

  另外感谢楼主对中国剩余定理的详细解释, 让我一个弱鸡看得明白: http://www.cnblogs.com/walker01/archive/2010/01/23/1654880.html

转载于:https://www.cnblogs.com/FriskyPuppy/p/7382860.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值