【HDOJ】3652 B-number

时间:2021-03-06 06:13:02

终于自己写出来一道数位DP啊。继续训练DP。

 /* 3652 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxl = ;
int dp[maxl][][][];
char s[]; void solve() {
int len = strlen(s); rep(i, , len)
s[i] -= ''; memset(dp, , sizeof(dp));
dp[][][][] = ; rep(i, , len) {
rep(j, , ) {
rep(k, , ) {
if (!dp[i][][j][k])
continue;
int dtmp = (i==) ? s[i]+:;
rep(d, , dtmp) {
int jj = (j* + d)%;
int kk = ;
int p = ; if (k == ) {
kk = ;
} else if (k == ) {
if (d == ) {
kk = ;
} else if (d == ) {
kk = ;
}
} else {
if (d == ) {
kk = ;
}
} if (i== && d==s[i])
p = ; dp[i+][p][jj][kk] += dp[i][][j][k];
}
}
} rep(j, , ) {
rep(k, , ) {
if (!dp[i][][j][k])
continue;
rep(d, , s[i]+) {
int jj = (j* + d)%;
int kk = ;
int p = ; if (k == ) {
kk = ;
} else if (k == ) {
if (d == ) {
kk = ;
} else if (d == ) {
kk = ;
}
} else {
if (d == ) {
kk = ;
}
} if (d == s[i])
p = ; dp[i+][p][jj][kk] += dp[i][][j][k];
}
}
}
} int ans = dp[len][][][];
printf("%d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int x; while (scanf("%d", &x)!=EOF) {
++x;
sprintf(s, "%d", x);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}