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. 为tomcat启用nio机制

    tomcat的运行模式有3种.修改他们的运行模式.3种模式的运行是否成功,可以看他的启动控制台,或者启动日志.或者登录他们的默认页面http://localhost:8080/查看其中的服务器状态. ...

  2. js中JSON对象和字符串对象相互转化

    JSON.stringify(value [, replacer] [, space]) //作用,将json数据转化为字符串value:是必须要的字段.就是你输入的对象,比如数组啊,类啊等等. re ...

  3. Spring 3.x企业应用开发实战(14)----事务

    Spring虽然提供了灵活方便的事务管理功能,但这些功能都是基于底层数据库本身的事务处理机制工作的.要深入了解Spring的事务管理和配置,有必要先对数据库事务的基础知识进行学习. 何为数据库事务 “ ...

  4. linux多线程驱动中调用udelay()对整个系统造成的影响(by liukun321咕唧咕唧)

    以前没考虑过这个问题,而且之前可能运气比较好,虽然用了udelay但也没出什么奇怪的问题,今天在 CSDN上看到了一篇关于此问题帖子,觉得很受用,再此做简要的记录和分析: 驱动开的是内核线程 跟普通进 ...

  5. 【POJ】3076 Sudoku

    DLX第一题,模板留念. /* 3076 */ #include <iostream> #include <string> #include <map> #incl ...

  6. Android开发之应用程序窗体显示状态操作(requestWindowFeature()的应用)

    转自:http://www.cnblogs.com/salam/archive/2010/11/30/1892143.html 我们在开发程序是经常会需要软件全屏显示.自定义标题(使用按钮等控件)和其 ...

  7. rebuild过程

    /** The class defining a handle to an Innodb table */ class ha_innobase: public handler { row_prebui ...

  8. memcached SASL验证状态安全绕过漏洞

    漏洞版本: memcached 1.x 漏洞描述: Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. Memcached在处理链接的SASL验证状态时存在错 ...

  9. 将现有Ubuntu系统做成LiveCD

    制作LiveCD包的工具有不少,其中比较出名的就是UCK和Reconstructor.但是这两个工具都不能把当前的操作系统制成LiveCD,备份当前操作系统,并制成LiveCD的工具也有,比如国人编写 ...

  10. sharepoint2010网站根据权限隐藏ribbon

    转:http://www.it165.net/design/html/201302/1734.html 项目要求让普通用户看不到"网站操作",为了解决该问题,我找了好几篇博客,但都 ...