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. [转] JAVA正则表达式:Pattern类与Matcher类详解(转)

    java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和 Matcher Pattern 一个Pattern是一个正则表达式经编译后的 ...

  2. Java Inner class

    2016-03-27 内部类:就是把一个类的定义放在另外一个外围类定义的里面. class OutterClass{ class InnerClass { } } 1. 内部类主要有四种:成员内部类( ...

  3. nyoj 2 括号配对问题

    括号配对问题 时间限制:3000 ms  |            内存限制:65535 KB 难度:3   描述 现在,有一行括号序列,请你检查这行括号是否配对.   输入 第一行输入一个数N(0& ...

  4. 网页设定固定背景图片(抄袭自百度FM)

    这个新技能,我是从百度FM中学习到的. 在网页中,有一个id为"body-bg"的层, html代码: <div id="body-bg" style=& ...

  5. Jquery关闭离开页面时提醒

    [导读] 离开页面提示多般是放到了发新闻或写日志的页面,我们在百度空间或QQ空间在我们未保存信息时如果离开页面都有提示了,下面我来介绍利用jquery的beforeunload来实现此方法. jque ...

  6. C#里面比较时间大小三种方法

    1.比较时间大小的实验 string st1="12:13";string st2="14:14";DateTime dt1=Convert.ToDateTim ...

  7. 小改动,大作为——C# 4.0中的微小改动

    1.可选参数和命名实参 可选参数和命名实参就如同一对好基友,因为它们经常一起使用. 1.1 可选参数 可选参数重在“可选”,即在调用方法时,该参数可以明确指定实参,也可以不指定.如下代码所示,下面代码 ...

  8. JavaScript学习笔记 -- ES6学习(三) 变量的解构赋值

    1.解构赋值的定义 在ES6中,允许按照一定模式,从数组和对象中提取值(所谓解构),然后对变量进行赋值. var a = 1; var b = 2; var c = 3; //等价于 var [a, ...

  9. AJAX 一些常用方法

    abort() 停止当前请求getAllResponseHeaders() 返回包含HTTP请求的所有响应头信息,其中响应头包括Content-Length,Date,URI等内容.getRespon ...

  10. IOS代理

    之前看过一些关于代理的资料,始终感觉还是很模糊,最近抽出一段空闲的时间,将这块内容整理下: 什么是代理,顾名思义就是帮别人做些事情,比如买房子,当我们买房子时,我们一般会找房屋中介,因为他们就是干这件 ...