bzoj2187 -- 类欧几里得算法

用类欧不断缩小规模,就能在O(T*log2n)时间内求出答案。

题解:http://blog.csdn.net/coldef/article/details/62035919

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 #define ll long long
 6 struct Node{
 7     ll x,y;
 8     Node(){}
 9     Node(ll x,ll y):x(x),y(y){}
10 }a,b,c;
11 ll g,i,j,k,n,m;
12 inline ll Gcd(ll x,ll y){return y==0?x:Gcd(y,x%y);}
13 inline ll F(ll x,ll y){return (x-1)/y+1;}
14 inline Node Solve(Node a,Node b){
15     g=Gcd(a.x,a.y);a.x/=g;a.y/=g;
16     g=Gcd(b.x,b.y);b.x/=g;b.y/=g;
17     if(a.x==0)return Node(1,b.y/b.x+1);
18     if(a.x/a.y+2<=F(b.x,b.y))return Node(a.x/a.y+1,1);
19     if(a.x/a.y>=1){
20         int x=a.x/a.y;
21         Node c=Solve(Node(a.x%a.y,a.y),Node(b.x-x*b.y,b.y));
22         return Node(c.x+x*c.y,c.y);
23     }
24     if(b.x>b.y)return Node(1,1);
25     Node c=Solve(Node(b.y,b.x),Node(a.y,a.x));
26     return Node(c.y,c.x);
27 }
28 int main()
29 {
30     while(scanf("%lld%lld%lld%lld",&a.x,&a.y,&b.x,&b.y)!=EOF){
31         g=Gcd(a.x,a.y);a.x/=g;a.y/=g;
32         g=Gcd(b.x,b.y);b.x/=g;b.y/=g;
33         c=Solve(a,b);
34         printf("%lld/%lld\n",c.x,c.y);
35     }
36     return 0;
37 }
bzoj2187

 

转载于:https://www.cnblogs.com/gjghfd/p/6552142.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值