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<j≤K×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;让你把这一串数字复制k次,求出来每个数的右边有多少个比他小的数,并且输出总和;
思路;
遍历最初的数组(),找出来每个数右边有几个比他小的数,求出总数cot;然后再找出来每个数左边比它小的数有几个,求出总数cnt;根据数学推导:复制k次后,cot会计算k*(1+k)/2次;cnt会计算(1-k)*k/2次;
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int mod=1e9+7;
int a[20010];
int main()
{
ll n,k;
scanf("%lld %lld",&n,&k);
ll cot=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j]) cot++;
}
}
ll cnt=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<i;j++)
{
if(a[j]<a[i]) cnt++;
}
}
ll sum=0;
ll kk=k*(k+1)/2;
if(kk==1) cout <<cot;
else
{
ll kkk=(k-1)*(k)/2;
sum=cot%mod*(kk%mod)%mod+cnt%mod*(kkk%mod)%mod;
cout <<sum%mod<<endl;
}
return 0;
}