题目链接

http://codeforces.com/problemset/problem/691/E

题意

给出一个长度为n的序列,从其中选择k个数 组成长度为k的序列,因为(k 有可能 > n) 那么数字是可以重复选择的

使得 aj 属于 a1 -> ak-1 满足 aj ^ aj + 1 中二进制表示中1的个数是3的倍数

思路

很显然 当k == 1的时候,不存在 aj 属于 a1 -> a0 那么 自然是满足的 也就是说 k == 1 的时候 答案就是n

那么 k == 2 的时候 用一个二维01矩阵表示 a[i] ^ a[j] 是否满足条件 如果是 就为1

最后把这个二维矩阵的和 加起来

然后是 k >= 3 的情况

根据矩阵乘法的性质

我们知道 矩阵a * 矩阵b = 矩阵ans

ans[i][j] = a[i][1] * b[1][j] + …… + a[i][n - 1] * b[n - 1][j]

那么很显然 当 k == 3的时候

a[i][1] * b[1][j] 表示的是 数列 arr[i] arr[1] arr[j] 这个数列是否满足题目条件

加入 易知 只有当 arr[i][1] == 1 && arr[1][j] == 1的时候 才是符合的

那么其相乘起来 也是1 是一个长度为3 的满足条件的序列

由此观之,如果 k == 3 只要算 k == 2 的时候 构造的那个矩阵 的 平方 再求和 就是答案

那么 k > 3的时候 答案就是 对 k == 2 的那个矩阵 算 k - 1次幂 就可以

用矩阵快速幂优化

AC代码

#pragma comment(linker, "/STACK:102400000,102400000")

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define pb push_back
#define fi first
#define se second
#define L(on) ((on)<<1)
#define R(on) (L(on) | 1)
#define mkp(a, b) make_pair(a, b)
#define bug puts("***bug***");
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define CLR(a, b) memset(a, (b), sizeof(a));
#define syn_close ios::sync_with_stdio(false); cin.tie(0);
#define sp system("pause");
//#define gets gets_s using namespace std; typedef long long ll;
typedef long double ld;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector < vi > vvi; const double PI = acos(-1.0);
const double EI = exp(1.0);
const double eps = 1e-8; inline int read()
{
char c = getchar(); int ans = 0, vis = 1;
while (c < '0' || c > '9') { if (c == '-') vis = -vis; c = getchar(); }
while (c >= '0' && c <= '9') { ans = ans * 10 + c - '0'; c = getchar(); }
return ans * vis;
} const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const int maxn = (int)1e2 + 10;
const int MAXN = (int)1e4 + 10;
const ll MOD = (ll)1e9 + 7; int n;
ll k;
ll arr[maxn]; struct Matrix
{
ll G[maxn][maxn];
int len;
Matrix () {}
Matrix operator * (const Matrix& r) const
{
Matrix tmp; tmp.len = len;
CLR(tmp.G, 0);
for (int i = 0; i < len; i++)
for (int j = 0; j < len; j++)
for (int k = 0; k < len; k++)
tmp.G[i][j] = (tmp.G[i][j] + G[i][k] * r.G[k][j]) % MOD;
return tmp;
}
}base; Matrix pow_mod(Matrix base, ll count)
{
Matrix ans; ans.len = base.len;
CLR(ans.G, 0);
for (int i = 0; i < ans.len; i++)
ans.G[i][i] = 1;
while (count)
{
if (count & 1)
ans = ans * base;
base = base * base;
count >>= 1;
}
return ans;
} ll ok(ll x)
{
ll ans = 0;
while (x)
{
if (x & 1) ans++;
x >>= 1;
}
return (ans % 3 == 0);
} void input()
{
scanf("%d%lld", &n, &k);
for (int i = 0; i < n; i++)
scanf("%lld", arr + i);
base.len = n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
base.G[i][j] = ok(arr[i] ^ arr[j]);
} void solve()
{
base = pow_mod(base, k - 1);
ll ans = 0;
for (int i = 0; i < base.len; i++)
for (int j = 0; j < base.len; j++)
ans = (ans + base.G[i][j]) % MOD;
cout << ans << endl;
} int main()
{
input(); solve();
}

CodeForces - 691E Xor-sequences 【矩阵快速幂】的更多相关文章

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

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

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

    You are given n integers a1,  a2,  ...,  an. A sequence of integers x1,  x2,  ...,  xk is called a & ...

  3. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

  4. Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  5. codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)

    题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...

  6. Codeforces 954 dijsktra 离散化矩阵快速幂DP 前缀和二分check

    A B C D 给你一个联通图 给定S,T 要求你加一条边使得ST的最短距离不会减少 问你有多少种方法 因为N<=1000 所以N^2枚举边数 迪杰斯特拉两次 求出Sdis 和 Tdis 如果d ...

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

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

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

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

  9. codeforces 691E Xor-sequences 矩阵快速幂

    思路:刚开始 n个元素,a[i][j]代表以i开头,j结尾的二元组符合条件的有多少 这是等于长度为2的数量 长度为3的数量为a*a,所以长度为n的数量是a^(k-1) 然后就是矩阵快速幂,然而我并不能 ...

随机推荐

  1. Python开发qq批量登陆

    操作步骤: 1.打开qq软件 2.移动鼠标到qq输入处 3.在输入处,点击鼠标,输入帐号 4.模拟按下tab键,输入密码,模拟点回车登录 #coding=utf-8 import os import ...

  2. Jetty锁定文件的问题

    在windows系统上,jetty默认在运行时会锁定部署的文件.这对于需要在程序运行期间动态生成或改动某些文件就变得不能执行!对于这一点,Jetty的官网上专门有文章进行了解释:http://docs ...

  3. 源代码分析Fragmentd的BackStack管理过程

    1. Fragment基本使用方法 为了管理Activity中的fragments.须要调用Activity中的getFragmentManager()方法.由于FragmentManager的API ...

  4. UVA 1363 Joseph's Problem 找规律+推导 给定n,k;求k%[1,n]的和。

    /** 题目:Joseph's Problem 链接:https://vjudge.net/problem/UVA-1363 题意:给定n,k;求k%[1,n]的和. 思路: 没想出来,看了lrj的想 ...

  5. 下载 Microsoft SQL Server JDBC 驱动程序

    JDBC 驱动程序中使用 Maven 中心 JDBC 驱动程序可以通过将其添加为依赖项在 POM.xml 文件中使用以下代码添加到 Maven 项目: XML复制 <dependency> ...

  6. RMI 连接超时时间设定

    System.setProperty("sun.rmi.transport.tcp.responseTimeout", "2000"); System.setP ...

  7. 求出10000以内所有的完全数-python

    题目: 如何用python去求出下一个(大于28的)完全数? (求出10000以内所有的完全数) 分析: 如果一个数恰好等于它的因子之和,则称该数为"完全数".各个小于它的约数(真 ...

  8. java前端传入的json字符串保存到表中的方法

    表 service_goods_base 字段如下: 传入的json 字符串: servicePictureArray  :  [{"picServiceUrl": "h ...

  9. org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [cn.facekee.cms.entity.CmsFansgroup#195]

    刚开始报错还是报的稀奇古怪的错误,让我纠结了好久,再三检查报错的位置,发现并没有错误,最后认真分析查看每行报错的信息才找到如题所述的错误!!!!! 报这种错误的原因可能是POJO映射文件中的字段和数据 ...

  10. 关于错误处理程序中【return】的用法

    先让俺这位新人帮各位有幸游览到我博客文章的叔叔阿姨哥哥姐姐们解释一下什么是错误处理?即:当程序发生错误时,保证程序不会异常中断的机制. 那么为什么程序中会有错误处理呢?像我们通常无论是玩手机或者玩游戏 ...