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. [算法 ...
随机推荐
- 51nod1448(yy)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1448 题意: 中文题诶~ 不过要仔细看题, 原来颜色是被覆盖 ...
- 「十二省联考 2019」字符串问题——SAM+DAG
题目 [题目描述] Yazid 和 Tiffany 喜欢字符串问题.在这里,我们将给你介绍一些关于字符串的基本概念. 对于一个字符串 $S$, 我们定义 $\lvert S\rvert$ 表示 $S$ ...
- kuangbin专题十六 KMP&&扩展KMP HDU2594 Simpsons’ Hidden Talents
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marg ...
- 小程序不在以下 request 合法域名列表中
我们如果是正式上线可以在小程序后台配置合法域名,必须是https 测试时我们可以关闭验证 在 设置 - 项目设置 里勾选不校验https 和 TLS
- django ORM 连表查询2
set() 更新model对象的关联对象 book_obj=models.Book.objects.first() book_obj.authors.set([2,3]) 把book_obj这个对象重 ...
- zabbix 告警 JXM
告警 虚拟机上网 [root@test1 alertscripts]# route -n [root@test1 alertscripts]# route add default gw 172.25. ...
- wireshark开发环境搭建
自己完成了wireshark开发环境的搭建,主要参考资料是wireshark的官方developer-guide.pdf,网址:https://www.wireshark.org/docs/. 现把搭 ...
- python模块之wordcloud
wordcloud官方文档: http://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html#wordcloud.Wor ...
- ctrip-apollo
云端多网卡问题: 参考:https://blog.csdn.net/buyaore_wo/article/details/79847404
- 安装Chrome插件Markdown Preview Plus
1.在谷歌应用商店,安装Chrome插件Markdown Preview Plus 2.设置Markdown Preview Plus (1)鼠标左键该拓展插件 (2)鼠标右键该插件 3.将mar ...