Levy Conjecture

来源:互联网 发布:常熟淘宝美工招聘 编辑:程序博客网 时间:2024/06/02 04:26

Levy Conjecture

Problem code: LEVY

  • Submit
  • My Submissions
  • All Submissions

Problem Statement

Levy's conjecture, named after Hyman Levy, states that all odd integers greater than5 can be represented as the sum of an odd prime number and an even semiprime. To put it algebraically,2n + 1 = p + 2q always has a solution in primes p andq (not necessary to be distinct) forn > 2. (Source: Wikipedia)

In this problem, given a positive integer N (not necessary to be odd integer greater than5). Your task is to calculate how many distinct ordered pairs(p, q) such that N = p + 2q, where p andq are primes.

Input

The first line of input contains an integer T, denoting the number of test cases. ThenT test cases follow.

Each test case consists of exactly one line containing an integer N.

Constraints

  • 1T100000 (105)
  • 1N10000 (104)

Output

For each test case, output the number of ordered pairs (p, q) of primes such thatN = p + 2q.

Example

Input:32711Output:012

Explanation

Case #1: There are no ordered pairs (p, q) such thatp + 2q = 2.

Case #2: There is only one ordered pair (p, q) = (3, 2) such thatp + 2q = 7.

Case #3: There are two ordered pairs (p, q) = (7, 2), (5, 3) such thatp + 2q = 11.


 

 

#include<iostream>
#include<stdlib.h>
#include<cmath>
using namespace std;

#define N  100000
int sieve[N + 1];
int prime[100002],k=0;

//质数表
void Cprime(){
 int i;
 for( i = 2; i <= N; i++) sieve[i] = 1;
 for(i = 2; i <= N / 2; i++) sieve[i * 2] = 0;
 int p = 2;
 while(p * p <= N)
 {
  p = p + 1;
  while(sieve[p] == 0)
   p++;
  int t = p * p;
  int s = 2 * p;
  while(t <= N)
  {
   sieve[t] = 0;
   t = t + s;
  }
 }
 for(i=2;i<=N;i++)
  if(sieve[i]!=0)
   prime[k++]=i;
  return ;
}
int main()
{
 int t;
 cin>>t;
 Cprime();
 sieve[1]=sieve[0]=0;
 while(t--)
 {
  int n,i,count=0;
  cin>>n;
  for(i=0;i<k;i++)
  {
   if((2*prime[i])<n&&sieve[n-2*prime[i]]!=0)
   {
   // cout<<prime[i]<<" "<<n-2*prime[i]<<endl;
    count++;
   }
   else
    if((2*prime[i])>=n)
     break;
  }
  cout<<count<<endl;
 }
 return 0;
}

原创粉丝点击