2200: Fracions
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 3s | 8192K | 292 | 107 | Standard |
Input and Output
For each case there are tow positive intergers a and b( 0 < a < b <= 100), output the minimum sum.Sample Input
2 3 3 4 4 15
Sample Output
6 6 16Hint: 2/3 = 1/3 + 1/3, 4/15 = 1/6 + 1/10
This problem is used for contest: 29
#include<stdio.h>
int gcd(int x,int y) { if(y==0) return x;return gcd(y,x%y);}
int n,m,min;
void dfs(int id,int nown,int nowm,int nowsum)
{
// printf("%d...%d....%d....%d/n",id,nown,nowm,nowsum);
if(nowsum>min) return ;
if(id>min) return ;
if(nown*m>nowm*n) return ;
if(nown*m==nowm*n) {if(nowsum<min) min=nowsum;return ;}
dfs(id+1,nown*id+nowm,id*nowm,nowsum+id);
dfs(id,nown*id+nowm,id*nowm,nowsum+id);
dfs(id+1,nown,nowm,nowsum);
}
int main()
{
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m)==2)
{
int t=gcd(n,m);n/=t,m/=t;
min=n*m;
dfs(2,0,1,0);
printf("%d/n",min);
}
return 0;
}