题解:CF107D Crime Management
wangyibo201026 · · 题解
solution
考虑到所有限制的乘积
首先注意到同一种字符的不同限制不好处理,我们直接模它们的乘积,然后在最后看一下模这些值为
肯定是 DP 的,
然后
code
code:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define lep( i, l, r ) for ( int i = ( l ); i <= ( r ); ++ i )
#define rep( i, r, l ) for ( int i = ( r ); i >= ( l ); -- i )
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair < int, int > pii;
char _c; bool _f; template < class type > inline void read ( type &x ) {
_f = 0, x = 0;
while ( _c = getchar (), !isdigit ( _c ) ) if ( _c == '-' ) _f = 1;
while ( isdigit ( _c ) ) x = x * 10 + _c - '0', _c = getchar (); if ( _f ) { x = -x; }
}
template < class type > inline void chkmin ( type &x, type y ) { x = ( x <= y ? x : y ); }
template < class type > inline void chkmax ( type &x, type y ) { x = ( x >= y ? x : y ); }
const int N = 1e3 + 5;
const int M = 128;
const int mod = 12345;
int n, m, cnt, ans;
int tong[N];
vector < int > chifan[N];
map < vector < int >, int > mp;
vector < int > lim, tmp;
struct matrix {
int n, m;
int a[M][M];
matrix () {
memset ( a, 0, sizeof ( a ) );
}
} base, p;
matrix mul ( matrix a, matrix b ) {
matrix c;
c.n = a.n, c.m = b.m;
for ( int k = 1; k <= a.m; k ++ ) {
for ( int i = 1; i <= a.n; i ++ ) {
for ( int j = 1; j <= b.m; j ++ ) {
c.a[i][j] += a.a[i][k] * b.a[k][j] % mod;
c.a[i][j] %= mod;
}
}
}
return c;
}
matrix fast_pow ( matrix a, int b ) {
matrix res;
res.n = a.n, res.m = a.m;
for ( int i = 1; i <= res.n; i ++ ) {
res.a[i][i] = 1;
}
while ( b ) {
if ( b & 1 ) {
res = mul ( res, a );
}
b >>= 1;
a = mul ( a, a );
}
return res;
}
void dfs ( int x ) {
if ( x == lim.size () ) {
mp[tmp] = ++ cnt;
return ;
}
for ( int i = 0; i < lim[x]; i ++ ) {
tmp.push_back ( i );
dfs ( x + 1 );
tmp.pop_back ();
}
}
void dfs2 ( int x, int id ) {
if ( x == lim.size () ) {
int u = mp[tmp], tmpp = tmp[id];
tmp[id] = ( tmp[id] + 1 ) % lim[id];
int v = mp[tmp];
tmp[id] = tmpp;
base.a[u][v] ++;
return ;
}
for ( int i = 0; i < lim[x]; i ++ ) {
tmp.push_back ( i );
dfs2 ( x + 1, id ) ;
tmp.pop_back ();
}
}
void dfs3 ( int x ) {
if ( x == lim.size () ) {
int cnt = 0, f = 1;
for ( int i = 'A'; i <= 'Z'; i ++ ) {
if ( tong[i] ) {
int flag = 1;
for ( int k : chifan[i] ) {
if ( tmp[cnt] % k == 0 ) {
flag = 0;
break;
}
}
if ( flag ) {
f = 0;
break;
}
cnt ++;
}
}
if ( f == 1 ) {
ans += p.a[1][mp[tmp]];
ans %= mod;
}
return ;
}
for ( int i = 0; i < lim[x]; i ++ ) {
tmp.push_back ( i );
dfs3 ( x + 1 );
tmp.pop_back ();
}
}
void Solve () {
ios :: sync_with_stdio ( false );
cin.tie ( 0 ), cout.tie ( 0 );
cin >> n >> m;
for ( int i = 1; i <= m; i ++ ) {
char c;
int k;
cin >> c >> k;
if ( !tong[c] ) {
tong[c] = k;
}
else {
tong[c] *= k;
}
chifan[c].push_back ( k );
}
for ( int i = 'A'; i <= 'Z'; i ++ ) {
if ( tong[i] ) {
lim.push_back ( tong[i] );
}
}
dfs ( 0 );
int c = 0;
for ( int i = 'A'; i <= 'Z'; i ++ ) {
if ( tong[i] ) {
dfs2 ( 0, c ++ );
}
}
base.n = base.m = cnt, p.n = 1, p.m = cnt;
p.a[1][1] = 1;
base = fast_pow ( base, n );
p = mul ( p, base );
dfs3 ( 0 );
cout << ans;
}
signed main () {
#ifdef judge
freopen ( "Code.in", "r", stdin );
freopen ( "Code.out", "w", stdout );
freopen ( "Code.err", "w", stderr );
#endif
Solve ();
return 0;
}