Roads Around the Farm
InterviewTime limit1sMemory limit128 MB
Given N cows and a difference K, a group of size s splits into two nonempty groups differing by K when possible; count the final grazing groups.
- Level
Medium4 of 10
- Topics
- Recursion, Math, Divide and conquer, Implementation
- Solved
- No attempts yet
Problem
Farmer John's cows have taken an interest in exploring the territory around the farm. Initially, all () cows set off down a road together as one big group. When the group reaches a fork in the road, it sometimes breaks into two smaller (nonempty) groups, each continuing down one of the roads. When one of those groups reaches another fork, it may split again, and so on.
The cows split in a peculiar way: if they can divide into two groups whose sizes differ by exactly (), they split that way; otherwise they stop exploring and begin grazing peacefully.
Assuming the road always provides new forks, compute the final number of groups of peacefully grazing cows.
Input
The first line contains two space-separated integers and .
Output
Print a single integer: the number of groups of grazing cows.
Hint
For and there are 3 final groups (containing 2, 1, and 3 cows).
6
/ \
2 4
/ \
1 3