Logu P1595 envelope problem, logu p1595 envelope

Source: Internet
Author: User

Logu P1595 envelope problem, logu p1595 envelope
Description

Someone wrote n letters and n envelopes, if all the letters were wrong. The number of different situations in which the wrong envelope is loaded for all messages.

Input/Output Format

Input Format:

 

Number of envelopes n (n <= 20)

 

Output Format:

 

An integer represents the number of cases.

 

Input and Output sample input sample #1: Copy
2
Output example #1: Copy
1
Input example #2: Copy
3
Output sample #2: Copy
2

 

Recursive Formula: $ f [n] = (n-1) * (f [n-1] + f [N-2]) $

# Include <iostream> # include <cstdio> using namespace std; int main () {int N; cin> N; int a1 = 0, a2 = 1, a3; for (int I = 3; I <= N; I ++) {a3 = (I-1) * (a1 + a2); a1 = a2, a2 = a3 ;} if (N = 1) cout <a1; else if (N = 2) cout <a2; else cout <a3; return 0 ;}

 

 

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.