题目大意:给出n,求1~n所有数的约数个数的和。
将“1~n所有数的约数”的模板中的factor[i*j].push_back(i)改为FactorCnt[i*j]++,最后再求一次和即可。
#include <cstdio> #include <cstring> using namespace std; const int MAX_N = 1000010; int Proceed(int n) { static int FactorCnt[MAX_N]; memset(FactorCnt, 0, sizeof(FactorCnt)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n / i; j++) FactorCnt[i*j]++; int ans = 0; for (int i = 1; i <= n; i++) ans += FactorCnt[i]; return ans; } int main() { int n; scanf("%d", &n); printf("%d ", Proceed(n)); return 0; }