当前位置:网站首页>数学(快速幂)
数学(快速幂)
2022-06-30 10:42:00 【Dαīsч】
一、快速幂
通过快速幂,我们计算a^8时,不再是a*a*a*a*a*a*a*a,而是a*a,a^2*a^2,a^4*a^4,这样我们求a^b将是O(log b)的复杂度
ll quick_pow(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1)
ans = (ans * a) % mod;
b >>= 1;
a = (a * a) % mod;
}
return ans;
}二、矩阵快速幂
利用快速幂,可以求一个矩阵(方阵)的n次幂
ll temp[maxn][maxn];
ll res[maxn][maxn];
ll a[maxn][maxn];
ll n, m;
void multi(ll a[][maxn], ll b[][maxn])
{
memset(temp, 0, sizeof temp);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
temp[i][j] = (temp[i][j] + a[i][k] * b[k][j]) % mod;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = temp[i][j];
}
void qpow(ll a[][maxn], ll m)
{
memset(res, 0, sizeof(res));
for (int i = 0; i < n; i++)
res[i][i] = 1;
while (m)
{
if (m & 1)
multi(res, a); //res=res*a, a矩阵累积次方,当遇到奇数时乘到res上
multi(a, a); //a=a*a
m >>= 1;
}
//最后答案是res
}
void solve()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
qpow(a, m);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << res[i][j] << ' ';
}
cout << '\n';
}
}结构体版本,重载乘法运算符
struct mat
{
int a[maxn][maxn];
int* operator [] (int i)
{
return a[i];
}
mat() { memset(a, 0, sizeof a); }
mat operator * (mat b)
{
mat res;
for (int i = 0; i < maxn; i++)
for (int j = 0; j < maxn; j++)
for (int k = 0; k < maxn; k++)
(res[i][j] += 1LL * a[i][k] * b[k][j] % mod) %= mod;
return res;
}
};
mat qpow(mat a, ll n)
{
mat res;
for (int i = 0; i < maxn; i++)
{
res.a[i][i] = 1;
}
while (n)
{
if (n & 1)
res = res * a;
a = a * a, n >>= 1;
}
return res;
}
void solve()
{
mat a;
ll n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
a = qpow(a, m);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << a[i][j] << ' ';
}
cout << '\n';
}
}应用:当我们需要求一个递推式的第n项(例如
)时,可以利用矩阵乘法,通过对矩阵求幂得到第n项
边栏推荐
- 记一次ViewPager + RecyclerView的内存泄漏
- Every time I look at my colleagues' interface documents, I get confused and have a lot of problems...
- datax - 艰难debug路
- SQL必需掌握的100个重要知识点:更新和删除数据
- ArrayList与顺序表
- 微信表情符号被写入判决书,你发的每个 emoji 都可能成为呈堂证供
- 第一届中国数字藏品大会即将召开
- 【leetcode 239】滑动窗口
- LVGL 8.2 Image
- Time complexity and space complexity
猜你喜欢

CP2112使用USB转IIC通信教学示例

MySQL导出sql脚本文件

Rejuvenated Dell and apple hit each other, and the two old PC enterprises declined rapidly

Deep dive kotlin synergy (16): Channel
![[STL source code analysis] iterator](/img/e8/7c69cf6e96ecfa053494397a21eff0.jpg)
[STL source code analysis] iterator
![When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]](/img/2a/f07a7006e0259d78d046b30c761764.jpg)
When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]

数据库什么时候需要使用索引【杭州多测师】【杭州多测师_王sir】

记一次ViewPager + RecyclerView的内存泄漏

【STL源码剖析】容器(待补充)

科普达人丨漫画图解什么是eRDMA?
随机推荐
[STL source code analysis] container (to be supplemented)
SQL必需掌握的100个重要知识点:使用子查询
内存逃逸分析
The jetpack compose dropdownmenu is displayed following the finger click position
科普达人丨漫画图解什么是eRDMA?
100 important knowledge points that SQL must master: Combined Query
Qt之实现动效导航栏
datax - 艰难debug路
从开源项目探讨“FPGA挖矿”的本质
Pytorch notes: validation, model eval V.S torch. no_ grad
Classic interview question: responsible modules, how do you design test cases for these function points? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]
LVGL 8.2 Simple Drop down list
100 important knowledge points that SQL must master: summary data
【leetcode 239】滑动窗口
智能DNA分子纳米机器人模型来了
pytorch 笔记 torch.nn.BatchNorm1d
[leetcode 239] sliding window
Time complexity and space complexity
SQL必需掌握的100个重要知识点:创建和操纵表
LVGL 8.2 menu from a drop-down list