hdu 5187 快速幂快速乘法

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;
}
时间: 2024-08-26 15:07:47

hdu 5187 快速幂快速乘法的相关文章

hdu 5187 高速幂高速乘法

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 wa

HDU - 5187 zhx&#39;s contest(快速幂+快速乘法)

作为史上最强的刷子之一,zhx的老师让他给学弟(mei)们出n道题.zhx认为第i道题的难度就是i.他想要让这些题目排列起来很漂亮. zhx认为一个漂亮的序列{ai}下列两个条件均需满足. 1:a1..ai是单调递减或者单调递增的. 2:ai..an是单调递减或者单调递增的. 他想你告诉他有多少种排列是漂亮的.因为答案很大,所以只需要输出答案模p之后的值. Input Multiply test cases(less than 10001000). Seek EOF as the end of

HDU 4965 Fast Matrix Caculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 )

HDU 4965 Fast Matrix Calculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 1001 #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 6 typedef long lo

斐波那契优化 快速幂+矩阵乘法

题目:你能求得第n个斐波那契数吗?0<n<maxlongint 由于结果太大,输出的结果mod32768 思路:一般的求斐波那契数列的方法有递归,动归,或者用滚动优化,但是空间复杂或者时间复杂度都太高了,现在有一种用矩阵加快速幂的优化算法,可以让时间复杂度维持在logn. 具体的 初始化一个2×2的矩阵,初始值为{1,0,0,1} 则分别代表{a2,a1,a1,a0},把此矩阵平方后得到{2,1,1,0}分别代表{a3,a2,a2,a1}如此下去,便可以得到规律,其实这个算法主要就是优化在快速

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

Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 1227 Case Time Limit: 2000MS Description A cellular automaton is a collection of cells on a grid of specified shape that evolves through a number of dis

取模性质,快速幂,快速乘,gcd和最小公倍数

一.取模运算 取模(取余)运算法则: 1. (a+b)%p=(a%p+b%p)%p; 2.(a-b)%p=(a%p-b%p)%p; 3.(a*b)%p=(a%p * b%p)%p; 4.(a^b)%p=(   (a%p)^b  )%p; 5. (  (a+b)%p+c  )%p=( a+(b+c)%p  )%p; 6.( a*(b*c)%p )%p =( c*(a*b)%p )%p; 7.( (a+b)%p*c )%p= ( (a*c)%p + (b*c)%p )%p; 几条重要性质: 1.a≡

hdu 5187 快速幂+快速乘法

简单找出规律,答案为(2^n-2 )%p(1特判) 然而  n,p的最大值为 1e18 因此显然要快速幂,而且由于1e18 的平方超long long 所以在乘的时候要用快速乘法,快速乘法的原理和快速幂一样,a^b是 b个a相乘 ,快速乘法是b个a相加 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; long

poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化

题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮,问最后这n个猫各自有多少坚果. 题解:构造(n+1)*(n+1)的单位矩阵,data[i][j]表示第i个猫与第j个猫进行交换,最后一列的前n项就是每个猫的坚果数目,s操作就交换对应行,矩阵快速幂时间复杂度O(n^3*log2(m))会超时,我们注意到在n*n的范围内每一行只有一个1,利用稀疏矩阵的

乘方快速幂 OR 乘法快速幂

关于快速幂这个算法,已经不想多说,很早也就会了这个算法,但是原来一直靠着模板云里雾里的,最近重新学习,发现忽视了一个重要的问题,就是若取模的数大于int型,即若为__int64的时候应该怎么办,这样就得用到乘法快速幂+乘方快速幂了. 快速幂一般是为了解决乘方取模问题的,显然思想就是二分,下面贴上快速幂模板: 1 __int64 mulpow(__int64 a,__int64 p,__int64 m) 2 { 3 __int64 ans = 1; 4 while(p) 5 { 6 if(p&1)