首页 > 其他 > 详细

hdu 5150(水题)

时间:2016-07-15 09:36:11      阅读:249      评论:0      收藏:0      [点我收藏+]

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;
}

 

hdu 5150(水题)

原文:http://www.cnblogs.com/liyinggang/p/5672415.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!