zoukankan      html  css  js  c++  java
  • HDU5317——素数筛——RGCDQ

    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
    2 2 3 3 5
     


    Sample Output
    1 1
     


    Author
    ZSTU
     


    Source
     
    /*素数筛
    然后对所有情况讨论
    */
    
    
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    int flag[1000005];
    int cout[1000005];
    int sum[1000005][10];
    int res[10];
    void inti()
    {
        for(int i = 2; i <= 1000000; i++){
            if(!flag[i]){
                for(int j = i;j <= 1000000; j+=i){
                    flag[j] = 1;
                    cout[j]++;
                }
            }
        }
        for(int i = 0; i <= 7; i++){
            for(int j = 2; j <= 1000000; j++){
                sum[j][i] = sum[j-1][i] + (cout[j] == i + 1);
            }
        }
    }
    
    
    int main()
    {
    inti();
    int T;
    scanf("%d", &T);
    int L, R;
    while(T--){
        scanf("%d%d", &L,&R);
        int ans = 1;
        for(int i = 0 ; i <= 7; i++)
            res[i+1] = sum[R][i] - sum[L-1][i];
            if(res[2] + res[4] + res[6] >= 2) ans = 2;
            if(res[3] + res[6] >= 2) ans = 3;
            if(res[4] >= 2) ans = 4;
            if(res[5] >= 2) ans = 5;
            if(res[6] >= 2) ans = 6;
            if(res[7] >= 2) ans = 7;
            printf("%d
    ", ans);
    }
    return 0;
    }
    

      

  • 相关阅读:
    整型
    圆的面积和周长
    买菜
    keil 生成 bin 文件 gd32为例
    内存中 1k 代表什么
    正反转 步进电机 驱动器 编码器
    stlink 无法再keil中识别 按下复位键可以识别
    单片机的时钟,系统时钟
    db9串口接头的定义
    E面波导和H面波导的问题
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4694183.html
Copyright © 2011-2022 走看看