HDU5974(数学题)

来源:互联网 发布:手机一键刷机软件 编辑:程序博客网 时间:2024/04/27 23:48

太天真了,以为104直接跑暴力了,结果T到哭泣,12W的数据量,只能用公式求。
这个题要推出来gcd(x,y)=gcd(a,b),然后转换成一元二次方程的韦达定理,接下来就是验证方程是否有整数解。
code:

#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<iostream>#include<string>#include <set>#include<time.h>//a&3==a%4using namespace std ;#define ll long long#define mem(a) memset(a,0,sizeof(a))const double eps = 1e-8;const int maxn = 110010;//须填写const int inf = 0x3f3f3f3f;ll gcd(ll a,ll b){    return a==0?b:gcd(b%a,a);}int main(){    ll a,b;    while(scanf("%lld%lld",&a,&b)!=EOF)    {        ll g=gcd(a,b);        a/=g;        b/=g;        if(a*a-4LL*b<0LL)        {            printf("No Solution\n");        }        else        {            ll x=(a+sqrt(a*a-4*b))/2;            ll y=a-x;            ll tmp;            if(x*y==b)            {                if(x>y)                {                    tmp=x;                    x=y;                    y=tmp;                }                printf("%lld %lld\n",x*g,y*g);            }            else            {                printf("No Solution\n");            }        }    }    return 0;}//寇瑟茹酒
原创粉丝点击