Project Euler:Problem 77 Prime summations
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Project Euler:Problem 37 Truncatable primes
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remo ...
随机推荐
- struts2框架搭建(一)
struts2是一个基于mvc的web应用框架.struts2本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器层(Controller)来建立模型与视图的数据交互. str ...
- 2018年排名前20的数据科学Python库
Python 在解决数据科学任务和挑战方面继续处于领先地位.业已证明最有帮助的Python库,我们选择 20 多个库,因为其中一些库是相互替代的,可以解决相同的问题.因此,我们将它们放在同一个分组. ...
- WIN10 64位下VS2015 C#直接添加 halcon 12导出的CS文件实现视觉检测
C# halcon 12 联合编程的 实例 1.先调试好halcon程序,我以读取图片的程序为例.
- layui 下拉框取值
layui.use('form', function () { var form = layui.form; form.on('select(Status)', function (data) { c ...
- Git 从github克隆文件至本地
学习阶段,同一个项目,如何保障家与公司的代码同步的问题,可以使用git克隆来解决 在家将项目提交到了GitHub上,现在来到公司,需要将GitHub上的项目克隆到本地,那么对于公司的电脑来说,同样需要 ...
- 小程序text组件内部上边距的问题
index.wxml: <view class="slogan"> <text> 建立跨文化的全球视野,做世界公民 </text> </v ...
- Redis好在哪?
Redis免费入门教程:阿里云大学—开发者课堂 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis ...
- BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会 树形DP + 带权重心
Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1<=N<=100,0 ...
- 重写servlet,可以获取不同的方法
public class BaseServlet extends HttpServlet { @Override public void service(HttpServletRequest requ ...
- 如何在Loadrunner11中解决HTTP BASIC认证登录报401的问题
在对Carte+kettle的性能测试过程中,通过在loadrunner中用web_set_user("cluster", "cluster","17 ...