NC216203. Multipleof8
描述
输入描述
The first line contains a single integer — the number of test cases.
Each test case has numbers represent the amount of to .
输出描述
For each test case, output a positive integer that is a multiple of , or output to indicate that there is no such answer.
示例1
输入:
5 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0
输出:
8 16 64 -1 88888888888888888888
C++(clang++11) 解法, 执行用时: 597ms, 内存消耗: 2844K, 提交时间: 2020-12-26 17:13:34
#include<bits/stdc++.h> using namespace std; int t, a[11]; string ans; void update(string &s) { if(s.size() > ans.size() || s.size() == ans.size() && s > ans) ans = s; } void f(int n) { string s = to_string(n); int b[11]; memcpy(b, a, sizeof(a[0])*10); for(auto ch : s) b[ch-'0']--; for(int i = 0; i < 10; i++) if(b[i] < 0) return; if(n) update(s); for(; s.size() < 3; s = "0" + s); memcpy(b, a, sizeof(a[0])*10); for(auto ch : s) b[ch-'0']--; string prefix; for(int i = 9; i >= 0; i--) { if(b[i] < 0) return; else for(; b[i]--; prefix += char(i+'0')); } s = prefix + s; if(s[0] != '0') update(s); } int main() { for(cin >> t; t--; ) { ans = ""; for(int i = 0; i < 10; i++) cin >> a[i]; for(int i = 0; i < 1000; i += 8) f(i); cout << (ans == "" ? "-1" : ans) << "\n"; } }