BZOJ1621: [Usaco2008 Open]Roads Around The Farm分岔路口 (DFS)
1621: [Usaco2008 Open]Roads Around The Farm分岔路口
Time Limit: 5 Sec Memory Limit: 64 MB Submit: 698 Solved: 513 [Submit][Status][Discuss]
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只.最后有三群奶牛.
思路;可分的条件是n>=k+2并且n和k的奇偶性相同。dfs即可。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年04月04日 星期一 01时49分36秒
4File Name :code/bzoj/1621.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33int n,k;
34int ans;
35
36int dfs( int n,int k)
37{
38 // cout<<"n:"<<n<<" k:"<<k<<endl;
39 if ((n+k)%2==1) return 1;
40 if (n<k+2) return 1;
41 int res = dfs((n+k)/2,k)+dfs((n-k)/2,k);
42 return res;
43}
44int main()
45{
46 #ifndef ONLINE_JUDGE
47 freopen("code/in.txt","r",stdin);
48 #endif
49 cin>>n>>k;
50 ans = dfs(n,k);
51 cout<<ans<<endl;
52 #ifndef ONLINE_JUDGE
53 fclose(stdin);
54 #endif
55 return 0;
56}