BZOJ 1833: [ZJOI2010]count 数字计数
Description
问 \([L,R]\) 中0-9的个数.
Sol
数位DP.
预处理好长度为 \(i\), 最高位为 \(j\) 的数位之和.
然后从上往下计算,不要忘记往下走的同时要把高位的贡献加上去..
Code
/**************************************************************
Problem: 1833
User: BeiYu
Language: C++
Result: Accepted
Time:40 ms
Memory:1396 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; typedef long long LL;
const int N = 65; struct S {
LL a[10];
S(LL v=0) { for(int i=0;i<10;i++) a[i]=v; }
}f[N][10][2]; S operator + (const S &a,const S &b) {
S c;for(int i=0;i<10;i++) c.a[i]=a.a[i]+b.a[i];
return c;
}
S operator - (const S &a,const S &b) {
S c;for(int i=0;i<10;i++) c.a[i]=a.a[i]-b.a[i];
return c;
}
S operator * (const S &a,const LL &b) {
S c;for(int i=0;i<10;i++) c.a[i]=a.a[i]*b;
return c;
}
void Print(const S &a) { for(int i=0;i<9;i++) cout<<a.a[i]<<" ";cout<<a.a[9]; } LL l,r;
LL pow10[N]; inline LL Pow(LL a,LL b,LL r=1) { return pow10[b]; }
void init() {
pow10[0]=1;
for(int i=1;i<=15;i++) pow10[i]=pow10[i-1]*10LL; for(int i=0;i<10;i++) {
f[1][i][0]=f[1][i][1]=f[1][i-1][0];
f[1][i][0].a[i]+=1,f[1][i][1].a[i]+=1;
}
for(int l=2;l<=14;l++) {
f[l][0][0]=f[l-1][9][0];
f[l][0][1]=f[l-1][9][1];
f[l][0][1].a[0]+=Pow(10,l-1);
for(int i=1;i<10;i++) {
f[l][i][0]=f[l][i-1][0]+f[l-1][9][1];
f[l][i][0].a[i]+=Pow(10,l-1);
f[l][i][1]=f[l][i-1][1]+f[l-1][9][1];
f[l][i][1].a[i]+=Pow(10,l-1);
}
} // for(int l=1;l<=5;l++) for(int i=0;i<10;i++) {
// cout<<l<<":"<<i<<endl;
// Print(f[l][i][0]),Print(f[l][i][1]);
// cout<<"-------------------------------"<<endl;
// }
}
S calc(LL x) {
LL g=0,nn;S r,t;
for(int i=13;~i;i--) {
if(x/pow10[i]) {
nn=(LL)(x/pow10[i])*pow10[i];
r=r+t*nn; t.a[x/pow10[i]]++; r=r+f[i+1][x/pow10[i]-1][g];
g=1;
// cout<<i+1<<" "<<x/pow10[i]-1<<" "<<g<<endl;
}else {
if(g) t.a[0]++;
}x%=pow10[i];
// cout<<i<<" ";Print(r);
}return r;
}
int main() {
init(); cin>>l>>r; S ans1=calc(r+1);
S ans2=calc(l); // Print(ans1),Print(ans2);
Print(ans1-ans2);
return 0;
}
BZOJ 1833: [ZJOI2010]count 数字计数的更多相关文章
- BZOJ 1833: [ZJOI2010]count 数字计数( dp )
dp(i, j, k)表示共i位, 最高位是j, 数字k出现次数. 预处理出来. 差分答案, 对于0~x的答案, 从低位到高位进行讨论 -------------------------------- ...
- [BZOJ 1833] [ZJOI2010] count 数字计数 【数位DP】
题目链接:BZOJ - 1833 题目分析 数位DP .. 用 f[i][j][k] 表示第 i 位是 j 的 i 位数共有多少个数码 k . 然后差分询问...Get()中注意一下,如果固定了第 i ...
- bzoj 1833 [ZJOI2010]count 数字计数(数位DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1833 [题意] 统计[a,b]区间内各数位出现的次数. [思路] 设f[i][j][k ...
- BZOJ 1833 ZJOI2010 count 数字计数 数位DP
题目大意:求[a,b]间全部的整数中0~9每一个数字出现了几次 令f[i]为i位数(算前导零)中每一个数出现的次数(一定是同样的,所以仅仅记录一个即可了) 有f[i]=f[i-1]*10+10^(i- ...
- bzoj 1833: [ZJOI2010]count 数字计数【数位dp】
非典型数位dp 先预处理出f[i][j][k]表示从后往前第i位为j时k的个数,然后把答案转换为ans(r)-ans(l-1),用预处理出的f数组dp出f即可(可能也不是dp吧--) #include ...
- 1833: [ZJOI2010]count 数字计数
1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 2951 Solved: 1307[Submit][ ...
- 【BZOJ】1833 [ZJOI2010]count 数字计数
[算法]数位DP [题解] 记忆化搜索 #include<cstdio> #include<algorithm> #include<cstring> #define ...
- 1833: [ZJOI2010]count 数字计数 - BZOJ
Description给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次.Input输入文件中仅包含一行两个整数a.b,含义如上所述.Output输出文件中包含一 ...
- 【BZOJ】1833: [ZJOI2010] count 数字计数(数位dp)
题目 传送门:QWQ 分析 蒟蒻不会数位dp,又是现学的 用$ dp[i][j][k] $ 表示表示长度为i开头j的所有数字中k的个数 然后预处理出这个数组,再计算答案 代码 #include < ...
随机推荐
- iOS Webview 实现修改javascript confirm 和 alert
贴代码: @interface UIWebView (JavaScriptAlert) -(void) webView:(UIWebView *)sender runJavaScriptAlertPa ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- 【jQuery api】isFunction()
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Prince2七大原则(4)
我们先来回顾一下,PRINCE2七大原则分别是持续的业务验证,经验学习,角色与责任,按阶段管理,例外管理,关注产品,剪裁. 第四个原则:按阶段管理. 阶段管理其实是给高层提供了项目生命周期中相对应的控 ...
- Dedecms去掉URL中a目录的方法
本文实例讲述了Dedecms去掉URL中a目录的方法.分享给大家,供大家参考.具体分析如下: 使用dedecms的朋友可能会发现自己的URL目录生成是会自动带有一个/A/目录了,那么要如何去掉URL中 ...
- C#汉字转拼音帮助类
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...
- python word
代码: #coding=utf-8 __author__ = 'zhm' from win32com import client as wc import os import time import ...
- Fxx and game
可提交的传送门http://acm.hdu.edu.cn/showproblem.php?pid=5945 分析:这道题目可以采用动态规划来解决 设f[i]表示把i变成1的最小代价. 所以有:f[i] ...
- 正则表达式解析url参数
解析url参数正则:(?<=\?|&)[\w\={}\\\\,-:'\s'""]*(?=[^#\s]|) 意思是(?<=\?|&) 从?或&符号 ...
- 单元测试与Moq
这个篇幅里面,记录单元测试与Moq模拟包的知识点. 单元测试 每一个模块,都应该有对应的单元测试.单元测试可以保证你的代码准确性,大大减少出现BUG的几率.一个好的单元测试,也是重构代码必不可少的部分 ...