D. Painting The Wall

User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n × n table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below.

  1. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2.
  2. User ainta choose any tile on the wall with uniform probability.
  3. If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it.
  4. Then he takes a rest for one minute even if he doesn't paint the tile. And then ainta goes to step 1.

However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes no time at all.

Input

The first line contains two integers n and m (1 ≤ n ≤ 2·103; 0 ≤ m ≤ min(n2, 2·104)) — the size of the wall and the number of painted cells.

Next m lines goes, each contains two integers ri and ci (1 ≤ ri, ci ≤ n) — the position of the painted cell. It is guaranteed that the positions are all distinct. Consider the rows of the table are numbered from 1 to n. Consider the columns of the table are numbered from1 to n.

Output

In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10 - 4 absolute or relative error.

Sample test(s)
input
5 22 34 1

output
11.7669491886

input
2 21 11 2

output
2.0000000000

input
1 11 1

output
0.0000000000

题意:有一个n*n的墙,现在小明来刷墙,如果每一行每一列都至少有一个格子刷过了就停止工作,否则每次随机选一个格子,如果刷过了就不刷如果没刷过就刷,然后休息一分钟,求停止工作时时间的数学期望(开始之前已经有m个格子刷过了)题解:dp[i][j]表示还有i行j列未刷     初始化: dp[i][0]=((n-i)/n)*dp[i][0]+dp[i-1][0]*i/n+1;dp[0][j]=((n-j)/n)*dp[0][j]+dp[0][j-1]*j/n+1;     转移:   dp[i][j]=dp[i][j]*(n-i)(n-j)/n^2+dp[i-1][j]*(i*(n-j))/n^2+dp[i][j-1]*((n-i)*j)/n^2+dp[i-1][j-1]*(i*j)/n^2+1;

#include<iostream>
#include<cstdio>
using namespace std;
double dp[2010][2010];
int n,m,a[2010],b[2010];
int main()
{cin>>n>>m;int x,y;int l=n,r=n;for(int i=0; i<m; i++){cin>>x>>y;if(!a[x]) l--;if(!b[y]) r--;a[x]=1,b[y]=1;}for(int i=1; i<=n; i++) dp[i][0]=dp[i-1][0]+(double)n/i;for(int j=1; j<=n; j++) dp[0][j]=dp[0][j-1]+(double)n/j;for(int i=1; i<=n; i++){for(int j=1; j<=n; j++){dp[i][j]=(dp[i-1][j]*i*(n-j)+n*n+dp[i][j-1]*j*(n-i)+dp[i-1][j-1]*i*j)/(n*n-(n-i)*(n-j));}}printf("%0.10f\n",dp[l][r]);return 0;
}

代码

转载于:https://www.cnblogs.com/zxhl/p/4846283.html

Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP相关推荐

  1. Codeforces Round #700 (Div. 2) D2 Painting the Array II(最通俗易懂的贪心策略讲解)看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 整场比赛的A ~ E 6题全,全部题目超高质量题解链接: Codeforces Round #700 ...

  2. Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes kmp + dp

    传送门 文章目录 题意: 思路: 题意: 思路: 通过完美子串的定义,我们不难发现满足条件的子串就是kmpkmpkmp中ne[n]ne[n]ne[n]不断向前跳得到的串,现在问题就是如何求这些前缀串在 ...

  3. Codeforces Round #595 (Div. 3) F. Maximum Weight Subset 树形dp

    传送门 文章目录 题意: 思路: 题意: n≤200n\le200n≤200 思路: 明显的树形dpdpdp,所以考虑一下dpdpdp状态. 这个题状态挺神的..可能是因为我太菜了,看了半天才看懂. ...

  4. Codeforces Round #620 (Div. 2) F2. Animal Observation (hard version) dp + 线段树

    传送门 文章目录 题意: 思路: 题意: 比如下面这个图: 思路: 对于这个题,比较容易就能考虑到dpdpdp,设f[i][j]f[i][j]f[i][j]为到了第iii行,覆盖了[j,j+k−1][ ...

  5. Codeforces Round #450 (Div. 2)D. Unusual Sequences[数论][组合数学][dp II]

    题目:http://codeforces.com/contest/900/problem/D 题意:找到加和为m的且gcd为n的数列种类数 分析:可以转化为求gcd为1的加和为m/n的种类数,假设有m ...

  6. Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp

    链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为 ...

  7. Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess DP

    C. Gerald and Giant Chess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  8. Codeforces Round #466 (Div. 2): E. Cashback(单调队列+DP)

    题目链接:http://codeforces.com/contest/940/problem/E 题意:给你一个长为n的序列和一个数字c,你要将这个序列切成若干段,对于每一段,这段中最小的[n/c]个 ...

  9. Codeforces Round #585 (Div. 2)E. Marbles(状压dp)

    https://codeforces.com/contest/1215/problem/E cnt[x][j]表示把x这种颜色全放到j这种颜色前面所需要的交换次数. 其实这题跟上一篇博客那题是同一个套 ...

最新文章

  1. html实现带有数字的列表,纯CSS实现雨滴形状的数字序号列表
  2. python123测试3平方根格式化,【Python3学习】走进Python
  3. [置顶] 将项目从tomcat 迁移到JBoss
  4. 一个资深投行女销售和低调IT创业男的故事
  5. Github使用1-入门
  6. 权限操作-springSecurity快速入门
  7. CMDB服务器管理系统【s5day90】:创建资产更新服务器硬盘信息
  8. 线程重命名java_线程重命名(Netty)和设计模式Decorator
  9. F - 最短路 HDU - 2544(最短路的模板)
  10. java代表预设一个SQL_java-io基础-3-压缩和解压
  11. 系统分析与设计(个人总结)
  12. 混合储能系统能量管理simulink仿真模型。 蓄电池和超级电容构成的混合储能系统能量管理控制策略
  13. python如何连接sql_python连接SQL数据库
  14. 各类型商户微信认证方法
  15. 产品经理必懂的28个心理学效应
  16. EAN13条形码生成器
  17. 7 款常用的 PostgreSQL GUI 工具测评
  18. 图像与视频处理中的优化方法
  19. 【COM编程】如何往IE工具条添加按钮
  20. python画笑脸表情_用Matplotlib,妈妈再也不担心我没有表情包斗图了

热门文章

  1. linux shell中各种分号和括号,linux shell 各种分号,括号使用方法总结
  2. 安装python要注意什么_安装python注意事项
  3. linux 下 .sh 文件语法
  4. topcoder SRM712 Div1 LR
  5. (转)找回vss超级管理员密码
  6. 双频无线网安装设置(5g ) for linux
  7. 多线程——线程间的同步通信
  8. iOS开发学习-nonatomic和atomic的区别
  9. FusionChart完全入门手册8
  10. C# java 有关“字节序”的描述 .