POJ 1061 + POJ 1006

POJ 1061 青蛙的约会 http://poj.org/problem?id=1061
Description

两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。
Input

输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。
Output

输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"
Sample Input

1 2 3 4 5
Sample Output

4

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<ctime>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 100100;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0){
        x = 1;
        y = 0;
        return a;
    }
    ll g = exgcd(b,a%b,y,x);
    y -= a/b * x;
    return g;
}
int main(void)
{
    ll x,y,m,n,l;
    cin>>x>>y>>m>>n>>l;
    ll a = m-n;
    ll b = l;
    ll c = y-x;
    if(a<0){
        a = -a,c = -c;
    }
    ll t,q;
    ll g = exgcd(a,b,t,q);
    if(c%g!=0){
        printf("Impossible\n");
    }
    else{
        t = c*t/g;
        if(t>=0)    t = t%(b/g);
        else    t = t%(b/g)+(b/g);

        printf("%lld\n",t);
    }
    return 0;
}

POJ 1006 http://poj.org/problem?id=1006
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.

题意

输入p,q,i,d,(多组输入,当p=q=i=d=-1时,结束)求解x,满足 { x ≡ p 1 ( m o d   23 ) x ≡ e 1 ( m o d   28 ) x ≡ i 1 ( m o d   33 ) \left\{ \begin{aligned} x ≡ p_1 (mod\ 23)\\ x ≡ e_1 (mod\ 28)\\ x ≡ i_1 (mod\ 33) \end{aligned} \right. xp1(mod 23)xe1(mod 28)xi1(mod 33)其中p1 = p%23,q1=q%28,i1=i%33,输出 x-d,如果x-d<=0,输出x-d + 23 * 28 * 33。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<ctime>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 100100;
int p[maxn],q[maxn];
int exgcd(int a,int b,int &x,int &y){
    if(b==0){
        x = 1;
        y = 0;
        return a;
    }
    int g = exgcd(b,a%b,y,x);
    y -= a/b * x;
    return g;
}
int CRT()
{
    int x,y,N = 1;
    int ans = 0;
    for(int i=1;i<=3;i++)   N *= q[i];
    for(int i=1;i<=3;i++){
        int tmp = N / q[i];
        int g = exgcd(tmp,q[i],x,y);
        int tp = q[i]/g;
        x = (x%tp + tp)%tp;
        ans = (ans + p[i]*tmp*x)%N;
    }
    return ans%N;
}

int main(void)
{
    int d;
    int ca = 1;
    q[1] = 23;
    q[2] = 28;
    q[3] = 33;
    while(1){
        int cnt = 0;
        for(int i=1;i<=3;i++){
            scanf("%d",&p[i]);
            if(p[i]==-1)    cnt++;
            p[i] = (p[i]%q[i]+q[i])%q[i];
        }
        scanf("%d",&d);
        if(d==-1)   cnt++;
        if(cnt==4)  break;
        int dd = CRT()-d;
        if(dd<=0)    dd +=  23*28*33;
        printf("Case %d: the next triple peak occurs in %d days.\n",ca++,dd);

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

逃夭丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值