Codeforces 474F(分块)
来源:互联网 发布:抓阄软件 编辑:程序博客网 时间:2024/06/10 13:01
题意:给长度为N的数列,t个询问[l,r],求区间[l,r]中不能整除区间中其它数的数有多少个。
这题和之前一道分块题简直一模一样,于是就用分块做了。
没个询问的答案,就是区间长度减去值为区间gcd的数的个数。那么预处理出每段的答案及这段的gcd,可以很容易推出答案。
#include<bits/stdc++.h>using namespace std;const int N = 5e5 + 20;const int M = 500;int n, t, s[N], l, r;int unit, siz, ans[M][M], gans[M][M];int g, temp, cnt;int gcd(int a, int b){ return b ? gcd(b, a % b) : a;}int main(){ memset(ans, 0, sizeof(ans)); memset(gans, 0, sizeof(gans)); scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &s[i]); unit = sqrt(n); siz = (n + unit) / unit; for(int i = 0; i <= n; i += unit){ g = s[i + 1], cnt = 0, temp; for(int j = i; j <= n; j++){ temp = gcd(g, s[j]); if(g != temp) cnt = 0, g = temp; if(s[j] == g) cnt++; if(j % unit == 0) ans[i / unit][j / unit] = cnt, gans[i / unit][j / unit] = g; } ans[i / unit][siz] = cnt; gans[i / unit][siz] = g; } scanf("%d", &t); while(t--){ scanf("%d%d", &l, &r); int x = (l + unit) / unit * unit, y = r / unit * unit; int res; if(x >= y) g = s[l], res = 1, x = y = l; else g = gans[x / unit][y / unit], res = ans[x / unit][y / unit]; for(int i = l; i < x; i++){ temp = gcd(g, s[i]); if(g != temp) res = 0, g = temp; if(g == s[i]) res++; } for(int i = y + 1; i <= r; i++){ temp = gcd(g, s[i]); if(g != temp) res = 0, g = temp; if(g == s[i]) res++; } printf("%d\n", r - l + 1 - res); } return 0;}
阅读全文
0 0
- Codeforces 474F(分块)
- [分块] Codeforces 436F && Zepto Code Rush 2014 F. Banners
- [分块] Codeforces 436F Zepto Code Rush 2014 F. Banners
- [Segment tree Beats! || 分块] Codeforces 793F Tinkoff Challenge
- 【codeforces 731F】【前缀和 分块求和 好题】F. Video Cards
- Codeforces Round #376 (Div. 2) F. Video Cards (数论 前缀和 分块求和)
- Codeforces Round #442 (Div. 2) F. Ann and Books (莫队分块)
- Codeforces 474 F. Ant colony
- Codeforces 659F F
- codeforces 474F F. Ant colony(线段树+数论)
- codeforces 474F F. Ant colony(线段树+数论)
- codeforces 455D 分块
- codeforces 474F 区间gcd + 离散化
- codeforces 474F Ant colony(线段树)
- Codeforces 474D (五一训练 F)+DP
- CodeForces 13E Holes(分块)
- Codeforces 234 F. Fence
- 【Codeforces 500F】Dp
- WIN10 FOR mysql-5.7.18安装,MySQL服务无法启动
- Java static{} 语句详解
- 什么是BI、ETL、DW
- Java Web 3.2.3课后思考
- 感知器算法----Matlab实现
- Codeforces 474F(分块)
- Hadoop2.6.4运行Wordcount程序
- 属性动画AnimatorDemo
- Android基于线性加速度计的惯性导航
- 什么是手机App界面设计规范?
- 框架超强辅助,逆向工程生成器
- sublime设置了build system但是使用automatic提示no build system问题
- HBase工具之BulkLoad
- jQuery的一些简单知识点(一)