關鍵點:
合數:可以分解成任意質數的乘積
求出質數表,然後處理輸入
#include<iostream>
#include<stdio.h>
using namespace std;
#define SIZE (1<<24)
int sushuVec[1080000] ;
bool sushuB[SIZE+1];
int totalnum;
void getSushuVec()
{
totalnum = 0;
memset(sushuB,true,sizeof(sushuB));
memset(sushuVec,0,sizeof(sushuVec));
for(int i=2;i<=SIZE;++i)
{
if(sushuB[i] == true)
{
sushuVec[totalnum] = i;
++totalnum;
}
for(int j=0;j<totalnum && i*sushuVec[j]<=SIZE;++j)
{
sushuB[ i*sushuVec[j] ] =false;
if(i%sushuVec[j] == 0)
break;
}
}
}
int main()
{
int a,result = 0;
getSushuVec();
while(cin>>a)
{
if(a == EOF)
break;
for(int i=0;sushuVec[i] <=(a>>1);++i)
{
if(sushuB[a-sushuVec[i] ])
++result;
}
printf("%d",result);
result =0;
}
return 1;
}