中国剩余定理 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 ...
随机推荐
- js之substr和substring的差别
今天有人在群里问这两个的差别,借这个机会在这罗列下 substring(from,to) 開始和结束的位置,从零開始的索引 參数 描写叙述 from 必需. 一个非负的整数,规定要提取 ...
- Python中文问题研究
我曾经在深入浅出java中文问题系 列中研究过java的中文问题,现在中文问题已经不再羁绊我在java世界中漫游的脚步了.最近,对Python产生了浓厚的兴趣,谁知道跟中文问题这个 老朋友又一次不期而 ...
- Serializable 序列化 The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.
Java 序列化接口Serializable详解 - 火星猿类 - 博客园 http://www.cnblogs.com/tomtiantao/p/6866083.html 深入理解JAVA序列化 - ...
- An O(ND) Difference Algorithm and Its Variations (1986)
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 The problems of finding a longest com ...
- 20170225 ABAP获取字符串长度/字节长度
函数YGET_CHAR_LONG: FUNCTION YGET_CHAR_LONG. *"-------------------------------------------------- ...
- PR修改例子
DATA: lt_items_old LIKE TABLE OF bapiebanv WITH HEADER LINE. DATA: lt_items_new LIKE TABLE ...
- svn服务器搭建与迁移
2016-11-21更新: 今天被svn的钩子搞了半天,网上找解决方法都无效,下午被我试出来了,特此记录. 在svn的钩子中可以使用update来更新配置文件,比如ansible的,puppet的,具 ...
- MySQL性能优化-I/O相关配置参数
本文介绍InnoDB和MyISAM两种存储引擎的I/O相关参数配置. 1.InnoDB I/O相关配置 Innodb是一种事务型的存储引擎,为了减少提交事务时产生的io开销,innodb采用了写日志 ...
- 一步一步学Silverlight 2系列(9):使用控件模板
述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- Unbuntu 终端中使用Tab键不能自动补全
解决方案 1.利用vi编辑器打开 /etc/bash.bashrc文件(需要root权限) sudo vi /etc/bash.bashrc 2.找到文件中的下列代码 #enable bash com ...