Codeforces Round #511 Div2 C. Enlarge GCD

來源:互聯網
上載者:User

標籤:sign   name   pac   max   puts   com   char   ons   can   

http://codeforces.com/contest/1047/problem/C

問題

給一個序列 \(A\),計原序列所有數的最大公約數為 \(p\)。現在要刪除一些數形成一個新序列,計新序列所有數的最大公約數為 \(q\) 問最少刪除多少數能使 \(q > p\)。

題解

先求出 \(p\),然後枚舉 \(q > p\),計算一下序列中 \(q\) 的倍數有哪些。注意到若存在 \(a | q\) 且 \(a > p\),那麼 \(a\) 顯然優於 \(p\),因此用一個類似篩法的方法枚舉就好了,複雜度 \(O(N \cdot \log{\log{N}})\) 。

#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;typedef pair<int, int> pii;int rint() {    int n, c, sgn = 0;    while ((c = getchar()) < ‘-‘);    if (c == ‘-‘) n = 0, sgn = 1;    else n = c - ‘0‘;    while ((c = getchar()) >= ‘0‘) {        n = 10 * n + c - ‘0‘;    }    return sgn ? -n : n;}const int N = 300010;const int MAX = 1.5e7 + 10;int n;bool mark[MAX];int cnt[MAX];int main() {    scanf("%d", &n);    int g = 0;    for (int i = 0; i < n; i++) {        int x;        scanf("%d", &x);        cnt[x]++;        g = __gcd(g, x);    }    int ans = n;    for (int d = g + 1; d < MAX; d++) {        if (mark[d]) continue;        int need = 0;        for (ll j = d; j < MAX; j += d) {            mark[j] = true;            need += cnt[j];        }        if (need > 0) {            ans = min(ans, n - need);        }    }    if (ans == n) puts("-1");    else printf("%d\n", ans);    fprintf(stderr, "%.3lf sec\n", double(clock()) / CLOCKS_PER_SEC);    return 0;}

Codeforces Round #511 Div2 C. Enlarge GCD

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.