您的位置:首页 > 其它

STD 2015 hdu5317 打表预处理

2015-07-29 14:00 246 查看


RGCDQ

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1146 Accepted Submission(s): 516



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


暴力 + 预处理 (队友写的 完爆各种大暴力算法)
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
#include <algorithm>
using namespace std;

const int M = 1e6 + 5;
int a[M]= {0};
int pr[M][9];
int z[10]={0};

int gcd (int x,int y)
{
return y==0 ? x : gcd(y,x%y);
}

void init()
{
for(int i = 2; i < M; i++)
{
if(!a[i])
{
for(int j = i + i; j < M; j+=i)
{
a[j]++;
}
a[i] = 1 ;
}
z[a[i]]=i;
pr[i][1]=z[1];
pr[i][2]=z[2];
pr[i][3]=z[3];
pr[i][4]=z[4];
pr[i][5]=z[5];
pr[i][6]=z[6];
pr[i][7]=z[7];
pr[i][8]=z[8];
}
}

int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
int x,y;
scanf("%d%d",&x,&y);
//        printf("%I64d %I64d",a[x],a[y]);
int _max=1;
if (pr[y][2]>=x)
{
if (pr[(pr[y][2]-1)][2]>=x || pr[y][4]>=x || pr[y][6]>=x)
_max=2;
}
if (pr[y][3]>=x)
{
if (pr[(pr[y][3]-1)][3]>=x || pr[y][6]>=x) _max=3;
}
if (pr[y][4]>=x)
{
if (pr[(pr[y][4]-1)][4]>=x) _max=4;
if (_max==1 && pr[y][6]>=x) _max=2;
}
if (pr[y][5]>=x)
if (pr[(pr[y][5]-1)][5]>=x && pr[y][5]>=x) _max=5;
if (pr[y][6]>=x && pr[(pr[y][6]-1)][6]>=x) _max=6;
if (pr[y][7]>=x && pr[(pr[y][7]-1)][7]>=x) _max=7;
printf("%d\n",_max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: