用類歐不斷縮小規模,就能在O(T*log2n)時間內求出答案。 題解:http://blog.csdn.net/coldef/article/details/62035919 代碼: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream ...
用類歐不斷縮小規模,就能在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