UVA11161 Help My Brother (II)大数+递推

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11161 Help My Brother (II)大数+递推相关的知识,希望对你有一定的参考价值。

A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.
f ( 1 ) = 1 , f ( 2 ) = 1 , f ( n > 2 ) = f ( n − 1 ) + f ( n − 2 ) f(1) = 1, f(2) = 1, f(n > 2) = f(n − 1) + f(n − 2) f(1)=1,f(2)=1,f(n>2)=f(n1)+f(n2)
    Little Tuhin is playing with Fibonacci numbers. He took a paper and began to write non-negative integers such that every nth line contains a total of f(n) numbers. He noted the median of the lines.
    He came to me with a confused smile. He told me, “I think I have found a formula to find the median of a certain line, but I am not sure about my formula. Can you write a code for me? I will give a line number and you have to tell me the median of that line.”
His paper is noted below:
0 Median: 0
1 Median: 1
2 3 Median: 2
4 5 6 Median: 5
7 8 9 10 11 Median: 9
12 13 14 15 16 17 18 19 Median: 15
. . .
    If there are n numbers in a certain line then
M e d i a n = { ( n + 1 ) / 2 -th Element, n odd n / 2 -th Element, n even Median = \\left \\{ \\begin{array}{lr} (n + 1)/2 &\\text{-th Element, n odd}\\\\ n/2 & \\text{-th Element, n even} \\end{array} \\right. Median={(n+1)/2n/2-th Element, n odd-th Element, n even
    My computer is not working well. So, I cannot help him right now. Can anyone help me with the code?
Input
The input file contains several sets of inputs. The total number of sets will be less than 100. The description of each set is given below:
    Each set starts with one integer n (1 ≤ n ≤ 1500) which indicates the line number.
    The input will be terminated by the set where n = 0. And this set should not be processed.
Output
For each set in the input, you should first print the set number starting from 1. And the next line should be the median of the nth line. You can assume that the output will fit into 350 digits.
    See the sample input-output for more details. Output should be formatted like the sample output.
Sample Input
1
2
3
4
5
6
0
Sample Output
Set 1:
0
Set 2:
1
Set 3:
2
Set 4:
5
Set 5:
9
Set 6:
15

问题链接UVA11161 Help My Brother (II)
问题简述:(略)
问题分析:大数递推问题,用Java来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的Java语言程序如下:

/* UVA11161 Help My Brother (II) */

import java.util.Scanner;
import java.math.BigInteger;

public class Main {
	public static void main(String args[]){
		Scanner input = new Scanner(System.in);
		int N = 1500 + 1;
		int caseno = 0;
		BigInteger f[] = new BigInteger[N + 1];
		f[1] = BigInteger.ZERO;
		f[2] = BigInteger.ONE;
		for(int i = 3; i <= N; i++)
			f[i] = f[i - 2].add(f[i - 1]).add(BigInteger.ONE);
		int n = input.nextInt();
		while (n != 0) {
			BigInteger median = f[n].add(f[n+1]).subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));
			System.out.printf("Set %d:\\n", ++caseno);
			System.out.println(median);
			n = input.nextInt();
		}
	}
}

以上是关于UVA11161 Help My Brother (II)大数+递推的主要内容,如果未能解决你的问题,请参考以下文章

nyoj 282 You are my brother

NBUT 1218 You are my brother

Uva11384 Help is needed for Dexter

UVA 11440 Help Tomisu

UVa 11440 - Help Tomisu(欧拉函数 + 问题转换)

uva 11384 Help is needed for Dexter