WA :Maximize It

c-plus-plus
i_hate_error
i_hate_errors
lunchtime

#1

Problem : https://www.codechef.com/LTIME66B/problems/MXM

My Solution : https://www.codechef.com/viewsolution/21692278

Working well with most of my Testcases ; Please help !


#2

It does not work for the following simple test case:

1
5 2
1 1 3 3 8

Your code generates 3, while it should be 4.

The code has multitude of problems, starting with

        int n,k;
        char numbers[n+1];
        int sum=0,sum1=0,prod=1,fprod=1,res=0,p;
        cin>>n>>k;
        for(int i=0;i>p;
            for(int j=0;j<p;j++) {
                num=num*k;
            }
            numbers*=num;
        }
  1. You define array numbers[n+1] at the moment when n is not properly initialized yet.
  2. You store values of powers of p in char which is limited by 127.

What’s the most interesting is that your code is still able to solve some of the test cases.


#3

@oleg_b Thank You ! Even ‘long long int’ won’t work for storing the values of the (integer)^(power) , Please correct me if I am wrong ?


#4

Yes, ‘long long int’ does not have sufficient capacity. The approach should be quite different - you need to represent the numbers in the base of k. For example, if k is 10, then 10^100 is a vector with 101 elements, all of them are zeros, except the last one which is 1.