天天看點

Codeforces Round #589 (Div. 2)

比賽連結:Codeforces Round #589 (Div. 2)

官方題解:Codeforces Round #589 (Div. 2) Editorial

A. Distinct Digits

題意

給定兩個整數 \(l\) 和 \(r\),求一個 \(x\) 滿足 \(l \le x \le r\) 且 \(x\) 的每一位都不同。

思路

直接暴力。

代碼

#include <bits/stdc++.h>
using namespace std;

bool judge(int x) {
    int m[100] = {0};
    while(x) {
        if(m[x % 10]) {
            return false;
        }
        m[x % 10]++;
        x /= 10;
    }
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int l, r;
    while(cin >> l >> r) {
        int f = 0;
        for(int i = l; i <= r; ++i) {
            if(judge(i)) {
                f = 1;
                cout << i << endl;
                break;
            }
        }
        if(f == 0) cout << -1 << endl;
    }
    return 0;
}
           

B. Filling the Grid

給定一個 \(h \times w\) 的矩形,以及 \(h\) 個 \(r\) 和 \(w\) 個 \(c\)。

\(r_i\) 表示第 \(i\) 行左邊界從左往右連續的黑色格子的是 \(r_i\) 個。

\(c_i\) 表示第 \(i\) 列上邊界從上往下連續的黑色格子的是 \(c_i\) 個。

給出 \(h, w, r[], c[]\),求可以構造出多少種矩形滿足條件。

模拟

對每行每列模拟,填充黑色格子和白色格子(黑色格子旁邊一個格子一定是白色),如果行列有沖突就輸出零,否則找出所有的沒填充的格子的個數 \(cnt\),答案為 \(2 ^ {cnt} \mod 10^9 + 7\)。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const ll maxn = 1e3 + 10;

ll c[maxn], r[maxn];
ll g[maxn][maxn];

ll qmod(ll a, ll b, ll m) {
    a %= m;
    ll res = 1;
    while (b > 0) {
        if (b & 1) res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll h, w;
    cin >> h >> w;
    for(ll i = 1; i <= h; ++i) {
        cin >> r[i];
        for(ll j = 1; j <= r[i]; ++j) {
            g[i][j] = 1;
        }
        g[i][r[i] + 1] = 2;
    }
    ll f = 1;
    for(ll i = 1; i <= w; ++i) {
        cin >> c[i];
        for(ll j = 1; j <= c[i]; ++j) {
            if(g[j][i] == 2) {
                f = 0;
            }
            g[j][i] = 1;
        }
        if(g[c[i] + 1][i] == 1) {
            f = 0;
        }
        g[c[i] + 1][i] = 2;
    }
    if(f == 0) {
        cout << 0 << endl;
    } else {
        ll ans = 0;
        for(ll i = 1; i <= h; ++i) {
            for(ll j = 1; j <= w; ++j) {
                if(!g[i][j]) {
                    ++ans;
                }
            }
        }
        cout << qmod(2, ans, mod) << endl;
    }
    return 0;
}
           

C. Primes and Multiplication

\(g(x, p)\) 定義為最大的 \(p ^ k\) 滿足 \(p^k\) 整除 \(x\)。

\(f(x, y)\) 定義為所有 \(g(y, p)\) 的乘積,其中 \(p\) 是 \(x\) 的質因數。

給定 \(x\) 和 \(n\),求 \(\prod_{i=1}^n f(x,i) \mod \ (10^9 + 7)\)。

了解了 \(g(x, p)\) 的含義就容易做了。

\(g(x, p)\) 其實為 \(x\) 分解質因數後 \(p\) 出現的次數。

先求出 \(x\) 的所有質因數。然後對于每個質因數,求出 \(1\) 到 \(n\) 中這個質因數出現的次數。也就是求 \(n!\) 的這個質因數的個數。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;

vector<ll> divide(ll x) {
    vector<ll> ans;
    for(int i = 2; i <= x / i; ++i) {
        if (x % i == 0) {
            ans.push_back(i);
            while (x % i == 0) x /= i;
        }
    }
    if (x > 1) ans.push_back(x);
    return ans;
}

ll qmod(ll a, ll b, ll m) {
    if(!b) return 1 % m;
    ll ans = 1;
    while(b) {
        if(b & 1) ans = (ans * a) % m;
        a = (a * a) % m;
        b >>= 1;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll x, n;
    cin >> x >> n;
    vector<ll> p = divide(x);
    ll ans = 1;
    for(int i = 0; i < p.size(); ++i) {
        ll tmp = n;
        while(tmp >= p[i]) {
            ans = ans * qmod(p[i], tmp / p[i], mod) % mod;
            tmp /= p[i];
        }
    }
    cout << ans % mod << endl;
    return 0;
}
           

D. Complete Tripartite

給定一個無向無環圖。問能否将點集分成 \(3\) 個,使得每一個點集内的任意兩點沒有邊,每一個點集内的點與其他所有點集内的所有點都有一條邊。

哈希

特殊的三分圖。

考慮到一個集合内的所有點都與其他兩個集合内的所有點有邊,也就是一個集合内的每個點的所有邊連出去的點都是相同的,根據這個哈希,如果哈希值有 \(3\) 個,就按哈希值分類即可。

我的做法是把所有的點賦一個互不相同的正整數值。每個點的哈希值 \(h[i]\) 為它的所有邊的另外一個點的值的和。

注意判斷一下哈希值為 \(0\) 的情況。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 10;
map <ll, vector<ll>> mp;
ll e[maxn], ans[maxn], h[maxn];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    mt19937 rnd(time(0));
    ll n, m;
    cin >> n >> m;
    unordered_map<ll, int> ump;
    for(int i = 1; i <= n; ++i) {
        h[i] = rnd();
        while(ump[h[i]] || h[i] == 0) {
            h[i] = rnd();
        }
        ump[h[i]]++;
    }
    for(int i = 0; i < m; ++i) {
        ll a, b;
        cin >> a >> b;
        e[a] += h[b];
        e[b] += h[a];
    }
    int f = 1;
    for(int i = 1; i <= n; ++i) {
        if(e[i]) {
            mp[e[i]].push_back(i);
        } else {
            f = 0;
        }
    }
    if(mp.size() != 3 || f == 0) {
        cout << -1 << endl;
    } else {
        ll x = 0;
        for(auto it = mp.begin(); it != mp.end(); ++it) {
            ++x;
            for(int i = 0; i < (it->second).size(); ++i) {
                ans[(it->second)[i]] = x;
            }
        }
        for(int i = 1; i <= n; ++i) {
            cout << ans[i] << " ";
        }
        cout << endl;
    }
    return 0;
}