BZOJ3329: Xorequ(二进制数位dp 矩阵快速幂)
题意
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<bits/stdc++.h>
#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;
}
/*
1
5
*/
BZOJ3329: Xorequ(二进制数位dp 矩阵快速幂)的更多相关文章
- BZOJ3329 Xorequ(数位dp+矩阵快速幂)
显然当x中没有相邻的1时该式成立,看起来这也是必要的. 于是对于第一问,数位dp即可.第二问写出dp式子后发现就是斐波拉契数列,矩阵快速幂即可. #include<iostream> #i ...
- BZOJ 3329 Xorequ:数位dp + 矩阵快速幂
传送门 题意 现有如下方程:$ x \oplus 3x = 2x $ 其中 $ \oplus $ 表示按位异或. 共 $ T $ 组数据,每组数据给定正整数 $ n $,任务如下: 求出小于等于 $ ...
- hdu5564--Clarke and digits(数位dp+矩阵快速幂)
Clarke and digits 问题描述 克拉克是一名人格分裂患者.某一天,克拉克变成了一个研究人员,在研究数字. 他想知道在所有长度在[l,r]之间的能被7整除且相邻数位之和不为k的正整数有多少 ...
- HUST 1569(Burnside定理+容斥+数位dp+矩阵快速幂)
传送门:Gift 题意:由n(n<=1e9)个珍珠构成的项链,珍珠包含幸运数字(有且仅由4或7组成),取区间[L,R]内的数字,相邻的数字不能相同,且旋转得到的相同的数列为一种,为最终能构成多少 ...
- 2018.09.27 hdu5564Clarke and digits(数位dp+矩阵快速幂)
传送门 好题啊. 我只会写l,rl,rl,r都很小的情况(然而题上并没有这种数据范围). 但这个dp转移式子可以借鉴. 我们用f[i][j][k]f[i][j][k]f[i][j][k]表示当前在第i ...
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
- 【BZOJ】2004: [Hnoi2010]Bus 公交线路 状压DP+矩阵快速幂
[题意]n个点等距排列在长度为n-1的直线上,初始点1~k都有一辆公车,每辆公车都需要一些停靠点,每个点至多只能被一辆公车停靠,且每辆公车相邻两个停靠点的距离至多为p,所有公车最后会停在n-k+1~n ...
- 【BZOJ】4861: [Beijing2017]魔法咒语 AC自动机+DP+矩阵快速幂
[题意]给定n个原串和m个禁忌串,要求用原串集合能拼出的不含禁忌串且长度为L的串的数量.(60%)n,m<=50,L<=100.(40%)原串长度为1或2,L<=10^18. [算法 ...
随机推荐
- cuda by example
int offset= x+y*dim x 线程块内的线程索引 y 线程块索引 dim 线程块的维度 tid = threadIdx.x+blockIdx.x*blockDim.x 计算大于或 ...
- sql开发技巧总结-1
1.数据库分类 关系型 非关系型 2.sql语句分类 sql: ddl数据库定义语言 tpl事物处理语言 dcl数据控制语言 dml数据操作语言(insert delete update sele ...
- LinkedList实现原理
原文链接:https://www.jianshu.com/p/56c77c517e71 本文对LinkedList的实现讨论都基于JDK8版本 Java中的LinkedList类实现了List接口和D ...
- git 本地分支与远程分支相关操作记录
1.远程分支中有新增分支,但自己的本地分支没有对应同步 git checkout -b [remote-branch-name] origin/[remote-branch-name] 2. 查看本地 ...
- 使用Tensorflow object detection API——环境搭建与测试
[软件环境搭建] 操作系统:windows 10 64位 内存:8G CPU:I7-6700 Tensorflow: 1.4 Python:3.5 Anaconda3 (64-bit) 以上环境搭建请 ...
- shell操作数组
#!/bin/bash nums=( ) echo ${#nums[*]} #向数组中添加元素 nums[]="http://c.biancheng.net/shell/" ech ...
- Infiniband交换机的FabricMonitor加载数据hang
刚刚帮客户将Exadata中Infiniband交换机的固件版本从2.1.3-1 升级到2.2.7-1,但升级后发现Infiniband交换机的FabricMonitor功能无法使用,具体如下图所示. ...
- .net mvc 框架实现后台管理系统
原文: https://www.cnblogs.com/ymnets/p/3424309.html -->底层实现 https://www.layui.com/ -->UI实现
- fatal error C1859: “Release\IWBServer.pch”意外的预编译头错误,只需重新运行编译器就可能修复此问题
解决方案 1. 创建预编译头(/Yc) -- > stdafx.cpp 使用预编译头(/Yu) 2. complie 3. 使用预编译头(/Yu) -- ...
- ie7,ie8 js中变量名和页面元素ID重名,报错
js变量名和一个div的id重名,报错.不知所以然...做个标记