中国剩余定理 POJ 1006 Biorhythms
题意: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的更多相关文章
- POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)
POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...
- POJ 1006 - Biorhythms (中国剩余定理)
B - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...
- POJ 1006 Biorhythms(中国剩余定理)
题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110991 Accepted: 34541 Des ...
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
- POJ 1006 Biorhythms --中国剩余定理(互质的)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103539 Accepted: 32012 Des ...
- POJ 1006 Biorhythms (数论-中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 111285 Accepted: 34638 Des ...
- PKU POJ 1006 Biorhythms (中国剩余定理)
中国剩余定理 x = ai (mod mi) ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk 其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st M ...
随机推荐
- Creating a .bash_profile on your mac
A typical install of OS X won't create a .bash_profile for you. When you want to run functions from ...
- Spark 学习笔记:(三)Spark SQL
参考:https://spark.apache.org/docs/latest/sql-programming-guide.html#overview http://www.csdn.net/arti ...
- 成本函数计算方法J
J = 1/(2*m) * sum((X*theta - y).^2); OR
- min-width 和 @media screen
min-width可以容器设置最小宽度,低于改宽度时,会自动加上滚动条,支持ie7及ie7+: @media only screen and (min-width: /*最小宽度(要加单位px)*/) ...
- B.大钉骑马走江湖
江湖是什么,对于在象棋界厮杀的大钉来说,江湖就是一个矩阵,他的目标,就是在江湖之中骑着马,从他的位置出发,走到终点. 当然,大钉的马也遵从中国象棋中的“马走日”的规则,而且在矩阵中,也会有一些障碍物, ...
- [RK3399][Android7.1] 调试笔记 --- 模块编译32位动态库【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/78923784 Platform: RK3399 OS: Android 7.1 Board: ...
- 【CQ18阶梯赛第一场】题解
[A-风格不统一如何写程序] 输入字符串,得到长度,对于每个字符:如果是大写,则改为:‘_’+小写:如果是‘_’则忽略‘_’,并且把后面的小写改为大写. #include<cstdio> ...
- MongoDB之shard_副本集和分片部署
机器角色分配和拓扑环境如下: -------------------配置副本集s1-------------------------------1.创建目录在s1h1上创建如下目录[root@node ...
- 【hyddd驱动开发学习】DDK与WDK
最近尝试去了解WINDOWS下的驱动开发,现在总结一下最近看到的资料. 1.首先,先从基础的东西说起,开发WINDOWS下的驱动程序,需要一个专门的开发包,如:开发JAVA程序,我们可能需要一个JDK ...
- Laravel 5 微信小程序扩展
小程序官方的加解密 SDK 已经非常清楚了,只不过改成 Laravel 风格而已,仅仅相当于搬砖工.至于重复造轮子,我发现其他人的扩展解密用户信息的时候代码出错了,并且需要安装一个 Laravel 的 ...