CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A
A. Golden System
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputPiegirl got bored with binary, decimal and other integer based counting systems. Recently she discovered some interesting properties about number
, in particular that q2 = q + 1, and she thinks it would make a good base for her new unique system. She called it "golden system". In golden system the number is a non-empty string containing 0‘s and 1‘s as digits. The decimal value of expressiona0a1...an equals to .Soon Piegirl found out that this system doesn‘t have same properties that integer base systems do and some operations can not be performed on it. She wasn‘t able to come up with a fast way of comparing two numbers. She is asking for your help.
Given two numbers written in golden system notation, determine which of them has larger decimal value.
Input
Input consists of two lines — one for each number. Each line contains non-empty string consisting of ‘0‘ and ‘1‘ characters. The length of each string does not exceed 100000.
Output
Print ">" if the first number is larger, "<" if it is smaller and "=" if they are equal.
Sample test(s)
input
1000111
output
<input
0010011
output
=input
110101
output
>Note
In the first example first number equals to
, while second number is approximately1.6180339882 + 1.618033988 + 1 ≈ 5.236, which is clearly a bigger number.In the second example numbers are equal. Each of them is ≈ 2.618.
思路:最开始想推每一项的公式,不行,系数太大!后来想把前面的1全部转化为后面的1,发现这样的话也会2^100000太大!
后来发现如果一个字符串中出现的第一个1比另一个字符串中的第一个1高两位的话,就是这个串大,否则转化为后面的1(也就是第i位的1等于第i-1位的1和第i-2位的1)然后再逐位判断对多10^5。
处理时要把字符串反转,放在vector里面然后reverse(),竟然超时,换做直接字符串反转函数,AC 30ms!
1 #include<cstdio> 2 3 #include<iostream> 4 5 #include<cmath> 6 7 #include<stdlib.h> 8 9 #include<vector> 10 11 #include<cstring> 12 13 #include<map> 14 15 #include<algorithm> 16 17 #include<string.h> 18 19 #define M(a,b) memset(a,b,sizeof(a)) 20 21 #define INF 0x3f3f3f3f 22 23 24 25 using namespace std; 26 27 28 29 char a[100005],b[100005]; 30 31 int num[100005]; 32 33 vector<char> v1,v2; 34 35 void strRev(char *s) 36 37 { 38 39 char temp, *end = s + strlen(s) - 1; 40 41 while( end > s) 42 43 { 44 45 temp = *s; 46 47 *s = *end; 48 49 *end = temp; 50 51 --end; 52 53 ++s; 54 55 } 56 57 } 58 59 60 61 int main() 62 63 { 64 65 while(scanf("%s",a)==1) 66 67 { 68 69 scanf("%s",b); 70 71 int n = max(strlen(a),strlen(b)); 72 73 strRev(a); 74 75 strRev(b); 76 77 int strla = strlen(a); 78 79 int strlb = strlen(b); 80 81 int tem = strlen(a)-strlen(b); 82 83 if(tem < 0) 84 85 { 86 87 for(int i = 0;i<-tem;i++) 88 89 a[strla+i] = ‘0‘; 90 91 } 92 93 else 94 95 { 96 97 for(int i = 0;i<tem;i++) 98 99 b[strlb+i] = ‘0‘; 100 101 } 102 103 /*for(int i = 0;i<n;i++) cout<<a[i]; 104 105 cout<<endl; 106 107 for(int i = 0;i<n;i++) cout<<b[i]; 108 109 cout<<endl;*/ 110 111 for(int i = 0;i<n;i++) 112 113 { 114 115 if(a[i] == ‘1‘ && b[i] == ‘1‘) num[i+2] = 0; 116 117 else if(a[i] == ‘0‘ && b[i] == ‘0‘) num[i+2] = 0; 118 119 else if(a[i] == ‘1‘ && b[i] == ‘0‘) num[i+2] = 1; 120 121 else if(a[i] == ‘0‘ && b[i] == ‘1‘) num[i+2] = -1; 122 123 } 124 125 num[0] = 0; 126 127 num[1] = 0; 128 129 //for(int i = 0;i<n+2;i++) cout<<num[i]; 130 131 //cout<<endl; 132 133 for(int i = n+1;i>=0;i--) 134 135 { 136 137 if(i==1&&num[i]==0) puts("="); 138 139 if(num[i]==2){puts(">"); break;} 140 141 if(num[i]==-2){puts("<"); break;} 142 143 if(num[i] == 0) continue; 144 145 if(num[i] == 1) 146 147 { 148 149 if(num[i-1]==0||num[i-1]==1) {puts(">"); break;} 150 151 if(num[i-1]==-1) num[i-1]+=1, num[i-2]+=1; 152 153 } 154 155 if(num[i] == -1) 156 157 { 158 159 if(num[i-1]==0||num[i-1]==-1) {puts("<"); break;} 160 161 if(num[i-1]==1) num[i-1]-=1, num[i-2]-=1; 162 163 } 164 165 } 166 167 } 168 169 return 0; 170 171 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。