-
Notifications
You must be signed in to change notification settings - Fork 0
/
#012:Highly_divisible_triangular_number.cpp
67 lines (58 loc) · 1.3 KB
/
#012:Highly_divisible_triangular_number.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int getNumberOfDivisors(long n)
{
int divisors = 0;
for (long i = 1; i < sqrt(n); i++)
{
if (!(n % i))
divisors += 2;
}
if (ceil(sqrt(n)) == floor(sqrt(n)))
divisors++;
return (divisors);
}
void triangleNumber(vector<int> v, int size)
{
long tmp, size_uniq;
int n, sum;
vector<int> v_uniq = v;
vector<long> res;
sort (v_uniq.begin(), v_uniq.end());
v_uniq.erase(unique(v_uniq.begin(), v_uniq.end()), v_uniq.end());
size_uniq = v_uniq.size();
n = 1;
sum = 0;
for (int i = 0; i < size_uniq; i++)
{
tmp = 0;
while (tmp <= v_uniq[i]) {
sum += n;
tmp = getNumberOfDivisors(sum);
n++;
}
res.push_back(sum);
sum -= --n;
}
for (int i = 0; i < size; i++)
{
auto it = find(v_uniq.begin(), v_uniq.end(), v[i]);
n = distance(v_uniq.begin(), it);
cout << res[n] << endl;
}
}
int main() {
int n, x;
vector<int> input;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> x;
input.push_back(x);
}
triangleNumber(input, n);
return 0;
}