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. HDU 1166 敌兵布阵(线段树 单点更新)

     点我看题目  题意 :HDU的中文题也不常见....这道题我就不详述了..... 思路 :这个题用线段树用树状数组都可以,用线段树的时候要注意输入那个地方,输入一个字符串的时候不要紧接着输入两个数字 ...

  2. js设置radio选中

    在页面数据绑定时,经常会遇到给radio设置选中,以下是我写的js方法,经测试可以使用.欢迎拍砖 <html> <head> <script type="tex ...

  3. ANDROID_MARS学习笔记_S01原始版_017_绑定SERVICE

    一.流程 1.编写service,重写onBind(Intent intent),返回自定义的Binder 2.自写义Binder,提供一个可访问的方法,以传递数据 3.点击界面按钮会开启servic ...

  4. Android笔记5-与USB HID 设备通信(一)

    1.了解 支持USB 主机(host)或者从机(accessary )模式最终是取决于设备的硬件,而与平台版本无关.我们可以通过usesfeature这个方法来查询自己的设备是否支持USB主从.   ...

  5. 关于fastclick.js

    Fastclick fastclick.js解决了什么问题? 自己接触WebApp开发的前期, 总感觉WebApp上的按键操作不如NativeApp的灵敏, 好像有那么一小点延迟. 后来才知道, 这是 ...

  6. JAVA信号量使用示例程序

    JAVA里也有强大的信号量工具Semaphore,使用信号量可以很方便的实现线程同步功能,以解决类似于 生产者——消费者 之类的典型问题. 下面是我的测试程序,大部分看注释就可以明白用法: impor ...

  7. EF双向一对一中的坑

    EF版本 6.0 在项目中双向一对一关系是普遍存在的,如果不仔细检查,并不容易发现这个坑 下面新建两个类(假设这两个类是一对一的关系)对应实体都设置为可延迟加载 映射关系为: 再建一个数据访问类: 运 ...

  8. ☀【移动】UC极速模式

    UC浏览器的部分版本默认是“极速”模式,有何办法能控制UC自动改变其浏览模式? √http://www.zhihu.com/question/20582949 关于UC极速模式下访问网站错乱 √htt ...

  9. .net文件压缩和解压及中文文件夹名称乱码问题

    /**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...

  10. Windows下的.NET+ Memcached安装

    转载请标明出处: http://www.yaosansi.com/ 原文:http://www.yaosansi.com/post/1396.html Memcached官方:http://danga ...