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

63 篇文章 0 订阅
28 篇文章 0 订阅
题目链接:

  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=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;
}
/*
//

//
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值