codeforces 691E Xor-sequences(矩阵快速幂)

引自:http://www.cnblogs.com/shuguangzw/p/5674089.html

/* ***********************************************
Author        :devil
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=1e2+10;
LL a[N],k,ans;
int n;
struct Matrix
{
    LL a[N][N];
    Matrix()
    {
        memset(a,0,sizeof(a));
    }
    void init()
    {
        for(int i=1;i<=n;i++)
            a[i][i]=1;
    }
    Matrix operator *(const Matrix &rhs)const
    {
        Matrix ret;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                for(int k=1;k<=n;k++)
                    ret.a[i][j]=(ret.a[i][j]+a[i][k]*rhs.a[k][j]%mod)%mod;
        return ret;
    }
    Matrix operator ^(LL mi)const
    {
        Matrix tmp=(*this),ret;
        ret.init();
        while(mi)
        {
            if(mi&1)ret=ret*tmp;
            tmp=tmp*tmp;
            mi>>=1;
        }
        return ret;
    }
}cur;
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d%I64d",&n,&k);
    for(int i=1; i<=n; i++)
        scanf("%I64d",&a[i]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(__builtin_popcountll(a[i]^a[j])%3==0)
                cur.a[i][j]++;
    cur=cur^(k-1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            ans=(ans+cur.a[i][j])%mod;
    printf("%I64d\n",ans);
    return 0;
}
时间: 2024-11-11 10:03:10

codeforces 691E Xor-sequences(矩阵快速幂)的相关文章

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

题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. 1 /* 2 | 1, -1 | | fn | 3 | 1, 0 | | fn-1 | 4 */ 5 #include <iostream> 6 #include <cstdio> 7 #include <cstring> 8 using namespace std; 9 typedef __int64 LL; 10 LL mod =

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, please calculate fn modulo 1000000007 (109 + 7). Input The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single i

Codeforces 450B - Jzzhu and Sequences ( 矩阵快速幂 )

题意: 给定f1和f2,求fn 分析: 特判f1,f2 当n>=3时使用矩阵快速幂即可( 简单题 ) 将公式转化一下 , 可以得到一个变换矩阵 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MAT_SIZE 2 #define MOD 10

CodeForces 185A. Plant(矩阵快速幂)

题目链接:http://codeforces.com/problemset/problem/185/A Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides int

Jzzhu and Sequences (矩阵快速幂 + 取模)

题目链接 Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn modulo 1000000007 (109?+?7). Input The first line contains two integers x and y (|x|,?|y|?≤?109). The second line contains a sin

Codeforces 1197F Coloring Game 矩阵快速幂 (看题解)

Coloring Game 我写的复杂度是 1000 * 64 * 64 * 64 * log(1e9),  感觉这个东西是很好想的, 肯定是T了的. 其实可以优化掉一个64, 就是在转移的时候用64 * 64的矩阵和 64 * 1的答案相邻相乘, 这样就可以优化掉一个64了, 以前好像没有见过这种小技巧. #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h>

【矩阵快速幂 】Codeforces 450B - Jzzhu and Sequences (公式转化)

[题目链接]click here~~ [题目大意] Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn modulo1000000007(109?+?7). [解题思路] /*A - Jzzhu and Sequences Codeforces 450B - Jzzhu and Sequences ( 矩阵快速幂 )

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

Xor-sequences CodeForces - 691E 题意:在有n个数的数列中选k个数(可以重复选,可以不按顺序)形成一个数列,使得任意相邻两个数异或的结果转换成二进制后其中1的个数是三的倍数.求可能形成的不同数列个数(只要选出的数列中,任意两个元素在原序列中的位置不同,就算作不同的序列,比如在原数列[1,1]中选1个,那么第一个1和第二个1要分开算). 方法: 很容易列出dp方程: dp[k][i]表示取了k个,最后一个在第i位.a[i][j]表示i和j异或结果转换成二进制后1的个数

Codeforces Round #257 (Div. 2)B 矩阵快速幂

B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn m