[英]An algorithm to find number of numbers in a given limit 'l' (0 <= l <= 10^16) with atleast one occurence of a single digit number 'n'?
First of all, this is not a homework question. 首先,这不是一个作业问题。 I was going through some combinatorial problems to solve algorithmically and I really just need to get a start towards solving this problem.
我正在研究一些组合问题,以便通过算法解决问题,实际上,我只需要开始解决该问题。
My initial thought was to declare an array of size 17 initialize it and run a loop to find the occurrence of a number, say '5' using a simple search. 我最初的想法是声明一个大小为17的数组以对其进行初始化,然后运行循环以查找数字的出现,并使用简单的搜索将其表示为“ 5”。 But the solution looks tedious and ugly.
但是解决方案看起来很乏味且丑陋。
Thanks. 谢谢。
Assuming you mean 0 <= l < 10^16 (not <= 10^16), all integers in this range have 10 digits (allowing for leading 0s). 假设您的意思是0 <= l <10 ^ 16(不是<= 10 ^ 16),则此范围内的所有整数都有10位数字(允许前导0)。 There are 10^16 total values in this range.
此范围内有10 ^ 16个总计值。 I'd write the problem as:
我将问题写为:
Number with digit n = 10^16 - number WITHOUT digit n . 具有数字n的数字= 10 ^ 16-没有数字n的数字。
So how many ways can we not choose n in the 1's place? 那么,有多少种方法我们不能在1的位置选择n ? 9 ways.
9种方式。 How many ways can we not put an 'n' in the 10s OR 1's place?
我们有几种方法不能将“ n”放在10或1的位置? 9*9.
9 * 9。 Following this logic, there are 9^16 ways of not putting an n in any of the 16 possible slots.
按照这种逻辑,有9 ^ 16种方式将n不放入16个可能的时隙中的任何一个。
So your answer is 10^16 - 9^16. 因此,您的答案是10 ^ 16-9 ^ 16。
If you actually meant 0 <= l <= 16, that range has only one more number, namely 10^16. 如果您实际上是说0 <= l <= 16,则该范围只有一个数字,即10 ^ 16。 The leading digit of this number is 1, so if n = 1 you have exactly 10^16 - 9^16 + 1 values with a 1 in it.
该数字的前导数字为1,因此,如果n = 1,则您正好有10个16-9 ^ 16 + 1个值,其中有1个。 If n != 1 then the previous answer holds.
如果n!= 1,则前一个答案成立。
Let: 让:
G(m)
Be the number of valuesi
, where0<=i<=10^m
that contain the digitn!=0
G(m)
是值i
的数量,其中0<=i<=10^m
包含数字n!=0
As the previous answer says, when n!=0
, the answer is: 如前一个答案所述,当
n!=0
,答案是:
G(m) = 10^m - 9^m
So, G(16) = 10,000,000,000,000,000 - 1,853,020,188,851,841 = 8,126,979,811,148,159
因此,
G(16) = 10,000,000,000,000,000 - 1,853,020,188,851,841 = 8,126,979,811,148,159
When n=0
, there are two possible ways to look at it. 当
n=0
,有两种方法可以查看它。
1) Only digits after the first non-zero digit are counted 2) All m
digits are counted, even if the number starts with many 0
digits 1)仅对第一个非零数字之后的数字进行计数2)即使所有数字以
0
数字开头,也将对所有m
数字进行计数
For the second case, then the answer when n=0
is the same as when n!=0
. 对于第二种情况,则
n=0
时的答案与n!=0
时的答案相同。
F(m)
Be the number of valuesi
, where0<=i<=10^m
that contain the digitn=0
F(m)
是值i
的数量,其中0<=i<=10^m
包含数字n=0
For the first case, then the answer is a little more complicated. 对于第一种情况,答案要复杂一些。 In an
m
digit number, the first digit is not 0, but the remaining m-1
digits can be anything. 在
m
位数字中,第一位不是0,但其余的m-1
位可以是任何数字。 So, G(m-1)
of the numbers defined by those m-1' digits can contain a zero. Since there are 9 (non zero) possibilities for the leading digit,
因此,由这些
m-1' digits can contain a zero. Since there are 9 (non zero) possibilities for the leading digit,
定义的数字G(m-1)
m-1' digits can contain a zero. Since there are 9 (non zero) possibilities for the leading digit,
m-1' digits can contain a zero. Since there are 9 (non zero) possibilities for the leading digit,
F(m) = 9*G(m-1)`. m-1' digits can contain a zero. Since there are 9 (non zero) possibilities for the leading digit,
F(m)= 9 * G(m-1)`。
Let
H(m,n)
Be the number of valuesi
, where0<=i<=10^m
that contain the digitn
令
H(m,n)
为值i
的数量,其中0<=i<=10^m
包含数字n
Then: 然后:
H(m,n) = G(m)
ifn!=0
, and9*G(m-1)
ifn=0
H(m,n) = G(m)
如果n!=0
,和9*G(m-1)
如果n=0
where 哪里
G(m) = 10^m - 9^m
Thus, H(16,n) = 8,126,979,811,148,159
if n!=0
and H(16,0) = 7,146,979,811,148,159
因此,如果
n!=0
且H(16,0) = 7,146,979,811,148,159
,则H(16,n) = 8,126,979,811,148,159
H(16,0) = 7,146,979,811,148,159
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.