Description

Golden ratio base (GRB) is a non-integer positional numeral system that uses the golden ratio (the irrational number (1+√5)/2 ≈ 1.61803399 symbolized by the Greek letter φ) as its base. It is sometimes referred to as base-φ, golden mean base, phi-base, or, phi-nary.       
Any non-negative real number can be represented as a base-φ numeral using only the digits 0 and 1, and avoiding the digit sequence "11" � this is called a standard form. A base-φ numeral that includes the digit sequence "11" can always be rewritten in standard form, using the algebraic properties of the base φ ― most notably that φ + 1 = φ 2 . For instance, 11(φ) = 100(φ). Despite using an irrational number base, when using standard form, all on-negative integers have a unique representation as a terminating (finite) base-φ expansion. The set of numbers which possess a finite base-φ representation is the ring Z[1 + √5/2]; it plays the same role in this numeral systems as dyadic rationals play in binary numbers, providing a possibility to multiply.       
Other numbers have standard representations in base-φ, with rational numbers having recurring representations. These representations are unique, except that numbers (mentioned above) with a terminating expansion also have a non-terminating expansion, as they do in base-10; for example, 1=0.99999….       
Coach MMM, an Computer Science Professor who is also addicted to Mathematics, is extremely interested in GRB and now ask you for help to write a converter which, given an integer N in base-10, outputs its corresponding form in base-φ.      
              

Input

There are multiple test cases. Each line of the input consists of one positive integer which is not larger than 10^9. The number of test cases is less than 10000. Input is terminated by end-of-file.      
              

Output

For each test case, output the required answer in a single line. Note that trailing 0s after the decimal point should be wiped. Please see the samples for more details.      
              

Sample Input

1
2
3
6
10
              

Sample Output

1
10.01
100.01
1010.0001
10100.0101

Hint

 
 
由于φ + 1 = φ 2,两边同乘φ k,得到φ k+1+φ k=φ k+2,说明只有有两位是1,就往前进一位。此外由φ + 1 = φ 2推到的2φ 2=φ 3+1,同理可知:φ k+3+φ k=2φ k+2,说明每一位的2都可以,由它前一位和它的后两位的1构成,这样就能将所有大于2的数降成1.再配合之前的,反复模拟便可得。由于当场没有估算这个数的长度,所以采用两个数组分别存了整数部分和小数部分。整体效率不是非常高,但是在短时间内做出来还是很高兴的。
 
 
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define esp 1e-10
#define N 100 using namespace std; int z[N], x[N], lenz, lenx; bool judge ()
{
if(z[0] && x[0])
return 0;
for (int i = 0; i < lenx; ++i)
if (x[i] > 1 || (x[i] && x[i+1]))
return 0; for (int i = 0; i < lenz; ++i)
if (z[i] > 1 ||(z[i] ==1 && z[i+1] == 1))
return 0; return 1;
} void doz (int i)
{
if (i == lenz-1)
lenz++;
int up = z[i] / 2;
z[i] = z[i] & 1;
z[i+1] += up;
if (i >= 2)
z[i-2] += up;
else
{
if (lenx < 3 - i)
lenx = 3 - i;
x[1-i] += up;
}
} void dox (int i)
{
if (i+3 > lenx)
lenx = i + 3;
int up = x[i] / 2;
x[i] = x[i] & 1;
x[i+2] += up;
if (i == 0)
z[0] += up;
else
x[i-1] += up;
} void qt (int n)
{
memset (z, 0, sizeof(z));
memset (x, 0, sizeof(x));
lenz = 1;
lenx = 0;
z[0] = n;
while (!judge ())
{
for (int i = lenx-1; i >= 0; --i)
{ if (i == 0 && x[i] > 0 && x[i+1] > 0)
{
int up = min (x[i], x[i+1]);
z[0] += up;
x[0] -= up;
x[1] -= up;
continue;
}
else if (x[i] > 0 && x[i+1] > 0)
{
int up = min (x[i], x[i+1]);
x[i-1] += up;
x[i+1] -= up;
x[i] -= up;
continue;
}
if (x[i] > 1)
{
dox (i);
continue;
} }
while(x[lenx-1] == 0)
lenx--;
for (int i = 0; i < lenz; ++i)
{ if (i == 0 && z[i] > 0 && x[0] > 0)
{
if (i == lenz-1)
lenz++;
int up = min (z[i], x[0]);
z[1] += up;
z[0] -= up;
x[0] -= up;
continue;
}
else if (z[i] > 0 && z[i+1] > 0)
{
if (i+3 > lenz)
lenz = i + 3;
int up = min (z[i], z[i+1]);
z[i+2] += up;
z[i+1] -= up;
z[i] -= up;
continue;
}
if (z[i] > 1)
{
doz(i);
continue;
}
}
}
while(x[lenx-1] == 0)
lenx--;
} int main()
{
//freopen ("test.txt", "r", stdin);
int n;
while (scanf ("%d", &n) != EOF)
{
qt (n);
for (int i = lenz - 1; i >= 0; --i)
printf ("%d", z[i]);
if (lenx > 0)
printf (".");
for (int i = 0; i < lenx; ++i)
printf ("%d", x[i]);
printf ("\n");
}
return 0;
}

ACM学习历程——HDU4814 Golden Radio Base(数学递推) (12年成都区域赛)的更多相关文章

  1. ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)

    Sample Input 9 5 6 7 8 113 1205 199312 199401 201314 Sample Output Case #1: 5 Case #2: 16 Case #3: 8 ...

  2. ACM学习历程—HDU1023 Train Problem II(递推 && 大数)

    Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know  ...

  3. ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)

    Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...

  4. ACM学习历程—HDU 5326 Work(树形递推)

    Problem Description It’s an interesting experience to move from ICPC to work, end my college life an ...

  5. AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)

    Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...

  6. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  7. ACM学习历程—SNNUOJ1213 加油站问题(动态规划 || 数学)

    题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1213 这是这次微软实习面试的一道题,当时只相出了一个2n的做法,面试官让我优化成n的做 ...

  8. ACM学习历程—HDU 5073 Galaxy(数学)

    Description Good news for us: to release the financial pressure, the government started selling gala ...

  9. ACM学习历程—FZU2191完美的数字(数学)

    Description Bob是个很喜欢数字的孩子,现在他正在研究一个与数字相关的题目,我们知道一个数字的完美度是 把这个数字分解成三个整数相乘A*A*B(0<A<=B)的方法数,例如数字 ...

随机推荐

  1. u-boot-2014-04 网络不通解决一例

    不久前我移植了u-boot-214-04到Tq2440的板子上,基本功能都有了,网卡也可以使用了.有一天打算把u-boot-2010-06也也一直到tq2440上,移植完后发现u-boot-214-0 ...

  2. html中文件类型的accept属性有哪些

    *.3gpp audio/3gpp, video/3gpp 3GPP Audio/Video *.ac3 audio/ac3 AC3 Audio *.asf allpication/vnd.ms-as ...

  3. PHP和mysql的长连接

    关于 PHP MySQL 长连接.连接池的一些探索 PHP连接MySQL的方式,用的多的是mysql扩展.mysqli扩展.pdo_mysql扩展,是官方提供的.php的运行机制是页面执行完会释放所有 ...

  4. 【Python + Selenium】初次用IE浏览器之报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones.

    初次用IE浏览器运行自动化程序时,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launchi ...

  5. pooler [转]

    pooler和poolboy都是用erlang写的管理进程池的库. pooler/poolboygithub : seth/pooler · GitHubgithub : devinus/poolbo ...

  6. zoj 2068 - Chopsticks

    题目:非常多人在一起吃饭.有两组单支的筷子,定义badness为一对筷子长度差的平方,求最小的badness和. 分析:dp,最大公共子序列类似物. 这里利用数学关系找到一个结论: a < b ...

  7. C - The C Answer (2nd Edition) - Exercise 1-1

    /* Run the "hello, world" program on your system. Experiment with leaving out parts of the ...

  8. python 基础 1.5 python 数据类型(一)--整型 浮点型 布尔型及字符串和常用方法

    一.python 数据类型:数值,字符串,列表,元组,字典.以下操作是在linux 下 ipython中进行 1.数值 1>123  与  “123”的区别 答:123为数值,“123”在pyt ...

  9. IOS GameCenter验证登陆

    #import "GameKitHelper.h" #import "GameConstants.h" @interface GameKitHelper () ...

  10. 将众多小文件输入Hadoop的解决方案 可挂载的HDFS

    配置HDFS为可挂载后: 1-可挂载后才支持非完整POSIX语义: 2-仍然不支持随机写入,仍然为“一次写入,多次读取”: 3-可能误用,导致众多小文件: : 1-使用Solr存储和检索小文件: 2- ...