hdu 5187 高速幂高速乘法
http://acm.hdu.edu.cn/showproblem.php?pid=5187
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.
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)
2 233
3 5
2
1HintIn 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
/**
hdu 5187 高速幂高速乘法
题目大意:(转)数字1~n,按某种顺序排列。且满足下列某一个条件:(1)a1~ai递增,ai~an递减(2)a1~ai递减,ai~an递增。
问有多少种不同的排列。
解题思路:首先是所有递减或所有递增各一种;另外就是满足上列两个条件的情况了。要想满足条件(1)那就仅仅能把最大的n放在i位置,
共同拥有C(1,n-1)+C(2。n-1)+。。。 +C(n-2,n-1)即2^(n-1)-2;条件(2)与(1)同样,所以共同拥有(2^(n-1)-2)*2+2=2^n-2.
**/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL; LL n,p; LL qui_mul(LL x,LL m)///高速乘法
{
LL re=0;
while(m)
{
if(m&1)
{
re=(re+x)%p;
}
x=(x+x)%p;
m>>=1;
}
return re;
} LL qui_pow(LL a,LL n)///高速幂
{
LL ret=1;
LL tem=a%p;
while(n)
{
if(n%1)ret=qui_mul(ret,temp)%p;
temp=qui_mul(temp,temp)%p;
n>>=1;
}
return ret;
} int main()
{
while(~scanf("%I64d%I64d",&n,&p))
{
if(n==1)
{
if(p==1)
printf("0\n");
else
printf("1\n");
}
printf("%I64d\n",(qui_mul(2,n)-2)%p);
}
return 0;
}
hdu 5187 高速幂高速乘法的更多相关文章
- HDU - 5187 - zhx's contest (高速幂+高速乘)
zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3048 Accepted: 12 ...
- hdu 5187 快速幂 + 快速乘 值得学习
就是以那个ai为分水岭,左边和右边都分别是单调增或单调减如图 就这四种情况,其中头两种总共就是两个序列,也就是从头到尾递增和从头到尾递减. 后两种方式就是把序列中德数分 ...
- hdu 4704 Sum (整数和分解+高速幂+费马小定理降幂)
题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7). 当中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3. ...
- hdu 5318 The Goddess Of The Moon 矩阵高速幂
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 The Goddess Of The Moon Time Limit: 6000/3000 MS ( ...
- hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...
- hdu 4965 Fast Matrix Calculation(矩阵高速幂)
题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...
- HDU 1575 Tr A(矩阵高速幂)
题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...
- HDU 2256 Problem of Precision(矩阵高速幂)
题目地址:HDU 2256 思路: (sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n; 这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6); a ...
随机推荐
- Windows下与Linux下编写socket程序的区别 《转载》
原文网址:http://blog.chinaunix.net/uid-2270658-id-308160.html [[Windows]] [Windows: 头文件的区别] #include< ...
- MFC自绘控件学习总结第二贴
首先感谢大家对第一帖的支持,应一些网友烈要求下面我在关于上一贴的一些补充和说明(老鸟可以无视)这一贴是实战+理论不知道第一帖的先看第一帖:http://topic.csdn.net/u/2011071 ...
- MySQL installer
- windows消息处理(强烈推荐,收藏)
由于看了一下,比较好理解,暂时先放到这里,待有空再翻译.只是在每节后大致介绍一下讲的内容. 感觉写的比较全,无论从消息的原理还是从MFC操作上来说,值得一看,我也在此做个收藏. (一) 说明:以下首先 ...
- LIS问题分析
题目来源,待字闺中,原创@陈利人 ,欢迎大家继续关注微信公众账号"待字闺中" 原题这个LIS问题,可不是Longest Increasing Subsequence,而是Large ...
- Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性加入图标
注:(图中每个条目和图标都是由代码动态生成) 代码动态布局,并须要为每个条目设置图标,此时用到了 android:drawableLeft="@drawable/icon" 父x ...
- Oracle 12C 简介
2013年6月26日,Oracle Database 12c版本正式发布,首先发布的版本号是12.1.0.1.0,率先提供下载的平台有Linux和Solaris: Oracle官方下载地址: http ...
- 与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室
原文:与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室 [索引页][源码下载] 与众不同 windows phon ...
- [Usaco2008 Dec]Patting Heads
It's Bessie's birthday and time for party games! Bessie has instructed the N (1 <= N <= 100,00 ...
- POJ 1562 Oil Deposits
转载请注明出处:http://blog.csdn.net/a1dark 大规模的图论切题之旅正式开始了.由于今天停了一天的电.所以晚上才开始切题.直到昨晚才把图论大概看了一遍.虽然网络流部分还是不怎么 ...