HDU 6644 11 Dimensions(DP+思维)

传送门

/*************************************************************************
	> File Name: 3.cpp
	> Author: Knowledge_llz
	> Mail: 925538513@qq.com 
	> Blog: https://www.cnblogs.com/Knowledge-Pig/ 
	> Created Time: 2021/11/11 19:47:51
 ************************************************************************/

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define LL long long
#define pb push_back
#define fi first
#define se second
#define pr pair<int,int>
#define mk(a,b) make_pair(a,b)
#define endl '\n'
using namespace std;
const LL inf = 1e18;
const int maxx = 6e4 + 10, mod = 1e9 + 7;
LL n, m, q, p[maxx][21], a[maxx];
unsigned LL dp[maxx][21];
char s[maxx];
void init(){
	for(int j = 1; j <= 20; ++j){
		p[0][j] = 1;
		for(int i = 1; i <= 50000; ++i)
			if(j > 1) p[i][j] = p[i - 1][j] * 10 % j;
			else p[i][j] = p[i - 1][j] * 10 % mod;
	}
}
void clear(){
	for(int i = 0; i <= n; ++i) for(int j = 0; j <= m; ++j) dp[i][j] = 0;
}
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	init();	int T; cin >> T;
	while(T--){
		cin >> n >> m >> q;
		cin >> s + 1;
		LL x = 0, sum = 0; int cnt = 0;
		for(int i = n; i >= 1; --i){
			if(s[i] != '?'){
				x = (x + p[n - i][m] * (s[i] - '0')) % m;
				sum = (sum + p[n - i][1] * (s[i] - '0')) % mod;
			}
			else a[++cnt] = n - i;
		}
		x = (m - x) % m;
		clear();
		dp[0][0] = 1;
		bool flag = 1; 
		for(int i = 1; i <= cnt && flag; ++i)
			for(int j = 0; j <= 9 && flag; ++j)
				for(int k = 0; k < m; ++k){
					int t = (k + j * p[a[i]][m]) % m;
					dp[i][t] = dp[i][t] + dp[i - 1][k];
					if(dp[i][t] > inf || dp[i][t] < 0) dp[i][t] = inf + 10;
				}
		while(q--){
			LL k, now = x, ans = sum;
			cin >> k;
			if(dp[cnt][now] < k) cout << -1 << endl;
			else{
				for(int i = min(cnt, 20); i >= 1; --i){
					for(int j = 0; j < 10; ++j){
						int nxt = ((now - j * p[a[i]][m]) % m + m) % m;
						if(dp[i - 1][nxt] < k) k -= dp[i - 1][nxt];
						else{
							ans = (ans + p[a[i]][1] * j) % mod;
							now = nxt;
							break;
						}
					}
				}
				cout << ans << endl;
			}
		}
	}
	return 0;
}
posted @ 2021-11-12 19:16  Knowledge-Pig  阅读(89)  评论(0)    收藏  举报