【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
题目链接:
http://poj.org/problem?id=1006
http://acm.hdu.edu.cn/showproblem.php?pid=1370
题目大意:
(X+d)%23=a1,(X+d)%28=a2,(X+d)%33=a3,给定a1,a2,a3,d,求最小的X。
题目思路:
【中国剩余定理】
23,28,33互素,可以套中国剩余定理。
也可以直接手算逆元。
33×28×a模23的逆元为8,则33×28×8=5544;
23×33×b模28的逆元为19,则23×33×19=14421;
23×28×c模33的逆元为2, 则23×28×2=1288。
因此有(5544×p+14421×e+1288×i)%lcm(23,28,33)=n+d (lcm(23,28,33)= 21252)
所以n=(5544×p+14421×e+1288×i-d)%21252
本题所求的是最小整数解,避免n为负,因此最后结果 n=(n+21252)% 21252
so n=(5544*p+14421*e+1288*i-d+21252)%21252;
//
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 4
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll;
LL ans;
int p[N],a[N];
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(!b){x=,y=;return a;}
LL d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
LL CRT(int nn)
{
LL sum=,tot=,tott,x,y;
int i;
for(i=;i<=nn;i++)tot*=p[i];
for(i=;i<=nn;i++)
{
tott=tot/p[i];
exgcd(tott,p[i],x,y);
x=(x%p[i]+p[i])%p[i];
sum=((sum+a[i]*tott%tot*x)%tot+tot)%tot;
}
return sum;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k,ii;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
p[]=;p[]=;p[]=;
while(~scanf("%d",&n))
{
ans=;
a[]=n;
scanf("%d%d%d",&a[],&a[],&lll);
if(a[]+a[]+a[]+lll==-)break;
ans=CRT();
ans=(ans-lll+p[]*p[]*p[]-)%(p[]*p[]*p[])+;
printf("Case %d: the next triple peak occurs in %lld days.\n",++cass,ans);
}
return ;
}
/*
// //
*/
千万不要点
【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms的更多相关文章
- 中国剩余定理 POJ 1006 Biorhythms
题目传送门 题意:POJ有中文题面 分析:其实就是求一次同余方程组:(n+d)=p(%23), (n+d)=e(%28), (n+d)=i(%33),套用中国剩余定理模板 代码: /********* ...
- 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)
0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...
- hdu 1370 Biorhythms
中国剩余定理……. 链接http://acm.hdu.edu.cn/showproblem.php?pid=1370 /**************************************** ...
- POJ 1006 - Biorhythms (中国剩余定理)
B - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...
- POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)
POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...
- POJ 1006 Biorhythms(中国剩余定理)
题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110991 Accepted: 34541 Des ...
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
随机推荐
- [Firebase] Deploy you website to Firebase
If you are looking for a host website, you can try Firebase, heroku or AWS... Today, I tried to depl ...
- Linux基础系列—Linux内核源码目录结构
/** ****************************************************************************** * @author 暴走的小 ...
- centos7 部署ssserver
centos7 部署shadowsocks服务端 为什么要选centos7? 以后centos7 肯定是主流,在不重要的环境还是尽量使用新系统吧 centos7 的坑 默认可能会有firewall 或 ...
- Android MediaCodec 使用例子
Android MediaCodec 使用例子 下面的例子是使用MediaCodec 录制到文件的例子. 1 public class AvcEncoder { private MediaCodec ...
- jQuery中在当前页面弹出一个新的界面
W.$.dialog({ content:'url:wswgrkbillController.do?snh&id='+b+'&bh='+c+'&ck='+d+'&sl= ...
- 一个类实现多个接口的demo
//A接口 interface A{ public int getA(); } //B接口 interface B{ public int getB(); } //实现了某个接口必须实现其全部的方法 ...
- DHCP服务器
DHCP指的是由服务器控制一段IP地址范围,客户机登录服务器时就可以自动获得服务器分配的IP地址和子网掩码.首先,DHCP服务器必须是一台安装有Windows 2000 Server/Advanced ...
- C#基础知识01(continue、break 和 return、ref 和 out)
break[跳出循环或者退出一个switch语句]由于它是用来退出循环或者switch语句的,所以只有当它出现在这些语句中时才是合法的. continue 语句和break语句相似,只是它不是退出一个 ...
- 【转】 UITableView 的indexPath
原文:http://blog.csdn.net/mengtnt/article/details/6733691 前面说过了viewController的一些基本注意事项.这里针对不同的viewCont ...
- firebug js版
1.有些时候如 ie6 7 8 你觉得F12 不好用的话 你可以直接 把这两个js 引用到html 里面 <script src="https://getfirebug.com/fi ...