题目链接

传送门

思路

由\(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. concurrent(六)同步辅助器CyclicBarrier & 源码分析

    参考文档:Java多线程系列--“JUC锁”10之 CyclicBarrier原理和示例:https://www.cnblogs.com/skywang12345/p/3533995.html简介Cy ...

  2. 【Gamma】 Phylab 展示博客

    目录 [Gamma] Phylab 展示博客 发布地址 网站:PhyLab GitHub Release: WhatAHardChoice/Phylab Gamma版本 一.团队简介 二.项目目标 2 ...

  3. mapReduce 大数据离线分析

    数据分析一般分为两种,一种是在线一种是离线 流程: 一般都是对于日志文件的采集和分析 场景实例(某个电商网站产生的用户访问日志(access.log)进行离线处理与分析的过程) 1.需求: 基于Map ...

  4. | C语言I作业01

    C语言I作业01 标签:18软件 李煦亮 1.1 你对软件工程专业了解是怎样? 对软件工程的了解是从人工智能频繁地出现在各大新闻,新闻报道了许多高校针对人工智能开设了相关课程或者专业,软件工程是开设的 ...

  5. Disable foreign key checks during import

    The command SET FOREIGN_KEY_CHECKS=0; sets the value of a variable in a session scope. So it affects ...

  6. Sitecore 8.2 数据库权限设置

    在我的一个项目中,客户决定改变基础设施.在这个过程中,我得到了一些新的东西需要学习.在本文中,我将分享有关Sitecore数据库权限的经验. 在将数据库从一个服务器移动到另一个服务器时,您需要检查提供 ...

  7. 【题解】Luogu P5468 [NOI2019]回家路线

    原题传送门 前置芝士:斜率优化 不会的可以去杜神博客学 这道题我考场上只会拆点跑最短路的70pts做法 后来回家后发现错误的爆搜都能拿满分(刀片) 还有很多人\(O(mt)\)过的,还是要坚持写正解好 ...

  8. Java学习:JDBC各类详解

    JDBC各个类详解 代码实现: //1.导入驱动jar包 //2.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //3.获取数据库连对象 ...

  9. Linux学习笔记之iptables学习笔记

    iptables系列学习推荐: http://www.zsythink.net/archives/category/%e8%bf%90%e7%bb%b4%e7%9b%b8%e5%85%b3/iptab ...

  10. IIS 7.5绑定中文域名转码启动站点报“值不在预期的范围内”

    问题现象 IIS 7.5在绑定中文域名转码后,启动站点会出现[值不在预期的范围内]: 解决方案 此问题是由于中文域名绑定错误导致的,IIS 7.5针对中文域名会自动转换为punycode码,所以不需要 ...