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. 【Luogu】P3927 SAC E#1 - 一道中档题 Factorial

    [题目]洛谷10月月赛R1 提高组 [题意]求n!在k进制下末尾0的个数,n<=1e18,k<=1e16. [题解]考虑10进制末尾0要考虑2和5,推广到k进制则将k分解质因数. 每个质因 ...

  2. git 配置多用户

    .ssh 下的 config.txt 内容 # 配置github.com Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa_ ...

  3. Linux简介——(一)

    1. 常见操作系统 - 服务端操作系统 : linux.unix.windows server - 单机操作系统 : windows(dos .ucdos.win95.win98.win2000.xp ...

  4. 基于ansj_seg和nlp-lang的简单nlp工具类

    1.首先在pom中引入ansj_seg和nlp-lang的依赖包, ansj_seg包的作用: 这是一个基于n-Gram+CRF+HMM的中文分词的java实现: 分词速度达到每秒钟大约200万字左右 ...

  5. perl发送post数据

    把post数据写进一个匿名数组里就行 #!/usr/bin/env perl -w use strict; use LWP::UserAgent; my $ua = LWP::UserAgent-&g ...

  6. KVM虚拟机建立快照

    部分转载: http://blog.csdn.net/gg296231363/article/details/6899533 windows虚拟机默认镜像格式为raw,快照默认格式为qcow2.win ...

  7. UNIX v6

    UNIX v6 http://download.csdn.net/download/u013896535/9106775 https://github.com/chromium/mini_chromi ...

  8. Smarty模板快速入门

    文件下载 1.下载地址:http://www.smarty.net/ 2.我下载的版本是3.1.27 ,将下载的文件smarty-3.1.27.zip解压出来,然后将libs文件夹的所有文件复制到你的 ...

  9. centos创建子用户

    #adduser 用户名 #passwd 用户名 设置自己的密码即可 授权 个人用户的权限只可以在本home下有完整权限,其他目录要看别人授权.而经常需要root用户的权限,这时候sudo可以化身为r ...

  10. 《逐梦旅程 WINDOWS游戏编程之从零开始》笔记5——Direct3D中的顶点缓存和索引缓存

    第12章 Direct3D绘制基础 1. 顶点缓存 计算机所描绘的3D图形是通过多边形网格来构成的,网网格勾勒出轮廓,然后在网格轮廓的表面上贴上相应的图片,这样就构成了一个3D模型.三角形网格是构建物 ...