Increasing Speed Limits

Increasing Speed Limits

Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 186 Accepted Submission(s): 86
 
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
 
 
Source
2009 Multi-University Training Contest 6 - Host by WHU
 
Recommend
gaojie
 
/*
题意:按照题目给出的循环条件:
    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
    求出一个长度为n的序列A,然后求数列A的严格递增的子序列的个数
    例如第一组样例,按照循环条件求出的序列为 1,2,1,2,3(不要怀疑,这就是按照循环来求出来的)
    递增子序列为:
    {1},{2},{1},{2},{3},
    {1,2},{1,2},{1,3},{2,3},{1,2},
    {1,3},{2,3},{1,2,3},{1,2,3},{1,2,3}.
    
初步思路:树状数组求递增子序列
*/

#include<bits/stdc++.h>
#define N 500010
#define mod 1000000007
#define lowbit(x) x&(-x)
using namespace std;
struct node
{
    int id;
    long long w;
    bool operator< (const node& b)const{
        return w<b.w;
    }
    
}s[N];
long long n,m,x,y,z;
long long t;
long long A[N];
long long mapn[N];///用于映射数组,标记元素的id
long long c[N];
int len =0;///用于标记去重后的序列长度
long long res=0;
void update(int x,int val){
    while(x<N){
        c[x]+=val;
        c[x]%=mod;
        x+=lowbit(x);
    }
}
long long getsum(int x){
    long long res=0;
    while(x>0){
        res+=c[x];
        res%=mod;
        x-=lowbit(x);
    }
    return res;
}
void init(){
    memset(c,0,sizeof c);
    res=0;
}
int main(){
    // freopen("in.txt","r",stdin);
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++){
        printf("Case #%d: ",ca);
        init();
        scanf("%lld%lld%lld%lld%lld",&n,&m,&x,&y,&z);
        for(int i=0;i<m;i++){
            scanf("%lld",&A[i]);
        }
        for(int i=0;i<n;i++){///因为树状数组0这位是处理不到的所以坐标整个向右平移以为
            s[i+1].w=A[i%m]+1;
            s[i+1].id=i+1;
            A[i%m]=(x*A[i%m]+y*(i+1))%z;
        }
        
        // for(int i=1;i<=n;i++){
            // cout<<s[i].w<<" ";
        // }cout<<endl;
        
        sort(s+1,s+n+1);///排好序然后用树状数组进行求最长上升子序列
        int cur=0;
        for(int i=1;i<=n;i++){
            if(s[i].w==s[i-1].w)
                mapn[s[i].id]=cur;
            else 
                mapn[s[i].id]=++cur;
        }
        
        // for(int i=1;i<=n;i++){
            // cout<<mapn[s[i]]<<" ";
        // }cout<<endl;
        
        long long ans=0;
        for(int i=1;i<=n;i++){
            update(mapn[i],getsum(mapn[i]-1)+1);
        }
        printf("%d\n",getsum(n));
    }
    return 0;
}

 

posted @ 2017-03-02 22:20  勿忘初心0924  阅读(242)  评论(0编辑  收藏  举报