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
题目大意:定义函数F(x)为x分解为质因数乘积中质因数的个数。对于给定的区间[l,r],求出maxGCD(F(i),F(j))(l<=i<j<=r)的值。 

解题思路:预处理出1000000以内的F(x)可以发现,F(x)的取值是1,2,3,4,5,6,7。而求GCD时,一定是这几个数的组合。那么CGD的结果无非是1,2,3,4,5,6,7。如果我们知道区间[l,r]内,7的值有多少;6的值有多少;5的值有多少;4的值有多少;3的值有多少;2的值有多少;1的值有多少,那么当7的值的个数大于等于2时,结果一定是7;当7的个数小于2,而6的个数大于等于2时,结果一定是6;……………………。因此只要求出区间[l,r]内,1,2,3,4,5,6,7的个数即可,如何求解!!!我们假设s[i][j]的值表示2到i内,F(x)=j(2<=x<=i)的个数,那么区间[l,r]内,7的个数为s[r][7]-s[l-1][7];6的个数为s[r][6]-s[l-1][6];………………,1的个数为s[r][1]-s[l-1][1]

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-4)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1e9+7
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
#define MAX 1000005
int f[MAX];
int s[MAX][10];
int sum[10];
int main()
{
    int i,j,n,x;
    memset(f,0,sizeof(f));//初始化
    for(i=2;i<MAX;i++)//遍历2到n个数,因为这些数可能是素因子
    {
        if(f[i]==0)//判断是否是素因子
        {
            for(j=i;j<MAX;j=j+i)//寻找以i为素因子的数
            {
                f[j]++;
            }
        }
    }
    //利用递推公式s[i][j]=s[i][j]+s[i-1][j]求解s[i][j]
       s[2][1]=1;
       for(int i=1;i<MAX;i++)
       {
           s[1][i]=0;
       }
    for(int i=2;i<=MAX;i++)
    {
        for(int j=1;j<=7;j++)
        {
            if(f[i]==j)
            s[i][j]=s[i-1][j]+1;
            else
                s[i][j]=s[i-1][j];
        }
    }
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=7;i++)
        {
            sum[i]=s[m][i]-s[n-1][i];
        }
        if(sum[7]>=2)
        {
            printf("7\n");
            continue;
        }
        if(sum[6]>=2)
        {
            printf("6\n");
            continue;
        }
        if(sum[5]>=2)
        {
            printf("5\n");
            continue;
        }
       if(sum[4]>=2)
       {
           printf("4\n");
           continue;
       }
     if(sum[3]>=2||(sum[3]>=1&&sum[6]>=1))
       {
           printf("3\n");
           continue;
       }
     if(sum[2]>=2||(sum[2]>=1&&sum[4]>=1)||(sum[2]>=1&&sum[6]>=1)||(sum[4]>=1&&sum[6]>=1))
        {
            printf("1\n");
            continue;
        }
        printf("1\n");

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值