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

Total Submission(s): 2388    Accepted Submission(s): 755

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

 
Source
 
Recommend
hujie
 

贪心?

答案是2^n-2

只是用来练快速乘的

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define LL long long
using namespace std;
const int mxn=;
LL n,p;
LL ksmul(LL a,LL b){
a%=p;b%=p;LL res=;
while(b){
if(b&){res+=a;if(res>p)res-=p;}
a=(a<<)%p;
b>>=;
}
return res;
}
LL ksm(LL x,LL k){
LL res=;
while(k){
if(k&)res=ksmul(res,x);
x=ksmul(x,x);
k>>=;
}
return res;
}
int main(){
while(scanf("%lld%lld",&n,&p)!=EOF){
if(n==){
if(p==)printf("0\n");
else printf("1\n");
continue;
}
LL ans=ksm(,n);ans-=;
if(ans<)ans=((ans%p)+p)%p;
printf("%lld\n",ans);
}
return ;
}

HDU5187 zhx's contest的更多相关文章

  1. zhx and contest (枚举  + dfs)

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

  2. zhx's contest (矩阵快速幂 + 数学推论)

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

  3. HDOJ 5188 zhx and contest 贪婪+01背包

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

  4. hdu 5188 zhx and contest [ 排序 + 背包 ]

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

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

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

  6. hdu 5187 zhx's contest (快速幂+快速乘)

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

  7. 【HDU5187】zhx's contest

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

  8. HDU5187 zhx&#39;s contest(计数问题)

    主题链接: http://acm.hdu.edu.cn/showproblem.php?pid=5187 题意: 从1~n,有多少种排列 使得 a1~ai 满足单调递增或者单调递减. ai~an 满足 ...

  9. HDU 5188 zhx and contest(带限制条件的 01背包)

    Problem Description As one of the most powerful brushes in the world, zhx usually takes part in all ...

随机推荐

  1. python sys模块和序列化模块

    sys模块是与python解释器交互的一个接口: sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0),错误退出sys.exit( ...

  2. 1911: [Apio2010]特别行动队(斜率优化)

    链接 思路 斜率优化dp. 代码 #include<cstdio> #include<algorithm> #include<cstring> #include&l ...

  3. P1498 南蛮图腾

    P1498 南蛮图腾 题目描述 自从到了南蛮之地,孔明不仅把孟获收拾的服服帖帖,而且还发现了不少少数民族的智慧,他发现少数民族的图腾往往有着一种分形的效果(看Hint),在得到了酋长的传授后,孔明掌握 ...

  4. 《Cracking the Coding Interview》——第17章:普通题——题目7

    2014-04-28 23:28 题目:给定一个数字,用英语把它读出来. 解法:ZOJ上有相反的题目.如果我要用中文读书来呢? 代码: // 17.7 Read an integer in Engli ...

  5. QBASIC教程

    Qbasic 程序设计入门 BASIC(Beginner’s All-purpose Symbolic Instruction Code 的缩写,意为初学者通用符号指令代码)语言是在1964年由美国的 ...

  6. 【Text Justification】cpp

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  7. Python编码、流程控制、格式化输出

    Python编码 初始编码: 电脑的传输,还有储存,实际上都是010101010 ASCII码: (American Standard Code for Information Interchange ...

  8. java初学2

    1.数组操作类Arrays与System public static void arraycopy(Object src, int srcPos, Object dest,int destPos,in ...

  9. OpenStack-Ironic裸金属简介

    一,Ironic简述 简而言之,OpenStack Ironic就是一个进行裸机部署安装的项目.    所谓裸机,就是指没有配置操作系统的计算机.从裸机到应用还需要进行以下操作:  (1)硬盘RAID ...

  10. 动态删边SPFA: [HNOI2014]道路堵塞

    [HNOI2014]道路堵塞 题目描述 $A$ 国有 $N$座城市,依次标为$1$到$N$.同时,在这$N$座城市间有$M$条单向道路,每条道路的长度是一个正整数.现在,$A$国交通部指定了一条从城市 ...