• Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)


    E. Explosion Exploit
    time limit per test
    2.0 s
    memory limit per test
    256 MB
    input
    standard input
    output
    standard output

    In a two player card game, you have nn minions on the board and the opponent has mm minions. Each minion has a health between 11 and 66.

    You are contemplating your next move. You want to play an "Explosion" spell which deals dd units of damage randomly distributed across all minions. The damage is dealt one unit at a time to some remaining minion on the board. Each living minion (including your own) has the same chance of receiving each unit of damage. When a minion receives a unit of damage, its health is decreased by one. As soon as the health of a minion reaches zero, it is immediately removed from the board, before the next damage is dealt. If there are no minions left on the board, any excess damage caused by the spell is ignored.

    Given the current health of all minions, what is the probability that the Explosion will remove all of the opponent's minions? Note that it does not matter if all your own minions die in the process as well, and the damage continues to be dealt even if all your own minions are gone.

    Input

    The first line of input contains the three integers nn, mm, and dd (1n,m51≤n,m≤5, 1d1001≤d≤100). Then follows a line containing nn integers, the current health of all your minions. Finally, the third line contains mm integers, the current health of all the opponent's minions. All healths are between 11 and 66 (inclusive).

    Output

    Output the probability that the Explosion removes all the opponent's minions, accurate up to an absolute error of 10610−6.

    Examples
    input
    Copy
    1 2 2
    2
    1 1
    output
    Copy
    0.33333333
    input
    Copy
    2 3 12
    3 2
    4 2 3
    output
    Copy
    0.13773809

    题意:
    我方有n个人,敌方有m个人,你可以攻击d次。每次等概率击中一个血量不为0的人,被击中的人掉一点血。
    问最后敌方全部阵亡的概率。
    解法:
    记忆化搜索+状压。
    假如己方有三个人,血量2 3 3 和3 2 3 要看成同一种状态,否者会TLE或者会MLE
    对敌方也是如此,但是不可以把敌我混起来。
    代码:
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    #define fuck(x) cout<<#x<<" = "<<x<<endl;
    #define ls (t<<1)
    #define rs ((t<<1)+1)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 100086;
    const int inf = 2.1e9;
    const ll Inf = 999999999999999999;
    const int mod = 1000000007;
    const double eps = 1e-6;
    const double pi = acos(-1);
    int hp1[10],hp2[10];
    int n,m,k;
    map<ll,double>dp;
    ll over;
    ll get_state(){
        ll ans=0;
        for(int i=1;i<=6;i++){
            ans=ans*7+hp2[i];
        }
        for(int i=1;i<=6;i++){
            ans=ans*7+hp1[i];
        }
        return ans;
    }
    double dfs(ll state,ll t){
        if(dp.count(state)){return dp[state];}
        double &res=dp[state];
        if(state<over){return res=1.0;}
        if(t==0){return res=0.0;}
        int ans=0;
        for(int i=1;i<=6;i++){
            if(!hp1[i]){continue;}
            ans+=hp1[i];
            hp1[i]--;hp1[i-1]++;
            ll new_state=get_state();
            res+=(hp1[i]+1)*dfs(new_state,t-1);
            hp1[i]++;hp1[i-1]--;
        }
        for(int i=1;i<=6;i++){
            if(!hp2[i]){continue;}
            ans+=hp2[i];
            hp2[i]--;hp2[i-1]++;
            ll new_state=get_state();
            res+=(hp2[i]+1)*dfs(new_state,t-1);
            hp2[i]++;hp2[i-1]--;
        }
        res/=ans;
        return res;
    }
    int main(){
        scanf("%d%d%d",&n,&m,&k);
        int x;
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            hp1[x]++;
        }
        for(int i=1;i<=m;i++){
            scanf("%d",&x);
            hp2[x]++;
        }
        for(int i=1;i<=6;i++){
            over=over*7+6;
        }
        printf("%.8f",dfs(get_state(),k));
        return 0;
    }
    View Code
  • 相关阅读:
    运行pyzbar时报 Could not find module ‘libzbar-64.dll ‘or one of its dependenci的错误
    [Tips] pandas 如何根据index进行slice
    [Tips] pandas逐行遍历
    [Tips] Docker build
    [Tips] redis的key自动过期策略
    [Tips] No module named ipykernel错误
    [Tips] mac下anconda python环境中site-package的位置
    [Tips] pandas获取每月最后一天
    [Tips] docker的daemon.json设置私有镜像仓库
    [Tips] nginx端口转发
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/10518850.html
Copyright © 2020-2023  润新知