当前位置:网站首页>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, '%');
}
}
边栏推荐
- Numpy np. Max and np Maximum implements the relu function
- R language uses data The table package performs data aggregation statistics, calculates window statistics, calculates the median of sliding groups, and merges the generated statistical data into the o
- 2022 northeast four provinces match VP record / supplementary questions
- How should intermediate software designers prepare for the soft test
- vulnhub之narak
- Slam mapping and autonomous navigation simulation based on turnlebot3
- Niuniu's team competition
- vulnhub之raven2
- Spl06-007 air pressure sensor (example of barometer)
- Duplicate numbers in the array of sword finger offer 03
猜你喜欢
MCDF实验1
uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
外插散点数据
Vulnhub narak
导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
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
Raven2 of vulnhub
随机推荐
如何将数字字符串转换为整数
Machine learning 3.2 decision tree model learning notes (to be supplemented)
DS90UB949
rxjs Observable filter Operator 的实现原理介绍
OpenStack中的测试分类
并发编程-单例
ftp登录时,报错“530 Login incorrect.Login failed”
Sheet1$.输出[Excel 源输出].列[XXX] 出错。返回的列状态是:“文本被截断,或者一个或多个字符在目标代码页中没有匹配项。”。
STL tutorial 10 container commonalities and usage scenarios
Vulnhub narak
Vulnhub's cereal
The R language uses the hist function in the native package (basic import package, graphics) to visualize the histogram plot
Redis things
vulnhub之presidential
Vulnhub's presidential
ArcGIS应用(二十一)Arcmap删除图层指定要素的方法
Hongmeng third training (project training)
银泰百货点燃城市“夜经济”
Kibana - installation and configuration of kibana
vulnhub之Ripper