uva 10712 - Count the Numbers(数位dp)
题目链接: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)的更多相关文章
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 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 ...
- poj 3252 Round Numbers(数位dp 处理前导零)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- POJ3252 Round Numbers —— 数位DP
题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Su ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- hdu 4722 Good Numbers( 数位dp入门)
Good Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 【BZOJ-1833】count数字计数 数位DP
1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 2494 Solved: 1101[Submit][ ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
随机推荐
- 19. 删除链表的倒数第N个节点
19. 删除链表的倒数第N个节点 题意 删除链表的倒数第N个结点 解题思路 先让快结点移动n个位置,接着再让慢结点和快结点同时移动,发现出慢结点就是要删除的结点,将前结点指向删除结点的下一个结点即可: ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem H. Parallel Worlds 计算几何
Problem H. Parallel Worlds 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7 ...
- [原创]SOAPUI工具介绍
[原创]SOAPUI工具介绍 一 官方网站:http://www.soapui.org/二 下载地址:http://sourceforge.net/projects/soapui/files/三 so ...
- jQueryUI常用功能实战
本系列文章导航 从零开始学习jQuery (一) 开天辟地入门篇 从零开始学习jQuery (二) 万能的选择器 从零开始学习jQuery (三) 管理jQuery包装集 从零开始学习jQuery ( ...
- tomcat开启SSL8443端口的方法
参考文献: http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html http://blog.sina.com.cn/s/blog_682b5aa1 ...
- SQL 版本说明
http://www.cnblogs.com/SameZhao/p/6184924.html The ProductMajorVersion产品主版本号 如: 12为 SQL SERVER 2014 ...
- mixpanel实验教程(1)
一.关于 mixpanel 这个我不想多说,不明确请看官方手冊:https://mixpanel.com/help/reference/ 二.注冊 mixpanel.com 是一个商业机构.它的用户分 ...
- 在Delphi中DBGrid有一个MouseMove事件,当鼠标移动时怎么知道光标在哪个单元格上面
procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);var coords:TGr ...
- [Bug]Unable to start process dotnet.exe
This morning I did a sync of a repo using of Visual Studio and then tried to run a web application I ...
- 基于设备树的TQ2440 DMA学习(3)—— DMA控制器驱动
作者 彭东林pengdonglin137@163.com 平台 TQ2440Linux-4.9 概述 上一篇直接操作DMA控制器实现了一个mem2mem的DMA传输,但是这样不符合linux driv ...