-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathADATOMAT.cpp
More file actions
92 lines (82 loc) · 2.47 KB
/
ADATOMAT.cpp
File metadata and controls
92 lines (82 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// main.cpp
// practice
//
// Created by Mahmud on 04/09/18.
// Copyright © 2018 Mahmud. All rights reserved.
//
/*
Radix sort snippet belongs to Extazy.
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
const int N = 20000005;
const unsigned POWER_OF_TWO = 16;
const unsigned BASE = 1u<<POWER_OF_TWO;
const unsigned NAIVE_SORT_BOUND = 1u<<16;
int t, n, a, b, x;
int sz;
pair<int, int> arr[N];
namespace radix_sort {
pair < unsigned short, unsigned > *v1[BASE], *v2[BASE];
unsigned global_sz1[BASE],global_sz2[BASE],sz1[BASE],sz2[BASE];
void radix_sort() {
unsigned i,j,idx=0;
memset(sz1,0,sizeof(sz1));
memset(sz2,0,sizeof(sz2));
for(i=1;i<=sz;i++) {
++sz1[arr[i].first&(BASE-1)];
++sz2[arr[i].first>>POWER_OF_TWO];
}
for(i=0;i<BASE;i++) {
if(global_sz1[i]<sz1[i]) {
v1[i]=(pair < unsigned short, unsigned > *)malloc(sz1[i]*sizeof(pair < unsigned short, unsigned >));
global_sz1[i]=sz1[i];
}
if(global_sz2[i]<sz2[i]) {
v2[i]=(pair < unsigned short, unsigned > *)malloc(sz2[i]*sizeof(pair < unsigned short, unsigned >));
global_sz2[i]=sz2[i];
}
}
memset(sz1,0,sizeof(sz1));
memset(sz2,0,sizeof(sz2));
for(i=1;i<=sz;i++) {
v1[arr[i].first&(BASE-1)][sz1[arr[i].first&(BASE-1)]++]=(make_pair(arr[i].first>>POWER_OF_TWO,arr[i].second));
}
for(i=0;i<BASE;i++) {
for(j=0;j<sz1[i];j++) {
v2[v1[i][j].first][sz2[v1[i][j].first]++]=make_pair(i,v1[i][j].second);
}
}
for(i=0;i<BASE;i++) {
for(j=0;j<sz2[i];j++) {
arr[++idx]=make_pair(((i<<POWER_OF_TWO)|v2[i][j].first),v2[i][j].second);
}
}
}
}
int main() {
cin >> t;
while (t --) {
cin >> n >> a >> b >> x;
sz = n;
arr[1].first = x;
for (int i = 2; i <= n; i ++) {
arr[i].first = (1LL * arr[i - 1].first * a + b) % 1000000007;
}
radix_sort::radix_sort();
int result = 0;
for (int i = 1; i <= n; i ++) {
result = (result + 1LL * i * arr[i].first % 1000000007);
if (result >= 1000000007) result -= 1000000007;
}
cout << result << endl;
}
return 0;
}