Knights0.
Knights
t數(shù)軸上有n個(gè)騎士位于1,2,3,...n,移動(dòng)速度相同,初始移動(dòng)方向已知,當(dāng)兩個(gè)騎士相遇時(shí),各有50%的概率贏,輸了就死了,并且移動(dòng)到0和n+1的位置時(shí)移動(dòng)方向會(huì)翻轉(zhuǎn),問最右的騎士存活的概率。
首先,不能用一維dp。然后就是用\(f[i][j]\)表示前i割其實(shí)有j割向右走的概率。
#include <cstdio> #include <cstring> using namespace std;typedef long long LL; const LL maxn=1005, mod=1e9+7; LL T, n, a[maxn], ca, inv2; LL f[maxn][maxn];LL fpow(LL a, LL x){LL ans=1, base=a;for (; x; x>>=1, base*=base, base%=mod)if (x&1) ans*=base, ans%=mod;return ans; }int main(){scanf("%lld", &T); inv2=fpow(2, mod-2);while (T--){scanf("%lld", &n);for (LL i=1; i<=n; ++i) scanf("%lld", &a[i]);a[1]=1; a[n]=0; //肯定會(huì)回頭走 memset(f, 0, sizeof(f));f[0][0]=1;for (LL i=1; i<=n; ++i)for (LL j=i; j>0; --j){if (a[i]){ f[i][j]=f[i-1][j-1]; continue; }f[i][j]=(f[i][j+1]+f[i-1][j])*inv2%mod;if (j==1) f[i][j]=(f[i][j+1]+f[i-1][j])%mod;}printf("Case #%lld: %lld\n", ca++, (f[n][1]*inv2%mod));}return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/MyNameIsPc/p/9480899.html
總結(jié)
- 上一篇: 解决VS2017引用报错问题
- 下一篇: 在这个智能数字时代,处处拉拢着我们的视线