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

本文主要是介绍【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
*/




这篇关于【Codeforces Round 326 (Div 2)D】【DP】Duff in Beach 数列重复取数最多k次使得单调不下降的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/610495

相关文章

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm

hdu4865(概率DP)

题意:已知前一天和今天的天气概率,某天的天气概率和叶子的潮湿程度的概率,n天叶子的湿度,求n天最有可能的天气情况。 思路:概率DP,dp[i][j]表示第i天天气为j的概率,状态转移如下:dp[i][j] = max(dp[i][j, dp[i-1][k]*table2[k][j]*table1[j][col] )  代码如下: #include <stdio.h>#include

poj2406(连续重复子串)

题意:判断串s是不是str^n,求str的最大长度。 解题思路:kmp可解,后缀数组的倍增算法超时。next[i]表示在第i位匹配失败后,自动跳转到next[i],所以1到next[n]这个串 等于 n-next[n]+1到n这个串。 代码如下; #include<iostream>#include<algorithm>#include<stdio.h>#include<math.

poj3261(可重复k次的最长子串)

题意:可重复k次的最长子串 解题思路:求所有区间[x,x+k-1]中的最小值的最大值。求sa时间复杂度Nlog(N),求最值时间复杂度N*N,但实际复杂度很低。题目数据也比较水,不然估计过不了。 代码入下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring

usaco 1.1 Broken Necklace(DP)

直接上代码 接触的第一道dp ps.大概的思路就是 先从左往右用一个数组在每个点记下蓝或黑的个数 再从右到左算一遍 最后取出最大的即可 核心语句在于: 如果 str[i] = 'r'  ,   rl[i]=rl[i-1]+1, bl[i]=0 如果 str[i] = 'b' ,  bl[i]=bl[i-1]+1, rl[i]=0 如果 str[i] = 'w',  bl[i]=b

uva 10154 DP 叠乌龟

题意: 给你几只乌龟,每只乌龟有自身的重量和力量。 每只乌龟的力量可以承受自身体重和在其上的几只乌龟的体重和内。 问最多能叠放几只乌龟。 解析: 先将乌龟按力量从小到大排列。 然后dp的时候从前往后叠,状态转移方程: dp[i][j] = dp[i - 1][j];if (dp[i - 1][j - 1] != inf && dp[i - 1][j - 1] <= t[i]

uva 10118 dP

题意: 给4列篮子,每次从某一列开始无放回拿蜡烛放入篮子里,并且篮子最多只能放5支蜡烛,数字代表蜡烛的颜色。 当拿出当前颜色的蜡烛在篮子里存在时,猪脚可以把蜡烛带回家。 问最多拿多少只蜡烛。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cs

uva 10069 DP + 大数加法

代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <cl

uva 10029 HASH + DP

题意: 给一个字典,里面有好多单词。单词可以由增加、删除、变换,变成另一个单词,问能变换的最长单词长度。 解析: HASH+dp 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc