题目链接

传送门

思路

由\(a\bigoplus b=c\rightarrow a=c\bigoplus b\)得原式可化为\(x\bigoplus 2x=3x\)。

又异或是不进位加法,且\(2x=1<<x,3x=(1<<x)+x\),因此可知\((x\&2x)=0\),也就是说\(x\)的二进制中没有相邻的\(1\)。

第一问就可以用数位\(DP\)来写。

对于第二问我们可以考虑递推式,我们定义\(f(x)\)表示\(2^x\)时满足等式的数的个数,则

  • 如果第\(n\)位是\(1\),那么第\(n-1\)位就必须是\(0\),此时就相当于忽略第\(n,n-1\)位,转变成最高位是\(n-2\)的个数,因此\(f(n)\)可以从第\(n-2\)位转移过来;
  • 如果第\(n\)位是\(0\),那么就相当于忽略第\(n\)位,转变成最高位是\(n-1\)的个数,因此\(f(n)\)可以从第\(n-1\)位转移过来。

最后得到递推式\(f(n)=f(n-1)+f(n-2)\),也就是斐波那契数列,注意该递推式中的\(n\)是指\(2\)进制中最高位是多少,也就是题目中的\(n-1\),因次本题答案是\(f(n+1)\)。

其实这两个规律可以通过打表找出来的~

代码

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 50000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int t;
LL n;
int a[62];
int f[3], base[3][3];
LL dp[62][2][2][2]; LL dfs(int pos, int pre, int flag, bool limits, bool lead) {
if(pos == -1) return flag && (!lead);
if(!limits && dp[pos][pre][flag][lead] != -1) return dp[pos][pre][flag][lead];
int up = limits ? a[pos] : 1;
LL ans = 0;
for(int i = 0; i <= up; ++i) {
if(i == 0) ans += dfs(pos - 1, 0, flag, limits && i == a[pos], lead);
else ans += dfs(pos - 1, 1, flag && (pre != 1), limits && a[pos] == i, 0);
}
if(!limits) dp[pos][pre][flag][lead] = ans;
return ans;
} LL solve(LL x) {
int len = 0;
while(x) {
a[len++] = x % 2;
x >>= 1;
}
return dfs(len - 1, 0, 1, 1, 1);
} void mul() {
int c[3];
memset(c, 0, sizeof(c));
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < 2; ++j) {
c[i] = (c[i] + 1LL * f[j] * base[j][i] % mod) % mod;
}
}
memcpy(f, c, sizeof(c));
} void mulself() {
int c[3][3];
memset(c, 0, sizeof(c));
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < 2; ++j) {
for(int k = 0; k < 2; ++k) {
c[i][j] = (c[i][j] + 1LL * base[i][k] * base[k][j] % mod) % mod;
}
}
}
memcpy(base, c, sizeof(c));
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
memset(dp, -1, sizeof(dp));
scanf("%d", &t);
while(t--) {
scanf("%lld", &n);
printf("%lld\n", solve(n));
base[0][0] = 1, base[0][1] = 1;
base[1][0] = 1, base[1][1] = 0;
f[0] = f[1] = 1;
while(n) {
if(n & 1) mul();
mulself();
n >>= 1;
}
printf("%d\n", f[0]);
}
return 0;
}

Xorequ(BZOJ3329+数位DP+斐波那契数列)的更多相关文章

  1. [ZJOI2011]细胞——斐波那契数列+矩阵加速+dp

    Description bzoj2323 Solution 题目看起来非常复杂. 本质不同的细胞这个条件显然太啰嗦, 是否有些可以挖掘的性质? 1.发现,只要第一次分裂不同,那么互相之间一定是不同的( ...

  2. DP思想在斐波那契数列递归求解中的应用

    斐波那契数列:1, 1, 2, 3, 5, 8, 13,...,即 f(n) = f(n-1) + f(n-2). 求第n个数的值. 方法一:迭代 public static int iterativ ...

  3. 斐波那契数列 矩阵乘法优化DP

    斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007​\),\(n\le 10^{18}​\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...

  4. HDU 2041 超级楼梯 (斐波那契数列 & 简单DP)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041 题目分析:题目是真的水,不难发现规律涉及斐波那契数列,就直接上代码吧. 代码如下: #inclu ...

  5. 斐波那契数列,跳台阶(dp思想)

    一 . 斐波那契数列:1,1,2,3,5,8,13,21 即后一项是前两项的和. class Solution { private: ]; public: Solution() { memset(ar ...

  6. hdu-5686 Problem B(斐波那契数列)

    题目链接: Problem B Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  7. 斐波那契数列 Library

    http://acm.tju.edu.cn/toj/showp3267.html3267.   Library Time Limit: 1.0 Seconds   Memory Limit: 6553 ...

  8. luoguP4000 斐波那契数列

    题目链接 luoguP4000 斐波那契数列 题解 根据这个东西 https://www.cnblogs.com/sssy/p/9418732.html 我们可以找出%p意义下的循环节 然后就可以做了 ...

  9. 算法 递归 迭代 动态规划 斐波那契数列 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

随机推荐

  1. Thrift源码分析(二)-- 协议和编解码

    协议和编解码是一个网络应用程序的核心问题之一,客户端和服务器通过约定的协议来传输消息(数据),通过特定的格式来编解码字节流,并转化成业务消息,提供给上层框架调用. Thrift的协议比较简单,它把协议 ...

  2. js密码加密

    1.base64加密:在页面中引入base64.js文件,调用方法为: <!DOCTYPE HTML> <html> <head> <meta charset ...

  3. 【C语言】获得数组长度

    c语言中,定义数组后可以用sizeof命令获取数组的长度(可容纳元素个数): 如: { int data[5]; int length; length=sizeof(data)/sizeof(data ...

  4. Azkaban 3.x 编译及部署

    一.Azkaban 源码编译 1.1 下载并解压 Azkaban 在 3.0 版本之后就不提供对应的安装包,需要自己下载源码进行编译. 下载所需版本的源码,Azkaban 的源码托管在 GitHub ...

  5. APUE之第5章——标准I/O库

    一.知识回顾:文件I/O 文件 I/O 是不带缓冲的 I/O(unbuffered I/O),指每个 read 和 write 都调用内核中的一个系统调用. 对于内核而言,所有打开的文件都通过文件描述 ...

  6. IdentityServer4学习及简单使用

    本文,主要用来记录IdentityServer4的简单使用. 一. IdentityServer的预备知识 要学习IdentityServer,需要了解下基于Token的验证体系,其中涉及到Token ...

  7. NetCore 统一处理 webapi 返回null 转为“”

    数据库中部分表字段允许空值,则代码中实体类对应的字段类型为可空类型Nullable<>,如int?,DateTime?,null值字段序列化返回的值都为null,前端对应字段赋值需要做nu ...

  8. MySql安装学习笔记

    参考地址:http://www.cnblogs.com/laumians-notes/p/9069498.html 该文章仅作为自己的学习笔记 一.准备mysql-8.0.11-winx64压缩包(示 ...

  9. SQL根据指定节点ID获取所有父级节点和子级节点

    --根据指定节点ID获取所有子节点-- WITH TEMP AS ( SELECT * FROM table_name WHERE Id=' --表的主键ID UNION ALL SELECT T0. ...

  10. HDFS文件浏览页返回上级目录功能

    1.效果预览 Hadoop自带的效果 修改后,多了一个../按钮,点击可以回到上级目录 2.查找页面和JS文件 我们在浏览器上可以看到访问了explorer.html页面,可以尝试使用find命令查找 ...