POJ 1061 青蛙的約會

來源:互聯網
上載者:User

標籤:poj   刷題   數學   

中文題啊中文題,每次看到都是很興奮的~


題目大意:自己看,全是中文~



解題思路:

對於題目可以列出:(x+m*s)-(y+n*s)=k*l;

   整理得:(n-m)*s+k*l=x-y

s,k為未知數。可以用擴充GCD來解。

可以解的情況是若且唯若未知數係數的最大公因數可以去整除(x-y)的時候。



下面是代碼:

#include <set>#include <map>#include <queue>#include <math.h>#include <vector>#include <string>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#define eps 1e-6#define pi acos(-1.0)#define inf 107374182#define inf64 1152921504606846976#define lc l,m,tr<<1#define rc m + 1,r,tr<<1|1#define iabs(x)  ((x) > 0 ? (x) : -(x))#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))#define clearall(A, X) memset(A, X, sizeof(A))#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))#define memcopyall(A, X) memcpy(A , X ,sizeof(X))#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )using namespace std;long long exgcd(long long m,long long n,long long &x,long long &y){    long long x1,y1,x0,y0;    x0=1; y0=0;    x1=0; y1=1;    x=0; y=1;    long long r=m%n;    long long q=(m-r)/n;    while(r)    {        x=x0-q*x1; y=y0-q*y1;        x0=x1; y0=y1;        x1=x; y1=y;        m=n; n=r; r=m%n;        q=(m-r)/n;    }    return n;}int main(){    long long a,b,c,x,y,m,n,l,x1,x2,mod;    while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l)!=EOF)    {        a=m-n;        b=l;        c=y-x;        if(a<0)        {            a=-a;            c=-c;        }        mod=exgcd(a,l,x1,x2);        l/=mod;        if(iabs(x-y)%mod)puts("Impossible");        else printf("%lld\n",((c/mod*x1)%l+l)%l);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.