【洛谷P2907】 【USACO08OPEN】农场周围的道路 水模拟分治
P2907 [USACO08OPEN]农场周围的道路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.
约翰的N(1≤N≤1,000,000,000)只奶牛要出发去探索牧场四周的土地.她们将沿着一条路走,一直走到三岔路口(可以认为所有的路口都是这样的).这时候,这一群奶牛可能会分成两群,分别沿着接下来的两条路继续走.如果她们再次走到三岔路口,那么仍有可能继续分裂成两群继续走. 奶牛的分裂方式十分古怪:如果这一群奶牛可以精确地分成两部分,这两部分的牛数恰好相差K(1≤K≤1000),那么在三岔路口牛群就会分裂.否则,牛群不会分裂,她们都将在这里待下去,平静地吃草. 请计算,最终将会有多少群奶牛在平静地吃草.
输入输出格式
输入格式:
- Line 1: Two space-separated integers: N and K
输出格式:
- Line 1: A single integer representing the number of groups of grazing cows
输入输出样例
6 2
3
说明
There are 6 cows and the difference in group sizes is 2.
There are 3 final groups (with 2, 1, and 3 cows in them).
6/ \2 4/ \1 3
没啥好说的,开始还想数学能不能O(1)做出来。。。干脆直接模拟吧,据说主定理推出来是O(logn)
不会尽管留言或Q568251782
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream> const int INF = 9999999; long long n;
int cnt;
int k; void dfs(int rest)
{
if( (rest > k) && !( (rest % 2) ^ ( k % 2 ) ) )//等价写法: if( n < 2+k || (n+k)&1 ){ tot++; return ; }
{
int a = (rest + k)/2;//我想优化一下少算一次。。但是这样写会更优:dfs((rest + k)/2);dfs((rest - k)/2);
dfs(a);
dfs(rest - a);
}
else
{
cnt++;
}
} int main()
{
scanf("%d%d", &n, &k);
dfs(n);
printf("%d", cnt);
return 0;
}
【洛谷P2907】 【USACO08OPEN】农场周围的道路 水模拟分治的更多相关文章
- bzoj1621 / P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm
P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm 基础dfs,按题意递归即可. #include<iostream> #include< ...
- 洛谷P2905 [USACO08OPEN]农场危机Crisis on the Farm
P2905 [USACO08OPEN]农场危机Crisis on the Farm 题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每 ...
- 洛谷 P2905 [USACO08OPEN]农场危机Crisis on the Farm
题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < ...
- 洛谷.4655.[CEOI2017]Building Bridges(DP 斜率优化 CDQ分治)
LOJ 洛谷 \(f_i=s_{i-1}+h_i^2+\min\{f_j-s_j+h_j^2-2h_i2h_j\}\),显然可以斜率优化. \(f_i-s_{i-1}-h_i^2+2h_ih_j=f_ ...
- Bzoj1018/洛谷P4246 [SHOI2008]堵塞的交通(线段树分治+并查集)
题面 Bzoj 洛谷 题解 考虑用并查集维护图的连通性,接着用线段树分治对每个修改进行分治. 具体来说,就是用一个时间轴表示图的状态,用线段树维护,对于一条边,我们判断如果他的存在时间正好在这个区间内 ...
- 【洛谷4219】[BJOI2014]大融合(线段树分治)
题目: 洛谷4219 分析: 很明显,查询的是删掉某条边后两端点所在连通块大小的乘积. 有加边和删边,想到LCT.但是我不会用LCT查连通块大小啊.果断弃了 有加边和删边,还跟连通性有关,于是开始yy ...
- 题解 洛谷 P3396 【哈希冲突】(根号分治)
根号分治 前言 本题是一道讲解根号分治思想的论文题(然鹅我并没有找到论文),正 如论文中所说,根号算法--不仅是分块,根号分治利用的思想和分块像 似却又不同,某一篇洛谷日报中说过,分块算法实质上是一种 ...
- 洛谷 P3676 - 小清新数据结构题(动态点分治)
洛谷题面传送门 题目名称好评(实在是太清新了呢) 首先考虑探究这个"换根操作"有什么性质.我们考虑在换根前后虽然每个点的子树会变,但整棵树的形态不会边,换句话说,割掉每条边后,得到 ...
- 洛谷 P4183 - [USACO18JAN]Cow at Large P(点分治)
洛谷题面传送门 点分治 hot tea. 首先考虑什么样的点能够对以 \(u\) 为根的答案产生 \(1\) 的贡献.我们考虑以 \(u\) 为根对整棵树进行一遍 DFS.那么对于一个点 \(v\), ...
随机推荐
- php链表笔记:链表的检测
<?php /** * Created by PhpStorm. * User: huizhou * Date: 2018/12/2 * Time: 11:48 */ /** * 链表的检测 * ...
- 报错C1189 #error: "No Target Architecture"
根本原因: 是因为单独包含了一些windows.h已经包含了的头文件如"fileapi.h","WinUser.h",但是却没有包含windows.h 或者先包 ...
- PAT甲级——A1071 Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- Python学习day45-数据库(总结)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- 转载:Linux 安装Java
1.到官网下载 jdk-8u131-linux-x64.tar.gz 官网地址:http://www.Oracle.com/technetwork/java/javase/downloads/jdk8 ...
- 跟我一起在ubuntu中安装docker
卸载旧版本 $ sudo apt-get remove docker docker-engine docker.io 查看ubuntu版本 设置安装源 通过如下步骤,设置安装源仓库,这里我们使用阿里源 ...
- LUOGU P3960 列队 (noip2017 day2T3)
传送门 解题思路 记得当时考试我还是个孩子,啥也不会QAQ.现在回头写,用动态开点的线段树,在每行和最后一列开线段树,然后对于每次询问,把x行y列的删去,然后再把x行m列的元素加入x行这个线段树,然后 ...
- 洛谷 P3750 [六省联考2017]分手是祝愿
传送门 题解 //Achen #include<algorithm> #include<iostream> #include<cstring> #include&l ...
- (转载)My97 datepicker使用指南
这里先显示大家都可以看到的My97DatePicker的用法: WdatePicker日历控件使用方法(http://www.cnblogs.com/yuhanzhong/archive/2011/0 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转]
模板方法模式定义了一个算法骨架,允许子类对算法的某个或某些步骤进行重写(override). 1 2{<HeadFirst设计模式>之模板方法模式 } 3{ 编译工具: Del ...