zoukankan      html  css  js  c++  java
  • HDU-5317

    RGCDQ

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


    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
    2
    2 3
    3 5
    Sample Output
    1
    1
     
    Source
     
    /**
        题意:给出一个区间,然后求区间的数的质因子个数之间的最大公约数
        思路:枚举2~N之间的每个数的质因子的个数,计算L ~ R 之间的数的
              质因子的个数,因为之前打过表,2~N之间的最大的质因子个数是7
              所以,计算L ~ R 之间的1~7出现的次数,然后进行统计枚举,
              被自己蠢dao了
    **/
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <stdio.h>
    #include <string.h>
    #define maxn 1000000+10
    using namespace std;
    int num[maxn];
    int sum[maxn][10];
    int mmap[10];
    int temp[20];
    void init()
    {
        memset(num,0,sizeof(num));
        memset(sum,0,sizeof(sum));
        for(int i=2;i<maxn;i++)
        {
            if(num[i]) continue;
            num[i] = 1;
            for(int j=2;j*i<maxn;j++)
            {
                num[j*i] ++;
            }
        }
        sum[2][1] = 1;
        for(int i=3;i<maxn;i++)
        {
            for(int j=1;j<=7;j++)
            {
                sum[i][j] = sum[i-1][j];
            }
            sum[i][num[i]]++;
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int T;
        scanf("%d",&T);
        init();
        while(T--)
        {
            int left,right;
            scanf("%d %d",&left,&right);
            int cet = 0;
            memset(temp,0,sizeof(temp));
            memset(mmap,0,sizeof(mmap));
            for(int i=1;i<=7;i++)
            {
                mmap[i] = sum[right][i] - sum[left][i];
                if(num[left] == i)mmap[i]++;
                if(mmap[i] >= 2)
                {
                    temp[cet++] = i;
                    temp[cet++] = i;
                }
                else if(mmap[i] == 1)
                    temp[cet++] = i;
            }
            int mmax = 0;
            for(int i=0;i<cet;i++)
            {
                for(int j=i+1;j<cet;j++)
                {
                    mmax = max(mmax,__gcd(temp[i],temp[j]));
                }
            }
            printf("%d
    ",mmax);
        }
        return 0;
    }
  • 相关阅读:
    top、ps -ef、ps aux的区别及内容详解
    img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案
    Scrapy命令行调用传入自定义参数
    查询Linux CPU架构
    LeetCode 216. 组合总和 III | Python
    LeetCode 40. 组合总和 II | Python
    LeetCode 39. 组合总和 | Python
    LeetCode 77. 组合 | Python
    LeetCode 347. 前 K 个高频元素 | Python
    LeetCode 107. 二叉树的层次遍历 II | Python
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4684579.html
Copyright © 2011-2022 走看看