Problem:
モジュロ・クエリ
あなたに N 枚のカードを渡します。どのカードにも一つだけ自然数が書いてあります。
これから質問として、適当な自然数を言います。あなたが持っているカードに書いてある数を私が言った数で割ったときに得られる余りのうち最も大きなものを答えてください。
たとえば、あなたは 3 枚のカードを持っていて、それぞれ 9、3、8 と書いてあるとします。私が「4」と言ったら、9 と 3 と 8 をそれぞれ4 で割った余りを求めてください。余りはそれぞれ 1、3、0 ですが、この中でもっとも大きな余りは3 なので、3 が正しい答えになります。
では始めましょうか。え？ カードがいっぱいあるとたいへんだ？ しょうがないですね。それではコ
ンピュータを使って最大の余りを見つけることにしましょう。カードに書いてある数を、質問された数で割った余りのうち、最大のものを見つけるプログラムを作成してください。なお、質問は１回だけでなく何度もしますが、同じ数を 2 回以上質問することはありません。
入力
入力は１つのデータセットからなる。入力データは以下の形式で与えられる。
N Q
c
1
c
2
... c
N
q
1
q
2
:
q
Q
１行目にカードの枚数 N (2 ≤ N ≤ 300000) と質問の回数 Q (2 ≤ Q ≤ 100000)  が１つの空白区切りで与えられ、２行目にカードに書かれた数 c
i
(1 ≤ c
i
≤ 300000) が１つの空白区切りで与えられる。続くQ 行に質問として与えられる数 q
i
(1 ≤ q
i
≤ 300000) が与えられる。
出力
質問ごとに最大の余りを１行に出力する。
入力例
3 3
9 3 8
4
6
5
出力例
3
3
4

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N, Q;
    cin >> N >> Q;
    vector<int> c(N);
    int size = 0;
    for(int i=0; i<N; ++i) {
        cin >> c[i];
        size = max(size, c[i]);
    }
    size += 1;
    vector<int> ma(size+1);
    for(int i=0; i<N; ++i) {
        ma[c[i]+1] = c[i];
    }
    for(int i=1; i<size; ++i) {
        ma[i] = max(ma[i], ma[i-1]);
    }
    for(int i=0; i<Q; ++i) {
        int q;
        cin >> q;
        int res = 0;
        for(int now=size-1; now != 0;) {
            res = max(res, now%q);
            now = ma[now - now%q];
        }
        cout << res << endl;
    }
}
accuracy: 1
time_ms: 170
memory_kb: 5244
final_rank: 10

code_snippet: #include <climits>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

const size_t N_MIN = 2;
const size_t N_MAX = 300000;
const size_t Q_MIN = 2;
const size_t Q_MAX = 100000;
const size_t CQ_MIN = 1;
const size_t CQ_MAX = 300000;

int main()
{
	int n = 0;
	cin >> n;
	if (n < N_MIN || N_MAX < n)
	{
		cout << N_MIN << " <= n <= " << N_MAX << ") but input " << n << endl;
		exit(1);
	}
	int q = 0;
	cin >> q;
	if (q < Q_MIN || Q_MAX < q)
	{
		cout << Q_MIN << " <= q <= " << Q_MAX << ") but input " << q << endl;
		exit(1);
	}
	int cMax = INT_MIN;

	vector<int> clist(n);
	for (int i = 0; i < n; i++)
	{
		cin >> clist[i];
		if (clist[i] < CQ_MIN || CQ_MAX < clist[i])
		{
			cout << CQ_MIN << " <= c" << " <= " << CQ_MAX << ") but input " << clist[i] << endl;
			exit(1);
		}
		if (clist[i] > cMax)
		{
			cMax = clist[i];
		}
	}

	vector<bool> cmap(cMax + 2);
	for (auto iter : clist)
	{
		cmap[iter] = true;
	}

	vector<int> lmap(cMax + 2);
	int m = 0;
	for (int i = 0; i < cMax + 2; i++)
	{
		lmap[i] = m;
		if (cmap[i])
		{
			m = i;
		}
	}

	vector<int> qlist(q);
	for (int i = 0; i < q; i++)
	{
		cin >> qlist[i];
		if (qlist[i] < CQ_MIN || CQ_MAX < qlist[i])
		{
			cout << CQ_MIN << " <= q" << " <= " << CQ_MAX << ") but input " << qlist[i] << endl;
			exit(1);
		}

		int maxv = 0;
		int current = m;
		while (current != 0)
		{
			int p = current % qlist[i];
			maxv = max(maxv, p);
			if (current - p < 0)
			{
				break;
			}
			current = lmap[current - p];
		}
		cout << maxv << endl;
	}
}
accuracy: 1
time_ms: 170
memory_kb: 5876
final_rank: 13

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;

int main() {
    const int UPPER_BOUND_C = 300000;
    int floor_c[UPPER_BOUND_C + 1] = {};

    int N, Q; cin >> N >> Q;

    for (int c, i = 0; i < N; i++) {
        cin >> c; floor_c[c] = c;
    }

    for (int i = 1; i <= UPPER_BOUND_C; i++) {
        floor_c[i] = max(floor_c[i - 1], floor_c[i]);
    }

    for (int q, i = 0; i < Q; i++) {
        cin >> q;

        int max_r = floor_c[UPPER_BOUND_C] % q;
        for (int j = q; j <= UPPER_BOUND_C; j += q) {
            max_r = max(max_r, floor_c[j - 1] % q);
        }

        cout << max_r << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 4580
final_rank: 1

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;

int main() {
    const int UPPER_BOUND_C = 100000;
    int floor_c[UPPER_BOUND_C + 1] = {};

    int N, Q; cin >> N >> Q;

    for (int c, i = 0; i < N; i++) {
        cin >> c; floor_c[c] = c;
    }

    for (int i = 1; i <= UPPER_BOUND_C; i++) {
        floor_c[i] = max(floor_c[i - 1], floor_c[i]);
    }

    for (int q, i = 0; i < Q; i++) {
        cin >> q;

        int max_r = floor_c[UPPER_BOUND_C] % q;
        for (int j = q; j <= UPPER_BOUND_C; j += q) {
            max_r = max(max_r, floor_c[j - 1] % q);
        }

        cout << max_r << endl;
    }

    return 0;
}
accuracy: 0.8
time_ms: 120
memory_kb: 3728
final_rank: 19

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;

int main() {
    const int UPPER_BOUND_C = 100000;
    int floor_c[UPPER_BOUND_C + 1] = {};

    int N, Q; cin >> N >> Q;

    for (int c, i = 0; i < N; i++) {
        cin >> c; floor_c[c] = c;
    }

    for (int i = 1; i <= UPPER_BOUND_C; i++) {
        floor_c[i] = max(floor_c[i - 1], floor_c[i]);
    }

    for (int q, i = 0; i < Q; i++) {
        cin >> q;

        int max_r = floor_c[UPPER_BOUND_C] % q;
        for (int j = q; j < UPPER_BOUND_C; j += q) {
            max_r = max(max_r, floor_c[j - 1] % q);
        }

        cout << max_r << endl;
    }

    return 0;
}
accuracy: 0.6
time_ms: 120
memory_kb: 3728
final_rank: 20

code_snippet: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <array>
constexpr int MaxValue = 300000;
inline int solve(const std::vector<int>& values , int DivValue){
    int max = 0;
    int ans;
    int LastFlag = 0;
    for(int i = 0; i <= MaxValue; i += DivValue){
        auto itr = std::lower_bound(values.begin() , values.end() , i);
        if(itr == values.end() && LastFlag++) break;
        --itr;
        ans = *itr % DivValue;
        if(max < ans) max = ans;
    }
    return max;
}
int main(){
    int N , Q;
    std::cin >> N >> Q;
    std::vector<int> values(N);
    for(auto& value : values) std::cin >> value;
    std::sort(values.begin() , values.end());
    int DivValue;
    for(int i = 0; i < Q; i++){
        std::cin >> DivValue;
        std::cout << solve(values , DivValue) << std::endl;
    }
    return 0;
}
accuracy: 1
time_ms: 350
memory_kb: 4024
final_rank: 14

code_snippet: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <array>
constexpr int MaxValue = 300000;
inline int solve(const std::vector<int>& values , int DivValue){
    int max = 0;
    int ans;
    for(int i = 0; i <= MaxValue + DivValue; i += DivValue){
        auto itr = std::lower_bound(values.begin() , values.end() , i);
        --itr;
        ans = *itr % DivValue;
        if(max < ans) max = ans;
    }
    return max;
}
int main(){
    int N , Q;
    std::cin >> N >> Q;
    std::vector<int> values(N);
    for(auto& value : values) std::cin >> value;
    std::sort(values.begin() , values.end());
    int DivValue;
    for(int i = 0; i < Q; i++){
        std::cin >> DivValue;
        std::cout << solve(values , DivValue) << std::endl;
    }
    return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 4168
final_rank: 16

code_snippet: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <array>
constexpr int MaxValue = 300000;
inline int solve(const std::vector<int>& values , int DivValue){
    int max = 0;
    int ans;
    for(int i = 0; i <= MaxValue + DivValue; i += DivValue){
        auto itr = std::lower_bound(values.begin() , values.end() , i);
        //if(itr == values.end()) break;
        --itr;
        ans = *itr % DivValue;
        if(max < ans) max = ans;
    }
    return max;
}
int main(){
    int N , Q;
    std::cin >> N >> Q;
    std::vector<int> values(N);
    for(auto& value : values) std::cin >> value;
    std::sort(values.begin() , values.end());
    int DivValue;
    for(int i = 0; i < Q; i++){
        std::cin >> DivValue;
        std::cout << solve(values , DivValue) << std::endl;
    }
    return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 4140
final_rank: 15

code_snippet: #include <iostream>
#include <string>
#include <vector> //sort逆
#include <algorithm>
//sort(vec.begin(), vec.end());
//vec.erase(unique(vec.begin(), vec.end()), vec.end());
#include <math.h>
#include <stdio.h>
#include <iomanip> //fixed<<setprecision()
#include <complex> //複素数
/*
 例 実部1.5、虚部2.5の複素数オブジェクトを作る
complex<double> c(1.5, 2.5);
real	実部を取得する
imag	虚部を取得する
abs	複素数の絶対値を得る
arg	複素数の偏角を得る
norm	複素数体のノルムを得る
conj	共役複素数を得る
proj	リーマン球面への射影を得る	C++11
polar	複素数を極形式で指定して作る
cout<<c; -> (a,b)
*/
#include <tuple> //tuple<int,...> get<1>(a)

using namespace std;
//a=97
const int MOD = 1000000007;
const long long max10_18 = 1000000000000000000;

//2*(2*(2+3))
#define bigint vector<int>
#define bigintint vector<vector<int>>
#define loop(x) for(int i=0;i<x;i++)
#define loop1(x) for(int i1=0;i1<x;i1++)
#define loop2(x) for(int i2=0;i2<x;i2++)
#define loop3(x) for(int i3=0;i3<x;i3++)

#define print1(x) for(int print1_a=0;print1_a<x.size();print1_a++) cout<<x[print1_a]<<" ";
#define print2(x) for(int print2_a=0;print2_a<x.size();print2_a++){ for(int print2_b=0;print2_b<x[print2_a].size();print2_b++) cout<<x[print2_a][print2_b]<<" "; cout<<endl;}

#define ss string
#define dd double
#define ll long long

#define v(x) vector<x>
#define vv(x) vector<vector<x>>

#define get(x,y) get<y>(x) //tuple
static const int MAX=300000;
int to[MAX+1];
bool B[MAX+1];

int main(){
    int N; cin>>N;
    int Q; cin>>Q;
    loop(N){
        int c; cin>>c;
        B[c]=true;
    }

    int m=0;
    loop(MAX+1){
        to[i]=m;
        if(B[i]) m=i;
    }

    loop(Q){
        int q; cin>>q;
        int maxv=0;
        int now=m;
        while(now){
            int p=now%q;
            maxv=max(p,maxv);
            if ( now - p < 0 ) break;
            now=to[now-p];
        }
        cout<<maxv<<endl;
    }
}
accuracy: 1
time_ms: 160
memory_kb: 4608
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
#define myMAX 300003
#define rep(i, n) for (int i = 0; i < (int)(n); i++)//rep(i,回数)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
int main(){
	
	long long int N, Q,t,maxi=0,q,mtmo;
	vector<bool>c(myMAX);
	vector<long long int>l(myMAX);
	cin >> N >> Q;
	rep1 (i,N+1) {
		cin >> t;
		c[t] = true;
	}
	rep1 (i,myMAX) {
		l[i] = maxi;
		if (c[i])maxi = i;
	}
	rep(i, Q) {
		cin >> q;
		mtmo = 0;
		t = maxi;
		while (t) {
			mtmo = max(mtmo, t % q);
			t -= t % q;
			t = l[t];
		}
		cout << mtmo << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 5036
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
#define myMAX 300003
#define rep(i, n) for (int i = 0; i < (int)(n); i++)//rep(i,回数)
int main(){
	
	long long int N, Q,t,maxi=0,q,mtmo;
	vector<bool>c(myMAX);
	vector<long long int>l(myMAX);
	cin >> N >> Q;
	for (int i = 1; i < N + 1; i++) {
		cin >> t;
		c[t] = true;
	}
	for (int i = 1; i < myMAX; i++) {
		l[i] = maxi;
		if (c[i])maxi = i;
	}
	rep(i, Q) {
		cin >> q;
		mtmo = 0;
		t = maxi;
		while (t) {
			mtmo = max(mtmo, t % q);
			t -= t % q;
			t = l[t];
		}
		cout << mtmo << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 5020
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)//rep(i,回数)
int main(){
	long long int N, Q,t,q,kdk,minc=1145141919,maxc=0;
	cin >> N >> Q;
	vector<int>c(300003);
	vector<long long int>l(300003);
	rep(i, N) {
		cin >> t;
		c[t] = 1;
		minc=min(minc, t);
	}
	t = 0;
	for (int i = 1; i < 300001; i++) {
		l[i] = t;
		if (c[i]) {
			t = i;
			maxc = max(t,maxc);
		}
	}


	for (int i = 0; i < Q; i++) {
		cin >> q;
		t = maxc;
		int ta=maxc;
		kdk = 0;
		for (;;) {


			if (kdk < t % q)
				kdk = t % q;
			t -= t % q;
			t = l[t];
			if (t==0)
				break;

		}
		cout << kdk << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 6228
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)//rep(i,888)
#define MAX 300000
long long int N, Q,c,q,s=0,m,p,maxa;
bool a[MAX+1];
int b[MAX + 1];
int main() {

	cin >> N >> Q;
	for (int i = 0; i < N; i++) {
		cin >> c;
		a[c] = true;
	}
	for (int i = 1; i < MAX + 1; i++) {
		b[i] = s;
		if (a[i])s = i;
	}
	for (int i = 0; i < Q; i++) {
		cin >> q;
		maxa = 0;
		int cur = s;
		while (cur) {
			 p = cur%q;
			maxa=max(maxa, p);
			if (cur - p < 0)break;
			cur = b[cur - p];
		}
		cout << maxa << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 4444
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)//rep(i,888)
#define MAX 300000
bool c[MAX + 1];
long long int l[MAX + 1];
long long int N,Q,m,s=0,q,p,maxs;
int main() {
	cin >> N >> Q;
	for (int i = 0; i < N; i++) {
		cin >> m;
		c[m] = true;
	}
	s = 0;
	for (int i = 1; i <= MAX ; i++) {
		l[i] = s;
		if (c[i])s = i;
	}
	for (int i = 0; i < Q; i++) {
		cin >> q;
		maxs = 0;
		int cur = s;
		while (cur) {
			p = cur % q;
			maxs = max(maxs, p);
			if (cur - p < 0)break;
			cur = l[cur - p];
		}
		cout << maxs << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 5648
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)//rep(i,888)
static const int   MYMAX=300000;
bool c[MYMAX + 1];
int l[MYMAX + 1];
int maxa;		int cur;
int n, q, Q, p, u;
int main() {
	cin >> n >> Q;
	for (int i = 0; i < n; i++) {
		cin >> p;
		c[p] = true;
	}
	int m = 0;
	for (int i = 1; i <= MYMAX +1; i++) {
		l[i] = m;
		if (c[i])m = i;
	}
	for (int i = 0; i < Q; i++) {
		cin >> q;
		maxa = 0;
		cur = m;
		while (cur) {
			p = cur % q;
			//maxa = std::max({ maxa, p });
			maxa = max(maxa, p);
			if (cur - p < 0)break;
			cur = l[cur - p];
		}
		cout << maxa << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 4552
final_rank: 8

code_snippet: #include <iostream>
using namespace std;
static const int MAX = 300000;

bool T[MAX + 1];
int L[MAX + 1];


int main() {
    int N, Q, q, c;

    cin >> N >> Q;
    for (int i = 0; i < N; i++) {
        cin >> c;
        T[c] = true;
    }

    int m = 0;
    for (int i = 1; i <= MAX; i++) {
        L[i] = m;
        if (T[i]) m = i;
    }

    for (int i = 0; i < Q; i++) {
        cin >> q;
        int maxv = 0;
        int cur = m;
        while (cur) {
            int p = cur % q;
            maxv = max(maxv, p);
            if (cur < p) break;
            cur = L[cur - p];
        }
        cout << maxv << endl;
    }


    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 4600
final_rank: 9

code_snippet: #include <iostream>
using namespace std;
static const int MAX = 300000;

bool T[MAX + 1];
int L[MAX + 1];


int main() {
    int N, Q, q, c;

    cin >> N >> Q;
    for (int i = 0; i < N; i++) {
        cin >> c;
        T[c] = true;
    }

    int m = 0;
    for (int i = 1; i <= MAX; i++) {
        L[i] = m;
        if (T[i]) m = i;
    }

    for (int i = 0; i < Q; i++) {
        cin >> q;
        int maxv = 0;
        int cur = m;
        while (cur) {
            int p = cur % q;
            maxv = max(maxv, p);
            if (cur < p) break;
            cur = L[cur - p];
        }
        cout << maxv << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 4452
final_rank: 4

code_snippet: #include <iostream>
using namespace std;
static const int MAX = 300000;

bool T[MAX + 1];
int L[MAX + 1];


int main() {
    int N, Q, q, c;

    cin >> N >> Q;
    for (int i = 0; i < N; i++) {
        cin >> c;
        T[c] = true;
    }

    int m = 0;
    for (int i = 1; i <= MAX; i++) {
        L[i] = m;
        if (T[i]) m = i;
    }

    for (int i = 0; i < Q; i++) {
        cin >> q;
        int maxv = 0;
        int cur = m;
        while (cur) {
            int p = cur % q;
            maxv = max(maxv, p);
            if (cur - p < 0) break;
            cur = L[cur - p];
        }
        cout << maxv << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 4540
final_rank: 3

code_snippet: #include <iostream>
using namespace std;
static const int MAX = 300000;

bool T[MAX + 1];
int L[MAX + 1];


int main() {
    int N, Q, q, x;

    cin >> N >> Q;
    for (int i = 0; i < N; i++) {
        cin >> x;
        T[x] = true;
    }

    int m = 0;
    for (int i = 1; i <= MAX; i++) {
        L[i] = m;
        if (T[i]) m = i;
    }

    for (int i = 0; i < Q; i++) {
        cin >> q;
        int maxv = 0;
        int cur = m;
        while (cur) {
            int p = cur % q;
            maxv = max(maxv, p);
            if (cur - p < 0) break;
            cur = L[cur - p];
        }
        cout << maxv << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 4456
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
static const int MAX = 300000;

bool T[MAX+1];
int L[MAX+1];
 
int main() {
    int N, Q, q, x; cin >> N >> Q;
    for(int i=0; i<N; i++) { // Tに格納
        cin >> x;
        T[x] = true;
    }

    int m=0; // mはカードの最大値
    for(int i=1; i<MAX+1; i++) { // Lに格納
        L[i] = m;
        if(T[i]) m = i;
    }

    for(int i=0; i<Q; i++) {
        cin >> q;
        int maxv=0;
        int cur = m;
        while(cur) {
            int p = cur%q;
            maxv = max(maxv, p);
            cur -= p;
            if(cur < 0) break;
            cur = L[cur];
        }
        cout << maxv << endl;
    }
}
accuracy: 1
time_ms: 210
memory_kb: 4540
final_rank: 6