HDU5187 zhx & #39; s contest (count problem)
Question:
From 1 ~ N, how many sort
Make a1 ~ Ai can be monotonic or monotonic.
Ai ~ An is monotonically increasing or decreasing.
Obvious combination Problems
Select the number of I from n. The remaining number must meet the monotonic increasing or decreasing law, so the method is unique.
Ans = (C (N, 0) + C (N, 1) +... + C (N, N) = 2 ^ N;
But in this case, the monotonic increasing and monotonic decreasing are calculated twice, so we need to subtract 2.
Ans = 2 ^ n-2;
Note that n = 1, because n is relatively large, pay attention to multiplication Overflow
The Code is as follows:
#include
#include
#include
#include using namespace std;typedef long long LL;LL multi(LL a,LL b, LL c){ LL ans = 0; while(b){ if(b&1){ ans= (ans+a)%c; b--; } b>>=1; a=(a+a)%c; } return ans;}LL quick_mod(LL a,LL b,LL c){ LL ans = 1; while(b){ if(b&1){ ans = multi(ans,a,c); b--; } b>>=1; a=multi(a,a,c); } return ans ;}int main(){ LL n,p; while(~scanf(%lld%lld,&n,&p)){ if(n==1){ printf(%d,1%p); continue; } LL ans = 2; ans = quick_mod(ans,n,p); ans =(ans - 2 + p)%p; printf(%I64d,ans); } return 0;}