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 首先考虑怎样对某个固定的串计 ...
随机推荐
- SQL Server 编程入门
一.T—SQL 的组成 1.DML(数据操作语言 Data Manipulation Language) 查询.插入.删除和修改数据库中的数据.SELECT.INSERT.UPDATE.DELETE ...
- ES3之bind方法的实现模拟
扩展Function原型方法,此处用myBind来模拟bind实现 Function.prototype.myBind = function(o /*,args*/){ //闭包无法获取t ...
- 恩智浦Freescale Cortex-A9 迅为IMX6开发板平台初体验
iTOP-i.MX6 开发板预装 Android4.4 系统,采用 9.7 寸(或者 7 寸或者 4.3 寸)IPS 屏 幕,至少 5 点以上触控,操作流畅,无论是高清视频.游戏等都会有上佳的表现,实 ...
- 扩展IHttpHandler
前面提到当请求进入到IIS,IIS转发给ISAPI后会根据文件的后缀名来决定将请求转发给对应的处理程序进行处理,当然一大部分的处理都是交给了AspNet_ISAPI 了.但是,AspNet_ISAPI ...
- 【经验】停止Smart Card服务
Windows+R键调出运行 输入 services.msc 有一项Smart Card的服务找到他->属性->启动类型(设置为禁用 )->确定,然后重新启动服务
- 允许IIS下载无后缀文件及“请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。”的解决方法
1)增加MIME类型 ,如下 application/octet-stream 2)注意是"." , ".*"则适用于任何有文件后缀时使用,无后缀请不要加*
- phphstrom改变项目中文件排列方式
1.View>Tool Win dows>Project 效果图: 2.File->settings (Ctrl+Alt+S)-> Editor->General-> ...
- div 可视化区域弹窗居中
效果: css: .div_alt { position: fixed; border-radius: 5px; top: 50%; left: 50%; width: auto; min-width ...
- 2018湖南省第14届大学生计算机程序设计竞赛 A字符画
Description 读入 w,请输出 2018 的字符画,两个数字之间有 w 个空格.具体格式请参考样例输出. 1 ≤ w ≤ 2018 Input 输入文件只包含 1 个整数 w. Output ...
- 笔试算法题(21):将stack内外颠倒 & 判断扑克牌顺子
出题:要求用递归将一个栈结构的元素内外颠倒: 分析: 本题再次说明系统栈是程序员最好的帮手,但递归度较高所以时间复杂度较大,可以使用空间换时间的方法(额外数组保存栈元素,然后逆向压入): 第一层递归( ...