Gaotianjia @ 2023-04-06 03:19:17
// 长整数的奇怪实现.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<string>
#include<cmath>
using namespace std;
string s;
string z;
int n;
bool show0 = false;
void out(long long& a) {
string s = to_string(a);
int k = s.length();
string p = "";
for (int j = 0; j < 16 - k; j++) {
p += "0";
}
s = p + s;
cout << s;
}
class num_1 {
public:
static long long H;
static long long E;
long long high;
long long low;
long long exceed;
num_1() {
high = low = 0;
exceed = 0;
}
void out() {
::out(high);
::out(low);
}
void out1() {
if (!show0)
{
if (high == 0) {
cout << low;
}
else {
cout << high;
show0 = true;
::out(low);
}
}
else
{
out();
}
}
num_1 operator+(num_1& b) {
num_1 ans = *this;
ans = ans + b.low;
ans.high += b.high;
return ans;
}
num_1 operator+(long long& b) {
num_1 ans = *this;
ans.low += b;
if (ans.low > H) {
ans.low -=(H + 1);
ans.high += 1;
}
return ans;
}
bool operator==(num_1& n) {
return(high == n.high && low == n.low);
}
bool operator>(num_1& n) {
if (high > n.high) {
return true;
}
else if (high == n.high) {
if (low > n.low) {
return true;
}
}
return false;
}
bool operator<(num_1& n) {
if (high < n.high) {
return true;
}
else if (high == n.high) {
if (low < n.low) {
return true;
}
}
return false;
}
void clearhigh() {
high -= (H + 1);
}
static num_1 mul(long long& a, long long& b) {
long long p1 = a % 100000000;
long long p2 = a / 100000000;
long long p3 = b % 100000000;
long long p4 = b / 100000000;
long long q1 = p1 * p3;
long long q2 = p2 * p3;
long long q3 = p4 * p1;
long long q4 = p4 * p2;
num_1 ans;
ans.low = q1 + (q2 % 100000000) * 100000000 + (q3 % 100000000) * 100000000;
ans.high = q4 + q2 / 100000000 + q3 / 100000000;
if (ans.low > 9999999999999999) {
ans.high += ans.low / 10000000000000000;
ans.low %= 10000000000000000;
}
return ans;
}
void in(string& s,int a) {
low = high = 0;
long long temp = 1;
int i = a;
for (; i < (a + 16); i++) {
if (i >= ::n) {
return;
}
else
{
low = low + temp * (s[i] - '0');
temp *= 10;
}
}
temp = 1;
for (; i < (a + 32); i++) {
if (i >= ::n) {
break;
}
else
{
high = high + temp * (s[i] - '0');
temp *= 10;
}
}
}
};
long long num_1::H;
long long num_1::E;
class num_2 {
public:
num_1 high;
num_1 low;
static num_1 H;
static num_1 E;
num_2() {
high = low = num_1();
}
void out() {
high.out();
low.out();
}
void out1() {
num_1 t;
if (!show0)
{
if (high == t) {
low.out1();
}
else {
high.out1();
show0 = true;
low.out();
}
}
else
{
out();
}
}
num_2 static mul(num_1& m, num_1& n) {
num_1 q1 = num_1::mul(m.low, n.low);
num_1 q2 = num_1::mul(m.high, n.low);
num_1 q3 = num_1::mul(n.high, m.low);
num_1 q4 = num_1::mul(m.high, n.high);
num_2 ans;
ans.low = q1;
num_1 temp = q2 + q3;
temp = temp + q1.high;
ans.low.high = temp.low;
ans.high.low = temp.high;
if (ans.high.low > num_1::H) {
ans.high.low -= (num_1::H + 1);
ans.high.high += 1;
}
ans.high = ans.high + q4;
return ans;
}
num_2 operator+(num_1& b) {
num_2 ans = *this;
ans.low = ans.low + b;
if (ans.low > H ) {
ans.low.clearhigh();
long long t = 1;
ans.high = ans.high + t;
}
return ans;
}
num_2 operator+(num_2& b) {
num_2 ans = *this;
ans = ans + b.low;
ans.high = ans.high + b.high;
return ans;
}
bool operator==(num_2& n) {
return(high == n.high && low == n.low);
}
bool operator>(num_2& n) {
if (high > n.high) {
return true;
}
else if (high == n.high) {
if (low > n.low) {
return true;
}
}
return false;
}
bool operator<(num_2& n) {
if (high < n.high) {
return true;
}
else if (high == n.high) {
if (low < n.low) {
return true;
}
}
return false;
}
void clearhigh() {
high.clearhigh();
}
void in(string& s,int a) {
high.in(s, a + 32);
low.in(s, a);
}
};
num_1 num_2::H;
num_1 num_2::E;
class num_3 {
public:
num_2 high;
num_2 low;
static num_2 H;
static num_2 E;
num_3() {
high = low = num_2();
}
void out() {
high.out();
low.out();
}
void out1() {
num_2 t;
if (!show0)
{
if (high == t) {
low.out1();
}
else {
high.out1();
show0 = true;
low.out();
}
}
else
{
out();
}
}
num_3 static mul(num_2& m, num_2& n) {
num_2 q1 = num_2::mul(m.low, n.low);
num_2 q2 = num_2::mul(m.high, n.low);
num_2 q3 = num_2::mul(n.high, m.low);
num_2 q4 = num_2::mul(m.high, n.high);
num_3 ans;
ans.low = q1;
num_2 temp = q2 + q3;
temp = temp + q1.high;
ans.low.high = temp.low;
ans.high.low = temp.high;
if (ans.high.low > num_2::H) {
ans.high.low.clearhigh();
ans.high.high = ans.high.high + num_2::E;
}
ans.high = ans.high + q4;
return ans;
}
num_3 operator+(num_2& b) {
num_3 ans = *this;
ans.low = ans.low + b;
if (ans.low > H) {
ans.low.clearhigh();
ans.high = ans.high + num_2::E;
}
return ans;
}
num_3 operator+(num_3& b) {
num_3 ans = *this;
ans = ans + b.low;
ans.high = ans.high + b.high;
return ans;
}
bool operator==(num_3& n) {
return(high == n.high && low == n.low);
}
bool operator>(num_3& n) {
if (high > n.high) {
return true;
}
else if (high == n.high) {
if (low > n.low) {
return true;
}
}
return false;
}
bool operator<(num_3& n) {
if (high < n.high) {
return true;
}
else if (high == n.high) {
if (low < n.low) {
return true;
}
}
return false;
}
void clearhigh() {
high.clearhigh();
}
void in(string& s, int a) {
high.in(s, a + 64);
low.in(s, a);
}
};
num_2 num_3::H;
num_2 num_3::E;
class num_4 {
public:
num_3 high;
num_3 low;
static num_3 H;
static num_3 E;
num_4() {
high = low = num_3();
}
void out() {
high.out();
low.out();
}
void out1() {
num_3 t;
if (!show0)
{
if (high == t) {
low.out1();
}
else {
high.out1();
show0 = true;
low.out();
}
}
else
{
out();
}
}
num_4 static mul(num_3& m, num_3& n) {
num_3 q1 = num_3::mul(m.low, n.low);
num_3 q2 = num_3::mul(m.high, n.low);
num_3 q3 = num_3::mul(n.high, m.low);
num_3 q4 = num_3::mul(m.high, n.high);
num_4 ans;
ans.low = q1;
num_3 temp = q2 + q3;
temp = temp + q1.high;
ans.low.high = temp.low;
ans.high.low = temp.high;
if (ans.high.low > num_3::H) {
ans.high.low.clearhigh();
ans.high.high = ans.high.high + num_3::E;
}
ans.high = ans.high + q4;
return ans;
}
num_4 operator+(num_3& b) {
num_4 ans = *this;
ans.low = ans.low + b;
if (ans.low > H) {
ans.low.clearhigh();
ans.high = ans.high + num_3::E;
}
return ans;
}
num_4 operator+(num_4& b) {
num_4 ans = *this;
ans = ans + b.low;
ans.high = ans.high + b.high;
return ans;
}
bool operator==(num_4& n) {
return(high == n.high && low == n.low);
}
bool operator>(num_4& n) {
if (high > n.high) {
return true;
}
else if (high == n.high) {
if (low > n.low) {
return true;
}
}
return false;
}
bool operator<(num_4& n) {
if (high < n.high) {
return true;
}
else if (high == n.high) {
if (low < n.low) {
return true;
}
}
return false;
}
void clearhigh() {
high.clearhigh();
}
void in(string& s, int a) {
high.in(s, a + 128);
low.in(s, a);
}
};
num_3 num_4::H;
num_3 num_4::E;
class num_5 {
public:
num_4 high;
num_4 low;
static num_4 H;
static num_4 E;
num_5() {
high = low = num_4();
}
void out() {
high.out();
low.out();
}
void out1() {
num_4 t;
if (!show0)
{
if (high == t) {
low.out1();
}
else {
high.out1();
show0 = true;
low.out();
}
}
else
{
out();
}
}
num_5 static mul(num_4& m, num_4& n) {
num_4 q1 = num_4::mul(m.low, n.low);
num_4 q2 = num_4::mul(m.high, n.low);
num_4 q3 = num_4::mul(n.high, m.low);
num_4 q4 = num_4::mul(m.high, n.high);
num_5 ans;
ans.low = q1;
num_4 temp = q2 + q3;
temp = temp + q1.high;
ans.low.high = temp.low;
ans.high.low = temp.high;
if (ans.high.low > num_4::H) {
ans.high.low.clearhigh();
ans.high.high = ans.high.high + num_4::E;
}
ans.high = ans.high + q4;
return ans;
}
num_5 operator+(num_4& b) {
num_5 ans = *this;
ans.low = ans.low + b;
if (ans.low > H) {
ans.low.clearhigh();
ans.high = ans.high + num_4::E;
}
return ans;
}
num_5 operator+(num_5& b) {
num_5 ans = *this;
ans = ans + b.low;
ans.high = ans.high + b.high;
return ans;
}
bool operator==(num_5& n) {
return(high == n.high && low == n.low);
}
bool operator>(num_5& n) {
if (high > n.high) {
return true;
}
else if (high == n.high) {
if (low > n.low) {
return true;
}
}
return false;
}
bool operator<(num_5& n) {
if (high < n.high) {
return true;
}
else if (high == n.high) {
if (low < n.low) {
return true;
}
}
return false;
}
void clearhigh() {
high.clearhigh();
}
void in(string& s, int a) {
high.in(s, a + 256);
low.in(s, a);
}
};
num_4 num_5::H;
num_4 num_5::E;
class num_6 {
public:
num_5 high;
num_5 low;
static num_5 H;
static num_5 E;
num_6() {
high = low = num_5();
}
void out() {
high.out();
low.out();
}
void out1() {
num_5 t;
if (!show0)
{
if (high == t) {
low.out1();
}
else {
high.out1();
show0 = true;
low.out();
}
}
else
{
out();
}
}
num_6 static mul(num_5& m, num_5& n) {
num_5 q1 = num_5::mul(m.low, n.low);
num_5 q2 = num_5::mul(m.high, n.low);
num_5 q3 = num_5::mul(n.high, m.low);
num_5 q4 = num_5::mul(m.high, n.high);
num_6 ans;
ans.low = q1;
num_5 temp = q2 + q3;
temp = temp + q1.high;
ans.low.high = temp.low;
ans.high.low = temp.high;
if (ans.high.low > num_5::H) {
ans.high.low.clearhigh();
ans.high.high = ans.high.high + num_5::E;
}
ans.high = ans.high + q4;
return ans;
}
num_6 operator+(num_5& b) {
num_6 ans = *this;
ans.low = ans.low + b;
if (ans.low > H) {
ans.low.clearhigh();
ans.high = ans.high + num_5::E;
}
return ans;
}
num_6 operator+(num_6& b) {
num_6 ans = *this;
ans = ans + b.low;
ans.high = ans.high + b.high;
return ans;
}
bool operator==(num_6& n) {
return(high == n.high && low == n.low);
}
bool operator>(num_6& n) {
if (high > n.high) {
return true;
}
else if (high == n.high) {
if (low > n.low) {
return true;
}
}
return false;
}
bool operator<(num_6& n) {
if (high < n.high) {
return true;
}
else if (high == n.high) {
if (low < n.low) {
return true;
}
}
return false;
}
void clearhigh() {
high.clearhigh();
}
void in(string& s, int a) {
high.in(s, a + 512);
low.in(s, a);
}
};
num_5 num_6::H;
num_5 num_6::E;
class num_7 {
public:
num_6 high;
num_6 low;
static num_6 H;
static num_6 E;
num_7() {
high = low = num_6();
}
void out() {
high.out();
low.out();
}
void out1() {
num_6 t;
if (!show0)
{
if (high == t) {
low.out1();
}
else {
high.out1();
show0 = true;
low.out();
}
}
else
{
out();
}
}
num_7 static mul(num_6& m, num_6& n) {
num_6 q1 = num_6::mul(m.low, n.low);
num_6 q2 = num_6::mul(m.high, n.low);
num_6 q3 = num_6::mul(n.high, m.low);
num_6 q4 = num_6::mul(m.high, n.high);
num_7 ans;
ans.low = q1;
num_6 temp = q2 + q3;
temp = temp + q1.high;
ans.low.high = temp.low;
ans.high.low = temp.high;
if (ans.high.low > num_6::H) {
ans.high.low.clearhigh();
ans.high.high = ans.high.high + num_6::E;
}
ans.high = ans.high + q4;
return ans;
}
num_7 operator+(num_6& b) {
num_7 ans = *this;
ans.low = ans.low + b;
if (ans.low > H) {
ans.low.clearhigh();
ans.high = ans.high + num_6::E;
}
return ans;
}
num_7 operator+(num_7& b) {
num_7 ans = *this;
ans = ans + b.low;
ans.high = ans.high + b.high;
return ans;
}
bool operator==(num_7& n) {
return(high == n.high && low == n.low);
}
bool operator>(num_7& n) {
if (high > n.high) {
return true;
}
else if (high == n.high) {
if (low < n.low) {
return true;
}
}
return false;
}
bool operator<(num_7& n) {
if (high < n.high) {
return true;
}
else if (high == n.high) {
if (low > n.low) {
return true;
}
}
return false;
}
void clearhigh() {
high.clearhigh();
}
void in(string& s, int a) {
high.in(s, a + 1024);
low.in(s, a);
}
};
num_6 num_7::H;
num_6 num_7::E;
class num_8 {
public:
num_7 high;
num_7 low;
static num_7 H;
static num_7 E;
num_8() {
high = low = num_7();
}
void out() {
high.out();
low.out();
}
void out1() {
num_7 t;
if (high == t) {
low.out1();
}
else {
high.out1();
show0 = true;
low.out();
}
}
num_8 static mul(num_7& m, num_7& n) {
num_7 q1 = num_7::mul(m.low, n.low);
num_7 q2 = num_7::mul(m.high, n.low);
num_7 q3 = num_7::mul(n.high, m.low);
num_7 q4 = num_7::mul(m.high, n.high);
num_8 ans;
ans.low = q1;
num_7 temp = q2 + q3;
temp = temp + q1.high;
ans.low.high = temp.low;
ans.high.low = temp.high;
if (ans.high.low > num_7::H) {
ans.high.low.clearhigh();
ans.high.high = ans.high.high + num_7::E;
}
ans.high = ans.high + q4;
return ans;
}
num_8 operator+(num_7& b) {
num_8 ans = *this;
ans.low = ans.low + b;
if (ans.low > H) {
ans.low.clearhigh();
ans.high = ans.high + num_7::E;
}
return ans;
}
num_8 operator+(num_8& b) {
num_8 ans = *this;
ans = ans + b.low;
ans.high = ans.high + b.high;
return ans;
}
bool operator==(num_8& n) {
return(high == n.high && low == n.low);
}
bool operator>(num_8& n) {
if (high > n.high) {
return true;
}
else if (high == n.high) {
if (low > n.low) {
return true;
}
}
return false;
}
bool operator<(num_8& n) {
if (high < n.high) {
return true;
}
else if (high == n.high) {
if (low < n.low) {
return true;
}
}
return false;
}
void clearhigh() {
high.clearhigh();
}
void in(string& s, int a) {
high.in(s, a + 1024);
low.in(s, a);
}
};
num_7 num_8::H;
num_7 num_8::E;
void ini() {
num_1::H = 9999999999999999;
num_1::E = 1;
num_2::H.high = num_2::H.low = num_1::H;
num_2::E.low = 1;
num_3::H.high = num_3::H.low = num_2::H;
num_3::E.low = num_2::E;
num_4::H.high = num_4::H.low = num_3::H;
num_4::E.low = num_3::E;
num_5::H.high = num_5::H.low = num_4::H;
num_5::E.low = num_4::E;
num_6::H.high = num_6::H.low = num_5::H;
num_6::E.low = num_5::E;
num_7::H.high = num_7::H.low = num_6::H;
num_7::E.low = num_6::E;
num_8::H.high = num_8::H.low = num_7::H;
num_8::E.low = num_7::E;
}
template<int N>
class N_NUM{
public:
N_NUM<N - 1> high;
N_NUM<N - 1> low;
N_NUM<N-1> H;
N_NUM<N-1> E;
N_NUM() {
if (N == 1) {
high = low = N_NUM<N - 1>();
H.high = high.H;
H.low = high.H;
E.low = low.E;
}
else
{
high = low = N_NUM<N - 1>();
H.high = high.H;
H.low = high.H;
E.low = low.E;
}
}
void out() {
high.out();
low.out();
}
void out1() {
N_NUM<N-1> t;
if (high == t) {
low.out1();
}
else {
high.out1();
show0 = true;
low.out();
}
}
N_NUM static mul(N_NUM<N-1>& m, N_NUM<N-1>& n) {
N_NUM<N - 1> q1 = N_NUM<N - 1>::mul(m.low, n.low);
N_NUM<N - 1> q2 = N_NUM<N - 1>::mul(m.high, n.low);
N_NUM<N - 1> q3 = N_NUM<N - 1>::mul(n.high, m.low);
N_NUM<N - 1> q4 = N_NUM<N - 1>::mul(m.high, n.high);
N_NUM ans;
ans.low = q1;
N_NUM<N - 1> temp = q2 + q3;
temp = temp + q1.high;
ans.low.high = temp.low;
ans.high.low = temp.high;
if (ans.high.low > m.H) {
ans.high.low.clearhigh();
ans.high.high = ans.high.high + m.E;
}
ans.high = ans.high + q4;
return ans;
}
N_NUM operator+(N_NUM<N - 1>& b) {
N_NUM ans = *this;
ans.low = ans.low + b;
if (ans.low > H) {
ans.low.clearhigh();
ans.high = ans.high + b.E;
}
return ans;
}
N_NUM operator+(N_NUM& b) {
N_NUM ans = *this;
ans = ans + b.low;
ans.high = ans.high + b.high;
return ans;
}
bool operator==(N_NUM& n) {
return(high == n.high && low == n.low);
}
bool operator>(N_NUM& n) {
if (high > n.high) {
return true;
}
else if (high == n.high) {
if (low > n.low) {
return true;
}
}
return false;
}
bool operator<(N_NUM& n) {
if (high < n.high) {
return true;
}
else if (high == n.high) {
if (low < n.low) {
return true;
}
}
return false;
}
void clearhigh() {
high.clearhigh();
}
void in(string& s, int a) {
high.in(s, a + pow(2,N+6));
low.in(s, a);
}
};
template<>
class N_NUM<0> :public num_3{
public:
N_NUM(num_3& N) {
num_3::low = N.low;
num_3::high = N.high;
}
N_NUM() {
num_3 N;
num_3::low = N.low;
num_3::high = N.high;
}
N_NUM static mul(num_2& m, num_2& n) {
num_2 q1 = num_2::mul(m.low, n.low);
num_2 q2 = num_2::mul(m.high, n.low);
num_2 q3 = num_2::mul(n.high, m.low);
num_2 q4 = num_2::mul(m.high, n.high);
num_3 ans;
ans.low = q1;
num_2 temp = q2 + q3;
temp = temp + q1.high;
ans.low.high = temp.low;
ans.high.low = temp.high;
if (ans.high.low > m.H) {
ans.high.low.clearhigh();
ans.high.high = ans.high.high + m.E;
}
ans.high = ans.high + q4;
return ans;
}
N_NUM operator+(num_3& b) {
num_3 ans = *this;
ans = ans + b.low;
ans.high = ans.high + b.high;
return ans;
}
N_NUM operator+(num_2& b) {
num_3 ans = *this;
ans.low = ans.low + b;
if (ans.low > H) {
ans.low.clearhigh();
ans.high = ans.high + num_2::E;
}
return ans;
}
};
int main()
{
ini();
cin >> z;
string s = z;
n = s.length();
for (int i = 0; i < n; i++) {
z[i] = s[n -1 - i];
}
N_NUM<6> p;
p.in(z, 0);
cin >> z;
s = z;
n = s.length();
for (int i = 0; i < n; i++) {
z[i] = s[n -1 - i];
}
N_NUM<6> q;
q.in(z, 0);
N_NUM<7> ans;
N_NUM<4> i;
i = i + i.E;
ans = N_NUM<7>::mul(q, p);
ans.out1();
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
by BBDD @ 2023-04-06 06:22:01
您是小学生吗,典型的以自我为中心,有啥都要炫耀,最烦这种。您自己去看洛谷新人必读吧。
by a1co0av5ce5az1cz0ap_ @ 2023-04-06 06:47:53
怎么写这么长,连位运算都没有,jbl
by Fading_Happiness @ 2023-04-06 07:23:16
tlqtj, jbl
by Caiest_Oier @ 2023-04-06 07:57:22
@Gaotianjia 极其差劲的封装
by FFTotoro @ 2023-04-06 08:26:40
tlqtj,jbl
by anonymous_letter @ 2023-04-06 08:31:19
@Gaotianjia 写的很好,jbl
by cyhwzoi @ 2023-05-08 22:03:08
6 一千多行 jbl
by Zjh_abcdbt_rnfmabj @ 2023-07-04 13:43:47
牛