codeforces 475D
题意:给定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的更多相关文章
- Codeforces 475D 题解(二分查找+ST表)
题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...
- Codeforces 475D CGCDSSQ(分治)
题意:给你一个序列a[i],对于每个询问xi,求出有多少个(l,r)对使得gcd(al,al+1...ar)=xi. 表面上是询问,其实只要处理出每个可能的gcd有多少个就好了,当左端点固定的时候,随 ...
- Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...
- 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值
题目链接 题意 给定一个长度为 \(n\) 的数列 \(a_1,...,a_n\) 与 \(q\) 个询问 \(x_1,...,x_q\),对于每个 \(x_i\) 回答有多少对 \((l,r)\) ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
随机推荐
- Codeforces559C Gerald and Giant Chess
一道计数类\(DP\) 原题链接 我们可以先计算从左上角到右下角总的路径,再减去经过黑色方格的路径即是答案. 总路径数可以用组合数直接计算:\(C_{H+W-2}^{H-1}\) 因为从左上角到右下角 ...
- MyBatis Generator介绍
MyBatis Generator介绍 MyBatis Generator (MBG) 是一个Mybatis的代码生成器 MyBatis 和 iBATIS. 他可以生成Mybatis各个版本的代码,和 ...
- hook api实现
https://www.cnblogs.com/findumars/p/8734116.html https://blog.csdn.net/lonelyrains/article/details/2 ...
- Struts问题
1.struts框架的5大组件:mvc,标签库,校验框架,国际化,tiles; 2.struts的9大核心类以及与mvc对应的关系: C ActionServlet RequestProcessor ...
- AUC和ROC
https://www.cnblogs.com/gatherstars/p/6084696.html
- 效率类APP原型制作分享----Timeglass
本原型由国产Mockplus(原型工具)和iDoc(智能标注,一键切图工具)提供. 主要页面:启动页面.主页.添加事件页面.设置页面等. mp文件下载:点击这里 在线预览:http://run.moc ...
- 解决maven update project 后项目jdk变成1.5的问题
一.问题描述 在Eclipse中新建了一个Maven工程, 然后更改JDK版本为1.7, 结果每次使用Maven > Update project的时候JDK版本都恢复成1.5. 二.原因分析 ...
- ListView 删除item删除不了的问题解决办法
下面的方法是删除不了item的: Integer pos = Integer.valueOf(msg.getBody().toString()); adapter.getList().remove(p ...
- Linux学习笔记:Jenkins安装
操作系统是CentOS 7,安装Jenkins 首先安装jdk,可在Oracle jdk和Openjdk中任选其一安装Oracle jdk步骤见: https://www.cnblogs.com/ ...
- 使用delphi 10.2 开发linux 上的Daemon
delphi 10.2 支持linux, 而且官方只是支持命令行编程,目地就是做linux 服务器端的开发. 既然是做linux服务器端的开发,那么普通的命令行运行程序,然后等待开一个黑窗口的方式就 ...