HDU 3652

这题是成都赛区网络赛的一题。题意是计算1-n中有多少个数字既是13的倍数同时又含有字串13,1 <= n <= 1000000000。

#include using namespace std; int dp[9][3][13]; int GetCount(int ss, int sr, int sl) { memset(dp, 0, sizeof(dp)); dp[0][ss][sr] = 1; for (int i = 0; i < sl; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 13; k++) { if (dp[i][j][k] > 0) { for (int x = 0; x <= 9; x++) { int r = (k * 10 + x) % 13; int s = 0; if (j == 2) { s = 2; } else if (x == 1) { s = 1; } else if (x == 3 && j == 1) { s = 2; } dp[i+1][s][r] += dp[i][j][k]; } } } } } return dp[sl][2][0]; } int GetSta(int num) { int s = 0; int a = 0, b = 0; if (num % 10 == 1) { s = 1; } while (num) { b = a; a = num % 10; num /= 10; if (a == 1 && b == 3) { s = 2; } } return s; } int e[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; void solve(int n) { int ans = 0, sn = 0; for (int i = 8; i >= 0; i--) { while (sn + e[i] - 1 <= n) { int ss = GetSta(sn / e[i]); int sr = (sn / e[i]) % 13; ans += GetCount(ss, sr, i); sn += e[i]; } } printf("%d/n", ans); } int main() { int n; while (scanf("%d", &n) != EOF) { solve(n); } return 0; } 

 

你可能感兴趣的:(HDU 3652)