結果

問題 No.344 ある無理数の累乗
ユーザー eQe
提出日時 2025-05-05 12:31:17
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 12,580 bytes
コンパイル時間 7,057 ms
コンパイル使用メモリ 334,376 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-05 12:31:26
合計ジャッジ時間 8,230 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
namespace my{
#define eb emplace_back
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define jo(a,b) a##b
#define FO_IMPL(n,c) for(ll jo(_i,c)=n;jo(_i,c)-->0;)
#define FO(n) FO_IMPL(n,__COUNTER__)
#define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__))
#define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a)
#define maybe(p,c) (p?c:remove_cvref_t<decltype(c)>{})
#define base_operator(op,type) auto operator op(const type&v)const{auto copy=*this;return copy op##=v;}
#define entry void solve();void solve2();}int main(){my::io();my::solve();}namespace my{
#define use_ml using ml=atcoder::modint;
auto&operator>>(istream&i,atcoder::modint&x){int t;i>>t;x=t;return i;}
auto&operator<<(ostream&o,const atcoder::modint&x){return o<<(int)x.val();}
auto&operator>>(istream&i,atcoder::modint998244353&x){int t;i>>t;x=t;return i;}
auto&operator<<(ostream&o,const atcoder::modint998244353&x){return o<<(int)x.val();}
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
constexpr auto range(ll s,ll b){ll a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};}
constexpr auto range(ll s,ll a,ll b,ll c=1){return array{a-s,b,(1-s*2)*c};}
const string newline{char(10)};
const string space{char(32)};
constexpr auto even(auto x){return~x&1;}
auto max(auto...a){return max(initializer_list<common_type_t<decltype(a)...>>{a...});}
auto min(auto...a){return min(initializer_list<common_type_t<decltype(a)...>>{a...});}
auto mod(auto a,auto b){return(a%=b)<0?a+b:a;}

template<class A,class B>struct pair{
  A a;B b;
  pair()=default;
  pair(A a,B b):a(a),b(b){}
  pair(const std::pair<A,B>&p):a(p.first),b(p.second){}
  auto operator<=>(const pair&)const=default;
  pair operator+(const pair&p)const{return{a+p.a,b+p.b};}
  friend istream&operator>>(istream&i,pair&p){return i>>p.a>>p.b;}
  friend ostream&operator<<(ostream&o,const pair&p){return o<<p.a<<space<<p.b;}
};

auto&rev(auto&a){ranges::reverse(a);return a;}

template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;

template<class V>concept vectorial=is_base_of_v<vector<typename remove_cvref_t<V>::value_type>,remove_cvref_t<V>>;
template<class V>constexpr int rank(){if constexpr(vectorial<V>)return rank<typename V::value_type>()+1;else return 0;}
template<class T>struct core_t_helper{using core_t=T;};
template<vectorial V>struct core_t_helper<V>{using core_t=typename core_t_helper<typename V::value_type>::core_t;};
template<class T>using core_t=core_t_helper<T>::core_t;
template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}
template<class T>ostream&operator<<(ostream&o,const vector<T>&v){ll n=v.size();fo(i,n)o<<v[i]<<maybe(i<n-1,space);return o;}
template<vectorial V>ostream&operator<<(ostream&o,const vector<V>&v){ll n=v.size();fo(i,n)o<<v[i]<<maybe(i<n-1,newline);return o;}

template<class V>struct vec;
template<int rank,class T>struct hvec_helper{using type=vec<typename hvec_helper<rank-1,T>::type>;};
template<class T>struct hvec_helper<0,T>{using type=T;};
template<int rank,class T>using hvec=typename hvec_helper<rank,T>::type;

template<class V>struct vec:vector<V>{
  static constexpr int R=rank<vec<V>>();
  using C=core_t<V>;
  using vector<V>::vector;
  vec(const vector<V>&v){vector<V>::operator=(v);}
  vec(const auto&...a)requires(sizeof...(a)>=3){resizes(a...);}
  void resizes(const auto&...a){*this=make(a...);}
  static auto make(ll n,const auto&...a){
    if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]);
    else return vec<decltype(make(a...))>(n,make(a...));
  }

  vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;}
  vec&operator+=(const vec&u){vec&v=*this;assert(v.size()==u.size());fo(i,v.size())v[i]+=u[i];return v;}
  vec&operator-=(const vec&u){vec&v=*this;assert(v.size()==u.size());fo(i,v.size())v[i]-=u[i];return v;}
  vec&operator+=(const C&c){fe(*this,e)e+=c;return*this;}
  vec&operator*=(const C&c){fe(*this,e)e*=c;return*this;}
  base_operator(^,vec)
  base_operator(+,vec)
  base_operator(-,vec)
  base_operator(+,C);
  base_operator(*,C);

  vec&operator++(){fe(*this,e)++e;return*this;}
  vec&operator--(){fe(*this,e)--e;return*this;}

  ll size()const{return vector<V>::size();}

  auto&emplace_back(auto&&...a){vector<V>::emplace_back(std::forward<decltype(a)>(a)...);return*this;}
  auto pop_back(){auto r=this->back();vector<V>::pop_back();return r;}

  auto scan(const auto&f)const{
    pair<C,bool>r{};
    fe(*this,e)if constexpr(!vectorial<V>)r.b?f(r.a,e),r:r={e,1};else if(auto s=e.scan(f);s.b)r.b?f(r.a,s.a),r:r=s;
    return r;
  }
  auto max()const{return scan([](auto&a,auto b){if(a<b)a=b;}).a;}
  auto min()const{return scan([](auto&a,auto b){if(b<a)a=b;}).a;}

  auto rev()const{vec v=*this;ranges::reverse(v);return v;}
  auto rev(ll l,ll r)const{vec v=*this;ranges::reverse(v.begin()+l,v.begin()+r);return v;}
};
template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>;
vec(ll)->vec<ll>;

void lin(auto&...a){(cin>>...>>a);}
auto sin(){string s;lin(s);return s;}

void pp(const auto&...a){ll n=sizeof...(a);((cout<<a<<maybe(--n>0,space)),...);cout<<newline;}

template<class T>concept modulary=requires(T t){t.mod();};

namespace fft{
using real=double;
struct complex{
  real x,y;
  complex()=default;
  complex(real x,real y):x(x),y(y){}
  inline complex operator+(const complex &c)const{return complex(x+c.x,y+c.y);}
  inline complex operator-(const complex &c)const{return complex(x-c.x,y-c.y);}
  inline complex operator*(const complex &c)const{return complex(x*c.x-y*c.y,x*c.y+y*c.x);}
  inline complex conj()const{return complex(x,-y);}
};

const real PI=acosl(-1);
ll base=1;
vector<complex>rts={{0,0},{1,0}};
vector<int>fft_rev={0,1};

void ensure_base(int nbase){
  if(nbase<=base)return;
  fft_rev.resize(1<<nbase);
  rts.resize(1<<nbase);
  fo(i,1<<nbase)fft_rev[i]=(fft_rev[i>>1]>>1)+((i&1)<<(nbase-1));
  while(base<nbase){
    real angle=PI*2.0/(1<<(base+1));
    fo(i,1<<(base-1),1<<base){
      rts[i<<1]=rts[i];
      real angle_i=angle*(2*i+1-(1<<base));
      rts[(i<<1)+1]=complex(std::cos(angle_i),std::sin(angle_i));
    }
    ++base;
  }
}

void fast_fourier_transform(vector<complex>&a,int n){
  assert((n&(n-1))==0);
  int zeros=__builtin_ctz(n);
  ensure_base(zeros);
  int shift=base-zeros;
  fo(i,n)if(i<(fft_rev[i]>>shift))swap(a[i],a[fft_rev[i]>>shift]);

  for(int k=1;k<n;k<<=1){
    for(int i=0;i<n;i+=2*k){
      for(int j=0;j<k;j++){
        complex z=a[i+j+k]*rts[j+k];
        a[i+j+k]=a[i+j]-z;
        a[i+j]=a[i+j]+z;
      }
    }
  }
}
}

template<class T>struct arbitrary_mod_convolution{
  using real=fft::real;
  using complex=fft::complex;
  arbitrary_mod_convolution(){}

  std::vector<T>multiply(const std::vector<T>&a,const std::vector<T>&b,int need=-1){
    if(need==-1)need=a.size()+b.size()-1;
    int nbase=0;
    while((1<<nbase)<need)nbase++;
    fft::ensure_base(nbase);
    int sz=1<<nbase;
    std::vector<complex>fa(sz);
    fo(i,a.size())fa[i]=complex(a[i].val()&((1<<15)-1),a[i].val()>>15);

    fft::fast_fourier_transform(fa,sz);
    std::vector<complex>fb(sz);
    if(a==b){
      fb=fa;
    }else{
      fo(i,b.size())fb[i]=complex(b[i].val()&((1<<15)-1),b[i].val()>>15);
      fft::fast_fourier_transform(fb,sz);
    }
    real ratio=0.25/sz;
    complex r2(0,-1),r3(ratio,0),r4(0,-ratio),r5(0,1);
    for(int i=0;i<=(sz>>1);i++){
      int j=(sz-i)&(sz-1);
      complex a1=(fa[i]+fa[j].conj());
      complex a2=(fa[i]-fa[j].conj())*r2;
      complex b1=(fb[i]+fb[j].conj())*r3;
      complex b2=(fb[i]-fb[j].conj())*r4;
      if(i!=j){
        complex c1=(fa[j]+fa[i].conj());
        complex c2=(fa[j]-fa[i].conj())*r2;
        complex d1=(fb[j]+fb[i].conj())*r3;
        complex d2=(fb[j]-fb[i].conj())*r4;
        fa[i]=c1*d1+c2*d2*r5;
        fb[i]=c1*d2+c2*d1;
      }
      fa[j]=a1*b1+a2*b2*r5;
      fb[j]=a1*b2+a2*b1;
    }
    fft::fast_fourier_transform(fa,sz);
    fft::fast_fourier_transform(fb,sz);
    std::vector<T>ret(need);
    fo(i,need){
      int64_t aa=llround(fa[i].x);
      int64_t bb=llround(fb[i].x);
      int64_t cc=llround(fa[i].y);
      aa=T(aa).val(),bb=T(bb).val(),cc=T(cc).val();
      ret[i]=aa+(bb<<15)+(cc<<30);
    }
    return ret;
  }
};

template<class T>struct formal_power_series:vec<T>{
  using vec<T>::vec;
  using fps=formal_power_series;

  static constexpr ll SPARSE_THRESHOLD=20;

  static inline arbitrary_mod_convolution<T>fft;
  static fps mul(const fps&a,const fps&b){
    if constexpr(!modulary<T>)return atcoder::convolution_ll(a,b);
    else if constexpr(is_same_v<T,atcoder::modint998244353>)return atcoder::convolution(a,b);
    else return fft.multiply(a,b);
  }

  auto operator<=>(const fps&f)const{return this->size()<=>f.size();}

  auto&shrink(){while(this->size()>1&&this->back()==T{})this->pop_back();return*this;}
  fps pre(ll deg)const{fps r(this->begin(),this->begin()+min(this->size(),deg));r.resize(deg);return r;}

  fps&operator+=(const fps&g){if(g.size()>this->size())this->resize(g.size());fo(i,g.size())(*this)[i]+=g[i];return*this;}
  fps&operator-=(const fps&g){if(g.size()>this->size())this->resize(g.size());fo(i,g.size())(*this)[i]-=g[i];return*this;}
  fps&operator*=(const fps&g){return*this=(this->size()&&g.size()?mul(*this,g):fps{});}

  fps&operator>>=(ll sz){if(this->size()<=sz)return*this=fps{};this->erase(this->begin(),this->begin()+sz);return*this;}
  fps&operator<<=(ll sz){this->insert(this->begin(),sz,T{});return*this;}

  fps&operator/=(const fps&g){
    ll I1=0,I2=0;
    while(I1<this->size()&&(*this)[I1]==0)++I1;
    while(I2<g.size()&&g[I2]==0)++I2;
    assert(I1>=I2);
    ll L=max(this->size(),g.size());
    return*this=((*this>>I2)*(g>>I2).inv(L)).pre(L);
  }

  base_operator(+,fps)
  base_operator(-,fps)
  base_operator(*,fps)
  base_operator(/,fps)
  fps operator-()const{auto r=*this;fe(r,x)x=-x;return r;}
  fps operator>>(ll sz)const{return fps{*this}>>=sz;}
  fps operator<<(ll sz)const{return fps{*this}<<=sz;}

  fps&operator+=(const T&c){if(!this->size())this->resize(1);(*this)[0]+=c;return*this;}
  fps&operator-=(const T&c){if(!this->size())this->resize(1);(*this)[0]-=c;return*this;}
  fps&operator*=(const T&c){fo(i,this->size())(*this)[i]*=c;return*this;}
  fps&operator/=(const T&c){T c_inv=T{1}/c;fo(i,this->size())(*this)[i]*=c_inv;return*this;}
  base_operator(+,T)
  base_operator(-,T)
  base_operator(*,T)
  base_operator(/,T)

  T operator()(T x)const{T r=0,xi=1;fe(*this,ai)r+=ai*xi,xi*=x;return r;}

  fps inv_sparse(ll deg=-1)const{
    assert((*this)[0]!=T{});
    ll n=this->size();
    if(deg==-1)deg=n;
    vec<pair<ll,T>>p;
    fo(i,1,n)if((*this)[i]!=T{})p.eb(i,(*this)[i]);
    fps r(deg);
    r[0]=T{1}/(*this)[0];
    fo(i,1,deg){
      T t{};
      fe(p,k,fk){
        if(i-k<0)break;
        t-=fk*r[i-k];
      }
      r[i]=r[0]*t;
    }
    return r;
  }

  ll nonzero_terms_count()const{ll r=0;fe(*this,e)r+=(e!=T{});return r;}

  fps inv(ll deg=-1)const{
    assert((*this)[0]!=T{});
    if(deg==-1)deg=this->size();
    if(nonzero_terms_count()<SPARSE_THRESHOLD)return inv_sparse(deg);
    fps r{T{1}/(*this)[0]};
    for(ll i=1;i<deg;i<<=1)r=(r*2-this->pre(i<<1)*(r*r)).pre(i<<1);
    return r.pre(deg);
  }

  fps rev()const{fps r{*this};ranges::reverse(r);return r;}

  static auto polynomial_division(const fps&f,const fps&g){
    ll n=f.size()-g.size()+1;
    fps q=n>0?(f.rev().pre(n)*g.rev().inv(n)).pre(n).rev():fps{};
    fps r=(f-g*q).shrink();
    return pair{q,r};
  }
};
template<class T>using fps=formal_power_series<T>;
template<class T>T bostan_mori(ll n,fps<T>P,fps<T>Q){
  if(P.shrink().size()>=Q.shrink().size()){
    auto[q,r]=fps<T>::polynomial_division(P,Q);
    return(n<q.size()?q[n]:0)+bostan_mori(n,r,Q);
  }

  P.resize(Q.size());
  while(n){
    fps<T>Qsym=Q;
    fo(i,1,Qsym.size(),2)Qsym[i]=-Qsym[i];

    fps<T>PQ=P*Qsym;
    fps<T>QQ=Q*Qsym;
    bool f=n&1;
    fo(i,f,PQ.size(),2)P[i>>1]=PQ[i];
    fo(i,0,QQ.size(),2)Q[i>>1]=QQ[i];
    n>>=1;
  }
  return P[0]/Q[0];
}

template<class T>T kth_linear_recurrence_relation(ll n,const vec<T>&a,const vec<T>&c){
  assert(a.size()==c.size());
  fps<T>Q=(-fps<T>(c)<<1)+1;
  fps<T>P=(Q*a).pre(a.size());
  return bostan_mori(n,P,Q);
}

entry
void solve(){
  LL(N);
  use_ml
  ml::set_mod(1000);
  ml XN=kth_linear_recurrence_relation(N,vec<ml>{2,2},vec<ml>{2,2});
  pp(XN-even(N));
}}
0