题意:给定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. ssm介绍

    1.Spring     Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE ...

  2. Github远程仓库关联

    一.Git的安装 1.git的安装和配置 (1)配置用户名和邮箱,如下所示: $ git config --global user.name [username] $ git config --glo ...

  3. 性能监控工具——Cacti安装文档

    一.Cacti安装说明 1.安装说明 一般性的安装说明,详细的操作系统具体的安装说明可用于Linux. 2.服务器安装要求 RRDTool 1.2.x或更高版本 MySQL 4.1.x或5.x更高版本 ...

  4. [Robot Framework] Jenkins上调用Rebot命令时执行报错不往下执行其他命令

    在配置jenkins job时,添加构建步骤Execute Windows batch command,输入执行rebot命令 报错信息: Call C:\Python27\Scripts\rebot ...

  5. 论坛:排序 >>case..when..then ..end的妙用

    a.主题列表按 最后更新时间 进行排序 数据库SQL语句中没有if..else的判断语句,但是oracle中有decode()函数可以实现这种判断语句,但是还可以用case..when..then . ...

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

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

  7. centos6.5 yum安装postgresql9.3

    rpm -ivh http://download.postgresql.org/pub/repos/yum/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-2.n ...

  8. 码云-中国的github

    # 域名城 https://www.domain.cn/ # 码云 https://gitee.com

  9. 【算法专题】工欲善其事必先利其器—— 常用函数和STL

    一.    常用函数 #include <stdio.h> int getchar( void );               //读取一个字符, 一般用来去掉无用字符 char *ge ...

  10. JAVA钩子方法+模板方法

    模板方法: 写一个抽象类,这个抽象类有多个抽象方法,里面设立一个模板方法,这个模板方法也可以称之为模板算法,设立不同方法的执行顺序,封装业务流程,暴露出去: 模板方法模式的特点很好总结,它将一般性的可 ...