hdu 5317 合数分解+预处理
2016-08-21 10:45
211 查看
RGCDQ
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2818 Accepted Submission(s): 1108
[align=left]Problem Description[/align]
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)
[align=left]Input[/align]
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
[align=left]Output[/align]
For each query,output the answer in a single line.
See the sample for more details.
[align=left]Sample Input[/align]
2
2 3
3 5
[align=left]Sample Output[/align]
1
1
/* hdu 5317 合数分解+预处理 problem: 查找区间[l,r]中 gcd(F[a[i]],F[a[j]])的最大值. F[x]为x的分解出的质因子种类数 solve: 可以先计算一下,1e6时质因子最多有7个. 所以可以dp[maxn][7]先预处理出质因子个数的前缀和. 然后查找 1~7谁出现了2次及以上 hhh-2016-08-21 10:38:45 */ #pragma comment(linker,"/STACK:124000000,124000000") #include <algorithm> #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <vector> #include <map> #define lson ch[0] #define rson ch[1] #define ll long long #define clr(a,b) memset(a,b,sizeof(a)) #define key_val ch[ch[root][1]][0] using namespace std; const int maxn = 1000000; const int INF = 1e9+10; int prime[maxn+1]; void getPrime() { memset(prime,0,sizeof(prime)); for(int i = 2;i <= maxn;i++) { if(!prime[i]) prime[++prime[0]] = i; for(int j = 1;j <= prime[0] && prime[j] <= maxn/i;j++) { prime[prime[j]*i] = 1; if(i % prime[j] == 0) break; } } } int getFactor(int x) { int t = x; int fant = 0; for(int i = 1;prime[i] <= t/prime[i];i++) { if(t % prime[i] == 0) { fant ++; while(t % prime[i] == 0) t /= prime[i]; } } if(t != 1) fant ++; return fant; } int dp[maxn+1][7]; int main() { getPrime(); for(int i = 0;i <= 7;i++) dp[0][i] = 0; for(int i = 1;i <= maxn;i++) { int t = getFactor(i); for(int j = 0;j < 7;j++) { if(t == j+1) dp[i][j] = dp[i-1][j] + 1; else dp[i][j] = dp[i-1][j]; } } int T; int a,b; scanf("%d",&T); while(T--) { scanf("%d%d",&a,&b); int tMax = 0; for(int i = 6;i >= 0;i--) { if(dp[b][i] - dp[a-1][i] > 1) { tMax =i; break; } } printf("%d\n",tMax+1); } }
相关文章推荐
- 【数学】Codeforces 707C Pythagorean Triples
- ELF文件解析器支持x86x64ELF文件
- Java基本功面试题
- Android存储系统的架构与设计
- 【Linux】进程等待&程序替换
- leetcode 72 Edit Distance
- JS数组去重
- Ubuntu root 密码忘记-恢复
- Java经典算法题目面试题经常出现的
- Android存储系统的架构与设计
- Android存储系统的架构与设计
- CentOS6.x双网卡采用主-备份策略绑定(bond)
- java.io.IOException Connection Reset by Peer 的原因是什么
- Codeforces Round #368 (Div. 2)(B. Bakery 贪心)
- [转]浅谈TCP/IP网络编程中socket的行为
- 【数学相关、规律】Codeforces 707C Pythagorean Triples
- 软件开发生命周期及文档
- 电子商务基础技术架构
- 一套Java程序员笔试题
- 关于模运算的性质