小易总是感觉饥饿,所以作为章鱼的小易经常出去寻找贝壳吃。最开始小易在一个初始位置x_0。对于小易所处的当前位置x,他只能通过神秘的力量移动到 4 * x + 3或者8 * x + 7。因为使用神秘力量要耗费太多体力,所以它只能使用神秘力量最多100,000次。贝壳总生长在能被1,000,000,007整除的位置(比如:位置0,位置1,000,000,007,位置2,000,000,014等)。小易需要你帮忙计算最少需要使用多少次神秘力量就能吃到贝壳。
输入描述:
输入一个初始位置x_0,范围在1到1,000,000,006
输出描述:
输出小易最少需要使用神秘力量的次数,如果使用次数使用完还没找到贝壳,则输出-1
输入例子:
125000000
输出例子:
1
import java.util.*;
public class Main {
private static final int MOD = 1000000007;
private static long bfs(long x0) {
Queue<Long> que = new LinkedList<>();
Map<Long,Integer> vis = new HashMap<>();
vis.put(x0,0);
que.add(x0);
while (!que.isEmpty()) {
long x = que.poll();
int cnt = vis.get(x);
if (x == 0) {
return cnt;
}
if (cnt > 100000) {
continue;
}
long nx = (4 * x + 3) % MOD;
if (vis.get(nx) == null) {
vis.put(nx,cnt + 1);
que.add(nx);
}
nx = (8 * x + 7) % MOD;
if (vis.get(nx) == null) {
vis.put(nx,cnt + 1);
que.add(nx);
}
}
return -1;
}
public static void main(String[] arg) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
long x0 = scan.nextLong();
System.out.println(bfs(x0));
}
scan.close();
}
}