题目链接: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 系统及编程相关知识总汇

    Linux  C function() 参考手册 STL 学习文档 Linux内核

  2. 关于dubbo服务的xml配置文件报错的问题

    在配置dubbo服务的过程中,经常会遇到虽然程序能够跑起来,但是配置文件一堆红叉,虽然不影响功能,但是确实很让人恶心. 报错信息如下: Multiple annotations found at th ...

  3. 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)(转)

    对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过 Python语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...

  4. MySQL错误:TIMESTAMP with implicit DEFAULT value is deprecated

    用于存放数据库的文件夹不为空,清空了再来一次!

  5. HDU 4733 G(x) (2013成都网络赛,递推)

    G(x) Time Limit: 2000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. Golang 处理 Json(二):解码

    golang 编码 json 还比较简单,而解析 json 则非常蛋疼.不像 PHP 一句 json_decode() 就能搞定.之前项目开发中,为了兼容不同客户端的需求,请求的 content-ty ...

  7. 【Go命令教程】10. go fix 与 go tool fix

    命令 go fix 会把指定 代码包 的所有 Go 语言源码文件中的旧版本代码修正为新版本的代码.这里所说的版本即 Go 语言的版本.代码包的所有 Go 语言源码文件不包括其子代码包(如果有的话)中的 ...

  8. 打通Linux脉络系列:进程、线程和调度

    http://edu.csdn.net/huiyiCourse/series_detail/60 http://edu.csdn.net/mycollege

  9. [转]LRU缓存实现(Java)

    LRU Cache的LinkedHashMap实现 LRU Cache的链表+HashMap实现 LinkedHashMap的FIFO实现 调用示例 LRU是Least Recently Used 的 ...

  10. 一个例子来看C#泛型是如何登场的

    有这样一个有关汽车的类. public class Car { public int ID { get; set; } public string Make { get; set; } } 现在,在客 ...