代码:
#include<iostream> #include<math.h> using namespace std; int a[100001] = { 0 }; int main() { int N; while (cin >> N) { int sum = N - 2; int i = 2; while (1) { if (a[i] == 0) { a[i] = pow(i + 1, 2)-1; } if (N >= a[i]) { sum += (N - a[i]) / i + 1; } else { break; } i++; } cout << sum << endl; } return 0; }
#include<iostream> #include<math.h> using namespace std; int main() { int N; while (cin >> N) { if (N <= 2) { printf("0\n"); continue; } long long sum = N - 2; for (long long i = 2; (i + 1)*(i + 1) - 1 <= N; i++) { sum += (N - (i + 1)*(i + 1) + 1 + i)/i; } printf("%lld\n", sum); } return 0; }