提示:
1. 本題的暴力可以先寫好備着 , 總是有用的。
2. 看到T的範圍 , 我打賭跟快速冪有關……
詳細題解和分析在代碼後:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <complex>
#include <climits>
#include <cassert>
using namespace std;
const int maxn = 120;
const int maxm = 102;
int modu;
typedef long long ll;
struct Matrix
{
int n , m;
int a[maxm][maxm];
Matrix(int n=0 , int m=0){ this->n = n; this->m = m; memset(a , 0 , sizeof a); }
int* operator [] ( int b ) { return a[b]; }
Matrix operator *(Matrix b)
{
Matrix res(n , b.m);
assert(m == b.n);
for(int i=1;i<=n;i++) for(int j=1;j<=b.m;j++) for(int k=1;k<=m;k++) (res[i][j] += a[i][k]*b[k][j]) %= modu;
return res;
}
};
Matrix powMatrix(Matrix a , int n)
{
Matrix res;
res.n = res.m = a.n;
for(int i=1;i<=res.n;i++) res[i][i] = 1;
while(n)
{
if(n&1) res = res*a;
a = a*a;
n >>= 1;
}
return res;
}
int n , m , x , p , t;
string d[maxn][maxn];
Matrix timeFly(Matrix a , int t)
{
int b[maxn][maxn];
for(int i=0;i<t;i++)
{
memset(b , 0 , sizeof b);
for(int j=1;j<=n;j++) for(int k=1;k<=m;k++)
{
char c = d[j][k][i%d[j][k].size()];
if(c == 'C') (b[j][k] += (x+a[j][k])%p) %= p;
if(c == 'U') (b[j-1][k] += a[j][k]) %= p;
if(c == 'D') (b[j+1][k] += a[j][k]) %= p;
if(c == 'L') (b[j][k-1] += a[j][k]) %= p;
if(c == 'R') (b[j][k+1] += a[j][k]) %= p;
}
for(int j=1;j<=n;j++) for(int k=1;k<=m;k++) a[j][k] = b[j][k];
}
return a;
}
void print(Matrix a)
{
for(int i=1;i<=n;i++)
{
for(int j=1,k;j<=m;j=k)
{
for(k=j;k<=m && a[i][k] == a[i][j];k++);
cout<<(k-j)<<" "<<a[i][j]<<(k>m?"\n":" ");
}
}
}
int id(int x , int y) { return (x-1)*m + y; }
__inline Matrix modifyTrans(Matrix a , Matrix b)
{
Matrix res;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)if(a[i][j])
{
int x = (a[i][j]+m-1)/m , y = (a[i][j]%m==0)?m:a[i][j]%m;
res[i][j] = b[x][y];
}
return res;
}
__inline Matrix modifyNow(Matrix a , Matrix b , Matrix c)
{
Matrix res = c;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)if(b[i][j])
{
int x = (b[i][j]+m-1)/m , y = (b[i][j]%m==0)?m:b[i][j]%m;
(res[x][y] += a[i][j]) %= modu;
}
return res;
}
int main()
{
cin>>n>>m>>x>>p>>t;
modu = p;
for(int i=1;i<=n;i++) for(int j=1,k;j<=m;j++) cin>>k>>d[i][j];
//puts("OK");
/*if(t <= 1e4)
{
print(timeFly(Matrix() , t));
return 0;
}
else */
{
//puts("OK");
Matrix now = timeFly(Matrix() , 63) , res;
Matrix trans(n , m);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
{
int x = i , y = j; bool ok = true;
for(int k=0,nx,ny;k<63;k++)
{
char c = d[x][y][k%d[x][y].size()];
if(c == 'C') nx = x , ny = y;
if(c == 'U') nx = x-1 , ny = y;
if(c == 'D') nx = x+1 , ny = y;
if(c == 'L') nx = x , ny = y-1;
if(c == 'R') nx = x , ny = y+1;
x = nx;
y = ny;
if(!x || x>n || !y || y>m) { ok = false; break; }
}
if(ok) trans[i][j] = id(x , y);
}
int req = t/63;
while(req)
{
if(req&1) res = modifyNow(res , trans , now);
now = modifyNow(now , trans , now);
trans = modifyTrans(trans , trans);
req >>= 1;
}
print(timeFly(res , t%63));
}
return 0;
}
題解:
看到T的範圍,博主第一反應,矩陣快速冪?!然後啪啪啪寫了一份矩陣的代碼,仔細看了範圍,貌似只能過8個點的樣子。正解並不是用矩陣來加速而是通過一種類似倍增置換的方法,因爲矩陣是多元關係的轉化,浪費了巨多時間。
如果你僅僅構想出矩陣的解法,說明你沒有發掘問題的特殊性,這玩意是個置換,所以並不需要用關係矩陣來體現問題的(也可以說是壓縮了一個及其稀疏的矩陣),我們只需要用一個
具體來說,由於本題的條件,循環節最長是63,於是我們可以暴力算出