That is, with that AI as a watershed, the left and right are monotonically increasing or monotonically decreasing.
In these four cases, the first two are in total two sequences, that is, from beginning to end and decreasing from start to finish.
The latter two ways is the sequence of the number of Germans into the left and right, divided after the left and right sides of the internal row of the method has been determined, as for the AI has been determined (not the largest global is the smallest), and in addition to each of the number of AI have a choice in the left or in two options, so is 2^ (n-1) And this includes the first two scenarios, so to 4, the end should be 2^n-2 species.
Look at the data range to know to use the fast power, but unfortunately if only with the fast power will be wrong, should be the range of N and P is 10^18, fast power in the multiplication before the modulo p may be more than a long long int. So think with addition, A*b is b a add, each step is modulo p,8 a add =c c+c = 16 A sum, and fast power a truth. And then using the principle of fast power to keep two points, this is called fast multiplication.
There is special data, when the n input is 1, the case number is not 0 but 1, of course, not as the BC said the result is 1, but 1MODp.
1 //2 //main.cpp3 //hdu51874 //5 //Created by Opas on 15/3/18.6 //Copyright (c) 2015 Opas. All rights reserved.7 //8 9#include <iostream>Ten#include <cstdio> One#include <string.h> A using namespacestd; -typedefLong LongLL; - ll Modj (ll A, ll B, ll m) { theLL ans=0; - while(b) { - if(b&1) ans= (ans+a)%m; -b>>=1; +A= (a+a)%m; - } + returnans; A } at ll MODx (ll A, ll B, ll m) { -LL ans=1; - while(b) { - if(b&1) ans=Modj (ans,a,m); -b>>=1; -A =Modj (a,a,m); in } - returnans; to } + intMainintargcConst Char*argv[]) { - LL n,p; the while(SCANF ("%lld%lld", &n,&p) = =2){ * if(n==1){ $printf"%d\n",p>1?1:0);Continue;Panax Notoginseng } -printf"%lld\n", (MODx (2, N, p)-2+P)%p); the } + return 0; A}
HDU 5187 fast Power + fast plus worth learning