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. 实现一个脚本语言Raven(一)

    之前实现了Raven语言的0.1版,仅仅支持表达式处理与控制语句,由于不支持数组.函数.类,甚至都不是图灵完全的语言. 现在参考vczh的博客打算重新写一遍Raven语言.陈祖不愧是神啊,高中就写出支 ...

  3. 在Windows下自动运行Modelsim

    首先声明:该文章是在刘志伟老师的<Modelsim的Tcl命令>的基础上写的,希望我们能越来越自动化. 1.编写好源文件.包含asyn_fifo.v.fifomem.v.rptr_empt ...

  4. 在某些情况下明明添加了引用,为何VS还报错"XXX"不存在类型或命名空间(是否缺少程序集引用)

    程序主结构:两个程序集DLL,一个OpticalAlarm(主程序),一个OpticalAlarm.Common 问题描述:搭建程序框架时,使用了log4net进行日志处理,在OpticalAlarm ...

  5. HDU-1018(简单数学)

    Big Number Problem Description In many applications very large integers numbers are required. Some o ...

  6. jBPM5 vs Actitivi

    http://www.blogways.net/blog/2013/07/16/activiti-jbpm-compare.html jBPM是目前市场上主流开源工作引擎之一,在创建者Tom Baey ...

  7. 用正则表达式解析XML

    import java.util.regex.*; import java.util.*; /** * * <p>Title: Document</p> * * <p&g ...

  8. sublime_text编辑器下载安装使用

    1.sublime_text下载 https://www.sublimetext.com/3   (选择相对应的版本) 2.通过package control安装插件 https://packagec ...

  9. H5之canvas简单入门

    <canvas></canvas>是html5出现的新标签,像所有的dom对象一样它有自己本身的属性.方法和事件,其中就有绘图的方法,js能够调用它来进行绘图 <canv ...

  10. ImageIcon图像处理相关测试【一些特殊的处理方式】

    /*************以下源码通过测试******************************/ package cn.jason.ios.images; import java.awt.F ...