zoukankan      html  css  js  c++  java
  • Codeforces Round #641 (Div. 2)

    比赛链接:https://codeforces.com/contest/1350

    A - Orac and Factors

    题意

    记 $f(n)$ 为 $n$ 的最小非 $1$ 因子,输出执行 $k$ 次 $n = n +  f(n)$ 的结果。

    题解

    $n$ 为偶数,最小非 $1$ 因子恒为 $2$,即 $n + 2 * k$ 。

    $n$ 为奇数,最小非 $1$ 因子为奇数,即 $n + f(n) + 2 * (k - 1)$ 。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    int f(int n) {
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0)
                return i;
        return n;
    }
    
    void solve() {
        int n, k; cin >> n >> k;
        if (n % 2 == 0) 
            cout << n + 2 * k << "
    ";
        else
            cout << n + f(n) + 2 * (k - 1) << "
    ";
    }
    
    int main() {
        int t; cin >> t;
        while (t--) solve();
    }

    B - Orac and Models

    题意

    在数组 $s$ 中寻找一个最长序列,要求序列严格递增,且每对相邻元素中后者的下标为前者的倍数。

    (1 - indexed)

    题解

    $dp_i$ 为以下标 $i$ 结尾的序列的最大长度。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    void solve() {
        int n; cin >> n;
        int a[n + 1] = {};
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        map<int, int> dp;
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            int mx = 0;
            for (int j = 1; j * j <= i; j++) {
                if (i % j == 0) {
                    if (a[i] > a[j]) mx = max(mx, dp[j]);
                    if (a[i] > a[i / j]) mx = max(mx, dp[i / j]);
                }
            }
            dp[i] = 1 + mx;
            ans = max(ans, dp[i]);
        }
        cout << ans << "
    ";
    }
    
    int main() {
        int t; cin >> t;
        while (t--) solve();
    }

    C - Orac and LCM

    题意

    求 $lcm(gcd(a_i, a_j) | i < j)$ 。

    题解一

    设 $d_i$ 为除 $a_i$ 外 $n - 1$ 个元素的集合,答案即为 $lcm(gcd(d_i))$。

    证明

    如果一个数为 $n - 1$ 个元素的因子,那么这个数一定为每个 $lcm$ 的因子,最终要求的 $gcd$ 即为能包含所有因子的 $lcm$ 。

    代码

    #include <bits/stdc++.h>
    using ll = long long;
    using namespace std;
    
    const int N = 1e5 + 100;
    int a[N], pre[N], suf[N];
    
    int main() {
        int n; cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        for (int i = 0; i < n; i++) {
            pre[i + 1] = __gcd(pre[i], a[i]);
        }
        for (int i = n - 1; i >= 0; i--) {
            suf[i] = __gcd(suf[i + 1], a[i]);
        }
        ll res = 1;
        for (int i = 0; i < n; i++) {
            ll x = __gcd(pre[i], suf[i + 1]);
            res = res * x / __gcd(res, x);
        }
        cout << res;
    }

    题解二

    对每个数做质因数分解,记录所有质因数的出现次数和每个数中的个数,取出现次数 ≥ $n - 1$ 的质因数的次小值。

    证明

    单独考虑每个质因数,因为是两两求 $lcm$,所以在所有 $lcm$ 中该质因数的个数在 次小值~最大值 之间,如果有 $≥2$ 个数不含该质因数,那么该质因数就不会出现在所求答案中,因为由这些未出现的数两两组成的 $lcm$ 中是一定不含该质因数的,否则,因为要求所有 $lcm$ 的 $gcd$,即该质因数在 $lcm$ 中的最小值,即在所有元素中的次小值。 

    代码

    #include <bits/stdc++.h>
    using ll = long long;
    using namespace std;
    
    const int N = 2e5 + 100;
    int d[N];
    int ans[N][3];
    
    void init() {
        for (int i = 2; i < N; i++) {
            if (d[i]) continue;
            for (int j = i; j < N; j += i)
                if (d[j] == 0) 
                    d[j] = i;
        }
        for (int i = 0; i < N; i++)
            ans[i][0] = ans[i][1] = 100;
    }
    
    int main() {
        init();
        int n; cin >> n;
        for (int i = 0; i < n; i++) {
            int x; cin >> x;
            while (x > 1) {
                int p = d[x];
                int t = 0;
                while (x % p == 0) {
                    x /= p;
                    t++;
                }
                for (int j = 0; j < 2; j++) {
                    if (t < ans[p][j])
                        swap(t, ans[p][j]);
                }
                ans[p][2]++;
            }
        }
        ll res = 1;
        for (int p = 2; p < N; p++) {
            if (ans[p][2] <= n - 2) continue;
            int t = 0;
            if (ans[p][2] == n - 1) 
                t = ans[p][0];
            else 
                t = ans[p][1];
            while (t--) res *= p;
        }
        cout << res;
    }

    题解三

    对每个数做因子分解,取所有出现次数 ≥ $n - 1$ 的因子的 $lcm$ 。

    证明

    同思路一。

    代码

    #include <bits/stdc++.h>
    using ll = long long;
    using namespace std;
    
    const int N = 2e5 + 100;
    int cnt[N];
    
    int main() {
        int n; cin >> n;
        for (int i = 0; i < n; i++) {
            int x; cin >> x;
            for (int j = 1; j * j <= x; j++) {
                if (x % j == 0) {
                    ++cnt[j];
                    if (x / j != j) ++cnt[x / j];
                }
            }
        }
        ll res = 1;
        for (ll i = 1; i < N; i++)
            if (cnt[i] >= n - 1)
                res = res * i / __gcd(res, i);
        cout << res;
    }

    D. Orac and Medians

    题意

    有一个大小为 $n$ 的数组,每次可选取一段连续区间将其中的元素都替换为该区间第 $lfloor frac{len + 1}{2} floor$ 小的元素,问能否将数组中的所有元素都替换为 $k$ 。

    题解

    首先数组中需要有 $k$,其次,如果有两个相邻的数不小于 $k$,我们就可以不断地取长为 $3$ 的区间将所有元素都替换为不小于 $k$ 的元素,然后再取长为 $2$ 的区间将所有元素替换为 $k$ 。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 1e5 + 100;
    int a[N];
    
    bool solve() {
        int n, k; cin >> n >> k;
        bool find = false;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            if (a[i] < k) a[i] = 0;
            else if (a[i] == k) a[i] = 1, find = true;
            else a[i] = 2;
        }
        if (!find) return 0;
        if (n == 1) return 1;
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n and j - i <= 2; j++)
                if (a[i] and a[j]) return 1;
        return 0;
    }
    
    int main() {
        int t; cin >> t;
        while (t--) cout << (solve() ? "yes" : "no") << "
    ";
    }

    E. Orac and Game of Life

    题意

    有一个由黑白方块组成的网格,每轮每个方块变换规则如下:

    • 如果该方块不与同色方块相邻,保持原色。
    • 否则,下一轮中该方块改变颜色。

    问在第 $p$ 轮中某个方块是什么颜色。

    题解

    如果有一对相邻方块同色,那么它们接下来每轮都会改变颜色。

    所以求出每个方块与相邻方块同色至少需要多少轮即可。

    代码

    #include <bits/stdc++.h>
    using ll = long long;
    using namespace std;
    
    const ll INF = 2e18;
    const int N = 1010;
    const int dir[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
    string s[N];
    int n, m;
    ll dis[N][N];
    queue<pair<int, int>> que;
    
    bool checkCell(int x, int y) {
        return 0 <= x and x < n and 0 <= y and y <= m;
    }
    
    void dfs() {
        for (int x = 0; x < n; x++) {
            for (int y = 0; y < m; y++) {
                dis[x][y] = INF;
                for (int i = 0; i < 4; i++) {
                    int nx = x + dir[i][0], ny = y + dir[i][1];
                    if (!checkCell(nx, ny)) continue;
                    if (s[x][y] == s[nx][ny]) dis[x][y] = 0;
                }
                if (dis[x][y] == 0) que.push({x, y});
            }
        }
    }
    
    void bfs() {
        while (!que.empty()) {
            int x = que.front().first, y = que.front().second;
            que.pop();
            for (int i = 0; i < 4; i++) {
                int nx = x + dir[i][0], ny = y + dir[i][1];
                if (!checkCell(nx, ny)) continue;
                if (dis[nx][ny] <= dis[x][y] + 1) continue;
                dis[nx][ny] = dis[x][y] + 1;
                que.push({nx, ny});
            }
        }
    }
    
    int main() {
        int k; cin >> n >> m >> k;
        for (int i = 0; i < n; i++) {
            cin >> s[i];
        }
        dfs(), bfs();
        for (int i = 0; i < k; i++) {
            ll x, y, t; cin >> x >> y >> t;
            --x, --y;
            char ans = s[x][y];
            if (t >= dis[x][y] and (t - dis[x][y]) % 2 == 1) ans ^= 1;
            cout << ans << "
    ";
        }
    }

    参考了:CKang 、scott_wu 、tourist Um_nik 的代码。

  • 相关阅读:
    nyoj-115-城市平乱(dijkstra算法)
    如何在大学里脱颖而出(其二)
    P6880-[JOI 2020 Final]オリンピックバス【最短路】
    P6847-[CEOI2019]Magic Tree【dp,线段树合并】
    P6800-[模板]Chirp Z-Transform【NTT】
    P5470-[NOI2019]序列【模拟费用流】
    P6563-[SBCOI2020]一直在你身旁【dp,单调队列】
    CF587F-Duff is Mad【AC自动机,根号分治】
    P7405-[JOI 2021 Final]雪玉【二分】
    互斥锁,IPC队列
  • 原文地址:https://www.cnblogs.com/Kanoon/p/12884421.html
Copyright © 2011-2022 走看看