Codeforces 441E Valera and Number dp

谁借莪1个温暖的怀抱¢ 2021-12-17 00:17 324阅读 0赞

Valera and Number

感觉想了挺久的。。

dp[ o ][ i ][ mask ] , 其中mask表示最后9位是什么。

如果mask == 0 , 表示进行了o轮, 当前lowbit是哪位这种状态的概率。

如果mask != 0 , 表示进行了o轮, 最后9位为mask, 从第10位开始连续多少个1这种状态的概率。

从dp[ o ][ i ][ 0 ] 经过 +1 这种操作的时候就可以舍弃掉 i , 因为这些连续的1已经没有用了, 即使后面全部是+1也加不到这位。

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define LD long double
  4. #define ull unsigned long long
  5. #define fi first
  6. #define se second
  7. #define mk make_pair
  8. #define PLL pair<LL, LL>
  9. #define PLI pair<LL, int>
  10. #define PII pair<int, int>
  11. #define SZ(x) ((int)x.size())
  12. #define ALL(x) (x).begin(), (x).end()
  13. #define fio ios::sync_with_stdio(false); cin.tie(0);
  14. using namespace std;
  15. const int N = 250 + 7;
  16. const int inf = 0x3f3f3f3f;
  17. const LL INF = 0x3f3f3f3f3f3f3f3f;
  18. const int mod = 1e9 + 7;
  19. const double eps = 1e-10;
  20. const double PI = acos(-1);
  21. template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
  22. template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
  23. template<class T, class S> inline bool chkmax(T &a, S b) {
  24. return a < b ? a = b, true : false;}
  25. template<class T, class S> inline bool chkmin(T &a, S b) {
  26. return a > b ? a = b, true : false;}
  27. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  28. int x, k, p;
  29. double dp[2][250][1 << 9];
  30. double p1, p2;
  31. double (*f)[1 << 9] = dp[0];
  32. double (*g)[1 << 9] = dp[1];
  33. inline int getLow(int mask) {
  34. for(int i = 0; ; i++) if(mask >> i & 1) return i;
  35. }
  36. int main() {
  37. scanf("%d%d%d", &x, &k, &p);
  38. p1 = 1.0 * p / 100;
  39. p2 = 1 - p1;
  40. int c = 0, mask = (x & 511);
  41. if(mask) {
  42. for(int i = 9; ; i++) {
  43. if(x >> i & 1) c++;
  44. else break;
  45. }
  46. } else {
  47. c = getLow(x);
  48. }
  49. f[c][mask] = 1;
  50. for(int o = 0; o < k; o++) {
  51. swap(f, g);
  52. for(int i = 0; i <= 240; i++)
  53. for(int mask = 0; mask < (1 << 9); mask++)
  54. f[i][mask] = 0;
  55. for(int i = 0; i < 240; i++) {
  56. for(int mask = 0; mask < (1 << 9); mask++) {
  57. if(mask) {
  58. // +1
  59. if(mask + 1 == (1 << 9)) {
  60. f[i + 9][0] += g[i][mask] * p2;
  61. } else {
  62. f[i][mask + 1] += g[i][mask] * p2;
  63. }
  64. // *2
  65. int nmask = (mask << 1) & 511;
  66. int bit = mask >> 8 & 1;
  67. if(nmask) {
  68. if(bit) {
  69. f[i + 1][nmask] += g[i][mask] * p1;
  70. } else {
  71. f[0][nmask] += g[i][mask] * p1;
  72. }
  73. } else {
  74. f[9][0] += g[i][mask] * p1;
  75. }
  76. } else {
  77. // +1
  78. f[0][1] += g[i][mask] * p2;
  79. // *2
  80. f[i + 1][0] += g[i][mask] * p1;
  81. }
  82. }
  83. }
  84. }
  85. double ans = 0;
  86. for(int i = 0; i <= 240; i++) {
  87. for(int mask = 0; mask < (1 << 9); mask++) {
  88. if(mask) {
  89. ans += getLow(mask) * f[i][mask];
  90. } else {
  91. ans += i * f[i][mask];
  92. }
  93. }
  94. }
  95. printf("%.12f\n", ans);
  96. return 0;
  97. }
  98. /*
  99. */

转载于:https://www.cnblogs.com/CJLHY/p/11114448.html

发表评论

表情:
评论列表 (有 0 条评论,324人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Codeforces 1215E 状压DP

    题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段。 思路:由于题目中的颜色种类很少,考虑状压DP。设dp

    相关 Codeforces 750E 线段树DP

    题意:给你一个字符串,有两种操作:1:把某个位置的字符改变。2:询问l到r的子串最少需要删除多少个字符,使得这个子串含有2017子序列,并且没有2016子序列? 思路:线段树

    相关 Codeforces 735E 树形DP

    题意:给你一棵树,你需要在这棵树上选择一些点染成黑色,要求染色之后树中任意节点到离它最近的黑色节点的距离不超过m,问满足这种条件的染色方案有多少种? 思路:设dp\[x\]\

    相关 Codeforces 722E 组合数学 DP

    题意:有一个n \ m的棋盘,你初始在点(1, 1),你需要去点(n, m)。你初始有s分,在这个棋盘上有k个点,经过一次这个点分数就会变为s / 2(向上取整),问从起点到终