Suppose we put two panes of glass back-to-back. How many ways a are there for light rays to pass through or is reflected after changing direction n times? Following figure shows the situations when the value of n is 0, 1 and 2.
Input
It is a set of lines with an integer n where 0≤n≤1000 in each of them.
Output
For every one of the integers a line containing a as described above.
Sample Input
0
1
2
Sample Output
1
2
3
Title Link: UVA10334 Ray through glasses
Question brief: (slightly)
Problem Analysis:
The problem is actually the Fibonacci sequence, but the starting item values are different. The recurrence type is as follows:
F0=0
f1=2
......
Fn=fn-2 + fn-1 (n>=2)
The large number calculation is realized by the simulation method, see the program.
Program Description:
It is most convenient to use Java program to complete the calculation of large numbers.
Preface: (slightly)
Reference link: (slightly)
The C + + language program for AC is as follows:
AC's C + + Language Program (analog method) is as follows:
/* UVA10334 Ray through glasses
/* #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 1000;
const int N2 = 1000/17;
Const ULL BASE = 1e17;
Ull fib[n + 1][n2 + 1];
void Setfib ()
{
fib[0][0] = 1;
Fib[1][0] = 2;
int len = 1;
for (int i = 2; I <= N; i++) {
int carry = 0;
for (int j = 0; J < Len; J +) {
fib[i][j] = Fib[i-2][j] + fib[i-1][j] + carry;
carry = fib[i][j]/BASE;
FIB[I][J]%= BASE;
if (carry)
fib[i][len++] = Carry
}
}
int main ()
{
memset (fib, 0, sizeof (FIB));
Setfib ();
int n;
while (~SCANF ("%d", &n)) {
int k = N2;
while (fib[n][k] = = 0)
k--;
printf ("%lld", Fib[n][k]);
for (int i = k-1 i >= 0; i--)
printf ("%017lld", Fib[n][i));
printf ("\ n");
}
return 0;
}
The Java language Program for AC is as follows:
/* UVA10334 Ray through glasses * *
import Java.math.BigInteger;
Import Java.util.Scanner;
public class Main {public
static void Main (string[] args) {
Scanner cin = new Scanner (system.in);
biginteger[] fib = new biginteger[1001];
Fib[0] = new BigInteger ("1");
FIB[1] = new BigInteger ("2");
for (int i = 2; I <= 1000 i + +)
fib[i] = Fib[i-1].add (fib[i-2));
while (Cin.hasnext ())
{
int N = Cin.nextint ();
System.out.println (Fib[n]);}}