打卡记录
Plus and Multiply(模拟)
链接
要满足 a x + b ∗ y = n a^x + b * y = n ax+b∗y=n 的关系,可以枚举满足 b ∗ y = n − a x b * y = n - a ^ x b∗y=n−ax 的可余条件。
t = int(input())
for _ in range(t):n, a, b = map(int, input().split())if n == 1 or b == 1 or n % b == 1:print('YES')elif a == 1:if n % b == 1:print('YES')else:print('NO')else:mul = 1while mul <= n:if (n - mul) % b == 0:print('YES')breakmul *= aelse:print('NO')
到达首都的最少油耗(dfs)
链接
从每条路要被走过的车的次数来进行考虑。
class Solution:def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:n = len(roads) + 1g = [[] for _ in range(n)]g[0] = [-1]for x, y in roads:g[y].append(x)g[x].append(y)ans = 0def dfs(x, fa):res = 0for y in g[x]:if y == fa:continuenonlocal anstmp = dfs(y, x) + 1ans += (tmp - 1) // seats + 1res += tmpreturn resdfs(0, -1)return ans