2016 Multi-University Training Contest 1 1004 hdu 5726 二分+RMQ
来源:互联网 发布:求仁得仁 又何怨乎 编辑:程序博客网 时间:2024/05/17 04:15
链接:戳这里
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There are Q(Q≤100,000) queries. For each query l,r you have to calculate gcd(al,,al+1,...,ar) and count the number of pairs(l′,r′)(1≤l<r≤N)such that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar).
Input
The first line of input contains a number T, which stands for the number of test cases you need to solve.
The first line of each case contains a number N, denoting the number of integers.
The second line contains N integers, a1,...,an(0<ai≤1000,000,000).
The third line contains a number Q, denoting the number of queries.
For the next Q lines, i-th line contains two number , stand for the li,ri, stand for the i-th queries.
Output
For each case, you need to output “Case #:t” at the beginning.(with quotes, t means the number of the test case, begin from 1).
For each query, you need to output the two numbers in a line. The first number stands for gcd(al,al+1,...,ar) and the second number stands for the number of pairs(l′,r′) such that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar).
Sample Input
1
5
1 2 4 6 7
4
1 5
2 4
3 4
4 4
Sample Output
Case #1:
1 8
2 4
2 4
6 1
思路:
RMQ统计预处理[l,r]内的gcd值
枚举左端点,随着右端点增大,gcd值越来越小,二分这些变小的节点,统计出线段的gcd值所作的贡献,map累加每个贡献的数量
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include<ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;ll a[100100];ll mp[100100][65];ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b);}int n,q;void RMQ_init(){ for(int i=1;i<=n;i++) mp[i][0]=a[i]; int m=log(n)/log(2); for(int i=1;i<=m;i++){ for(int j=n;j>=1;j--){ mp[j][i]=mp[j][i-1]; if(j+(1<<(i-1))<=n) mp[j][i]=gcd(mp[j][i],mp[j+(1<<(i-1))][i-1]); } }}int query(int l,int r){ int m=log(r-l+1)/log(2); return gcd(mp[l][m],mp[r-(1<<m)+1][m]);}map<int,ll>V;int main(){ int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++){ scanf("%d",&n); V.clear(); for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); RMQ_init(); for(int i=1;i<=n;i++){ ll tmp=a[i]; int l=i,r=n,mid,ans=l,O=l; while(tmp>1){ O=l;r=n;ans=-1; while(l<=r){ mid=(l+r)/2; if(query(i,mid)<tmp) r=mid-1; else { l=mid+1; ans=mid; } } if(tmp==1) break; V[tmp]+=ans-O+1; l=ans+1; if(l>n) break; tmp=query(i,l); } if(tmp==1){ V[1]+=n-l+1; } } scanf("%d",&q); printf("Case #%d:\n",cas); while(q--){ int l,r; scanf("%d%d",&l,&r); ll x=query(l,r); printf("%I64d %I64d\n",x,V[x]); } } return 0;}
0 0
- 2016 Multi-University Training Contest 1 1004 hdu 5726 二分+RMQ
- RMQ+二分——GCD ( HDU 5726 ) ( 2016 Multi-University Training Contest 1 1004 )
- 2016 Multi-University Training Contest 1 GCD【RMQ+二分】
- 2016 Multi-University Training Contest 1 1004 GCD HDU 5726 (RMQ)
- 2016 Multi-University Training Contest 1-1004---HDU 5726 GCD
- hdu 5289 Assignment 二分+RMQ 2015 Multi-University Training Contest 1 02
- hdu 5726 2016 Multi-University Training Contest 1(二分+dp)
- (HDU 5726)2016 Multi-University Training Contest 1 GCD(数学)
- 2016 Multi-University Training Contest 1-1005---HDU 5727 Necklace(枚举+二分图匹配)
- 2014 Multi-University Training Contest 1 - 1004 / hdu 4864 Task
- (HDU 5733)2016 Multi-University Training Contest 1 tetrahedron(几何)
- 2016 Multi-University Training Contest 1 1002 hdu 5724 博弈
- 2016 Multi-University Training Contest 1 C Game(hdu 5725)
- HDU 5724 Chess (from: 2016 Multi-University Training Contest 1)
- hdu 5732 subway(2016 Multi-University Training Contest 1 1010)
- (HDU 5728)2016 Multi-University Training Contest 1 PowMod(数论)
- 【Hdu 5723】Abandoned country【2016 Multi-University Training Contest 1】
- hdu 5289 - Assignment(2015 Multi-University Training Contest 1 )单调队列+RMQ+树状数组
- C++11中的匿名函数(lambda函数,lambda表达式)
- malloc、calloc、realloc函数剖析
- 最长子序列和Maximum Continuous Partial SumST(AC)
- 图片上传
- java 获取系统中默认的编码
- 2016 Multi-University Training Contest 1 1004 hdu 5726 二分+RMQ
- 在oracle中一次执行多条语句
- RGB图像语义分割前沿:CRF as RNN
- IOS警告imgName' used as the name of the previous parameter rather than as part of the selector
- PBR贴图转换2——PBR 作了哪些改变
- 利用ffmpeg和opencv进行视频的解码播放
- oracle+关于"SP2-0618:+无法找到会话标识符。启用检查+PLUSTRACE+角色"错误
- ListView中点击冲突解决方法
- java使用cookie和session