Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:复杂度卡得有点过分了,不到1e6的复杂度过不了

Posted by ALLACS at 2017-10-08 18:46:32 on Problem 2739
In Reply To:复杂度卡得有点过分了,不到1e6的复杂度过不了 Posted by:ALLACS at 2017-10-06 12:32:44
//尺取法
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2000;
const int maxn2=10000+5;
bool p[maxn2];
int prime[maxn];
int sum[maxn];

int cnt=0;                                                                 //素数个数

//素数筛

void is_prime()
{
    memset(p,true,sizeof(p));
    memset(prime,0,sizeof(prime));
    p[0]=p[1]=false;
    for(int i=2;i<=maxn2;i++)
    {
        if(p[i]) {
                     prime[cnt++]=i;
                     for(int j=2*i;j<=maxn2;j+=i)
                           p[j]=false;
                  }
    }

}

//求sum数组
void quick_sum()                        //得到sum数组,sum数组的大小为1230
{
    memset(sum,0,sizeof(sum));
    for(int i=0;i<cnt;i++)
         sum[i+1]=sum[i]+prime[i];
}
int main()
{
    is_prime();
    quick_sum();
    int n;
    while(scanf("%d",&n)==1&&n)
    {
        int t=0;
        for(int e=0;sum[e]+n<=sum[cnt];e++)
        {
            int k=lower_bound(sum+e,sum+cnt+1,sum[e]+n)-sum;
            if(sum[k]==sum[e]+n) t++;
        }
        cout<<t<<endl;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator