Page 1 of 1

10506 - The Ouroboros problem

Posted: Thu Jun 19, 2003 5:45 am
by _evilgeek
There seem to be issues with the test data (or verifier) for this problem. I say this because I wrote my own verifier, and it returned "YES" for all valid input (besides the cases where n=1, where I verified the "0" output by my program by hand). Following is the verifier code (again, it doesn't work if n=1, and I know why):

[c]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int m, n;
int ntom;
char blah[200000];

int doit(int st) {
int i, j=0;
for (i = st; i < st+m; i++) {
j *= n;
j += blah-'0';
}
return j;
}

main() {
int seen[131072];
int i;
while(scanf("%i %i %s", &m, &n, blah) != EOF) {
ntom=1;
for (i = 0; i < m; i++) ntom *= n;
if (strlen(blah) != ntom) goto reject;
strncat(blah, blah, ntom);
memset(seen, 0, sizeof(seen));
for (i = 0; i < ntom; i++) {
seen[doit(i)]++;
}
for (i = 0; i < ntom; i++) {
if (seen != 1) goto reject;
}
printf("YES\n");
continue;
reject:
printf("NO %i %i\n", m, n);
}
}
[/c]

When run on every legal input, my program outputs something which this verifier says is a "YES" or a "NO m 1". The "NO m 1" cases are actually correct, since the solution outputs a lone "0."

If I've done something stupid, please let me know.

Nothing stupid - bad test data

Posted: Wed Jul 02, 2003 8:26 pm
by Carlos
Hi!
The test data was wrong, sorry. We've changed it and we're rejudging all submissions.

10506 TLE ouroborous

Posted: Fri Jun 25, 2004 1:59 am
by abishek
hi, i have just got the most expected answer TLE from the judge fot this question.
I had done the ouroborous snake problem within 8.00 secs in some other volume. thought i would use the same backtrack stratergy here, but it doesnot seem to work. I use the following idea:

first for each position, try 0--->n-1 and only if the ouroborous constructed so far is valid, i proceed to the next one. could some one help me on this. i use stl sets. is that the reason?

thanks
abi

Posted: Sun Sep 19, 2004 4:59 pm
by sidky
I just solved it through backtracking, and my solution is not too intelligent. But, it managed to get accepted in 0.004 seconds. My approach is similar to yours.
I have never used Set class. So, i dont know whether it is because STL. But, TLE looks a little too much to me. Look at your prunning too.

Posted: Mon Sep 20, 2004 1:30 pm
by abishek
that must be very nice
i have solved this problem now with the FKM algorithm.
really your backtrack must be very efficient as the FKM algorithm will give the answer bit by bit.
thanks for your reply
bye
abi

Posted: Tue Sep 21, 2004 6:06 am
by Observer
Hi. I've read something about FKM algorithm. However, I don't know how to use it to generate our desired sequence bit by bit, so... any hints?

(Btw, my backtracking solution gets Accepted in 0.008 s. Have some room of optimization still...)

Posted: Tue Sep 21, 2004 7:19 am
by abishek
i think you should try to read the new fasicles of Donald Knuth. One of them (Generating all n-tupules) contains the FKM algorithm in a beautiful manner.