HDU 5726 GCD
来源:互联网 发布:高尔夫gte数据 编辑:程序博客网 时间:2024/04/29 14:04
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 958 Accepted Submission(s): 288
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 numberN , denoting the number of integers.
The second line containsN integers, a1,...,an(0<ai≤1000,000,000) .
The third line contains a numberQ , denoting the number of queries.
For the nextQ lines, i-th line contains two number , stand for the li,ri , stand for the i-th queries.
The first line of each case contains a number
The second line contains
The third line contains a number
For the next
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 forgcd(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) .
For each query, you need to output the two numbers in a line. The first number stands for
Sample Input
151 2 4 6 741 52 43 44 4
Sample Output
Case #1:1 82 42 46 1
大致题意就是,给出一串数列,问你L到R之间的GCD,并要求出整个串中有几个子串的DCG和L到R的GCD相同。
用ST数组存放区间GCD值,用MAP存放整个序列中GCD为I的子串个数。
从L起始,向右延伸的子串,其GCD值都是单调非增的、所以可以枚举起点用二分来快速找到每个GCD对应的区间。
而数的范围是1e9,所以每个数的质因子个数最多有log(1e9)个
#include <iostream>#include <cstdio>#include <cstring>#include <map>#define LL long longusing namespace std;int st[100000+500][20];int num[100000+500];int n,m;map<int,LL>mp;int __gcd(int a,int b){ while(b) { int t=a%b; a=b; b=t; } return a;}void init(){ for(int i=1;i<=n;i++) st[i][0]=num[i]; for(int j=1;(1<<j)<=n;j++) { for(int i=1;i+(1<<j)-1<=n;i++) { st[i][j]=__gcd(st[i][j-1],st[i+(1<<(j-1))][j-1]); } }}int find_gcd(int l,int r){ int i=0; while((1<<(i+1))<=r-l+1) i++; return __gcd(st[l][i],st[r-(1<<i)+1][i]);}int main(){ int t,tt=1; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&num[i]); init(); scanf("%d",&m); for(int i=1;i<=m;i++) { int l,r; scanf("%d%d",&l,&r); if(l>r) swap(l,r); num[i]=find_gcd(l,r); mp[num[i]]=0; } for(int i = 1;i<=n;i++) { int np = i; while(np<=n) { int sum = find_gcd(i,np); int l = np,r = n; int cnt = 0; while(l<=r) { int mid = (l+r)/2; if(find_gcd(i,mid)==sum) { l = mid+1; cnt = mid; } if(find_gcd(i,mid)<sum) r = mid-1; else l = mid+1,cnt=mid; } mp[sum]+=cnt-np+1; np = r+1; } } printf("Case #%d:\n",tt++); for(int i=1;i<=m;i++) { printf("%d %lld\n",num[i],mp[num[i]]); } } return 0;}
0 0
- HDU 5726 GCD
- 多校&&HDU 5726 GCD
- HDU 5726 GCD RMQ
- HDU-5726 GCD
- HDU 5726 GCD
- HDU 5726-GCD
- HDU 5726 GCD
- HDU-5726 GCD
- HDU 5726 GCD
- hdu 5726(区间gcd)
- HDU 5726 GCD
- HDU 5726 GCD
- HDU 5726 - GCD
- HDU 5726 GCD
- HDU 5726 GCD 倍增
- hdu 5726 GCD (区间gcd-RMQ)
- HDU 5726 GCD [RMQ+二分]
- HDU 5726 GCD(RMQ + 二分)
- 安卓APK免杀教程
- Droid Plugin首页、文档和下载
- 黑客技术,
- theano卷积神经网络实现
- UWP项目中加载svg矢量图
- HDU 5726 GCD
- http协议要点二
- HDU1213
- Spring源码解析(一) Spring事务控制之Hibernate
- [leetcode] 238. Product of Array Except Self
- 【Android基础知识】Android多线程下载
- ajax请求json数据
- 初识DataFrames
- canvas arcTo()用法详解