在POJ上有译文(原文右上角),选择语言:简体中文

求解同余方程组:
x=ai(mod mi) i=1~r, m1,m2,...,mr互质
利用中国剩余定理
令M=m1*m2*...*mr,Mi=M/mi
因为mi两两互质,所以(Mi,mi)=1
令Mi*yi=1(mod mi)的解为yi,即Mi模mi的逆元
则方程的解为:
(a1*M1*y1+a2*M2*y2+...+ar*Mr*yr)%M

方法一:用扩展欧几里德求逆元

#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
const int M=;
int a[],m[]={,,};
int sp,se,si,d;
int exgcd(int a,int b,int &x,int &y){
if(b==){
x=;
y=;
return a;
}
int d=exgcd(b,a%b,x,y);
int tmp=x;
x=y;
y=tmp-a/b*y;
return d;
}
int main()
{
int cases=,ans;
int ni[],Mi[],x,y;
//因为mi是固定的,所以可以先将对应的逆元用扩展欧几里德求出来
for(int i=;i<;i++){
Mi[i]=M/m[i];
exgcd(Mi[i],m[i],x,y);
ni[i]=x;
}
while(scanf("%d%d%d%d",&sp,&se,&si,&d)!=EOF){
if(sp==-)
break;
a[]=sp;a[]=se;a[]=si;
ans=;
for(int i=;i<;i++){
ans=(ans+(a[i]*Mi[i]%M)*ni[i]%M)%M;
ans=(ans+M)%M;
}
if(ans<=d){
ans=ans+M-d;
}
else
ans=ans-d;
printf("Case %d: the next triple peak occurs in %d days.\n",++cases,ans); }
return ;
}

方法二:枚举求Mi*yi

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; const int M=;
int m[]= {,,};
int a[];
int sp,se,si,d;
int Mii[],Mi[];
int value; //求最大公约数,最小公倍数则两数相乘除以它们的最大公约数
int gcd(int a,int b) {
if(b==)
return a;
else
return gcd(b,a%b);
}
int main() {
int cases=;
Mii[]=Mi[]=m[]*m[];
Mii[]=Mi[]=m[]*m[];
Mii[]=Mi[]=m[]*m[];
//枚举求Mi*yi
for(; Mii[]%m[]!=; Mii[]+=Mi[]) {
}
for(; Mii[]%m[]!=; Mii[]+=Mi[]) {
}
for(; Mii[]%m[]!=; Mii[]+=Mi[]) {
}
while(scanf("%d%d%d%d",&sp,&se,&si,&d)!=EOF) {
if(sp==-)
break;
a[]=sp;
a[]=se;
a[]=si;
value=((Mii[]*a[]+Mii[]*a[]+Mii[]*a[])%M+M)%M;
int ans;
if(value<=d) {
ans=value+M-d;
} else
ans=value-d;
printf("Case %d: the next triple peak occurs in %d days.\n",++cases,ans); }
return ;
}

POJ 1006 Biorhythms (中国剩余定理)的更多相关文章

  1. POJ 1006 - Biorhythms (中国剩余定理)

    B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  2. POJ 1006 Biorhythms(中国剩余定理)

    题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...

  3. POJ 1006 Biorhythms --中国剩余定理(互质的)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103539   Accepted: 32012 Des ...

  4. PKU POJ 1006 Biorhythms (中国剩余定理)

    中国剩余定理 x = ai (mod mi)  ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk     其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st   M ...

  5. POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)

    POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...

  6. Biorhythms(中国剩余定理)

    http://shuxueshi.jie.blog.163.com/blog/static/13611628820104179856631/ 这篇博客写的很棒! #include<stdio.h ...

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

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

  8. poj 1006:Biorhythms(水题,经典题,中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Des ...

  9. 中国剩余定理 POJ 1006 Biorhythms

    题目传送门 题意:POJ有中文题面 分析:其实就是求一次同余方程组:(n+d)=p(%23), (n+d)=e(%28), (n+d)=i(%33),套用中国剩余定理模板 代码: /********* ...

随机推荐

  1. 包装类(Wrapper Class)

    1)包装类.针对于原生数据类型的包装.所有的包装类(8个)对位于java.lang包下.java中的8个包装类分别是:Byte,Short,Integer,Long,Float.Double,Char ...

  2. CSV 文件读取类

    class CsvReader { private $csv_file; private $spl_object = null; private $error; public function __c ...

  3. Android--调用系统的DownLoadManager去下载文件

    代码里面有详细的注释: /** * 该方法是调用了系统的下载管理器 */ public void downLoadApk(Context context,String url){ /** * 在这里返 ...

  4. hdu 3288 Resource Allocation

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3288 Resource Allocation Description HDU-Sailormoon i ...

  5. meteor 实现 微信分享

    Template.hello.events({ 'click button': function () { // increment the counter when button is clicke ...

  6. CentOs环境下PHP支持PDO_MYSQL

    一.下载相应tgz包: http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz二.安装开始 解压# tar zxvf PDO_MYSQL-1.0.2.tgz # cd ...

  7. MongoDB学习笔记-创建、更新、删除文档

    创建     MongoDB中使用insert方法来向集合插入文档,然后保存到MongoDB中.     db.foo.insert({"hehe":"呵呵"} ...

  8. SVN基于一个branch创建新branch

    在本地现有Branch(Checkout出来的目录)上,右键SVN,选择Branch/Tags,选择目录.比如https://sadcsc01.pmmr.com/web/Jaguar/branches ...

  9. Qt 读取txt文件乱码的解决办法

    Qt 读取txt文本乱码问题 2015-05-20 15:46 方法一:使用QString的fromLocal8Bit()函数 复制代码 QFile txtfile(filePath);        ...

  10. snmp数据包分析

    今天看了一下snmp数据包的报文格式,用wireshark抓了两个数据包来分析. 先说说snmp get-request的书报包格式吧,get-next-request,get-response,se ...