[Tencent second Campus Programming marathon] HDU-4525, Wei cat series of stories-eat chicken legs

Source: Internet
Author: User

Original question Portal: Wei cat series story-eating chicken legs

The question is as follows (some traps are added Bold and underlineDisplay) Problem descriptionWei is not an ordinary cat. Ordinary cats like to eat fish, but Wei is the favorite to eat chicken legs. He keeps eating and eating every day. He eats one chicken leg after another. Now he has encountered a problem. If his weight is too fat, his master will not give him chicken legs, so he needs your help.
Wei's body consists of N organs. His body is very special, so his growth is also very special (Don't ask why, it's strange enough to eat chicken legs ). His growth has a factor of K1 and K2, and the daily growth is related to the previous day. We assume that the numbers of the N organs on day I are a (I, 1 ), A (I, 2), a (I, 3 )...... A (I, n:
A (I + 1, 1) = K1 * a (I, 1) + K2 * a (I, 2)
A (I + 1, 2) = K1 * a (I, 2) + K2 * a (I, 3)
......
A (I + 1, n) = K1 * a (I, n) + K2 * a (I, 1)
Wei's weight is equal to the sum of the values of all his organs, and he also has a special function, that is, he will automatically detect his weight, if his weight is larger than K, then the growth will automatically stop (in order to eat chicken legs every day), because of the Special Body Structure of Weiwei cat, his weight may become negative.
Now I will give you the initial values of N organs and their growth Coefficients K1 and K2. May I ask him? Several days laterWill stop growing ( If the initial weight is greater than K, the growth stops after 0 days.), If he can never stop growth, then output "inf ". (No output in quotation marks) InputThe first line of the input data is a positive integer T, indicating that there are T groups of test data;
The first row of each group of data contains four numbers: N, K1, K2, and K. It indicates that Wei cat has n organs, and its growth coefficient is K1 and K2 ( It may be decimal.When the weight exceeds K, he stops growing.
The next line contains n ai numbers, which indicates the number of the first day of each organ in the Weiwei cat.

[Technical Specification]
T <= 100
1 <= n <= 10000
-100 <= K1, K2 <= 100
1 <= k <= 10 ^ 18
1 <= AI <= 1000 (1 <= I <= N)

 

OutputFor each group of test data, first output "case # X:", X indicates the number of the test case, and then output a number ans, which means that it will stop growing after ans days, output INF if it does not stop
For details, see sample output.

 

Sample Input2
5 1 1 10
1 1 1 1
5 1 500
1 1 1 1

 

Sample outputCase #1: 2 case #2: 7 Question Analysis:Add all Recursive formulas (left-side addition and right-side addition) in the question, and get a recursive formula for weight growth: WI + 1 = (K1 + K2) WI, it can be seen that if the initial weight is not greater than K, (K1 + K2) is the cause of weight gain or reduction. When | k1 + K2 | <= 1, the weight will never exceed the given K value. The Code is as follows:
 1 package org.contests; 2  3 import java.util.Scanner; 4  5 public class ChickenLegs { 6     public static void main(String[] args) { 7         Scanner scan = new Scanner(System.in); 8         int totalGroupNum = scan.nextInt(); 9         for(int ti = 0; ti < totalGroupNum; ti++){10             int rowNum = scan.nextInt();11             double k1 = scan.nextDouble();12             double k2 = scan.nextDouble();13             long k = scan.nextLong();14             int dayNum = 1;15             boolean isInf = false;16             17             double totalWeight = 0;18             19             for(int ri = 0; ri < rowNum; ri++){20                 totalWeight += scan.nextInt();21             }22             23             while(totalWeight <= k){24                 25                 if(Math.abs(k1 + k2) <= 1){// | k1 + k2 | <= 126                     isInf = true;27                     break;28                 }29                 30                 totalWeight = (k1 + k2) * totalWeight;31                 dayNum++;32             }33             34             if(isInf){35                 System.out.println("Case #"+(ti+1)+": inf");36             }else{37                 System.out.println("Case #"+(ti+1)+": "+(dayNum-1));38             }39         }40     }41 }

 

 

During HDU submission, the Java code does not require a package statement. You need to change the Class Name of the public class to main. The following code submits the AC code in HDU:

 1 import java.util.Scanner; 2  3 public class Main{ 4     public static void main(String[] args) { 5         Scanner scan = new Scanner(System.in); 6         int totalGroupNum = scan.nextInt(); 7         for(int ti = 0; ti < totalGroupNum; ti++){ 8             int rowNum = scan.nextInt(); 9             double k1 = scan.nextDouble();10             double k2 = scan.nextDouble();11             long k = scan.nextLong();12             int dayNum = 1;13             boolean isInf = false;14             15             double totalWeight = 0;16             17             for(int ri = 0; ri < rowNum; ri++){18                 totalWeight += scan.nextInt();19             }20             21             while(totalWeight <= k){22                 23                 if(Math.abs(k1 + k2) <= 1){// | k1 + k2 | <= 124                     isInf = true;25                     break;26                 }27                 28                 totalWeight = (k1 + k2) * totalWeight;29                 dayNum++;30             }31             32             if(isInf){33                 System.out.println("Case #"+(ti+1)+": inf");34             }else{35                 System.out.println("Case #"+(ti+1)+": "+(dayNum-1));36             }37         }38     }39 }

Tucao:

Due to some traps in learning skills and questions, I have completed this question within the specified time, but it is still far away ~~

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.