---
# System prepended metadata

title: PoTD Solutions - 31st May

---

PoTD Solutions - 31st May
------
### 1. CP - Easy
https://codeforces.com/blog/entry/91195

### 2. Puzzles - Medium
Yes, Wobushko can steal the election. Suppose in general that the number of voters is $N$ and we write $N$ as a product of non-trivial factors $N = N_k = n_1 \times n_2 \times \cdots\times n_k$. Then, letting $m_i = \left \lfloor{n_i/2}\right \rfloor +1$ we see that it is enough to have $M_k = m_1 \times m_2 \times \cdots \times m_k$ supporters in order to win the election. This follows by induction on $k$. The base case $k = 1$ is trivial. To win for general $k$, we have to divide the electorate into $N_{k-1}$ groups of size $n_k$. Wobushko then packs $M_{k-1}$ of these groups with $m_k$ of his supporters. After the vote there are $N_{k-1}$ voters altogether of which $M_{k-1}$ are supporters of Wobushko, completing the induction.
In the numerical example we write $20, 000, 000 = 5^7 \times 4^4$. In which case Wobushko needs to have $3^7 \times 3^4 = 177147$ supporters which is less than $1$ percent.

### 3. Systems - Hard

```cpp=
string ident(string arg)      //string passed by value(copied into arg)
{
    return arg;               //return string(move the value of arg out of ident() to a caller)
}
int main()
{
    string s1 {"Adams"};      //initialize string(construct in s1).
    s1 = ident(s1);           //copy s1 into ident()
                              //move the result of ident(s1) into s1;
                              //s1's value is "Adams".
    string s2 {"Pratchett"};  //initialize string(construct in s2)
    s1 = s2;                  //copy the value of s2 into s1
                              //both s1 and s2 have the value "Pratchett".
}
```
Clearly, after the call of `ident()`, the value of `s1` ought to be `"Adams"`. We copy the value of `s1` into the argument `arg`, then we move the value of `arg` out of the function call and (back) into `s1`. Next, we construct `s2` with the value `"Prachett"` and copy it into `s1`. Finally, at the exit from `main()` we destroy the variables `s1` and `s2`. The difference between *move* and *copy* is that after a copy two objects must have the same value, whereas after a move the source of the move is not required to have its original value. Moves can be used when the source object will not be used again. They are particularly useful for implementing the notion of moving a resource.

Several functions are used here:
* A constructor initializing a `string` with a string literal (used for `s1` and `s2`)
* A copy constructor copying a `string` (into the function argument `arg`)
* A move constructor moving the value of a `string` (from `arg` out of `ident()` into a temporary variable holding the result of `ident(s1)`)
* A move assignment moving the value of a `string` (from the temporary variable holding the result of `ident(s1)` into `s1`)
* A copy assignment copying a `string` (from `s2` into `s1`)
* A destructor releasing the resources owned by `s1`, `s2`, and the temporary variable holding the result of `ident(s1)`



