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.再配合之前的,反复模拟便可得。由于当场没有估算这个数的长度,所以采用两个数组分别存了整数部分和小数部分。整体效率不是非常高,但是在短时间内做出来还是很高兴的。
 
 
代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <vector>
  10. #include <queue>
  11. #include <string>
  12. #define inf 0x3fffffff
  13. #define esp 1e-10
  14. #define N 100
  15.  
  16. using namespace std;
  17.  
  18. int z[N], x[N], lenz, lenx;
  19.  
  20. bool judge ()
  21. {
  22. if(z[0] && x[0])
  23. return 0;
  24. for (int i = 0; i < lenx; ++i)
  25. if (x[i] > 1 || (x[i] && x[i+1]))
  26. return 0;
  27.  
  28. for (int i = 0; i < lenz; ++i)
  29. if (z[i] > 1 ||(z[i] ==1 && z[i+1] == 1))
  30. return 0;
  31.  
  32. return 1;
  33. }
  34.  
  35. void doz (int i)
  36. {
  37. if (i == lenz-1)
  38. lenz++;
  39. int up = z[i] / 2;
  40. z[i] = z[i] & 1;
  41. z[i+1] += up;
  42. if (i >= 2)
  43. z[i-2] += up;
  44. else
  45. {
  46. if (lenx < 3 - i)
  47. lenx = 3 - i;
  48. x[1-i] += up;
  49. }
  50. }
  51.  
  52. void dox (int i)
  53. {
  54. if (i+3 > lenx)
  55. lenx = i + 3;
  56. int up = x[i] / 2;
  57. x[i] = x[i] & 1;
  58. x[i+2] += up;
  59. if (i == 0)
  60. z[0] += up;
  61. else
  62. x[i-1] += up;
  63. }
  64.  
  65. void qt (int n)
  66. {
  67. memset (z, 0, sizeof(z));
  68. memset (x, 0, sizeof(x));
  69. lenz = 1;
  70. lenx = 0;
  71. z[0] = n;
  72. while (!judge ())
  73. {
  74. for (int i = lenx-1; i >= 0; --i)
  75. {
  76.  
  77. if (i == 0 && x[i] > 0 && x[i+1] > 0)
  78. {
  79. int up = min (x[i], x[i+1]);
  80. z[0] += up;
  81. x[0] -= up;
  82. x[1] -= up;
  83. continue;
  84. }
  85. else if (x[i] > 0 && x[i+1] > 0)
  86. {
  87. int up = min (x[i], x[i+1]);
  88. x[i-1] += up;
  89. x[i+1] -= up;
  90. x[i] -= up;
  91. continue;
  92. }
  93. if (x[i] > 1)
  94. {
  95. dox (i);
  96. continue;
  97. }
  98.  
  99. }
  100. while(x[lenx-1] == 0)
  101. lenx--;
  102. for (int i = 0; i < lenz; ++i)
  103. {
  104.  
  105. if (i == 0 && z[i] > 0 && x[0] > 0)
  106. {
  107. if (i == lenz-1)
  108. lenz++;
  109. int up = min (z[i], x[0]);
  110. z[1] += up;
  111. z[0] -= up;
  112. x[0] -= up;
  113. continue;
  114. }
  115. else if (z[i] > 0 && z[i+1] > 0)
  116. {
  117. if (i+3 > lenz)
  118. lenz = i + 3;
  119. int up = min (z[i], z[i+1]);
  120. z[i+2] += up;
  121. z[i+1] -= up;
  122. z[i] -= up;
  123. continue;
  124. }
  125. if (z[i] > 1)
  126. {
  127. doz(i);
  128. continue;
  129. }
  130. }
  131. }
  132. while(x[lenx-1] == 0)
  133. lenx--;
  134. }
  135.  
  136. int main()
  137. {
  138. //freopen ("test.txt", "r", stdin);
  139. int n;
  140. while (scanf ("%d", &n) != EOF)
  141. {
  142. qt (n);
  143. for (int i = lenz - 1; i >= 0; --i)
  144. printf ("%d", z[i]);
  145. if (lenx > 0)
  146. printf (".");
  147. for (int i = 0; i < lenx; ++i)
  148. printf ("%d", x[i]);
  149. printf ("\n");
  150. }
  151. return 0;
  152. }

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. gulp 静态资源版本控制

    package.json { "name": "gulp", "version": "0.0.1", "des ...

  2. 华为云测平台服务再升级!华为M5系列平板调测能力正式上线!

    ​​​6月1日,华为M5系列平板设备兼容性测试和远程真机调试功能在华为终端开放实验室正式上线!助力您的产品在大屏适配上快人一步! 华为终端开放实验室DevEco平台现已提供基于华为M5系列平板设备的兼 ...

  3. SQLServer -- 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'T_FLOW'中的标识列指定显式值。

    SET IDENTITY_INSERT TABLE_NAME ON; INSERT INTO TABLE_NAME(XXX, XXX,..., XXX) SELECT XXX, XXX,..., XX ...

  4. css 字体的unicode码

    微软雅黑: YaHei宋体: SimSun黑体: SimHei ;

  5. HTML 学习笔记 JQuery(DOM 操作3)

    设置和获取HTML 文本 和 值 1.html()方法 类似于JavaScript中的innerHTML属性,可以用来读取或者设置某个元素中的HTML内容 例子 <html> <he ...

  6. php总结1 ——php简介、工作原理、运行环境、文件构成、语法结构、注释

    1.1 PHP  超文本预处理程序.实际就是制作网站的脚本程序 1.2 运行环境: wamp——windowns+apache+mySQL+php    常用于开发.学习和研究 lamp ——linu ...

  7. python数据分析之:绘图和可视化

    在数据分析领域,最出名的绘图工具就是matlib.在Python同样有类似的功能.就是matplotlib.前面几章我们都在介绍数据的生成,整理,存储.那么这一章将介绍如果图形化的呈现这些数据.来看下 ...

  8. web前端开发-Ajax(1)

    1.简单简绍Ajax的功能 Ajax是处于前端和后端之间的这么一个东西,他可以拿到你前端form的内容,并且在你触发Ajax的时候,先将某些数据发送到服务器端,等接受到服务器 返回的数据时,执行某个函 ...

  9. Java for LeetCode 127 Word Ladder

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  10. Gemini.Workflow 双子工作流入门教程三:定义流程:流程节点、迁移条件参数配置

    简介: Gemini.Workflow 双子工作流,是一套功能强大,使用简单的工作流,简称双子流,目前配套集成在Aries框架中. 下面介绍本篇教程:定义流程:流程节点.迁移条件参数配置. 一.普通节 ...