Results 1 to 25 of 25

Thread: Riddle

  1. #1

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Cool Riddle

    This is taken from the entry test of a CS contest I recently entered.

    Write a program (Pascal/C/C++) that inputs a number and outputs whether or not this number is an average of three different numbers that are powers of two. (1 counts as a power of two.)
    Make it as efficient as possible, both in time complexity and memory usage.

  2. #2
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Riddle

    Originally posted by Yonatan
    Write a program (Pascal/C/C++) that inputs a number...
    Is this just any number (i.e. not necessarily integer)?

  3. #3
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    do you have a limit to the number? ie it fits in a 32-bit integer or maybe i have to use long?
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  4. #4

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Oops, forgot.
    The number is between 1 and 100,000.

  5. #5

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Originally posted by bugzpodder
    do you have a limit to the number? ie it fits in a 32-bit integer or maybe i have to use long?
    32-bit integer and long are the same (in C/C++ at least).

  6. #6
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    i typed this up so there is probably a few syntax errors
    program runs in O(logN) time and uses O(0) storage
    I doubt you can make it any more efficient

    Code:
    #include <iostream.h>
    
    bool isrequired(int k, int lvl){
        while (k>=0 && k%2==0) k/=2;
        if (lvl==2)
            return (--k==0);
        else return isrequired(--k,lvl+1);
    }
    
    }
    
    void main(){
       int k;
       cin>>k;
       if (isrequired(k*3,0))
             cout<<"good number";
       else cout<<"bad number";
    }
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  7. #7

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    It doesn't use O(0) storage. (Hmm, shouldn't that be O(1)?) It's recursive, thus uses O(log n) storage, for the recursion stack (though in this case it is very easy to turn it into an iterative algorithm.

    I assume it works, but I'm not going to test it since there's a better solution, both in time and storage. Any ideas?

  8. #8
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    yeah i meant to say O(1). no one asked you to test my solution my solution is good enough for me and i can almost guarentee that there is not an O(1) algorithm -- so if you think you got it why don't you show me. btw it is the while loop that gives O(logN) runtime, NOT RECURSION. here i'll take away the recursion.

    Code:
    #include <iostream.h>
    
    
    void main(){
       int k,i;
       cin>>k;
       k*=3;
       for (i=0;i<3;i++){
           while (k>=0 && k%2==0) k/=2;
            k--;
       }
       
       if (k==0)  
             cout<<"good number";
       
       else cout<<"bad number";
    }
    Last edited by bugzpodder; Dec 31st, 2002 at 11:19 AM.
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  9. #9

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Now find the O(1) algorithm

  10. #10
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    not possible. but by all means, prove me wrong!
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  11. #11

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Yes possible
    I'll post it tomorrow to give people more time to think about it.

  12. #12
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    In the mean time here is a challenge for you. Lets see if you can do this problem (I am not even asking for a best algorithm): I am just asking for a solution that would work for any case correctly within 5 seconds time limit.

    Camelot

    Centuries ago, King Arthur and the Knights of the Round Table used to meet every year on New Year's Day to celebrate their fellowship. In remembrance of these events, we consider a board game for one player, on which one chesspiece king and several knight pieces are placed on squares, no two knights on the same square.

    The King can move to any adjacent square from to as long as it does not fall off the board.


    A Knight can jump in an L shape, as long as it does not fall off the board:


    During the play, the player can place more than one piece in the same square. The board squares are assumed big enough so that a piece is never an obstacle for any other piece to move freely.

    The player's goal is to move the pieces so as to gather them all in the same square - in the minimal number of moves. To achieve this, he must move the pieces as prescribed above. Additionally, whenever the king and one or more knights are placed in the same square, the player may choose to move the king and one of the knights together henceforth, as a single knight, up to the final gathering point. Moving the knight together with the king counts as a single move.

    Write a program to compute the minimum number of moves the player must perform to produce the gathering. The pieces can gather on any square, of course.

    PROGRAM NAME: camelot
    INPUT FORMAT
    Line 1: Two space-separated integers: R,C, the number of rows and columns on the board. There will be no more than 26 columns and no more than 40 rows.
    Line 2..end: The input file contains a sequence of space-separated letter/digit pairs, 1 or more per line. The first pair represents the board position of the king; subsequent pairs represent positions of knights. There might be 0 knights or the knights might fill the board. Rows are numbered starting at 1; columns are specified as upper case characters starting with `A'.

    SAMPLE INPUT (file camelot.in)
    8 8
    D 4
    A 3 A 8
    H 1 H 8

    The king is positioned at D4. There are four knights, positioned at A3, A8, H1, and H8.

    OUTPUT FORMAT
    A single line with the number of moves to aggregate the pieces.
    SAMPLE OUTPUT (file camelot.out)
    10

    SAMPLE OUTPUT ELABORATION
    They gather at B5.
    Knight 1: A3 - B5 (1 move)
    Knight 2: A8 - C7 - B5 (2 moves)
    Knight 3: H1 - G3 - F5 - D4 (picking up king) - B5 (4 moves)
    Knight 4: H8 - F7 - D6 - B5 (3 moves)
    1 + 2 + 4 + 3 = 10 moves.
    Last edited by bugzpodder; Dec 31st, 2002 at 06:39 PM.
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    dumdedum

    Code:
    #include <iostream.h>
    inline int bsf(int x){
    	int y;
    	__asm{ 
    		mov eax,x
    		bsf ebx,eax
    		mov y,ebx
    	}
    	return y;
    }
    int main(){
       int k;
    	cin>>k;
    	k*=3;
    	if (k=k>>(bsf(k)+1))
    		if (k=k>>(bsf(k)+1)){
    		    if (!(k>>(bsf(k)+1))){
    				cout<<"good number"<<endl;
    			return 0;
    		}
    	cout<<"bad number"<<endl;
    	return 0;
    }
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Kedaman has the idea.
    I didn't use a crappy bit scan though, I used:
    Code:
    int CheckNumber(int N)
    {
        N *= 3;
        return (N &= (N - 1)) && (N &= (N - 1)) && !(N &= (N - 1));
    }
    Anyone got any idea how/why this works?

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    interesting..
    it flips all the bits with and after lowest set bit, and by &= you'll flip the lowest set bit
    beats mine

    the powers of two wasn't that tricky, summing different powers of two will only set the particular bit, so you'll have 3 bits set.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    ahh bit operations... never touched the subject b4!
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    mathematicians and even programmers shouldn't be dealing with bit operations but here we are
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  18. #18
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    well think you guys can come up with a feasible approach to my challenge?
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  19. #19
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    so are the knights only able to travel together with one move when with the king or can they travel together without the king?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  20. #20
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    each pieces (knight and king) is able to move like they do on a chessboard. for each and every move each pieces make, you add the 1 to number of totalmoves. one single knight can pick up one single king by being in the same square anywhere on the board, and you may move both of them like a knight in one single move. A knight cannot pick up another knight so even if n knights were to move together (ie from A to B simutaneously) they still count as n moves
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  21. #21
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    ah so in other words a king cannot be picked up by two knights? sorry I got the impression that several knights could go together if with a king.. I'm on something, but hey is it nessesary with the input and output files and stuff? i just want to work out an algoritm
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  22. #22

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Originally posted by kedaman
    mathematicians and even programmers shouldn't be dealing with bit operations but here we are
    eh? Of course we should

  23. #23
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    Originally posted by kedaman
    ah so in other words a king cannot be picked up by two knights? sorry I got the impression that several knights could go together if with a king.. I'm on something, but hey is it nessesary with the input and output files and stuff? i just want to work out an algoritm
    do what you gotta do
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  24. #24
    Addicted Member
    Join Date
    Aug 2002
    Location
    London UK
    Posts
    255
    Mr Hooper, haven't seen you around in a while...
    Not at all related to sheep...

  25. #25
    Junior Member
    Join Date
    Aug 2002
    Location
    Phoenix
    Posts
    30

    answer

    **edit**
    Last edited by Amithran; Jan 5th, 2003 at 02:23 AM.
    I'm in college now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width