题目链接

传送门

思路

由\(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. Gamma阶段第九次scrum meeting

    每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #91 用户体验与优化https://github.com/rRetr0Git/rateMyCourse/issues/91(持续完成) # ...

  2. spark 读取 ftp

    class FtpShow(spark: SparkSession, map: Map[String, String]) { private val path = map(FtpOptions.PAT ...

  3. mongo 复制一个表的数据到另一个表中

    club表: { "_id" : ObjectId("592e94fee820cc1813f0b9a2"), "id":1, "n ...

  4. Keras 使用过程问题汇总

    以下是Keras 使用过程出现的一些问题: (1)Keras 后端选择问题 一开始是选用的Theano,结果迭代一轮所花时间很长: 后面改用:TensorFlow作为后端,结果果然变快了: 改完Ten ...

  5. odoo前端必填提示

  6. Tensorflow2 快速简单安装命令

    使用如下命令 pip3 install numpy pandas matplotlib sklearn tensorflow==2.0.0-alpha0 -i https://pypi.doubani ...

  7. intellij idea快速通过mapper跳转到xml文件

    安装完之后重启idea即可!

  8. pychram 激活码

    转自博客:https://blog.csdn.net/may_ths/article/details/84032217 激活码到期时间: 2020.06 K6IXATEF43-eyJsaWNlbnNl ...

  9. Java随堂笔记一

    今天开始了Java的正式复习,因为有两三年没有接触Java了,所以打算开始从头复习. 下面使课堂的一些随堂笔记,如果有遗忘,我可以随时翻阅该博客. public static void main(St ...

  10. Windows和Linux简单命令的总结

    MS-DOS 命令提示符(cmd) 启动:                      Win+R,输入cmd回车 切换盘符            盘符名称: 进入文件夹              cd ...