Project Euler:Product-sum numbers (problem 88) C++
A natural number, N, that can be written as the sum and product of a given set of at least two natural numbers, {a1, a2, ... , ak} is called a product-sum number: N = a1 + a2 + ... + ak = a1 × a2 × ... × ak.
For example, 6 = 1 + 2 + 3 = 1 × 2 × 3.
For a given set of size, k, we shall call the smallest N with this property a minimal product-sum number. The minimal product-sum numbers for sets of size, k = 2, 3, 4, 5, and 6 are as follows.
k=2: 4 = 2 × 2 = 2 + 2
k=3: 6 = 1 × 2 × 3 = 1 + 2 + 3
k=4: 8 = 1 × 1 × 2 × 4 = 1 + 1 + 2 + 4
k=5: 8 = 1 × 1 × 2 × 2 × 2 = 1 + 1 + 2 + 2 + 2
k=6: 12 = 1 × 1 × 1 × 1 × 2 × 6 = 1 + 1 + 1 + 1 + 2 + 6
Hence for 2≤k≤6, the sum of all the minimal product-sum numbers is 4+6+8+12 = 30; note that 8 is only counted once in the sum.
In fact, as the complete set of minimal product-sum numbers for 2≤k≤12 is {4, 6, 8, 12, 15, 16}, the sum is 61.
What is the sum of all the minimal product-sum numbers for 2≤k≤12000?
求积和数的一道题目,大多都是递归。
继续推导可以发现,f(k)的取值在[k,2k]之间。
可以推出,k=num-因子和+(num-因子和)*1;
那好了,就是写个<set>去重,写出递归函数就可以。
#include<iostream>
#include<set>
using namespace std;
set<int> Q;
set<int>::iterator it;
bool re(int x,int y,int z);
int getn(int n)
{
for(int k=n+1;k<=2*n;k++) //k的取值 k---2k
{
if(re(k,k,n)) //num, sum, digit
return k;
}
}
bool re(int x,int y,int z)
{
//cout<<x<<" "<<y<<" "<<z<<endl;
if(y<z)
return 0;
if(x==1)
return y==z;
if(z==1)
return x==y;
for(int i=2;i<=x;i++)
{
if(x%i==0)
{
// cout<<" i="<<i<<endl;
if(re(x/i,y-i,z-1))
return 1;
} }
return 0;
}
int main()
{
int n;
long long s=0;
for(int i=2;i<=12000;i++)
{
n=getn(i);
Q.insert(n); //此处可以直接判断 insert()的返回值,求和。
}
for(it=Q.begin();it!=Q.end();it++)
{
s+=*it;
}
cout<<s<<endl;
} //execution time : 21.385 s
Project Euler:Product-sum numbers (problem 88) C++的更多相关文章
- Project Euler:Problem 88 Product-sum numbers
A natural number, N, that can be written as the sum and product of a given set of at least two natur ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- Project Euler:Problem 61 Cyclical figurate numbers
Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...
- Project Euler:Problem 42 Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- Project Euler:Problem 87 Prime power triples
The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...
- Project Euler:Problem 28 Number spiral diagonals
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...
- Project Euler:Problem 32 Pandigital products
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...
- Project Euler:Problem 76 Counting summations
It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...
- Project Euler:Problem 34 Digit factorials
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...
随机推荐
- 用jlink在mini2440上烧写uboot
首先,附上我安装jlink驱动: http://download.csdn.net/detail/zzmno1/3776716#comment 以及我使用的uboot.bin文件下载地址: http: ...
- dotnet使用Selenium执行自动化任务
如果要做百度文库,百度贴吧,百度知道签到,你,会怎么做?前不久我还会觉得这好像太麻烦了,now,soeasy. 自动化测试工具:Selenium Selenium是一个用于Web应用程序测试的工具.S ...
- php设计模式--注册器模式
之前介绍过工厂模式和策略模式有两种方式去调用方法,一种是用工厂方法调用,一种是单例调取实例,那实际上这两种都有一个缺点(可能也不应该称为缺点,也可叫不足),有一个比较好的方法就是已经创建好的这个对象, ...
- 7_linux下PHP、Apache、Mysql服务的安装
1.首先安装之前,要确保你的虚拟机能连上外网. Mysql: 1.yum -y install mysql 连接数据库命令行模式 2.yum install mysql-server 安装mys ...
- 浏览器F12进行Web程序调试
转自http://www.cnblogs.com/yougewe/p/5152700.html 引语:如今的整个Web开发行业甚至说整个软件开发行业,已经相当成熟,基本上已经很少找不到没有前人做过的东 ...
- 【JSP】JSP Action动作标签
Action动作标签简述 JSP action是JSP技术体系内置的一组标签,使用无需导入,或者添加另外的库.JSP action标签都是对Java代码的逻辑的封装.主要使用的是下面这些. 标签 作用 ...
- v-cloak 实现vue实例未编译完前不显示
前言: 由于网速原因,(ps:之前同事无意间在网速很差的情况下测出的)在使用vue开发时,会由于vue实例还没编译成功的时候数据绑定的"Mustache"标签会闪现一下,造成不好的 ...
- this和super的运用
这几天看到类在继承时会用到this和super,这里就做了一点总结,与各位共同交流,有错误请各位指正~ this this是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针. this ...
- macaca 测试web(3)
上回书说到 macaca 测试web(2) 使用ddt做参数驱动化, 有些人会说,你好low,我说怎么low呢,他说你看看你的脚本就放在一个文件里,对于我们小白来说,这看起来很乱啊,能不能给我拆分, ...
- Day3 - Linux系统安装_Centos6.9
第1章 虚拟机安装 1.1 镜像下载 1.1.1 新版本下载 http://mirrors.aliyun.com #阿里云官方镜像站点 1.1.2 旧版本下载 http://vault.cento ...