Max GCD(思维+数学)
题目描述
We have a sequence of N integers: A1,A2,⋯,AN.
You can perform the following operation between 0 and K times (inclusive):
·Choose two integers i and j such that i≠j, each between 1 and N (inclusive). Add 1 to Ai and −1 to
Aj, possibly producing a negative element.
Compute the maximum possible positive integer that divides every element of A after the operations. Here a positive integer x divides an integer y if and only if there exists an integer z such that
y=xz.
You can perform the following operation between 0 and K times (inclusive):
·Choose two integers i and j such that i≠j, each between 1 and N (inclusive). Add 1 to Ai and −1 to
Aj, possibly producing a negative element.
Compute the maximum possible positive integer that divides every element of A after the operations. Here a positive integer x divides an integer y if and only if there exists an integer z such that
y=xz.
Constraints
·2≤N≤500
·1≤Ai≤106
·0≤K≤109
·All values in input are integers.
输入
Input is given from Standard Input in the following format:
N K
A1 A2 ⋯ AN−1 AN
输出
Print the maximum possible positive integer that divides every element of A after the operations.
样例输入 Copy
【样例1】
2 3
8 20
【样例2】
2 10
3 5
【样例3】
4 5
10 1 2 22
【样例4】
8 7
1 7 5 6 8 2 6 5
样例输出 Copy
【样例1】
7
【样例2】
8
【样例3】
7
【样例4】
5
提示
样例1解释
7 will divide every element of A if, for example, we perform the following operation:
·Choose i=2,j=1. A becomes (7,21).We cannot reach the situation where 8 or greater integer divides every element of A.
7 will divide every element of A if, for example, we perform the following operation:
·Choose i=2,j=1. A becomes (7,21).We cannot reach the situation where 8 or greater integer divides every element of A.
样例2解释
Consider performing the following five operations:
·Choose i=2,j=1. A becomes (2,6).
·Choose i=2,j=1. A becomes (1,7).
·Choose i=2,j=1. A becomes (0,8).
·Choose i=2,j=1. A becomes (−1,9).
·Choose i=1,j=2. A becomes (0,8).
Then, 0=8×0 and 8=8×1, so 8 divides every element of A. We cannot reach the situation where 9 or greater integer divides every element of A.
题意:给你0-k次,选连个数,一个+1,一个-1。求经过这样操作,最后所有数的最大gcd是多少。
枚举总和的因数,然后判断在k次操作是否可行。更新答案。
将数组mod枚举数的值排序,贪心,最大的+,最小的-。
#pragma GCC optimize(3 , "Ofast" , "inline")
#include <bits/stdc++.h>
#define rep(i , a , b) for(register ll i=(a);i<=(b);i++)
#define rop(i , a , b) for(register int i=(a);i<(b);i++)
#define per(i , a , b) for(register int i=(a);i>=(b);i--)
#define por(i , a , b) for(register int i=(a);i>(b);i--)
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pi;
const int maxn = 1e5 + 10;
ll a[555];
ll b[555];
ll s = 0;
ll n , k;
bool check (ll x) {
if ( s % x )
return false;
rep (i , 1 , n) {
b[ i ] = a[ i ] % x;
}
sort (b + 1 , b + 1 + n);
ll res = 0;
for ( int i = 1 , j = n ; i <= j ; ) {
if ( b[ i ] % x == 0 ) {
i ++;
continue;
}
if ( b[ j ] % x == 0 ) {
j --;
continue;
}
ll mi = min (b[ i ] % x , x - b[ j ] % x);
b[ i ] -= mi;
b[ j ] += mi;
res += mi;
}
if ( res <= k ) return true;
return false;
}
template<class T>
inline void read (T &x) {
x = 0;
int sign = 1;
char c = getchar ();
while (c < '0' || c > '9') {
if ( c == '-' ) sign = - 1;
c = getchar ();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar ();
}
x = x * sign;
}
int main () {
read (n);
read (k);
rep (i , 1 , n) read (a[ i ]) , s += a[ i ];
ll x = sqrt (s) + 1;
ll ans = 0;
per (i , x , 1) {
if ( check (i)) ans = max (ans , ll (i));
if ( check (s / i)) ans = max (ans , ll (s / i));
}
cout << ans << endl;
return 0;
}