中国剩余定理(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中国剩余定理模板的更多相关文章

  1. POJ 2891 中国剩余定理(不互素)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 17877 ...

  2. poj 1006 Biorhythms (中国剩余定理模板)

    http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...

  3. 中国剩余定理模板 51nod 1079

    题目链接:传送门 推荐博客:https://www.cnblogs.com/freinds/p/6388992.html (证明很好,代码有误). 1079 中国剩余定理  基准时间限制:1 秒 空间 ...

  4. Monkey Tradition---LightOj1319(中国剩余定理模板)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 题意:有 n 个猴子,n 棵树,树的高度为 L ,每个猴子刚开始的时候都在树的底 ...

  5. POJ 2891 中国剩余定理的非互质形式

    中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...

  6. 51nod 1079 中国剩余定理模板

    中国剩余定理就是同余方程组除数为质数的特殊情况 我直接用同余方程组解了. 记得exgcd后x要更新 还有先更新b1再更新m1,顺序不能错!!(不然会影响到b1的更新) #include<cstd ...

  7. [洛谷P1495] 曹冲养猪 (中国剩余定理模板)

    中国剩余定理(朴素的)用来解线性同余方程组: x≡a[1] (mod m[1]) x≡a[2] (mod m[2]) ...... x≡a[n] (mod m[n]) 定义ms=m[1]*m[2]*. ...

  8. 中国剩余定理模板&俄罗斯乘法

    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, ...

  9. poj 1006 中国剩余定理解同余方程

    其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...

随机推荐

  1. pwn学习日记Day10 《程序员自我修养》读书笔记

    第一章 从 Hello world 说起 抛出问题: 1.程序为什么要被编译器编译后才能执行? 2.编译器在把C语言程序转换成可以执行的机器码的过程中做了什么,怎么做的? 3.最后编译出来的可执行文件 ...

  2. linux内核中rtc框架选用什么接口来注册rtc设备呢?

    1. 有哪些接口? 1.1 devm_rtc_device_register 1.2  devm_rtc_allocate_device和 rtc_register_device 2. 1.1与1.2 ...

  3. Storm和Hadoop 区别

    Storm - 大数据Big Data实时处理架构   什么是Storm? Storm是:• 快速且可扩展伸缩• 容错• 确保消息能够被处理• 易于设置和操作• 开源的分布式实时计算系统- 最初由Na ...

  4. LeetCode_9. Palindrome Number

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  5. Sql 中常用日期转换Convert(Datetime) convert datetime

    Convert(data_type,expression[,style]) Convert(varchar(10),字段名,转换格式) 说明:此样式一般在时间类型(datetime,smalldate ...

  6. 人人都可以写的一个Python可视化小程序,带你走进编程的世界

    当年的PHP号称是最好的编程语言,今天的Python就是最简单的编程语言,一个小小的程序,寥寥几行代码,带你体验一下编程的乐趣. 最简单的编程语言 今天要介绍的小工具是Python环境安装好之后,自带 ...

  7. golang web框架设计6:上下文设计

    context,翻译为上下文,为什么要设计这个结构?就是把http的请求和响应,以及参数结合在一起,便于集中处理信息,以后框架的扩展等.好多框架比如gin,都是有这个上下文结构. context结构为 ...

  8. webdriervAPI(鼠标事件)

    from  selenium  import  webdriver from selenium.webdriver.common.action_chains import ActionChains 导 ...

  9. 关于antd form表单getFieldsError方法

    getFieldsError()方法其实只有required:true时,双向数据绑定. {getFieldDecorator('note', { rules: [{ required: true, ...

  10. [Python[Anaconda & PyTorch]] -- 使用conda 安装 Torch 出现错误 --Windows

    ... (⊙o⊙)… ... 当时具体的错误我没有截图, 用这个命令时 , conda 会报无法在源中找到PyTorch, 还是什么的错误 有很大的一个可能是, 安装的Anaconda 是32 位的, ...