hdu 5351

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <algorithm>
  5 #include <cstring>
  6 #include <cmath>
  7 #include <stack>
  8 #include <vector>
  9 #include <queue>
 10 #include <map>
 11 #include <set>
 12 #include <climits>
 13 #include <cassert>
 14 #define LL long long
 15 #define lson lo, mi, rt << 1
 16 #define rson mi + 1, hi, rt << 1 | 1
 17 
 18 using namespace std;
 19 const int maxn = 1000 + 10;
 20 const int inf = 0x3f3f3f3f;
 21 const double eps = 1e-8;
 22 const double pi = acos(-1.0);
 23 const double ee = exp(1.0);
 24 
 25 const int mod = 258280327;
 26 
 27 struct huge
 28 {
 29 #define N_huge 850
 30 #define base 100000000
 31     static char s[N_huge*10];
 32     typedef long long value;
 33     value a[N_huge];
 34     int len;
 35     void clear()
 36     {
 37         len=1;
 38         a[len]=0;
 39     }
 40     huge()
 41     {
 42         clear();
 43     }
 44     huge(value x)
 45     {
 46         *this=x;
 47     }
 48     huge operator =(huge b)
 49     {
 50         len=b.len;
 51         for (int i=1; i<=len; ++i)a[i]=b.a[i];
 52         return *this;
 53     }
 54     huge operator +(huge b)
 55     {
 56         int L=len>b.len?len:b.len;
 57         huge tmp;
 58         for (int i=1; i<=L+1; ++i)tmp.a[i]=0;
 59         for (int i=1; i<=L; ++i)
 60         {
 61             if (i>len)tmp.a[i]+=b.a[i];
 62             else if (i>b.len)tmp.a[i]+=a[i];
 63             else
 64             {
 65                 tmp.a[i]+=a[i]+b.a[i];
 66                 if (tmp.a[i]>=base)
 67                 {
 68                     tmp.a[i]-=base;
 69                     ++tmp.a[i+1];
 70                 }
 71             }
 72         }
 73         if (tmp.a[L+1])tmp.len=L+1;
 74         else tmp.len=L;
 75         return tmp;
 76     }
 77     huge operator -(huge b)
 78     {
 79         int L=len>b.len?len:b.len;
 80         huge tmp;
 81         for (int i=1; i<=L+1; ++i)tmp.a[i]=0;
 82         for (int i=1; i<=L; ++i)
 83         {
 84             if (i>b.len)b.a[i]=0;
 85             tmp.a[i]+=a[i]-b.a[i];
 86             if (tmp.a[i]<0)
 87             {
 88                 tmp.a[i]+=base;
 89                 --tmp.a[i+1];
 90             }
 91         }
 92         while (L>1&&!tmp.a[L])--L;
 93         tmp.len=L;
 94         return tmp;
 95     }
 96     huge operator *(huge b)
 97     {
 98         int L=len+b.len;
 99         huge tmp;
100         for (int i=1; i<=L; ++i)tmp.a[i]=0;
101         for (int i=1; i<=len; ++i)
102             for (int j=1; j<=b.len; ++j)
103             {
104                 tmp.a[i+j-1]+=a[i]*b.a[j];
105                 if (tmp.a[i+j-1]>=base)
106                 {
107                     tmp.a[i+j]+=tmp.a[i+j-1]/base;
108                     tmp.a[i+j-1]%=base;
109                 }
110             }
111         tmp.len=len+b.len;
112         while (tmp.len>1&&!tmp.a[tmp.len])--tmp.len;
113         return tmp;
114     }
115     pair<huge,huge> divide(huge a,huge b)
116     {
117         int L=a.len;
118         huge c,d;
119         for (int i=L; i; --i)
120         {
121             c.a[i]=0;
122             d=d*base;
123             d.a[1]=a.a[i];
124             //while (d>=b){d-=b;++c.a[i];}
125             int l=0,r=base-1,mid;
126             while (l<r)
127             {
128                 mid=(l+r+1)>>1;
129                 if (b*mid<=d)l=mid;
130                 else r=mid-1;
131             }
132             c.a[i]=l;
133             d-=b*l;
134         }
135         while (L>1&&!c.a[L])--L;
136         c.len=L;
137         return make_pair(c,d);
138     }
139     huge operator /(value x)
140     {
141         value d=0;
142         huge tmp;
143         for (int i=len; i; --i)
144         {
145             d=d*base+a[i];
146             tmp.a[i]=d/x;
147             d%=x;
148         }
149         tmp.len=len;
150         while (tmp.len>1&&!tmp.a[tmp.len])--tmp.len;
151         return tmp;
152     }
153     value operator %(value x)
154     {
155         value d=0;
156         for (int i=len; i; --i)d=(d*base+a[i])%x;
157         return d;
158     }
159     huge operator /(huge b)
160     {
161         return divide(*this,b).first;
162     }
163     huge operator %(huge b)
164     {
165         return divide(*this,b).second;
166     }
167     huge &operator +=(huge b)
168     {
169         *this=*this+b;
170         return *this;
171     }
172     huge &operator -=(huge b)
173     {
174         *this=*this-b;
175         return *this;
176     }
177     huge &operator *=(huge b)
178     {
179         *this=*this*b;
180         return *this;
181     }
182     huge &operator ++()
183     {
184         huge T;
185         T=1;
186         *this=*this+T;
187         return *this;
188     }
189     huge &operator --()
190     {
191         huge T;
192         T=1;
193         *this=*this-T;
194         return *this;
195     }
196     huge operator ++(int)
197     {
198         huge T,tmp=*this;
199         T=1;
200         *this=*this+T;
201         return tmp;
202     }
203     huge operator --(int)
204     {
205         huge T,tmp=*this;
206         T=1;
207         *this=*this-T;
208         return tmp;
209     }
210     huge operator +(value x)
211     {
212         huge T;
213         T=x;
214         return *this+T;
215     }
216     huge operator -(value x)
217     {
218         huge T;
219         T=x;
220         return *this-T;
221     }
222     huge operator *(value x)
223     {
224         huge T;
225         T=x;
226         return *this*T;
227     }
228     //huge operator /(value x){huge T;T=x;return *this/T;}
229     //huge operator %(value x){huge T;T=x;return *this%T;}
230     huge operator *=(value x)
231     {
232         *this=*this*x;
233         return *this;
234     }
235     huge operator +=(value x)
236     {
237         *this=*this+x;
238         return *this;
239     }
240     huge operator -=(value x)
241     {
242         *this=*this-x;
243         return *this;
244     }
245     huge operator /=(value x)
246     {
247         *this=*this/x;
248         return *this;
249     }
250     huge operator %=(value x)
251     {
252         *this=*this%x;
253         return *this;
254     }
255     bool operator ==(value x)
256     {
257         huge T;
258         T=x;
259         return *this==T;
260     }
261     bool operator !=(value x)
262     {
263         huge T;
264         T=x;
265         return *this!=T;
266     }
267     bool operator <=(value x)
268     {
269         huge T;
270         T=x;
271         return *this<=T;
272     }
273     bool operator >=(value x)
274     {
275         huge T;
276         T=x;
277         return *this>=T;
278     }
279     bool operator <(value x)
280     {
281         huge T;
282         T=x;
283         return *this<T;
284     }
285     bool operator >(value x)
286     {
287         huge T;
288         T=x;
289         return *this>T;
290     }
291     huge operator =(value x)
292     {
293         len=0;
294         while (x)a[++len]=x%base,x/=base;
295         if (!len)a[++len]=0;
296         return *this;
297     }
298     bool operator <(huge b)
299     {
300         if (len<b.len)return 1;
301         if (len>b.len)return 0;
302         for (int i=len; i; --i)
303         {
304             if (a[i]<b.a[i])return 1;
305             if (a[i]>b.a[i])return 0;
306         }
307         return 0;
308     }
309     bool operator ==(huge b)
310     {
311         if (len!=b.len)return 0;
312         for (int i=len; i; --i)
313             if (a[i]!=b.a[i])return 0;
314         return 1;
315     }
316     bool operator !=(huge b)
317     {
318         return !(*this==b);
319     }
320     bool operator >(huge b)
321     {
322         return !(*this<b||*this==b);
323     }
324     bool operator <=(huge b)
325     {
326         return (*this<b)||(*this==b);
327     }
328     bool operator >=(huge b)
329     {
330         return (*this>b)||(*this==b);
331     }
332     huge str(char s[])
333     {
334         int l=strlen(s);
335         value x=0,y=1;
336         len=0;
337         for (int i=l-1; i>=0; --i)
338         {
339             x=x+(s[i]-'0')*y;
340             y*=10;
341             if (y==base)a[++len]=x,x=0,y=1;
342         }
343         if (!len||x)a[++len]=x;
344     }
345     void read()
346     {
347         scanf("%s",s);
348         this->str(s);
349     }
350     void print()
351     {
352         printf("%d",(int)a[len]);
353         for (int i=len-1; i; --i)
354         {
355             for (int j=base/10; j>=10; j/=10)
356             {
357                 if (a[i]<j)printf("0");
358                 else break;
359             }
360             printf("%d",(int)a[i]);
361         }
362         printf("\n");
363     }
364 };
365 char huge::s[N_huge*10];
366 
367 huge fib[maxn];
368 
369 void init()
370 {
371     fib[1] = 1;
372     fib[2] = 1;
373     for (int i = 3; i < maxn; i++)
374     {
375         fib[i] = fib[i - 1] + fib[i - 2];
376     }
377 }
378 
379 int main()
380 {
381 #ifdef LOCAL
382     freopen("in.txt", "r", stdin);
383 #endif // LOCAL
384     init();
385     int ncase;
386     scanf("%d", &ncase);
387     while (ncase--)
388     {
389         int n;
390         scanf("%d", &n);
391         huge m;
392         m.read();
393         if (m == 1 || m == 2)
394         {
395             printf("0\n");
396             continue;
397         }
398         int i;
399         for (i = 2; i < n + 2; i++)
400         {
401             if (m + 1 < fib[i])
402                 break;
403         }
404         m = m - fib[i - 2];
405         m %= mod;
406         m.print();
407     }
408     return 0;
409 }

 

posted on 2015-08-04 20:57  Unico  阅读(183)  评论(0)    收藏  举报