当前位置:网站首页>[number theory] congruence (7): fast power, fast power of matrix
[number theory] congruence (7): fast power, fast power of matrix
2022-04-22 07:20:00 【default111】
The congruence problem is 7part, My blog links :
- Basic concepts and properties
- Inverse element : Concept 、 Solution method and derivation
- Linear congruence equation
- Higher order congruence equation :BSGS Algorithm ( Large and small step algorithm 、 Bashan algorithm )
- Fast power and fast power of matrix
Fast power
Ordinary fast power
form : x n m o d m x^n \mod m xnmodm
principle : take n n n Split into binary , The original formula becomes x n 0 × 2 0 + n 1 × 2 1 + ⋯ + n k × 2 k = x n 0 × 2 0 ⋅ x n 1 × 2 1 ⋯ x n k × 2 k = ( x 2 0 ) n 0 ⋅ ( x 2 1 ) n 1 ⋯ ( x 2 k ) n k \begin{aligned} &x^{n_0\times 2^{0}+n_1\times 2^{1}+\cdots+n_k\times 2^{k}}\\=&x^{n_0\times 2^{0}}\cdot x^{n_1\times 2^{1}}\cdots x^{n_k\times 2^{k}}\\=&(x^{2^{0}})^{n_0}\cdot(x^{2^{1}})^{n_1}\cdots(x^{2^{k}})^{n_k} \end{aligned} ==xn0×20+n1×21+⋯+nk×2kxn0×20⋅xn1×21⋯xnk×2k(x20)n0⋅(x21)n1⋯(x2k)nk
// Fast power : Calculation x^n mod m
ll qpow(ll x, ll n, ll m)
{
ll ans = 1ll;
while (n)
{
if (n & 1)
ans = (ans * x) % m;
x = x * x % m;
n >>= 1ll;
}
return ans;
}
Matrix fast power
form : n × n n\times n n×n Matrix A A A Of k k k Power : A k A^k Ak
Like the ordinary fast power principle , It's just matrix multiplication
struct matrix
{
ll a[N][N];
ll m;
int n;
matrix(int x = 0, ll mod) : n(x), m(mod) {
}
void build() // Read in the matrix
{
scanf("%d", &n);
scanf("%lld", &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%lld", &a[i][j]);
}
void init() // The matrix is initialized to the identity matrix
{
for (int i = 1; i <= n; i++)
a[i][i] = 1ll;
}
matrix operator*(const matrix &b)
{
matrix nxt;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
nxt.a[i][j] = (nxt.a[i][j] % m + a[i][k] % m * b.a[k][j] % m) % m;
return nxt;
}
};
matrix matrix_qpow(matrix A, ll k)
{
matrix ans(A.n, A.m);
ans.init();
while (k)
{
if (k & 1)
ans = ans * A;
A = A * A;
k >>= 1ll;
}
}
版权声明
本文为[default111]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220611441099.html
边栏推荐
- Choose any novel website, crawl any novel and save it in the form of Notepad.
- Distributed task scheduling and computing framework: powerjob quick start (local ide version) 02
- Clion and dynamic link library
- Define an abstract role class with name, age, gender, hobbies and other member variables. It is required to hide all variables as far as possible (private if possible), and then read and write each va
- Jeecg project deployment notes
- 【SVN】Subversion安装使用笔记
- . net learning notes (2) -- ubiquitous reflection (including the method of reading JSON)
- JVM中的逃逸分析,可以实现不在堆上分配内存
- [number theory] congruence (V): multivariate linear congruence equation
- ERROR: [Hsi 55-1545] ,无法正常生成fsbl,Unable to read in MSS file,Failed to closesw system.mss
猜你喜欢

模二除运算的上商原则

(二)Sql Server的基本配置以及使用Navicat连接Sql Server

SQL server stored procedure development notes - piecemeal problems and operations on operation files
![. net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]](/img/c8/e7579543d7911a907ae19f357478d8.png)
. net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]

Wechat browser cannot save cookies for a long time

Pycharm only needs five steps to improve the download speed by using Tsinghua image source

. net learning notes (2) -- ubiquitous reflection (including the method of reading JSON)

完成一个学生信息管理系统,系统练习面向对象、函数、字符串等知识。实现知识的综合应用。 使用类、函数、数据库等来实现

定义类Shape作为父类,并在类中定义方法求周长和面积; (2)定义Shape子类圆形(circle),具有半径属性和常量PI,同时重写父类中的方法; (3)定义Shape子类长方形(rect

Design a circle class with private member radius representing radius and function get_ Radius () is used to obtain the radius and area () is used to calculate the area of the circle; (2) Define a tabl
随机推荐
【数论】欧拉函数(基本性质、递推法、公式法、线性筛法)
Distributed task scheduling and computing framework: introduction to powerjob 01
Pixhawk4+Up Board / NUC Implement VIO By Deploying T265
Eight functions of random library
把上一次作业第一部分,有参数的类,改成无参数方式呈现,功能不变。
(六) Sql Server的DCL丶DML语法
JVM中的逃逸分析,可以实现不在堆上分配内存
【数论】同余(二):逆元
VSCode打开小程序运行到微信开发者工具WXML 文件编译报错
Quartus II防止信号被综合
【数论】同余(三):一元线性同余方程
[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted
Build ES6 development environment and compile in real time
【数论】素数(五):梅森素数(Lucas_Lehmer判定法)
ASP. Net daily development notes ---- parsing and transforming XML
短路
安裝和修改uTools及vscode插件安裝路徑
【数论】同余(四):一元线性同余方程组(两两相消、中国剩余定理)
Notes on daily development ---- some easy-to-use settings on vs
ASP. Net daily development notes ----- IIS server supports downloading APK