Combined mathematics-sorting formula of full dislocation

Source: Internet
Author: User
It's not easy to use problem description, one of the series. People often feel that it's really not easy to do a good job. Indeed, failure is much easier than success!
It is not easy to do the "one thing" thing well. If you want to succeed forever and never fail, it is even harder, just as spending money is always easier than making money.
Even so, I still want to tell you that it is not easy to fail to a certain extent. For example, when I was in high school, there was a magical girl who did all the 40 single-choice questions wrong during the English test! Everyone has learned probability theory and should know the probability of such a situation. So far, I think this is a magic thing. If we use a classic comment, we can conclude that it is not difficult for a person to make a wrong choice question. What is difficult is to make all the mistakes wrong or wrong.

Unfortunately, this kind of small probability event happened again, and it was around us:
This is the case -- HDU has a male student named 8006, who has made countless friends. Recently, this student played a romantic game and wrote a letter to each of the N netizens. This is nothing, the worst thing is, he put all the messages in the wrong envelope! Note: It's all wrong!

The question is: How many possible error methods are there for the poor 8006 students? The input data contains multiple test instances. Each test instance occupies one row and each row contains a positive integer of N (1 <n <= 20). N indicates the number of netizens of 8006. Output: for each line of input, please output the number of possible error methods. The output of each instance occupies one line. Sample input23 sample output12 authorlcy

 

Mean: 

Omitted

Analyze:

It is a simple application of the error sorting formula. The following describes the formula for troubleshooting.

The so-called "wrong sorting" refers to the full dislocation sorting formula, which is called the "Wrong envelope Loading Problem" by the famous mathematician Leonhard Euler (1707-1783 ", he solved the following problems:

A person writes n different letters and corresponding N different envelopes. He puts all the N letters in the wrong envelope and asks how many kinds of wrong envelopes can be installed?

 

Recursive Formula: F (n) = (n-1) * {f (n-1) + f (n-2 )}

 

 

Time Complexity:O (N)

 

Source code:

 

// Memory   Time// 1347K     0MS// by : Snarl_jsb// 2014-09-15-21.27#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<vector>#include<queue>#include<stack>#include<map>#include<string>#include<climits>#include<cmath>#define N 1000010#define LL long longusing namespace std;long long a[N];void cuopai(long long n)  ////   Formula :  f(n)=(n-1)*{f(n-1)+f(n-2)} ;{    a[1]=0,a[2]=1;    for(long long i=3;i<=n;i++)    {        a[i]=(i-1)*(a[i-1]+a[i-2]);    }}int main(){//    freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);//    freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);    cuopai(30);    int n;    while(cin>>n)    {        cout<<a[n]<<endl;    }    return 0;}

  

Combined mathematics-sorting formula of full dislocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.