zhx's contest

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

Total Submission(s): 3835    Accepted Submission(s): 1255

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


# include <iostream>
# include <cstring>
# include <cstdio>
using namespace std;
typedef long long LL;
LL p,mod;LL n;
inline LL quick_mul(LL x,LL y,LL MOD){
x=x%MOD,y=y%MOD;
return ((x*y-(LL)(((long double)x*y+0.5)/MOD)*MOD)%MOD+MOD)%MOD;
}
LL qmod(LL a, LL b)
{
LL ans = 1, pow = a%mod;
while(b)
{
if(b&1) ans = (quick_mul(ans,pow,mod))%mod;
pow = (quick_mul(pow,pow,mod))%mod;
b >>= 1;
}
return ans;
}
int main()
{
while(~scanf("%lld%lld",&n,&p))
{
mod=p;
LL ans=qmod(2,n);
ans=(ans-2+mod)%mod;
printf("%lld\n",ans);
}
return 0;
}

hdu 5187 zhx's contest (快速幂+快速乘)的更多相关文章

  1. hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]

    传送门 zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. hdu 5187 zhx's contest

    题目分析如果n=1,答案是1,否则答案是2n−2. 证明:ai肯定是最小的或者最大的.考虑另外的数,如果它们的位置定了的话,那么整个序列是唯一的. 那么ai是最小或者最大分别有2n−1种情况,而整个序 ...

  3. HDU 5187 zhx's contest 快速幂,快速加

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5187 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

  4. HDU - 5187 zhx's contest(快速幂+快速乘法)

    作为史上最强的刷子之一,zhx的老师让他给学弟(mei)们出n道题.zhx认为第i道题的难度就是i.他想要让这些题目排列起来很漂亮. zhx认为一个漂亮的序列{ai}下列两个条件均需满足. 1:a1. ...

  5. HDU 4549 矩阵快速幂+快速幂+欧拉函数

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  6. 取模性质,快速幂,快速乘,gcd和最小公倍数

    一.取模运算 取模(取余)运算法则: 1. (a+b)%p=(a%p+b%p)%p; 2.(a-b)%p=(a%p-b%p)%p; 3.(a*b)%p=(a%p * b%p)%p; 4.(a^b)%p ...

  7. HDU - 5187 - zhx&#39;s contest (高速幂+高速乘)

    zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  8. SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1

    5152. Brute-force Algorithm EXTREME Problem code: BFALG Please click here to download a PDF version ...

  9. HDU 5607 graph 矩阵快速幂 + 快速幂

    这道题得到了学长的助攻,其实就是一个马尔科夫链,算出一步转移矩阵进行矩阵快速幂就行了,无奈手残 这是我第一回写矩阵快速幂,写的各种毛病,等到调完了已经8点44了,交了一发,返回PE,(发现是少了换行) ...

随机推荐

  1. Linux(CentOS7)系统中部署Django web框架

    1. 概述 部署django和vue架在逻辑上可以分为web层与数据库层:web前端通过实现了WSGI协议的模块对python代码进行解析,而python代码中则通过特定于数据库的操作接口对数据库进行 ...

  2. tesseract ocr .Net demo

    环境vs 2019 .Net 4.8 新建一个wpf工程,拖放上一个button一个textbox nuget下载tesseract,版本信息如图所示 MainWindow.xaml.cs文件代码如下 ...

  3. Codeforces 1247E. Rock Is Push

    传送门 显然考虑 $dp$ ,设 $fx[i][j]$ 表示从 $(i,j)$ 出发往下走一格,最终到达 $(n,m)$ 的方案数,$fy[i][j]$ 表示从 $(i,j)$ 出发往右走一格,最终到 ...

  4. MySQL SELECT语法(四)UNION语法详解

    源自MySQL 5.7 官方手册:13.2.9.3 UNION Syntax 一.UNION语法 UNION用于将多个SELECT语句的结果合并到一个结果集中. SELECT ... UNION [A ...

  5. MySQL 聚合函数与count()函数

    一.MySQL中的聚合函数 MySQL 5.7文档的章节:12.20.1 Aggregate (GROUP BY) Function “聚合/组合”函数(group (aggregate) funct ...

  6. 浅谈后缀数组SA

    这篇博客不打算讲多么详细,网上关于后缀数组的blog比我讲的好多了,这一篇博客我是为自己加深印象写的. 给你们分享了那么多,容我自私一回吧~ 参考资料:这位dalao的blog 一.关于求Suffix ...

  7. 服务返回的json数据过大,nginx无法返回给client

    现象:请求同样的服务器,N多个接口中,只有一个接口未返回:从日志看,请求已到后端服务,并返回 解决方案:配置nginx缓冲大小 ###Nginx的缓冲区的大小 proxy_buffer_size 5m ...

  8. win10下搭建vue开发环境

    特别说明:下面任何命令都是在windows的命令行工具下进行输入,打开命令行工具的快捷方式如下图:     详细的安装步骤如下: 一.安装node.js 说明:安装node.js的windows版本后 ...

  9. IoC框架介绍

    转载自:http://blog.csdn.net/wanghao72214/article/details/3969594 1 IoC理论的背景    我们都知道,在采用面向对象方法设计的软件系统中, ...

  10. JDK + Tomcat 安装 + 制作自定义镜像【第 2 篇 Tomcat】

    [第 1 篇 JDK]:https://www.cnblogs.com/del88/p/11842387.html[第 2 篇 Tomcat]:https://www.cnblogs.com/del8 ...