1
0
0
0
0
players are participating in a round-robin tournament (each player plays against another player exactly once). Each player in the tournament is assigned a popularity index, a positive integer between
1
and
1
0
0
inclusive. The number of tickets sold in the game between two players is equal to the product of their popularity indices. Find the last three digits of the total number of tickets sold in the tournament.
Example
Say players
A
,
B
,
C
are participating in a tournament. The following table lists their popularity indices.
Player
A
B
C
Popularity index
5
7
8
The following table lists the number of tickets sold in each game.
Game
A vs B
B vs C
C vs A
Number of tickets sold
5
×
7
=
3
5
7
×
8
=
5
6
8
×
5
=
4
0
The total number of tickets sold is
5
6
+
4
0
+
3
5
=
1
3
1
.
Input file
This file
contains
1
0
0
0
0
lines with a positive integer between
1
and
1
0
0
in each line. The number written in the
i
th
line denotes the popularity index of player
i
.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
It is better to reduce both S1 and S2 modulo 2000 to avoid big numbers. Then the numerator evaluates to 2 x m o d ( 2 0 0 0 ) = 2 x + 2 0 0 0 m . Dividing this by 2, we get x + 1 0 0 0 m which is x m o d ( 1 0 0 0 ) and this is our desired result.
Log in to reply
And can you please check the input file ? I am getting last three digits as 441.
Log in to reply
Oops, I accidentally generated 10000 numbers with a random number generator another time after I got my answer. Well, that was silly. My sincere apologies. I've corrected the answer.
please explain how summation of Pi*Pj (i<>j)= sum(P1+....P1000)^2- sum(p1^2+.........P1000^2)
The figure I get is 126553032441.
I even tried the brute force method. Still the same.
Using Excel also gives the above result.
Log in to reply
Me too. I thought it was a straight nested for but I'm wrong apparently
Suppose we already have the number of tickets for three players n = a b + a c + b c . For a new player, we add a d + b d + c d = ( a + b + c ) ⋅ d .
Doing the same for each new player, we add a number of ticket that equals the sum of previous values multiplied by the new value.
1 2 3 4 5 6 7 8 9 10 |
|
x=open("C:\Users\MASBA\Dropbox\P-index.txt","r")
k=0
a=[]
for i in x:
a.append(i)
s=0
for p in range(len(a)-1):
for q in range(p+1,len(a)):
s=s+int(a[p])*int(a[q])
print s
This reads the data and calculates the value directly
a=[]
ticks=0
f = open('tournament.txt', 'r')
for i in f:
a.append(int(i))
print(len(a))
for i in range(len(a)):
for j in range(i+1,len(a)):
pop = (a[i]*a[j])
ticks+= pop
ticks = ticks % 1000
print(ticks)
My Pascal solution, I think it's O(n) solution
var
i : integer;
A : array[1..10000] of integer;
sum : longint;
sticket : longint;
begin
sum := 0;
for i := 1 to 10000 do begin
readln(A[i]);
sum := sum + A[i];
end;
sticket := 0;
for i := 1 to 9999 do begin
sum := sum - A[i];
sticket := sticket + (sum mod 1000) * A[i];
if (sticket >= 1000) then sticket := sticket mod 1000;
end;
writeln(sticket);
end.
It gave result 441.
My simple Python solution:
import operator
summation = 0; tickets = 0
for line in open('input.txt'):
tickets += int(line)*summation
summation += int(line)
print (operator.mod(tickets, 1000))
I used a simple Python coding. Just the simple logic of:
{ \sum { i=1 }^{ 9999 }{ \left( { a } { i }\sum { j=i+1 }^{ 10000 }{ { a } { j } } \right) } ]
where a n is the popularity index.
from sys import argv
script, filename = argv
txt = open(filename)
s = []
for i in range(10000):
s.append(int(txt.readline()))
sum = 0
for j in range(9999):
for k in range(j+1,10000):
sum += s[j]*s[k]
print j, sum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Python bruteforce + string slicing (or mod by 1000)
1 2 3 4 5 6 7 8 9 |
|
Though it's bruteforce, here rejecting the other digits and keeping the last 3 digit only saves complex computation.
Answer: 441
Here is another solution: First count the number of players with popularity
pop
in [1,...,100]. This groups the players into 100 popularity bins (I'll call them
count
below), which can be used to caluclate the number of tickets like this:
1 2 3 4 5 |
|
Throw in a
sum = sum % 1000
in the loop in case the number precision isn't sufficient.
First thing to notice is that we have a triangular sum. To avoid repeats of matches. The sum can be written as ( i = 0 ∑ 1 0 0 0 0 j = i + 1 ∑ 1 0 0 0 0 ( P i × P j m o d 1 0 0 0 ) ) m o d 1 0 0 0 using the distributive properties of m o d .
Here is my C# code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
This runs pretty fast. (Under a second easily).
I have almost the same code but without the mod 1000. Why did you use that? My answer is wrong so I'm trying to understand. - Cheers
Log in to reply
That is to reduce the load of the calculations by simply pairwise multiplying the last three digits of the popularity indices and adding them. Since the question asks for the last three digits of the answer, this works.
Darn I wanted to make the number of players large so that the brute force approach fails. Apparently I should have chosen a larger number.
StreamReader sr = new StreamReader(@"....\indexes.txt"); long kq = 0; string cidx; List<int> idx = new List<int>(); for (int i = 0; i < 10000; i++) { cidx = sr.ReadLine(); idx.Add(Convert.ToInt32(cidx)); } for (int i = 0; i < 10000 - 1; i++) { for (int j = i + 1; j < 10000; j++) { kq += (idx[i] * idx[j]); } } kq = kq % 1000; R e s u l t = 4 4 1
Problem Loading...
Note Loading...
Set Loading...
Let P i be the popularity index of player i . The desired quantity is i = j ∑ P i P j , which is also equal to 2 ( i = 1 ∑ 1 0 0 0 0 P i ) 2 − i = 1 ∑ 1 0 0 0 0 P i 2 . We write the following code to print this.