Professional Documents
Culture Documents
(CPSIG)
This document is written to help beginners in the world of competitive
programming. The aim of this document is to discuss some algorithms
and techniques which help in competitive coding. Some classic
problems on various Online Judges have been discussed. Also list of
some good questions have been compiled. There is no prerequisite as
such but knowledge of at least one programming language is still
preferred.
Introduction
Competitive Programming in summary, is this: Given wellknown Computer Science (CS) problems, solve them as quickly as
possible!.
Lets digest the terms one by one. The term well-known CS
problems implies that in competitive programming, we are dealing
with solved CS problems and not research problems (where the solutions
are still unknown). Definitely, some people (at least the problem setter)
have solved these problems before. 'Solve them' implies that we must
push our CS knowledge to a certain required level so that we can
produce working codes that can solve these problems too in terms of
getting the same output as the problem setter using the problem setters
secret input data. As quickly as possible is the competitive element
which is a very natural human behavior.
Please note that being well-versed in competitive programming is not
the end goal, it is just the means. The true end goal is to produce allrounded computer scientists/programmers who are much more ready to
produce better software or to face harder CS research problems in the
future.
The founders of ACM International Collegiate Programming Contest
(ICPC) have this vision.
Some general tips:
Type code fast: When you can solve the same number of
problems as your competitor, it is now down to typing speed.
Quickly Identify Problem Types: It is important to identify the
category of the problem. It is good know if it an adhoc or a DP or a
Graph problem. We will be discussing these in details.
Do Algorithm Analysis: It is important you must know how
efficient is your algorithm is and how much space it occupies.
Online Judges
An online judge is an online system to test programs in programming
contests. They are also used to practice for such contests. Many of these
systems organize their own contests. The system can compile and
execute codes, and test them with pre-constructed data.
The following are some popular online judges:
SPOJ:
1. It is a problems archive ,(Recommended For ALL
Beginners)
2. Start with problems having maximum number of
submissions. Solve first 20 problems then start skipping .
3. When you develop little confidence start following some
good coders (check their initial submissions) .
4. Then start solving problem topic wise.
5. Never get stuck for too long in initial period . Try to google
your doubts or contact someone ( BUT ONLY IN
BEGINNING ).
6. Before getting into live contests like codechef/codeforces
solve at least 50 problems on SPOJ.
CODECHEF:
1 Only 3 Contests per month(MUST for all).
2 Even if you are not able to solve a problem do always look at
its editorials and then code and submit it(because it is the
only way you will learn).
3 And even if you able to solve then also check the editorials
and look at codes of top coders . See how they implemented
their code.
CODEFORCES:
1. 4-5 2 Hrs long contest( Once you develop some confidence ).
TOPCODER:
1. Start once you have proper experience and can write code
very fast.
If you carefully take a look at the code the first line ` int prod=1`
gets executed only once. The line ` prod*=i ` gets executed N times.
And the line ` return prod ` gets executed only once. Hence the
overall time complexity is hence 1*N + 2 . We ignore constants and just
say that the complexity of the program is O(n).
Concept:
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=co
mplexity1
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=co
mplexity2
Video Tutorials:
http://www.youtube.com/watch?v=OpebHLAf99Y&list=PL2_aWCzG
MAwI9HK8YPVBjElbLbI3ufctn
http://www.youtube.com/watch?v=PFd5s0bHgAQ&list=PL2_aWCzGM
AwI9HK8YPVBjElbLbI3ufctn
Problemset:
Basic Track:
SEQ, SPP, FIBOSUM, FIBTWIST, PRIME1, PRIMEZUK, DCEPCA06
, LASTDIG2, GCD2, KOPC12B,RANGZER2, ZSUM
Advanced Track:
PON, PAGAIN, HNUMBERS, POWPOW, NDIVPHI, SQFREE, ETF,
PROOT, NGM2
Would like to discuss the problem ZSUM. Before reading the problem
please go through the links suggested above and read about time
complexity.
Problem Statement:
For two given integers n and k find (Zn+Zn-1-2Zn-2)mod 10000007 ,
where Zn=Sn+Pn and Sn=1k+2k+3k+..+nk and Pn=11+22+33++nn.
Input
There are several test cases [ 10000 ].In each case two space separated
positive integers n and k are given.
For last test case n and k are given as 0 0 ,which is not to be processed.
1<n<200000000 , 0<k<1000000.
Output
For each case print the asked value in separate line.
Time Limit
1s
Strategies:
The easiest way to think will be finding Sn and Pn and then find Zn.
But if we carefully see the time taken to compute Sn alone is O(n log2k).
Cleary it will give TLE. ( As 200000000*log21000000 > 106)
So we need to change our approach what if I directly calculate (Zn+Zn-12Zn-2).
After some Maths one can easily arrive on the following formula.