The first real question of the 11th Blue Bridge Cup Provincial Competition [Python]

The first real question of the 11th Blue Bridge Cup Provincial Competition [Python]

1. Divide the sequence

insert image description here

n = int(input())

while n > 1:
    print(n,end=' ')
    n //= 2

print(1)

2. Decoding

insert image description here

s = input()
l = len(s)
ss = []
for i in range(0,l):
    if s[i].isdigit():
        for j in range(1,int(s[i])):
            ss.append(s[i - 1])
    else:
        ss.append(s[i])
for i in ss:
    print(i,end='')

3. Walk the square

insert image description here

n,m = map(int,input().split())
dp = [[0] * (m + 5) for i in range(0,n + 5)]

for i in range(1,n + 1):
    for j in range(1,m + 1):
        if i == 1 and j == 1:
            dp[i][j] = 1
        elif i % 2 == 0 and j % 2 == 0:
            dp[i][j] = 0
        else:
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
print(dp[n][m])

4. Integer concatenation

insert image description here

Ideas:

It can be seen that what we are looking for is this number:

A j * 10 lenA i + A i % k == 0

That is to say, the value of A j multiplied by 10 to the power of len(Ai) divided by k and Ai divided by k should add up to 0 or k.

Then we preprocess it directly. For each input number, we calculate the value of its multiplied by an order divided by k. Considering that the value of Ai can reach up to 1e9, we need a total of 1 to 10 orders. We have a total of 10 dictionaries (hashes) are required to store.

n,k = map(int,input().split())
x = list(map(int,input().split()))
d = [dict() for i in range(0,11)]
for i in x:
    for j in range(1,11):
        if i * (10 ** j) % k in d[j]:
            d[j][i * (10**j) % k]+=1
        else:
            d[j][i * (10**j) % k] = 1
# print(d)
ans = 0
for i in x:
    l = len(str(i))
    if (k - i % k) % k in d[l]:
        if i * (10 ** j) % k == (k - i % k) % k:
            ans += d[l][(k - i % k) % k] - 1
            # 如果数和本身拼接起来也可以的话,那么要减去自身
        else:
            ans += d[l][(k - i % k) % k]
print(ans)

5. Super glue

insert image description here

ideas

Idea 1:

We assume that the first time to merge any two stones a, b next to each other, then the glue required is to multiply the two stones a*b, and then get the weight of (a+b), if you continue to merge after this, (a +b) Also multiply other stones, and then get the weight as (a+b+…), so it can be seen that each stone must be multiplied by the next stone once, and whether the order is multiplied first or later does not affect result, so we can merge directly from left to right.

Idea 2:

If there are two stones a and b, they need glue a*b if they are combined. We assume that all stones are regarded as two parts, and they are regarded as two parts respectively. We know that the order has no effect, so no matter what How to combine, the result is the same.

n = int(input())
m = list(map(int,input().split()))
ans = 0
sum = 0
for i in range(0,n - 1):
    sum += m[i]
    ans += sum * m[i + 1]
print(ans)

6. Network Analysis

None at this time.

Guess you like

Origin blog.csdn.net/qq_45985728/article/details/123794198