結果

問題 No.3676 Cuboid Alignment
コンテスト
ユーザー Taiki0715
提出日時 2026-09-04 23:21:53
言語 C++23
(gcc 15.3.0 + boost 1.92.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 44,977 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,738 ms
コンパイル使用メモリ 476,448 KB
実行使用メモリ 227,828 KB
最終ジャッジ日時 2026-09-04 23:22:13
合計ジャッジ時間 16,214 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 11 WA * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2>istream &operator>>(istream&,pair<T1,T2>&);
template<typename...Args>istream &operator>>(istream&,tuple<Args...>&a);
template<typename T>istream &operator>>(istream&is,vector<T>&a);
template<typename T,size_t N>istream &operator>>(istream&is,array<T,N>&a);
template<typename T1,typename T2>
istream &operator>>(istream&is,pair<T1,T2>&a){
  is>>a.first>>a.second;
  return is;
}
template<size_t pos,typename...Args>
void read_tuple(istream&is,tuple<Args...>&a){
  if constexpr(pos<tuple_size<tuple<Args...>>::value){
    is>>get<pos>(a);
    read_tuple<pos+1>(is,a);
  }
}
template<typename...Args>
istream &operator>>(istream&is,tuple<Args...>&a){
  read_tuple<0>(is,a);
  return is;
}
template<typename T>
istream &operator>>(istream&is,vector<T>&a){
  for(T&x:a)is>>x;
  return is;
}
template<typename T,size_t N>
istream &operator>>(istream&is,array<T,N>&a){
  for(T&x:a)is>>x;
  return is;
}
template<typename T1,typename T2>ostream &operator<<(ostream&os,const pair<T1,T2>&);
template<typename...Args>ostream &operator<<(ostream&os,const tuple<Args...>&);
template<typename T>ostream &operator<<(ostream&os,const vector<T>&);
template<typename T,typename Seq,typename Comp>ostream &operator<<(ostream&os,priority_queue<T,Seq,Comp>);
template<typename T>ostream &operator<<(ostream&os,queue<T>);
template<typename T>ostream &operator<<(ostream&os,deque<T>);
template<typename T>ostream &operator<<(ostream&os,stack<T>);
template<typename T,size_t N>ostream &operator<<(ostream&os,const array<T,N>&);
template<typename Key,typename Val,typename Comp>ostream &operator<<(ostream&os,const map<Key,Val,Comp>&);
template<typename Key,typename Val,typename Hash>ostream &operator<<(ostream&os,const unordered_map<Key,Val,Hash>&);
template<typename T,typename Comp>ostream &operator<<(ostream&os,const set<T,Comp>&);
template<typename T,typename Comp>ostream &operator<<(ostream&os,const multiset<T,Comp>&);
template<typename T,typename Hash>ostream &operator<<(ostream&os,const unordered_set<T,Hash>&);
template<typename T1,typename T2>
ostream &operator<<(ostream&os,const pair<T1,T2>&a){
  os<<a.first<<' '<<a.second;
  return os;
}
template<size_t pos,typename...Args>
void write_tuple(ostream&os,const tuple<Args...>&a){
  if constexpr(pos<tuple_size<tuple<Args...>>::value){
    if constexpr(pos>0)os<<' ';
    os<<get<pos>(a);
    write_tuple<pos+1>(os,a);
  }
}
template<typename...Args>
ostream &operator<<(ostream&os,const tuple<Args...>&a){
  write_tuple<0>(os,a);
  return os;
}
template<typename T>
ostream &operator<<(ostream&os,const vector<T>&a){
  os<<'{';
  for(int i=0;i<(int)a.size();i++){
    os<<a[i];
    if(i+1!=a.size())os<<',';
  }
  os<<'}';
  return os;
}
template<typename T,typename Seq,typename Comp>
ostream &operator<<(ostream&os,priority_queue<T,Seq,Comp>a){
  os<<'{';
  if(!a.empty()){
    os<<a.top();a.pop();
    while(!a.empty()){
      os<<',';
      os<<a.top();
      a.pop();
    }
  }
  os<<'}';
  return os;
}
template<typename T>
ostream &operator<<(ostream&os,queue<T>a){
  os<<'{';
  if(!a.empty()){
    os<<a.front();a.pop();
    while(!a.empty()){
      os<<',';
      os<<a.front();
      a.pop();
    }
  }
  os<<'}';
  return os;
}
template<typename T>
ostream &operator<<(ostream&os,deque<T>a){
  os<<'{';
  if(!a.empty()){
    os<<a.front();a.pop_front();
    while(!a.empty()){
      os<<',';
      os<<a.front();
      a.pop_front();
    }
  }
  os<<'}';
  return os;
}
template<typename T>
ostream &operator<<(ostream&os,stack<T>a){
  os<<'{';
  if(!a.empty()){
    os<<a.top();a.pop();
    while(!a.empty()){
      os<<',';
      os<<a.top();
      a.pop();
    }
  }
  os<<'}';
  return os;
}
template<typename T,size_t N>
ostream &operator<<(ostream&os,const array<T,N>&a){
  os<<'{';
  for(int i=0;i<(int)a.size();i++){
    os<<a[i];
    if(i+1!=a.size())os<<',';
  }
  os<<'}';
  return os;
}
template<typename Key,typename Val,typename Comp>
ostream &operator<<(ostream&os,const map<Key,Val,Comp>&a){
  if(a.empty()){
    os<<"{}";
    return os;
  }
  auto itr=a.begin();
  os<<"{["<<itr->first<<","<<itr->second<<']';
  while(++itr!=a.end())os<<",["<<itr->first<<','<<itr->second<<']';
  os<<'}';
  return os;
}
template<typename Key,typename Val,typename Hash>
ostream &operator<<(ostream&os,const unordered_map<Key,Val,Hash>&a){
  if(a.empty()){
    os<<"{}";
    return os;
  }
  auto itr=a.begin();
  os<<"{["<<itr->first<<","<<itr->second<<']';
  while(++itr!=a.end())os<<",["<<itr->first<<','<<itr->second<<']';
  os<<'}';
  return os;
}
template<typename T,typename Comp>
ostream &operator<<(ostream&os,const set<T,Comp>&a){
  if(a.empty()){
    os<<"{}";
    return os;
  }
  auto itr=a.begin();
  os<<'{'<<*itr;
  while(++itr!=a.end())os<<','<<*itr;
  os<<'}';
  return os;
}
template<typename T,typename Comp>
ostream &operator<<(ostream&os,const multiset<T,Comp>&a){
  if(a.empty()){
    os<<"{}";
    return os;
  }
  auto itr=a.begin();
  os<<'{'<<*itr;
  while(++itr!=a.end())os<<','<<*itr;
  os<<'}';
  return os;
}
template<typename T,typename Hash>
ostream &operator<<(ostream&os,const unordered_set<T,Hash>&a){
  if(a.empty()){
    os<<"{}";
    return os;
  }
  auto itr=a.begin();
  os<<'{'<<*itr;
  while(++itr!=a.end())os<<','<<*itr;
  os<<'}';
  return os;
}
using namespace std;
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int){a.first++,a.second++;}
template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int){a.first--,a.second--;}
template<typename T>void operator++(vector<T>&a,int){for(auto &i:a)i++;}
template<typename T>void operator--(vector<T>&a,int){for(auto &i:a)i--;}
#define overload3(_1,_2,_3,name,...) name
#define rep1(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,l,r) for(int i=(int)(l);i<(int)(r);i++)
#define rep(...) overload3(__VA_ARGS__,rep2,rep1)(__VA_ARGS__)
#define reps(i,l,r) rep2(i,l,r)
#define all(x) x.begin(),x.end()
#define pcnt(x) __builtin_popcountll(x)
#define fin(x) return cout<<(x)<<'\n',static_cast<void>(0)
#define yn(x) cout<<((x)?"Yes\n":"No\n")
#define uniq(x) sort(all(x)),x.erase(unique(all(x)),x.end())
template<typename T>
inline int fkey(vector<T>&z,T key){return lower_bound(z.begin(),z.end(),key)-z.begin();}
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<typename T,size_t n,size_t id=0>
auto vec(const int (&d)[n],const T &init=T()){
  if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));
  else return init;
}
#ifdef LOCAL
#include<debug.h>
#define SWITCH(a,b) (a)
#else
#define debug(...) static_cast<void>(0)
#define debugg(...) static_cast<void>(0)
#define SWITCH(a,b) (b)
#endif
struct Timer{
  clock_t start;
  Timer(){
    start=clock();
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout<<fixed<<setprecision(16);
  }
  inline double now(){return (double)(clock()-start)/1000;}
  #ifdef LOCAL
  ~Timer(){
    cerr<<"time:";
    cerr<<now();
    cerr<<"ms\n";
  }
  #endif
}timer;
void SOLVE();
int main(){
  int testcase=1;
  //cin>>testcase;
  for(int i=0;i<testcase;i++){
    SOLVE();
  }
}
#include<type_traits>
#include<optional>
#include<initializer_list>
constexpr bool isprime_constexpr(unsigned long long n){
  if(n==998244353)return true;
  if(n==1000000007)return true;
  if(n<64)return 2891462833508853932ll>>n&1;
  if(n%2==0)return false;
  unsigned long long d=n-1;
  int s=0;
  while(!(d&1))d>>=1,s++;
  int q=63;
  while(!(d>>q))q--;
  unsigned long long r=n;
  for(int i=0;i<5;i++)r*=2-r*n;
  auto redc=[&r,&n](__uint128_t x)->unsigned long long {
    x=(x+__uint128_t((unsigned long long)x*-r)*n)>>64;
    return x>=n?x-n:x;
  };
  __uint128_t r2=-__uint128_t(n)%n;
  unsigned long long one=redc(r2);
  for(unsigned long long base:{2,325,9375,28178,450775,9780504,1795265022}){
    if(base%n==0)continue;
    unsigned long long a=base=redc((base%n)*r2);
    for(int i=q-1;i>=0;i--){
      a=redc(__uint128_t(a)*a);
      if(d>>i&1)a=redc(__uint128_t(a)*base);
    }
    if(a==one)continue;
    for(int i=1;a!=n-one;i++){
      if(i>=s)return false;
      a=redc(__uint128_t(a)*a);
    }
  }
  return true;
}
constexpr std::pair<long long,long long>ext_gcd(long long a,long long b){
  if(b==0)return std::make_pair(1,0);
  auto [x,y]=ext_gcd(b,a%b);
  std::swap(x,y);
  return std::make_pair(x,y-a/b*x);
}
template<std::signed_integral T>
constexpr std::pair<T,T> inv_mod(T a,T b){
  a%=b;
  if(a<0)a+=b;
  if(a==0)return std::make_pair(b,0);
  T s=b,t=a;
  T m0=0,m1=1;
  while(t){
    T u=s/t;
    s-=t*u;
    m0-=m1*u;
    std::swap(s,t);
    std::swap(m0,m1);
  }
  if(m0<0)m0+=b/s;
  return std::make_pair(s,m0);
}
template<auto m>
struct modint{
  static_assert(1<=m&&m<(1ull<<63));
  using value_type=std::conditional_t<((m>>31)==0),uint32_t,uint64_t>;
  using mul_type=std::conditional_t<((m>>31)==0),uint64_t,__uint128_t>;
private:
  value_type v;
  static constexpr value_type umod=m;
  constexpr modint sqrt_impl()const{
    if(this->val()<=1)return *this;
    if(umod%8==1){
      modint b=2;
      while(b.pow((umod-1)/2).val()==1)b++;
      value_type m2=umod-1;
      int e=0;
      while(m2%2==0)m2>>=1,e++;
      modint x=this->pow((m2-1)/2);
      modint y=(*this)*x*x;
      x*=*this;
      modint z=b.pow(m2);
      while(y.val()!=1){
        int j=0;
        modint t=y;
        while(t.val()!=1)t*=t,j++;
        z=z.pow((value_type(1))<<(e-j-1));
        x*=z;
        z*=z;
        y*=z;
        e=j;
      }
      return x;
    }
    else if(umod%8==5){
      modint res=this->pow((umod+3)/8);
      if((res*res).val()==this->val())return res;
      else return res*modint(2).pow((umod-1)/4);
    }
    else return this->pow((umod+1)/4);
  }
  template<typename U,std::enable_if_t<std::unsigned_integral<U>||std::is_same_v<U,__uint128_t>,std::nullptr_t> =nullptr>
  static constexpr value_type take_mod(U x){
    if constexpr(std::numeric_limits<U>::max()<umod)return x;
    if constexpr(umod==(1ull<<61)-1){
      value_type res=(x>>61)+(x&umod);
      if(res>=umod)res-=umod;
      return res;
    }
    if constexpr(umod==(1ull<<61)-(1ull<<24)+1){
      value_type high=x>>61,low=x&((1ull<<61)-1);
      mul_type t=low+(mul_type(high)<<24)-high;
      high=t>>61,low=t&((1ull<<61)-1);
      low=low+(mul_type(high)<<24)-high;
      if(low>=umod)low-=umod;
      return low;
    }
    return x%umod;
  }
public:
  constexpr modint():v(0){}
  template<typename U,std::enable_if_t<std::signed_integral<U>||std::is_same_v<U,__int128_t>,std::nullptr_t> =nullptr>
  constexpr modint(U x){
    x%=std::make_signed_t<value_type>(umod);
    v=x>=0?x:x+umod;
  }
  template<typename U,std::enable_if_t<std::unsigned_integral<U>||std::is_same_v<U,__uint128_t>,std::nullptr_t> =nullptr>
  constexpr modint(U x):v(take_mod<U>(x)){}
  static constexpr value_type mod(){return umod;}
  template<typename U>
  static constexpr modint raw(U x){
    modint res;
    res.v=x;
    return res;
  }
  constexpr std::make_signed_t<value_type> val()const{return v;}
  constexpr modint &operator+=(const modint&b){
    this->v+=b.v;
    if(this->v>=umod)this->v-=umod;
    return *this;
  }
  constexpr modint &operator-=(const modint&b){
    this->v-=b.v;
    if(this->v>=umod)this->v+=umod;
    return *this;
  }
  constexpr modint &operator*=(const modint&b){
    this->v=take_mod(mul_type(this->v)*mul_type(b.v));
    return *this;
  }
  constexpr modint &operator/=(const modint&b){return *this*=b.inv();}
  constexpr modint operator+()const{return *this;}
  constexpr modint operator-()const{return modint()-*this;}
  friend constexpr modint operator+(const modint&a,const modint&b){return modint(a)+=b;}
  friend constexpr modint operator-(const modint&a,const modint&b){return modint(a)-=b;}
  friend constexpr modint operator*(const modint&a,const modint&b){return modint(a)*=b;}
  friend constexpr modint operator/(const modint&a,const modint&b){return modint(a)/=b;}
  constexpr auto operator<=>(const modint&)const=default;
  constexpr modint operator++(int){
    modint res=*this;
    this->v++;
    if(this->v==umod)this->v=0;
    return res;
  }
  constexpr modint operator--(int){
    modint res=*this;
    if(this->v==0)this->v=umod;
    this->v--;
    return res;
  }
  template<std::integral U>
  constexpr modint pow(U k)const{
    if constexpr(std::is_signed_v<U>){
      assert(0<=k);
    }
    modint res=1,a(*this);
    while(k){
      if(k&1)res*=a;
      a*=a;
      k>>=1;
    }
    return res;
  }
  constexpr modint inv()const{
    if constexpr(isprime_constexpr(umod)){
      if(std::is_constant_evaluated()){
        if(v==0){
          throw "no inverse";
        }
      }
      else assert(v!=0);
      return pow(umod-2);
    }
    else{
      modint res;
      auto [g,x]=inv_mod<std::make_signed_t<value_type>>(this->v,umod);
      if(std::is_constant_evaluated()){
        if(g!=1){
          throw "no inverse";
        }
      }
      else assert(g==1);
      res.v=x;
      return res;
    }
  }
  std::optional<modint>sqrt()const{
    if(this->val()<=1||this->pow((umod-1)/2)==1)return std::make_optional(this->sqrt_impl());
    else return std::nullopt;
  }
  friend std::istream &operator>>(std::istream&is,modint&b){
    long long a;
    is>>a;
    b=modint(a);
    return is;
  }
  friend std::ostream &operator<<(std::ostream&os,const modint&b){
    os<<b.val();
    return os;
  }
};
template<auto m>
struct std::hash<modint<m>>{
  std::size_t operator()(modint<m>x)const{
    return std::hash<typename modint<m>::value_type>(x.val());
  }
};
using mint998=modint<998244353>;
using mint107=modint<1000000007>;
using mint61=modint<2305843009213693951>;
using mint6124=modint<2305843009196916737>;
#define NTT_SIMD
#include<immintrin.h>
constexpr int factorize_constexpr(unsigned long long n,unsigned long long*a){
  if(n<=1)return 0;
  int ptr=0;
  while(n%2==0){
    a[ptr++]=2;
    n/=2;
  }
  while(n>1){
    if(isprime_constexpr(n)){
      a[ptr++]=n;
      break;
    }
    unsigned long long pf=n;
    unsigned long long x=0,y=0;
    int c=1;
    do{
      x=((__uint128_t)x*x+c)%pf;
      y=((__uint128_t)y*y+c)%pf;
      y=((__uint128_t)y*y+c)%pf;
      unsigned long long g=std::gcd(x>y?x-y:y-x,pf);
      if(g==pf){
        c++;
        x=y=0;
        continue;
      }
      if(g>1)pf=g;
    }while(!isprime_constexpr(pf));
    while(n%pf==0){
      a[ptr++]=pf;
      n/=pf;
    }
  }
  return ptr;
}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits<=32),T>pow_mod(T a,T n,T mod){
  using u64=unsigned long long;
  u64 res=1;
  while(n>0){
    if(n&1)res=((u64)res*a)%mod;
    a=((u64)a*a)%mod;
    n>>=1;
  }
  return T(res);
}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),T>pow_mod(T a,T n,T mod){
  using u128=__uint128_t;
  u128 res=1;
  while(n>0){
    if(n&1)res=((u128)res*a)%mod;
    a=((u128)a*a)%mod;
    n>>=1;
  }
  return T(res);
}
namespace Random{
constexpr unsigned long long to_seed(const char*s){
  unsigned long long h=14695981039346656037ULL;
  while(*s){
    h^=static_cast<unsigned char>(*s++);
    h*=1099511628211ULL;
  }
  return h;
}
constexpr unsigned long long constexpr_random_seed=(to_seed(__TIME__)*0x9e3779b97f4a7c15ULL)^to_seed(__DATE__);
constexpr unsigned long long next_value(unsigned long long n){
  n^=n<<13;
  n^=n>>7;
  n^=n<<17;
  return n;
}
}
constexpr unsigned long long primitive_root_constexpr(unsigned long long x){
  if(!isprime_constexpr(x))throw "not prime";
  if(x==167772161)return 3;
  if(x==469762049)return 3;
  if(x==754974721)return 11;
  if(x==880803841)return 26;
  if(x==998244353)return 3;
  if(x==2)return 1;
  unsigned long long a[64]={};
  int ptr=factorize_constexpr(x-1,a);
  for(unsigned long long v=Random::constexpr_random_seed;;){
    unsigned long long g=v%(x-1)+1;
    bool ok=true;
    for(int i=0;i<ptr;i++){
      if(i>0&&a[i-1]==a[i])continue;
      if(pow_mod<unsigned long long>(g,(x-1)/a[i],x)==1){
        ok=false;
        break;
      }
    }
    if(ok)return g;
    v=Random::next_value(v);
  }
}
#include<concepts>
template<typename T>
constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>msb(T n){return n==0?-1:31-__builtin_clz(n);}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>msb(T n){return n==0?-1:63-__builtin_clzll(n);}

template<typename T>
constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>lsb(T n){return n==0?-1:__builtin_ctz(n);}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>lsb(T n){return n==0?-1:__builtin_ctzll(n);}

template<typename T>
constexpr std::enable_if_t<std::is_integral_v<T>,T>floor_pow2(T n){return n==0?0:T(1)<<msb(n);}

template<typename T>
constexpr std::enable_if_t<std::is_integral_v<T>,T>ceil_pow2(T n){return n<=1?1:T(1)<<(msb(n-1)+1);}

template<std::integral T>
constexpr T safe_div(T a,T b){return a/b-(a%b&&(a^b)<0);}
template<std::integral T>
constexpr T safe_ceil(T a,T b){return a/b+(a%b&&(a^b)>0);}
template<auto m>
struct ntt_root{
  static_assert(1<=m&&m<(1ull<<63));
  static_assert(isprime_constexpr(m));
  using value_type=std::conditional_t<((m>>31)==0),uint32_t,uint64_t>;
  using mul_type=std::conditional_t<((m>>31)==0),uint64_t,__uint128_t>;
  static constexpr int rank2=lsb(m-1);
  static constexpr value_type g=primitive_root_constexpr(m);
  std::array<value_type,rank2+1>root,invroot;
  std::array<value_type,std::max(0,rank2-1)>rate2,invrate2;
  std::array<value_type,std::max(0,rank2-2)>rate3,invrate3;
  constexpr ntt_root(){
    root[rank2]=pow_mod<value_type>(g,m>>rank2,m);
    invroot[rank2]=pow_mod<value_type>(root[rank2],m-2,m);
    for(int i=rank2-1;i>=0;i--){
      root[i]=(mul_type)root[i+1]*root[i+1]%m;
      invroot[i]=(mul_type)invroot[i+1]*invroot[i+1]%m;
    }
    value_type prod=1,invprod=1;
    for(int i=0;i<rank2-1;i++){
      rate2[i]=(mul_type)root[i+2]*prod%m;
      invrate2[i]=(mul_type)invroot[i+2]*invprod%m;
      prod=(mul_type)prod*invroot[i+2]%m;
      invprod=(mul_type)invprod*root[i+2]%m;
    }
    prod=invprod=1;
    for(int i=0;i<rank2-2;i++){
      rate3[i]=(mul_type)root[i+3]*prod%m;
      invrate3[i]=(mul_type)invroot[i+3]*invprod%m;
      prod=(mul_type)prod*invroot[i+3]%m;
      invprod=(mul_type)invprod*root[i+3]%m;
    }
  }
};
template<unsigned m>
struct Montgomery{
  static_assert(m%2==1);
  static_assert(m>=3);
  static_assert(m<(1u<<30));
  static constexpr unsigned r=[](){
    unsigned res=m;
    for(int i=0;i<4;i++)res*=2-m*res;
    return res;
  }();
  static constexpr unsigned r2=-((unsigned long long)m)%m;
  static constexpr unsigned reduce(unsigned long long x){
    return (x+(unsigned long long)((unsigned)x*(unsigned)(-r))*m)>>32;
  }
  static constexpr unsigned mod(){return m;}
};
template<typename MM>
__attribute__((target("avx2")))
inline __m256i add_simd(__m256i x,__m256i y){
  x=_mm256_add_epi32(x,y);
  x=_mm256_sub_epi32(x,_mm256_set1_epi32(MM::mod()*2));
  __m256i mask=_mm256_cmpgt_epi32(_mm256_set1_epi32(0),x);
  x=_mm256_add_epi32(x,_mm256_and_si256(_mm256_set1_epi32(MM::mod()*2),mask));
  return x;
}
template<typename MM>
__attribute__((target("avx2")))
inline __m256i sub_simd(__m256i x,__m256i y){
  x=_mm256_sub_epi32(x,y);
  __m256i mask=_mm256_cmpgt_epi32(_mm256_set1_epi32(0),x);
  x=_mm256_add_epi32(x,_mm256_and_si256(_mm256_set1_epi32(MM::mod()*2),mask));
  return x;
}
template<typename MM>
__attribute__((target("avx2")))
inline void mul_simd(__m256i&x,int y){
  __m256i x0246=_mm256_mul_epu32(x,_mm256_set1_epi32(y));
  __m256i x0246_2=_mm256_mul_epu32(x0246,_mm256_set1_epi32(-MM::r));
  x0246_2=_mm256_mul_epu32(x0246_2,_mm256_set1_epi32(MM::mod()));
  x0246=_mm256_add_epi64(x0246,x0246_2);
  __m256i x1357=_mm256_mul_epu32(_mm256_srli_epi64(x,32),_mm256_set1_epi32(y));
  __m256i x1357_2=_mm256_mul_epu32(x1357,_mm256_set1_epi32(-MM::r));
  x1357_2=_mm256_mul_epu32(x1357_2,_mm256_set1_epi32(MM::mod()));
  x1357=_mm256_add_epi64(x1357,x1357_2);
  x=_mm256_blend_epi32(_mm256_srli_epi64(x0246,32),x1357,0b10101010);
}
template<typename MM>
__attribute__((target("avx2")))
inline void mul_simd(__m256i&x,const __m256i&y){
  __m256i x0246=_mm256_mul_epu32(x,y);
  __m256i x0246_2=_mm256_mul_epu32(x0246,_mm256_set1_epi32(-MM::r));
  x0246_2=_mm256_mul_epu32(x0246_2,_mm256_set1_epi32(MM::mod()));
  x0246=_mm256_add_epi64(x0246,x0246_2);
  __m256i x1357=_mm256_mul_epu32(_mm256_srli_epi64(x,32),_mm256_srli_epi64(y,32));
  __m256i x1357_2=_mm256_mul_epu32(x1357,_mm256_set1_epi32(-MM::r));
  x1357_2=_mm256_mul_epu32(x1357_2,_mm256_set1_epi32(MM::mod()));
  x1357=_mm256_add_epi64(x1357,x1357_2);
  x=_mm256_blend_epi32(_mm256_srli_epi64(x0246,32),x1357,0b10101010);
}
namespace ntt_simd_impl{
alignas(32) unsigned b[1<<24];
template<typename T>
__attribute__((target("avx2")))
void dft_simd(std::vector<T>&a){
  using MM=Montgomery<T::mod()>;
  static constexpr ntt_root<T::mod()>r;
  static constexpr std::array<unsigned,r.rate2.size()>rate2_m=[](){
    std::array<unsigned,r.rate2.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.rate2[i]*MM::r2);
    return res;
  }();
  static constexpr std::array<unsigned,r.rate3.size()>rate3_m=[](){
    std::array<unsigned,r.rate3.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.rate3[i]*MM::r2);
    return res;
  }();
  alignas(32) static constexpr std::array<unsigned,(r.rank2-3)*8>rate4_m=[](){
    std::array<unsigned,(r.rank2-3)*8>res;
    unsigned prod=1;
    for(int i=0;i<=r.rank2-4;i++){
      unsigned v=(unsigned long long)r.root[i+4]*prod%MM::mod();
      v=MM::reduce((unsigned long long)v*MM::r2);
      res[i*8]=MM::reduce(MM::r2);
      for(int j=1;j<8;j++)res[i*8+j]=MM::reduce((unsigned long long)res[i*8+j-1]*v);
      prod=(unsigned long long)prod*r.invroot[i+4]%MM::mod();
    }
    return res;
  }();
  int h=lsb(a.size());
  int len=0;
  for(int i=0;i<(int)a.size();i++)b[i]=a[i].val();
  for(int i=0;i<(int)a.size();i+=8){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+i));
    mul_simd<MM>(u,MM::r2);
    _mm256_storeu_si256((__m256i*)(b+i),u);
  }
  while(len+4<h){
    int p=1<<(h-len-2);
    unsigned rot=rate3_m[0],imag=MM::reduce((unsigned long long)r.root[2]*MM::r2);
    {
      for(int i=0;i<p;i+=8){
        __m256i b0=_mm256_loadu_si256((__m256i*)(b+i));
        __m256i b1=_mm256_loadu_si256((__m256i*)(b+i+p));
        __m256i b2=_mm256_loadu_si256((__m256i*)(b+i+p*2));
        __m256i b3=_mm256_loadu_si256((__m256i*)(b+i+p*3));
        __m256i m=sub_simd<MM>(b1,b3);
        mul_simd<MM>(m,imag);
        __m256i b02=add_simd<MM>(b0,b2);
        __m256i b0n2=sub_simd<MM>(b0,b2);
        __m256i b13=add_simd<MM>(b1,b3);
        _mm256_storeu_si256((__m256i*)(b+i),add_simd<MM>(b02,b13));
        _mm256_storeu_si256((__m256i*)(b+i+p),sub_simd<MM>(b02,b13));
        _mm256_storeu_si256((__m256i*)(b+i+p*2),add_simd<MM>(b0n2,m));
        _mm256_storeu_si256((__m256i*)(b+i+p*3),sub_simd<MM>(b0n2,m));
      }
    }
    for(int s=1;s<(1<<len);s++){
      unsigned rot2=MM::reduce((unsigned long long)rot*rot);
      unsigned rot3=MM::reduce((unsigned long long)rot2*rot);
      int offset=s<<(h-len);
      for(int i=0;i<p;i+=8){
        __m256i b0=_mm256_loadu_si256((__m256i*)(b+offset+i));
        __m256i b1=_mm256_loadu_si256((__m256i*)(b+offset+i+p));
        mul_simd<MM>(b1,rot);
        __m256i b2=_mm256_loadu_si256((__m256i*)(b+offset+i+p*2));
        mul_simd<MM>(b2,rot2);
        __m256i b3=_mm256_loadu_si256((__m256i*)(b+offset+i+p*3));
        mul_simd<MM>(b3,rot3);
        __m256i m=sub_simd<MM>(b1,b3);
        mul_simd<MM>(m,imag);
        __m256i b02=add_simd<MM>(b0,b2);
        __m256i b0n2=sub_simd<MM>(b0,b2);
        __m256i b13=add_simd<MM>(b1,b3);
        _mm256_storeu_si256((__m256i*)(b+offset+i),add_simd<MM>(b02,b13));
        _mm256_storeu_si256((__m256i*)(b+offset+i+p),sub_simd<MM>(b02,b13));
        _mm256_storeu_si256((__m256i*)(b+offset+i+p*2),add_simd<MM>(b0n2,m));
        _mm256_storeu_si256((__m256i*)(b+offset+i+p*3),sub_simd<MM>(b0n2,m));
      }
      rot=MM::reduce((unsigned long long)rot*rate3_m[lsb(~(unsigned)s)]);
    }
    len+=2;
  }
  if(len+4==h){
    unsigned rot=MM::reduce(MM::r2);
    for(int s=0;s<(1<<len);s++){
      int offset=s<<(h-len);
      __m256i b0=_mm256_loadu_si256((__m256i*)(b+offset));
      __m256i b1=_mm256_loadu_si256((__m256i*)(b+offset+8));
      mul_simd<MM>(b1,rot);
      _mm256_storeu_si256((__m256i*)(b+offset),add_simd<MM>(b0,b1));
      _mm256_storeu_si256((__m256i*)(b+offset+8),sub_simd<MM>(b0,b1));
      rot=MM::reduce((unsigned long long)rot*rate2_m[lsb(~(unsigned)s)]);
    }
  }
  unsigned one=MM::reduce(MM::r2);
  unsigned r2=MM::reduce((unsigned long long)r.root[2]*MM::r2);
  unsigned r3=MM::reduce((unsigned long long)r.root[3]*MM::r2);
  unsigned r3_2=MM::reduce((unsigned long long)r3*r3);
  unsigned r3_3=MM::reduce((unsigned long long)r3_2*r3);
  __m256i p1=_mm256_set_epi32(r3_3,r3_2,r3,one,one,one,one,one);
  __m256i p2=_mm256_set_epi32(r2,one,one,one,r2,one,one,one);
  __m256i rot=_mm256_set1_epi32(one);
  for(int s=0;s<(int)a.size()/8;s++){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+s*8));
    mul_simd<MM>(u,rot);
    __m256i v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11110000);
    v=_mm256_permute2x128_si256(v,v,0b01);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p1);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11001100);
    v=_mm256_shuffle_epi32(v,0b01001110);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p2);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b10101010);
    v=_mm256_shuffle_epi32(v,0b10110001);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,1);
    _mm256_storeu_si256((__m256i*)(b+s*8),u);
    for(int i=0;i<8;i++){
      if(b[s*8+i]>=T::mod())b[s*8+i]-=T::mod();
      a[s*8+i]=T::raw(b[s*8+i]);
    }
    mul_simd<MM>(rot,_mm256_loadu_si256((__m256i*)(rate4_m.data()+8*lsb(~(unsigned)s))));
  }
}
template<typename T>
__attribute__((target("avx2")))
void idft_simd(std::vector<T>&a){
  using MM=Montgomery<T::mod()>;
  static constexpr ntt_root<T::mod()>r;
  static constexpr std::array<unsigned,r.invrate3.size()>invrate3_m=[](){
    std::array<unsigned,r.invrate3.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.invrate3[i]*MM::r2);
    return res;
  }();
  alignas(32) static constexpr std::array<unsigned,(r.rank2-3)*8>invrate4_m=[](){
    std::array<unsigned,(r.rank2-3)*8>res;
    unsigned prod=1;
    for(int i=0;i<=r.rank2-4;i++){
      unsigned v=(unsigned long long)r.invroot[i+4]*prod%MM::mod();
      v=MM::reduce((unsigned long long)v*MM::r2);
      res[i*8]=MM::reduce(MM::r2);
      for(int j=1;j<8;j++)res[i*8+j]=MM::reduce((unsigned long long)res[i*8+j-1]*v);
      prod=(unsigned long long)prod*r.root[i+4]%MM::mod();
    }
    return res;
  }();
  int h=lsb(a.size());
  int len=h;
  for(int i=0;i<(int)a.size();i++)b[i]=a[i].val();
  unsigned one=MM::reduce(MM::r2);
  unsigned r2=MM::reduce((unsigned long long)r.invroot[2]*MM::r2);
  unsigned r3=MM::reduce((unsigned long long)r.invroot[3]*MM::r2);
  unsigned r3_2=MM::reduce((unsigned long long)r3*r3);
  unsigned r3_3=MM::reduce((unsigned long long)r3_2*r3);
  __m256i p1=_mm256_set_epi32(r3_3,r3_2,r3,one,one,one,one,one);
  __m256i p2=_mm256_set_epi32(r2,one,one,one,r2,one,one,one);
  __m256i rot=_mm256_set1_epi32(one);
  for(int s=0;s<(int)a.size()/8;s++){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+s*8));
    mul_simd<MM>(u,MM::r2);
    __m256i v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b10101010);
    v=_mm256_shuffle_epi32(v,0b10110001);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p2);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11001100);
    v=_mm256_shuffle_epi32(v,0b01001110);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p1);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11110000);
    v=_mm256_permute2x128_si256(v,v,0b01);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,rot);
    _mm256_storeu_si256((__m256i*)(b+s*8),u);
    mul_simd<MM>(rot,_mm256_loadu_si256((__m256i*)(invrate4_m.data()+8*lsb(~(unsigned)s))));
  }
  len-=3;
  while(len>=2){
    int p=1<<(h-len);
    unsigned rot=invrate3_m[0],imag=MM::reduce((unsigned long long)r.invroot[2]*MM::r2);
    {
      for(int i=0;i<p;i+=8){
        __m256i b0=_mm256_loadu_si256((__m256i*)(b+i));
        __m256i b1=_mm256_loadu_si256((__m256i*)(b+i+p));
        __m256i b2=_mm256_loadu_si256((__m256i*)(b+i+p*2));
        __m256i b3=_mm256_loadu_si256((__m256i*)(b+i+p*3));
        __m256i b01=add_simd<MM>(b0,b1);
        __m256i b0n1=sub_simd<MM>(b0,b1);
        __m256i b23=add_simd<MM>(b2,b3);
        __m256i k=sub_simd<MM>(b2,b3);
        mul_simd<MM>(k,imag);
        _mm256_storeu_si256((__m256i*)(b+i),add_simd<MM>(b01,b23));
        b1=add_simd<MM>(b0n1,k);
        _mm256_storeu_si256((__m256i*)(b+i+p),b1);
        b2=sub_simd<MM>(b01,b23);
        _mm256_storeu_si256((__m256i*)(b+i+p*2),b2);
        b3=sub_simd<MM>(b0n1,k);
        _mm256_storeu_si256((__m256i*)(b+i+p*3),b3);
      }
    }
    for(int s=1;s<(1<<(len-2));s++){
      int offset=s<<(h-len+2);
      unsigned rot2=MM::reduce((unsigned long long)rot*rot);
      unsigned rot3=MM::reduce((unsigned long long)rot2*rot);
      for(int i=0;i<p;i+=8){
        __m256i b0=_mm256_loadu_si256((__m256i*)(b+offset+i));
        __m256i b1=_mm256_loadu_si256((__m256i*)(b+offset+i+p));
        __m256i b2=_mm256_loadu_si256((__m256i*)(b+offset+i+p*2));
        __m256i b3=_mm256_loadu_si256((__m256i*)(b+offset+i+p*3));
        __m256i b01=add_simd<MM>(b0,b1);
        __m256i b0n1=sub_simd<MM>(b0,b1);
        __m256i b23=add_simd<MM>(b2,b3);
        __m256i k=sub_simd<MM>(b2,b3);
        mul_simd<MM>(k,imag);
        _mm256_storeu_si256((__m256i*)(b+offset+i),add_simd<MM>(b01,b23));
        b1=add_simd<MM>(b0n1,k);
        mul_simd<MM>(b1,rot);
        _mm256_storeu_si256((__m256i*)(b+offset+i+p),b1);
        b2=sub_simd<MM>(b01,b23);
        mul_simd<MM>(b2,rot2);
        _mm256_storeu_si256((__m256i*)(b+offset+i+p*2),b2);
        b3=sub_simd<MM>(b0n1,k);
        mul_simd<MM>(b3,rot3);
        _mm256_storeu_si256((__m256i*)(b+offset+i+p*3),b3);
      }
      rot=MM::reduce((unsigned long long)rot*invrate3_m[lsb(~(unsigned)s)]);
    }
    len-=2;
  }
  if(len==1){
    for(int i=0;i<(1<<(h-1));i+=8){
      __m256i u=_mm256_loadu_si256((__m256i*)(b+i));
      __m256i v=_mm256_loadu_si256((__m256i*)(b+(1<<(h-1))+i));
      _mm256_storeu_si256((__m256i*)(b+i),add_simd<MM>(u,v));
      _mm256_storeu_si256((__m256i*)(b+(1<<(h-1))+i),sub_simd<MM>(u,v));
    }
  }
  for(int i=0;i<(int)a.size();i+=8){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+i));
    mul_simd<MM>(u,1);
    _mm256_storeu_si256((__m256i*)(b+i),u);
    for(int j=0;j<8;j++){
      if(b[i+j]>=T::mod())b[i+j]-=T::mod();
      a[i+j]=T::raw(b[i+j]);
    }
  }
}
template<typename T>
__attribute__((target("avx2")))
void transposed_dft_simd(std::vector<T>&a){
  using MM=Montgomery<T::mod()>;
  static constexpr ntt_root<T::mod()>r;
  static constexpr std::array<unsigned,r.rate2.size()>rate2_m=[](){
    std::array<unsigned,r.rate2.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.rate2[i]*MM::r2);
    return res;
  }();
  static constexpr std::array<unsigned,r.rate3.size()>rate3_m=[](){
    std::array<unsigned,r.rate3.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.rate3[i]*MM::r2);
    return res;
  }();
  alignas(32) static constexpr std::array<unsigned,(r.rank2-3)*8>rate4_m=[](){
    std::array<unsigned,(r.rank2-3)*8>res;
    unsigned prod=1;
    for(int i=0;i<=r.rank2-4;i++){
      unsigned v=(unsigned long long)r.root[i+4]*prod%MM::mod();
      v=MM::reduce((unsigned long long)v*MM::r2);
      res[i*8]=MM::reduce(MM::r2);
      for(int j=1;j<8;j++)res[i*8+j]=MM::reduce((unsigned long long)res[i*8+j-1]*v);
      prod=(unsigned long long)prod*r.invroot[i+4]%MM::mod();
    }
    return res;
  }();
  for(int i=0;i<(int)a.size();i++)b[i]=a[i].val();
  int h=lsb(a.size());
  int len=h;
  unsigned one=MM::reduce(MM::r2);
  unsigned r2=MM::reduce((unsigned long long)r.root[2]*MM::r2);
  unsigned r3=MM::reduce((unsigned long long)r.root[3]*MM::r2);
  unsigned r3_2=MM::reduce((unsigned long long)r3*r3);
  unsigned r3_3=MM::reduce((unsigned long long)r3_2*r3);
  __m256i p1=_mm256_set_epi32(r3_3,r3_2,r3,one,one,one,one,one);
  __m256i p2=_mm256_set_epi32(r2,one,one,one,r2,one,one,one);
  __m256i rot=_mm256_set1_epi32(one);
  for(int s=0;s<(int)a.size()/8;s++){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+s*8));
    mul_simd<MM>(u,MM::r2);
    __m256i v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b10101010);
    v=_mm256_shuffle_epi32(v,0b10110001);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p2);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11001100);
    v=_mm256_shuffle_epi32(v,0b01001110);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p1);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11110000);
    v=_mm256_permute2x128_si256(v,v,0b01);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,rot);
    _mm256_storeu_si256((__m256i*)(b+s*8),u);
    mul_simd<MM>(rot,_mm256_loadu_si256((__m256i*)(rate4_m.data()+8*lsb(~(unsigned)s))));
  }
  len-=3;
  if(len%2!=0){
    unsigned rot=one;
    for(int s=0;s<(1<<(len-1));s++){
      int offset=s<<(h-len+1);
      __m256i b0=_mm256_loadu_si256((__m256i*)(b+offset));
      __m256i b1=_mm256_loadu_si256((__m256i*)(b+offset+8));
      _mm256_storeu_si256((__m256i*)(b+offset),add_simd<MM>(b0,b1));
      b0=sub_simd<MM>(b0,b1);
      mul_simd<MM>(b0,rot);
      _mm256_storeu_si256((__m256i*)(b+offset+8),b0);
      rot=MM::reduce((unsigned long long)rot*rate2_m[lsb(~(unsigned)s)]);
    }
    len--;
  }
  while(len>0){
    int p=1<<(h-len);
    unsigned rot=one,imag=MM::reduce((unsigned long long)r.root[2]*MM::r2);
    for(int s=0;s<(1<<(len-2));s++){
      const unsigned rot1=rot,rot2=MM::reduce((unsigned long long)rot*rot),rot3=MM::reduce((unsigned long long)rot*rot2);
      int offset=s<<(h-len+2);
      for(int i=0;i<p;i+=8){
        __m256i a0=_mm256_loadu_si256((__m256i*)(b+i+offset));
        __m256i a1=_mm256_loadu_si256((__m256i*)(b+i+offset+p));
        __m256i a2=_mm256_loadu_si256((__m256i*)(b+i+offset+p*2));
        __m256i a3=_mm256_loadu_si256((__m256i*)(b+i+offset+p*3));
        __m256i k=sub_simd<MM>(a2,a3);
        mul_simd<MM>(k,imag);
        __m256i a01=add_simd<MM>(a0,a1),a23=add_simd<MM>(a2,a3);
        _mm256_storeu_si256((__m256i*)(b+i+offset),add_simd<MM>(a01,a23));
        a2=sub_simd<MM>(a01,a23);
        mul_simd<MM>(a2,rot2);
        _mm256_storeu_si256((__m256i*)(b+i+offset+p*2),a2);
        a01=sub_simd<MM>(a0,a1);
        a1=add_simd<MM>(a01,k);
        a3=sub_simd<MM>(a01,k);
        mul_simd<MM>(a1,rot1);
        mul_simd<MM>(a3,rot3);
        _mm256_storeu_si256((__m256i*)(b+i+offset+p),a1);
        _mm256_storeu_si256((__m256i*)(b+i+offset+p*3),a3);
      }
      if(s+1!=1<<(len-2))rot=MM::reduce((unsigned long long)rot*rate3_m[lsb(~(unsigned)s)]);
    }
    len-=2;
  }
  for(int i=0;i<(int)a.size();i+=8){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+i));
    mul_simd<MM>(u,1);
    _mm256_storeu_si256((__m256i*)(b+i),u);
    for(int j=0;j<8;j++){
      if(b[i+j]>=T::mod())b[i+j]-=T::mod();
      a[i+j]=T::raw(b[i+j]);
    }
  }
}
template<typename T>
__attribute__((target("avx2")))
void transposed_idft_simd(std::vector<T>&a){
  using MM=Montgomery<T::mod()>;
  static constexpr ntt_root<T::mod()>r;
  static constexpr std::array<unsigned,r.invrate3.size()>invrate3_m=[](){
    std::array<unsigned,r.invrate3.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.invrate3[i]*MM::r2);
    return res;
  }();
  alignas(32) static constexpr std::array<unsigned,(r.rank2-3)*8>invrate4_m=[](){
    std::array<unsigned,(r.rank2-3)*8>res;
    unsigned prod=1;
    for(int i=0;i<=r.rank2-4;i++){
      unsigned v=(unsigned long long)r.invroot[i+4]*prod%MM::mod();
      v=MM::reduce((unsigned long long)v*MM::r2);
      res[i*8]=MM::reduce(MM::r2);
      for(int j=1;j<8;j++)res[i*8+j]=MM::reduce((unsigned long long)res[i*8+j-1]*v);
      prod=(unsigned long long)prod*r.root[i+4]%MM::mod();
    }
    return res;
  }();
  unsigned one=MM::reduce(MM::r2);
  for(int i=0;i<(int)a.size();i++)b[i]=MM::reduce((unsigned long long)a[i].val()*MM::r2);
  int h=lsb(a.size());
  int len=0;
  if(h%2==0){
    for(int i=0;i<(1<<(h-1));i+=8){
      __m256i u=_mm256_loadu_si256((__m256i*)(b+i));
      __m256i v=_mm256_loadu_si256((__m256i*)(b+(1<<(h-1))+i));
      _mm256_storeu_si256((__m256i*)(b+i),add_simd<MM>(u,v));
      _mm256_storeu_si256((__m256i*)(b+(1<<(h-1))+i),sub_simd<MM>(u,v));
    }
    len++;
  }
  while(len+3<h){
    int p=1<<(h-len-2);
    unsigned rot=one,imag=MM::reduce((unsigned long long)r.invroot[2]*MM::r2);
    for(int s=0;s<(1<<len);s++){
      const unsigned rot1=rot,rot2=MM::reduce((unsigned long long)rot*rot),rot3=MM::reduce((unsigned long long)rot*rot2);
      int offset=s<<(h-len);
      for(int i=0;i<p;i+=8){
        __m256i a0=_mm256_loadu_si256((__m256i*)(b+i+offset));
        __m256i a1=_mm256_loadu_si256((__m256i*)(b+i+offset+p));
        mul_simd<MM>(a1,rot1);
        __m256i a2=_mm256_loadu_si256((__m256i*)(b+i+offset+p*2));
        mul_simd<MM>(a2,rot2);
        __m256i a3=_mm256_loadu_si256((__m256i*)(b+i+offset+p*3));
        mul_simd<MM>(a3,rot3);
        __m256i k=sub_simd<MM>(a1,a3);
        mul_simd<MM>(k,imag);
        __m256i a02=add_simd<MM>(a0,a2),a13=add_simd<MM>(a1,a3);
        _mm256_storeu_si256((__m256i*)(b+i+offset),add_simd<MM>(a02,a13));
        _mm256_storeu_si256((__m256i*)(b+i+offset+p),sub_simd<MM>(a02,a13));
        a02=sub_simd<MM>(a0,a2);
        _mm256_storeu_si256((__m256i*)(b+i+offset+p*2),add_simd<MM>(a02,k));
        _mm256_storeu_si256((__m256i*)(b+i+offset+p*3),sub_simd<MM>(a02,k));
      }
      if(s+1!=1<<len)rot=MM::reduce((unsigned long long)rot*invrate3_m[lsb(~(unsigned)s)]);
    }
    len+=2;
  }
  unsigned r2=MM::reduce((unsigned long long)r.invroot[2]*MM::r2);
  unsigned r3=MM::reduce((unsigned long long)r.invroot[3]*MM::r2);
  unsigned r3_2=MM::reduce((unsigned long long)r3*r3);
  unsigned r3_3=MM::reduce((unsigned long long)r3_2*r3);
  __m256i p1=_mm256_set_epi32(r3_3,r3_2,r3,one,one,one,one,one);
  __m256i p2=_mm256_set_epi32(r2,one,one,one,r2,one,one,one);
  __m256i rot=_mm256_set1_epi32(one);
  for(int s=0;s<(int)a.size()/8;s++){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+s*8));
    mul_simd<MM>(u,rot);
    __m256i v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11110000);
    v=_mm256_permute2x128_si256(v,v,0b01);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p1);
      v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11001100);
    v=_mm256_shuffle_epi32(v,0b01001110);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p2);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b10101010);
    v=_mm256_shuffle_epi32(v,0b10110001);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,1);
    _mm256_storeu_si256((__m256i*)(b+s*8),u);
    for(int j=0;j<8;j++){
      if(b[s*8+j]>=T::mod())b[s*8+j]-=T::mod();
      a[s*8+j]=T::raw(b[s*8+j]);
    }
    mul_simd<MM>(rot,_mm256_loadu_si256((__m256i*)(invrate4_m.data()+8*lsb(~(unsigned)s))));
  }
}
}
using ntt_simd_impl::dft_simd;
using ntt_simd_impl::idft_simd;
using ntt_simd_impl::transposed_dft_simd;
using ntt_simd_impl::transposed_idft_simd;
template<typename T>
void dft(std::vector<T>&a){
  using value_type=typename T::value_type;
  using mul_type=typename T::mul_type;
  #ifdef NTT_SIMD
  if constexpr(std::numeric_limits<value_type>::digits<=32){
    if((int)a.size()>=32){
      dft_simd(a);
      return;
    }
  }
  #endif
  static constexpr ntt_root<T::mod()>r;
  static constexpr mul_type mod2=(mul_type)T::mod()*T::mod();
  int n=a.size();
  int h=lsb(n);
  int len=0;
  while(len<h){
    if(h-len==1){
      T rot=T::raw(1);
      for(int s=0;s<(1<<len);s++){
        int of=s*2;
        T u=a[of],v=a[of+1]*rot;
        a[of]=u+v;
        a[of+1]=u-v;
        rot*=T::raw(r.rate2[lsb(~(unsigned int)s)]);
      }
      len++;
    }
    else{
      int p=1<<(h-len-2);
      T rot=T::raw(1),imag=T::raw(r.root[2]);
      for(int s=0;s<(1<<len);s++){
        const mul_type rot1=rot.val(),rot2=(rot*rot).val(),rot3=(rot*T::raw(rot2)).val();
        int of=s<<(h-len);
        for(int i=0;i<p;i++){
          const mul_type a0=a[i+of].val(),a1=(mul_type)a[i+of+p].val()*rot1,a2=(mul_type)a[i+of+p*2].val()*rot2,a3=(mul_type)a[i+of+p*3].val()*rot3;
          const mul_type m=(mul_type)T(a1+mod2-a3).val()*imag.val();
          const mul_type k=mod2-a2;
          a[i+of]=a0+a2+a1+a3;
          a[i+of+p]=a0+a2+(mod2*2-a1-a3);
          a[i+of+p*2]=a0+k+m;
          a[i+of+p*3]=a0+k+(mod2-m);
        }
        rot*=T::raw(r.rate3[lsb(~(unsigned int)s)]);
      }
      len+=2;
    }
  }
}
template<typename T>
void idft(std::vector<T>&a){
  using value_type=typename T::value_type;
  using mul_type=typename T::mul_type;
  #ifdef NTT_SIMD
  if constexpr(std::numeric_limits<value_type>::digits<=32){
    if((int)a.size()>=32){
      idft_simd(a);
      return;
    }
  }
  #endif
  static constexpr ntt_root<T::mod()>r;
  int n=a.size();
  int h=lsb(n);
  int len=h;
  while(len){
    if(len==1){
      int p=1<<(h-1);
      for(int i=0;i<p;i++){
        T u=a[i],v=a[i+p];
        a[i]=u+v;
        a[i+p]=u-v;
      }
      len--;
    }
    else{
      int p=1<<(h-len);
      T rot=T::raw(1),imag=T::raw(r.invroot[2]);
      for(int s=0;s<(1<<(len-2));s++){
        const mul_type rot1=rot.val(),rot2=(rot*rot).val(),rot3=(rot*T::raw(rot2)).val();
        int of=s<<(h-len+2);
        for(int i=0;i<p;i++){
          const mul_type a0=a[i+of].val(),a1=a[i+of+p].val(),a2=a[i+of+p*2].val(),a3=a[i+of+p*3].val();
          const mul_type k=T((T::mod()+a2-a3)*imag.val()).val();
          a[i+of]=a0+a1+a2+a3;
          a[i+of+p]=(a0+T::mod()-a1+k)*rot1;
          a[i+of+p*2]=(a0+a1+T::mod()*2-a2-a3)*rot2;
          a[i+of+p*3]=(a0+T::mod()*2-a1-k)*rot3;
        }
        rot*=T::raw(r.invrate3[lsb(~(unsigned int)s)]);
      }
      len-=2;
    }
  }
}
template<typename T>
std::vector<T>ntt_convolution(std::vector<T> a,std::vector<T> b){
  int n=a.size(),m=b.size(),s=n+m-1;
  if(std::min(n,m)<60){
    if(n==0||m==0)return {};
    std::vector<T>ret(s,0);
    if(n<m)for(int i=0;i<m;i++)for(int j=0;j<n;j++)ret[i+j]+=a[j]*b[i];
    else for(int i=0;i<n;i++)for(int j=0;j<m;j++)ret[i+j]+=a[i]*b[j];
    return ret;
  }
  int z=ceil_pow2(s);
  a.resize(z,0);
  b.resize(z,0);
  dft(a),dft(b);
  std::vector<T>c(z);
  for(int i=0;i<z;i++)c[i]=a[i]*b[i];
  idft(c);
  T g=T::raw(z).inv();
  for(int i=0;i<s;i++)c[i]*=g;
  return {c.begin(),c.begin()+s};
}
using mint=modint<469762049>;
void SOLVE(){
  int x,y,z;
  cin>>x>>y>>z;
  swap(x,z);
  vector<vector<string>>s(x*2,vector<string>(y*2,string(z*2,'?')));
  rep(i,x)rep(j,y){
    cin>>s[i][j];
    s[i][j].resize(z*2);
  }
  rep(i,x)rep(j,y)rep(k,z){
    rep(bit,1,8){
      s[i+(bit&1)*x][j+(bit>>1&1)*y][k+(bit>>2&1)*z]=s[i][j][k];
    }
  }
  debug(s);
  vector<vector<string>>t(x,vector<string>(y));
  rep(i,x)rep(j,y)cin>>t[i][j];
  int ans=1e9;
  vector<vector<vector<int>>>val(x,vector<vector<int>>(y,vector<int>(z)));
  {
    vector<mint>f(18*x*y*z),g(9*x*y*z);
    rep(i,x*2)rep(j,y*2)rep(k,z*2){
      if(s[i][j][k]=='B'){
        f[i*(y*z*9)+j*(z*3)+k]=1;
      }
    }
    rep(i,x)rep(j,y)rep(k,z){
      if(t[i][j][k]=='W'){
        g[(x-i-1)*(y*z*9)+(y-j-1)*(z*3)+k]=1;
      }
    }
    f=ntt_convolution(f,g);
    rep(i,x)rep(j,y)rep(k,z){
      val[i][j][k]+=f[(x-1+i)*y*z*9+(y-1+j)*z*3+(z-1+k)].val();
    }
  }
  {
    vector<mint>f(18*x*y*z),g(9*x*y*z);
    rep(i,x*2)rep(j,y*2)rep(k,z*2){
      if(s[i][j][k]=='W'){
        f[i*(y*z*9)+j*(z*3)+k]=1;
      }
    }
    rep(i,x)rep(j,y)rep(k,z){
      if(t[i][j][k]=='B'){
        g[(x-i-1)*(y*z*9)+(y-j-1)*(z*3)+k]=1;
      }
    }
    f=ntt_convolution(f,g);
    rep(i,x)rep(j,y)rep(k,z){
      val[i][j][k]+=f[(x-1+i)*y*z*9+(y-1+j)*z*3+(z-1+k)].val();
    }
  }
  rep(i,x)rep(j,y)rep(k,z){
    int cnt=0;
    cnt=val[i][j][k];
    // rep(i2,x)rep(j2,y)rep(k2,z){
    //   if(s[i+i2][j+j2][k+k2]=='B'&&t[i2][j2][k2]=='W')cnt++;
    //   if(s[i+i2][j+j2][k+k2]=='W'&&t[i2][j2][k2]=='B')cnt++;
    // }
    chmin(ans,cnt);
  }
  cout<<ans<<endl;
}
0