HDU 5187 zhx's contest(防爆__int64 )

本文探讨了在解决问题时如何通过排列组合创造出美丽而有效的序列。通过详细解释美丽序列的定义及其规则,作者展示了如何计算一系列问题难度的排列组合方式,并提供了具体的计算公式和示例输入输出,为读者提供了一种全新的思考问题解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Problem Description
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.
 

Input
Multiply test cases(less than 1000). Seek EOF as the end of the file.
For each case, there are two integers n and p separated by a space in a line. (1n,p1018)
 

Output
For each test case, output a single line indicating the answer.
 

Sample Input
2 233 3 5
 

Sample Output
2 1

思路:枚举  减 减     ;  减   加      ;           加  减                      加      加             一共     2^(n-1)*2-2

                     1                  2^(n-1) -2              2^(n-1) -2                1





//  2^n-2

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 1001


ll n,mod;

ll fdd(ll x,ll m)   //计算  x*m 居然不能直接算,否者会爆__int64 
{
	ll ans=0;
	while(m)
	{
		if(m&1) ans=(ans+x)%mod;
		x=(x+x)%mod;
		m>>=1;
	}
   return ans;
}

ll pow_(ll n,ll m)
{
     ll ans=1;

     while(m)
	 {
	 	 if(m&1) ans=fdd(ans,n);  //计算 ans*n
	 	 n=fdd(n,n);              //计算 n*n
	 	 m>>=1;
	 }
      return (ans-2+mod)%mod;
}

int main()
{
	while(~scanf("%I64d%I64d",&n,&mod))
	{
		  ll ans=2;
		  if(n==1)
		  {
            ans=n%mod;
		  	pf("%I64d\n",ans);
		  	continue;
		  }
		  printf("%I64d\n",pow_(ans,n));
	}

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值