codeforces round 589

题意

给定一个 h×wh×w 的矩形,以及 hh 个 rr 和 ww 个 cc。

riri 表示第 ii 行左边界从左往右连续的黑色格子的是 riri 个。

cici 表示第 ii 列上边界从上往下连续的黑色格子的是 cici 个。

给出 h,w,r[],c[]h,w,r[],c[],求可以构造出多少种矩形满足条件。

思路

模拟

对每行每列模拟,填充黑色格子和白色格子(黑色格子旁边一个格子一定是白色),如果行列有冲突就输出零,否则找出所有的没填充的格子的个数 cntcnt,答案为 2cntmod109+72cntmod109+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

题意

g(x,p)g(x,p) 定义为最大的 pkpk 满足 pkpk 整除 xx。

f(x,y)f(x,y) 定义为所有 g(y,p)g(y,p) 的乘积,其中 pp 是 xx 的质因数。

给定 xx 和 nn,求 ni=1f(x,i)mod (109+7)∏i=1nf(x,i)mod (109+7)。

思路

理解了 g(x,p)g(x,p) 的含义就容易做了。

g(x,p)g(x,p) 其实为 xx 分解质因数后 pp 出现的次数。

先求出 xx 的所有质因数。然后对于每个质因数,求出 11 到 nn 中这个质因数出现的次数。也就是求 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;
}

  

原文地址:https://www.cnblogs.com/hgangang/p/12436171.html