【Codeforces Round 326 (Div 2)D】【DP】Duff in Beach 数列重复取数最多k次使得单调不下降

D. Duff in Beach
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

While Duff was resting in the beach, she accidentally found a strange array b0, b1, ..., bl - 1 consisting of l positive integers. This array was strange because it was extremely long, but there was another (maybe shorter) array, a0, ..., an - 1 that b can be build from a with formula: bi = ai mod n where a mod b denoted the remainder of dividing a by b.

Duff is so curious, she wants to know the number of subsequences of b like bi1, bi2, ..., bix (0 ≤ i1 < i2 < ... < ix < l), such that:

  • 1 ≤ x ≤ k
  • For each 1 ≤ j ≤ x - 1
  • For each 1 ≤ j ≤ x - 1bij ≤ bij + 1. i.e this subsequence is non-decreasing.

Since this number can be very large, she want to know it modulo 109 + 7.

Duff is not a programmer, and Malek is unavailable at the moment. So she asked for your help. Please tell her this number.

Input

The first line of input contains three integers, n, l and k (1 ≤ n, kn × k ≤ 106 and 1 ≤ l ≤ 1018).

The second line contains n space separated integers, a0, a1, ..., an - 1 (1 ≤ ai ≤ 109 for each 0 ≤ i ≤ n - 1).

Output

Print the answer modulo 1 000 000 007 in one line.

Sample test(s)
input
3 5 3
5 9 1
output
10
input
5 10 3
1 2 3 4 5
output
25
Note

In the first sample case, . So all such sequences are:  and .

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1e6+10,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
map<int,int>mop;
map<int,int>::iterator it;
int n,k;LL l;
int a[N];
int s[2][N];
int sum[N];
void add(int &x,int y)
{
	x+=y;
	if(x>=Z)x-=Z;
}
int main()
{
	while(~scanf("%d%lld%d",&n,&l,&k))
	{
		//第一步:离散化
		mop.clear();
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			mop[a[i]]=0;
		}
		int top=0;
		for(it=mop.begin();it!=mop.end();++it)it->second=top++;
		for(int i=0;i<n;i++)a[i]=mop[a[i]];

		//第二步:求出取数个数为1时的答案
		int ans=l%Z;

		//第三部:枚举取数个数(>=2),并做状态转移。
		MS(s,0);
		for(int i=0;i<n;i++)++s[0][a[i]];
		for(int i=0;i<top;i++)s[0][i]+=s[0][i-1];
		MC(sum,s[0]);
		LL len=l/n;//len表示a[]总共产生的完整循环次数
		int rst=l%n;//rst表示构不成完整循环次数的余数
		int tak=min((LL)k,len);//求出我们所能在完整循环次数中得到的最大取数次数
		int u=0;
		int v=1;
		for(int tim=2;tim<=tak;tim++)
		{
			for(int j=0;j<top;j++)s[v][j]=0;
			for(int j=0;j<n;j++)add(s[v][a[j]],s[u][a[j]]);
			for(int j=1;j<top;j++)add(s[v][j],s[v][j-1]);
			if(tim<k)for(int j=0;j<top;j++)add(sum[j],s[v][j]);
			LL num=(len+1-tim)%Z;
			add(ans,num*s[v][top-1]%Z);
			u^=1;
			v^=1;
		}
		//什么时候会有尾数也作为至少2次取数的累加呢?
		//条件1:有尾数;条件2:k>=2,想要取至少2次;条件3:len>=1,能够取至少2次。
		if(rst&&len>=1&&k>=2)for(int j=0;j<rst;j++)add(ans,sum[a[j]]);
		printf("%d\n",ans);
	}
	return 0;
}
/*
【trick&&吐槽】
加油!难题可以慢慢来分析!

1,一个特判没写,导致没能AK,好伤心!
2,不要忘了只要加法会爆,都要取模
3,乘法之前也要取模防止一乘就爆


【题意】
给你一个长度为l([1,1e18])的数列b[],
这个数列是由长度为n的数列a[],在基于循环节的条件下形成的,即对于任意p∈[0,l),都有b[p]=a[p%n]。
我们想求出b[]的子序列(子序列指下标递增即可,不必须是连续的),长度在[1,k]范围。
有1<=n,k,n*k<=1e6
使得该子序列满足——
1,子序列的长度在[1,k]范围
2,b[p]=a[p%n],也就是说b[]是由a[]循环了一定次数构成的,最后一次可能不完全循环。
那么,我们所选定的b[],要满足:其相邻的下标属于相邻的循环次数。
更确切地说,比如说这个自序列的第i位是b[j],第i+1位是b[k],那么要有[j/n]+1=[k/n]
3,所选定的b[]是单调不下降的

【类型】
DP

【分析】
首先,我们考虑这个数列要如何构造。
每个相邻的取数,都取自a[]的循环。
于是我们发现,如果l是n的倍数,那其实题意可以转换为——
我们可以做[1,k]次取数,每次取数都在a[]中任意取一个数,
对于取数后构成的数列c[],要有c[i]<=c[i+1]对于整个数列都成立。

于是我们想到对整个数列做离散化。
f[i][j]表示对于这个数列,我们现在已经取了i个数,最后一个数是第j大的条件下,取数方案是多少
s[i][j]表示对于这个数列,我们现在已经取了i个数,最后一个数是前j大的条件下,取数方案是多少
那么——
f[i][j]+=s[i-1][j];
s[i][j]=∑s[i][1~j]
我们求出这个数列的最大完成循环节数len,
如果这取数都是在len中实现的话,答案累计进去(len+1-i)*s[i][top],
如果这取数,只有i个在len中实现,最后一个数是取自不完整循环中的话,我们把它累计进一个前缀数组即可。
然后再枚举不完整循环中的每个数作为最后一次取数的值,把前导引为这个前缀数组,更新答案即可。

【时间复杂度&&优化】
这个DP,我们最多取k次数,每次取数考虑n个,于是总的时间复杂度为O(nk)。

【数据】
input
8 1001000
100 99 98 97 96 ... 3 2 1
output
8
*/




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值