容斥原理--计算错排的方案数 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背包那样用滚动 ...
随机推荐
- Retrofit的优点
Retrofit的优点 可以配置不同HTTP client来实现网络请求,如okhttp.httpclient等 将接口的定义与使用分离开来,实现结构. 支持多种返回数据解析的Converter可以快 ...
- easyui datagird 解决行高不一致问题!
<style>.datagrid-btable .datagrid-cell {padding: 6px 4px;overflow: hidden;text-overflow: ellip ...
- addEventListener和JavaScript的事件机制
JavaScript的事件处理分为两个阶段: 捕获阶段:从根节点向event.target层层传递 冒泡阶段:从event.target向根节点层层传递 addEventListener(eventN ...
- 斗鱼刷弹幕js代码
对于一个网络喷子(like me)来说,喷人必须高效. var script=document.createElement("script"); script.type=" ...
- web下载附件及修改名称
/** * @param: url 附件地址 * @param: filename 下载后的文件名 */ function download(url, filename) { getBlob(url, ...
- XGBoost 完整推导过程
参考: 陈天奇-"XGBoost: A Scalable Tree Boosting System" Paper地址: <https://arxiv.org/abs/1603 ...
- packstack快速部署openstack
环境准备 建议16GB RAM sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/config systemctl stop firewall ...
- 如何在docker镜像里安装pycuda和numba?
其实,安装numba还好,直接pip install numba就可以. 但pycuda就不那么友好了. 默认安装时,可能会报如下错误: src/:: fatal error: curand.h: N ...
- 【洛谷P5331】 [SNOI2019]通信
洛谷 题意: \(n\)个哨站排成一列,第\(i\)个哨站的频段为\(a_i\). 现在每个哨站可以选择: 直接连接到中心,代价为\(w\): 连接到前面某个哨站\(j(j<i)\),代价为\( ...
- appium 爬取抖音
1.MongoDB.py import pymongo from pymongo.collection import Collection client = pymongo.MongoClient(h ...