博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LOJ #2145. 「SHOI2017」分手是祝愿
阅读量:4593 次
发布时间:2019-06-09

本文共 1615 字,大约阅读时间需要 5 分钟。

题目链接

题解

一道画风正常的……期望DP?

首先考虑如何以最小步数熄灭所有灯:贪心地从大到小枚举灯,如果它亮着则修改它。可以求出总的最小步数,设为\(cnt\)

然后开始期望DP。设\(dp[i]\)为当前最小步数是\(i\),总最小步数是\(i\),要达到最小步数是\(i - 1\)的状态,期望要走多少步。则有\(\frac{i}{n}\)的几率恰好走了该走的一步,而有\(\frac{n - i}{n}\)的几率走错了(回到了\(dp[i + 1]\)表示的状态)。

则:\[dp[i] = \frac{i}{n} + \frac{n - i}{n}(1 + dp[i + 1] + dp[i])\]

就可以推出来了。

答案就是:\((\sum_{i = k + 1}^{cnt} dp[i] + min(cnt, k)) * n!\)

#include 
#include
#include
#include
#include
#include
#define space putchar(' ')#define enter putchar('\n')using namespace std;typedef long long ll;template
void read(T &x){ char c; bool op = 0; while(c = getchar(), c < '0' || c > '9') if(c == '-') op = 1; x = c - '0'; while(c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0'; if(op) x = -x;}template
void write(T x){ if(x < 0) putchar('-'), x = -x; if(x >= 10) write(x / 10); putchar('0' + x % 10);}const int N = 100005, P = 100003;int n, m, a[N], cnt;ll dp[N], ans;ll qpow(ll a, ll x){ ll ret = 1; while(x){ if(x & 1) ret = ret * a % P; a = a * a % P; x >>= 1; } return ret;}int main(){ read(n), read(m); for(int i = 1; i <= n; i++) read(a[i]); for(int i = n; i; i--) if(a[i]){ cnt++; for(int j = 1; j * j <= i; j++) if(i % j == 0){ a[j] ^= 1; if(j * j < i) a[i / j] ^= 1; } } for(int i = n; i; i--) dp[i] = 1 + (n - i) * qpow(i, P - 2) % P * (1 + dp[i + 1]) % P; for(int i = cnt; i > m; i--) ans = (ans + dp[i]) % P; ans = (ans + min(cnt, m)) % P; for(int i = 2; i <= n; i++) ans = ans * i % P; write(ans), enter; return 0;}

转载于:https://www.cnblogs.com/RabbitHu/p/LOJ2145.html

你可能感兴趣的文章
linux用户组管理
查看>>
Console-算法[for]-输出等腰三角形
查看>>
随机数产生方法小知识点
查看>>
Angular2.js——表单(上)
查看>>
aspose将datatable导出2
查看>>
windows下 JDK安装
查看>>
JS学习之闭包的理解
查看>>
Java学习之内部类
查看>>
Oracle内部视图:x$ktfbfe
查看>>
/etc/fstab文件中的一些参数
查看>>
雅可比矩阵与雅可比行列式
查看>>
Programming With Objective-C---- Introduction ---- Objective-C 学习(一)
查看>>
正则表达式语法大全
查看>>
DateUtils
查看>>
pb开发的客户端,使用oracle 9i客户端 提示oci.dll could not be loaded
查看>>
wordpress调用指定post type文章怎么操作
查看>>
magento开发手册之目录结构
查看>>
换个红圈1微信头像恶搞一下好友
查看>>
javascript学习_廖大_20170218
查看>>
bzoj2038: [2009国家集训队]小Z的袜子(hose) 莫队
查看>>