codeforces 691E(矩阵乘法)
3 seconds
256 megabytes
standard input
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.
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).
Print the only integer c — the number of "xor-sequences" of length k modulo 109 + 7.
- 5 2
15 1 2 4 8
- 13
- 5 1
15 1 2 4 8
- 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(矩阵乘法)的更多相关文章
- codeforces 691E 矩阵快速幂+dp
传送门:https://codeforces.com/contest/691/problem/E 题意:给定长度为n的序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二 ...
- Xor-sequences CodeForces - 691E || 矩阵快速幂
Xor-sequences CodeForces - 691E 题意:在有n个数的数列中选k个数(可以重复选,可以不按顺序)形成一个数列,使得任意相邻两个数异或的结果转换成二进制后其中1的个数是三的倍 ...
- Codeforces 400C 矩阵乘法 数学规律
今天下午Virtual了一套最近的CF题,第三题给TLE了,就跑过去上课了. 这题给定一个由二进制表示的矩阵,当询问3的时候,求矩阵的值,矩阵的值是所有第i行乘以第i列的值的总和,然后还有1 b是翻转 ...
- Codeforces 691E题解 DP+矩阵快速幂
题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...
- Codeforces 1106F Lunar New Year and a Recursive Sequence | BSGS/exgcd/矩阵乘法
我诈尸啦! 高三退役选手好不容易抛弃天利和金考卷打场CF,结果打得和shi一样--还因为queue太长而unrated了!一个学期不敲代码实在是忘干净了-- 没分该没分,考题还是要订正的 =v= 欢迎 ...
- CodeForces - 691E Xor-sequences 【矩阵快速幂】
题目链接 http://codeforces.com/problemset/problem/691/E 题意 给出一个长度为n的序列,从其中选择k个数 组成长度为k的序列,因为(k 有可能 > ...
- Codeforces 506E - Mr. Kitayuta's Gift(神仙矩阵乘法)
Codeforces 题目传送门 & 洛谷题目传送门 神仙题 %%%%%%%%%%%%% u1s1 感觉这道题风格很省选( 下记 \(m=|s|\),首先探讨 \(n+m\) 为偶数的情形. ...
- Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)
Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...
- Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...
随机推荐
- 如何成为一名优秀的 iOS 开发工程师
如果你是一位专业的iOS开发工程师,你应该为自己感到自豪.因为你能在强大的iOS系统下,一展身手实现自己和他人的想法,这是一件令人无比激动的事情. 作为一名iOS开发工程师,你一定想成为行业的佼佼者. ...
- 2019最新Android面试题
原文链接:https://blog.csdn.net/wen_haha/article/details/88362469版权声明:本文为博主原创文章,转载请附上博文链接! 前言 金三银四到来了,找工作 ...
- robotframework + python2.7.9 + selenium 2.44.0 + selenium2library1.7 测试环境搭建成功!
真心不容易呀!开源软件搭建挺麻烦的,各种组件未必要使用最新的版本:有些最新版本反而不兼容.需要仔细看官方说明书来进行搭建(官方网站都是英文),所以闹得重新安装了几次. 先上测试用例通过的图:
- LoadRunner脚本回放与设置
一.runtime setting 1.迭代次数设置与迭代步长(循环间隔时间) 2.日志打印设置 二.实时观看回放 1.动态回放与静态回放(静态回放时,不会有逐行高亮显示:动态回放时高亮显 ...
- EditText自动弹出软键盘
editText.requestFocus() editText.isFocusable = true editText.isFocusableInTouchMode = true val timer ...
- iOS----时间日期处理
时间日期处理 1.NSDateFormatter 日期格式化 ①可以把NSString 类型转为 NSDate类型 举例 把 "2015-08-23 19:46:14" 转为NSD ...
- VBox虚拟机安装debian
决定在win7上装一个Linux虚拟机用作Linux开发学习,虽然win7下已经有了Cygwin,还是想在一个比较完整的环境下.前面装过Ubuntu发现界面太笨重了,考虑重新换一个,同时比较喜欢apt ...
- 【C++】类型转换简述:四种类型转换方式的说明及应用
本文主要简述在C++中四种类型转换的方式:static_cast.reniterpret_cast.const_cast和dynamic_cast. 在介绍C++类型转换方式之前,我们先来看看C语言的 ...
- 浅谈CSS中的定位知识
1,静态定位(static) 表示按照正常定位方案,元素盒按照在文档流中出现的顺序依次格式化: 2,相对定位(relative) 将移动元素盒,但是它在文档流中的原始空间会保留下来: 相对定位元素有如 ...
- (转)Nutz | Nutz项目整合Spring实战
http://blog.csdn.net/evan_leung/article/details/54767143 Nutz项目整合Spring实战 前言 Github地址 背景 实现步骤 加入spri ...