当前位置:网站首页>Mathematics (fast power)
Mathematics (fast power)
2022-06-30 11:10:00 【D αī s ч】
One 、 Fast power
By fast exponentiation , We calculated a^8 when , No more a*a*a*a*a*a*a*a, It is a*a,a^2*a^2,a^4*a^4, So we ask a^b It will be O(log b) Complexity
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;
}Two 、 Matrix fast power
Use fast power , You can find a matrix ( Matrix ) Of n The next power
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 Matrix cumulative power , When an odd number is encountered, multiply it to res On
multi(a, a); //a=a*a
m >>= 1;
}
// The final answer is 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';
}
}Structure version , Overloaded multiplication operator
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';
}
} application : When we need to find a recurrence of the first n term ( for example
) when , You can use matrix multiplication , By exponentiating the matrix, we get the n term
边栏推荐
- File sharing server
- 8行代码实现快速排序,简单易懂图解!
- 100 important knowledge points that SQL must master: Combined Query
- 【无标题】
- The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented
- Mysql database foundation: constraint and identification columns
- Review of mathematical knowledge: curve integral of the second type
- WireGuard简单配置
- 创建型-配置工厂
- List介绍
猜你喜欢

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

The life, working principle and application of electrochemical oxygen sensor

Dickinson's soul chooses its companion

Jetpack Compose DropdownMenu跟随手指点击位置显示

China will force a unified charging interface. If Apple does not bow its head, iPhone will be kicked out of the Chinese market

Deep dive kotlin synergy (16): Channel

【IC5000教程】-01-使用daqIDEA图形化debug调试C代码

20万奖金池!【阿里安全 × ICDM 2022】大规模电商图上的风险商品检测赛火热报名中!...

Every time I look at my colleagues' interface documents, I get confused and have a lot of problems...

promise async和await的方法与使用
随机推荐
线代(高斯消元法、线性基)
100 important knowledge points that SQL must master: creating and manipulating tables
SQL必需掌握的100个重要知识点:使用子查询
焕发青春的戴尔和苹果夹击,两大老牌PC企业极速衰败
LVGL 8.2 Image styling and offset
【leetcode 239】滑动窗口
LiveData源码赏析三 —— 常见问题
The intelligent DNA molecular nano robot model is coming
Deep dive kotlin synergy (18): hot and cold data flow
国产自研系统的用户突破4亿,打破美国企业的垄断,谷歌后悔不迭
100 important knowledge points that SQL must master: using table aliases
【西安交通大学】考研初试复试资料分享
创建型-配置工厂
在IPhone12的推理延迟仅为1.6 ms!Snap等详析Transformer结构延迟,并用NAS搜出移动设备的高效网络结构...
LVGL 8.2 Checkboxes as radio buttons
SQL必需掌握的100个重要知识点:汇总数据
ArrayList and sequence table
[leetcode 16] sum of three numbers
100 important knowledge points that SQL must master: insert data
MySQL export SQL script file