RGCDQ(求质因子个数+规律)

171 篇文章 0 订阅
79 篇文章 0 订阅


Link:http://acm.hdu.edu.cn/showproblem.php?pid=5317


RGCDQ

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


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
 



AC code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#define LL long long
#define MAXN 1000010
using namespace std;
int n,k,i,j,mm;
int f[1000010];
LL ans=0;
int gcd(int a,int b)
{
	if(a<b)
		swap(a,b);
	if(b==0)
		return a;
	return gcd(b,a%b);
}
int cnt[8];
int s[1000001][8];
void init()
{
	memset(f,0,sizeof(f));
	memset(s,0,sizeof(s));
	for(int i=2;i<=1000000;i++) // 确保i为素数,i必须从2开始,不能从1开始!!! 
	{
		if(f[i])//筛除合数 ,确保i为素数 
			continue;
		f[i]=1;
		for(j=2;j*i<=1000000;j++)//j*i为以i为质因子的数 
		{
			f[j*i]++;
		}
	}
//	s[2][1]=1;
	memset(cnt,0,sizeof(cnt));
	for(int i=1;i<=1000000;i++)
	{
     	cnt[f[i]]++;
		for(j=1;j<=7;j++)
		{
			s[i][j]=cnt[j];
		//	s[i][j]=s[i-1][j];
		}
		//s[i][f[i]]++;
	}	
}
int main()
{
	init();
	//freopen("D:\in.txt","r",stdin);	
	int T,st,ed,ans,i,j;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&st,&ed);
		int c[8];
		memset(c,0,sizeof(c));
		ans=0;
		int num=0;
		int a[22];//存储区间各个质因子的个数的值 
		for(i=1;i<=7;i++)
		{
			c[i]=s[ed][i]-s[st][i];
			if(i==f[st])
				c[i]++;
			if(c[i]>=2)
			{
				a[++num]=i;
				a[++num]=i;
			}
			else if(c[i]==1)
			{
				a[++num]=i;
			}
		}		
		for(i=1;i<=num-1;i++)
		{
			for(j=i+1;j<=num;j++)
			{
				ans=max(ans,gcd(a[i],a[j]));
			}
		}	
		printf("%d\n",ans);
	}
	return 0;
 } 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值