题目要求指定范围内约数数目最多的数以及对应的约数数量。
根据唯一分解定理,对于任意正整数,有:
那么根据乘法原理,约数的总数目为:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int MAXN = 1000001;
int main() {
int T;
cin >> T;
int Left, Right;
while (T--) {
cin >> Left >> Right;
int Ans = 0, Node = Left;
for (int n = Left; n <= Right; ++n) {
int Number = n;
int Count = 1;
int Max = ceil(sqrt(Right));
int NumberOfDifP = 0;
for (int i = 2; i <= Max; ++i) {
if (Number % i == 0) {
++NumberOfDifP;
int temp = 0;
while (Number % i == 0) {
Number /= i;
++temp;
}
Count *= (temp + 1);
}
}
if (NumberOfDifP == 0) {
Count = 1;
}
if (Number != 1) {
++Count;
}
if (Ans < Count) {
Ans = Count;
Node = n;
}
}
printf("Between %d and %d, %d has a maximum of %d divisors.\n", Left, Right, Node, Ans);
}
return 0;
}
来源:CSDN
作者:ORZZROORZZRO
链接:https://blog.csdn.net/qq_42971794/article/details/104625216