Codeforces 101243 B Hanoi tower

题意:对于给定N个碟子的汉诺塔,问当三个柱子上的碟子数目第一次相同时,需要移动几步。数据保证N0(mod3)

思路:打表找规律。


// 打表代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.prefs.PreferenceChangeListener;

import javax.security.auth.x500.X500Principal;
import javax.swing.InternalFrameFocusTraversalPolicy;


public class Main {
    public static void main(String args[]) throws IOException
    {
        Scanner cin = new Scanner(System.in);
        try{cin=new Scanner(new FileInputStream("input.in"));}catch(Exception e){}
        int n;
        for(int T=3;T<=300;T+=3)
        {
            //n=cin.nextInt();
            n=T;
            int all=n;
            n=n/3;
            BigInteger ans = BigInteger.ONE;
            BigInteger up =BigInteger.ONE;
            if(all==3)
            {
                ans=BigInteger.valueOf(2);
            }
            else if(all%2==0)
            {
                for(int i=0;i<n;i++)
                {
                    //System.out.println(up);
                    ans=ans.add(up);
                    up=up.multiply(BigInteger.valueOf(2));
                }
                up=up.add(BigInteger.ONE);
                for(int i=0;i<n-1;i++)
                {
                    //System.out.println(up);
                    ans=ans.add(up);
                    up=up.multiply(BigInteger.valueOf(2));
                }
            }
            else 
            {
                for(int i=0;i<n-1;i++)
                {
                    //System.out.println(up);
                    ans=ans.add(up);
                    up=up.multiply(BigInteger.valueOf(2));
                }
                up=up.add(BigInteger.ONE);
                for(int i=0;i<n;i++)
                {
                    //System.out.println(up);
                    ans=ans.add(up);
                    up=up.multiply(BigInteger.valueOf(2));
                    if(i==n-2)up=up.subtract(BigInteger.ONE);
                }
            }
            String aaString = ans.toString();
            //System.out.println(ans);
            FileWriter fileWriter = new FileWriter(new File("output.txt"));  
            ///String s = new String("This is a test!  \n" + "aaaa");  
           // fileWriter.write(aaString); 
            System.out.println("\""+ans+"\",");
        }
    }
}
//C++ ac代码
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
string ans[120] = { "2",
"9",
"38",
"135",
"542",
"2079",
"8318",
"32895",
"131582",
"524799",
"2099198",
"8390655",
"33562622",
"134225919",
"536903678",
"2147516415",
"8590065662",
"34359869439",
"137439477758",
"549756338175",
"2199025352702",
"8796095119359",
"35184380477438",
"140737496743935",
"562949986975742",
"2251799847239679",
"9007199388958718",
"36028797153181695",
"144115188612726782",
"576460752840294399",
"2305843011361177598",
"9223372039002259455",
"36893488156009037822",
"147573952598266347519",
"590295810393065390078",
"2361183241469182345215",
"9444732965876729380862",
"37778931863094600663039",
"151115727452378402652158",
"604462909807864343166975",
"2417851639231457372667902",
"9671406556919232420904959",
"38685626227676929683619838",
"154742504910681330455412735",
"618970019642725321821650942",
"2475880078570795734170337279",
"9903520314283182936681349118",
"39614081257132309534260330495",
"158456325028529238137041321982",
"633825300114115263698305023999",
"2535301200456461054793220095998",
"10141204801825837463773439328255",
"40564819207303349855093757313022",
"162259276829213372398777265029119",
"649037107316853489595109060116478",
"2596148429267413850294045183574015",
"10384593717069655401176180734296062",
"41538374868278621172359158709616639",
"166153499473114484689436634838466558",
"664613997892457937028364282443595775",
"2658455991569831748113457129774383102",
"10633823966279326985536299491456450559",
"42535295865117307942145197965825802238",
"170141183460469231740910675752738881535",
"680564733841876926963642703010955526142",
"2722258935367507707743890347601564794879",
"10889035741470030830975561390406259179518",
"43556142965880123323459523703856007479295",
"174224571863520493293838094815424029917182",
"696898287454081973173581491830620002713599",
"2787593149816327892694325967322480010854398",
"11150372599265311570770220319565615575597055",
"44601490397061246283080881278262462302388222",
"178405961588244985132295190914152631338270719",
"713623846352979940529180763656610525353082878",
"2854495385411919762116609717830853229927202815",
"11417981541647679048466438871323412919708811262",
"45671926166590716193865302138111296192894730239",
"182687704666362864775461208552445184771578920958",
"730750818665451459101843020821051317142553624575",
"2923003274661805836407372083284205268570214498302",
"11692013098647223345629481079581903386505809756159",
"46768052394588893382517924318327613546023239024638",
"187072209578355573530071668259090783432992763150335",
"748288838313422294120286673036363133731971052601342",
"2993155353253689176481146576088573851923483438612479",
"11972621413014756705924586304354295407693933754449918",
"47890485652059026823698344753189666898758131930628095",
"191561942608236107294793379012758667595032527722512382",
"766247770432944429179173514194124611452059698541363199",
"3064991081731777716716694056776498445808238794165452798",
"12259964326927110866866776219678353547520673527267065855",
"49039857307708443467467104878713414190082694109068263422",
"196159429230833773869868419485143095817481649838694072319",
"784637716923335095479473677940572383269926599354776289278",
"3138550867693340381917894711643447289308309891028789231615",
"12554203470773361527671578846573789157233239564115156926462",
"50216813883093446110686315385819787653847372230899364003839",
"200867255532373784442745261543279150615389488923597456015358",
"803469022129495137770981046171215126561215611592144769253375" };

int main()
{
    int n;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
        while (cin >> n)
    {

        n /= 3;
        n--;
        cout << ans[n] << endl;
    }
}
©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值