题目链接
https://ac.nowcoder.com/acm/contest/8827/F
题意
给出一定数量的火柴棍,将这些火柴棍按照七段码形式摆放组成数字 A
问满足以上情况的最大数字 A 称为魔法数字
思路
直接暴力枚举所有满足情况的数字 A ,随着k的增大,魔法数字的总数是不断减小的。以下证明
代码
#include <bits/stdc++.h> #define sc(x) scanf("%lld", &(x)) #define pr(x) printf("%lld\n", x) #define mem(x, y) memset(x, y, sizeof(x)) #define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define pt putchar(10) #define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++) #define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--) using namespace std; typedef __int128 ll; const int N = 1e5 + 7; const ll mod = 1e9 + 7; const double PI = acos(-1); const int INF = 0x3f3f3f3f; ll m, n, i, j; int c[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; ll ans[10010]; inline void write(__int128 x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } void dfs(int x, ll now, string s, int cost) { int i, j; if (x > 0) ans[cost] = max(ans[cost], now); int start = 0; if (x == 0) start = 1; for (i = start; i <= 9; i++) { if ((now * 10 + i) % (x + 1) == 0) { char ch = i + '0'; dfs(x + 1, now * 10 + i, s + ch, cost + c[i]); } } return; } string t; int main() { mem(ans, -1); dfs(0, 0, "", 0); cin >> t; if (t.size() > 7) cout << "-1" << endl; else { n = atoi(t.c_str()); write(ans[n]); } }