跳到正文

题解

题解:CF107D Crime Management

wangyibo201026约 8 分钟
原文
文章信息与版本

文章信息

发布
2024年11月26日
收录
2026年1月28日
更新
2026年11月8日
篇幅
约 8 分钟
题目
CF107D

版本

  1. v2当前
  2. v1

solution

考虑到所有限制的乘积 ≤123\le 123,n≤1018n \le 10^{18},就想到大概是矩阵乘法了。

首先注意到同一种字符的不同限制不好处理,我们直接模它们的乘积,然后在最后看一下模这些值为 00 的状态的值就好。

肯定是 DP 的,fi,j,k,...f_{i, j, k, ...} 表示到了第 ii 位每种字符的出现次数模限制之后的数,虽然有很多维,但是注意到乘积 ≤123\le 123,所以我的 DP 数组除了第一维都可以被映射到一个不超过 123123 大小的数组上。

然后 n≤1018n \le 10^{18},我们将 DP 数组转化为二维,发现每一次转移本质上相同,可以爆搜把矩阵打出来,矩阵快速幂即可。

code

code:

C++
#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;
}
原文