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++的更多相关文章

  1. 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 ...

  2. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  3. Project Euler:Problem 61 Cyclical figurate numbers

    Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. SpringMVC详解(四)------SSM三大框架整合之登录功能实现

    为了后面讲解的需要,我们取数据都会从数据库中获取,所以这里先讲讲三大框架(Spring.SpringMVC.MyBatis)的整合.前面讲解 MyBatis 时,写了一篇 MyBatis 和 Spri ...

  2. [js高手之路]构造函数的基本特性与优缺点

    上文,通过基本的对象创建问题了解了构造函数,本文,我们接着上文继续了解构造函数的基本特性,以及优缺点. 每个对象上面都有一个constructor属性( 严格意义上来说,是原型上的,对象是通过查找到原 ...

  3. 解决若要安装 Microsoft Office 2010,需要MSXML 版本 6.10.1129的问题

    单击 开始单击 运行键入 注册表编辑器然后单击 确定. 找到HKEY_CLASSES_ROOT\TypeLib\{F5078F18-C551-11D3-89B9-0000F81FE221}\6.0\0 ...

  4. cygwin环境c语言开发

    . 在windows上开发c语言,使用sublime编辑器 在工具栏tools-->run 结果报错,原因是没有在GNU环境下使用sublime text2 在 cygwin环境下启动subli ...

  5. 阿里云服务器(Windows)如何下载文件

    背景:公司只有我一个技术,在我之前还有一个老技术,属于兼职状态,为了尽快熟悉公司网站及app项目情况,我联系了老技术,请他尽快将代码发给我,他说代码文件过大,问我能不能连上服务器下载.百度了很多,都不 ...

  6. 工作中git常用命令

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px "PingFang SC" } ...

  7. WEB前端规范命名

    头部 header ----------------用于头部 主要内容 main ------------用于主体内容(中部) 左侧 main-left -------------左侧布局 右侧 ma ...

  8. js实现每次程序发送一个数据 ,多次发送不一样,5秒后继续执行多次程序,判断如果五秒后发送过来的数据和上次不一样,少的删除多的增加

    /*存储设备ID*/var IDSNew = new Array();//判断是否已经启用服务var isopen = true;//需要放到接收设备数据处IDSNew[client.deviceId ...

  9. 神奇的background

    background:url() fixed ....  可以实现页面向下滚动时背景图片 保持位置不变   感觉好像背景在随鼠标滚动而滚动一样

  10. Spring详解(五)------AOP

    这章我们接着讲 Spring 的核心概念---AOP,这也是 Spring 框架中最为核心的一个概念. PS:本篇博客源码下载链接:http://pan.baidu.com/s/1skZjg7r 密码 ...