如何有效地获得给定范围内的除数和
How to get the sum of divisors in a given range efficiently?
我试图在给定范围 a、b a <= b 中找到c
的所有除数之和。
我试图从 a
循环到 b
并求和 c
的所有除数,但这似乎效率低下,因为 a 和 b 之间的绝对差可以是 10^9。有没有办法降低这种方法的时间复杂度?
int a, b, c;
cin >> a >> b >> c;
long long sum = 0;
for (int i = a; i <= b; i++) {
if (i % c == 0) {
ans += i;
}
}
cout << sum << endl;
注意:问题尚不清楚我们是否需要对除数(在描述中)或可分整数(在代码示例中)求和。答案总结了可分割的项目。
这很简单。
- 求
from
,最小值,使得from % c == 0 && from >= a
- 求
to
,最大值,使得to % c == 0 && to <= b
.
int n = (to - from) / c + 1;
return n * (to + from) / 2;
返回to - from + c
.当to
可能溢出您的类型并且from
可能下溢时,请注意边界条件。
要查找from
请执行以下操作:
if (c < 0) c *= -1; // works unless c == MIN_INT
if (a % c == 0)
from = a;
else if (a >= 0)
from = (a / c * c) + c
else
from = a / c * c;
同样适用于to
,但考虑到我们需要向下舍入而不是向上舍入的事实。
另外,需要单独处理a > b
的情况。
编辑
下面是没有循环、递归或容器的完整代码。它在 O(1) 中运行:
int a, b, c;
std::cin >> a >> b >> c;
if (!std::cin) {
std::cout << "input errorn";
return 0;
}
if (c < 0) c*= -1;
const int from = [a,c] {
// no rounding needed
if (a % c == 0) return a;
// division rounds down to zero
if (a > 0) return (1 + a / c) * c;
// division rounds up to zero
return a / c * c;
}();
const int to = [b,c] {
// no rounding needed
if (b % c == 0) return b;
// division rounds down to zero
if (b > 0) return (b / c) * c;
// division rounds up to zero
return (b / c - 1) * c;
}();
int64_t sum = 0;
if (from <= to)
{
const int n = (to - from) / c + 1;
sum = n * (to + from) / 2;
}
std::cout << sum << 'n';
首先确定所有作为 c 除数的素数。 这将给你留下一个数字列表[w,x,y,z...]。 然后保留此列表中也是除数的所有整数倍的哈希表集。
int a, b, c;
cin >> a >> b >> c;
long long sum = 0;
std::vector<int> all_prime_factors = // Get all prime factors of c
std::unordered_set<int> factorSet;
for (int primefactor : all_prime_factors)
{
int factor = primefactor;
while (factor <= b)
{
if (factor % c == 0)
factorSet.insert(factor);
factor += primefactor;
}
}
for (int x : factorSet)
{
sum += x;
}
cout << sum << endl;
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 错误:未在此范围内声明'reverse'
- 有效地使用std::unordered_map来插入或增加键的值
- 并行用于C++17中数组索引范围内的循环
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 不计算一个范围内的完美数
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 如何有效地在 std::vector 中插入一对?
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- 有效地计算多维数组的累积和?
- "using namespace"子句在什么范围内有效?
- 如何使用包含内部类的类实例有效地从内部类访问成员?
- 如何有效地获得给定范围内的除数和
- 如何在大数组中最有效地增加指定范围内的值,然后找到最大值
- C++中数组范围内的有效元素计数
- 有没有一种方法可以有效地进行小的固定范围乘法
- 测试角度是否在范围内(有效且优雅)
- 如何检查两个数字是否在浮点类型的精度限制的有效数字"x"范围内?
- 是否有可能使用CUDA来有效地计算排序数组内元素的频率?