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. [更新中]【fit-flow使用总结】djang开发中git flow使用总结

    djang开发中git flow使用总结 初次接触可以先看看此链接上关于git flow的东西http://danielkummer.github.io/git-flow-cheatsheet/ind ...

  2. PowerDesigner CDM中取消默认不能存在同名主键的方法

    This data item is already used in a primary identifier.Normalization rules prevent ... 处理的方法为: 菜单栏上的 ...

  3. 【Android 多媒体应用】使用 MediaPlayer 播放视频

    1.MainActivity.java import android.media.AudioManager; import android.media.MediaPlayer; import andr ...

  4. 关于win7 下双击不能打开jar 文件

    关于这个问题解决如下: 我的java 安装路径为C:\java\jdk1.6\bin 1,首先检查jdk 的路径是否安装正确. 2,导出jar 包时,是否有添加 main class. 如果通过在do ...

  5. Access restriction required library rt.jar

    在JAVA项目开发中,使用到了BASE64Decoder,但编辑运行时却会出现以下错误:Access restriction required library rt.jar,这里就详细的说明一下如何解 ...

  6. day70 12-存储过程和存储函数

    什么是相关子查询? 这是一个子查询,子查询本身又是一个多表查询.where不能用组函数,但是可以用字符函数instr().除了order by排序没有考,查询语句的所有内容都考了.这个题有点难度. 今 ...

  7. docker创建容器打开两个端口

     docker run -d -it --name c6_3 -v :/mnt -p 5000:8000 -p 3000  centos  注释: -v 后面为共享文件夹

  8. jquery 使用记录

    1.jquery 筛选一个属性符合多个条件  var myTag=$("input[id=myid][name=myname][type=button]").length; 2.j ...

  9. 更新anaconda及所有包

    ################################## # 更新Anaconda conda update conda # 更新所有包 conda update --all ###### ...

  10. CodeForces 384E Propagating tree (线段树+dfs)

    题意:题意很简单么,给定n个点,m个询问的无向树(1为根),每个点的权值,有两种操作, 第一种:1 x v,表示把 x 结点加上v,然后把 x 的的子结点加上 -v,再把 x 的子结点的子结点加上 - ...