http://acm.hdu.edu.cn/showproblem.php?pid=5187

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
/**
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 高速幂高速乘法的更多相关文章

  1. HDU - 5187 - zhx&#39;s contest (高速幂+高速乘)

    zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  2. [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)

    Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 12 ...

  3. hdu 5187 快速幂 + 快速乘 值得学习

    就是以那个ai为分水岭,左边和右边都分别是单调增或单调减如图         就这四种情况,其中头两种总共就是两个序列,也就是从头到尾递增和从头到尾递减.         后两种方式就是把序列中德数分 ...

  4. hdu 4704 Sum (整数和分解+高速幂+费马小定理降幂)

    题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7). 当中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                 ...

  5. 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 ( ...

  6. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  7. hdu 4965 Fast Matrix Calculation(矩阵高速幂)

    题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...

  8. HDU 1575 Tr A(矩阵高速幂)

    题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...

  9. 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 ...

随机推荐

  1. 【Demo 0002】Android 提醒框

    本章学习要点:   //==: Alert Dialog void showAlertDialog() { final Builder builder = new AlertDialog.Builde ...

  2. 在Qt中使用ActiveX控件

    Qt的windows商业版本提供了ActiveQt这个framework,使用这个组件我们可以在Qt中使用ActiveX控件,并且也开发基于Qt的ActiveX控件.ActiveQt包含了两个组件QA ...

  3. mysql5.6 主从配置

    参考网址:http://www.cnblogs.com/zhoujie/p/mysql1.html http://kerry.blog.51cto.com/172631/277414/ 1.配置主库: ...

  4. 主从mysql 同步设置

    GRANT REPLICATION SLAVE ON *.* TO 'root'@'%' IDENTIFIED BY 'zhoubt';grant all privileges on *.* to  ...

  5. XMPP入门

    本人原创,很多其它xmpp知识.ios知识.其它分享知识见:我的个人博客 简单介绍: 1.XMPP是可扩展消息与存在协议,主要用于im. 2.他是一种类似于http协议的传输数据协议.过程如:&quo ...

  6. android json 解析 简单示例

    1 下面是一个简单的json 解析的demo,废话不多说,直接上代码 package com.sky.gallery; import java.io.ByteArrayOutputStream; im ...

  7. Abot 爬虫

    Abot 爬虫分析-整体结构 1. 引言 在Github 上搜索下Web Crawler 有上千个开源的项目,但是C#的仅仅只有168 个,相比于Java 或者Python 确实少的可怜.如果按照St ...

  8. Delphi中获取Unix时间戳及注意事项(c语言中time()是按格林威治时间计算的,比北京时间多了8小时)

    uses DateUtils;DateTimeToUnix(Now) 可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60这是因为Now是当前时区的时间 ...

  9. 实现 select中指定option选中触发事件

    我们在用到下拉列表框select时,需要对选中的<option>选项触发事件,其实<option>本身没有触发事件方法,我们只有在select里的onchange方法里触发. ...

  10. hash应用以及vector的使用简介:POJ 3349 Snowflake Snow Snowflakes

    今天学的hash.说实话还没怎么搞懂,明天有时间把知识点总结写了,今天就小小的写个结题报告吧! 题意: 在n (n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角 ...