首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】458A - Golden System(数论 & 模拟)

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

作者头像
FishWang
发布2025-08-26 20:42:05
发布2025-08-26 20:42:05
7900
代码可运行
举报
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
复制
1000
111

output

代码语言:javascript
代码运行次数:0
运行
复制
<

input

代码语言:javascript
代码运行次数:0
运行
复制
00100
11

output

代码语言:javascript
代码运行次数:0
运行
复制
=

input

代码语言:javascript
代码运行次数:0
运行
复制
110
101

output

代码语言:javascript
代码运行次数:0
运行
复制
>

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级算法题

在黄金系统下面等于 ,其中 是0或者1。

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

Input

代码语言:javascript
代码运行次数:0
运行
复制
单组测试数据。
第一行有一个字符串a。
第二行有一个字符串b。
他们都是非空串,可能有前导0,并且只有0和1组成,长度不超过100000。

Output

代码语言:javascript
代码运行次数:0
运行
复制
如果a>b,输出>;
如果a=b,输出=;
如果a<b,输出<;

Input示例

代码语言:javascript
代码运行次数:0
运行
复制
00100
11

Output示例

代码语言:javascript
代码运行次数:0
运行
复制
=

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)这个不等式,自己思考一下,不难想。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档