The Best Gift

杰克计划为艾米莉购买两本不同类型的书籍作为生日礼物。本文章介绍了一个算法问题,探讨如何从多种类型的书籍中选择不同的两种进行组合,旨在找出所有可能的选择方案。

Description

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.

Input

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.

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 Input

Input
4 3
2 1 3 1
Output
5
Input
7 4
4 2 3 1 2 4 3
Output
18

Hint

The answer to the first test sample equals 5 as Sasha can choose:

  1. the first and second books,
  2. the first and third books,
  3. the first and fourth books,
  4. the second and third books,
  5. the third and fourth books.


#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int a[200010];
int main()
{
    long n;
    int m;
    scanf("%ld%d",&n,&m);
    long long sum=0;
    int num;
    memset(a,0,sizeof(a));
    for(long i=1;i<=n;i++)
        {
            cin>>num;
            sum+=i-1-a[num];//与之前的书都能满足条件;
            a[num]++;//书的种类加加;
        }
    printf("%lld\n",sum);
    return 0;
}
As we all know the fact, you can use several Source Words to compose an Interesting Word. Little Gyro has already learnt this fantastic idea from one of his best friends Brother Yu. On Little Gryo's birthday, fortunately, Little Gryo received a gift from Brother Yu. The gift consists of n Interesting Words S 1 ​ ,S 2 ​ ,...,S n ​ . Little Gyro also thought those words were really fantastic. So he decided to play with these words. For each Interesting Word S i ​ , Little Gyro will select a period of consecutive letters P i ​ , and splice into a new word T within the given order, and then he defined these kind of words "Fascinating Words". It means you can take apart a Fascinating Word T and form n period of consecutive letters P 1 ​ +P 2 ​ +...+P n ​ from the given Interesting Words. Specially, if Little Gyro considers the Interesting Word really useless, he'll not choose any letter from this Interesting Word either. For example, supposed that there are some Interesting Words: S 1 ​ ="telephone", S 2 ​ ="microscope". Little Gyro may select a period of consecutive letters from each given word such as: P 1 ​ ="tele", P 2 ​ ="scope". And then form the Fascinating Word T=P 1 ​ +P 2 ​ ="telescope". Specially, Little Gyro also can only select P 2 ​ ="scope" and form the Fascinating Word T=P 1 ​ +P 2 ​ ="scope", if he dislikes the first Interesting Word. Now given all the Interesting Words that Little Gyro has received on his birthday, Little Gyro wants to know the total number of the different Fascinating Words that he can generate. Input Specification: Each input file only contains one test case. The first line contains an integer n (1 ≤ n ≤ 2000), indicating the number of the Interesting Words. Then, the following n lines, each line contains an Interesting Word S i ​ (1 ≤ ∑ i=1 n ​ ∣S i ​ ∣ ≤ 5×10 4 ). It's guaranteed that the Interesting Words can only be made up by the lowercase letters, and the sum of the length of the Interesting Words S i ​ of all test cases will not exceed 5×10 4 . Output Specification: For each test case, output the number of the different Fascinating Words that Little Gyro can generate. Because the number may be very large, just output the number mod 10 9 +7. Sample Input 1: 2 aa ab Sample Output 1: 8 Sample Input 2: 3 abb aca aba Sample Output 2: 139 Hint: For the first sample, Little Gyro can generate 8 different Fascinating Words in total, including "a", "b", "aa", "ab", "aaa", "aab", "aaab" and the empty word.
10-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值