中国剩余定理(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. np.random.choices的使用

    在看莫烦python的RL源码时,他的DDPG记忆库Memory的实现是这样写的: class Memory(object): def __init__(self, capacity, dims): ...

  2. 通过sqlserver用户操作远程服务器

    USE masterGORECONFIGURE --先执行一次刷新,处理上次的配置GOEXEC sp_configure 'show advanced options',1 --启用xp_cmdshe ...

  3. mongoose 实现 增、删、改、查

    mongoose常用的API 增 save是一个实例方法,使用时需要先 new Model() 来实例化 //保存一个用户信息,userobj为你创建的文档对象模型里的字段,需正确对应传入 const ...

  4. IDEA中log4j.properties配置文件详解

    配置实例 ### 配置根 ### log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,D ...

  5. 013-java中的IO操作-InputStream/Reader、OutputStream/Writer

    一.概述 IO流用来处理设备之间的数据传输,上传文件和下载文件,Java对数据的操作是通过流的方式,Java用于操作流的对象都在IO包中. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称 ...

  6. linux服务端导入oracle数据库.sql脚本

    一般情况下,后缀名为.sql或者为记事本类型的文本脚本可以通过打开后复制或者直接在客户端打开执行,但如果脚本比较大时(比如文件达到几百M以上), 普通文本工具和数据库客户端都无法打开,哪怕可以打开,也 ...

  7. PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

    1030 Travel Plan (30 分)   A traveler's map gives the distances between cities along the highways, to ...

  8. iOS-在AFN基础上进行网络请求的封装

    网络请求的思路:如果请求成功的话AFN的responseObject就是解析好的. 1发送网络请求:get/post/或者别的 带上URL,需要传的参数 2判断后台网络状态码有没有请求成功: 3 请求 ...

  9. Dart Language samples

    Hello World Every app has a main() function. To display text on the console, you can use the top-lev ...

  10. Mybatis之自动生成

    使用Mybatis来自动生成我们的dao接口,mapper文件和实体类. 1.pom.xml依赖: <dependencies> <dependency> <groupI ...