HDU 3030 - Increasing Speed Limits

Problem Description

You were driving along a highway when you got caught by the road police for speeding. It turns out that they\‘ve been following you, and they were amazed by the fact that you were accelerating the whole time without using the brakes! And now you desperately need an excuse to explain that.

You‘ve decided that it would be reasonable to say "all the speed limit signs I saw were in increasing order, that\‘s why I‘ve been accelerating". The police officer laughs in reply, and tells you all the signs that are placed along the segment of highway you drove, and says that‘s unlikely that you were so lucky just to see some part of these signs that were in increasing order.

Now you need to estimate that likelihood, or, in other words, find out how many different subsequences of the given sequence are strictly increasing. The empty subsequence does not count since that would imply you didn‘t look at any speed limits signs at all!

For example, (1, 2, 5) is an increasing subsequence of (1, 4, 2, 3, 5, 5), and we count it twice because there are two ways to select (1, 2, 5) from the list.

Input

The first line of input gives the number of cases, N. N test cases follow. The first line of each case contains n, m, X, Y and Z each separated by a space. n will be the length of the sequence of speed limits. m will be the length of the generating array A. The next m lines will contain the m elements of A, one integer per line (from A[0] to A[m-1]).

Using A, X, Y and Z, the following pseudocode will print the speed limit sequence in order. mod indicates the remainder operation.

for i = 0 to n-1
print A[i mod m]
A[i mod m] = (X * A[i mod m] + Y * (i + 1)) mod Z

Note: The way that the input is generated has nothing to do with the intended solution and exists solely to keep the size of the input files low.

1 ≤ m ≤ n ≤ 500 000

Output

For each test case you should output one line containing "Case #T: S" (quotes for clarity) where T is the number of the test case and S is the number of non-empty increasing subsequences mod 1 000 000 007.

Sample Input

2
5 5 0 0 5
1
2
1
2
3
6 2 2 1000000000 6
1
2

Sample Output

Case #1: 15 Case #2: 13

大致题意:

  求上升子序列的个数

  序列怎么出来的呢,好难懂:

    for i = 0 to n-1
    print A[i mod m]
    A[i mod m] = (X * A[i mod m] + Y * (i + 1)) mod Z

  取m=3为例 输入完a[] 以后,a[]不是序列 要按照他的循环 打印 a[0] ,a[1], a[2],a[0],a[1],a[2]....如此,每打印一个做一次A第三行的变换,最后就是0 -> n-1 的序列了。

解题思路:

  树状数组+离散化。

  动规求法: dp[i]=∑dp[j](j<i&&ans[j]<ans[i])

  依据树状数组快速统计可加类区间数据的应用,

  可转化成 dp[i]=sum(f[i]-1)+1 ;即以前f[i]-1个数据为底的个数再加上自身。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 #define N  500005
 6 #define mod 1000000007
 7 long long c[N],a[N],b[N],f[N],T,n,m,x,y,z,ans,size;
 8 void modify(int x,int num){while(x<=n)c[x]+=num,c[x]%=mod,x+=x&-x;}
 9 long long sum(int x){int s=0;while(x>0)s+=c[x],s%=mod,x-=x&-x;return s;}
10 void ini(){
11     memset(c,0,sizeof(c));
12     ans=0;
13     scanf("%d%d%lld%lld%lld",&n,&m,&x,&y,&z);
14     for(int i=0;i<m;i++) scanf("%lld",&a[i]);
15     for(int i=0;i<n;i++){
16         f[i]=b[i+1]=a[i%m];
17         a[i%m]=(x*a[i%m]+y*(i+1))%z;
18     }
19 }
20 int main(){
21     scanf("%d",&T);
22     for(int K=1;K<=T;K++)
23     {
24         ini();
25         sort(b+1,b+n+1);//离散化
26         size=unique(b+1,b+n+1)-(b+1);
27         for(int i=0;i<n;i++){
28             int p=lower_bound(b+1,b+size+1,f[i])-b;
29             long long tot=sum(p-1)+1;
30             ans+=tot;
31             ans%=mod;
32             modify(p,tot);
33         } printf("Case #%d: %lld\n",K,ans);
34     } return 0;
35 }
时间: 2024-11-05 12:16:51

HDU 3030 - Increasing Speed Limits的相关文章

hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 481    Accepted Submission(s): 245 Problem Description You were driving along a highway when you got caught by the road p

HDU FatMouse&#39;s Speed (LIS)

FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9871    Accepted Submission(s): 4374Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster it

hdu 3030

这道题主要就是问你,长度为n的序列,有多少种上升的子序列 当前点的情况种数等于前面所有小于它的点的种数相加 + 1 1就是只有这一个点的时候的序列 那就是要多次查询前面比它小的点的种数的和 那么就是区间求和 用到树状数组就过了 一开始我用的a[k]表示这个点的值等于k时有多少种情况,但是后来考虑到对 输入的值没有限制 有可能这个点的值等于 100000000 那我就要建这么大的数组 明显过不去 并且我们只要直到这个点前面的种数和就行 排序后查找就行了 #include <iostream> #

HDU 专题分类

[背包问题] 2602 Bone Collector 1114 Piggy-Bank 1203 I NEED A OFFER! 1171 Big Event in HDU 1059 Dividing 2844 Coins 2191 悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 2159 FATE 1561 The more, The Better 1011 Starship Troopers 2639 Bone Collector II 3033 I love sneakers! 2955

POJ 3653 &amp; ZOJ 2935 &amp; HDU 2722 Here We Go(relians) Again(最短路dijstra)

题目链接: PKU:http://poj.org/problem?id=3653 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1934 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2722 Description The Gorelians are a warlike race that travel the universe conquering new world

hdu 2722 Here We Go(relians) Again (最短路径)

Here We Go(relians) Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 685    Accepted Submission(s): 335 Problem Description The Gorelians are a warlike race that travel the universe conquer

POJ 3653 &amp;amp; ZOJ 2935 &amp;amp; HDU 2722 Here We Go(relians) Again(最短路dijstra)

题目链接: PKU:http://poj.org/problem? id=3653 ZJU:problemId=1934" target="_blank">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1934 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=2722 Description The Gorelians are a warlike rac

HDU 2722 Here We Go(relians) Again (spfa)

Here We Go(relians) Again Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 1   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description The Gorelians

poj 2501 Average Speed

Average Speed Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4842   Accepted: 2168 Description You have bought a car in order to drive from Waterloo to a big city. The odometer on their car is broken, so you cannot measure distance. But