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. N个牛走路,遇到分叉,如果能分成人数之差为K的两堆,则分头行进. 否则就不走了.问最多分成几个块.

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

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 直接dfs就行了;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream> //#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int n, k;
int ans; void dfs(int cursum) {
if ((cursum + k) % 2 == 1 || (cursum - k) % 2 == 1 || cursum <= k) {
ans++;
return;
} if ((cursum + k) % 2 == 0) { dfs((cursum + k) / 2); }
if ((cursum - k) % 2 == 0) {
dfs((cursum - k) / 2);
} } int main()
{
//ios::sync_with_stdio(0);
rdint(n); rdint(k);
dfs(n);
cout << ans << endl;
return 0;
}

[USACO08OPEN]农场周围的道路Roads Around The Farm BZOJ 1621 DFS的更多相关文章

  1. bzoj1621 / P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm

    P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm 基础dfs,按题意递归即可. #include<iostream> #include< ...

  2. 【洛谷P2907】 【USACO08OPEN】农场周围的道路 水模拟分治

    P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm 题目描述 Farmer John's cows have taken an interest in ex ...

  3. bzoj1605 / P2905 [USACO08OPEN]农场危机Crisis on the Farm

    P2905 [USACO08OPEN]农场危机Crisis on the Farm 发现总步数$k<=30$,考虑用$k$瞎搞 设$f[u][i][j]$表示已经吹$u$次哨,全体奶牛向右走$i ...

  4. 洛谷P2905 [USACO08OPEN]农场危机Crisis on the Farm

    P2905 [USACO08OPEN]农场危机Crisis on the Farm 题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每 ...

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

    1621: [Usaco2008 Open]Roads Around The Farm分岔路口 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 521  S ...

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

    题目 1621: [Usaco2008 Open]Roads Around The Farm分岔路口 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 56 ...

  7. LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    今天我来给大家带来一片蒟蒻题解 ~~真香 LGOJ P2921  [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上 ...

  8. 缩点【洛谷P2921】 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    [洛谷P2921] [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  9. 「USACO08DEC」「LuoguP2921」在农场万圣节Trick or Treat on the Farm(tarjan

    题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定 ...

随机推荐

  1. 使用pip一次升级所有安装的Python包(太牛了)

    import pip from subprocess import call for dist in pip.get_installed_distributions(): call("pip ...

  2. lineNumber: 8; columnNumber: 128; cvc-elt.1: 找不到元素 'beans' 的声明

    转自:https://blog.csdn.net/java_yejun/article/details/51036638 spring和mybatis整合时出现了lineNumber: 8; colu ...

  3. 部署和调优 1.3 pureftp部署和优化-2

    登录ftp,用远程的一台机器,执行 lftp 如果没有这个命令,需要安装 yum install -y lftp 登录ftp lftp ftpuser1@192.168.1.117 输入口令,即密码 ...

  4. python中not的用法

    python中的not具体表示是什么: 在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法: ...

  5. MyBatis总结一:快速入门

    简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 ...

  6. C#简单的图片合成及防止并发的办法

    /// <summary> /// 合成图 /// </summary> private string ComposeCarBrandBadImage(AnonAttachme ...

  7. vue mock数据设置

    1.新建mock文件夹 2.添加你需要的数据例如新建商品表goods.json { "status":"0", "result":[ { & ...

  8. MyBatis01 MyBatis基础知识【搞清楚原理】

    1 MyBatis是什么 mybatis是一个持久层的框架,它对jdbc做了封装:是apache下的顶级项目 mybatis让程序将主要精力放在sql上,通过mybatis提供的映射方式,自由灵活生成 ...

  9. nginx负载均衡, 配置地址带端口

    nginx.conf  配置如下: upstream wlcf.dev.api { server 127.0.0.1:8833; server 127.0.0.2:8833; } server { l ...

  10. c# 新中新二代身份证阅读,包含头像,支持华视

    需要用到dll和文件: 其中3个dll文件是需要调用的dll,license.dat文件为解压图片的授权文件 以下是需要用到的dll里面的方法: /************************端口 ...