UVaLive 3357 Pinary (Fib数列+递归)
题意:求第 k 个不含前导 0 和连续 1 的二进制串。
析:1,10,100,101,1000,...很容易发现长度为 i 的二进制串的个数正好就是Fib数列的第 i 个数,因为第 i 个也有子问题,其子问题也就是Fib,这样就可以用递归来解决了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int maxn = 1000 + 10;
const int maxm = 1e5 + 10;
const LL mod = 1000000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x; scanf("%d", &x); return x; }
vector<int> v; void dfs(int n, int last){
if(n == 0){
while(last-- > 0) putchar('0');
return ;
}
int pos = lower_bound(v.be, v.ed, n) - v.be;
if(n < v[pos]) --pos;
for(int i = pos; i < last; ++i) putchar('0');
putchar('1');
dfs(n-v[pos], pos-1);
} int main(){
v.pb(1); v.pb(1);
for(int i = 2; i < 40; ++i) v.pb(v[i-1] + v[i-2]);
v[0] = 0;
int T; cin >> T;
while(T--){
scanf("%d", &n);
dfs(n, -1);
putchar('\n');
}
return 0;
}
UVaLive 3357 Pinary (Fib数列+递归)的更多相关文章
- 动态规划之Fib数列类问题应用
一,问题描述 有个小孩上楼梯,共有N阶楼梯,小孩一次可以上1阶,2阶或者3阶.走到N阶楼梯,一共有多少种走法? 二,问题分析 DP之自顶向下分析方式: 爬到第N阶楼梯,一共只有三种情况(全划分,加法原 ...
- 1022. Fib数列
https://acm.sjtu.edu.cn/OnlineJudge/problem/1022 Description 定义Fib数列:1,1,2,3,5,8,13,…1,1,2,3,5,8,13, ...
- FIB数列
斐波那契级数除以N会出现循环,此周期称为皮萨诺周期. 下面给出证明 必然会出现循环 这是基于下面事实: 1. R(n+2)=F(n+2) mod P=(F(n+1)+F(n)) mod P=(F(n+ ...
- bzoj5104: Fib数列
Description Fib数列为1,1,2,3,5,8... 求在Mod10^9+9的意义下,数字N在Fib数列中出现在哪个位置 无解输出-1 Input 一行,一个数字N,N < = 10 ...
- 【bzoj5118】Fib数列2 费马小定理+矩阵乘法
题目描述 Fib定义为Fib(0)=0,Fib(1)=1,对于n≥2,Fib(n)=Fib(n-1)+Fib(n-2) 现给出N,求Fib(2^n). 输入 本题有多组数据.第一行一个整数T,表示数据 ...
- HDU3977 Evil teacher 求fib数列模p的最小循环节
In the math class, the evil teacher gave you one unprecedented problem! Here f(n) is the n-th fibona ...
- [bzoj5118]Fib数列2_费马小定理_矩阵乘法
Fib数列2 bzoj-5118 题目大意:求Fib($2^n$). 注释:$1\le n\le 10^{15}$. 想法:开始一看觉得一定是道神题,多好的题面啊?结果...妈的,模数是质数,费马小定 ...
- 【BZOJ5104】Fib数列(BSGS,二次剩余)
[BZOJ5104]Fib数列(BSGS,二次剩余) 题面 BZOJ 题解 首先求出斐波那契数列的通项: 令\(A=\frac{1+\sqrt 5}{2},B=\frac{1-\sqrt 5}{2}\ ...
- @bzoj - 5104@ Fib数列
目录 @description@ @solution@ @accepted code@ @details@ @description@ Fib数列为1,1,2,3,5,8... 求在Mod10^9+9 ...
随机推荐
- iOS - 常用本机URL跳转设置
import UIKit class ViewController: UIViewController { override func touchesBegan(_ touches: Set<U ...
- SAP transportation
1.CONFIGURATION TRANSPORT flow:DEV(100) --scc1--> DEV(400) --STMS after release-> QAS(510) --S ...
- 云计算之 PaaS详解
PaaS是Platform-as-a-Service的缩写,意思是平台即服务. Paas - 概述 计算机技术 PaaS(Platform-as-a-Service:平台即服务) 全称:(Platfo ...
- errror:[test_rig3.launch] is neither a launch file in package [svo_ros] nor is [svo_ros] a launch file name The traceback for the exception was written to the log file
1. 打开一个终端,运行roscore 2. 打开另一个终端,运行 roslaunch svo_ros test_rig3.launch 出现errror: 忘记关键步骤了 $ cd <path ...
- grep与正则表达式的使用
正则表达式以及grep的使用 grep是一种文本过滤工具(模式:pattern)基本使用用法如下: grep [option] PATTERN FILE grep [OPTIONS] [-e PATT ...
- go语言websocket使用与客户端html5调用
我们通过使用如下库创建websocket服务 go get golang.org/x/net/websocket websocket服务端的代码如下: package main; import ( & ...
- rbac 表结构的。设计
1. 问:为什么程序需要权限控制? 答:生活中的权限限制,① 看灾难片电影<2012>中富人和权贵有权登上诺亚方舟,穷苦老百姓只有等着灾难的来临:② 屌丝们,有没有想过为什么那些长得漂亮身 ...
- StackExchange.Redis使用以及封装
来源:http://www.cnblogs.com/qtqq/p/5951201.html Redis安装:http://www.runoob.com/redis/redis-install.html ...
- Vue 快速原型开发
快速原型开发 注意: 是:serve 而不是 server 通过使用 vue serve 和 vue build 命令对单个 *.vue 文件进行快速原型开发,不过这需要先额外安装一个全局的扩展 go ...
- 制作根文件系统之内核如何启动init进程
start_kernel其实也是内核的一个进程,它占用了进程号0,start_kernel的内容简写如下: asmlinkage void __init start_kernel(void) //内核 ...