結果
問題 | No.97 最大の値を求めるくえり |
ユーザー |
![]() |
提出日時 | 2015-03-09 22:37:38 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 548 ms / 5,000 ms |
コード長 | 2,086 bytes |
コンパイル時間 | 687 ms |
コンパイル使用メモリ | 87,272 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-24 17:00:48 |
合計ジャッジ時間 | 5,778 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
ソースコード
#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 100003unsigned 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 = 1if( 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=1if( p==0 ){a=0;b=1;return;}int c = q/p;int r = q%p;exgcd(p,r,a,b);// a'=(a+cb), b'=ba-=c*b;return;}int rev(int q, int m){// return p s.t. pq%mod=1// pq = a*mod+1// pq-a*mod=1int 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++ ){long long q;cin >> q;long long 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",(int)y);}return 0;}