题解
2024-08-03 14:15:35
发布于:湖南
30阅读
0回复
0点赞
还行
无脑复制模板
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char A[100005], B[100005];
struct node{
char a[100005];
int len = 0, f = 1;
node(){
memset(a, 0, sizeof(a));
}
void __init(char *A){
len = strlen(A);
for(int i = 0; i < len; i++){
a[len - i] = A[i] - '0';
}
while(a[len] == -3) a[len--] = 0, f = -f;//处理负号,用while是为了防止有些人重复输-卡bug
}
void __init(int n){//将整型数据转为高精度数
while(n){
a[++len] = n % 10;
n /= 10;
}
}
void print(){
if(f == -1) putchar('-');
for(int i = len; i >= 1; i--) putchar(a[i] + '0');//倒序输出
}
void print_f(){
cout << (f == -1 ? '-' : '\0') << int(a[len]) << '.';
for(int i = len - 1, j = 1; i >= 1 && j <= 6; i--, j++) putchar(a[i] + '0');
if(len == 1) cout << 0;
printf("*10^%d\n", len - 1);
}
int turn_int(){
int tmp = 0;
for(int i = len; i >= 1; i--){
tmp = tmp * 10 + a[i];
}
return tmp * f;
}
node _cpy(const node a, int dst) const{//除法的靠右对齐函数
node tmp;
tmp.f = a.f, tmp.len = a.len + dst - 1;
for(int i = 1; i <= a.len; i++){
tmp.a[i + dst - 1] = a.a[i];
}return tmp;
}
bool operator > (const node &b) const{
if(f == 1 && b.f == -1) return 1;
if(f == -1 && b.f == 1) return 0;
if(f == -1 && b.f == -1){
node a1 = *this, b1 = b;
a1.f = b1.f = 1;
return !(a1 > b1);
}
if(len > b.len) return 1;//比较长度
if(len < b.len) return 0;
for(int i = len; i >= 1; i--){
if(a[i] > b.a[i]) return 1;//逐位比较
if(a[i] < b.a[i]) return 0;
}return 0;
}
bool operator < (const node &b) const{
if(f == 1 && b.f == -1) return 0;
if(f == -1 && b.f == 1) return 1;
if(f == -1 && b.f == -1){
node a1 = *this, b1 = b;
a1.f = b1.f = 1;
return !(a1 < b1);
}
if(len > b.len) return 0;//比较长度
if(len < b.len) return 1;
for(int i = len; i >= 1; i--){
if(a[i] > b.a[i]) return 0;//逐位比较
if(a[i] < b.a[i]) return 1;
}return 0;
}
bool operator <= (const node &b) const{
return !(*this > b);
}
bool operator >= (const node &b) const{
return !(*this < b);
}
node operator + (const node &b) const{//加法
node c;
c.len = max(len, b.len);
node a1 = *this, b1 = b;
if(a1.f == 1 && b1.f == -1){
b1.f = 1;
return a1 - b1;
}
if(a1.f == -1 && b1.f == 1){
a1.f = 1;
return b1 - a1;
}
if(a1.f == -1 && b1.f == -1){
a1.f = b1.f = 1;
c.f = -c.f;
}
for(int i = 1; i <= c.len; i++){
c.a[i] += a[i] + b.a[i];
if(c.a[i] >= 10) c.a[i + 1]++, c.a[i] -= 10;//进位
}if(c.a[c.len + 1]) c.len++;
while(c.a[c.len] == 0 && c.len > 1) c.len--;
return c;
}
node operator - (const node &b) const{//减法
node c;
node a1 = *this, b1 = b;
if(a1.f == 1 && b1.f == -1){
b1.f = 1;
return a1 + b1;
}
if(a1.f == -1 && b1.f == 1){
a1.f = 1;
c = a1 + b1;
c.f = -c.f;
return c;
}
if(a1.f == -1 && b1.f == -1){
a1.f = b1.f = 1;
c.f = -c.f;
}
if(b1 > a1){
swap(a1, b1);
c.f = -c.f;
}
c.len = len;
for(int i = 1; i <= a1.len; i++){
c.a[i] += a1.a[i] - b1.a[i];
if(c.a[i] < 0) c.a[i + 1]--, c.a[i] += 10;//借位
}while(c.a[c.len] == 0 && c.len > 1) c.len--;
return c;
}
node operator * (const node &b) const{//乘法
node c;
c.len = len + b.len;
if(f + b.f == 0){
c.f = -c.f;
}
for(int i = 1; i <= len; i++){
int x = 0;
for(int j = 1; j <= b.len; j++){
int weishu = i + j - 1;
c.a[weishu] += x + a[i] * b.a[j];
x = c.a[weishu] / 10;
c.a[weishu] %= 10;
}c.a[i + b.len] = x;
}while(c.a[c.len] == 0 && c.len > 1) c.len--;
return c;
}
node operator / (const node &b) const{//除法(难度突增!)
node c;
c.len = 1;
node a1 = *this, b1 = b;
if(a1.f == 1 && b1.f == -1){
b1.f = 1;
c.f = -c.f;
}
if(a1.f == -1 && b1.f == 1){
a1.f = 1;
c.f = -c.f;
}
if(a1.f == -1 && b1.f == -1){
a1.f = b1.f = 1;
}
if(a1 < b1) return c;
c.len = a1.len - b1.len + 1;
for(int i = c.len; i >= 1; i--){
node tmp = _cpy(b1, i);//右对齐
while(a1 >= tmp){//不断相减
c.a[i]++;
a1 = a1 - tmp;
}
}while(c.a[c.len] == 0 && c.len > 1) c.len--;
return c;
}
node operator % (const node &b) const{//取余
node a1 = *this, b1 = b;
b1.f = 1;
if(a1.f == -1){
a1.f = 1;
return b1 - (a1 % b1);
}
if(a1 < b1) return a1;
int len = a1.len - b1.len + 1;
for(int i = len; i >= 1; i--){
node tmp = _cpy(b1, i);//右对齐
while(a1 >= tmp){//不断相减
a1 = a1 - tmp;
}
}while(a1.a[a1.len] == 0 && a1.len > 1) a1.len--;
return a1;
}
node operator ^ (const int b) const{//乘方
//范围:-2*10^9~2*10^9
node null;
null.f = -1;
if(b < 0) return null;//你输入的是什么
node tmp;
tmp.len = tmp.a[1] = 1;
node a1 = *this;
int t = b;
while(t){
if(t & 1){
if(a1.len + tmp.len > 1e5) return null;//太大了
tmp = tmp * a1;//乘
}
if(a1.len + a1.len > 1e5) return null;
a1 = a1 * a1, t >>= 1;//自乘
}return tmp;
}
void operator += (node &b){
*this = *this + b;
}
void operator -= (node &b){
*this = *this - b;
}
void operator *= (node &b){
*this = *this * b;
}
void operator /= (node &b){
*this = *this / b;
}
void operator %= (node &b){
*this = *this % b;
}
void operator ^= (int b){
*this = *this ^ b;
}
};
int main(){
node _2, ans, _1;
_2.__init(2), _1.__init(1);
int n;
cin >> n;
ans = (_2 ^ n) - _1;
ans.print();
return 0;
}
这里空空如也
有帮助,赞一个