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

Posted bobhuang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2016-2017 ACM Central Region of Russia Quarterfinal Programming Contest BHanoi tower相关的知识,希望对你有一定的参考价值。

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,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
",f);
            f1=1;
            return;
        }
    }
    else
    {
        hanoi(n-1,a,c,b);
        f++;
        if(move(n,a,c))
        {
            printf("%d
",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的主要内容,如果未能解决你的问题,请参考以下文章

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

2016-2017 ACM-ICPC Southeastern European Regional Programming Contest (SEERC 2016)

2016-2017 ACM-ICPC CHINA-Final

2016-2017 ACM-ICPC CHINA-Final Solution

ACM ICPC 2016–2017, NEERC, Northern Subregional Contest Problem J. Java2016

2016-2017 ACM-ICPC CHINA-Final 个人题解