Problem A: Sigma Function
Problem A: Sigma Function
Sigma Function
Input: Standard Input
Output: Standard Output
Sigma function is an interesting function in Number Theory. It is denoted by the Greek letter Sigma
(σ). This function actually denotes the sum of all divisors of a number. For example σ(24) =
1+2+3+4+6+8+12+24=60. Sigma of small numbers is easy to find but for large numbers it is very
difficult to find in a straight forward way. But mathematicians have discovered a formula to find
sigma. If the prime power decomposition of an integer
n = p1e1 * p2e2 * p3e3 * ... * pnen−−11 * pnen , then
p1e1 +1 − 1 p2e2 +1 − 1 p3e3 +1 − 1 pnen−−11 +1 − 1 pnen +1 − 1
σ ( n) = * * * ... * *
p1 − 1 p2 − 1 p3 − 1 pn −1 − 1 pn − 1
For some n the value of σ(n) is odd and for others it is even. Given a value n, you will have to find
how many integers from 1 to n have even value of σ.
Input
The input file contains at most 100 lines of inputs.
Input is terminated by a line containing a single zero. This line should not be processed.
Output
For each line of input produce one line of output. This line denotes how many numbers between 1
and N (inclusive) has even value of function σ.
A claw is defined as a pointed curved nail on the end of each toe in birds, some
reptiles, and some mammals. However, if you are a graph theory enthusiast, you may
understand the following special class of graph as shown in the following figure by
the word claw.
If you are more concerned about graph theory terminology, you may want to define
claw as K1,3.
Let’s leave the definition for the moment & come to the problem. You are given a
simple undirected graph in which every vertex has degree 3. You are to figure out
whether the graph can be decomposed into claws or not.
Just for the sake of clarity, a decomposition of a graph is a list of sub-graphs such that
each edge appears in exactly one sub-graph in the list.
Input
There will be several cases in the input file. Each case starts with the number of
vertices in the graph, V (4<=V<=300). This is followed by a list of edges. Every line
in the list has two integers, a & b, the endpoints of an edge (1<=a, b<=V). The edge
list ends with a line with a pair of 0. The end of input is denoted by a case with V=0.
This case should not be processed.
Output
For every case in the input, print YES if the graph can be decomposed into claws &
NO otherwise.
Given the height of each non leaf node in a forest, find the number of ways to build the
forest. All the trees are complete binary tree, and you can assume that, there are always
enough leaf nodes to complete the tree.
You have to consider two cases, when all nodes are of same color, and when all are of
different color.
Please remember, for colored case, these two trees are different. But, within a forest,
ordering of trees doesn’t matter.
3 1000
211
As we know, in an n-based number system, there are n different types of digits. In this
way, a 1-based number system has only 1 type of digit, the ‘0’. Here are the rules to
interpret 1-based numbers. Each number consists of some space separated blocks of 0.
A block may have 1, 2 or more 0s. There is a ‘flag’ variable associated with each
number
• A block with a single 0 sets ‘flag’ variable to 1
• A block with two 0s sets the ‘flag’ to 0
• If there are n (n > 2) 0s in a block, n – 2 binary digits with the current value of
flag is appended to your number.
Note that, the first block of every number will have at most 2 0s. For example, the 1-
base number 0 0000 00 000 0 0000 is equivalent to binary 11011.
• 1st block sets the flag to 1
• 2nd block has 4 0s. So append flag(=1) 4 – 2 = 2 times (11).
• 3rd block has 2 0s. Set the flag to 0
• 4th block has 3 0s. Append flag(=0) 3-2 = 1 time (110).
• 5th block has a single 0. Set flag = 1
• 6th and block has 4 0s. Append flag(=0) 4-2=2 times (11011).
The final binary number won’t have more than 30 digits. Once, you’ve completed the
process, convert the binary value to decimal & print, you’re done!
Input
Input will have at most 100 test cases. Each case consists of a 1-based number as
described above. A number may be spanned to multiple lines but a single block will
always be in a single line. Termination of a case will be indicated by a single ‘#’ char
which will be space-separated from the last digit of your input number. The last case
in the input is followed by a ‘~’ character indicating, end of input.
Output
For each test case, output a single line with the decimal equivalent value of your given
1-based number.
Sir Jagadish Chandra Bose (জগদীশ চnd বসু , Jôgodish Chôndro Boshu) (November
30, 1858 – November 23, 1937) was a Bengali physicist and science fiction writer,
is considered the father of radio science. He made remarkable progress in his
research of remote wireless signaling as well as in plant physiology. Sir Bose
scientifically proved parallelism between animal and plant tissues using his own
invention crescograph to measure plant response to various stimuli. His
experiments showed that plants grow faster in pleasant music and its growth
retards in noise or harsh sound. [ courtesy : www.wikipedia.org ]
Our hero little Dablu (his actual name is Bablu, see “Shahnaj o Captain
Dablu” by Dr. Md. Zafar Iqbal) is conducting some experiments on plants.
As demonstrated by Sir J.C. Bose, Dablu is upto observing the influence of
music on plants growth. He plays a certain tune to a certain plant for
elongated period of time and takes snapshots of the plant in regular
intervals. Dablu does not believe in uprooting a plant completely or yanking a branch of the plant for
so called ‘speciment collection’ purpose, plants are also living beings, after all. Instead, he would take
a picture of the plant, convert this picture into a string to represent the plant according to a L-System
grammer defined by himself. The string will be stored into his PDA along with time-stamp and other
meta-data. Upon returning to his lab, he would download the snapshots (L-System strings) from his
PDA into his computer. Then he will use a program written by you to reconstruct the plant pictures
from the L-System strings. Only after that he could pay attention to his actual research work i.e.,
analyzing the pictures to figure out the influence of music on growth of plants.
You are to write a program to convert L-System strings into pictures of trees, you need to be properly
informed of the L-System. Aristid Lindenmayer (November 17, 1925 – October 30, 1989) was a
Hungarian biologist who developed a formal language called L-systems or Lindenmeyer Systems to
model plants. The L-System consists of the following components.
V : an alphabet of symbols.
S : a non-empty starting word (or seed)
P : a set of productions rules of the form a -> A , where a ε V and A is a string consisting of
symbols from V only.
For the job at hand, Dablu has supported us with the following alphabet of the L-System he is using.
V = { U , L , l , R , r , f , P , p , [ , ]}
Since it is not our job to convert the plants into L-System strings, we are not really bothered about the
seed or the production rules he uses to encode a plant into L-System string. We are concerned with
interprating each of the symbols in his alphabet on the picture. According to Dablu, the procedure of
drawing a plant from its L-System string should be considered similar to drawing a plant with a pencil
on a paper. We start by putting the tip of the pencil on paper. Then draw line sements in various
directions representing stems of the plant in various spatial orientations. Each of the symbols in V has
a corresponding direction of line segment or any other artifact to be drawn on the paper, which is
given in the following table. Note that the pencil keeps moving according to the drawing. Dablu has
also told us that all the given strings will start with either ‘U’ or ‘[’.
Symbol Drawn Interpretation
as
Upward stroke of pencil, drawing an upright stem/trank of the plant.
U |
A horizontal leftward stroke, drawing a horizontal- stem.
L _
Left inclined stroke, drawing a leftward inclined stem.
l \
A horizontal rightward stroke, drawing a horizontal- stem.
R _
Right inclined stroke, drawing a rightward inclined stem.
r /
A flower.
f *
A growing fruit.
p o
A fully grown fruit.
P O
Indicates the starting of a branch. Whatever is found after a ‘[’ symbol is on this
[ branch of the plant. Note that there a branch itself can have one or more sub-branches
as well.
Indicates the end of a branch. When this symbol is found, the pencil is brought back to
]
the position from where this branch had started.
Input
There will be several lines of input. An L-System string on each line, terminating with a ‘\n’,
containing up to 500 symbols from V and only from V. Input file terminates with EOF. You can
assume that all given strings will be valid and will draw a plant if converted correctly.
Output
For each L-System string your output should be the picture of a correctly drawn plant enclosed in a
rectangular frame similar to the ones shown in sample output. Note that the corners of the character at
the corners of the rectangles are ‘.’(period). There should be no extra white space up/down/right/left
of the plant inside the frame. If you need to overwrite any character while drawing, just go do it.
You are given the task to design a lighting system for a huge conference hall. After
doing a lot of calculation & sketching, you have figured out the requirements for an
energy-efficient design that can properly illuminate the entire hall. According to your
design, you need lamps of n different power ratings. For some strange current
regulation method, all the lamps need to be fed with the same amount of current. So,
each category of lamp has a corresponding voltage rating. Now, you know the number
of lamps & cost of every single unit of lamp for each category. But the problem is,
you are to buy equivalent voltage sources for all the lamp categories. You can buy a
single voltage source for each category (Each source is capable of supplying to
infinite number of lamps of its voltage rating.) & complete the design. But the
accounts section of your company soon figures out that they might be able to reduce
the total system cost by eliminating some of the voltage sources & replacing the
lamps of that category with higher rating lamps. Certainly you can never replace a
lamp by a lower rating lamp as some portion of the hall might not be illuminated then.
You are more concerned about money-saving than energy-saving. Find the minimum
possible cost to design the system.
Input
Each case in the input begins with n (1<=n<=1000), denoting the number of
categories. Each of the following n lines describes a category. A category is described
by 4 integers - V (1<=V<=132000), the voltage rating, K (1<=K<=1000), the cost of a
voltage source of this rating, C (1<=C<=10), the cost of a lamp of this rating & L
(1<=L<=100), the number of lamps required in this category. The input terminates
with a test case where n = 0. This case should not be processed.
Output
For each test case, print the minimum possible cost to design the system.
You are given n rods of length 1, 2…, n. You have to pick any 3 of them & build a
triangle. How many distinct triangles can you make? Note that, two triangles will be
considered different if they have at least 1 pair of arms with different length.
Input
The input for each case will have only a single positive integer n (3<=n<=1000000).
The end of input will be indicated by a case with n<3. This case should not be
processed.
Output
For each test case, print the number of distinct triangles you can make.
In the ancient pirate ages, the Pirate Land was divided into two teams of pirates,
namely, the Buccaneer and the Barbary pirates. Each pirate’s team was not fixed,
sometimes the opponent pirates attacked and he was taken away to the other pirate
team. All on a sudden a magician appeared in the Pirate Land, where he was making
transition of pirates from their team to other team at his own will. Of course, handy
spells were used. The process of changing team was known as mutating.
There were N pirates and all of the pirates have a unique id from 0 to N-1. The great
magician could mutate a bunch of pirates with consecutive id’s to another one.
Suppose there were 100 pirates in the pirate land and all of them were Barbary pirates,
then the magician could cast a spell to change pirates with id’s from 10 to 33 to
Buccaneer pirates. Then the whole pirate land would have 24 Buccaneer and 76
Barbary pirates.
The magician was very fast casting the spell. Once, God started to dislike this. God
had favor for the Buccaneer pirates and God asked the magician, “Tell me, how many
of the pirates of index from 2 to 30 are Buccaneer pirates?”. Now the magician was
puzzled as he was only efficient in spells, not counting ☺
Being clever enough, the magician captured a clever man from the Earth Land. And
unfortunately it’s you! Now you have to answer the God’s questions.
Input
Output
For each test print the case number as the sample output suggests. Then for each of
“God’s query”, output the query number, colon(:) and a space and the answer to the
query as the sample suggest.
Explanation:
Case1:
The pirate land is as follows (N = 18)
101010101010001000
Before God’s first query it was as follows
000000111111111111
Case 2:
The pirate land is as follows (N=9)
111000000
Input specification:
Each case will define two binary strings (x & y). You can assume that length of each
string will not exceed 30. The program will terminate with a 0 0.
111
10
---
000
111
----
1110
10
111
---
10
10
10
----
1110
------------------------------------------------
Problem setter: M. Mizanur Rahman