Answer the question
In order to leave comments, you need to log in
Is it possible to count the number of digits in a document that contains the results of the count?
I recently came up with a problem that I can't solve. I'm wondering if she has a solution in the form of a universal algorithm.
There is a document that contains random data in the form of letters and numbers. It is necessary to add a table or a histogram of the use of numbers to the end of this document in such a way that it takes into account itself.
For example, source document: Варгхарбл 012
Document with results:
Варгхарбл 012
+-----+------+
|Цифра|Кол-во|
+-----+------+
| 0 | 2 |
+-----+------+
| 1 | 7 |
+-----+------+
| 2 | 5 |
+-----+------+
| 3 | 1 |
+-----+------+
| 4 | 1 |
+-----+------+
| 5 | 2 |
+-----+------+
| 6 | 1 |
+-----+------+
| 7 | 2 |
+-----+------+
| 8 | 1 |
+-----+------+
| 9 | 1 |
+-----+------+
Answer the question
In order to leave comments, you need to log in
I'm not sure if this problem can be solved if the document consists of 97 nines.
Another nine will be in the left column of the table. Any of the numbers from 0 to 8 is unlikely to occur 9 times. So nines will be 98+ the number of nines in the last cell of the table. So it turns out:
If there are 0 nines in the last cell, then it contains the number 98+0=98
If there is one nine in it, then it has 98+1=99
If
it has two nines, then it has 98+2=100
cases, we get a contradiction.
UPD. The task is solved quite simply.
Suppose we have already collected statistics on the number of different digits in the body of the document (B[0] zeros, B[1] ones ...)
Let's immediately add one to these numbers to take into account the left column.
Let us estimate from above the number of digits in the right part of the table. For example, like S=10*(log_10(B[0]+..+B[9])+2).
Each digit in the complete document will occur no more than B[n]+S times, which means that on the right side of the table for it there will be a number lying from D0[n]=B[n] to D1[n]=B[n]+S .
Let's optimize the ranges D0..D1. To do this, for each digit, let's go through all the numbers from D0[n] to D1[n], look at the number of different digits in each of these numbers, take the minimum and maximum values of occurrences. For example, if D0[n]=997, D1[n]=1013, then in the cell corresponding to n, 0,1 and 9 can occur from 0 to 3 times, and the remaining numbers - 0 or 1 times.
Let's sum up the obtained ranges for all n - we will get a new table of ranges D0n, D1n. Let's intersect them with D0,D1.
There are 3 options.
All received ranges coincided with D0..D1. We exit the cycle.
One of the new ranges is empty. There are no solutions in this branch, we return from the function.
Some range has changed. We continue to optimize.
After the ranges have stabilized, we look at their lengths. If they are all of length 1 (ie D0=D1), then we have found a solution. If not, then we take the shortest range of length greater than 1, iterate over all its values D0[n]<=u<=D1[n], substitute each of them into a copy of the range table (D0n[n]=D1[n]=u ) and recursively call the optimization function.
The search turns out to be small, here are examples (ncall is the number of recursive calls):
For 1 2 3 4 5 6 7 8 9 10
0->5 1->7 2->5 3->5 4->6 5->10 6 ->9 7->10 8->10 9->
0->2 1->7 2->5 3->1 4->1 5->2 6->1 7->2 8->1 9->1
ncall=486
For 0 0 0 0 0 0 1 0 0 0
ncall=464
For 0 0 0 0 0 0 0 1 0 0
0->1 1->7 2->2 3->3 4->1 5->1 6->1 7->3 8->1 9->1
0->1 1->6 2->3 3->3 4->1 5->1 6->2 7->2 8->1 9->1
0- >1 1->6 2->4 3->1 4->2 5->1 6->2 7->2 8->1 9->1
ncall=482
For 3997 19995 5677 998 799996 392 7 94 8998 99985
0->4014 1->20000 2->5679 3->1000 4->800001 5->394 6->10 7->96 8->9000 9->99994
0->4013 1->20001 2->5679 3->1001 4->800000 5->394 6->10 7->96 8->9000 9->99994
ncall=47356
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question