解题思路
手推了两个就发现了规律:
我们拿
4
4
4举例子,因为4在数列中是最大的,所以它一定是单峰,然后我们考虑在4的两旁填数:
- 左边没有,右边三个, a n s + = C 3 0 ; ans+=C_3^0; ans+=C30;
- 左边一个, a n s + = C 3 1 ; ans+=C_3^1; ans+=C31;
- 左边两个, a n s + = C 3 2 ; ans+=C_3^2; ans+=C32;
- 左边三个,右边没有, a n s + = C 3 3 ; ans+=C_3^3; ans+=C33;
这就是杨辉三角上的一行, a n s = 2 ( 4 − 1 ) = C 3 0 + C 3 1 + C 3 2 + C 3 3 = 8 ans=2^{(4-1)}=C_3^0+C_3^1+C_3^2+C_3^3=8 ans=2(4−1)=C30+C31+C32+C33=8
题目的n很大,要用快速幂
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll n;
ll ksm(ll x,ll b){
ll ans=1,base=x;
while(b)
{
if(b&1)ans=(ans*base)%mod;
base=(base*base)%mod;
b=b>>1;
}
return ans;
}
int main(){
scanf("%lld",&n);
printf("%lld",ksm(2,n-1));
}