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. ubuntu 忘记密码如何 修改密码

    ubuntu 忘记密码如何 修改密码 这个链接讲的很不错 https://blog.csdn.net/zd147896325/article/details/81664558 本来我只是玩一玩,但是我 ...

  2. Codeforces Round #590 (Div. 3)补题

    要想上2000分,先刷几百道2000+的题再说 ---某神 题目 E F 赛时是否尝试 × × tag math bitmask 难度 2000 2400 状态 ∅ √ 解 E 待定 F 传送门 第一 ...

  3. pthread_cancel 相关

    假设线程A对线程B发出了一个取消请求.通过如下函数: #include <pthread.h> int pthread_cancel(pthread_t thread); 参数: thre ...

  4. 批量Insert

    oracle INSERT ALL ,) ,) ,) FROM DUAL

  5. JAVA线程池例子

    用途及用法 网络请求通常有两种形式:第一种,请求不是很频繁,而且每次连接后会保持相当一段时间来读数据或者写数据,最后断开,如文件下载,网络流媒体等.另 一种形式是请求频繁,但是连接上以后读/写很少量的 ...

  6. 【SpringBoot】自动配置

    一.取值 1.1 @Value 1.2 ConfigurationProperties 二.导入配置文件 2.1 @PropertySource 三.配置文件的加载 3.1 默认的加载顺序 3.2 外 ...

  7. 一周死磕fastreport ----ASP.NET (三)

    做了一周,然而说着很快  首先拖一个WebReport 点击design report 设置模板 引入dll using引用 设置好就打印就可以了 未来几天, 然后都在设置样式 ....如何就一周过去 ...

  8. 实用:Java基础流计算

    java的流不常用,每次学习完都懂,过了一段时间就全忘了... 记录下一点实用的东西... 需求: 截取文件的前250kb内容 public static void main(String[] arg ...

  9. 学习kafka笔记

    一:概念 Kafka是一个分布式的消息队列,相当于我们生活中的快递柜,快递点,快递员将快递放到快递柜中,收件人去取,把快递(消息)的入柜(入队)和处理进行解耦,使得双方以自己合适的时间和频率处理快递, ...

  10. BZOJ5206 [Jsoi2017]原力[根号分治]

    这是一个三元环计数的裸题,只是多了一个颜色的区分和权值的计算罢了. 有一种根号分治的做法(by gxz) 这种复杂度的证明特别显然,思路非常简单,不过带一个log,可以用unordered_map或者 ...