POj3292 Semi-prime H-numbers

Source: Internet
Author: User

POj3292 Semi-prime H-numbers

  Ed Screening + Violence Watch

first hit the H-prime table , each to find a h-prime, to sieve away its multiples,

Here can be pruned, only need to sift out similar 4n+1 such as the number, if I is h-prime, only need to sift off 5i,9i,... (4k+1) I, (k=1,2,3,4,5 ...)

The proof is as follows, set X*I is a multiple of I, just make x*i=4m+1, (m=1,2,3,...) When you find the X

Set I=4n+1, then:

(x*i)%4=x* (4*n+1)%4= (x*4*n+x)%4=x%4=1;

So X=4k+1 (k=1,2,3,4 ...)

  h-prime table, and then hit the H-semi-primes table , each enumeration two h-prime, get H-semi-primes, and Mark H-semi-primes.

After the table, 16ms water over, because the second table when the judge condition is (h_prime[i]*h_prime[i]<=size), so the final complexity is still smaller than O (n^2).

/** created:2016 March 30 20:57 49 Second Wednesday * author:akrusher**/#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#include<string>#include<vector>#include<deque>#include<list>#include<Set>#include<map>#include<stack>#include<queue>#include<numeric>#include<iomanip>#include<bitset>#include<sstream>#include<fstream>using namespacestd;#defineRep (i,a,n) for (int i=a;i<n;i++)#definePer (i,a,n) for (int i=n-1;i>=a;i--)#defineIn (n) scanf ("%d",& (n))#defineIn2 (X1,X2) scanf ("%d%d",& (x1),& (x2))#defineINLL (n) scanf ("%i64d",& (n))#defineInll2 (X1,X2) scanf ("%i64d%i64d",& (x1),& (x2))#defineINLLD (n) scanf ("%lld",& (n))#defineInlld2 (X1,X2) scanf ("%lld%lld",& (x1),& (x2))#defineINF (n) scanf ("%f",& (n))#defineInf2 (X1,X2) scanf ("%f%f",& (x1),& (x2))#defineINLF (n) scanf ("%lf",& (n))#defineINLF2 (X1,X2) scanf ("%lf%lf",& (x1),& (x2))#defineInc (STR) scanf ("%c",& (str))#defineINS (str) scanf ("%s", (str))#defineOut (x) printf ("%d\n", (x))#defineOut2 (x1,x2) printf ("%d%d\n", (x1), (x2))#defineOutf (x) printf ("%f\n", (x))#defineOUTLF (x) printf ("%lf\n", (x))#defineOUTLF2 (x1,x2) printf ("%lf%lf\n", (x1), (x2));#defineOUTLL (x) printf ("%i64d\n", (x))#defineOUTLLD (x) printf ("%lld\n", (x))#defineOUTC (str) printf ("%c\n", (str))#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#defineSZ (x) ((int) (x). Size ())#defineMem (x, y) memset (x,y,sizeof (×));typedef vector<int>Vec;typedefLong LongLl;typedef pair<int,int>P;Const intdx[4]={1,0,-1,0},dy[4]={0,1,0,-1};Const intinf=0x3f3f3f3f;Constll mod=1e9+7; ll Powmod (ll A,ll b) {ll res=1; a%=mod; for(; b;b>>=1){if(b&1) Res=res*a%mod;a=a*a%mod;}returnRes;}Const BOOLAc=true; ll h_prime[1000005];//Storage H_primeBOOLis_h_prime[1000005];ll num[1000005];//count the number of products consisting of two h_prime number within IBOOLused[1000005];Constll size=1000001;//violence to the table, first hit the prime table, and then hit by two prime numbers of the tablevoidSieve () {//H_prime Filteringll q=0;  for(LL i=5; i<=size;i+=4) is_h_prime[i]=true;  for(LL i=5; i<=size;i+=4){        if(Is_h_prime[i]) {h_prime[q++]=i;  for(LL j=5*i;j<=size;j+=4*i) is_h_prime[j]=false;//filter out multiple + pruning (filter 4n+1 only)        }        }}voidFactor () {//enumeration of the number of products consisting of two h_prime numbers (whether the tag is used)ll cnt=0;  for(LL i=0; h_prime[i]*h_prime[i]<=size;i++){         for(LL j=i;h_prime[j]*h_prime[i]<=size;j++){            if(!used[h_prime[j]*H_prime[i]]) {Used[h_prime[j]*h_prime[i]]=true; }        }    }     for(LL i=1; i<=size;i++) {//H-semi-primes playing table    if(Used[i]) {CNT++; } Num[i]=cnt;//num has to be assigned regardless of the CNT addition. }}            intMain () {ll n;    Sieve (); factor ();  while(INLLD (n) = =1){    if(n==0) Break; printf ("%lld%lld\n", N,num[n]); }    return 0;}

POj3292 Semi-prime H-numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.