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. decimal扩展方法(转换为字符串,去掉末尾的0)

    /// <summary> /// 转换为字符串,去掉末尾0 /// </summary> /// <param name="target">被 ...

  2. postman也可以使用F12功能

    背景: 做过接口测试的话,大多数都知道或使用过postman工具,使用postman的时候,有时候希望也可以像chrome一样使用F12功能,这样方便观察一些数据,尤其是当你使用了postman的变量 ...

  3. 【Centos7】hostnamectl 设置主机名

    Centos7中提供了设置主机名的工具 hostnamectl hostname有三种状态 static(永久) transient(瞬态) pretty (灵活) 查看主机名状态 [oracle@h ...

  4. Windows桌面快捷图标上的小箭头的恢复

    Windows桌面快捷图标上的小箭头的恢复.. 桌面快捷图标上的小箭头的恢复: cmd /k reg add "HKEY_CLASSES_ROOT\lnkfile" /v IsSh ...

  5. CY7C68013A控制传输

    大家好,你们的大熊又回来了.本篇文章我们来重点了解一下USB设备的四大传输方式之一--控制传输.不同于其他三种传输方式,控制传输有其独特的作用和功能,是一个USB设备必须支持的传输方式.控制传输对带宽 ...

  6. web中转发、重定向等问题的路径

    web中常用路径,转发,重定向,form表单action的路径 路径的写法: a.绝对路径写法:ServeltContext都必须用绝对路径."/" b.相对路径:其他情况都可以使 ...

  7. 数据库-MYSQL安装配置和删除

    * 课程回顾: * 完成注册和登陆的功能. * 准备的工作 * 技术.开源jar包 * 开发的功能使用MVC模式 * C:控制层(接收请求和从客户端发送过来的参数) * 接收参数(request对象) ...

  8. 线性代数-矩阵-转置 C和C++的实现

    原理解析: 本节介绍矩阵的转置.矩阵的转置即将矩阵的行和列元素调换,即原来第二行第一列(用C21表示,后同)与第一行第二列(C12)元素调换位置,原来c31与C13调换.即cij与cji调换 . (此 ...

  9. 谈谈个人网站的建立(二)—— lucene的使用

    首先,帮忙点击一下我的网站http://www.wenzhihuai.com/ .谢谢啊,如果可以,GitHub上麻烦给个star,以后面试能讲讲这个项目,GitHub地址https://github ...

  10. jQuery遍历-同胞

    同胞拥有相同的父元素. 通过 jQuery,您能够在 DOM 树中遍历元素的同胞元素. 在 DOM 树中水平遍历 有许多有用的方法让我们在 DOM 树进行水平遍历: siblings() next() ...