码迷,mamicode.com
首页 > 其他好文 > 详细

2016-2017 ACM Central Region of Russia Quarterfinal Programming Contest BHanoi tower

时间:2018-09-17 20:59:35      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:.sh   The   value   AMM   val   pre   review   img   integer   

B Hanoi tower

It has become a good tradition to solve the “Hanoi tower” puzzle at programming contests in Rybinsk. We will review the rules briefly.

技术分享图片

2*n/3-1移到C上,再从A移动一个到B上

再把C的n/3-1移到B上,现在所有上面都是1/3了

队友得到了一个神奇的公式2^(n-n/3-1)+2^(n/3-1)-1

但是经过提交是不行的,所以暴力打表找规律

#include <stdio.h>
using namespace std;
int num[3],f,f1;
int move(int n,int a,int b)
{
    //printf("Move disk %d from %c to %c\n",n,a,b);
    num[a]--,num[b]++;
    if(num[0]==num[1]&&num[1]==num[2])return 1;
    return 0;
}
void hanoi(int n,int a,int b,int c)
{
    if(f1)return;
    if(n==1)
    {
        if(move(n,a,c))
        {
            printf("%d\n",f);
            f1=1;
            return;
        }
    }
    else
    {
        hanoi(n-1,a,c,b);
        f++;
        if(move(n,a,c))
        {
            printf("%d\n",f);
            f1=1;
            return;
        }
        hanoi(n-1,b,a,c);
    }
}
int main()
{
    int n;
    for(int n=3; n<=30; n+=3)
    {
        num[0]=n,num[1]=num[2]=f=0,f1=0;
        hanoi(n,0,1,2);
    }
    return 0;
}

很快就会发现偶数的猜想是对的,所以对奇数进行讨论,发现正好是*4+2

暴力代码,交的表,因为莫名RE

import java.math.*;
import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        BigInteger tmp=BigInteger.ZERO;
        for(int n=3; n<=300; n+=3)
        {
            if(n/3%2==1)
            {
                System.out.println("\""+tmp.multiply(BigInteger.valueOf(4))
                .add(BigInteger.valueOf(2))+"\",");
            }
            else
            {
                int x=n-n/3-1;
                int y=n/3-1;
                tmp=cal(x).add(cal(y)).subtract(BigInteger.ONE);
                System.out.println("\""+tmp+"\",");
            }
        }
    }
    static BigInteger cal(int x)
    {
        BigInteger ans=BigInteger.ONE;
        ans=ans.shiftLeft(x);
        return ans;
    }
}

 

2016-2017 ACM Central Region of Russia Quarterfinal Programming Contest BHanoi tower

标签:.sh   The   value   AMM   val   pre   review   img   integer   

原文地址:https://www.cnblogs.com/BobHuang/p/9664780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!