-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathint128.cpp
More file actions
139 lines (127 loc) · 2.85 KB
/
Copy pathint128.cpp
File metadata and controls
139 lines (127 loc) · 2.85 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
//
// 128 ビット整数 __int128 型のラッパー
//
// verified
// Yosupo Library Checker - Sum of Floor of Linear
// https://judge.yosupo.jp/problem/sum_of_floor_of_linear
//
#include <bits/stdc++.h>
using namespace std;
// int 128
using i128 = __int128;
i128 to_integer(const string &s) {
i128 res = 0;
for (auto c : s) {
if (isdigit(c)) res = res * 10 + (c - '0');
}
if (s[0] == '-') res *= -1;
return res;
}
istream& operator >> (istream &is, i128 &x) {
string s;
is >> s;
x = to_integer(s);
return is;
}
ostream& operator << (ostream &os, const i128 &x) {
i128 ax = (x >= 0 ? x : -x);
char buffer[128];
char *d = end(buffer);
do {
--d;
*d = "0123456789"[ax % 10];
ax /= 10;
} while (ax != 0);
if (x < 0) {
--d;
*d = '-';
}
int len = end(buffer) - d;
if (os.rdbuf()->sputn(d, len) != len) {
os.setstate(ios_base::badbit);
}
return os;
}
i128 gcd(i128 a, i128 b) {
if (a < 0) a = -a;
if (b < 0) b = -b;
if (b == 0) return a;
else return gcd(b, a % b);
}
using u128 = __uint128_t;
u128 to_uinteger(const string &s) {
u128 res = 0;
for (auto c : s) {
if (isdigit(c)) res = res * 10 + (c - '0');
}
return res;
}
istream& operator >> (istream &is, u128 &x) {
string s;
is >> s;
x = to_uinteger(s);
return is;
}
ostream& operator << (ostream &os, const u128 &x) {
u128 ax = x;
char buffer[128];
char *d = end(buffer);
do {
--d;
*d = "0123456789"[ax % 10];
ax /= 10;
} while (ax != 0);
if (x < 0) {
--d;
*d = '-';
}
int len = end(buffer) - d;
if (os.rdbuf()->sputn(d, len) != len) {
os.setstate(ios_base::badbit);
}
return os;
}
//------------------------------//
// Examples
//------------------------------//
void mini_test() {
i128 a = 99;
i128 b = to_integer("993421434234324234234432421234324");
cout << a + b << endl;
cout << a * b << endl;
cout << a % b << endl;
cout << b * 2 << endl;
}
// Library Checker
// sum_{i=0}^{n-1} floor((a * i + b) / m)
template<class T> T floor_sum(T n, T a, T b, T m) {
if (n == 0) return 0;
T res = 0;
if (a >= m) {
res += n * (n - 1) * (a / m) / 2;
a %= m;
}
if (b >= m) {
res += n * (b / m);
b %= m;
}
if (a == 0) return res;
T ymax = (a * n + b) / m, xmax = ymax * m - b;
if (ymax == 0) return res;
res += (n - (xmax + a - 1) / a) * ymax;
res += floor_sum(ymax, m, (a - xmax % a) % a, a);
return res;
}
void YosupoSumOfFloorOfLinear() {
int T;
cin >> T;
while (T--) {
i128 N, M, A, B;
cin >> N >> M >> A >> B;
cout << floor_sum(N, A, B, M) << endl;
}
}
int main() {
mini_test();
//YosupoSumOfFloorOfLinear();
}