【高精度】【找规律】Gym - 101243B - Hanoi tower
题意:给你一个经典的汉诺塔递归程序,问你最少几步使得三个柱子上的盘子数量相同。(保证最开始盘子数量可以被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的更多相关文章
- 递推+高精度+找规律 UVA 10254 The Priest Mathematician
题目传送门 /* 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子 ...
- bzoj 1002 [FJOI2007]轮状病毒 高精度&&找规律&&基尔霍夫矩阵
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2234 Solved: 1227[Submit][Statu ...
- HDU 5351——MZL's Border——————【高精度+找规律】
MZL's Border Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- Codeforces Gym 100114 A. Hanoi tower 找规律
A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...
- 【洛谷】2144:[FJOI2007]轮状病毒【高精度】【数学推导??(找规律)】
P2144 [FJOI2007]轮状病毒 题目描述 轮状病毒有很多变种.许多轮状病毒都是由一个轮状基产生.一个n轮状基由圆环上n个不同的基原子和圆心的一个核原子构成.2个原子之间的边表示这2个原子之间 ...
- [FJOI2007]轮状病毒 题解(dp(找规律)+高精度)
[FJOI2007]轮状病毒 题解(dp(找规律)+高精度) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1335733 没什么好说的,直接把规律找出来,有 ...
- codeforces Gym 100418D BOPC 打表找规律,求逆元
BOPCTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- 打表找规律C - Insertion Sort Gym - 101955C
题目链接:https://cn.vjudge.net/contest/273377#problem/C 给你 n,m,k. 这个题的意思是给你n个数,在对前m项的基础上排序的情况下,问你满足递增子序列 ...
- bzoj1002 [FJOI2007]轮状病毒——找规律+高精度
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1002 打表找规律,似乎是这样:https://blog.csdn.net/fzhvampir ...
随机推荐
- [Unity]模拟雨水的折射效果
用GrabPass做的小玩具. 并不是真的计算了折射,只是简单地扰动了uv,对于雨水来说效果已经足够好了. Shader代码: Shader "Unlit/Rain" { Prop ...
- HDU 2639 Bone Collector II (dp)
题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...
- A题 hdu 1235 统计同成绩学生人数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1235 统计同成绩学生人数 Time Limit: 2000/1000 MS (Java/Others) ...
- Ubuntu 14.04开启ssh服务
sudo apt-get install openssh-server sudo apt-get install openssh-client sudo service ssh restart
- Linux 入门记录:八、Linux 文件系统
一.文件系统 操作系统通过文件系统管理文件及数据,磁盘或分区需要创建文件系统之后,才能被操作系统所用,创建文件系统的过程又称之为格式化.没有文件系统的设备又称之为裸设备(raw),某些环境会需要裸设备 ...
- python安装基础
. python安装 //先查看是否存在python的包,如果没有,那可以用yum或去python的官网安装 [root@localhost ~]# rpm -qa|grep python pytho ...
- [ python ] 下划线的意义和一些特殊方法
Python用下划线 Python用下划线为变量前缀和后缀制定特殊变量 _xxx 不能用 'from module import *' 导入__xxx__ 系统定义名字__xxx 类中的私有变量名 核 ...
- django给视图添加缓存功能
在开发过程中,有些视图只是查询数据,而且查询的数据一般不会变化.例如,做地址模块时,省市县都是不会变的.如果用户每次请求地址视图时,都要执行视图返回数据,会给服务端带来不必要的压力.这时候可以用到缓存 ...
- 禅道BUG管理工具使用链接存储
http://www.zentao.net/book/zentaopmshelp/259.html
- LeetCode解题报告—— Bus Routes
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...