Biorhythms(求模线性方程组--中国剩余定理的完美诠释)

181 篇文章 0 订阅
79 篇文章 0 订阅

Link:http://poj.org/problem?id=1006


Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 121769 Accepted: 38381

Description

Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier. 
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak. 

Input

You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.

Output

For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form: 

Case 1: the next triple peak occurs in 1234 days. 

Use the plural form ``days'' even if the answer is 1.

Sample Input

0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

Sample Output

Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

Source



题意翻译:人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天的时间(距给定时间的天数)。例如:给定时间为10,下次出现三个高峰同天的时间是12,则输出2(注意这里不是3)。


编程思想:周期性问题充分利用%运算,推出模线性方程组,然后利用中国剩余定理模板求解问题或者直接自己动手模拟中国剩余定理的求解过程,然后直接输出答案。

抽象出题意:

求最小n满足

(n+d)%23=p  

(n+d)%28=e

(n+d)%33=i



AC code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#define PI acos(-1.0)
#define LINF 1000000000000000000LL
#define eps 1e-8
#define LL long long
#define MAXN 100010 
#define MOD 1000000007
using namespace std;
const int INF=0x3f3f3f3f;
LL Mod;  
LL gcd(LL a, LL b)  
{  
    if(b==0)  
        return a;  
    return gcd(b,a%b);  
}  
LL exgcd(LL A,LL &x,LL B,LL &y)
{
    LL x1,y1,x0,y0;
    x0=1;y0=0;
    x1=0;y1=1;
    LL r=(A%B+B)%B;
    LL q=(A-r)/B;
    x=0;y=1;
    while(r)
    {
        x=x0-q*x1;
        y=y0-q*y1;
        x0=x1;
        y0=y1;
        x1=x;y1=y;
        A=B;B=r;r=A%B;
        q=(A-r)/B;
    }
    return B;
}
LL ABS(LL x)
{
	return x>=0?x:-x;
}
/*接下来介绍乘法逆元:
若m≥1,gcd(a,m)=1,则存在c使得 ca≡1(mod m) 我们把c称为是a对模m的逆,记为 a-1(mod m)或a-1 可以用扩展欧几里德算法求a-1
应用: 求(a/b)%c时,若a为大整数时可以写成 ((a%c)*((b-1)%c))%c,注意:这里(b-1)是b%c的乘法逆元,不是b减 1!!! 
还可以使用扩展欧几里德算法求乘法逆元:*/
//a在模n乘法下的逆元,没有则返回-1(a * b % n == 1,已知a,n,求b就是求a模n的乘法逆元)    
LL inv(LL a, LL n)  
{  
    LL x,y;  
    //int64 t = Extend_Euclid(a,n,x,y); 
	/*******************************************
	扩展欧几里得算法求乘法逆元
	调用:exgcd(a,x,b,y);
	则:  x为a模b的乘法逆元,y为b模a的乘法逆元
	********************************************/ 
    LL t = exgcd(a,x,n,y);  
    if(t != 1)  
        return -1;  
    return (x%n+n)%n;  
}  
  
//将两个方程合并为一个  
bool merge(LL a1, LL n1, LL a2, LL n2, LL& a3, LL& n3)  
{  
    LL d = gcd(n1,n2);  
    LL c = a2-a1;  
    if(c%d)  
        return false;  
    c = (c%n2+n2)%n2;  
    c /= d;  
    n1 /= d;  
    n2 /= d;  
    c *= inv(n1,n2);  
    c %= n2;  
    c *= n1*d;  
    c += a1;  
    n3 = n1*n2*d;  
    a3 = (c%n3+n3)%n3;  
    return true;  
}  
  
//求模线性方程组x=ai(mod ni),ni可以不互质  
LL China_Reminder2(LL len, LL* a, LL* n)//len为模方程的式子总数,a[i]、n[i]分别表示第i个式子的余数、除数  
{  
    LL a1=a[0],n1=n[0];  
    LL a2,n2;  
    for(int i = 1; i < len; i++)  
    {  
        LL aa,nn;  
        a2 = a[i],n2=n[i];  
        if(!merge(a1,n1,a2,n2,aa,nn))  
            return -1;//无解输出-1  
        a1 = aa;  
        n1 = nn;  
    }  
    Mod = n1;  
    return (a1%n1+n1)%n1;  
}  
LL a[1000],b[1000];  
int main()  
{  
    int i;  
    LL date; 
    b[0]=23;
	b[1]=28;
	b[2]=33; 
	int cas=0;
    while(~scanf("%I64d%I64d%I64d%I64d",&a[0],&a[1],&a[2],&date))  
    {  
    	if(a[0]==-1&&a[1]==-1&&a[2]==-1&&date==-1) break;
    	cas++;
    	LL ans=China_Reminder2(3,a,b);
		ans=(ans-date)%21252;
		if(ans<=0)
			ans+=21252;
		printf("Case %d: the next triple peak occurs in %I64d days.\n",cas,ans);
    }  
    return 0;  
}
/*
抽象出题意:

求最小n满足

(n+d)%23=p  

(n+d)%28=e

(n+d)%33=i
*/

为了更好地理解和体会中国剩余定理的思想,下面附上别人写的另一种AC方式:

直接模拟算出答案过程及实现代码(下面内容来自博客:http://blog.csdn.net/shanshanpt/article/details/8724769):

poj 1006 题的思路不是很难的,可以转化数学式:

现设 num 是下一个相同日子距离开始的天数

         p,e,i,d 如题中所设!

那么就可以得到三个式子:( num + d ) % 23 == p; ( num + d ) % 28 == e; ( num + d ) % 33 == i;

p,e,i,d 是我们输入的,那么我们需要求出num即可,为了方便,我们将num+d暂时作为一个整体!令x = num + d;

即:x % 23 == p; x % 28 == e; x % 33 == i;求x

怎么办?这就涉及到所谓的 “ 中国剩余定理 ”( 概念自己google,很easy )

《孙子算经》中有“物不知数”问题:“今有物不知其数,三三数之余二 ,五五数之余三 ,七七数之余二,问物几何?”答为“23”。

 --------这个就是传说中的“中国剩余定理”。 其实题目的意思就是,n % 3 = 2, n % 5 = 3, n % 7 = 2; 问n是多少?

那么他是怎么解决的呢?

看下面:

题目中涉及 3, 5,7三个互质的数、

令:5 * 7 * a % 3 = 1;  --------------> a = 2; 即5 * 7 * 2 = 70;

        3 * 7 * b % 5 = 1;  --------------> b = 1; 即3 * 7 * 1 = 21;

        3 * 5 * c % 7 = 1;  --------------> c  = 1; 即3 * 5 * 1 = 15;

为什么要使余数为1:是为了要求余数2的话,只要乘以2就可以,要求余数为3的话,只要乘以3就可以!

( 因为题目想要n % 3 =2, n % 5 =3, n % 7 =2; )

那么:要使得n % 3 = 2,那么( 5 * 7 * 2 )*2  % 3 = 2;( 因为5 * 7 * 2 % 3 = 1 )

同理: 要使得n % 5 = 3,那么( 3 * 7 * 1 )*3  % 5 = 3;( 因为3 * 7 * 1 % 5 = 1 )

同理:要使得n % 7 = 2,那么( 3 * 5 * 1 )* 2  % 7 = 2;( 因为3 * 5 * 1 % 7 = 1 )

那么现在将( 5 * 7 * 2 )* 2和( 3 * 7 * 1 )* 3和( 3 * 5 * 1 )* 2相加会怎么样呢?我们知道

( 5 * 7 * 2 )* 2可以被5和7整除,但是%3等于2

( 3 * 7 * 1 )* 3可以被3和7整除,但是%5等于3

( 3 * 5 * 1 )* 2可以被3和5整除,但是%7等于2

那么即使相加后,%3, 5, 7的情况也还是一样的!

那么就得到一个我们暂时需要的数( 5 * 7 * 2 )* 2 +( 3 * 7 * 1 )* 3 +( 3 * 5 * 1 )* 2 = 233

但不是最小的!所有我们还要 233 % ( 3 * 5 * 7 ) == 23  得解!


/******************************************************************************************************************************************************/

// 以上就是算法解析,貌似讲的不是很清晰,哎,大家见谅咯~


现在看看此题:x % 23 == p; x % 28 == e; x % 33 == i;求x

按照以上算法: 

使 33 * 28 * a % 23 = 1,得a = 6; 33 * 28 * 6 = 5544; 

使23 * 33 * b % 28 = 1, 得b = 19;23 * 33 * 19 = 14421; 
使23 * 28 * c % 33 = 1, 得c = 2;  23 * 28 * 2 = 1288。 

那么x  =  5544 * p + 14421 * e + 1288 * i

那么x-d即相差的时间天数!

因为有范围限制,那么(x-d) %= 21252;且如果此时<=0,那么(x-d)  += 21252   ,以上都只是为了保证在范围内而已~


/*
  中国剩余定理:出自《孙子算经》
  
*/

#include<stdio.h>

#define MAX 21252

int main()
{
	int p, e, i, d, n, count = 0;
	
	while( scanf("%d%d%d%d", &p, &e, &i, &d) != EOF )
	{
                count++;
		if(p == -1 && e == -1 && i == -1 && d == -1)
		{
			break;
                }

		n = ( 5544 * p + 14421 * e + 1288 * i - d ) % MAX;
		
		if( n <= 0 )   // 范围限制 
		{
			n += 21252;
                }
        
		printf("Case %d: the next triple peak occurs in %d days.\n", count, n );
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值