結果

問題 No.551 夏休みの思い出(2)
ユーザー char134217728char134217728
提出日時 2017-08-15 22:00:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,364 ms / 4,000 ms
コード長 1,879 bytes
コンパイル時間 1,237 ms
コンパイル使用メモリ 168,892 KB
実行使用メモリ 69,280 KB
最終ジャッジ日時 2024-04-21 12:34:27
合計ジャッジ時間 16,147 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 29 ms
7,408 KB
testcase_18 AC 84 ms
19,696 KB
testcase_19 AC 80 ms
19,696 KB
testcase_20 AC 84 ms
19,568 KB
testcase_21 AC 114 ms
19,700 KB
testcase_22 AC 12 ms
5,376 KB
testcase_23 AC 20 ms
7,408 KB
testcase_24 AC 56 ms
11,504 KB
testcase_25 AC 30 ms
7,412 KB
testcase_26 AC 54 ms
11,632 KB
testcase_27 AC 529 ms
69,148 KB
testcase_28 AC 467 ms
69,148 KB
testcase_29 AC 398 ms
69,140 KB
testcase_30 AC 486 ms
69,148 KB
testcase_31 AC 408 ms
69,148 KB
testcase_32 AC 471 ms
69,152 KB
testcase_33 AC 478 ms
69,144 KB
testcase_34 AC 449 ms
69,016 KB
testcase_35 AC 403 ms
69,148 KB
testcase_36 AC 423 ms
69,020 KB
testcase_37 AC 1,364 ms
69,280 KB
testcase_38 AC 1,066 ms
69,276 KB
testcase_39 AC 544 ms
69,144 KB
testcase_40 AC 870 ms
69,276 KB
testcase_41 AC 508 ms
69,272 KB
testcase_42 AC 1,080 ms
69,064 KB
testcase_43 AC 560 ms
69,148 KB
testcase_44 AC 649 ms
69,280 KB
testcase_45 AC 562 ms
69,140 KB
testcase_46 AC 1,230 ms
69,276 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:28:1: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
   28 | main(){
      | ^~~~

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define FORR(i,a,b) for (int i=(a);i>=(b);i--)
#define pb push_back

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef set<int> si;
const int inf = 1e9;
const int mod = 1e9+7;

ll p, q, r;
vector<pair<ll, int> > m;
ll qu[2][10000];
vector<pair<ll, int> > li;
ll powMod(ll a, ll n, ll m){
  ll p = 1;
  while(n > 0){
    if(n & 1) p = p * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return p;
}

main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> p >> r >> q;
  ll inv2 = powMod(2, p - 2, p), invr = powMod(r, p - 2, p);
  ll a, b, c, d;
  FOR(i, 0, q){
    cin >> a >> b >> c;
    d = powMod(a, p - 2, p);
    b = b * d % p * inv2 % p;
    c = c * d % p;
    d = b * b % p;
    c = (d - c + p) % p;
    qu[0][i] = b;
    qu[1][i] = -1;
    if(c != 0){
      li.pb(pair<ll, int>(c, i));
      qu[1][i] = 1;
    }
  }
  ll e = min(p-1, 3000000ll);
  a = 1;
  FOR(i, 0, e){
    m.pb(pair<ll, int>(a, i));
    a = a * r % p;
  }
  a = powMod(invr, e, p);
  sort(m.begin(), m.end());
  m.pb(pair<ll, int>(mod, -1));
  vector<pair<ll, int> >::iterator itr;
  pair<ll, int> lin;
  FOR(i, 0, 1+p/e){
    FOR(j, 0, li.size()){
      lin = li[j];
      itr = lower_bound(m.begin(), m.end(), pair<ll, int>(lin.first, 0));
      if(itr->first == lin.first){
        qu[1][lin.second] = i*e + itr->second;
      }
      li[j].first = lin.first * a % p;
    }
  }
  FOR(i, 0, q){
    if(qu[1][i] < 0){
      cout << (p - qu[0][i])%p << "\n";
    }else if(qu[1][i] % 2 != 0){
      cout << -1 << "\n";
    }else{
      ll ans1, ans2;
      ans1 = (powMod(r, qu[1][i] / 2, p) - qu[0][i] + p)%p;
      ans2 = (powMod(r, qu[1][i] / 2 + (p-1) / 2, p) - qu[0][i] + p)%p;
      if(ans1 > ans2)swap(ans1, ans2);
      cout << ans1 << " " << ans2 << "\n";
    }
  }
}
0