Gym101194H(Great Cells)-思维

题目链接:Great Cells


题意:有一张N*M的格子纸,每个格子可以填1到K之间的数。如果一个格子里的数严格大于本行的其他格子里的数,并且严格大于本列的的其他格子里的数,则这个格子叫做Great Cell。Ag表示有Ag种填法使得格子纸中恰有g个Great Cell。

求:


思路:


Gym101194H(Great Cells)-思维_第1张图片



代码:

# pragma comment(linker, "/STACK:1024000000,1024000000")
# include 
# include 
# include 
# include 
# include 
# include 
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
int n, m, k;

int fast_pow(int x, int n) {
    int r = 1;
    while (n) {
        if (n & 1) r = (ll)r * x % mod;
        x = (ll)x * x % mod;
        n >>= 1;
    }
    return r;
}

int main(void)
{
    int T, Case = 0; scanf("%d", &T);
    while (T-- && scanf("%d %d %d", &n, &m, &k)) {
        if (n == 1 && m == 1) { printf("Case #%d: %d\n", ++Case, k); continue; }
        int ans = fast_pow(k, n * m);
        for (int i = 1; i <= k; ++i) {
            ans = (ll)ans + (ll)m * n % mod * fast_pow(i - 1, n - 1 + m - 1) % mod * fast_pow(k, (n - 1) * (m - 1)) % mod;
            ans %= mod;
        }
        printf("Case #%d: %d\n", ++Case, ans);
    }

    return 0;
}


你可能感兴趣的:(Gym101194H(Great Cells)-思维)