Codeforces Round 900 (Div. 3) 题解 | JorbanS

A-How Much Does Daytona Cost?

string solve() {
    cin >> n >> m;
    bool flag = false;
    while (n --) {
        int x; cin >> x;
        if (x == m) flag = true;
    }
    return flag ? yes : no;
}

B-Aleksa and Stack

void solve() {
    cin >> n;
    int a = 2, b = 3;
    while (n --) {
        cout << a << ' ';
        int x = b + 1;
        while (3 * x % (a + b) == 0) x ++;
        a = b, b = x;
    }
    cout << endl;
}

C-Vasilije in Cacak

题解 只要 x x x 在最小值和最大值区间内, x x x 就一定能被满足,部分可以用 R M Q RMQ RMQ 来理解

string solve() {
    cin >> n >> k >> x;
    ll minn = (1 + k) * k / 2, maxx = (n * 2 - k + 1) * k / 2;
    return x < minn || x > maxx ? no : yes;
}

D-Reverse Madness

题解 对于每个区间分别处理,二分分类区间,每个区间用差分处理

string solve() {
    cin >> n >> k >> s;
    for (int i = 1; i <= n; i ++) a[i] = pre[i] = 0;
    for (int i = 1; i <= k; i ++) cin >> l[i];
    for (int i = 1; i <= k; i ++) cin >> r[i], lr[i] = l[i] + r[i];
    cin >> q;
    vector<vector<int>> v(n + 1);
    for (int i = 1; i <= q; i ++) {
        int x; cin >> x;
        int p = lower_bound(r + 1, r + k + 1, x) - r;
        v[p].push_back(min(x, lr[p] - x));
    }
    for (int i = 1; i <= k; i ++) {
        a[l[i]] = pre[l[i] - 1] = 0;
        for (auto L : v[i]) a[L] ++, a[lr[i] - L + 1] --;
        for (int j = l[i]; j <= lr[i] / 2; j ++) pre[j] = pre[j - 1] + a[j];
    }
    for (int i = 1; i <= k; i ++)
        for (int j = l[i]; j <= lr[i] / 2; j ++)
            if (pre[j] % 2) swap(s[j - 1], s[lr[i] - j - 1]);
    return s;
}

E-Iva & Pav

题解 R M Q RMQ RMQ,但是因为 D D D 题浪费太多时间了,没时间写了(

赛后补了后面的查询部分,一下 A C AC AC

void solve() {
    cin >> n;
    vector<vector<int>> to(n + 2, vector<int>(19));
    for (int i = 1; i <= n; i ++) cin >> f[i][0], to[i][0] = i + 1;
    for (int i = 1; i <= 17; i ++) {
        for (int j = 1; j <= n; j ++) {
            if (!to[to[j][i - 1]][i - 1]) break;
            to[j][i] = to[to[j][i - 1]][i - 1];
            f[j][i] = f[j][i - 1] & f[to[j][i - 1]][i - 1];
        }
    }
    cin >> m;
    while (m --) {
        int l, k; cin >> l >> k;
        int r = l, now = f[l][0];
        if (now < k) {
            cout << -1 << ' ';
            continue;
        }
        for (int i = 17; i >= 0; i --) {
            if (to[r][i] && (now & f[r][i]) >= k) {
                now &= f[r][i];
                r = to[r][i];
            }
        }
        cout << r - 1 << ' ';
    }
    cout << endl;
}

F - Vasilije Loves Number Theory

题解 对于 n = p 1 x 1 ⋅ p 2 x 2 … n=p_{1}^{x_{1}}·p_{2}^{x_{2}}… n=p1x1p2x2 而言, d ( n ) = ( x 1 + 1 ) ⋅ ( x 2 + 1 ) … d(n)=(x_{1}+1)·(x_{2}+1)… d(n)=(x1+1)(x2+1)

对于两数 a , b a,b a,b g c d ( a , b ) = 1 gcd(a,b)=1 gcd(a,b)=1 时, d ( n ) d(n) d(n) 是个积性函数

因为要判断 d ( a ⋅ n ) = n d(a·n)=n d(an)=n a a a 是否存在,而且有 g c d ( a , n ) = 1 gcd(a,n)=1 gcd(a,n)=1,说明 n n n 乘上 a a a 计算 d ( a ⋅ n ) d(a·n) d(an) 值时, a a a 不能修改 n n n 原有因子的数量,因此 d ( a ⋅ n ) = d ( a ) ⋅ d ( n ) d(a·n)=d(a)·d(n) d(an)=d(a)d(n),也就是说只要 d ( n ) ∣ n d(n)|n d(n)n 就一定能找到 a a a

由于题目数据范围 d ( n ) ≤ 1 0 9 d(n)\le 10^9 d(n)109,故 n n n 会爆 l o n g   l o n g long~long long long

所以不能直接判断 n % d ( n ) n\%d(n) n%d(n) 是否为 0 0 0 来判断,因此对 ∏ n \prod n n 逐步除以 g c d ( n , d ( ∏ n ) ) gcd(n,d(\prod n)) gcd(n,d(n)) 即可,若结果为 1 1 1 则符合

vector<int> primes;

void getPrimes() {
    vector<bool> vis(1e6 + 1);
    for (int i = 2; i <= 1e6; i ++) {
        if (!vis[i]) primes.push_back(i);
        for (int x : primes) {
            if (x * i > 1e6) break;
            vis[x * i] = true;
            if (x % i == 0) break;
        }
    }
}

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

void solve() {
    int n, m; cin >> n >> m;
    map<int, int> mp, mpN;
    auto getDivisors = [&](int x) -> void {
        for (auto i : primes) {
            if (i > x) break;
            while (x % i == 0) mp[i] ++, x /= i;
        }
        if (x != 1) mp[x] ++;
    };
    getDivisors(n);
    mpN = mp;
    vector<int> a;
    a.push_back(n);
    while (m --) {
        int op; cin >> op;
        if (op == 1) {
            int x; cin >> x;
            getDivisors(x);
            int res = 1;
            for (auto [u, v] : mp) res *= v + 1;
            a.push_back(x);
            for (auto it : a) res /= gcd(res, it);
            cout << (res == 1 ? yes : no) << endl;
        } else {
            mp = mpN;
            a.clear();
            a.push_back(n);
        }
    }
    cout << endl;
}

int main() {
    FastIO
    getPrimes();
    Cases
    solve();
    return 0;
}

你可能感兴趣的:(OI,题解,算法)