当前位置: 代码迷 >> 综合 >> 2020牛客国庆集训派对day2-J VIRUS OUTBREAK
  详细解决方案

2020牛客国庆集训派对day2-J VIRUS OUTBREAK

热度:97   发布时间:2024-02-24 20:03:30.0

链接:https://ac.nowcoder.com/acm/contest/7818/J
来源:牛客网

题目描述
The State Veterinary Services Department recently reported an outbreak of a newly found cow disease. All cows found to have affected by the disease have since euthanized because of the risk to the industry. Number of affected cows increased to 21, 34 and reached 55 after eight, nine and ten hours respectively.
You have been assigned by the authority to study the pattern of the outbreak and develop a program to predict the next number of affected cows so that the authorities could prepare and act accordingly.
输入描述:
Input will consist of a series of positive integer numbers no greater than 490 each on a separate line represent the hours. The input process will be terminated by a line containing -1.
输出描述:
For each input value, the output contains a line in the format: Hour X : Y cow(s) affected , where X is the hour, and Y is the total affected cows that need to be euthanized based on the hour given by X.

输入

1
4
6
11
-1

输出

Hour: 1: 1 cow(s) affected
Hour: 4: 3 cow(s) affected
Hour: 6: 8 cow(s) affected
Hour: 11: 89 cow(s) affected

题解:直接看样例就不难看出是求斐波那契数列,再一看数据范围,是大数的斐波那契数列,像这种类型的题目两种情况:
1.当n非常大,一般会利用矩阵快速幂,然后对1e+7取余,
2.就像这题,利用大数字类求解

python写法:

f = [0] * 500
f[0], f[1] = 0, 1
for i in range(495):if i < 2:continuef[i] = f[i-1] + f[i-2]while True:x = int(input())if x == -1:breakprint("Hour: " + str(x) +": " + str(f[x]) + " cow(s) affected")

java写法:

import java.math.BigInteger;
import java.util.Scanner;public class Main {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);int n = 1;int a = 0,b = 1,c = 1;BigInteger f[] = new BigInteger [491];f[1] = f[2] = new BigInteger("1");for(int i = 3; i <= 490; i++){
    f[i] = f[i-1].add(f[i-2]);}while(true){
    int h = in.nextInt();if(h == -1){
    break;}System.out.println("Hour: " +h + ": "+ f[h].toString()+ " cow(s) affected");}}
}