Educational Codeforces Round 14 E.Xor-sequences

时间:2023-03-08 17:20:27

题目链接

分析:K很大,以我现有的极弱的知识储备,大概应该是快速幂了。。。怎么考虑这个快速幂呢,用到了dp的思想。定义dp[i][j]表示从a[i]到a[j]的合法路径数。那么递推式就是dp[i][j]=∑k(dp[i][k]∗dp[k][j])。每次进行这样一次计算,那么序列的长度就会增加一,因此只要将这个式子做k次就行了。怎么满足相邻两个数异或值的1的个数为3倍数呢?这就是用到矩阵的时候了。枚举ij,建立一个N∗N的矩阵,当a[i]⊗a[j]为3的倍数,m[i][j]为1,否则为零。再考虑到矩阵的乘法其实和刚才的dp递推式是一样的??!!因此只要将矩阵乘K−1次就行了。

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31; /*****************************************************/ const int maxn = 1e2 + 5;
LL p[maxn];
struct Mat {
int n;
LL a[105][105];
Mat(int _n = 0) : n(_n) {mem(a, 0);}
Mat operator *(const Mat &rhs) const {
int n = rhs.n;
Mat c(n);
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
for (int k = 0; k < n; k ++)
c.a[i][j] = (c.a[i][j] + a[i][k] * rhs.a[k][j] % mod) % mod;
return c;
}
};
int count(LL k) {
int ans = 0;
while (k) {
if (k & 1) ans ++;
k >>= 1;
}
return ans;
}
Mat qpow(Mat A, LL b) {
int n = A.n;
Mat c(n);
for (int i = 0; i < n; i ++) c.a[i][i] = 1;
while (b) {
if (b & 1) c = c * A;
b >>= 1;
A = A * A;
}
return c;
}
int main(int argc, char const *argv[]) {
int N;
LL K;
cin>>N>>K;
for (int i = 0; i < N; i ++) cin>>p[i];
Mat A(N);
for (int i = 0; i < N; i ++)
for (int j = 0; j < N; j ++)
if (count(p[i] ^ p[j]) % 3 == 0)
A.a[i][j] = 1;
A = qpow(A, K - 1);
LL ans = 0;
for (int i = 0; i < N; i ++)
for (int j = 0; j < N; j ++)
ans = (ans + A.a[i][j]) % mod;
cout<<ans<<endl;
return 0;
}