Vasya and Beautiful Arrays

CodeForces - 354C

Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n.

Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array a left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by k for each number).

The seller can obtain array b from array a if the following conditions hold: b**i > 0; 0 ≤ a**i - b**i ≤ k for all 1 ≤ i ≤ n.

Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain).

Input

The first line contains two integers n and k (1 ≤ n ≤ 3·105; 1 ≤ k ≤ 106). The second line contains n integers a**i (1 ≤ a**i ≤ 106) — array a.

Output

In the single line print a single number — the maximum possible beauty of the resulting array.

Examples

Input

6 13 6 10 12 13 16

Output

3

Input

5 38 21 52 15 77

Output

7

Note

In the first sample we can obtain the array:

3 6 9 12 12 15

In the second sample we can obtain the next array:

7 21 49 14 77

题意:

给你一个含有n个数的数组,和一个整数k。

对于数组中的每一个数\(a[i]\), 可以减去\([0,k]\) 。问你修改之后数组的最大公约数是多少?

思路:

首先确定答案的上下界限。

设mn 是数组a中的最小数。

设mx是数组a中的最大值。

显然答案的最大值是mn

再考虑下,如果mn>=k+1 ,

那么答案的最小值是k+1 ,因为 将a[i] 对k+1 取模,剩余的每一个a[i]<=k,那么都可以将大于0的a[i],减为0,即gcd为k+1.

所以现在确定的上下届为\([k+1,mn]\)

那么我们不妨枚举gcd,

从mn 枚举到k+1。

那么这个过程是\(O(n)\)的

对于当前枚举到的gcd为x,如何判断可以修正数组使gcd为x呢?

我们看下只有当一个数\(a[i]\) 在这个区间\([i*x,i*x+k]\)中才可以变为x的倍数。

如果每一个数都在这个区间,那么整个数组就可以修改为每一个a[i] 都是 x的倍数。

那么我们不妨枚举x的倍数i,利用前缀和在\(O(1)\) 时间内获得区间中有多少个数,

最后看总个数是否为N,就可以判断x是否满足条件了。

枚举x的倍数时间复杂度为\(O(log_x(mx))\)

总时间复杂度是\(O(n*logn)\) 可以通过。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int k;
int vis[maxn];
int sum[maxn];
int a[maxn];
int mn = inf;
int mx = -1;
bool check(int x)
{
int cnt = 0;
for (int i = 1; i * x <= mx; i++)
{
cnt += sum[min(i * x + k, mx)] - sum[i * x - 1];
}
return cnt == n;
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin >> n >> k;
repd(i, 1, n)
{
cin >> a[i];
vis[a[i]]++;
mn = min(mn, a[i]);
mx = max(mx, a[i]);
}
repd(i, 1, mx)
{
sum[i] = sum[i - 1] + vis[i];
}
// [ k+1 , mn ]
//
if (mn <= k + 1)
{
cout << mn << endl;
}
else
{
for (int i = mn; i >= k + 1; i--)
{
if (check(i))
{
cout << i << endl;
break;
}
}
} return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Vasya and Beautiful Arrays CodeForces - 354C (数论,枚举)的更多相关文章

  1. E. Vasya and Beautiful Arrays

    http://codeforces.com/contest/355/problem/E 每个数都可以变成段 [a-k,a], 某一个因子是否被所有的段包含,就是把这个因子以及它的所有倍数看成点, 看是 ...

  2. Coprime Arrays CodeForces - 915G (数论水题)

    反演一下可以得到$b_i=\sum\limits_{d=1}^i{\mu(i)(\lfloor \frac{i}{d} \rfloor})^n$ 整除分块的话会T, 可以维护一个差分, 优化到$O(n ...

  3. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  4. D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)

    D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...

  5. Codeforces - 55D Beautiful numbers (数位dp+数论)

    题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...

  6. Codeforces 354C 暴力 数论

    题意:给你一个数组,你可以把数组中的数减少最多k,问数组中的所有数的GCD最大是多少? 思路:容易发现,GCD的上限是数组中最小的那个数,而因为最多可以减少k,及可以凑出来的余数最大是k,那么GCD的 ...

  7. Codeforces Round #319 (Div. 2) C Vasya and Petya's Game (数论)

    因为所有整数都能被唯一分解,p1^a1*p2^a2*...*pi^ai,而一次询问的数可以分解为p1^a1k*p2^a2k*...*pi^aik,这次询问会把所有a1>=a1k &&am ...

  8. CodeForces 300C --数论

    A - A Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  9. CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26

    /* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...

随机推荐

  1. javascript语法 1.运算符 2. 流程控制 3. 函数 4. 四种变量 5. 数据类型的运用 6. js页面交互

    1.运算符 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...

  2. [Kevin英语情报局]那些年我们说过的中式英语

    一. blonde hair 金色头发 grey hair 白头发 baijiu 白酒 white wine 白葡萄酒 I don't think it's right 我认为不正确 I'm chin ...

  3. codevs 3031:最富有的人

    题目描述 Description 在你的面前有n堆金子,你只能取走其中的两堆,且总价值为这两堆金子的xor值,你想成为最富有的人,你就要有所选择. 输入描述 Input Description 第一行 ...

  4. 服务器TIME_WAIT和CLOSE_WAIT区别及解决方案

    系统上线之后,通过如下语句查看服务器时,发现有不少TIME_WAIT和CLOSE_WAIT. netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) ...

  5. SSM框架的整合与使用——实现简单的转账系统

    一.整合思路 SSM框架即SpringMVC + Spring + MyBati框架集,是一种轻量级的Web开源框架.它们各自在JAVA三层架构中负责的模块如下图所示: 其中,SpringMVC与Sp ...

  6. 洛谷 P4198 楼房重建 线段树维护单调栈

    P4198 楼房重建 题目链接 https://www.luogu.org/problemnew/show/P4198 题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上 ...

  7. 数据结构与算法之排序算法(python实现)

    1.冒泡排序 冒泡排序的原理是依次比较相邻的两个数,如果前一个数比后一个数大则交换位置,这样一组比较下来会得到该组最大的那个数,并且已经放置在最后,下一轮用同样的方法可以得到次大的数,并且被放置在正确 ...

  8. 编写程序模拟strlwr()和strupr()函数功能

    strlwr(字符串)strlwr()的作用是将字符串中大写字母转换成小写字母 strupr(字符串)strupr()的作用是将字符串中小写字母转换成大写字母 /* strlwr(字符串) strlw ...

  9. 怎样在浏览器端增加一条Cookie

    可以使用 document.cookie, 这个属性可读可写, 读时是读取所有没有设置HttpOnly的cookie作为一个字符串返回, 写时是将一个cookie写入到document.cookie中 ...

  10. IP 、127.0.0.1、localhost 三者区别

    一.Ping命令 1.Ping命令,用来检查两台物理机间的TCP/IP网络是否通畅或者网络连接速度,是TCP/IP协议的一部分. 2.PING (Packet Internet Groper),因特网 ...