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

 #include<stdio.h>
typedef long long ll ;
ll n , mod ; ll mut (ll a , ll b)
{
ll ans = ;
while (b) {
if (b & ) {
ans = (ans + a) % mod ;
}
a = (a + a ) % mod ;
b >>= ;
}
return ans % mod ;
} void mgml (ll a , ll b)
{
ll ans = ;
while (b) {
if (b & ) {
ans = mut (ans , a ) ;
}
b >>= ;
a = mut ( a , a ) ;
}
ans -= ;
ans %= mod ;
ans += mod ;
ans %= mod ;
printf ("%I64d\n" , ans ) ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
while (~ scanf ("%I64d%I64d" , &n , &mod)) {
if (n == ) {
printf ("%I64d\n" , 1LL % mod ) ;
continue ;
}
mgml ( , n ) ;
}
return ;
}

这道题到处是坑,推出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. GDB代码调试与使用

    GDB代码调试与使用 Linux下GDB调试代码 源代码 编译生成执行文件 gcc -g test.c -o test 使用GDB调试 启动GDB:gdb test 从第一行列出源代码:list 直接 ...

  2. JSTL(JSP Standard Tag Library)读书笔记

    分类                                       Preifx                                          范例 核心标签库--- ...

  3. Dandelion - Distributed Computing on GPU Clusters

    linq on GPUs 非常期待中 看起来很cool,期望早点面世

  4. js回调

    请先看着一片blog: http://www.jb51.net/article/53027.htm 回调的两种使用方法: 1.一般的传函数.2.匿名函数 3.使用回调函数再使用call方法. 判断一个 ...

  5. [C#基础]ref和out的区别

    在C#中通过使用方法来获取返回值时,通常只能得到一个返回值.因此,当一个方法需要返回多个值的时候,就需要用到ref和out,那么这两个方法区别在哪儿呢? MSDN:       ref 关键字使参数按 ...

  6. CsharpThinking---代码契约CodeContract(八)

    代码契约(Code Contract):它并不是语言本身的新功能,而是一些额外的工具,帮助人们控制代码边界. 代码契约之于C#,就相当于诗词歌赋之于语言. --- C# in Depth 一,概述 1 ...

  7. mysqldump使用方法

    1.mysqldump的几种常用方法: (1)导出整个数据库(包括数据库中的数据) mysqldump -u username -p dbname > dbname.sql (2)导出数据库结构 ...

  8. chromium获取代码和编译

    转自360公司的一位仁兄,链接地址:http://blog.gclxry.com/?p=364 之前一直是用一个chromium27的代码来研究chromium的代码.自己也调用chromium co ...

  9. 让less编译通过css滤镜

    写IE6 hack的时候,发现在less中直接写css滤镜是会报错的,不能编译通过. 解决方法为:用~“”把相关的css代码包裹起来,例如: _top:~"expression(docume ...

  10. Xunit

    Attributes Note: This table was written back when xUnit.net 1.0 has shipped, and needs to be updated ...