It is possible to write ten as the sum of primes in exactly five different ways:

7 + 3

5 + 5

5 + 3 + 2

3 + 3 + 2 + 2

2 + 2 + 2 + 2 + 2

What is the first value which can be written as the sum of primes in over five thousand different ways?

#include <iostream>
#include <string>
using namespace std; int prime[1000]; //存储前1000个质数
bool vis[10000]; void getPrime()
{
int count = 0;
memset(vis, 0, sizeof(vis));
for (int i = 2; i < 10000; i++)
{
if (!vis[i])
{
if (count >= 1000)
break;
prime[count++] = i;
for (int j = i*i; j < 10000; j += i)
vis[j] = 1;
}
}
}
int main()
{
getPrime();
int *ways;
int num = 2;
while (true)
{ ways = new int[num+1];
for (int i = 0; i < num + 1; i++)
ways[i] = 0;
ways[0] = 1;
for (int i = 0; i < 1000; i++)
{
for (int j = prime[i]; j <= num; j++)
{
ways[j] += ways[j - prime[i]];
}
}
//cout << num <<" " << ways[num]<< endl;
if (ways[num]>5000)
break;
else
num++;
}
cout << num << endl; system("pause");
return 0;
}

Project Euler:Problem 77 Prime summations的更多相关文章

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

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

  3. Project Euler:Problem 41 Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  4. Project Euler:Problem 55 Lychrel numbers

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

  5. Project Euler:Problem 47 Distinct primes factors

    The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The ...

  6. Project Euler:Problem 63 Powerful digit counts

    The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...

  7. Project Euler:Problem 86 Cuboid route

    A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...

  8. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  9. Project Euler:Problem 37 Truncatable primes

    The number 3797 has an interesting property. Being prime itself, it is possible to continuously remo ...

随机推荐

  1. tp 3.1.3 动态切换模板问题

    if($this->isMobile()) { C('DEFAULT_THEME', 'mobile'); // 这里定义手机模板目录 C('TMPL_CACHE_PREFIX', 'm_'); ...

  2. B - Is your horseshoe on the other hoof?

    Problem description Valera the Horse is going to the party with friends. He has been following the f ...

  3. CSS画各种二维图形

    1.效果 2.源码 <%@ page contentType="text/html;charset=UTF-8" language="java" %> ...

  4. Java基础13一异常

    1.异常概念 异常是程序在运行期发生的不正常的事件,它会打断指令的正常执行流程. 设计良好的程序应该在异常发生时提供处理这些不正常事件的方法,使程序不会因为异常的发生而阻断或产生不可预见的结果. Ja ...

  5. Windows7 win10 系统如何强制禁用驱动程序签名

    转载自奇兔 Win7 64位系统禁用驱动程序签名强制    Win7系统是比较稳定的一款系统,也是最多人在使用的一款系统.当我们在Win7系统中安装驱动程序的时候,对安装的驱动程序需要数字签名,否则驱 ...

  6. 复习java基础第二天(异常处理)

    一.常见的异常类型: public class TestException { public static void main(String[] args) { int i = 10; //数学异常: ...

  7. 查看占用某端口的进程——netstat、findstr 的使用

    netstat   检验本机各端口的网络连接情况 -a 显示所有连接和侦听端口(如Windows共享服务 的135,445端口) -n 不进行IP地址到主机名的解析 -o 显示拥有的与每个连接关联的进 ...

  8. GDI 映射模式(11)

    概述 调用 SetMapMode 函数可以设置映射模式: int SetMapMode( HDC hdc, // 设备环境句柄 int fnMapMode // 要设置的映射模式 ); 同样,调用 G ...

  9. webstorm中vue项目--运行配制

    ## npm搭建的项目,需要运行npm run dev来启动 webstorm作为一款优秀的编辑器,通过配置运行设置,达到一键运行 1.添加node.js配置 2.configuration-> ...

  10. 多叉树结构的数据,parent表示法转成children表示法

    最近碰到的问题,有个数组,数组元素是对象,该对象的结构就如树的parent表示法的节点一样.形象点讲就是该数组存放了树的所有“叶子节点”,并且叶子节点内存有父节点,一直到根节点为止,就如存了一条从叶子 ...