1009: [HNOI2008]GT考试
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4266 Solved: 2616
[Submit][Status][Discuss]
Description
阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字。
他的不吉利数学A1A2...Am(0<=Ai<=9)有M位,不出现是指X1X2...Xn中没有恰好一段等于A1A2...Am. A1和X1可以为
0
Input
第一行输入N,M,K.接下来一行输入M位的数。 N<=10^9,M<=20,K<=1000
Output
阿申想知道不出现不吉利数字的号码有多少种,输出模K取余的结果.
Sample Input
4 3 100
111
111
Sample Output
81
HINT
析:先用KMP把不能出现的串先匹配出来,然后再对每种状态进行计算,dp[i][j] 表示已经匹配到 i 并转移到 j 有多少种,但是时间复杂度太高,所以要用矩阵快速幂来进行优化。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #include <numeric> #define debug() puts("++++") #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e17; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-3; const int maxn = 20 + 10; const int maxm = 3e5 + 10; const int mod = 100003; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, -1, 0, 1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } char s[maxn]; int f[maxn], K; struct Matrix{ int a[maxn][maxn]; int n; Matrix(){ ms(a, 0); } void toOne(){ FOR(i, 0, n) a[i][i] = 1; } Matrix operator * (const Matrix &rhs){ Matrix res; res.n = n; FOR(i, 0, n) FOR(j, 0, n) for(int k = 0; k < n; ++k) res.a[i][j] = (res.a[i][j] + a[i][k] * rhs.a[k][j]) % K; return res; } }; Matrix fast_pow(Matrix a, int n){ Matrix res; res.n = a.n; res.toOne(); while(n){ if(n & 1) res = res * a; n >>= 1; a = a * a; } return res; } int main(){ scanf("%d %d %d", &n, &m, &K); scanf("%s", s); f[0] = f[1] = 0; for(int i = 1; i < m; ++i){ int j = f[i]; while(j && s[j] != s[i]) j = f[j]; f[i+1] = s[j] == s[i] ? j+1: 0; } Matrix x; x.n = m; for(int i = 0; i < m; ++i){ for(int t = 0; t < 10; ++t){ int j = i; while(j && s[j] - '0' != t) j = f[j]; if(s[j] - '0' == t) ++j; ++x.a[i][j]; } } Matrix ans = fast_pow(x, n); int res = 0; for(int i = 0; i < m; ++i) res = (res + ans.a[0][i]) % K; printf("%d ", res); return 0; }