Codeforces 691E Xor-sequences(矩阵快速幂)

E - Xor-sequences
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u


E - Xor-sequences
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 ≤ 1001 ≤ 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

Input
5 2
15 1 2 4 8
Output
13
Input
5 1
15 1 2 4 8
Output
5

dp[k][j] += dp[k - 1][i] (如果a[i]^a[j]中二进制1的格式模3等于0)

其中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;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值