Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 224 Accepted Submission(s): 117
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.
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
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
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
题意是给出一个长度为m的数组,按一定的规则生成一个长度为n的数组,然后找出一个严格递增的序列的数目(可以只有一个数);
思路:先把数组b离散化排序,然后从1~n找出在大小比c[i]小且以它为尾数的序列的长度,注意只有c[i]的序列也可以;很好的题
Case #2: 13
题意是给出一个长度为m的数组,按一定的规则生成一个长度为n的数组,然后找出一个严格递增的序列的数目(可以只有一个数);
思路:先把数组b离散化排序,然后从1~n找出在大小比c[i]小且以它为尾数的序列的长度,注意只有c[i]的序列也可以;很好的题
代码:
1 #include <cstdlib>
2 #include <iostream>
3 #include <algorithm>
4 #define lowbit(x) x&(-x)
5 using namespace std;
6
7 const int maxn = 500050;
8 const __int64 mod =1000000007;
9 __int64 a[maxn],b[maxn],c[maxn],e[maxn];
10 int k;
11 int find(__int64 key)
12 {
13 int l=1,r=k;
14 while(l<=r)
15 {
16 int mid = (l+r)>>1;
17 if(b[mid]==key) return mid;
18 if(b[mid]<key)
19 l=mid+1;
20 else
21 r=mid-1;
22 }
23 }
24
25 void insert(int p,int d)
26 {
27 for(int i=p;i<=k;i+=lowbit(i))
28 {
29 e[i]+=d;
30 e[i]%=mod;
31 }
32 }
33
34 __int64 query(int p)
35 {
36 __int64 sum=0;
37 for(int i=p;i>0;i-=lowbit(i))
38 {
39 sum+=e[i];
40 sum%=mod;
41 }
42 return sum;
43 }
44 int main(int argc, char *argv[])
45 {
46 int t,cas=1;
47 __int64 n,m,x,y,z;
48 scanf("%d",&t);
49 while(t--)
50 {
51 scanf("%I64d%I64d%I64d%I64d%I64d",&n,&m,&x,&y,&z);
52 for(int i=0;i<m;i++)
53 scanf("%I64d",&a[i]);
54 for(int i=0;i<=n-1;i++)
55 {
56 b[i+1]=a[i%m];
57 c[i+1]=b[i+1];
58 a[i%m]=(x*a[i%m]+y*(i+1))%z;
59 }
60 sort(b+1,b+1+n);
61 // for(int i=1;i<=n;i++)
62 // printf("%I64d ",c[i]);
63 k = 1;
64 for(int i=2;i<=n;i++)
65 if(b[i]!=b[i-1]) b[++k]=b[i];
66 memset(e,0,sizeof(e));
67 __int64 sum= 0 ;
68 for(int i=1;i<=n;i++)
69 {
70 int ans = find(c[i]);//查询c[i]所在的位置
71 int num=query(ans-1);//查询在ans这个位置之前且比c[i]小的组合数
72 num%=mod;
73 sum+=num+1;//"1"单独以c[i]打头
74 sum%=mod;
75 insert(ans,num+1);
76 }
77 printf("Case #%d: %I64d\n",cas++,sum);
78 }
79 system("PAUSE");
80 return EXIT_SUCCESS;
81 }