Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(dp + 矩阵快速幂)

时间:2021-07-27 18:20:12

题意:

b109,n105,
MOD x=k,x,k100

分析:


dp[i][j]:=xi,(10+k),xj
b,ans=dpb[0][k]
dp1[i][j],,dp2[i][k]=9j=0dp1[i][j]dp1[j][k],

f[i][mod]:=i,xj
dp1[i][j],ans=f[b][k]=f[0][0]dpb[0][k],
,O(x3logb)
,b,O(x2logb)

给出第一种代码:

//
// Created by TaoSama on 2016-01-31
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 100 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

typedef long long LL;

struct Matrix {
int row, col;
LL mat[N][N];
void init(int row, int col, bool one = false) {
this->row = row; this->col = col;
memset(mat, 0, sizeof mat);
if(!one) return;
for(int i = 0; i < row; ++i) mat[i][i] = 1;
}
Matrix operator* (const Matrix& rhs) {
Matrix ret; ret.init(row, rhs.col);
for(int k = 0; k < col; ++k) {
for(int i = 0; i < row; ++i) {
if(mat[i][k] == 0) continue;
for(int j = 0; j < rhs.col; ++j) {
if(rhs.mat[k][j] == 0) continue;
ret.mat[i][j] += mat[i][k] * rhs.mat[k][j] % MOD;
ret.mat[i][j] %= MOD;
}
}
}
return ret;
}
Matrix operator^ (LL n) {
Matrix ret, x = *this;
ret.init(row, col, 1);
while(n) {
if(n & 1) ret = ret * x;
x = x * x;
n >>= 1;
}
return ret;
}
} A;

int n, b, k, x;
int cnt[10];

int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

while(scanf("%d%d%d%d", &n, &b, &k, &x) == 4) {
memset(cnt, 0, sizeof cnt);
for(int i = 1; i <= n; ++i) {
int z; scanf("%d", &z);
++cnt[z];
}
printf("%d\n", sizeof(long double));
A.init(x, x);
for(int i = 0; i < x; ++i)
for(int j = 0; j < 10; ++j)
A.mat[i][(i * 10 + j) % x] += cnt[j];

Matrix ans = A ^ b;
printf("%I64d\n", ans.mat[0][k]);
}
return 0;
}