码迷,mamicode.com
首页 > 其他好文 > 详细

51NOD 1491 黄金系统 && Codeforces 458 A. Golden System(斐波那契数列 + 找规律)

时间:2016-08-21 12:33:33      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

传送门
q = 5+12在黄金系统下面a0a1...an等于 ni=0ai?qn?i,其中ai0 或者 1

现在给出两个黄金系统下面的数字,请比较他们的大小。

Input

单组测试数据。

第一行有一个字符串 a

第二行有一个字符串 b

他们都是非空串,可能有前导 0,并且只有 01组成,长度不超过 100000

Output

如果 a>b,输出 >

如果 a b,输出 =

如果 a<b,输出 <

Input示例

00100
11

Output示例

=

解题思路:

其实通过样例我们可以发现这样一个规律: qn = qn?1 + qn?2,那么我们现在知道这个规律

我们就可以,通过将所有 ai1 的 用一个数组 f 保存下来,如果是 字符串 a 的话 f[n?i?1]

就加加,如果是 b 的话,就减减, 然后一步步转移到 f[0]  f[1] 上,然后判断这两个数就行

了。因为中间转移的时候 会爆 long long 所以,我们将 f 数组定义成一个 double 的就可以了,

就最后判断 一下 q?f[1]+f[0] 的符号就行了。

My Code

/**
2016 - 08 - 19 下午
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9+5;
const int MAXN = 1e5+5;
const int MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
LL Scan_LL()///输入外挂
{
    LL res=0,ch,flag=0;
    if((ch=getchar())==‘-‘)
        flag=1;
    else if(ch>=0&&ch<=9)
        res=ch-0;
    while((ch=getchar())>=0&&ch<=9)
        res=res*10+ch-0;
    return flag?-res:res;
}
int Scan_Int()///输入外挂
{
    int res=0,ch,flag=0;
    if((ch=getchar())==‘-‘)
        flag=1;
    else if(ch>=0&&ch<=9)
        res=ch-0;
    while((ch=getchar())>=0&&ch<=9)
        res=res*10+ch-0;
    return flag?-res:res;
}
void Out(LL a)///输出外挂
{
    if(a>9)
        Out(a/10);
    putchar(a%10+0);
}
char a[MAXN], b[MAXN];
double f[MAXN];
int main()
{
    while(~scanf("%s%s",a,b))
    {
        int lena = strlen(a);
        int lenb = strlen(b);
        memset(f, 0, sizeof(f));
        for(int i=0; i<lena; i++)
            if(a[i] == 1)
                f[lena-i-1]++;
        for(int i=0; i<lenb; i++)
            if(b[i] == 1)
                f[lenb-i-1]--;
        int tmp = max(lena, lenb);
        for(int i=tmp-1; i>=2; i--)
        {
            f[i-1] += f[i];
            f[i-2] += f[i];
        }
        if(!f[0] && !f[1])
        {
            puts("=");
            continue;
        }
        double ans = (sqrt(5.0)+1)*0.5*f[1] + f[0];
        if(ans < 0.0)
            puts("<");
        else
            puts(">");
    }
    return 0;
}

51NOD 1491 黄金系统 && Codeforces 458 A. Golden System(斐波那契数列 + 找规律)

标签:

原文地址:http://blog.csdn.net/qingshui23/article/details/52266534

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!