hdu 5317 (普通素数筛的应用)+(前缀和预处理)

来源:互联网 发布:软件测评收费 编辑:程序博客网 时间:2024/05/29 09:14

RGCDQ 

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2917    Accepted Submission(s): 1126


Problem Description
Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more interesting things about GCD. Today He comes up with Range Greatest Common Divisor Query (RGCDQ). What’s RGCDQ? Please let me explain it to you gradually. For a positive integer x, F(x) indicates the number of kind of prime factor of x. For example F(2)=1. F(10)=2, because 10=2*5. F(12)=2, because 12=2*2*3, there are two kinds of prime factor. For each query, we will get an interval [L, R], Hdu wants to know maxGCD(F(i),F(j)) (Li<jR)
 

Input
There are multiple queries. In the first line of the input file there is an integer T indicates the number of queries.
In the next T lines, each line contains L, R which is mentioned above.

All input items are integers.
1<= T <= 1000000
2<=L < R<=1000000
 

Output
For each query,output the answer in a single line. 
See the sample for more details.


Sample Input
22 33 5
 

Sample Output
11
 
题目传送门:点击打开链接
根据题意f(i)代表整数i的质因子的个数。普通的素数筛是把得到的质数的倍数都标记一遍,然而这道题是求一个数的质因子个数,那么我们得到的质因子是不是就是这个质数所有倍数的一种质因子。所以我们把标记c[i]=1改成++c[i], 即c[i]的含义是i的质因子的种类也同时代表i不是质数。
本以为在用一下暴力就可以解决这道题,但是tle了,仔细看一下题t <=1e6再加上r<=1e6最坏的情况运算1e12次。
没有办法了,于是就想到是不是有规律的想法  我竟然把2-1e6的c[i]打了出来,本以为c[i]的最大值非常大,一看最大值竟然是7。
所以我就想到遍历一下l-r用一个数组记录c[i]的出现次数,然后暴力1-7,但是还是tle。
看别人的标题写着前缀和,我就很不明白为什么能用到前缀和,想了很久以前做题用到前缀和都是一维的,然而这有7个数,突然在课上我想到可以二维啊这样应该也不会出现超内存的问题,所以写了之后ac了。写到这  想到7个一维数组也是可以的。。。。tsum[i][j]其中1<=j<=7 表示从2-i中间整数  j(整数的质因子个数)    出现的次数,当j出现两次时那么gcd等于j,还有(2,4)=2,(3,6)=3。。 中间l-r j出现的次数=tsum[r][j]-tsum[l-1][j]
在做这题的期间 发现了有趣的现象   在hdu oj上关云长 那些提交的人   原来是在vjudge 提交hdu的题在hdu显示关云长等名称
#include <iostream>#include <cstring>#include <cstdio>#define maxn 1000100using namespace std;int n,m,c[maxn],tsum[maxn][8];void getprime(){    memset(c,0,sizeof(c));    for(int i=2;i<=1e6;++i)    {        if(!c[i])        {            ++c[i];            for(int j=i+i;j<=1e6;j+=i)                ++c[j];        }    }    for(int i=1;i<=7;++i)        tsum[1][i]=0;    for(int i=2;i<=1e6;++i)    {        tsum[i][c[i]]=tsum[i-1][c[i]]+1;        for(int j=1;j<=7;++j)            if(j!=c[i])                tsum[i][j]=tsum[i-1][j];       // cout<<i<<": ";        //for(int j=1;j<=7;++j)       // cout<<tsum[i][j]<<" ";        //cout<<endl;    }}int main(int argc, const char * argv[]){    getprime();    int t,l,r;    int data[10]={0};    scanf("%d",&t);    while(t--)    {        int ma=1;        scanf("%d%d",&l,&r);        for(int i=7;i>=2;--i)        {            data[i]=tsum[r][i]-tsum[l-1][i];            if(data[i]>1)            {                ma=max(ma,i);                break;            }        }        if(ma<3&&data[3]&&data[6])            ma=max(ma,3);        if(ma<2&&data[2]&&data[4])            ma=max(ma,2);        printf("%d\n",ma);    }    return 0;}




0 0
原创粉丝点击