首页 > 代码库 > HDU 5150 Sum Sum Sum 素数
HDU 5150 Sum Sum Sum 素数
Sum Sum Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 290 Accepted Submission(s): 194
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.
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. The second line contains N integers. Each integer is between 1 and 1000.
In each test case:
The first line contains a integer N. 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
35 6 7110
Sample Output
120
难点是把:primes[1]=1;
#include <stdio.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <math.h>#include <string.h>using namespace std;const int MAXN = 1001;bool flag[MAXN];int primes[MAXN], pi;void GetPrime_1(){ int i, j; pi = 0; memset(flag, false, sizeof(flag)); for (i = 2; i < MAXN; i++) if (!flag[i]) { primes[i] = 1;//素数标识为1 for (j = i; j < MAXN; j += i) flag[j] = true; }}int main(){ memset(primes,0,sizeof(primes)); GetPrime_1(); primes[1]=1; int n; while(scanf("%d",&n)!=EOF) { long long ans=0; int a; for(int i=0;i<n;i++) { cin>>a; if(primes[a]==1) ans+=a; } cout<<ans<<endl; } return 0;}
HDU 5150 Sum Sum Sum 素数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。