【CodeForces】458A - Golden System(数论 & 模拟)

    xiaoxiao2026-03-31  5

    51Nod - 1491链接:点击打开题目

    CodeForces - 458A链接:点击打开题目

    A. Golden System time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

    Piegirl got bored with binary, decimal and other integer based counting systems. Recently she discovered some interesting properties about number , in particular that q2 = q + 1, and she thinks it would make a good base for her new unique system. She called it "golden system". In golden system the number is a non-empty string containing 0's and 1's as digits. The decimal value of expressiona0a1...an equals to .

    Soon Piegirl found out that this system doesn't have same properties that integer base systems do and some operations can not be performed on it. She wasn't able to come up with a fast way of comparing two numbers. She is asking for your help.

    Given two numbers written in golden system notation, determine which of them has larger decimal value.

    Input

    Input consists of two lines — one for each number. Each line contains non-empty string consisting of '0' and '1' characters. The length of each string does not exceed 100000.

    Output

    Print ">" if the first number is larger, "<" if it is smaller and "=" if they are equal.

    Examples input 1000 111 output < input 00100 11 output = input 110 101 output > Note

    In the first example first number equals to , while second number is approximately1.6180339882 + 1.618033988 + 1 ≈ 5.236, which is clearly a bigger number.

    In the second example numbers are equal. Each of them is  ≈ 2.618.

    中文题目:

    1491 黄金系统 题目来源:  CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40  难度:4级算法题  收藏  取消关注

    q=5+12 在黄金系统下面 a0a1...an 等于  ni=0aiqni  ,其中  ai  是0或者1。

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

    Input 单组测试数据。 第一行有一个字符串a。 第二行有一个字符串b。 他们都是非空串,可能有前导0,并且只有0和1组成,长度不超过100000。 Output 如果a>b,输出>; 如果a=b,输出=; 如果a<b,输出<; Input示例 00100 11 Output示例 =

    CF上给出了一个公式:q^2 = q + 1。即斐波那契数列的递推公式。

    还有一个不等式:q^n>q^0+q^1+q^2+...+q^(n-2)

    那么我们把两个串先合并同类项,变成一个式子,用0、1、-1表示。然后用递推公式把数全部推到num[ 0 ]和num[ 1 ]

    最后判断 q*num[1] + num[0] 的正负即可。

    这里说明一下,如果中间num的某一项为2或者-2就可以直接输出了,因为:q^n>q^0+q^1+q^2+...+q^(n-2)这个不等式,自己思考一下,不难想。

    代码如下:

    #include <stdio.h> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define CLR(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f #define LL long long #define MAX 100000 int main() { char a[MAX+11]; char b[MAX+11]; int num[MAX+11]; double q = (sqrt(5)+1) / 2.0; scanf ("%s%s",a,b); int l1,l2,l; l1 = strlen(a); l2 = strlen(b); l = max (l1 , l2); l1--; l2--; for (int i = 0 ; i < l ; i++) { if (l1 != -1 && l2 != -1) { num[i] = (a[l1] > b[l2]); if (a[l1] < b[l2]) num[i] = -1; l1--; l2--; } else if (l1 == -1) { if (b[l2] == '1') num[i] = -1; else num[i] = 0; l2--; } else { if (a[l1] == '1') num[i] = 1; else num[i] = 0; l1--; } } for (int i = l-1 ; i >= 2 ; i--) { if (num[i] >= 2) //q^n>q^0+q^1+q^2+...+q^(n-2) { puts(">"); return 0; } else if (num[i] <= -2) { puts("<"); return 0; } else if (num[i] == 1) { num[i-1]++; num[i-2]++; } else if (num[i] == -1) { num[i-1]--; num[i-2]--; } } if (num[1]*q + num[0] > 0) puts(">"); else if (num[1]*q + num[0] < 0) puts("<"); else puts("="); return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1308366.html
    最新回复(0)