Codeforces 475D CGCDSSQ(分治)
题意:给你一个序列a[i],对于每个询问xi,求出有多少个(l,r)对使得gcd(al,al+1...ar)=xi.
表面上是询问,其实只要处理出每个可能的gcd有多少个就好了,当左端点固定的时候,随着右端点的移动,gcd必然是单调非增的,而且个数不会超过log(a[i])个,所以总的不同的个数的上界是nlog(ai),所以求出所有是可行的。
一个分治的做法是这样的,对于一个区间[l,r],分治成[l,mid],[mid+1,r]求解,然后就是合并,合并的时候首先求以[l,mid]右端点为结束点的gcd,然后是[mid+1,r]的左端点为起始点的gcd,两边for一遍,由于不同的gcd最多只有log(ai)个,所以合并的时候就是log(ai)^2。
所以复杂度大致是这样的 T(n)=2*T(n/2)+log(ai)^2+O(n) 所以大致可以看成是T(n)=2*T(n/2)+O(n),所以复杂度大致就是nlogn的级别的。
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std; #define maxn 110000
#define ll long long
#define MP make_pair int n;
int a[maxn];
map<int,ll> m; int gcd(int a,int b){
return a&&b? gcd(b,a%b):a+b;
} void solve(int l,int r)
{
if(r-l<=3){
for(int i=l;i<=r;++i){
int g=a[i];
for(int j=i;j<=r;++j){
g=gcd(g,a[j]);
m[g]++;
}
}
return; }
int mid=(l+r)>>1;
solve(l,mid);
solve(mid+1,r);
vector<pair<int,ll> > ls;
vector<pair<int,ll> > rs; int cur=-1;
ll cnt=0;
int g=a[mid];
for(int i=mid;i>=l;--i){
g=gcd(g,a[i]);
if(g!=cur) {
if(cur!=-1) ls.push_back(MP(cur,cnt));
cur=g;cnt=1;
}
else{
++cnt;
}
}
ls.push_back(MP(cur,cnt)); cur=-1;cnt=0;g=a[mid+1];
for(int i=mid+1;i<=r;++i){
g=gcd(g,a[i]);
if(g!=cur) {
if(cur!=-1) rs.push_back(MP(cur,cnt));
cur=g;cnt=1;
}
else{
++cnt;
}
}
rs.push_back(MP(cur,cnt)); for(int i=0;i<ls.size();++i){
for(int j=0;j<rs.size();++j){
int g=gcd(ls[i].first,rs[j].first);
ll num=ls[i].second*rs[j].second;
m[g]+=num;
}
}
} int main()
{
while(~scanf("%d",&n)){
for(int i=1;i<=n;++i){
scanf("%d",a+i);
}
m.clear();
solve(1,n);
int q,xi;
scanf("%d",&q);
while(q--){
scanf("%d",&xi);
if(m.count(xi)) printf("%I64d\n",m[xi]);
else puts("0");
}
}
return 0;
}
Codeforces 475D CGCDSSQ(分治)的更多相关文章
- codeforces 475D. CGCDSSQ
D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes Given a sequence of int ...
- Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...
- Codeforces 475D CGCDSSQ 区间gcd值
题目链接 题意 给定一个长度为 \(n\) 的数列 \(a_1,...,a_n\) 与 \(q\) 个询问 \(x_1,...,x_q\),对于每个 \(x_i\) 回答有多少对 \((l,r)\) ...
- Codeforces 293E 点分治+cdq
Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...
- codeforces 161D 点分治
传送门:https://codeforces.com/problemset/problem/161/D 题意: 求树上点对距离恰好为k的点对个数 题解: 与poj1741相似 把点分治的模板改一下即可 ...
- [CF 475D] CGCDSSQ (RMQ)
题目链接:http://codeforces.com/contest/475/problem/D 是昨天晚上的CF题目,题意是给定你n个数,问你所有子区间内的最小公约数是x的个数是多少 问的康神,了解 ...
- Codeforces 888G(分治+trie)
按位贪心,以当前考虑位是0还是1将数分成两部分,则MST中这两部分之间只会存在一条边,因为一旦有两条或以上的边,考虑两条边在原图中所成的环,显然这两条边有一条是环上的权值最大边,不会出现在MST中.则 ...
- Codeforces 888G Xor-MST - 分治 - 贪心 - Trie
题目传送门 这是一条通往vjudge的高速公路 这是一条通往Codeforces的高速公路 题目大意 给定一个$n$阶完全图,每个点有一个权值$a_{i}$,边$(i, j)$的权值是$(a_{i}\ ...
- Codeforces 990G 点分治+暴力
题意:给出一棵点带权的树,求i\(\in\)[1,200000]所有路径的上点权的gcd==i的个数. 考虑点分治,对于一棵以u为根的子树,如何统计经过u的路径的答案? 显然既然是经过点u的路径,那么 ...
随机推荐
- spring debug
DispatcherServlet{ getHandler()}handlerMappings{ RequestMappingHandlerMapping BeanNameUrlHandlerMapp ...
- java 网页页面抓取标题和正文
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- Datastage数据装载报错:Consumed more than 1000000 bytes looking for record delimiter
使用Datastage装载数据时报错如下图: 使用ds进行数据传输时,出现上述问题,最终找到了问题的原因: 我所使用的数据文件比较大,上传到服务器的时候传了80%就出现服务器存储空间不够,我删除以前的 ...
- acdream 1738 世风日下的哗啦啦族I
原题链接:http://acdream.info/problem?pid=1738 树套树裸题,如下: #include<algorithm> #include<iostream&g ...
- nodejs base64 编码解码
普通字符串 编码解码: var b = new Buffer('JavaScript'); var s = b.toString('base64'); // SmF2YVNjcmlwdA== var ...
- jquery.form的使用
插件API http://malsup.com/jquery/form/#api Jquery.form.js是支持文件异步上传的插件,jq插件自然基本前提当然是要引用Jquery.js 1.0 基本 ...
- extension 的一个应用 - 优化图片的读取机制
枚举和 extension 都是 swift 中非常好用的特性.这里我们就来讨论一个应用的例子,供大家参考. 我们在开发 app 的时候,都会用到各种图片资源,而我们读取图片资源时主要是通过UIIma ...
- MVC4.0 利用HandleErrorAttribute和log4net实现记录异常日志功能
1.MVC4.0中HandleErrorAttribte已经帮我们处理了异常问题,当我们新建一个非空的MVC项目时候,在FilterConfig中会发现这样的代码 public class Filte ...
- [转]ldconfig几个需要注意的地方
[转]ldconfig几个需要注意的地方 http://www.cnblogs.com/arci/archive/2011/03/19/1988952.html 1. 往/lib和/usr/lib里面 ...
- How to Notify Command to evaluate in mvvmlight
How to Raize Command to evalituate in mvvm In mvvmlight, we bind our control to the relaycommand obj ...