uva 763 Fibinary Numbers

2024-03-24 08:18
文章标签 numbers uva 763 fibinary

本文主要是介绍uva 763 Fibinary Numbers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原题:
The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence “1010” is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence are:
1, 2, 3, 5, 8, 13, 21, …
Where each term is the sum of the two preceding terms (note that there is only one 1 in the sequence as defined here). Using this scheme, the sequence “1010” could be interpreted as 1·5+0·3+1·2+0·1 = 7.
This representation is called a Fibinary number.
Note that there is not always a unique Fibinary representation of every number. For example the number 10 could be represented as either 8 + 2 (10010) or as 5 + 3 + 2 (1110). To make the Fibinary
representations unique, larger Fibonacci terms must always be used whenever possible (i.e. disallow 2 adjacent 1’s). Applying this rule to the number 10, means that 10 would be represented as 8+2 (10010).
Write a program that takes two valid Fibinary numbers and prints the sum in Fibinary form.
Input
The input file contains several test cases with a blank line between two consecutive.
Each test case consists in two lines with Fibinary numbers. These numbers will have at most 100 digits.
Output
For each test case, print the sum of the two input numbers in Fibinary form. It must be a blank line between two consecutive outputs.
Sample Input
10010
1
10000
1000
10000
10000
Sample Output
10100
100000
100100

中文:
给你两个斐波那契进制序列,让你求这个数的和,结果也用斐波那契序列表示,要求不能有两个1相邻。

#include <bits/stdc++.h>
using namespace std;
const int maxn=1000;/*精度位数*/
/*(必选)类与基础功能定义,用法类似于unsigned(非负)*/
class bign
{friend istream& operator>>(istream&,bign&);/*输入运算符友元*/friend ostream& operator<<(ostream&,const bign&);/*输出运算符友元*/friend bign operator+(const bign&,const bign&);/*加号运算符友元*/friend bign operator*(const bign&,const bign&);/*乘号运算符友元*/friend bign operator*(const bign&,int);/*高精度乘以低精度乘法友元*/friend bign operator-(const bign&,const bign&);/*减号运算符友元*/friend bign operator/(const bign&,const bign&);/*除法运算符友元*/friend bign operator%(const bign&,const bign&);/*模运算符友元*/friend bool operator<(const bign&,const bign&);/*逻辑小于符友元*/friend bool operator>(const bign&,const bign&);/*逻辑大于符友元*/friend bool operator<=(const bign&,const bign&);/*逻辑小于等于符友元*/friend bool operator>=(const bign&,const bign&);/*逻辑大于等于符友元*/friend bool operator==(const bign&,const bign&);/*逻辑等符友元*/friend bool operator!=(const bign&,const bign&);/*逻辑不等符友元*/private:int len,s[maxn];public:bign(){memset(s,0,sizeof(s));len=1;}bign operator=(const char* num){int i=0,ol;ol=len=strlen(num);while(num[i++]=='0'&&len>1)len--;memset(s,0,sizeof(s));for(i=0;i<len;i++)s[i]=num[ol-i-1]-'0';return *this;}bign operator=(int num){char s[maxn];sprintf(s,"%d",num);*this=s;return *this;}bign(int num){*this=num;}bign(const char* num){*this=num;}string str() const{int i;string res="";for(i=0;i<len;i++)res=char(s[i]+'0')+res;if(res=="")res="0";return res;}};
/*(可选)基本逻辑运算符重载*/
bool operator<(const bign& a,const bign& b)
{int i;if(a.len!=b.len)return a.len<b.len;for(i=a.len-1;i>=0;i--)if(a.s[i]!=b.s[i])return a.s[i]<b.s[i];return false;}
bool operator>(const bign& a,const bign& b){return b<a;}
bool operator<=(const bign& a,const bign& b){return !(a>b);}
bool operator>=(const bign& a,const bign& b){return !(a<b);}
bool operator!=(const bign& a,const bign& b){return a<b||a>b;}
bool operator==(const bign& a,const bign& b){return !(a<b||a>b);}
/*(可选)加法运算符重载*/
bign operator+(const bign& a,const bign& b)
{int i,max=(a.len>b.len?a.len:b.len),t,c;bign sum;sum.len=0;for(i=0,c=0;c||i<max;i++){t=c;if(i<a.len)t+=a.s[i];if(i<b.len)t+=b.s[i];sum.s[sum.len++]=t%10;c=t/10;}return sum;}
/*(可选)乘法运算符重载(高精度乘高精度)*/
bign operator*(const bign& a,const bign& b)
{int i,j;bign res;for(i=0;i<a.len;i++){for(j=0;j<b.len;j++){res.s[i+j]+=(a.s[i]*b.s[j]);res.s[i+j+1]+=res.s[i+j]/10;res.s[i+j]%=10;}}res.len=a.len+b.len;while(res.s[res.len-1]==0&&res.len>1)res.len--;if(res.s[res.len])res.len++;return res;}
/*高精度乘以低精度(注意:必须是bign*int顺序不能颠倒,要么会与高精度乘高精度发生冲突*/
bign operator*(const bign& a,int b)
{int i,t,c=0;bign res;for(i=0;i<a.len;i++){t=a.s[i]*b+c;res.s[i]=t%10;c=t/10;}res.len=a.len;while(c!=0){res.s[i++]=c%10;c/=10;res.len++;}return res;}
/*(可选)减法运算符重载*/
bign operator-(const bign& a,const bign& b)
{bign res;int i,len=(a.len>b.len)?a.len:b.len;for(i=0;i<len;i++){res.s[i]+=a.s[i]-b.s[i];if(res.s[i]<0){res.s[i]+=10;res.s[i+1]--;}}while(res.s[len-1]==0&&len>1)len--;res.len=len;return res;}
/*(可选)除法运算符重载(注意:减法和乘法运算和>=运算符必选)*/
bign operator/(const bign& a,const bign& b)
{int i,len=a.len;bign res,f;for(i=len-1;i>=0;i--){f=f*10;f.s[0]=a.s[i];while(f>=b){f=f-b;res.s[i]++;}}while(res.s[len-1]==0&&len>1)len--;res.len=len;return res;}
/*(可选)模运算符重载(注意:减法和乘法运算和>=运算符必选)*/
bign operator%(const bign& a,const bign& b)
{int i,len=a.len;bign res,f;for(i=len-1;i>=0;i--){f=f*10;f.s[0]=a.s[i];while(f>=b){f=f-b;res.s[i]++;}}return f;}
/*(可选)X等运算符重载(注意:X法必选)*/
bign& operator+=(bign& a,const bign& b)
{a=a+b;return a;}
bign& operator-=(bign& a,const bign& b)
{a=a-b;return a;}
bign& operator*=(bign& a,const bign& b)
{a=a*b;return a;}
bign& operator/=(bign& a,const bign& b)
{a=a/b;return a;}
/*可选前缀++/--与后缀++/--(注意:加法必选)*/
bign& operator++(bign& a)
{a=a+1;return a;}
bign& operator++(bign& a,int)
{bign t=a;a=a+1;return t;}
bign& operator--(bign& a)
{a=a-1;return a;}
bign& operator--(bign& a,int)
{bign t=a;a=a-1;return t;}
istream& operator>>(istream &in,bign& x)
{string s;in>>s;x=s.c_str();return in;}
ostream& operator<<(ostream &out,const bign& x)
{out<<x.str();return out;}
bign fib[130];
bign get_num(string a)
{bign tmp=0;for(int j=1,i=a.size()-1;i>=0;i--,j++){if(a[i]=='1')tmp+=fib[j];}return tmp;
}
fstream in,out;
int find_max(int s,int e,bign tar)
{for(int i=e;i>=s;i--){if(fib[i]<=tar)return i;}
}
int main()
{ios::sync_with_stdio(false);
//    in.open("in.txt");
//    out.open("out.txt");fib[1]=1,fib[2]=2;for(int i=3;i<=120;i++)fib[i]=fib[i-2]+fib[i-1];bign a,b,tmp;string s1,s2;vector<int> ans;int t=0;while(cin>>s1>>s2){if(t++)cout<<endl;ans.clear();tmp=0;tmp+=get_num(s1);tmp+=get_num(s2);int inx=120;while(true){int ind=lower_bound(fib+1,fib+inx,tmp)-fib;if(fib[ind]==tmp){tmp-=fib[ind];ans.push_back(ind);break;}else{tmp-=fib[ind-1];ans.push_back(ind-1);if(tmp==0)break;}}/*while(true){int ind=find_max(1,inx,tmp);tmp-=fib[ind];ans.push_back(ind);if(tmp==0)break;inx=ind;}*/bool mark[501];int ter=-1;memset(mark,0,sizeof(mark));for(auto x:ans){if(x>ter)ter=x;mark[x]=1;}for(int i=ter;i>=1;i--)cout<<mark[i];if(ans.size()==1&&ans.back()==0)cout<<0;cout<<endl;}
//    in.close();
//    out.close();return 0;
}

解答:

此题高精度,斐波那契第100个数会超过unsigned long long。

首先打表,注意要打表到120位,因为两个100位斐波那契数的和会超过100位(废话)。
再把两个给定序列转换成10进制求和,然后在斐波那契数当中从后向前每次进去最大能减去的数,把减去数的下标保存即可。
查找最大能减去的斐波那契数可以用二分实现。

这篇关于uva 763 Fibinary Numbers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/840983

相关文章

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

uva 568 Just the Facts(n!打表递推)

题意是求n!的末尾第一个不为0的数字。 不用大数,特别的处理。 代码: #include <stdio.h>const int maxn = 10000 + 1;int f[maxn];int main(){#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALf[0] = 1;for (int i = 1; i <=

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

uva 10014 Simple calculations(数学推导)

直接按照题意来推导最后的结果就行了。 开始的时候只做到了第一个推导,第二次没有继续下去。 代码: #include<stdio.h>int main(){int T, n, i;double a, aa, sum, temp, ans;scanf("%d", &T);while(T--){scanf("%d", &n);scanf("%lf", &first);scanf

uva 10916 Factstone Benchmark(打表)

题意是求 k ! <= 2 ^ n ,的最小k。 由于n比较大,大到 2 ^ 20 次方,所以 2 ^ 2 ^ 20比较难算,所以做一些基础的数学变换。 对不等式两边同时取log2,得: log2(k ! ) <=  log2(2 ^ n)= n,即:log2(1) + log2(2) + log2 (3) + log2(4) + ... + log2(k) <= n ,其中 n 为 2 ^

uva 10025 The ? 1 ? 2 ? ... ? n = k problem(数学)

题意是    ?  1  ?  2  ?  ...  ?  n = k 式子中给k,? 处可以填 + 也可以填 - ,问最小满足条件的n。 e.g k = 12  - 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 with n = 7。 先给证明,令 S(n) = 1 + 2 + 3 + 4 + 5 + .... + n 暴搜n,搜出当 S(n) >=