1. 程式人生 > >UVA 題目11089 Fi-binary Number(找規律)

UVA 題目11089 Fi-binary Number(找規律)

A Fi-binary number is a number that contains only 0 and 1. It does not contain any leading 0. And
also it does not contain 2 consecutive 1. The first few such number are 1, 10, 100, 101, 1000, 1001,
1010, 10000, 10001, 10010, 10100, 10101 and so on. You are given n. You have to calculate the n-th
Fi-Binary number.
Input
The first line of the input contains one integer T the number of test cases. Each test case contains one
integer n.
Output
For each test case output one line containing the n-th Fi-Binary number.
Constraints
• 1 ≤ N ≤ 109
Sample Input
4
10
20
30
40
Sample Output
10010
101010
1010001

10001001

題目大意:是Fi-binary Number的定義時由0和1構成,且沒有連續的兩個一,從第一個往後一次是 1, 10, 100, 101, 1000, 1001,
1010, 10000, 10001, 10010, 10100, 10101 。。。。。,現在給你一個n問第n項是什麼

窩找的規律麻煩了一點。。。看別人找的挺短的,,莫名uva過了,zzulioj過不了。。

我的規律是;長度相等的個數成斐波那契數,長度1的有1個,長度2的有1,長度3的有兩個,長度4的有3個。。

例如第20項為101010,先求出n屬於第幾個長度,答案是6,101010=100000+1010,,1010又等於1000+10,10是2的冪次方了(第某長度第一項。。)就結束了。。就是這個從長的往短的找。。

ac程式碼

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#define LL long long
#define INF 0x3f3f3f3f3f3f
using namespace std;
LL fac[55],sum[55];
void init()
{
    int i=1;
    fac[1]=1;
    sum[1]=1;
    sum[2]=2;
    fac[2]=1;
    for(i=3;i<=45;i++)
    {
        fac[i]=fac[i-1]+fac[i-2];
        sum[i]=sum[i-1]+fac[i];
    }
}
int bseach(LL val)
{
    int l=1,r=45;
    int ans=45;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(sum[mid]>=val)
        {
            ans=mid;
            r=mid-1;
        }
        else
            l=mid+1;
    }
    return ans;
}
LL bit[60];
LL Pow(LL a,int n)
{
    if(n==0)
        return 1;
    LL ans=1;
    int i;
    for(i=1;i<=n;i++)
        ans*=a;
    return ans;
}
int main()
{
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        LL n;
        int i;
        scanf("%lld",&n);
        int c=bseach(n);
        //printf("%d\n",c);
        LL ans=0;
        ans+=Pow(2,c-1);
        while(n-sum[c-1]!=1)
        {
            n=n-sum[c-1]-1;
            //printf("%lld\n",n);
            c=bseach(n);
            ans+=Pow(2,c-1);
        }
        int k=0;
        while(ans)
        {
            LL temp=ans%2;
            bit[k++]=temp;
            ans/=2;
        }
        for(i=k-1;i>=0;i--)
            printf("%lld",bit[i]);
        printf("\n");
    }
}


相關推薦

UVA 題目11089 Fi-binary Number規律

A Fi-binary number is a number that contains only 0 and 1. It does not contain any leading 0. And also it does not contain 2 consecutive

2018 ICPC 沈陽網絡賽預賽 Supreme Number規律

std str c_str 位數 clu lib 由於 for else 【傳送門】https://nanti.jisuanke.com/t/31452 【題目大意】:給定一個數字(最大可達10100),現在要求不超過它的最大超級質數。超級質數定義:對於一個數,把它看成

Happy Number規律

[提交] [狀態] [討論版] [命題人:admin] 題目描述 Consider the following function f defined for any natural number n: f(n) is the number obtained by summing up

UVA 11384 最少操作使數列變為零 規律

用最少的操作次數把序列1,2,....,n 中的所有數都變成0. #include<iostream> #include<cstdio> #include<cstr

Number Sequence規律

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 151858    Acc

hdu6198 number number number規律+矩陣快速冪

Problem Description We define a sequence F: ⋅ F0=0,F1=1; ⋅ Fn=Fn−1+Fn−2 (n≥2). Give you an integer k, if a positive number n ca

hihocode 1584 : Bounce 規律2017 北京網絡賽G

hihocode sin scanf 容易 pac pre ace clas esp 題目鏈接 比賽時隨便找了個規律,然後隊友過了。不過那個規律具體細節還挺煩的。剛剛偶然看到Q巨在群裏提到的他的一個思路,妙啊,很好理解,而且公式寫起來也容易。OrzQ巨 #includ

Fibonacci數列規律

math println scanner line for each str BE java void 題目描述 Fibonacci數列是這樣定義的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibo

構造隊列規律

發現 void desc ger 一個空格 .... esc exti () 題目描述 小明同學把1到n這n個數字按照一定的順序放入了一個隊列Q中。現在他對隊列Q執行了如下程序: while(!Q.empty()) //隊列不空,執行循環 {

xdoj 1330---異或規律

ref pri discus 運算 討論版 \n 打表 discuss 一道 我是打表找的規律 233 樣例什麽作用都沒有 只會迷惑作用。。。 1330: 天才琪露諾的完美算數教室 時間限制: 1 Sec 內存限制: 128 MB Special Judge提

Segments CodeForces 909B 規律

arr ssi wid mov single 分享圖片 要求 num 最優 Description You are given an integer N. Consider all possible segments (線段,劃分)on the coo

Codeforces Round #514 (Div. 2) C.Sequence Transformation規律

題意:給定一個數字,找從1到n這n個數字的最大公約數,然後從這n個數字中去掉一個數字,使得去掉之後剩下的n-1個數字的最大公約數能比之前的大,每次都去掉一個數字,使得最大公約數能夠最快的上升。 思路:列出1到9的答案:1:1,2:12,3:113,4:1124,5:11124,6:111226,

HDU 6267 Master of Random 規律

題意: 給你n(n<=1e5)個節點。第i個節點的父親是隨機的,範圍為0~i-1。 定義一棵樹的值為    其中val[i]為以i為根的子樹的所有點的權值之和。 給你a[i](i=1~n)(每個點的權值),求所有可能的樹的平均值。

HDU 3788——ZOJ問題規律

題目連結 從樣例中找到其規律 就是第一次出現z的之前字元的個數 ×第一次出現z和最後出現j之間的‘o’的個數(且不能為空)=最後出現j之後的字元個數Accepted;(同理zoj也適用) #include<iostream> #include<set> #incl

hihoCoder - 1878 Palindromes 規律2018ICPC北京I

時間限制:1000ms 單點時限:1000ms 記憶體限制:512MB 描述 Recently, Nvoenewr learnt palindromes in his class. A palindrome is a nonnegative integer that is t

ACM 2018 北京區域賽 I - Palindromes 規律

HihoCoder - 1878 題目大意:        給出k,讓求出第k個迴文數(k的“長度”不超過1e5) 題解:        真是一道給人警醒的題目 &

HDU-4015:Mario and Mushrooms規律

Mario and Mushrooms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe

zcmu-2269: Double Cola規律

2269: Double Cola Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 34  Solved: 26 [Submit][Status][Web Board] Description time limit

55 博弈規律

Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the game an

Pagodas 規律

n pagodas were standing erect in Hong Jue Si between the Niushou Mountain and the Yuntai Mountain, labelled from 1 to n. However, only two