Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- typedef pair<ll , string> ls;
- #define S second
- #define F first
- const int N = 1e6 + 6e1;
- const int M = 64;
- unordered_map<ll , string> dp;
- int n ,cnt1[M] ,cnt2[M];
- ll A ,B ,C ,X ,Y;
- ls s1[N] ,s2[N];
- inline int _10(char B64)
- {
- if( isdigit(B64) ) return int(B64 - '0'); // 10
- if( isupper(B64) ) return int(B64 - 'A' + 10);// 26
- if( islower(B64) ) return int(B64 - 'a' + 36);// 26
- if( B64 == '{' ) return 62;
- return 63;
- }
- inline string _64(ll x)
- {
- if( dp.find(x) != dp.end() ) return dp[x];
- string r;
- ll tmpX = x;
- do
- {
- char c;
- int m = tmpX % 64;
- if( m < 10 ) c = char(m + '0');
- else if( m < 36 ) c = char(m - 10 + 'A');
- else if( m < 62 ) c = char(m - 36 + 'a');
- else if( m == 62 ) c = '{';
- else c = '|';
- r += c;
- tmpX /= 64;
- }
- while( tmpX > 0 );
- return dp[x] = r;
- }
- int main()
- {
- /// - Require BigO <= O(200000000) == 2s - ///
- /// ---- Cur BigO = O(420005760) > 2s ---- ///
- int Tc;
- scanf("%d" ,&Tc);
- while( Tc-- )
- {
- /// --- Input --- ///
- scanf("%d" ,&n);
- scanf("%lld%lld%lld" ,&A ,&B ,&C);
- scanf("%lld%lld" ,&X ,&Y);
- /// --- Make S[N] --- ///
- s1[0].F = A;
- s1[0].S = _64(A);
- for(int i = 1 ; i < n ; i++)
- {
- s1[i].F = s1[i - 1].F * B;
- s1[i].F %= C;
- s1[i].F += A;
- s1[i].F %= C;
- s1[i].S = _64(s1[i].F);
- }
- /// --- Radix Sort --- ///
- for(int d = 0 ; d < 5 ; d++)
- {
- if( d % 2 )
- {
- for(int i = 0 ; i < n ; i++)
- {
- int c = (d < (int)s2[i].S.size()) ? _10(s2[i].S[d]) : 0;
- cnt2[c] ++;
- }
- cnt1[0] = 0;
- for(int c = 1 ; c < 64 ; c++)
- {
- cnt2[c] += cnt2[c - 1];
- cnt1[c] = 0;
- }
- for(int i = n - 1 ; i >= 0 ; i--)
- {
- int c = (d < (int)s2[i].S.size()) ? _10(s2[i].S[d]) : 0;
- s1[ cnt2[c] - 1 ] = s2[i];
- cnt2[c] --;
- }
- }
- else
- {
- for(int i = 0 ; i < n ; i++)
- {
- int c = (d < (int)s1[i].S.size()) ? _10(s1[i].S[d]) : 0;
- cnt1[c] ++;
- }
- cnt2[0] = 0;
- for(int c = 1 ; c < 64 ; c++)
- {
- cnt1[c] += cnt1[c - 1];
- cnt2[c] = 0;
- }
- for(int i = n - 1 ; i >= 0 ; i--)
- {
- int c = (d < (int)s1[i].S.size()) ? _10(s1[i].S[d]) : 0;
- s2[ cnt1[c] - 1 ] = s1[i];
- cnt1[c] --;
- }
- }
- }
- /// --- Get Hash Value --- ///
- ll V = 0;
- for(int i = 0 ; i < max(n ,64) ; i++)
- {
- if( i < n )
- {
- V *= X;
- V %= Y;
- V += s2[i].F;
- V %= Y;
- }
- if( i < 64 )
- {
- cnt1[i] = 0;
- cnt2[i] = 0;
- }
- }
- /// --- Output --- ///
- printf("%lld\n" ,V);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment