-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConstant.cpp
More file actions
175 lines (152 loc) · 3.89 KB
/
Constant.cpp
File metadata and controls
175 lines (152 loc) · 3.89 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "Constant.h"
int DBGtest;
ll Constant::Clock::global_clock[101] = {0};
void Constant::Clock::print_clock(int id) {
DBGprint("clock: %d %f\n", id, global_clock[id] * 1.0 * microseconds::period::num / microseconds::period::den);
}
double Constant::Clock::get_clock(int id) {
return global_clock[id] * 1.0 * microseconds::period::num / microseconds::period::den;
}
void Constant::Util::int_to_char(char* &p, int u) {
*p++ = u & 0xff;
*p++ = u >> 8 & 0xff;
*p++ = u >> 16 & 0xff;
*p++ = u >> 24 & 0xff;
}
void Constant::Util::ll_to_char(char *&p, ll u) {
*p++ = u & 0xff;
*p++ = u >> 8 & 0xff;
*p++ = u >> 16 & 0xff;
*p++ = u >> 24 & 0xff;
*p++ = u >> 32 & 0xff;
*p++ = u >> 40 & 0xff;
*p++ = u >> 48 & 0xff;
*p++ = u >> 56 & 0xff;
}
int Constant::Util::char_to_int(char* &p) {
int ret = 0;
ret = *p++ & 0xff;
ret |= (*p++ & 0xff) << 8;
ret |= (*p++ & 0xff) << 16;
ret |= (*p++ & 0xff) << 24;
return ret;
}
ll Constant::Util::char_to_ll(char *&p) {
ll ret = 0;
ret = (ll)(*p++ & 0xff);
ret |= (ll)(*p++ & 0xff) << 8;
ret |= (ll)(*p++ & 0xff) << 16;
ret |= (ll)(*p++ & 0xff) << 24;
ret |= (ll)(*p++ & 0xff) << 32;
ret |= (ll)(*p++ & 0xff) << 40;
ret |= (ll)(*p++ & 0xff) << 48;
ret |= (ll)(*p++ & 0xff) << 56;
return ret;
}
void Constant::Util::int_to_header(char *p, int u) {
*p++ = u & 0xff;
*p++ = u >> 8 & 0xff;
*p++ = u >> 16 & 0xff;
*p++ = u >> 24 & 0xff;
}
int Constant::Util::header_to_int(char *p) {
int ret = 0;
ret = *p++ & 0xff;
ret |= (*p++ & 0xff) << 8;
ret |= (*p++ & 0xff) << 16;
ret |= (*p++ & 0xff) << 24;
return ret;
}
int Constant::Util::getint(char *&p) {
while (!isdigit(*p))
p++;
int ret = 0;
while (isdigit(*p)) {
ret = 10*ret + *p - '0';
p++;
}
return ret;
}
ll Constant::Util::getll(char *&p) {
while (!isdigit(*p))
p++;
ll ret = 0;
while (isdigit(*p)) {
ret = 10*ret + *p - '0';
p++;
}
return ret;
}
ll Constant::Util::randomlong() {
return rand() % MOD;
}
ll128 Constant::Util::get_residual(ll128 a) {
return (a % MOD + MOD) % MOD;
}
ll128 Constant::Util::get_sign(ll128 a) {
return a > MOD / 2 ? a - MOD : a;
}
ll128 Constant::Util::get_abs(ll128 a) {
return a > 0 ? a : -a;
}
ll128 Constant::Util::sqrt(ll128 a) {
return power(a, MOD + 1 >> 2);
}
ll128 Constant::Util::inverse(ll128 a, ll128 b) {
return power(a, b-2);
}
ll128 Constant::Util::power(ll128 a, ll128 b) {
ll128 ret = 1;
a = (a%MOD+MOD)%MOD;
b = (b%MOD+MOD)%MOD;
if (b == 0)
return 1;
while (b > 0) {
if (b&1) ret = ret * a;
ret = get_residual(ret);
a = a * a;
a = get_residual(a);
b >>= 1;
}
return ret;
}
ll128 Constant::Util::cal_perm(ll128 *key, int l, int k) {
if (!l)
return 1;
vector<ll128> a(l+1);
a[0] = 1;
for (int i = 0; i < M; i++)
if (i != k)
for (int j = l; j; j--) {
a[j] += a[j - 1] * key[i];
a[j] = get_residual(a[j]);
}
return a[l];
}
std::ostream&
operator<<( std::ostream& dest, __int128_t value )
{
std::ostream::sentry s( dest );
if ( s ) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[ 128 ];
char* d = std::end( buffer );
do
{
-- d;
*d = "0123456789"[ tmp % 10 ];
tmp /= 10;
} while ( tmp != 0 );
if ( value < 0 ) {
-- d;
*d = '-';
}
int len = std::end( buffer ) - d;
if ( dest.rdbuf()->sputn( d, len ) != len ) {
dest.setstate( std::ios_base::badbit );
}
}
return dest;
}
const ll Constant::inv2 = Constant::Util::inverse(2, MOD);
const ll Constant::inv2_m = Constant::Util::inverse(1 << DECIMAL_PLACES, MOD);