Codeforces edu round3, codeforcesround3
B. The Best Gift Portal: http://codeforces.com/problemset/problem/609/ B
Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there areNBooks on sale from oneMGenres.
In the bookshop, Jack decides to buy two books of different genres.
Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. options are considered different if they differ in at least one book.
The books are given by indices of their genres. The genres are numbered from 1M.
Input
The first line contains two positive integersNAndM(2 cores ≤ CoresNLimit ≤ limit 2 · 105, limit 2 limit ≤ limitMLimit ≤ limit 10)-the number of books in the bookstore and the number of genres.
The second line contains a sequenceA1, bytes,A2, middle..., middle ,...,AN, WhereAI(1 digit ≤ DigitAILimit ≤ limitM) Equals the genre ofI-Th book.
It is guaranteed that for each genre there is at least one book of that genre.
Output
Print the only integer-the number of ways in which Jack can choose books.
It is guaranteed that the answer doesn't exceed the value 2 · 109.
Sample test (s)Input
4 3
2 1 3 1
Output
5
Input
7 4
4 2 3 1 2 4 3
Output
18
Note
The answer to the first test sample equals 5 as Sasha can choose:
Yes, I have timed out. · test19 must be big data, but I cannot find any way to optimize it. Keep the trap and try again later
1 #include <stdio.h> 2 #include <stdlib.h> 3 int books[20000]; 4 int main(int argc, char *argv[]) 5 { 6 int n,m; 7 __int64 ways; 8 while(scanf("%d%d",&n,&m)!=EOF) 9 {10 ways=0;11 int i,j;12 //for(i=0;i<n;i++)13 //scanf("%d",&books[i]);14 scanf("%d",&books[0]);15 for(i=1;i<n;i++)16 {17 scanf("%d",&books[i]);18 for(j=0;j<i;j++)19 {20 if(books[i]!=books[j])21 ways++;22 }23 }24 printf("%I64d\n",ways);25 }26 return 0;27 }
Today, cf looked at other people's code and used a mathematical method to access the Internet ···
# Include <stdio. h> # include <stdlib. h> # include <memory. h> int a [200000], B [11]; // group B is used to record the book type int main (int argc, char * argv []) {int n, m; int I, count; int k; while (scanf ("% d", & n, & m )! = EOF) {memset (B, 0, sizeof (0); k = 0; for (I = 0; I <n; I ++) {scanf ("% d", & a [I]); B [a [I] ++; // number of books of the corresponding category ++} count = n; for (I = 1; I <= m; I ++) {count = count-B [I]; k + = B [I] * count; // The number of B [I] types of books multiplied by the number of other types of books remaining} printf ("% d \ n", k);} return 0 ;}