CodeForces - 690A2 Collective Mindsets (medium) nim博弈

A2. Collective Mindsets (medium)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Way to go! Heidi now knows how many brains there must be for her to get one. But throwing herself in the midst of a clutch of hungry zombies is quite a risky endeavor. Hence Heidi wonders: what is the smallest number of brains that must be in the chest for her to get out at all (possibly empty-handed, but alive)?

The brain dinner night will evolve just as in the previous subtask: the same crowd is present, the N - 1 zombies have the exact same mindset as before and Heidi is to make the first proposal, which must be accepted by at least half of the attendees for her to survive.

Input

The only line of input contains one integer: N, the number of attendees (1 ≤ N ≤ 109).

Output

Output one integer: the smallest number of brains in the chest which allows Heidi to merely survive.

Examples

input

Copy

1

output

Copy

0

input

Copy

3

output

Copy

1

input

Copy

99

output

Copy

49

找规律;

通过前16个可以发现:

奇数除以二就行了;

偶数有如下规律:

0                                                                            2

0   1                                                                       4   6

0   1    2    3                                                           8    10    12   14

0   1    2    3    4   5   6  7                                       16   18    20   22   24   26   28  30

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <queue>
using namespace std;
typedef long long ll;

int main()
{
    int n;
    while(cin >> n){
        if(n%2==1)
            cout << n/2 << endl;
        else{
            int ans = 1;
            while(ans <= n){
                ans *= 2;
            }
            cout << (n-ans/2)/2 <<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42754600/article/details/81213393