poj 1006中国剩余定理模板
中国剩余定理(CRT)的表述如下
设正整数两两互素,则同余方程组
有整数解。并且在模下的解是唯一的,解为
其中,而
为
模
的逆元。
模板:
int crt(int a[],int m[],int n)
{
int e=;
int mod=;
for(int i=; i<=n; i++) mod*=m[i];
for(int i=; i<=n; i++)
{
int temp=mod/m[i];
e=(e+a[i]*temp*inv(temp,m[i]))%mod; // 这里求逆元 是对mi求,,
}
return e;
}
poj 1006ac代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int mod;
int exgcd(int a,int b,int &x,int &y)
{
if(b==)
{
x=;
y=;
return a;
}
int temp=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return temp;
}
int inv(int a,int b)
{
int x,y;
int temp=exgcd(a,b,x,y);
if(x<) x+=b/temp;
return x;
}
int crt(int a[],int m[],int n)
{
int e=;
int mod=;
for(int i=; i<=n; i++) mod*=m[i];
for(int i=; i<=n; i++)
{
int temp=mod/m[i];
e=(e+a[i]*temp*inv(temp,m[i]))%mod; // 这里求逆元 是对mi求,,
}
return e;
}
int main()
{
int a[],m[];
m[]=;
m[]=;
m[]=;
int s;
int Case=;
while(cin>>a[]>>a[]>>a[]>>s)
{
if(a[]==- && a[]==- && a[]==- && s==-) break;
int e=;
Case++;
e=crt(a,m,);
if(e <= s) e+=;
printf("Case %d: the next triple peak occurs in %d days.\n",Case,e-s);
}
return ;
}
poj 1006中国剩余定理模板的更多相关文章
- POJ 2891 中国剩余定理(不互素)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 17877 ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- 中国剩余定理模板 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 2891 中国剩余定理的非互质形式
中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...
- 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]*. ...
- 中国剩余定理模板&俄罗斯乘法
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, ...
- poj 1006 中国剩余定理解同余方程
其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...
随机推荐
- Hadoop环境搭建|第四篇:hive环境搭建
一.环境搭建 注意:hive版本不能太高,否则会出现兼容性问题 1.1.上传hive安装包 创建文件夹用于存放hive文件命令:mkdir hive 1.2.解压hive安装包 命令:tar -zxv ...
- An error occurred while starting a transaction on the provider connection. See the inner exception for details.
用EntityFramework循环操作数据时,报了如下错误 An error occurred while starting a transaction on the provider connec ...
- Change Assembly Version in a compiled .NET assembly
Change Assembly Version in a compiled .NET assembly You can use ILMerge: ILMerge.exe Foo.dll /ver:1. ...
- Ubuntu 18.04安装Samba服务器及配置
Ubuntu 18.04安装Samba服务器及配置 局域网下使用samba服务在Linux系统与Windows系统直接共享文件是一项很方便的操作.以Ubuntu为例配置samba服务,Linux服务器 ...
- kotlin之高阶函数
高阶函数是一种特殊的函数,它接受函数作为参数,或者返回一个函数 import java.awt.geom.Area fun main(arg: Array<String>) { val m ...
- Windows 7开发:UAC数据重定向 - Win32 Native
Windows 7开发:UAC数据重定向 - Win32 Native 目标 本动手实验中,您将会学习如何: • 故障排除一个文件重定向 问题 • 使用Process Monitor查找引起问题的根本 ...
- CISCN 2019 writeup
划水做了两个pwn和两个逆向...... 二进制题目备份 Re easyGO Go语言,输入有Please字样,ida搜索sequence of bytes搜please的hex值找到字符串变量,交叉 ...
- Build Telemetry for Distributed Services之OpenTracing指导:C#
官网链接:https://opentracing.io/guides/ 官方微博:https://medium.com/opentracing Welcome to the OpenTracing G ...
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_24-页面预览-页面预览测试
通过nginx转发到预览的地址 重启nginx 添加页面预览按钮 调整下列表的列的宽度
- 20190615 NACE关于采购订单的输出类型
项目已经做好的配置,我们复盘一下 一.使用NACE 进入输出控制条件: EF 采购订单,首先看->输出类型 标准是使用 nast 作为记录表 1输出类型, 2过程, 3 存取顺序,4 条件记录: ...