cqyz oj | unimodal arrangement

Description

  1 ~ n of a full array A [i] is unimodal if and only if there is an x such that:

   A [. 1] <A [2] <... <A [x]> A [x +. 1 ]> ...> a [n]

  , for example, for a full array 9 125798643 arrangement is unimodal, singlet 123456789 is arranged, but not 356,298,741.

  Find the whole arrangement of n number of single peak.

Input

  Enter a number n.

Output

  N number of output single peak full array arrangement. Because this number may be large, so you just need to mod it output value of 1234567.

Sample Input 1 

3

Sample Output 1

4

Hint

n<=2 000 000 000
 
 
 

 
The method we have constructed a sequence to meet the requirements that the descending order n to 1 in order to fill the vacancies n.
Because it is "unimodal", so the number of sides to fill the current number must be filled out before, select one of the positions filled, otherwise there will be multimodal.
Such a state can arise: when DP [i] [j] denotes the number of the left has been completed vacancies i, j vacancies right side, the number of aligned single peak
Equation came out: dp [i] [j] = dp [i-1] [j] (the current number of fill in the gaps left) + dp [i] [j-1] (the current number of fill vacancies on the right)
Boundary came out: dp [0] [j] = 1, dp [i] [0] = 1
The answer came out: ans = dp [0] [n-1] + dp [1] [n-2] + ...... + dp [n-1] [0]
 
But the great range of n, this space-time will not work.
See little to transfer equations and n, it is better to make a list of it:
1,1,1,1,1,1,1,1,1...
1,2,3,4,5,6,7,8,9...
1,3,6,10...
1,4,10...
1,5...
We ask answer is the number of a sub-diagonal and
And then look at this:
1=1
1+1=2
1+2+1=4
1+3+3+1=8
...
 
This is not Pascal's Triangle do? This becomes a math problem: Find the n-th row of Pascal's Triangle and.
And see what the law have not found? Yes, the answer is 2 the n--1
Where n ranges <= 2000000000, to this question here, it becomes a
 
Fast power! ! !
 

After the code is no need to have a hold put. Due to the modulo, do not write precision. The process may exceed the range of int, so when the count is to open long long, so that you can before.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define mo 1234567
using namespace std;
typedef long long ll;
int qkpow(int a,int p){
    ll r=1,t=a;
    while(p){
        if(p&1) r = (r*t)%mo;
        t=(t*t)%mo;
        p>>=1;
    }
    return (int)r;
}
int main() {
    int p;
    scanf("%d",&p);
    printf("%d",qkpow(2,p-1));
    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/de-compass/p/11523581.html