HDU5187 zhx's contest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2388 Accepted Submission(s): 755
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.
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. (1≤n,p≤1018)
For each case, there are two integers n and p separated by a space in a line. (1≤n,p≤1018)
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
贪心?
答案是2^n-2
只是用来练快速乘的
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<vector> 8 #define LL long long 9 using namespace std; 10 const int mxn=100010; 11 LL n,p; 12 LL ksmul(LL a,LL b){ 13 a%=p;b%=p;LL res=0; 14 while(b){ 15 if(b&1){res+=a;if(res>p)res-=p;} 16 a=(a<<1)%p; 17 b>>=1; 18 } 19 return res; 20 } 21 LL ksm(LL x,LL k){ 22 LL res=1; 23 while(k){ 24 if(k&1)res=ksmul(res,x); 25 x=ksmul(x,x); 26 k>>=1; 27 } 28 return res; 29 } 30 int main(){ 31 while(scanf("%lld%lld",&n,&p)!=EOF){ 32 if(n==1){ 33 if(p==1)printf("0\n"); 34 else printf("1\n"); 35 continue; 36 } 37 LL ans=ksm(2,n);ans-=2; 38 if(ans<0)ans=((ans%p)+p)%p; 39 printf("%lld\n",ans); 40 } 41 return 0; 42 }
本文为博主原创文章,转载请注明出处。