Let's Learn it Together !

ِAn educational blog from FCIS'2011 students ..

Dear All,

Firstly, please read Prime Numbers and Primality Test.

Great! But, what if we calculate the whole array before submitting the problem to the judge?!

This is actually a very important trick that all good ACMers must know, so please read this message with great concentration.

Firstly, thank you very much, Hafez. You've chosen a very important topic; and a glitch that happens to appear every now and then in ACM problems – which is dealing with prime numbers.

I'll now solve problem 10924.

We have the string length = 20 as a maximum. This means that the "longest" string (I mean the string with the maximum sum) is "ZZZZZZZZZZZZZZZZZZZZ", which is equal to 52 * 20 = 1040. This means that – simply – the array called "prime" can be completely computed at compile time, so that we don't consume time at the judge.

So, I solved the whole issue on 2 steps. Firstly, I'll write a complete program to generate the array isPrime.

Then I'll just take this ready-made array and put it in my easy program

And here we come to the most important lesson… that’s actually my aim behind sending this message…

WHENEVER THE DATA CAN BE INDEPENDENT OF THE INPUT, COMPUTE IT AT COMPILE TIME.

And I think you now understand that it’s actually your task to determine what data are independent of the input. This is very important, and can easily improve the performance of your solution drastically.

This is the pdf version of this article.

3 comments:

thanks dr.omer

f3lan kan el7eta deh bet3ml ma3ia mashkel m3a el judge we kont hatganen

thanks very much:)

There is also another site for problem solving which only needs the final answer and totally depends on writing an efficient algorithm and not brute force which can stay for days running like this :
"The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?"

http://projecteuler.net

المقال ده فادنى جدا فى حل مسألة
Ugly Numbers

لينك المسألة أهو

http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=3&problem=72&mosmsg=Submission+received+with+ID+7029509

http://acm.pku.edu.cn/JudgeOnline/problem?id=1338

Post a Comment