容斥原理--计算错排的方案数 UVA 10497
错排问题是一种特殊的排列问题。
模型:把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的更多相关文章
- 【BZOJ4563】[Haoi2016]放棋子 错排+高精度
[BZOJ4563][Haoi2016]放棋子 Description 给你一个N*N的矩阵,每行有一个障碍,数据保证任意两个障碍不在同一行,任意两个障碍不在同一列,要求你在这个矩阵上放N枚棋子(障碍 ...
- 题解 P4071 【[SDOI2016]排列计数】 (费马小定理求组合数 + 错排问题)
luogu题目传送门! luogu博客通道! 这题要用到错排,先理解一下什么是错排: 问题:有一个数集A,里面有n个元素 a[i].求,如果将其打乱,有多少种方法使得所有第原来的i个数a[i]不在原来 ...
- Codeforces 888D: Almost Identity Permutations(错排公式,组合数)
A permutation \(p\) of size \(n\) is an array such that every integer from \(1\) to \(n\) occurs exa ...
- HDU 2068 RPG的错排
要求答对一半或以上就算过关,请问有多少组答案能使他顺利过关. 逆向思维,求答错一半或以下的组数 1,错排 错排公式的由来 pala提出的问题: 十本不同的书放在书架上.现重新摆放,使每本书都不在原来放 ...
- E - 不容易系列之(4)――考新郎 错排数公式
国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的: 首先,给每位新娘打扮得几乎一模一 ...
- 组合数们&&错排&&容斥原理
最近做了不少的组合数的题这里简单总结一下下 1.n,m很大p很小 且p为素数p要1e7以下的 可以接受On的时间和空间然后预处理阶乘 Lucas定理来做以下是代码 /*Hdu3037 Saving B ...
- CF GYM100548 (相邻格子颜色不同的方案数 2014西安现场赛F题 容斥原理)
n个格子排成一行,有m种颜色,问用恰好k种颜色进行染色,使得相邻格子颜色不同的方案数. integers n, m, k (1 ≤n, m ≤ 10^9, 1 ≤ k ≤ 10^6, k ≤ n, m ...
- 卡特兰数&错排&一个一直记不住的公式
卡特兰数 公式:f(x)=f(2)*f(x-1)+f(3)*f(x-2)+......+f(x-1)*f(2) #include<iostream>#include<cstdlib& ...
- 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背包那样用滚动 ...
随机推荐
- sql 动态行转列 (2005及以上版本)
表数据: sql: --pivot方案 sql 2005及以上版本 ) Set @sql=(Select DISTINCT ','+ N'[' +pref_name+N']' FROM dbo.Pop ...
- deepin可视化程序打不开问题排查方法
anyconnect是一个VPN软件,在deepin系统下安装完成之后,并不能够直接使用,点击启动图标之后没有反应. 要想分析问题,必须从命令行入手,错误会打印在控制台. 如何根据一个图标来找到一个程 ...
- 关于git回退版本的一点心得
我由于开发中不小心在master分支上开发,忘记了切换分支,最后我直接在master分支上提交,push,在开发分支上merge了master分支. 然后,同事告诉我他的代码要准备上线了,然而我的代码 ...
- python3使用模块
Python内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env python3 # -*- codi ...
- 阿里云 centos7 安装MySQL8.0.13
1.下载MySQL安装包(这里是有技巧的,说不定我这时写这个的时候版本还是你看到时的旧版本了,如果已经不是8.0了,可以根据这样来 下新版本) 先进入官网 再将这两者一结合,就是最新版本的了 所以 [ ...
- Nexus6P 设置Debug模式
一劳永逸,设置手机进入Debug模式实现完美Root 使用mkbootimg进行boot.ing编辑 将修改的系统官方Rom包中的boot.ing和mkbooting工具中的mkboot.mkboot ...
- AI 图像识别的测试
随着AI 的浪潮发展,AI 的应用场景越来越广泛,其中计算机视觉更是运用到我们生活中的方方面面.作为一个测试人员,需要紧跟上 AI 的步伐,快速从传统业务测试,转型到 AI 的测试上来.而人脸识别作为 ...
- vue-cli 3 和 vue-cli 2的区别
分享文章: 浅谈vue-cli 3 和 vue-cli 2的区别 https://blog.csdn.net/weixin_42080056/article/details/81631661 vue- ...
- 2019.6.11_MySQL进阶三:临时表
临时表 临时表主要应用于保存一些临时数据.临时表只在当前连接可见.当关闭连接时,MySQL会自动删除表并且释放空间.临时表在MySQL 3.23版本中添加,低于 3.23版本就无法使用MySQL的临时 ...
- LG4171/BZOJ1823 「JSOI2010」满汉全席 2-SAT
问题描述 LG4171 BZOJ1823 题解 显然,每个评委对每个材料的满式/汉式要求是对\(n\)个元素的\(0,1\)取值限制. 显然想到\(\mathrm{2-SAT}\) 于是就可以切掉了. ...