[Usaco2008 open] roads around the farm branch [water question]

Source: Internet
Author: User
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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.