题目链接:poj 3208 Apocalypse Someday
题目大意:给定n,输出第n大包含666的数字。
解题思路:数位dp,用类似AC自动机的思想进行转移。首先dp[i][j]表示说i位最后有j个连续6的情况数,这个预处理出
来。那么dp[i][3]即为i位有多少个满足的数。给定n,先确定位数d。然后从最高位向下判断,一开始肯定是需要3个连续
的6,所以u为3,然后根据后面添加的数字动态修改u值进行判断。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxd = 10;
const int status = 4;
const int g[4][12] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{3, 3, 3, 3, 3, 3, 0, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 1, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 2, 3, 3, 3}};
ll n, dp[maxd+5][status];
int main () {
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= maxd; i++) {
dp[i][0] = (dp[i-1][0] + dp[i-1][1] + dp[i-1][2]) * 9;
dp[i][1] = dp[i-1][0];
dp[i][2] = dp[i-1][1];
dp[i][3] = dp[i-1][3] * 10 + dp[i-1][2];
}
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%lld", &n);
int d = 0, u = 3;
while (dp[d][3] < n)
d++;
while (d) {
ll k = 0;
// printf("%d:\n", d);
for (int i = 0; i < 10; i++) {
ll tmp = 0;
for (int j = 3; j >= g[u][i]; j--)
tmp += dp[d-1][j];
// printf("n:%lld status: %d %d %lld\n", n, u, i, tmp);
if (k + tmp >= n) {
printf("%d", i);
u = g[u][i];
break;
}
k += tmp;
}
n -= k;
d--;
}
printf("\n");
}
return 0;
}