結果
| 問題 |
No.551 夏休みの思い出(2)
|
| コンテスト | |
| ユーザー |
char134217728
|
| 提出日時 | 2017-08-15 22:00:23 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 1,655 ms / 4,000 ms |
| コード長 | 1,879 bytes |
| コンパイル時間 | 1,442 ms |
| コンパイル使用メモリ | 167,280 KB |
| 実行使用メモリ | 69,276 KB |
| 最終ジャッジ日時 | 2024-10-13 11:03:13 |
| 合計ジャッジ時間 | 18,751 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 47 |
コンパイルメッセージ
main.cpp:28:1: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
28 | main(){
| ^~~~
ソースコード
#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";
}
}
}
char134217728