Tencent programming marathon's fifth round-first question (Wei cat series story-eating chicken legs)

Source: Internet
Author: User

Problem description Wei cat is not a common cat. Ordinary cats like to eat fish, but Wei cat prefers to eat chicken legs most. 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. Could you tell me that he will stop growing in a few days? If he will never stop growing, then he will output "inf ". (No output in quotation marks)


The first line of 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, K2, 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)


Output for 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.
For details, see sample output.


Sample Input

25 1 1 101 1 1 1 15 1 1 5001 1 1 1 1
 


Sample output

Case #1: 2Case #2: 7

AC code:

#include <stdio.h>#include <string.h>#include <stdlib.h>int main(){int testcase,i,n,count,ans;double k,sum,k1,k2,temp;scanf("%d",&testcase);for(count=1; count<=testcase; count++){ans = 0;sum = 0;scanf("%d %lf %lf %lf",&n,&k1,&k2,&k);for(i=0;i<n;i++){scanf("%lf",&temp);sum = sum + temp;}if(sum > k)printf("Case #%d: 0\n",count);else if(k1+k2>1 || k1+k2<-1){while(sum<=k){sum = sum * ( k1 + k2 );ans++;}printf("Case #%d: %d\n",count,ans);}else{printf("Case #%d: inf\n",count);}}return 0;}


Question Analysis: This question is not difficult, but the AC rate is extremely low, because there is a hidden trap in the question.

From the topic description, we can abstract the formula sum (I + 1) = (K1 + K2) * sum (I). The goal is to stop growth when sum is greater than K; if sum is never greater than K, the growth cannot be stopped. You can directly output INF.

Since-100 <= K1, K2 <= 100, K1 + K2 can be positive or negative, or 0. When k1 + K2 is equal to 0,-1, or 1, it is obviously impossible to stop growth and output INF. In other cases, you can calculate the number of iterations Based on sum (I + 1) = (K1 + K2) sum (I, the number of days after which the growth can be stopped.

Now let's take a look at the trap of this question: 64-bit integer will overflow During computation. The value range of K is 1 <= k <= 10 ^ 18, which is a 32-bit integer variable but can be expressed as a 64-bit integer. The maximum value of a signed 64-bit integer is 2 ^ 63-1, which is greater than 10.
^ 18. At first glance, there seems to be no problem. However, the formula sum (I + 1) = (K1 + K2) * sum (I) exceeds 2 ^ 63-1 during iteration, which means an overflow error occurs. The reason is that sum (I) can theoretically be equal to K. The maximum value of K1 + K2 is 200, multiplied by 200*10.
^ 18 is greater than 2 ^ 63-1, which proves that an overflow occurs in extreme cases. Therefore, an error occurs when a 64-bit integer variable is used. It must be expressed as a double integer, even if the input is an integer. When I was doing this, I had no idea where the error was. I found the problem only after the competition was over.

If you want to use a 64-bit integer variable, you need to make some changes to the program. Do not overflow or AC during multiplication. The Code is as follows:

#include <stdio.h>#include <string.h>#include <stdlib.h>int main(){int testcase,i,n,count,ans;__int64 k,sum,k1,k2,temp;scanf("%d",&testcase);for(count=1; count<=testcase; count++){ans = 1;sum = 0;scanf("%d %I64d %I64d %I64d",&n,&k1,&k2,&k);for(i=0;i<n;i++){scanf("%I64d",&temp);sum = sum + temp;}if(sum > k)printf("Case #%d: 0\n",count);else if(k1+k2>1 || k1+k2<-1){while(1.0*(k1+k2)*sum<=1.0*k){sum = sum * ( k1 + k2 );ans++;}printf("Case #%d: %d\n",count,ans);}else{printf("Case #%d: inf\n",count);}}return 0;}

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.