poj-1061 luogu-1516 青蛙的約會
阿新 • • 發佈:2018-12-18
題目連結: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() #definep(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)