forked from markhary/codility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDiv.cpp
More file actions
37 lines (30 loc) · 792 Bytes
/
CountDiv.cpp
File metadata and controls
37 lines (30 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// https://app.codility.com/programmers/lessons/5-prefix_sums/count_div/
//
// Task Score: 100%
// Correctness: 100%
// Performance: 100%
// Detected time complexity: O(1)
//
#include <iostream>
#include <vector>
#include "macros.h"
using namespace std;
namespace countdiv
{
int bruteForce(int A, int B, int K)
{
int count = 0;
for (int i = A; i <= B; i++) {
count += !(i%K);
}
return count;
}
int solution(int A, int B, int K)
{
// Have to separate these because of integer rounding
// in this case int(B/K) - int (A/K) != int (B-A)/K
// The rest is just simple math and % cleverness
// Not sure why this is a prefix sum solution
return ( B/K - A/K + !(A%K) );
}
}