Primes Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2841 Accepted Submission(s): 1276
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.
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
9
Sample Output
0
2
2
题意:找到三个素数 i,j,k 满足 i<=j<=k 并且 i+j+k == n 输入n,问满足这个条件的有多少。
题解:开始的时候直接枚举 i (1-n/2) 炸掉了,后来估计了一下 i j 不会超过 n/2。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include <queue>
using namespace std;
bool p[];
void init(){
p[] = true;
for(int i=;i<=;i++){
if(!p[i]){
for(int j=i*i;j<=;j+=i) p[j] = true;
}
}
}
int main(){
init();
int n;
while(scanf("%d",&n)!=EOF){
int cnt = ;
for(int i=;i<n/;i++){
for(int j=i;j<n/;j++){
int k = n-i-j;
if(!p[i]&&!p[j]&&!p[k]&&k>=j){
cnt++;
}
}
}
printf("%d\n",cnt);
}
return ;
}