Single-peak arrangement

Source: Internet
Author: User

"Title description"

An n's full permutation a[i] is a single-peak, when and only if there is an X that makes A[1]<a[2]<...<a[x]>a[x+1]>...>a[n].

For example, for a 9 full arrangement, 125798643 is a single-peak arrangement, and 123456789 is a single-peak arrangement, but 356298741 is not.

Try to find the number of a single peak of N.

"Input Format"

An integer n

"Output Format"

The total number of single-peak permutations.

Because the result can be large, output the result to 1234567 after the modulo.

"Sample Input"

3

"Sample Output"

4

"Data Range"

1<=n<=2000000000

Analysis

First, be clear: n must be the highest point of the "peak", followed by n-1,n-2,...,1. Among them, n-1 this number can be placed on the left of the "mountain", can also be placed on the right of the "mountain", and then n-2 is also so ... Until 1, there are two kinds of placement schemes, so the final result is 2^n-1.

Only know that this is not enough, if the loop to find the time, so the use of fast power or divide the solution, the following code is divided.

var
  n:longint;
function FCT (x:longint): Qword;
Begin
  If X=0 then exit (1);
  If X=1 then exit (2);
  FCT:=SQR (FCT (x Div 2)) mod 1234567;
  If x mod 2<>0 then fct:=fct*2 mod 1234567;
End;
Begin
  Read (n);
  Write (FCT (n-1));
End.

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.