[HNOI2015]亚瑟王

嘟嘟嘟


翻某(些)人的做题记录看到这道题的。


知道是期望dp,但就是没想出来,看题解后才知道是状态设的不好。“良好的状态是AC的一半啊……”
我设的是dp[i][j]表示第\(i\)个人在\(j\)轮后出招的概率,而题解是\(r\)轮后,前\(i\)个人中有\(j\)个人出招的概率。
剩下的我感觉题解讲的非常清楚,不必我多说,所以给个链接吧:大佬的题解


听说这题有人被卡常,反正我是没有。非要卡的话,观察每次乘方的数,指数每次+1,所以可以继承上一次的乘方答案,从而省去快速幂。
(不想卡就写了个\(O(n ^ 2logn)\)的了)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
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 maxn = 225;
inline 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;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n, m;
db p[maxn];
int d[maxn];
db dp[maxn][maxn];

In db quickpow(db a, ll b)
{
  db ret = 1;
  for(; b; b >>= 1, a *= a)
    if(b & 1) ret *= a;
  return ret;
}

int main()
{
  int T = read();
  while(T--)
    {
      n = read(); m = read();
      for(int i = 1; i <= n; ++i) scanf("%lf", &p[i]), d[i] = read();
      dp[0][0] = 1;
      db ans = 0;
      for(int i = 1; i <= n; ++i)
	for(int j = 0; j <= m; ++j)
	  {
	    dp[i][j] = dp[i - 1][j] * quickpow(1 - p[i], m - j);
	    if(j) dp[i][j] += dp[i - 1][j - 1] * (1 - quickpow(1 - p[i], m - j + 1));
	    ans += dp[i - 1][j] * (1 - quickpow(1 - p[i], m - j)) * d[i];
	  }
      printf("%.10lf\n", ans);
    }
  return 0;
}
posted @ 2019-02-14 14:09  mrclr  阅读(169)  评论(0编辑  收藏  举报