华师一附中OI组
标题:
(重载)高精度任意数快速幂
[打印本页]
作者:
Settwarl
时间:
2014-12-23 19:19
标题:
(重载)高精度任意数快速幂
支持5万位。用运算符重载实现。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAXL=50000;
struct SClong
{
int len,s[MAXL];
SClong(){len=1;memset(s,0,sizeof(s));}
SClong operator=(const char* num)
{
len=strlen(num);
for(int i=1;i<=len;i++)s[i-1]=num[len-i]-'0';
return *this;
}
SClong operator=(int num)
{
char a[MAXL];
sprintf(a,"%d",num);
*this=a;
return *this;
}
SClong(const char* num){*this=num;}
SClong(int num){*this=num;}
SClong operator+(const SClong &num)
{
SClong c;
c.len=max(len,num.len)+1;
for(int i=0;i<c.len;i++)
{
c.s[i]=c.s[i]+s[i]+num.s[i];
c.s[i+1]+=c.s[i]/10;
c.s[i]%=10;
}
if(c.s[c.len-1]==0)--c.len;
return c;
}
SClong operator+=(const SClong &num)
{
*this=*this+num;
return *this;
}
SClong operator*(const SClong &num)
{
SClong c;
c.len=len+num.len;
for(int i=0;i<len;i++)
for(int j=0;j<num.len;j++)
{
c.s[i+j]=c.s[i+j]+s[i]*num.s[j];
c.s[i+j+1]+=c.s[i+j]/10;
c.s[i+j]=c.s[i+j]%10;
}
if(c.s[c.len-1]==0)--c.len;
return c;
}
SClong operator*=(const SClong &num)
{
*this=*this*num;
return *this;
}
};
ostream &operator<<(ostream&out,const SClong&num)
{
for(int i=num.len-1;i>=0;--i)cout<<num.s[i];
}
istream &operator>>(istream&in, SClong&num)
{
char n[MAXL];
in>>n;
num=n;
return in;
}
SClong exp(SClong a,int t)
{
SClong ans=1,base=a;
while(t>0)
{
if(t%2)ans*=base;
t/=2;
base*=base;
}
return ans;
}
int main()
{
SClong m;
int n;
while(true)
{
cin>>m>>n;
cout<<exp(m,n);
cout<<endl<<endl;
}
return 0;
}
复制代码
欢迎光临 华师一附中OI组 (http://hsyit.cn/)
Powered by Discuz! X3.2