zhx's contest

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

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

  1. #include<stdio.h>
  2. typedef long long ll ;
  3. ll n , mod ;
  4.  
  5. ll mut (ll a , ll b)
  6. {
  7. ll ans = ;
  8. while (b) {
  9. if (b & ) {
  10. ans = (ans + a) % mod ;
  11. }
  12. a = (a + a ) % mod ;
  13. b >>= ;
  14. }
  15. return ans % mod ;
  16. }
  17.  
  18. void mgml (ll a , ll b)
  19. {
  20. ll ans = ;
  21. while (b) {
  22. if (b & ) {
  23. ans = mut (ans , a ) ;
  24. }
  25. b >>= ;
  26. a = mut ( a , a ) ;
  27. }
  28. ans -= ;
  29. ans %= mod ;
  30. ans += mod ;
  31. ans %= mod ;
  32. printf ("%I64d\n" , ans ) ;
  33. }
  34.  
  35. int main ()
  36. {
  37. //freopen ("a.txt" , "r" , stdin ) ;
  38. while (~ scanf ("%I64d%I64d" , &n , &mod)) {
  39. if (n == ) {
  40. printf ("%I64d\n" , 1LL % mod ) ;
  41. continue ;
  42. }
  43. mgml ( , n ) ;
  44. }
  45. return ;
  46. }

这道题到处是坑,推出2^n - 2照样进坑;
首先要防止n = 1 , p = 1 这种情况;

然后要防止ans - 2 < 0;

然后  是 快速幂 的内部优化,让人耳目一新啊

zhx's contest (矩阵快速幂 + 数学推论)的更多相关文章

  1. LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...

  2. HDU 3117 Fibonacci Numbers( 矩阵快速幂 + 数学推导 )

    链接:传送门 题意:给一个 n ,输出 Fibonacci 数列第 n 项,如果第 n 项的位数 >= 8 位则按照 前4位 + ... + 后4位的格式输出 思路: n < 40时位数不 ...

  3. 【洛谷P1962 斐波那契数列】矩阵快速幂+数学推导

    来提供两个正确的做法: 斐波那契数列双倍项的做法(附加证明) 矩阵快速幂 一.双倍项做法 在偶然之中,在百度中翻到了有关于斐波那契数列的词条(传送门),那么我们可以发现一个这个规律$ \frac{F_ ...

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

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

  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. BZOJ3157/BZOJ3516 国王奇遇记(矩阵快速幂/数学)

    由二项式定理,(m+1)k=ΣC(k,i)*mi.由此可以构造矩阵转移,将mi*ik全部塞进去即可,系数即为组合数*m.复杂度O(m3logn),因为大常数喜闻乐见的T掉了. #include< ...

  7. 矩阵快速幂 hud 1575 Tr A 模板 *

    Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

    从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...

  9. 【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂

    原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串 ...

随机推荐

  1. Anaconda安装更新库

    平台:win64+anaconda 1. 如何查看已安装的库 打开 Anaconda Command Prompt ,在命令提示符窗口中输入以下命令: pip list # 或者 conda list ...

  2. Linux下线程池的理解与简单实现

    首先,线程池是什么?顾名思义,就是把一堆开辟好的线程放在一个池子里统一管理,就是一个线程池. 其次,为什么要用线程池,难道来一个请求给它申请一个线程,请求处理完了释放线程不行么?也行,但是如果创建线程 ...

  3. CSS 外边距合并

    外边距合并指的是,当两个垂直外边距相遇时,它们将形成一个外边距. 合并后的外边距的高度等于两个发生合并的外边距的高度中的较大者. 外边距合并 外边距合并(叠加)是一个相当简单的概念.但是,在实践中对网 ...

  4. 多个相同name的文本输入框,输入其中一个后,使剩下的不能输入值

    可以用blur或keyup事件响应: 实现一: <body> <input type="text" id="AfterOtOt1" name= ...

  5. [BZOJ 1266][AHOI2006]上学路线(最短路+最小割)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1266 分析: 很简单的题目,容易想到就是把所有的最短路径挑出来,然后在这个图里跑最小割 ...

  6. 深入javascript

    1.不定参数的使用 <!DOCTYPE html> <html> <head> <title>json</title> <script ...

  7. 《TCP/IP详解卷1:协议》第17、18章 TCP:传输控制协议(2)-读书笔记

    章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...

  8. CSS3——动画效果

    CSS3动画在Style里面就实现了以往我们用JQ写的动画效果,着实简便了不少~ 简单Demo: html代码: <div id="dv1"></div> ...

  9. 关于checkbox最保险和最模棱两可的方法

    最保险的方法: 判断是否是选中的checkbox $('input:checked').length>0 要使checkbox呈现选中状态,最保险的方法,调用input.click()方法 最模 ...

  10. ansible-1 的安装

    该文章摘自:http://my.oschina.net/firxiao/blog/343395,该文章制作笔记使用,不做他用,转载请注明原文链接出处 Ansible 默认是基于SSH协议进行通信的. ...