[题目链接]
http://poj.org/problem?id=3208
[算法]
数位DP
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; int T; long long n; long long f[25][5]; inline void dp() { int i,j; f[0][0] = 1; for (i = 0; i <= 20; i++) { for (j = 0; j < 3; j++) { f[i + 1][j + 1] += f[i][j]; f[i + 1][0] += f[i][j] * 9; } f[i + 1][3] += f[i][3] * 10; } } inline void calc(long long n) { int i,j,k,len,now; long long sum; for (len = 3; f[len][3] < n; len++); now = 0; sum = 0; for (i = len; i >= 1; i--) { for (j = 0; j <= 9; j++) { if (j == 6 || now == 3) { sum = f[i - 1][3]; for (k = max(3 - now - (j == 6),0); k < 3; k++) sum += f[i - 1][k]; if (sum < n) n -= sum; else { printf("%d",j); if (now < 3 && j == 6) now++; break; } } else { if (f[i - 1][3] < n) n -= f[i - 1][3]; else { now = 0; printf("%d",j); break; } } } } printf(" "); } int main() { dp(); scanf("%d",&T); while (T--) { scanf("%lld",&n); calc(n); } return 0; }