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 ...
随机推荐
- MySQL 集群
MySQL Galera介绍 主要功能: 同步复制 真正的multi-master,即所有节点可以同时读写数据库 自动的节点成员控制,失效节点自动被清除 新节点加入数据自动复制 真正的并行复制,行级 ...
- Codeforces Round #404 (Div. 2) A - Anton and Polyhedrons 水题
A - Anton and Polyhedrons 题目连接: http://codeforces.com/contest/785/problem/A Description Anton's favo ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem B. Travelling Camera Problem set贪心
Problem B. Travelling Camera Problem 题目连接: http://www.codeforces.com/gym/100253 Description Programm ...
- HQL的内连接查询
/** * HQL的内连接查询 * String hql="from Customer c inner join fetch c.linkmans"; */ @Test publi ...
- 移动前端开发和 Web 前端开发的区别是什么
可以分成两部分理解1.服务器端开发,也叫后台开发,这是唯一的,对应不同的平台,他负责数据的分发与存储,和一些逻辑的处理.逻辑处理的多少由业务的复杂程度决定.服务端相对独立,与平台没啥关系. 2..1中 ...
- FireDAC 下的 Sqlite [3] - 获取数据库的基本信息
在空白窗体上添加: TFDConnection, TFDPhysSQLiteDriverLink, TFDGUIxWaitCursor, TMemo procedure TForm1.FormCrea ...
- C++ STL set::find的用法
参考: http://blog.csdn.net/lihao21/article/details/6302196 /* class for function predicate * - opera ...
- OFbiz--简单介绍
一.简单介绍 OFBiz是一个很著名的电子商务平台,是一个很著名的开源项目,提供了创建基于最新J2EE/XML规范和技术标准,构建大中型企业级.跨平台.跨数据库.跨应用server的多层.分布式电子商 ...
- Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(6) Locale
本章介绍Locale. 1 Locale 介绍 Locale 表示地区.每一个Locale对象都代表了一个特定的地理.政治和文化地区. 在操作 Date, Calendar等表示日期/时间的对象时,经 ...
- iOS 32位、 64位系统兼容性设置-Xcode创建支持IOS4.3以上版本的应用的方法
方法一: 如果是Xcode 5的话步骤为 点击项目名称->Build Settings->搜索 Architectures 这个里面的原始的值是Standard architectures ...