记录编号 |
402894 |
评测结果 |
AAAAAAAAAA |
题目名称 |
增强的加法问题 |
最终得分 |
100 |
用户昵称 |
sxysxy |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.006 s |
提交时间 |
2017-05-08 11:38:39 |
内存使用 |
8.31 MiB |
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstdarg>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int gcd(int a, int b){return b?gcd(b, a%b):a;}
struct virt{
double a, b;
virt(double x = 0, double y = 0){a = x, b = y;}
virt operator+(virt &r){return virt(a+r.a, b+r.b);}
virt operator-(virt &r){return virt(a-r.a, b-r.b);}
virt operator*(double p){return virt(a*p, b*p);}
virt operator*(virt &r){return virt(a*r.a-b*r.b, a*r.b+b*r.a);}
};
virt pa[(1<<18)+1];
virt pb[(1<<18)+1];
const double PI = acos(-1);
void rader(virt *a, int n){
int j = n>>1;
for(int i = 1; i < n-1; i++){
if(i < j)swap(a[i], a[j]);
int k = n>>1;
while(j >= k){
j -= k; k >>= 1;
}if(j < k)j += k;
}
}
void fft(virt *a, int n, bool idft = false){
rader(a, n);
double pi = idft?PI:-PI;
for(int h = 2; h <= n; h <<= 1){
virt omgn(cos(2*pi/h), sin(2*pi/h));
for(int j = 0; j < n; j += h){
virt omg(1, 0);
for(int k = j; k < j+h/2; k++){
virt u = a[k], v = omg*a[k+h/2];
a[k] = u+v, a[k+h/2] = u-v;
omg = omg*omgn;
}
}
}
if(idft)for(int i = 0; i < n; i++)a[i].a /= n;
}
void mul(vector<int> &a, vector<int> &b){
int la = 0, lb = 0;
int s = 0;
for(vector<int>::iterator it = a.begin(); it != a.end(); ++it)
pa[la++] = *it;
for(vector<int>::iterator it = b.begin(); it != b.end(); ++it)
pb[lb++] = *it;
while((1<<s) <= la+lb)s++; s = 1<<s;
fft(pa, s); fft(pb, s);
for(int i = 0; i < s; i++)pa[i] = pa[i]*pb[i];
fft(pa, s, true);
a.resize(la+lb-1);
for(int i = 0; i <= la+lb-2; i++)
a[i] = (int)(pa[i].a+0.5);
}
struct bignum{
vector<int> bits;
inline void init(int v){
bits.push_back(v);
}
inline int length(){return bits.size();}
void addw(bignum &o){
int s = 0;
if(length() > o.length()){
s = length(); o.bits.resize(s);
}else{
s = o.length(); bits.resize(s);
}
for(int i = 0; i < s; i++){
bits[i] += o.bits[i];
}
if(bits[s-1] > 10)bits.push_back(0);
for(int i = 0; i+1 < length(); i++){
bits[i+1] += bits[i]/10;
bits[i] %= 10;
}
}
void print(){
for(int i = bits.size()-1; ~i; i--)printf("%d", bits[i]);
}
void get(){
static char buf[600];
scanf("%s", buf);
for(int i = 0; buf[i]; i++)bits.push_back(buf[i]^0x30);
reverse(bits.begin(), bits.end());
}
};
int main(){
freopen("add.in", "r", stdin);
freopen("add.out", "w", stdout);
bignum a, b; a.get(); b.get();
a.addw(b); a.print();
return 0;
}