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. Jmeter 执行java脚本结束时提示:he JVM should have exitted but did not

    使用jmeter执行java协议测试结束时会提示:he JVM should have exitted but did not ,jmeter2.11以后的可以 通过设置:      jmeteren ...

  2. linux下监控jvm 使用的方法

    之前一直用jconsole监控jvm,图形界面简单易用,最近因为需要在纯linux下进行操作,所以总结了一下 linux下监控jvm的例子,这次主要用到了jstat工具, 各个参数意义: jstat ...

  3. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  4. vi/vim 基本使用

    摘要: 在minicom终端里修改开发板中的文件时,必须要用到vi,因为开发板中并不像开发主机那样,有gedit和kscope这样的编辑器:还有,即便是在开发主机上,也会经常用到vi,因为vi使用起来 ...

  5. Form表单中的三种查询方法

    1.使用:parameter.G_query_find参数: IF (NAME_IN('PO_HEADERS.PO_HEADER_ID') IS NOT NULL) THEN    :paramete ...

  6. Android开发UI之动画侦听

    动画侦听使用了AnimationListener接口,需要实现三个方法onAnimationStart().onAnimationRepeat().onAnimationEnd() 代码: 实现But ...

  7. bzoj1492

    好题+神题,首先肯定是dp,我们设f[i]为到第i天能获得的最多的B卷(设获得的钱数亦可)由题目hint可知,要么全买要么全卖,我们有f[i]=max(maxmoney,f[j]*b[i]+f[j]* ...

  8. 手机端的mousedown

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Spring概述--1

    1.1.1  Spring是什么 Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发.应用程序是由 ...

  10. Enum 枚举

    一: 1. foreach (int val in Enum.GetValues(typeof(AppEnum.HarbourStatus))) { ddlStatus.Items.Add(new L ...