HDU6822:Paperfolding——题解

http://acm.hdu.edu.cn/showproblem.php?pid=6822

给一张纸,随机四个方向折 $n$ 次,然后横竖切一刀,问纸片的期望个数。

题咋都是期望(

可以发现,上下折一次相当于横着切刀数翻一倍,左右折一次相当于竖着切刀数翻一倍。

设上下折次数为 $x$,则纸片数为 $(2^x+1)(2^{n-x}+1)$。

那么期望为( $P(x)$千万别求错(被坑了),然后看到组合数想到二项式定理即可以推出):$2*3^n/2^n+2^n+1$

#include<bits/stdc++.h>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
const ll p=998244353;
inline ll read(){
    ll X=0,w=0;char ch=0;
    while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
    while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
void write(ll x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
ll qpow(ll k,ll n){
    ll res=1;k%=p;
    while(n){
        if(n&1){
            res=res*k%p;
        }
        k=k*k%p;n>>=1;
    }
    return res;
}
int main(){
    int T=read();
    while(T--){
        //2*3^n/2^n+2^n+1
        ll n=read();
        ll n2=qpow(2,n);
        ll ans=2*qpow(3,n)%p;
        ans=ans*qpow(n2,p-2)%p;
        ans=(ans+n2+1)%p;
        write(ans);enter;
    }
    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

 +本文作者:luyouqi233。               +

 +欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

posted @ 2020-08-04 18:02  luyouqi233  阅读(216)  评论(0编辑  收藏  举报