zhx's contest

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

Total Submission(s): 448    Accepted Submission(s): 147

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
 

思路:由题意能够求出答案为(2^n-2)%p



可是n。p都是LL型的,高速幂的时候会爆LL,所以这里要用到高速乘法,高速乘法事实上和高速幂差点儿相同。就是把乘号改为加号



注意:当n为1时。要输出1,而当p为1时要输出0。



AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std; LL n, p; LL multi(LL a, LL b) { //高速乘法。事实上和高速幂差点儿相同
LL ret = 0;
while(b) {
if(b & 1) ret = (ret + a) % p;
a = (a + a) % p;
b >>= 1;
}
return ret;
} LL powmod(LL a, LL b) { //高速幂
LL ret = 1;
while(b) {
if(b & 1) ret = multi(ret, a) % p;
a = multi(a, a) % p;
b >>= 1;
}
return ret;
} int main() {
while(cin >> n >> p) {
if(p == 1) {
cout << 0 << endl;
} else if(n == 1) {
cout << 1 << endl;
} else {
LL ans = powmod(2, n) - 2;
if(ans < 0) ans += p;
cout << ans << endl;
}
}
return 0;
}

HDU - 5187 - zhx&#39;s contest (高速幂+高速乘)的更多相关文章

  1. HDU 5187 zhx&#39;s contest(防爆__int64 )

    Problem Description As one of the most powerful brushes, zhx is required to give his juniors n probl ...

  2. hdu 5187 高速幂高速乘法

    http://acm.hdu.edu.cn/showproblem.php?pid=5187 Problem Description As one of the most powerful brush ...

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

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

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

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

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

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

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

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

  7. hdu 5187 zhx's contest

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

  8. HDU - 5186 - zhx&#39;s submissions (精密塔尔苏斯)

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

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

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

随机推荐

  1. HDU1395+快速幂

    #include<stdio.h> int fast_pow( int a,int b,int mod ){ ; ){ == ){ res = res*a%mod; } a = a*a%m ...

  2. 你所不知道的string.xml

    String 能被应用程序或者其他资源文件(比如layout XML)引用的单个字符串. 注意:字符串是简单类型资源,是用名称(name)(而非XML文件名)来直接引用的.因此,在一个XML文件里,可 ...

  3. 【Quick 3.3】资源脚本加密及热更新(二)资源加密

    [Quick 3.3]资源脚本加密及热更新(二)资源加密 注:本文基于Quick-cocos2dx-3.3版本编写 一.介绍 在前一篇文章中介绍了代码加密,加密方式是XXTEA.对于资源文件来说,同样 ...

  4. Android进阶篇-内存管理

    很多时候我们需要考虑Android平台上的内存管理问题,Dalvik VM给每个进程都分配了一定量的可用堆内存,当我们处理一些耗费资源的操作时可能会产生OOM错误(OutOfMemoryError)这 ...

  5. S5P4418开发板使用要点

    一.调试工具 1)USB 转串口驱动:用于驱动板子的串口板,电脑与板子进行调试串口通信2)量产软件 USB驱动安装:用于 NXUsbBurner_Ver1.0.6 软件镜像烧写(量产工具)3)调试工具 ...

  6. 【HDOJ】1504 Disk Tree

    文件可以重名.先按字典序将路径排序,再过滤掉公共前缀.其中的问题是'\'的ASCII比[A-Z0-9]大,将它替换为空格.否则字典序有问题. /* 1504 */ #include <iostr ...

  7. Windows系统性能提升方法

    看前提醒:在确认没有病毒和流氓软件的前提下,建议优化电脑:以下操作已经在Win7上试验,Win7以上的园友自己试验,自己感受,对电脑无害,但操作时请务必小心 设置虚拟内存 虚拟内存最小值物理内存1.5 ...

  8. bzoj3144

    最小割解决最优值最突出的特点就是要将对象划分到两个集合中这题很明显,裸的最小割先把点连成一根根柱子,就是p(x,y,k)-->p(x,y,k+1)流量是P(x,y,k+1)的不和谐值然后s与p( ...

  9. VisualC#数据库高级教程文档分享

    这一节我们演示下怎样使用VS2010创建与发布MVC3建立的网站.使用VS2010创建MVC3.0网站,需要下载MVC3.0的安装包,这个大家可以去网络上下载.     1.项目创建         ...

  10. C# asp.net 操作Word的前提配置和简单的方法

    操作的前提: 1.要保证机器本身要安装OFFICE. 有时安装了Office,但是不能找到Microsoft Word 11.0(或者更高的版本) Object Library.那可能是因为在安装of ...