题目链接:uva 10712 - Count the Numbers

题目大意:给出n,a。b。问说在a到b之间有多少个n。

解题思路:数位dp。dp[i][j][x][y]表示第i位为j的时候。x是否前面是相等的。y是否已经出现过n。对于n=0的情况要特殊处理前导0,写的很乱。搓死。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
typedef long long ll;
const int N = 20;
const int M = 1005; ll A, B, n, a[N], dp[N][M][2][2]; void del (ll u, ll* p) {
ll& c = p[0];
c = 0; while (u) {
p[++c] = u % 10;
u /= 10;
} if (c == 0)
p[++c] = 0; for (int i = 1; i <= c / 2; i++)
swap(p[i], p[c-i+1]);
} ll cat (ll u) { if (u == 0)
return 1; int s = 0;
ll f[N][N][2];
memset(f, 0, sizeof(f)); for (int i = 1; i <= a[0]; i++) { for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
f[i][j][1] += f[i-1][k][1];
if (j)
f[i][j][0] += f[i-1][k][0];
else
f[i][j][1] += f[i-1][k][0];
}
} if (a[i] == 0)
s = 1;
else if (i > 1)
f[i][0][1]++; for (int j = 1; j < a[i]; j++)
f[i][j][s]++;
if (i > 1) {
for (int j = 1; j < 10; j++)
f[i][j][0]++;
}
} ll ans = 0;
if (s)
ans++; for (int i = 0; i < 10; i++)
ans += f[a[0]][i][1];
return ans + 1;
} ll solve (ll u) {
if (u < n)
return 0; del(u, a); if (n == 0)
return cat(u); memset(dp, 0, sizeof(dp)); dp[0][0][1][0] = 1; ll v = n, tmp = 1; if (v) {
while (v) {
v /= 10;
tmp *= 10;
}
} else {
tmp = 10;
}
ll mod = tmp / 10; for (int i = 1; i <= a[0]; i++) { for (int j = 0; j < tmp; j++) { for (int k = 0; k < 10; k++) {
int x = (j % mod) * 10 + k; if (x == n) {
dp[i][x][0][1] += (dp[i-1][j][0][0] + dp[i-1][j][0][1]);
if (k < a[i])
dp[i][x][0][1] += (dp[i-1][j][1][0] + dp[i-1][j][1][1]);
else if (k == a[i])
dp[i][x][1][1] += (dp[i-1][j][1][0] + dp[i-1][j][1][1]);
} else {
dp[i][x][0][0] += dp[i-1][j][0][0];
dp[i][x][0][1] += dp[i-1][j][0][1]; if (k < a[i]) {
dp[i][x][0][0] += dp[i-1][j][1][0];
dp[i][x][0][1] += dp[i-1][j][1][1];
} else if (k == a[i]) {
dp[i][x][1][0] += dp[i-1][j][1][0];
dp[i][x][1][1] += dp[i-1][j][1][1];
}
}
}
}
} int c = a[0];
ll ans = 0;
for (int i = 0; i < tmp; i++)
ans += (dp[c][i][0][1] + dp[c][i][1][1]);
return ans;
} int main () {
while (scanf("%lld%lld%lld", &A, &B, &n) == 3) {
if (A == -1 || B == -1 || n == -1)
break;
printf("%lld\n", solve(B) - solve(A-1));
}
return 0;
}

uva 10712 - Count the Numbers(数位dp)的更多相关文章

  1. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  2. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  3. Codeforces Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

  4. poj 3252 Round Numbers(数位dp 处理前导零)

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  5. POJ3252 Round Numbers —— 数位DP

    题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Su ...

  6. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

  7. hdu 4722 Good Numbers( 数位dp入门)

    Good Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. 【BZOJ-1833】count数字计数 数位DP

    1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 2494  Solved: 1101[Submit][ ...

  9. SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]

    题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...

随机推荐

  1. linux soname

    在linux下使用动态库时,经常会发现明明编译时指定的是libA.so,可是程序运行时或通过ldd查看依赖却是libA.so.XXX, 原因跟linux下so库的soname有关,查看so库的sona ...

  2. Linux __attribute__(("hidden"))、default

    记录下: Linux下导出so库接口时在下面情况下无法导出(编译时增加了__attribute__(("hidden"))属性). void * __attribute__((&q ...

  3. LocalCache

    public static class LocalCacheHelper { ; //5分钟过期 public static T GetCache<T>(string cacheKey) ...

  4. DTrace memory leak 内存泄露

    http://blog.sina.com.cn/s/blog_538040b70100eecn.html   如下程序用于跟踪,在分配和回收都会触发探针 #!/usr/sbin/dtrace -s p ...

  5. linux C宏定义 转

    写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性等等.下面列举一些成熟软件中常用得宏定义...... 1,防止一个头文件被重复包含 #ifndef COMDEF_H ...

  6. .Net Discovery 系列之五--深入浅出.Net实时编译机制(上)

    欢迎阅读“.Net Discovery 系列”文章,本文将分上.下两部分为大家讲解.Net JIT方面的知识,敬请雅正. JIT(Just In Time简称JIT)是.Net边运行边编译的一种机制, ...

  7. 使用Brackets

    Brackets功能还是很强大的. 官网:brackets.io常见问题解决:https://github.com/adobe/brackets/wiki/Troubleshooting快捷键:htt ...

  8. msgpack的数据序列和还原

    msgpack的数据序列和还原 msgpack不仅可以序列一些常规的数据类型的数据,比如:string.datetime.integer...... 还能序列olevariant.stream 这就非 ...

  9. <fmt:formatNumber>标签

    <fmt:formatNumber>标签用于格式化数字,百分比,货币. 属性 <fmt:formatNumber>标签有如下属性: 属性 描述 是否必要 默认值 value 要 ...

  10. git push origin master:master

    $git push origin master:master (在local repository中找到名字为master的branch,使用它去更新remote repository下名字为mast ...