[BZOJ] 1621: [Usaco2008 Open]Roads Around The Farm分岔路口
1621: [Usaco2008 Open]Roads Around The Farm分岔路口
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 897 Solved: 665
[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.
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
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
Analysis
Emmm... 刚开始以为还要枚举什么的,结果发现奶牛的分群如果满足条件的话就是个定值: x + ( x + k ) = remain
显然 x 是个定值
那么直接DFS好了
至于复杂度.. 我觉得显然不会爆栈,但还是不会证明qwq
Code
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 5 int ans = 0,k; 6 7 void dfs(int remain){ 8 if((remain-k)%2 == 0 && remain > k){ 9 int x = (remain-k)/2; 10 dfs(x); 11 dfs(x+k); 12 }else ans++; 13 } 14 15 int main(){ 16 int n; 17 scanf("%d%d",&n,&k); 18 dfs(n); 19 printf("%d",ans); 20 21 return 0; 22 }
转载请注明出处 -- 如有意见欢迎评论