# UV 12105-Bigger is Better (dp)

Source: Internet
Author: User

Link to the question: Ultraviolet A 12105-Bigger is Better

There are n matches, and a number can be divided by m, and the maximum.

Solution: dp [I] [j] indicates that I match is used to form a number that modulo m and j, but the State retains a string.

```#include

#include

#include

#include

using namespace std;const int N = 105;const int M = 3005;const int need[N] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};int n, m;char dp[N][M][55];bool cmp(char* a, char* b) {int la = strlen(a);int lb = strlen(b);if (la != lb)return la < lb;return strcmp(a, b) < 0;}void solve (char* a, char* b, int k) {char tmp[55];memset(tmp, 0, sizeof(tmp));strcpy(tmp, b);int len = strlen(tmp);if (tmp[0] == '0')tmp[0] = '0' + k;elsetmp[len] = '0' + k;if (cmp(a, tmp))strcpy(a, tmp);}int main () {int cas = 1;while (scanf("%d", &n) == 1 && n) {scanf("%d", &m);char ans[55];memset(ans, 0, sizeof(ans));memset(dp, 0, sizeof(dp));dp[0][0][0] = '0';for (int i = 0; i <= n; i++) {for (int j = 0; j <= m; j++) {if (strlen(dp[i][j]) == 0)continue;for (int k = 0; k < 10; k++) {if (i + need[k] > n) continue;int t = (j*10 + k)%m;solve(dp[i+need[k]][t], dp[i][j], k);}}if (i && cmp(ans, dp[i][0]))memcpy(ans, dp[i][0], sizeof(dp[i][0]));}printf("Case %d: ", cas++);if (ans[0] == '\0')printf("-1\n");elseprintf("%s\n", ans);}return 0;}

```

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.