[sitch cup · blue bridge on cloud - algorithm training camp] week 1

Posted by Amman-DJ on Fri, 07 Jan 2022 05:02:48 +0100

First week of Blue Bridge Cup training camp

Question 1 running training

Problem description
Xiao Ming wants to do a running training. At the beginning, Xiao Ming is full of physical strength, and the physical strength value is 10000.

If Xiao Ming runs, he will lose 600 physical strength per minute.
If Xiao Ming has a rest, he will increase his physical strength by 300 per minute.
The loss and increase of physical strength vary evenly.

Xiao Ming plans to run for one minute, rest for one minute, run for another minute, rest for another minute... This cycle.
If Xiaoming's physical strength reaches 0 at a certain time, he will stop exercising. How long will Xiaoming stop exercising.
In order to make the answer an integer, please output the answer in seconds. Only the number, not the unit, is filled in the answer.

Answer submission
This is a result filling question. You just need to calculate the result and submit it.
The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.

Answer: 280

Factorial divisor of question 2

Problem description
Define factorial n= one × two × three × ··· × n.
Excuse me 100! (factorial of 100) how many divisors are there.
Answer submission
This is a question to fill in the blanks. You just need to calculate the results and submit them.
The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.

# Principle 1: any positive integer can be expressed as the product of several prime numbers (except 1)
# Principle 2: for a positive integer n, the number of divisors of N can be obtained by multiplying the number of different prime numbers of N by one
# 100! =  1*2*3*4*...* 100 is the number of different prime numbers required to decompose each factor of the factorial. The accumulation is equal to the prime number composition of the factorial of 100,
n = 100
# Put prime numbers within 100 into p
p = [2]
for i in range(3, n+1):
    j = 2
    while j<i:
        if i % j == 0:
            break
        j += 1
    else:
        p.append(i)      
# Use a dictionary to record different prime numbers plus 1, eliminating the problem of adding 1 later
m = {}
for i in p:
    m[i] = 1
# The factorial factor is traversed and decomposed to obtain the number corresponding to each prime number
for i in range(2, n+1):
    x = i
    for j in p:
        while x % j == 0:
            x //= j
            m[j] += 1            
# Obtain results
f = 1
for i in m.values():
    f *= i
print(f)  # 39001250856960000

3. Find out the stack order

Problem description
Planet X pays special attention to order. All roads are one-way streets.
A fleet of 16 beetles started successively according to the number, caught in other traffic flows and moved forward slowly.
There is a dead end on the roadside, which can only allow one car to pass. It is a temporary checkpoint, as shown in the figure.
Planet X is so rigid that every passing car is required to enter the checkpoint, or it may be released without inspection, or it may be carefully inspected.
If the order of vehicles entering and leaving the checkpoint can be staggered arbitrarily.
So, how many possible orders will the team take when it gets on the road again?
For convenience, it is assumed that the checkpoint can accommodate any number of cars.
Obviously, if the fleet has only one vehicle, there may be one order; 2 possible sequences of 2 vehicles; There are 5 possible sequences for 3 vehicles.
There are 16 cars now, pro! You need to calculate the number of possible orders.

Answer submission
This is an integer. Please submit the answer through the browser and do not fill in any redundant content (such as explanatory text).

# Similar to the Hanoi Tower problem
def f(n):
    temp = 0
    if n == 0:
        temp += 1
    elif n == 1:
        temp += 1
    elif n == 2:
        temp += 2
    elif n == 3:
        temp += 5
    else:
        for i in range(0, n):
            temp += f(i)*f(n-1-i)
    return temp


print(f(16))  # 35357670

Problem 4 Goldbach decomposition

def is_prime(n):
    if n < 2:
        return False
    if n == 2:
         return True
    else:
        for i in range(2, n):
            if n % i == 0:
                return False
        return True


def finding(m):
    for i in range(2, m//2 + 1):
        if is_prime(i) and is_prime(m-i):
            return min(i, m - i)


p = []
for i in range(4, 10001, 2):
    p.append(finding(i))
print(max(p))  # 173

5 book arrangement

Title Description
10 books numbered 1 ~ 10 shall be placed on the bookshelf, and the books with adjacent numbers shall not be placed in adjacent positions.
Please calculate how many different permutations there are.

Note that what needs to be submitted is an integer. Do not fill in any redundant content.

import itertools
nums = [i for i in range(10)]
n = 0


def check(ls):
    for i in range(len(ls)-1):
        if abs(ls[i] - ls[i-1]) == 1:
            return False
    return True


for num in itertools.permutations(nums, 10):
    if check(num):
        n += 1


print(n)  # 479306

Question 6 monkeys are divided into bananas

for i in range(6, 10000):
   a = i
   if a%5 == 1:
       a = (a-1)/5*4
       if a%5 == 2:
           a = (a-2)/5*4
           if a%5 == 3:
               a = (a-3)/5*4
               if a%5 == 4:
                   a = (a-4)/5*4
                   if a%5 == 0 and a>0:
                       print(i)  # 3141
                       break

7 slightly smaller scores

Back to primary school----
True fraction: fraction whose numerator is less than denominator
Reduced fraction: the coprime of numerator and denominator, that is, the maximum common divisor is 1
The entrance verification method of Planet x math city is:
A true score is displayed on the screen. You need to quickly find a reduced score smaller than it. The larger the score, the better.
At the same time, limit the denominator of your score to no more than 100.

def fun(a, b):
    if b == 0:
        return a
    return fun(b, a % b)


N = int(input("Please enter the numerator:"))
D = int(input("Please enter denominator:"))

dp = []
dq = []
for d in range(1, 101):
    for n in range(1, d):
        if (n/d) < (N/D) and fun(d, n) == 1:
            dp.append([n, d, n/d])
            dq.append(n/d)
for i in range(len(dp)):
    if dp[i][2] == max(dq):
        print(dp[i])


Their values in turn represent the numerator, denominator and overall size

8 questions excel address

Time limit: 1.0s memory limit: 256.0MB

Problem description

The address representation of Excel cells is very interesting. It uses letters to represent column numbers.
For example,
A stands for column 1,
B represents column 2,
Z represents column 26,
AA means column 27,
AB stands for column 28,
BA stands for column 53,
  ...
Of course, the maximum column number of Excel is limited, so it is not difficult to convert.
If we want to generalize this representation, can we convert large numbers into long letter sequences?
This topic is to output the corresponding Excel address representation of the input number.

sample input
26
sample output
Z

sample input
2054
sample output
BZZ

Data scale and agreement
We agree that the input integer range [12147483647]
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms
Please output in strict accordance with the requirements, and do not print superfluous contents such as "please input...".
be careful:
The main function needs to return 0;
Only ANSI C/ANSI C + + standards are used;
Do not call special functions that depend on the compilation environment or operating system.
All dependent functions must be explicitly #include in the source file
Common header files cannot be omitted through project settings.
When submitting programs, pay attention to selecting the desired language type and compiler type.

def ascii(n):
    dp = []
    while n > 0:
        if n%26 == 0:
            dp.append(chr(26+64))
            n //= 26
            n -= 1
        else:
            dp.append(chr(n%26 + 64))
            n //= 26
    dp.reverse()
    for i in dp:
        print(i, end="")


def main():
	while True:
	    num = int(input())
	    ascii(num)
	    print()


main()

Question 9 date

Xiao Ming is sorting out a batch of historical documents. Many dates appear in these historical documents. Xiao Ming knows that these dates are from January 1, 1960 to December 31, 2059. What bothers Xiao Ming is that the formats of these dates are very different, including year / month / day, month / day / year and day / month / year. What is more troublesome is that the first two digits of the year are omitted, so that there are many possible dates corresponding to a date in the literature.

For example, 02 / 03 / 04 may be March 4, 2002, February 3, 2004 or March 2, 2004.

Given a date in the literature, can you help Xiao Ming judge which possible dates correspond to it?

input
A date in the format "AA/BB/CC". (0 <= A, B, C <= 9)

output
Output several different dates, one line for each date, in the format of "yyyy MM DD". Multiple dates are arranged from morning to night.

sample input
02/03/04

sample output
2002-03-04
2004-02-03
2004-03-02

Resource agreement:
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms

# There are many conditions for judgment, Zhuo!

i_time = input()
# i_time = "02/03/04"
i_ls = i_time.split("/", -1)  # i_ls sort
time = [[] for i in range(3)]
time[0] = [int(i_ls[0]), i_ls[1], i_ls[2]]  # specific date
time[1] = [int(i_ls[2]), i_ls[0], i_ls[1]]  # Month day year
time[2] = [int(i_ls[2]), i_ls[1], i_ls[0]]  # Sun Moon year

# Judge whether leap year
def is_leap(num):
    if (num % 4 == 0 and num % 100 != 0) or num % 400 == 0:
        return True
    else:
        return False
# Processing year
for i in range(3):
    if 0 < time[i][0] <= 59:
        time[i][0] += 2000
    elif 60 <= time[i][0] <= 99:
        time[i][0] += 1900

# Processing month: 0 < month < 13
for i in range(3):
    if int(time[i][1]) > 12:
        time[i] = []


def month(mon):
    if mon in [2, 4, 6, 9, 11]:
        return True
    else:
        return False


# Consider the impact of leap years and months on days
for i in range(3):
    if len(time[i]) == 0:
        break
    if int(time[i][2]) > 31:
        time[i] = []
        break
    if time[i][2] == "31" and month(int(time[i][1])):
        time[i] = []
        break
    if time[i][1] == "02":
        if is_leap(time[i][0]):
            temp = 28
        else:
            temp = 29
        if int(time[i][2]) > temp:
            time[i] = []
            break


res = ""
for i in range(3):
    if len(time[i]) == 0:
        break
    res = ""
    time[i][1] = "-"+time[i][1]
    time[i][2] = "-"+time[i][2]
    for m in range(3):
        res += str(time[i][m])
    print(res)
if len(res) == 0:
    print("No compliance date")

10 integer division

The division of a positive integer n is the expression that turns n into the sum of a series of positive integers. Note that the partition has nothing to do with the order, for example, 6 = 5 + 1 It is the same partition as 6 = 1 + 5. In addition, the integer itself is also a partition.
For example, for a positive integer n=5, it can be divided into:
1+1+1+1+1
1+1+1+2
1+1+3
1+2+2
2+3
1+4
5
Enter description
Enter a positive integer n

Output description
Output the total number of n integer partitions k

sample input
5

sample output
7

Question 11 one step away

Waking up from a coma, Xiao Ming finds himself locked up in a waste mine truck on Planet X.
The mine car stopped on a flat abandoned track.
In front of him were two buttons, which read "F" and "B".

Xiao Ming suddenly remembered that these two buttons can control the mine car to move forward and backward on the track.
Press F to advance 97 meters. Pressing B will move back 127 meters.
Through the dim light, Xiao Ming sees a monitoring probe 1 meter in front of him.
He had to try to stop the harvester right under the camera before he had a chance to get help from his companions.
Perhaps it can be done by multiple operations F and B.

The power on the mine car is not enough, and the yellow warning light is flashing silently
Each F or B operation will consume a certain amount of energy.
Xiao Ming quickly calculated how many operations it would take to park the mine car exactly one meter ahead.

Please fill in the minimum number of operations required to achieve the goal.

Note that what needs to be submitted is an integer. Do not fill in any irrelevant content (such as explanation, etc.)

Question 12 cheerleading questions

The robot show cheerleaders on Planet X have two kinds of clothes, A and B.
Their performance this time is to build a robot tower.

similar:

     A
    B B
   A B A
  A A B B
 B B B A B
A B A B B A

The tower rules in the team are:

A can only stand on the shoulders of AA or BB.
B can only stand on the shoulders of AB or BA.

Your task is to help cheerleaders calculate how many kinds of towers can be formed when given the number of A and B.

Enter two integers M and N in one line, with spaces separated (0 < M, n < 500), representing the number of people in A and B respectively, so as to ensure the rationality of the number of people.

It is required to output an integer indicating the number of patterns that can be generated.

For example:
User input:
1 2

The program should output:
3

Another example:
User input:
3 3

The program should output:
4

Resource agreement:
Peak memory consumption < 256M
CPU consumption < 1000ms

Fill in the blanks with seven stars in question 13

As shown in the figure below. Fill in the number 1 ~ 14 on the 14 nodes of the seven pointed star without repetition or omission. The sum of the four numbers on each line must be equal.
Picture description

Three numbers have been given in the figure. Please calculate the number to be filled in other places. The answer is unique.
After filling in, please output 4 numbers of the green node (from left to right, separated by spaces).

Topics: Python Algorithm