题目链接:

pid=5317" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=5317

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)) (L≤i<j≤R)

 
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

题意:

一个函数 :f(x)它的值是x的素因子不同的个数;

如:f(2) = 1, f(3) = 1。

当中(L<=i<j<=R),即区间内随意不相等的两个数的最大公约数的最大值;

PS:

由于2*3*5*7*11*13*17 > 1e6!

所以f(x)的值最大为7;

我们先打表求出每一个f(x)的值;

//int s[maxn][10];//前i个F中j的个数

然后再利用前缀和s[r][i] - s[l-1][i]。

求出区间[l, r]的值。

代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 1000000+7
int prim[maxn];
int s[maxn][10];//前i个F中j的个数
int GCD(int a, int b)
{
if(b==0)
return a;
return GCD(b, a%b);
}
void init()
{
memset(prim, 0, sizeof(prim));
memset(s, 0, sizeof(s));
for(int i = 2; i < maxn; i++)
{
if(prim[i]) continue;
prim[i] = 1;
for(int j = 2; j * i < maxn; j++)
{
prim[j*i]++;//不同素数个数
}
}
s[2][1] = 1;
for(int i = 3; i < maxn; i++)
{
for(int j = 1; j <= 7; j++)
{
s[i][j] = s[i-1][j];
}
s[i][prim[i]]++;
}
}
int main()
{
int t;
int l, r;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&l,&r);
int c[17];
int k = 0;
for(int i = 1; i <= 7; i++)
{
int tt = s[r][i] - s[l-1][i];
if(tt >= 2)//超过两个以上记为2个就可以
{
c[k++] = i;
c[k++] = i;
}
else if(tt == 1)
{
c[k++] = i;
}
}
int maxx = 1;
for(int i = 0; i < k-1; i++)
{
for(int j = i+1; j < k; j++)
{
int tt = GCD(c[i],c[j]);
maxx = max(maxx, tt);
}
}
printf("%d\n",maxx);
}
return 0;
}

最新文章

  1. css-css权威指南学习笔记4
  2. background-image小解
  3. phonegap学习笔记
  4. java 中集合和数组互相转换
  5. 加速chrome之Vimium快捷键
  6. 微信支付官方SDK V3 .NET版的坑
  7. 使用导入导出进行备份和恢复OCR(10g)
  8. MFC读写配置文件
  9. C# openfiledialog设置filter属性后达不到过滤效果的原因之一
  10. 常用Linux命令笔记
  11. 恶补web之二:css知识(3)
  12. bootstrap学习: 折叠插件和面板
  13. unity修改脚本的图标
  14. jQuery Distpicker插件 省市区三级联动 动态赋值修改地址
  15. linux日常命令之三
  16. FLV 封装格式解析
  17. ActiveMQ 的线程池
  18. 【Canal源码分析】TableMetaTSDB
  19. 【WordPress】外网访问WordPress时无法加载样式表CSS
  20. Elasticsearch查询规则(一)match和term

热门文章

  1. html只能有一个id,并且id的值只能是一个
  2. 理解OAuth 2.0 - 阮一峰的网络日志
  3. [Angular] HttpParams
  4. mysql 表的timestamp为自动添加
  5. C#中防止程序多次运行
  6. FTP中的授权规则
  7. Source Insight 3.50.0065使用详解
  8. 每日技术总结:Better-scroll应用于弹出层内容滚动
  9. 3、U-boot的环境变量: bootcmd 和bootargs
  10. Diskpart工具应用两则:MBR/GPT分区转换 &amp;amp; 基本/动态磁盘转换