Primes Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2473 Accepted Submission(s): 1076
Problem Description
Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n.
Input
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer
n(n≤10000)
.
Output
For each test case, print the number of ways.
Sample Input
3 9
Sample Output
0 2
Source
//
// week6_c.cpp
// 2
//
// Created on 16/4/11.
// Copyright © 2016年 stan. All rights reserved.
//
#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
bool IsPrime[10001];
int i,j;
for(i=0;i<=10001;++i)
IsPrime[i]=true;
IsPrime[0]=IsPrime[1]=false;
for(i=2;i<=10001;++i)
{
if(IsPrime[i])
{
for(j=2*i;j<=10001;j+=i)
IsPrime[j]=false;
}
}
int n;
while (cin>>n&&n!=EOF) {
int i=0,j=0,sum=0;
for (i=2; i<n; i++) {
for (j=i; j<=(n-i)/2; j++) {
if (IsPrime[i]&&IsPrime[j]&&IsPrime[n-i-j]) {
sum++;
}
}
}
cout<<sum<<endl;
}
return 0;
}