-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChef and the stones.cpp
More file actions
64 lines (57 loc) · 1.76 KB
/
Copy pathChef and the stones.cpp
File metadata and controls
64 lines (57 loc) · 1.76 KB
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
/*
Chef has two piles of stones with him, one has n1 stones and the other has n2 stones.
Fired up by boredom, he invented a game with the two piles.
Before the start of the game Chef chooses an integer m.
In the j-th move:
He chooses a number xj such that 1 ≤ xj ≤ m, and removes xj stones from both the piles (this is only possible when both the piles have ≥ xj stones).
The number chosen must be unique over all the moves in the game. That is, for all k < j, xj ≠ xk.
The game stops when Chef is unable to make any more moves.
Chef wants to make the moves in such a way that the sum of the number of stones remaining in the two
piles is minimized. Please help Chef find this.
*/
#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
void minSum(ll, ll, ll);
void _minSum(ll, ll, ll&, vector<ll>);
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t, n1, n2, m;
cin >> t;
while(t--){
cin >> n1 >> n2 >> m;
minSum(n1, n2, m);
cout << endl;
}
return 0;
}
void minSum(ll n1, ll n2, ll m){
vector<ll> vec;
ll res = INT_MAX;
for(ll i = 1; i <= m; i++)
vec.push_back(i);
_minSum(n1, n2, res, vec);
cout << res;
}
void _minSum(ll n1, ll n2, ll &res, vector<ll> vec){
if(vec.empty()){
ll t = n1 + n2;
if(res > t)
res = t;
}
else{
for(ll i = 0; i < vec.size(); i++){
if(n1 - vec[i] >= 0 && n2 - vec[i] >= 0){
vector<ll> temp = vec;
temp.erase(temp.begin() + i);
_minSum(n1 - vec[i], n2 - vec[i], res, temp);
}
else{
ll t = n1 + n2;
if(res > t)
res = t;
}
}
}
}