正题
nowcoder 1103A
题目大意
有一个数组,将其复制k遍,求所有区间内不同元素个数的和
解题思路
同一个区间内有相同的数那么在最右边的数计算贡献
记 rrr 为右边第一个相等的数,那么贡献就是到 rrr 的距离乘上左边的数字个数(有哪些区间可以让这个数计算贡献)
code
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define wyc 1000000007
#define N 100100
using namespace std;
ll n,k,m,gg,ans,a[N],b[N],L[N],rs[N];
ll ksm(ll x,ll y)
{ll z=1;while(y){if(y&1)z=z*x%wyc;x=x*x%wyc;y>>=1;}return z;
}
int main()
{scanf("%lld%lld",&n,&k);for(ll i=1;i<=n;++i){scanf("%lld",&a[i]);b[i]=a[i];}sort(b+1,b+1+n);m=unique(b+1,b+1+n)-b-1;for(ll i=1;i<=n;++i)a[i]=lower_bound(b+1,b+1+m,a[i])-b;for(ll i=1;i<=m;++i)L[i]=n+1;for(ll i=n;i>0;--i){rs[i]=L[a[i]];L[a[i]]=i;}gg=ksm(2,wyc-2);for(ll i=1;i<=n;++i)ans=(ans+(i+n*(k-1)%wyc)%wyc*(rs[i]-i)%wyc)%wyc;for(ll i=1;i<=n;++i){if(rs[i]==n+1)rs[i]=n+L[a[i]];ans=(ans+(i+i+(k-2)*n%wyc)%wyc*(k-1)%wyc*gg%wyc*(rs[i]-i)%wyc)%wyc;}printf("%lld",ans);return 0;
}