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

同样是素数筛法,为什么我的266ms,心痛!哪位大侠能把我看看哪里可以节约时间吗?

Posted by guojiale at 2015-06-19 13:06:02 on Problem 3006
#include <stdio.h>
#include <stdlib.h>

int tab[1000000];
int main()
{
    int a,d,n,i,j;
    tab[0]=0;tab[1]=0;
    for(i=2;i<1000000;i++)
        tab[i]=1;
    for(i=2;i<=1000000;i++)
    {
        if(tab[i] == 1)
        {
            for(j=2;j*i<1000000; j++)
            {
                tab[i*j]= 0;
            }
        }
    }

    while(scanf("%d %d %d",&a,&d,&n) != EOF && n)
    {
        int cout=0;
        for(i=0;cout!=n;i++)
        {
            if(tab[a+i*d] == 1)
                cout++;
        }
        printf("%d\n",a+(i-1)*d);
    }
    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