Bigger is Better
https://odzkskevi.qnssl.com/91b457ef612009fbd8a70f1852aad0cc?v=1508483406
【题解】
dp[i][j]表示前i位数mod m = j的最小火柴数
据此找到位数i,从高位开始枚举做即可
需要与处理iej mod k的值
mo[i][j][k]表示i * 10^j mod k的值
注意::::::
0 mod 任何非零的数 都是 0!!!!!!!!!
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cstring> 6 #include <vector> 7 #define min(a, b) ((a) < (b) ? (a) : (b)) 8 #define max(a, b) ((a) > (b) ? (a) : (b)) 9 10 inline void swap(long long&a, long long&b) 11 { 12 long long tmp = a;a = b;b = tmp; 13 } 14 15 inline void read(long long&x) 16 { 17 x = 0;char ch = getchar(), c = ch; 18 while(ch < '0' || ch > '9')c = ch, ch = getchar(); 19 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar(); 20 } 21 22 const long long INF = 0x3f3f3f3f; 23 const long long MAXN = 100 + 10; 24 const long long MAXM = 3000 + 10; 25 const long long num[10] = {6,2,5,5,4,5,6,3,7,6}; 26 27 long long n,m,t,dp[MAXN][MAXM],mo[10][MAXN][MAXM]; 28 29 int main() 30 { 31 for(register long long i = 1;i <= 9;++ i) 32 for(register long long j = 0;j <= MAXN - 10;++ j) 33 for(register long long k = 1;k <= MAXM - 10;++ k) 34 mo[i][j][k] = (j == 0 ? i : (mo[i][j - 1][k] * 10)) % k; 35 while(scanf("%d", &n) != EOF && n) 36 { 37 ++ t; 38 printf("Case %lld: ", t); 39 read(m); 40 memset(dp, 0x3f, sizeof(dp)); 41 dp[0][0] = 0; 42 for(register long long i = 0;i < n;++ i) 43 for(register long long j = 0;j < m;++ j) 44 for(register long long k = 0;k <= 9;++ k) 45 dp[i + 1][(j * 10 + k)%m] = min(dp[i + 1][(j * 10 + k)%m], dp[i][j] + num[k]); 46 long long flag = 0; 47 //dp[i][j]表示i位数modm = j的最小火柴数 48 //mo[i][j][k]表示iej mod k的值 49 for(register long long i = n;i >= 1;-- i) 50 { 51 if(dp[i][0] <= n) 52 for(register long long k = 9;k >= 1;-- k) 53 { 54 long long ans = 0; 55 if(dp[i - 1][(m - mo[k][i - 1][m])%m] + num[k] <= n) 56 { 57 flag = 1; 58 printf("%lld", k); 59 long long tmp = num[k], tmp2 = mo[k][i - 1][m]; 60 for(register long long j = i - 1;j >= 1;-- j) 61 { 62 for(register long long kk = 9;kk >= 0;-- kk) 63 { 64 if(dp[j - 1][(m - (tmp2 + mo[kk][j - 1][m]) % m) % m] + num[kk] + tmp <= n) 65 { 66 tmp2 = (tmp2 + mo[kk][j - 1][m]) % m; 67 tmp += num[kk]; 68 printf("%lld", kk); 69 break; 70 } 71 } 72 } 73 putchar(' '); 74 } 75 if(flag)break; 76 } 77 if(flag)break; 78 } 79 if(!flag) 80 { 81 if(n >= 6)printf("0 "); 82 else printf("-1 "); 83 } 84 } 85 return 0; 86 }