# 第 252 场周赛题解

# Q1 1952. 三除数 (opens new window)

正常模拟即可。

class Solution5830 {
    fun isThree(n: Int): Boolean {
        var tmp = 0
        for (i in 1..n) {
            if (n % i == 0) {
                tmp++
                if (tmp > 3) return false
            }
        }
        return tmp == 3
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

# Q2 1953. 你可以工作的最大周数 (opens new window)

只有最大值超过总数的一半,才无法满额工作,仅能工作max2+1max * 2 + 1天。

class Solution5831 {
    fun numberOfWeeks(milestones: IntArray): Long {
        var sum = 0L
        var max = 0L
        milestones.forEach {
            sum += it
            max = maxOf(max, it.toLong())
        }
        return if (sum - max >= max) {
            sum
        } else {
            (sum - max) * 2 + 1
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Q3 1954. 收集足够苹果的最小花园周长 (opens new window)

无聊的数学计算。

class Solution5187 {
    fun minimumPerimeter(neededApples: Long): Long {
        var n = 0L
        var count = 0L
        while (count < neededApples) {
            n += 1
            count += 12 * n * n
        }
        return 8 * n
    }
}
1
2
3
4
5
6
7
8
9
10
11

# Q4 1955. 统计特殊子序列的数目 (opens new window)

遍历,分别当前以0120、1、2​结尾的序列数。

本周是无聊的一周,数学题没啥意思。

class Solution5833 {
    fun countSpecialSubsequences(nums: IntArray): Int {
        val mod = 1000000007L
        var a = 0L
        var b = 0L
        var c = 0L
        for (x in nums) {
            when (x) {
                0 -> a = (a * 2 + 1) % mod
                1 -> b = (b * 2 + a) % mod
                2 -> c = (c * 2 + b) % mod
            }
        }
        return c.toInt()
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16