Death to Binary? 模拟+斐波那契进制数标准式
来源:互联网 发布:arm双核编程 编辑:程序博客网 时间:2024/06/07 18:42
题意:用斐波那契数来计数,一个字符串s,字符串中只有0或1,f[i]代表第i个斐波那契数
0代表0*f[i],1代表1*f[i],从字符串最右边开始,第i个字符代表f[i],
例如1010=0*f[0]+1*f[1]+0*f[2]+1*f[3]=0+2+0+5=7;
标准字符串:字符串中任意两个1不相邻
难点:将所给字符串转化成标准字符串
思路:因为任一斐波那契数都是前两个相加,因此从小于x的第一个斐波那契数开始计数,则下一位一定不会被选
例如x=20=101010 1 2 3 5 8 13 21
第一位选13,x-=f[5]=7,这时x必然小于8;第二位必定是 0,依次计算,答案101010
反着想,如果计算之后x=8,则x+f[5]=21,当x=21时,答案是1000000,还是标准式
其他就是模拟,照着形式来;
#include<cstdio>#include<iostream>#include<cmath>#include<cstring>#include<string>#include<algorithm>using namespace std;typedef long long ll;ll f[45];string s1,s2,s3;void fbnq(){ f[0]=1;f[1]=2; for(int i=2;i<45;++i) f[i]=f[i-1]+f[i-2];}ll base(string a){ ll ans=0; int cnt=a.size(); for(int i=0;i<cnt;++i) if(a[i]=='1') ans+=f[i]; return ans;}void check(ll x,string &a){ int i; for(i=44;i>=0;i--) if(x>=f[i]) break; for(int j=i;j>=0;j--) if(x>=f[j]) { a+='1'; x-=f[j]; } else a+='0'; if(i<0) a+='0';}int main(){ fbnq(); while(cin>>s1>>s2) { reverse(s1.begin(),s1.end()); reverse(s2.begin(),s2.end()); ll num1=base(s1); ll num2=base(s2); ll num3=num1+num2; s1.clear(); s2.clear(); s3.clear(); check(num1,s1); check(num2,s2); check(num3,s3); printf(" ");for(int i=0;i<s3.size()-s1.size();i++) printf(" ");cout<<s1<<endl; printf("+ ");for(int i=0;i<s3.size()-s2.size();i++) printf(" ");cout<<s2<<endl; printf(" ");for(int i=0;i<s3.size();i++) printf("-");printf("\n"); printf(" ");cout<<s3<<endl; printf("\n"); } return 0;}
The group of Absurd Calculation Maniacs has discovered a great new way how to count. Instead of using the ordinary decadic numbers, they use Fibonacci base numbers. Numbers in this base are expressed as sequences of zeros and ones similarly to the binary numbers, but the weights of bits (fits?) in the representation are not powers of two, but the elements of the Fibonacci progression (1, 2, 3, 5, 8,... - the progression is defined by F0 = 1, F1 = 2 and the recursive relation F n = F n-1 + Fn-2 for n >= 2).
For example 1101001 Fib = F0 + F3 + F5 + F6 = 1 + 5 + 13 + 21 = 40.
You may observe that every integer can be expressed in this base, but not necessarily in a unique way - for example 40 can be also expressed as 10001001 Fib. However, for any integer there is a unique representation that does not contain two adjacent digits 1 - we call this representation canonical. For example 10001001 Fibis a canonical Fibonacci representation of 40.
To prove that this representation of numbers is superior to the others, ACM have decided to create a computer that will compute in Fibonacci base. Your task is to create a program that takes two numbers in Fibonacci base (not necessarily in the canonical representation) and adds them together.
For example 1101001 Fib = F0 + F3 + F5 + F6 = 1 + 5 + 13 + 21 = 40.
You may observe that every integer can be expressed in this base, but not necessarily in a unique way - for example 40 can be also expressed as 10001001 Fib. However, for any integer there is a unique representation that does not contain two adjacent digits 1 - we call this representation canonical. For example 10001001 Fibis a canonical Fibonacci representation of 40.
To prove that this representation of numbers is superior to the others, ACM have decided to create a computer that will compute in Fibonacci base. Your task is to create a program that takes two numbers in Fibonacci base (not necessarily in the canonical representation) and adds them together.
The input consists of several instances, each of them consisting of a single line. Each line of the input contains two numbers X and Y in Fibonacci base separated by a single space. Each of the numbers has at most 40 digits. The end of input is not marked in any special way.
The output for each instance should be formated as follows:
The first line contains the number X in the canonical representation, possibly padded from left by spaces. The second line starts with a plus sign followed by the number Y in the canonical representation, possibly padded from left by spaces. The third line starts by two spaces followed by a string of minus signs of the same length as the result of the addition. The fourth line starts by two spaces immediately followed by the canonical representation of X + Y. Both X and Y are padded from left by spaces so that the least significant digits of X, Y and X + Y are in the same column of the output. The output for each instance is followed by an empty line.
The first line contains the number X in the canonical representation, possibly padded from left by spaces. The second line starts with a plus sign followed by the number Y in the canonical representation, possibly padded from left by spaces. The third line starts by two spaces followed by a string of minus signs of the same length as the result of the addition. The fourth line starts by two spaces immediately followed by the canonical representation of X + Y. Both X and Y are padded from left by spaces so that the least significant digits of X, Y and X + Y are in the same column of the output. The output for each instance is followed by an empty line.
11101 11011 1
100101+ 10001 ------- 1001000 1+ 1 -- 10
阅读全文
0 0
- Death to Binary? 模拟+斐波那契进制数标准式
- Death to Binary?
- POJ 2116 Death to Binary?(模拟题)
- 【NOIP2012模拟11.8】斐波那契
- death
- LightOJ-1105 Fi Binary Number(斐波那契数列)
- NOJ1113 斐波那契数应用 模拟
- 【模拟】XMU 1599 斐波那契汤
- 斐波那契数的栈模拟递归算法
- [NOIP2017模拟][洛谷3938]斐波那契
- 用vb模拟斐波那契钟效果
- 比较二分查找与斐波那契查找(binary search & fibonacci search)
- codeforces #307 D. GukiZ and Binary Operations(各种快速幂+斐波那契)
- The Apple Watch Face That Counts Down to Your Death
- 栈实现斐波那契数列递归过程的非递归模拟
- 九度OJ 1075 斐波那契数列 (模拟,递归)
- poj 2413 大数模拟(区间内的斐波那契数个数)
- 用栈模拟斐波那契数列,实现非递归的过程
- Jquery 基础--选择器
- pro JPA2 精通java持久化API 第十章
- linux驱动开发简要流程
- PC/APP前端项目调试方式
- A
- Death to Binary? 模拟+斐波那契进制数标准式
- 基于图像识别的火灾探测技术
- MySQL存储引擎通常有哪3种?各自分别有什么特点?应用场景是哪些?
- hdu_2348_三分求解最值方程_数学题_少用tan
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
- HDU 6096 String 字典树数组建树
- 消除过期的对象引用。
- Just a Hook ---线段树区间更新
- nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)