Sicily 1166 Computer Transformat
来源:互联网 发布:北龙中网域名认证 编辑:程序博客网 时间:2024/06/05 19:48
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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
23
Sample Output
11
Solution
题目的意思是找出有多少个连续的0,就是找规律看看。
n=1 ---------> 01 --------> 0
n=2 ---------> 1001 --------> 1
n=3 ---------> 01101001 --------> 1
n=4 ---------> 1001011001101001 --------> 3
n=5 ---------> 01101001100101101001011001101001 --------> 5
算着的时候,我就发现0*2+1 = 1, 1*2-1 = 1, 1*2+1 = 3, 3*2-1 = 5,蛮符合的,就推论n=6的时候为5*2+1 = 11,发现没错。
于是就得出了f(n) = f(n-1)*2 + (-1)^n,注意n有1000之大,所以要用高精度乘法,每次乘最多进一位,于是位数最多也就1000,敲出来就A啦
(PS:其实加一减一是看一下前一个序列首尾是否相同的时候,列表并综合结果发现的)
#include <iostream>#include <cstring>using namespace std;int ans[1005][1005];//高精度乘法的结果数组int main(){ int i, j, n; memset(ans, 0, sizeof(ans)); ans[1][0] = 0; for (int i = 2; i < 1005; ++i)//简单粗暴的乘法,可以优化的 { for (j = 0; j < 1005; ++j) ans[i][j] = ans[i-1][j] * 2; if (i%2) ans[i][0] -= 1;//处理加一减一 else ans[i][0] += 1; for (j = 0; j < 1005; ++j) if (ans[i][j] > 9) { ans[i][j+1] += ans[i][j] / 10; ans[i][j] %= 10; } } while (cin >> n) { i = 1004; while (ans[n][i] == 0 && i > 0) --i;//输出0的时候的控制 while (i >= 0) cout << ans[n][i--]; cout << endl; } return 0;}
- Sicily 1166 Computer Transformat
- sicily 1166. Computer Transformat
- 1166. Computer Transformat
- sicily1166. Computer Transformat
- Sicily 1282. Computer Game
- sicily--1282. Computer Game
- Sicily 1282 Computer Game
- Sicily 1717. Computer
- sicily 1282 Computer Game
- #sicily#1082. Computer Game
- computer
- computer
- computer
- computer
- computer
- Apache Commons Collections基本操作(Predicate、Transformat、Closure等)
- Computer and Computer Science
- Computer Glossary
- spoj375Query on a tree
- Linux GCC常用命令
- C++学习 STL之六:map/multimap用法详解
- C++ 重载赋值操作符
- the info.plist in the package must contain the CFBundleShortVersinString key
- Sicily 1166 Computer Transformat
- 新架构下的html代码
- 两个字符串中最大的公共字串
- java反射机制
- C++学习 STL之七:STL各种容器的使用时机详解
- rest API 测试注意事项
- 90%的android开发者都会遇到的问题,当sjk_daemon遇见ADB server didn't ACK
- C++学习 STL之一:字符串用法详解
- Memcached--JAVA接口使用说明