码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-5317

时间:2015-07-29 00:35:49      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

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之间的最大的质因子个数是8
          所以,计算L ~ R 之间的1~7出现的次数,然后进行统计枚举,
          一道很简单的题,被自己蠢哭了
**/
#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\n",mmax);
    }
    return 0;
}

 

HDU-5317

标签:

原文地址:http://www.cnblogs.com/chenyang920/p/4684579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!