[2016-2017 ACM-ICPC CHINA-Final]-Problem H. Great Cells(贡献,组合数学)

[2016-2017 ACM-ICPC CHINA-Final]-Problem H. Great Cells

题面:

题意:

给定\(n,m,k\)三个数,代表有一个\(n*m\)的方格,每一个方格中有一个在范围\([1,k]\)的数,

其中,若一个数\(a_{i,j}\),比其所在行和所在列中其他的都大,则成为该方格是一个great cell。

\(A_g\)代表方格中一共有\(\mathit g\)个great cell的方案数。

现在让您编写一个程序求出:

\[\sum_{g=0}^{n*m}(g+1)*A_g\ mod \ (10^9+7) \]

思路:

\(n>m\)

\[\sum_{g=0}^{n*m}(g+1)*A_g\ mod \ (10^9+7) \\ =\sum_{g=0}^{n}(g+1)*A_g\ mod \ (10^9+7) \\ =(\sum_{g=0}^{n}(g)*A_g+\sum_{g=0}^{n}A_g )\ mod \ (10^9+7) \\ =(\sum_{g=0}^{n}(g)*A_g+k^{n*m} )\ mod \ (10^9+7) \\ =(\sum_{i=1}^{n}\sum_{j=1}^{m}contrib(i,j)+k^{n*m} )\ mod \ (10^9+7) \\ \]

其中\(contrib(i,j)\)\(a_{i,j}\)为great cell时对答案的贡献,

容易发现对于\(i\in[1,n],j\in[1,m]\)\(contrib(i,j)\)都是相等的。

所以有:

\[(\sum_{i=1}^{n}\sum_{j=1}^{m}contrib(i,j)+k^{n*m} )\ mod \ (10^9+7) \\ =(Contrib*n*m+k^{n*m} )\ mod \ (10^9+7) \\ \]

对于某个cell为great cell的贡献为:

\[Contrib = \sum_{i=0}^{k-1} i^{(n-1+m-1)} * k^{(n-1)*(m-1)} \]

至此,本题就已经解决了。

时间复杂度为:\(O(T\times k\times log_2(nm))\)

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n, m, k;
const ll mod = 1e9 + 7ll;
ll dp[202][202][2];
int main()
{
#if DEBUG_Switch
    freopen("D:\\code\\input.txt", "r", stdin);
#endif
    //freopen("D:\\code\\output.txt","w",stdout);
    int t;
    t = readint();
    for (int icase = 1; icase <= t; ++icase) {
        n = readint(); m = readint(); k = readint();
        if (n > m) {
            swap(n, m);
        }
        ll val = 0ll;
        repd(i, 0, k - 1) {
            val = (val + powmod(i, n - 1 + m - 1, mod)) % mod;
        }
        val = val * powmod(k, (n - 1) * (m - 1), mod) % mod;
        ll ans = val * n % mod * m % mod + powmod(k, n * m, mod);
        ans %= mod;
        printf("Case #%d: %lld\n", icase, ans );
    }

    return 0;
}



posted @ 2020-12-02 16:33  茄子Min  阅读(130)  评论(0编辑  收藏  举报