-1

Details of the purpose of writing the program is given on the link : https://www.codechef.com/problems/COOKMACH/

And the error

Time limit exceeded

      Sub-Task  Task   #       Score    Result (time)

         1        0    NA       AC       (0.000000)

         1        1    NA       TLE      (1.010000)

         1        2    NA       TLE      (1.010000)

         1        3    NA       TLE      (1.010000) 



        Final Score -> 0.000000 Result - TLE 

         2        4    NA        TLE      (1.010000)

         2        5    NA        TLE      (1.010000)

         2        6    NA        TLE      (1.010000) 

         7      NA     WA                 (0.000000)

Final Score - 0.000000 Result - TLE

the code is

#include <stdio.h>
int main(void)
{
    int test, set, des, a = 1, ctr = 0, str = 0, x;
    scanf("%d", &test);
    if (test > 0 && test <= 200)
    {
        for (x = 0; x < test; x++)
        {
            ctr = 0;
            scanf("%d", &set);
            scanf(" %d\n", &des);
            if ((set > 0 && set <= 10000000) && (des > 0 && des <= 10000000))
            {
                if (set <= 100 && des <= 100)
                {
                    if (set == des)
                        ctr = 0;
                    if (set == 1)
                    {
                        while (set != des)
                        {
                            set = set * 2;
                            ctr++;
                        }
                    }
                    else if (set != 1)
                    {
                        if (des % 2 == 0)
                        {
                            while (a < des)
                            {
                                a = a * 2;
                                str++;
                            }
                        }

                        if (a == des || des == 1)
                        {
                            if (set < des)
                            {
                                if (set % 2 == 0)
                                {
                                    while (set != des)
                                    {
                                        set = set * 2;
                                        ctr++;

                                    }
                                }
                                else if (set % 2 == 1)
                                {
                                    set = (set - 1) / 2;
                                    ctr++;
                                    while (set != des)
                                    {
                                        set = set * 2;
                                        ctr++;
                                    }
                                }
                            }
                            if (set > des)
                            {

                                if (set % 2 == 0)
                                {

                                    while (set != des)
                                    {
                                        set = set / 2;
                                        ctr++;

                                    }
                                }
                                else if (set % 2 == 1)
                                {
                                    set = (set - 1) / 2;
                                    ctr++;
                                    while (set != des)
                                    {
                                        set = set / 2;
                                        ctr++;
                                    }
                                }
                            }
                        }
                    }
                }
                printf("%d\n", ctr);
            }
        }
        return 0;
    }
}
soumya
  • 3,801
  • 9
  • 35
  • 69
Purvi Sampat
  • 21
  • 1
  • 9
  • 3
    "How to overcome it?" -- Code something faster. (Sorry. That answer is about as helpful as the question, I know.) Debugging 101: Code, input, expected output, observed output. It would be nice if some sense could be made of this question without going through the external link. Comments would also not be amiss. – DevSolar Sep 08 '15 at 11:38
  • Several of those `if`s can be removed as it is stated that "_1 ≤ T ≤ 200 , 1 ≤ A ≤ 107 , 1 ≤ B ≤ 107_". Also, `scanf(" %d\n",&des);` --> `scanf("%d",&des);` – Spikatrix Sep 08 '15 at 11:43
  • Hey! Stupid idea here: can you try to use scanf("%d\n", &test); for the number of input line. Maybe your program blocks waiting for a number when it gets the '\n' character. I think @CoolGuy is right: you don't have to check the constraints they're more like a contract on the input to help you. – A wild elephant Sep 08 '15 at 11:47
  • 1
    @Awildelephant `%d` won't scan `\n`. It skips them. – Spikatrix Sep 08 '15 at 11:48
  • @CoolGuy I thought it would keep it in the buffer, thus making any following try to match a number fail. – A wild elephant Sep 08 '15 at 11:50
  • 1
    @Awildelephant It stays in the `stdin`. `%d` , skips leading whitespace characters. As `\n` is a whitespace character, it will read and discard it and will wait for a number. Try `int num; scanf("%d", &num); printf("num=%d\n", num); scanf("%d, &num); printf("num=%d\n", num);` and give the input, say, `2\n3\n`. You'll find that 2 and 3 gets printed and not 2 and `\n`. – Spikatrix Sep 08 '15 at 11:53
  • @CoolGuy I tried your changes time limit is still exceeded. – Purvi Sampat Sep 08 '15 at 11:59
  • @DevSolar Do you want me to add the question here , in the comments or in the question. – Purvi Sampat Sep 08 '15 at 12:00
  • 2
    Edit your question. The idea is that it should be short, but contain all info necessary to answer it. For the same reason, link-only answers are discouraged: Both question and answer should be helpful to future visitors. Hence, your question should probably not be about "how do I pass exercise X", but more specific. "How do I find out where my program wastes time", or "how to improve this algorithm". Ideally be even more specific like that, telling us what exactly you have already tried and what *exactly* you need help with. (**We** don't care for that exercise, it's *your* learning process.) – DevSolar Sep 08 '15 at 12:08
  • 1
    thanks @DevSolar I will surely keep in mind and edit the question asap. :) – Purvi Sampat Sep 08 '15 at 12:42

3 Answers3

0

Focus more on bitwise operators like bit shifting and bitwise swap of two integers. My solution for this looks like

#define swap(a, b) (a ^= b, b ^= a, a ^= b)

int main() {
    long a, b;
    int count, T;
    scanf("%d", &T);
    while (T--) {
        scanf("%ld%ld", &a, &b);
        for (count = 0; a & (a - 1); a >>= 1)
            ++count;
        if (a > b)
            swap(a, b);
        for (; a < b; a <<= 1)
            ++count;
        printf("%d\n", count);
    }
    return 0;
}
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • please could you explain bit shifting and bitwise swap of the integers as you see I am a complete beginner. Kindly help – Purvi Sampat Sep 08 '15 at 12:40
  • @PurviSampat Why not read it yourself http://www.cprogramming.com/tutorial/bitwise_operators.html and https://graphics.stanford.edu/~seander/bithacks.html – Shreevardhan Sep 08 '15 at 12:43
  • 1
    @PurviSampat: Multiplications, divisions and modulo are expensive operations. A bit shift to the left is the same as `*= 2`. A shift to the right is the same as `/= 2`. And a `% 2 == 0` is the same as checking if the lowermost bit is set or not (`& 1`). (If it's not set, the number is even.) – DevSolar Sep 08 '15 at 13:12
0

You can overcome the TLE by using the correct algorithm which is suitable for your program which can be easily decided by looking into the program constraints.

you can refer to this tutorial which clearly explains below points-

  1. What is TLE?

  2. Why TLE occurs?

  3. How to overcome TLE?

Kanahaiya
  • 406
  • 2
  • 11
0

Use this:

C / C++

ios_base::sync_with_stdio(false); 
cin.tie(NULL) ;

Python

import psyco
psyco.full()

Java Do not use Scanner class, use BufferedReader instead.