How many Fibs? POJ - 2413

How many Fibs? POJ - 2413

高精模板

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 typedef long long B_INT;
  5 const char p_c[]="%08lld";
  6 const char i_c[]="%lld";
  7 int l1,l2;
  8 struct Bigint
  9 {
 10 /*
 11 基本类型(char,int,float,double等)的静态成员可以在类里面声明并初始化,
 12 非基本类(char[],string ,自定义等)必须在类里面声明,类外初始化。
 13 */
 14     static const B_INT p=8;//压p位,int压4位加乘法可能导致溢出
 15     static const B_INT base=100000000;//压p位,最大数为10^p-1
 16     static const int maxl=3000;
 17     B_INT a[maxl];//a[0]->len,a[i]->从后往前数第i个p位
 18     Bigint()
 19     {
 20         memset(a,0,sizeof(a));
 21     }
 22 //  Bigint(char *str)
 23 //  {
 24 //      memset(a,0,sizeof(a));
 25 //      B_INT k=0,p=1;
 26 //      char *str1=str+strlen(str)-1;
 27 //      while(str1>=str)
 28 //      {
 29 //          k=k+p*(*str1-48);
 30 //          if(p==base)
 31 //          {
 32 //              a[++a[0]]=k%base;
 33 //              k/=base;
 34 //              p=1;
 35 //          }
 36 //          str1--;
 37 //          p*=10;
 38 //      }
 39 //      a[++a[0]]=k;
 40 //  }
 41     Bigint(const Bigint& b)
 42     {
 43         memcpy(a,b.a,sizeof(a));
 44     }
 45     Bigint& operator=(const Bigint& b)
 46     {
 47         memcpy(a,b.a,sizeof(a));
 48         return *this;
 49     }
 50     Bigint& operator=(char *str)
 51     {
 52         memset(a,0,sizeof(a));
 53         B_INT k=0,p=1;
 54         char *str1=str+strlen(str)-1;
 55         while(str1>=str)
 56         {
 57             k=k+p*(*str1-48);
 58             if(p==base)
 59             {
 60                 a[++a[0]]=k%base;
 61                 k/=base;
 62                 p=1;
 63             }
 64             str1--;
 65             p*=10;
 66         }
 67         a[++a[0]]=k;
 68         return *this;
 69     }
 70     Bigint operator+(const Bigint &b) const
 71     {
 72         Bigint c;
 73         B_INT i;
 74         c.a[0]=std::max(a[0],b.a[0]);
 75         for(i=1;i<=c.a[0];i++)
 76         {
 77             c.a[i]+=a[i]+b.a[i];
 78             c.a[i+1]=c.a[i]/base;
 79             c.a[i]%=base;
 80         }
 81         if(c.a[c.a[0]+1]>0)
 82             c.a[0]++;
 83         return c;
 84     }
 85     Bigint operator*(const Bigint &b) const
 86     {
 87         Bigint c;
 88         B_INT i,j;
 89         for(i=1;i<=a[0];i++)
 90             for(j=1;j<=b.a[0];j++)
 91                 c.a[i+j-1]+=a[i]*b.a[j];
 92         c.a[0]=a[0]+b.a[0]-1;
 93         for(i=1;i<=c.a[0];i++)
 94         {
 95             c.a[i+1]+=c.a[i]/base;
 96             c.a[i]%=base;
 97         }
 98         if(c.a[c.a[0]+1]>0)
 99             c.a[0]++;
100         return c;
101     }
102     Bigint operator-(const Bigint &b) const//要求保证减数小于被减数
103     {
104         Bigint c;
105         B_INT i;
106         c.a[0]=std::max(a[0],b.a[0]);
107         for(i=1;i<=c.a[0];i++)
108             c.a[i]=a[i]-b.a[i];
109         for(i=1;i<=c.a[0];i++)
110             if(c.a[i]<0)
111             {
112                 c.a[i]+=base;
113                 c.a[i+1]--;
114             }
115         while(c.a[c.a[0]]==0&&c.a[0]>0)
116             c.a[0]--;
117         return c;
118     }
119     Bigint& operator+=(const Bigint &b)
120     {
121         *this=*this+b;
122         return *this;
123     }
124     Bigint& operator-=(const Bigint &b)
125     {
126         *this=*this-b;
127         return *this;
128     }
129     Bigint& operator*=(const Bigint &b)
130     {
131         *this=(*this)*b;
132         return *this;
133     }
134     bool operator<(const Bigint &b) const
135     {
136         if(a[0]!=b.a[0])
137             return a[0]<b.a[0];
138         for(B_INT i=a[0];i>0;i--)
139             if(a[i]!=b.a[i])
140                 return a[i]<b.a[i];
141         return false;//相等
142     }
143     /*
144     非静态成员函数后面加const(加到非成员函数或静态成员后面会产生编译错误),
145     表示成员函数隐含传入的this指针为 const指针,
146     决定了在该成员函数中,
147     任意修改它所在的类的成员的操作都是不允许的
148     (因为隐含了对this指针的const引用);
149     唯一的例外是对于 mutable修饰的成员。
150     加了const的成员函数可以被非const对象和const对象调用,
151     但不加const的成员函数只能被非const对象调用。
152     下方b是const,const函数不能修改其数据成员
153     */
154     bool operator>(const Bigint &b) const
155     {
156         return b<*this;
157         /*
158         if(a[0]!=b.a[0])
159             return a[0]>b.a[0];
160         for(int i=a[0];i>0;i--)
161             if(a[i]!=b.a[i])
162                 return a[i]>b.a[i];
163         return false;//相等
164         */
165     }
166     bool operator<=(const Bigint &b) const
167     {
168         return !(b<*this);
169         /*
170         if(a[0]!=b.a[0])
171             return a[0]>b.a[0];
172         for(int i=a[0];i>0;i--)
173             if(a[i]!=b.a[i])
174                 return a[i]>b.a[i];
175         return true;//相等
176         */
177     }
178     bool operator>=(const Bigint &b) const
179     {
180         return !(*this<b);
181         /*
182         if(a[0]!=b.a[0])
183             return a[0]>b.a[0];
184         for(int i=a[0];i>0;i--)
185             if(a[i]!=b.a[i])
186                 return a[i]>b.a[i];
187         return true;//相等
188         */
189     }
190     bool operator==(const Bigint &b) const
191     {
192         if(a[0]!=b.a[0])
193             return false;
194         for(B_INT i=a[0];i>0;i--)
195             if(a[i]!=b.a[i])
196                 return false;
197         return true;
198     }
199     bool operator!=(const Bigint &b) const
200     {
201         return !(*this==b);
202     }
203     void print()
204     {
205         printf(i_c,a[a[0]]);
206         for(B_INT i=a[0]-1;i>0;i--)
207             printf(p_c,a[i]);
208     }
209 }x[602],y,z;
210 char str1[1001],str2[1001];
211 int main()
212 {
213     int i;
214     x[1]="1";
215     x[2]="2";
216     for(i=3;i<=600;i++)
217         x[i]=x[i-1]+x[i-2];
218     scanf("%s%s",str1,str2);
219     while(strcmp(str1,"0")!=0||strcmp(str2,"0")!=0)
220     {
221         y=str1;
222         z=str2;
223         for(l1=1;l1<=600;l1++)
224             if(x[l1]>=y)
225                 break;
226         for(l2=1;l2<=600;l2++)
227             if(x[l2]>z)
228                 break;
229         printf("%d\n",l2-l1);
230         memset(str1,0,sizeof(str1));
231         memset(str2,0,sizeof(str2));
232         scanf("%s%s",str1,str2);
233     }
234     return 0;
235 }
posted @ 2017-11-08 18:36  hehe_54321  阅读(319)  评论(0编辑  收藏  举报
AmazingCounters.com