#include #include using namespace std; typedef long long Int; 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)); } void generateA(int N, int A[]) { for(int i = 0; i < N; ++ i) A[i] = xor128() % 100003; } // a x + b y = gcd(a, b) Int extgcd(Int a, Int b, Int &x, Int &y) { Int g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; } Int invMod(Int a, Int m) { Int x, y; if (extgcd(a, m, x, y) == 1) return (x + m) % m; else return 0; // unsolvable } int ex[100010]; int al[100010]; int main(){ memset(al,-1,sizeof(al)); int N,Q; cin >> N >> Q; vector A(N); set w; for(int i = 0 ; i < N ; i++){ ex[A[i] = xor128() % 100003] = true; } sort(A.begin(),A.end()); if( A.size() > 100 ){ al[0] = 0; for(int i = 0 ; i < Q ; i++){ int q; cin >> q; if( al[q] == -1 ){ int t = invMod(q,100003); for(int j = 100002 ; j >= 0 ; j--){ if( ex[(long long)j*t%100003] ){ al[q] = j; break; } } } cout << al[q] << endl; } }else{ al[0] = 0; for(int i = 0 ; i < Q ; i++){ int q; cin >> q; if( al[q] == -1 ){ int ans = 0; for(int j = 0 ; j < N ; j++) ans = max(ans,(long long)A[j]*q%100003); al[q] = ans; } cout << al[q] << endl; } } }