結果

問題 No.551 夏休みの思い出(2)
ユーザー beetbeet
提出日時 2018-11-23 16:03:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,305 ms / 4,000 ms
コード長 2,307 bytes
コンパイル時間 2,230 ms
コンパイル使用メモリ 206,096 KB
実行使用メモリ 164,524 KB
最終ジャッジ日時 2023-08-26 04:25:30
合計ジャッジ時間 47,247 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
4,384 KB
testcase_01 AC 61 ms
4,376 KB
testcase_02 AC 59 ms
4,380 KB
testcase_03 AC 60 ms
4,376 KB
testcase_04 AC 61 ms
4,376 KB
testcase_05 AC 61 ms
4,380 KB
testcase_06 AC 62 ms
4,376 KB
testcase_07 AC 61 ms
4,376 KB
testcase_08 AC 62 ms
4,380 KB
testcase_09 AC 62 ms
4,380 KB
testcase_10 AC 64 ms
4,384 KB
testcase_11 AC 64 ms
4,376 KB
testcase_12 AC 61 ms
4,376 KB
testcase_13 AC 60 ms
4,376 KB
testcase_14 AC 61 ms
4,380 KB
testcase_15 AC 62 ms
4,376 KB
testcase_16 AC 59 ms
4,376 KB
testcase_17 AC 140 ms
13,648 KB
testcase_18 AC 245 ms
30,444 KB
testcase_19 AC 229 ms
29,304 KB
testcase_20 AC 223 ms
30,452 KB
testcase_21 AC 375 ms
44,312 KB
testcase_22 AC 91 ms
6,152 KB
testcase_23 AC 114 ms
9,480 KB
testcase_24 AC 197 ms
23,420 KB
testcase_25 AC 134 ms
13,256 KB
testcase_26 AC 201 ms
22,924 KB
testcase_27 AC 1,984 ms
164,380 KB
testcase_28 AC 1,966 ms
164,380 KB
testcase_29 AC 1,672 ms
164,336 KB
testcase_30 AC 1,939 ms
164,344 KB
testcase_31 AC 1,726 ms
164,388 KB
testcase_32 AC 1,973 ms
164,400 KB
testcase_33 AC 1,922 ms
164,524 KB
testcase_34 AC 1,917 ms
164,284 KB
testcase_35 AC 1,757 ms
164,328 KB
testcase_36 AC 1,875 ms
164,512 KB
testcase_37 AC 2,305 ms
164,276 KB
testcase_38 AC 2,187 ms
164,380 KB
testcase_39 AC 2,050 ms
164,348 KB
testcase_40 AC 2,106 ms
164,384 KB
testcase_41 AC 1,957 ms
164,324 KB
testcase_42 AC 2,158 ms
164,384 KB
testcase_43 AC 1,948 ms
164,332 KB
testcase_44 AC 2,014 ms
164,384 KB
testcase_45 AC 1,960 ms
164,512 KB
testcase_46 AC 2,267 ms
164,340 KB
testcase_47 AC 59 ms
4,376 KB
testcase_48 AC 60 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

Int MOD = 1000000007;
template<typename T>
struct Mint{
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }
  
  Mint inv(){return pow(MOD-2);}
  
  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}
  
  Mint operator+(Mint a) const{return Mint(v)+=a;};
  Mint operator-(Mint a) const{return Mint(v)-=a;};
  Mint operator*(Mint a) const{return Mint(v)*=a;};
  Mint operator/(Mint a) const{return Mint(v)/=a;};

  Mint operator-(){return v?MOD-v:v;}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  // find x s.t. a^x = b
  static T log(Mint a,Mint b){
    static T sq = 3000000;
    static unordered_map<T, T> dp;
    if(dp.empty()){
      Mint res(1);
      for(int r=0;r<sq;r++){
        if(!dp.count(res.v)) dp[res.v]=r;
        res*=a;
      }
    }
    static Mint p=a.inv().pow(sq);
    Mint res=b;
    for(int q=0;q<=MOD/sq+1;q++){
      if(dp.count(res.v)){
        T idx=q*sq+dp[res.v];
        if(idx>0) return idx;
      }
      res*=p;
    }    
    return T(-1);
  }
};
//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  using M = Mint<int>;
  int r;
  cin>>MOD>>r;
  M R(r);
  int q;
  cin>>q;
  M TI=M(2).inv();
  for(int i=0;i<q;i++){
    int a,b,c;
    cin>>a>>b>>c;      
    M A(a),B(b),C(c);
    M AI=A.inv(); 
    M X=AI*B*TI;
    M D=X*X-AI*C;
    M E=-AI*B*TI;
    if(D.v==0){
      cout<<E.v<<"\n";
      continue;
    }
    int k=M::log(R,D);
    if(k&1){
      cout<<-1<<"\n";
      continue;
    }
    M F=R.pow(k/2);      
    int x=(E+F).v,y=(E-F).v;
    if(x>y) swap(x,y);
    cout<<x<<" "<<y<<"\n";
  }
  cout<<flush;
  return 0;
}
0