POJ1012

import java.util.Scanner;

public class Main
{
static int k;
static int bnum;
static int m;
static int[] resu=new int[14];

public static void main(String[] args) throws FileNotFoundException
{
    Scanner sc=new Scanner(System.in);
    sc=new Scanner(new File("/storage/emulated/0/AppProjects/Test/src/joseph"));
    while(true){
        k=sc.nextInt();
        if(k==0)
            break;
        if(resu[k]!=0){
            System.out.println(resu[k]);
            continue;
        }
        boolean is=true;
        for(int i=k+k/2+1;i>1;i++){
            if(i%(2*k)>=1&&i%(2*k)<=k)
                continue;
            bnum=k;
            m=i;
            is=isSafe(2*k,1,k);
            if(is){
                resu[k]=i;
                System.out.println(i);
                break;
            }
        }
    }
}

private static boolean isSafe(int n,int s,int t){
    if(bnum==0)
        return true;
    int res=m%n;
    if(res==0)
        res=n;
    if(res>=s&&res<=t)
        return false;
    bnum--;
    return isSafe(n-1,(n-res+s)%n,(n-res+s)%n+k-1);
}

}

sample input:
1
2
3
4
5
6
7
8
9
10
11
12
13
0

sample output:
2
7
5
30
169
441
1872
7632
1740
93313
459901
1358657
2504881

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值