結果

問題 No.551 夏休みの思い出(2)
ユーザー chocoruskchocorusk
提出日時 2019-10-26 15:36:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 4,000 ms
コード長 1,887 bytes
コンパイル時間 980 ms
コンパイル使用メモリ 109,000 KB
実行使用メモリ 4,476 KB
最終ジャッジ日時 2023-10-12 10:34:30
合計ジャッジ時間 4,466 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 6 ms
4,352 KB
testcase_05 AC 12 ms
4,348 KB
testcase_06 AC 17 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,476 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,352 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 54 ms
4,352 KB
testcase_28 AC 51 ms
4,352 KB
testcase_29 AC 45 ms
4,348 KB
testcase_30 AC 72 ms
4,352 KB
testcase_31 AC 45 ms
4,348 KB
testcase_32 AC 62 ms
4,348 KB
testcase_33 AC 55 ms
4,352 KB
testcase_34 AC 48 ms
4,356 KB
testcase_35 AC 51 ms
4,352 KB
testcase_36 AC 54 ms
4,352 KB
testcase_37 AC 54 ms
4,348 KB
testcase_38 AC 58 ms
4,348 KB
testcase_39 AC 56 ms
4,348 KB
testcase_40 AC 58 ms
4,348 KB
testcase_41 AC 92 ms
4,352 KB
testcase_42 AC 54 ms
4,348 KB
testcase_43 AC 52 ms
4,352 KB
testcase_44 AC 51 ms
4,348 KB
testcase_45 AC 51 ms
4,352 KB
testcase_46 AC 53 ms
4,348 KB
testcase_47 AC 1 ms
4,352 KB
testcase_48 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
ll powmod(ll a, ll k, ll mod){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=mod;
        }
        ap=ap*ap;
        ap%=mod;
        k>>=1;
    }
    return ans;
}
ll inv(ll a, ll mod){
    return powmod(a, mod-2, mod);
}
ll modsqrt(int p, int a){
	if(a==0) return 0;
	int q=p-1, s=0;
	while((q&1)==0){
		q>>=1;
		s++;
	}
	int z=2;
	while(1){
		if(powmod(z, (p-1)/2, p)==p-1) break;
		z++;
	}
	ll c=powmod(z, q, p);
	ll r=powmod(a, (q+1)/2, p), t=powmod(a, q, p);
	int m=s;
	while(t>1){
		ll tp=t;
		int k=-1;
		for(int i=1; i<m; i++){
			tp=tp*tp%p;
			if(tp==1){
				k=i; break;
			}
		}
		if(k==-1) return -1;
		ll cp=c;
		for(int i=0; i<m-k-1; i++) cp=cp*cp%p;
		c=cp*cp%p;
		t=c*t%p;
		r=cp*r%p;
		m=k;
	}
	return r;
}
int main()
{
    ll p, r;
    cin>>p>>r;
    int q;
    cin>>q;
    for(int i=0; i<q; i++){
        ll a, b, c;
        cin>>a>>b>>c;
        ll ainv=inv(a, p);
        (b*=ainv)%=p;
        (c*=ainv)%=p;
        (b*=((p+1)/2))%=p;
        ll d=(b*b+p-c)%p;
        ll e=modsqrt(p, d);
        if(e==-1){
            cout<<-1<<endl;
        }else{
            ll x1=(e-b+p)%p, x2=(-e-b+2*p)%p;
            if(x1==x2){
                cout<<x1<<endl;
            }else{
                if(x1>x2) swap(x1, x2);
                cout<<x1<<" "<<x2<<endl;
            }
        }
    }
    return 0;
}
0