zoukankan      html  css  js  c++  java
  • Knights0.

    Knights

    t数轴上有n个骑士位于1,2,3,...n,移动速度相同,初始移动方向已知,当两个骑士相遇时,各有50%的概率赢,输了就死了,并且移动到0和n+1的位置时移动方向会翻转,问最右的骑士存活的概率。

    首先,不能用一维dp。然后就是用(f[i][j])表示前i割其实有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;  //肯定会回头走 
    		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
    ", ca++, 
    			(f[n][1]*inv2%mod));
    	}
    	return 0;
    }
    
  • 相关阅读:
    CF161D Distance in Tree
    [WC2010]重建计划 长链剖分
    [FJOI2014]最短路径树问题 长链剖分
    [Vani有约会]雨天的尾巴 线段树合并
    Friend Links
    Nerdtree+高亮+图标配置
    【CF1416C】XOR Inverse
    01-Trie 学习
    【[USACO19DEC】Milk Visits G
    【ARC069D】Flags
  • 原文地址:https://www.cnblogs.com/MyNameIsPc/p/9480899.html
Copyright © 2011-2022 走看看