BZOJ 3329 - Xorequ - 数位DP, 矩乘
Solution
发现 $x \ xor \ 2x = 3x$ 仅当 $x$ 的二进制中没有相邻的 $1$
对于第一个问题就可以进行数位DP 了。
但是对于第二个问题, 我们只能通过递推 打表 来算出答案了。
推公式 打表 可知, 这是一个斐波那契数列, $a_0 = 1, a_1 = 2, a_2 = 3$....
通过矩阵快速幂优化递推就可以过啦
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
#define ll long long
using namespace std; const int mod = 1e9 + ; const int N = ; int T, a[];
ll sum[N][], n; struct matrix {
ll s[][];
matrix operator * (const matrix &b) const {
matrix re;
memset(re.s, , sizeof(re.s));
for(int i = ; i <= ; ++i)
for(int k = ; k <= ; ++k)
for(int j = ; j <= ; ++j)
re.s[i][j] = (re.s[i][j] + s[i][k] * b.s[k][j]) % mod;
return re;
}
}ans, st; struct node {
int id;
ll in, out1, out2;
}b[]; ll read() {
ll X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} ll dfs(int pos, int pre, int lim, int lead) {
if(!pos) return lead == ;
if(!lim && !lead && sum[pos][pre] != -)
return sum[pos][pre];
int up = lim ? a[pos] : ;
ll tmp = ;
for(int i = ; i <= up; ++i) {
if(pre && i)
continue;
tmp += dfs(pos - , i, lim && a[pos] == i, lead && i == );
}
if(!lim && !lead)
sum[pos][pre] = tmp;
return tmp;
} ll work(ll x) {
int len = ;
while(x) a[++len] = x % , x /= ;
return dfs(len , , true, true);
} inline bool cmp1(const node &A, const node &B ) {
return A.in < B.in;
} inline bool cmp2(const node &A, const node &B) {
return A.id < B.id;
} void print(ll x) {
sort(b + , b + + T, cmp1);
memset(sum, -, sizeof(sum));
memset(st.s, , sizeof(st.s));
memset(ans.s, , sizeof(ans.s));
st.s[][] = st.s[][] = st.s[][] = ;
ans.s[][] = ;
ans.s[][] = ;
printf("%lld\n", work(x));
for(; x; x >>= , st = st * st)
if(x & ) ans = ans * st;
printf("%lld\n", (ans.s[][] % mod + mod) % mod);
} int main()
{
T = rd;
for(; T; T--) print(rd);
}
BZOJ 3329 - Xorequ - 数位DP, 矩乘的更多相关文章
- BZOJ 3329: Xorequ [数位DP 矩阵乘法]
3329: Xorequ 题意:\(\le n \le 10^18\)和\(\le 2^n\)中满足\(x\oplus 3x = 2x\)的解的个数,第二问模1e9+7 \(x\oplus 2x = ...
- BZOJ 3329 Xorequ (数位DP、矩阵乘法)
手动博客搬家: 本文发表于20181105 23:18:54, 原地址https://blog.csdn.net/suncongbo/article/details/83758728 题目链接 htt ...
- BZOJ 3329: Xorequ(数位dp+递推)
传送门 解题思路 可以把原式移项得\(x\)^\(2x\)=\(3x\),而\(x+2x=3x\),说明\(x\)二进制下不能有两个连续的\(1\).那么第一问就是一个简单的数位\(dp\),第二问考 ...
- BZOJ.3329.Xorequ(数位DP)
题目链接 x^3x=2x -> x^2x=3x 因为a^b+((a&b)<<1)=a+b,x^2x=x+2x,所以x和2x的二进制表示中不存在相邻的1. (或者,因为x+2x ...
- BZOJ 3329 Xorequ 数字DP+矩阵乘法
标题效果:特定n,乞讨[1,n]内[1,2^n]差多少x满足x^3x=2x x^3x=2x相当于x^2x = 3x 和3x=x+2x 和2x=x<<1 因此x满足条件IFFx&(x ...
- bzoj 3329: Xorequ【数位dp+矩阵乘法】
注意第一问不取模!!! 因为a+b=a|b+a&b,a^b=a|b-a&b,所以a+b=a^b+2(a&b) x^3x==2x可根据异或的性质以转成x^2x==3x,根据上面的 ...
- BZOJ3329 Xorequ(数位DP)
题目大意:x xor 2x=3x(与x xor 3x=2x等价)求满足等式且小于n的x的个数,与满足等式小于2n的数的个数. 因为异或是不进位的二进制加法,那么因为结果正好和加法相同,那么说明x在二进 ...
- [BZOJ 3329]Xorequ
Description 题库链接 给出 \(n\) ,分别求 \(\leq n\) 和 \(\leq 2^n\) 的满足方程 \[x\oplus 3x=2x\] 的正整数解个数. \(1\leq n\ ...
- bzoj 3209 bzoj1799 数位dp
3209: 花神的数论题 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2267 Solved: 1040[Submit][Status][Disc ...
随机推荐
- jquery iframe父子框架中的元素访问方法
在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素 1. 格式:window. ...
- linux内核配置 kbuild
Linux 内核配置机制 http://blog.csdn.net/dianhuiren/article/details/6917132 linux kbuild文档 http://blog.chin ...
- CSS样式表的写作规范
推荐大家使用的CSS书写规范.顺序 写了这么久的CSS,但自己都没有按照良好的CSS书写规范来写CSS代码,东写一段西写一段,命名也是想到什么写什么,过一段时间自己都不知道写的是那一块内容, 这样会影 ...
- java.lang.NoClassDefFoundError: net.tsz.afinal.FinalHttp
java.lang.NoClassDefFoundError: net.tsz.afinal.FinalHttpat com.hbjyjt.oa.utils.i.<init>(HttpRe ...
- windows环境下搭建kafka
注意:请确保本地Java环境变量配置成功 1.安装Zookeeper Kafka的运行依赖于Zookeeper,所以在运行Kafka之前我们需要安装并运行Zookeeper 1.1 下载安装文件: h ...
- POJ-3078.Shuffle'm Up(简单模拟题)
这道题做了有四个小时吧,今天一整天都处于边玩边学的状态,我很是不喜欢...一开始用了20分钟模拟,过了样例后TLE了,就在考虑是不是判断是否重复判定的数组开大了,结果一直蛙,后面想到了map判重,结果 ...
- C++ 智能指针shared_ptr的实现
#include <memory> #include <iostream> using namespace std; template<typename T> cl ...
- 【校招面试 之 网络】第2题 TCP的可靠传输、流量控制、滑动窗口
1.可靠传输 (1)三次握手 TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接: (1)第一次握手:建立连接时,客户端A发送SYN包(SYN=j)到服务器B,并进入SYN_S ...
- [leetcode]387. First Unique Character in a String第一个不重复字母
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- linux 备忘录
1. ps aux|grep 程序 -------->查看当前程序是否运行 ps aux|grep nginx 2. tar -zxvf 压缩包 ---------> 解压缩 tar -z ...