結果

問題 No.1287 えぬけー
ユーザー eQeeQe
提出日時 2024-09-04 02:47:33
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 11,974 bytes
コンパイル時間 6,349 ms
コンパイル使用メモリ 336,656 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-09-04 02:47:44
合計ジャッジ時間 10,536 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
13,756 KB
testcase_01 AC 46 ms
6,940 KB
testcase_02 AC 49 ms
6,940 KB
testcase_03 AC 76 ms
6,940 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
namespace my{
using namespace atcoder;
using ml=modint998244353;
auto&operator<<(std::ostream&o,const ml&x){return o<<x.val();}
auto&operator>>(std::istream&i,ml&x){long long t;i>>t;x=t;return i;}
void main();void solve();
}
int main(){my::main();}
namespace my{
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define RD(T,...) T __VA_ARGS__;li(__VA_ARGS__)
#define LL(...) RD(ll,__VA_ARGS__)
#define JO(a,b) a##b
#define jo(a,b) JO(a,b)
#define FO(n) for(ll jo(i,__LINE__)=n;jo(i,__LINE__)-->0;)
#define FOR(i,...) for(auto[i,i##O,i##E]=range(0,__VA_ARGS__);i<i##O;i+=i##E)
#define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__))
#define of(i,...) for(auto[i,i##O,i##E]=range(1,__VA_ARGS__);i>=i##O;i-=i##E)
#define fe(v,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):v)
#define I template
#define J class
using namespace std;
I<J A,J B>constexpr bool same=is_same_v<A,B>;
using is=istream;using os=ostream;using dd=long double;using str=string;
using ll=long long;using ull=unsigned long long;using lll=__int128_t;using ulll=__uint128_t;
os&operator<<(os&o,const ulll&x){return(x<10?o:o<<x/10)<<ll(x%10);}
os&operator<<(os&o,const lll&x){return o<<str(x<0,'-')<<ulll(x>0?x:-x);}
constexpr dd ee=1e-12;
constexpr ll oo=3e18;
constexpr ll dx[]{-1,0,1,0,-1,1,1,-1},dy[]{0,-1,0,1,-1,-1,1,1};
constexpr char sp=32,nl=10;
auto Yes(bool y){return y?"Yes":"No";}
auto No(){return Yes(0);}
auto range(bool s,ll a,ll b=oo,ll c=1){if(b==oo)b=a,(s?b:a)=0;return tuple{a-s,b,c};}
ll rand(ll l=oo,ll r=oo){static ull a=495;a^=a<<7,a^=a>>9;return r!=oo?a%(r-l)+l:l!=oo?a%l:a;}
bool eve(ll x){return~x&1;}
lll pw(lll x,lll n,ll m=0){lll r=1;while(n)n&1?r*=x:r,x*=x,m?r%=m,x%=m:r,n>>=1;return r;}
I<J T>T sq(T a){return a*a;}
I<J T>T zz(T x){return x<0?-x:x;}
I<J T>ll len(const T&a){return a.size();}
I<J...A>auto min(const A&...a){return min(initializer_list<common_type_t<A...>>{a...});}

I<J A,J B=A>struct cp{
  A a={};B b={};
  cp(){}
  cp(A a,B b):a(a),b(b){}
  cp(pair<A,B>p):a(p.first),b(p.second){}
  bool operator==(const cp&c)const{return a==c.a&&b==c.b;}
  auto operator<=>(const cp&c)const{return a!=c.a?a<=>c.a:b<=>c.b;}
  friend is&operator>>(is&i,cp&c){return i>>c.a>>c.b;}
  friend os&operator<<(os&o,const cp&c){return o<<c.a<<sp<<c.b;}
};using cl=cp<ll>;
I<J A,J B=A,J C=A>struct tr{
  A a={};B b={};C c={};
  tr(){}
  tr(A a,B b,C c):a(a),b(b),c(c){}
  bool operator==(const tr&t)const{return a==t.a&&b==t.b&&c==t.c;}
  auto operator<=>(const tr&t)const{return a!=t.a?a<=>t.a:b!=t.b?b<=>t.b:c<=>t.c;}
  friend is&operator>>(is&i,tr&t){return i>>t.a>>t.b>>t.c;}
  friend os&operator<<(os&o,const tr&t){return o<<t.a<<sp<<t.b<<sp<<t.c;}
};using tl=tr<ll>;

I<J T>T&sort(T&a){sort(all(a));return a;}
I<J T>decltype(auto)first(T&a){return*begin(a);}
I<J T>decltype(auto)last(T&a){return*rbegin(a);}
I<J T>auto pop_front(T&a){assert(len(a));auto r=first(a);a.pop_front();return r;}
I<J T>auto pop_back(T&a){assert(len(a));auto r=last(a);a.pop_back();return r;}

I<J T,size_t n>using array=std::array<T,n>;
I<J T,size_t n>os&operator<<(os&o,const array<T,n>&a){fo(i,n)o<<a[i]<<str(i!=n-1,sp);return o;}
I<J T,J U=T>using map=std::map<T,U>;
I<J T,J U>os&operator<<(os&o,const map<T,U>&m){fe(m,e)o<<e.first<<sp<<e.second<<nl;return o;}
I<J T,J U=T>using umap=unordered_map<T,U>;
I<J T,J U>os&operator<<(os&o,const umap<T,U>&m){fe(m,e)o<<e.first<<sp<<e.second<<nl;return o;}
I<size_t n>using bset=bitset<n>;
I<size_t n>os&operator<<(os&o,const bset<n>&b){fo(i,n)o<<b[i];return o;}

I<J...A>os&operator<<(os&o,const tuple<A...>&t){apply([&](const auto&...a){ll i=sizeof...(a);(((o<<a<<str(--i>0,sp))),...);},t);return o;}

I<J T,J F>struct priority_queue:std::priority_queue<T,vector<T>,F>{
  priority_queue(const initializer_list<T>&a={}){fe(a,e)this->emplace(e);}
  priority_queue(const vector<T>&a){fe(a,e)this->emplace(e);}
  T front(){return this->top();}
  void pop_front(){this->pop();}
  friend os&operator<<(os&o,priority_queue q){while(len(q))o<<my::pop_front(q)<<str(len(q)>0,sp);return o;}
};
I<J T>using max_priority_queue=priority_queue<T,less<>>;
I<J T>using min_priority_queue=priority_queue<T,greater<>>;

I<J V>struct ve;
I<J V>constexpr bool isv=0;
I<J V>constexpr bool isv<ve<V>> =1;
I<J V>constexpr bool isv<vector<V>> =1;
I<J V>auto rawv(V){if constexpr(isv<V>)return rawv(V(1)[0]);else return V();}

I<J V>struct ve:vector<V>{
  using vector<V>::vector;
  using T=decltype(rawv(V()));
  I<J U>ve(const vector<U>&v={}){static_assert(isv<V> ==isv<U>);fe(v,e)this->eb(e);}

  ve&operator+=(const ve&u){auto&v=*this;fo(i,len(v))v[i]+=u[i];return v;}
  ve&operator-=(const ve&u){auto&v=*this;fo(i,len(v))v[i]-=u[i];return v;}
  ve&operator^=(const ve&u){fe(u,e)this->eb(e);return*this;}
  ve operator+(const ve&u)const{return ve(*this)+=u;}
  ve operator-(const ve&u)const{return ve(*this)-=u;}
  ve operator^(const ve&u)const{return ve(*this)^=u;}

  ve&operator+=(const T&x){auto&v=*this;fe(v,e)e+=x;return v;}
  ve&operator-=(const T&x){auto&v=*this;fe(v,e)e-=x;return v;}
  ve&operator*=(const T&x){auto&v=*this;fe(v,e)e*=x;return v;}
  ve operator+(const T&x)const{return ve(*this)+=x;}
  ve operator-(const T&x)const{return ve(*this)-=x;}
  ve operator*(const T&x)const{return ve(*this)*=x;}
  ve&operator++(){return*this+=1;}
  ve&operator--(){return*this-=1;}
  ve operator-()const{return ve(*this)*=-1;}
  I<size_t n>auto&operator+=(const bset<n>&a){fo(i,n)(*this)[i]+=a[i];return*this;}
  I<size_t n>auto&operator-=(const bset<n>&a){fo(i,n)(*this)[i]-=a[i];return*this;}

  auto lower_bound(const V&x)const{return std::lower_bound(all(*this),x);}
  auto upper_bound(const V&x)const{return std::upper_bound(all(*this),x);}

  I<J F>auto scan(F f)const{cp<T,bool>r;fe(*this,e)if constexpr(!isv<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;}
  T min()const{return scan([](T&a,const T&b){a>b?a=b:0;;}).a;}
  ve zeta()const{ve v=*this;if constexpr(isv<V>)fe(v,e)e=e.zeta();fo(i,len(v)-1)v[i+1]+=v[i];return v;}

  void emplace_front(const V&x={}){this->emplace(this->begin(),x);}
  void pop_front(){this->erase(this->begin());}

  friend is&operator>>(is&i,ve&v){fe(v,e)i>>e;return i;}
  friend os&operator<<(os&o,const ve&v){fe(v,e)o<<e<<str(&e!=&v.back(),isv<V>?nl:sp);return o;}
};
I<J T=ll,size_t n,size_t i=0>auto vec(const ll(&s)[n],T x={}){if constexpr(n==i+1)return ve<T>(s[i],x);else{auto X=vec<T,n,i+1>(s,x);return ve<decltype(X)>(s[i],X);}}
I<ll n,J...A>void set_size(const ll(&l)[n],A&...a){((a=vec(l,rawv(a))),...);}
using u1=ve<ll>;using u2=ve<ve<ll>>;

void io(){cin.tie(0)->sync_with_stdio(0);cout<<fixed<<setprecision(15);cerr<<nl;}
I<J...A>void li(A&...a){(cin>>...>>a);}
I<char c=sp,J...A>void pp(const A&...a){ll i=sizeof...(a);((cout<<a<<str(--i>0,c)),...);cout<<nl;}

ll floor_sqrt(ll x){ll r=sqrt(zz(x-ee));while(r+1<=x/(r+1))++r;return r;}
I<J T,J U=T>auto rle(const ve<T>&a){ve<cp<T,U>>r;fe(a,e)len(r)&&e==r.back().a?++r.back().b:r.eb(e,1).b;return r;}
I<J T,J U=T>auto rce(ve<T>a){return rle<T,U>(sort(a));}
ll inv(ll x,ll m){ll a=(x%m+m)%m,b=m,u=1,v=0;while(b)u-=a/b*v,swap(u,v),a-=a/b*b,swap(a,b);return(u%m+m)%m;}
I<J F>struct rec:F{rec(F&&f):F(std::forward<F>(f)){}I<J...A>decltype(auto)operator()(A&&...a)const{return F::operator()(*this,std::forward<A>(a)...);}};

ve<ll>prime_enumerate(ll n){
  ve<bool>sieve(n/3+1,1);
  for(ll p=5,d=4,i=1,rn=floor_sqrt(n);p<=rn;p+=d=6-d,i++){
    if(!sieve[i])continue;
    for(ll q=sq(p)/3,r=d*p/3+(d*p%3==2),s=p*2;q<len(sieve);q+=r=s-r)sieve[q]=0;
  }
  ve<ll>r{2,3};
  for(ll p=5,d=4,i=1;p<=n;p+=d=6-d,i++)if(sieve[i])r.eb(p);
  while(len(r)&&r.back()>n)r.pop_back();
  return r;
}

struct montgomery64{
  using ml=montgomery64;
  using i64=ll;
  using u64=ull;
  using u128=__uint128_t;

  static inline u64 m=998244353;
  static inline u64 miv;
  static inline u64 n2;

  static void set_mod(u64 m){
    assert(m<(1ULL<<63));
    assert(m&1);
    ml::m=m;
    n2=-u128(m)%m;
    miv=m;
    fo(5)miv*=2-m*miv;
    assert(m*miv==1);
  }

  static u64 mod(){
    return m;
  }

  u64 a;
  montgomery64(const i64&a=0):a(reduce((u128)(a%(i64)m+m)*n2)){}

  static u64 reduce(const u128&a){
    u128 r=(a+u128(u64(a)*-miv)*m)>>64;
    return r>=m?r-m:r;
  }

  ml&operator+=(const ml&b){if((a+=b.a)>=m)a-=m;return*this;}
  ml&operator-=(const ml&b){if(i64(a-=b.a)<0)a+=m;return*this;}
  ml&operator*=(const ml&b){a=reduce(u128(a)*b.a);return*this;}
  ml&operator/=(const ml&b){*this*=b.inv();return*this;}

  ml operator+(const ml&b)const{return ml(*this)+=b;}
  ml operator-(const ml&b)const{return ml(*this)-=b;}
  ml operator*(const ml&b)const{return ml(*this)*=b;}
  ml operator/(const ml&b)const{return ml(*this)/=b;}
  bool operator==(const ml&b)const{return a==b.a;}
  bool operator!=(const ml&b)const{return a!=b.a;}
  ml operator-()const{return ml()-ml(*this);}

  ml pow(u128 n)const{
    ml r(1),x(*this);
    while(n){
      if(n&1)r*=x;
      x*=x;
      n>>=1;
    }
    return r;
  }

  ml inv()const{

    u64 a=this->a,b=m,u=1,v=0;
    while(b)u-=a/b*v,swap(u,v),a-=a/b*b,swap(a,b);
    return u;
  }

  u64 val()const{
    return reduce(a);
  }

  friend is&operator>>(is&i,ml&b){
    ll t;i>>t;b=t;
    return i;
  }

  friend os&operator<<(os&o,const ml&b){
    return o<<b.val();
  }
};

template<class mont>bool miller_rabin(ll n,ve<ll>as){
  ll t=0,d=n-1;
  while(eve(d))d>>=1;

  if(mont::mod()!=n)mont::set_mod(n);
  mont one=1,minus_one=n-1;
  fe(as,a){
    if(a%n==0)continue;
    ll t=d;
    mont y=mont(a).pow(t);
    while(t!=n-1&&y!=one&&y!=minus_one)y*=y,t<<=1;
    if(y!=minus_one&&eve(t))return 0;
  }
  return 1;
}

bool is_prime(ll n){
  if(eve(n))return n==2;
  if(n<=1)return 0;
  if(n<4759123141LL)return miller_rabin<montgomery64>(n,{2,7,61});
  return miller_rabin<montgomery64>(n,{2,325,9375,28178,450775,9780504,1795265022});
}

template<class mont>ll pollard_rho(ll n){
  if(eve(n))return 2;
  if(is_prime(n))return n;
  if(mont::mod()!=n)mont::set_mod(n);
  mont R,one=1;
  auto f=[&](mont x){return x*x+R;};
  while(1){
    mont x,y,ys,q=one;
    R=rand(2,n),y=rand(2,n);
    ll g=1;
    constexpr ll m=128;
    for(ll r=1;g==1;r<<=1){
      x=y;
      fo(r)y=f(y);
      for(ll k=0;g==1&&k<r;k+=m){
        ys=y;
        for(ll i=0;i<m&&i<r-k;++i)q*=x-(y=f(y));
        g=std::gcd(q.val(),n);
      }
    }
    if(g==n)do g=std::gcd((x-(ys=f(ys))).val(),n);while(g==1);
    if(g!=n)return g;
  }
}

ve<ll>inner_factorize(ll n){
  if(n<=1)return{};
  ll d=pollard_rho<montgomery64>(n);
  if(d==n)return{d};
  return inner_factorize(d)^inner_factorize(n/d);
}

ve<cl>factorize(ll n){
  return rce(inner_factorize(n));
}

template<class mont>ll primitive_root(ll p){
  if(p==2)return 1;

  ve<ll>primes;
  fe(factorize(p-1),a,b)primes.eb(a);

  if(mont::mod()!=p)mont::set_mod(p);

  while(1){
    mont a=rand(1,p);
    bool f=1;
    fe(primes,k)if(a.pow((p-1)/k).val()==1)f=0;
    if(f)return a.val();
  }
}

ve<ll>prime_table(ll n){
  ve<bool>r(n+1);
  fe(prime_enumerate(n),p)r[p]=1;
  return r;
}

template<class T>T gcd(T a,T b){
  if(b==0)return a;
  return gcd(b,a%b);
}

template<class T>cp<T>bezout(T a,T b){
  if(b==0)return{1,0};
  auto[s,t]=bezout(b,a%b);
  return{t,s-a/b*t};
}
template<class T>cp<T>bezout(T a,T b,T c){
  T g=gcd(a,b);
  if(c<0)a=-a,b=-b,c=-c;
  if(c%g)return{oo,oo};
  auto[x,y]=bezout(zz(a),zz(b));
  if(a<0)x=-x;
  if(b<0)y=-y;
  return{x*(c/g),y*(c/g)};
}

ll mlog(ll a,ll b,ll m){
  a=(a%m+m)%m,b=(b%m+m)%m;
  if(b%std::gcd(a,m))return-1;
  ll rm=floor_sqrt(m)+1;

  map<ll>d;
  ll t=a;
  fo(q,1,rm){
    if(!d.count(t))d[t]=q;
    (t*=a)%=m;
  }

  ll A=pw(inv(a,m),rm,m);
  t=b;
  fo(p,rm){
    if(t==1&&p>0)return p*rm;
    else if(d.count(t))return p*rm+d[t];
    (t*=A)%=m;
  }
  return-1;
}

void main(){io();ll T=1;li(T);fo(T)solve();}
void solve(){
  LL(b,a);
  ll p=modint1000000007::mod();
  ll g=primitive_root<montgomery64>(p);

  ll k=mlog(pw(g,a,p),b,p);
  pp(pw(g,k,p));
}}
0