結果

問題 No.981 一般冪乗根
ユーザー eQeeQe
提出日時 2024-11-01 09:31:24
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 8,505 bytes
コンパイル時間 7,313 ms
コンパイル使用メモリ 336,580 KB
実行使用メモリ 814,720 KB
最終ジャッジ日時 2024-11-01 09:32:03
合計ジャッジ時間 35,875 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 4 ms
6,820 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 6 ms
6,820 KB
testcase_26 AC 5 ms
6,816 KB
testcase_27 MLE -
testcase_28 -- -
evil_60bit1.txt -- -
evil_60bit2.txt -- -
evil_60bit3.txt -- -
evil_hack -- -
evil_hard_random -- -
evil_hard_safeprime.txt -- -
evil_hard_tonelli0 -- -
evil_hard_tonelli1 -- -
evil_hard_tonelli2 -- -
evil_hard_tonelli3 -- -
evil_sefeprime1.txt -- -
evil_sefeprime2.txt -- -
evil_sefeprime3.txt -- -
evil_tonelli1.txt -- -
evil_tonelli2.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
namespace my{
using namespace std;
#define eb emplace_back
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define FO(n) for(ll ij=n;ij--;)
#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 of(i,...) for(auto[i,i##stop,i##step]=range(1,__VA_ARGS__);i>=i##stop;i-=i##step)
#define fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a)
#define multiple_testcases void solve();}int main(){my::io();int T;std::cin>>T;while(T--)my::solve();}namespace my{
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
using ull=unsigned long long;
using ulll=__uint128_t;
using lll=__int128_t;
istream&operator>>(istream&i,ulll&x){ull t;i>>t;x=t;return i;}
ostream&operator<<(ostream&o,const ulll&x){return(x<10?o:o<<x/10)<<ll(x%10);}
istream&operator>>(istream&i,lll&x){ll t;i>>t;x=t;return i;}
ostream&operator<<(ostream&o,const lll&x){return o<<string(x<0,'-')<<ulll(x>0?x:-x);}
auto range(bool s,auto...a){array<ll,3>r{0,0,1};ll I=0;((r[I++]=a),...);if(!s&&I==1)swap(r[0],r[1]);r[0]-=s;return r;}
constexpr char nl=10;
constexpr char sp=32;
lll pw(lll x,ll n){assert(n>=0);lll r=1;while(n)n&1?r*=x:r,x*=x,n>>=1;return r;}

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;
  friend ostream&operator<<(ostream&o,const pair&p){return o<<p.a<<sp<<p.b;}
};

template<class F=less<>>auto&sort(auto&a,const F&f={}){ranges::sort(a,f);return a;}

template<class T,class U>ostream&operator<<(ostream&o,const std::pair<T,U>&p){return o<<p.first<<sp<<p.second;}
template<class T,size_t n>ostream&operator<<(ostream&o,const array<T,n>&a){fo(i,n)o<<a[i]<<string(i!=n-1,sp);return o;}
template<class T,class U>ostream&operator<<(ostream&o,const unordered_map<T,U>&m){fe(m,e)o<<e.first<<sp<<e.second<<nl;return o;}

template<class V>concept vectorial=is_base_of_v<vector<typename V::value_type>,V>;
template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}
template<class V>ostream&operator<<(ostream&o,const vector<V>&v){fe(v,e)o<<e<<string(&e!=&v.back(),vectorial<V>?nl:sp);return o;}

template<class V>struct vec:vector<V>{
  using vector<V>::vector;
  vec(const vector<V>&v){vector<V>::operator=(v);}

  vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;}
  vec operator^(const vec&u)const{return vec{*this}^=u;}
  vec&operator++(){fe(*this,e)++e;return*this;}
  vec&operator--(){fe(*this,e)--e;return*this;}
};

void lin(auto&...a){(cin>>...>>a);}
template<char c=sp>void pp(const auto&...a){ll n=sizeof...(a);((cout<<a<<string(--n>0,c)),...);cout<<nl;}

template<class T,class U=T>auto rle(const vec<T>&a){vec<pair<T,U>>r;fe(a,e)r.size()&&e==r.back().a?++r.back().b:r.eb(e,1).b;return r;}
template<class T,class U=T>auto rce(vec<T>a){return rle<T,U>(sort(a));}

auto mod(auto a,auto m){return(a%=m)<0?a+m:a;}
auto inv_mod(auto x,auto m){assert(gcd(x,m)==1);decltype(x)a=mod(x,m),b=m,u=1,v=0;while(b)swap(u-=a/b*v,v),swap(a-=a/b*b,b);return mod(u,m);}
auto pow_mod(auto x,auto n,auto m){if(n<0)n=-n,x=inv_mod(x,m);decltype(x)r=1;while(n){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}

uint64_t kth_root_floor(uint64_t a,ll k){
  if (k==1)return a;
  auto is_within=[&](uint32_t x){uint64_t t=1;fo(k)if(__builtin_mul_overflow(t,x,&t))return false;return t<=a;};

  uint64_t r=0;
  of(i,sizeof(uint32_t)*CHAR_BIT)if(is_within(r|(1u<<i)))r|=1u<<i;
  return r;
}

auto sqrt_floor(auto x){return kth_root_floor(x,2);}

ll rand(auto...a){array<ll,2>v{0,0};ll I=0;((v[I++]=a),...);auto[l,r]=v;if(I==1)swap(l,r);static ll t=495;t^=t<<7,t^=t>>9;return l<r?mod(t,r-l)+l:t;}

struct montgomery64{
  using i64=__int64_t;
  using u64=__uint64_t;
  using u128=__uint128_t;

  static inline u64 N=998244353;
  static inline u64 N_inv;
  static inline u64 R2;

  static void set_mod(u64 N){
    assert(N<(1ULL<<63));
    assert(N&1);
    montgomery64::N=N;
    R2=-u128(N)%N;
    N_inv=N;
    fo(5)N_inv*=2-N*N_inv;
    assert(N*N_inv==1);
  }

  static u64 mod(){
    return N;
  }

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

  static u64 reduce(const u128&T){
    u128 r=(T+u128(u64(T)*-N_inv)*N)>>64;
    return r>=N?r-N:r;
  }

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

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

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

  montgomery64 inv()const{
    u64 a=this->a,b=N,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 istream&operator>>(istream&i,montgomery64&b){
    ll t;i>>t;b=t;
    return i;
  }

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

template<class modular>bool miller_rabin(ll n,vec<ll>as){
  ll d=n-1;
  while(~d&1)d>>=1;

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

bool is_prime(ll n){
  if(~n&1)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 modular>ll pollard_rho(ll n){
  if(~n&1)return 2;
  if(is_prime(n))return n;
  if((ll)modular::mod()!=n)modular::set_mod(n);
  modular R,one=1;
  auto f=[&](const modular&x){return x*x+R;};
  while(1){
    modular 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;
  }
}

auto factorize(ll n){
  auto f=[](auto&f,ll m){
    if(m==1)return vec<ll>{};
    ll d=pollard_rho<montgomery64>(m);
    return d==m?vec<ll>{d}:f(f,d)^f(f,m/d);
  };
  return rce(f(f,n));
}

template<class T>T gcd(T a,T b){return b?gcd(b,a%b):a>0?a:-a;}
template<class...A>auto gcd(const A&...a){common_type_t<A...>r=0;((r=gcd(r,a)),...);return r;}

ll kth_root_mod_prime(ll a,ll k,ll P){
  if(k==0)return(a==1?1:-1);
  if(a==0)return 0;
  if(P==2)return a;
  k=mod(k,P-1);

  ll g=gcd(k,P-1);
  if(pow_mod(a,(P-1)/g,P)!=1)return-1;

  ll c=inv_mod(k/g,(P-1)/g);
  a=pow_mod(a,c,P);
  (k*=c)%=P-1;

  auto pe_root=[&](ll c,ll p,ll e){
    ll t=0;
    ll pt=1;
    ll s=P-1;
    while(s%p==0)++t,s/=p;

    ll v=1;
    while(pow_mod(v,(P-1)/p,P)==1)++v;

    ll pe=pw(p,e);
    ll u=inv_mod(-s,pe);
    ll z=pow_mod(c,(s*u+1)/pe,P);
    ll c_inv=inv_mod(c,P);

    while(1){
      ll zpe=pow_mod(z,pe,P);
      ll zpe_c=mod((lll)zpe*c_inv,P);
      ll t_dash=0;
      while(pow_mod(zpe_c,pw(p,t_dash),P)!=1)++t_dash;
      if(t_dash==0)break;

      ll E=t-t_dash;
      ll vspE=pow_mod(v,s*pw(p,E-e),P);

      ll A=pow_mod(v,s*pw(p,t-1),P);
      ll B=pow_mod(inv_mod(zpe_c,P),pw(p,t_dash-1),P);

      ll R=sqrt_floor(p)+1;
      ll A_inv=inv_mod(A,P);

      unordered_map<int,int>dict;
      ll k=-1;
      fo(i,R+1)dict[B*pow_mod(A_inv,R*i,P)]=i;
      fo(j,R)if(ll key=pow_mod(A,j,P);dict.contains(key))k=R*dict[key]+j;
      z=mod(z*pow_mod(vspE,k,P),P);
      assert(k!=-1);
    }
    return z;
  };

  fe(factorize(k),p,e)a=pe_root(a,p,e);
  return a;
}

multiple_testcases
void solve(){
  LL(P,k,a);
  pp(kth_root_mod_prime(a,k,P));
}}
0