HDU 6377 度度熊看球赛 (计数DP)

度度熊看球赛

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 293    Accepted Submission(s): 130


Problem Description
世界杯正如火如荼地开展!度度熊来到了一家酒吧。

有  N 对情侣相约一起看世界杯,荧幕前正好有 2×N 个横排的位置。

所有人都会随机坐在某个位置上。

当然,如果某一对情侣正好挨着坐,他们就会有说不完的话,影响世界杯的观看。

一般地,对于一个就座方案,如果正好有 K 对情侣正好是挨着坐的,就会产生 DK 的喧闹值。

度度熊想知道随机就座方案的期望喧闹值。

为了避免输出实数,设答案为 ans,请输出 ans×(2N)! mod P 的值。其中 P=998244353
 
Input
有多组数据(不超过  1000 组),读到EOF结束。

对于每一组数据,读入两个数 N 和 D 。

1N,D1000
 
Output
对于每一组数据,输出一个数表示答案。
 
Sample Input
1 10 2 3
 
Sample Output
20 104
 
Source
 
Recommend
chendu
 
Statistic |  Submit |  Discuss |  Note

析:这是一个计数DP,要先预处理,dp[i][j] 表示前 i 对情侣有 j 对坐在一起的数量。期望的话,最后再加起来了就好了。下面进行分情况讨论每一种情况。

一、第 i 对情侣坐在一起,那么位置又有两种情况,1.他们拆散一对情侣,那么就是 dp[i][j] += 2 * j * dp[i-1][j]。2.他们不拆散任何一对情侣,那么就要插空了,dp[i][j] += 2 * (2*i-j) * dp[i-1][j-1]。 i -1 对人,j 对 情侣,有 2 * i - j  个空。

二、第 i 对情况不坐在一起,那么位置又有三种情况,1.他们不拆散任何一对情侣,那就是插空,dp[i][j] += (2*i-j-1) * (2*i-j-2) * dp[i-1][j]。2.拆散一对情侣,其中一个拆散,另一个插空,dp[i][j] += 2 * (j+1) * (2*i-j-2) * dp[i-1][j+1]。3.拆散两对情侣,那么每个人都去坐到情侣中间,dp[i][j] += (j+2) * (j+1) * dp[i-1][j+2]。

最后只要使用期望加起来就好。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std;
 
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const LL mod = 998244353LL;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x;  scanf("%d", &x);  return x; }

LL dp[maxn][maxn];

void init(){
  dp[0][0] = 1LL;
  for(int i = 1; i <= 1000; ++i)
    for(int j = 0; j <= i; ++j){
      dp[i][j] = 2 * j * dp[i-1][j] % mod;  // 1.1
      if(j)  dp[i][j] = (dp[i][j] + 2 * (2*i-j) * dp[i-1][j-1]) % mod;  // 1.2
      dp[i][j] = (dp[i][j] + (2*i-j-1) * (2*i-j-2) * dp[i-1][j]) % mod;  // 2.1
      if(j + 1 <= i)  dp[i][j] = (dp[i][j] + 2 * (j+1) * (2*i-j-2) * dp[i-1][j+1]) % mod;  // 2.2
      if(j + 2 <= i)  dp[i][j] = (dp[i][j] + (j+2) * (j+1) * dp[i-1][j+2]) % mod;  // 2.3
    }
}

int main(){
  init();
  while(scanf("%d %d", &n, &m) == 2){
  LL ans = 0;
  LL x = 1;
  for(int i = 0; i <= n; ++i, x = x * m % mod)
    ans = (ans + dp[n][i] * x) % mod;
  printf("%I64d\n", ans);
  }
  return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/dwtfukgv/p/9487469.html