// Problem: UVA12413 Big Decimal Calculator
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/UVA12413
// Memory Limit: 0 MB
// Time Limit: 10000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
// 10^5 each BIT
typedef short BIT;
typedef int GBIT;
const int MAX_FLOAT_BIT_COUNT = 15;
const int BIT_BOUND = 10000;
const int BIT_LEN = 4;
const int MAX_STR_LEN = MAX_FLOAT_BIT_COUNT * BIT_LEN;
const int VALUE_PART_LENGTH = 5;
const int DIVIDE_VALUE_DELTA = 1;
const GBIT MAX_GBIT = 2147483647;
const GBIT GBIT_LIMIT = MAX_GBIT - (GBIT) BIT_BOUND * BIT_BOUND;
struct Bdouble{
vector<BIT> Int;
vector<BIT> Flt;
bool isNeg;
Bdouble(){
Int.clear(); Int.push_back(0);
Flt.clear(); isNeg = false;
}
void minimize(){
while(!Flt.empty() && Flt.back() == 0)
Flt.pop_back();
while(Int.size()>1 && Int.back() == 0)
Int.pop_back();
}
template<class T>
Bdouble(T x){
c_str_bound(to_string(x).c_str());
}
inline void c_str_bound(const char* x){
Int.clear(); Flt.clear();
isNeg = false;
int len = strlen(x);
int y = 0;
while(y < len && (x[y] == '+' || x[y] == '-')){
if(x[y] == '-') isNeg ^= 1;
++y;
}
int p = y;
while(p < len && x[p] != '.') ++p;
for(int q=p-1; q>=y; q-=BIT_LEN){
BIT b = 0;
for(int j=max(y, q-BIT_LEN+1); j<=q; j++)
b = b * 10 + (x[j] ^ 48);
Int.push_back(b);
}
if(Int.empty()) Int.push_back(0);
for(int q=p+1; q<len; q+=BIT_LEN){
BIT b = 0;
for(int j=q; j<q+BIT_LEN; j++)
b = b * 10 + (j>=len?0:(x[j] ^ 48));
Flt.push_back(b);
if((int)Flt.size() == MAX_FLOAT_BIT_COUNT)
break;
}
minimize();
}
Bdouble(const char* x){c_str_bound(x);}
Bdouble(char* x){c_str_bound(x);}
Bdouble(string x){c_str_bound(x.c_str());}
string str(int fixed = MAX_STR_LEN){
string ret = "", q, r;
Bdouble t;
if(fixed >= 0 && fixed < MAX_STR_LEN){
string str = "";
if(isNeg) str = "-";
str += "0.";
for(int i=0; i<fixed; i++) str += "0";
str += "5";
t = Bdouble(str);
(*this) = (*this) + t;
}
if(isNeg) ret += "-";
ret += to_string(Int.back());
for(int i=Int.size()-2; i>=0; i--){
q = to_string(Int[i]);
if((int)q.size() != BIT_LEN)
q = string(BIT_LEN - q.size(), '0') + q;
ret += q;
}
if(fixed == 0) (*this) = (*this) - t;
if(fixed <= 0) return ret;
ret += ".";
for(int i=0; i<(int)Flt.size(); i++){
q = to_string(Flt[i]);
if((int)q.size() != BIT_LEN)
q = string(BIT_LEN - q.size(), '0') + q;
if(fixed <= BIT_LEN){
ret += q.substr(0, fixed);
return ret;
}
ret += q;
fixed -= BIT_LEN;
}
if(fixed != MAX_STR_LEN)
(*this) = (*this) - t;
return ret + string(fixed, '0');
}
Bdouble operator - (){
Bdouble ret = *this;
ret.isNeg ^= 1;
return ret;
}
Bdouble abs(){
Bdouble ret = *this;
ret.isNeg = false;
return ret;
}
bool operator < (Bdouble& x){
if(isNeg ^ x.isNeg)
return isNeg;
if(Int.size() != x.Int.size())
return (Int.size() < x.Int.size()) ^ isNeg;
for(int i=Int.size()-1; i>=0; i--)
if(Int[i] != x.Int[i])
return (Int[i] < x.Int[i]) ^ isNeg;
for(int i=0, p, q; i<(int)max(Flt.size(), x.Flt.size()); i++){
p = (i>=(int)Flt.size())?0:Flt[i];
q = (i>=(int)x.Flt.size())?0:x.Flt[i];
if(p != q)
return (p < q) ^ isNeg;
}
return false;
}
bool operator > (Bdouble x){
return x < (*this);
}
bool operator <= (Bdouble x){
return !(x < (*this));
}
bool operator >= (Bdouble x){
return !((*this) < x);
}
bool operator == (Bdouble x){
if(isNeg ^ x.isNeg) return false;
if(Int.size() != x.Int.size()) return false;
for(int i=Int.size()-1; i>=0; i--)
if(Int[i] != x.Int[i])
return false;
for(int i=0, p, q; i<(int)max(Flt.size(), x.Flt.size()); i++){
p = (i>=(int)Flt.size())?0:Flt[i];
q = (i>=(int)x.Flt.size())?0:x.Flt[i];
if(p != q) return false;
}
return true;
}
bool operator != (Bdouble& x){
return !((*this) == x);
}
Bdouble operator + (Bdouble x){
if(isNeg ^ x.isNeg)
return (*this) - (-x);
vector<BIT> I(max(x.Int.size(), Int.size()) + 1);
vector<BIT> F(max(x.Flt.size(), Flt.size()) + 1);
for(int i=0; i<(int)Int.size(); i++)
I[i] += Int[i];
for(int i=0; i<(int)x.Int.size(); i++)
I[i] += x.Int[i];
for(int i=0; i<(int)Flt.size(); i++)
F[i+1] += Flt[i];
for(int i=0; i<(int)x.Flt.size(); i++)
F[i+1] += x.Flt[i];
for(int i=(int)F.size()-1; i>0; i--){
F[i-1] += F[i] / BIT_BOUND;
F[i] %= BIT_BOUND;
}
I[0] += F[0];
for(int i=0; i<(int)I.size()-1; i++){
I[i+1] += I[i] / BIT_BOUND;
I[i] %= BIT_BOUND;
}
Bdouble ret;
for(int i=1; i<(int)F.size(); i++)
ret.Flt.push_back(F[i]);
while(I.size()>1 && I.back()==0) I.pop_back();
ret.Int = I;
ret.isNeg = isNeg;
ret.minimize();
if (ret.Flt.empty() && ret.Int.back() == 0)
ret.isNeg = false;
return ret;
}
Bdouble operator - (Bdouble x){
if(isNeg ^ x.isNeg)
return (*this) + (-x);
Bdouble* a = this;
Bdouble* b = &x;
Bdouble ret;
ret.isNeg = (*this) <= x;
if(((*this) < x) ^ isNeg)
a = &x, b = this;
vector<BIT> I(max(x.Int.size(), Int.size()) + 1);
vector<BIT> F(max(x.Flt.size(), Flt.size()) + 1);
for(int i=0; i<(int)a->Int.size(); i++)
I[i] += a->Int[i];
for(int i=0; i<(int)b->Int.size(); i++)
I[i] -= b->Int[i];
for(int i=0; i<(int)a->Flt.size(); i++)
F[i+1] += a->Flt[i];
for(int i=0; i<(int)b->Flt.size(); i++)
F[i+1] -= b->Flt[i];
for(int i=(int)F.size()-1; i>0; i--){
if(F[i] >= 0) continue;
--F[i-1]; F[i] += BIT_BOUND;
}
I[0] += F[0];
for(int i=0; i<(int)I.size()-1; i++){
if(I[i] >= 0) continue;
--I[i+1]; I[i] += BIT_BOUND;
}
ret.Flt.assign(F.begin()+1, F.end());
ret.Int = I;
ret.minimize();
if (ret.Flt.empty() && ret.Int.back() == 0)
ret.isNeg = false;
return ret;
}
Bdouble operator * (Bdouble x){
if(x.checkZero() || checkZero())
return Bdouble();
Bdouble ret;
ret.isNeg = isNeg ^ x.isNeg;
vector<BIT> v1(0), v2(0);
vector<GBIT> v3(0);
v1 = Int; reverse(v1.begin(), v1.end());
v1.insert(v1.end(), Flt.begin(), Flt.end());
v2 = x.Int; reverse(v2.begin(), v2.end());
v2.insert(v2.end(), x.Flt.begin(), x.Flt.end());
reverse(v1.begin(), v1.end());
reverse(v2.begin(), v2.end());
v3.resize(v1.size() + v2.size(), 0);
GBIT tmp, a;
for(int i=0; i<(int)v1.size(); i++)
for(int j=0; j<(int)v2.size(); j++){
tmp = (GBIT) v1[i] * v2[j];
if(GBIT_LIMIT - tmp < v3[i+j]){
a = tmp / BIT_BOUND;
tmp %= BIT_BOUND;
tmp += v3[i+j];
a += tmp / BIT_BOUND;
tmp %= BIT_BOUND;
v3[i+j+1] += a;
}
else tmp += v3[i+j];
v3[i+j] = tmp;
}
int f = Flt.size() + x.Flt.size();
if(f > MAX_FLOAT_BIT_COUNT)
if(v3[f - MAX_FLOAT_BIT_COUNT - 1] >= BIT_BOUND / 2)
++v3[f - MAX_FLOAT_BIT_COUNT];
for(int i=0; i<(int)v3.size()-1; i++){
v3[i+1] += v3[i] / BIT_BOUND;
v3[i] %= BIT_BOUND;
}
ret.Flt.assign(v3.begin() + max(f - MAX_FLOAT_BIT_COUNT, 0), v3.begin() + f);
reverse(ret.Flt.begin(), ret.Flt.end());
ret.Int.assign(v3.begin() + f, v3.end());
ret.minimize(); return ret;
}
Bdouble intMul(BIT x){
if(x == 0 || checkZero())
return Bdouble();
Bdouble ret;
ret.isNeg = isNeg ^ (x<0);
vector<BIT> v1(0);
vector<GBIT> v3(0);
v1 = Int; reverse(v1.begin(), v1.end());
v1.insert(v1.end(), Flt.begin(), Flt.end());
reverse(v1.begin(), v1.end());
v3.resize(v1.size() + 1, 0);
GBIT tmp, a;
for(int i=0; i<(int)v1.size(); i++){
tmp = (GBIT) v1[i] * x;
if(GBIT_LIMIT - tmp < v3[i]){
a = tmp / BIT_BOUND;
tmp %= BIT_BOUND;
tmp += v3[i];
a += tmp / BIT_BOUND;
tmp %= BIT_BOUND;
v3[i+1] += a;
}
else tmp += v3[i];
v3[i] = tmp;
}
int f = Flt.size();
if(f > MAX_FLOAT_BIT_COUNT)
if(v3[f - MAX_FLOAT_BIT_COUNT - 1] >= BIT_BOUND / 2)
++v3[f - MAX_FLOAT_BIT_COUNT];
for(int i=0; i<(int)v3.size()-1; i++){
v3[i+1] += v3[i] / BIT_BOUND;
v3[i] %= BIT_BOUND;
}
ret.Flt.assign(v3.begin() + max(f - MAX_FLOAT_BIT_COUNT, 0), v3.begin() + f);
reverse(ret.Flt.begin(), ret.Flt.end());
ret.Int.assign(v3.begin() + f, v3.end());
ret.minimize(); return ret;
}
void LeftSft(){
vector<BIT> I = Int, F = Flt;
BIT a = 0;
if(F.size())
Flt.assign(F.begin()+1, F.end()), a = F[0];
Int.clear(); Int.push_back(a);
Int.insert(Int.end(), I.begin(), I.end());
minimize();
}
void RightSft(){
vector<BIT> I = Int, F = Flt;
BIT a = 0;
Int.assign(I.begin()+1, I.end()), a = I[0];
if(Int.empty()) Int.push_back(0);
Flt.clear(); Flt.push_back(a);
Flt.insert(Flt.end(), F.begin(), F.end());
minimize();
}
bool checkZero(){
return Int.size() == 1 && Int.back() == 0 && Flt.size() == 0;
}
BIT getValue(int x){
if(x < 0) return Flt[-1-x];
return Int[x];
}
double getPartOfValue(int b){
double ret = 0.0, base = 1.0;
int l = -1 * Flt.size(), r = Int.size() - 1;
for(int i=b; i<=r; i++){
ret += base * getValue(i);
base *= BIT_BOUND;
}
base = 1.0 / BIT_BOUND;
for(int i=b-1; i>=max(r-VALUE_PART_LENGTH+1, l); i--){
ret += base * getValue(i);
base /= BIT_BOUND;
}
return ret;
}
Bdouble operator / (Bdouble x){
bool n = isNeg ^ x.isNeg;
Bdouble a = abs(), b = x.abs();
if(a.checkZero()) return Bdouble();
int dgt = 0;
while(a.Int.back() == 0)
a.LeftSft(), --dgt;
while(b.Int.back() == 0)
b.LeftSft(), ++dgt;
while(a.Int.size() < b.Int.size())
a.LeftSft(), --dgt;
while(a.Int.size() > b.Int.size())
b.LeftSft(), ++dgt;
int bs = b.Int.size()-1;
double X, Y = b.getPartOfValue(bs);
vector<BIT> ans(dgt + 2 + MAX_FLOAT_BIT_COUNT, 0);
for(int i=ans.size()-1; i>=0; i--){
if(a.checkZero()) break;
if(a.Int.size() >= b.Int.size()){
X = a.getPartOfValue(bs);
BIT q = X / Y;
bool check = false;
Bdouble curr = b.intMul(max(q-DIVIDE_VALUE_DELTA, 1));
for(int j=max(q-DIVIDE_VALUE_DELTA, 1); j<=min(q+DIVIDE_VALUE_DELTA, BIT_BOUND-1); j++){
if(curr >= a){
check = true;
ans[i] = j-1;
break;
}
curr = curr + b;
}
if(!check) ans[i] = BIT_BOUND - 1;
}
a = a - (b.intMul(ans[i]));
a.LeftSft();
}
if(ans[0] >= BIT_BOUND / 2) ++ans[1];
while((int)ans.size() < MAX_FLOAT_BIT_COUNT + 2)
ans.push_back(0);
for(int i=1; i<(int)ans.size(); i++){
ans[i+1] += ans[i] / BIT_BOUND;
ans[i] %= BIT_BOUND;
}
Bdouble ret;
ret.isNeg = n;
ret.Flt.assign(ans.begin()+1, ans.begin()+MAX_FLOAT_BIT_COUNT+1);
reverse(ret.Flt.begin(), ret.Flt.end());
ret.Int.assign(ans.begin()+MAX_FLOAT_BIT_COUNT+1, ans.end());
ret.minimize(); return ret;
}
void read(){
string str;
cin >> str;
c_str_bound(str.c_str());
}
void print(int fixed){
printf("%s\n", str(fixed).c_str());
}
};
Bdouble getEps(int x){
string str = "0.";
for(int i=1; i<x; i++) str += "0";
str += "1";
return Bdouble(str);
}
Bdouble eps = getEps(MAX_FLOAT_BIT_COUNT);
Bdouble exp(Bdouble x){
Bdouble ret(1);
Bdouble curr = x, frac(1);
while(1){
if(curr.checkZero()) break;
ret = ret + curr;
curr = curr * x;
curr = curr / (frac = frac + 1);
}
return ret;
}
Bdouble sqrt(Bdouble x){
Bdouble curr = x;
Bdouble last;
Bdouble t(2);
do{
last = curr;
curr = (curr + x / curr) / t;
}while(!(curr-last).checkZero());
return curr;
}
Bdouble ln(Bdouble x){
Bdouble q(1);
while(x >= 1.7666) q = q * 2, x = sqrt(x);
while(x <= 0.2333) q = q * 2, x = sqrt(x);
x = (x - 1) / (x + 1);
Bdouble frac(1), curr = x;
x = x * x;
Bdouble ans(0);
Bdouble t(2);
while(1){
Bdouble q = curr / frac;
if(q.checkZero()) break;
ans = ans + q;
curr = curr * x;
frac = frac + t;
}
return ans * t * q;
}
Bdouble pow(Bdouble x, Bdouble y){
return exp(x * ln(y));
}
Bdouble sin(Bdouble x){
Bdouble curr = x, frac(2), ans(0);
while(1){
Bdouble q = curr;
curr = curr * x / frac * x / (frac + 1);
frac = frac + 2;
q = q - curr;
ans = ans + q;
curr = curr * x / frac * x / (frac + 1);
frac = frac + 2;
if(curr.checkZero()) break;
}
return ans;
}
Bdouble cos(Bdouble x){
Bdouble curr(1), frac(1), ans(0);
while(1){
Bdouble q = curr;
curr = curr * x / frac * x / (frac + 1);
frac = frac + 2;
q = q - curr;
ans = ans + q;
curr = curr * x / frac * x / (frac + 1);
frac = frac + 2;
if(curr.checkZero()) break;
}
return ans;
}
Bdouble tan(Bdouble x){
return sin(x) / cos(x);
}
Bdouble atan_2(Bdouble x){
Bdouble _2 = x * x;
Bdouble _3 = _2 * x;
Bdouble _4 = _3 * x;
Bdouble m(3), s(4), curr = x * 3 - _3,
p = (x - _3) * 4, ans(0);
while(1){
Bdouble q = curr / m;
if(q.checkZero()) break;
ans = ans + q;
curr = curr + p;
p = p * _4;
curr = curr * _4;
m = m + s * 4 + 16;
s = s + 8;
}
return ans;
}
Bdouble atan_1(Bdouble x){
static Bdouble atan01 = atan_2(0.1), _01 = Bdouble(0.1);
if(x > 0.1)
return atan_2((x - _01) / (x * _01 + 1)) + atan01;
return atan_2(x);
}
Bdouble PI = atan_1(Bdouble(1)) * 4;
Bdouble atan(Bdouble x){
if(x.isNeg) return -atan(-x);
static Bdouble _1 = Bdouble(1);
if(x > 1)
return atan_1((x - _1) / (x * _1 + _1)) + (PI / 4);
return atan_1(x);
}
Bdouble asin(Bdouble x){
return atan(x / sqrt(Bdouble(1) - x * x));
}
Bdouble acos(Bdouble x){
return PI / 2 - asin(x);
}
Bdouble atan2(Bdouble y, Bdouble x){
if(x.checkZero()){
if(y.isNeg) return -(PI / 2);
return PI / 2;
}
if(!x.isNeg) return atan(y / x);
if(y.isNeg) return atan(y / x) - PI;
return atan(y / x) + PI;
}
string op;
inline int readint(){
int a; cin >> a; return a;
}
int main(){
Bdouble A, B;
cin.tie(0);
while(cin >> op){
if(op == "add"){A.read();B.read();(A+B).print(readint());}
else if(op == "sub"){A.read();B.read();(A-B).print(readint());}
else if(op == "mul"){A.read();B.read();(A*B).print(readint());}
else if(op == "div"){A.read();B.read();(A/B).print(readint());}
else if(op == "pow"){A.read();B.read();exp(B*ln(A)).print(readint());}
else if(op == "atan2"){A.read();B.read();atan2(A,B).print(readint());}
else if(op == "exp"){A.read();exp(A).print(readint());}
else if(op == "ln"){A.read();ln(A).print(readint());}
else if(op == "sqrt"){A.read();sqrt(A).print(readint());}
else if(op == "asin"){A.read();asin(A).print(readint());}
else if(op == "acos"){A.read();acos(A).print(readint());}
else if(op == "atan"){A.read();atan(A).print(readint());}
else if(op == "sin"){A.read();sin(A).print(readint());}
else if(op == "cos"){A.read();cos(A).print(readint());}
else if(op == "tan"){A.read();tan(A).print(readint());}
else break;
}
return 0;
}剪贴板
剪贴板 1f1y1exh
剪贴板信息
剪贴板信息
- 收录
- 更新