Learn C++ from a baby! Record the questions in the process of CSP-J preparation and study, and record every moment.
Attached is a summary post: Analysis of the real questions of the CSP-J preliminary competition over the years | Summary
#include <iostream>
using namespace std;
const int n = 100000;
const int N = n + 1;
int m;
int a[N], b[N], c[N], d[N]; //a[N]标记一个数是不是质数,b[N]用来存质数,c[N]用来存最小质因数的个数,d[N]=p^0+p^1+p^2+...+p^num,p指最大质因子
int f[N], g[N]; //f[N]用来存约数个数,g[N]用来存所有约数之和
void init()
{
f[1] = g[1] = 1;
for (int i=2; i<=n; i++) { //枚举i
if (!a[i]) {
b[m++] = i; //使用数组b存放地i个质数
c[i] = 1, f[i] = 2;
d[i] = 1, g[i] = i + 1;
}
for (int j=0; j<m&&b[j]*i<=n; j++) {
int k = b[j]; //k为当前的质数
a[i*k] = 1; //标记质数k的i倍为合数
if (i%k==0) { //遇到i的最小质因数就停止枚举
c[i*k] = c[i] + 1;
f[i*k] = f[i] / c[i*k] * (c[i*k]+1);
d[i*k] = d[i];
g[i*k] = g[i] * k + d[i];
break;
}
else {
c[i*k] = 1;
f[i*k] = 2 * f[i];
d[i*k] = g[i];
g[i*k] = g[i] * (k+1);
}
}
}
}
int main()
{
init();
int x;
cin >> x;
cout << f[x] << ' ' << g[x] << endl;
return 0;
}
Assume that the input x is a natural number not exceeding 1000, complete the following true-false and multiple-choice questions:
28. If the input is not "1", deleting line 13 will not affect the output result. ( )
[Answer]: Yes
【Analysis】
Line 13 preprocesses 1. The enumeration starts from 2. There is no data output related to 1 in the whole process. If the input starts from 2, there will be no impact.
29. "f[i]/c[i*k]" in line 25 may be indivisible and rounded down. ( )
[Answer]: Wrong
【Analysis】
f[i] represents the number of prime factors of i, c[i*k] represents the minimum number of prime factors of i*k, f[i]=(num1+1)*(num2+1)*...* (numn+1), where numx is a prime factor, c[i*k]=c[i]+1 (see code)=num1+1, the relationship between these two numbers must be multiples
30. After executing init(), the f array is not monotonically increasing, but the g array is monotonically increasing. ( )
[Answer]: Wrong
【Analysis】
The sum of divisors is not necessarily monotonically increasing, such as g[8]=1+2+4+8=15, g[9]=1+3+9=13
31. The time complexity of the init function is ( )
AO(n)
B.O(nlogn)
C.O(n*n^(1/2))
D.O(n^2)
[Answer]: A
【Analysis】
Characteristics of Euler Linear Screen
32. After executing init(), ( ) among f[1], f[2], f[3]... f[100] is equal to 2.
A.23
B.24
C.25
D.26
[Answer]: C
【Analysis】
Only the number of divisors of prime numbers is 2, so we need to find out how many prime numbers there are in 1-100. There are 25. Choose C.
33. When the input is "1000", the output is ( ).
A.“15 1340”
B.“15 2340”
C.“16 2340”
D.“16 1340”
[Answer]: C
【Analysis】
1000=2^3+5^3, the number of divisors=(3+1)*(3+1)=16. There are two divisors of 1000 and 500, which must exceed 1340, so choose C