博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ3329: Xorequ(二进制数位dp 矩阵快速幂)
阅读量:6927 次
发布时间:2019-06-27

本文共 1987 字,大约阅读时间需要 6 分钟。

题意

Sol

挺套路的一道题

首先把式子移一下项

\(x \oplus 2x = 3x\)

有一件显然的事情:\(a \oplus b \leqslant c\)

又因为\(a \oplus b + 2(a \& b) = c\)

那么\(x \& 2x = 0\)

也就是说,\(x\)的二进制表示下不能有相邻位

第一问直接数位dp即可

第二问比较interesting,设\(f[i]\)表示二进制为\(i\)的方案数,转移时考虑上一位选不选

如果能选,方案数为\(f[i - 2]\)

不选的方案数为\(f[i - 1]\)

#include
#define LL long long //#define int long long #define file {freopen("a.in", "r", stdin); freopen("a.out", "w", stdout);}using namespace std;const int MAXN = 233, mod = 1e9 + 7;inline LL read() { char c = getchar(); LL x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f;}LL N;struct Matrix { int m[3][3]; Matrix() { memset(m, 0, sizeof(m)); } Matrix operator * (const Matrix &rhs) const { Matrix ans; for(int k = 1; k <= 2; k++) for(int i = 1; i <= 2; i++) for(int j = 1; j <= 2; j++) (ans.m[i][j] += 1ll * m[i][k] * rhs.m[k][j] % mod) %= mod; return ans; }};Matrix MatrixPow(Matrix a, LL p) { Matrix base; for(int i = 1; i <= 2; i++) base.m[i][i] = 1; while(p) { if(p & 1) base = base * a; a = a * a; p >>= 1; } return base;}LL num[MAXN], tot; LL f[MAXN][2];LL dfs(int x, bool lim, bool pre) { if(!lim && (~f[x][pre])) return f[x][pre]; if(x == 0) return 1; LL ans = 0; if(!pre && (num[x] == 1 || (!lim))) ans += dfs(x - 1, lim, 1); ans += dfs(x - 1, lim && num[x] == 0, 0); if(!lim) f[x][pre] = ans; return ans;}LL dp(LL x) { tot = 0; while(x) num[++tot] = x & 1, x >>= 1; return dfs(tot, 1, 0);} main() {// file; memset(f, -1, sizeof(f)); int T = read(); while(T--) { N = read(); printf("%lld\n", dp(N) - 1); Matrix a; a.m[1][1] = 1; a.m[1][2] = 1; a.m[2][1] = 1; a.m[2][2] = 0; a = MatrixPow(a, N); printf("%d\n", (a.m[1][1] + a.m[1][2]) % mod); } return 0;}/*15*/

转载地址:http://itkjl.baihongyu.com/

你可能感兴趣的文章
Implementations of interface through Reflection 反射根据继承的信息查找指定的类
查看>>
java split方法
查看>>
JSTL / Filter
查看>>
很好的验证码
查看>>
Swift进阶之路(一)——单例模式、属性传值、代理传值、闭包传值
查看>>
IPV4
查看>>
XML基础之Jdom和DOM4J解析(转)
查看>>
Ad Hoc Distributed Queries的启用与关闭
查看>>
安全的“野指针”
查看>>
EXCEL实战技巧与数据分析(一)基础应用
查看>>
使用NIFTI指令画nii图像
查看>>
利用缓存、Timer间隔时间发送微信的实例,很有用的例子
查看>>
CentOS7使用firewalld打开关闭防火墙与端口
查看>>
UIPageViewController
查看>>
在线UI设计
查看>>
2017最好的JavaScript框架、库和工具 — SitePoint
查看>>
Linux 集群
查看>>
R语言推特twitter转发可视化分析
查看>>
模仿也是提高,纯css小技巧实现头部进度条
查看>>
js 改变只读属性的值
查看>>