Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// --- 31623 = O(181960626) --- ///
- /// --- Best Base is: Base31623 for (2 digit for 1e9) --- ///
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e6 + 6e1;
- const int M = 31623;
- int n ,cnt1[M] ,cnt2[M];
- int s1[N][3] ,s2[N][3] ,R[N];
- int a ,b ,c ,x ,y;
- bool vs[N];
- int main()
- {
- int Tc;
- scanf("%d" ,&Tc);
- while( Tc-- )
- {
- scanf("%d%d%d%d%d%d" ,&n ,&a ,&b ,&c ,&x ,&y);
- s1[0][0] = a;
- s1[0][1] = a % M;
- s1[0][2] = a / M;
- int L1 = s1[0][1] ,R1 = s1[0][1];
- int L2 = s1[0][2] ,R2 = s1[0][2];
- for(int i = 1 ; i < n ; i++)
- {
- s1[i][0] = ((1ll * s1[i - 1][0] % c * b % c) % c + a % c) % c;
- s1[i][1] = s1[i][0] % M;
- s1[i][2] = s1[i][0] / M;
- L1 = min(L1 ,s1[i][1]);
- R1 = max(R1 ,s1[i][1]);
- L2 = min(L2 ,s1[i][2]);
- R2 = max(R2 ,s1[i][2]);
- }
- int K1 = R1 - L1 + 1;
- int K2 = R2 - L2 + 1;
- int K = max(K1 , K2);
- /// ----------------------------------------------- ///
- for(int i = 0 ; i < n ; i++)
- {
- cnt1[ s1[i][1] - L1 ] ++;
- cnt2[ s1[i][2] - L2 ] ++;
- }
- for(int i = 1 ; i < K ; i++)
- {
- if(i < K1) cnt1[i] += cnt1[i - 1];
- if(i < K2) cnt2[i] += cnt2[i - 1];
- }
- int j = n - 1;
- for(int i = n - 1 ; i >= 0 ; i--)
- {
- s2[ cnt1[ s1[i][1] - L1 ] - 1 ][0] = s1[i][0];
- s2[ cnt1[ s1[i][1] - L1 ] - 1 ][1] = s1[i][1];
- s2[ cnt1[ s1[i][1] - L1 ] - 1 ][2] = s1[i][2];
- vs[ cnt1[ s1[i][1] - L1 ] - 1 ] = 1;
- cnt1[ s1[i][1] - L1 ] --;
- while( vs[j] )
- {
- R[ cnt2[ s2[j][2] - L2 ] - 1 ] = s2[j][0];
- cnt2[ s2[j][2] - L2 ] --;
- j--;
- }
- }
- /// ----------------------------------------------- ///
- int v = 0;
- for(int i = 0 ; i < max(n ,K) ; i++)
- {
- if( i < n )
- {
- v = ((1ll * v % y * x % y) % y + R[i] % y) % y;
- vs[i] = 0;
- }
- if( i < K1 ) cnt1[i] = 0;
- if( i < K2 ) cnt2[i] = 0;
- }
- printf("%d\n" ,v);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement