• [ABC127E] Cell Distance


    • 原题链接
    • 题意:给出 (n imes m) 的矩阵,都小于 (2e5) ,求从 (n imes m) 个矩阵中拿出 (k <= n imes m) 个点,在所有方案中,求每个方案中,每个点距离其他点的曼哈顿距离之和,然后求所有方案的各个距离之和的总和。
    • 题解:枚举横着两个点的距离为 (1 <= lenx <= n) 然后,其贡献为 (lenx imes ( n - lenx) imes m^2),然后对答案的贡献值就是显然从所有中选出 (k-2) 个的方案数乘贡献,即 $$sum_{lenx = 1}^{n} (nm-2, k-2) imes lenx imes ( n - lenx) imes m^2$$
      所以同理,这只是算了一半的答案,另一半则是 $$sum_{leny = 1}^{n} (nm-2, k-2) imes leny imes ( m - leny ) imes n^2$$
    • 代码:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    const int N = 200009;
    typedef long long ll;
    const ll mod = 1000000007;
    ll fac[N];
    ll q_mi(ll a, ll k, ll mod) {
        ll ret = 1;
        ll x = a;
        while (k) {
            if (k & 1) (ret *= x) %= mod;
            (x *= x) %= mod;
            k >>= 1;
        }
        return ret;
    }
    ll inv(ll a) { return (q_mi(a, mod - 2, mod) % mod + mod) % mod; }
    ll C(ll n, ll m) {return fac[n] * (inv(fac[m])) % mod * inv(fac[n-m]) % mod;}
    void init() {
        fac[0] = 1;
        for (int i = 1; i < N; i++) {
            (fac[i] = fac[i-1] * i) %= mod;
        }
    }
    
    
    void solve() {
        ll n, m, k;cin >> n >> m >> k;
        ll ans = 0;
        for (ll i = 1; i <= n; i++) {
            (ans += C(n*m-2, k-2) * (n - i) % mod * m % mod * m % mod  * i%mod) %= mod;
        }
        for (ll i = 1; i <= m; i++) {
             (ans += C(n*m-2, k-2) * (m - i) % mod * n % mod * n % mod  * i%mod) %= mod;
        }
        cout << ans << endl;
    }
    signed main() {
        ios::sync_with_stdio(0);
        init();
        ll t = 1;//cin >> t;
        while (t--)solve();
        return 0;
    }
    
    
  • 相关阅读:
    Windows下好用的git客户端--GitExtentions
    高分辨率下放大netbeans中的小图标
    小书匠使用手册
    win8 telnet VirtualBox中的redhat9
    win8安装新字体
    netbeans设置字体
    win7下Chrome有两个图标的解决方法
    【转】HDU-6035-Colorful Tree
    POJ1703--Find them, Catch them(种类并查集)
    KMP的妙用(利用next数组寻找字符串的循环节)
  • 原文地址:https://www.cnblogs.com/Xiao-yan/p/14551672.html
Copyright © 2020-2023  润新知