传送门

zhx's contest

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

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
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5189 5184 5181 5180 5177 
 
 
哎,醉死了,2个坑点,相乘会爆long long 要用快速幂,n=1时要特判p=1;
其他转官方题解了:http://bestcoder.hdu.edu.cn/
  1. 1002 zhx and contest
  2. 如果n=1 
  3.  
  4. ,答案是1 
  5.  
  6. ,否则答案是2 n 2 
  7.  

  8. 证明:a i  
  9.  
  10. 肯定是最小的或者最大的。考虑另外的数,如果它们的位置定了的话,那么整个序列是唯一的。
  11. 那么a i  
  12.  
  13. 是最小或者最大分别有2 n1  
  14.  
  15. 种情况,而整个序列单调增或者单调减的情况被算了2次,所以要减2
  16. 要注意的一点是因为p>2 31  
  17.  
  18. ,所以要用快速乘法。用法与快速幂相同。如果直接乘会超过long long范围,从而wa掉。
13127194 2015-03-14 22:34:13 Accepted 5187 109MS 1664K 1179 B G++ czy
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <stack>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <queue>
  7. #include <map>
  8. #include <string>
  9.  
  10. #define ll long long
  11. int const N = ;
  12. int const M = ;
  13. int const inf = ;
  14. ll const mod = ;
  15.  
  16. using namespace std;
  17.  
  18. ll n,p;
  19. ll ans;
  20.  
  21. ll quickmul(ll x,ll m)
  22. {
  23. ll re=;
  24. while(m){
  25. if(m&){
  26. re=(re+x)%p;
  27. }
  28. m/=;
  29. x=(x+x)%p;
  30. }
  31. return re;
  32. }
  33.  
  34. ll quickpow(ll x,ll m)
  35. {
  36. ll re=;
  37. while(m)
  38. {
  39. if(m&){
  40. re=quickmul(re,x);
  41. }
  42. m/=;
  43. x=quickmul(x,x);
  44. }
  45. return re;
  46. }
  47.  
  48. void ini()
  49. {
  50.  
  51. }
  52.  
  53. void solve()
  54. {
  55. if(n==){
  56. if(p!=)
  57. ans=;
  58. else
  59. ans=;
  60. return;
  61. }
  62. else{
  63. ans=quickpow(2LL,n)-2LL;
  64. ans=(ans+p)%p;
  65. }
  66. }
  67.  
  68. void out()
  69. {
  70. printf("%I64d\n",ans);
  71. }
  72.  
  73. int main()
  74. {
  75. //freopen("data.in","r",stdin);
  76. //scanf("%d",&T);
  77. //for(cnt=1;cnt<=T;cnt++)
  78. while(scanf("%I64d%I64d",&n,&p)!=EOF)
  79. {
  80. ini();
  81. solve();
  82. out();
  83. }
  84. }

再贴一发Java的程序:

13131089 2015-03-15 10:47:30 Accepted 5187 421MS 9640K 858 B Java czy
  1. //import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4.  
  5. public class Main {
  6. static BigInteger quickpow (BigInteger x, long m, BigInteger p )
  7. {
  8. BigInteger re = BigInteger.ONE;
  9. while(m >= 1)
  10. {
  11. if(m % 2 == 1){
  12. re = re.multiply(x).mod(p);
  13. }
  14. x = x.multiply(x).mod(p);
  15. m = m / 2;
  16. }
  17. return re;
  18. }
  19.  
  20. public static void main(String[] args){
  21. Scanner in = new Scanner(System.in);
  22. long n;
  23. BigInteger p;
  24. BigInteger ans;
  25. while(in.hasNext())
  26. {
  27. n = in.nextLong();
  28. p = in.nextBigInteger();
  29. if(n == 1){
  30. if(p.equals(BigInteger.ONE)){
  31. ans = BigInteger.ZERO;
  32. }
  33. else{
  34. ans = BigInteger.ONE;
  35. }
  36. }
  37. else{
  38. ans = quickpow(BigInteger.valueOf(2),n,p).subtract(BigInteger.valueOf(2));
  39. ans = ans.add(p).mod(p);
  40. }
  41. System.out.println(ans);
  42. }
  43. }
  44. }

hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]的更多相关文章

  1. hdu 5187 zhx's contest

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

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

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

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

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

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

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

  5. HDU 3032 multi-sg 打表找规律

    普通NIM规则加上一条可以分解为两堆,标准的Multi-SG游戏 一般Multi-SG就是根据拓扑图计算SG函数,这题打表后还能发现规律 sg(1)=1 sg(2)=2 sg(3)=mex{0,1,2 ...

  6. HDU 4549 矩阵快速幂+快速幂+欧拉函数

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  7. 取模性质,快速幂,快速乘,gcd和最小公倍数

    一.取模运算 取模(取余)运算法则: 1. (a+b)%p=(a%p+b%p)%p; 2.(a-b)%p=(a%p-b%p)%p; 3.(a*b)%p=(a%p * b%p)%p; 4.(a^b)%p ...

  8. 【bzoj4870】[Shoi2017]组合数问题 dp+快速幂/矩阵乘法

    题目描述 输入 第一行有四个整数 n, p, k, r,所有整数含义见问题描述. 1 ≤ n ≤ 10^9, 0 ≤ r < k ≤ 50, 2 ≤ p ≤ 2^30 − 1 输出 一行一个整数 ...

  9. hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...

随机推荐

  1. 写给技术lead的招聘指南

    工作这么久,面试过的工程师不下两三百人.大部份招到的人都比靠谱当然也有失败的例子.把亲身经历总结如下: 1. 什么人一定不能招: 理解能力差: 对你提出的问题,答不对题,重复提问.面试官可以在面试当中 ...

  2. Django请求,响应,ajax以及CSRF问题

    二.request对象常用属性: Attribute Description path 请求页面的全路径,不包括域名端口参数.例如: /users/index method 一个全大写的字符串,表示请 ...

  3. 1-2 编程基础 GDB程序调试

    简介 GDB是GNU发布的一款功能强大的程序调试工具.GDB主要完成下面三个方面的功能: 1.启动被调试程序 2.让被调试的程序在指定的位置停住. 3.当程序被停住时,可以检查程序状态(如变量值). ...

  4. // mounted: {}, 原来是 空方法 导致了 vue 的警告 !| [Vue warn]: Error in mounted hook: "TypeError: handlers[i].call is not a function"

    // mounted: {}, 原来是 空方法 导致了 vue 的警告 !| vue.runtime.esm.js?2b0e:587 [Vue warn]: Error in mounted hook ...

  5. numpy次方计算

    >>> 2**np.arange(3, 6) array([ 8, 16, 32])

  6. luogu P3916 图的遍历

    P3916 图的遍历 题目描述 给出 N 个点, M 条边的有向图,对于每个点 v ,求 A(v) 表示从点 v 出发,能到达的编号最大的点. 输入输出格式 输入格式: 第1 行,2 个整数 N,MN ...

  7. OVOO

    题目描述: $zhx$有一个棵$n$个点的树,每条边有个权值. 定义一个连通块为一个点集与使这些点连通的所有边(这些点必须连通). 定义一个连通块的权值为这个连通块的边权和(如果一个连通块只包含一个点 ...

  8. 如何用纯 CSS 创作 404 文字变形为 NON 文字的交互特效

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ZoxjXm 可交互视频教 ...

  9. systemverilog:task

    1.task declaration 个人喜欢ANSI C格式的声明: task mytask(output int x,input logic y); ...... endtask 注意端口列表的方 ...

  10. Android 图片设置圆角 方法之二

    Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片.接下来我们再介绍一种方法. 首先, 自定义ImageView: android:id="@+id/iv" ...