一只兔子藏身于20个圆形排列的洞中(洞从1开始编号),一只狼从x号洞开始找,下次隔一个洞找(及在x+2号洞找),在下次个两个洞找(及在x+5号洞找),它找了n次仍然没有找到。问兔子可能在那些洞中。
输入描述:
输入有多组数据,每组数据一行两个整数分别为x和n(x <= 20,n <= 100000)
输出描述:
每组数据一行按从小到大的顺序输出兔子可能在的洞,数字之间用空格隔开。若每个洞都不肯能藏着兔子,输出-1。
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int x;
int n;
while(sc.hasNext()){
x = sc.nextInt();
n = sc.nextInt();
boolean[] flag = new boolean[21];
int cur = x;
flag[cur] = true;
for(int i=2; i<=n; i++){
cur = (cur + i) % 20;
if (cur == 0)
cur = 20;
flag[cur] = true;
}
ArrayList<Integer> result = new ArrayList<Integer>();
for(int i=1; i<21; i++){
if(flag[i] == false){
result.add(i);
}
}
if(result.size() == 0){
System.out.println(-1);
continue;
}
Collections.sort(result);
for(Integer i : result){
System.out.print(i + " ");
}
System.out.println();
}
}
}