当前位置:网站首页>Wrong arrangement (lottery, email)
Wrong arrangement (lottery, email)
2022-07-03 11:51:00 【-YIN】
Staggered algorithm
Staggered concept :

n An ordered element should have n! A different arrangement , If an arrangement Make all elements not in their original positions On , This arrangement is called Dislocation ; Some are called rearrangement .
Such as ,1 2 The wrong arrangement of is the only , namely 2 1.1 2 3 The wrong arrangement is 3 1 2,2 3 1. These two can be regarded as 1 2 Dislocation ,3 Respectively with 1、2 From transposition .
Recursive relation derivation
To find its recurrence relation , A two-step :
First step , Consider the n Elements , Put it in a certain position , For example, location k , Altogether n-1 Planting ;
The second step , Consider the k Elements , There are two scenarios :
- Put it in place n, So for division n Outside of the n-1 Elements , Due to the first k An element is placed in position n, So the rest is n-2 The staggered arrangement of elements can be , Species D(n-2) Release method ;
- The first k Elements are not placed in position n, At this time, for this n-1 Staggered arrangement of elements , Yes D(n-1) Planting .
To sum up, we can get D(n) = (n-1) * ( D(n-2) + D(n-1) )
Specifically ,D(1) = 0, D(2) = 1
Besides :
There are also several staggered generation algorithms : Recursive method , If you are interested in the screening method based on dictionary order and the improved dictionary order method, you can have an in-depth understanding
Example
Cattle from — email
use A、B、C… Means three emails ,a、b、c … Express n People to send . Record the total number of misfits as
D(n). Suppose that A Wrong delivery b In the , There are two kinds of wrong installation methods that contain this error :
- to b Sent A mail , At this time, every kind of wrong hair A->b The relationship has been established ( The rest is related to A、B、a、b irrelevant ), due D(n - 2) Kind of wrong hair method .
- to b send out A、B Another email , At this time, it is actually ( except A In addition to the ) n- 1 An email B、C issue ( except b Outside of the ) n - 1 personal a、c… …, Obviously, there are ways to send errors at this time D(n - 1) Kind of .
All in all A Wrong delivery b In the case of , Common wrong hair method D(n- 2)+ D(n- 1) Kind of .
A Wrong delivery c, issue d… Of n - 2 Under a mistake , There are also D(n- 1)+ D(n - 2) Kind of wrong hair method , therefore D(n) = (n-1) [ D(n-2) + D(n-1) ]
Specifically ,D(0) = 0, D(1) = 0, D(2) = 1
#include <iostream>
using namespace std;
// Dislocation
/* arr[1] = 0, arr[2] = 1; The recursive formula :arr[n] = (n-1)*(arr[n-1]+arr[n-1]); */
int main()
{
long long arr[22] = {
0, 0, 1};
for(int i = 3; i < 21; ++i)
arr[i] = (i-1) * (arr[i-1]+arr[i-2]);
int n;
while(cin >> n){
cout << arr[n] << endl;
}
return 0;
}
The annual meeting draw
Cattle from — The annual meeting draw
This year's annual awards are especially awesome. , But the rules for winning awards are wonderful :
- First , All the staff put a note with their names in the lucky draw box ;
- When all the notes have been added , Take a note from the box ;
- If the note drawn is written with your name , that “ congratulations , I won the prize !”
Now let me tell you the number of people attending the party , Please calculate the probability that no one will win the prize ?

Reference code :
#include <iostream>
using namespace std;
// Recursively calculate the number of staggered sequences
long long Derangement(int n){
if(1 == n) return 0;
if(2 == n) return 1;
// The recursive formula
return (n-1)*(Derangement(n-2)+Derangement(n-1));
}
// Calculate the factorial ( All possible sequence numbers )
long long factorial(int n){
long long f = 1;
for(int i = 1; i <= n; ++i)
f *= i;
return f;
}
int main()
{
int n;
while(cin >> n){
double res = (double)Derangement(n) / factorial(n);
printf("%.2lf%c\n",res*100, '%');
}
}
边栏推荐
- Slam mapping and autonomous navigation simulation based on turnlebot3
- 量化计算调研
- How to get started embedded future development direction of embedded
- Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
- Kibana~Kibana的安装和配置
- previous permutation lintcode51
- OpenStack中的测试分类
- Vulnhub's presidential
- Ripper of vulnhub
- vulnhub之momentum
猜你喜欢

金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~

Hongmeng third training (project training)

Based on MCU, how to realize OTA differential upgrade with zero code and no development?

Web安全总结

Viewing binary bin files with notepad++ editor

vulnhub之raven2

cgroup简介

Slam mapping and autonomous navigation simulation based on turnlebot3

PHP server interacts with redis with a large number of close_ Wait analysis

Kibana~Kibana的安装和配置
随机推荐
Kubernetes 三打探针及探针方式
Go language to realize static server
Nestjs配置服务,配置Cookie和Session
How to make others fear you
"Jianzhi offer 04" two-dimensional array search
Vulnhub's presidential
Excel表格转到Word中,表格不超边缘纸张范围
How to mix embedded MCU, arm and DSP?
VS2015的下载地址和安装教程
Concurrent programming - singleton
R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)
Mmc5603nj geomagnetic sensor (Compass example)
Qt+VTK+OCCT读取IGES/STEP模型
Asyncio warning deprecationwarning: there is no current event loop
DNS多点部署IP Anycast+BGP实战分析
mysql使用update联表更新的方法
如何将数字字符串转换为整数
(数据库提权——Redis)Redis未授权访问漏洞总结
小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
《剑指offer 03》数组中重复的数字


