Xor-sequences (codeforces 691E)

Problem

You are given n integers a1,a2,...,an a 1 ,     a 2 ,     . . . ,     a n .

A sequence of integers x1,x2,...,xk x 1 ,     x 2 ,     . . . ,     x k is called a “xor-sequence” if for every 1  ≤  i  ≤  k - 1 the number of ones in the binary representation of the number xi x i xor x o r xi+1 x i   +   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 10 9   +   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 10 18 ) — the number of given integers and the length of the “xor-sequences”.

The second line contains n integers ai (0 ≤ ai ≤  1018 10 18 ).

Output

Print the only integer c — the number of “xor-sequences” of length k modulo 109+7 10 9   +   7 .

Sample Input

5 2
15 1 2 4 8

5 1
15 1 2 4 8

Sample Output

13
5


题目描述:

给出序列 a1,a2,...,an a 1 , a 2 , . . . , a n ,从中选出k个数(可重复选择)构成序列 x1,x2,...,xk x 1 , x 2 , . . . , x k ,需满足 xi x i 异或 xi+1 x i + 1 的值在二进制表示下1的个数是3的倍数( 1i<n 1 ≤ i < n ),问共有多少种选择方案?

分析:

如果 xi x i 异或 xi+1 x i + 1 的值在二进制表示下1的个数是3的倍数,那么 xi x i xi+1 x i + 1 可以连接,即 i i i+1 存在转移,令 d[i][j]=1 d [ i ] [ j ] = 1 ,得到转移矩阵。对转移矩阵做 k1 k − 1 次矩阵乘法,得到最终矩阵,将最终矩阵所有的值加起来即为答案,用矩阵快速幂加速。
解释:最终矩阵 d[i][j]=x d [ i ] [ j ] = x 意义为,以 ai a i 起始, aj a j 结束的满足条件的序列数量为 x x <script type="math/tex" id="MathJax-Element-1707">x</script> 个。

AC代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<time.h>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>

using namespace std;

const int mod = 1e9+7;

struct Matrix
{
    int n,m;
    long long d[100][100];
    Matrix (int a,int b)
    {
        n = a, m = b;
        memset(d,0,sizeof(d));
    }
    void Copy(int *tmp)
    {
        for (int i=0;i<n;i++)
            for (int j=0;j<m;j++)
            {
                d[i][j] = *tmp;
                tmp++;
            }
    }
    friend Matrix operator * (const Matrix &a,const Matrix &b)
    {
        Matrix c(a.n,b.m);
        for (int i=0;i<a.n;i++)
            for (int j=0;j<b.m;j++)
            {
                long long tmp = 0;
                for (int k=0;k<a.m;k++)
                {
                    tmp += a.d[i][k] * b.d[k][j] % mod;
                    if (tmp >= mod) tmp -= mod; 
                }
                c.d[i][j] = tmp;
            }
        return c;
    }
};

int n;
long long k,a[110];

int getnum(long long x)
{
    int cnt = 0;
    while (x)
    {
        cnt += x&1;
        x>>=1;
    }
    return cnt % 3;
}

void solve(long long m)
{
    Matrix A(100,100);
    for (int i=0;i<n;i++) A.d[i][i] = 1;

    Matrix B(100,100);
    for (int i=0;i<n;i++) 
        for (int j=0;j<n;j++)
            if (!getnum(a[i]^a[j])) B.d[i][j] = 1;  

    while (m)
    {
        if (m & 1) A = A*B;
        B = B*B;
        m = m>>1;
    }

    long long cnt = 0;
    for (int i=0;i<n;i++) 
        for (int j=0;j<n;j++)
        {
            cnt = cnt + A.d[i][j];
            if (cnt >= mod) cnt -= mod;
        }
    printf("%lld\n",cnt);
    return;
}

int main()
{
    while (scanf("%d %lld",&n,&k)!=EOF)
    {
        for (int i=0;i<n;i++) scanf("%lld",&a[i]);

        if (k == 1) 
        {
            printf("%d\n",n);
            continue;
        }

        solve(k-1);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值