24h購物| | PChome| 登入
2013-01-04 15:22:12| 人氣1,035| 回應0 | 上一篇 | 下一篇

[UVA][Trie][大數] 12333 - Revenge of Fibonacci

推薦 0 收藏 0 轉貼0 訂閱站台


  I: Revenge of Fibonacci 

The well-known Fibonacci sequence is defined as following:


F(0) = F(1) = 1  
F(n) = F(n - 1) + F(n - 2)    $displaystyle forall$n$displaystyle ge$2  

Here we regard n as the index of the Fibonacci number F(n).

This sequence has been studied since the publication of Fibonacci's book Liber Abaci. So far, many properties of this sequence have been introduced.

You had been interested in this sequence, while after reading lots of papers about it. You think there's no need to research in it anymore because of the lack of its unrevealed properties. Yesterday, you decided to study some other sequences like Lucas sequence instead.

Fibonacci came into your dream last night. ``Stupid human beings. Lots of important properties of Fibonacci sequence have not been studied by anyone, for example, from the Fibonacci number 347746739…''

You woke up and couldn't remember the whole number except the first few digits Fibonacci told you. You decided to write a program to find this number out in order to continue your research on Fibonacci sequence.

Input 

There are multiple test cases. The first line of input contains a single integer T denoting the number of test cases ( T$ le$50000).

For each test case, there is a single line containing one non-empty string made up of at most 40 digits. And there won't be any unnecessary leading zeroes.

Output 

For each test case, output the smallest index of the smallest Fibonacci number whose decimal notation begins with the given digits. If no Fibonacci number with index smaller than 100000 satisfy that condition, output `-1' instead -– you think what Fibonacci wants to told you beyonds your ability.

Sample Input 

15
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610

Sample Output 

Case #1: 0
Case #2: 25
Case #3: 226
Case #4: 1628
Case #5: 49516
Case #6: 15
Case #7: 15
Case #8: 15
Case #9: 43764
Case #10: 49750
Case #11: 10
Case #12: 51
Case #13: -1
Case #14: 1233
Case #15: 22374



前綴保留 40 位即可,將 10 萬以內的 費式樹列插進 Trie 中,
使用億進制。


#include <stdio.h>
#include <string.h>
const int base = 100000000;
int F[3][100000];
char buf[100];
struct TrieNd {
    int link[10], res;
} ND[4000000];
int NDsize = 0;
void insTrie(char *s, int val) {
    static int i, idx;
    for(i = 0, idx = 0; s[i] && i < 40; i++) {
        if(ND[idx].link[s[i]-'0'] == 0) {
            NDsize++;
            memset(&ND[NDsize], 0, sizeof(TrieNd));
            ND[idx].link[s[i]-'0'] = NDsize;
            ND[NDsize].res = val;
        }
        idx = ND[idx].link[s[i]-'0'];
    }
}
char* funcH(int n, int len) { // head 40 digits
    char *p = buf;
    for(int i = 0; i < 6 && len >= 0; i++, len--) {
        *p++ = F[n][len]/10000000%10+'0';
        *p++ = F[n][len]/1000000%10+'0';
        *p++ = F[n][len]/100000%10+'0';
        *p++ = F[n][len]/10000%10+'0';
        *p++ = F[n][len]/1000%10+'0';
        *p++ = F[n][len]/100%10+'0';
        *p++ = F[n][len]/10%10+'0';
        *p++ = F[n][len]%10+'0';
    }
    *p = '\0';
    p = buf;
    while(*p == '0')    p++;
    return p;
}
void funcF() {
    F[0][0] = 1;
    F[1][0] = 1;
    insTrie(funcH(0, 0), 0);
    int fa = 0, fb = 1, fc = 2;
    int len = 0, i, j;
    for(i = 2; i < 100000; i++) {
        for(j = 0; j <= len; j++)
            F[fc][j] = F[fa][j] + F[fb][j];
        for(j = 0; j <= len; j++) {
            if(F[fc][j] >= base) {
                F[fc][j+1] += F[fc][j]/base;
                F[fc][j] %= base;
            }
        }
        if(F[fc][len+1])    len++;
        insTrie(funcH(fc, len), i);
        fa++, fb++, fc++;
        if(fa == 3) fa = 0;
        if(fb == 3) fb = 0;
        if(fc == 3) fc = 0;
    }
}
int main() {
    memset(&ND[0], 0, sizeof(TrieNd));
    funcF();
    int t, cases = 0;
    char cmd[50];
    int i, idx;
    scanf("%d", &t);
    while(t--) {
        scanf("%s", cmd);
        for(i = 0, idx = 0; cmd[i]; i++) {
            if(!ND[idx].link[cmd[i]-'0'])
                break;
            idx = ND[idx].link[cmd[i]-'0'];
        }
        int res = -1;
        if(cmd[i] == '\0')
            res = ND[idx].res;
        printf("Case #%d: %d\n", ++cases, res);
    }
    return 0;
}


台長: Morris
人氣(1,035) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][掃描線] 972 - Horizon Line
此分類上一篇:[UVA][拓樸排序] 1119 - Project File Dependencies

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文