HDU - 6644 11 Dimensions (dp,思维暴力)

Problem Description

11 Dimensions is a cute contestant being talented in math. One day, 11 Dimensions came across a problem but didn't manage to solve it. Today you are taking training here, so 11 Dimensions turns to you for help.

You are given a decimal integer S with n bits s1s2…sn(0≤si≤9), but some bits can not be recognized now(replaced by "?''). The only thing you know is that Sis a multiple of a given integer m.

There may be many possible values of the original S, please write a program to find the k-th smallest value among them. Note that you need to answer q queries efficiently.

Input

The first line of the input contains an integer T(1≤T≤10000), denoting the number of test cases.

In each test case, there are three integers n,m,q(1≤n≤50000,2≤m≤20,1≤q≤100000) in the first line, denoting the length of S, the parameter m, and the number of queries.

In the second line, there is a string s of length n, denoting the given decimal integer S. It is guaranteed that si is either an integer within [0,9] or ``?'', and s1 is always an integer within [1,9].

For the next q lines, each line contains an integer ki(1≤ki≤1018), denoting each query.

It is guaranteed that ∑n≤500000 and ∑q≤10^6.

Output

For each query, print a single line containing an integer, denoting the value of S. If the answer exists, print Smod(10^9+7) instead, otherwise print ``-1''.

Sample Input

1
5 5 5
2??3?
1
2
3
100
10000

Sample Output

20030
20035
20130
24935
-1

 

      多次查询第K小的使所给数字%m==0的数字是多少。

      先进行dp,dp[i][j]表示填了i个之后取模等于J的情况有多少种。因为可知k<=1e18的,所以::

       然后开始从大到小尝试放数就可以了。

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 50005;
const int mod = 1e9 + 7;
#define ll long long
#define itn int
const ll INF = 1e18 + 5;
int pow_mod(ll a, ll b, ll c) {
	ll ans = 1;
	while (b > 0) {
		if (b & 1) ans = ans * a % c;
		a = a * a % c;
		b /= 2;
	}
	return ans;
}
ll dp[maxn][20];
int pos[maxn];
ll pm[maxn], pmod[maxn];
string res;
int n, m, q, cnt = 0;
void init() {
	for (int i = 1; i <= cnt; i++) {
		pm[i] = pow_mod(10, pos[i], m);
		pmod[i] = pow_mod(10, pos[i], mod);
	}
	for (int i = 0; i <= cnt; i++) {
		for (int j = 0; j < 20; j++)
			dp[i][j] = 0;
	}
	dp[0][0] = 1;
}
int main() {
	int te;
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> te;
	while (te--) {
		cin >> n >> m >> q;
		cin >> res; int len = n; cnt = 0;
		ll tmpans = 0, summ = 0;//不算问号的数字的取余mod,不算问号的数字的取余m
		for (int i = 0; i < len; i++) {
			tmpans *= 10; summ *= 10;
			if (res[i] != '?') {
				tmpans += res[i] - '0';
				summ += res[i] - '0';
			}
			summ %= m; tmpans %= mod;
		}
		for (int i = len - 1; i >= 0; i--) {
			if (res[i] == '?') {
				pos[++cnt] = len - i - 1;
			}
		}
		summ = (m - summ) % m;
		init();
		for (int i = 1; i <= cnt; i++) {
			for (int j = 0; j <= 9; j++) {
				int now = j * pm[i] % m;
				for (int k = 0; k < m; k++) {
					int ne = (now + k) % m;
					dp[i][ne] += dp[i - 1][k];
					dp[i][ne] = min(dp[i][ne], INF);
				}
			}
		}
		while (q--) {
			ll ans = tmpans;
			ll k; cin >> k;
			if (dp[cnt][summ] < k) {
				cout << "-1\n";
				continue;
			}
			int now = summ;
			for (int i = min(cnt, 30); i >= 1; i--) {
				for (int j = 0; j <= 9; j++) {
					int ne = ((now - j * pm[i] % m) + m) % m;
					if (dp[i - 1][ne] < k) {
						k -= dp[i - 1][ne];
					}
					else {
						ans += j * pmod[i] % mod;
						ans %= mod;
						now = ne;
						break;
					}
				}
			}
			cout << ans % mod << "\n";
		}
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值