hdu_5187_zhx's contest

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
Hint
In the first case, both sequence {1, 2} and {2, 1} are legal. In the second case, sequence {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1} are legal, so the answer is 6 mod 5 = 1
 
看了网上大神的思路:
转自:http://blog.csdn.net/qingshui23/article/details/61627248
 
首先我们将题目给定的条件在细化一下:
(1)a1..ai是单调递增的,那么ai..an一定是单调递减的,而且 ai=n
(2)a1..ai是单调递减的,那么ai..an一定是单调递增的,而且 ai=1
将条件细化成这样之后就好想多了,其实 (1)(2) 是没有什么区别的,就拿第一个来说,因为 ai=n 这是确定的,所以只需要确定 i 的位置就行啦,确定好 i 的位置之后,剩下的就是简单的组合数学了,假设 n=5,那么 i 的位置有 5 个,刨除掉 1,还有 4 个数
i=1C04
i=2C14
i=3C24
i=4C34
i=5C44
所以总数是 24 根据二项式定理,那么满足第 (2) 个条件的也有 24 个,但是中间有重复的,就是单调递增的和单调递减的多算了以此,所以减 2,总数就是 2422
可以推出这个题的总的方法数就是 2n122, 即 2n2,还需要注意的问题就是,这个取模的数太大,所以在进行快速幂的时候不要直接乘,要进行快速乘法。
i=1与i=n的递增与递减,计算了两遍。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100005
ll mul(ll a,ll b,ll m)
{
    ll res=0;
    while(b)
    {
        if(b&1) res+=a;
        if(res>m) res-=m;
        a+=a;
        if(a>m)
        a-=m;
        b>>=1;
    }
    return res;
}
ll pow(ll a,ll b,ll m)
{
    ll res=1;
    while(b)
    {
        if(b&1) res=mul(res,a,m);
        a=mul(a,a,m);
        b>>=1;
    }
    return res;
}

int main()
{
    ll n,p;

    while(~scanf("%lld%lld",&n,&p))
    {
        if(n==1&&p!=1)
            {puts("1");continue;}
        else if(n==1&&p==1)
            {puts("0");continue;}
        ll ans=pow(2,n,p)-2;
        ans%=p;
        ans=(ans+p)%p;
        printf("%lld\n",ans);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/ygtzds/p/8167630.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值