-
Notifications
You must be signed in to change notification settings - Fork 0
/
Split_Permutation.cpp
60 lines (53 loc) · 991 Bytes
/
Split_Permutation.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
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
bool helper(ll n, ll mid)
{
ll sum = (n * (n + 1)) / 2;
if (sum % mid != 0)
{
return false;
}
ll targetSum = sum / mid;
ll currentSum = 0;
ll count = 0;
for (ll i = 1; i <= n; ++i)
{
currentSum += i;
if (currentSum == targetSum)
{
currentSum = 0;
count++;
}
else if (currentSum > targetSum)
{
return false;
}
}
return count == mid;
}
int main()
{
int test;
cin >> test;
while (test--)
{
ll n;
cin >> n;
ll l = 1, h = (n * (n + 1)) / 2;
while (l <= h)
{
ll mid = l + (h - l) / 2;
if (helper(n, mid))
{
l = mid + 1;
}
else
{
h = mid - 1;
}
}
cout << h << endl;
}
return 0;
}