Connection Status:
Competition Arena > DistancesBetweenNumbers
SRM 807 · 2021-06-07 · by misof · Graph Theory, Math
Class Name: DistancesBetweenNumbers
Return Type: long
Method Name: count
Arg Types: (int, vector<int>)
Problem Statement

Problem Statement

Given two non-negative integers X and Y, their distance is defined as the number of base-10 digits in which they differ.

For example:

  • distance(47, 47) = 0
  • distance(42, 47) = distance(47, 42) = 1: these numbers have the same tens digit and different ones digits
  • distance(24, 42) = 2: we are comparing each position separately, not just the sets of digits
  • distance(1234, 37) = 3: if one number is shorter, the missing digits are zero, so distance(1234, 37) = distance(1234, 0037)

You are given a long sequence A that consists of N non-negative integers.

For each pair of indices i and j (0 <= i < j < N), Lucy calculated the distance between A[i] and A[j]. Then, Lucy computed the sum of all those distances.

Return the final value obtained by Lucy.


In order to keep the input size small, you are only given a prefix of A, the rest is generated pseudorandomly. Please use the pseudocode or code given below to generate A.


Pseudocode:

A = an empty array of length N
L = length(Aprefix)

for i = 0 to L-1:
    A[i] = Aprefix[i]

for i = L to N-1:
    A[i] = (A[i-1] * 1103515245 + 12345) modulo 2^31

-------------------------------------------------------------------------------------

Java:

int[] A = new int[N];
int L = Aprefix.length;
for (int i=0; i<L; ++i) A[i] = Aprefix[i];
for (int i=L; i<N; ++i) A[i] = (int)((A[i-1] * 1103515245L + 12345L) % (1L << 31));

-------------------------------------------------------------------------------------

C++:

vector<int> A(N);
int L = Aprefix.size();
for (int i=0; i<L; ++i) A[i] = Aprefix[i];
for (int i=L; i<N; ++i) A[i] = (A[i-1] * 1103515245LL + 12345LL) % (1LL << 31);

Notes

  • The reference solution does not depend on A being pseudorandom. It would correctly solve any input of the given size.

Constraints

  • N will be between 2 and 100,000, inclusive.
  • Aprefix will contain between 1 and min(100, N) elements, inclusive.
  • Each element of Aprefix will be between 0 and 2^31 - 1, inclusive.
Examples
0)
4
{47, 47, 47, 47}
Returns: 0

All numbers are the same, all pairwise distances are zero.

1)
4
{47, 47, 42, 47}
Returns: 3

Three pairs have distance 0, three pairs have distance 1.

2)
4
{1, 10, 100, 1000}
Returns: 12

Each pair of elements of this sequence has distance 2.

3)
10
{1234567, 1234890}
Returns: 389

The full array A looks as follows: A = { 1234567, 1234890, 1979817275, 1396317272, 1752240561, 191800982, 48419351, 727754244, 1439963117, 1491506722 }. If you are writing your own code to generate A, please watch out for integer overflows.

4)
100000
{1234567, 1234890}
Returns: 43301479228

Submissions are judged against all 49 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class DistancesBetweenNumbers with a public method long long count(int N, vector<int> Aprefix) · 49 test cases · 2 s / 256 MB per case

Submitting as anonymous