-
Dec 29th, 2002, 06:40 PM
#1
Thread Starter
Guru
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.
-
Dec 31st, 2002, 04:13 AM
#2
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)?
-
Dec 31st, 2002, 08:40 AM
#3
Fanatic Member
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)!
-
Dec 31st, 2002, 08:54 AM
#4
Thread Starter
Guru
Oops, forgot.
The number is between 1 and 100,000.
-
Dec 31st, 2002, 08:55 AM
#5
Thread Starter
Guru
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).
-
Dec 31st, 2002, 09:14 AM
#6
Fanatic Member
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)!
-
Dec 31st, 2002, 09:59 AM
#7
Thread Starter
Guru
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?
-
Dec 31st, 2002, 11:11 AM
#8
Fanatic Member
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)!
-
Dec 31st, 2002, 02:42 PM
#9
Thread Starter
Guru
Now find the O(1) algorithm
-
Dec 31st, 2002, 04:51 PM
#10
Fanatic Member
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)!
-
Dec 31st, 2002, 05:34 PM
#11
Thread Starter
Guru
Yes possible
I'll post it tomorrow to give people more time to think about it.
-
Dec 31st, 2002, 06:29 PM
#12
Fanatic Member
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)!
-
Jan 1st, 2003, 06:57 AM
#13
transcendental analytic
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.
-
Jan 1st, 2003, 01:09 PM
#14
Thread Starter
Guru
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?
-
Jan 1st, 2003, 01:47 PM
#15
transcendental analytic
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.
-
Jan 1st, 2003, 01:51 PM
#16
Fanatic Member
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)!
-
Jan 1st, 2003, 01:56 PM
#17
transcendental analytic
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.
-
Jan 1st, 2003, 01:57 PM
#18
Fanatic Member
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)!
-
Jan 2nd, 2003, 02:33 AM
#19
transcendental analytic
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.
-
Jan 2nd, 2003, 07:59 AM
#20
Fanatic Member
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)!
-
Jan 2nd, 2003, 09:23 AM
#21
transcendental analytic
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.
-
Jan 2nd, 2003, 11:59 AM
#22
Thread Starter
Guru
Originally posted by kedaman
mathematicians and even programmers shouldn't be dealing with bit operations but here we are
eh? Of course we should
-
Jan 2nd, 2003, 12:57 PM
#23
Fanatic Member
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)!
-
Jan 2nd, 2003, 05:10 PM
#24
Addicted Member
Mr Hooper, haven't seen you around in a while...
Not at all related to sheep...
-
Jan 5th, 2003, 02:19 AM
#25
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|