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

模型:把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. Java开发中对Redis的基本操作

    Jedis操作redis指令 import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; impo ...

  2. AutoLayout的使用

    虽然苹果提供了AutoresizingMask的布局方式,这个方式局限性太大:只能解决父控件和子控件间的相对关系: 因此,推出了AutoLayout:苹果官方也是推荐开发者尽量使用autolayout ...

  3. 基本SQL语句使用方法

    结构:增: create database 库名 charset 字符集: create table 表名称(字段名 类型 约束 ,字段名 类型 约束) not null 非空primary key ...

  4. Python乘法口诀表

    乘法口诀表 print("乘法口诀表") for i in range(1,10):     for j in range(1,i+1):         print(str(i) ...

  5. Codeforces F. Maxim and Array(构造贪心)

    题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. Rust中的Cargo工作空间实践

    这是为了开发大型程序,分治crate用的. 目录结构如下: 一,根cargo.toml内容 [workspace] members = [ "adder", "add-o ...

  7. RabbitMQ的几个常见问题

    1. 如何保证消息尽量发送成功? 问题描述: 如果没有启动消费者,重启了RabbitMQ服务,队列和消息都会丢失. 解决方案: 针对这个问题,有以下几个机制可以解决: 生产者确认: 持久化: 手动AC ...

  8. 解决chrome连接自建https服务器报“您的连接不是私密连接”问题

    前一段时间,Chrome 突然显示出了“您的连接不是私密连接”,这下可难受了,大部分的网站打开都有问题. 找了各种方法,各种设置都是不行. 一.暴力.费力的方法直接卸载 Chrome ,删除一切数据以 ...

  9. 《2019面向对象程序设计(java)课程学习进度条

    周次 (阅读/编写)代码行数 发布博客量/评论他人博客数量 课余学习时间(小时) 学习收获最大的程序 阅读或编译让我 第一周 20/5 1/0 4 编译九九乘法表 第二周 100/10 2/0 5 第 ...

  10. 201871010131-张兴盼《面向对象程序设计(java)》第十四周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业要求在哪里 https://www.cnblogs.com/lily-2018/p/1 ...