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 long n,p;
long long q_mul(long long a,long long x)
{
    long long ans=0;
    long long res=a;
    while(x){
        if(x & 1){
            ans=(ans+res) % p;
        }
        res = (res + res) % p;
        x>>=1;
    }
    return ans;
}
long long q_pow(long long a,long long x)
{
    long long ans=1;
    long long res=a;
    while(x){
        if(x & 1) {
            ans=q_mul(ans,res) % p;
        }
        res=q_mul(res,res);
        x>>=1;
    }
    return ans;
}
int main()
{
    while(scanf("%lld%lld",&n,&p)!=EOF){
        if(n==1){
            printf("%lld\n",n%p);
        }
        else{
            long long ans=((q_pow(2,n)-2) %p +p ) %p;
            printf("%lld\n",ans);
        }
    }
    return 0;
}

时间: 2024-08-24 02:35:19

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 快速幂快速乘法

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≡

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)