CF 691E Xor-sequences 矩阵快速幂 + dp
来源:互联网 发布:君威气襄电脑数据恢复 编辑:程序博客网 时间:2024/06/06 08:32
题意:给定序列,从序列中选择k(1≤k≤1e18)个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二进制表示中1的个数是3的倍数。问长度为k的满足条件的 序列有多少种?
与这题几乎一个套路
http://blog.csdn.net/viphong/article/details/52984918
dp[i][j]表示长度为i时,序列结尾为j的方案数
那么递推方程为 dp[i][j]+=dp[i-1][k] (bitcount(ai,aj)%3==0)
m太大,这样的线性递推可以构造快速幂
因此只需要维护一个n*n的矩阵即可
系数矩阵超级好构造
for (int i=0; i<n; i++) { for (int j=0; j<n; j++) if ( bitcount(aa[i]^aa[j] ) %3==0) c.mat[j][i]=1; }
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int N = 100;const long long mod=1000000007;struct Matrix{ long long mat[N][N];} ;Matrix unit_matrix ;long long n ;long long k=100;Matrix mul(Matrix a, Matrix b) //矩阵相乘{ Matrix res; for(int i = 0; i < k; i++) for(int j = 0; j < k; j++) { res.mat[i][j] = 0; for(int t = 0; t < k; t++) { res.mat[i][j] += a.mat[i][t] * b.mat[t][j]; res.mat[i][j] %= mod; } } return res;}Matrix pow_matrix(Matrix a, long long m) //矩阵快速幂{ Matrix res = unit_matrix; while(m != 0) { if(m & 1) res = mul(res, a); a = mul(a, a); m >>= 1; } return res;}long long aa[105];inline int bitcount(long long a){ int ret=0; while(a) { a=a^(a&-a); ret++; } return ret;}Matrix get(long long n ,long long times){ k=n; Matrix ori; memset( ori.mat ,0,sizeof ori.mat); for (int i=0; i<n; i++) ori.mat[0][i]=1; Matrix c; memset( c.mat ,0,sizeof c.mat); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) if ( bitcount(aa[i]^aa[j] ) %3==0) c.mat[j][i]=1; } Matrix ans = pow_matrix(c, times-1); ans = mul(ori,ans); return ans;}int main(){ int i, j, t; //初始化单位矩阵 //类似快速幂的 ans=1; 如今是ans=单位矩阵 memset(unit_matrix.mat,0,sizeof unit_matrix.mat); for(i = 0; i < k; i++) unit_matrix.mat[i][i] = 1; long long times; scanf("%lld%lld",&n,×); for (int i=0; i<n; i++)scanf("%lld",&aa[i]); if (k==1) { printf("%lld\n",n); return 0; } Matrix tmp=get(n,times); long long ans=0; for (int j=0; j<n; j++) ans=(tmp.mat[0][j]+ans)%mod; printf("%lld\n", ans); return 0;}
0 0
- CF 691E Xor-sequences 矩阵快速幂 + dp
- Codeforces 691E Xor-sequences【矩阵快速幂,好题】
- Codeforces 691E Xor-sequences(矩阵快速幂)
- Codeforces 691 E Xor-sequences 矩阵快速幂
- CodeForces-691E Xor-sequences(矩阵快速幂)
- codeforces 691E Xor-sequences(矩阵快速幂)
- Codeforces 691E Xor-sequences【矩阵快速幂,好题】
- Jzzhu and Sequences - CF 450B 矩阵快速幂版
- CF 450 B Jzzhu and Sequences(矩阵快速幂)
- cf#341-E. Wet Shark and Blocks --DP+矩阵快速幂
- 514E (矩阵快速幂+DP)
- Educational Codeforces Round 14 E.Xor-sequences
- Jzzhu and Sequences 【矩阵快速幂】
- CodeForces450B Jzzhu and Sequences (矩阵快速幂)
- ASC(1)E(矩阵快速幂+简单DP)
- CF round#420 div.2 E Okabe and El Psy Kongroo【矩阵快速幂】
- CF 242E XOR on Segment
- cf 459E DP
- 找不到org.apache.http
- linux学习路程01———文件系统
- java 判断中文字符
- 【Launcher】获取最新Launcher源码,并且导入到Android Studio
- MapReduce编程
- CF 691E Xor-sequences 矩阵快速幂 + dp
- android 一个很好使用的 LogUtils 日志工具类
- Windows7和Ubuntu双系统安装。
- 读-Martin Fowler-企业应用架构模式
- 矩形旋转碰撞,OBB方向包围盒算法实现
- C++ const变量
- JZOJ 4822 【NOIP2016提高A组集训第1场10.29】完美标号
- 使用fiddler进行genymotion安卓虚拟机手机抓包
- 使用Clion新建项目出现编译出错