POJ 2282 && HDU 1663 The Counting Problem(数论)
来源:互联网 发布:js new一个对象 编辑:程序博客网 时间:2024/06/05 11:33
Description
统计区间[a,b]中数字0~9出现的次数
Input
多组输入,每组输入两个整数a和b,以0 0结束输入
Output
对于每组输入,输出区间[a,b]中0~9出现的次数
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
Solution
首先的思路是求出[1,n]中0~9出现的次数,这样用solve[1,b]-solve[1,a-1]就是solve[a,b],下面先以4321为例计算[1,1234]中2出现的次数
个位为2 左边的可能值为0~431,右边没有数字,所以是432*1
十位为2 左边的可能值为0~42时,右边可能值为0~9,左边为43时,右边可能值为0~1,所以是43*10+1*2
百位为2 左边可能值为0~4,右边可能值为0~99,所以是5*100
千位为2 左边没有数字,右边可能值为0~999,所以是1*1000
以此类推,1~9的计算情况与2相同,而0的数量因为存在前置0的情况,所以每次把当前位左边的可能值减一即可
Code
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;ll ans[10];void solve(int s){ if(s<0)return ; int cur=1,rest=0;//rest表示当前位右边的数值,cur表示当当前位右边可以任意取时的情况数 while(s>0) { int now=s%10;//从最低位往最高位累计 s/=10; for(int i=0;i<10;i++) { if(now>i)//如果当前位的值大于要计算的数字 { ans[i]+=s*cur; if(i)ans[i]+=cur; } else if(now==i)//如果当前位的值等于要计算的数字 { if(s&&i==0) ans[i]+=(s-1)*cur+rest+1; else if(i>0) ans[i]+=s*cur+rest+1; } else if(now<i)//如果当前位的值小于要计算的数字 ans[i]+=s*cur; } rest+=now*cur;//更新值 cur*=10;//更新值 } }int main(){ ll a,b; while(cin>>a>>b) { if(a==0&&b==0)break; if(a>b)swap(a,b);//a,b大小不知道 memset(ans,0,sizeof(ans));//初始化 solve(a-1); for(int i=0;i<10;i++) ans[i]=-ans[i]; solve(b); for(int i=0;i<10;i++)//按格式输出 printf("%lld%c",ans[i],i==9?'\n':' '); } return 0;}
- POJ 2282 && HDU 1663 The Counting Problem(数论)
- poj 2282 The Counting Problem
- POJ-2282-The Counting Problem
- [POJ] 2282 -> The Counting Problem
- POJ 2282 The Counting Problem
- POJ 2282 The Counting Problem
- POJ题目2282 The Counting Problem(数学)
- POJ 2282 The Counting Problem (数位dp)
- poj 2282 The Counting Problem (数位DP)
- POJ 2282 The Counting Problem,组合数学
- The Counting Problem - POJ 2282 数位dp
- 小白算法练习 hdu 不要62 POJ 2282 the Counting problem 数位dp
- poj 2282 The Counting Problem && poj 3286 How many 0's? (数位dp)
- poj 2282 The Counting Problem 按位统计
- POJ 2282-The Counting Problem(组合数学_区间计数)
- BZOJ 1833 & POJ 2282 The Counting Problem & huangyueying 贪心详解
- (Realx 数论1.28)POJ 2282 The Counting Problem(区间统计数字:统计a、b之间各个数字(0~9)出现的次数)
- POJ 2545 && HDU 3199 Hamming Problem(数论)
- 一些经典的计算机书籍
- [CodeVS1080] 线段树练习(单点修改+求和)
- 颁奖晚会(HDU 2048)---15.7.25
- 背单词App开发日记4
- uva10285 Longest Run on a Snowboard(dp之记忆化搜索 )
- POJ 2282 && HDU 1663 The Counting Problem(数论)
- FSMO角色的转移/夺取的过程(用于如硬件更新,DC损坏,让BDC工作)
- 用ntdsutil来清除无效的DC信息
- learn how to learn
- 自定义struts(三)--ImitateStruts实现可插拔的拦截器
- win32编成入门-位图
- DNS主从复制及区域传送
- 会计转行SAP FICO实施顾问,百万年薪等你来挑战!
- 管理 Outlook 2003 中的脱机通讯簿