CF# Educational Codeforces Round 3 B. The Best Gift
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 are n books on sale from one of m genres.
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 1 to m.
The first line contains two positive integers n and m (2 ≤ n ≤ 2·105, 2 ≤ m ≤ 10) — the number of books in the bookstore and the number of genres.
The second line contains a sequence a1, a2, ..., an, where ai (1 ≤ ai ≤ m) equals the genre of the i-th book.
It is guaranteed that for each genre there is at least one book of that genre.
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.
4 3
2 1 3 1
5
7 4
4 2 3 1 2 4 3
18
The answer to the first test sample equals 5 as Sasha can choose:
- the first and second books,
- the first and third books,
- the first and fourth books,
- the second and third books,
- the third and fourth books.
题意:有n本书,m个种类。
问选择两个种类有多少种方法。
分析:排列组合。
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define MIT (2147483647) 21 #define INF (1000000001) 22 #define MLL (1000000000000000001LL) 23 #define sz(x) ((int) (x).size()) 24 #define clr(x, y) memset(x, y, sizeof(x)) 25 #define puf push_front 26 #define pub push_back 27 #define pof pop_front 28 #define pob pop_back 29 #define mk make_pair 30 31 inline int Getint() 32 { 33 int Ret = 0; 34 char Ch = ' '; 35 bool Flag = 0; 36 while(!(Ch >= '0' && Ch <= '9')) 37 { 38 if(Ch == '-') Flag ^= 1; 39 Ch = getchar(); 40 } 41 while(Ch >= '0' && Ch <= '9') 42 { 43 Ret = Ret * 10 + Ch - '0'; 44 Ch = getchar(); 45 } 46 return Flag ? -Ret : Ret; 47 } 48 49 const int N = 200010, M = 15; 50 int n, m; 51 int cnt[M]; 52 53 inline void Input() 54 { 55 scanf("%d%d", &n, &m); 56 for(int i = 0; i < n; i++) 57 { 58 int x; 59 scanf("%d", &x); 60 cnt[x]++; 61 } 62 } 63 64 inline void Solve() 65 { 66 int ans = 0; 67 for(int i = 1; i <= m; i++) 68 for(int j = i + 1; j <= m; j++) 69 ans += cnt[i] * cnt[j]; 70 cout << ans << endl; 71 } 72 73 int main() 74 { 75 freopen("a.in", "r", stdin); 76 Input(); 77 Solve(); 78 return 0; 79 }