牛客2018多校训练------Filling pools(组合数)

链接:https://www.nowcoder.com/acm/contest/146/B
来源:牛客网
 

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Niuniu is interested in a game. The goal of the game is to fill a pool with water. The pool is a n*n square. Every cell of the pool is either empty or filled with water. All the cells are empty at the beginning. NiuNiu has to choose exactly one cell in each row and each column to fill with water. Every moment, for every cell, if there're at least 2 adjacent cells filled with water, the cell will be filled with water too. Note that two cells are adjacent if and only if they share a side. Niuniu wants to calculate the number of ways to fill the pool. The answer may be large, so you only need to calculate it modulo 998244353.

输入描述:

There’s only one number n in the only row. (1 ≤ n < 262144)

输出描述:

You should print exactly one number, which is the answer modulo 998244353.

 

示例1

输入

3

输出

6

示例2

输入

4

输出

22

说明

There're 2 ways which cannot fill the pool.

{(1,3),(2,1),(3,4),(4,2)}

{(1,2),(2,4),(3,1),(4,3)}

示例3

输入

50

输出

780401176

 

 n*m的水池,最开始所有的格子都为空。在几个格子中注入水,每一行每一列最多放一个格子。如果一个格子有两个相邻的格子里都有水,那这个格子也会自动装满水。问最初注水时有多少种方案数使最终水池里充满水。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=998244353;
const ll MAXN=262144+5;
 
ll a[MAXN];
ll fact[MAXN],ifact[MAXN];//fact[i]是i的阶乘,ifact[i]是阶乘的除法逆元,两者用于求组合数
 
ll pow_mod(ll n,ll k,ll mod) //快速幂求n^k余m的结果
{
    ll res=1;
    n=n%mod;
    while(k>0)
    {
        if(k&1)
            res=res*n%mod;
        n=n*n%mod;
        k>>=1;
    }
    return res;
}
 
void init()//初始化
{
    fact[0]=ifact[0]=1;
    for(int i=1;i<MAXN;++i)
    {
        fact[i]=(fact[i-1]*i)%MOD;
        ifact[i]=pow_mod(fact[i],MOD-2,MOD);
    }
}
 
ll C(ll n,ll m)//求组合数
{
    if(n<m||m<0)
        return 0;
    return  fact[n]*ifact[m]%MOD*ifact[n-m]%MOD;
}
 
int main()
{
    init();
    ll n;
    while(scanf("%lld",&n)!=EOF)
    {
        if(n==1)
            cout<<"1"<<endl;
        else
        {
            n--;
            ll ans=0;
            for(int k=0;k<=n;k++)
            {
                ans=(ans+(pow_mod(2,k,MOD)*C(n,k)%MOD)*C(n,k-1)%MOD)%MOD;
            }
            cout<<ans*pow_mod(n,MOD-2,MOD)%MOD<<endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值