Codeforces 385CBear and Prime Numbers Prime table

Source: Internet
Author: User

At first glance at this topic, I think it may be difficult to see not very understand, but look at the given hint after the idea is very clear

Consider the first sample. Overall, the first sample has 3 queries. The first query L = 2, R = comes. You need to count F (2) + F (3) + F (5) + F (7) + f (one) = 2 + 1 + 4 + 2 + 0 = 9.The Second query comes L = 3, R = 12. You need to count F (3) + F (5) + F (7) + f (one) = 1 + 4 + 2 + 0 = 7.The Third query comes L = 4, R = 4. As this interval have no prime numbers, then the sum equals 0.

The range of Xn is in (2≤ xi ≤107), while L, R is in the range (2≤ liri ≤2 109), easy to calculate the time is not consideredafter 7

First of all write a prime number quickly hit the table, but also statistics on the L, the R range of each number to meet the problem conditions of the total (although it seems that the table method is really slow)

Then notice, because the number of queries, up to 5 * 104 times, so at the time of the statistics can calculate the cumulative and to save time

Source Code:

//#pragma COMMENT (linker, "/stack:16777216")//For C + + Compiler#include <stdio.h>#include<iostream>#include<fstream>#include<cstring>#include<cmath>#include<stack>#include<string>#include<map>#include<Set>#include<list>#include<queue>#include<vector>#include<algorithm>#defineMax (b) ((a) > (b))? (a): (b))#defineMin (b) ((a) < (a))? (a): (b))#defineAbs (x) (((x) > 0)? (x): (-(x)))#defineMOD 1000000007#definePi ACOs (-1.0)using namespaceStd;typedefLong Longll; typedef unsignedLong Longull; typedef unsignedint        UINT; typedef unsignedCharUchar; template<classT> InlinevoidCheckmin (T &a,t b) {if(a>b) a=b;} Template<classT> InlinevoidCheckmax (T &a,t b) {if(a<b) a=b;}Const DoubleEPS = 1e-7      ;Const intN = About            ;Const intM =1100011*2      ;Constll P =10000000097ll;Const intMAXN =10900000    ;intCNT[MAXN],A[MAXN];BOOLCHECK[MAXN];voidInit_prime () { for(inti =2; i < MAXN; ++i) {//Prime number playing table        if(!Check[i]) {             for(intj = i; J < Maxn; J + =i) {Check[j]=true; Cnt[i]+=A[j]; }        }    }}intMain () {Std::ios::sync_with_stdio (false); intI, J, T, K, U, V, numcase =0; intN, q, x, y; CIN>>N;  for(i =1; I <= N; ++i) {cin>>x; ++A[x];    } init_prime ();  for(i =2; i < MAXN; ++i) {Cnt[i]+ = Cnt[i-1];//accumulate and save time to query} cin>>T;  while(t--) {cin>> x >>y; Checkmin (x,10000000); Checkmin (Y,10000000); cout<< Cnt[y]-cnt[x-1] <<Endl; }    return 0;}

Codeforces 385CBear and Prime Numbers Prime table

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.