Painting The Wall 期望DP Codeforces 398_B

时间:2023-01-16 10:42:26
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 &num;233 &lpar;Div&period; 2&rpar;D&period; Painting The Wall 概率DP

                                                                                   D. Painting The Wall ...

  2. &lbrack;Codefoeces398B&rsqb;Painting The Wall&lpar;概率DP&rpar;

    题目大意:一个$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. &lbrack;Codeforces 865C&rsqb;Gotta Go Fast&lpar;期望dp&plus;二分答案&rpar;

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

  5. &lbrack;Codeforces 553E&rsqb;Kyoya and Train&lpar;期望DP&plus;Floyd&plus;分治FFT&rpar;

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

  6. Codeforces 908 D&period;New Year and Arbitrary Arrangement &lpar;概率&amp&semi;期望DP&rpar;

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

  7. 【CF398B】B&period; 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&period; 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. 修正iOS从照相机和相册中获取的图片方向(转)

    - (UIImage *)fixOrientation { // No-op if the orientation is already correct if (self.imageOrientati ...

  2. Oracle正则表达式

       Oracle正则表达式 正则表达式具有强大.便捷.高效的文本处理功能.能够添加.删除.分析.叠加.插入和修整各种类型的文本和数据.Oracle从10g开始支持正则表达式. 下面通过一些例子来说明 ...

  3. Installing Ubuntu on a Pre-Installed Windows 8 &lpar;64-bit&rpar; System &lpar;UEFI Supported&rpar;

    http://askubuntu.com/questions/221835/installing-ubuntu-on-a-pre-installed-windows-8-64-bit-system-u ...

  4. 一步步优化JVM六:优化吞吐量

    如果你已经进行完了前面的步骤了,那么你应该知道这是最后一步了.在这一步里面,你需要测试应用的吞吐量和为了更高的吞吐量而优化JVM.    这一步的输入就是应用的吞吐量性能要求.应用的吞吐量是在应用层面 ...

  5. JDBC完成的三个基本工作

    JDBC完成的三个基本工作 1.与数据库建立连接 2.执行SQL语句 3.获得SQL语句的执行结果

  6. &lbrack;SCOI 2016&rsqb;美味

    Description 题库链接 给你一个长度为 \(n\) 的序列 \(A\) . \(m\) 组询问 \((b,x,l,r)\) 询问 \[\max_{i=l}^r b\oplus (A_i+x) ...

  7. BASE64Decoder小解

    BASE64Decoder小解 Base64 是网络上最常见的用于传输8Bit 字节代码的编码方式之一,大家可以查看RFC2045 -RFC2049 ,上面有MIME 的详细规范. Base64 要求 ...

  8. java web service 写入图片到web&sol;img&sol;

    获取本类service路径,然后字符串截取和拼接 String classpath= this.getClass().getResource("/").getPath(); Str ...

  9. group&lowbar;concat的使用以及乱码

    1.group_concat子查询返回数字是乱码,既不是utf8也不是gbk,后来看了下子表的字段编码是gbk的,但sql整体返回的是utf8,group_concat前 把字段转换成utf8的,运行 ...

  10. 【新题】OCP 062题库出现很多新题-6

    6.Which four statements are true about database instance behavior? A) Redo log files can be renamed ...