Description John's n (1 ≤ n ≤ 1,000,000,000) cows are leaving to explore the land around the farm. they will walk along a road and walk all the way to the three forks (we can think that all intersections are like this ). at this time, this group of cows may be divided into two groups, respectively, along the next two ways to continue. if they go to the crossroads again, they may continue to split into two groups. the way cows split is odd: if a group of cows can be precisely divided into two parts, the number of cows in these two parts is exactly K (1 ≤ k ≤ 1000 ), then the herd will split at the Sancha intersection. otherwise, the herd will not split, and they will all stay here and eat grass calmly. calculate how many cows will graze in peace. input
Two integers N and K.
Output
Number of cattle at the end.
Sample input6 2
Input details:
There are 6 cows and the difference in group sizes is 2.
Sample output3
Output details:
There are 3 final groups (with 2, 1, and 3 cows in them ).
6
/\
2 4
/\
1 3
Hint
Six cows are divided into two and four. Four cows are divided into one and three, and three cows are finally added.
Very watery question, = but still 1wa...
Previously, I thought it could be divided into x = n/2 + k/2, y = n/2-(k-k/2)
Apparently, N's parity issue is not taken into account-I did not think of it.
Then find
X + y = n, x-y = K;
X = (n + k)/2;
So every recursion, X, Y = (n + k)/2-k;
When n <= K, or (n + k) % 2 = 1, return
Code:
#include <cstdio>#include <iostream>using namespace std;int n,k;int solve(int n){if((n+k)%2 || n<=k)return 1;int x;return solve(x=(n+k)/2)+solve(n-x);}int main() {freopen("raft.in","r",stdin);freopen("raft.out","w",stdout);cin>>n>>k;cout<< solve(n) << endl;return 0;}
[Usaco2008 open] roads around the farm branch [water question]