CF45F.Goats and Wolves

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Once Vasya needed to transport m goats and m wolves from riverbank to the other as quickly as possible. The boat can hold n animals and Vasya, in addition, he is permitted to put less than n animals in the boat. If in one place (on one of the banks or in the boat) the wolves happen to strictly outnumber the goats, then the wolves eat the goats and Vasya gets upset. When Vasya swims on the boat from one shore to the other, he must take at least one animal to accompany him, otherwise he will get bored and he will, yet again, feel upset. When the boat reaches the bank, first all the animals get off simultaneously, and then the animals chosen by Vasya simultaneously get on the boat. That means that at the moment when the animals that have just arrived have already got off and the animals that are going to leave haven't yet got on, somebody might eat someone. Vasya needs to transport all the animals from one river bank to the other so that nobody eats anyone and Vasya doesn't get upset. What is the minimal number of times he will have to cross the river?

曾经,瓦西亚需要尽快将 mm 只山羊和 mm 只狼从河的一岸运送到另一岸。船最多可容纳 nn 只动物,且瓦西亚本人也必须在船上;此外,他允许在船上装载少于 nn 只动物。若在某一地点(即某一边河岸上或船内),狼的数量严格多于山羊的数量,则狼会吃掉山羊,瓦西亚便会感到不快。当瓦西亚划船从一岸驶向另一岸时,他必须至少携带一只动物同行,否则他会因无聊而再次感到不快。当船抵达岸边时,所有动物同时下船,然后瓦西亚选定的动物再同时上船。这意味着:在刚刚抵达的动物已全部下船、而即将出发的动物尚未上船的那一瞬间,可能发生捕食事件。瓦西亚需将所有动物从河的一岸安全运送到另一岸,使得无人被吃,且瓦西亚不感到不快。他最少需要多少次横渡河流?

输入格式

The first line contains two space-separated numbers m and n (1 ≤ m, n ≤ 105) — the number of animals and the boat's capacity.

第一行包含两个用空格分隔的数字 mmnn1m,n1051 ≤ m, n ≤ 10^5)——分别表示动物的数量和船的载重容量。

输出格式

If it is impossible to transport all the animals so that no one got upset, and all the goats survived, print -1. Otherwise print the single number — how many times Vasya will have to cross the river.

如果无法运送所有动物,使得没有任何动物生气且所有山羊都存活,则输出 -1;否则输出一个整数——即瓦夏需要横渡河流的次数。

输入输出样例

  • 输入#1

    3 2

    输出#1

    11
  • 输入#2

    33 3

    输出#2

    -1

说明/提示

The first sample match to well-known problem for children.

第一个样例匹配儿童熟知的问题。

输入解题思路,AI测评打分。不知道怎么写?

首页