// 三种乘法赛跑：小学竖式 / Karatsuba / 快速傅里叶（NTT 版）
// 位数每翻一倍，量一次时间，看时间翻几倍。
// 编译：cc -O3 -march=native -o bench bench.c
// 用法：./bench check | ./bench school K0 K1 | ./bench kara K0 K1 | ./bench ntt K0 K1
//   K 是 log2(32 位块数)：K=10 → 1024 块 = 32768 位 ≈ 9864 位十进制数
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef uint32_t u32; typedef uint64_t u64; typedef __uint128_t u128;

static double now(void){ struct timespec ts; clock_gettime(CLOCK_MONOTONIC,&ts); return ts.tv_sec+ts.tv_nsec*1e-9; }
static u64 rng=88172645463325252ULL;
static u32 rnd(void){ rng^=rng<<13; rng^=rng>>7; rng^=rng<<17; return (u32)rng; }

// ---- 小学竖式：每一位乘每一位，n*n 次小乘法 ----
static void school(const u32*a,const u32*b,u32*r,size_t n){
  memset(r,0,2*n*sizeof(u32));
  for(size_t i=0;i<n;i++){
    u64 carry=0, ai=a[i];
    for(size_t j=0;j<n;j++){
      u64 t=ai*b[j]+r[i+j]+carry;
      r[i+j]=(u32)t; carry=t>>32;
    }
    r[i+n]=(u32)carry;
  }
}

// ---- Karatsuba：拆两半，4 次乘法换成 3 次，再递归 ----
static int add_n(u32*r,const u32*a,const u32*b,size_t n){ u64 c=0; for(size_t i=0;i<n;i++){ c+= (u64)a[i]+b[i]; r[i]=(u32)c; c>>=32;} return (int)c; }
static int sub_n(u32*r,const u32*a,const u32*b,size_t n){ int64_t c=0; for(size_t i=0;i<n;i++){ c+=(int64_t)a[i]-b[i]; r[i]=(u32)c; c>>=32;} return (int)c; }
static void add_into(u32*r,size_t rn,const u32*a,size_t an){ u64 c=0; size_t i=0; for(;i<an;i++){ c+=(u64)r[i]+a[i]; r[i]=(u32)c; c>>=32;} for(;c&&i<rn;i++){ c+=r[i]; r[i]=(u32)c; c>>=32;} }
static void sub_from(u32*r,size_t rn,const u32*a,size_t an){ int64_t c=0; size_t i=0; for(;i<an;i++){ c+=(int64_t)r[i]-a[i]; r[i]=(u32)c; c>>=32;} for(;c&&i<rn;i++){ c+=r[i]; r[i]=(u32)c; c>>=32;} }
#define KCUT 32
static void kara(const u32*a,const u32*b,u32*r,size_t n,u32*ws){
  if(n<=KCUT){ school(a,b,r,n); return; }
  size_t h=n/2;
  const u32*a0=a,*a1=a+h,*b0=b,*b1=b+h;
  kara(a0,b0,r,h,ws);          // 低位 × 低位
  kara(a1,b1,r+n,h,ws);        // 高位 × 高位
  u32*sa=ws, *sb=ws+h+1, *m=ws+2*(h+1), *nws=ws+2*(h+1)+2*(h+1);
  sa[h]=add_n(sa,a0,a1,h); sb[h]=add_n(sb,b0,b1,h);
  // (a0+a1)(b0+b1)：h+1 块，最高块只有 0/1，单独补
  kara(sa,sb,m,h,nws);
  m[2*h]=0; m[2*h+1]=0;
  if(sa[h]) add_into(m+h,h+2,sb,h);
  if(sb[h]) add_into(m+h,h+2,sa,h);
  if(sa[h]&&sb[h]) add_into(m+2*h,2,(u32[]){1},1);
  sub_from(m,2*h+2,r,2*h);       // 减掉 低×低
  sub_from(m,2*h+2,r+n,2*h);     // 减掉 高×高 → 剩下中间那项
  add_into(r+h,2*n-h,m,2*h+2);
}

// ---- 快速傅里叶（数论变换 NTT，模 2^64-2^32+1，结果精确）----
static const u64 P=0xFFFFFFFF00000001ULL;
static inline u64 mulm(u64 a,u64 b){ u128 x=(u128)a*b; u64 lo=(u64)x, hi=(u64)(x>>64);
  u64 hh=hi>>32, hl=hi&0xFFFFFFFFULL; u64 t=lo-hh; if(lo<hh) t+=P; u64 u=(hl<<32)-hl; u64 res=t+u; if(res<t||res>=P) res-=P; return res; }
static inline u64 addm(u64 a,u64 b){ u64 s=a+b; if(s<a||s>=P) s-=P; return s; }
static inline u64 subm(u64 a,u64 b){ return a>=b? a-b : a+P-b; }
static u64 powm(u64 a,u64 e){ u64 r=1; while(e){ if(e&1) r=mulm(r,a); a=mulm(a,a); e>>=1;} return r; }
static void ntt(u64*x,size_t n,int inv){
  for(size_t i=1,j=0;i<n;i++){ size_t bit=n>>1; for(;j&bit;bit>>=1) j^=bit; j^=bit; if(i<j){u64 t=x[i];x[i]=x[j];x[j]=t;} }
  for(size_t len=2;len<=n;len<<=1){
    u64 w=powm(7,(P-1)/len); if(inv) w=powm(w,P-2);
    size_t half=len/2; u64*tw=malloc(half*sizeof(u64)); tw[0]=1; for(size_t k=1;k<half;k++) tw[k]=mulm(tw[k-1],w);
    for(size_t i=0;i<n;i+=len) for(size_t k=0;k<half;k++){ u64 u=x[i+k], v=mulm(x[i+k+half],tw[k]); x[i+k]=addm(u,v); x[i+k+half]=subm(u,v); }  // 一只「蝴蝶」
    free(tw);
  }
  if(inv){ u64 ni=powm(n%P,P-2); for(size_t i=0;i<n;i++) x[i]=mulm(x[i],ni); }
}
static void nttmul(const u32*a,const u32*b,u32*r,size_t n){
  size_t m=2*n, N=1; while(N<2*m) N<<=1;   // 16 位一段
  u64*fa=calloc(N,8),*fb=calloc(N,8);
  for(size_t i=0;i<n;i++){ fa[2*i]=a[i]&0xFFFF; fa[2*i+1]=a[i]>>16; fb[2*i]=b[i]&0xFFFF; fb[2*i+1]=b[i]>>16; }
  ntt(fa,N,0); ntt(fb,N,0); for(size_t i=0;i<N;i++) fa[i]=mulm(fa[i],fb[i]); ntt(fa,N,1);
  u128 c=0; for(size_t i=0;i<2*n;i++){ c+=fa[2*i]; c+= (u128)fa[2*i+1]<<16; r[i]=(u32)c; c>>=32; }
  free(fa); free(fb);
}

static double run(const char*alg,size_t n,int reps){
  u32*a=malloc(n*4),*b=malloc(n*4),*r=malloc(2*n*4+16); u32*ws=NULL;
  for(size_t i=0;i<n;i++){ a[i]=rnd(); b[i]=rnd(); }
  if(!strcmp(alg,"kara")) ws=malloc(sizeof(u32)*(8*n+256));
  double best=1e30;
  for(int k=0;k<reps;k++){ double t0=now();
    if(!strcmp(alg,"school")) school(a,b,r,n); else if(!strcmp(alg,"kara")) kara(a,b,r,n,ws); else nttmul(a,b,r,n);
    double t=now()-t0; if(t<best) best=t; }
  free(a);free(b);free(r);free(ws); return best;
}

int main(int argc,char**argv){
  if(argc>=2&&!strcmp(argv[1],"check")){
    for(int K=4;K<=13;K++){ size_t n=(size_t)1<<K; u32*a=malloc(n*4),*b=malloc(n*4),*r1=malloc(2*n*4),*r2=malloc(2*n*4+16),*r3=malloc(2*n*4+16),*ws=malloc(sizeof(u32)*(8*n+256));
      for(size_t i=0;i<n;i++){a[i]=rnd();b[i]=rnd();}
      school(a,b,r1,n); kara(a,b,r2,n,ws); nttmul(a,b,r3,n);
      printf("K=%d kara %s ntt %s\n",K, memcmp(r1,r2,2*n*4)?"WRONG":"ok", memcmp(r1,r3,2*n*4)?"WRONG":"ok");
      free(a);free(b);free(r1);free(r2);free(r3);free(ws);} return 0; }
  const char*alg=argv[1]; int K0=atoi(argv[2]), K1=atoi(argv[3]);
  double prev=0;
  for(int K=K0;K<=K1;K++){ size_t n=(size_t)1<<K; int reps = K<K1-2?3:1; double t=run(alg,n,reps);
    printf("%s\t%d\t%zu\t%.0f\t%.6f\t%s%.3f\n",alg,K,n,n*32*0.30102999566,t,prev>0?"x":"",prev>0?t/prev:0); fflush(stdout); prev=t; }
}
