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 2
2 3
4 1
output
11.7669491886
input
2 2
1 1
1 2
output
2.0000000000
input
1 1
1 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[][];
int n,m,a[],b[];
int main()
{
cin>>n>>m;
int x,y;
int l=n,r=n;
for(int i=; i<m; i++)
{
cin>>x>>y;
if(!a[x]) l--;
if(!b[y]) r--;
a[x]=,b[y]=;
}
for(int i=; i<=n; i++) dp[i][]=dp[i-][]+(double)n/i;
for(int j=; j<=n; j++) dp[][j]=dp[][j-]+(double)n/j;
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{dp[i][j]=(dp[i-][j]*i*(n-j)+n*n+dp[i][j-]*j*(n-i)+dp[i-][j-]*i*j)/(n*n-(n-i)*(n-j));
}
}
printf("%0.10f\n",dp[l][r]);
return ;
}

代码

Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP的更多相关文章

  1. Codeforces Round #301 (Div. 2) D. Bad Luck Island 概率DP

    D. Bad Luck Island Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/pr ...

  2. Codeforces Round #256 (Div. 2) C. Painting Fence 或搜索DP

    C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...

  3. Codeforces Round #256 (Div. 2) C. Painting Fence (搜索 or DP)

    [题目链接]:click here~~ [题目大意]:题意:你面前有宽度为1,高度给定的连续木板,每次能够刷一横排或一竖列,问你至少须要刷几次. Sample Input Input 5 2 2 1 ...

  4. Codeforces Round #105 (Div. 2) D. Bag of mice 概率dp

    题目链接: http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test2 secondsmemo ...

  5. Codeforces Round #293 (Div. 2) D. Ilya and Escalator 概率DP

    D. Ilya and Escalator time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #284 (Div. 2) D. Name That Tune [概率dp]

    D. Name That Tune time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. Codeforces Round #597 (Div. 2) E. Hyakugoku and Ladders 概率dp

    E. Hyakugoku and Ladders Hyakugoku has just retired from being the resident deity of the South Black ...

  8. Codeforces Round #233 (Div. 2) B. Red and Blue Balls

    #include <iostream> #include <string> using namespace std; int main(){ int n; cin >&g ...

  9. 贪心 Codeforces Round #173 (Div. 2) B. Painting Eggs

    题目传送门 /* 题意:给出一种方案使得abs (A - G) <= 500,否则输出-1 贪心:每次选取使他们相差最小的,然而并没有-1:) */ #include <cstdio> ...

随机推荐

  1. 安装Yellowfin报错——No such file or directory: '/tmp/pip-build-jykvuD/YellowFin/README.md'

    https://blog.csdn.net/quqiaoluo5620/article/details/80608474 在Pycharm中安装Yellowfin时一直报错"no such ...

  2. Linux下MySQL 5.7的初始化

    要用管理员账号运行. systemctl start mysql#启动MySQL服务 mysqld_safe --user=mysql &#启动MySQL服务(安全方式) mysql -u r ...

  3. (3)Gojs model简介

    (3)Gojs model简介 在GoJS中,model用来存储表的基本数据,包括node.link等具体对象和属性,与其在视觉上的展示效果不相关.model中往往只保存相对简单的数据,最方便且持久化 ...

  4. HDU - 4544 湫湫系列故事——消灭兔子(优先队列+贪心)

    题目: 最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏. 游戏规则很简单,用箭杀死免子即可. 箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别 ...

  5. KBE——查询

    关于def文件的flags(这个标签表明了属性的作用域) 没有只在client的作用域

  6. Go:slice

    一.切片创建方式 func main() { // 创建切片方式1 // 让切片引用一个数组 array := [...]int{1, 2, 3, 4} slice1 := array[1:3] fm ...

  7. IDEA基本使用及配置(2)

    IDEA配置:File >> Setiings进入配置界面 1.主题配置:默认两种主题,黑色.白色,可以自己在网上下载,然后File >> Import Setiings导入, ...

  8. source insight中的快捷键总结

    1.快捷键 1,Shift+F8高亮显示指定字符. 2,Ctrl+F找出来的结果用F4,F3前进后退查找. 3,Alt+,后退alt+.前进查找关键字. 4,Alt+G或者F5跳转到某个固定的行号. ...

  9. PLSQLDeveloper安装与配置(详细图文)

    PLSQLDeveloper安装与配置(详细图文) 听语音 | 浏览:21912 | 更新:2016-10-24 17:12 1 2 3 4 5 6 7 分步阅读 在公司做项目时需要使用PLSQL D ...

  10. BNUOJ 26283 The Ghost Blows Light

    The Ghost Blows Light Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. O ...