注:个人做题总结以方便复习

L题:L is the Only Lovely Problem

emmm 签到题

把输入的全部变成小写或大写,再判断是不是符合要求就行,上代码:

import sys

s = sys.stdin.readline().strip().lower()
if s[:6] == "lovely": print("lovely")
else: print("ugly")

B题:Classical String Problem

题目描述:
Given a string S consists of lower case letters. You're going to perform Q operations one by one. Each operation can be one of the following two types:

  • Modify: Given an integer x. You need to modify S according to the value of x. If x is positive, move the leftmost x letters in S to the right side of S; otherwise, move the rightmost |x| letters in S to the left side of S.
  • Answer: Given a positive integer x. Please answer what the x-th letter in the current string S is.

你会发现这道题的Modify操作只是把字符串的某长度的前缀移到最后或把某长度的后缀移到最前面,emmm,真模拟的话肯定TLE,这时候就会想到,他只是要查询第几个字符是什么,而这种操作我们维护一个指针 idx表示字符串开头的下标。每次M操作时就改变 idx即可,而A操作时,返回s[ (idx+x-1) % len(s)] 就是答案,上代码:

import sys

s = sys.stdin.readline().strip()
idx,p = 0,len(s)
n = int(sys.stdin.readline())
for _ in range(n):
    a,b = sys.stdin.readline().split()
    b = int(b)
    if a == "A": print(s[(idx + b - 1) % p])
    else: idx = (idx + b) % p

A题: Clam and Fish

题目描述:

There is a fishing game as following:

  • The game contains n stages, numbered from 1 to n.

  • There are four types of stages (numbered from 0 to 3):

type 0: There are no fish and no clam in this stage.

type 1: There are no fish and one clam in this stage.

type 2: There are one fish and no clam in this stage.

type 3: There are one fish and one clam in this stage.

In each stage, you can do exactly one of the following four actions.

  1. If there is a clam in the stage, you can use this clam to make one pack of fish bait. And the number of packs of fish bait you have is increased by one.  You can use this pack of fish bait to catch fish after this stage.

  2. If there is one fish in the stage, you can catch this fish without any fish bait. After this stage, the number of packs of fish bait you have is not changed.

  3. If you have at least one pack of fish bait. You can always catch one fish by using exactly one pack of fish bait even if there are no fish in this stage. After this stage, the number of packs of fish bait you have is decreased by one.

  4. You can do nothing.

Now, you are given nnn and the type of each stage. Please calculate the largest number of fish you can get in the fishing game.

这道题(感觉)是到思维题,我们发现获得鱼饵再转换为鱼至少要两个stage,而直接抓鱼只要一个stage就能得到一条鱼,所以能抓鱼肯定抓鱼嘛 抓鱼吃多香。然后就是 type 0的时候,有鱼饵肯定就干钓鱼这事是吧,没有就啥也不做。然后就是考虑 type 1怎么处理,是抓鱼饵还是钓鱼,这时候我们只要之前从后往前枚举一边给的字符串,统计后缀中2和3的数量并用 cnt记录,就可以O(1)的时间内算出哪怕鱼饵无限多我们最多也只能转换成鱼的数量, 这时候如果 len(s) - i - 1 - cnt[i] <= bait and bait != 0 就说明鱼饵已经够了,我们只要钓鱼就好,否则就抓鱼饵,上代码:

import sys

t = int(sys.stdin.readline())
while t > 0:
    t -= 1
    n = int(sys.stdin.readline())
    s = sys.stdin.readline().strip()
    cnt = [0]*(len(s))
    cnt[len(s) - 1] =  0
    for i in range(len(s)-2,-1,-1):
        cnt[i] = cnt[i+1] + 1 if s[i+1] == '2' or s[i+1] == '3' else cnt[i+1]
    bait,ans = 0,0
    for i in range(len(s)):
        if s[i] == '2' or s[i] == '3': ans += 1
        elif s[i] == '1':
            if len(s) - i - 1 - cnt[i] <= bait and bait != 0:
                bait -= 1
                ans += 1
            else: bait += 1
        else:
            if bait != 0:
                bait -= 1
                ans += 1
    print(ans)

F题:Fraction Construction Problem

题目描述:
There are t queries.
In each query, you are given two positive integers a and b (a,b<2e6)
Please print a line consists of four positive integers c,d,e,f satisfying the following constraints:

    • \frac{c}{d} - \frac{e}{f} = \frac{a}{b}
    • d < b and f < b
    • 1 <= c,e <= 4e12

If there are multiple solutions, you can print anyone.

If there are no solution, please print "-1 -1 -1 -1" in this line.

这是一道数学题,首先b = 1时直接输出四个-1就行,然后我们发现如果a,b能找到不是1的最大公约数(gcd)的话直接就可以输出(a+b)/gcd(a,b),  b/gcd(a,b), 1, 1就满足题意了。那如果a,b互质呢,我们把式子化简一下就成了\frac{cf-de}{df} = \frac{a}{b}, 我们可令df = b,且 gcd( d , f ) = 1,那么分子之间直接用扩展欧几里得来做就行了 x = c, y = -d, 然后用exgcd解出 x,y后乘个a就行了,上代码:

import sys

def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

def exgcd(a, b):
    if b == 0: return (a, 1, 0)
    d_, x_, y_ = exgcd(b, a%b)
    d,x,y = d_, y_, x_-a//b*y_
    return (d,x,y)

t = int(sys.stdin.readline())
while t > 0:
    t -= 1
    a, b = map(int, sys.stdin.readline().split())
    if b == 1:
        print(-1,-1,-1,-1)
        continue
    gc = gcd(a,b)
    if gc != 1:
        print((a+b)//gc, b//gc, 1, 1)
        continue
    d,f = 2,b
    while d*d <= b:
        if b % d == 0 and gcd(d,b//d) == 1:
            f = b // d
            break  # 穷举法求素因子
        d += 1
    if f == b:
        print(-1,-1,-1,-1)
        continue
    r, x, y = exgcd(f, d) # 解方程
    x, y = x * a, y * a  # 求 =a 的解
    if x > 0 and y < 0: print(x,d,-y,f)
    else: print(y,f,-x,d)

Logo

欢迎加入我们的广州开发者社区,与优秀的开发者共同成长!

更多推荐