Description
You are given n integers a1, a2, ..., an.
A sequence of integers x1, x2, ..., xk is called a "xor-sequence" if for every 1 ≤ i ≤ k - 1 the number of ones in the binary representation of the number xixi + 1's is a multiple of 3 and
for all 1 ≤ i ≤ k. The symbol
is used for the binary exclusive or operation.
How many "xor-sequences" of length k exist? Output the answer modulo 109 + 7.
Note if a = [1, 1] and k = 1 then the answer is 2, because you should consider the ones from a as different.
Input
The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1018) — the number of given integers and the length of the "xor-sequences".
The second line contains n integers ai (0 ≤ ai ≤ 1018).
Output
Print the only integer c — the number of "xor-sequences" of length k modulo 109 + 7.
Sample Input
5 2 15 1 2 4 8
13
5 1 15 1 2 4 8
5
其中k代表长度,j代表给出的序列中的位置
由于k很多,但是我们分析可以看出,dp[k][j] = dp[k - 1][i] * a[i][j](a[i][j] 即a[i]^a[j]中二进制1的格式模3是否等于0)
所以可以用矩阵快速幂解决
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
typedef long long LL;
typedef vector<LL>vec;
typedef vector<vec>mat;
const int MAXN = 1e2 + 5;
const LL mod = 1e9 + 7;
mat mul(mat &A, mat &B) {
mat C(A.size(), vec(B[0].size()));
for(int i = 0; i < A.size(); i ++) {
for(int k = 0; k < B.size(); k ++) {
for(int j = 0; j < B[0].size(); j ++) {
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
}
}
}
return C;
}
mat pow(mat A, LL n) {
mat B(A.size(), vec(A.size()));
for(int i = 0; i < A.size(); i ++) {
B[i][i] = 1;
}
while(n > 0) {
if(n & 1) B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
int countbits(LL x) {
int res = 0;
while(x) {
x &= x - 1;
res ++;
}
return res;
}
int n;
LL k, O[MAXN];
int main() {
while(~scanf("%d%I64d", &n, &k)) {
for(int i = 0; i < n; i ++) scanf("%I64d", &O[i]);
mat A(n, vec(n));
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
if(countbits(O[i] ^ O[j]) % 3 == 0) {
A[i][j] = 1;
} else A[i][j] = 0;
}
}
A = pow(A, k - 1);
LL ret = 0;
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
ret = (ret + A[i][j]) % mod;
}
}
printf("%I64d\n", ret);
}
return 0;
}