Filling pools

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

题目描述

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

输入

复制

扫描二维码关注公众号,回复: 2831089 查看本文章
50

输出

复制

780401176
#include<bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i=a;i<b;i++)
typedef long long ll;
bool is_prime(int x){if(x<2)return false;int m=sqrt(x+0.5);rep(i,2,m+1)if(x%i==0)return false;return true;}

const int mod=998244353;
const int maxn=3e5;


//大组合数取模(当n,m大于p的时候也能用)
//begin
ll pow_mod(ll base,int n,int mod){
	ll ans=1;
	while(n){
		if(n&1)ans=(ans*base)%mod;
		base=(base*base)%mod;
		n>>=1;
	}
	return ans%mod;
}
ll F[maxn*2],inv[maxn];
void init(){
    rep(i,1,maxn)inv[i]=pow_mod(i,mod-2,mod);
	F[0]=1;for(int i=1;i<maxn*2;i++)F[i]=F[i-1]*i%mod;
}
//注意求逆元的时候用费马小定理前提:mod是素数
ll C(int n,int k){
     ll ans=(F[k]*F[n-k])%mod;
	 ans=pow_mod(ans,mod-2,mod);
	 return (ans*F[n])%mod;
}
ll Lucas(int n,int m){
	return m==0?1:(C(n%mod,m%mod)*Lucas(n/mod,m/mod))%mod;
}
//end



//a(n) = sum (k = 0..n, C(n+k, n)*C(n, k)/(k+1)).
int main(){
    init();
    int n;
    scanf("%d",&n);
    n--;
    ll ans=0;
    rep(k,0,n+1)  ans=(ans+Lucas(n+k,n)*Lucas(n,k)%mod*inv[k+1]%mod)%mod;
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/81606208