RGCDQ(求质因子个数+规律)
2015-07-29 11:48
405 查看
Link:http://acm.hdu.edu.cn/showproblem.php?pid=5317
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)) (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
Sample Output
Source
2015 Multi-University Training Contest 3
AC code:
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)) (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
2015 Multi-University Training Contest 3
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; }
相关文章推荐
- 四种主流聚类方法之我见
- Hive 简介及优化策略
- Lua的面向对象——类和继承
- synchronized的JAVA底层实现
- windows + maven + eclipse
- RSA不对称加密,公钥加密私钥解密,私钥加密公钥解密
- 跨域方案:jsonp的使用
- 对于pike 并发在于结构,并行在于执行的思考
- 解决burp suite 使用chrome访问https失真的问题
- Android:Layout_weight的深刻理解
- 再见,OI
- 如何解决“不能打开数据库,用户NT AUTHORITY\NETWORK SERVICE登录失败”的错误呢?
- 2.3-4 递归实现插入排序
- 深入理解jvm学习笔记1
- 类、对象、方法中常见问题
- hdu 4630 线段树+离线处理
- robotium官网
- java包 和 物理目录 解惑(转载)
- [MarsZ]Unity3d游戏开发之Unity3d全策划配置新手指引
- 实现ie6下的居中