hdu 5187 zhx's contest (快速幂+快速乘)
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 (快速幂+快速乘)的更多相关文章
- hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]
传送门 zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 5187 zhx's contest
题目分析如果n=1,答案是1,否则答案是2n−2. 证明:ai肯定是最小的或者最大的.考虑另外的数,如果它们的位置定了的话,那么整个序列是唯一的. 那么ai是最小或者最大分别有2n−1种情况,而整个序 ...
- HDU 5187 zhx's contest 快速幂,快速加
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5187 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
- HDU - 5187 zhx's contest(快速幂+快速乘法)
作为史上最强的刷子之一,zhx的老师让他给学弟(mei)们出n道题.zhx认为第i道题的难度就是i.他想要让这些题目排列起来很漂亮. zhx认为一个漂亮的序列{ai}下列两个条件均需满足. 1:a1. ...
- HDU 4549 矩阵快速幂+快速幂+欧拉函数
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- 取模性质,快速幂,快速乘,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 ...
- HDU - 5187 - zhx's contest (高速幂+高速乘)
zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 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 ...
- HDU 5607 graph 矩阵快速幂 + 快速幂
这道题得到了学长的助攻,其实就是一个马尔科夫链,算出一步转移矩阵进行矩阵快速幂就行了,无奈手残 这是我第一回写矩阵快速幂,写的各种毛病,等到调完了已经8点44了,交了一发,返回PE,(发现是少了换行) ...
随机推荐
- (二十四)JDBC应用的事务管理(转账事例)
目录 利用 Dbutils 进行事务操作(以转账为例) 转账实现方式(不优雅的方式) ThreadLocal 类 转账实现方式(优雅的方式) 利用 Dbutils 进行事务操作(以转账为例) 我们只在 ...
- C程序设计语言练习 第三章
3.3 else-if语句 折半查找,这里通过一个折半查找说明三路判定程序的用法.该函数用于判定已排序好的数组v中是否存在某个特定的值.数组v的元素必须以升序排列.如果v中包含x,则该函数返回x在v中 ...
- ASP.NET跨平台、分布式技术架构技术栈概览 (迄今为止最全的.NET技术栈)
今天有个学技术的小兄弟问我,现在这么多的技术我要学哪个?我说你根据岗位来学,学好了哪一门都可以在社会上立足,如今已经早已不是我们当年学习IT时候那么单纯了,给他讲了很多,发现现在的技术栈变得层次复杂且 ...
- Redis学习存档(2)——通过Java使用Redis:Jedis
一.创建项目,引入jedis jar包 可在百度搜索maven repository 进入后搜索jedis,复制依赖包到pom.xml文件中 <project xmlns="http: ...
- 【组成原理】BYTE ME!
题目描述 Parity is an important concept in data transmission. Because the process is not error proof, p ...
- 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务
来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...
- mybatis的BLOB存储与读取
http://blog.csdn.net/luyinchangdejiqing/article/details/45096689 简单介绍一下背景环境,web开发避免不了照片附件之类的东东,原先是存到 ...
- SQLSERVER 在PROCEDURE 中动态执行SQL语句【EXEC】并获取
1.直接上代码 CREATE PROCEDURE [dbo].[TEST] AS BEGIN DECLARE )='N8-4F', --構建SQL需要的條件 ),--構建後的SQL語句 @cnt in ...
- PLSQL PL/SQL Developer Oracle 使用技巧 常用设置 卡顿问题 病毒防范( 附带:配置文件)
相关工具版本: PL/SQL Developer: 9.0.4.1644 Oracle : Oracle Database 10g Enterprise Edition Release 10.2.0. ...
- 关于写SQL语句的技巧
一.SQL总结写法 SQL的写法无非就是几种,关联查询,子查询,分组函数,各种函数的使用 1.首先根据要做的需求,先分析一下,需要用到哪些查询,例如要用到关联查询,就先把用到的表列出来,比如a,b,c ...