POJ2282:The Counting Problem(数位DP)
Description
1024 1025 1026 1027 1028 1029 1030 1031 1032
there are ten 0's in the list, ten 1's, seven 2's, three 3's, and etc.
Input
Output
Sample Input
1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0
Sample Output
1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106
16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200
413 1133 503 503 503 502 502 417 402 412
196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471
294 1256 296 296 296 296 287 286 286 247
题意:求出区间内0~9的个数
思路:dp[i][j],代表长度为i的数字里面共有几个j
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; __int64 dp[9] = {1,10,100,1000,10000,100000,1000000,10000000,100000000};
//只考虑以某个数开头,例如9XXX,不算XXX里的9,共头1000个9
__int64 solve(__int64 n,__int64 pos)
{
__int64 left,m,sum = 0;
__int64 i;
for(i = 1; i<9; i++)
{
left = n/dp[i];//以123为例,第一次循环求出得出12
if(!pos)
left--;
sum+=left*dp[i-1];//12后面的数,每种都只有1个
m = (n%dp[i]-n%dp[i-1])/dp[i-1];//求出12后面的数确切是什么
if(m>pos)
sum+=dp[i-1];//因为m>pos,所以pos的数目即为m后面所有这个数字的和,而m是第i位,所以总和加上dp[i-1]
else if(m==pos)
sum+=n%dp[i-1]+1;//求出m后的数字是几,总数还要加上m本身的个数
if(n<dp[i])//退出条件
break;
}
return sum;
} int main()
{
__int64 n,m;
__int64 i;
while(~scanf("%I64d%I64d",&n,&m),n+m)
{
if(n>m)
swap(n,m);
printf("%I64d",solve(m,0)-solve(n-1,0));
for(i = 1; i<=9; i++)
printf(" %I64d",solve(m,i)-solve(n-1,i));
printf("\n");
} return 0;
}
POJ2282:The Counting Problem(数位DP)的更多相关文章
- 『The Counting Problem 数位dp』
The Counting Problem Description 求 [L,R]内每个数码出现的次数. Input Format 若干行,一行两个正整数 L 和 R. 最后一行 L=R=0,表示输入结 ...
- UVA - 1640 The Counting Problem (数位dp)
题意:统计l-r中每种数字出现的次数 很明显的数位dp问题,虽然有更简洁的做法但某人已经习惯了数位dp的风格所以还是选择扬长避短吧(说白了就是菜啊) 从高位向低位走,设状态$(u,lim,ze)$表示 ...
- hdu 5106 Bits Problem(数位dp)
题目链接:hdu 5106 Bits Problem 题目大意:给定n和r,要求算出[0,r)之间全部n-onebit数的和. 解题思路:数位dp,一个ct表示个数,dp表示和,然后就剩下普通的数位d ...
- hiho1259 A Math Problem (数位dp)
题目链接:http://hihocoder.com/problemset/problem/1259 题目大意:g(t)=(f(i)%k=t)的f(i)的个数 求所有的(0-k-1)的g(i)的异或总值 ...
- 哈尔滨工程大学ACM预热赛 G题 A hard problem(数位dp)
链接:https://ac.nowcoder.com/acm/contest/554/G Now we have a function f(x): int f ( int x ) { if ( ...
- POJ2282 The Counting Problem
题意 Language:DefaultEspañol The Counting Problem Time Limit: 3000MS Memory Limit: 65536K Total Submis ...
- POJ2282 The Counting Problem(数位DP)
用dp[pos][val][cnt]表示状态,pos是数位,val是当前统计的数字,cnt是目前统计的目标数字的出现次数 注意状态的转移过程,统计数字0时前导0的影响. 1 #include<c ...
- nowcoder A hard problem /// 数位DP
题目大意: 称一个数x的各个数位之和为f(x) 求区间L R之间 有多少个数x%f(x)==0 #include <bits/stdc++.h> using namespace std; ...
- Gym - 102040B Counting Inversion (数位dp)
题意:求[a,b]区间内的数字中正序对的个数. 具体思路参考: https://blog.csdn.net/weixin_43135318/article/details/88061396 https ...
随机推荐
- token验证-微信公众平台开发3(asp.net)
童鞋们直接看代码吧:(我这里是ashx处理程序写的类,开发过网站的一般都知道) <%@ WebHandler Language="C#" class="weixin ...
- MVC5 + EF6 入门完整教程 (1)
第0课 从0开始 ASP.NET MVC开发模式和传统的WebForm开发模式相比,增加了很多"约定". 直接讲这些 "约定" 会让人困惑,而且东西太多容易忘记 ...
- 3527: [Zjoi2014]力 - BZOJ
题目大意:给出n个数qi,定义 Fj为 令 Ei=Fi/qi,求Ei. 看了很久题解,终于有些眉目,因为知道要用FFT,所以思路就很直了 其实我们就是要±1/(j-i)^2 ( j-i大 ...
- 使用Ninject+Moq在单元测试中抽象数据访问层
一.测试方法的业务逻辑时,通常都需要从数据库读取测试数据,但是每次初始化数据库数据都很麻烦,也会影响到其它业务对数据的访问,怎样抽象数据访问层呢?就是用Moq去模拟数据访问的逻辑 二.步骤如下 ...
- 【CentOS】Eclipse插件egit使用
1.简介 2.安装 3.配置 4.使用 5.补充说明 参考资料: http://yufenfei.iteye.com/blog/1750124 1.简介 EGit就是一款Eclips ...
- A*(A星)算法python实现
在春节放假前两天我偶然看到了A\*算法(A\*算法是一个启发式的地图寻路算法),感觉挺有意思.正好放假前也没有什么事情,就花了一个下午写出算法的骨架,节后又花了半天时间完善屏幕输出的细节并且调试完成. ...
- UIResponder
原网址:http://www.cnblogs.com/kuku/archive/2011/11/12/2246389.html 在 iOS 中,一个 UIResponder 对象表示一个可以接收触摸屏 ...
- sublime 配置C++
1. 安装C语言编译器MinGW,并把MinGW安装目录下的bin目录添加到环境变量PATH里.详细方法参照MinGW安装和使用 2. 在SublimeText安装目录下的Data\Packages\ ...
- hadoop-ha QJM 架构部署
公司之前老的hadoop集群namenode有单点风险,最近学习此链接http://www.binospace.com/index.php /hdfs-ha-quorum-journal-manage ...
- 驱动笔记 - Makefile
ifneq ($(KERNELRELEASE),) obj-m := hello.ohello-objs := main.o add.o else KDIR := /lib/modules/2.6.1 ...