[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 作用:搜索不经常改变的文件如配 ...
随机推荐
- loj6102 「2017 山东二轮集训 Day1」第三题
传送门:https://loj.ac/problem/6102 [题解] 贴一份zyz在知乎的回答吧 https://www.zhihu.com/question/61218881 其实是经典问题 # ...
- Spring理论基础-控制反转和依赖注入
第一次了解到控制反转(Inversion of Control)这个概念,是在学习Spring框架的时候.IOC和AOP作为Spring的两大特征,自然是要去好好学学的.而依赖注入(Dependenc ...
- SQL Server Delete Duplicate Rows
There can be two types of duplication of rows in a table 1. Entire row getting duplicated because th ...
- centos 挂载数据盘
第一.检查硬盘设备是否有数据盘 fdisk -l 第二.数据硬盘分区 fdisk /dev/vdb 第三.ext3格式化分区 mkfs.ext3 /dev/vdb1 第四.挂载新分区 A - 新建目录 ...
- 运维开发:python websocket网页实时显示远程服务器日志信息
功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...
- Linux内核堆栈使用方法 进程0和进程1【转】
转自:http://blog.csdn.net/yihaolovem/article/details/37119971 目录(?)[-] 8 Linux 系统中堆栈的使用方法 81 初始化阶段 82 ...
- xxx_initcall相关知识
参考文件include/linux/init.h /* * Early initcalls run before initializing SMP. * * Only for built-in cod ...
- 【模板】解决二分图匹配的强力算法——Hopcroft-Karp算法
详细解释 参见:http://blog.csdn.net/wall_f/article/details/8248373 简要过程 HK算法可以当成是匈牙利算法的优化版,和dinic算法的思想比较类似. ...
- JSP(3) - 9个JSP内置对象 - 小易Java笔记
1.9个JSP内置对象 内置对象引用名称 对应的类型 request HttpServletRequest response HttpServletResponse config Servle ...
- thinkphp模板常用的方法
thinkphp模板我是看了3.2的文档,对里面的东西过了一遍,然后在写到需要用到模板的东西的时候就有印象,有的能直接回顾,但是有的就可能只知道有这个东西,但是不知道怎么用,所以就重新查手册,这个的话 ...
