2200: Fracions

2200: Fracions


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K292107Standard
A fracion a/b(a < b) can be expressed in the way 1/b1 + 1/b2 + ... + 1/bn. Now can you achieve it and make the sum of b1 to bn minimum?

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
16
Hint: 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值