题目链接:Sum Sum Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1590 Accepted Submission(s): 931 Problem Description We call a positive number X P-number if there is not a positive number that is less than X and the greatest common divisor of these two numbers is bigger than 1. Input There are several test cases. Output For each test case, output the sum of P-numbers of the sequence. Sample Input 3 5 6 7 1 10 Sample Output 12 0 |
题意简述:
测试数据有多组,每组首先输入正整数n(1≤n≤1000),然后输入n个整数(1≤整数≤1000)。计算其中素数的和。
程序说明:
使用筛选法打表,再计算总和。
C语言程序如下:
/* HDU5150 Sum Sum Sum */
#include <stdio.h>
#include <memory.h>
#include <math.h>
#define MAXN 1000
int notprime[MAXN+1];
void esieve(int p[], int n)
{
int i, j;
memset(p, 0, MAXN+1);
// 筛选
int max = (int)sqrt(n);
for(i=2; i<=max; i++) {
if(p[i] == 0) {
for(j=i+i; j<=n; j+=i)
p[j] = 1;
}
}
}
int main(void)
{
esieve(notprime, MAXN);
int n, sum, val, i;
while(scanf("%d", &n) != EOF) {
sum = 0;
for(i=1; i<=n; i++) {
scanf("%d", &val);
if(!notprime[val])
sum += val;
}
printf("%d\n", sum);
}
return 0;
}