dp[i][j][k]表示第i位填数字k时,与后面的相连模数为j时,后面的数字最小填多少。

测得我提心吊胆还以为复杂度高了,结果出来46ms还是cf评测姬强啊。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <fstream>
#include <bitset>
#define init(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define irep(i, a, b) for (int i = a; i >= b; i--)
using namespace std; typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18; template <typename T> void read(T &x) {
x = 0;
int s = 1, c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') s = -1;
for (; isdigit(c); c = getchar())
x = x * 10 + c - 48;
x *= s;
} template <typename T> void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
} template <typename T> void writeln(T x) {
write(x);
puts("");
} const int maxn = 1e3 + 5;
char str[maxn];
int p, st, m;
int dp[maxn][maxn][10], Tenpow[maxn]; int main() {
scanf("%s%d", str + 1, &p);
int n = strlen(str + 1); for (int i = 1, t = 1; i <= n; i++, t = t * 10 % p) Tenpow[n - i + 1] = t; init(dp, -1);
rep(i, 0, 9)
dp[n + 1][0][i] = 0;
irep(i, n + 1, 2)
irep(j, p - 1, 0) {
if (i == n + 1 && j) continue;
rep(k, 0, 9) {
if (dp[i][j][k] == -1) continue; if (str[i - 1] != '?') {
int d = str[i - 1] - '0';
dp[i - 1][(d * Tenpow[i - 1] % p + j) % p][d] = k;
} else {
rep(t, 0, 9) {
dp[i - 1][(t * Tenpow[i - 1] % p + j) % p][t] = k;
}
}
break;
}
} rep(i, 1, 9)
if (dp[1][0][i] >= 0) {
st = i;
break;
}
if (st) {
rep(i, 1, n) {
printf("%d", st);
int d = st;
st = dp[i][m][d];
m = (m - d * Tenpow[i] % p + p) % p;
}
} else puts("*");
return 0;
}

最新文章

  1. Block产生的内存泄露,以及解决方法
  2. css总结
  3. 为什么这样写js:(function ($) { })(jQuery);
  4. VS2015生成64位dll文件
  5. MYSQL管理之主从同步管理 转载
  6. Android4.2增加新键值
  7. 使apache解析域名到目录的方法
  8. javascript 学习总结(九)面向对象编程
  9. muduo库整体架构简析
  10. seo从业者发展方向
  11. 调试和运行matlab代码(源程序)的技巧和教程
  12. db2服务器端授权
  13. GlusterFS分布式存储系统中更换故障Brick的操作记录1
  14. 排序算法(8)--Merge Sorting--归并排序--Merge sort--归并排序
  15. JavaScript:Events
  16. EGL 1.0 学习笔记
  17. c# &amp;与&amp;&amp; 和 |与||的区别
  18. P1546 最短网络(codevs | 2627村村通)
  19. String、Date、Calendar之间的转换
  20. [004] last_k_node

热门文章

  1. 使用单例模式设计PDO数据操作类
  2. 简单封装微信小程序
  3. 脚踏实地学C#3-装箱和拆箱
  4. Canvas HTML5
  5. 在新建FileInputStream时使用当前相对路径或者绝对路径作为参数的问题
  6. 本机连接调试Erlang结点与rebar3编译
  7. hdu-4989 Summary(水题)
  8. 51Nod - 1055:最长等差数列 (求最长的等差数列)
  9. HihoCoder 1473 : 小Ho的强迫症( 欧几里得 )
  10. 【EOJ Monthly 2018.2 (Good bye 2017)】