Problem - 691E - Codeforces
题意:
思路:
有人只会暴力DP忘记矩阵快速幂怎么写了
Code:
#include <bits/stdc++.h>
#define int long long
using i64 = long long;
using namespace std;
const int N = 1e2 + 10;
const int mod = 1e9 + 7;
int n, k;
int a[N];
struct Matrix {
int m[N][N];
void init() {
for (int i = 1; i <= n; i ++) {
m[i][i] = 1;
}
}
void clr() {
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
m[i][j] = 0;
}
}
}
};
Matrix Mul(Matrix a, Matrix b) {
Matrix ans;
ans.clr();
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
for (int k = 1; k <= n; k ++) {
ans.m[i][j] += a.m[i][k] * b.m[k][j];
ans.m[i][j] %= mod;
}
}
}
return ans;
}
Matrix ksm(Matrix a, int b) {
Matrix res;
res.clr();
res.init();
while(b) {
if (b & 1) res = Mul(res, a);
a = Mul(a, a);
b >>= 1;
}
return res;
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n ;i ++) {
cin >> a[i];
}
Matrix Base;
Base.clr();
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
if (__builtin_popcountll(a[i] ^ a[j]) % 3 == 0) {
Base.m[i][j] = 1;
}
}
}
Base = ksm(Base, k - 1);
int ans = 0;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
ans += Base.m[i][j];
ans %= mod;
}
}
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while(t --) {
solve();
}
return 0;
}