GYM 101889E(dp)
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;
}
GYM 101889E(dp)的更多相关文章
- Gym - 100989M(dp)
George met AbdelKader in the corridor of the CS department busy trying to fix a group of incorrect e ...
- GYM 101933A(dp)
要点 \(\sum{w_i} <= 1e8\)是有意味的. 设\(dp[i]\)为至少可以承受重量\(i\)的最大可达高度.转移时可以转移的\(j\)必须满足加上它之后得保证各层不能超重,所以\ ...
- GYM 101673G(dp)
dp[i][j][0/1]:第i天处于第j状态要不要吃. const int maxn = 1e2 + 5; int n, a[maxn], b[maxn]; int dp[maxn][maxn][2 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
随机推荐
- 高精度乘法(FFT)
学会了FFT之后感觉自己征服了世界! 当然是幻觉... 不过FFT还是很有用的,在优化大规模的动规问题的时候有极大效果. 一般比较凶残的计数动规题都需要FFT(n<=1e9). 下面是高精度乘法 ...
- Codeforces Round #363 (Div. 2) B. One Bomb —— 技巧
题目链接:http://codeforces.com/contest/699/problem/B 题解: 首先统计每行每列出现'*'的次数,以及'*'出现的总次数,得到r[n]和c[m]数组,以及su ...
- ckplayer 中的style.swf 中的 style.xml 中的修改方法
style.swf ---- > style.zip ---- > 解压成文件夹 ---- > 打开style.xml ---- > 修改 最重要的是修改保存style.xml ...
- NSMutableURLRequest,在POST方式下传递参数
1. [代码][C/C++]代码 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; NSUs ...
- codeforces B. Ping-Pong (Easy Version) 解题报告
题目链接:http://codeforces.com/problemset/problem/320/B 题目意思:有两种操作:"1 x y" (x < y) 和 " ...
- Android Studio 使用Gradle多渠道打包
第一步:配置AndroidManifest.xml 以友盟渠道为例,渠道信息一般都是写在 AndroidManifest.xml文件中,大约如下: <meta-data android:name ...
- POJ 2970 The lazy programmer(贪心+单调优先队列)
A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is ...
- 解决最近windows版本Node.js中npm出现的“Error: ENOENT, stat 'C:\Users\UserName\AppData\Roaming\npm”的问题
(转载请注明出处,from www.cnblogs.com/xdxer) 问题可能如下所示 解决方案: 在 'C:\Users\UserName\AppData\Roaming\‘ 下手动的增加一个文 ...
- HDU2444(二分图判定+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- plsql 免oracle客户端安装
PLSQL Developer 11.0.0.1762 中文绿色注册版(免Oracle11g客户端) 免安装Oracle客户端,绿色无公害. 说明: 1.点击 "启动PLSQL.exe&qu ...