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; }