Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef pair<int ,string> is;
- typedef vector< int > vi;
- typedef long long ll;
- #define S second
- #define F first
- const int N = 1e6 + 6e1;
- int n ,a ,b ,c ,x ,y;
- vector< is > s ,tmpS;
- inline int getDec(char B64)
- {
- if( isdigit(B64) ) return (int)B64 - '0';
- if( 'a' <= B64 && B64 <= 'z' ) return (int)B64 - 'a' + 10;
- return (int)B64 - 'A' + 36;
- }
- inline string get64(int dc)
- {
- string r;
- while( dc )
- {
- int m = dc % 64;
- if( m < 10 )
- r += char(m + '0');
- else if( m < 36 )
- r += char(m - 10 + 'a');
- else
- r += char(m - 36 + 'A');
- dc /= 64;
- }
- return r;
- }
- void makeS()
- {
- ll num = a;
- string B64 = get64(a);
- s.resize(n);
- tmpS.resize(n);
- s[0] = is(num ,B64);
- for(int i = 1 ; i < n ; i++)
- {
- num = s[i - 1].F;
- num %= c;
- num *= 1ll * b;
- num %= c;
- num += a;
- num %= c;
- B64 = get64(num);
- s[i] = is(num ,B64);
- }
- }
- bool countSort(int d)
- {
- bool Res = false;
- vector< int > f(64 ,0);
- for(int i = 0 ; i < n; i++)
- {
- int dec = 0;
- if( d < (int)s[i].S.size() )
- {
- Res = true;
- dec = getDec(s[i].S[d]);
- }
- f[dec] += 1;
- tmpS[i] = s[i];
- }
- if( Res )
- {
- for(int i = 1 ; i < 64; i++)
- {
- f[i] += f[i - 1];
- }
- for(int i = n - 1 ; i >= 0 ; i--)
- {
- int dec = 0;
- if( d < (int)tmpS[i].S.size() )
- dec = getDec(tmpS[i].S[d]);
- s[ f[dec] - 1 ] = tmpS[i];
- f[dec] -= 1;
- }
- }
- return Res;
- }
- void RadixSort()
- {
- int d = 0;
- bool flag = true;
- while( flag )
- {
- flag = countSort(d);
- d += 1;
- }
- }
- ll getHash()
- {
- ll res = 0;
- for(is i : s)
- {
- res *= 1ll * x;
- res %= y;
- res += i.F;
- res %= y;
- }
- return res;
- }
- void Solve()
- {
- scanf("%d%d%d%d%d%d" ,&n ,&a ,&b ,&c ,&x ,&y);
- makeS();
- RadixSort();
- printf("%lld\n" ,getHash());
- }
- int main()
- {
- int Tc; scanf("%d" ,&Tc);
- while( Tc-- ) Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement