Codeforces 385C Bear and Prime Numbers [Prime Sieve Method]

Source: Internet
Author: User




Code:

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < algorithm> #include <vector> #include <string> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #define INF 0x7fffffff#define SUP 0x80000000#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;const int n=1000007;int tot;int pri[n];bool valid[10*n];    int s[n];int cnt[10*n];void Getpri () {mem (valid,true);    MEM (s,0);    Valid[1]=false;    tot=0;            for (int i=2;i<=10000000;i++) {if (Valid[i]) {pri[++tot]=i;  S[tot]=s[tot-1]+cnt[i];   Maintain prefix and for (int j=i+i;j<=10000000;j+=i) {s[tot]+=cnt[j];            Using the nature of sieve method Valid[j]=false; }}}//for (int i=1;i<=10;i++)//printf ("%d%d\n", Pri[i],s[i]);}    int main () {int n,m,v;    scanf ("%d", &n); for (int i=0;i<n;i++) {ScanF ("%d", &v);    cnt[v]++;    } getpri ();    Pri[++tot]= (1<<31-1);    int l,r,x,y;    scanf ("%d", &m);        for (int i=0;i<m;i++) {scanf ("%d%d", &l,&r);        X=lower_bound (pri+1,pri+tot,l)-pri;        Y=upper_bound (pri+1,pri+tot,r)-pri-1;        cout<<x<< "^" <<y<<endl;        if (x<=y) printf ("%d\n", s[y]-s[x-1]);    else printf ("0\n"); } return 0;} All right, =_=!. Here is my brain residue code (line Sun + composite decomposition + Tree-like tree + two points)//right vote when the template is used.    /* int Pri[n];bool valid[10000007];int dp[n];int c[n];int tot;void getpri () {mem (valid,true);    Tot=0;valid[1]=false;        for (int i=2;i<=10000000;i++) {if (valid[i]) pri[++tot]=i; for (int j=1; (j<=tot) && (i*pri[j]<=10000000); j + +) {valid[i*pri[j]]=false;        Ïê¼ûºìiutyuop¡¾if (i%pri[j]==0) break;    }//cout<< "--" <<endl; }}int lowbit (int x) {return x& (-X);} void Add (int x,int v) {for (int i=x;i<=tot;i+=lowbit (i))       C[i]+=v;}    int sum (int x) {if (dp[x]!=-1) return dp[x];    int ret=0;    for (int i=x;i>0;i-=lowbit (i)) ret+=c[i];    Dp[x]=ret; return ret;}        void Pribreak (int x) {for (int i=1;i<=tot;i++) {if (x%pri[i]==0) {Add (i,1);        } while (x%pri[i]==0) {x/=pri[i];    } if (x==1) return;    }}int Main () {int n,m;    Getpri ();    Pri[++tot]= (1<<31-1);        while (scanf ("%d", &n) ==1) {int A;        MEM (c,0);        MEM (dp,-1);            for (int i=0;i<n;i++) {scanf ("%d", &a);        Pribreak (a);        }//for (int i=1;i<=10;i++)//printf ("%d%d--\n", pri[i],c[i]);        scanf ("%d", &m);        int l,r,x,y;            for (int i=0;i<m;i++) {scanf ("%d%d", &l,&r);            X=lower_bound (pri+1,pri+tot,l)-pri;            Y=upper_bound (pri+1,pri+tot,r)-pri-1; if (x<=y) printf ("%d\n", Sum (y)-SUm (x-1));        else printf ("0\n"); }} return 0;} */


Codeforces 385C Bear and Prime Numbers [Prime Sieve Method]

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.