容斥原理--计算错排的方案数 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背包那样用滚动 ...
随机推荐
- Vertx和Jersey集成使用
为了更好地解耦和提高性能,一般将工程的接口部分剥离出来形成一个单独的工程,这样不仅能提高性能,增强可维护性,并且在后台工程宕掉的话对客户端接口的影响较小. 公司使用了Vertx和Jersey,Vert ...
- CMake编译的VS工程,安装时遇到错误:error MSB3073: 命令“setlocal
错误提示 70>CMake Error at src/base/cmake_install.cmake:63 (file): 70> file INSTALL cannot find 70 ...
- git使用读书笔记
Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable ...
- ios 点击webview获取图片url (js交互)
加手势 -(void)handleSingleTap:(UITapGestureRecognizer *)sender { CGPoint pt = [sender locationInView:_c ...
- [基础]斯坦福cs231n课程视频笔记(三) 训练神经网络
目录 training Neural Network Activation function sigmoid ReLU Preprocessing Batch Normalization 权重初始化 ...
- JS高阶---函数
[问题] [主体] 1.什么是函数? ①实现特定功能 ②多条语句的封装体 ③可以重复执行的代码块 2.为什么用函数? 提高代码的复用性,提升效率 3.如何定义函数? ①函数声明定义 ②函数表达式定义③ ...
- 破解Beyond Compare 4
Beyond Compare 4 30天试用期后,破解方法. 方法一:在安装目录下找到文件BCUnrar.dll,比如:D:\software\Beyond Compare 4,重命名该文件即可. 重 ...
- 基于DBUtils实现数据库连接池及flask项目部署
阅读目录 flask中是没有ORM的,如果在flask里面连接数据库有两种方式 数据库连接池原理 模式一: 模式二: 数据库连接池 flask中是没有ORM的,如果在flask里面连接数据库有两种方式 ...
- 17-numpy笔记-莫烦pandas-5
代码 import pandas as pd import numpy as np left=pd.DataFrame({'key':['K0','K1','K2','K3'], 'A':['A0', ...
- Maven 本地仓库同步到私服中
步骤: 第一步:找到安装私服的目录中plexus.properties文件. 地址:C:\Windows\apache-tomcat-7.0.26\webapps\nexus-2.7.0-06\WEB ...