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. 整体二分(模板二)动态区间第K大

    这才是更一般的二分写法--HDU5412 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio>// ...

  2. Go语言学习之main包的讲解

    ### Go语言学习之main包的讲解 1.Go中main函数不支持任何返回值 2.可以通过os.Exit(0)来返回状态 func main(){ fmt.Println("hellow ...

  3. zookeeper集群搭建与升级

    zookeeper 1.zookeeper功能 1-1.配置管理 集中管理配置文件实现服务治理 1-2.命名服务 如为了通过网络访问一个系统,我们得知道对方的IP地址,但是IP地址对人非常不友好,这个 ...

  4. 不吹不黑,赞一下应用运维管理的cassacdra

    不吹不黑的为菊厂的应用运维管理AOM点个赞.Why? 某菊厂应用运维管理工具AOM每天处理着亿级条数据,这么多数据是怎么存储的呢? 说到数据存储就会想到关系型数据库,比如mysql,oracle,sy ...

  5. 完全卸载RabbitMQ和Erlang

    要从计算机中完全卸载RabbitMQ和Erlang,请执行以下操作:(1)打开Windows控制面板,双击“程序和功能”. (2)在当前安装的程序列表中,右键单击RabbitMQ Server,然后单 ...

  6. Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/THCTensorMath.cu:26

    Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/TH ...

  7. DSN 建立达梦7(DM)连接

    (DSN)Data Source Name 数据源名称 “ODBC数据源管理器”提供了三种DSN,分别为用户DSN.系统DSN和文件DSN.其中:      用户DSN会把相应的配置信息保存在Wind ...

  8. docker 第六篇 dockerfile

    复习下镜像生成途径 Dockerfile 基于容器制作 什么是dockerfile: 用来构建镜像的源码,在配置文件中调用命令,这些命令是用来生成docker镜像的. dockerfile的语法格式: ...

  9. 测试clang-format的格式化效果

    我自己写的业余框架已告一段落,主体功能已完成,剩下的就是优化.第一个要优化的,就是代码格式.我一直是用编辑器写代码的,从之前的UltraEdit到notepad++到sublime text,再到现在 ...

  10. 获取impala下所有的数据库建表语句

    方法一: 现在的导出还是有缺陷的,导出的文件中还是存在其他不必要的信息 #!/bin/bash ##获取数据库 databases=$(hive -e "show databases; ex ...