hdu 5150(水题)

时间:2024-09-20 23:07:32

Sum Sum Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1033    Accepted Submission(s): 612

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.
Now you are given a sequence of integers. You task is to calculate the sum of P-numbers of the sequence.
Input
There are several test cases.
In each test case:
The first line contains a integer N(1≤N≤1000). The second line contains N integers. Each integer is between 1 and 1000.
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
Source
题意:一个数如果与小于等于它的非负整数的最大公约数是1,那么他就是P-number,给出一个子序列,问里面的P-number之和.
题解:注意不仅仅是素数,1也是P-number
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = ;
int p[N];
void init(){
for(int i=;i<=;i++){
if(!p[i]){
for(int j=i*i;j<=;j+=i){
p[j] = true;
}
}
} }
int main()
{
init();
int n;
while(scanf("%d",&n)!=EOF){
int sum = ;
for(int i=;i<=n;i++){
int v;
scanf("%d",&v);
if(!p[v]) sum+=v;
}
printf("%d\n",sum);
}
return ;
}