UVA1210Sum of Consecutive Prime Numbers(素数打表 + 连续和)

题目链接

题意:输入一个数n (2 <= n <= 10000) 有多少种方案可以把n写成若干个连续素数之和

打出10000之内的素数表,然后再打出每个可能得到的和的方案数的表

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 using namespace std;
 6 const int Max = 10000;
 7 int prime[Max + 5],total,flag[Max + 5];
 8 int dp[6000000];  //可以求出10000所有素数和为5000000多
 9 void get_prime()
10 {
11     memset(flag, 0, sizeof(flag));
12     total = 0;
13     for(int i = 2; i <= Max; i++)
14     {
15         if(flag[i] == 0)
16         {
17             prime[ ++total ] = i;
18             for(int j = i; j <= Max / i; j++)
19                 flag[i * j] = 1;
20         }
21     }
22 }
23 void init()
24 {
25     memset(dp, 0, sizeof(dp));
26     int ans;
27     for(int i = 1; i <= total; i++)
28     {
29         ans = 0;
30         for(int j = i; j <= total; j++)  //第i个为起点,第j个为终点的素数段
31         {
32             ans += prime[j];
33             dp[ans]++;
34         }
35     }
36 }
37 int main()
38 {
39     get_prime();
40     init();
41     int n;
42     while(scanf("%d", &n) != EOF && n)
43     {
44         printf("%d\n", dp[n]);
45     }
46     return 0;
47 }

时间: 2024-10-24 16:26:19

UVA1210Sum of Consecutive Prime Numbers(素数打表 + 连续和)的相关文章

poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697   Accepted: 10800 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

CodeForces 385C Bear and Prime Numbers 素数打表

第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample has 3 queries. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. The second query comes

POJ 2739 Sum of Consecutive Prime Numbers(素数)

http://poj.org/problem?id=2739 题意: 给你一个10000以内的自然数X,然后问你这个数x有多少种方式能由连续的素数相加得来? 分析: 首先用素数筛选法把10000以内的素数都找出来按从小到大保存到prime数组中. 然后找到数X在prime中的上界, 如果存在连续的素数之和==X, 那么一定是从一个比X小的素数开始求和(不会超过X的上界),直到和sum的值>=X为止. 所以我们暴力枚举10000以内的所有可能的素数相加和的起始点i,然后求连续素数的和,看看当前以p

POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20020   Accepted: 10966 Description Some positive integers can be represented by a sum of one or more consecutive

Sum of Consecutive Prime Numbers(素数打表+尺取)

Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53

POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895   Accepted: 10906 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

poj2739 Sum of Consecutive Prime Numbers (素数打表)

D - Sum of Consecutive Prime Numbers Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2739 Appoint description: System Crawler (2016-05-05) Description Some

POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Accepted: 10619 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations d

poj 2739 Sum of Consecutive Prime Numbers 尺取法

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21924   Accepted: 11996 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio