[BZOJ2712][Violet 2][类欧几里得算法]棒球

来源:互联网 发布:网络阅读远远超越了 编辑:程序博客网 时间:2024/04/27 16:47

跟BZOJ2187类似
把小数转化成分数,就行了。

#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>using namespace std;typedef long long ll;typedef pair<ll,ll> pairs;int n;ll r,x,a,b,c,d;ll gcd(ll a,ll b){    return b?gcd(b,a%b):a;}inline void simplify(ll &a,ll &b){    ll g=gcd(a,b); a/=g; b/=g;}pairs solve(ll p1,ll q1,ll p2,ll q2){    simplify(p1,q1); simplify(p2,q2);    ll a=p1/q1+1,b=(ll)ceil((double)p2/q2)-1;    if(a<=b) return pairs(a,1);    if(p1==0) return pairs(1,q2/p2+1);    if(p1<=q1&&p2<=q2){        pairs r=solve(q2,p2,q1,p1);        swap(r.first,r.second);        return r;    }    ll t=p1/q1;    pairs r=solve(p1%q1,q1,p2-t*q2,q2);    r.first+=t*r.second;    return r;}int main(){    while(~scanf("%d 0.%lld",&n,&r)){        if(r==0){ puts("1"); continue; }        x=1; for(int i=1;i<=n+1;i++) x*=10;        a=r*10-5; b=x; c=r*10+5; d=x;        simplify(a,b); simplify(c,d);        pairs Ans=solve(a,b,c,d);        printf("%lld\n",min(Ans.second,b));    }}
0 0
原创粉丝点击