poj 1980 Unit Fraction Partition

Description

Afraction whose numerator is 1 and whose denominator is a positiveinteger is called a unit fraction. A representation of a positiverational number p/q as the sum of finitely many unit fractions iscalled a partition of p/q into unit fractions. For example, 1/2 +1/6 is a partition of 2/3 into unit fractions. The difference inthe order of addition is disregarded. For example, we do notdistinguish 1/6 + 1/2 from 1/2 +1/6. 

For given fourpositive integers p, q, a, and n, count the number of partitions ofp/q into unit fractions satisfying the following twoconditions. 

The partition is thesum of at most n many unitfractions. 
The product of thedenominators of the unit fractions in the partition is less than orequal to a. 
For example, if(p,q,a,n) = (2,3,120,3), you should report 4since 
poj <wbr>1980 <wbr>Unit <wbr>Fraction <wbr>Partition(搜索求划分成单元分数的方案数)

enumerates all of thevalid partitions. 

Input

Theinput is a sequence of at most 200 data sets followed by aterminator. 

A data set is a linecontaining four positive integers p, q, a, and n satisfying p,q<= 800, a <= 12000 and n<= 7. The integers are separated by aspace. 

The terminator iscomposed of just one line which contains four zeros separated by aspace. It is not a part of the input data but a mark for the end ofthe input. 

Output

Theoutput should be composed of lines each of which contains a singleinteger. No other characters should appear in theoutput. 

The output integercorresponding to a data set p, q, a, n should be the number of allpartitions of p/q into at most n many unit fractions such that theproduct of the denominators of the unit fractions is less than orequal to a. 

SampleInput

2 3 120 3
2 3 300 3
2 3 299 3
2 3 12 3
2 3 12000 7
54 795 12000 7
2 3 300 1
2 1 200 5
2 4 54 2
0 0 0 0

SampleOutput

4
7
6
2
42
1
0
9
3

Source

 
解析:
   题目大意:给定一个分数,把它划分为若干个单元分数,n为最大的个数,且单元分数的分母之积不能超过a.
    搜索即可。
   
代码:
   

#include 

using namespace std;

int tot;

int p,q,a,n;

 

int cmp(int p1,int q1,int p2,int q2){

       return p1*q2-q1*p2;

}

 

void sub(int &p1,int &q1,int p2,int q2){

     p1 p1*q2 p2*q1;

     q1 q1*q2;

}

void dfs(int left_m,int left_d,int prev,int left,int layer){

       int next_m next_d;

       if(layer == n+1 && left_m!=0) return;

       if(layer <= n+1 && left_m == 0) {

             tot++;

         return;

       }

 

       for(int prev;d <= left;d++){

           if(cmp(left_m,left_d,1,d) 0) continue;

               sub(next_m left_m next_d left_d,1,d);

 

           if(cmp(next_m,next_d, layer d) 0) break;

               dfs(next_m next_d  left/d layer+1);

       }

}

int main(){

 

  while(scanf("%d%d%d%d",&p,&q,&a,&n),p!=0){

        tot 0;

        dfs(p,q,1,a,1);   

        printf("%d\n",tot);

  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值