更新:
update240819:增加 free 函数释放内存,修复 div_int 答案为 0 的情况
长度固定,防止了 vector 动态分配内存常数过大。
定义:
__int114514 只是一个容器,没有重载运算符。定义时必须明确指出容量大小两种构造方法,例如 __int114514 a(100)。
函数:
注:参数若没有说明,均为 __int114514 类型
-
void read(a):读入数字到容器a -
void write(a):输出到容器a -
void add(a,b,c):计算a+b存到c中 -
void sub(a,b,c):计算a-b存到c中 -
void mul(a,b,c):计算a*b存到c中
输入(a、b)不能与输出(c)是一个变量。例如,mul(a,a,b) 是正确的,mul(a,b,a) 是错误的。
void div(a,b,c,d,div_cache tmp):计算a/b存到c中,余数为d,以tmp作为缓存
缓存容量大小不能小于 a 和 b 的大小
同上,输入(a、b)不能与输出(c、d)是一个变量。
void mul_fft(a,b,c,fft_cache tmp):计算a*b存到c中,以tmp作为缓存
缓存容量大小不能小于 2ab 的大小
void div_int(a,int b):让a除以b
void 类型,调用时直接修改 a 的值。
void mul_int(a,int b):让a乘以b
void 类型,调用时直接修改 a 的值。
int mod_int(a,int b):返回a取模b的值
int 类型,不会修改 a 的值。
__int114514 maketmp(int a):类型转换
传入 int 类型,生成一个 __int114514 类型的容器,用于实现高精度与低精度的加减
效率
| 函数 | 时间复杂度 | 常数 |
|---|---|---|
add,sub,mod_int,mul_int,div_int | 中等 | |
mul,div | 中等 | |
mul_fft | 大 |
经测试,FFT 能过P1919,而且非常快 从大佬那里抄的当然快
Code:
#include<bits/stdc++.h>
const double PI=acos(-1.0), PI_2=PI/2.0;
using namespace std;
#define _1e9 1000000000
#define _1e18 1000000000000000000LL
typedef complex<double> cpx;
struct __int114514 {
/*
used:已使用容量
data:容量开头指针,从个位开始往上存
f:是否为负数
前 used 项的值是有效的,后面的多余空间值无效(没有初始化)
*/
int* data;
int used;
bool f; // 负数标记
explicit __int114514(int n) {
data = (int*)malloc(sizeof(int) * n);
f=0, *data=0, used=1;
}
~__int114514(){
free(data);
}
__int114514(__int114514 &a) {
used=a.used, f=a.f;
data = (int*)malloc(sizeof(int) * (a.used+1));
memcpy(data, a.data, sizeof(int) * (a.used+1));
}
__int114514 & operator = (__int114514 &a) {
used=a.used, f=a.f;
memcpy(data, a.data, sizeof(int) * (a.used+1));
return *this;
}
int & operator [] (int i) {
return data[i];
}
void delete0(){
while(data[used-1] == 0 && used>1) used--;
}
};
// 缓存容器
struct div_cache {
long long* f;
int* g;
explicit div_cache(int n) {
f = (long long*)malloc(sizeof(long long) * n);
g = (int*)malloc(sizeof(int) * n);
}
};
struct fft_cache {
double *fa, *fb;
cpx *w;
int init_l;
explicit fft_cache(int n) {
fa = (double*)malloc(sizeof(double) * n);
fb = (double*)malloc(sizeof(double) * n);
w = (cpx*)malloc(sizeof(cpx) * (n>>3));
init_l = -1;
}
};
//高精度乘除低精度
void div_int(__int114514 &a, int b) {
if(b<0) a.f=!a.f, b=-b;
long long t=0;
for (int i=a.used-1; ~i; --i) {
t = t*_1e9+a[i];
a[i] = t/b;
t %= b;
}
a.delete0();
}
int mod_int(__int114514 &a, int b) {
long long t=0;
for (int i=a.used-1; ~i; --i) t=(t*_1e9+a[i])%b;
return t;
}
void mul_int(__int114514 &a, int b) {
if(b<0) a.f=!a.f, b=-b;
if(b==0) {
a.used=1, a[0]=0;
return;
}
long long tmp=0;
for(int i=0; i<a.used; ++i) {
long long cache = 1LL*a[i]*b+tmp;
a[i]=cache%_1e9, tmp=cache/_1e9;
}
if(tmp) a[a.used++] = tmp;
}
int unsigned_cmp(__int114514 &a, __int114514 &b) { // a>b
if(a.used > b.used) return 1;
if(a.used < b.used) return 0;
for(int i=a.used-1; ~i; --i) {
if(a[i] > b[i]) return 1;
if(a[i] < b[i]) return 0;
}
return 0;
}
void unsigned_add(__int114514 &a, __int114514 &b, __int114514 &c) {
int i=0;
for (long long t=0; i<a.used||i<b.used||t; ++i) {
if (i<a.used) t += a[i];
if (i<b.used) t += b[i];
c[i] = t%_1e9;
t /= _1e9;
}
c.used=i;
}
void unsigned_sub(__int114514 &a, __int114514 &b, __int114514 &c) {
int i=0;
for(long long t=0; i<a.used; ++i) {
t+=a[i];
if(i<b.used) t-=b[i];
if(t<0) c[i]=t+_1e9, t=-1; // 退位
else c[i]=t, t=0;
}
c.used=i, c.delete0();
}
void unsigned_mul(__int114514 &a, __int114514 &b, __int114514 &c) {
if((a.used==1&&a[0]==0) || (b.used==1&&b[0]==0)) { // 0 乘以任何数都为零
c.used=1, c[0]=0;
return;
}
c.used = a.used+b.used;
for(int i=0; i<c.used; ++i) c[i]=0;
for(int i=0; i<a.used; ++i) {
for(int j=0; j<b.used; ++j) {
int k=i+j;
long long cache = c[k]+1LL*a[i]*b[j];
c[k+1]+=cache/_1e9, c[k]=cache%_1e9;
}
}
while(c[c.used-1] == 0) --c.used;
}
void fft_init(int l, cpx *w, int &init_l) {
if (l<=init_l) return;
int t=__lg(l-1);
l=1<<t, *w=cpx(1.0, 0.0), init_l=l<<1;
for (int i=1; i<l; i<<=1) w[i]=polar(1.0,PI_2/i);
for (int i=1; i<l; ++i) w[i]=w[i&(i-1)]*w[i&-i];
}
void fft_dif(cpx *f, int &L, cpx *w) {
for(int l=L>>1,r=L; l; l>>=1,r>>=1) {
for(cpx *j=f,*o=w; j!=f+L; j+=r,++o) {
for(cpx *k=j; k!=j+l; ++k) {
cpx x=*k, y=k[l]*(*o);
*k=x+y, k[l]=x-y;
}
}
}
}
void fft_dit(cpx *f, int &L, cpx *w) {
for(int l=1,r=2; l<L; l<<=1,r<<=1) {
for(cpx *j=f,*o=w; j!=f+L; j+=r,++o) {
for(cpx *k=j; k!=j+l; ++k) {
cpx x=*k, y=k[l];
*k=x+y, k[l]=(x-y)*conj(*o);
}
}
}
}
void basic_fft(double *f, int lim, double *g, cpx *w, int &w_init_l) {
cpx *F=(cpx*)f, *G=(cpx*)g;
int l=lim>>1;
fft_init(l,w,w_init_l), fft_dif(F,l,w), fft_dif(G,l,w);
double fx=2.0/lim, fx2=0.5/lim;
F[0]=(F[0]*G[0]+2*F[0].imag()*G[0].imag())*fx, F[1]=F[1]*G[1]*fx;
for (int k=2,m=3; k<l; k<<=1,m<<=1) {
for (int i=k,j=i+k-1; i<m; ++i,--j) {
cpx oi=(F[i]+conj(F[j])), hi=(F[i]-conj(F[j]));
cpx Oi=(G[i]+conj(G[j])), Hi=(G[i]-conj(G[j]));
cpx r0=oi*Oi-hi*Hi*((i&1)?-w[i>>1]:w[i>>1]), r1=Oi*hi+oi*Hi;
F[i]=(r0+r1)*fx2, F[j]=conj(r0-r1)*fx2;
}
}
fft_dit(F,l,w);
}
void unsigned_mul_fft(__int114514 &a, __int114514 &b, __int114514 &c, double *fa, double *fb, cpx *w, int &w_init_l) {
if((a.used==1&&a[0]==0) || (b.used==1&&b[0]==0)) { // 0 乘以任何数都为零
c.used=1, c[0]=0;
return;
}
// if(a.used<=10 && b.used<=10) { // 数字过小,直接调用朴素乘法
// unsigned_mul(a,b,c);
// return;
// }
int n = 1;
{
int __Acnt, __Bcnt;
// 本来是压 9 位储存,为防止精度爆炸,转换成压 4 位计算
{
for(int i=0; i<a.used/4; ++i) // 4 个 9 位 --> 9 个 4 位
fa[i*9]=(a[i*4]%10000), fa[i*9+1]=(a[i*4]/10000%10000), fa[i*9+2]=(a[i*4]/100000000+a[i*4+1]%1000*10), fa[i*9+3]=(a[i*4+1]/1000%10000), fa[i*9+4]=(a[i*4+1]/10000000+a[i*4+2]%100*100), fa[i*9+5]=(a[i*4+2]/100%10000), fa[i*9+6]=(a[i*4+2]/1000000+a[i*4+3]%10*1000), fa[i*9+7]=(a[i*4+3]/10%10000), fa[i*9+8]=(a[i*4+3]/100000%10000);
int f4v=0, f4len=0;
__Acnt=a.used/4*9;
for(int i=a.used/4*4; i<a.used; ++i) {
for(int j=0,pw=1; j<9; j++,pw*=10) {
f4v=(a[i]/pw%10)*((int)pow(10,f4len))+f4v, f4len++;
if(f4len == 4) fa[__Acnt++]=(f4v), f4v=0, f4len=0;
}
}
if(f4len > 0) fa[__Acnt++]=(f4v), f4v=0, f4len=0;
while (n < __Acnt) n<<=1;
}{
for(int i=0; i<b.used/4; ++i) // 4 个 9 位 --> 9 个 4 位
fb[i*9]=(b[i*4]%10000), fb[i*9+1]=(b[i*4]/10000%10000), fb[i*9+2]=(b[i*4]/100000000+b[i*4+1]%1000*10), fb[i*9+3]=(b[i*4+1]/1000%10000), fb[i*9+4]=(b[i*4+1]/10000000+b[i*4+2]%100*100), fb[i*9+5]=(b[i*4+2]/100%10000), fb[i*9+6]=(b[i*4+2]/1000000+b[i*4+3]%10*1000), fb[i*9+7]=(b[i*4+3]/10%10000), fb[i*9+8]=(b[i*4+3]/100000%10000);
int f4v=0, f4len=0;
__Bcnt=b.used/4*9;
for(int i=b.used/4*4; i<b.used; ++i) {
for(int j=0,pw=1; j<9; j++,pw*=10) {
f4v=(b[i]/pw%10)*((int)pow(10,f4len))+f4v, f4len++;
if(f4len == 4) fb[__Bcnt++]=(f4v), f4v=0, f4len=0;
}
}
if(f4len > 0) fb[__Bcnt++]=(f4v), f4v=0, f4len=0;
while (n < __Bcnt) n<<=1;
}
n<<=1;
for(int i=__Acnt; i<n; ++i) fa[i]=0;
for(int i=__Bcnt; i<n; ++i) fb[i]=0;
}
// FFT 计算
basic_fft(fa,n,fb,w,w_init_l);
// 进位处理
long long tmp=0;
for (int i=0; i<=a.used+b.used; ++i) c[i]=0;
c.used=0, c.f=0;
{
int f9v=0, f9len=0;
for(int i=0; i<n; ++i) {
tmp += fa[i]+0.5;
for(int j=0,pw=1; j<4; j++,pw*=10) {
f9v=(tmp/pw%10)*((int)pow(10,f9len))+f9v, f9len++;
if(f9len == 9) c[c.used++]=f9v, f9v=0, f9len=0;
}
tmp /= 10000;
}
if(f9len > 0) c[c.used++]=f9v, f9v=0, f9len=0;
}
while (c[c.used-1] == 0) --c.used;
}
inline void unsigned_mul_fft(__int114514 &a, __int114514 &b, __int114514 &c, fft_cache tmp) {
unsigned_mul_fft(a,b,c,tmp.fa,tmp.fb,tmp.w,tmp.init_l);
}
bool chu_cmp(const long long *a, const int &a_h, const int &a_t, const int *b, const int &bsize) {
if(a_t-a_h+1 > bsize) return 1;
if(a_t-a_h+1 < bsize) return 0;
for(int i=bsize-1; ~i; --i) {
if(a[a_h+i] > b[i]) return 1;
if(a[a_h+i] < b[i]) return 0;
}
return 1;
}
void unsigned_div(__int114514 &a, __int114514 &b, __int114514 &c, __int114514 &d, long long* f, int* g) {
// a/b = c ...... d
// f:被除数缓冲区
// g:试商缓冲区
int _head=a.used+2, _tail=a.used+1, f_len;
long long ct;
if (a.used < b.used) {
c.used=1, c[0]=0, d=a;
return;
}
c.used=0, c.f=0;
for(int i=a.used-1; ~i; --i) {
f[--_head] = a[i];
if(_tail-_head+1 >= b.used) {
int l,r,p=0;
// 用首几位相除估计商的范围
if(_tail-_head+1 == b.used) l=f[_tail]/(b[b.used-1]+1), r=(f[_tail]+1)/b[b.used-1];
else {
#ifdef __SIZEOF_INT128__
if(b.used >= 3) {
__int128 ff = (__int128(f[_tail]*_1e9+f[_tail-1])*_1e9+f[_tail-2])*_1e9+f[_tail-3];
__int128 gg = (__int128(b[b.used-1]*_1e9+b[b.used-2])*_1e9+b[b.used-3]);
l=ff/(gg+1), r=min((long long)((ff+1)/gg),999999999LL);
} else
#else
#warning this compiler does not support __int128, which may slow down the run speed
#endif
l=(f[_tail]*_1e9+f[_tail-1])/(b[b.used-1]+1), r=min((f[_tail]*_1e9+f[_tail-1]+1)/b[b.used-1],999999999LL);
}
while(l <= r) { // 二分试商
int mid = (l+r) >> 1;
f_len=0, ct=0; // 高精乘低精(试商)
for(int i=0; i<(int)b.used; ++i) ct+=1LL*b[i]*mid, g[f_len++]=ct%_1e9, ct/=_1e9;
while(ct) g[f_len++]=ct%_1e9, ct/=_1e9;
if(chu_cmp(f,_head,_tail,g,f_len)) p=mid, l=mid+1;
else r=mid-1;
}
for(int j=_head; j<_head+b.used; ++j) { // 减
f[j] -= 1LL*b[j-_head]*p;
if(f[j] < 0) f[j+1]-=(-f[j])/_1e9+((-f[j])%_1e9>0), f[j]=(f[j]+_1e18)%_1e9;
}
c[c.used++]=p;
while (_head <= _tail && f[_tail] == 0) --_tail;
} else c[c.used++]=0;
}
reverse(c.data, c.data+c.used);
while (c.used > 1 && c[c.used-1] == 0) --c.used;
if(_head <= _tail) {
d.used=0, d.f=0;
for(int i=_head; i<=_tail; ++i) d[d.used++] = (int)f[i];
} else {
d.used=1, d.f=0, d[0]=0;
}
}
inline void unsigned_div(__int114514 &a, __int114514 &b, __int114514 &c, __int114514 &d, div_cache &tmp) {
unsigned_div(a,b,c,d,tmp.f,tmp.g);
}
inline void sub(__int114514 &a, __int114514 &b, __int114514 &c) noexcept {
if(a.f == b.f) {
if(unsigned_cmp(a,b)) // 判断够不够减
unsigned_sub(a,b,c), c.f=a.f;
else
unsigned_sub(b,a,c), c.f=!a.f;
} else {
unsigned_add(a,b,c), c.f=a.f;
}
}
inline void add(__int114514 &a, __int114514 &b, __int114514 &c) {
if(a.f == b.f) {
unsigned_add(a,b,c), c.f=a.f;
} else {
if(unsigned_cmp(a,b)) unsigned_sub(a,b,c), c.f=a.f;
else unsigned_sub(b,a,c), c.f=b.f;
}
}
inline void mul(__int114514 &a, __int114514 &b, __int114514 &c) {
unsigned_mul(a,b,c), c.f=a.f^b.f;
}
inline void mul_fft(__int114514 &a, __int114514 &b, __int114514 &c, fft_cache tmp) {
unsigned_mul_fft(a,b,c,tmp), c.f=a.f^b.f;
}
inline void div(__int114514 &a, __int114514 &b, __int114514 &c, __int114514 &d, div_cache tmp) {
unsigned_div(a,b,c,d,tmp.f,tmp.g), c.f=a.f^b.f;
}
void read(__int114514 &a) {
a.used=0;
string s;
cin >> s;
if(s[0] == '-') a.f=1;
else a.f=0;
for (int i=s.size()-1; i>=a.f; i-=9) {
int st = max((int)a.f, i-8), len = i-st+1;
a[a.used++] = (stoi(s.substr(st,len)));
}
a.delete0();
}
void write(__int114514 &a) {
if(a.f && !(a[0]==0 && a.used==1)) putchar('-');
cout << a[a.used-1];
for (int i=a.used-2; ~i; --i) cout << setw(9) << setfill('0') << a[i];
}
__int114514 & maketmp(int x) {
if(x >= _1e9) {
static __int114514 tmp(2);
if(x<0) x=-x, tmp.f=1;
tmp[0]=x%_1e9, tmp[1]=x/_1e9, tmp.used=2;
return tmp;
} else {
static __int114514 tmp(1);
if(x<0) x=-x, tmp.f=1;
tmp[0]=x;
return tmp;
}
}使用示例
计算
#include<bits/stdc++.h>
#include"int114514mini"
using namespace std;
__int114514 ans((1<<22)+3),x((1<<22)+3);
fft_cache cache((1<<22)+3);
int main(){
int s = clock();
ans.used=1, ans[0]=1;
x.used=1, x[0]=2;
int b = 1e7;
while(b){
if(b&1){
mul_fft(ans,x,ans,cache);
}
mul_fft(x,x,x,cache);
b>>=1;
}
cout << "time:" << clock()-s;
freopen("data.txt","w",stdout);
write(ans);
return 0;
}