3329: Xorequ
3329: Xorequ
https://www.lydsy.com/JudgeOnline/problem.php?id=3329
分析:
因为a+b = a^b + ((a&b)<<1)
所以(x&(2x))<<1是0,就是没有相邻的1。然后计算多少x满足没有相邻的1。
第一问:数位dp一下,dp[i][j]到第i位,上一个数是j的方案数。
第二问:一共n位数,只有第n位为1,所以这n位没有限制,f[i]表示到第i位,的方案数,f[i]=f[i-1]+f[i-2]。看第i位是不是1。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define fi(s) freopen(s,"r",stdin);
#define fo(s) freopen(s,"w",stdout);
using namespace std;
typedef long long LL; inline LL read() {
LL x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int mod = 1e9 + ; LL dp[][], num[]; struct Matrix{
int a[][];
void Clear() { memset(a, , sizeof(a)); }
void init() { a[][] = a[][] = a[][] = ; }
Matrix operator * (const Matrix &A) const {
Matrix C; C.Clear();
for (int k=; k<; ++k)
for (int i=; i<; ++i)
for (int j=; j<; ++j)
C.a[i][j] = (C.a[i][j] + 1ll * a[i][k] * A.a[k][j]) % mod;
return C;
}
};
LL dfs(int x,int last,bool lim) {
if (!x) return ;
if (!lim && dp[x][last]) return dp[x][last];
int u = lim ? num[x] : ; // u = lim ? num[x] : 0 !!!
LL res = ;
for (int i=; i<=u; ++i)
if (!last || !i) res += dfs(x - , i, lim && i==u);
if (!lim) dp[x][last] = res;
return res;
}
LL Calc1(LL n) {
int tot = ;
while (n) {
num[++tot] = n & ;
n >>= ;
}
return dfs(tot, , ) - ;
}
LL Calc2(LL n) {
Matrix A; A.Clear(); A.init();
Matrix res; res.Clear(); res.a[][] = res.a[][] = ;
while (n) {
if (n & ) res = res * A;
A = A * A;
n >>= ;
}
return (res.a[][] + res.a[][]) % mod;
}
int main() {
int T = read();
while (T--) {
LL n = read();
printf("%lld\n%lld\n", Calc1(n), Calc2(n));
}
return ;
}
3329: Xorequ的更多相关文章
- 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
Description 题库链接 给出 \(n\) ,分别求 \(\leq n\) 和 \(\leq 2^n\) 的满足方程 \[x\oplus 3x=2x\] 的正整数解个数. \(1\leq n\ ...
- 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, 矩乘
Solution 发现 $x \ xor \ 2x = 3x$ 仅当 $x$ 的二进制中没有相邻的 $1$ 对于第一个问题就可以进行数位DP 了. 但是对于第二个问题, 我们只能通过递推 打表 来算 ...
- 【BZOJ】3329: Xorequ
[题意]给定方程x^3x=2x,求<=x和<=2^x的满足方程的正整数个数. [算法]数位DP,矩阵快速幂 [题解]异或相当于不进位加法. 移项得,x^2x=3x,又因为x+2x=3x,所 ...
- BZOJ 3329 Xorequ:数位dp + 矩阵快速幂
传送门 题意 现有如下方程:$ x \oplus 3x = 2x $ 其中 $ \oplus $ 表示按位异或. 共 $ T $ 组数据,每组数据给定正整数 $ n $,任务如下: 求出小于等于 $ ...
- 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,根据上面的 ...
- 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 ...
随机推荐
- mvc:view-controller 标签
一.SpringMVC 会把ModelAndView 的model中的数据放到request 域中 二.如果不想让请求经过任何handler,而直接响应页面,可以使用 mvc:view-control ...
- 关于HiddenHttpMethodFilter
这个类的代码比较少,所以把整个类的代码都复制过来.在注释中添加上自己的理解. public class HiddenHttpMethodFilter extends OncePerRequestFil ...
- AWESOME SWIFT-swift.libhunt.com-swift类库网站
https://swift.libhunt.com/categories/688-events 29 Events libraries and projects ORDERED BY POPULARI ...
- CPU与GPU区别大揭秘
http://blog.csdn.net/xiaolang85/article/details/51500340 有网友在网上提问:“为什么现在更多需要用的是 GPU 而不是 CPU,比如挖矿甚至破解 ...
- ASP.NET Web API编程——文件上传
首先分别介绍正确的做法和错误的做法,然后分析他们的不同和错误之处,以便读者在实现此功能时可避开误区 1正确的做法 public class AvaterController : BaseApiCont ...
- 从数据库中取出的数据,字段名为gb2312的 数据转码为utf8
$pj = Pj::find()->where($map)->asArray()->one(); if(!empty($pj)) { foreach ($pj as $k=>$ ...
- 记一次数据库同步经历(sql server 2008)
前阵子搞了下数据库同步,大概意思就是服务器上有一个数据库,与本地数据库进行同步,服务器上的数据库有什么改变,可以同步到本地数据库中.做之前百度了下,流程分以下三步, 第一步: 服务器上的数据库进行发布 ...
- Swift_方法
Swift_方法 点击查看源码 ///方法 class Methods: NSObject { func test() { // self.testInstanceMethods() //实例方法 s ...
- SQLMAP使用详解
使用示例 python sqlmap.py -u "http://xx.com/member.php?id=XX" -p id --dbms "Mysql" ...
- 复习宝典之Spring
查看更多宝典,请点击<金三银四,你的专属面试宝典> 第六章:Spring Spring容器是Spring的核心,一切Spring bean都存储在Spring容器内,并由其通过IoC技术管 ...