POJ 1006 同余方程组
以前的做法
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
LL ai[],ri[],M;
void Exgcd(LL a,LL b,LL& d,LL& x,LL& y)
{
if(b == ) { d = a, x = , y = ;}
else {
Exgcd(b,a%b,d,y,x); y -= x*(a / b);
}
}
LL gcd(LL a,LL b)
{
return b == ? a : gcd(b,a%b);
}
LL solve(int n)
{
LL a1,r1;
LL a, b, c;
LL x, y, d;
a1 = ai[], r1 = ri[];
for(int i = ; i <= n; ++i)
{
a = a1, b = ai[i], c = ri[i] - r1;
Exgcd(a,b,d,x,y);
if(c % d) return -;
LL t = b / d;
x = (x * (c / d)% t + t) % t;
r1 = a1 * x + r1;
a1 = a1 *(ai[i] / d);
}
return r1;
}
int main()
{
LL p,e,k,d;
int Kase = ;
while(cin >> p >> e >>k >>d)
{
ai[] = , ai[] = , ai[] = ;
ri[] = p , ri[] = e, ri[] = k;
if(p == - && e == - && k == - && d == -) break;
LL ans = solve();
M = ;
for(int i = ; i <= ; ++i) M = M * ai[i] / gcd(M,ai[i]);
if(ans >= M) ans %= M; // 求最小的值.
while(ans <= d) ans += M;
printf("Case %d: the next triple peak occurs in %lld days.\n",Kase++,ans-d);
}
}
中国剩余定理撸一发
不过中国剩余定理 ai[i] 需要互质
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
LL ai[],ri[],M;
void Exgcd(LL a,LL b,LL& d,LL& x,LL& y)
{
if(b == ) { d = a, x = , y = ;}
else {
Exgcd(b,a%b,d,y,x); y -= x*(a / b);
}
}
LL gcd(LL a,LL b)
{
return b == ? a : gcd(b,a%b);
}
LL China(int n)
{
M = ;
LL Mi,x,y,d,ans = ;
for(int i = ; i <= n; ++i) M *= ai[i];
for(int i = ; i <= n; ++i)
{
Mi = M / ai[i];
Exgcd(Mi,ai[i],d,x,y);
ans = (ans + Mi * x * ri[i]) % M;
}
if(ans < ) ans += M;
return ans ;
}
int main()
{
LL p,e,k,d;
int Kase = ;
while(cin >> p >> e >>k >>d)
{
ai[] = , ai[] = , ai[] = ;
ri[] = p , ri[] = e, ri[] = k;
if(p == - && e == - && k == - && d == -) break;
LL ans = China();
/*M = 1;
for(int i = 1; i <= 3; ++i) M = M * ai[i] / gcd(M,ai[i]);*/
//if(ans >= M) ans %= M; // 求最小的值.
while(ans <= d) ans += M;
printf("Case %d: the next triple peak occurs in %lld days.\n",Kase++,ans-d);
}
}
POJ 1006 同余方程组的更多相关文章
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
- 【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组
http://poj.org/problem?id=2891 题意:与中国剩余定理不同,p%ai=bi,此处的ai(i=1 2 3 ……)是不一定互质的,所以要用到的是同余方程组,在网上看到有人称为拓 ...
- POJ 1006 Biorhythms(中国剩余定理)
题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...
- poj 1006 中国剩余定理解同余方程
其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...
- POJ 1006 - Biorhythms (中国剩余定理)
B - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...
- 【poj2891】同余方程组
同余方程组 例题1:pku2891Strange Way to Express Integers 中国剩余定理求的同余方程组mod 的数是两两互素的.然而本题(一般情况,也包括两两互素的情况,所以中国 ...
- [POJ 1006]生理周期
Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰 ...
- POJ 1006 生理周期(中国剩余定理)
POJ 1006 生理周期 分析:中国剩余定理(注意结果要大于d即可) 代码: #include<iostream> #include<cstdio> using namesp ...
- POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)
POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...
随机推荐
- 01-Unity深入浅出(一)
一. 温故而知新 在开始学习Unity框架之前,有必要温习一下 [依赖倒置原则]和[手写IOC], 因为我们框架代码的构建都是基于[依赖倒置原则]的,而Unity框架的核心思想就是IOC和DI,所以有 ...
- [Android] Android 使用 Greendao 操作 db sqlite
Android 使用 Greendao 操作 db sqlite GreenDAO是一个开源的安卓ORM框架,能够使SQLite数据库的开发再次变得有趣.它减轻开发人员处理低级数据库需求,同时节省开发 ...
- Python高性能编程
一.进程池和线程池 1.串行 import time import requests url_lists = [ 'http://www.baidu.com', 'http://fanyi.baidu ...
- python模块之hashlib
摘要算法 1. 摘要算法又称为哈希算法.散列算法,是通过函数将任意长度的数据转化成固定长度的数据串(通常用16进制的字符串表示). 2. 摘要算法将通过摘要函数f()将数据转化成固定长度的摘要(dig ...
- protobuf使用简介
官网:https://github.com/google/protobuf 环境:windows,java 1. protobuf概述protobuf是Google开发一种数据描述格式,能够将结构化数 ...
- java的TCP和UDP编程
TCP 客户端: import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter ...
- hadoop3.x的安装
请看https://www.cnblogs.com/garfieldcgf/p/8119506.html
- bean的实例化方式
spring中bean的实例化方式有三种,1.构造器实例化,2.实例工厂实例化,3.静态工厂实例化 1.构造器实例化方式 public class bean1 { public bean1() { } ...
- String,StringBuffer,StringBulider
StringBuffer.StringBuilder.String中都实现了CharSequence接口. CharSequence是一个定义字符串操作的接口,它只包括length().charAt( ...
- Lattice
Lattice是一个无环WFSA,结点可以是hmm状态.hmm(音素).词,每个结点是一段音频在某个时间的对齐 用训练好的声学模型.现成的语言模型和发音字典构建解码网络(wfst),最后将提取的测试集 ...