Mash

advertisement
Problem I. StanchoHash
Stancho have a new super cool task for you. You have probably heard about his sorting
algorithm research. He has a lot of sorting programs on his Intel 8080 computer. In order to
prove that StanchoSort is the best of them, he has to compare their efficiency. That’s why he
has created some very cool number sequences. He wants to calculate a hash code for each of
them. Unfortunately Stancho doesn’t trust in the popular hashing algorithms like MD5, CRC,
SHA1, etc. He has invented a better one – StanchoHash. It’s very simple. Multiply the first
element by 1, the second by 2, the third by 3, etc. The sum of these products is the hash of the
sequence. For example, the hash code of (3, 4, 5) is 3*1 + 4*2 + 5*3, which he can’t
calculate (he is very bad at algebra). So you have to write a program, which by given
characteristics of a sequence, calculate its hash code.
Input:
Read the input data from standard input. It contains several test cases. Each of them
contains four integers on a single line – N (1 <= N <= 1000), B (1 <= B <= 1000), S (1 <= S
<= 1000), and I.
N is the number of elements in the sequence, B is the smallest element, S is the step between
the elements (a 3 elements sequence with B = 3 and S = 5 will have the elements 3, 8, 13 in
some order). I is the number of inversions – the number of pairs of elements in the sequence,
for which the left element (with lower index) is greater than the right one. There will always
exist a sequence with the given properties.
Output:
For each given test case, print to the standard output the hash code of the sequence
with given properties. If there are more than one sequences with these properties, print the
hash code of the lexicographically largest.
Sample Input:
2 1 1 0
3 4 5 2
7 8 9 10
Sample Output:
5
49
953
Download