【矩阵快速幂】P2100 凌乱的地下室|省选-
本文涉及知识点
【矩阵快速幂】封装类及测试用例及样例
P2100 凌乱的地下室
题目描述
小Z家的地下室里并排放n个小方块(小Z是一位MC狂热爱好者,喜欢用小方块装饰他家的地下室),并且每个方块都不一样(小Z喜欢各不相同的东西),比如有草方块、大理石、黑曜石等。
小Z喜欢以一种特殊的顺序摆放这些小方块,比如:草方块、大理石、黑曜石。一天,小D帮助小Z整理地下室,可是智商捉急的小D将所有小方块搬出来后忘记了它们原来的具体位置,凭着模糊的印象,他可能把原来放在第i个位置上的小方块放到第(i-1)、i、(i+1)个位置中的任意一个上(当然,第1个不可能放到第0个位置上,第n个不可能放到第(n+1)个位置上),比如(对应上面那个例子):大理石、草方块、黑曜石。
小Z是一个心胸宽广的人,他希望计算一下小D一共会有几种可能的摆放结果,并不追究小D的责任(追究了只会更乱……)。由于他自己的智商也比较捉急,所以如果答案很大的话他只想看到最后的8位(前导零就不要给他看了)。
输入格式
一行,一个正整数n,代表小Z家的地下室一共有n个小方块。
输出格式
一行,一个正整数,表示小D一共有几种可能的摆放结果,只输出后8位,前导零不输出。
输入输出样例 #1
输入 #1
3
输出 #1
3
输入输出样例 #2
输入 #2
987
输出 #2
223731
说明/提示
【样例解释1】
接着题目中的例子,一共有3种:(草方块,大理石,黑曜石)、(大理石,草方块,黑曜石)、(草方块,黑曜石,大理石)。
【样例解释2】
一共有……00223731种摆放结果,由于前导零不输出,因此输出223731。
【数据规模】
一共有50个测试点。
其中第1~15个:1<n<=10^6
其中第16~25个:106<n<=1016
其中第26~50个:1016<n<=101000
【时空限制】
0.2s/64MB
状态机动态规划+10进制矩阵快速幂
方块编号从1到n改成0到n-1。
dp[i]记录位置[0…i]放置了砖块[0…i]的方案数,有如下两种情况:
一,砖块i放在位置i。dp[i]+= dp[i-1]。
二,砖块i不放在i,一定放在i-1。且砖块i-1一定放在i。即dp[i] += dp[i-2]。
时间复杂度:O(n) n = 101000,远远超时。
用矩阵指数幂。
pre = {dp[i-1],dp[i]} 初始值{1,1}cur = {dp[i],dp[i+1]}。
n等于1,返回1。
prematn,答案是ans[0]。
mat = {{0,1},{1,1}}
时间复杂度:O(Mlongm) m是大整数的位数,大约100。logn大约4000。
可以不转高精度大数,直接10进制。
res = pre。从低位到高位处理:
{t = s[i]-‘0’;
res = resmatt
mat = mat10}
时间复杂度:O(100010)
注意:
本题mod超过3e9,故a b+c* d后,无符号long long 会溢出。
无符号的减法不能a-b,如果结果小于0,在加MOD。必须:a+MOD-b
核心代码
#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 <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;
};
template<class T = long long>
class CMatMul
{
public:
CMatMul(T llMod = 1e9 + 7) :m_llMod(llMod) {}
// 矩阵乘法
vector<vector<T>> multiply(const vector<vector<T>>& a, const vector<vector<T>>& b) {
const int r = a.size(), c = b.front().size(), iK = a.front().size();
assert(iK == b.size());
vector<vector<T>> ret(r, vector<T>(c));
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
for (int k = 0; k < iK; k++)
{
ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]) % m_llMod;
}
}
}
return ret;
}
// 矩阵快速幂
vector<vector<T>> pow(const vector<vector<T>>& a, vector<vector<T>> b, T n) {
vector<vector<T>> res = a;
for (; n; n /= 2) {
if (n % 2) {
res = multiply(res, b);
}
b = multiply(b, b);
}
return res;
}
vector<vector<T>> TotalRow(const vector<vector<T>>& a)
{
vector<vector<T>> b(a.front().size(), vector<T>(1, 1));
return multiply(a, b);
}
protected:
const T 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 str) {
vector<vector<long long>> pre = { {1,1} };
vector<vector<long long>> mat = { {0,1},{1,1} };
CMatMul<> matMul(100'000'000);
for (int i = str.length() - 1; i >= 0; i--) {
const int t = str[i] - '0';
pre = matMul.pow(pre, mat, t);
mat = matMul.pow(mat, mat, 9);
}
return pre[0][0];
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
ios::sync_with_stdio(0);
char ch;
vector<char> v;
while (cin >> ch) {
v.emplace_back(ch);
}
v.emplace_back(0);
#ifdef _DEBUG
//printf("T=%lld,", T);
//Out(ks, "ks=");
//Out(edge, ",edge=");
/*Out(edge, "edge=");
Out(que, "que=");*/
#endif // DEBUG
auto res = Solution().Ans(v.data());
cout << res;
return 0;
}
单元测试
string str;
TEST_METHOD(TestMethod1)
{
auto res = Solution().Ans("1");
AssertEx(1, res);
}
TEST_METHOD(TestMethod2)
{
auto res = Solution().Ans("2");
AssertEx(2, res);
}
TEST_METHOD(TestMethod3)
{
auto res = Solution().Ans("0");
AssertEx(1, res);
}
TEST_METHOD(TestMethod4)
{
auto res = Solution().Ans("3");
AssertEx(3, res);
}
TEST_METHOD(TestMethod5)
{
auto res = Solution().Ans("987");
AssertEx(223731, 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++**实现。