codeforces 475D CGCDSSQ ST表+二分
来源:互联网 发布:人事管理数据流程图 编辑:程序博客网 时间:2024/04/18 15:03
点击打开链接链接
Given a sequence of integers a1, ..., an and q queries x1, ..., xq on it. For each query xi you have to count the number of pairs (l, r)such that 1 ≤ l ≤ r ≤ n and gcd(al, al + 1, ..., ar) = xi.
is a greatest common divisor of v1, v2, ..., vn, that is equal to a largest positive integer that divides all vi.
The first line of the input contains integer n, (1 ≤ n ≤ 105), denoting the length of the sequence. The next line contains n space separated integers a1, ..., an, (1 ≤ ai ≤ 109).
The third line of the input contains integer q, (1 ≤ q ≤ 3 × 105), denoting the number of queries. Then follows q lines, each contain an integer xi, (1 ≤ xi ≤ 109).
For each query print the result in a separate line.
32 6 3512346
12201
710 20 3 15 1000 60 16101234561020601000
14022202211
问有几对l r使得从l到r的gcd为x
用线段树会T
ST表 然后二分
代码如下
#include<cstdio>#include<cstring>#include<algorithm>#include<map>#define MAXN 111111using namespace std;int n,stgcd[MAXN][35],num[MAXN];int prelog2[MAXN];map<int ,long long > ans;int gcd(int a,int b){ if(b==0)return a; return gcd(b,a%b);}void init(){ prelog2[1]=0; for(int i=2;i<=n;i++){prelog2[i]=prelog2[i-1];if((1<<prelog2[i]+1)==i) prelog2[i]++; } for(int i=n;i>=1;i--){stgcd[i][0]=num[i];for(int j=1;(i+(1<<j)-1)<=n;j++){ stgcd[i][j]=gcd(stgcd[i][j-1],stgcd[i+(1<<j-1)][j-1]);} }}int getgcd(int l,int r){ int len=r-l+1; return gcd(stgcd[l][prelog2[len]],stgcd[r-(1<<prelog2[len])+1][prelog2[len]]);}void solve(){ for(int i=1;i<=n;i++){int ed=i,l,r,m,pos;while(ed<=n){ int xx=getgcd(i,ed); l=ed,r=n,pos=-1; while(l<=r){if(r-l<=1){ if(getgcd(i,r)==xx)pos=r; else if(getgcd(i,l)==xx)pos=l; break;}m=l+r>>1;if(getgcd(i,m)==xx) l=m;else r=m; } ans[xx]+=pos-ed+1; ed=pos+1;} }}int main(){ int q,xx; scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&num[i]); init(); solve(); scanf("%d",&q); while(q--){scanf("%d",&xx);printf("%I64d\n",ans[xx]); } return 0;}
- codeforces 475D CGCDSSQ ST表+二分
- [Codeforces#475D]CGCDSSQ(st表+二分+数学相关)
- codeforces 475D CGCDSSQ (st表+数论)
- 【codeforces】 475D CGCDSSQ
- Codeforces 475 D. CGCDSSQ
- CodeForces 475D CGCDSSQ
- CodeForces 475D CGCDSSQ
- CodeForces 475D CGCDSSQ RMQ
- Codeforces 475D CGCDSSQ 题解
- codeforces 689D ST表+二分 模板
- 【CODEFORCES】 D. CGCDSSQ
- codeforces 689D Friends and Subsequences st表+二分
- 【并查集分块】Codeforces 475D CGCDSSQ
- codeforces Round #361 D. Friends and Subsequences (ST表,二分)
- Codeforces 689D. Friends and Subsequences(二分/尺取 + ST表)
- ST表与二分 (CodeForces 689D-Friends and Subsequences)
- CodeForces 359 D.Pair of Numbers (二分+ST)
- Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
- saveHierarchyState问题
- 状态栏显示
- 第17周项目3 胖子伤不起
- floodlight路由机制分析
- linux cmp命令参数及用法详解--比较两个文件
- codeforces 475D CGCDSSQ ST表+二分
- Win7 NFS 设置详解
- 消除warning:PerformSelector may cause a leak because its selector is unknown
- R语言之RMysql 包安装
- 1412200906-hd-矩形A+B
- 12.Hibernate双向基于链表的1-N映射
- 八方位位置判断代码——1
- Ubuntu14.04如何备份和恢复系统
- C++学习笔记之三(C++语句基础之表达式与语句)