2018 Meituan Spring Recruitment (Second Batch) Problem Solving

  Algorithm post, a total of two programming questions, the topics are as follows:

first question:

# Give you array A, ask ΣΣA[gcd(i,j)],1<=i<=n,1<=j<=m
# Input:
# Four integers per row, N,n,m,p , where N represents the length of the A array, and n, m, and p are parameters; for the A array, the following is obtained:
#
# A[1]=p, A[x]=(A[x-1]+153)%p
#
# Data range
#
# Small data n,m<=N<=1000,p<=1000
#
# Big data n,m<=N<=100000,p<=100000

#Where gcd(i,j) represents the greatest convention of i and j.

code show as below:

#include<iostream>
#include<unordered_map>
#include<queue>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<sstream>
using namespace std;

int gcd(int  a,int b)
{
    if(a < b)
        return gcd(b,a);
    return b == 0 ? a : (b,a % b);

}
int getRs(int A[], int n, int m)
{
    int rs = 0;
    for(int i = 1; i <=n ; i++)
        for(int j = 1; j <=m; j++)
            rs += A[gcd(i,j)];
    return rs;
}
intmain()
{
    int A[100001];
    int N,n,m,p;
    cin>>N>>n>>m>>p;
    A[1] = p;
    for(int i = 2; i <= N; i++)
        A[i] = (A[i - 1] + 153) % p;
    cout<<getRs(A,n,m)<<endl;

}

In this way, the AC can only be 60%. When the rs is changed from int to long long, the AC can be 90%, and the remaining data is still timeout. Tried optimizing from the function of the greatest common divisor, but it didn't work. It is estimated that it should be optimized from the superposition of two loops in the getRs function. It can be seen that for two numbers a and b, the gcd(i,j) sum of i = a && j = b

The result of gcd(i,j) of j = a && i = b is the same, that is to say, in the two superposition loops, half of the calculation is actually repeated, but if the gcd of all combinations is stored, the memory will overflow. Anyone who has a good idea can introduce it!

Second question:

# Xiao Ming is exploring the mysteries of mathematics. Now she wants to know how many digits there are in positive integers within n. Leading zeros are not counted.

# For example: When n is 13, 12345678910111213, a total of 17 digits, the output is 17.

# Input: a number T (T<=100) in the first line, indicating the number of data groups.

# For each set of data, the first line of 1 integer n (1 <= n <= 10^9)
# For each set of data, output a line representing the number of digits and

=========================================================================================================================

For this question, I used four methods, and the first three all timed out.

Method 1: Loop directly from i to n, and determine the number of digits of each number. The way to find the number of digits is as follows:

int cnt = 0;
while(n)
{
    cnt++;
    n /= 10;
}

Method 2: Loop directly from 1 to n, and convert each number into a string, and then use the size() method to find its length, roughly as follows:

int cnt = 0;
while(n)
{
    cnt++;
    n /= 10;
}

Method 3: Inspired by method 2, directly turn each number into a substring string, and finally combine these substrings into a large string, the result is the size() of the string, which is roughly as follows:

int fun(int num)
{
string str;
for(int i = 1; i <= num; i++)
     str += to_string(i);
return str.size();
}

But if you think about it carefully, it is not much different from method 2, the essence is the same wow.

Method 4:

It seems that it is impossible to ask for it directly, so we can only find the rules. Careful analysis of the subject, you can find such a rule:

Let f(n) denote the maximum number composed of digits from 1 to n when the length of the number num is n (for example, when n=3, the maximum number is 999), and the number of digits formed is summed. So:

When n = 0, f(0) = 0

When n = 1 [1 ~ 9], f(1) = 9

When n = 2 [1 ~ 9, 10 ~ 99], f(2) = (99 - 9) * 2 + 9 = (10^2 - 1 - 9) * 2 + f(1) = 189 

When n = 3, [1 ~ 99, 100 ~ 999], f(3) = (999 -99 )* 3 + 189 = (10 ^ 3 -1 -99 )* 3 + f(2)

...

When n = 9, f(9) = [10^9 - (10^8 - 1)] * 9 + f(8)

So for a number num of length n, its digit sum = f(n- 1) + [num - 10 ^(n - 1) + 1] * n, where the second half represents num and n The number of digits by which the maximum number of -1 digits differs. For example, 456 can be divided into two parts, 1 ~ 99 and 100 ~ 456, and the sum of the digits of 100 ~ 456 is (456 - 100 +1) * 3. (plus 1 is to include 456 itself)

code show as below:

#include<iostream>
#include<unordered_map>
#include<queue>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<sstream>
using namespace std;

long long getRs(long long f[],int num)
{
    int len = to_string(num).size();
    return f [len - 1] + (num - pow (10, len - 1) + 1) * len;
}
intmain()
{
 long long f[10] = {0,9,189,2889,38889,488889,5888889,68888889,788888889,8888888889};
 int n;
 int num;
 cin>>n;
 while(n--)
 {
     cin>>num;
     cout<<getRs(f,num)<<endl;
 }
}

The time complexity of this method is O(1) and will never time out! I first realized and calculated the value corresponding to each f(n) here, or you can calculate the value of f(n) without realizing it. It is also possible to iteratively calculate according to the formula.

Note: The returned result should be long long (it is best not to use long here, because the representation range of long is the same as that of int in 32-bit machines, and long long occupies 32-bit machines or 64-bit machines. 8 bytes, so no problem!)

From these questions, a very important conclusion can be drawn: before doing the questions, it is very important to know whether the simple int type can meet the requirements of the question, and the limit value of the sampling example to see if the int will overflow, which is very important a little! ! ! In addition, the value that causes the overflow does not necessarily refer to the variable, but may also include the return value of the function. It must be carefully considered before AC! ! ! ( This is also reflected in JD's written test questions)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324694352&siteId=291194637