E. Xor-sequences
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 xi  xi  +  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.

Examples
input
5 2
15 1 2 4 8
output
13
input
5 1
15 1 2 4 8
output
5
思路:很好的一道矩阵乘法题,需要建立起模型。先预处理出一个矩阵,第i行j个表示a[i]异或a[j]是否能被3整除,然后再将矩阵计算k-1次幂,最后将矩阵中所有元素加起来。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#define mo 1000000007
using namespace std;
struct Matrix
{
long long a[][];
};
long long n,k,a[];
Matrix st,ans;
Matrix mul(Matrix a,Matrix b)
{
Matrix c;
int i,j,k;
//<F5>a.a[1][1]=1;
for (i=;i<=n;i++)
for (j=;j<=n;j++)
{
c.a[i][j]=;
for (k=;k<=n;k++)
c.a[i][j]=(c.a[i][j]+(a.a[i][k]*b.a[k][j]%mo))%mo;
}
return c;
}
Matrix power(Matrix a,long long b)
{
Matrix c;
int i;
memset(c.a,,sizeof(c.a));
for (i=;i<=n;i++) c.a[i][i]=;
while (b)
{
if (b&) c=mul(c,a);
b>>=;
a=mul(a,a);
}
return c;
}
bool can(long long x)
{
long long ret=;
while (x)
{
ret+=(x&);
x>>=;
}
return (ret%==);
}
int main()
{
scanf("%lld%lld",&n,&k);
int i,j;
for (i=;i<=n;i++) scanf("%lld",&a[i]);
for (i=;i<=n;i++)
for (j=;j<=n;j++) st.a[i][j]=can(a[i]^a[j]);
ans=power(st,k-);
long long ret=;
//cout<<ans.a[i][j]<<endl;
for (i=;i<=n;i++)
for (j=;j<=n;j++) ret=(ret+ans.a[i][j])%mo;
printf("%lld\n",ret);
return ;
}

codeforces 691E(矩阵乘法)的更多相关文章

  1. codeforces 691E 矩阵快速幂+dp

    传送门:https://codeforces.com/contest/691/problem/E 题意:给定长度为n的序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二 ...

  2. Xor-sequences CodeForces - 691E || 矩阵快速幂

    Xor-sequences CodeForces - 691E 题意:在有n个数的数列中选k个数(可以重复选,可以不按顺序)形成一个数列,使得任意相邻两个数异或的结果转换成二进制后其中1的个数是三的倍 ...

  3. Codeforces 400C 矩阵乘法 数学规律

    今天下午Virtual了一套最近的CF题,第三题给TLE了,就跑过去上课了. 这题给定一个由二进制表示的矩阵,当询问3的时候,求矩阵的值,矩阵的值是所有第i行乘以第i列的值的总和,然后还有1 b是翻转 ...

  4. Codeforces 691E题解 DP+矩阵快速幂

    题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...

  5. Codeforces 1106F Lunar New Year and a Recursive Sequence | BSGS/exgcd/矩阵乘法

    我诈尸啦! 高三退役选手好不容易抛弃天利和金考卷打场CF,结果打得和shi一样--还因为queue太长而unrated了!一个学期不敲代码实在是忘干净了-- 没分该没分,考题还是要订正的 =v= 欢迎 ...

  6. CodeForces - 691E Xor-sequences 【矩阵快速幂】

    题目链接 http://codeforces.com/problemset/problem/691/E 题意 给出一个长度为n的序列,从其中选择k个数 组成长度为k的序列,因为(k 有可能 > ...

  7. Codeforces 506E - Mr. Kitayuta's Gift(神仙矩阵乘法)

    Codeforces 题目传送门 & 洛谷题目传送门 神仙题 %%%%%%%%%%%%% u1s1 感觉这道题风格很省选( 下记 \(m=|s|\),首先探讨 \(n+m\) 为偶数的情形. ...

  8. Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)

    Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...

  9. Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...

随机推荐

  1. FreeMarker-网页静态化

    网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类的频道. 网页静态化技术和缓存技术的共同点都是为了减轻数据库的访问压力,但是具体的应用场景不同,缓存比较适合小规 ...

  2. eclipse debug java 源码

    当我们需要研究java SE的时候,debug 源码是个不错的选择,可以帮助我们清楚了解java 封装jar包的具体实现. 因为oracle 提供的源码jar包为了节省空间,所以没有将调试信息一起打包 ...

  3. CAS4.0 server 环境的搭建

    1.上cas的官网下载cas server 官网地址:https://github.com/Jasig/cas/releases,下载好后 解压下载的 cas-server-4.0.0-release ...

  4. 调试SQL Server的存储过程及用户定义函数

    分类: 数据库管理 2005-06-03 13:57 9837人阅读 评论(5) 收藏 举报 sql server存储vb.net服务器sql语言 1.在查询分析器中调试 查询分析器中调试的步骤如下: ...

  5. Java对Redis基本使用

    1 引入jar包 java是通过Jedis对redis进行操作的,首先引入jedis.jar <dependency> <groupId>redis.clients</g ...

  6. 翻译:高级t - sql第1级的阶梯:使用交叉连接来引入高级t - sql

    高级t - sql第1级的阶梯:使用交叉连接来引入高级t - sql 源于:格雷戈里·拉森,2016/02/19(首次出版:2014/12/17 翻译:刘琼滨 谢雪妮 徐雅莉 赖慧芳 链接:http: ...

  7. swift不同地方 命名空间module iOS

    Objective-C没有命名空间,为了避免冲突,Objective-C的类型一般都会加上两到三个字母的前缀,比如Apple保留的NS和UI前缀,各个系统框架的前缀,各个系统框架的前缀SK(Store ...

  8. delphi 7 生成 调用 bat文件的exe文件

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  9. Perl字符集就是方括号(或称中括号)里一连串可能的字符,只匹配单一字符,该单一字符可以是字符集里的任何一个,“-”在字符集里有特殊含义:表示某个范围的字符。而字符集意外的连字符不具有特殊意义。

    Perl字符集就是方括号(或称中括号)里一连串可能的字符,只匹配单一字符,该单一字符可以是字符集里的任何一个,“-”在字符集里有特殊含义:表示某个范围的字符.而字符集意外的连字符不具有特殊意义.

  10. 第3节 mapreduce高级:12、mapreduce相关的参数调整

    5.1 多job串联 一个稍复杂点的处理逻辑往往需要多个mapreduce程序串联处理,多job的串联可以借助mapreduce框架的JobControl实现 示例代码: ControlledJob ...