hdu 5150(水题)

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(1N1000). 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 = 1005;
int p[N];
void init(){
    for(int i=2;i<=1000;i++){
        if(!p[i]){
            for(int j=i*i;j<=1000;j+=i){
                p[j] = true;
            }
        }
    }

}
int main()
{
    init();
    int n;
    while(scanf("%d",&n)!=EOF){
        int sum = 0;
        for(int i=1;i<=n;i++){
            int v;
            scanf("%d",&v);
            if(!p[v]) sum+=v;
        }
        printf("%d\n",sum);
    }
    return 0;
}

 

posted @ 2016-07-15 09:12  樱花庄的龙之介大人  阅读(162)  评论(0)    收藏  举报