wenbao与高精度

 

模板

--------------------------------------------------

 

  1 #ifndef BIGNUM
  2 #define BIGNUM
  3 class BigNum {
  4 #define MAXSIZEOFBIGNUM 500
  5 #define BASE 10
  6 #define DLEN 1
  7 public:
  8     int Len;
  9     int d[MAXSIZEOFBIGNUM];
 10 public:
 11     BigNum(void);
 12     BigNum(const int);
 13     BigNum(const char *);
 14     BigNum(const BigNum &);
 15     BigNum & operator = (const BigNum &);
 16     void clear(void);
 17     friend istream& operator>>(istream&,BigNum&);
 18     friend ostream& operator<<(ostream&,BigNum&);
 19     bool operator == (const BigNum &) const;
 20     bool operator > (const BigNum &) const;
 21     bool operator < (const BigNum &) const;
 22     bool operator >= (const BigNum &) const;
 23     bool operator <= (const BigNum &) const;
 24     BigNum operator + (const BigNum &) const;
 25     BigNum operator - (const BigNum &) const;
 26     BigNum operator * (const BigNum &) const;
 27     BigNum operator / (const BigNum &) const;
 28     BigNum operator % (const BigNum &) const;
 29     void operator ++ (void);
 30     void operator -- (void);
 31     BigNum operator + (const int &) const;
 32     BigNum operator - (const int &) const;
 33     BigNum operator * (const int &) const;
 34     BigNum operator / (const int &) const;
 35     int operator % (const int &) const;
 36     BigNum operator ^ (const int &) const;
 37     ~BigNum () {}
 38 };
 39 BigNum::BigNum() {
 40     Len=0;
 41     memset(d,0,sizeof(d));
 42 }
 43 BigNum::BigNum(const int ops) {
 44     int x=ops;
 45     Len=0;
 46     memset(d,0,sizeof(d));
 47     while (x) {
 48         Len++;
 49         d[Len]=x%BASE;
 50         x/=BASE;
 51     }
 52 }
 53 BigNum::BigNum(const char * ops) {
 54     int L=strlen(ops)-1,b=0;
 55     memset(d,0,sizeof(d));
 56     while (ops[b]=='0') b++;
 57     Len=0;
 58     while (L-b+1>=DLEN) {
 59         int x=0;
 60         for (int i=L-DLEN+1; i<=L; i++) x=x*10+ops[i]-'0';
 61         Len++;
 62         d[Len]=x;
 63         L-=DLEN;
 64     }
 65     int x=0;
 66     for (int i=b; i<=L; i++) x=x*10+ops[i]-'0';
 67     Len++;
 68     d[Len]=x;
 69 }
 70 BigNum::BigNum(const BigNum &ops):Len(ops.Len) {
 71     memset(d,0,sizeof(d));
 72     for(int i=1; i<=Len; i++) d[i]=ops.d[i];
 73 }
 74 BigNum & BigNum::operator = (const BigNum &ops) {
 75     memset(d,0,sizeof(d));
 76     Len=ops.Len;
 77     for(int i=1; i<=Len; i++)
 78         d[i]=ops.d[i];
 79     return *this;
 80 }
 81 void BigNum::clear(void) {
 82     for (int i=1; i<=MAXSIZEOFBIGNUM-2; i++) {
 83         if (d[i]<0) {
 84             d[i]+=BASE;
 85             d[i+1]--;
 86         }
 87         if (d[i]>=BASE) {
 88             d[i]-=BASE;
 89             d[i+1]++;
 90         }
 91     }
 92     for (int i=MAXSIZEOFBIGNUM-1; i>=1; i--)
 93         if (d[i]>0) {
 94             Len=i;
 95             return;
 96         }
 97     Len=0;
 98 }
 99 istream& operator>>(istream &in,BigNum &ops) {
100     char str[MAXSIZEOFBIGNUM+100];
101     in>>str;
102     int L=strlen(str),b=0;
103     while (str[b]=='0') b++;
104     ops.Len=0;
105     for (int i=L-1; i>=b; i--) {
106         ops.Len++;
107         ops.d[ops.Len]=str[i]-'0';
108     }
109     return in;
110 }
111 ostream& operator<<(ostream& out,BigNum& ops) {
112     for (int i=ops.Len; i>=1; i--) out<<ops.d[i];
113     if (ops.Len==0) out<<"0";
114     return out;
115 }
116 bool BigNum::operator == (const BigNum &ops) const {
117     if (Len!=ops.Len) return false;
118     for (int i=Len; i>=1; i--)
119         if (d[i]!=ops.d[i]) return false;
120     return true;
121 }
122 bool BigNum::operator > (const BigNum &ops) const {
123     if (Len<ops.Len) return false;
124     else if (Len>ops.Len) return true;
125     else {
126         for (int i=Len; i>=1; i--)
127             if (d[i]<ops.d[i]) return false;
128             else if (d[i]>ops.d[i]) return true;
129     }
130     return false;
131 }
132 bool BigNum::operator < (const BigNum &ops) const {
133     if (Len<ops.Len) return true;
134     else if (Len>ops.Len) return false;
135     else {
136         for (int i=Len; i>=1; i--)
137             if (d[i]<ops.d[i]) return true;
138             else if (d[i]>ops.d[i]) return false;
139     }
140     return false;
141 }
142 bool BigNum::operator >= (const BigNum &ops) const {
143     if (Len<ops.Len) return false;
144     else if (Len>ops.Len) return true;
145     else {
146         for (int i=Len; i>=1; i--)
147             if (d[i]<ops.d[i]) return false;
148             else if (d[i]>ops.d[i]) return true;
149     }
150     return true;
151 }
152 bool BigNum::operator <= (const BigNum &ops) const {
153     if (Len<ops.Len) return true;
154     else if (Len>ops.Len) return false;
155     else {
156         for (int i=Len; i>=1; i--)
157             if (d[i]<ops.d[i]) return true;
158             else if (d[i]>ops.d[i]) return false;
159     }
160     return true;
161 }
162 BigNum BigNum::operator + (const BigNum &ops) const {
163     BigNum ret(*this);
164     for (int i=1; i<=ops.Len; i++) ret.d[i]+=ops.d[i];
165     ret.clear();
166     return ret;
167 }
168 BigNum BigNum::operator - (const BigNum &ops) const {
169     BigNum ret(*this);
170     for (int i=ops.Len; i>=1; i--) ret.d[i]-=ops.d[i];
171     ret.clear();
172     return ret;
173 }
174 BigNum BigNum::operator * (const BigNum &ops) const {
175     BigNum ret,now(*this);
176     for (int i=1; i<=now.Len; i++)
177         for (int j=1; j<=ops.Len; j++)
178             ret.d[i+j-1]+=now.d[i]*ops.d[j];
179     for (int i=1; i<=MAXSIZEOFBIGNUM-2; i++)
180         if (ret.d[i]>=BASE) {
181             ret.d[i+1]+=ret.d[i]/BASE;
182             ret.d[i]%=BASE;
183         }
184     for (int i=MAXSIZEOFBIGNUM-1; i>=1; i--)
185         if (ret.d[i]>0) {
186             ret.Len=i;
187             break;
188         }
189     return ret;
190 }
191 BigNum BigNum::operator / (const BigNum &ops) const {
192     BigNum now=(*this),div,mod;
193     div.Len=now.Len;
194     mod.Len=0;
195     for (int j=now.Len; j>=1; j--) {
196         mod.Len++;
197         for (int p=mod.Len; p>=2; p--) mod.d[p]=mod.d[p-1];
198         mod.d[1]=now.d[j];
199         while (mod>=ops) {
200             div.d[j]++;
201             mod=mod-ops;
202         }
203         if (mod.Len==1 && mod.d[1]==0) mod.Len--;
204     }
205     div.clear();
206     mod.clear();
207     return div;
208 }
209 BigNum BigNum::operator % (const BigNum &ops) const {
210     BigNum now=(*this),div,mod;
211     div.Len=now.Len;
212     mod.Len=0;
213     for (int j=now.Len; j>=1; j--) {
214         mod.Len++;
215         for (int p=mod.Len; p>=2; p--) mod.d[p]=mod.d[p-1];
216         mod.d[1]=now.d[j];
217         while (mod>=ops) {
218             div.d[j]++;
219             mod=mod-ops;
220         }
221         if (mod.Len==1 && mod.d[1]==0) mod.Len--;
222     }
223     div.clear();
224     mod.clear();
225     return mod;
226 }
227 void BigNum::operator ++ (void) {
228     d[1]++;
229     for (int i=1; i<=MAXSIZEOFBIGNUM-2; i++)
230         if (d[i]>=BASE) {
231             d[i]-=BASE;
232             d[i+1]++;
233         } else break;
234     if (d[Len+1]>0) Len++;
235 }
236 void BigNum::operator -- (void) {
237     d[1]--;
238     for (int i=1; i<=MAXSIZEOFBIGNUM-2; i++)
239         if (d[i]<0) {
240             d[i]+=BASE;
241             d[i+1]--;
242         } else break;
243     if (d[Len]==0) Len--;
244 }
245 BigNum BigNum::operator + (const int & ops) const {
246     BigNum ret=(*this);
247     ret.d[1]+=ops;
248     ret.clear();
249     return ret;
250 }
251 BigNum BigNum::operator - (const int & ops) const {
252     BigNum ret=(*this);
253     ret.d[1]-=ops;
254     ret.clear();
255     return ret;
256 }
257 BigNum BigNum::operator * (const int & ops) const {
258     BigNum ret(*this);
259     for (int i=1; i<=ret.Len; i++) ret.d[i]*=ops;
260     for (int i=1; i<=MAXSIZEOFBIGNUM-2; i++)
261         if (ret.d[i]>=BASE) {
262             ret.d[i+1]+=ret.d[i]/BASE;
263             ret.d[i]%=BASE;
264         }
265     for (int i=MAXSIZEOFBIGNUM-1; i>=1; i--)
266         if (ret.d[i]>0) {
267             ret.Len=i;
268             return ret;
269         }
270     ret.Len=0;
271     return ret;
272 }
273 BigNum BigNum::operator / (const int & ops) const {
274     BigNum ret;
275     int down=0;
276     for(int i=Len; i>=1; i--) {
277         ret.d[i]=(d[i]+down*BASE)/ops;
278         down=d[i]+down*BASE-ret.d[i]*ops;
279     }
280     ret.Len=Len;
281     while(ret.d[ret.Len]==0 && ret.Len>1)
282         ret.Len--;
283     return ret;
284 }
285 int BigNum::operator % (const int &ops) const {
286     int mod=0;
287     for(int i=Len; i>=1; i--)
288         mod=((mod*BASE)%ops+d[i])%ops;
289     return mod;
290 }
291 BigNum BigNum::operator ^ (const int &ops) const {
292     BigNum t,ret(1);
293     if(ops==0)return ret;
294     if(ops==1)return *this;
295     int m=ops,i;
296     while(m>1) {
297         t=*this;
298         for(i=1; (i<<1)<=m; i<<=1)
299             t=t*t;
300         m-=i;
301         ret=ret*t;
302         if(m==1)ret=ret*(*this);
303     }
304     return ret;
305 }
306 #endif

 

 

-----------------------------------------------------

 

http://poj.org/problem?id=1737

 

n个各不相同的点能组成多少无向连通图?

 

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 using namespace std;
  5 
  6 #ifndef BIGNUM
  7 #define BIGNUM
  8 class BigNum {
  9 #define MAXSIZEOFBIGNUM 500
 10 #define BASE 10
 11 #define DLEN 1
 12 public:
 13     int Len;
 14     int d[MAXSIZEOFBIGNUM];
 15 public:
 16     BigNum(void);
 17     BigNum(const int);
 18     BigNum(const char *);
 19     BigNum(const BigNum &);
 20     BigNum & operator = (const BigNum &);
 21     void clear(void);
 22     friend istream& operator>>(istream&,BigNum&);
 23     friend ostream& operator<<(ostream&,BigNum&);
 24     bool operator == (const BigNum &) const;
 25     bool operator > (const BigNum &) const;
 26     bool operator < (const BigNum &) const;
 27     bool operator >= (const BigNum &) const;
 28     bool operator <= (const BigNum &) const;
 29     BigNum operator + (const BigNum &) const;
 30     BigNum operator - (const BigNum &) const;
 31     BigNum operator * (const BigNum &) const;
 32     BigNum operator / (const BigNum &) const;
 33     BigNum operator % (const BigNum &) const;
 34     void operator ++ (void);
 35     void operator -- (void);
 36     BigNum operator + (const int &) const;
 37     BigNum operator - (const int &) const;
 38     BigNum operator * (const int &) const;
 39     BigNum operator / (const int &) const;
 40     int operator % (const int &) const;
 41     BigNum operator ^ (const int &) const;
 42     ~BigNum () {}
 43 };
 44 BigNum::BigNum() {
 45     Len=0;
 46     memset(d,0,sizeof(d));
 47 }
 48 BigNum::BigNum(const int ops) {
 49     int x=ops;
 50     Len=0;
 51     memset(d,0,sizeof(d));
 52     while (x) {
 53         Len++;
 54         d[Len]=x%BASE;
 55         x/=BASE;
 56     }
 57 }
 58 BigNum::BigNum(const char * ops) {
 59     int L=strlen(ops)-1,b=0;
 60     memset(d,0,sizeof(d));
 61     while (ops[b]=='0') b++;
 62     Len=0;
 63     while (L-b+1>=DLEN) {
 64         int x=0;
 65         for (int i=L-DLEN+1; i<=L; i++) x=x*10+ops[i]-'0';
 66         Len++;
 67         d[Len]=x;
 68         L-=DLEN;
 69     }
 70     int x=0;
 71     for (int i=b; i<=L; i++) x=x*10+ops[i]-'0';
 72     Len++;
 73     d[Len]=x;
 74 }
 75 BigNum::BigNum(const BigNum &ops):Len(ops.Len) {
 76     memset(d,0,sizeof(d));
 77     for(int i=1; i<=Len; i++) d[i]=ops.d[i];
 78 }
 79 BigNum & BigNum::operator = (const BigNum &ops) {
 80     memset(d,0,sizeof(d));
 81     Len=ops.Len;
 82     for(int i=1; i<=Len; i++)
 83         d[i]=ops.d[i];
 84     return *this;
 85 }
 86 void BigNum::clear(void) {
 87     for (int i=1; i<=MAXSIZEOFBIGNUM-2; i++) {
 88         if (d[i]<0) {
 89             d[i]+=BASE;
 90             d[i+1]--;
 91         }
 92         if (d[i]>=BASE) {
 93             d[i]-=BASE;
 94             d[i+1]++;
 95         }
 96     }
 97     for (int i=MAXSIZEOFBIGNUM-1; i>=1; i--)
 98         if (d[i]>0) {
 99             Len=i;
100             return;
101         }
102     Len=0;
103 }
104 istream& operator>>(istream &in,BigNum &ops) {
105     char str[MAXSIZEOFBIGNUM+100];
106     in>>str;
107     int L=strlen(str),b=0;
108     while (str[b]=='0') b++;
109     ops.Len=0;
110     for (int i=L-1; i>=b; i--) {
111         ops.Len++;
112         ops.d[ops.Len]=str[i]-'0';
113     }
114     return in;
115 }
116 ostream& operator<<(ostream& out,BigNum& ops) {
117     for (int i=ops.Len; i>=1; i--) out<<ops.d[i];
118     if (ops.Len==0) out<<"0";
119     return out;
120 }
121 bool BigNum::operator == (const BigNum &ops) const {
122     if (Len!=ops.Len) return false;
123     for (int i=Len; i>=1; i--)
124         if (d[i]!=ops.d[i]) return false;
125     return true;
126 }
127 bool BigNum::operator > (const BigNum &ops) const {
128     if (Len<ops.Len) return false;
129     else if (Len>ops.Len) return true;
130     else {
131         for (int i=Len; i>=1; i--)
132             if (d[i]<ops.d[i]) return false;
133             else if (d[i]>ops.d[i]) return true;
134     }
135     return false;
136 }
137 bool BigNum::operator < (const BigNum &ops) const {
138     if (Len<ops.Len) return true;
139     else if (Len>ops.Len) return false;
140     else {
141         for (int i=Len; i>=1; i--)
142             if (d[i]<ops.d[i]) return true;
143             else if (d[i]>ops.d[i]) return false;
144     }
145     return false;
146 }
147 bool BigNum::operator >= (const BigNum &ops) const {
148     if (Len<ops.Len) return false;
149     else if (Len>ops.Len) return true;
150     else {
151         for (int i=Len; i>=1; i--)
152             if (d[i]<ops.d[i]) return false;
153             else if (d[i]>ops.d[i]) return true;
154     }
155     return true;
156 }
157 bool BigNum::operator <= (const BigNum &ops) const {
158     if (Len<ops.Len) return true;
159     else if (Len>ops.Len) return false;
160     else {
161         for (int i=Len; i>=1; i--)
162             if (d[i]<ops.d[i]) return true;
163             else if (d[i]>ops.d[i]) return false;
164     }
165     return true;
166 }
167 BigNum BigNum::operator + (const BigNum &ops) const {
168     BigNum ret(*this);
169     for (int i=1; i<=ops.Len; i++) ret.d[i]+=ops.d[i];
170     ret.clear();
171     return ret;
172 }
173 BigNum BigNum::operator - (const BigNum &ops) const {
174     BigNum ret(*this);
175     for (int i=ops.Len; i>=1; i--) ret.d[i]-=ops.d[i];
176     ret.clear();
177     return ret;
178 }
179 BigNum BigNum::operator * (const BigNum &ops) const {
180     BigNum ret,now(*this);
181     for (int i=1; i<=now.Len; i++)
182         for (int j=1; j<=ops.Len; j++)
183             ret.d[i+j-1]+=now.d[i]*ops.d[j];
184     for (int i=1; i<=MAXSIZEOFBIGNUM-2; i++)
185         if (ret.d[i]>=BASE) {
186             ret.d[i+1]+=ret.d[i]/BASE;
187             ret.d[i]%=BASE;
188         }
189     for (int i=MAXSIZEOFBIGNUM-1; i>=1; i--)
190         if (ret.d[i]>0) {
191             ret.Len=i;
192             break;
193         }
194     return ret;
195 }
196 BigNum BigNum::operator / (const BigNum &ops) const {
197     BigNum now=(*this),div,mod;
198     div.Len=now.Len;
199     mod.Len=0;
200     for (int j=now.Len; j>=1; j--) {
201         mod.Len++;
202         for (int p=mod.Len; p>=2; p--) mod.d[p]=mod.d[p-1];
203         mod.d[1]=now.d[j];
204         while (mod>=ops) {
205             div.d[j]++;
206             mod=mod-ops;
207         }
208         if (mod.Len==1 && mod.d[1]==0) mod.Len--;
209     }
210     div.clear();
211     mod.clear();
212     return div;
213 }
214 BigNum BigNum::operator % (const BigNum &ops) const {
215     BigNum now=(*this),div,mod;
216     div.Len=now.Len;
217     mod.Len=0;
218     for (int j=now.Len; j>=1; j--) {
219         mod.Len++;
220         for (int p=mod.Len; p>=2; p--) mod.d[p]=mod.d[p-1];
221         mod.d[1]=now.d[j];
222         while (mod>=ops) {
223             div.d[j]++;
224             mod=mod-ops;
225         }
226         if (mod.Len==1 && mod.d[1]==0) mod.Len--;
227     }
228     div.clear();
229     mod.clear();
230     return mod;
231 }
232 void BigNum::operator ++ (void) {
233     d[1]++;
234     for (int i=1; i<=MAXSIZEOFBIGNUM-2; i++)
235         if (d[i]>=BASE) {
236             d[i]-=BASE;
237             d[i+1]++;
238         } else break;
239     if (d[Len+1]>0) Len++;
240 }
241 void BigNum::operator -- (void) {
242     d[1]--;
243     for (int i=1; i<=MAXSIZEOFBIGNUM-2; i++)
244         if (d[i]<0) {
245             d[i]+=BASE;
246             d[i+1]--;
247         } else break;
248     if (d[Len]==0) Len--;
249 }
250 BigNum BigNum::operator + (const int & ops) const {
251     BigNum ret=(*this);
252     ret.d[1]+=ops;
253     ret.clear();
254     return ret;
255 }
256 BigNum BigNum::operator - (const int & ops) const {
257     BigNum ret=(*this);
258     ret.d[1]-=ops;
259     ret.clear();
260     return ret;
261 }
262 BigNum BigNum::operator * (const int & ops) const {
263     BigNum ret(*this);
264     for (int i=1; i<=ret.Len; i++) ret.d[i]*=ops;
265     for (int i=1; i<=MAXSIZEOFBIGNUM-2; i++)
266         if (ret.d[i]>=BASE) {
267             ret.d[i+1]+=ret.d[i]/BASE;
268             ret.d[i]%=BASE;
269         }
270     for (int i=MAXSIZEOFBIGNUM-1; i>=1; i--)
271         if (ret.d[i]>0) {
272             ret.Len=i;
273             return ret;
274         }
275     ret.Len=0;
276     return ret;
277 }
278 BigNum BigNum::operator / (const int & ops) const {
279     BigNum ret;
280     int down=0;
281     for(int i=Len; i>=1; i--) {
282         ret.d[i]=(d[i]+down*BASE)/ops;
283         down=d[i]+down*BASE-ret.d[i]*ops;
284     }
285     ret.Len=Len;
286     while(ret.d[ret.Len]==0 && ret.Len>1)
287         ret.Len--;
288     return ret;
289 }
290 int BigNum::operator % (const int &ops) const {
291     int mod=0;
292     for(int i=Len; i>=1; i--)
293         mod=((mod*BASE)%ops+d[i])%ops;
294     return mod;
295 }
296 BigNum BigNum::operator ^ (const int &ops) const {
297     BigNum t,ret(1);
298     if(ops==0)return ret;
299     if(ops==1)return *this;
300     int m=ops,i;
301     while(m>1) {
302         t=*this;
303         for(i=1; (i<<1)<=m; i<<=1)
304             t=t*t;
305         m-=i;
306         ret=ret*t;
307         if(m==1)ret=ret*(*this);
308     }
309     return ret;
310 }
311 #endif
312 
313 BigNum C(int N,int K) {
314     BigNum ret(1);
315     for (int i=0; i<K; i++) ret=ret*(N-i);
316     for (int i=1; i<=K; i++) ret=ret/i;
317     return ret;
318 }
319 BigNum f[60];
320 int main() {
321     f[1]=1;
322     f[2]=1;
323     f[3]=4;
324     f[4]=38;
325     for (int i=5; i<=50; i++) {
326         int pow=i*(i-1)/2;
327         BigNum T=(BigNum)2^pow;
328         for (int j=1; j<i; j++) {
329             BigNum tmp,com;
330             tmp=(BigNum)2^((i-j)*(i-j-1)/2);
331             tmp=tmp*f[j];
332             com=C(i-1,j-1);
333             tmp=tmp*com;
334             T=T-tmp;
335         }
336         f[i]=T;
337     }
338     int N;
339     while (scanf("%d",&N)!=EOF,N!=0) cout<<f[N]<<endl;
340     return 0;
341 }

 

 

-------------------------------------------------------------

 

 

 

 

 

只有不断学习才能进步!

 

posted @ 2018-04-14 13:50  wenbao  阅读(85)  评论(0编辑  收藏  举报