快速幂算法(学习笔记)
快速幂----即“模平方重复计算法”
对于指数位置上比较大的数,直接用pow函数可能会超时,因为要计算很多次。
这里直接给出实例,容易理解:(字丑,求轻喷~)
例题1:HDU 2035 ---- 人见人爱A^B
Problem Description
求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”
Input
输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。
Output
对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。
Sample Input
2 3
12 6
6789 10000
0 0
Sample Output
8
984
1
直接上代码:
#include <iostream>
using namespace std;
typedef long long LL;
LL quick_pow( LL a, LL b, LL m )
{
LL res=1;
while ( b > 0 )
{
if ( b % 2 == 1 )
{
res*=a; // 累乘求积,
res%=m; // 防止溢出,每一步都取余一下(模一下),最终结果不会改变,因为只计算最后三位的数
}
a*=a; // 底数平方
a%=m;
b=(b>>1); // 指数右移,即丢弃最后一位
}
return res;
}
int main()
{
int a, b;
cin>>a>>b;
while( a!=0 && b!=0 )
{
cout<<quick_pow( a , b , 1000 )<<endl;
cin>>a>>b;
}
return 0;
}
例题2. HDU 1575 ----Tr A
Problem Description
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
Input
数据的第一行是一个T,表示有T组数据。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。
Output
对应每组数据,输出Tr(A^k)%9973。
Sample Input
2
2 2
1 0
0 1
3 99999999
1 2 3
4 5 6
7 8 9
Sample Output
2
2686
思路:A^k 需要运用快速幂,即使A是个矩阵(方阵)也可以用快速幂
废话不多说,直接上代码:
注:为了避免溢出,需要对每一步进行取模运算
#include <iostream>
using namespace std;
typedef long long ll;
struct matrix
{
int m[10][10];
};
matrix mul( matrix a , matrix b )
{
matrix tmp;
for ( int r=0 ; r<10 ; r++ )
{ // 对临时矩阵的初始化
for ( int c=0 ; c<10 ; c++ )
{
tmp.m[r][c]=0;
}
}
for ( int r=0 ; r<10 ; r++ )
{ // 矩阵乘法
for ( int c=0 ; c<10 ; c++ )
{
for ( int i=0 ; i<10 ; i++ )
tmp.m[r][c]+=a.m[r][i]*b.m[i][c];
}
}
return tmp;
}
ll matrixpow( matrix x , ll k ,ll m)
{
matrix res;
for ( int r=0 ; r<10 ; r++ )
{ // 对临时矩阵的初始化
for ( int c=0 ; c<10 ; c++ )
{
if ( r == c ) res.m[r][c]=1;
else res.m[r][c]=0;
}
}
while ( k>0 )
{
if ( k % 2 == 1)
{
res=mul( res , x );
for ( int r=0 ; r<10 ; r++ )
{ // 对每个元素取模
for ( int c=0 ; c<10 ; c++ )
{
res.m[r][c] %= m;
}
}
}
x=mul( x , x );
for ( int r=0 ; r<10 ; r++ )
{ // 对每个元素取模
for ( int c=0 ; c<10 ; c++ )
{
x.m[r][c] %= m;
}
}
k=(k>>1);
}
int sum=0;
for ( int r=0 ; r<10 ; r++ )
{
for ( int c=0 ; c<10 ; c++ )
{
if ( r==c )
{
sum+=res.m[r][c];
sum %= m;
}
}
}
return sum;
}
int main()
{
int t; cin>>t;
while( t-- )
{
int n; ll k;
cin>>n>>k;
matrix x;
for ( int r=0 ; r<10 ; r++ )
{ // 对矩阵初始化,不进行初始化,将会在矩阵的每一个位置生成随机数
for ( int c=0 ; c<10 ; c++ )
{
x.m[r][c]=0;
}
}
for ( int r=0 ; r<n ; r++ )
{
for ( int c=0 ; c<n ; c++ )
cin>>x.m[r][c];
}
cout<<matrixpow( x , k , 9973 ) % 9973<<endl;
}
return 0;
}
例题3. POJ 3070 ---- Fibonacci
链接: http://poj.org/problem?id=3070
仍然是矩阵快速幂的运用
代码:
#include <iostream>
using namespace std;
typedef long long ll;
struct matrix
{
int m[2][2];
};
matrix multiply( matrix a , matrix b )
{
matrix tmp;
for ( int r=0 ; r<2 ; r++ )
{
for ( int c=0 ; c<2 ; c++)
{
tmp.m[r][c]=0;
}
}
for ( int r=0 ; r<2 ; r++ )
{
for ( int c=0 ; c<2 ; c++)
{
for (int i=0 ; i<2 ; i++)
{
tmp.m[r][c]+=a.m[r][i]*b.m[i][c];
tmp.m[r][c] %= 10000;
}
}
}
return tmp;
}
ll Fib ( ll n )
{
matrix res, base;
// 初始化底数矩阵
for ( int r=0 ; r<2 ; r++ )
{
for ( int c=0 ; c<2 ; c++)
{
if ( r==1 && c==1 ) base.m[r][c]=0;
else base.m[r][c]=1;
}
}
// 初始化累乘矩阵
for ( int r=0 ; r<2 ; r++ )
{
for ( int c=0 ; c<2 ; c++)
{
if ( r==c ) res.m[r][c]=1;
else res.m[r][c]=0;
}
}
while ( n>0 )
{
if ( n % 2 == 1 )
{
res=multiply( res , base );
}
base=multiply( base , base );
n=(n>>1);
}
return res.m[0][1];
}
int main()
{
ll n; cin>>n;
while ( n != -1 )
{
if ( n == 0 )
cout<<"0"<<endl;
else
cout<<Fib(n)<<endl;
cin>>n;
}
return 0;
}