数论、、gcd

RGCDQ

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


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
 

Author
ZSTU
 

Source
 

Recommend
wange2014


这题还不错,mark一下~

题意:F(x)表示数x的素因子个数,求区间[L,R]中max GCD(F(i),F(j)),(L<=i<j<=R)

分析:由于询问次数T很大,所以对于每次询问几乎要O(1)的时间复杂度求出答案才可以。然而要求max GCD(F(i),F(j)) 应该要遍历L~R才对,,但这样必然超时。对于这种询问次数非常大的题目,一般都是通过预处理记录答案的。虽然L~R非常大,但是通常会有一些特性使得一个非常小的变量就可以遍历所有答案,发现很多题都是这样。。。这题通过观察可知,F(x)非常小,最多只有7。所以可以首先通过筛法算出F(i),然后用f[i][num]来记录数2~i中num(1<=num<=7))的个数。预处理完这些后就可以在每次询问中快速计算答案:首先答案只会是1~7,已知L、R就可知L~R中素因子个数1~7的个数,它们的gcd是很有限的几个,全部手算也行。

code:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = 1e6+10;
int F[N], f[N][8], cnt[8];
int main()
{
    int T, L, R;

    for(int i=2; i<N; i++) if(!F[i])
    {
        for(int j=i; j<N; j+=i)
            F[j]++;
    } //通过筛法算出所有F(x)
    for(int i=2; i<N; i++)
    {
        cnt[F[i]]++; //素因子个数的个数
        for(int j=1; j<=7; j++)
        {
            f[i][j] = cnt[j];
        }
    }
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &L,&R);
        int vis[10]={0}, maxn = 1;
        for(int i=2; i<=7; i++)
        {
            if(f[R][i]-f[L-1][i] >= 2) maxn = max(maxn,i);
            else if(f[R][i]-f[L-1][i] >= 1) vis[i] = 1;
        }
        for(int i=2; i<=7; i++)
        {
            if(!vis[i]) continue;
            for(int j=2; j<=7; j++)
            {
                if(!vis[j] || i == j) continue;
                maxn = max(maxn,__gcd(i,j));
            }
        }
        printf("%d\n", maxn);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值