Death to Binary? 模拟+斐波那契进制数标准式

题意:用斐波那契数来计数,一个字符串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 + F n-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. 
Input
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.
Output
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. 
Sample Input
11101 1101
1 1
Sample Output
   100101
+   10001
  -------
  1001000

   1
+  1
  --
  10


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值