-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path758.cpp
More file actions
70 lines (55 loc) · 1.36 KB
/
Copy path758.cpp
File metadata and controls
70 lines (55 loc) · 1.36 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
#include<iostream>
#include<cstring>
using namespace std;
void print(int* arr, int left, int right) {
for (int i = left; i < right; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int caseNum;
cin >> caseNum;
//cout << sizeof(int) * 10000000 / 1024 << endl;
for (int c = 0; c < caseNum; c++) {
int n, mod;
cin >> n >> mod;
int* arr = new int[n + 1];
arr[0] = 0;
arr[1] = 1;
int max = 0;
for (int i = 2; i <= n; i++) {
arr[i] = (arr[i - 2] % mod + arr[i - 1] % mod) % mod;
max = max > arr[i] ? max : arr[i];
}
int rad = 1024;
int power[5] = { 0, 10, 20, 30, 40 };
for (int digit = 0; max / (1 << power[digit]) > 0; digit++) {
int* radix = new int[rad];
int* tempArr = new int[n+1];
memset(radix, 0, sizeof(int)*rad);
for (int i = 1; i <= n; i++) {
radix[(arr[i] >> power[digit]) & (rad-1)]++;
}
for (int d = 1; d < rad; d++) {
radix[d] += radix[d - 1];
}
for (int i = n; i > 0; i--) {
tempArr[radix[(arr[i] >> power[digit]) & (rad-1)]--] = arr[i];
}
memcpy(arr, tempArr, sizeof(int)*(n+1));
//print(arr, 1, n + 1);
delete[] radix;
delete[] tempArr;
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
long long temp = arr[i] % mod;
ans = (ans % mod + (temp * (i % mod)) % mod) % mod;
}
cout << ans << endl;
delete[] arr;
}
system("pause");
return 0;
}