HDU6822:Paperfolding——題解
阿新 • • 發佈:2020-08-04
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') usingnamespace 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/+
+++++++++++++++++++++++++++++++++++++++++++