1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <iostream> #include <cmath> #include <vector> #include <algorithm> using namespace std; long long solve1(int n) { vector<bool> p(n + 1, true); vector<long long> f(n + 1, 0), g(n + 1, 0); f[1] = 1; for (int i = 2; i * i <= n; i++) { if (p[i]) { vector<int> d; for (int k = i; k <= n; k *= i)d.push_back(k); reverse(d.begin(), d.end()); for (int k : d) { for (int j = k; j <= n; j += k) { if (p[j]) { p[j] = false; f[j] = i; g[j] = k; } } } } } for (int i = sqrt(n) + 1; i <= n; i++) { if (p[i]) { f[i] = i; g[i] = i; } } long long sum = 1; for (int i = 2; i <= n; i++) { f[i] = f[i / g[i]] * (g[i] * f[i] - 1) / (f[i] - 1); sum += f[i]; } return sum; } long long solve2(int n) { long long sum = 0; for (int i = 1; i <= n; i++) { sum += i * (n / i); } return sum; } int main() { int n; cin >> n; cout << solve1(n) << endl; cout << solve2(n) << endl; return 0; } |
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
将第 15 行删去,输出不变。()
2、当输入为 10
时,输出的第一行大于第二行。()
3、当输入为 1000
时,输出的第一行与第二行相等。()
4、solve1(n)
的时间复杂度为()。
5、solve2(n)
的时间复杂度为()。
6、当输入为 5
时,输出的第二行为( )。