Sunteți pe pagina 1din 6

The travelling ant

There is an Ant that lives in Baskerville and loves to travel. As Baskerville is


a small place, it consists of only 5 cities placed one next to each other.

There is a train between each successive cities ie between City 1 - City 2,


City 2 - City 3 ... City 5 - City 1. Note that our Ant loves to travel and gets
happy after making exactly N train trips and returning back to home.
Ant lives in the city 1 from where she begins her journey. She asks you to
find the number of ways she can make N train trips and come back to home.

Since the number of ways can be huge, print that number modulo 10^9 + 7.

Input
First line contains T, the number of test cases.
Then T lines follow.
Each line contains a single integer n, representing the number of train trips
the ant needs to make.

Output
For each test case, print a single line containing the answer to the problem.

Constraints
1 <= T <= 1000
0 <= n <= 10^18
Explanation

In first case, ant has to make 0 trips. So the ant stays at city 1 and has only 1
option.
In second case, ant has to make 3 trips. No matter what combination we try,
we can never reach back to city 1 back after 3 trips. So answer is 0.
In third case, ant makes 4 trips. There are 6 ways in which it can reach back
to city 1.
Way 1: 1-->2-->1-->2-->1
Way 2: 1-->2-->3-->2-->1
Way 3: 1-->5-->1-->5-->1
Way 4: 1-->5-->4-->5-->1
Way 5: 1-->5-->1-->2-->1
Way 6: 1-->2-->1-->5-->1

Time Limit: 1.0 sec(s) for each input file.


Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded if any testcase passes.
Allowed Languages: C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy,
Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin,
Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP,
Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Visual Basic

Solution:

1. import java.io.PrintWriter;
2. import java.io.InputStream;
3. import java.io.IOException;
4. import java.util.Hashtable;
5. import java.util.Arrays;
6. class TestClass
7. {
8. static int mod = (int)1e9+7;
9. public static void main(String args[])throws IOException
10. {
11. InputReader reader = new InputReader(System.in);
12. PrintWriter ob=new PrintWriter(System.out);
13. int t=reader.readInt();
14. while(t-->0)
15. {
16. long n = reader.readLong();
17. ob.println(myformula(n));
18. }
19. ob.close();
20. }
21. static long myformula(long n) {
22. if(n==1) return 0;
23. if(n==0) return 1;
24. return (2*1L*formula(n-2))%mod;
25. }
26.
27. static long formula(long n) {
28. int c = -1;
29. if(n%2==0) c=1;
30. return ((powmod(2L,n+1,mod) + mod +
c*1L*lucas(n+2))%mod)*inverse(5)%mod;
31. }
32. public static long powmod(long a, long k, int m) {
33. if ( k == 0 ) {
34. return 1;
35. }
36. long t = powmod(a, k/2, m);
37. t = (t * t) % m;
38. if (k % 2 == 1) {
39. t = (t * a) % m;
40. }
41. return t;
42. }
43. static long lucas(long n) {
44. if(n==0)
45. return 2;
46. return fibonacci(2*n)*inverse(fibonacci(n))%mod;
47. }
48. public static long inverse(long a) {
49. return powmod(a, (long)(mod-2) , mod);
50. }
51. static long fibonacci(long n)
52. {
53. long F[][] = new long[][]{{1,1},{1,0}};
54. if (n == 0)
55. return 0;
56. powFib(F, n-1);
57. return F[0][0];
58. }
59.
60. static void multiply(long F[][], long M[][])
61. {
62. long a = (F[0][0]%mod*M[0][0]%mod +
F[0][1]%mod*M[1][0]%mod)%mod;
63. long b = (F[0][0]%mod*M[0][1]%mod +
F[0][1]%mod*M[1][1]%mod)%mod;
64. long c = (F[1][0]%mod*M[0][0]%mod +
F[1][1]%mod*M[1][0]%mod)%mod;
65. long d = (F[1][0]%mod*M[0][1]%mod +
F[1][1]%mod*M[1][1]%mod)%mod;
66.
67. F[0][0] = a;
68. F[0][1] = b;
69. F[1][0] = c;
70. F[1][1] = d;
71. }
72.
73. static void powFib(long F[][], long n)
74. {
75. if( n == 0 || n == 1)
76. return;
77. long M[][] = new long[][]{{1,1},{1,0}};
78.
79. powFib(F, n/2);
80. multiply(F, F);
81.
82. if (n%2 != 0)
83. multiply(F, M);
84. }
85.
86. static final class InputReader {
87. private final InputStream stream;
88. private final byte[] buf = new byte[1024];
89. private int curChar;
90. private int numChars;
91.
92. public InputReader(InputStream stream) {
93. this.stream = stream;
94. }
95.
96. private int read() throws IOException {
97. if (curChar >= numChars) {
98. curChar = 0;
99. numChars = stream.read(buf);
100. if (numChars <= 0) {
101. return -1;
102. }
103. }
104. return buf[curChar++];
105. }
106.
107. public final int readInt() throws IOException {
108. return (int)readLong();
109. }
110.
111. public final long readLong() throws IOException {
112. int c = read();
113. while (isSpaceChar(c)) {
114. c = read();
115. if (c == -1) throw new IOException();
116. }
117. boolean negative = false;
118. if (c == '-') {
119. negative = true;
120. c = read();
121. }
122. long res = 0;
123. do {
124. res *= 10;
125. res += c - '0';
126. c = read();
127. } while (!isSpaceChar(c));
128. return negative ? -res : res;
129. }
130.
131. public final int[] readIntArray(int size) throws IOException {
132. int[] array = new int[size];
133. for (int i=0; i<size; i++) {
134. array[i] = readInt();
135. }
136. return array;
137. }
138.
139. public final long[] readLongArray(int size) throws IOException {
140. long[] array = new long[size];
141. for (int i=0; i<size; i++) {
142. array[i] = readLong();
143. }
144. return array;
145. }
146.
147. public final String readString() throws IOException {
148. int c = read();
149. while (isSpaceChar(c)) {
150. c = read();
151. }
152. StringBuilder res = new StringBuilder();
153. do {
154. res.append((char)c);
155. c = read();
156. } while (!isSpaceChar(c));
157. return res.toString();
158. }
159.
160. private boolean isSpaceChar(int c) {
161. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
162. }
163. }
164.
165. }

S-ar putea să vă placă și