题意讲某个二进制按照规则每一位对应斐波那契数生成新的数字,然后2个数字求和。再求由该规则生成的二进制串。并且要求尽量用更大项的fib数(题目提示不能由连续的1就是2个连续的1(11)不如100更优)

用大数处理出100项fib。然后模拟交替置位位0或者1,输出

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int numlen=;
struct bign {
int len, s[numlen];
bign() {
memset(s, , sizeof(s));
len = ;
}
bign(int num) { *this = num; }
bign(const char *num) { *this = num; }
bign operator = (const int num) {
char s[numlen];
sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator = (const char *num) {
len = strlen(num);
while(len > && num[] == '') num++, len--;
for(int i = ;i < len; i++) s[i] = num[len-i-] - '';
return *this;
} void deal() {
while(len > && !s[len-]) len--;
} bign operator + (const bign &a) const {
bign ret;
ret.len = ;
int top = max(len, a.len) , add = ;
for(int i = ;add || i < top; i++) {
int now = add;
if(i < len) now += s[i];
if(i < a.len) now += a.s[i];
ret.s[ret.len++] = now%;
add = now/;
}
return ret;
}
bign operator - (const bign &a) const {
bign ret;
ret.len = ;
int cal = ;
for(int i = ;i < len; i++) {
int now = s[i] - cal;
if(i < a.len) now -= a.s[i];
if(now >= ) cal = ;
else {
cal = ; now += ;
}
ret.s[ret.len++] = now;
}
ret.deal();
return ret;
}
bign operator * (const bign &a) const {
bign ret;
ret.len = len + a.len;
for(int i = ;i < len; i++) {
for(int j = ;j < a.len; j++)
ret.s[i+j] += s[i]*a.s[j];
}
for(int i = ;i < ret.len; i++) {
ret.s[i+] += ret.s[i]/;
ret.s[i] %= ;
}
ret.deal();
return ret;
} bign operator * (const int num) {
// printf("num = %d\n", num);
bign ret;
ret.len = ;
int bb = ;
for(int i = ;i < len; i++) {
int now = bb + s[i]*num;
ret.s[ret.len++] = now%;
bb = now/;
}
while(bb) {
ret.s[ret.len++] = bb % ;
bb /= ;
}
ret.deal();
return ret;
} bign operator / (const bign &a) const {
bign ret, cur = ;
ret.len = len;
for(int i = len-;i >= ; i--) {
cur = cur*;
cur.s[] = s[i];
while(cur >= a) {
cur -= a;
ret.s[i]++;
}
}
ret.deal();
return ret;
} bign operator % (const bign &a) const {
bign b = *this / a;
return *this - b*a;
} bign operator += (const bign &a) { *this = *this + a; return *this; }
bign operator -= (const bign &a) { *this = *this - a; return *this; }
bign operator *= (const bign &a) { *this = *this * a; return *this; }
bign operator /= (const bign &a) { *this = *this / a; return *this; }
bign operator %= (const bign &a) { *this = *this % a; return *this; } bool operator < (const bign &a) const {
if(len != a.len) return len < a.len;
for(int i = len-;i >= ; i--) if(s[i] != a.s[i])
return s[i] < a.s[i];
return false;
}
bool operator > (const bign &a) const { return a < *this; }
bool operator <= (const bign &a) const { return !(*this > a); }
bool operator >= (const bign &a) const { return !(*this < a); }
bool operator == (const bign &a) const { return !(*this > a || *this < a); }
bool operator != (const bign &a) const { return *this > a || *this < a; } string str() const {
string ret = "";
for(int i = ;i < len; i++) ret = char(s[i] + '') + ret;
return ret;
}
};
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;
}
char a[numlen],b[numlen];
bign fib[numlen];
void init()
{
fib[]=;fib[]=; fib[]=;
for (int i=;i<numlen;i++) fib[i]=fib[i-]+fib[i-];
}
bign trans(char *a)
{
bign sum=;
int len=strlen(a);
for (int i=;i<=len;i++)
if (a[i-]=='')
sum+=fib[len-i+];
//cout<<sum<<endl;
return sum;
}
bign tmp;
void slove(bign sum)
{
if (sum==tmp) {puts("");return ;}
int i=;
for (i=;i<numlen;i++)
if (fib[i]>sum) break;
i--;
bool flag=true;
for (;i>;i--)
{
//cout<<sum<<' '<<fib[i]<<endl;
if (fib[i]<=sum && flag)
{
printf("");
sum=sum-fib[i];
flag=false;
}
else
{
printf("");
flag=true;
}
}
putchar('\n');
}
int main()
{
init();
bool first=false;
while (scanf("%s%s",a,b)!=EOF)
{
if (first) putchar('\n');
else first=true;
tmp=;
bign num1=trans(a);
bign num2=trans(b);
bign sum=num1+num2;
//cout<<sum<<endl;
//for (int i=1;i<=10;i++) cout<<fib[i]<<' ';cout<<endl;
slove(sum);
}
return ;
}

最新文章

  1. Android消息传递之EventBus 3.0使用详解
  2. wampServer图标为橙色无法启动原因之一
  3. DataTable 除去列中重复值
  4. Android Jni变量对照表
  5. JVM笔记5:Class文件结构
  6. mvc导航配置(View页面)
  7. js onblur 和 onkeyup 事件用法
  8. Spring框架下的单元测试
  9. 数据结构(C语言版)顺序栈相关算法的代码实现
  10. Java 8的新特性—终极版
  11. PHP系统左侧菜单栏的管理与实现
  12. socketWriter.go
  13. Html5知识点
  14. Cinder组件
  15. Swoft 图片上传与处理
  16. luogu1330 封锁阳光大学 (dfs)
  17. 巧用CASE WHEN 验证用户登录信息
  18. AndroidsStudio_找Bug
  19. Vim for Windows --ctags
  20. 关于tp5 的验证码遇到的一些问题

热门文章

  1. proteus中蜂鸣器不响的原因
  2. PHP.25-TP框架商城应用实例-后台2-商品列表页-搜索、翻页、排序
  3. 一个简单的同步集群的shell脚本
  4. echart图表展示数据-简单的柱状图
  5. angular 模块化之directive
  6. Flash文件在asp页面无法播放,网页上面的Flash文件在火狐浏览器不播放
  7. USACO Section1.2 Dual Palindromes 解题报告
  8. 常用模块(string)
  9. 【Python】print 方法的参数
  10. PAT——乙级1028