中国剩余定理模板poj1006
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm> using namespace std;
typedef long long ll;
const int maxn = ;
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (b == )
{
x = ;
y = ;
return a;
}
ll r = exgcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - a / b * y;
return r;
}
ll china_remainder(int a[], int w[], int n)//w存放除数,a存放余数
{
ll M = , ans = , x, y;
for (int i = ; i < n; i++)
M *= w[i];
for (int i = ; i < n; i++)
{
ll m = M / w[i];
exgcd(m, w[i], x, y);
ans = (ans + x * m * a[i]) % M;
}
return (ans % M + M) % M;
}
int main()
{
int T, m, a[maxn], w[maxn];
w[] = ; w[] = ; w[] = ;
ll n = ;
int p, e, i, d, kase = ;
while (~scanf("%d %d %d %d", &p, &e, &i, &d))
{
if (p == - && e == - && i == - && d == -)
break;
a[] = p;
a[] = e;
a[] = i;
ll ans = china_remainder(a, w, n);
ans -= d;
int mod = w[] * w[] * w[];
ans = (ans % mod + mod) % mod;
if (ans == )
ans = mod;
printf("Case %d: the next triple peak occurs in %lld days.\n", ++kase, ans); }
return ;
}
中国剩余定理模板poj1006的更多相关文章
- 中国剩余定理模板 51nod 1079
题目链接:传送门 推荐博客:https://www.cnblogs.com/freinds/p/6388992.html (证明很好,代码有误). 1079 中国剩余定理 基准时间限制:1 秒 空间 ...
- Monkey Tradition---LightOj1319(中国剩余定理模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 题意:有 n 个猴子,n 棵树,树的高度为 L ,每个猴子刚开始的时候都在树的底 ...
- poj 1006中国剩余定理模板
中国剩余定理(CRT)的表述如下 设正整数两两互素,则同余方程组 有整数解.并且在模下的解是唯一的,解为 其中,而为模的逆元. 模板: int crt(int a[],int m[],int n) { ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- 【学习笔记-中国剩余定理】POJ1006 Biorhythms
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 139500 Accepted: 44772 Des ...
- 51nod 1079 中国剩余定理模板
中国剩余定理就是同余方程组除数为质数的特殊情况 我直接用同余方程组解了. 记得exgcd后x要更新 还有先更新b1再更新m1,顺序不能错!!(不然会影响到b1的更新) #include<cstd ...
- [洛谷P1495] 曹冲养猪 (中国剩余定理模板)
中国剩余定理(朴素的)用来解线性同余方程组: x≡a[1] (mod m[1]) x≡a[2] (mod m[2]) ...... x≡a[n] (mod m[n]) 定义ms=m[1]*m[2]*. ...
- 【数论】【中国剩余定理】poj1006 生理周期
CRT用于求解一元线性同余方程组(模数互质),实际上模数不互质我们也可以解决,在之前的某篇文章里提过.如下 http://www.cnblogs.com/autsky-jadek/p/6596010. ...
- 中国剩余定理模板&俄罗斯乘法
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){ if(!b){d=a;x=1LL;y=0LL;} else {ex_gcd(b,a%b,d, ...
随机推荐
- 手工构建ISO的基本步骤
1.完成rpm包的构建 登录测试机,ssh 10.xx.xx.xxx cd /home/svn/desktop/trunk/ svn update ...
- java单点登录系统CAS的简单使用
转:http://blog.csdn.net/yunye114105/article/details/7997041 背景 有几个相对独立的java的web应用系统, 各自有自己的登陆验证功能,用户在 ...
- spring mvc ModelAndView 404的原因
在使用ModelAndView时不要导入 import org.springframework.web.portlet.ModelAndView; 而要导入以下这个包 import org.sprin ...
- 转:2014年最酷的30个JavaScript库
原文来自于:http://www.gbtags.com/gb/share/3701.htm 使用JavaScript库将会使开发变的更简单,大部分JavaScript库提供的功能都是极好的,当我们在为 ...
- ios入门之c语言篇——基本函数——4——数值交换函数
一个常用函数,被整理出来,免得每次 都要写 参数返回值解析: 参数: *a:int*,需要交换值的第一个变量: *b:int*,需要交换值的第二个变量: 返回值: (无) 函数解析: swap(&am ...
- Hibernate如何一个类映射两个表
一个User类有username,password属性,还有 otherInformation等其他属性,username和password映射到一个表,otherInformation等其他属性映射 ...
- NtQuerySystemInformation的使用(提供50余种信息)
今天,我们主要讨论的是一个函数NtQuerySystemInformation(ZwQuerySystemInformation).当然,你不要小看这么一个函数,它却为我们提供了丰富的系统信息,同时还 ...
- CSS3中text-overflow支持以...代替超出文本
CSS3中text-overflow支持以...代替超出文本. 1.div1:默认状态.超出文本默认显示在div外 2.div2:text-overflow:ellipsis; 使用text-over ...
- Nodejs in Visual Studio Code 12.构建单页应用Scrat实践
1.开始 随着前端工程化深入研究,前端工程师现在碉堡了,甚至搞了个自己的前端网站http://div.io/需要邀请码才能注册,不过里面的技术确实牛.距离顶级的前端架构,目前博主应该是far away ...
- [Locked] Factor combinations
Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...