300231: CF45F. Goats and Wolves

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Goats and Wolves

题意翻译

有一次,瓦西亚需要尽快把他的羊和狼运到河对岸。 规定: 每次渡河船上必须有瓦西亚和$1−n$只动物 如果在河岸上或船上,狼的数量刚好超过山羊,那么狼就会吃掉山羊。注意,当船靠岸羊和狼上下船的过程中也会发生狼吃羊的情况。 现在问你,如果瓦西亚要把所有动物运到河对岸并保证动物一直不少,他过河的次数最少是多少? 输入为一行,即动物数量和船的容量。 输出为一行,如果有方案可行,输出最少次数,否则输出$-1$。

题目描述

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?

输入输出格式

输入格式


The first line contains two space-separated numbers $ m $ and $ n $ ( $ 1<=m,n<=10^{5} $ ) — the number of animals and the boat's capacity.

输出格式


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

3 2

输出样例 #1

11

输入样例 #2

33 3

输出样例 #2

-1

说明

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

Input

题意翻译

有一次,瓦西亚需要尽快把他的羊和狼运到河对岸。 规定: 每次渡河船上必须有瓦西亚和$1−n$只动物 如果在河岸上或船上,狼的数量刚好超过山羊,那么狼就会吃掉山羊。注意,当船靠岸羊和狼上下船的过程中也会发生狼吃羊的情况。 现在问你,如果瓦西亚要把所有动物运到河对岸并保证动物一直不少,他过河的次数最少是多少? 输入为一行,即动物数量和船的容量。 输出为一行,如果有方案可行,输出最少次数,否则输出$-1$。

加入题单

算法标签: