ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)
Description
A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, after the first time step, the sequence 0 1 is obtained; after the second, the sequence 1 0 0 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on.
How many pairs of consequitive zeroes will appear in the sequence after n steps?
Input
Every input line contains one natural number n (0 < n ≤1000).
Output
For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.
Sample Input
2
3
Sample Output
1
1
题目大意就是有0-> 10, 1->01这两种变换,起始状态是1,让求某次变换后连续两个0的个数,此处有且仅两个0连续。
首先枚举前几项的话
0:1
1:01
2:1001
3:01101001
4:1001011001101001
5:01101001100101101001011001101001
……
发现两个个规律,
1:
所有项前一半和后一半互反,此外,偶数项对称。
想来也是,只要满足某个前一项前一半和后一半互反,0->10,1->01后,自然造成后一项同样互反的形式。
同样的前一个偶数项的对称会导致后面奇数项的前一半和后一半都对称,自然导致后一个偶数项对称。
此处严谨证明应由数学归纳法。
2:
后一项等于前一项取反拼上前一项。
发现奇数项由于前后互反,在交接处会形成01,那么下一项在对称情况下,交界处必形成1001。所以偶数项的0对会多生成一个。
有了这些就知道了两项间的关系。
于是由2得:
zero[i] = zero[i-1]+one[i-1]+(i-1)%2;
one[i] = zero[i-1]+one[i-1];
由于递推式看起来增长速度就和费波拉契有的一比,估计需要大数。实测确实需要。
代码:
import java.math.BigInteger;
import java.util.Scanner; public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
BigInteger one[] = new BigInteger[];
BigInteger zero[] = new BigInteger[];
one[] = new BigInteger("");
one[] = new BigInteger("");
zero[] = new BigInteger("");
zero[] = new BigInteger("");
for (int i = ; i <= ; ++i)
{
zero[i] = zero[i-].add(one[i-]).add(new BigInteger(Integer.toString((i-)%)));
one[i] = zero[i-].add(one[i-]);
}
int n;
while (input.hasNext())
{
n = input.nextInt();
System.out.println(zero[n]);
}
}
}
ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)的更多相关文章
- ACM学习历程—HDU5396 Expression(递推 && 计数)
Problem Description Teacher Mai has n numbers a1,a2,⋯,an and n−1 operators("+", "-&qu ...
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)
Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...
- ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)
Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...
- ACM学习历程—HDU 5326 Work(树形递推)
Problem Description It’s an interesting experience to move from ICPC to work, end my college life an ...
- ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)
Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N- ...
- ACM学习历程—NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推)
Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
随机推荐
- poj2485
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27912 Accepted: 12734 Descri ...
- python基础-第五篇-5.1冒泡排序
几个月过去了,小白逐渐对公司的后端服务熟悉了,不过这天小白又接到一封神秘邮件,是景女神发来的:公司急需一批对语言算法有些了解的优秀员工,鉴于你在公司的表现很不错,现在给到你一个培训机会,请速到开发部报 ...
- Linux项目部署发布
Linux项目部署发布 1.部署环境准备,准备python3和虚拟环境解释器,virtualenvwrapper pip3 install -i https://pypi.douban.com/sim ...
- 几款Java常用基础工具库
通用工具类(字符串.时间格式化.BeanUtils.IO) 1. commons-lang3库 1.1. org.apache.commons.lang3.StringUtils类 日常代码中,我们经 ...
- 【windows】远程桌面报错:由于CredSSP加密Oracle修正
对于windows家庭版用户,无法打开gepdit.msc需要手动修改注册表 创建红色部分目录 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curre ...
- QT里面的delay使用
void delay() { QTime dieTime= QTime::currentTime().addSecs(1); while( QTime::currentTime() < dieT ...
- HDU - 3081 Marriage Match II 【二分匹配】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意 有n对男女 女生去选男朋友 如果女生从来没和那个男生吵架 那么那个男生就可以当她男朋友 女 ...
- java搭建 SpringMVC+Mybatis(SMM)+mybatis-generate
搭建SSM系统,首先要了解整个过程: 1.创建spring-mvc项目 2.在maven中添加要引用的jar包(使用框架都是较新的版本:) 3. jdbc.xml +spring-mybatis.xm ...
- python cookbook 迭代器与生成器
代理迭代 a = [1, 2, 3] for i in iter(a): print(i) for i in a.__iter__(): print(i) 这里的两个方法是一样的,调用iter()其实 ...
- [转]详解Java解析XML的四种方法
XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这里面包括的内容有DOM ...