Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1867 Accepted Submission(s): 596
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.
For each case, there are two integers n and p separated by a space in a line. (1≤n,p≤1018)
#include<iostream> #include<cstdio> #include<cstring> #include <algorithm> #include <math.h> using namespace std; typedef long long LL; LL n,m; LL modular_multi(LL a, LL b, LL c) /// a * b % c { LL res, temp; res = 0, temp = a % c; while (b) { if (b & 1) { res += temp; if (res >= c) { res -= c; } } temp <<= 1; if (temp >= c) { temp -= c; } b >>= 1; } return res; } LL modular_exp(LL a, LL b, LL c) { LL res, temp; res = 1 % c, temp = a % c; while (b) { if (b & 1) { res = modular_multi(res, temp, c); } temp = modular_multi(temp, temp, c); b >>= 1; } return res; } int main() { while(scanf("%lld%lld",&n,&m)!=EOF) { LL ans = modular_exp(2,n,m); printf("%lld\n",(ans-2+m)%m); } return 0; }