Xorequ(BZOJ3329+数位DP+斐波那契数列)
题目链接
思路
由\(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+斐波那契数列)的更多相关文章
- [ZJOI2011]细胞——斐波那契数列+矩阵加速+dp
Description bzoj2323 Solution 题目看起来非常复杂. 本质不同的细胞这个条件显然太啰嗦, 是否有些可以挖掘的性质? 1.发现,只要第一次分裂不同,那么互相之间一定是不同的( ...
- DP思想在斐波那契数列递归求解中的应用
斐波那契数列:1, 1, 2, 3, 5, 8, 13,...,即 f(n) = f(n-1) + f(n-2). 求第n个数的值. 方法一:迭代 public static int iterativ ...
- 斐波那契数列 矩阵乘法优化DP
斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007\),\(n\le 10^{18}\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...
- HDU 2041 超级楼梯 (斐波那契数列 & 简单DP)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041 题目分析:题目是真的水,不难发现规律涉及斐波那契数列,就直接上代码吧. 代码如下: #inclu ...
- 斐波那契数列,跳台阶(dp思想)
一 . 斐波那契数列:1,1,2,3,5,8,13,21 即后一项是前两项的和. class Solution { private: ]; public: Solution() { memset(ar ...
- hdu-5686 Problem B(斐波那契数列)
题目链接: Problem B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 斐波那契数列 Library
http://acm.tju.edu.cn/toj/showp3267.html3267. Library Time Limit: 1.0 Seconds Memory Limit: 6553 ...
- luoguP4000 斐波那契数列
题目链接 luoguP4000 斐波那契数列 题解 根据这个东西 https://www.cnblogs.com/sssy/p/9418732.html 我们可以找出%p意义下的循环节 然后就可以做了 ...
- 算法 递归 迭代 动态规划 斐波那契数列 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
随机推荐
- Spring Boot进阶系列三
Thymeleaf是官方推荐的显示引擎,这篇文章主要介绍怎么让spring boot整合Thymeleaf. 它是一个适用于Web和独立环境的现代服务器端Java模板引擎. Thymeleaf的主要 ...
- 【操作系统之七】Linux常用命令之tail
一.概念linux tail命令用途是按照要求将指定的文件的最后部分输出到标准设备,一般是终端,就是把某个档案文件的最后几行显示到终端上,如果该档案有更新,tail会自动刷新,确保你看到最新的档案内容 ...
- cad.arx 自定义实体之编译第一个项目(甜头)
本篇不从零开始讲如何制造自定义图元,而是教新手们如何设置了环境之后编译张帆书中的代码. 利用vs2010编译 张帆<AutoCAD ObjectARX(VC)开发基础与实例教程>一书中的自 ...
- .NetCore快速搭建ELK分布式日志中心
懒人必备:.NetCore快速搭建ELK分布式日志中心 该篇内容由个人博客点击跳转同步更新!转载请注明出处! 前言 ELK是什么 它是一个分布式日志解决方案,是Logstash.Elastaics ...
- docker 学习操作记录 1
记录1 Xshell (Build ) Copyright (c) NetSarang Computer, Inc. All rights reserved. Type `help' to learn ...
- linux阿里云服务器更换镜像的方法
linux阿里云服务器更换镜像的方法 1 先进入硬盘创建快照 生成自定义镜像 ps:他可以在阿里云各个服务器上共享 再左侧镜像 点击去可以看到共享 直接进ecs 关闭服务器 重新初始化硬盘 然后主界面 ...
- Git flow 工作流与规范
概述 简版图: PS. 可能用到的命令: 1.从指定 commit拉出新分支 git checkout commitId -b 本地新branchName git checkout 9fbc3d0 ...
- [转帖]B4. Concurrent JVM 锁机制(synchronized)
B4. Concurrent JVM 锁机制(synchronized) https://www.cnblogs.com/zlxyt/p/11050346.html 挺好的 感觉这个文章写的 不过想要 ...
- php开始,html应用的一些不错收藏
来源:http://happymc.iteye.com/link?tag=%E4%B8%AA%E4%BA%BA%E6%94%B6%E8%97%8F%E7%9A%84%E5%A5%BD%E7%BD%91 ...
- 宝塔webhook配合码云,本地git push 服务器自动pull
emmmm,这其实是一个很简单的一件事情,但是有很多坑,记录一下 先大概讲一下原理吧,就是每次您 push 代码后,都会给远程 HTTP URL 发送一个 POST 请求 更多说明 » 然后在宝塔这边 ...