Just Make Sure Your Computer Doesn't Crash

10000 10000 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 1 and 100 100 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 A,B,C are participating in a tournament. The following table lists their popularity indices. Player Popularity index A 5 B 7 C 8 \begin{array}{|c|c|} \text{Player} & \text{Popularity index} \\ \hline A & 5 \\ \hline B & 7 \\ \hline C & 8 \end{array} The following table lists the number of tickets sold in each game. Game Number of tickets sold A vs B 5 × 7 = 35 B vs C 7 × 8 = 56 C vs A 8 × 5 = 40 \begin{array}{|c|c|} \text{Game} & \text{Number of tickets sold} \\ \hline \text{A vs B} & 5 \times 7 = 35 \\ \hline \text{B vs C} & 7 \times 8= 56 \\ \hline \text{C vs A} & 8 \times 5 = 40 \end{array} The total number of tickets sold is 56 + 40 + 35 = 131. 56+40+35 = 131.

Input file
This file contains 10000 10000 lines with a positive integer between 1 1 and 100 100 in each line. The number written in the i th i^{\text{th}} line denotes the popularity index of player i . i.


The answer is 441.

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.

12 solutions

Let P i P_i be the popularity index of player i . i. The desired quantity is i j P i P j , \displaystyle \sum_{i \neq j} P_i P_j, which is also equal to ( i = 1 10000 P i ) 2 i = 1 10000 P i 2 2 . \dfrac{\left( \displaystyle \sum_{i=1}^{10000} P_i \right) ^2 - \displaystyle \sum_{i=1}^{10000} P_i^2}{2}. We write the following code to print this.

f = open('input.txt', 'r') #open the file
S1= 0 #variable to store the sum of the popularity indices
S2= 0 #variable to store the sum of the squares of the popularity        indices
for line in f:
  S1 += int(line) #increment S1 by the number written in the line 
  S2 += int(line)**2 #increment S2 by the square of the number written in the line
tickets= (S1**2 - S2)/2 #find the total number of tickets sold
print tickets #print

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 ( 2000 ) = 2 x + 2000 m 2x \mod(2000) = 2x+ 2000 m . Dividing this by 2, we get x + 1000 m x+1000m which is x m o d ( 1000 ) x \mod (1000) and this is our desired result.

Abhishek Sinha - 6 years, 11 months ago

Log in to reply

And can you please check the input file ? I am getting last three digits as 441.

Abhishek Sinha - 6 years, 11 months ago

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.

Sreejato Bhattacharya - 6 years, 11 months ago

please explain how summation of Pi*Pj (i<>j)= sum(P1+....P1000)^2- sum(p1^2+.........P1000^2)

vikas kumar - 6 years, 11 months ago

The figure I get is 126553032441.

I even tried the brute force method. Still the same.

Using Excel also gives the above result.

Venu Gopal - 6 years, 11 months ago

Log in to reply

Me too. I thought it was a straight nested for but I'm wrong apparently

Amanda Hogan - 6 years, 11 months ago
Laurent Shorts
Apr 28, 2016

Suppose we already have the number of tickets for three players n = a b + a c + b c n=ab + ac + bc . For a new player, we add a d + b d + c d = ( a + b + c ) d ad + bd + cd = (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
indexes = [48,85,40, ... ,21,3,22]

tickets = 0 
partialSum = indexes[0]

for i in xrange(1, len(indexes)):
        tickets += indexes[i] * partialSum
        partialSum += indexes[i]

print tickets

Masbahul Islam
Dec 9, 2015

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

Chuck Jerian
Mar 26, 2015

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.

Giuseppe Marcucci
Dec 28, 2014

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))
Chew-Seong Cheong
Sep 20, 2014

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 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

Drop TheProblem
Sep 16, 2014
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
using namespace std;
int main() {
    int h[10005],ris=0;
    ifstream in("tournament.txt");
    ofstream out("output.txt");
    for (int i=0;i<10000;i++)
        in>>h[i];
    for (int i=0;i<9999;i++)     
        for (int j = i+1; j < 10000; j++) 
            ris=(ris+(h[i]*h[j])%1000)%1000;
    out<<ris<<endl;
    return 0;
} 

Sudip Maji
Aug 15, 2014

Python bruteforce + string slicing (or mod by 1000)

1
2
3
4
5
6
7
8
9
data = []
with open('tournament.txt') as fp:
    for line in fp:
        data.append(int(line))
s = '0'
for num, i in enumerate(data):
    for j in data[num+1:]:
        s = str(int(s) +int(str(i*j)[-3::1]))[-3::1]
print s

Though it's bruteforce, here rejecting the other digits and keeping the last 3 digit only saves complex computation.

Answer: 441

Erik Weitnauer
Jul 8, 2014

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
sum = 0;
for pop in [1,...,100]:
  sum += pop*pop * count[pop]*(count[pop]-1)/2;
  for pop2 in [pop+1,...,100]:
    sum += pop*pop2 * count[pop]*count[pop2];

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 10000 j = i + 1 10000 ( P i × P j m o d 1000 ) ) m o d 1000 \left(\sum^{10000}_{i=0}\sum^{10000}_{j=i+1}(P_i\times P_j\mod 1000)\right)\mod 1000 using the distributive properties of m o d \mod{} .


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
int[] popularities = new int[10000];
string[] popStrings = 
System.IO.File.ReadAllLines(@"!!_FileLocationCensored_!!");

for (int i = 0; i < 10000; i++)
{
    popularities[i] = int.Parse(popStrings[i]); ;
}

int numOfTickets = 0;

for (int i = 0; i < 10000; i++)
{
    for (int j = i + 1; j < 10000; j++)
    {
        numOfTickets += popularities[i] * popularities[j];
         numOfTickets %= 1000;
    }
}

Console.WriteLine(numOfTickets);
Console.ReadLine();

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

Amanda Hogan - 6 years, 11 months ago

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.

Sreejato Bhattacharya - 6 years, 10 months ago

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.

Sreejato Bhattacharya - 6 years, 10 months ago
Aaaaa Bbbbb
Jul 1, 2014

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 = 441 Result = \boxed{441}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...