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 ...
随机推荐
- php修改密码
为了让页面更为好看一些,我一般会选择bootstrap,写起来虽然看着麻烦,但是我们真正需要的只有中间的内容 下面是html的内容 <div id="tbx"" ...
- 【BZOJ3784】树上的路径 点分治序+ST表
[BZOJ3784]树上的路径 Description 给定一个N个结点的树,结点用正整数1..N编号.每条边有一个正整数权值.用d(a,b)表示从结点a到结点b路边上经过边的权值.其中要求a< ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Vue学习-基础语法
Vue v-if指令 Vue.js的指令是以v-开头的,它们作用于HTML元素,指令提供了一些特殊的特性,将指令绑定在元素上时,指令会为绑定的目标元素添加一些特殊的行为,我们可以将指令看作特殊的HTM ...
- JVM相关的几个基本概念
1.虚拟机是指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统 2.常见的虚拟机有VMWare,Visual Box,JVM等 VMWare或者Visual Box模拟的 ...
- Symfony3 查询搜索功能DQL语句like查询
//前台页面代码 <form method="GET" action="{{ path('staff_index') }}" name="sea ...
- 忘记apple id如何更新应用?
最近ytkah的app有很多更新提示,之前注册的apple id好久没登录了,突然提示说登录需要验证安全问题,哪还记得噢,最要命的是邮箱收到的加密邮件也需要验证.重新注册一个吧,这次要注意保存相关信息 ...
- input type="radio" jquery判断checked的三种方法:
<input type="radio" name="radioname" value="" />全部 <input typ ...
- 第11条:用zip函数同时遍历两个迭代器
核心知识点: (1)内置的zip函数可以平行地遍历多个迭代器. (2)python3中地zip相当于生成器,会在遍历过程中逐次产生元祖.而python2中地zip则是直接把这些元祖完全生成好,并一次性 ...
- swift ! 和 ? 的学习
刚开始学 这两个符号 不确定到底是否是可以互相替代 用的都混淆了 今天好好做个总结 如果声明一个变量 如下 var name:String? //只声明 没做初始化赋值 说明 当前name 是 n ...