码迷,mamicode.com
首页 > 其他好文 > 详细

高精度模板(From JCVB)

时间:2018-07-16 19:35:44      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:==   11.2   oar   bitset   define   getch   friend   int()   std   

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<cstdlib>
  6 #include<cmath>
  7 #include<vector>
  8 #include<map>
  9 #include<set>
 10 #include<queue>
 11 #include<bitset>
 12 #define cl(a) memset(a,0,sizeof(a))
 13 using namespace std;
 14 typedef long long ll;
 15 typedef double db;
 16 const db pi=3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862;
 17 void gn(int &x){
 18     int sg=1;char c;while(((c=getchar())<0||c>9)&&c!=-);
 19     if(c==-)sg=-1,x=0;else x=c-0;
 20     while((c=getchar())>=0&&c<=9)x=x*10+c-0;
 21     x*=sg;
 22 }
 23 void gn(ll &x){
 24     int sg=1;char c;while(((c=getchar())<0||c>9)&&c!=-);
 25     if(c==-)sg=-1,x=0;else x=c-0;
 26     while((c=getchar())>=0&&c<=9)x=x*10+c-0;
 27     x*=sg;
 28 }
 29 int mo=1000000007;
 30 int inf=1000000000;
 31 db eps=1e-6;
 32 //ll inf=1000000000000000000ll;
 33 int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
 34 int gcd(int a,int b){return b?gcd(b,a%b):a;}
 35 int dx[4]={1,0,-1,0};
 36 int dy[4]={0,1,0,-1};
 37 #define x1 x192837465
 38 #define x2 x123456789
 39 #define y1 y192837465
 40 #define y2 y123456789
 41 
 42 #include<cstdio>
 43 #include<vector>
 44 #include<algorithm>
 45 using namespace std;
 46 
 47 // upd 2015.11.28
 48 class Bignum{
 49     private:
 50         vector<int>a;
 51         void clear(){a.resize(1);a[0]=0;}
 52         void set(int n){
 53             if(clear(),n){            
 54                 int t;a.clear();
 55                 while(n)a.push_back(n-(t=n/10000)*10000),n=t;
 56             }
 57         }
 58         void set(long long n){
 59             if(clear(),n){            
 60                 long long t;a.clear();
 61                 while(n)a.push_back(n-(t=n/10000)*10000),n=t;
 62             }
 63         }
 64         void clearzero(){
 65             int l=a.size();
 66             while(l>1 && a[l-1]==0)l--;
 67             a.resize(l);
 68         }
 69         void set(char*st){
 70             char c;vector<int> s;a.clear();
 71             while(((c=*(st++))<0 || c>9));s.push_back(c-0);
 72             while((c=*(st++))>=0&&c<=9)s.push_back(c-0);          
 73             int l=s.size();
 74             for (int i=l-1;i-3>=0;i-=4)a.push_back(s[i]+s[i-1]*10+s[i-2]*100+s[i-3]*1000);
 75             if(l&3){
 76                 int tmp=0;
 77                 for (int i=0;i<(l&3);i++)tmp=tmp*10+s[i];
 78                 a.push_back(tmp);
 79             }
 80             clearzero();
 81         }
 82     public:
 83         Bignum(){clear();}
 84         Bignum(int n){set(n);}
 85         Bignum(long long n){set(n);}
 86         Bignum(char*s){set(s);}
 87         Bignum& operator=(int a){set(a);return *this;}
 88         Bignum& operator=(long long a){set(a);return *this;}
 89         Bignum& operator=(char *s){set(s);return *this;}
 90         int size()const{
 91             if(a.back()>=1000)return a.size()<<2;
 92             else if(a.back()>=100)return (a.size()<<2)-1;
 93             else if(a.back()>=10)return (a.size()<<2)-2;
 94             else return (a.size()<<2)-3;
 95         }
 96         long long toint()const{
 97             long long ans=0;
 98             for (int i=int(a.size())-1;i>=0;i--)ans=ans*10000+a[i];
 99             return ans;
100         }
101         int tostr(char *s)const{
102             int tot=0;
103             for (unsigned int i=0;i<a.size();i++)
104                 if(i+1==a.size()){
105                     int x=a[i];
106                     while(x)s[tot++]=0+x%10,x/=10;
107                 }else{
108                     int x=a[i];
109                     for (int j=0;j<4;j++)s[tot++]=0+x%10,x/=10;
110                 }
111             if(tot==0)s[tot++]=0;
112             for (int i=0,j=tot-1;i<j;i++,j--)swap(s[i],s[j]);
113             s[tot]=0;
114             return tot;
115         }
116         int toarr(int *s)const{
117             int tot=0;
118             for (unsigned int i=0;i<a.size();i++)
119                 if(i+1==a.size()){
120                     int x=a[i];
121                     while(x)s[tot++]=x%10,x/=10;
122                 }else{
123                     int x=a[i];
124                     for (int j=0;j<4;j++)s[tot++]=x%10,x/=10;
125                 }
126             if(tot==0)s[tot++]=0;
127             return tot;
128         }
129         int comp(const Bignum&x)const{
130             if(a.size()<x.a.size())return 1;
131             if(a.size()>x.a.size())return -1;
132             for (int i=a.size()-1;i>=0;i--)
133                 if(a[i]<x.a[i])return 1;
134                 else if(a[i]>x.a[i])return -1;
135             return 0;
136         }
137         bool operator<(const Bignum&x)const{return comp(x)==1;}
138         bool operator>(const Bignum&x)const{return comp(x)==-1;}
139         bool operator<=(const Bignum&x)const{return comp(x)!=-1;}
140         bool operator>=(const Bignum&x)const{return comp(x)!=1;}
141         bool operator!=(const Bignum&x)const{return comp(x)!=0;}
142         bool operator==(const Bignum&x)const{return comp(x)==0;}    
143         Bignum& operator+=(const Bignum&x){
144             int l=max(a.size(),x.a.size())+1;
145             a.resize(l);
146             for (unsigned int i=0;i<x.a.size();i++)a[i]+=x.a[i];
147             for (int i=0;i<l;i++)if(a[i]>=10000)a[i]-=10000,a[i+1]++;
148             clearzero();return *this;
149         }
150         Bignum& operator-=(const Bignum&x){
151             for (unsigned int i=0;i<x.a.size();i++)a[i]-=x.a[i];
152             for (unsigned int i=0;i<a.size();i++)if(a[i]<0)a[i]+=10000,a[i+1]--;
153             clearzero();return *this;
154         }
155         Bignum operator+(const Bignum&x)const{Bignum y=*this;y+=x;return y;}        
156         Bignum operator-(const Bignum&x)const{Bignum y=*this;y-=x;return y;}        
157         Bignum operator*(const Bignum&x)const{
158             int l=a.size()+x.a.size();
159             Bignum z;z.a.clear();
160             vector<long long>tmp;tmp.resize(l);
161             for (unsigned int i=0;i<x.a.size();i++)
162                 for (unsigned int j=0;j<a.size();j++)
163                     tmp[i+j]+=x.a[i]*a[j];
164             for (int i=0;i<l;i++){
165                 long long t=tmp[i]/10000;
166                 z.a.push_back(int(tmp[i]-t*10000));
167                 t?tmp[i+1]+=t:0;
168             }
169             z.clearzero();return z;
170         }
171         Bignum operator*(long long x)const{
172             if(x>=10000)return *this * Bignum(x);
173             int l=a.size()+2;
174             Bignum z;z.a.resize(l);
175             for (unsigned int i=0;i<a.size();i++)z.a[i]=a[i]*x;
176             for (int i=0,t;i<l;i++)z.a[i+1]+=(t=z.a[i]/10000),z.a[i]-=t*10000;
177             z.clearzero();return z;
178         }
179         friend Bignum operator*(long long x,const Bignum&a){return a*x;}
180         Bignum& operator*=(long long x){return *this=*this*x;}
181         Bignum& operator*=(const Bignum&x){return *this=*this*x;}        
182         Bignum operator/(const Bignum &x)const{
183             Bignum b=*this,q,tmp;q.a.resize(b.a.size());    
184             for(int i=int(b.a.size())-int(x.a.size());b>=x;i--,b.clearzero()){
185                 tmp.a.assign(b.a.begin()+i,b.a.end());                
186                 int l=0,r=10000-1,m;
187                 while(l<=r){
188                     m=(l+r)>>1;
189                     if(x*m<=tmp)l=m+1;
190                     else r=m-1;
191                 }
192                 for (unsigned int j=0;j<tmp.a.size();j++)b.a[j+i]=0;
193                 q.a[i]=r;tmp-=x*r;
194                 for (unsigned int j=0;j<tmp.a.size();j++)b.a[j+i]=tmp.a[j];
195             }
196             q.clearzero();return q;
197         }
198         Bignum operator%(const Bignum &x)const{return *this-*this/x*x;}
199         Bignum& operator/=(const Bignum &x){return *this=*this/x;}
200         Bignum& operator%=(const Bignum &x){return *this=*this%x;}
201 
202         Bignum& operator/=(long long x){
203             if(x>=10000)return *this/=Bignum(x);
204             else{
205                 for (int i=int(a.size())-1;i>=0;i--){
206                     int t=a[i]/x;
207                     if(i)a[i-1]+=(a[i]-t*x)*10000;
208                     a[i]=t;
209                 }
210                 clearzero();
211                 return *this;
212             }
213         }
214         Bignum operator/(long long x)const{
215             if(x>=10000)return *this / Bignum(x);
216             else{
217                 Bignum y=*this;y/=x;return y;
218             }
219         }
220         long long operator%(long long x)const{
221             if(x>=10000)return (*this%Bignum(x)).toint();
222             else{
223                 long long ans=0;
224                 for (int i=int(a.size())-1;i>=0;i--)ans=(ans*10000+a[i])%x;
225                 return ans;
226             }
227         }
228         int operator%(int x)const{
229             return int(operator%((long long)x));
230         }
231         Bignum& operator%=(long long x){
232             if(x>=10000)return *this%=Bignum(x);
233             else return *this=*this%x;
234         }
235         int to2(int *s)const{
236             int tot=0;
237             vector<unsigned long long>t;
238             for (unsigned int i=0;i<a.size();i++)t.push_back((unsigned long long)a[i]);
239             while(!t.empty()){
240                 for (int i=int(t.size())-1;i>=0;i--){
241                     unsigned long long r=t[i]>>32;
242                     if(i==0){
243                         s[tot++]=int(t[i]&0xffffffff);
244                     }else{
245                         t[i-1]+=(t[i]&((unsigned long long)0xffffffff))*10000;
246                     }
247                     t[i]=r;
248                 }
249                 while(!t.empty() && t.back()==0)t.pop_back();
250             }
251             return tot;
252         }
253         bool input(){
254             char c;vector<int> s;a.clear();
255             while(((c=getchar())<0 || c>9) && c!=EOF);
256             if(c==EOF){a.push_back(0);return 0;}
257             s.push_back(c-0);
258             while((c=getchar())>=0&&c<=9)s.push_back(c-0);          
259             int l=s.size();
260             for (int i=l-1;i-3>=0;i-=4)a.push_back(s[i]+s[i-1]*10+s[i-2]*100+s[i-3]*1000);
261             if(l&3){
262                 int tmp=0;
263                 for (int i=0;i<(l&3);i++)tmp=tmp*10+s[i];
264                 a.push_back(tmp);
265             }
266             clearzero();
267             return 1;
268         }
269         void output()const{
270             for (int i=int(a.size())-1;i>=0;i--)
271                 if(i==int(a.size())-1)printf("%d",a[i]);
272                 else printf("%04d",a[i]);
273         }
274         void outputln()const{
275             output();putchar(\n);
276         }
277         friend Bignum max(const Bignum &a,const Bignum &b){return a>b?a:b;}
278         friend Bignum min(const Bignum &a,const Bignum &b){return a<b?a:b;}
279 };
280 Bignum a,N;
281 int  main()
282 {
283     int tes;gn(tes);
284     while(tes--){
285         int n;gn(n);
286         if(n<=10){
287             printf("%d\n",n*(n-1)*(n-2)*(n-3)/24+n);
288         }else{
289             N=n;
290             a=N*(N-1)*(N-2)*(N-3)/24+N;
291             a.outputln();
292         }
293         }
294         return 0;
295         }

 

高精度模板(From JCVB)

标签:==   11.2   oar   bitset   define   getch   friend   int()   std   

原文地址:https://www.cnblogs.com/myx12345/p/9319061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!