其他分享
首页 > 其他分享> > Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!) F. Kuroni and the Punishment

Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!) F. Kuroni and the Punishment

作者:互联网

可以发现答案不大于 \(n\),因为如果都是奇数可以让它们都变成偶数
如果我们假定了一个 \(\gcd\),我们就可以线性得到答案,每个数向上或者向下移动到 \(\gcd\) 的倍数位置
因为答案不大于 \(n\),那么就有至少有 \(\lceil \frac{n}{2} \rceil\) 个数操作次数不大于 \(1\)
那么 \(\gcd\) 就在权值 \(x\)、\(x+1\)、\(x-1\) 的质因子中
那么就随机 \(100\) 个位置,把它们权值进行分解
最后check每一个质因子即可

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<ll, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) / 2)
#define lowbit(i) ((i) & (-i))
#define ll long long
#define ull unsigned long long
#define db double
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define Edg int ccnt=1,head[N],to[E],ne[E];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[E],ne[E],c[E];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1e9 + 7;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2) {int ans = 1; for (; b; a = 1LL * a * a % MOD, b >>= 1)if (b & 1)ans = 1LL * ans * a % MOD; return ans % MOD;}
template<class T>T gcd(T a, T b) { while (b) { a %= b; std::swap(a, b); } return a; }
template<class T>bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T>bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
    int x = 0, f = 1; char ch = getc();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
    while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
    return x * f;
}
inline ll __() {
    ll x = 0, f = 1; char ch = getc();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
    while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
    return x * f;
}

const int N = 1e6 + 7;
int prime[N / 10], prin;
ll a[N];
int n;
void init() {
    static bool vis[N];
    rep (i, 2, N) {
        if (!vis[i]) prime[++prin] = i;
        rep (j, 1, prin + 1) {
            int v = i * prime[j];
            if (v >= N) break;
            vis[v] = 1;
            if (i % prime[j] == 0) break;
        }
    }
}
std::vector<ll> pr;
std::random_device rd;
std::mt19937 mt(rd());

void div(ll n) {
    rep (i, 1, prin + 1) {
        if (n < prime[i]) break;
        if (n % prime[i]) continue;
        pr.pb(prime[i]);
        while (n % prime[i] == 0) n /= prime[i];
    }
    if (n > 1) pr.pb(n);
}

int solve(ll k) {
    int res = 0;
    rep (i, 0, n) {
        ll temp = 0;
        if (a[i] < k)   temp = k- a[i];
        else temp = std::min(a[i] % k, k - a[i] % k);
        res = std::min((ll)n, res + temp);
    }
    return res;
}

int main() {
#ifdef LOCAL
    freopen("ans.out", "w", stdout);
#endif
    n = _();
    init();
    std::vector<int> index;
    rep (i, 0, n) a[i] = __(), index.pb(i);
    std::shuffle(index.begin(), index.end(), mt);
    rep (i, 0, index.size()) {
        if (i > 111) break;
        div(a[index[i]]);
        div(a[index[i]] + 1);
        if (a[index[i]] > 1) div(a[index[i]] - 1);
    }
    std::sort(pr.begin(), pr.end());
    pr.erase(unique(pr.begin(), pr.end()), pr.end());
    int ans = n;
    rep (i, 0, pr.size()) chkmin(ans, solve(pr[i]));
    printf("%d\n", ans);
    return 0;
}

标签:pr,index,Rated,int,Challenge,Kuroni,std,ccnt,define
来源: https://www.cnblogs.com/Mrzdtz220/p/12408132.html