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. (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
就是求(2^n-2)%p
注意的是 需要用到大数乘法,因为快速幂会爆long long
快速乘法的思想和快速幂的思想一样,快速幂是求一个数的高次幂,快速乘法是求两个数相乘,
什么时候才用得到快速乘法呢,当两个数相称可能超过long long 范围的时候用,
因为在加法运算的时候不会超,而且可以直接取模,这样就会保证数据超不了了。
ac代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000007
ll n, p;
ll multi(ll a, ll b)
{ //快速乘法
ll ret = 0;
while(b) {
if(b & 1) ret = (ret + a) % p;
a = (a + a) % p;
b >>= 1;
}
return ret;
}
ll power(ll a, ll b) { //快速幂
ll ret = 1;
while(b) {
if(b & 1) ret = multi(ret, a) % p; //ret=(ret*a)%p
a = multi(a, a) % p; //a=(a*a)%p
b >>= 1;
}
return ret;
}
int main()
{
while(cin>>n>>p)
{
if(p==1)cout<<"0"<<endl;
else if(n==1)cout<<"1"<<endl;
else
{
ll ans=power(2,n)-2;
if(ans<0)ans+=p;
cout<<ans<<endl;
}
}
return 0;
}