A Simple Math Problem

本文介绍了一种利用矩阵快速幂解决线性递推问题的方法,通过具体的代码实现展示了如何进行矩阵初始化、矩阵相乘及获取矩阵的幂次运算。适用于解决特定形式的线性组合问题。

针对该问题的形式是线性组合,因此可以考虑矩阵相乘的算法。

#pragma warning(disable:4996)
#include<iostream>
using namespace std;
long k, mod;
struct Matrix {
int m[12][12];
};
Matrix factors;
void init()
{
memset(factors.m, 0, sizeof(factors.m));
for (int i = 0; i<9; i++)
factors.m[i][i+1] = 1;
}


//两个矩阵相乘
Matrix multiply(Matrix a, Matrix b)
{
Matrix result;
memset(result.m, 0, sizeof(result.m));

for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 10; k++)
result.m[i][j] = (result.m[i][j] + a.m[i][k] * b.m[k][j]) % mod;
result.m[i][j] %= mod;

}

}

return result;
}
//获取矩阵的n次方,很重要
Matrix getPow(Matrix a,Matrix b, int n)
{

while(n)

if (n & 1) b = multiply(a, b);
n >>= 1;
a = multiply(a, a);
}
return b;
}




int main()
{
//freopen("a.txt", "r", stdin);
while (cin >> k >> mod)
{
init();
for (int i = 0; i < 10; i++)
{
cin >> factors.m[i][0];
}
if (k<10) {
cout << k%mod << endl;
continue;
}
Matrix t;
memset(t.m, 0, sizeof(t.m));
for(int j = 0; j< 10; j++)
t.m[j][j] = 1;
Matrix re = getPow(factors, t, k - 9);

long result = 0;
for (int j = 0; j < 10; j++)
{
result += (re.m[j][0] * (9-j)) % mod;
}
cout << result%mod<< endl;
}
return 0;
}

PS D:\Python\projects\math_mode> pip install sklearn WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/sklearn/ Collecting sklearn Using cached sklearn-0.0.post12.tar.gz (2.6 kB) Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [15 lines of output] The 'sklearn' PyPI package is deprecated, use 'scikit-learn' rather than 'sklearn' for pip commands. Here is how to fix this error in the main use cases: - use 'pip install scikit-learn' rather than 'pip install sklearn' - replace 'sklearn' by 'scikit-learn' in your pip requirements files (requirements.txt, setup.py, setup.cfg, Pipfile, etc ...) - if the 'sklearn' package is used by one of your dependencies, it would be great if you take some time to track which package uses 'sklearn' instead of 'scikit-learn' and report it to their issue tracker - as a last resort, set the environment variable SKLEARN_ALLOW_DEPRECATED_SKLEARN_PACKAGE_INSTALL=True to avoid this error More information is available at https://github.com/scikit-learn/sklearn-pypi-package [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. PS D:\Python\projects\math_mode>
最新发布
05-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值