题干:

2018百度之星复赛晋级名单出炉(增加20%晋级名额)~

zhx's contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3779    Accepted Submission(s): 1226

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

BestCoder Round #33

解题报告:

首先这题找规律,题目推出来通式是:2^n - 2。证明如下:

推的过程就是一共有四种情况: 升升,升降,降升,降降,其中升升和降降最简单,一共有两种,复杂的就是升降和降升这两种情况,首先来看降生,那么ai一定是最小值,因为两边都算ai了,所有当在第一个空的时候,前面一共有Cn-11, 后面就自动的确定了,在第二位的时候,有Cn-12, 同理到最后Cn-1n-2,所以加起来就是2n-1-2,这是降升,同理升降也是这么多,所以最后结果就是(2n-1-2) * 2 + 2 = 2n-2;

注意的是这题的乘法不能直接乘法,因为longlong * longlong可能会爆掉,所以这里用快速乘法,把longlong * longlong转化成longlong + longlong 去做。

AC代码:

#include<iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long LL;LL qmul(LL a, LL k, LL mod) { //快速乘法LL ans = 0;//加法的幺元while (k) {if (k & 1) ans = (ans + a)%mod;a = (a + a) % mod;//和快速幂一样,只不过这里是加k >>= 1;}return ans;
}
LL qpow(LL a, LL k, LL mod) { //快速幂LL ans = 1;while (k) {if (k & 1) ans = qmul(ans, a, mod);//不能直接乘a = qmul(a, a, mod);k >>= 1;}return ans;
}int main() {LL n, p;while (~scanf("%I64d %I64d", &n, &p)) {if (n == 1) { //特判一下printf("%I64d\n", 1 % p);continue;}printf("%I64d\n", (qpow(2, n, p) - 2 + p) % p);//这一步注意,不要为负数}return 0;
}

【HDU - 5187】zhx's contest (快速幂+ 快速乘,模板)相关推荐

  1. HDU - 5187 zhx's contest(快速幂+快速加+组合数学)

    题目链接:点击查看 题目大意:给出一个n,现在有1~n的全排列,规定如果可以将其中一种排列分为两部分: a1-ai单调递增/单调递减 ai-an单调递增/单调递减 则称这种排列为美丽的排列,现在问一共 ...

  2. 【牛客每日一题】4.16 逆序对 ( 数学 , 排列组合 ,快速幂 , 快速乘 )

    [每日一题]逆序对 链接:https://ac.nowcoder.com/acm/problem/14731 来源:牛客网 题目描述 求所有长度为n的01串中满足如下条件的二元组个数: 设第i位和第j ...

  3. 快速幂----快速求解底数的n次幂

    目录 一.快速幂 1.问题的引入 2.快速幂的介绍 3.核心思想 4.代码实现 二.Pow(x, n) 1.题目描述 2.问题分析 3.代码实现 三.猴子碰撞的方法数 1.题目描述 2.问题分析 3. ...

  4. HDU4549(矩阵快速幂+快速幂)

    f(n)=a^f(n-1) + b^f(n-2):计算矩阵部分用矩阵快速幂:计算a的幂次和b的幂次用快速幂. #include<iostream> #include<algorith ...

  5. POJ 3070 Fibonacci(矩阵快速幂入门、模板)

    ? 题目链接:http://poj.org/problem?id=3070 ?   这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...

  6. 矩阵快速幂(附模板)

    求解矩阵 A 的 N 次方,我们可以类比整数快速幂,写一个矩阵的结构体,用一个matmul函数来定义矩阵的乘法,具体实现过程与整数快速幂类似(整数快速幂) 模板 struct mat {ll m[ma ...

  7. 整数快速幂(原理+模板)

    原理 求xNx^NxN时,根据n的二进制位数,分为xn1+xn2+xn3+xn4--x^{n1}+x^{n2}+x^{n3}+x^{n4}--xn1+xn2+xn3+xn4-- 例如:在求x18x^{ ...

  8. 快速幂 快速乘原理讲解(模板)

    目录 1 问题描述 2 原因分析 3 解决方法 4 快速幂讲解 5 快速乘讲解 6 完整代码 7 References 1 问题描述 我们发现,在 int 型下使用 pow 函数求 ,结果为 124 ...

  9. 快速幂和矩阵快速幂详解+模板

    1.快速幂 一般的,我们都知道求只需要连续乘3次2就能得到,那么等于多少呢?其实这个一很简单,不就是13个2相乘吗,连续乘13次2就行了.那么,呢? 是不是要连续乘100次.1000次,我们将这类问题 ...

最新文章

  1. 基于html5海贼王单页视差滚动特效
  2. BZOJ 2055: 80人环游世界 [上下界费用流]
  3. == 捕获对象时的模式切换 ==
  4. echo怎么把日志清空_电脑越来越卡到底怎么办?一分钟教你释放C盘空间,瞬间提速5倍...
  5. Oracle入门(十四.16)之捕获用户定义的异常
  6. STL 容器和迭代器连载6_顺序容器的操作3
  7. 判断上三角矩阵_线性代数15——矩阵空间\对角矩阵\和秩1矩阵
  8. 2018可能大火的物联网应用
  9. Atitit.php  nginx页面空白 并返回500的解决
  10. 效果良好!构造一个输入速度的神经网络,以DQN方式实现小游戏的自动控制
  11. Android n multi-window多窗口支持
  12. 2013年全国天线年会参展商名录及观展指南
  13. 【亲测有效】硬盘/分区修复教程
  14. elasticsearch 如何清理过期的数据
  15. 我努力了18年,不是为了和你一起喝咖啡姐妹篇
  16. 计算某年新年是星期几的公式【不过这天怎么也该是假期吧?】
  17. 安卓和IOS时间解析时间格式不一致的问题
  18. 仇人与恩人- 挺有意义的
  19. 泽塔云荣膺“中国高科技高成长50强”,成唯一上榜超融合企业
  20. BLDC 6步换相 simulink

热门文章

  1. [Leedcode][JAVA][第470题][Ran7()实现Rand10()]
  2. elementui图片上传php,vue+element-ui+富文本————图片上传
  3. 同名字的数值求和插入行_EXCEL条件求和的6种技术,你会的超过3种吗?
  4. js层级选择框样式_【JavaWeb】85:jQuery的各种选择器
  5. html页面 wordpress,WordPress纯代码实现前端页面HTML完美压缩
  6. android 使用Binder通信
  7. Tecplot如何导入多个DAT文件后激活solution time按钮
  8. 解决VC不包含stdint.h头文件问题
  9. ad电阻原理图_负载电阻的原理及应用
  10. python i开发工具_Python轻量级开发工具Genay使用