【洛谷P2907】 【USACO08OPEN】农场周围的道路 水模拟分治
来源:互联网 发布:熟悉办公软件 编辑:程序博客网 时间:2024/05/01 15:47
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】农场周围的道路 水模拟分治
- luogu2907 [USACO08OPEN]农场周围的道路Roads Around The Farm
- USACO08OPEN
- 18.寻找你周围的道路:在设有障碍物的网格中寻路
- 2014.7.7 模拟赛【小K的农场】
- 洛谷 1993 小 K 的农场
- 洛谷 P1993 小 K 的农场
- 洛谷 P1993 小 K 的农场
- 洛谷P2910 [USACO08OPEN]寻宝之路Clear And Present Danger
- NOIP模拟:边的处理(分治)
- 我周围的雷人雷语
- 我周围的生活
- JZOJ 3806. 【NOIP2014模拟8.24】小X 的道路修建
- 【NOIP2014模拟8.24】小X 的道路修建
- {题解}[jzoj3806]【NOIP2014模拟8.24】小X 的道路修建
- JZOJ 3806 【NOIP2014模拟8.24】小X的道路修建
- 相连的农场题解
- 题目:相连的农场
- 并查集---畅通工程
- Mi3拆机后电话接听方有杂音
- 图片服务器搭建
- struts2表单校验与错误信息处理
- linux驱动第一个程序
- 【洛谷P2907】 【USACO08OPEN】农场周围的道路 水模拟分治
- TensorFlow学习笔记--1.0 版本的一个小坑记录
- POJ3669 Meteor Shower(bfs)
- ubuntu16.04下安装MySQL-pthon,遇到:EnvironmentError: mysql_config not found问题
- 动态加载apk的类
- Master的注册机制和状态管理
- 黄旗山
- 回调机制的一些理解
- STM32F030用IAR+JLINK在线调试无法下载FLASH问题