結果

問題 No.551 夏休みの思い出(2)
ユーザー 小指が強い人小指が強い人
提出日時 2017-08-20 02:42:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,984 ms / 4,000 ms
コード長 3,915 bytes
コンパイル時間 2,361 ms
コンパイル使用メモリ 180,884 KB
実行使用メモリ 87,972 KB
最終ジャッジ日時 2024-04-22 17:25:58
合計ジャッジ時間 17,886 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 6 ms
5,376 KB
testcase_05 AC 12 ms
5,376 KB
testcase_06 AC 17 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 6 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 427 ms
9,592 KB
testcase_28 AC 406 ms
9,204 KB
testcase_29 AC 215 ms
5,376 KB
testcase_30 AC 336 ms
8,312 KB
testcase_31 AC 214 ms
5,376 KB
testcase_32 AC 328 ms
8,188 KB
testcase_33 AC 406 ms
9,724 KB
testcase_34 AC 268 ms
7,944 KB
testcase_35 AC 247 ms
5,376 KB
testcase_36 AC 272 ms
5,884 KB
testcase_37 AC 1,984 ms
87,972 KB
testcase_38 AC 1,565 ms
57,588 KB
testcase_39 AC 526 ms
14,876 KB
testcase_40 AC 1,114 ms
42,740 KB
testcase_41 AC 435 ms
12,828 KB
testcase_42 AC 1,655 ms
59,256 KB
testcase_43 AC 552 ms
15,000 KB
testcase_44 AC 779 ms
24,932 KB
testcase_45 AC 606 ms
16,412 KB
testcase_46 AC 1,767 ms
82,676 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> veci;
typedef vector<ll> vecll;
typedef vector<string> vecs;
template<class T,class U> using Hash=unordered_map<T,U>;

#define REP(i, a, n) for(ll i = a; i < (ll)n; i++)
#define RREP(i, a, n) for(ll i = n-1; i >= (ll)a; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define MD 1000000007

template<class T> T read(){T a;cin >> a;return a;}
template<class T> void read(T& a){cin >> a;}
template<class T, class ...Args> void read(T& a, Args&... args){cin >> a; read(args...);} 
template<class T> void rarr(T a, int n){for(int i = 0; i < n; i++) {cin >> a[i];}}
template<char c = ' ',class T> void write(T a){cout << fixed << setprecision(12) << a << endl;}
template<char c = ' ',class T,class ...Args> void write(T a, Args... args){cout << fixed << setprecision(4) << a << c; write<c>(args...);}
template<class T> void warr(T a, const char* c = " "){cout << a;for(int i = 1; i < (int)a.size(); i++)cout << c << a[i];cout << endl;}
template<class T> void warr(T a, int n, const char* c = " "){cout << a[0];for(int i = 1; i < n; i++)cout << c << a[i];cout << endl;}
void split(string s, string delim, veci& result){result.clear();string::size_type pos = 0;while(pos != string::npos){string::size_type p = s.find(delim, pos);if(p == string::npos){result.push_back(atoi(s.substr(pos).data()));break;}else {result.push_back(atoi(s.substr(pos, p - pos).data()));}pos = p + delim.size();}}
void split(string s, string delim, vecs& result){result.clear();string::size_type pos = 0;while(pos != string::npos){string::size_type p = s.find(delim, pos);if(p == string::npos){result.push_back(s.substr(pos));break;}else {result.push_back(s.substr(pos, p - pos));}pos = p + delim.size();}}
ull gcd(ull a, ull b){while(true){ull k = a % b;if(k == 0)return b;a = b;b = k;}}
ull comb(ull n, ull m){ull p=1;m=min(m,n-m);for(ull i=1;i<=m;i++){p*=n-i+1;p/=i;}return p;}

ull power( const ull& a, ull k, const ull& n )
{
  if ( a == 0 || n == 0 ) return( 0 );
  if ( k == 0 ) return( 1 % n );

  ull currentMod = a % n;
  ull currentValue = ( ( k & 1 ) > 0 ) ? currentMod : 1;

  for ( k >>= 1 ; k > 0 ; k >>= 1 ) {
    currentMod = ( currentMod * currentMod ) % n;
    if ( ( k & 1 ) > 0 )
      currentValue = ( currentValue * currentMod ) % n;
  }

  return( currentValue );
}

int main(void)
{
    ll p,r,q;
    unordered_map<int,int> mp;
    read(p,r,q);
    ll w,h;
    if(p<1e6){
        w=h=ceil(sqrt(p));
    }
    else {
        w=ceil(p/500.0);
        h=500;
    }
    ll ri=1;
    rep(i,w){
        mp[ri]=i;
        ri=(ri*r)%p;
    }
    //int i2=power(2,p-2,p);
    int d2p=(p-1)/2;
    ll irm=power(power(r,p-2,p),w,p);
    rep(i,q){
        ll a,b,c;
        read(a,b,c);
        if(b==0&&c==0){
            write(0);
            continue;
        }
        ll i2a=power(2*a,p-2,p);
        ll d=(((b*b)%p)-4*((a*c)%p))%p;
        if(d==0){
            write(((-b+p)*i2a)%p);
            continue;
        }
        if(d<0)d=(d+p)%p;
        ll v=d;
        set<int> res;
        rep(j,h){
            auto it=mp.find(v);
            if(it!=mp.end()){
                ll x=j*w+it->second;
                //write(i,x);
                if(x%2==0){
                    ll gx1=power(r,x/2,p);
                    ll gx2=power(r,x/2+d2p,p);
                    ll x1=(gx1-b+p)*i2a;
                    ll x2=(gx2-b+p)*i2a;
                    res.insert(x1%p);
                    res.insert(x2%p);
                    break;
                }
            }
            v=(v*irm)%p;
        }
        //continue;
        if(res.size()>0){
            bool init=false;
            for(auto a:res){
                if(init)cout<<" ";
                cout<<a;
                init=true;
            }
            cout<<endl;
        }
        else write(-1);
    }
    return 0;
}
0