Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
来源:互联网 发布:淘宝店设置低于7折 编辑:程序博客网 时间:2024/05/01 16:32
题目链接:点击打开链接
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <iostream>#include <cmath>using namespace std;typedef long long ll;template <class T>inline bool rd(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1;}template <class T>inline void pt(T x) { if(x>9) pt(x/10); putchar(x%10+'0');}/////////////////////////const int N = 100000 + 2;struct Node { int pos,l,r; ll gval; Node(int pos = 0,int l = 0,int r = 0,ll gval = 0):pos(pos),l(l),r(r),gval(gval){} bool operator < (const Node & a) const { if(gval != a.gval) return gval < a.gval; if(pos != a.pos) return pos < a.pos; return r < a.r; }};int n, a[N], tot;vector<Node> vt[N];Node node[N * 50];ll sum[N * 50];void prepare() { for(int i = 0;i <= n;++i) vt[i].clear(); vt[n].push_back(Node(n,n,n,a[n])); Node ntmp; int cnt, Size; ll x; for(int i = n - 1;i >= 1;--i) { Size = vt[i + 1].size(); cnt = 1; vt[i].push_back(Node(i,i,i,a[i])); for(int j = 0;j < Size;++j) { ntmp = vt[i+1][j]; x = __gcd((ll)a[i],ntmp.gval); if(cnt && vt[i][cnt-1].gval == x) vt[i][cnt - 1].r = ntmp.r; else vt[i].push_back(Node(i,ntmp.l,ntmp.r,x)),cnt++; } } tot = 0; for(int i = 1;i <= n;++i) for(int j = 0;j < (int)vt[i].size();++j) node[++tot] = Node(vt[i][j]); sort(node + 1,node + tot + 1); sum[0] = 0; for(int i = 1;i <= tot;++i) sum[i] = sum[i-1] + node[i].r - node[i].l + 1;}int hehe;void work(int x) { int L, R, l = 0, r = tot + 1, mid; while (r - l > 1) { mid = (l + r) >> 1; if (node[mid].gval > x) r = mid; else l = mid; } -- r; if (r == 0 || node[r].gval != x) { putchar('0'); putchar('\n'); return ; } R = r; l = 0; r = tot + 1; while (r - l > 1) { mid = (l + r) >> 1; if (node[mid].gval >= x) r = mid; else l = mid; } L = r; pt(sum[R] - sum[L - 1]); putchar('\n');}int main() { rd(n); for (int i = 1; i <= n; ++i) rd(a[i]); prepare(); int Q, x; rd(Q); while (Q -- > 0) { rd(x); work(x); } return 0;}
0 0
- Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
- 【codeforces】 475D CGCDSSQ
- Codeforces 475 D. CGCDSSQ
- CodeForces 475D CGCDSSQ
- CodeForces 475D CGCDSSQ
- CodeForces 475D CGCDSSQ RMQ
- Codeforces 475D CGCDSSQ 题解
- 【莫比乌斯反演-求序列中数字两两互质的对数】ACdreamoj Number theory 1114
- CF 475D CGCDSSQ 枚举,思维+gcd
- HDU 5869 求区间中不同连续序列的gcd的个数(树状数组)
- codeforces 475D CGCDSSQ ST表+二分
- 【CODEFORCES】 D. CGCDSSQ
- codeforce_894C_给你原序列所有连续子序列的GCD从小到大,求原序列_构造
- BZOJ 2301 Problem B(x属于[a,b],y属于[c,d]满足gcd(x,y)=k的(x,y)的有序对数)
- 九度OJ 1534 数组中第K小的数字 && codeforces 448D Multiplication Table
- poj 1019 求一个1到n的序列中,第k个数字是多少
- 找逆序的对数&&和为n的两个数字&&和为n的连续正数序列
- Codeforces 61E Enemy is weak 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组
- C++标准库函数
- UVA - 107 The Cat in the Hat
- leetcode Permutation Sequence
- 使用glassfish在linux上的部署网站
- 在C + +实现
- Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
- IOS 启动画面和图标设置(适配IOS7 and Xcode5)
- [Codevs 1107][NOIP 1107]等价表达式
- php函数
- UI 常用方法总结之--- UIButton UIAlertView (不断更新中)
- 孤独
- css 后代选择器与子选择器的区别
- Java:String和Date、Timestamp之间的转换
- 查看主机上的用户