J - Kleene Inversion AtCoder - 5165

Problem Statement

 

We have a sequence of N integers A~=~A0,~A1,~…,~AN−1.

Let B be a sequence of K×N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.

Find the inversion number of B, modulo 109+7.

Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0≤i<jK×N−1) such that Bi>Bj.

Constraints

 

  • All values in input are integers.
  • 1≤N≤2000
  • 1≤K≤109
  • 1≤Ai≤2000

Input

 

Input is given from Standard Input in the following format:

N K
A0 A1 … AN−1

Output

 

Print the inversion number of B, modulo 109+7.

Sample Input 1

 

2 2
2 1

Sample Output 1

 

3

In this case, B~=~2,~1,~2,~1. We have:

  • B0>B1
  • B0>B3
  • B2>B3

Thus, the inversion number of B is 3.

Sample Input 2

 

3 5
1 1 1

Sample Output 2

 

0

A may contain multiple occurrences of the same number.

Sample Input 3

 

10 998244353
10 9 8 7 5 6 3 4 2 1

Sample Output 3

 

185297239

Be sure to print the output modulo 109+7.

思路:题意清晰,求 k - 1到 1 的和,注意取模;

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const ll mod = 1e9+7;


int num[2000+10];
int a[2000+10];
int b[2000+10];

int main()
{
	ll n,k;
	cin>>n>>k;
	for(int i=0; i<n; i++)
		cin>>num[i];
	for(int i=0; i<n; i++)
	{
		int j=i+1;
		while(j < n) 
		{
			if(num[j] < num[i]) a[i]++;	
			j++;		
		}
		if(!b[num[i]])
			{
				for(int k=0; k<n; k++)
				{
					if(num[k] < num[i])
					b[num[i]]++;
				}
			}
	}
//	for(int i=0; i<n; i++) cout<<a[i]<<' '<<b[num[i]]<<endl;

	ll ans=0;
	ll sum=(k-1)*k/2;//放在下面的求ans语句中就会wa,求逆元也不行,遇到除法还是单独运算比较好.
	sum = sum % mod;
	for(int i=0; i<n; i++)
	{
//		ans += b[num[i]]*sum%mod;
//		ans += k*a[i]%mod;
		
		ans=(ans+k*a[i]%mod+b[num[i]]*sum%mod)%mod;
	}
	cout<<ans%mod;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值