UVA10334 Ray through glasses large number __uva10334

Source: Internet
Author: User

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]);}}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.