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. angular的directive指令的link方法

    比如 指令标签 <mylink myLoad="try()"></mylink> link:function(scope,element,attr){ el ...

  2. 运行Django项目指定IP和端口

    默认IP和端口 python manage.py runserver 指定端口: python manage.py runserver 192.168.12.12:8080 此时会报错,我们需要修改配 ...

  3. .net core2.0 中使用DB First

    一.新建一个控制台测试项目 1.1.添加引用 1.2.修改项目文件 1.3.添加红框的内容 <ItemGroup> <DotNetCliToolReference Include=& ...

  4. 前端-Vue学习思维导图笔记

    看不清的朋友右键保存或者新窗口打开哦!喜欢我可以关注我,还有更多前端思维导图笔记有vue结构分析,JS基础,JQ,JS高级,Angular,git等等

  5. ListView中动态显示隐藏HeaderView和FooterView

    ListView中动态显示和隐藏Header&Footer 解决思路: 直接设置HeaderView和FooterView.setVisibility(View.GONE)无效, 布局仍然存在 ...

  6. js 全选选框与取消全选代码

    设置一个全选选框和四个子选框,要实现点击全选后四个子选框选中,取消全选后四个子选框也取消.全选后点击某个子选框,全选也能取消.当四个子选框都选中时,全选框也被选择. 实现代码: <script& ...

  7. MySQL在Linux下的表名如何不区分大小写

    MySQL在Linux下的表名如何不区分大小写   今天测试的时候,遇到一些问题,明明看到数据,就是查不出来;后来发现,在linux下, mysql的表名区分大小写,而在windows下是不区分,从w ...

  8. MySQL NULL 值如何处理?

    我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作. 为了处理这种情况,MySQL提 ...

  9. ext4的一些特性

    delalloc介绍 delalloc是ext4下的一个新特性,延迟分配技术Delay Allocation. 实现原理为: Buffer Write时数据会被保存到page cache中,但是系统并 ...

  10. Hzoi 2018.2.11多边形 区间DP

    给定一个由N个顶点构成的多边形,每个顶点被赋予一个整数值,而每条边则被赋予一个符号:+(加法运算)或者*(乘法运算),所有边依次用整数1到N标识. 一个多边形的图形表示 首次移动,允许将某条边删除: ...