[POJ1980]Unit Fraction Partition(搜索)
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 0Sample Output
4
7
6
2
42
1
0
9
3Source
沦落到要做普及组题目的地步了吗。。发现自己连搜索都不会写了。
几个可行性剪枝就可以了:乘积不超限,个数不超限,分数和不超过目标。
起先一直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(搜索)的更多相关文章
- 【题解】Unit Fraction Partition-C++
Description给出数字P,Q,A,N,代表将分数P/Q分解成至多N个分数之和,这些分数的分子全为1,且分母的乘积不超过A.例如当输入数据为2 3 120 3时,我们可以得到以下几种分法: In ...
- 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题) ...
- Hadoop官方文档翻译——MapReduce Tutorial
MapReduce Tutorial(个人指导) Purpose(目的) Prerequisites(必备条件) Overview(综述) Inputs and Outputs(输入输出) MapRe ...
- 泛函编程(34)-泛函变量:处理状态转变-ST Monad
泛函编程的核心模式就是函数组合(compositionality).实现函数组合的必要条件之一就是参与组合的各方程序都必须是纯代码的(pure code).所谓纯代码就是程序中的所有表达式都必须是Re ...
- Delphi XE5教程8:使用Delphi命名空间
// Project file declarations... //项目文件声明… program MyCompany.ProjectX.ProgramY; // Unit source file d ...
- project euler 26:Reciprocal cycles
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with d ...
- Spark Sort-Based Shuffle具体实现内幕和源码详解
为什么讲解Sorted-Based shuffle?2方面的原因:一,可能有些朋友看到Sorted-Based Shuffle的时候,会有一个误解,认为Spark基于Sorted-Based Shuf ...
- (转)Oracle分区表和索引的创建与管理
今天用到了Oracle表的分区,就顺便写几个例子把这个表的分区说一说: 一.创建分区表 1.范围分区 根据数据表字段值的范围进行分区 举个例子,根据学生的不同分数对分数表进行分区,创建一个分区表如下: ...
- linux 文件的查找和压缩
1.使用 locate 命令 需要安装:yum install mlocate -y 创建或更新 slocate/locate 命令所必需的数据库文件:updatedb 作用:搜索不经常改变的文件如配 ...
随机推荐
- 【51NOD-0】1058 N的阶乘的长度
[算法]数学 [题解]n!的位数相当于ans=log10(n!)上取整,然后就可以拆出来加了. 可以用log10(i)或log(i)/log(10) 阶乘好像有个斯特林公式…… #include< ...
- 上传漏洞新姿势(限Linux)
服务器:Linux当前环境:nginx/1.4.7PHP版本:PHP Version 7.0.0 上传情况简介:上传 111.jpg111 确实可以成功的但是上传 1.php.jpg1111.1 ...
- 【Python学习笔记】Coursera课程《Using Python to Access Web Data》 密歇根大学 Charles Severance——Week6 JSON and the REST Architecture课堂笔记
Coursera课程<Using Python to Access Web Data> 密歇根大学 Week6 JSON and the REST Architecture 13.5 Ja ...
- mysql查询语句的执行顺序(重点)
一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOI ...
- 详见github
本栏博客不再专门更新,详见:https://github.com/dxscjx123/LeetCode
- centos 挂在ntfs
Installing build-essentials in CentOS (make, gcc, gdb):http://www.techblogistech.com/2012/03/install ...
- mybatis模糊查询sql
今天下午做的一个功能,要用到模糊查询,字段是description,刚开始我的写法用的是sql中的模糊查询语句, 但是这个有问题,只有将字段的全部值传入其中,才能查询,所以不是迷糊查询. 后来经过搜索 ...
- mui 怎样监听scroll事件的滚动距离
var scroll = mui('.mui-scroll-wrapper').scroll(); document.querySelector('.mui-scroll-wrapper' ).add ...
- 1:django models
重温django model 1:many-to-many 的额外属性 一般情况下,many-to-many直接就可以满足我们的要求,考虑这样一种情况: 音乐家和乐团是many-to-many的关系, ...
- Mybatis学习—XML配置文件
总结自 Mybatis官方中文文档 XML 映射配置文件 MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置(settings)和属性(properties)信息.文档的顶层结构如 ...