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. Git 学习记录

    安装 1.  https://git-for-windows.github.io下载(网速慢的同学请移步国内镜像),然后按默认选项安装即可.安装完成后,在开始菜单里找到“Git”->“Git B ...

  2. CentOS 7 禁用IPV6以提高网速

    方法 1 编辑文件/etc/sysctl.conf,$vi /etc/sysctl.conf添加下面的行: net.ipv6.conf.all.disable_ipv6 = net.ipv6.conf ...

  3. GeoJSON相关操作

    简介 本文主要介绍GEOJSON相关的操作 环境 gt-geojson-.jar,jts-1.8.jar 直接上代码 代码 wkt格式的geometry转成json格式 public String e ...

  4. BitmapFactory 读取图片方法总结

    ①decodeFile(java.lang.String pathName)     ②decodeResource(android.content.res.Resources res, int id ...

  5. klee错误汇报二:KLEE的optimize选项的一个困惑

    问题已经提交github:https://github.com/klee/klee/issues/650 在一个对命令行参数进行建模的符号执行过程中,添加optimize选项与不添加optimize选 ...

  6. javascript 事件相关使用总结01

    javascript 事件相关使用总结01 这里总结一下js事件相关的经验. addEventLinstener()介绍 注册事件最基础的函数是这个 target.addEventListener(t ...

  7. Log4Net使用教程

    简介 为方便跟踪程序运行情况,我们可以记录系统运行异常日志,winform和web都可以通过继承异常或者try来实现. 官方网站:http://logging.apache.org/log4net/ ...

  8. Linux->Mysql安装调试

    环境准备 mysql下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-linux-glibc2.12-x86_64.tar ...

  9. Elasticsearch.yml

    cluster.name: elasticsearch配置es的集群名称,默认是elasticsearch,es会自动发现在同一网段下的es,如果在同一网段下有多个集群,就可以用这个属性来区分不同的集 ...

  10. UNIX和linux系统性能监控工具oswatcher

    可以在一台机器上运行oswatcher.把运行的结果拷贝到有vnc的机器上进行分析.java -jar oswbba.jar -i /mnt/hgfs/database/oswbb/archive . ...