Codeforces Round #641 (Div. 1) 题解

作者: xht37 分类: 题解 发布时间: 2020-05-13 00:22

Visits: 418

Codeforces Round #641 (Div. 1)

Orac and LCM

每个质因子考虑一下即可。

namespace shai {
    const int N = 1e6 + 7;
    int p[N], v[N], phi[N], miu[N];

    inline void init(int n) {
        v[1] = phi[1] = miu[1] = 1;
        for (int i = 2; i <= n; i++) {
            if (!v[i]) p[++p[0]] = v[i] = i, phi[i] = i - 1, miu[i] = -1;
            for (int j = 1; j <= p[0] && i * p[j] <= n && p[j] <= v[i]; j++)
                v[i*p[j]] = p[j],
                phi[i*p[j]] = phi[i] * (p[j] - 1 + (p[j] == v[i])),
                miu[i*p[j]] = p[j] == v[i] ? 0 : -miu[i];
        }
    }
}
using shai::p;

const int N = 1e5 + 7, M = 2e5 + 7;
int n, a[N], c[M];
pq<int> q[M];

inline vector<pair<ll, int>> divide(ll n) {
    vector<pair<ll, int>> p;
    for (ll i = 2; i * i <= n; i++)
        if (!(n % i)) {
            p.pb(mp(i, 0));
            while (!(n % i)) ++p.back().se, n /= i;
        }
    if (n > 1) p.pb(mp(n, 1));
    return p;
}

int main() {
    rd(n), rda(a, n);
    shai::init(2e5);
    for (int i = 1; i <= n; i++) {
        auto p = divide(a[i]);
        for (auto o : p) {
            q[o.fi].push(o.se);
            if (q[o.fi].size() > 2u) q[o.fi].pop();
            ++c[o.fi];
        }
    }
    ll ans = 1;
    for (int i = 1; i <= p[0]; i++) {
        int x = p[i];
        if (c[x] == n) {
            int y = q[x].top();
            while (y--) ans *= x;
        }
        if (c[x] == n - 1) {
            if (q[x].size() > 1u) q[x].pop();
            int y = q[x].top();
            while (y--) ans *= x;
        }
    }
    print(ans);
    return 0;
}

Orac and Medians

首先判一些特殊情况。

一般情况就是判是否存在连续三个数中有两个 $\ge k$。

const int N = 1e5 + 7;
int n, k, a[N];

inline void solve() {
    rd(n, k), rda(a, n);
    vi p;
    for (int i = 1; i <= n; i++)
        if (a[i] == k) p.pb(i);
    if (!p.size()) return prints("no");
    if (n == 1) return prints("yes");
    if (n == 2) return prints(a[1] >= k && a[2] >= k ? "yes" : "no");
    for (int i = 1; i <= n - 2; i++) {
        int t = (a[i] >= k) + (a[i+1] >= k) + (a[i+2] >= k);
        if (t >= 2) return prints("yes");
    }
    prints("no");
}

int main() {
    int T;
    rd(T);
    while (T--) solve();
    return 0;
}

Orac and Game of Life

随便 BFS 一下。

const int N = 1e3 + 7;
ll inf = 1e18 + 1;
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
int n, m, t;
ll d[N][N];
char s[N][N];

int main() {
    rd(n, m, t);
    for (int i = 1; i <= n; i++) rds(s[i], m);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            d[i][j] = inf;
    queue<pi> q;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            bool ok = 0;
            for (int o = 0; o < 4; o++) {
                int x = i + dx[o], y = j + dy[o];
                if (x < 1 || x > n || y < 1 || y > m) continue;
                if (s[x][y] == s[i][j]) ok = 1;
            }
            if (ok) d[i][j] = 0, q.push(mp(i, j));
        }
    while (q.size()) {
        int i = q.front().fi, j = q.front().se;
        q.pop();
        for (int o = 0; o < 4; o++) {
            int x = i + dx[o], y = j + dy[o];
            if (x < 1 || x > n || y < 1 || y > m) continue;
            if (d[x][y] > d[i][j] + 1) d[x][y] = d[i][j] + 1, q.push(mp(x, y));
        }
    }
    while (t--) {
        int x, y;
        ll z;
        rd(x, y);
        rd(z);
        if (z <= d[x][y]) print(s[x][y] - '0');
        else {
            int k = s[x][y] - '0';
            z -= d[x][y];
            k ^= z & 1;
            print(k);
        }
    }
    return 0;
}

Slime and Biscuits

题目太神仙了,先咕咕咕着。

Slime and Hats

题目太神仙了,先咕咕咕着。

Slime and Sequences

题目太神仙了,先咕咕咕着。

发表评论

电子邮件地址不会被公开。 必填项已用*标注