我们可以枚举因数来更新每个数字的中位因数里面小于等于 的那个。因为x最大为
,因此只需要枚举到
。然后暴力更新当前因数的倍数。这个复杂度是O(nlogn)
const int mod = 998244353; long long f[1000010]; class Solution { public: /** * * @param x int整型 给定的x * @return int整型 */ int sum(int x) { // write code here for(int i = 1; i <= 1000000; i ++) for(long long j = i; j * i <= 1000000; j ++) f[i * j] = (i + j) / 2; for(int i = 1; i <= 1000000; i ++) f[i] = (f[i] + f[i - 1]) % mod; return f[x]; } };