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>去重,写出递归函数就可以。

  1. #include<iostream>
  2. #include<set>
  3. using namespace std;
  4. set<int> Q;
  5. set<int>::iterator it;
  6. bool re(int x,int y,int z);
  7. int getn(int n)
  8. {
  9. for(int k=n+1;k<=2*n;k++) //k的取值 k---2k
  10. {
  11. if(re(k,k,n)) //num, sum, digit
  12. return k;
  13. }
  14. }
  15. bool re(int x,int y,int z)
  16. {
  17. //cout<<x<<" "<<y<<" "<<z<<endl;
  18. if(y<z)
  19. return 0;
  20. if(x==1)
  21. return y==z;
  22. if(z==1)
  23. return x==y;
  24. for(int i=2;i<=x;i++)
  25. {
  26. if(x%i==0)
  27. {
  28. // cout<<" i="<<i<<endl;
  29. if(re(x/i,y-i,z-1))
  30. return 1;
  31. }
  32.  
  33. }
  34. return 0;
  35. }
  36. int main()
  37. {
  38. int n;
  39. long long s=0;
  40. for(int i=2;i<=12000;i++)
  41. {
  42. n=getn(i);
  43. Q.insert(n); //此处可以直接判断 insert()的返回值,求和。
  44. }
  45. for(it=Q.begin();it!=Q.end();it++)
  46. {
  47. s+=*it;
  48. }
  49. cout<<s<<endl;
  50. }
  51.  
  52. //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. java修饰符public final static abstract transient

    JAVA 修饰符public final static abstract transient  关键字: public final static abstract ...  1.public prot ...

  2. webService接口交互

    1.需要在XXXXX.wsdl中配置相应的类与service方法. 2.启动你的项目,打开浏览器,输入地址:http://localhost:8080/lis/services,就能看到你XXXXX. ...

  3. cygwin环境c语言开发

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

  4. Redis笔记——技术点汇总

    目录 · 特点 · 安装 · 数据库 · 服务器命令 · 数据类型及其操作命令 · 数据结构 · string · list · set · hash · zset · 发布与订阅 · 排序 · 事务 ...

  5. Markov不等式,Chebyshev不等式

    在切诺夫界的证明中用到了Markov不等式,证明于此~顺便把Chebyshev不等式也写上了

  6. Thread.interrupt()方法理解

    原博客地址: 多线程编程 实战篇 (四) 不客气地说,至少有一半人认为,线程的"中断"就是让线程停止. 如果你也这么认为,那你对多线程编程还没有入门. 在java中,线程的中断(i ...

  7. [知了堂学习笔记]_JSON数据操作第1讲(初识JSON)

    一.认识JSON 什么是JSON? JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式..它基于 ECMAScript (w3c制定的js规 ...

  8. jQuery遍历-同胞

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

  9. Informatica学习:1、安装介质的获取与安装

    本文目标: 为方便学习Informatica工具,在个人电脑上部署Informatica Powercenter. 所用系统:win7 64位. Informatica安装包括服务器端.客户端安装两个 ...

  10. 在htnl中,<input tyle = "text">除了text外还有几种种新增的表单元素

    input标签新增属性       <input   list='list_t' type="text" name='user' placeholder='请输入姓名' va ...