Codeforces 354C 暴力 数论
题意:给你一个数组,你可以把数组中的数减少最多k,问数组中的所有数的GCD最大是多少?
思路:容易发现,GCD的上限是数组中最小的那个数,而因为最多可以减少k,及可以凑出来的余数最大是k,那么GCD的下限是k + 1,所以当最小的数小于等于k + 1时,答案是最小的数。如果最小的数大于k + 1,我们从大到小枚举GCD,假设当前枚举的数是x,那么如果一个数在[t * x, t * x + k](t是一个常数)之间,那么就可以被凑出来,我们看一下最后凑出来的数是不是n个就可以了。我们可以用前缀和优化,这样查询区间操作是O(1)的。因为调和复杂度是nlogn的,所有可以过。
代码:
#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define db double
#define pii pair<int, int>
using namespace std;
const int maxn = 300010;
int a[maxn];
int sum[1000010];
int main() {
int n, k, mi = 1e9;
// freopen("Cin.txt", "r", stdin);
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
mi = min(mi, a[i]);
}
if(mi <= k + 1) {
printf("%d\n", mi);
}
for (int i = 1; i <= n; i++) {
sum[a[i]]++;
}
for (int i = 1; i <= 1e6; i++)
sum[i] += sum[i - 1];
for (int i = mi; i >= k + 1; i--) {
int now = 0;
for (int j = 1; i * j <= 1e6; j++) {
now += sum[min(1000000, i * j + k)] - sum[i * j - 1];
if(now == n) {
printf("%d\n", i);
return 0;
}
}
}
}
Codeforces 354C 暴力 数论的更多相关文章
- Vasya and Beautiful Arrays CodeForces - 354C (数论,枚举)
Vasya and Beautiful Arrays CodeForces - 354C Vasya's got a birthday coming up and his mom decided to ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...
- codeforces 735D Taxes(数论)
Maximal GCD 题目链接:http://codeforces.com/problemset/problem/735/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个n(2≤n≤2e9) ...
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...
- Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力
Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...
- Codeforces Round #425 (Div. 2) Problem B Petya and Exam (Codeforces 832B) - 暴力
It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...
- codeforces 691F 暴力
传送门:https://codeforces.com/contest/691/problem/F 题意:给你n个数和q次询问,每次询问问你有多少对ai,aj满足ai*aj>=q[i],注意 a* ...
- Vicious Keyboard CodeForces - 801A (暴力+模拟)
题目链接 题意: 给定一个字符串,最多更改一个字符,问最多可以有多少个“VK”子串? 思路: 由于数据量很小,不妨尝试暴力写.首先算出不更改任何字符的情况下有多个VK字串,然后尝试每一次更改一个位置的 ...
随机推荐
- 【LeetCode】队列 queue(共8题)
[346]Moving Average from Data Stream [353]Design Snake Game [363]Max Sum of Rectangle No Larger Than ...
- Linux 给用户 赋某个文件夹操作的权限
https://my.oschina.net/cqyj/blog/1796047 在root用户登录的情况,赋予opt目录给liuhai这个用户权限 示例代码: # 将目录/opt 及其下面的所有文件 ...
- 基于vue的移动端web音乐播放器
声明 以下只是学习完慕课网huangyi老师实战视频课程的笔记内容,仅供个人参考学习使用.如果对Vue2.0实战高级-开发移动端音乐WebApp感兴趣的话,请移步这里:https://coding.i ...
- hadoop通过java输出HAFS上的文件内容
package org.apache.hadoop.book;import java.io.InputStream;import java.net.URL;import org.apache.hado ...
- springBoot相关(二)
Spring Boot 三大特性: 组件自动装配:Web MVC .Web Flux .JDBC等 嵌入式Web容器:Tomcat.Jetty以及Undertow 生产准备特性:指标.健康检查.外部化 ...
- postman自动化接口测试
背景描述 有一个项目要使用postman进行接口测试,接口所需参数有: appid: 应用标识: sign:请求签名,需要使用HMACSHA1加密算法计算,签名串是:{appid}${url}${st ...
- Git项目实战篇
版本控制器:Git # 达到多人协同开发的目的 安装 """1.下载对应版本:https://git-scm.com/download2.安装git:在选取安装路径的下一 ...
- [CSP-S模拟测试]:序列(主席树)
题目描述 小$A$把自己之前得到的序列展示给了小$B$,不过这一次,他并不要求小$B$模仿他之前的行为.他给了小$B$一些询问,每个询问都是$l\ r\ x$的形式,要求小$B$数出在序列的第$l$个 ...
- (选做)实现mypwd
选做 实现mypwd 实验内容: 1.学习pwd命令. 2.研究pwd实现需要的系统调用(man -k; grep),写出伪代码. 3.实现mypwd. 4.测试mypwd. 实验步骤: 学习pwd命 ...
- PyQuery爬取历史天气信息
1.准备工作: 网址:https://lishi.tianqi.com/xian/index.html 爬虫类库:PyQuery,requests 2.网页分析: 红线部分可更改为需要爬取的城市名,如 ...