zoj1633 Big String dfs
2017-07-11 13:36
323 查看
Big String
Time Limit: 2 Seconds Memory Limit: 65536 KB
We will construct an infinitely long string from two short strings: A = "^__^" (four characters), and B = "T.T" (three characters). Repeat the following steps:
Concatenate A after B to obtain a new string C. For example, if A = "^__^" and B = "T.T", then C = BA = "T.T^__^".
Let A = B, B = C -- as the example above A = "T.T", B = "T.T^__^".
Your task is to find out the n-th character of this infinite string.
Input
The input contains multiple test cases, each contains only one integer N (1 <= N <= 2^63 - 1). Proceed to the end of file.
Output
For each test case, print one character on each line, which is the N-th (index begins with 1) character of this infinite string.
Sample Input
Sample Output
Time Limit: 2 Seconds Memory Limit: 65536 KB
We will construct an infinitely long string from two short strings: A = "^__^" (four characters), and B = "T.T" (three characters). Repeat the following steps:
Concatenate A after B to obtain a new string C. For example, if A = "^__^" and B = "T.T", then C = BA = "T.T^__^".
Let A = B, B = C -- as the example above A = "T.T", B = "T.T^__^".
Your task is to find out the n-th character of this infinite string.
Input
The input contains multiple test cases, each contains only one integer N (1 <= N <= 2^63 - 1). Proceed to the end of file.
Output
For each test case, print one character on each line, which is the N-th (index begins with 1) character of this infinite string.
Sample Input
1 2 4 8
Sample Output
T . ^ T
import java.io.BufferedInputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { new Task().solve(); } } class Task { Scanner in = new Scanner(new BufferedInputStream(System.in)) ; PrintWriter out = new PrintWriter(System.out); final int N = 90 ; BigInteger[] fibo = new BigInteger[N+1] ; char[] res = {' ' , 'T' , '.' , 'T' , '^' , '_' , '_' , '^'} ; void dfs(BigInteger n){ if(n.compareTo(BigInteger.valueOf(7)) <= 0){ out.println(res[n.intValue()]) ; return ; } int i = 1 ; for(; i <= N ; i++){ if(fibo[i].compareTo(n) >= 0){ break ; } } dfs(n.subtract(fibo[i-1])) ; } void solve() { fibo[1] = BigInteger.valueOf(4) ; fibo[2] = BigInteger.valueOf(3) ; for(int i = 3 ; i <= N ; i++){ fibo[i] = fibo[i-1].add(fibo[i-2]) ; } while(in.hasNext()){ dfs(in.nextBigInteger()) ;//out.flush(); } out.flush(); } }
相关文章推荐
- ZOJ&nbsp;1633&nbsp;big&nbsp;string
- ZOJ 2571 Big String Outspread
- zoj 2571 Big String Outspread
- ZOJ 2571 Big String Outspread
- ZOJ-2571 Big String Outspread 模拟
- ZOJ Problem Set - 1002(DFS)
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String
- ZOJ 2110 Tempter of the Bone (DFS)
- ZOJ 1047 Image Perimeters(dfs+技巧)
- ZOJ 3228 Searching the String
- ZOJ Problem Set - 3861 ( DFS + 子集生成 + 有条件的全排列生成 )
- HDU 1010 Tempter of the Bone &&ZOJ 2110【DFS】
- ZOJ 3228 Searching the String
- zoj 2110 Tempter of the Bone (dfs)
- zoj 3811 条件下判定某遍历序列的合法性/ 一遍 dfs
- ZOJ 3761 Easy billiards[dfs]
- zoj 2100 dfs暴力宽搜
- ZOJ 2734 Exchange Cards(DFS)
- [DFS]FJSDFZOJ 1427 寻找国都名
- ZOJ 3644 Kitty's Game dfs,记忆化搜索,map映射 难度:2