Cell Distance(数论)

题目描述
We have a grid of squares with N rows and M columns. Let (i,j) denote the square at the i-th row from the top and j-th column from the left. We will choose K of the squares and put a piece on each of them.
If we place the K pieces on squares (x1,y1), (x2,y2), …, and (xK,yK), the cost of this arrangement is computed as:
在这里插入图片描述
Find the sum of the costs of all possible arrangements of the pieces. Since this value can be tremendous, print it modulo 109+7.
We consider two arrangements of the pieces different if and only if there is a square that contains a piece in one of the arrangements but not in the other.

Constraints
·2≤N×M≤2×105
·2≤K≤N×M
·All values in input are integers.

输入
Input is given from Standard Input in the following format:

N M K

输出
Print the sum of the costs of all possible arrangements of the pieces, modulo 109+7.

样例输入
2 2 2

样例输出
8

思路
本题通过计算两个块之间的曼哈顿距离来计算总贡献,因此可以通过已知结论任意两个块之间的曼哈顿距离的平均值为(n+m)/3来求得结果,已知从nm个块中任意选择k个块为C(nm,k),从k个块中任意选择2个块为
C(k,2),所以结果为C(n * m,k) * C(k,2) * (n+m)/3

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=200005;
const int M=4005;
const ll INF=1e14;
const ull sed=31;
const ll mod=1e9+7;
const double eps=1e-12;
typedef pair<int,int>P;
 
ll n,m,k;
ll fac[N],inv[N];
 
ll qpow(ll a,ll b)
{
    ll ret=1;
    while(b)
    {
        if(b&1) ret=ret*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ret;
}
 
void init()
{
    fac[0]=1;
    for(int i=1;i<N;i++) fac[i]=fac[i-1]*i%mod;
    inv[N-1]=qpow(fac[N-1],mod-2);
    for(int i=N-2;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
}
 
ll C(ll a,ll b)
{
    return fac[a]*inv[b]%mod*inv[a-b]%mod;
}
int main()
{
    init();
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m>>k;
    cout<<((C(n*m,k)*C(k,2)%mod)*((n+m)*qpow(3,mod-2)%mod))%mod<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值