zoukankan      html  css  js  c++  java
  • 【高精度】【找规律】Gym

    题意:给你一个经典的汉诺塔递归程序,问你最少几步使得三个柱子上的盘子数量相同。(保证最开始盘子数量可以被3整除)

    规律:ans(n)=2^(2*n/3-1)+t(n/3)。

    t(1)=0.

    t(n)=

    t(n-1)+1,n为偶数

    t(n-1)*4+2,n为奇数。

    Java文件读写主要有以下两种方法,第二种,输出格式更随心所欲,更实用:

    import java.util.*;
    import java.io.*;
    import java.math.*;
    
    public class Main{
    	public static void main(String[] argc){
    		BigInteger[] t=new BigInteger[305];
    		BigInteger[] pw=new BigInteger[305];
    		t[1]=BigInteger.ZERO;
    		for(int i=2;i<=100;++i){
    			if(i%2==0){
    				t[i]=t[i-1].add(BigInteger.ONE);
    			}
    			else{
    				t[i]=t[i-1].multiply(BigInteger.valueOf(4l)).add(BigInteger.valueOf(2l));
    			}
    		}
    		pw[0]=BigInteger.ONE;
    		for(int i=1;i<=300;++i){
    			pw[i]=pw[i-1].multiply(BigInteger.valueOf(2l));
    		}
    		Scanner cin = new Scanner(System.in);
    		try{cin=new Scanner(new FileInputStream("input.txt"));}catch(Exception e){}
    		int n=cin.nextInt();
    		cin.close();
    		/*pw[2*n/3-1].add(t[n/3]).toString()*/
    		File file=new File("output.txt");
    		try{
    			BufferedWriter bf=new BufferedWriter(new PrintWriter(file));
    			bf.append(pw[2*n/3-1].add(t[n/3]).toString());
    			bf.close();
    		}
    		catch(Exception e){}
        }
    }
    
    import java.util.*;
    import java.io.*;
    import java.math.*;
    
    public class Main{
    	public static void main(String[] argc){
    		BigInteger[] t=new BigInteger[305];
    		BigInteger[] pw=new BigInteger[305];
    		t[1]=BigInteger.ZERO;
    		for(int i=2;i<=100;++i){
    			if(i%2==0){
    				t[i]=t[i-1].add(BigInteger.ONE);
    			}
    			else{
    				t[i]=t[i-1].multiply(BigInteger.valueOf(4l)).add(BigInteger.valueOf(2l));
    			}
    		}
    		pw[0]=BigInteger.ONE;
    		for(int i=1;i<=300;++i){
    			pw[i]=pw[i-1].multiply(BigInteger.valueOf(2l));
    		}
    		Scanner cin = new Scanner(System.in);
    		try{cin=new Scanner(new FileInputStream("input.txt"));}catch(Exception e){}
    		int n=cin.nextInt();
    		cin.close();
    		/*pw[2*n/3-1].add(t[n/3]).toString()*/
    		//File file=new File("output.txt");
    		try{
    			FileWriter fw = new FileWriter("output.txt", true);
    			PrintWriter cout = new PrintWriter(fw);
    			cout.println(pw[2*n/3-1].add(t[n/3]));
    			cout.flush();
    			//BufferedWriter bf=new BufferedWriter(new PrintWriter(file));
    			//bf.append(pw[2*n/3-1].add(t[n/3]).toString());
    			//bf.close();
    		}
    		catch(Exception e){}
        }
    }
  • 相关阅读:
    Dijkstra-leetcode 743.网络延迟时间
    BFS-leetcode787 K站中转内最便宜的航班
    图论基础——单源最短路径问题
    DFS leetcode-547 朋友圈
    SpringBoot 使用注解向容器中注册Bean的方法总结
    SpringBoot对SpringMVC的支持
    数据源简介
    Spring MVC简介
    2020-2-10 Python 列表切片陷阱:引用、复制与深复制
    2020-2-2 语法糖
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/7625774.html
Copyright © 2011-2022 走看看