Bzoj1621: [usaco open] roads around the farm branch Intersection

Source: Internet
Author: User
1621: [usaco open] roads around the farm branch intersection time limit: 5 sec memory limit: 64 MB
Submit: 521 solved: 380
[Submit] [Status] 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.

 

Source

Silver

Problem: Simulate the Code:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #define inf 100000000012 #define maxn 300+1013 #define maxm 500+10014 #define ll long long15 using namespace std;16 inline ll read()17 {18     ll x=0,f=1;char ch=getchar();19     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}20     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}21     return x*f;22 }23 int n,k,ans=0;24 void dfs(int x)25 {26     if(x-k<2||((x&1)!=(k&1)))ans++;27     else28     {29       int y=(x-k)>>1;dfs(y);y=x-y;dfs(y);30     }31 }32 int main()33 {34     freopen("input.txt","r",stdin);35     freopen("output.txt","w",stdout);36     n=read();k=read();37     dfs(n);38     printf("%d\n",ans);39     return 0;40 }
View code

 

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.