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

本文共 2042 字,大约阅读时间需要 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*/

转载于:https://www.cnblogs.com/zwfymqz/p/9749495.html

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-05 微服务调用方式之feign 实战 订单调用商品服务...
查看>>
UI基础--烟花动画
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>