您的位置:首页 > 科技 > 能源 > 美空摄影网_做一个微信小程序要多少钱_无排名优化_流量精灵app

美空摄影网_做一个微信小程序要多少钱_无排名优化_流量精灵app

2025/4/10 15:51:56 来源:https://blog.csdn.net/he_zhidan/article/details/145982759  浏览:    关键词:美空摄影网_做一个微信小程序要多少钱_无排名优化_流量精灵app
美空摄影网_做一个微信小程序要多少钱_无排名优化_流量精灵app

本文涉及知识点

【矩阵快速幂】封装类及测试用例及样例

P1397 [NOI2013] 矩阵游戏

题目描述

婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的 n n n m m m 列的矩阵(你不用担心她如何存储)。她生成的这个矩阵满足一个神奇的性质:若用 F [ i , j ] F[i,j] F[i,j] 来表示矩阵中第 i i i 行第 j j j 列的元素,则 F [ i , j ] F[i,j] F[i,j] 满足下面的递推式:

F [ 1 , 1 ] = 1 F [ i , j ] = a × F [ i , j − 1 ] + b , j ≠ 1 F [ i , 1 ] = c × F [ i − 1 , m ] + d , i ≠ 1 \begin{aligned} F[1, 1] &= 1 \\ F[i, j] &=a\times F[i, j-1]+b, &j\neq 1 \\ F[i, 1] &=c\times F[i-1, m]+d, &i\neq 1 \\ \end{aligned} F[1,1]F[i,j]F[i,1]=1=a×F[i,j1]+b,=c×F[i1,m]+d,j=1i=1

递推式中 a , b , c , d a,b,c,d a,b,c,d 都是给定的常数。

现在婷婷想知道 F [ n , m ] F[n,m] F[n,m] 的值是多少,请你帮助她。由于最终结果可能很大,你只需要输出 F [ n , m ] F[n,m] F[n,m] 除以 1 0 9 + 7 10^9+7 109+7 的余数。

输入格式

包含一行有六个整数 n , m , a , b , c , d n,m,a,b,c,d n,m,a,b,c,d。意义如题所述。

输出格式

包含一个整数,表示 F [ n , m ] F[n,m] F[n,m] 除以 1 0 9 + 7 10^9+7 109+7 的余数。

输入输出样例 #1

输入 #1

3 4 1 3 2 6

输出 #1

85

说明/提示

【样例1说明】

样例中的矩阵为:

( 1 4 7 10 26 29 32 35 76 79 82 85 ) \begin{pmatrix} 1 & 4 & 7 & 10 \\ 26 & 29 & 32 & 35 \\ 76 & 79 & 82 & 85 \\ \end{pmatrix} 126764297973282103585

数据范围

测试点编号数据范围
1 1 ≤ n , m ≤ 10 1 \le n,m \le 10 1n,m10 1 ≤ a , b , c , d ≤ 1000 1 \le a,b,c,d \le 1000 1a,b,c,d1000
2 1 ≤ n , m ≤ 100 1 \le n,m \le 100 1n,m100 1 ≤ a , b , c , d ≤ 1000 1 \le a,b,c,d \le 1000 1a,b,c,d1000
3 1 ≤ n , m ≤ 1 0 3 1 \le n,m \le 10^3 1n,m103 1 ≤ a , b , c , d ≤ 1 0 9 1 \le a,b,c,d \le 10^9 1a,b,c,d109
4 1 ≤ n , m ≤ 1 0 3 1 \le n,m \le 10^3 1n,m103 1 ≤ a , b , c , d ≤ 1 0 9 1 \le a,b,c,d \le 10^9 1a,b,c,d109
5 1 ≤ n , m ≤ 1 0 9 1 \le n,m \le 10^9 1n,m109 1 ≤ a = c ≤ 1 0 9 1 \le a = c \le 10^9 1a=c109 1 ≤ b = d ≤ 1 0 9 1 \le b = d \le 10^9 1b=d109
6 1 ≤ n , m ≤ 1 0 9 1 \le n,m \le 10^9 1n,m109 a = c = 1 a = c = 1 a=c=1 1 ≤ b , d ≤ 1 0 9 1 \le b,d \le 10^9 1b,d109
7 1 ≤ n , m , a , b , c , d ≤ 1 0 9 1 \le n,m,a,b,c,d \le 10^9 1n,m,a,b,c,d109
8 1 ≤ n , m , a , b , c , d ≤ 1 0 9 1 \le n,m,a,b,c,d \le 10^9 1n,m,a,b,c,d109
9 1 ≤ n , m , a , b , c , d ≤ 1 0 9 1 \le n,m,a,b,c,d \le 10^9 1n,m,a,b,c,d109
10 1 ≤ n , m , a , b , c , d ≤ 1 0 9 1 \le n,m,a,b,c,d \le 10^9 1n,m,a,b,c,d109
11 1 ≤ n , m ≤ 1 0 1 000 1 \le n,m \le 10^{1\,000} 1n,m101000 a = c = 1 a = c = 1 a=c=1 1 ≤ b , d ≤ 1 0 9 1 \le b,d \le 10^9 1b,d109
12 1 ≤ n , m ≤ 1 0 1 000 1 \le n,m \le 10^{1\,000} 1n,m101000 1 ≤ a = c ≤ 1 0 9 1 \le a = c \le 10^9 1a=c109 1 ≤ b = d ≤ 1 0 9 1 \le b = d \le 10^9 1b=d109
13 1 ≤ n , m ≤ 1 0 1 000 1 \le n,m \le 10^{1\,000} 1n,m101000 1 ≤ a , b , c , d ≤ 1 0 9 1 \le a,b,c,d \le 10^9 1a,b,c,d109
14 1 ≤ n , m ≤ 1 0 1 000 1 \le n,m \le 10^{1\,000} 1n,m101000 1 ≤ a , b , c , d ≤ 1 0 9 1 \le a,b,c,d \le 10^9 1a,b,c,d109
15 1 ≤ n , m ≤ 1 0 20 000 1 \le n,m \le 10^{20\,000} 1n,m1020000 1 ≤ a , b , c , d ≤ 1 0 9 1 \le a,b,c,d \le 10^9 1a,b,c,d109
16 1 ≤ n , m ≤ 1 0 20 000 1 \le n,m \le 10^{20\,000} 1n,m1020000 1 ≤ a , b , c , d ≤ 1 0 9 1 \le a,b,c,d \le 10^9 1a,b,c,d109
17 1 ≤ n , m ≤ 1 0 1 000 000 1 \le n,m \le 10^{1\,000\,000} 1n,m101000000 a = c = 1 a = c = 1 a=c=1 1 ≤ b , d ≤ 1 0 9 1 \le b,d \le 10^9 1b,d109
18 1 ≤ n , m ≤ 1 0 1 000 000 1 \le n,m \le 10^{1\,000\,000} 1n,m101000000 1 ≤ a = c ≤ 1 0 9 1 \le a = c \le 10^9 1a=c109 1 ≤ b = d ≤ 1 0 9 1 \le b = d \le 10^9 1b=d109
19 1 ≤ n , m ≤ 1 0 1 000 000 1 \le n,m \le 10^{1\,000\,000} 1n,m101000000 1 ≤ a , b , c , d ≤ 1 0 9 1 \le a,b,c,d \le 10^9 1a,b,c,d109
20 1 ≤ n , m ≤ 1 0 1 000 000 1 \le n,m \le 10^{1\,000\,000} 1n,m101000000 1 ≤ a , b , c , d ≤ 1 0 9 1 \le a,b,c,d \le 10^9 1a,b,c,d109

矩阵快速幂

令x = F[1,1],mat1 = {{x,1}} mat2= {{a,b}{0,1}} matAns= mat1 × \times × mat2,F[1,2]= matAns[0]
同理 mat3= mat2n-1,F[1,n]=mat1 × \times ×mat3
mat4 = {{c,d},{0,1}} mat5= mat4 × \times ×mat3
F[2,1] 可以通过 mat1 × \times × mat5 求得。
即F[m,n] = {{1,0}} × \times × mat3 × \times × mat5m-1
由于n是大整数,故用10进制的矩阵指数幂
mat2 $\rightarrow mat3
mat4 $times mat3 = mat5 , 求mat5的m-1方。

错误展示

 auto mat2 = multiply222(mat.data(), mat.data());auto mat3 = multiply222(mat.data(), mat2.data());auto mat9 = multiply222(mat3.data(), mat3.data());mat = multiply222(mat.data(), mat9.data());

错误原因:mat3*mat3=mat6,不是mat9。

代码

核心代码

#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include <array>#include <bitset>
using namespace std;template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {in >> pr.first >> pr.second;return in;
}template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t);return in;
}template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);return in;
}template<class T = int>
vector<T> Read() {int n;scanf("%d", &n);vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}template<class T = int>
vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}template<int N = 1'000'000>
class COutBuff
{
public:COutBuff() {m_p = puffer;}template<class T>void write(T x) {int num[28], sp = 0;if (x < 0)*m_p++ = '-', x = -x;if (!x)*m_p++ = 48;while (x)num[++sp] = x % 10, x /= 10;while (sp)*m_p++ = num[sp--] + 48;AuotToFile();}void writestr(const char* sz) {strcpy(m_p, sz);m_p += strlen(sz);AuotToFile();}inline void write(char ch){*m_p++ = ch;AuotToFile();}inline void ToFile() {fwrite(puffer, 1, m_p - puffer, stdout);m_p = puffer;}~COutBuff() {ToFile();}
private:inline void AuotToFile() {if (m_p - puffer > N - 100) {ToFile();}}char  puffer[N], * m_p;
};template<int N = 1'000'000>
class CInBuff
{
public:inline CInBuff() {}inline CInBuff<N>& operator>>(char& ch) {FileToBuf();ch = *S++;return *this;}inline CInBuff<N>& operator>>(int& val) {FileToBuf();int x(0), f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);val = f ? -x : x; S++;//忽略空格换行		return *this;}inline CInBuff& operator>>(long long& val) {FileToBuf();long long x(0); int f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);val = f ? -x : x; S++;//忽略空格换行return *this;}template<class T1, class T2>inline CInBuff& operator>>(pair<T1, T2>& val) {*this >> val.first >> val.second;return *this;}template<class T1, class T2, class T3>inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {*this >> get<0>(val) >> get<1>(val) >> get<2>(val);return *this;}template<class T1, class T2, class T3, class T4>inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);return *this;}template<class T = int>inline CInBuff& operator>>(vector<T>& val) {int n;*this >> n;val.resize(n);for (int i = 0; i < n; i++) {*this >> val[i];}return *this;}template<class T = int>vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {*this >> ret[i];}return ret;}template<class T = int>vector<T> Read() {vector<T> ret;*this >> ret;return ret;}
private:inline void FileToBuf() {const int canRead = m_iWritePos - (S - buffer);if (canRead >= 100) { return; }if (m_bFinish) { return; }for (int i = 0; i < canRead; i++){buffer[i] = S[i];//memcpy出错			}m_iWritePos = canRead;buffer[m_iWritePos] = 0;S = buffer;int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);if (readCnt <= 0) { m_bFinish = true; return; }m_iWritePos += readCnt;buffer[m_iWritePos] = 0;S = buffer;}int m_iWritePos = 0; bool m_bFinish = false;char buffer[N + 10], * S = buffer;
};class CMatMul22
{
public:CMatMul22(long long llMod = 1e9 + 7) :m_llMod(llMod) {}// 矩阵乘法std::array<long long,4> multiply222( const long long * a, const  long long* b) {std::array<long long, 4> res;res[0] = (a[0] * b[0] + a[1] * b[2]) % m_llMod;res[1] = (a[0] * b[1] + a[1] * b[3]) % m_llMod;res[2] = (a[2] * b[0] + a[3] * b[2]) % m_llMod;res[3] = (a[2] * b[1] + a[3] * b[3]) % m_llMod;return res;}std::array<long long, 2> multiply122(const long long* a, const  long long* b) {std::array<long long, 2> res;res[0] = (a[0] * b[0] + a[1] * b[2]) % m_llMod;res[1] = (a[0] * b[1] + a[1] * b[3]) % m_llMod;return res;}void  pow22(array<long long, 4>& res, array<long long, 4> mat,  long long n ) {for (; n; n /= 2) {if (1 & n) {res = multiply222(res.data(), mat.data());				}mat = multiply222(mat.data(), mat.data());}}array<long long, 4> pow22(array<long long, 4> mat, const string& str) {array<long long, 4> ans = { 1,0,0,1 };for (int i = str.length() - 1; i >= 0; i--) {const int t = str[i] - '0';pow22(ans, mat, t);	auto mat2 = multiply222(mat.data(), mat.data());auto mat4 = multiply222(mat2.data(), mat2.data());auto mat8 = multiply222(mat4.data(), mat4.data());mat = multiply222(mat2.data(), mat8.data());}return ans;}
protected:const  long long m_llMod;
};typedef unsigned long long ull;
typedef unsigned int uint;
typedef long long ll;template<long long MOD = 1000000007, class T1 = int, class T2 = long long>
class C1097Int
{
public:C1097Int(T1 iData = 0) :m_iData(iData% MOD){}C1097Int(T2 llData) :m_iData(llData% MOD) {}C1097Int  operator+(const C1097Int& o)const{return C1097Int(((T2)m_iData + o.m_iData) % MOD);}C1097Int& operator+=(const C1097Int& o){m_iData = ((T2)m_iData + o.m_iData) % MOD;return *this;}C1097Int& operator-=(const C1097Int& o){m_iData = ((T2)MOD + m_iData - o.m_iData) % MOD;return *this;}C1097Int  operator-(const C1097Int& o){return C1097Int(((T2)MOD + m_iData - o.m_iData) % MOD);}C1097Int  operator*(const C1097Int& o)const{return((T2)m_iData * o.m_iData) % MOD;}C1097Int& operator*=(const C1097Int& o){m_iData = ((T2)m_iData * o.m_iData) % MOD;return *this;}C1097Int  operator/(const C1097Int& o)const{return *this * o.PowNegative1();}C1097Int& operator/=(const C1097Int& o){*this /= o.PowNegative1();return *this;}bool operator==(const C1097Int& o)const{return m_iData == o.m_iData;}bool operator<(const C1097Int& o)const{return m_iData < o.m_iData;}C1097Int pow(T2 n)const{C1097Int iRet = (T1)1, iCur = *this;while (n){if (n & 1){iRet *= iCur;}iCur *= iCur;n >>= 1;}return iRet;}C1097Int PowNegative1()const{return pow(MOD - 2);}T1 ToInt()const{return ((T2)m_iData + MOD) % MOD;}
private:T1 m_iData = 0;;
};class Solution {
public:int Ans(string& strR, string& strC, int a, int b, int c, int d) {Sub(strR); Sub(strC);array<long long, 4> matAB = { {a,0,b,1} }, matCD = { {c,0,d,1} };array<long long, 4> unit = { {1,0,0,1} };CMatMul22 matMul;auto mat3 = matMul.pow22(matAB, strC);auto mat5 = matMul.multiply222(matCD.data(), mat3.data());	auto ans = matMul.multiply122(array<long long, 2>{ {1, 1}}.data(), mat3.data());mat5 = matMul.pow22( mat5, strR);ans = matMul.multiply122(ans.data(), mat5.data());return ans[0] % ((int)1e9 + 7);}void Sub(string& str) {for (int i = str.length() - 1; i >= 0; i--) {if ('0' == str[i]) { str[i] = '9'; }else { str[i]--; break; }}}
};int main() {
#ifdef _DEBUGfreopen("a.in", "r", stdin);
#endif // DEBUG	ios::sync_with_stdio(0);string strR, strC;int a, b, c, d;cin >> strR >> strC >> a >> b >> c >> d;
#ifdef _DEBUG		//printf("T=%lld,", T);//Out(ks, "ks=");//Out(edge, ",edge=");/*Out(edge, "edge=");Out(que, "que=");*/
#endif // DEBUG	auto res = Solution().Ans(strR,strC,a,b,c,d);cout << res;return 0;
}

单元测试

TEST_METHOD(TestMethod1){auto res = Solution().Ans(string("3"),string("4"), 1, 3, 2 ,6);AssertEx(85, res);}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com