USACO 2008 Open Silver-roads around the farm(水题)

Description

Farmer John's cows have taken an interest in exploring the territory around the farm. Initially, all N (1 ≤ N ≤ 1,000,000,000) cows commence traveling down a road in one big group. Upon encountering a fork in the road, the group sometimes chooses to break into two smaller (nonempty) groups with each group continuing down one of the roads.  When one of those groups arrives at another fork, it might split again, and so on.

The cows have crafted a peculiar way of splitting: if they can split into two groups such that the sizes of the groups differ by exactly K (1 ≤ K ≤ 1000), then they will split in that way; otherwise, they stop exploring and just start grazing peacefully.

Assuming that there will always be new forks in the road, compute the final number of groups of peacefully grazing cows.

Input

* Line 1: Two space-separated integers: N and K

Output

* Line 1: A single integer representing the number of groups of grazing cows

Sample Input

6 2

Sample Output

3

Hint

SAMPLE INPUT DETAILS:

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

SAMPLE OUTPUT DETAILS:

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

6
/ \
2   4
    / \
    1   3

 

慢慢领会吧,令人厌的递归。。。

#include <stdio.h>
int n,k;
int f(int n)
{
    if((n+k)%2||n<=k) return 1;
    int x;
    return f(x=(n+k)/2)+f(n-x);
}
int main()
{
  //int n,k;
  while(~scanf("%d%d",&n,&k))
  {
    printf("%d\n",f(n));
  }
  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值