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. (1≤n,p≤1018)
 
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=1:那么符合条件的有C04个
i=2:那么符合条件的有C14个
i=3:那么符合条件的有C24个
i=4:那么符合条件的有C34个
i=5:那么符合条件的有C44个
所以总数是 24 根据二项式定理,那么满足第 (2) 个条件的也有 24 个,但是中间有重复的,就是单调递增的和单调递减的多算了以此,所以减 2,总数就是 24∗2−2,
可以推出这个题的总的方法数就是 2n−1∗2−2, 即 2n−2,还需要注意的问题就是,这个取模的数太大,所以在进行快速幂的时候不要直接乘,要进行快速乘法。
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;
}

  

hdu_5187_zhx's contest的更多相关文章

  1. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  2. hdu 4946 2014 Multi-University Training Contest 8

    Area of Mushroom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. 2016 Multi-University Training Contest 2 D. Differencia

    Differencia Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  4. 2016 Multi-University Training Contest 1 G. Rigid Frameworks

    Rigid Frameworks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. hdu-5988 Coding Contest(费用流)

    题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  6. ZOJ 3703 Happy Programming Contest

    偏方记录背包里的物品.....每个背包的价值+0.01 Happy Programming Contest Time Limit: 2 Seconds      Memory Limit: 65536 ...

  7. 2012 Multi-University Training Contest 9 / hdu4389

    2012 Multi-University Training Contest 9 / hdu4389 打巨表,实为数位dp 还不太懂 先这样放着.. 对于打表,当然我们不能直接打,这里有技巧.我们可以 ...

  8. 2014 Multi-University Training Contest 9#11

    2014 Multi-University Training Contest 9#11 Killing MonstersTime Limit: 2000/1000 MS (Java/Others)   ...

  9. 2014 Multi-University Training Contest 9#6

    2014 Multi-University Training Contest 9#6 Fast Matrix CalculationTime Limit: 2000/1000 MS (Java/Oth ...

随机推荐

  1. 云栖神侠传—阿里云数据库专家德歌告诉你PostgreSQL的那些事

    什么是云栖神侠传: 云栖社区(http://yq.aliyun.com/?utm_source=yqdg),是阿里云面向开发者群体的开放型社区.在云栖社区中,活跃着许多阿里技术大牛,他们在自己的技术领 ...

  2. 【网络编程】TCP网络编程

    TCP网络程序设计 一.模型函数化 图片:TCP编程模型 1.1 创建套接字Socket #include <sys/types.h> /* See NOTES */ #include & ...

  3. Qt 之模型/视图(自定义按钮)

    https://blog.csdn.net/liang19890820/article/details/50974059 简述 衍伸前面的章节,我们对QTableView实现了数据显示.自定义排序.显 ...

  4. Appium 常用方法

    锁定 锁定屏幕 # python driver.lock(5) 将 app 置于后台 把当前应用放到后台去 # python driver.background_app(5) 收起键盘 收起键盘 # ...

  5. 微信证书 javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

    这几天在做微信退款接口,按照api写完之后,在本地测试了下没有问题,于是交给测试让他们在测试环境开测.他们说退款没有成功,感觉去查日志,发现后台报了 javax.net.ssl.SSLExceptio ...

  6. Fastdfs 部署干货

    tracker server and client:192.168.1.42 storage server:192.168.1.46 storage server:192.168.1.53 安装: 安 ...

  7. nginx 应用包编译及常用文件配置

    1.zlib wget http://www.zlib.net/fossils/zlib-1.2.8.tar.gz 2.openssl wget http://www.openssl.org/sour ...

  8. [翻译] NimbusKit

    注意:NimbusKit 是Github上iOS部分开源库排名前20中的一员. http://nimbuskit.info/ https://github.com/jverkoey/nimbus ht ...

  9. Hadoop学习---CentOS中hadoop伪分布式集群安装

    注意:此次搭建是在ssh无密码配置.jdk环境已经配置好的情况下进行的 可以参考: Hadoop完全分布式安装教程 CentOS环境下搭建hadoop伪分布式集群 1.更改主机名 执行命令:vi  / ...

  10. NET(C#):使用HttpWebRequest头中的Range下载文件片段

    转自:http://www.mgenware.com/blog/?p=220 HTTP请求包头信息中有一个Range属性可以指定索取部分HTTP请求的文件.在.NET中则通过HttpWebReques ...