[USCAO08OPEN]农场周围的道路Roads Around The Farm

英文题目

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

题目描述

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

输入输出样例

输入样例

6 2 

输出样例

3

分析

一开始我还以为是结论题
推了半天发现根本不是
然后就想到了递归
没错暴力模拟递归出结果
真的没啥好解释的啊
能分就分,不能分就不分停下来。
上代码。(蒟蒻的码风绝对独树一帜)
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>

using namespace std;

int k;//不全局也行,就得传 
int ans;//不全局就不写函数 

void fen(int)//分裂 

int main()
{
    int n;//如题 

    ans=0;//最终结果 

    scanf("%d",&n);//如题 
    scanf("%d",&k);//如题 

    fen(n);//走道了 

    printf("%d",ans);//输出走人 
    
    for(;;);//明眼人一看就知道防抄袭 

    return 0;
}

void fen(int x)//分裂,我习惯写后面 
{

    if(x<=k||(x-k)%2!=0)//不能分 
    {
        ans++;
        return;
    }
    
    fen((x-k)/2);//少的那群 
    fen((x+k)/2);//多的那群 

}
代码特短特简单

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值