zhx's contest

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

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-n n个数组成的序列,有多少种组合方式满足 a[1] - a[i] 单调递增/单调递减, a[i] - a[n] 单调递增/单调递减.
题解:如果想要a[1] - a[i] 单调递增,a[i] - a[n]单调递减,那么就只能将最大的n放到第 i 位置,那么当n的左边的排列确定了,那么其右边的排列也确定了,所以总共有C(0,n-1)+C(2,n-1)+...+C(n-2,n-1)+C(n-1,n-1) = 2^(n-1)种放法,反之就是将最小的1放到第i位置满足a[1]-a[i]单调递减,a[i]-a[n]单调递增了,情况数一样,但是由于单调递增和单调递减算了两次,所以要减2.答案为 2^n-2 ,由于n太大,所以才用了高精度快速幂(快速幂+快速乘法).
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
LL n,m; LL modular_multi(LL a, LL b, LL c) /// a * b % c
{
LL res, temp;
res = , temp = a % c;
while (b)
{
if (b & )
{
res += temp;
if (res >= c)
{
res -= c;
}
}
temp <<= ;
if (temp >= c)
{
temp -= c;
}
b >>= ;
}
return res;
}
LL modular_exp(LL a, LL b, LL c)
{
LL res, temp;
res = % c, temp = a % c;
while (b)
{
if (b & )
{
res = modular_multi(res, temp, c);
}
temp = modular_multi(temp, temp, c);
b >>= ;
}
return res;
}
int main()
{
while(scanf("%lld%lld",&n,&m)!=EOF)
{
LL ans = modular_exp(,n,m);
printf("%lld\n",(ans-+m)%m);
}
return ;
}

hdu 5187(高精度快速幂)的更多相关文章

  1. hdu 2462(欧拉定理+高精度快速幂模)

    The Luckiest number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  3. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  4. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  5. 洛谷试炼场-简单数学问题-P1045 麦森数-高精度快速幂

    洛谷试炼场-简单数学问题 B--P1045 麦森数 Description 形如2^P−1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果PP是个素数,2^P-1 不一定也是素数.到19 ...

  6. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  7. 随手练——HDU 5015 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...

  8. HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识

    求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...

  9. 读入 并查集 gcd/exgcd 高精度 快速幂

    ios_base::sync_with_stdio(); cin.tie(); ], nxt[MAXM << ], Head[MAXN], ed = ; inline void added ...

随机推荐

  1. 简单理解DES加密算法

    数据加密标准(Data Encryption Standard,DES)是当前使用最广泛的加密体制,对于任意的加密方案,总有两个输入:明文和密钥. 明文是64bits,密钥是56bits 加密过程就是 ...

  2. sql声明变量存储查询结果

    with t as 查到条件数据,然后在下面使用到t,用exists做判断会非常慢,改成left join会快很多. 我使用的数据库时2008Sql r2. 文章:SQL数据库中临时表.临时变量和WI ...

  3. sendto函数的坑

    测试unix数据报套接字时,一个程序收,一个程序发,分别绑定自己的socket.结果在收的部分,返回的发送方的地址总是空的,但是返回的地址长度又是对的. ) { bzero(&clientad ...

  4. PhoneGap & HTML5 学习资料网址

    PhoneGap 与 Application Cache应用缓存  http://www.html5cn.org/forum.php?mod=viewthread&tid=40272 加速We ...

  5. linux下生成core dump文件方法

    core 文件的简单介绍 当程序运行的过程中异常终止或崩溃,操作系统会将程序当时的内存状态记录下来,保存在一个文件中,这种行为就叫做Core Dump(中文有的翻译成“核心转储”).我们可以认为 co ...

  6. StrutsResultSupport的使用

    在有特殊情况时:如果没有异常信息,但是有错误并且有错误信息等内容:此时也需要进行友好的错误处理的话,那么可以借助StrutsResultSupport 返回结果类型来实现特定处理.此种方式先需要继承S ...

  7. 先查出已知的 然后将未知的当做 having里面的条件

    先查出已知的 然后将未知的当做 having里面的条件

  8. SQL语言:结构化查询语言

    SQL语言:结构化查询语言 程序员或者DBA(数据库管理员)使用SQL和DBBSM进行交互,操纵数据库中的资源 分类: 1.DDL 数据定义语言 结构 create  创建   database ta ...

  9. BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap

    一开始光知道pushdown却忘了pushup......... #include<cstdio> #include<iostream> #include<cstring ...

  10. vue 时间戳转 YYYY-MM-DD h:m:s

    export default function(data = 1){ let myDate; if(data !== 1){ myDate = new Date(data * 1000); }else ...