Topic 1451: One of the Not-easy series

Source: Internet
Author: User

Title Description:

We often feel that to do a good thing is really not easy, indeed, failure is much easier than success!
Doing "One" thing is not easy, if you want to always succeed and always never fail, it is even more difficult, like spending money is always easier than the truth.
That said, 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, in the English exam, unexpectedly put 40 single-choice questions all wrong! Everyone has learned probability theory, should know the probability of this situation, so far I think this is a magical thing. If we apply a classic comment, we can conclude that it is not difficult for a person to do a wrong choice, and it is difficult to do all wrong.

Unfortunately, this small probability event has happened again, and it's around us:
The thing is this--hdu has a network name called 8006 Male classmates, make countless friends, recently the classmate played a romantic, at the same time to n a Netizen each wrote a letter, this is nothing, it is, he unexpectedly put all the letters are loaded the wrong envelope! Watch out, it's all loaded wrong yo!

Now the question is: please help the poor 8006 students to calculate, altogether how many kinds of possible wrong way?

Input:

The input data contains more than one test instance, one row per test instance, each row containing a positive integer n (1<n<=20), and N represents the number of 8006 netizens.

Output:

For each line of input, output the number of possible error modes, and the output of each instance consumes one row.

Sample input:
23
Sample output:
12

Dislocation permutation problem
use a, B, C ... An envelope written with the names of N friends, A, B, c ... Represents n copies of the corresponding written stationery. The total number of wrong attire is recorded as f (n). Assuming that a error is loaded into B, there are two types of wrong-loading methods that contain the mistake: (1B is loaded in a, when the remainder of each type of error is unrelated to a, B, a, b, should be f (N-2) The method of wrong loading. (2b into an envelope outside a and B, at which time the letter-loading work is actually put (except for a) (N-1) Letter B, C ... Loading (except for B) n-1 envelopes A, C ..., obviously this fashion wrong method has F (n1) species. All in all, under the error of a loading B, there is a common wrong loading method F (n-2) +f (n1Two A loading C, loading D ... N-2 errors, there are also F (N-2) +f (n1), so: F (n)= (n1) {f (n1) +f (n2)} formula can be rewritten as f (n)-NF (n1) =-[f (n1)-(n1) F (n2)] (n>2so you can get f (n)-NF (n1) =-[f (n1)-(n1) F (n2)]=((-1)^2) [F (n2)-(n2) F (n3)]=((-1)^3) [F (n3)-(n3) F (n4)]=...=[(-1) ^ (n2)][f (2) -2f (1)] and finally get a simpler recursive f (n)=NF (n1)+(-1) ^ (n2) or an equivalent F (n)=NF (n1)+(-1) ^ (n) n=2,3,4......

The code is as follows

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <algorithm>5#include <iostream>6 using namespacestd;7typedefLong Longll;8 intN, K;9 Tenll dp[ -]; One intMainintargcChar Const*argv[]) A { -dp[0] =1; -dp[1] =1; thedp[2] =1; -dp[3] =2; -      for(inti =4; I <= -; i++) { -Dp[i] = (I-1) * (dp[i-1]+dp[i-2]); +     } -      while(SCANF ("%d", &n)! =EOF) { +printf"%lld\n", Dp[n]); A     } at     return 0; -}

Topic 1451: One of the Not-easy series

Related Article

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.