結果

問題 No.551 夏休みの思い出(2)
ユーザー eQeeQe
提出日時 2024-10-30 03:26:29
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 10,589 bytes
コンパイル時間 6,716 ms
コンパイル使用メモリ 335,032 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-30 03:26:40
合計ジャッジ時間 10,727 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 2 ms
6,816 KB
testcase_48 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

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 fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a)
#define single_testcase void solve();}int main(){my::io();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,ll a,ll b=1e18,ll c=1){if(b==1e18)b=a,(s?b:a)=0;return array{a-s,b,c};}
constexpr char nl=10;
constexpr char sp=32;
lll pw(lll x,ll n,ll m=0){assert(n>=0);lll r=1;while(n)n&1?r*=x:r,x*=x,m?r%=m,x%=m:r,n>>=1;return r;}
constexpr ll pw2(ll n){return 1LL<<n;}
template<class...A>auto max(const A&...a){return max(initializer_list<common_type_t<A...>>{a...});}
template<class...A>auto min(const A&...a){return min(initializer_list<common_type_t<A...>>{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;
  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;}
auto pop_back(auto&a){assert(a.size());auto r=*a.rbegin();a.pop_back();return r;}

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,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 T>struct core_type{using type=T;};
template<vectorial V>struct core_type<V>{using type=typename core_type<typename V::value_type>::type;};
template<class T>using core_t=core_type<T>::type;
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;}

  auto scan(const auto&f)const{pair<core_t<V>,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,const auto&b){a<b?a=b:0;}).a;}
  auto min()const{return scan([](auto&a,const auto&b){a>b?a=b:0;;}).a;}
};

template<bool is_negative=false>struct infinity{
  template<integral T>constexpr operator T()const{return numeric_limits<T>::max()*(1-is_negative*2);}
  template<floating_point T>constexpr operator T()const{return static_cast<ll>(*this);}
  template<class T>constexpr bool operator==(T x)const{return static_cast<T>(*this)==x;}
  constexpr auto operator-()const{return infinity<!is_negative>();}
  template<class A,class B>constexpr operator pair<A,B>()const{return pair<A,B>{*this,*this};}
};
constexpr infinity oo;

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));}

ll rand(ll l=oo,ll r=oo){if(l!=oo&&r==oo)r=l,l=0;static ll a=495;a^=a<<7,a^=a>>9;ll t=a;return l<r?((t%=(r-l))<0?t+r-l:t)+l:a;}

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 mod(T a,T m){return(a%=m)<0?a+m:a;}

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;}

template<class T>pair<T,T>ax_by_g(T a,T b){
  if(b==0)return{1,0};
  auto[s,t]=ax_by_g(b,a%b);
  return{t,s-a/b*t};
}

ll inv_mod(ll a,ll m){
  assert(gcd(a,m)==1);
  auto[x,y]=ax_by_g(a,m);
  return mod(x,m);
}

template<class T>T chinese_remainder_theorem_coprime(const vec<T>&a,vec<T>&m,T M=0){
  ll K=a.size();
  m.eb(M);
  vec<T>t(K),S(K+1),P(K+1,1);
  fo(i,K){
    t[i]=mod((a[i]-S[i])*inv_mod(P[i],m[i]),m[i]);
    fo(j,i+1,K+1){
      S[j]+=t[i]*P[j];
      P[j]*=m[i];
      if(m[j])S[j]%=m[j],P[j]%=m[j];
    }
  }
  ll r=S.back();
  m.pop_back();
  return S.back();
}

template<class T>T chinese_remainder_theorem(const vec<T>&a,const vec<T>&m,T M=0){
  ll K=a.size();
  fo(i,K)fo(j,i+1,K)if((a[i]-a[j])%gcd(m[i],m[j]))return-1;

  unordered_map<T,pair<T,T>>exponent_max_congruence;
  fo(i,K)fe(factorize(m[i]),p,b)if(exponent_max_congruence[p].b<b)exponent_max_congruence[p]={a[i],b};

  vec<T>a_mod_prime_pow,m_mod_prime_pow;
  fe(exponent_max_congruence,p,v){
    T pq=pw(p,v.b);
    a_mod_prime_pow.eb(v.a%pq);
    m_mod_prime_pow.eb(pq);
  }
  return chinese_remainder_theorem_coprime(a_mod_prime_pow,m_mod_prime_pow,M);
}

bool is_quadratic_residue(ll a,ll p){
  a=mod(a,p);
  if(a==0)return 1;
  if(p==2)return 1;
  return pw(a,(p-1)>>1,p)==1;
}

ll sqrt_mod_prime(ll a,ll p){
  a=mod(a,p);
  if(a==0)return 0;
  if(p==2)return a;
  if(!is_quadratic_residue(a,p))return-1;

  ll S=0,Q=p-1;
  while(~Q&1)Q>>=1,++S;

  ll R=pw(a,(Q+1)>>1,p),T=pw(a,Q,p),M=S;

  ll z=1;
  while(is_quadratic_residue(z,p))++z;
  ll c=pw(z,Q,p);

  while(T!=1){
    ll nxM=0,t=T;
    while(t!=1)++nxM,(t*=t)%=p;
    ll b=pw(c,pw2(S-nxM-1),p);
    (R*=b)%=p;
    (T*=(b*b)%p)%=p;
  }
  return min(R,p-R);
}

ll sqrt_mod_prime_pow(ll a,ll p,ll q){
  ll t1=sqrt_mod_prime(a,p);
  if(q==1)return t1;
  if(t1==-1)return-1;

  if(p==2){
    if(q==2){
      if(a%4!=1)return-1;
      return 1;
    }else{
      if(a%8!=1)return-1;
      ll tk=1;
      fo(k,3,q){
        ll sk=(tk*tk-a)>>k;
        tk+=pw2(k-1)*mod(sk,2LL);
      }
      return tk;
    }
  }else{
    ll tk=t1;
    ll pk=p;
    fo(k,1,q){
      ll sk=(tk*tk-a)/pk;
      tk+=pk*mod(-sk*inv_mod(tk*2,p),p);
      pk*=p;
    }
    return tk;
  }
}

ll sqrt_mod(ll a,ll m){
  if(a==0)return 0;
  assert(a&1||m&1);
  if(gcd(a,m)>1)return-1;

  vec<ll>remainders,moduli;
  fe(factorize(m),p,q){
    ll pq=pw(p,q);
    ll c=sqrt_mod_prime_pow(a,p,q);
    if(c==-1)return-1;
    remainders.eb(c%pq);
    moduli.eb(pq);
  }
  return chinese_remainder_theorem(remainders,moduli);
}

single_testcase
void solve(){
  LL(P,g);
  LL(Q);
  fo(Q){
    LL(a,b,c);
    ll r=sqrt_mod((b*b-a*c*4)*inv_mod(a*a*4,P),P);

    if(r==-1){
      pp(-1);
    }else if(r==0){
      pp(mod(-b*inv_mod(a*2,P)+r,P));
    }else{
      ll x1=mod(-b*inv_mod(a*2,P)+r,P);
      ll x2=mod(-b*inv_mod(a*2,P)-r,P);
      pp(min(x1,x2),max(x1,x2));
    }
  }
}}
0