hdu 5187(高精度快速幂)
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
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.
For each case, there are two integers n and p separated by a space in a line. (1≤n,p≤1018)
3 5
1
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
#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(高精度快速幂)的更多相关文章
- hdu 2462(欧拉定理+高精度快速幂模)
The Luckiest number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 2855 (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...
- HDU 4471 矩阵快速幂 Homework
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...
- HDU - 1575——矩阵快速幂问题
HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...
- 洛谷试炼场-简单数学问题-P1045 麦森数-高精度快速幂
洛谷试炼场-简单数学问题 B--P1045 麦森数 Description 形如2^P−1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果PP是个素数,2^P-1 不一定也是素数.到19 ...
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
- 随手练——HDU 5015 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...
- 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} ...
- 读入 并查集 gcd/exgcd 高精度 快速幂
ios_base::sync_with_stdio(); cin.tie(); ], nxt[MAXM << ], Head[MAXN], ed = ; inline void added ...
随机推荐
- SourceTree git的管理工具使用教程1
1SourceTree是一个window系统下的Git管理工具 2设置Git 工具——选项——Git设置 3拷贝远程的项目 新建/克隆(输入远程项目的url地址) 4验证(填写用户信息) 工具——选项 ...
- .aspx文件和.aspx.cs文件的区别与联系
http://zhidao.baidu.com/link?url=_SNw0EHJ8Wg__KanJrKQM3tVEUeFnVilZ6GGIN8ab69RLuyOWD__WyZb7Zb9dJjwDnL ...
- valgrind使用
参数配置 gcc -g: 增加调试信息,供valgrind精确定位. -O0:关闭gcc优化:优化产生的代码可能会造成valgrind误判. valgrind --leak-check=full no ...
- Jpeg-Baseline和Progressive JPEG的区别
原文来自 http://www.hdj.me/use-progressive-jpeg-in-web 看着不错,于是粘贴了过来 今天才认识到原来JPEG文件有两种保存方式他们分别是Baseline J ...
- Java IO 小结
Java IO 的学习需要明白流设计的体系结构,这样才能在实际需要的时候,通过API文档查阅,快速实现功能.
- MySQL 服务器安装及命令使用
本文来自实验楼相关部分的文档和实验操作过程. 一.MySQL简介 MySQL是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,提高了速度并提高了灵活性.My ...
- CDQZ 2017 游记
Day0: 提前放了一整天假,颓过去了.老吕让我去给B层的讲课,ppt还没做,只能在飞机上赶了QAQ.然后从上午到了衡水就一直在路上或者天上,到了晚上才到学校,然而ppt还是没有做完.还有,鄂尔多斯真 ...
- Linux(CentOS)用split命令分割文件
在 Linux 里,稍加不注意有可能会产生很大体积的日志文件,哪怕几百M,拖下来分析也很浪费时间,这个时候,如果可以把文件切割成 N 个小文件,拿最后一个文件就可以看到最近的日志了.有一些手段,比如用 ...
- codeforces 1077D
题目:https://codeforces.com/contest/1077/problem/D 题意:给你一个长度为n的串,你需要在里面找到出现次数最多的长度为k的子序列(子序列中元素可重复),求这 ...
- Join to domain powershell script
Join to domain powershell script $username = "domain\admin" $Password = "xxxxxxxx&quo ...