Sum Sum Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 146 Accepted Submission(s): 108
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.
P-number if there is not a positive number that is less than
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 integerN(1≤N≤1000) .
The second line containsN
integers. Each integer is between 1 and 1000.
In each test case:
The first line contains a integer
The second line contains
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/* HDU 5150 素数求和 注意1也算。。。 */ #include<iostream> #include<stdio.h> using namespace std; int hash[1001]; void getResult() { int i,j; memset(hash,0,sizeof(hash)); hash[1]=1; for(i=2;i<1001;i++) { if(hash[i]==0) { hash[i]=1; for(j=i*2;j<1001;j+=i) hash[j]=-1; } } } int main() { int n,a,sum,i; getResult(); while(scanf("%d",&n)!=EOF) { sum=0; for(i=0;i<n;i++) { scanf("%d",&a); if(hash[a]==1) sum+=a; } printf("%d\n",sum); } return 0; }