【CodeForces】458A - Golden System(数论 & 模拟)
2016-08-16 11:33
716 查看
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
output
input
output
input
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=0ai∗qn−i ,其中 ai 是0或者1。
现在给出两个黄金系统下面的数字,请比较他们的大小。
Input
Output
Input示例
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)这个不等式,自己思考一下,不难想。
代码如下:
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=0ai∗qn−i ,其中 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;
}
相关文章推荐
- CF457A:Golden System(数论 & 进制)
- 51NOD 1491 黄金系统 && Codeforces 458 A. Golden System(斐波那契数列 + 找规律)
- Codeforces 475C Kamal-ol-molk's Painting 模拟
- CodeForces - 368C Sereja and Algorithm (找规律&模拟)
- [数论] 51Nod 1217 Minimum Modular & Codeforces 303C #183 (Div. 1) Minimum Modular
- 【Codeforces 868 B. Race Against Time】& 模拟
- Codeforces 757F && NOIP2017 2017-09-28模拟T3
- Codeforces 475C Kamal-ol-molk's Painting 模拟
- codeforces-16A-A. Flag (C && 简单模拟 && 暴力农夫山泉)
- codeforces-3A-A. Winner (C++ && 简单模拟)
- 【恶心的模拟 && 找时刻问题】CodeForces - 883A Automatic Door
- HDU1013 Digital Roots 模拟&数论(我不会数论)
- Codeforces--658C--Bear and Forgotten Tree 3(模拟&&技巧)(好题)
- CodeForces - 659C Tanya and Toys (map&模拟)
- Codeforces Round #378 (Div. 2) C && Codeforces 733C (模拟)详解(两种写法)
- [Codeforces 66C] Petya and File System (模拟)
- ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)
- !codeforces 399C Cards-数论&贪心-(暴力枚举)
- CodeForces - 670D2 Magic Powder - 2 (二分&模拟)
- 【GDKOI2012模拟02.01】数字(找规律&&数论)