题意:给你一个经典的汉诺塔递归程序,问你最少几步使得三个柱子上的盘子数量相同。(保证最开始盘子数量可以被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){}
}
}

【高精度】【找规律】Gym - 101243B - Hanoi tower的更多相关文章

  1. 递推+高精度+找规律 UVA 10254 The Priest Mathematician

    题目传送门 /* 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子 ...

  2. bzoj 1002 [FJOI2007]轮状病毒 高精度&&找规律&&基尔霍夫矩阵

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2234  Solved: 1227[Submit][Statu ...

  3. HDU 5351——MZL's Border——————【高精度+找规律】

    MZL's Border Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  4. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  5. 【洛谷】2144:[FJOI2007]轮状病毒【高精度】【数学推导??(找规律)】

    P2144 [FJOI2007]轮状病毒 题目描述 轮状病毒有很多变种.许多轮状病毒都是由一个轮状基产生.一个n轮状基由圆环上n个不同的基原子和圆心的一个核原子构成.2个原子之间的边表示这2个原子之间 ...

  6. [FJOI2007]轮状病毒 题解(dp(找规律)+高精度)

    [FJOI2007]轮状病毒 题解(dp(找规律)+高精度) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1335733 没什么好说的,直接把规律找出来,有 ...

  7. codeforces Gym 100418D BOPC 打表找规律,求逆元

    BOPCTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  8. 打表找规律C - Insertion Sort Gym - 101955C

    题目链接:https://cn.vjudge.net/contest/273377#problem/C 给你 n,m,k. 这个题的意思是给你n个数,在对前m项的基础上排序的情况下,问你满足递增子序列 ...

  9. bzoj1002 [FJOI2007]轮状病毒——找规律+高精度

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1002 打表找规律,似乎是这样:https://blog.csdn.net/fzhvampir ...

随机推荐

  1. css纯样式导航

    <style>.dropdown {    position: relative;    display: inline-block;} .dropdown-content {    di ...

  2. ribbon设置url级别的超时时间

    序 ribbon的超时设置,只能按转发的serviceId来分的,无法像nginx那样直接在每个转发的链接里头设置超时时间.这里hack一下,实现url基本的ribbon超时时间设置.具体的思路就是重 ...

  3. %和format 细说

    Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为%还是format这根本就不算个问题.不信你 ...

  4. mysql之基本数据库操作(二)

    环境信息 数据库:mysql-5.7.20 操作系统:Ubuntu-16.04.3 mysql的启动.退出.重启 # 启动 $ sudo service mysqld start # 停止 $ sud ...

  5. 自己动手实现arm函数栈帧回溯【转】

    转自:http://blog.csdn.net/dragon101788/article/details/18668505 内核版本:2.6.14 glibc版本:2.3.6 CPU平台:arm gl ...

  6. linux 设备树【转】

    转自:http://blog.csdn.net/chenqianleo/article/details/77779439 [-] linux 设备树 为什么要使用设备树Device Tree 设备树的 ...

  7. python基础===Windows环境下使用pip install 安装出错"Cannot unpack file"解决办法

    不知道为什么,加了豆瓣镜像源还是不行 这个命令可以解决! pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douba ...

  8. 浅谈linux的死锁检测 【转】

    转自:http://www.blog.chinaunix.net/uid-25942458-id-3823545.html 死锁:就是多个进程(≥2)因为争夺资源而相互等待的一种现象,若无外力推动,将 ...

  9. spring mvc3+默认首页设置问题

    web.xml配置: <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-patte ...

  10. Activiti如何替换已部署流程图

    首先交代下背景:我们有一个已经上线的activiti工作流系统,对于流程图的操作已经封装好部署,查看,删除的接口.此时客户提出要修改个别流程图里的节点名称. 我的第一个想法就是本地修改流程图bpmn文 ...