HDU5187 zhx's contest 快速幂+快速乘 数学专题第一题

版权声明:https://blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/86529060

题目P2100

http://acm.hdu.edu.cn/showproblem.php?pid=5187

背景 Background
此系列问题为数论专题
具体参考博文
http://blog.csdn.net/chty2018/article/details/53432272
快速幂+快速乘
描述 Description
作为史上最贪玩的宏志狗,chty已经数月没交数学作业了,而他在月考中数学考了146分,完爆了全班同学,保平认为自己遭到了光速打脸,于是他给chty出了n道题,教他做人。
chty认为第i道题的难度就是i,他想让这些题目排列起来很漂亮。
chty认为一个漂亮的序列{an}下列两个条件均需满足。
1:a1…ai是单调递减或者单调递增的。
2:ai…an是单调递减或者单调递增的。
他想你告诉他有多少种排列是漂亮的。因为答案很大,所以只需要输出答案模p之后的值。
输入格式 Input Format
输入数据若干组
每组数据一行两个整数:n,p
输出格式 Output Format
每组数据输出排列的方案数模p的结果。
样例输入 Sample Input
2 233
3 5
样例输出 Sample Output
2
1
时间限制 Time Limitation
1s
注释 Hint
数据组数<=1000
1<=n,p<=10^18
来源 Source
【HDU5187】contest
题面数据来自宋逸群

题解

嗯,对,这道题也是当初无聊的时候乱翻题时看到的,题面真的有意思,然而当时是在是太菜了,没想到怎么写,当然现在一样很菜,在请教过邱神,康神,王神后,终于过了这道题。下面说思路。
我们根据题目,仔细想想就知道方案数为 2 n 2 2^n-2 ,之后再mod p就行了。
为什么呢?
因为…请跳转chty

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,mod;
inline ll Quick_mul(ll x,ll y)
{
	return ((x*y-(ll)(((long double)x*y+0.5)/mod)*mod)%mod+mod)%mod;
}
inline ll Quick_power(ll a,ll b)
{
	ll ans=1;
	while (b)
	{
		if (b&1)
			ans=Quick_mul(ans,a);
		a=Quick_mul(a,a);
		b>>=1;
	}
	return ans;
}
int main()
{
	while (cin>>n>>mod)
	{
		if (n==1)
			printf("%lld\n",n%mod);
		else
			printf("%lld\n",(Quick_power(2,n)+mod-2)%mod);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/huashuimu2003/article/details/86529060