题意:给定n(n<=100000)个1e9以内的数的数组a,然后最多有3*1e5的询问,对于每个询问,给定一个x,问有多少个(l<=r&&gcd(a[l],a[l+1]...a[r]) == x)

思路:昨天的比赛题。。可惜我被c题wa到放弃了这场比赛。。也就没看了。。不妨设G(l,r) = gcd(a[l], a[l+1]...a[r]);

其实题目最关键的的性质是对于G(l,r),G(l,r+1)后者肯定比前者更小。。

所以就可以暴力了。。从后往前扫描i,处理(i, n)这一段区间,处理处理完之后,就会出现G(i,i),G(i,i+1)..G(i, n),并且是递减的

所以相邻之间如果相同,我们就可以合并,具体操作可以用链表。。

这样最坏情况下每个数求log(1e9)次gcd,所以还是可以快速过的。。

code:

 #include <bits/stdc++.h>
using namespace std;
int a[], nxt[], n;
map<int, long long> mp; void solve(){
for (int i = ; i <= n; ++i)
scanf("%d", &a[i]), nxt[i] = i + ;
mp.clear();
for (int i = n; i >= ; --i){
int g = a[i], pre_g = g, pre = i;
for (int j = i; j <= n + ; j = nxt[j]){
if (j == n + ){
mp[pre_g] += (j - pre);
nxt[pre] = j;
break;
}
g = __gcd(a[j], g);
if (g != pre_g)
mp[pre_g] += (j - pre) , nxt[pre] = j , pre = j, pre_g = g;
}
}
int q, x;
scanf("%d", &q);
while (q--){
scanf("%d", &x);
printf("%I64d\n",mp[x]);
}
} int main(){
while (scanf("%d", &n) != EOF){
solve();
}
}

codeforces 475D的更多相关文章

  1. Codeforces 475D 题解(二分查找+ST表)

    题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...

  2. Codeforces 475D CGCDSSQ(分治)

    题意:给你一个序列a[i],对于每个询问xi,求出有多少个(l,r)对使得gcd(al,al+1...ar)=xi. 表面上是询问,其实只要处理出每个可能的gcd有多少个就好了,当左端点固定的时候,随 ...

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

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

  4. codeforces 475D. CGCDSSQ

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

  5. Codeforces 475D CGCDSSQ 区间gcd值

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

  6. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  9. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

随机推荐

  1. Get、Post 提交的乱码问题

    1.问题 在spring mvc开发的时候出现乱码问题: 2.解决方案 (1)Get提交:tomcat容器接收的造成的乱码问题,修改server.xml文件: (2)Post提交:在web.xml中配 ...

  2. POJ 2014.K-th Number 区间第k小 (归并树)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 57543   Accepted: 19893 Ca ...

  3. Zookeeper 修改heap size

    对应原文出处: https://support.pivotal.io/hc/en-us/articles/201861286-Zookeeper-service-heapsize-is-10GB-or ...

  4. python_docx制作word文档详细使用说明【转】

      目前网上对这一个库的介绍得很少,很零散,所以很多功能我是尽量参考其官网,但是官网上面很多功能目前只有说明文档,而代码并还没有及时更新,以至于按照官网上面做了,python却报错.比如:自定义表格的 ...

  5. mysql错误日志

    cat /etc/my.cnf

  6. IDEA 文件列表隐藏某后缀文件

    preferences -> Editor -> File Types -> ignore files and folders   添加后缀类型如:   *.meta

  7. 通过SD卡来安装Linux系统

    一.制作SD启动卡(安装Linux)步骤: 烧写原理:superboot-6410.bin(bootloader)+内核镜像文件+根文件系统 1.将SD卡插入USB接口的读卡器,并插在PC的USB口 ...

  8. windows10安装ubuntu on windows10

    安装向导 https://msdn.microsoft.com/en-us/commandline/wsl/install_guide

  9. boost-使用property_tree来解析xml、json

    property_tree是一个保存了多个属性值的树形数据结构,可以用来解析xml.json.ini.info文件.要使用property_tree和xml解析组件的话需要包含"boost/ ...

  10. 用mathematica求六元一次方程组且方程个数比变量个数少一个

    问题详见知乎:https://www.zhihu.com/question/68000713 我的问题:有5个方程,6个变量,其实我是想求出来de1=(系数)*dS1的形式,系数有Cij组成,Cij为 ...