#include <iostream>
#include <queue>
using namespace std;
int N, chk[100005], from[100005];
int nums[] = {3, 5, 8};
void solve() {
fill(chk, chk + 1 + 100000, 0);
fill(from, from + 1 + 100000, -1);
queue<int> q;
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] > 100000) break;
if(!chk[n + nums[i]]) {
chk[n + nums[i]] = 1;
from[n + nums[i]] = n;
q.push(n + nums[i]);
}
}
}
}
int main() {
int T;
solve();
scanf("%d",&T);
while(T--) {
scanf("%d",&N);
if(from[N] == -1) {
printf("%d\n", -1);
}
else {
vector<int> res;
int idx = N;
while(idx != 0) {
res.push_back(idx - from[idx]);
idx = from[idx];
}
for(int i = res.size() - 1;i>=0;i--)
printf("%d",res[i]);
printf("\n");
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2160 KB |
Output is correct |
2 |
Correct |
1 ms |
2160 KB |
Output is correct |
3 |
Correct |
1 ms |
2160 KB |
Output is correct |
4 |
Correct |
1 ms |
2160 KB |
Output is correct |
5 |
Correct |
1 ms |
2160 KB |
Output is correct |
6 |
Correct |
1 ms |
2160 KB |
Output is correct |
7 |
Correct |
1 ms |
2160 KB |
Output is correct |
8 |
Correct |
1 ms |
2160 KB |
Output is correct |
9 |
Correct |
0 ms |
2160 KB |
Output is correct |
10 |
Correct |
1 ms |
2160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
2156 KB |
SIGSEGV Segmentation fault |
2 |
Halted |
0 ms |
0 KB |
- |