hdu1465 Not Easy Series

Source: Internet
Author: User

Analysis: Recursion.

When N=1 and 2 o'clock are readily available, it is assumed that F (N-1) and F (N-2) have been obtained, focusing on the following conditions:

When there is N letter, the front N-1 letter can have N-1 or N-2 seal the wrong outfit
The former, for each kind of wrong outfit, can take a letter from N-1 letter and the nth seal wrong outfit, therefore =f (N-1) * (N-1)
The latter is simple, can only be not the wrong one and the nth exchange envelope, the wrong seal can be any of the front N-1 seal, so = F (N-2) * (N-1).

Available, f (n) = (n-1) * (f (n-1) +f (n-2)).

Small amount of data can be directly hit the table.

1#include <cstdio>2 using namespacestd;3 4 intn,sum;5 Long Longres[ +]={0,0,1,2};6 7 intMain ()8 {9      for(intI=4;i< +; i++){Tenres[i]= (I-1) * (res[i-1]+res[i-2]); One     } A      while(SCANF ("%d", &n)! =EOF) -     { -printf"%lld\n", Res[n]); the     } -     return 0; -}

Recursive representation

1#include <cstdio>2 #defineLL Long Long3 using namespacestd;4 5 intN;6 7LL F (intN)8 {9     if(n==1|| n==0)return 0;Ten     if(n==2)return 1; One     if(n==3)return 2; A     return(n1) * (f (n1) +f (n2)); - } -  the intMain () - { -      while(~SCANF ("%d",&N)) -     { +printf"%lld\n", f (n)); -     } +     return 0; A}

hdu1465 Not Easy Series

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.