Codeforces #144 (Div. 1) B. Table (组合数学+dp)

本文探讨了一个关于在特定矩阵中放置点的计数问题。给定一个n*m的矩阵,目标是在每个n*n的子矩阵中精确放置k个点,并计算所有可能的放置方式的数量。文章提供了详细的解题思路及C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

B.Table

题意:

\(n*m\)的矩阵使每个\(n*n\)矩阵里面准确包含\(k\)个点,问你有多少种放法。

\((1 ≤ n ≤ 100; n ≤ m ≤ 10^{18}; 0 ≤ k ≤ n^2)\)

题解:

- Let \(s_i\) number of points in the column \(i\).

1191408-20171012123626715-653303150.png

- Two neighboring squares are drawn at this picture, \(A\) is the number of point it the left area (it is one column), \(B\) is the number of points in the middle area and \(C\) is the number of points in the right area (it is one column too). That's why by definition we ###have:

1191408-20171012123726543-1109557845.png

- Therefore \(A = C\).

- That's why 1191408-20171012123846965-539230669.png

- Divide all columns by equivalence classes on the basis of \(i \mod n\) . For all \(a\) and \(b\) from one class \(s_a = s_b\).

cnta is number of columns in class with 1191408-20171012123932887-468857184.png

- There are \(C(n,k)^{cnt_a}\) ways to draw \(k\) points in the each of columns in the class \(a\) independendently of the other classes.

- \(dp[i][j]\) is number of ways to fill all columns in classes \(1, ... i\) in such way that .

1191408-20171012124050777-403784516.png

- \(cnt_i\) take only two values 1191408-20171012124148465-1365306960.png and 1191408-20171012124156949-1729697401.png

. Let's calc \(C(n,a)^{cnt_i}\) for all \(a\) and \(cnt_i\) and use it to calc our dp. We have \(O(n^2·k)\) complexity.

代码:

#include<bits/stdc++.h>
#pragma GCC optimize ("O3")
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
using namespace std;
const int N = 123;
const int K = 10000+1230;
ll pow1[N],pow2[N];
ll dp[N][K];
ll c[N][N];

//n*m的矩阵使每个n*n矩阵里面准确包含k个点,问你有多少种放法。

ll quick_pow(ll a,ll b)
{
    ll tmp=a;
    ll ans=1;
    while(b)
    {
        if(b&1) ans=(ans*tmp)%mod;
        tmp=(tmp*tmp)%mod;
        b>>=1;
    }
    return ans;
}
int main()
{
    int n,k;
    ll m;
    cin>>n>>m>>k;
    for(int i=0;i<=n;i++) c[i][0]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
        }
    }             
    for(int i=0;i<=n;i++)
    {
        pow1[i]=quick_pow(c[n][i],m/n);
        pow2[i]=(pow1[i]*c[n][i])%mod;
    }
    dp[0][0]=1;
    ll now;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=k;j++)
        {
            if(dp[i][j]!=0)
            {
                for(int p = 0;p <= n && j + p <= k;p++)
                {
                    if(i<m%n) now=pow2[p];
                    else now=pow1[p];
                    
                    dp[i+1][j+p]=(dp[i+1][j+p]+dp[i][j]*now)%mod;
                }
            }
        }
            
    }     
    cout<<dp[n][k]<<endl;
    return 0;
}

转载于:https://www.cnblogs.com/LzyRapx/p/7655564.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值