您的位置:首页 > 其它

【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms

2016-08-16 23:36 483 查看
题目链接:
  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
,a
;
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(!b){x=1,y=0;return a;}
LL d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
LL CRT(int nn)
{
LL sum=0,tot=1,tott,x,y;
int i;
for(i=1;i<=nn;i++)tot*=p[i];
for(i=1;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[1]=23;p[2]=28;p[3]=33;
while(~scanf("%d",&n))
{
ans=0;
a[1]=n;
scanf("%d%d%d",&a[2],&a[3],&lll);
if(a[1]+a[2]+a[3]+lll==-4)break;
ans=CRT(3);
ans=(ans-lll+p[1]*p[2]*p[3]-1)%(p[1]*p[2]*p[3])+1;
printf("Case %d: the next triple peak occurs in %lld days.\n",++cass,ans);
}
return 0;
}
/*
//

//
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: