結果

問題 No.97 最大の値を求めるくえり
ユーザー yaoshimaxyaoshimax
提出日時 2015-03-09 22:34:44
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,067 bytes
コンパイル時間 1,257 ms
コンパイル使用メモリ 85,668 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 22:47:32
合計ジャッジ時間 7,146 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 223 ms
4,376 KB
testcase_03 AC 466 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 217 ms
4,380 KB
testcase_12 AC 209 ms
4,376 KB
testcase_13 AC 206 ms
4,376 KB
testcase_14 AC 195 ms
4,380 KB
testcase_15 AC 190 ms
4,376 KB
testcase_16 AC 189 ms
4,376 KB
testcase_17 AC 190 ms
4,376 KB
testcase_18 AC 185 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>

using namespace std;
#define mod 100003

unsigned xor128_x = 123456789, xor128_y = 362436069, xor128_z = 521288629, xor128_w = 88675123;
unsigned xor128() {
	unsigned t = xor128_x ^ (xor128_x << 11);
	xor128_x = xor128_y; xor128_y = xor128_z; xor128_z = xor128_w;
	return xor128_w = xor128_w ^ (xor128_w >> 19) ^ (t ^ (t >> 8));
}
bool has[100003];
void generateA(int N, int A[]) {
    for(int i = 0; i < N; ++ i)
    {
       A[i] = xor128() % 100003;
      has[A[i]]=true;  
    } 
}

void exgcd(int p, int q, int& a, int& b ){
   // p*a + q*b = 1
   if( q<p ){
      exgcd(q,p,b,a);
      return;
   }
   //now we can safely say p<q
   //p*a+q*b=1, q=cp+r
   //p*a+(cp+r)b=1, p(a+cb)+rb=1
   if( p==0 ){
      a=0;
      b=1;
      return;
   }
   int c = q/p;
   int r = q%p;
   exgcd(p,r,a,b);// a'=(a+cb), b'=b
   a-=c*b;
   return;
}
int rev(int q, int m){
   // return p s.t. pq%mod=1
   // pq = a*mod+1
   // pq-a*mod=1
   int p,a;
   exgcd( q,m,p,a );
//   cout << q<<"*"<<p<<"+"<<m<<"*"<<a<<"=1"<<endl;
   if( p < 0 ){
     p+=((abs(p)+m-1)/m)*m;
   }
   else{
      p%=m;
   }
   return p;
}
int main(){
   int N,Q;
   cin >> N >> Q;
   int A[N];
   generateA(N,A);
   
   for( int i = 0 ; i <Q; i++ ){
      int q;
      cin >> q;
      int y = 0;
      if( N< 500 ){
         for( int i = 0 ; i <N; i++ ){
            y=max(y,A[i]*q%mod);
         }
      }else{
         if( q!= 0){
            int revq=rev(q,mod);
//            cout << revq <<"*"<<q<<"=1"<<endl;
            for( long long i = mod-1; i>= 0 ; i-- ){
               if( has[(i*revq)%mod]){
                  y=i;
                  break;
               }
            }
         }
      }
      printf("%d\n",y);
   }
   
   return 0;
}
0