[UVA][Trie][大] 12333 - Revenge of Fibonacci@Morris' Blog|PChome Online 人新台
2013-01-04 15:22:12| 人1,083| 回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 wht 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,083) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][描] 972 - Horizon Line
此分上一篇:[UVA][拓排序] 1119 - Project File Dependencies

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86