B. Painting The Wall
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

基本上是第一次做期望的题。。。

令f[i][j]表示当前图中有i行j列没有涂过色,然后用标准方法乱搞就行了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
using namespace std;
#define MAXN 2100 int ptc[MAXN],ptr[MAXN];
typedef long double real;
real dp[MAXN][MAXN];
int main()
{
int i,j,x,y,z;
real n;int m;
cin>>n>>m;
for (i=;i<m;i++)
{
scanf("%d%d",&x,&y);
ptc[x]=ptr[y]=;
}
int tr,tc;
tr=tc=;
for (i=;i<=n;i++)
{
if (!ptc[i])tc++;
if (!ptr[i])tr++;
}
dp[][]=;
real tot_p;
for (i=;i<=tr;i++)
{
dp[i+][]=dp[i][]+/(-(n-i-)/n);
}
for (i=;i<=tc;i++)
{
dp[][i+]=dp[][i]+/(-(n-i-)/n);
}
for (i=;i<=tr;i++)
{
for (j=;j<=tc;j++)
{
tot_p=(n-i)*j+(n-j)*i+i*j;
dp[i][j]=dp[i-][j-]*(i*j)/tot_p+dp[i][j-]*(n-i)*j/tot_p+
dp[i-][j]*(n-j)*i/tot_p+/(-(n-i)*(n-j)/(n*n));
}
}
double ans=dp[tr][tc];
printf("%.10f\n",ans);
return ;
}

Painting The Wall 期望DP Codeforces 398_B的更多相关文章

  1. Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP

                                                                                   D. Painting The Wall ...

  2. [Codefoeces398B]Painting The Wall(概率DP)

    题目大意:一个$n\times n$的棋盘,其中有$m$个格子已经被染色,执行一次染色操作(无论选择的格子是否已被染色)消耗一个单位时间,染色时选中每个格子的概率均等,求使每一行.每一列都存在被染色的 ...

  3. Codeforces - 1264C - Beautiful Mirrors with queries - 概率期望dp

    一道挺难的概率期望dp,花了很长时间才学会div2的E怎么做,但这道题是另一种设法. https://codeforces.com/contest/1264/problem/C 要设为 \(dp_i\ ...

  4. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  5. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

  6. Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)

    题目链接:New Year and Arbitrary Arrangement 题意: 有一个ab字符串,初始为空. 用Pa/(Pa+Pb)的概率在末尾添加字母a,有 Pb/(Pa+Pb)的概率在末尾 ...

  7. 【CF398B】B. Painting The Wall(期望)

    B. Painting The Wall time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. 【CodeForces】913 F. Strongly Connected Tournament 概率和期望DP

    [题目]F. Strongly Connected Tournament [题意]给定n个点(游戏者),每轮游戏进行下列操作: 1.每对游戏者i和j(i<j)进行一场游戏,有p的概率i赢j(反之 ...

  9. Codeforces 1139D(期望dp)

    题意是模拟一个循环,一开始有一个空序列,之后每次循环: 1.从1到m中随机选出一个数字添加进去,每个数字被选的概率相同. 2.检查这个序列的gcd是否为1,如果为1则停止,若否则重复1操作直至gcd为 ...

随机推荐

  1. ASP.NET 开发人员应该知道的8个网站

    1.CodeProject http://www.codeproject.com/ 2.DotNetNuke 3.4GuysFromRolla 4.DevSource 5.DevX http://ww ...

  2. AIDL通信原理

    AIDL (Android Interface Definition Language),通过定义通信接口来实现进程间通信.这是Google提供的一种在安卓应用进程间通信的工具.所以要了解AIDL的通 ...

  3. 移动平台作业——天气预报——天气数据的获得——为应用申请百度ak码

    需求: 可切换城市 可实时更新(按钮或手势或下拉刷新) 可现实未来三日的天气 不限制横屏或者竖屏,不限制布局样式,但要求得到的数据均需显示(北京.天气数据.天气图标) 提示: 获得实时天气数据(任选一 ...

  4. Bootstrap--组件之下拉菜单

    用于显示链接列表的可切换.有上下文的菜单. 对齐 B默认情况下,下拉菜单自动沿着父元素的上沿和左侧被定位为 100% 宽度. 为 .dropdown-menu 添加 .dropdown-menu-ri ...

  5. NUnit使用详解(一)

    转载:http://hi.baidu.com/grayworm/item/38e246165aa7b9433a176e90 NUnit是一个单元测试框架,专门针对于.NET来写的,它是是xUnit的一 ...

  6. WebView组件的应用

    1.什么是WebView? WebView(网络视图)能加载显示网页,可以将其视为一个浏览器,它使用了WebKit渲染引擎加载显示网页. <?xml version="1.0" ...

  7. VS2010 测试 -普通单元测试

    http://www.cnblogs.com/rhythmK/archive/2012/04/20/2458832.html

  8. ios fixed属性bug解决方法

    在内容层外面包一个div 加上样式:position:fixed;top:0px; bottom:50px;overflow:scroll; 就可以完美解决

  9. Xcode中需要熟悉的常用快捷键

    因为工作需要,笔者最近开始接触Xcode这款Mac系统下的强大的编程软件.因为个人习惯,每当接触新的软件的时候总会先去了解它的一些常用快捷键.经过多方查阅总结出以下内容,希望对刚刚接触Xcode的初学 ...

  10. IntelliJ IDEA14 和 Maven 系列:使用IntelliJ IDEA 14和Maven 7 创建java web项目(一)

    Intellij IDEA作为最好的Java IDE,创建Maven项目还是比较简单的,但是创建一个Maven Web项目还是要修改一些配置的,下面进行总结整理. 1前言 在创建项目中,IDEA提供了 ...