HDU 5187 zh's contest(快速幂 + 快速乘)

题目大意:

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 .

解题思路:

峰值要么是最大的要么是最小的,当峰值的位置确定之后,其他数要么在左边要么在右边共有2^(n-1)中选择,其中单调递增和单调递减两种情况算了两次要减去2.所以结果为2^n - 2;不过出题人非要把题目搞复杂来恶心人,不能用普通的乘法,必须用快速乘法,跟快速幂差不多。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define LL long long 
using namespace std;
LL mod;
LL mul(LL a, LL b)
{
	LL res = 0;
	a %= mod;
	while(b)
	{
		if(b & 1)
		{
			res = res + a;
			if(res >= mod) res -= mod;
		}
		a <<= 1;
		if(a >= mod) a -= mod;
		b >>= 1;
	}
	return res;
}
LL Pow(LL a, LL b)
{
	LL res = 1;
	while(b)
	{
		if(b & 1) res = mul(res, a);
		a = mul(a, a);
		b >>= 1;
	}
	return res;
}
int main()
{
	LL n;
	while(scanf("%I64d%I64d", &n, &mod)!=EOF)
	{
		if(n == 1)
		{
			printf("%I64d\n", n % mod);
			continue;
		}
		printf("%I64d\n", (Pow(2, n) - 2 + mod)%mod);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值