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的更多相关文章

  1. BZOJ 3329: Xorequ [数位DP 矩阵乘法]

    3329: Xorequ 题意:\(\le n \le 10^18\)和\(\le 2^n\)中满足\(x\oplus 3x = 2x\)的解的个数,第二问模1e9+7 \(x\oplus 2x = ...

  2. BZOJ 3329 Xorequ (数位DP、矩阵乘法)

    手动博客搬家: 本文发表于20181105 23:18:54, 原地址https://blog.csdn.net/suncongbo/article/details/83758728 题目链接 htt ...

  3. [BZOJ 3329]Xorequ

    Description 题库链接 给出 \(n\) ,分别求 \(\leq n\) 和 \(\leq 2^n\) 的满足方程 \[x\oplus 3x=2x\] 的正整数解个数. \(1\leq n\ ...

  4. 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 ...

  5. BZOJ 3329 - Xorequ - 数位DP, 矩乘

    Solution 发现 $x \ xor \  2x = 3x$ 仅当 $x$ 的二进制中没有相邻的 $1$ 对于第一个问题就可以进行数位DP 了. 但是对于第二个问题, 我们只能通过递推 打表 来算 ...

  6. 【BZOJ】3329: Xorequ

    [题意]给定方程x^3x=2x,求<=x和<=2^x的满足方程的正整数个数. [算法]数位DP,矩阵快速幂 [题解]异或相当于不进位加法. 移项得,x^2x=3x,又因为x+2x=3x,所 ...

  7. BZOJ 3329 Xorequ:数位dp + 矩阵快速幂

    传送门 题意 现有如下方程:$ x \oplus 3x = 2x $ 其中 $ \oplus $ 表示按位异或. 共 $ T $ 组数据,每组数据给定正整数 $ n $,任务如下: 求出小于等于 $ ...

  8. 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,根据上面的 ...

  9. 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 ...

随机推荐

  1. HDU 5724 Chess(SG函数)

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  2. UIView的多重属性

    1)几何属性: 2)位图属性:

  3. virtualbox 安装 mac os x lion 10.7实现全屏显示!

    1. 启动Virtual Box虚拟机,在虚拟机里编辑 /Library/Preferences/SystemConfiguration/com.apple.Boot.plist,找到 <dic ...

  4. 移动端 Touch 事件

    在移动端页面开发时,常常会用到touch事件,比如左滑右滑的轮播等.常用的触摸事件有touchstart,touchmove,touchend. 每个事件包含下面三个用于跟踪虎摸的属性: touche ...

  5. Kali-linux目录加密

    在Kali中提供了一个目录加密工具TrueCrypt.该工具是一款开源的绿色加密卷加密软件,不需要生成任何文件即可在硬盘上建立虚拟磁盘.用户可以按照盘符进行访问,所以虚拟磁盘上的文件都被自动加密,访问 ...

  6. dropout总结

    1.伯努利分布:伯努利分布亦称“零一分布”.“两点分布”.称随机变量X有伯努利分布, 参数为p(0<p<1),如果它分别以概率p和1-p取1和0为值.EX= p,DX=p(1-p). 2. ...

  7. JavaSE注释

    注解在JavaSE中算是比较高级的一种用法了,为什么要学习注解,我想大概有以下几个原因: 1. 可以更深层次地学习Java,理解Java的思想. 2. 有了注解的基础,能够方便阅读各种框架的源码,比如 ...

  8. springmvc错误集锦-dubbo包含低版本的spring包,依赖的时候应该排除Caused by: java.lang.reflect.MalformedParameterizedTypeException

    dubbo 常见错误 1. Caused by: java.lang.reflect.MalformedParameterizedTypeException 启动时报错,原因是dubbo 依赖 spr ...

  9. Android 复制 粘贴 剪贴板的使用 ClipboardManager

    Copy and Paste 版本:Android 4.0 r1  快速查看 用于复制粘贴数据的基于剪贴板的框架. 同时支持简单和复杂的数据,包括文本串.复杂的数据结构.文本和二进制流数据.程序 as ...

  10. Sublime text3学习小记(macOS系统下的安装使用)

    [注:以下内容参考https://blog.csdn.net/stilling2006/article/details/54376743] 一.认识Sublime text 1.一款跨平台代码编辑器, ...