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. 使用node成功安装完某插件typescript后,在使用时提示:tsc(或xxx)不是内部或外部命令,也不是可运行的程序或批处理文件

    具体出错情形: 使用npm安装typescript明明安装成功,但在使用时一直报错,报错语句为  tsc不是内部或外部命令,也不是可运行的程序或批处理文件 具体出错原因: node未正确安装,或相关环 ...

  2. HDU 4474 Yet Another Multiple Problem BFS

    题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0  1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...

  3. Git教程(3)git工作区与文件状态及简单示例

    基础 目录: working driectory  工作目录,就是我们的工作目录,其中包括未跟踪文件及暂存区和仓库目录. staging area   暂存区,不对应一个具体目录,其实只是git di ...

  4. Serializable资料整理

    1. 序列化 简单的说就是为了保存 内存中各种对象的状态(是实例变量,不是方法),并且可以把保存的对象读取出来. 虽然保存 object states的方法很多,但是Java提供了一种保存对象状态的机 ...

  5. Assembly之instruction之Indirect Autoincrement Mode

    Assembler Code Content of ROMMOV @R10+,0(R11)   MOV @R10+,0(R11) Length: One or two words Operation: ...

  6. rev

    功能说明:反向输出文件内容.   字符串反转   文本反转

  7. C# 获取 IEnumerable 集合的个数

    IEnumerable<DocApply> data1 = data.Where(n => n.DocName.Contains(search)); if (data1.GetEnu ...

  8. python写入Excel

    一.dataframe存入Excel中: 注意:openpyxl打开的文件需是xlsx的后缀,因为比较新的. from openpyxl import load_workbook import pan ...

  9. 【Shell编程】Shell程序设计

    1.Shell简介   作为Linux灵感来源的Unix系统最初是没有图形化界面的,所有的任务都是通过命令行来实现的.因此,Unix的命令行系统得到了很大的发展,逐步成为一个功能强大的系统.   Sh ...

  10. 09.正则表达式re-3.常用的匹配规则

    模式 描述 \w 匹配字母.数字及下划线 \W 匹配不是字母.数字及下划线的字符 \s 匹配任意空白字符,等价于[\t\n\r\f] \S 匹配任意非空字符 \d 匹配任意数字,等价于[0-9] \D ...