結果

問題 No.551 夏休みの思い出(2)
ユーザー beetbeet
提出日時 2018-11-23 15:50:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,302 bytes
コンパイル時間 1,883 ms
コンパイル使用メモリ 207,292 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 04:14:36
合計ジャッジ時間 9,525 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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(0);
    if(sq==0){
      T l=0,r=MOD;
      while(l+1<r){
        T m=(l+r)>>1;
        if(m*m<=MOD) l=m;
        else r=m;
      }
      sq=r;
    }
    map<Mint, T> dp;
    Mint res(1);
    for(int r=0;r<sq;r++){
      if(!dp.count(res)) dp[res]=r;
      res*=a;
    }
    Mint p=a.inv().pow(sq);
    res=b;
    for(int q=0;q<sq;q++){
      if(dp.count(res)){
        T idx=q*sq+dp[res];
        if(idx>0) return idx;
      }
      res*=p;
    }    
    return T(-1);
  }
};
//INSERT ABOVE HERE
signed main(){
  using M = Mint<int>;
  int r;
  cin>>MOD>>r;
  M R(r);
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    int a,b,c;
    cin>>a>>b>>c;      
    M A(a),B(b),C(c);
    M D=(A.inv()*B*M(2).inv()).pow(2)-A.inv()*C;
    M E=-A.inv()*B*M(2).inv();
    if(D.v==0){
      cout<<E.v<<endl;
      continue;
    }
    int k=M::log(R,D);
    if(k&1){
      cout<<-1<<endl;
      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<<endl;
  }
  return 0;
}
0