Cattle guest CSP-S improve the training camp before the group 4 A repeat array

Portal

Although the time to write the exam, but afterwards will depend on the solution to a problem mend the wailing!

Read the explanations really feel daily dementia.

Each different requirements and the number of interval number.

May wish to change my thinking, consider a number x, which is present in the interval would have to contribute 1 of which range.

There is a number in which the interval is not very good demand, because there may be a range of an x, two x ......

So then convert it, find no interval of x, and finally the total minus.

For an x, its existence position p1, p2 ...... pk.

Interval is the interval between each two x does not exist.

Provided spaced apart a distance L, then the number of the middle section is (L + 1) * L / 2.

k-array is true.

But we have to consider is generated between the array of the array of k, L = n-pk + p1-1, by k-1 times.

L = p1 - 1 and L = n - pk both cases calculated once instead of k times (at both ends)

Then modulo carefully.

#include<bits/stdc++.h>
#define LL long long
#define N 1000003
#define mod 1000000007 
#define re register
using namespace std;
LL read()
{
    LL x=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    return x*f;
}
LL n,K;
LL inv=500000004;
LL a[N],b[N];
vector<LL>V[N];
LL f(LL x)
{
    x%=mod;//!!!这一步!!! 
    return (((x+1)*x)%mod*inv)%mod;
}
int main()
{
    n=read();K=read();
    for(int i=1;i<=n;++i)b[i]=a[i]=read();
    sort(b+1,b+1+n);
    LL num=unique(b+1,b+1+n)-(b+1);
    for(int i=1;i<=n;++i)
    {
        a[i]=lower_bound(b+1,b+1+num,a[i])-b;
        V[a[i]].push_back(i);
    }
    LL ans=0;
    for(int i=1;i<=num;++i)
    {
        LL cnt=0;
        cnt+=f(V[i][0]-1);
        cnt%=mod;
        cnt+=f(n-V[i][V[i].size()-1]);
        cnt%=mod;
        LL sum=0;
        for(int j=1;j<V[i].size();++j)
          sum+=f(V[i][j]-V[i][j-1]-1),sum%=mod;
        sum=sum*K%mod;
        cnt=(sum+cnt)%mod;
        cnt+=((K-1)*f(n-V[i][V[i].size()-1]+V[i][0]-1))%mod;
        cnt%=mod;
        ans+=(f(n*K)-cnt+mod)%mod;
        ans%=mod;
    }
    printf("%lld\n",ans);
} 
/*
*/
View Code

 

Guess you like

Origin www.cnblogs.com/yyys-/p/11808534.html