poj 1980 Unit Fraction Partition

来源:互联网 发布:au视频软件下载 编辑:程序博客网 时间:2024/05/27 20:03

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 32 3 300 32 3 299 32 3 12 32 3 12000 754 795 12000 72 3 300 12 1 200 52 4 54 20 0 0 0

SampleOutput

4762421093

Source

Japan 2004Domestic
 
解析:
   题目大意:给定一个分数,把它划分为若干个单元分数,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);

  }

}

原创粉丝点击