错排问题是一种特殊的排列问题。

模型:把n个元素依次标上1,2,3.......n,求每一个元素都不在自己位置的排列数。

运用容斥原理,我们有两种解决方法:

1.

总的排列方法有A(n,n),即n!,设Ai 表示数i在第i个位置的全体排列,显然有Ai =(n-1)!。

同理可得Ai∩Aj=(n-2)!,那么每一个元素都不在原来位置的排列就有n!-C(n,1)*(n-1)!+C(n,2)*(n-2)!-.....+(-1)^n *C(n,n)*1!。

也就是n!*(a-1/1!+1/2!-1/3!+1/4!-.....+(-1)^n *1/n!)。

我们只要预处理n的阶乘就可以得到答案。

2.

用DP的思想来运用容斥原理。

设1,2......错排的方案数是dp[n],当我们任取一个数i来使之错位时都有两种情况:

①:数i先与其他n-1个数之一互换,然后剩下n-2个错排..dp[n]=(n-1)*dp[n-2]。

②:除数i外其他n-1个数错排,再从这n-1个数中选一个和i互换..dp[n]=(n-1)*dp[n-1]。

显然这两种错排形式是相互独立且互斥的,运用加法定理,那么我们有:dp[1]=0,dp[2]=1,dp[n]=(n-1)*(dp[n-1]+dp[n-2])。

--------

注意:

当n比较大时,错排的方案数也会变得相当大!所以一般错排的题都会用高精度。

【模板题】UVA 10497

Children are always sweet but they can sometimes make you feel bitter. In this problem, you will see
how Tintin, a ve year's old boy, creates trouble for his parents. Tintin is a joyful boy and is always
busy in doing something. But what he does is not always pleasant for his parents. He likes most to play
with household things like his father's wristwatch or his mother's comb. After his playing he places it
in some other place. Tintin is very intelligent and a boy with a very sharp memory. To make things
worse for his parents, he never returns the things he has taken for playing to their original places.
Think about a morning when Tintin has managed to `steal' three household objects. Now, in how
many ways he can place those things such that nothing is placed in their original place. Tintin does not
like to give his parents that much trouble. So, he does not leave anything in a completely new place;
he merely permutes the objects.
Input
There will be several test cases. Each will have a positive integer less than or equal to 800 indicating
the number of things Tintin has taken for playing. Each integer will be in a line by itself. The input
is terminated by a `-1' (minus one) in a single line, which should not be processed.
Output
For each test case print an integer indicating in how many ways Tintin can rearrange the things he has
taken.
Sample Input
2
3
4
-1
Sample Output
1
2
9

题意:

问n个数错排的方案数。

错排的思路如上,AC代码用的是思路2。

AC code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll m=1e10;
struct Bigint{
ll s[];
ll l;
Bigint()
{
l=;
memset(s,,sizeof(s));
}
void operator *=(int x)
{
ll d=;
for(int i=;i<=l;i++)
{
d+=s[i]*x;
s[i]=d%m;
d/=m;
}
while(d)
{
s[++l]=d%m;
d/=m;
}
}
void print()
{
printf("%llu",s[l]);
for(int i=l-;i>=;i--)
{
printf("%010llu",s[i]);
}
}
void set(ll x)
{
s[l]=x%m;
x/=m;
while(x)
{
++l;
s[l]=x%m;
x/=m;
}
}
}dp[];
Bigint operator +(Bigint b,Bigint& a)
{
ll d=;
b.l=max(b.l,a.l);
for(int i=;i<=b.l;i++)
{
b.s[i]+=(d+a.s[i]);
d=b.s[i]/m;
b.s[i]%=m;
}
if(d)
{
b.s[++b.l]=d;
}
return b;
}
int n;
int main()
{
//freopen("input.txt","r",stdin);
dp[].set();
dp[].set();
for(int i=;i<=;i++)
{
dp[i]=dp[i-]+dp[i-];
dp[i]*=(i-);
}
while(~scanf("%d",&n)&&n!=-)
{
dp[n].print();
printf("\n");
}
return ;
}

容斥原理--计算错排的方案数 UVA 10497的更多相关文章

  1. 【BZOJ4563】[Haoi2016]放棋子 错排+高精度

    [BZOJ4563][Haoi2016]放棋子 Description 给你一个N*N的矩阵,每行有一个障碍,数据保证任意两个障碍不在同一行,任意两个障碍不在同一列,要求你在这个矩阵上放N枚棋子(障碍 ...

  2. 题解 P4071 【[SDOI2016]排列计数】 (费马小定理求组合数 + 错排问题)

    luogu题目传送门! luogu博客通道! 这题要用到错排,先理解一下什么是错排: 问题:有一个数集A,里面有n个元素 a[i].求,如果将其打乱,有多少种方法使得所有第原来的i个数a[i]不在原来 ...

  3. Codeforces 888D: Almost Identity Permutations(错排公式,组合数)

    A permutation \(p\) of size \(n\) is an array such that every integer from \(1\) to \(n\) occurs exa ...

  4. HDU 2068 RPG的错排

    要求答对一半或以上就算过关,请问有多少组答案能使他顺利过关. 逆向思维,求答错一半或以下的组数 1,错排 错排公式的由来 pala提出的问题: 十本不同的书放在书架上.现重新摆放,使每本书都不在原来放 ...

  5. E - 不容易系列之(4)――考新郎 错排数公式

    国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的:  首先,给每位新娘打扮得几乎一模一 ...

  6. 组合数们&&错排&&容斥原理

    最近做了不少的组合数的题这里简单总结一下下 1.n,m很大p很小 且p为素数p要1e7以下的 可以接受On的时间和空间然后预处理阶乘 Lucas定理来做以下是代码 /*Hdu3037 Saving B ...

  7. CF GYM100548 (相邻格子颜色不同的方案数 2014西安现场赛F题 容斥原理)

    n个格子排成一行,有m种颜色,问用恰好k种颜色进行染色,使得相邻格子颜色不同的方案数. integers n, m, k (1 ≤n, m ≤ 10^9, 1 ≤ k ≤ 10^6, k ≤ n, m ...

  8. 卡特兰数&错排&一个一直记不住的公式

    卡特兰数 公式:f(x)=f(2)*f(x-1)+f(3)*f(x-2)+......+f(x-1)*f(2) #include<iostream>#include<cstdlib& ...

  9. UVa 11137 (完全背包方案数) Ingenuous Cubrency

    题意:用13.23……k3这些数加起来组成n,输出总方案数 d(i, j)表示前i个数构成j的方案数则有 d(i, j) = d(i-1, j) + d(i, j - i3) 可以像01背包那样用滚动 ...

随机推荐

  1. 邮箱图标的css样式

    <div> <div style="position:relative; height:40px;width: 70px;border:2px solid black; m ...

  2. android studio学习----常用快捷键

    Action Mac OSX Win/Linux 注释代码(//) Cmd + / Ctrl + / 注释代码(/**/) Cmd + Option + / Ctrl + Shift + / 格式化代 ...

  3. 【android】关于call拨号功能实现的记录

    前几天考试居然记错dial和call,故在此写上小demo来作区别,加深印象. 主要是实现call(拨通电话)功能,dial(拨电话)功能用作对比,话不多说,贴上代码. 1.创建布局文件如下: < ...

  4. Django forms 主要的标签介绍

    修改 forms.py from django import forms as DForms from django.forms import fields from django.forms imp ...

  5. django更换ORM连接处理(连接池)转

    1 概述 在使用 Django 进行 Web 开发时, 我们避免不了与数据库打交道. 当并发量低的时候, 不会有任何问题. 但一旦并发量达到一定数量, 就会导致 数据库的连接数会被瞬时占满. 这将导致 ...

  6. CCF-CSP 201709-3 JSON查询 题解

    试题编号: 201709-3 试题名称: JSON查询 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 JSON (JavaScript Object Notation) 是一 ...

  7. BERT安装与使用

    环境: python 3.5 tensorflow 1.12.1 bert-serving-server 1.9.1 bert-serving-cline 1.9.1 官网上说要保证Python &g ...

  8. Ubuntu下搭建Kubernetes集群(3)--k8s部署

    1. 关闭swap并关闭防火墙 首先,我们需要先关闭swap和防火墙,否则在安装Kubernetes时会导致不成功: # 临时关闭 swapoff -a # 编辑/etc/fstab,注释掉包含swa ...

  9. [原创]Appium与Appium desktop的区别

    1.两者都属于Appium 服务端 2.二者最新版本如下:地址:https://github.com/appium/appium-desktop/releases Appium 服务端支持的:地址:h ...

  10. python-分割url字符串

    url = ' http://images.jupiterimages.com/common/detail/27/68/22986827.jpg' url.strip().split('/')[-1] ...