Codeforces 385 C Bear and Prime Numbers

來源:互聯網
上載者:User

標籤:blog   http   io   os   ar   for   strong   sp   2014   

題目連結~~>

做題感悟:這題屬於想法題,比賽時直接做的 D 題,但是處理座標處理的頭暈眼花的結果到最後也沒AC。

解題思路:

               因為查詢的時候只考慮素數,so~我們只考慮素數就可以,這就需要篩素數,我們可以在篩素數的同時把某個素數出現的倍數加上,輸入的時候只要記錄某個數的個數就可以了。

代碼:

#include<iostream>#include<sstream>#include<map>#include<cmath>#include<fstream>#include<queue>#include<vector>#include<sstream>#include<cstring>#include<cstdio>#include<stack>#include<bitset>#include<ctime>#include<string>#include<iomanip>#include<algorithm>using namespace std  ;#define INT long long intconst int INF = 0x3f3f3f ;const double esp = 0.0000000001 ;const double PI = acos(-1.0) ;const int mod = 1000000007 ;const int MY = 100 + 5 ;const int MX = 10000000 + 5 ;int Max ,n ,m ;bool isprime[MX] ;int sum[MX] ,num[MX] ;void init() // 篩法同時記錄個數{    memset(isprime ,false ,sizeof(isprime)) ;    memset(sum ,0 ,sizeof(sum)) ;    for(int i = 2 ;i <= Max ; ++i)    {        sum[i] += sum[i-1] ;        if(!isprime[i])        {            sum[i] += num[i] ;            for(int j = i + i ;j <= Max ; j += i)            {                sum[i] += num[j] ;                isprime[j] = true ;            }        }    }}int main(){    int x ;    while(~scanf("%d" ,&n))    {        memset(num ,0 ,sizeof(num)) ;        Max = 0 ;        for(int i = 0 ;i < n ; ++i)        {            scanf("%d" ,&x) ;            num[x]++ ;         // 記錄個數            Max = max(Max ,x) ;        }        init() ;        scanf("%d" ,&m) ;        int le ,rt ;        for(int i = 0 ;i < m ; ++i)        {            scanf("%d%d" ,&le ,&rt) ;            if(rt > Max)  rt = Max ;            if(le > Max)  cout<<"0"<<endl ;            else      cout<<sum[rt]-sum[le-1]<<endl ;        }    }    return 0 ;}




Codeforces 385 C Bear and Prime Numbers

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.