之前大数的模板一直都没有系统的整理过,今天就在这里整理一下吧。加减乘都没什么说的,模拟小学运算就行,除操作基本是在“试商”,先令被除数位数与除数位数保持一致,然后利用减法试出这一位的商,然后记录余数,令余数*10+被除数下一位,成为新的“被除数片段”,重复以上操作直至耗尽被除数位数。
注:本模板仅用于大数的整数加减乘除操作,如需浮点数,需在本模板基础上略作修改(其实就是挪小数点)。
#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef unsigned long long ll;
//typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef queue<int> q_i;
typedef queue<string> q_s;
typedef queue<double> q_d;
typedef queue<ll> q_ll;
typedef queue<char> q_c;
typedef priority_queue<int> pq_i;
typedef priority_queue<string> pq_s;
typedef priority_queue<double> pq_d;
typedef priority_queue<ll> pq_ll;
typedef stack<int> s_i;
typedef stack<string> s_s;
typedef stack<double> s_d;
typedef stack<ll> s_ll;
typedef stack<char> s_c;
typedef map<ll,ll> m_ll_ll;
typedef map<int,ll> m_i_ll;
typedef map<int,int> m_i_i;
typedef map<string,ll> m_s_ll;
typedef map<char,int> m_c_i;
typedef map<char,ll> m_c_ll;
const ll INF=0x3f3f3f3f;
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define per(i,l,r) for(int i=r;i>=l;i--)
#define eif else if
#define N 100005
#define mm(dp) memset(dp,0,sizeof(dp))
#define mm1(dp) memset(dp,-1,sizeof(dp))
#define mm2(dp) memset(dp,0x3f,sizeof(dp))
#define IT set::iterator
#define fs(n) fixed<< setprecision(n)
//const double e=2.71828182845;
#define max(a,b) a>b?a:b
#define min(a,b) a
const double pi = acos(-1.0);
void read(int &x)
{
char ch=getchar();
x=0;
for(; ch<'0'||ch>'9'; ch=getchar());
for(; ch>='0'&&ch<='9'; x=x*10+ch-'0',ch=getchar());
}
inline void write(ll x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
}
float SqrtByCarmack( float number )
{
int i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( int * ) &y;
i = 0x5f375a86 - ( i >> 1 );
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) );
y = y * ( threehalfs - ( x2 * y * y ) );
y = y * ( threehalfs - ( x2 * y * y ) );
return number*y;
}
ll qpow(ll a,ll b,ll mod)
{
ll sum=1;
while(b)
{
if(b%2==1)
{
sum=sum*a%mod;
}
b/=2;
a=a*a%mod;
}
return sum;
}
int erfen(int *a,int start,int endd,int l)//小于等于l的最大值的角标
{
int mid=(start+endd)/2;
if((a[mid]<=l&&a[mid+1]>l)||(mid==endd&&a[mid]<=l))
return mid;
else if(a[mid]<=l)
return erfen(a,mid+1,endd,l);
else if(a[mid]>l)
{
if(start!=mid)
return erfen(a,start,mid,l);
else
return start-1;
}
}
ll prime[6] = {2, 3, 5, 233, 331};
ll qmul(ll x, ll y, ll mod)
{
return (x * y - (long long)(x / (long double)mod * y + 1e-3) *mod + mod) % mod;
}
bool Miller_Rabin(ll p)
{
if(p < 2)
return 0;
if(p != 2 && p % 2 == 0)
return 0;
ll s = p - 1;
while(! (s & 1))
s >>= 1;
for(int i = 0; i < 5; ++i)
{
if(p == prime[i])
return 1;
ll t = s, m = qpow(prime[i], s, p);
while(t != p - 1 && m != 1 && m != p - 1)
{
m = qmul(m, m, p);
t <<= 1;
}
if(m != p - 1 && !(t & 1))
return 0;
}
return 1;
}
ll gcd(ll x,ll y)
{
if(y==0)
return x;
else
return gcd(y,x%y);
}
string add(string x,string y)//高精度加法
{
int len1=x.size();
int len2=y.size();
int lenx=max(len1,len2);
int lenn=min(len1,len2);
reverse(x.begin(),x.end());
reverse(y.begin(),y.end());
string str;
int v=0;
for(int i=0; i<lenn; i++)
{
int u=(x[i]-'0')+(y[i]-'0')+v;
if(u>=10)
{
v=1;
u-=10;
}
else
v=0;
char ch=(char)(u+'0');
str+=ch;
}
if(v==1)
{
for(int i=lenn; i<lenx; i++)
{
int u;
if(len1>len2)
u=(x[i]-'0')+v;
else
u=(y[i]-'0')+v;
if(u>=10)
{
v=1;
u-=10;
}
else
v=0;
str+=(char)(u+'0');
}
}
else
{
for(int i=lenn; i<lenx; i++)
{
int u;
if(len1>len2)
u=x[i]-'0';
else
u=y[i]-'0';
str+=(char)(u+'0');
}
}
if(v==1)
str+='1';
reverse(str.begin(),str.end());
return str;
}
string minus1(string x,string y)//高精度减法
{
int flag=0;
string str;
if(x.compare(y)==0)
{
str="0";
return str;
}
else if(x.size()<y.size()||(x.size()==y.size()&&x.compare(y)<0))
{
string u=x;
x=y;
y=u;
flag=1;
}
int len1=x.size();
int len2=y.size();
reverse(x.begin(),x.end());
reverse(y.begin(),y.end());
int v=0;
for(int i=0; i<len2; i++)
{
int u=(x[i]-'0')-(y[i]-'0')+v;
if(u<0)
{
v=-1;
u+=10;
}
else
v=0;
char ch=(char)(u+'0');
str+=ch;
}
if(v==-1)
{
for(int i=len2; i<len1; i++)
{
int u;
u=(x[i]-'0')+v;
if(u<0)
{
v=-1;
u+=10;
}
else
v=0;
str+=(char)(u+'0');
}
}
else
{
for(int i=len2; i<len1; i++)
{
int u;
u=x[i]-'0';
str+=(char)(u+'0');
}
}
string str1;
int len=str.size();
reverse(str.begin(),str.end());
int flag1=0;
for(int i=0; i<len; i++)
{
if(str[i]!='0')
{
flag1=i;
break;
}
}
str1=str.substr(flag1);
reverse(str1.begin(),str1.end());
if(flag==1)
str1+='-';
reverse(str1.begin(),str1.end());
return str1;
}
string cheng(string x,string y)//高精度乘法
{
if((x.size()<y.size())||(x.size()==y.size()&&x.compare(y)<0))
{
string s;
s=x;
x=y;
y=s;
}
if(y.compare("0")==0)
{
string ss="0";
return ss;
}
int len1=x.size();
int len2=y.size();
reverse(x.begin(),x.end());
reverse(y.begin(),y.end());
string str;
int ans[90000];
memset(ans,0,sizeof(ans));
for(int i=0;i<len2;i++)
{
for(int j=0;j<len1;j++)
{
ans[i+j]+=(x[j]-'0')*(y[i]-'0');
}
}
int cnt=len1+len2;
for(int i=0;i<cnt;i++)
{
ans[i+1]+=(ans[i]/10);
ans[i]=ans[i]%10;
}
int k=0;
for(int i=cnt;i>=0;i--)
{
if(ans[i]!=0)
k=1;
if(ans[i]==0&&k==0)
continue;
char ch=(char)(ans[i]+'0');
str+=ch;
}
return str;
}
typedef struct
{
string shang,yu;
}STU;
STU chu(string a,string b)//高精度除法+取余
{
STU stu;
int lena=a.size();
int lenb=b.size();
if(minus1(b,"0")=="0")
{
stu.shang="error";
stu.yu="error";
return stu;
}
string str=minus1(a,b);
if(str[0]=='-')
{
stu.shang="0";
stu.yu=b;
return stu;
}
int u=lena-lenb;
string c="0";
string ans="";
string ss=a.substr(0,lenb);
for(int i=0;i<=u;i++)
{
c+="0";
if(i!=0)
ss=a.substr(i+lenb-1,1);
ss=add(ss,c);
int k=0;
int flag=0;
string s1="";
for(int j=0;j<=ss.size()-1;j++)
{
if(flag==0&&ss[j]!='0')
{
flag=1;
s1+=ss[j];
}
else if(flag==1)
{
s1+=ss[j];
}
}
ss=s1;
ss=minus1(ss,b);
while(ss[0]!='-')
{
k++;
ss=minus1(ss,b);
}
if(ss[0]=='-')
{
ss=ss.substr(1,ss.size()-1);
ss=minus1(b,ss);
}
switch(k)
{
case 1:
ans+="1";
break;
case 2:
ans+="2";
break;
case 3:
ans+="3";
break;
case 4:
ans+="4";
break;
case 5:
ans+="5";
break;
case 6:
ans+="6";
break;
case 7:
ans+="7";
break;
case 8:
ans+="8";
break;
case 9:
ans+="9";
break;
case 0:
ans+="0";
break;
default:
break;
}
c=ss;
}
int flag=0;
string s1="";
for(int j=0;j<=ans.size()-1;j++)
{
if(flag==0&&ans[j]!='0')
{
flag=1;
s1+=ans[j];
}
else if(flag==1)
{
s1+=ans[j];
}
}
ans=s1;
stu.shang=ans;
stu.yu=c;
return stu;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string a;
string b;
cin>>a>>b;
STU stu=chu(a,b);
cout<<stu.shang<<" "<<stu.yu<<endl;
return 0;
}