HDU_3555
2014-08-23 14:25
92 查看
题意:就是计算出从1-N之间有多少个49,与2089类似。
但是最后的答案为N+1减去所求的答案。
代码:
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
__int64 f[100][2], bit[100];
__int64 dfs(int p, int s, int e) {
if (p == -1) {
return 1;
}
if (!e && f[p][s]!=-1) return f[p][s];
__int64 res = 0;
int u = e ? bit[p] : 9;
for (int i = 0; i <= u; ++i) {
if(i==9&&s) {
continue;
}
res += dfs(p-1, i==4, e&&i==u);
}
return e ? res : f[p][s] = res;
}
__int64 cal(__int64 x) {
int idx = 0;
while (x) {
bit[idx++] = x % 10;
x /= 10;
}
return dfs(idx-1, 0, 1);
}
int main() {
__int64 T, l, r;
memset(f, -1, sizeof (f));
while(~scanf("%I64d", &T)) {
while (T--) {
scanf("%I64d",&r);
printf("%I64d\n", r-cal(r)+1);
}
}
return 0;
}
但是最后的答案为N+1减去所求的答案。
代码:
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
__int64 f[100][2], bit[100];
__int64 dfs(int p, int s, int e) {
if (p == -1) {
return 1;
}
if (!e && f[p][s]!=-1) return f[p][s];
__int64 res = 0;
int u = e ? bit[p] : 9;
for (int i = 0; i <= u; ++i) {
if(i==9&&s) {
continue;
}
res += dfs(p-1, i==4, e&&i==u);
}
return e ? res : f[p][s] = res;
}
__int64 cal(__int64 x) {
int idx = 0;
while (x) {
bit[idx++] = x % 10;
x /= 10;
}
return dfs(idx-1, 0, 1);
}
int main() {
__int64 T, l, r;
memset(f, -1, sizeof (f));
while(~scanf("%I64d", &T)) {
while (T--) {
scanf("%I64d",&r);
printf("%I64d\n", r-cal(r)+1);
}
}
return 0;
}
相关文章推荐
- hdu 3555
- hdu 3555 数位dp
- (数位DP 1.2)hdu 3555 Bomb(统计1~n中,包含49的数的个数)
- HDU3555——Bomb(数位DP)
- hdu 3555 Bomb ( 数位DP)
- hdu 3555 Bomb(数位dp)
- HDU 3555 Bomb (数位DP)
- HDU 3555 数位DP
- hdu 3555【数位DP】
- HDU - 3555 Bomb (数位DP)
- HDU 3555 Bomb (数位dp)
- HDU - 3555 Bomb(数位dp)
- hdu 3555 数位dp的dfs写法
- hdu 3555 数位dp入门
- hdu 3555 Bomb(数位DP)
- HDU 3555 Bomb题解(数位dp)
- 数位DP入门题 hdu 2089 hdu 3555
- HDU3555——Bomb(数位DP)
- HDU 2089 & HDU 3555 数位dp
- hdu 3555 Bomb 【数位DP】