HDU4336 Card Collector

vjudge嘟嘟嘟


看一眼数据范围,发现可以状压。
转移的话,就枚举接下来抽哪一张卡,发现可能转移到别的状态,可能还是这个状态。把方程列出来移项,就变成了\(a * x_i = 1 + p_j * x _j + p_k * x _ k + \ldots\)。然后我们逆推即可。
时间复杂度\(O(2 ^ n * n)\)。代码里写了个记搜。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxs = 1.1e6 + 5;
In ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
In void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif
}

int n;
db p[25], dp[maxs], sum = 0;

In db dfs(int S)
{
  if(dp[S] > eps || S == (1 << n) - 1) return dp[S];
  db ret = 1, P = 1 - sum;
  for(int i = 1; i <= n; ++i)
    if((S | (1 << (i - 1))) == S) P += p[i];
    else ret += p[i] * dfs(S | (1 << (i - 1)));
  return dp[S] = ret / (1 - P);
}

int main()
{
  //MYFILE();
  while(scanf("%d", &n) != EOF)
    {
      Mem(dp, 0); sum = 0;
      for(int i = 1; i <= n; ++i) {scanf("%lf", &p[i]); sum += p[i];}
      printf("%.8lf\n", dfs(0));
    }
  return 0;
}

猜你喜欢

转载自www.cnblogs.com/mrclr/p/10894380.html