2018暑假集训 DAY5 T4 幂运算
Posted greed-vi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018暑假集训 DAY5 T4 幂运算相关的知识,希望对你有一定的参考价值。
本道题我没打出来,因为c++字符串处理不会用,后来托这个网站的福,打出来了,用了高精度类,300多行,应该是我打过最长的代码了吧。
1 #include<bits/stdc++.h> 2 using namespace std; 3 class Int{ 4 #define BASE 1000000000 5 public: 6 typedef long long value; 7 void New(size_t l){ 8 if (a!=NULL)delete[] a;a=new value[l]; 9 len=1;a[0]=0;sign=1; 10 } 11 Int():a(NULL),base(BASE){New(1);} 12 Int(value x):a(NULL),base(BASE){New(1);*this=x;} 13 Int(value x,value _base):a(NULL),base(_base){New(1);*this=x;} 14 Int(const Int &B):a(NULL),base(BASE){New(1);*this=B;} 15 ~Int(){delete[] a;} 16 Int& operator =(value x){ 17 size_t l=1;for (value x1=max(x,-x);x1>=base;++l,x1/=base);New(l); 18 if (x<0)x=-x,sign=0;else sign=1; 19 len=0;while (x)a[len++]=x%base,x/=base; 20 if (!len)a[len++]=0; 21 return *this; 22 } 23 Int& operator =(const Int &A){ 24 New(A.len);len=A.len;memcpy(a,A.a,sizeof(value)*len); 25 base=A.base;sign=A.sign;return *this; 26 } 27 friend Int operator -(Int A){A.sign=1-A.sign;return A;} 28 bool operator !(){if (len==1&&a[0]==0)return 1;else return 0;} 29 friend Int operator +(Int A,Int B){ 30 if (A.sign!=B.sign){B.sign=1-B.sign;return A-B;} 31 if (A.base!=B.base) 32 if (A.base>B.base)B.set_base(A.base); 33 else A.set_base(B.base); 34 Int res;res.set_base(A.base); int len=A.len>B.len?A.len:B.len; 35 res.New(len+1);res.sign=A.sign; 36 memset(res.a,0,(len+1)*sizeof(value)); 37 for (int i=0;i<len;++i){ 38 if (i<A.len)res.a[i]+=A.a[i]; 39 if (i<B.len)res.a[i]+=B.a[i]; 40 } 41 for (int i=0;i<len;++i) 42 if (res.a[i]>=res.base)++res.a[i+1],res.a[i]-=res.base; 43 if (res.a[len])res.len=len+1;else res.len=len; 44 if (!res)res.sign=1;return res; 45 } 46 friend Int operator -(Int A,Int B){ 47 if (A.sign!=B.sign){B.sign=1-B.sign;return A+B;} 48 if (A.base!=B.base) 49 if (A.base>B.base)B.set_base(A.base); 50 else A.set_base(B.base); 51 if (small(A,B))swap(A,B),A.sign=1-A.sign; 52 Int res;res.set_base(A.base); int len=A.len>B.len?A.len:B.len; 53 res.New(len);res.sign=A.sign; 54 memset(res.a,0,len*sizeof(value)); 55 for (int i=0;i<len;++i){ 56 if (i>=B.len)res.a[i]+=A.a[i]; 57 else res.a[i]+=A.a[i]-B.a[i]; 58 if (res.a[i]<0)res.a[i]+=res.base,--res.a[i+1]; 59 } 60 while (len>1&&!res.a[len-1])--len;res.len=len; 61 if (!res)res.sign=1;return res; 62 } 63 friend Int operator *(Int A,Int B){ 64 if (A.base!=B.base) 65 if (A.base>B.base)B.set_base(A.base); 66 else A.set_base(B.base); 67 Int res;res.set_base(A.base); int len=A.len+B.len; 68 res.New(len);res.sign=(A.sign==B.sign); 69 memset(res.a,0,len*sizeof(value)); 70 for (int i=0;i<A.len;++i) 71 for (int j=0;j<B.len;++j){ 72 res.a[i+j]+=A.a[i]*B.a[j]; 73 res.a[i+j+1]+=res.a[i+j]/res.base; 74 res.a[i+j]%=res.base; 75 } 76 /* 77 for (int i=0;i<A.len;++i) 78 for (int j=0;j<B.len;++j)res.a[i+j]+=A.a[i]*B.a[j]; 79 for (int i=0;i<len-1;++i)res.a[i+1]+=res.a[i]/res.base,res.a[i]%=res.base; 80 */ 81 while (len>1&&!res.a[len-1])--len;res.len=len; 82 return res; 83 } 84 friend pair<Int,Int> divide(Int A,Int B){ 85 if (!B){puts("error:div zero!");for (;;);} 86 if (A.base!=B.base) 87 if (A.base>B.base)B.set_base(A.base); 88 else A.set_base(B.base); 89 if (small(A,B))return make_pair(Int(0),A); 90 Int C,D;C.set_base(A.base);D.set_base(A.base);C.New(A.len);C.len=A.len; 91 bool Csign=(A.sign==B.sign),Dsign=A.sign;A.sign=B.sign=1; 92 for (int i=A.len-1;i>=0;--i){ 93 C.a[i]=0;D=D*D.base;D.a[0]=A.a[i]; 94 int l=0,r=A.base-1,mid; 95 while (l<r){ 96 mid=(l+r+1)>>1; 97 if (small(B*mid,D+1))l=mid; 98 else r=mid-1; 99 } 100 C.a[i]=l;D=D-B*l; 101 } 102 C.sign=Csign;D.sign=Dsign;if (!D)D.sign=1; 103 while (C.len>1&&!C.a[C.len-1])--C.len; 104 return make_pair(C,D); 105 } 106 Int operator /(value x){ 107 if (!x){puts("error:div zero!");for (;;);} 108 value d=0;Int res;res.set_base(base);res.New(len);res.len=len; 109 if (x<0)x=-x,res.sign=(sign==0); 110 else res.sign=(sign==1); 111 for (int i=len-1;i>=0;--i) 112 d=d*base+a[i],res.a[i]=d/x,d%=x; 113 while (res.len>1&&!res.a[res.len-1])--res.len; 114 return res; 115 } 116 Int operator %(value x){ 117 value d=0;if (x<0)x=-x; 118 for (int i=len-1;i>=0;--i)d=(d*base+a[i])%x; 119 return d; 120 } 121 friend Int abs(Int A){A.sign=1;return A;} 122 friend bool small(Int A,Int B){ 123 if (A.base!=B.base) 124 if (A.base>B.base)B.set_base(A.base); 125 else A.set_base(B.base); 126 if (A.len!=B.len)return A.len<B.len; 127 for (int i=A.len-1;i>=0;--i) 128 if (A.a[i]!=B.a[i])return A.a[i]<B.a[i]; 129 return 0; 130 } 131 friend bool operator <(Int A,Int B){ 132 if (A.sign!=B.sign)return A.sign<B.sign; 133 return A.sign==1?small(A,B):small(B,A); 134 } 135 friend bool operator ==(Int A,Int B){ 136 if (A.base!=B.base) 137 if (A.base>B.base)B.set_base(A.base); 138 else A.set_base(B.base); 139 if (A.sign!=B.sign||A.len!=B.len)return 0; 140 for (int i=0;i<A.len;++i)if (A.a[i]!=B.a[i])return 0; 141 return 1; 142 } 143 friend bool operator !=(Int A,Int B){return !(A==B);} 144 friend bool operator >(Int A,Int B){return !(A<B||A==B);} 145 friend bool operator <=(Int A,Int B){return A<B||A==B;} 146 friend bool operator >=(Int A,Int B){return A>B||A==B;} 147 Int operator /(Int B){return divide(*this,B).first;} 148 Int operator %(Int B){return divide(*this,B).second;} 149 Int& operator +=(Int B){*this=*this+B;return *this;} 150 Int& operator -=(Int B){*this=*this-B;return *this;} 151 Int& operator *=(Int B){*this=*this*B;return *this;} 152 Int& operator /=(Int B){*this=*this/B;return *this;} 153 Int& operator %=(Int B){*this=*this%B;return *this;} 154 Int& operator ++(){*this=*this+1;return *this;} 155 Int& operator --(){*this=*this-1;return *this;} 156 Int operator ++(int){Int res(*this);*this=*this+1;return res;} 157 Int operator --(int){Int res(*this);*this=*this-1;return res;} 158 Int operator +(value x){return *this+Int(x,this->base);} 159 Int operator -(value x){return *this-Int(x,this->base);} 160 Int operator *(value x){return *this*Int(x,this->base);} 161 //Int operator /(value x){Int T;T=x;return *this/T;} 162 //Int operator %(value x){Int T;T=x;return *this%T;} 163 Int& operator *=(value x){*this=*this*x;return *this;} 164 Int& operator +=(value x){*this=*this+x;return *this;} 165 Int& operator -=(value x){*this=*this-x;return *this;} 166 Int& operator /=(value x){*this=*this/x;return *this;} 167 Int& operator %=(value x){*this=*this%x;return *this;} 168 bool operator ==(value x){return *this==Int(x,this->base);} 169 bool operator !=(value x){return *this!=Int(x,this->base);} 170 bool operator <=(value x){return *this<=Int(x,this->base);} 171 bool operator >=(value x){return *this>=Int(x,this->base);} 172 bool operator <(value x){return *this<Int(x,this->base);} 173 bool operator >(value x){return *this>Int(x,this->base);} 174 friend Int gcd(Int x,Int y){ 175 Int t;int cnt=0; 176 while (1){ 177 if (x<y)t=x,x=y,y=t; 178 if (y==0){ 179 while (cnt--)x*=2; 180 return x; 181 } 182 if (x%2==0&&y%2==0)x/=2,y/=2,++cnt; 183 else if (x%2==0)x/=2; 184 else if (y%2==0)y/=2; 185 else {t=x;x=y;y=t-y;} 186 } 187 } 188 void to_arr(char *c){ 189 char *c1=c; 190 for (int i=0;i<len-1;++i) 191 for (value x=a[i],b=base/10;b>=1;b/=10)*c1++=‘0‘+x%10,x/=10; 192 for (value x=a[len-1];x>0;x/=10)*c1++=‘0‘+x%10; 193 if (len==1&&a[len]==0)*c1++=‘0‘; 194 if (sign==0)*c1++=‘-‘;*c1=0;reverse(c,c1); 195 } 196 void from_arr(char *c){ 197 size_t base_l=get_basel(),b=1;int cl=strlen(c);value x=0; 198 New((cl+base_l-1)/base_l);len=0; 199 if (*c==‘-‘)sign=0,++c,--cl;else sign=1; 200 while (--cl>=0){ 201 x+=(c[cl]-‘0‘)*b;b*=10;if (b==base)a[len++]=x,x=0,b=1; 202 } 203 if (!len||x)a[len++]=x; 204 while (len>1&&!a[len-1])--len; 205 } 206 void set_base(int _base){ 207 if (base==_base)return; 208 char *c=new char[len*get_basel()+1]; 209 to_arr(c);base=_base;from_arr(c); 210 delete[] c; 211 } 212 void set_basel(int _l){ 213 size_t _base=1;while (_l--)_base*=10;set_base(_base); 214 } 215 void read(){ 216 vector<char> s;char ch; 217 scanf(" %c",&ch);if (ch==‘-‘)s.push_back(‘-‘),ch=getchar(); 218 for (;ch>=‘0‘&&ch<=‘9‘;ch=getchar())s.push_back(ch); 219 char *c=new char[s.size()+1]; 220 for (int i=0;i<s.size();++i)c[i]=s[i];c[s.size()]=0; 221 from_arr(c);delete[] c; 222 if (!*this)this->sign=1; 223 } 224 void print(){ 225 if (!sign)putchar(‘-‘); 226 printf("%d",int(a[len-1])); 227 for (int i=len-2;i>=0;--i){ 228 for (int j=base/10;j>=10;j/=10) 229 if (a[i]<j)putchar(‘0‘); 230 else break; 231 printf("%d",(int)a[i]); 232 } 233 } 234 string inttostring(int x){ 235 stringstream a; 236 a<<x; 237 string b; 238 a>>b; 239 return b; 240 } 241 string prin(){ 242 string ss=""; 243 if (!sign) ss+=‘-‘; 244 ss+=inttostring(a[len-1]); 245 for (int i=len-2;i>=0;--i){ 246 for (int j=base/10;j>=10;j/=10) 247 if (a[i]<j) ss+=‘0‘; 248 else break; 249 ss+=inttostring(a[i]); 250 } 251 return ss; 252 } 253 void println(){print();putchar(‘ ‘);} 254 private: 255 value *a,base;int len;bool sign; //0="-" 256 size_t get_basel()const 257 { 258 size_t res=0;for (int b=base/10;b>=1;b/=10,++res); 259 return res; 260 } 261 #undef BASE 262 };//高精类 263 string s,ans; 264 Int a,c=1,ll=1,l=1; 265 int b,k,ppop[3010]; 266 int main(){ 267 freopen("exp.in","r",stdin); 268 freopen("exp.out","w",stdout); 269 cin>>s; 270 cin>>b; 271 int tp=0;int len=s.size();int lls; 272 for(int i=0;i<len;i++){ 273 if(s[i]==‘.‘) lls=i; 274 } 275 for(int i=lls+1;i<len;i++){ 276 ppop[tp++]=s[i]; 277 } 278 tp--; 279 while(ppop[tp]==‘0‘) tp--; 280 k=s.length(); 281 while(s[k-1]==‘0‘){s.erase(k-1,1);k--;} 282 if (s[0]==‘.‘) s=‘0‘+s; 283 for (int i=0;i<s.length();i++) 284 { 285 if (s[i]==‘.‘) l*=10; 286 else if (l>=10) a=a*10+s[i]-‘0‘;else a=a*10+s[i]-‘0‘,l*=10; 287 } 288 for (int i=1;i<=b;i++) c=c*a,ll=ll*l;//c为去小数点的数,l为小数点pow(10,位数),c/l为answer,但有精度问题。 289 string qwq; 290 qwq=c.prin(); 291 string pwp; 292 pwp=ll.prin(); 293 int us=(tp+1)*b; 294 us=qwq.size()-us;//找小数点位数 295 bool bi=0; 296 // us--; 297 if (us<0) putchar(‘.‘),bi=1; 298 while(us<0) 299 { 300 putchar(‘0‘); 301 us++; 302 } 303 for(int i=0;i<qwq.size();i++) 304 { 305 if(i==us&&bi==0) putchar(‘.‘);//到小数点输出小数点,否则输出数字 306 putchar(qwq[i]); 307 } 308 return 0; 309 }
以上是关于2018暑假集训 DAY5 T4 幂运算的主要内容,如果未能解决你的问题,请参考以下文章