无敌的题解
2026-04-17 20:51:18
发布于:浙江
12阅读
0回复
0点赞
本题解配备了2种600行以上的解法,废话不多说,代码来临:
解法一:(611行)
#include <cstdio>
#include <cstring>
#include <cstdint>
#include <cstdarg>
#include <immintrin.h>
#pragma GCC optimize("Ofast", "unroll-loops", "no-stack-protector", "fast-math")
#pragma GCC target("avx2", "avx512f", "avx512vl", "fma", "bmi2", "popcnt")
#pragma GCC diagnostic ignored "-Wunused-function"
typedef long long ll;
typedef double lf;
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define elif else if
#define swap_d(a, b) do { ll _t = (a); (a) = (b); (b) = _t; } while(0)
#define swap_f(a, b) do { lf _t = (a); (a) = (b); (b) = _t; } while(0)
#define swap_c(a, b) do { char _t = (a); (a) = (b); (b) = _t; } while(0)
#define memcpy(r1, s, len) __builtin_memcpy((r1), (s), (len))
#define strlen(r1) __builtin_strlen((r1))
#define memcmp(s1, s2, n) __builtin_memcmp((s1), (s2), (n))
static const lf pi = 3.14159265358979323846;
static const lf e = 2.71828182845904523536;
static char _rbuf[1 << 16];
static char* _rptr = _rbuf;
static char* _rend = _rbuf;
// 获取下一个字符(带缓冲)
static inline char _gc() {
if (unlikely(_rptr == _rend)) { // 缓冲区耗尽是小概率事件
_rend = _rbuf + fread(_rbuf, 1, sizeof(_rbuf), stdin);
_rptr = _rbuf;
if (unlikely(_rptr == _rend)) return EOF; // 文件结束是极小概率事件
}
return *_rptr++;
}
static char _wbuf[1 << 16];
static char* _wptr = _wbuf;
// 输出单个字符(带缓冲)
static inline void _pc(char c) {
if (unlikely(_wptr == _wbuf + sizeof(_wbuf))) { // 缓冲区满是小概率事件
fwrite(_wbuf, 1, sizeof(_wbuf), stdout);
_wptr = _wbuf;
}
*_wptr++ = c;
}
// 刷新输出缓冲区
static inline void _return() {
if (likely(_wptr != _wbuf)) { // 通常为 True
fwrite(_wbuf, 1, _wptr - _wbuf, stdout);
_wptr = _wbuf;
}
}
namespace std{
struct string {
char r1[1001];
// 初始化空字符串
string() { r1[0] = '\0'; }
// 初始化为单字符
string(char c) { r1[0] = c; r1[1] = '\0'; }
// 从C字符串构造
string(const char* s) {
if (unlikely(!s)) { r1[0] = '\0'; return; }
size_t len = strlen(s);
if (len > 1000) len = 1000;
memcpy(r1, s, len);
r1[len] = '\0';
}
// 拷贝构造函数
string(const string& other) {
size_t len = strlen(other.r1);
if (len > 1000) len = 1000;
memcpy(r1, other.r1, len);
r1[len] = '\0';
}
// 赋值操作符重载
string& operator=(const string& other) {
if (this != &other) {
size_t len = strlen(other.r1);
if (len > 1000) len = 1000;
memcpy(r1, other.r1, len);
r1[len] = '\0';
}
return *this;
}
// 下标访问操作符重载(可写)
inline char& operator[](int i) { return r1[i]; }
// 下标访问操作符重载(只读)
inline const char& operator[](int i) const { return r1[i]; }
// 字符串相等判断操作符重载
bool operator==(const string& other) const {
size_t l1 = strlen(r1);
size_t l2 = strlen(other.r1);
if (l1 != l2) return false;
return memcmp(r1, other.r1, l1) == 0;
}
// 字符串不等判断操作符重载
bool operator!=(const string& other) const { return !(*this == other); }
// 字符串拼接操作符重载(返回新字符串)
string operator+(const string& other) const {
string res;
size_t l1 = strlen(r1);
size_t l2 = strlen(other.r1);
size_t total = l1 + l2;
if (total > 1000) total = 1000;
if (l1 > 0) memcpy(res.r1, r1, l1);
if (total > l1 && l2 > 0) {
size_t copy_len = (total - l1 < l2) ? (total - l1) : l2;
memcpy(res.r1 + l1, other.r1, copy_len);
}
res.r1[total] = '\0';
return res;
}
// 原地拼接字符串操作符重载(string)
string& operator+=(const string& other) {
size_t l1 = strlen(r1);
size_t l2 = strlen(other.r1);
if (l1 + l2 >= 1000) {
size_t space = 1000 - l1;
if (space > 0) memcpy(r1 + l1, other.r1, space);
r1[1000] = '\0';
} else {
memcpy(r1 + l1, other.r1, l2);
r1[l1 + l2] = '\0';
}
return *this;
}
// 原地拼接C字符串操作符重载
string& operator+=(const char* s) {
size_t l1 = strlen(r1);
size_t l2 = strlen(s);
if (l1 + l2 >= 1000) {
size_t space = 1000 - l1;
if (space > 0) memcpy(r1 + l1, s, space);
r1[1000] = '\0';
} else {
memcpy(r1 + l1, s, l2);
r1[l1 + l2] = '\0';
}
return *this;
}
// 原地拼接字符操作符重载
string& operator+=(char c) {
size_t len = strlen(r1);
if (likely(len < 1000)) {
r1[len] = c;
r1[len + 1] = '\0';
}
return *this;
}
// 获取字符串长度
inline int size() const { return (int)strlen(r1); }
// 判断字符串是否为空
inline bool empty() const { return r1[0] == '\0'; }
// 获取C风格字符串指针
inline const char* c_str() const { return r1; }
// 清空字符串
inline void clear() { r1[0] = '\0'; }
// 尾部追加字符
inline void push_back(char c) {
size_t len = strlen(r1);
if (likely(len < 1000)) {
r1[len] = c;
r1[len + 1] = '\0';
}
}
// 获取子串
string substr(int pos, int len = -1) const {
string res;
int sz = (int)strlen(r1);
if (pos < 0) pos = 0;
if (pos >= sz) return res;
if (len < 0 || pos + len > sz) len = sz - pos;
memcpy(res.r1, r1 + pos, (size_t)len);
res.r1[len] = '\0';
return res;
}
// 查找子串位置
int find(const string& sub) const {
if (sub.empty()) return 0;
const char* p = strstr(r1, sub.r1);
return p ? (int)(p - r1) : -1;
}
};
// C字符串 + 自定义字符串
inline string operator+(const char* s, const string& str) {
string res(s);
res += str;
return res;
}
// 字符 + 自定义字符串
inline string operator+(char c, const string& str) {
string res(c);
res += str;
return res;
}
// 字符串快读 (跳过空白)
inline string read_s() {
string a;
char c = _gc();
while (c <= 32) {
if (c == EOF) return a;
c = _gc();
}
int cnt = 0;
while (c > 32 && cnt < 1000) {
a.r1[cnt++] = c;
c = _gc();
}
a.r1[cnt] = '\0';
return a;
}
// 读取整行 (不含换行符)
inline void getline(string& s) {
char c = _gc();
int cnt = 0;
while (cnt < 1000) {
if (c == '\n' || c == '\r' || c == EOF) break;
s.r1[cnt++] = c;
c = _gc();
}
s.r1[cnt] = '\0';
}
// 字符串快写
inline void write_s(const string& a) {
const char* p = a.r1;
while (*p) _pc(*p++);
}
// 字符串反转
inline void reverse(string& s) {
int len = s.size();
int half = len >> 1;
char* start = s.r1;
char* end = s.r1 + len - 1;
for (int i = 0; i < half; ++i) {
swap_c(*start, *end);
start++;
end--;
}
}
}; // string 类型 字符串(带输出输入工具)
using namespace std;
namespace Code{
// 整数快读 (ll)
inline ll read_d() {
ll x = 0;
int f = 1;
char c = _gc();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = _gc();
if (unlikely(c == EOF)) break;
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = _gc();
}
return f == 1 ? x : -x;
}
// 浮点快读 (lf)
inline lf read_f() {
ll ip = 0;
lf f = 1.00;
char c = _gc();
// 1. 跳过前导空白和符号
while (c < '0' || c > '9') {
if (c == '-') f = -1.00;
c = _gc();
if (unlikely(c == EOF)) break;
}
// 2. 读取整数部分
while (c >= '0' && c <= '9') {
ip = (ip << 3) + (ip << 1) + (c ^ 48);
c = _gc(); // 此时 c 已经是小数点或者分隔符(如空格)
}
lf fp = 0.00, base = 0.1;
// 3. 直接使用刚才读到的 c 进行判断,不要重新 _gc()
if (c == '.') {
c = _gc(); // 只有确认是小数点后,才读取下一位小数
while (c >= '0' && c <= '9') {
fp += (c ^ 48) * base;
base *= 0.1;
c = _gc();
}
}
return double(ip + fp) * f;
}
inline void read_w(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char c;
while (*fmt) {
if (*fmt == '%') {
fmt++;
if (!*fmt) break;
if (*fmt == 's') {
string* s = va_arg(args, string*);
s -> clear();
c = _gc();
while (c <= 32) { if (c == EOF) goto end; c = _gc(); }
while (c > 32) {
s->push_back(c);
c = _gc();
if(c == EOF) break;
}
}
else if (*fmt == 'd') {
ll* val = va_arg(args, ll*);
*val = read_d();
}
else if (*fmt == 'c') {
char* val = va_arg(args, char*);
*val = _gc();
}
else if (*fmt == 'l') {
if (*(fmt+1) == 'f') {
fmt++;
lf* val = va_arg(args, lf*);
*val = read_f();
}
}
else if (*fmt == 'f') {
lf temp = read_f();
float* val = va_arg(args, float*);
*val = (float)temp;
}
} else {
c = _gc();
while (c <= 32 && *fmt != '%') c = _gc();
}
fmt++;
}
end:
va_end(args);
}
// 整数快写 (ll)
void write_d(ll x) {
if (x == 0) { _pc('0'); return; }
if (x < 0) { _pc('-'); x = -x; }
char buf[25];
char* ptr = buf + 24;
*ptr = '\0';
while (x > 0) {
*--ptr = '0' + (char)(x % 10);
x /= 10;
}
while (*ptr) _pc(*ptr++);
}
// 浮点快写 (指定位数)
inline void write_p(lf x, int max_decimal) {
if (x == 0.0) { _pc('0'); return; }
if (x < 0) { _pc('-'); x = -x; }
ll int_part = (ll)x;
write_d(int_part);
if (max_decimal <= 0) return;
_pc('.');
lf frac_part = x - (lf)int_part;
for (int i = 0; i < max_decimal; i++) {
frac_part *= 10.0;
ll digit = (ll)frac_part;
_pc('0' + (char)digit);
frac_part -= (lf)digit;
}
}
// 浮点快写 (默认15位精度)
inline void write_f(lf x) { write_p(x, 15); }
// 格式化输出 (类似printf)
void write_w(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
while (*fmt) {
if (*fmt == '%') {
fmt++;
if (!*fmt) break;
ll precision = -1;
if (*fmt == '.' && *(fmt+1) == '*') {
fmt += 2;
precision = va_arg(args, ll);
}
else if (*fmt == '.') {
fmt++;
ll p = 0;
bool has_digit = false;
while(*fmt >= '0' && *fmt <= '9') {
p = p * 10 + (*fmt - '0');
fmt++;
has_digit = true;
}
if (has_digit) precision = p;
}
switch (*fmt) {
case 'd':
case 'i':
write_d((ll)va_arg(args, ll));
break;
case 'l':
if (*(fmt+1) == 'd') {
write_d(va_arg(args, ll));
fmt++;
} else {
write_d((ll)va_arg(args, ll));
}
break;
case 'L':
if (*(fmt+1) == 'd') {
write_d(va_arg(args, ll));
fmt++;
}
break;
case 'f':
case 'F':
case 'g':
case 'G':
if (precision >= 0) {
write_p(va_arg(args, lf), precision);
} else {
write_f(va_arg(args, lf));
}
break;
case 's': {
const char* s = va_arg(args, const char*);
write_s(s);
break;
}
case 'c':
_pc((char)va_arg(args, int));
break;
case '%':
_pc('%');
break;
default:
_pc('%');
_pc(*fmt);
break;
}
} else if (*fmt == '\\') {
fmt++;
if (!*fmt) break;
switch (*fmt) {
case 'n': _pc('\n'); break;
case 't': _pc('\t'); break;
case 'r': _pc('\r'); break;
case '\\': _pc('\\'); break;
case '"': _pc('"'); break;
default:
_pc('\\');
_pc(*fmt);
break;
}
} else {
_pc(*fmt);
}
fmt++;
}
va_end(args);
}
};
using namespace Code; // C语言输出输入工具
namespace math{
// 整数数组求和
ll sum_d(ll a[], int n) {
ll sum = 0;
for (int i = 0; i < n; ++i) sum += a[i];
return sum;
}
// 浮点数组求和
lf sum_f(lf a[], int n) {
lf sum = 0;
for (int i = 0; i < n; ++i) sum += a[i];
return sum;
}
// 浮点乘方 (优化)
lf pow(lf a, lf b) {
ll ll_b = (ll)b;
lf lf_b = b - ll_b;
if (lf_b != 0.00) return __builtin_pow(a, b);
if (a == 1.0 || ll_b == 0) return 1.0;
if (a == 0.0) return 0.0;
if (a == -1.0) return (ll_b & 1) ? -1.0 : 1.0;
bool neg = false;
if (ll_b < 0) {
neg = true;
ll_b = -ll_b;
}
lf res = 1.0;
lf base = a;
while (ll_b > 0) {
if (ll_b & 1) res *= base;
base *= base;
ll_b >>= 1;
}
return neg ? 1.0 / res : res;
}
lf sqrt(lf x, lf n) {if (unlikely(n == 0 || x == 0)) return 0;
if (unlikely(n == 1)) return x;
if (likely(n == 2)) return __builtin_sqrt(x);
if (unlikely(x < 0 && static_cast<long long>(n) % 2 == 0)) {
return 0.0L / 0.0L;
}
// 3. 牛顿迭代法
long double y = x; // 初始猜测值,可以设为 x 或 1.0
long double prev_y;
const long double epsilon = 1e-15L; // 精度控制,当两次迭代结果差值小于此值时停止
do {
prev_y = y;
long double y_power_n_minus_1 = 1.0L;
for (int i = 0; i < static_cast<int>(n) - 1; ++i) {
y_power_n_minus_1 *= prev_y;
}
y = ((n - 1) * prev_y + x / y_power_n_minus_1) / n;
} while (y > prev_y ? y - prev_y > epsilon : prev_y - y > epsilon);
return y;
}
// 最大公约数
inline ll gcd(ll a, ll b) {
while (b) {
a %= b;
swap_d(a, b);
}
return a;
}
// 最小公倍数
inline ll lcm(ll a, ll b) {
if (a == 0 || b == 0) return 0;
return (a / gcd(a, b)) * b;
}
}; // 数学函数库
using namespace math;
// 自定义区域
// 主函数入口
int main() {
int a,b;//定义a,b
cin>>a>>b;//读取输入
cout<<a+b;//输出a+b的和
_return();
}
解法二:(634行)
#include <bits/stdc++.h>
#include<iostream>
#include <algorithm>
#include <math.h>
#include <cmath>
#include<string>
#include<cstring>
#include<list>
#include<map>
#include<queue>
#include<iterator>
#include<stack>
#include<cstdio>
#include <type_traits>
#include <functional>
#include <tuple>
#include <random>
#include <cstdint>
#include <unistd.h>
#include <vector>
#include <sstream>
#include <stdexcept>
#include <memory>
#include <numeric>
#include<list>
#include<map>
#include<queue>
#include<iterator>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <locale>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <shared_mutex>
#include <bit>
#include <version>
#include<assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
using namespace std;
int an[10010],bn[10010],cn[10010];
string s1,s2;
void stoh(string s,int h[]){
h[0]=s.size();
for(int i=1,j=s.size()-1;i<=h[0];i++,j--)h[i]=s[j]-'0';
}
void add(int a[],int b[],int c[]){
c[0]=max(a[0],b[0]);
for(int i=1;i<=c[0];i++){
c[i+1]=(a[i]+b[i]+c[i])/10;
c[i]=(a[i]+b[i]+c[i])%10;
}
if(c[c[0]+1]>0)c[0]++;
}
void print(int x[]){
for(int i=x[0];i>=1;i--)cout<<x[i];
}
void solve1(){
cin>>s1>>s2;
stoh(s1,an),stoh(s2,bn),add(an,bn,cn),print(cn);
}
const double PI=4*atan(1);
template<typename T>
void Swap(T &a,T &b){
T c=a;
a=b;
b=c;
return;
}
template<typename T>
T Max(const T &a,const T &b){
return a<b?b:a;
}
typedef long long ll;
struct comp{
double real,imag;
comp operator+(const comp &x)const{
return {real+x.real,imag+x.imag};
}
comp operator-(const comp &x)const{
return {real-x.real,imag-x.imag};
}
comp operator*(const comp &x)const{
return {real*x.real-imag*x.imag,real*x.imag+x.real*imag};
}
comp operator/(const unsigned &x)const{
return {real/(double)x,imag/(double)x};
}
};
void FFT(comp *f,unsigned n,int rev){
for(unsigned i=1,j=n>>1,k;i<n-1;i++){
if(i<j)
Swap(f[i],f[j]);
k=n>>1;
while(j>=k){
j-=k;
k>>=1;
}
j+=k;
}
for(unsigned l=2;l<=n;l<<=1){
double arg=2*PI*rev/l;
comp wn={cos(arg),sin(arg)};
for(unsigned i=0;i<n;i+=l){
comp w={1,0};
for(unsigned j=0;j<(l>>1);j++){
comp f1=f[i+j];
comp f2=f[i+j+(l>>1)];
f[i+j]=f1+w*f2;
f[i+j+(l>>1)]=f1-w*f2;
w=w*wn;
}
}
}
if(!~rev)
for(unsigned i=0;i<n;i++)
f[i]=f[i]/n;
}
#define BASE 100
template<const unsigned Size>
class bigint{
private:
unsigned len;
int num[Size];
void init(){
memset(num,0,sizeof(num));
len=1;
}
bool abs_greater_equal(const bigint &a)const{
if(len!=a.len)
return len>a.len;
for(int i=len;i;i--)
if(num[i]!=a.num[i])
return num[i]>a.num[i];
return 1;
}
public:
bigint(){
init();
}
void get_num(std::string s){
init();
int f=0;
unsigned slen=s.length();
if(s[0]=='-')
num[0]=f=1;
len=0;
unsigned temp=0,w=1;
for(int i=slen-1;i>=f;i--){
temp+=(s[i]^48)*w;
w=(w<<1)+(w<<3);
if(w==BASE||i==f){
num[++len]=(int)temp;
temp=0;
w=1;
}
}
if(temp||len==0)
num[++len]=temp;
}
bool operator<(const bigint &a)const{
if(num[0]&&!a.num[0])
return 1;
if(!num[0]&&a.num[0])
return 0;
if(num[0]){
if(len!=a.len)
return len>a.len;
for(int i=len;i;i--)
if(num[i]!=a.num[i])
return num[i]>a.num[i];
}
else{
if(len!=a.len)
return len<a.len;
for(int i=len;i;i--)
if(num[i]!=a.num[i])
return num[i]<a.num[i];
}
return 0;
}
bigint operator+(const bigint &a)const{
bigint res;
if(len==1&&num[1]==0){
res=a;
return res;
}
if(a.len==1&&a.num[1]==0){
res=*this;
return res;
}
if(num[0]==a.num[0]){
res.num[0]=num[0];
unsigned len_sum=1;
while(len_sum<len+a.len)
len_sum<<=1;
comp *fa=new comp[len_sum]();
comp *fb=new comp[len_sum]();
for(unsigned i=0;i<len;i++)
fa[i]={(double)num[i+1],0};
for(unsigned i=0;i<a.len;i++)
fb[i]={(double)a.num[i+1],0};
FFT(fa,len_sum,1);
FFT(fb,len_sum,1);
for(unsigned i=0;i<len_sum;i++)
fa[i]=fa[i]+fb[i];
FFT(fa,len_sum,-1);
res.len=Max(len,a.len);
ll temp=0;
for(unsigned i=0;i<res.len;i++){
ll val=(ll)round(fa[i].real)+temp;
res.num[i+1]=(int)(val%BASE);
temp=val/BASE;
}
if(temp)
res.num[++res.len]=temp;
while(res.len>1&&res.num[res.len]==0)
res.len--;
delete[] fa;
delete[] fb;
}
else{
if(abs_greater_equal(a)){
res.num[0]=num[0];
unsigned len_sum=1;
while(len_sum<len+a.len)
len_sum<<=1;
comp *fa=new comp[len_sum]();
comp *fb=new comp[len_sum]();
for(unsigned i=0;i<len;i++)
fa[i]={(double)num[i+1],0};
for(unsigned i=0;i<a.len;i++)
fb[i]={(double)a.num[i+1],0};
FFT(fa,len_sum,1);
FFT(fb,len_sum,1);
for(unsigned i=0;i<len_sum;i++)
fa[i]=fa[i]-fb[i];
FFT(fa,len_sum,-1);
res.len=Max(len,a.len);
ll temp=0;
for(unsigned i=0;i<res.len;i++){
ll val=(ll)round(fa[i].real)+temp;
if(val<0){
val+=BASE;
temp=-1;
}
else
temp=0;
res.num[i+1]=(int)(val%BASE);
}
if(temp)
res.num[++res.len]=temp;
while(res.len>1&&res.num[res.len]==0)
res.len--;
delete[] fa;
delete[] fb;
}
else{
res.num[0]=a.num[0];
unsigned len_sum=1;
while(len_sum<len+a.len)
len_sum<<=1;
comp *fa=new comp[len_sum]();
comp *fb=new comp[len_sum]();
for(unsigned i=0;i<len;i++)
fa[i]={(double)num[i+1],0};
for(unsigned i=0;i<a.len;i++)
fb[i]={(double)a.num[i+1],0};
FFT(fa,len_sum,1);
FFT(fb,len_sum,1);
for(unsigned i=0;i<len_sum;i++)
fa[i]=fb[i]-fa[i];
FFT(fa,len_sum,-1);
res.len=Max(len,a.len);
ll temp=0;
for(unsigned i=0;i<res.len;i++){
ll val=(ll)round(fa[i].real)+temp;
if(val<0){
val+=BASE;
temp=-1;
}
else
temp=0;
res.num[i+1]=(int)(val%BASE);
}
if(temp)
res.num[++res.len]=temp;
while(res.len>1&&res.num[res.len]==0)
res.len--;
delete[] fa;
delete[] fb;
}
if(res.len==1&&res.num[1]==0)
res.num[0]=0;
}
return res;
}
bigint operator*(const bigint &a)const{
bigint res;
if((len==1&&num[1]==0)||(a.len==1&&a.num[1]==0))
return res;
res.num[0]=num[0]^a.num[0];
unsigned len_sum=1;
while(len_sum<len+a.len)
len_sum<<=1;
comp *fa=new comp[len_sum]();
comp *fb=new comp[len_sum]();
for(unsigned i=0;i<len;i++)
fa[i]={(double)num[i+1],0};
for(unsigned i=0;i<a.len;i++)
fb[i]={(double)a.num[i+1],0};
FFT(fa,len_sum,1);
FFT(fb,len_sum,1);
for(unsigned i=0;i<len_sum;i++)
fa[i]=fa[i]*fb[i];
FFT(fa,len_sum,-1);
res.len=len+a.len;
ll temp=0;
for(unsigned i=0;i<res.len;i++){
ll val=(ll)(fa[i].real+0.5)+temp;
res.num[i+1]=(int)(val%BASE);
temp=val/BASE;
}
if(temp)
res.num[++res.len]=temp;
while(res.len>1&&res.num[res.len]==0)
res.len--;
delete[] fa;
delete[] fb;
return res;
}
void read(){
init();
std::string s;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
s.push_back('-');
ch=getchar();
}
while(ch>='0'&&ch<='9'){
s.push_back(ch);
ch=getchar();
}
get_num(s);
}
void print(){
if(num[0])
putchar('-');
bool leading_zero=1;
for(int i=len;i;i--){
if(leading_zero)
printf("%d",num[i]);
else
printf("%02d",num[i]);
leading_zero=0;
}
putchar('\n');
return;
}
};
const int N=1<<10,M=100;
int n,m;
bigint<114514> a,b,c;
void solve2(){
a.read();
b.read();
c=a+b;
c.print();
}
#define int long long
#define ull unsigned int
#define N 65536
int memory[N];
ull code[N] = {
0x0000000000000000, 0x0000000000000001, 0x5000000000010002, 0x1000000000000002, 0xf000000000000000
};
void solve3()
{
for(int i=0;;i=(i+1)%N)
{
int op=code[i]>>60;
if(op==0) //Input
{
int p=code[i]&65535;
cin >> memory[ (code[i]>>56)&1 ? memory[p] : p ];
}
else if(op==1) //Output
{
int p=code[i]&65535, val = memory[ (code[i]>>56)&1 ? memory[p] : p ];
if((code[i]>>57)&1)
{
cout<<char(val&127);
}
else
{
cout<<val;
}
}
else if(op==2) //Write
{
int p = (code[i]>>32)&65535, val = code[i]&((1ll<<32)-1);
memory[ (code[i]>>56)&1 ? memory[p] : p ] = val;
}
else if(op==3) //Copy
{
int pf = (code[i]>>16)&65535, pt = code[i]&65535;
int val = memory[ (code[i]>>55)&1 ? memory[pf] : pf ];
memory[ (code[i]>>56)&1 ? memory[pt] : pt ] = val;
}
else if(op==4) //Calculate (& | ~ ^ << >>)
{
int p1 = (code[i]>>32)&65535, p2 = (code[i]>>16)&65535, p3 = code[i]&65535;
int val1 = memory[ (code[i]>>58)&1 ? memory[p1] : p1 ],
val2 = memory[ (code[i]>>57)&1 ? memory[p2] : p2 ], val3;
int op2 = (code[i]>>52)&15;
if(op2==0)
{
val3=val1&val2;
}
else if(op2==1)
{
val3=val1|val2;
}
else if(op2==2)
{
val3=~val2;
}
else if(op2==3)
{
val3=val1^val2;
}
else if(op2==4)
{
val3=((val2>>6)?0:val1<<val2);
}
else
{
val3=((val2>>6)?0:val1>>val2);
}
memory[ (code[i]>>56)&1 ? memory[p3] : p3 ] = val3;
}
else if(op==5)
{
int p1 = (code[i]>>32)&65535, p2 = (code[i]>>16)&65535, p3 = code[i]&65535;
int val1 = memory[ (code[i]>>58)&1 ? memory[p1] : p1 ],
val2 = memory[ (code[i]>>57)&1 ? memory[p2] : p2 ], val3;
int op2 = (code[i]>>52)&15;
if(op2==0)
{
val3=val1+val2;
}
else if(op2==1)
{
val3=val1-val2;
}
else if(op2==2)
{
val3=val1*val2;
}
else if(op2==3)
{
val3=(val2?val1/val2:0);
}
else
{
val3=(val2?val1%val2:0);
}
memory[ (code[i]>>56)&1 ? memory[p3] : p3 ] = val3;
}
else if(op==6) //Goto
{
int p=code[i]&65535;
i = (code[i]>>56)&1 ? memory[p] : p;
i = (i+N-1)%N;
}
else if(op==7) //If-goto (> < == >= <= !=)
{
int p1 = (code[i]>>32)&65535, p2 = (code[i]>>16)&65535, p3 = code[i]&65535;
int val1 = memory[ (code[i]>>58)&1 ? memory[p1] : p1 ],
val2 = memory[ (code[i]>>57)&1 ? memory[p2] : p2 ];
int op2 = (code[i]>>52)&15;
bool flag;
if(op2==0)
{
flag=(val1>val2);
}
else if(op2==1)
{
flag=(val1<val2);
}
else if(op2==2)
{
flag=(val1==val2);
}
else if(op2==3)
{
flag=(val1>=val2);
}
else if(op2==4)
{
flag=(val1<=val2);
}
else
{
flag=(val1!=val2);
}
if(flag)
{
i = (code[i]>>56)&1 ? memory[p3] : p3;
i = (i+N-1)%N;
}
}
else if(op==8) //x++
{
int p=code[i]&65535;
memory[ (code[i]>>56)&1 ? memory[p] : p ]++;
}
else if(op==9) //x--
{
int p=code[i]&65535;
memory[ (code[i]>>56)&1 ? memory[p] : p ]--;
}
else if(op==15) //Exit
{
break;
}
else
{
cout<<"\n\nError: Invalid code\n\n";
}
}
}
signed main(void){
srand(time(0));
int x=1+rand()%4;
if(x==1)
{
solve1();
return 0;
}
else if(x==2)
{
solve2();
}
else if(x==3){
solve3();
}
return 0;
}
看完了点个赞吧!!!


全部评论 1
66666666666666666666
1周前 来自 浙江
0











有帮助,赞一个