Good Numbers
来源:互联网 发布:bcg矩阵是什么意思 编辑:程序博客网 时间:2024/05/22 12:58
if we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number.
You are required to count the number of good numbers in the range from A to B, inclusive.
You are required to count the number of good numbers in the range from A to B, inclusive.
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 1018).
21 101 20
Case #1: 0Case #2: 1
The answer maybe very large, we recommend you to use long long instead of int. 分析:
发现有近似num / 10的规律,仔细观察后发现其实确实粗略来看是每10个出现一个和为10的倍数
因为对于一个数,例如1X,这时候个位只有10 - 1 = 9才满足,对于一个三位数例如12X,则由于1和2是确定的,所以在120 - 129这十个数的范围内(也就是个位0 - 9)只有10 - 1 - 2 = 7,即127满足,对于大一点的数,例如637859X,6 + 3 + 7 + 8 + 5 + 9 = 38,因为最后一位肯定只能为正(不可能为-8),所以只有最后一位为40 - 38 = 2,即6378592符合条件,所有的数都这么看,会发现每次高位确定了,其实在个位上只有0 - 9当中的一个数能够满足,这就印证了num / 10的规律
最后只要注意下右边界就行了,即
0 - 90 -> 90 / 10 = 9个
但是
0 - 91 ->90 / 10 + 1 = 10个(由于91这个边界多加了一个)
处理方法是从90 / 10 * 10 = 90 -->91暴力扫一下,看有没有多出来一个能够满足各位和是10的倍数的,发现91是,所以就在9的基础上加1,因为我们证明了个位0 - 9有且只有一个数满足,所以只要扫过去发现一个直接返回就行了
注意:0可以被10整除!!
import java.util.*;public class Main{ static Scanner in = new Scanner(System.in); static long get(long num) { long n = num / 10 * 10;boolean flag = false;for (; n <= num; n++) {long tn = n, sum = 0;while (tn!=0) {sum += tn % 10;tn /= 10;}if (sum % 10 == 0) {flag = true;break;}}if (flag)return 1;elsereturn 0;}static long solve(long num) {if (num < 0) return 0;if (num < 10) return 1;return num / 10 + get(num);} public static void main(String args[]){ int k = in.nextInt(); int cas = 0; while(k-->0) { cas++; long a = in.nextInt(); long b = in.nextInt(); long cnt = solve(b) - solve(a - 1);//-1就是为了把0算进去 System.out.println("Case #"+cas+": "+cnt); } } }
阅读全文
0 0
- good numbers
- Good Numbers
- Good Numbers
- Good Numbers
- HDU 4722 Good Numbers
- hdu4722 Good Numbers
- HDU 4722 Good Numbers
- Hdu 4722 Good Numbers
- hdu 4722 Good Numbers
- hduoj 4722 Good numbers
- HDU 4722 Good Numbers
- HDU 4722 Good Numbers
- hdu 4722 Good Numbers
- HDU:Good Numbers
- HDU 4722 Good Numbers
- HDU 4722 Good Numbers
- HDU 4722-Good Numbers
- HDU 4722 Good Numbers
- 堆内存分配
- iOS二维码扫描
- 《Python学习手册》学习笔记(11)之第11章赋值、表达式和打印(关键词:编程语言/Python/赋值/表达式/打印)
- linux-image-3.19.0-66-generic造成dkpg无法正常工作的问题
- Java学习之基础训练三-----猜字符小游戏(自定义函数,在main函数中调用)
- Good Numbers
- ArrayList JDK9源码解读
- poj1236 Network of Schools (Targan强连通图缩点)
- 1.前端之路-一个能进行2d缩放旋转的小东西
- opencv是什么
- 排序算法Day2
- [2017.10.6]作业01
- <C语言>关于函数返回值是指针,取不到值的问题研究
- 递归+Dom4j实现数据库中树(Tree)XML的生成