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;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/8783458.html

时间: 2024-08-25 05:15:09

UVaLive 3357 Pinary (Fib数列+递归)的相关文章

动态规划之Fib数列类问题应用

一,问题描述 有个小孩上楼梯,共有N阶楼梯,小孩一次可以上1阶,2阶或者3阶.走到N阶楼梯,一共有多少种走法? 二,问题分析 DP之自顶向下分析方式: 爬到第N阶楼梯,一共只有三种情况(全划分,加法原理),从第N-1阶爬1阶到第N阶:从第N-2阶爬2阶到第N阶:从第N-3爬3阶到第N阶. 故:way(N)=way(N-1)+way(N-2)+way(N-3) 这与求Fib数列非常相似,当然,其他类似的问题也可以这样求解. 初始条件: way(1)=1 way(2)=2 way(3)=4 三,代码

acm 之fib数列——java

1022. Fib数列 Description 定义Fib数列:1,1,2,3,5,8,13,… 求第N项除以2010的余数 Input Format 输入仅一行,为一个整数N Output Format 输出仅一行,为第N项除以2010的余数 Sample Input 3 Sample Output 2 Limits: 对于70%的数据 N≤1,000,000 对于100%的数据 N≤210,000,000,000 这道题最让人着急的是数太大了,而且不可以使用递归,这种方法会暴栈,但是java

Fibonacci数列递归实现

一.什么是Fibonacci数列? 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为"兔子数列",指的是这样一个数列:1.1.2.3.5.8.13.21.34.--在数学上,斐波那契数列以如下被以递推的方法定义:F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)(n>=3,n∈N*) 由定义可知,这个数列从第3项开始,每一项都等于前两项之和

HDU 1005 Number Sequence 矩阵乘法 Fib数列

原题: http://acm.hdu.edu.cn/showproblem.php?pid=1005 题目大意: 按规律求出第n项. 由矩阵乘法我们可以知道: 所以对于fib数列我们可以用矩阵来求,由于矩阵可以左乘右乘,所以我们可以用快速幂来优化. #include<iostream> #include"string.h" #include<stdio.h> using namespace std; const int bc=2; const int mod =

FIB数列

斐波那契级数除以N会出现循环,此周期称为皮萨诺周期. 下面给出证明 必然会出现循环 这是基于下面事实: 1. R(n+2)=F(n+2) mod P=(F(n+1)+F(n)) mod P=(F(n+1) mod p +F(n) modp) mod p 2. 斐波那契数列的最大公约数定理:gcd(F(m),F(n))=F(gcd(m,n)) 最大公约数定理表明如果F(k)能被N整除,则F(ik)也能被N整除,这就表明了斐波那契数列所含因子的周期性,下面列举: 因子:2,3,4,5, 6,7,8,

1022. Fib数列

Description 定义Fib数列:1,1,2,3,5,8,13,- 求第N 项除以2010 的余数 Input Format 输入仅一行,为一个整数N Output Format 输出仅一行,为第N 项除以2010 的余数 Sample Input 3 Sample Output 2 Limits: 对于70%的数据 N≤1,000,000 对于100%的数据 #include<iostream>using namespace std; int main(){    int t;    

[bzoj5118]Fib数列2_费马小定理_矩阵乘法

Fib数列2 bzoj-5118 题目大意:求Fib($2^n$). 注释:$1\le n\le 10^{15}$. 想法:开始一看觉得一定是道神题,多好的题面啊?结果...妈的,模数是质数,费马小定理就tm完事了,将fib数列的通项公式列出来然后费马小定理... 最后,附上丑陋的代码... ...(照着郭爷一顿瞎jb敲) #include <iostream> #include <cstdio> #include <cstring> #include <algo

斐波那契数列递归内存溢出如何解决

斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368........某一项是前两项的和.使用递归调用时前四十项求解没有问题,但到底五十项的时候会出现内存溢出,求不出结果.所以要想求出更多的项必须使用非递归的方法求解,数据类型不能再是int,可以为double. 1.内存溢出的实例 package test.only

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

斐波那契数列:1, 1, 2, 3, 5, 8, 13,...,即 f(n) = f(n-1) + f(n-2). 求第n个数的值. 方法一:迭代 public static int iterativeFibonacci(int n) { //简单迭代 int a = 1, b = 1; for(int i = 2; i < n; i ++) { int tmp = a + b; a = b; b = tmp; } return b; } 方法二:简单递归 public static long