hdu 5042 GCD pair(数论 + 二分)
2014-09-28 14:44
501 查看
题意:给出n个数,定义F(l,r)为区间[l,r]的最大公约数。给定(l,r)求(k1,k2),或者给定(k1,k2)求(l,r)。k1定义为有所有的F值中有多少个不同的值小于等于F(l,r),k2定义为等于F(a,b)的值等于F(l,r)的pair(a,b)中,小于等于pair(l,r)的个数。
思路:首先要统计F(l,r)的值,由于确定一个l的话,那么对于r来说F(l,r)是非递增的,因此,可以分段去统计l值确定时的F值,那么可以用一个结构体来保存:pos为左边界的位置,l,r为右边界的范围,gval表示区间[pos,x]的F值都为gval(l<=x <= r),然后根据F的值来排序。接下来预处理一些东西就可以二分随便搞了,sum[i]表示前i个结点所有的pair的数量,cnt[i]表示前i个结点所有的不同的值的数量。
代码:
思路:首先要统计F(l,r)的值,由于确定一个l的话,那么对于r来说F(l,r)是非递增的,因此,可以分段去统计l值确定时的F值,那么可以用一个结构体来保存:pos为左边界的位置,l,r为右边界的范围,gval表示区间[pos,x]的F值都为gval(l<=x <= r),然后根据F的值来排序。接下来预处理一些东西就可以二分随便搞了,sum[i]表示前i个结点所有的pair的数量,cnt[i]表示前i个结点所有的不同的值的数量。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #include<stack> #include<set> #include<cmath> #include<vector> #include<bitset> #define inf 0x3f3f3f3f #define Inf 0x3FFFFFFFFFFFFFFFLL #define eps 1e-6 #define pi acos(-1.0) using namespace std; typedef long long ll; const int maxn = 100000 + 10; struct Node { int pos,l,r,gval; Node(int pos = 0,int l = 0,int r = 0,int 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; } }node[maxn*20]; ll sum[maxn*20]; int cnt[maxn*20]; int a[maxn],pa[maxn],pre[maxn],val[maxn],n,tot; int gcd(int a,int b) { return b == 0?a:gcd(b,a%b);} vector<Node>vt[maxn]; void precal() { for(int i = 0;i <= n;++i) vt[i].clear(); vt .push_back(Node(n,n,n,a )); Node ntmp; int x; for(int i = n - 1;i >= 1;--i) { int size = vt[i + 1].size(); int 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(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; cnt[1] = 1; for(int i = 2;i <= tot;++i) cnt[i] = cnt[i-1] + (node[i].gval != node[i-1].gval); // for(int i = 1;i <= tot;++i) // { // cout<<node[i].pos<<" "; // cout<<node[i].l<<" "; // cout<<node[i].r<<" "; // cout<<node[i].gval<<" "; // cout<<endl; // } } int f(int l,int r) { int L = 0,R = vt[l].size() - 1; while(L < R) { int m = (L + R)>>1; // cout<<vt[l][m].r<<endl; if(vt[l][m].r < r) L = m + 1; else R = m; } return vt[l][L].gval; } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); char str[20]; int t,tcase = 0,Q; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&Q); for(int i = 1;i <= n;++i) scanf("%d",&a[i]); precal(); ll x,y,ans1,ans2; Node tmp; printf("Case #%d:\n",++tcase); while(Q--) { scanf("%s",str); scanf("%I64d%I64d",&x,&y); if(str[0] == 'S') { if(cnt[tot] < x) { puts("-1"); continue; } int L = lower_bound(cnt + 1,cnt + tot + 1,x) - cnt; int R = upper_bound(cnt + 1,cnt + tot + 1,x) - cnt - 1; if(sum[R] - sum[L - 1] < y) { puts("-1"); continue; } int pos = lower_bound(sum + L,sum + R + 1,y + sum[L-1]) - sum; ll tx = sum[pos] - sum[L - 1] - y; ans1 = node[pos].pos; ans2 = node[pos].r - tx; printf("%I64d %I64d\n",ans1,ans2); } else { int val = f(x,y),p1,p2; tmp = Node(-inf,0,0,val); p1 = lower_bound(node + 1,node + tot + 1,tmp) - (node ); ans1 = cnt[p1]; tmp = Node(x,x,y,val); p2 = lower_bound(node + p1,node + tot + 1,tmp) - node; ans2 = sum[p2 - 1] - sum[p1 - 1] + y - node[p2].l + 1; printf("%I64d %I64d\n",ans1,ans2); } } } return 0; }
相关文章推荐
- HDU 5042 GCD pair 预处理+二分 分段
- HDU 5042 GCD pair 预处理+二分 分段
- hdu-5656 CA Loves GCD(dp+数论)
- hdu - GCD-1695 - 欧拉表+容斥 - 数论/组合学
- hdu 1787 GCD Again 欧拉函数小水水 数论
- HDU 5726 GCD (DP+二分)
- GCD HDU - 5726 (ST算法+二分)
- hdu 5726 GCD (二分+ST表)★
- hdu-5726 GCD 思维||二分
- 【HDU 5382】 GCD?LCM! (数论、积性函数)
- hdu 3641 数论 二分求符合条件的最小值数学杂题
- hdu 5780 gcd(线性筛+快速幂+数论)
- HDU 5726 GCD [ST表+暴力二分]【数据结构|杂类】
- HDU 1588(数论,构造二分矩阵+求幂运算+二分求和)
- HDU 5381 The sum of gcd (莫队 + 二分 + 区间GCD特性)
- HDU 5726 GCD (rmq+二分 or 线段树 维护区间gcd)
- HDU 5726 GCD(RMQ+二分)
- Hdu 1695 GCD (数论 容斥原理)
- Hdu 2504 又见GCD【基础数论】
- HDU 4983 Goffi and GCD(数论)