This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <queue>
#include <assert.h>
using namespace std;
int N, chk[1000005], from[1000005];
int nums[] = {3, 5, 8};
queue<int> q;
void solve() {
fill(chk, chk + 1 + 1000000, 0);
fill(from, from + 1 + 1000000, -1);
for(int i = 0;i<3;i++) {
q.push(nums[i]);
chk[nums[i]] = 1;
from[nums[i]] = 0;
}
while(!q.empty()) {
int n = q.front(); q.pop();
for(int i = 0;i<3;i++) {
if(n + nums[i] > 1000000) break;
if(!chk[n + nums[i]]) {
chk[n + nums[i]] = 1;
from[n + nums[i]] = n;
q.push(n + nums[i]);
}
}
}
}
void print(int idx) {
if(idx == 0) return;
print(from[idx]);
printf("%d",idx - from[idx]);
}
int main() {
int T;
solve();
scanf("%d",&T);
while(T--) {
scanf("%d",&N);
if(from[N] == -1) {
printf("%d\n", -1);
}
else {
print(N);
printf("\n");
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |