Since N ≤ 100, there is no problem with the full search, so you only have to pay attention to "Because the conversion is a kick after trying, so be sure to make it less than the number of tries".
N = int(input()) result = 0 for i in range(N // 5 + 1): for j in range(i + 1): for k in range(N // 3 + 1): if i * 5 + j * 2 + k * 3 == N: result += 1 print(result)
All the probabilities should be calculated and the maximum value should be taken. If the probability that a diamond is contained in a treasure box is x, it will be the probability x when it is in the case of STAY, and when it is not in the case of CHANGE. (Because you can open the treasure chest of the lost that you did not specify, if it is not in the specified treasure chest, it will be a treasure chest that definitely contains the CHANGE destination), so the probability is 1 --x.
P, Q, R = map(int, input().split()) a = P / (P + Q + R) b = Q / (P + Q + R) c = R / (P + Q + R) print(max(a, b, c, 1 - a, 1 - b, 1 - c))
Typical problem of DP. For each card not taken, the remainder obtained by dividing the sum of the integers written on the cards by 10 is used as the index, the number of cards taken is used as the value, and the maximum number of cards taken is taken. Just do DP.
N, *A = map(int, open(0).read().split()) dp = [-1] * 10 dp = 0 for a in A: t = [-1] * 10 for i in range(10): if dp[i] == -1: continue if t[i] < dp[i]: t[i] = dp[i] n = (i + a) % 10 if t[n] < dp[i] + 1: t[n] = dp[i] + 1 dp = t print(dp)
From Fermat's Little Theorem, a p-1 </ sup> ≡ 1 (mod p), so a (p-1) 2 </ sup> </ sup> ≡ 1 (mod p) However, (p-1) 2 </ sup> = p 2 </ sup> -2p + 1≡1 (mod p), so (p-1) 2 </ sup> Is generally the answer. Fermat's little theorem has the condition that a and p are mutually prime, so this answer cannot be used only for p = 2, but the answer for 2 is in the sample.
N, *p = map(int, open(0).read().split()) for x in p: if x == 2: print(2) else: print((x - 1) * (x - 1))