hdu 5187 zhx's contest

zhx's contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3119    Accepted Submission(s): 1006

Problem Description
As one of the most powerful brushes, zhx is required to give his juniors n problems.
zhx thinks the ith problem's difficulty is i. He wants to arrange these problems in a beautiful way.
zhx defines a sequence {ai} beautiful if there is an i that matches two rules below:
1: a1..ai are monotone decreasing or monotone increasing.
2: ai..an are monotone decreasing or monotone increasing.
He wants you to tell him that how many permutations of problems are there if the sequence of the problems' difficulty is beautiful.
zhx knows that the answer may be very huge, and you only need to tell him the answer module p.
 

 

Input
Multiply test cases(less than 1000). Seek EOF as the end of the file.
For each case, there are two integers n and p separated by a space in a line. (1n,p1018)
 
Output
For each test case, output a single line indicating the answer.
 
Sample Input
2 233 3 5
 
Sample Output
2 1
Hint
In the first case, both sequence {1, 2} and {2, 1} are legal. In the second case, sequence {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1} are legal, so the answer is 6 mod 5 = 1
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  6205 6204 6203 6202 6201 
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
long long n,p,num,ans;
long long fastmul(long long a,long long b,long long p){ 
    long long bns=0;  
    a=a%p;
    b=b%p;
    while(b>0){
        if(b&1) bns=(bns+a)%p;
        a=(a+a)%p;
        b>>=1;
    }
    return bns;
}
long long fastpow2(long long a,long long x,long long p){
    long long s=1%p;
    for(;x;x>>=1){
        if(x&1)
            s=(long long)fastmul(s,a,p)%p;
        a=(long long)fastmul(a,a,p)%p;
    }
    return s;
}
int main(){
//    freopen("permutation.in","r",stdin);
//    freopen("permutation.out","w",stdout);
    while(scanf("%I64d%I64d",&n,&p)!=EOF){
        if(n==1){
            cout<<"0"<<endl;
            continue;
        }
        num=fastpow2(2,n-2,p);
        ans=fastmul(4,num-1,p);
        ans=(ans+2)%p;
        printf("%I64d\n",ans);
    }
}

 

 
posted @ 2017-09-13 14:33  一蓑烟雨任生平  阅读(87)  评论(0编辑  收藏  举报