(纪中)4742. 单峰【快速幂】

在这里插入图片描述
在这里插入图片描述


解题思路

手推了两个就发现了规律:
我们拿 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(41)=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));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值