题意:给你一个序列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(分治)的更多相关文章

  1. codeforces 475D. CGCDSSQ

    D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes Given a sequence of int ...

  2. Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数

    题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...

  3. Codeforces 475D CGCDSSQ 区间gcd值

    题目链接 题意 给定一个长度为 \(n\) 的数列 \(a_1,...,a_n\) 与 \(q\) 个询问 \(x_1,...,x_q\),对于每个 \(x_i\) 回答有多少对 \((l,r)\) ...

  4. Codeforces 293E 点分治+cdq

    Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...

  5. codeforces 161D 点分治

    传送门:https://codeforces.com/problemset/problem/161/D 题意: 求树上点对距离恰好为k的点对个数 题解: 与poj1741相似 把点分治的模板改一下即可 ...

  6. [CF 475D] CGCDSSQ (RMQ)

    题目链接:http://codeforces.com/contest/475/problem/D 是昨天晚上的CF题目,题意是给定你n个数,问你所有子区间内的最小公约数是x的个数是多少 问的康神,了解 ...

  7. Codeforces 888G(分治+trie)

    按位贪心,以当前考虑位是0还是1将数分成两部分,则MST中这两部分之间只会存在一条边,因为一旦有两条或以上的边,考虑两条边在原图中所成的环,显然这两条边有一条是环上的权值最大边,不会出现在MST中.则 ...

  8. Codeforces 888G Xor-MST - 分治 - 贪心 - Trie

    题目传送门 这是一条通往vjudge的高速公路 这是一条通往Codeforces的高速公路 题目大意 给定一个$n$阶完全图,每个点有一个权值$a_{i}$,边$(i, j)$的权值是$(a_{i}\ ...

  9. Codeforces 990G 点分治+暴力

    题意:给出一棵点带权的树,求i\(\in\)[1,200000]所有路径的上点权的gcd==i的个数. 考虑点分治,对于一棵以u为根的子树,如何统计经过u的路径的答案? 显然既然是经过点u的路径,那么 ...

随机推荐

  1. selenium+python登录登出百度,等待页面加载,鼠标定位

    #coding:gbk from selenium import webdriver from selenium.webdriver.common.keys import Keys from sele ...

  2. android开发系列之git常用命令

    最近因为跳槽到新公司,然后新公司里面的代码管理工具是gitLab,所以我想在这篇博客里面整理一下git常用的语法. GitLab是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托 ...

  3. bash: 避免命令重复执行的简单脚本

    1. 根据命令生成md5做为文件名保存当前进程的pid2. 使用exec执行命令3. 如果再次执行, 使用ps -p检测上次pid是否有效, 如果是则exit 200.否则重复1.hadoop@ubu ...

  4. Swift 中使用Nimble 库进行单元测试

    Nimble 从字面上看是 敏捷,灵活 的意思.Nimble 是一个库,一个 断言库.这个库一般用于单元测试.Xcode 6 为我们集成了 XCTest 单元测试库.在正式介绍 Nimble 之前,我 ...

  5. maven学习手记 - 1

    学习目标 windows下安装maven环境: 使用命令创建maven项目结构: maven项目编译测试打包安装运行: 在maven项目中使用插件.   在windows下安装maven环境 在win ...

  6. [转]coredump简介与coredump原因总结

    [转]coredump简介与coredump原因总结 http://blog.sina.com.cn/s/blog_54f82cc201013srb.html 什么是coredump? 通常情况下co ...

  7. 微软职位内部推荐-Senior Software Engineer-SDP

    微软近期Open的职位: Position: Senior SDE The R&D of Shared Data Platform at Application and Services Gr ...

  8. HTML5就是现在:深入了解Polyfills

    http://blog.csdn.net/wang16510/article/details/8960312 https://github.com/Modernizr/Modernizr/wiki/H ...

  9. haproxy实现mysql从库负载均衡

    本文主要讲述通过haproxy实现mysql从库间的负载均衡,至于mysql主从的搭建,本文不再重述,可以参考我之前写的博客. 1.首先下载haproxy包 wget http://haproxy.1 ...

  10. LintCode-Serialization and Deserialization Of Binary Tree

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...