Unit Fraction Partition
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4571   Accepted: 1809

Description

A fraction whose numerator is 1 and whose denominator is a positive integer is called a unit fraction. A representation of a positive rational number p/q as the sum of finitely many unit fractions is called 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 in the order of addition is disregarded. For example, we do not distinguish 1/6 + 1/2 from 1/2 + 1/6.

For given four positive integers p, q, a, and n, count the number of
partitions of p/q into unit fractions satisfying the following two
conditions.

The partition is the sum of at most n many unit fractions.

The product of the denominators of the unit fractions in the partition is less than or equal to a.

For example, if (p,q,a,n) = (2,3,120,3), you should report 4 since



enumerates all of the valid partitions.

Input

The input is a sequence of at most 200 data sets followed by a terminator.

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

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

Output

The
output should be composed of lines each of which contains a single
integer. No other characters should appear in the output.

The output integer corresponding to a data set p, q, a, n should be
the number of all partitions of p/q into at most n many unit fractions
such that the product of the denominators of the unit fractions is less
than or equal to a.

Sample Input

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

Sample Output

4
7
6
2
42
1
0
9
3

Source

[Submit]   [Go Back]   [Status]   [Discuss]

沦落到要做普及组题目的地步了吗。。发现自己连搜索都不会写了。

几个可行性剪枝就可以了:乘积不超限,个数不超限,分数和不超过目标。

起先一直TLE,把循环中的除法提到外面就卡过了。

这种题目竟然也要做1h。。

 #include<cstdio>
#include<algorithm>
#define rep(i,l,r) for (int i=l; i<=r; i++)
using namespace std; int p,q,a,n,ans; void dfs(int mn,int num,int deno,int mul,int dq){
if (mul>a) return;
if (num*q==deno*p) ans++;
if (num*q>deno*p || dq==n) return;
int t=a/mul;
rep(i,mn,t) dfs(i,num*i+deno,deno*i,mul*i,dq+);
} int main(){
freopen("poj1980.in","r",stdin);
freopen("poj1980.out","w",stdout);
while (~scanf("%d%d%d%d",&p,&q,&a,&n)){
if (q==) return ;
ans=; dfs(,,,,); printf("%d\n",ans);
}
return ;
}

[POJ1980]Unit Fraction Partition(搜索)的更多相关文章

  1. 【题解】Unit Fraction Partition-C++

    Description给出数字P,Q,A,N,代表将分数P/Q分解成至多N个分数之和,这些分数的分子全为1,且分母的乘积不超过A.例如当输入数据为2 3 120 3时,我们可以得到以下几种分法: In ...

  2. AHOI2018训练日程(3.10~4.12)

    (总计:共90题) 3.10~3.16:17题 3.17~3.23:6题 3.24~3.30:17题 3.31~4.6:21题 4.7~4.12:29题 ZJOI&&FJOI(6题) ...

  3. Hadoop官方文档翻译——MapReduce Tutorial

    MapReduce Tutorial(个人指导) Purpose(目的) Prerequisites(必备条件) Overview(综述) Inputs and Outputs(输入输出) MapRe ...

  4. 泛函编程(34)-泛函变量:处理状态转变-ST Monad

    泛函编程的核心模式就是函数组合(compositionality).实现函数组合的必要条件之一就是参与组合的各方程序都必须是纯代码的(pure code).所谓纯代码就是程序中的所有表达式都必须是Re ...

  5. Delphi XE5教程8:使用Delphi命名空间

    // Project file declarations... //项目文件声明… program MyCompany.ProjectX.ProgramY; // Unit source file d ...

  6. project euler 26:Reciprocal cycles

    A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with d ...

  7. Spark Sort-Based Shuffle具体实现内幕和源码详解

    为什么讲解Sorted-Based shuffle?2方面的原因:一,可能有些朋友看到Sorted-Based Shuffle的时候,会有一个误解,认为Spark基于Sorted-Based Shuf ...

  8. (转)Oracle分区表和索引的创建与管理

    今天用到了Oracle表的分区,就顺便写几个例子把这个表的分区说一说: 一.创建分区表 1.范围分区 根据数据表字段值的范围进行分区 举个例子,根据学生的不同分数对分数表进行分区,创建一个分区表如下: ...

  9. linux 文件的查找和压缩

    1.使用 locate 命令 需要安装:yum install mlocate -y 创建或更新 slocate/locate 命令所必需的数据库文件:updatedb 作用:搜索不经常改变的文件如配 ...

随机推荐

  1. 【转载】VS2013安装需要IE10

    因为需要移动办公,需要给笔记本搭建编程环境.安装VS2013时遇到了小麻烦,提示我,需要安装IE10. 然后我很听话的按照提供的超链接,到了官网,下载了最新的IE11,然后安装,结果告诉我下载的IE版 ...

  2. 洛谷 P3375 【模板】KMP字符串匹配

    我这段时间因为字符串太差而被关了起来了(昨晚打cf不会处理字符串现场找大佬模板瞎搞,差点就凉了),所以决定好好补一下字符串的知识QAQ,暂时先学习kmp算法吧~ 题目链接:https://www.lu ...

  3. perl中设置POST登录时的重定向

    默认地, perl提交post登录时是不会重定向的 要让它重定向, 可以用如下方法: my $cookie = HTTP::Cookies->new(); push @{$ua->requ ...

  4. 【EverydaySport】健身笔记——静态牵拉

    静态牵拉一般在运动后进行,可以有效的提高肌肉的柔韧性和关节的灵活性,预防和缓解疼痛. 每个动作达到自己活动范围的最大,有牵拉感即说明有效,静态保持至少30秒,切勿震荡,进行2组. 1 大腿前群牵拉 2 ...

  5. (二十一)Makefile例子

    ROOT_PROJECT = .DIR_INC = -I$(ROOT_PROJECT)/include -I$(ROOT_PROJECT)/include/NE10 DIR_BIN = $(ROOT_ ...

  6. udpserver.pl 和 udpclient.pl

    udpserver.pl #!use/bin/perl -w use Socket; #导入Socket库 ,INADDR_ANY);#压入sockaddr_in模式,利用了全局当地压缩地点INADD ...

  7. [New learn]AutoLayout调查基于IB

    代码:https://github.com/xufeng79x/AutoLayout-IB 1.简介 Autolayout旨在解决不同高宽度的屏幕下的显示问题,通过增加给控件增加约束来达到不同屏幕间的 ...

  8. leetcode 121 122 123 . Best Time to Buy and Sell Stock

    121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...

  9. Dubbo之旅--注册中心

    在介绍Dubbo的内部逻辑的时候提到很多次注册中心的概念.实现注册中心的有很多,主要是以下四个注册中心分别是: Multicast注册中心 Zookeeper注册中心 Redis注册中心 Simple ...

  10. find命令的基本用法

     linux 中find 常用示例解析 find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…] [expression] 其实[-H] [-L] [- ...