poj-1061 luogu-1516 青蛙的约会

题目链接:http://poj.org/problem?id=1061  https://www.luogu.org/problemnew/show/P1516

由题意得

记g=gcd(m-n,l)

 

以上内容转载自https://www.luogu.org/blog/aiyoupass/solution-p1516

代码:

#include<iostream>
#include<cmath>
#include<cstdio>

#define rd(x) x=read()
#define p(x) {cout<<x<<endl;return 0;}

using namespace std;

typedef long long ll;

ll p,q,m,n,l;

inline ll read()
{
    ll f=1,x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    return x*f;
}

ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
void exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1,y=0;
        return;
    }
    exgcd(b,a%b,y,x);
    y-=a/b*x;
}//拓展欧几里得

int main()
{
    ll x,y;
    rd(p),rd(q),rd(m),rd(n),rd(l);
    ll g=gcd(m-n,l);
    ll d=l/g;
    if(d<0)d=-d;
    exgcd(m-n,l,x,y);
    if((q-p)%g)p("Impossible");
    x=(x*((q-p)/g)%d+d)%d;p(x); 
}

说明一下:POJ的编译系统十分严格

1.不能用bits/stdc++.h

2.abs不支持long long(QAQ)

猜你喜欢

转载自www.cnblogs.com/Robin20050901/p/10137467.html