首页 > 代码库 > BZOJ1621: [Usaco2008 Open]Roads Around The Farm分岔路口

BZOJ1621: [Usaco2008 Open]Roads Around The Farm分岔路口

1621: [Usaco2008 Open]Roads Around The Farm分岔路口

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 521  Solved: 380
[Submit][Status]

Description

    约翰的N(1≤N≤1,000,000,000)只奶牛要出发去探索牧场四周的土地.她们将沿着一条路走,一直走到三岔路口(可以认为所有的路口都是这样的).这时候,这一群奶牛可能会分成两群,分别沿着接下来的两条路继续走.如果她们再次走到三岔路口,那么仍有可能继续分裂成两群继续走.    奶牛的分裂方式十分古怪:如果这一群奶牛可以精确地分成两部分,这两部分的牛数恰好相差K(1≤K≤1000),那么在三岔路口牛群就会分裂.否则,牛群不会分裂,她们都将在这里待下去,平静地吃草.    请计算,最终将会有多少群奶牛在平静地吃草.

Input

   两个整数N和K.

Output

    最后的牛群数.

Sample Input

6 2

INPUT DETAILS:

There are 6 cows and the difference in group sizes is 2.

Sample Output

3

OUTPUT DETAILS:

There are 3 final groups (with 2, 1, and 3 cows in them).

6
/ \
2 4
/ \
1 3

HINT

 

   6只奶牛先分成2只和4只.4只奶牛又分成1只和3只.最后有三群奶牛.

 

Source

Silver

 
题解:
模拟一下即可
代码:
 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