LA 3357 (recursive search rule) pinary

Source: Internet
Author: User

n bits do not contain leading 0 without a number of consecutive 1 total fib (n), FIB (n) is the Fibonacci sequence.

So you can preprocess the FIB prefix and look for the nth number is the K-digit number, and then recursively calculate how many bits of the k-digit it is.

For example, to find a 11th number, find it is a 5-digit number, so the highest bit is a 1, and then it is the 4th number in the 5-digit number.

This is to find the third number, because the following number allows leading zeros, the third number is 100, so the 5th bit is 1, the 4th bit is 0, the back three bits is 100,

The answer is 10100.

It's a bit verbose, but it's easy to understand how to simulate it on paper.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6typedefLong LongLL;7 8 Const intMAXF =90000000+5;9 Const intMAXN = -;Ten  One LL FIB[MAXN], SUM[MAXN]; A intans[ -], p =0; -  - voidFintN) the { -     if(n = =0)return; -     intdigits = Lower_bound (sum+1, Sum+p, N)-sum; -ans[digits-1] =1; +N-= sum[digits-1]; -n--; + f (n); A } at  - intMain () - { -     intT, N; scanf"%d", &T); -  -fib[1] = fib[2] =1; in      for(P =3; fib[p-1] < MAXF; p++) Fib[p] = fib[p-1] + fib[p-2]; -sum[1] = fib[1]; to      for(inti =2; I < P; i++) Sum[i] = sum[i-1] +Fib[i]; +  -      while(t--) the     { *scanf"%d", &n); $memset (ans,0,sizeof(ans));Panax Notoginseng         intD = Lower_bound (sum+1, Sum+p, N)-sum; - f (n); the          for(inti = d1; I >=0; i--) printf ("%d", Ans[i]); +printf"\ n"); A     } the  +     return 0; -}
code June

LA 3357 (recursive search rule) pinary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.