problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02695 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int save[1050][20];
int now[20];
int n, m, Q, tot;
void dfs(int index, int p) {
if (index == n) {
for (int i = 0; i < n; i++) {
save[tot][i] = now[i];
}
tot++;
return;
}
if (index > n) {
return;
}
for (int i = p; i <= m; i++) {
now[index] = i;
dfs(index + 1, i);
}
}
void init() {
tot = 0;
dfs(0, 1);
}
int qu[60][10];
int get_sum(int index) {
int sum = 0;
for (int i = 0; i < Q; i++) {
if (save[index][qu[i][1] - 1] - save[index][qu[i][0] - 1] == qu[i][2]) {
sum += qu[i][3];
}
}
return sum;
}
int main() {
cin >> n >> m >> Q;
init();
for (int i = 0; i < Q; i++) {
cin >> qu[i][0] >> qu[i][1] >> qu[i][2] >> qu[i][3];
}
int maxx = 0;
for (int i = 0; i < tot; i++) {
int now = get_sum(i);
if (now >= maxx) {
maxx = now;
}
}
cout << maxx << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int save[100000][20];
int now[20];
int n, m, Q, tot;
void dfs(int index, int p) {
if (index == n) {
for (int i = 0; i < n; i++) {
save[tot][i] = now[i];
}
tot++;
return;
}
if (index > n) {
return;
}
for (int i = p; i <= m; i++) {
now[index] = i;
dfs(index + 1, i);
}
}
void init() {
tot = 0;
dfs(0, 1);
}
int qu[60][10];
int get_sum(int index) {
int sum = 0;
for (int i = 0; i < Q; i++) {
if (save[index][qu[i][1] - 1] - save[index][qu[i][0] - 1] == qu[i][2]) {
sum += qu[i][3];
}
}
return sum;
}
int main() {
cin >> n >> m >> Q;
init();
for (int i = 0; i < Q; i++) {
cin >> qu[i][0] >> qu[i][1] >> qu[i][2] >> qu[i][3];
}
int maxx = 0;
for (int i = 0; i < tot; i++) {
int now = get_sum(i);
if (now >= maxx) {
maxx = now;
}
}
cout << maxx << endl;
return 0;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define SZ(a) int((a).size())
#define _REP(_1, _2, _3, _4, name, ...) name
#define _REP4(i, b, e, s) \
for (decltype(e) _b = (b), _e = (e), i = _b + (0 < (s) ? 0 : (s)); \
(0 < (s) ? i < _e : _e <= i); i += (s))
#define _REP3(i, b, e) \
for (decltype(e) _b = (b), _e = (e), i = (_b < _e ? _b : _b - 1); \
(_b < _e ? i < _e : _e <= i); (_b < _e ? i++ : i--))
#define _REP2(i, n) for (decltype(n) i = 0, _n = (n); i < _n; i++)
#define _REP1(n) for (decltype(n) _i = 0, _n = (n); _i < _n; _i++)
#define REP(...) _REP(__VA_ARGS__, _REP4, _REP3, _REP2, _REP1)(__VA_ARGS__)
#define FOR(it, c) for (auto &&it = begin(c); it != end(c); it++)
#define ROF(it, c) for (auto &&it = rbegin(c); it != rend(c); it++)
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define INT(n) \
int n; \
assert(0 < scanf("%d", &n))
template <typename T>
bool next_combination(const T first, const T last, int k) {
const T subset = first + k;
// empty container | k = 0 | k == n
if (first == last || first == subset || last == subset) {
return false;
}
T src = subset;
while (first != src) {
src--;
if (*src < *(last - 1)) {
T dest = subset;
while (*src >= *dest) {
dest++;
}
iter_swap(src, dest);
rotate(src + 1, dest + 1, last);
rotate(subset, subset + (last - dest) - 1, last);
return true;
}
}
// restore
rotate(first, subset, last);
return false;
}
int main() {
INT(n);
INT(m);
INT(q);
int ans = 0;
vector<vector<int>> qs;
REP(q) {
INT(a);
INT(b);
INT(c);
INT(d);
qs.PB({a, b, c, d});
}
vector<int> v;
REP(i, n + m - 1) { v.PB(i + 1); }
do {
vector<int> w(n + m - 1, 0), as;
REP(i, m - 1) { w[v[i]] = 1; }
int ai = 1;
for (int e : w) {
if (e == 0) {
as.PB(ai);
} else {
ai++;
}
}
int s = 0;
for (vector<int> qi : qs) {
if (as[qi[1] - 1] - as[qi[0] - 1] == qi[2]) {
s += qi[3];
}
}
ans = max(ans, s);
} while (next_combination(v.begin(), v.end(), m - 1));
printf("%d\n", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define SZ(a) int((a).size())
#define _REP(_1, _2, _3, _4, name, ...) name
#define _REP4(i, b, e, s) \
for (decltype(e) _b = (b), _e = (e), i = _b + (0 < (s) ? 0 : (s)); \
(0 < (s) ? i < _e : _e <= i); i += (s))
#define _REP3(i, b, e) \
for (decltype(e) _b = (b), _e = (e), i = (_b < _e ? _b : _b - 1); \
(_b < _e ? i < _e : _e <= i); (_b < _e ? i++ : i--))
#define _REP2(i, n) for (decltype(n) i = 0, _n = (n); i < _n; i++)
#define _REP1(n) for (decltype(n) _i = 0, _n = (n); _i < _n; _i++)
#define REP(...) _REP(__VA_ARGS__, _REP4, _REP3, _REP2, _REP1)(__VA_ARGS__)
#define FOR(it, c) for (auto &&it = begin(c); it != end(c); it++)
#define ROF(it, c) for (auto &&it = rbegin(c); it != rend(c); it++)
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define INT(n) \
int n; \
assert(0 < scanf("%d", &n))
template <typename T>
bool next_combination(const T first, const T last, int k) {
const T subset = first + k;
// empty container | k = 0 | k == n
if (first == last || first == subset || last == subset) {
return false;
}
T src = subset;
while (first != src) {
src--;
if (*src < *(last - 1)) {
T dest = subset;
while (*src >= *dest) {
dest++;
}
iter_swap(src, dest);
rotate(src + 1, dest + 1, last);
rotate(subset, subset + (last - dest) - 1, last);
return true;
}
}
// restore
rotate(first, subset, last);
return false;
}
int main() {
INT(n);
INT(m);
INT(q);
int ans = 0;
vector<vector<int>> qs;
REP(q) {
INT(a);
INT(b);
INT(c);
INT(d);
qs.PB({a, b, c, d});
}
vector<int> v;
REP(i, n + m - 1) { v.PB(i); }
do {
vector<int> w(n + m - 1, 0), as;
REP(i, m - 1) { w[v[i]] = 1; }
int ai = 1;
for (int e : w) {
if (e == 0) {
as.PB(ai);
} else {
ai++;
}
}
int s = 0;
for (vector<int> qi : qs) {
if (as[qi[1] - 1] - as[qi[0] - 1] == qi[2]) {
s += qi[3];
}
}
ans = max(ans, s);
} while (next_combination(v.begin(), v.end(), m - 1));
printf("%d\n", ans);
return 0;
} | replace | 64 | 65 | 64 | 65 | -6 | free(): invalid pointer
|
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
#define LL long long
#define rep(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
const int mn = 17;
int a[mn], b[mn], c[mn], d[mn], v[mn] = {1};
int n, m, q, ans;
void dfs(int x) {
if (x == n + 1) {
int sum = 0;
for (int i = 1; i <= q; ++i) {
if (v[b[i]] - v[a[i]] == c[i])
sum += d[i];
}
ans = max(ans, sum);
return;
}
for (int i = v[x - 1]; i <= m; ++i) {
v[x] = i;
dfs(x + 1);
}
}
int main() {
scanf("%d%d%d", &n, &m, &q);
rep(i, 1, q) scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
dfs(1);
printf("%d", ans);
} | #include <bits/stdc++.h>
#define LL long long
#define rep(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
const int mn = 57;
int a[mn], b[mn], c[mn], d[mn], v[mn] = {1};
int n, m, q, ans;
void dfs(int x) {
if (x == n + 1) {
int sum = 0;
for (int i = 1; i <= q; ++i) {
if (v[b[i]] - v[a[i]] == c[i])
sum += d[i];
}
ans = max(ans, sum);
return;
}
for (int i = v[x - 1]; i <= m; ++i) {
v[x] = i;
dfs(x + 1);
}
}
int main() {
scanf("%d%d%d", &n, &m, &q);
rep(i, 1, q) scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
dfs(1);
printf("%d", ans);
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 10 + 5;
int a[maxn], b[maxn], c[maxn], d[maxn];
int n, m, Q;
int ans = 0;
int res[maxn];
void dfs(int step, int last) {
if (step == n + 1) {
int cost = 0;
for (int i = 1; i <= Q; i++) {
if (res[b[i]] - res[a[i]] == c[i])
cost += d[i];
}
ans = max(ans, cost);
return;
}
for (int i = last; i <= m; i++) {
res[step] = i;
dfs(step + 1, i);
res[step] = 0;
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> Q;
for (int i = 1; i <= Q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
dfs(1, 1);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 50 + 5;
int a[maxn], b[maxn], c[maxn], d[maxn];
int n, m, Q;
int ans = 0;
int res[maxn];
void dfs(int step, int last) {
if (step == n + 1) {
int cost = 0;
for (int i = 1; i <= Q; i++) {
if (res[b[i]] - res[a[i]] == c[i])
cost += d[i];
}
ans = max(ans, cost);
return;
}
for (int i = last; i <= m; i++) {
res[step] = i;
dfs(step + 1, i);
res[step] = 0;
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> Q;
for (int i = 1; i <= Q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
dfs(1, 1);
cout << ans << endl;
return 0;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02695 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
ll ans = 0;
int a[20], b[20], c[20];
ll d[20];
void dfs(vector<int> A, int N, int M, int Q) {
int P = A.size();
// cout << P << endl;
if (P - 1 == N) {
ll tmp = 0;
rep(i, Q) {
if (A[b[i]] - A[a[i]] == c[i])
tmp += d[i];
}
ans = max(ans, tmp);
// cout << "OK" << endl;
return;
}
for (int i = A[P - 1]; i <= M; i++) {
A.push_back(i);
dfs(A, N, M, Q);
A.pop_back();
}
}
int main() {
int N, M, Q;
cin >> N >> M >> Q;
rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
vector<int> T;
T.push_back(1);
dfs(T, N, M, Q);
cout << ans << endl;
} | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
ll ans = 0;
int a[60], b[60], c[60];
ll d[60];
void dfs(vector<int> A, int N, int M, int Q) {
int P = A.size();
// cout << P << endl;
if (P - 1 == N) {
ll tmp = 0;
rep(i, Q) {
if (A[b[i]] - A[a[i]] == c[i])
tmp += d[i];
}
ans = max(ans, tmp);
// cout << "OK" << endl;
return;
}
for (int i = A[P - 1]; i <= M; i++) {
A.push_back(i);
dfs(A, N, M, Q);
A.pop_back();
}
}
int main() {
int N, M, Q;
cin >> N >> M >> Q;
rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
vector<int> T;
T.push_back(1);
dfs(T, N, M, Q);
cout << ans << endl;
} | replace | 25 | 27 | 25 | 27 | 0 | |
p02695 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
typedef char SINT8;
typedef short SINT16;
typedef int SINT32;
typedef long long SINT64;
typedef double DOUBLE;
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) > (0) ? (a) : -(a))
#define rep(i, a, b) for (SINT64(i) = SINT64(a); (i) < SINT64(b); (i)++)
#define rrep(i, a, b) for (SINT64(i) = SINT64(a); (i) >= SINT64(b); (i)--)
#define put(a) cout << (a) << endl
#define puts(a) cout << (a) << " "
#define putr(a) \
rep(testIncrement, 0, a.size()) { puts(a[testIncrement]); } \
cout << endl
#define INF 1000000001
#define MOD 1000000007
#define INF64 1000000000000000001
#define F first
#define S second
#define Pii pair<SINT32, SINT32>
#define Pll pair<SINT64, SINT64>
#define Piii pair<SINT32, pair<SINT32, SINT32>>
#define Plll pair<SINT64, pair<SINT64, SINT64>>
#define Vll(a, b, c) vector<vector<SINT64>> (a)((b),vector<SINT64>((c))
#define Vlll(a, b, c, d) vector<vector<vector<SINT64>>> (a)((b),vector<vector<SINT64>>((c),vector<SINT64>((d)))
using namespace std;
SINT64 N;
SINT64 M;
SINT64 Q;
SINT64 DA[20][20];
SINT64 dfs(SINT64 d, vector<SINT64> dat) {
SINT64 aaa = 1;
rep(i, 0, dat.size()) { aaa = MAX(aaa, dat[i]); }
SINT64 ma = 0;
if (d == N) {
// putr(dat);
rep(i, 0, Q) {
if (DA[i][1] >= dat.size())
continue;
if (DA[i][0] >= dat.size())
continue;
if (dat[DA[i][1]] - dat[DA[i][0]] == DA[i][2]) {
ma += DA[i][3];
}
}
return ma;
}
rep(i, aaa, M + 1) {
dat.emplace_back(i);
SINT64 buf = dfs(d + 1, dat);
dat.pop_back();
ma = MAX(ma, buf);
}
return ma;
}
int main() {
cin >> N;
cin >> M;
cin >> Q;
rep(i, 0, Q) {
SINT64 a, b, c, d;
cin >> a >> b >> c >> d;
a--;
b--;
DA[i][0] = a;
DA[i][1] = b;
DA[i][2] = c;
DA[i][3] = d;
}
vector<SINT64> dat;
SINT64 ans = dfs(0, dat);
put(ans);
return 0;
}
// vector<vector<SINT64>> data(N,vector<SINT64>(N));
////2次元配列 vector<vector<vector<SINT64>>>
//data(N,vector<vector<SINT64>>(N,vector<SINT64>(N))); //3次元配列
// Vll(data,N,N); //2次元配列
// Vlll(data,N,N,N); //3次元配列
// sort(data.begin(),data.end());
// sort(data.begin(),data.end(),std::greater<SINT64>());
// __gcd(A,B);
// reverse(data.begin(),data.end());
// 関数へのvectorポインタ渡し
// void dfs(SINT64 poi, SINT64 d, vector<vector<Pll>>& data) {
// }
/* 複数条件ソート
bool sortcompare(Pll A, Pll B) {
if(A.F == B.F){
return A.S > B.S;
} else {
return A.F < B.F;
}
}
sort(data.begin(),data.end(),sortcompare);
*/
// タプル
// vector<tuple<SINT64,SINT64,SINT64>> edges;
// edges.emplace_back(a,b,c);
// cout << get<0>(edges[i]);
// cout << get<1>(edges[i]);
// cout << get<2>(edges[i]);
// sort(begin(edges), end(edges)); //ソート
// data.emplace_back(BUF); //後ろに追加
// data.erase(std::unique(data.begin(), data.end()), data.end());
// //ソート後に使用 同じ値を消す
// data.insert(data.begin() + X, 0); //X番目の要素に0を挿入
// 隣接リスト
// vector<vector<SINT64>> data(N);
// data[ A ].emplace_back( B );
// 両端キュー
// deque<SINT64> data;
// data.emplace_front(buf); //先頭挿入
// lower_boundは値がなければ最大値(.size())を返す
// posi = lower_bound(data.begin(),data.end(), X) - data.begin();
// // X以上を探す posi = lower_bound(data.begin(),data.end(),make_pair(X,0)) -
// data.begin(); //pair
/* 文字列回転
string N;
cin >> N;
N = N[N.length()-1] + N.substr(0,N.length()-1);
s = to_string(i); //ストリング変換
*/
/* 文字列合成
string N,M;
cin >> N >> M;
SINT64 ans = 0;
ans = stoi(N+M);
*/
/* 文字列変更
string s; cin >> s;
rep(i,0,s.length()) {
s[i] = (((s[i]-'A' + N) % 26) + 'A');
}
put(s);
*/
/*
//ワーシャルフロイド
vector<vector<SINT64>> dist(N,vector<SINT64>(N,INF64));
rep(i,0,N) {
dist[i][i] = 0;
}
rep(k,0,N) {
rep(i,0,N) {
rep(j,0,N) {
dist[i][j] = MIN(dist[i][j],
dist[i][k]+dist[k][j]);
}
}
}
*/
/* 優先度付きキュー
priority_queue<SINT64, vector<SINT64>, greater<SINT64>> bufq;
//小さいほうから取り出せる priority_queue<SINT64, vector<SINT64>> bufq;
//大きいほうから取り出せる
bufq.push(X); //X を挿入
bufq.top(); //先頭データ読み
bufq.pop(); //先頭データ削除
*/
/* キュー
queue<SINT64> bufq; //宣言
bufq.push(0); //挿入
bufq.front(); //先頭データ読み
bufq.pop(); //先頭データ削除
*/
/* SET コンテナ
set<SINT64> data;
data.insert(X); //X を挿入
data.erase(data.begin()); //先頭を削除
data.erase(--data.end()); //末尾を削除
*data.begin(); //先頭要素にアクセス
*data.rbegin(); //末尾要素にアクセス
//全表示
set<SINT64>::iterator it; //イテレータを用意
for(it = data.begin(); it != data.end(); it++) {
cout << *it << " ";
}
cout << endl;
//N番目を一部表示
set<SINT64>::iterator it; // イテレータを用意
it = data.begin();
rep (i,0,N) {
it++;
}
cout << *it << endl;
*/
/* map
map<string,SINT32> mp;
SINT32 N = 0;
SINT32 mx = 0;
cin >> N;
for (SINT32 i = 0; i < N; i++) {
string s;
cin >> s;
mp[s]++;
}
for(auto it=mp.begin();it!=mp.end();it++) {
mx=max(mx,it->second);
}
//abc146E
map<SINT64,SINT64> mp;
rep(i,0,N+1) {
ans += mp[rui[i]];
mp[rui[i]]++;
bufq.push(rui[i]);
if (bufq.size() == M) {
mp[bufq.front()]--;
bufq.pop();
}
}
*/
/*
//順列全表示
//sortしてからでないと全列挙にならない
sort(data.begin(),data.end());
do {
cout << buf << endl;
rep(i,0,R) {
cout << data[i] << " ";
}
cout << endl;
} while (next_permutation(data.begin(),data.end()));
*/
/* bit数数え上げ
SINT64 bits64(SINT64 bits)
{
bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}
*/
// bitシフトのLONG対応
// ans += (1L<<50);
// 桁指定表示
// ans = ans * M_PI;
// cout << setprecision(15) << ans << endl;
// 桁数0埋め
// cout << std::setfill('0') << std::right << std::setw(2) << 5; //05
// 2次元累積和
/*
vector<vector<SINT64>> data(H,vector<SINT64>(W));
vector<vector<SINT64>> rui(H+1,vector<SINT64>(W+1));
rep(i,0,H) {
rep(j,0,W) {
cin >> data[i][j];
}
}
rep(i,1,H+1) {
rep(j,1,W+1) {
rui[i][j] = data[i-1][j-1] + rui[i][j-1];
}
}
rep(i,1,H+1) {
rep(j,1,W+1) {
rui[i][j] += rui[i-1][j];
}
}
*/
// 逆元 コンビネーション
/*
SINT64 modpow(SINT64 a, SINT64 p) {
if (p == 0) return 1;
if (p % 2 == 0) {
//pが偶数の時
SINT64 halfP = p / 2;
SINT64 half = modpow(a, halfP);
//a^(p/2) をhalfとして、half*halfを計算
return half * half % MOD;
} else {
//pが奇数の時は、偶数にするために1減らす
return a * modpow(a, p - 1) % MOD;
}
}
SINT64 calcComb(SINT64 a, SINT64 b) {
SINT64 Mul = 1;
SINT64 Div = 1;
SINT64 ans = 0;
if (b > a - b) {
return calcComb(a, a - b);
}
rep(i,0,b) {
Mul *= (a - i);
Div *= (i + 1);
Mul %= MOD;
Div %= MOD;
}
ans = Mul * modpow(Div, MOD - 2) % MOD;
return ans;
}
*/
/* UNION FIND
class UnionFind {
public:
vector<SINT64> parent;
UnionFind(SINT64 N) {
parent = vector<SINT64>(N+10, -1); //少し多めに
}
SINT64 root(SINT64 A) {
if (parent[A] < 0) {
return A;
} else {
parent[A] = root(parent[A]);
return parent[A];
}
}
SINT64 size(SINT64 A) {
return parent[root(A)] * (-1);
}
bool judge(SINT64 A, SINT64 B) {
A = root(A);
B = root(B);
if (A == B) {
return true; //同じグループ
} else {
return false; //違うグループ
}
}
void connect(SINT64 A, SINT64 B) {
A = root(A);
B = root(B);
if (A != B) {
if (size(A) < size(B)) {
swap(A, B);
}
parent[A] += parent[B];
parent[B] = A;
}
}
};
UnionFind uni(N);
*/
/* セグ木
class SegTree {
private:
SINT64 size;
vector<SINT64> node;
SINT64 jugdement(SINT64 a, SINT64 b) {
SINT64 ans = 0;
ans = a+b;
return ans;
}
public:
//コンストラクタ
SegTree(vector<SINT64> data) {
SINT64 ori_size;
ori_size = data.size();
size = 1;
while (size < ori_size) {
size *= 2;
}
node.resize(2*size-1, 0);
rep(i,0,ori_size) {
node[size-1+i] = data[i];
}
rrep(i,size-2,0) {
node[i] = jugdement(node[2*i+1], node[2*i+2]);
}
}
//データ更新
void update(SINT64 x, SINT64 val) {
x += (size - 1);
node[x] = val+node[x];
while(x > 0) {
x = (x - 1) / 2;
node[x] = jugdement(node[2*x+1], node[2*x+2]);
}
}
//データ取得 [a,b)
SINT64 getdata(SINT64 a, SINT64 b, SINT64 k = 0, SINT64 l = 0, SINT64 r
= -1) { if (r < 0) { r = size;
}
//要求範囲外
if ((r <= a) || (b <= l)) {
return 0;
}
//完全要求区間内
if ((a <= l) && (r <= b)) {
return node[k];
}
SINT64 vl = getdata(a, b, 2*k+1, l, (l+r)/2);
SINT64 vr = getdata(a, b, 2*k+2, (l+r)/2, r);
return jugdement(vl, vr);
}
//表示
void disp() {
rep(i,0,size) {
puts(node[size-1+i]);
} cout << endl;
}
void alldisp() {
SINT64 cnt = 0;
SINT64 end = 2;
while (1) {
puts(node[cnt]);
if (cnt == end-2) {
end *= 2;
cout << endl;
}
cnt++;
if (cnt == size*2-1) {
break;
}
}
}
};
SegTree seg(N);
*/
/* 最大フロー最小カット
class Dinic {
struct EDGE {
SINT64 to;
SINT64 cap;
SINT64 rev;
};
vector<vector<EDGE>> G;
vector<SINT64> level;
vector<SINT64> root;
SINT64 N;
public:
Dinic(SINT64 n) {
N = n;
G.resize(N);
level.resize(N);
}
void add(SINT64 a, SINT64 b, SINT64 cap) {
G[a].emplace_back((EDGE){b,cap,(SINT64)G[b].size()});
G[b].emplace_back((EDGE){a,0,(SINT64)G[a].size()-1});
}
void bfs(SINT64 s) {
level[s] = 0;
queue<SINT64> q;
q.push(s);
while(q.size() != 0) {
SINT64 buf = q.front();
q.pop();
rep(i,0,G[buf].size()) {
EDGE now = G[buf][i];
if ((now.cap > 0) && (level[now.to] == -1)) {
level[now.to] = level[buf]+1;
q.push(now.to);
}
}
}
}
SINT64 dfs(SINT64 now, SINT64 g, SINT64 flow) {
if (now == g) return flow;
rep(i,root[now],G[now].size()) {
EDGE buf = G[now][i];
root[now] = i;
//dsf進捗更新 if ((buf.cap > 0) && (level[buf.to] > level[now])) { SINT64 mi =
MIN(buf.cap,flow); SINT64 nowf = dfs(buf.to,g,mi); if (nowf > 0) { G[now][i].cap
-= nowf; //順経路に容量削減 G[buf.to][buf.rev].cap +=
nowf; //逆経路に容量追加 return nowf;
//今回探索のFLOW追加数
}
}
}
return 0;
}
SINT64 act(SINT64 s, SINT64 g) {
SINT64 cnt = 0;
//最大FLOWカウント if (s == g) return cnt;
//スタート=ゴールは例外 while(1) { level.assign(N,-1);
//sからの最短距離初期化 root.assign(N,0);
//dsf進捗初期化 bfs(s); if (level[g] == -1) break;
//gへ到達不可 while(1) { SINT64 flow; flow = dfs(s,g,INF64); if (flow == 0)
break; cnt += flow;
}
}
return cnt;
}
};
*/
/* ダイクストラ
class Dijkstra {
vector<vector<Pll>> G;
vector<SINT64> dist;
public:
Dijkstra(SINT64 n) {
G.resize(n);
dist.resize(n, INF64);
}
void add(SINT64 a, SINT64 b, SINT64 cost) {
G[a].emplace_back(Pll(b,cost));
}
void form(SINT64 s) {
priority_queue<Pll, vector<Pll>, greater<Pll>> q;
q.push(Pll(0,s));
//cost,位置 while(q.size() != 0) { Pll now = q.top(); q.pop(); if (dist[now.S]
== INF64) { dist[now.S] = now.F; rep(i,0,G[now.S].size()) { Pll buf =
G[now.S][i]; if (dist[buf.F] == INF64) { q.push(Pll(buf.S+now.F,buf.F));
}
}
}
}
}
//form()で構築したsからの距離を返す
SINT64 get_dist(SINT64 a) {
if (dist[a] == INF64) {
return -1; //到達不可
} else {
return dist[a];
}
}
};
*/
/* LCA
class Lca {
vector<vector<SINT64>> G;
vector<vector<SINT64>> D; //ダブリング
vector<SINT64> depth;
SINT64 N;
SINT64 LOG_N;
public:
Lca(SINT64 n) {
N = n;
LOG_N = floor(log2(N));
G.resize(N);
D.resize(N);
depth.resize(N,-1);
}
void add(SINT64 a, SINT64 b) {
G[a].emplace_back(b);
G[b].emplace_back(a);
}
void bfs(SINT64 s) {
depth[s] = 0;
D[s].emplace_back(-1);
queue<SINT64> q;
q.push(s);
while(q.size() != 0) {
SINT64 now = q.front();
q.pop();
rep(i,0,G[now].size()) {
SINT64 next = G[now][i];
if (depth[next] == -1) {
depth[next] = depth[now]+1;
D[next].emplace_back(now);
q.push(next);
}
}
}
}
//頂点のsからのダブリング構築
void form(SINT64 s) {
bfs(s);
rep(i,1,LOG_N+1) {
rep(j,0,N) {
SINT64 buf = D[j][i-1];
if (buf == -1) {
D[j].emplace_back(-1);
} else {
D[j].emplace_back(D[buf][i-1]);
}
}
}
}
//aのx上の頂点を求める
SINT64 get(SINT64 a, SINT64 x) {
rrep(i,LOG_N,0) {
if (((x >> i) & 1) == 1) {
a = D[a][i];
if (a == -1) return -1;
}
}
return a;
}
//aとbの共通祖先を求める
SINT64 get_lca(SINT64 a, SINT64 b) {
if (depth[a] < depth[b]) swap(a,b);
SINT64 diff = depth[a] - depth[b];
a = get(a,diff);
//aのx上の頂点を求める if (a == b) return a; rrep(i,LOG_N,0) { if (D[a][i] !=
D[b][i]) { a = D[a][i]; b = D[b][i];
}
}
return D[a][0];
}
//aとbの共通祖先までの距離の合計を求める
SINT64 get_dist(SINT64 a, SINT64 b) {
SINT64 buf = get_lca(a,b);
return depth[a] + depth[b] - depth[buf]*2;
}
};
*/
/* ベルマンフォード
class Bellman {
struct EDGE {
SINT64 from;
SINT64 to;
SINT64 cost;
};
vector<EDGE> edges;
vector<SINT64> dist;
SINT64 N;
public:
Bellman(SINT64 n) {
N = n;
dist.resize(n, INF64);
}
void add(SINT64 from, SINT64 to, SINT64 cost) {
edges.emplace_back((EDGE){from,to,cost});
}
//sで構築したt迄の距離取得
SINT64 get_dist(SINT64 t) {
//到達不可はINF64
return dist[t];
}
//構築
//負の閉路無し : 0
//負の閉路有り : 1
//負の閉路有るが目的地gの更新は停止 : 2
SINT64 form(SINT64 s, SINT64 g) {
dist[s] = 0;
SINT64 cnt = 0;
SINT64 check = 0;
while(1) {
SINT64 renew = 0;
rep(i,0,edges.size()) {
EDGE e = edges[i];
if (dist[e.from] != INF64) {
if (dist[e.to] > dist[e.from] + e.cost)
{ renew = 1; dist[e.to] = dist[e.from] + e.cost;
}
}
}
if (renew == 0) return 0;
//N回更新後のgの距離と 2N回更新後のgの距離を比較
if (cnt == N) check = dist[g];
if (cnt > 2*N) {
if (check == dist[g]) return 2;
return 1;
}
cnt++;
}
}
};
*/
/*コンビネーション
class Comb {
vector<SINT64> base;
SINT64 N;
public:
Comb (SINT64 n) {
N = n+5;
base.resize(N);
base[0] = 1;
rep(i,1,N) {
base[i] = base[i-1]*i;
base[i] %= MOD;
}
}
SINT64 get_comb(SINT64 a, SINT64 b) {
SINT64 ans = 0;
SINT64 aa = base[a] * modpow(base[a-b], MOD - 2) % MOD;
ans = aa * modpow(base[b], MOD - 2) % MOD;
return ans;
}
SINT64 modpow(SINT64 a, SINT64 p) {
if (p == 0) return 1;
if (p % 2 == 0) {
SINT64 halfP = p / 2;
SINT64 half = modpow(a, halfP);
return half * half % MOD;
} else {
return a * modpow(a, p - 1) % MOD;
}
}
};
*/
/* SUFFIX ARRAY
class SuffixArray {
private:
vector<string> array; // サフィックスアレイ
vector<SINT64> lcp; // LCP
vector<SINT64> sais; // SA IS
string str;
public:
// コンストラクタ
SuffixArray (string s) {
str = s;
vector<SINT64> Vstr;
rep(i,0,str.length()) {
Vstr.emplace_back(str[i]);
}
sais_act(Vstr, sais, 255); // SAIS実行
// lcp_act(); //
隣り合うSUFFIXの先頭から同じ長さを算出
// suffix array 文字列作成
// array.resize(str.length());
// rep(i,0,array.size()) {
// array[i] = str.substr(sais[i]);
// }
// rep(i,0,array.size()) {put(array[i]);} // 表示用
}
// LCP作成
void lcp_act(void) {
lcp.resize(str.length());
vector<SINT64> buffer(str.length());
rep(i,0,str.length()) {
buffer[sais[i]] = i;
}
SINT64 cnt = 0;
rep(i,0,str.length()) {
if (buffer[i] >= str.length()-1) {
cnt = 0;
} else {
SINT64 a = buffer[i];
SINT64 b = buffer[i]+1;
while(1) {
if (cnt >= str.length() - sais[a])
break; if (cnt >= str.length() - sais[a]) break;
if (str[sais[a]+cnt] ==
str[sais[b]+cnt]) { cnt++; } else { break;
}
}
}
lcp[buffer[i]] = cnt;
if (cnt != 0) cnt--;
}
}
// 引数の文字列が何個含まれるか算出
SINT64 get_cnt(string t) {
SINT64 low,high;
SINT64 L,R;
L = -1;
R = str.length();
while(R-L > 1) {
SINT64 M = (R+L)/2;
string buf = str.substr(sais[M]);
if (buf.length() > t.length()) {
buf = buf.substr(0,t.length());
}
if (buf > t) {R = M;} else {L = M;}
}
high = R;
L = -1;
R = str.length();
while(R-L > 1) {
SINT64 M = (R+L)/2;
string buf = str.substr(sais[M]);
if (buf >= t) {R = M;} else {L = M;}
}
low = R;
return high - low;
}
// SAIS実行
void sais_act(vector<SINT64>& Vstr, vector<SINT64>& r_sais, SINT64 type)
{ Vstr.push_back(0); //
番兵追加 vector<SINT64> lms_seed; // LMS
ソート前 vector<SINT64> lms_sort; // LMS
ソート後 vector<SINT64> lms_long(Vstr.size(),0); // LMS 長さ
vector<SINT64> lms_type(Vstr.size(),1); // 0:L 1:S 2:LMS
vector<SINT64> lms_posi(Vstr.size(),-1); //
LMS内での位置
SINT64 len = 0;
// L S LMS判定 初期値は全てS
rrep(i,Vstr.size()-2,0) {
len++;
if (Vstr[i] > Vstr[i+1]) {
lms_type[i] = 0;
// L if (lms_type[i+1] == 1) {
lms_type[i+1] = 2;
// LMS lms_long[i+1] = len; // LMSの長さ格納 len = 1;
}
}
if (Vstr[i] == Vstr[i+1]) lms_type[i] = lms_type[i+1];
// 右と同じ
}
SINT64 cnt = 0;
rep(i,0,Vstr.size()) {
if (lms_type[i] == 2) lms_seed.emplace_back(i);
if (lms_type[i] == 2) lms_posi[i] = cnt++;
}
// Induced Sort初回
vector<SINT64> bucket;
// Induced Sort初回結果格納用 induced_sort(Vstr, lms_seed, bucket, lms_type,
type);
// lms_sortにLMSのソートを格納
rrep(i,Vstr.size()-1,0) {
if ((bucket[i] != -1) && (lms_type[bucket[i]] == 2)) {
lms_sort.emplace_back(bucket[i]);
}
}
SINT64 ok = 0; // 再帰必要性判定 SINT64 rank = 1;
// 再帰用文字 vector<SINT64> next(lms_sort.size(), 1); // 再帰用文字列
rrep(i,lms_sort.size()-2,0) {
SINT64 A = lms_long[lms_sort[i]];
SINT64 B = lms_long[lms_sort[i+1]];
if (A == B) {
SINT64 ck = 0;
rep(j,0,A) {
if (Vstr[lms_sort[i]+j] != Vstr[lms_sort[i+1]+j]) {
ck = 1;
break;
}
}
if (ck == 0) {
ok = 1; // 再帰必要
} else {
rank++;
}
} else {
rank++;
}
next[lms_posi[lms_sort[i]]] = rank;
}
if (ok == 1) {
vector<SINT64> recursive;
sais_act(next, recursive, rank+1);
rep(i,0,recursive.size()) {
lms_sort[recursive.size()-i-1] =
lms_seed[recursive[i]];
}
}
// SORT済みLMSでInduced Sorting
r_sais.resize(Vstr.size(),-1);
induced_sort(Vstr, lms_sort, r_sais, lms_type, type);
r_sais.erase(r_sais.begin()); // 番兵削除
}
// induced_sort
void induced_sort(vector<SINT64>& Vstr, vector<SINT64>& seed,
vector<SINT64>& bucket_sort, vector<SINT64>& lms_type, SINT64 type) {
vector<SINT64> bucket_cnt(type,0); //
バケット 文字種ごとの数 vector<SINT64> bucket_st(type,0);
// バケット 文字種の開始位置
vector<SINT64> bucket_end(type,0); //
バケット 文字種の終了位置 vector<SINT64> bucket_pre(Vstr.size(),-1); //
バケット 初回格納用 vector<SINT64> cnt1(type,0); vector<SINT64> cnt2(type,0);
vector<SINT64> cnt3(type,0);
bucket_sort.resize(Vstr.size(),-1);
// バケットソート位置作成
rep(i,0,Vstr.size()) bucket_cnt[Vstr[i]]++;
// 個数作成
rep(i,1,type) bucket_st[i] = bucket_st[i-1] + bucket_cnt[i-1];
// 開始位置
rep(i,0,type) bucket_end[i] = bucket_st[i] + bucket_cnt[i]-1;
// 終了位置
// LMSをbucket_preに格納
rep(i,0,seed.size()) {
SINT64 no = seed[i];
bucket_pre[bucket_end[Vstr[no]] - cnt1[Vstr[no]]] = no;
cnt1[Vstr[no]]++;
}
// Lをbucket_sortに格納
rep(i,0,Vstr.size()) {
if ((bucket_pre[i] != -1) && (bucket_pre[i] != 0)) {
if (lms_type[bucket_pre[i]-1] == 0) {
// -1がLの場合 SINT64 buf = Vstr[bucket_pre[i]-1]; bucket_pre [bucket_st[buf] +
cnt2[buf]] = bucket_pre[i]-1; bucket_sort[bucket_st[buf] + cnt2[buf]] =
bucket_pre[i]-1; cnt2[buf]++;
}
}
}
// Sをbucket_sortに格納
bucket_sort[0] = Vstr.size()-1;
// 番兵追加 rrep(i,Vstr.size()-1,0) { if ((bucket_sort[i] != -1) &&
(bucket_sort[i] != 0)) { if (lms_type[bucket_sort[i]-1] != 0) { //
-1がS(LMS)の場合 SINT64 buf = Vstr[bucket_sort[i]-1];
bucket_sort[bucket_end[buf] - cnt3[buf]]
= bucket_sort[i]-1; cnt3[buf]++;
}
}
}
}
};
*/
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
typedef char SINT8;
typedef short SINT16;
typedef int SINT32;
typedef long long SINT64;
typedef double DOUBLE;
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) > (0) ? (a) : -(a))
#define rep(i, a, b) for (SINT64(i) = SINT64(a); (i) < SINT64(b); (i)++)
#define rrep(i, a, b) for (SINT64(i) = SINT64(a); (i) >= SINT64(b); (i)--)
#define put(a) cout << (a) << endl
#define puts(a) cout << (a) << " "
#define putr(a) \
rep(testIncrement, 0, a.size()) { puts(a[testIncrement]); } \
cout << endl
#define INF 1000000001
#define MOD 1000000007
#define INF64 1000000000000000001
#define F first
#define S second
#define Pii pair<SINT32, SINT32>
#define Pll pair<SINT64, SINT64>
#define Piii pair<SINT32, pair<SINT32, SINT32>>
#define Plll pair<SINT64, pair<SINT64, SINT64>>
#define Vll(a, b, c) vector<vector<SINT64>> (a)((b),vector<SINT64>((c))
#define Vlll(a, b, c, d) vector<vector<vector<SINT64>>> (a)((b),vector<vector<SINT64>>((c),vector<SINT64>((d)))
using namespace std;
SINT64 N;
SINT64 M;
SINT64 Q;
SINT64 DA[100][100];
SINT64 dfs(SINT64 d, vector<SINT64> dat) {
SINT64 aaa = 1;
rep(i, 0, dat.size()) { aaa = MAX(aaa, dat[i]); }
SINT64 ma = 0;
if (d == N) {
// putr(dat);
rep(i, 0, Q) {
if (DA[i][1] >= dat.size())
continue;
if (DA[i][0] >= dat.size())
continue;
if (dat[DA[i][1]] - dat[DA[i][0]] == DA[i][2]) {
ma += DA[i][3];
}
}
return ma;
}
rep(i, aaa, M + 1) {
dat.emplace_back(i);
SINT64 buf = dfs(d + 1, dat);
dat.pop_back();
ma = MAX(ma, buf);
}
return ma;
}
int main() {
cin >> N;
cin >> M;
cin >> Q;
rep(i, 0, Q) {
SINT64 a, b, c, d;
cin >> a >> b >> c >> d;
a--;
b--;
DA[i][0] = a;
DA[i][1] = b;
DA[i][2] = c;
DA[i][3] = d;
}
vector<SINT64> dat;
SINT64 ans = dfs(0, dat);
put(ans);
return 0;
}
// vector<vector<SINT64>> data(N,vector<SINT64>(N));
////2次元配列 vector<vector<vector<SINT64>>>
//data(N,vector<vector<SINT64>>(N,vector<SINT64>(N))); //3次元配列
// Vll(data,N,N); //2次元配列
// Vlll(data,N,N,N); //3次元配列
// sort(data.begin(),data.end());
// sort(data.begin(),data.end(),std::greater<SINT64>());
// __gcd(A,B);
// reverse(data.begin(),data.end());
// 関数へのvectorポインタ渡し
// void dfs(SINT64 poi, SINT64 d, vector<vector<Pll>>& data) {
// }
/* 複数条件ソート
bool sortcompare(Pll A, Pll B) {
if(A.F == B.F){
return A.S > B.S;
} else {
return A.F < B.F;
}
}
sort(data.begin(),data.end(),sortcompare);
*/
// タプル
// vector<tuple<SINT64,SINT64,SINT64>> edges;
// edges.emplace_back(a,b,c);
// cout << get<0>(edges[i]);
// cout << get<1>(edges[i]);
// cout << get<2>(edges[i]);
// sort(begin(edges), end(edges)); //ソート
// data.emplace_back(BUF); //後ろに追加
// data.erase(std::unique(data.begin(), data.end()), data.end());
// //ソート後に使用 同じ値を消す
// data.insert(data.begin() + X, 0); //X番目の要素に0を挿入
// 隣接リスト
// vector<vector<SINT64>> data(N);
// data[ A ].emplace_back( B );
// 両端キュー
// deque<SINT64> data;
// data.emplace_front(buf); //先頭挿入
// lower_boundは値がなければ最大値(.size())を返す
// posi = lower_bound(data.begin(),data.end(), X) - data.begin();
// // X以上を探す posi = lower_bound(data.begin(),data.end(),make_pair(X,0)) -
// data.begin(); //pair
/* 文字列回転
string N;
cin >> N;
N = N[N.length()-1] + N.substr(0,N.length()-1);
s = to_string(i); //ストリング変換
*/
/* 文字列合成
string N,M;
cin >> N >> M;
SINT64 ans = 0;
ans = stoi(N+M);
*/
/* 文字列変更
string s; cin >> s;
rep(i,0,s.length()) {
s[i] = (((s[i]-'A' + N) % 26) + 'A');
}
put(s);
*/
/*
//ワーシャルフロイド
vector<vector<SINT64>> dist(N,vector<SINT64>(N,INF64));
rep(i,0,N) {
dist[i][i] = 0;
}
rep(k,0,N) {
rep(i,0,N) {
rep(j,0,N) {
dist[i][j] = MIN(dist[i][j],
dist[i][k]+dist[k][j]);
}
}
}
*/
/* 優先度付きキュー
priority_queue<SINT64, vector<SINT64>, greater<SINT64>> bufq;
//小さいほうから取り出せる priority_queue<SINT64, vector<SINT64>> bufq;
//大きいほうから取り出せる
bufq.push(X); //X を挿入
bufq.top(); //先頭データ読み
bufq.pop(); //先頭データ削除
*/
/* キュー
queue<SINT64> bufq; //宣言
bufq.push(0); //挿入
bufq.front(); //先頭データ読み
bufq.pop(); //先頭データ削除
*/
/* SET コンテナ
set<SINT64> data;
data.insert(X); //X を挿入
data.erase(data.begin()); //先頭を削除
data.erase(--data.end()); //末尾を削除
*data.begin(); //先頭要素にアクセス
*data.rbegin(); //末尾要素にアクセス
//全表示
set<SINT64>::iterator it; //イテレータを用意
for(it = data.begin(); it != data.end(); it++) {
cout << *it << " ";
}
cout << endl;
//N番目を一部表示
set<SINT64>::iterator it; // イテレータを用意
it = data.begin();
rep (i,0,N) {
it++;
}
cout << *it << endl;
*/
/* map
map<string,SINT32> mp;
SINT32 N = 0;
SINT32 mx = 0;
cin >> N;
for (SINT32 i = 0; i < N; i++) {
string s;
cin >> s;
mp[s]++;
}
for(auto it=mp.begin();it!=mp.end();it++) {
mx=max(mx,it->second);
}
//abc146E
map<SINT64,SINT64> mp;
rep(i,0,N+1) {
ans += mp[rui[i]];
mp[rui[i]]++;
bufq.push(rui[i]);
if (bufq.size() == M) {
mp[bufq.front()]--;
bufq.pop();
}
}
*/
/*
//順列全表示
//sortしてからでないと全列挙にならない
sort(data.begin(),data.end());
do {
cout << buf << endl;
rep(i,0,R) {
cout << data[i] << " ";
}
cout << endl;
} while (next_permutation(data.begin(),data.end()));
*/
/* bit数数え上げ
SINT64 bits64(SINT64 bits)
{
bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}
*/
// bitシフトのLONG対応
// ans += (1L<<50);
// 桁指定表示
// ans = ans * M_PI;
// cout << setprecision(15) << ans << endl;
// 桁数0埋め
// cout << std::setfill('0') << std::right << std::setw(2) << 5; //05
// 2次元累積和
/*
vector<vector<SINT64>> data(H,vector<SINT64>(W));
vector<vector<SINT64>> rui(H+1,vector<SINT64>(W+1));
rep(i,0,H) {
rep(j,0,W) {
cin >> data[i][j];
}
}
rep(i,1,H+1) {
rep(j,1,W+1) {
rui[i][j] = data[i-1][j-1] + rui[i][j-1];
}
}
rep(i,1,H+1) {
rep(j,1,W+1) {
rui[i][j] += rui[i-1][j];
}
}
*/
// 逆元 コンビネーション
/*
SINT64 modpow(SINT64 a, SINT64 p) {
if (p == 0) return 1;
if (p % 2 == 0) {
//pが偶数の時
SINT64 halfP = p / 2;
SINT64 half = modpow(a, halfP);
//a^(p/2) をhalfとして、half*halfを計算
return half * half % MOD;
} else {
//pが奇数の時は、偶数にするために1減らす
return a * modpow(a, p - 1) % MOD;
}
}
SINT64 calcComb(SINT64 a, SINT64 b) {
SINT64 Mul = 1;
SINT64 Div = 1;
SINT64 ans = 0;
if (b > a - b) {
return calcComb(a, a - b);
}
rep(i,0,b) {
Mul *= (a - i);
Div *= (i + 1);
Mul %= MOD;
Div %= MOD;
}
ans = Mul * modpow(Div, MOD - 2) % MOD;
return ans;
}
*/
/* UNION FIND
class UnionFind {
public:
vector<SINT64> parent;
UnionFind(SINT64 N) {
parent = vector<SINT64>(N+10, -1); //少し多めに
}
SINT64 root(SINT64 A) {
if (parent[A] < 0) {
return A;
} else {
parent[A] = root(parent[A]);
return parent[A];
}
}
SINT64 size(SINT64 A) {
return parent[root(A)] * (-1);
}
bool judge(SINT64 A, SINT64 B) {
A = root(A);
B = root(B);
if (A == B) {
return true; //同じグループ
} else {
return false; //違うグループ
}
}
void connect(SINT64 A, SINT64 B) {
A = root(A);
B = root(B);
if (A != B) {
if (size(A) < size(B)) {
swap(A, B);
}
parent[A] += parent[B];
parent[B] = A;
}
}
};
UnionFind uni(N);
*/
/* セグ木
class SegTree {
private:
SINT64 size;
vector<SINT64> node;
SINT64 jugdement(SINT64 a, SINT64 b) {
SINT64 ans = 0;
ans = a+b;
return ans;
}
public:
//コンストラクタ
SegTree(vector<SINT64> data) {
SINT64 ori_size;
ori_size = data.size();
size = 1;
while (size < ori_size) {
size *= 2;
}
node.resize(2*size-1, 0);
rep(i,0,ori_size) {
node[size-1+i] = data[i];
}
rrep(i,size-2,0) {
node[i] = jugdement(node[2*i+1], node[2*i+2]);
}
}
//データ更新
void update(SINT64 x, SINT64 val) {
x += (size - 1);
node[x] = val+node[x];
while(x > 0) {
x = (x - 1) / 2;
node[x] = jugdement(node[2*x+1], node[2*x+2]);
}
}
//データ取得 [a,b)
SINT64 getdata(SINT64 a, SINT64 b, SINT64 k = 0, SINT64 l = 0, SINT64 r
= -1) { if (r < 0) { r = size;
}
//要求範囲外
if ((r <= a) || (b <= l)) {
return 0;
}
//完全要求区間内
if ((a <= l) && (r <= b)) {
return node[k];
}
SINT64 vl = getdata(a, b, 2*k+1, l, (l+r)/2);
SINT64 vr = getdata(a, b, 2*k+2, (l+r)/2, r);
return jugdement(vl, vr);
}
//表示
void disp() {
rep(i,0,size) {
puts(node[size-1+i]);
} cout << endl;
}
void alldisp() {
SINT64 cnt = 0;
SINT64 end = 2;
while (1) {
puts(node[cnt]);
if (cnt == end-2) {
end *= 2;
cout << endl;
}
cnt++;
if (cnt == size*2-1) {
break;
}
}
}
};
SegTree seg(N);
*/
/* 最大フロー最小カット
class Dinic {
struct EDGE {
SINT64 to;
SINT64 cap;
SINT64 rev;
};
vector<vector<EDGE>> G;
vector<SINT64> level;
vector<SINT64> root;
SINT64 N;
public:
Dinic(SINT64 n) {
N = n;
G.resize(N);
level.resize(N);
}
void add(SINT64 a, SINT64 b, SINT64 cap) {
G[a].emplace_back((EDGE){b,cap,(SINT64)G[b].size()});
G[b].emplace_back((EDGE){a,0,(SINT64)G[a].size()-1});
}
void bfs(SINT64 s) {
level[s] = 0;
queue<SINT64> q;
q.push(s);
while(q.size() != 0) {
SINT64 buf = q.front();
q.pop();
rep(i,0,G[buf].size()) {
EDGE now = G[buf][i];
if ((now.cap > 0) && (level[now.to] == -1)) {
level[now.to] = level[buf]+1;
q.push(now.to);
}
}
}
}
SINT64 dfs(SINT64 now, SINT64 g, SINT64 flow) {
if (now == g) return flow;
rep(i,root[now],G[now].size()) {
EDGE buf = G[now][i];
root[now] = i;
//dsf進捗更新 if ((buf.cap > 0) && (level[buf.to] > level[now])) { SINT64 mi =
MIN(buf.cap,flow); SINT64 nowf = dfs(buf.to,g,mi); if (nowf > 0) { G[now][i].cap
-= nowf; //順経路に容量削減 G[buf.to][buf.rev].cap +=
nowf; //逆経路に容量追加 return nowf;
//今回探索のFLOW追加数
}
}
}
return 0;
}
SINT64 act(SINT64 s, SINT64 g) {
SINT64 cnt = 0;
//最大FLOWカウント if (s == g) return cnt;
//スタート=ゴールは例外 while(1) { level.assign(N,-1);
//sからの最短距離初期化 root.assign(N,0);
//dsf進捗初期化 bfs(s); if (level[g] == -1) break;
//gへ到達不可 while(1) { SINT64 flow; flow = dfs(s,g,INF64); if (flow == 0)
break; cnt += flow;
}
}
return cnt;
}
};
*/
/* ダイクストラ
class Dijkstra {
vector<vector<Pll>> G;
vector<SINT64> dist;
public:
Dijkstra(SINT64 n) {
G.resize(n);
dist.resize(n, INF64);
}
void add(SINT64 a, SINT64 b, SINT64 cost) {
G[a].emplace_back(Pll(b,cost));
}
void form(SINT64 s) {
priority_queue<Pll, vector<Pll>, greater<Pll>> q;
q.push(Pll(0,s));
//cost,位置 while(q.size() != 0) { Pll now = q.top(); q.pop(); if (dist[now.S]
== INF64) { dist[now.S] = now.F; rep(i,0,G[now.S].size()) { Pll buf =
G[now.S][i]; if (dist[buf.F] == INF64) { q.push(Pll(buf.S+now.F,buf.F));
}
}
}
}
}
//form()で構築したsからの距離を返す
SINT64 get_dist(SINT64 a) {
if (dist[a] == INF64) {
return -1; //到達不可
} else {
return dist[a];
}
}
};
*/
/* LCA
class Lca {
vector<vector<SINT64>> G;
vector<vector<SINT64>> D; //ダブリング
vector<SINT64> depth;
SINT64 N;
SINT64 LOG_N;
public:
Lca(SINT64 n) {
N = n;
LOG_N = floor(log2(N));
G.resize(N);
D.resize(N);
depth.resize(N,-1);
}
void add(SINT64 a, SINT64 b) {
G[a].emplace_back(b);
G[b].emplace_back(a);
}
void bfs(SINT64 s) {
depth[s] = 0;
D[s].emplace_back(-1);
queue<SINT64> q;
q.push(s);
while(q.size() != 0) {
SINT64 now = q.front();
q.pop();
rep(i,0,G[now].size()) {
SINT64 next = G[now][i];
if (depth[next] == -1) {
depth[next] = depth[now]+1;
D[next].emplace_back(now);
q.push(next);
}
}
}
}
//頂点のsからのダブリング構築
void form(SINT64 s) {
bfs(s);
rep(i,1,LOG_N+1) {
rep(j,0,N) {
SINT64 buf = D[j][i-1];
if (buf == -1) {
D[j].emplace_back(-1);
} else {
D[j].emplace_back(D[buf][i-1]);
}
}
}
}
//aのx上の頂点を求める
SINT64 get(SINT64 a, SINT64 x) {
rrep(i,LOG_N,0) {
if (((x >> i) & 1) == 1) {
a = D[a][i];
if (a == -1) return -1;
}
}
return a;
}
//aとbの共通祖先を求める
SINT64 get_lca(SINT64 a, SINT64 b) {
if (depth[a] < depth[b]) swap(a,b);
SINT64 diff = depth[a] - depth[b];
a = get(a,diff);
//aのx上の頂点を求める if (a == b) return a; rrep(i,LOG_N,0) { if (D[a][i] !=
D[b][i]) { a = D[a][i]; b = D[b][i];
}
}
return D[a][0];
}
//aとbの共通祖先までの距離の合計を求める
SINT64 get_dist(SINT64 a, SINT64 b) {
SINT64 buf = get_lca(a,b);
return depth[a] + depth[b] - depth[buf]*2;
}
};
*/
/* ベルマンフォード
class Bellman {
struct EDGE {
SINT64 from;
SINT64 to;
SINT64 cost;
};
vector<EDGE> edges;
vector<SINT64> dist;
SINT64 N;
public:
Bellman(SINT64 n) {
N = n;
dist.resize(n, INF64);
}
void add(SINT64 from, SINT64 to, SINT64 cost) {
edges.emplace_back((EDGE){from,to,cost});
}
//sで構築したt迄の距離取得
SINT64 get_dist(SINT64 t) {
//到達不可はINF64
return dist[t];
}
//構築
//負の閉路無し : 0
//負の閉路有り : 1
//負の閉路有るが目的地gの更新は停止 : 2
SINT64 form(SINT64 s, SINT64 g) {
dist[s] = 0;
SINT64 cnt = 0;
SINT64 check = 0;
while(1) {
SINT64 renew = 0;
rep(i,0,edges.size()) {
EDGE e = edges[i];
if (dist[e.from] != INF64) {
if (dist[e.to] > dist[e.from] + e.cost)
{ renew = 1; dist[e.to] = dist[e.from] + e.cost;
}
}
}
if (renew == 0) return 0;
//N回更新後のgの距離と 2N回更新後のgの距離を比較
if (cnt == N) check = dist[g];
if (cnt > 2*N) {
if (check == dist[g]) return 2;
return 1;
}
cnt++;
}
}
};
*/
/*コンビネーション
class Comb {
vector<SINT64> base;
SINT64 N;
public:
Comb (SINT64 n) {
N = n+5;
base.resize(N);
base[0] = 1;
rep(i,1,N) {
base[i] = base[i-1]*i;
base[i] %= MOD;
}
}
SINT64 get_comb(SINT64 a, SINT64 b) {
SINT64 ans = 0;
SINT64 aa = base[a] * modpow(base[a-b], MOD - 2) % MOD;
ans = aa * modpow(base[b], MOD - 2) % MOD;
return ans;
}
SINT64 modpow(SINT64 a, SINT64 p) {
if (p == 0) return 1;
if (p % 2 == 0) {
SINT64 halfP = p / 2;
SINT64 half = modpow(a, halfP);
return half * half % MOD;
} else {
return a * modpow(a, p - 1) % MOD;
}
}
};
*/
/* SUFFIX ARRAY
class SuffixArray {
private:
vector<string> array; // サフィックスアレイ
vector<SINT64> lcp; // LCP
vector<SINT64> sais; // SA IS
string str;
public:
// コンストラクタ
SuffixArray (string s) {
str = s;
vector<SINT64> Vstr;
rep(i,0,str.length()) {
Vstr.emplace_back(str[i]);
}
sais_act(Vstr, sais, 255); // SAIS実行
// lcp_act(); //
隣り合うSUFFIXの先頭から同じ長さを算出
// suffix array 文字列作成
// array.resize(str.length());
// rep(i,0,array.size()) {
// array[i] = str.substr(sais[i]);
// }
// rep(i,0,array.size()) {put(array[i]);} // 表示用
}
// LCP作成
void lcp_act(void) {
lcp.resize(str.length());
vector<SINT64> buffer(str.length());
rep(i,0,str.length()) {
buffer[sais[i]] = i;
}
SINT64 cnt = 0;
rep(i,0,str.length()) {
if (buffer[i] >= str.length()-1) {
cnt = 0;
} else {
SINT64 a = buffer[i];
SINT64 b = buffer[i]+1;
while(1) {
if (cnt >= str.length() - sais[a])
break; if (cnt >= str.length() - sais[a]) break;
if (str[sais[a]+cnt] ==
str[sais[b]+cnt]) { cnt++; } else { break;
}
}
}
lcp[buffer[i]] = cnt;
if (cnt != 0) cnt--;
}
}
// 引数の文字列が何個含まれるか算出
SINT64 get_cnt(string t) {
SINT64 low,high;
SINT64 L,R;
L = -1;
R = str.length();
while(R-L > 1) {
SINT64 M = (R+L)/2;
string buf = str.substr(sais[M]);
if (buf.length() > t.length()) {
buf = buf.substr(0,t.length());
}
if (buf > t) {R = M;} else {L = M;}
}
high = R;
L = -1;
R = str.length();
while(R-L > 1) {
SINT64 M = (R+L)/2;
string buf = str.substr(sais[M]);
if (buf >= t) {R = M;} else {L = M;}
}
low = R;
return high - low;
}
// SAIS実行
void sais_act(vector<SINT64>& Vstr, vector<SINT64>& r_sais, SINT64 type)
{ Vstr.push_back(0); //
番兵追加 vector<SINT64> lms_seed; // LMS
ソート前 vector<SINT64> lms_sort; // LMS
ソート後 vector<SINT64> lms_long(Vstr.size(),0); // LMS 長さ
vector<SINT64> lms_type(Vstr.size(),1); // 0:L 1:S 2:LMS
vector<SINT64> lms_posi(Vstr.size(),-1); //
LMS内での位置
SINT64 len = 0;
// L S LMS判定 初期値は全てS
rrep(i,Vstr.size()-2,0) {
len++;
if (Vstr[i] > Vstr[i+1]) {
lms_type[i] = 0;
// L if (lms_type[i+1] == 1) {
lms_type[i+1] = 2;
// LMS lms_long[i+1] = len; // LMSの長さ格納 len = 1;
}
}
if (Vstr[i] == Vstr[i+1]) lms_type[i] = lms_type[i+1];
// 右と同じ
}
SINT64 cnt = 0;
rep(i,0,Vstr.size()) {
if (lms_type[i] == 2) lms_seed.emplace_back(i);
if (lms_type[i] == 2) lms_posi[i] = cnt++;
}
// Induced Sort初回
vector<SINT64> bucket;
// Induced Sort初回結果格納用 induced_sort(Vstr, lms_seed, bucket, lms_type,
type);
// lms_sortにLMSのソートを格納
rrep(i,Vstr.size()-1,0) {
if ((bucket[i] != -1) && (lms_type[bucket[i]] == 2)) {
lms_sort.emplace_back(bucket[i]);
}
}
SINT64 ok = 0; // 再帰必要性判定 SINT64 rank = 1;
// 再帰用文字 vector<SINT64> next(lms_sort.size(), 1); // 再帰用文字列
rrep(i,lms_sort.size()-2,0) {
SINT64 A = lms_long[lms_sort[i]];
SINT64 B = lms_long[lms_sort[i+1]];
if (A == B) {
SINT64 ck = 0;
rep(j,0,A) {
if (Vstr[lms_sort[i]+j] != Vstr[lms_sort[i+1]+j]) {
ck = 1;
break;
}
}
if (ck == 0) {
ok = 1; // 再帰必要
} else {
rank++;
}
} else {
rank++;
}
next[lms_posi[lms_sort[i]]] = rank;
}
if (ok == 1) {
vector<SINT64> recursive;
sais_act(next, recursive, rank+1);
rep(i,0,recursive.size()) {
lms_sort[recursive.size()-i-1] =
lms_seed[recursive[i]];
}
}
// SORT済みLMSでInduced Sorting
r_sais.resize(Vstr.size(),-1);
induced_sort(Vstr, lms_sort, r_sais, lms_type, type);
r_sais.erase(r_sais.begin()); // 番兵削除
}
// induced_sort
void induced_sort(vector<SINT64>& Vstr, vector<SINT64>& seed,
vector<SINT64>& bucket_sort, vector<SINT64>& lms_type, SINT64 type) {
vector<SINT64> bucket_cnt(type,0); //
バケット 文字種ごとの数 vector<SINT64> bucket_st(type,0);
// バケット 文字種の開始位置
vector<SINT64> bucket_end(type,0); //
バケット 文字種の終了位置 vector<SINT64> bucket_pre(Vstr.size(),-1); //
バケット 初回格納用 vector<SINT64> cnt1(type,0); vector<SINT64> cnt2(type,0);
vector<SINT64> cnt3(type,0);
bucket_sort.resize(Vstr.size(),-1);
// バケットソート位置作成
rep(i,0,Vstr.size()) bucket_cnt[Vstr[i]]++;
// 個数作成
rep(i,1,type) bucket_st[i] = bucket_st[i-1] + bucket_cnt[i-1];
// 開始位置
rep(i,0,type) bucket_end[i] = bucket_st[i] + bucket_cnt[i]-1;
// 終了位置
// LMSをbucket_preに格納
rep(i,0,seed.size()) {
SINT64 no = seed[i];
bucket_pre[bucket_end[Vstr[no]] - cnt1[Vstr[no]]] = no;
cnt1[Vstr[no]]++;
}
// Lをbucket_sortに格納
rep(i,0,Vstr.size()) {
if ((bucket_pre[i] != -1) && (bucket_pre[i] != 0)) {
if (lms_type[bucket_pre[i]-1] == 0) {
// -1がLの場合 SINT64 buf = Vstr[bucket_pre[i]-1]; bucket_pre [bucket_st[buf] +
cnt2[buf]] = bucket_pre[i]-1; bucket_sort[bucket_st[buf] + cnt2[buf]] =
bucket_pre[i]-1; cnt2[buf]++;
}
}
}
// Sをbucket_sortに格納
bucket_sort[0] = Vstr.size()-1;
// 番兵追加 rrep(i,Vstr.size()-1,0) { if ((bucket_sort[i] != -1) &&
(bucket_sort[i] != 0)) { if (lms_type[bucket_sort[i]-1] != 0) { //
-1がS(LMS)の場合 SINT64 buf = Vstr[bucket_sort[i]-1];
bucket_sort[bucket_end[buf] - cnt3[buf]]
= bucket_sort[i]-1; cnt3[buf]++;
}
}
}
}
};
*/
| replace | 49 | 50 | 49 | 50 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 20;
int a[N], b[N], c[N], d[N], p[N];
int ans = 0;
int n, m, q;
void dfs(int x) {
int cnt = 0, i;
if (x == n + 1) {
for (i = 1; i <= q; i++)
if (p[b[i]] - p[a[i]] == c[i])
cnt += d[i];
ans = max(cnt, ans);
return;
}
for (p[x] = p[x - 1]; p[x] <= m; p[x]++)
dfs(x + 1);
}
int main() {
int i;
cin >> n >> m >> q;
for (i = 1; i <= q; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
p[0] = 1;
dfs(1);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 100;
int a[N], b[N], c[N], d[N], p[N];
int ans = 0;
int n, m, q;
void dfs(int x) {
int cnt = 0, i;
if (x == n + 1) {
for (i = 1; i <= q; i++)
if (p[b[i]] - p[a[i]] == c[i])
cnt += d[i];
ans = max(cnt, ans);
return;
}
for (p[x] = p[x - 1]; p[x] <= m; p[x]++)
dfs(x + 1);
}
int main() {
int i;
cin >> n >> m >> q;
for (i = 1; i <= q; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
p[0] = 1;
dfs(1);
cout << ans << endl;
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll N, M, Q;
vector<ll> A(10, 0), B(10, 0), C(10, 0), D(10, 0);
ll ans = 0;
ll dfs(vector<ll> V, int index) {
vector<ll> V2 = V;
ll ret = 0;
if (index < N - 1) {
for (ll i = V[index]; i <= M; i++) {
V2[index + 1] = i;
ret = max(ret, dfs(V2, index + 1));
}
} else {
for (ll i = 0; i < Q; i++) {
if (V[B[i]] - V[A[i]] == C[i])
ret += D[i];
}
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M >> Q;
for (ll i = 0; i < Q; i++) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
A[i]--, B[i]--;
}
vector<ll> V(N, 0);
V[0] = 1;
ans = dfs(V, 0);
cout << ans << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll N, M, Q;
vector<ll> A(50, 0), B(50, 0), C(50, 0), D(50, 0);
ll ans = 0;
ll dfs(vector<ll> V, int index) {
vector<ll> V2 = V;
ll ret = 0;
if (index < N - 1) {
for (ll i = V[index]; i <= M; i++) {
V2[index + 1] = i;
ret = max(ret, dfs(V2, index + 1));
}
} else {
for (ll i = 0; i < Q; i++) {
if (V[B[i]] - V[A[i]] == C[i])
ret += D[i];
}
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M >> Q;
for (ll i = 0; i < Q; i++) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
A[i]--, B[i]--;
}
vector<ll> V(N, 0);
V[0] = 1;
ans = dfs(V, 0);
cout << ans << '\n';
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
namespace variables {
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
struct triple {
ll first, second, third;
};
} // namespace variables
using namespace variables;
void testt();
namespace andu2006 {
/// debugging
void debug(set<ll> _var, string _name = "") {
cerr << "Debug #set " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END] : "
<< "size = " << _var.size() << "\n\n";
}
void light_debug(set<ll> _var, string _name = "") {
cerr << "Debug #set " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END]\n\n";
}
void debug(map<ll, ll> _var, string _name = "") {
cerr << "Debug #map (size = " << _var.size() << ")\n";
for (auto it : _var)
cerr << _name << "[" << it.first << "] = " << it.second << '\n';
}
void light_debug(map<ll, ll> _var, string _name = "") {
cerr << "Debug #map \n";
for (auto it : _var)
cerr << _name << "[" << it.first << "] = " << it.second << '\n';
}
void debug(deque<ll> _var, string _name = "") {
cerr << "Debug #deque " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END] : "
<< "size = " << _var.size() << "\n\n";
}
void light_debug(deque<ll> _var, string _name = "") {
cerr << "Debug #deque " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END]\n\n";
}
void debug(vector<ll> _var, string _name = "") {
cerr << "Debug #vector " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END] : "
<< "size = " << _var.size() << "\n\n";
}
void light_debug(vector<ll> _var, string _name = "") {
cerr << "Debug #vector " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END]\n\n";
}
void debug(string _var, string _name = "") {
cerr << "Debug #string " << _name << " = ";
cerr << '"' << _var << '"';
cerr << " : "
<< "size = " << _var.size() << "\n\n";
}
void light_debug(string _var, string _name = "") {
cerr << "Debug #string : " << _name << " = " << '"' << _var << '"' << "\n\n";
}
void debug(ll _var, string _name = "") {
cerr << "Debug #int64 : " << _name << " = " << _var << "\n\n";
}
void debug(ull _var, string _name = "") {
cerr << "Debug #uint64 : " << _name << " = " << _var << "\n\n";
}
void debug(uint _var, string _name = "") {
cerr << "Debug #uint32 : " << _name << " = " << _var << "\n\n";
}
void debug(int _var, string _name = "") {
cerr << "Debug #int32 : " << _name << " = " << _var << "\n\n";
}
void debug(char _var, string _name = "") {
cerr << "Debug #char : " << _name << " = '" << _var << "'\n\n";
}
void debug(bool _var, string _name = "") {
cerr << "Debug #Boolean : " << _name << " = " << (_var ? "1 True" : "0 False")
<< "\n\n";
}
/// misc
string emptyString;
vector<ll> emptyVector;
stack<ll> emptyStack;
queue<ll> emptyQueue;
void multipleTests() {
ll __test_count;
cin >> __test_count;
while (__test_count--)
testt();
}
void done() { exit(0); }
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void rseed() { srand(time(NULL)); }
void mreset(void *_pointer, uint _size) { memset(_pointer, 0, _size); }
/// compare functions
template <char compArg> bool compare(ll a, ll b) {
if (compArg == '<')
return a < b;
else if (compArg == '>')
return a > b;
return false;
}
template <char compArg1, char compArg2>
bool compair(pll a, pll b) { /// HAHA pun
return compare<compArg1>(a.first, b.first) ||
(a.first == b.first && compare<compArg2>(a.second, b.second));
}
template <char compArg1, char compArg2> struct objcompair {
bool operator()(pll a, pll b) {
return compare<compArg1>(a.first, b.first) ||
(a.first == b.first && compare<compArg2>(a.second, b.second));
}
};
/// binary functions
ll trailingZeroes(ll x) {
if (x == 0)
return 0;
return (x & 1 ? 0 : 1 + trailingZeroes(x / 2));
}
ll trailingOnes(ll x) {
if (x == 0)
return 0;
return (x & 1 ? 1 + trailingZeroes(x / 2) : 0);
}
ll trailingPowerOfTwo(ll x) { return 1ll << trailingZeroes(x); }
ll popCount(ll x) {
if (x == 0)
return x;
return 1 + popCount(x & -x);
}
ll log2(ll x) {
if (x == 0)
return -1;
return 1 + log2(x / 2);
}
ll getBit(ll x, ll pos) { return (x >> pos) & 1; }
ll cutLastOne(ll x) { return x & -x; }
/// modular functions; "mod" MUST BE PRIME
ll binPow(ll _num, ll _exponent, ll _mod) { /// a^b
if (_exponent == 0)
return 1;
ll _half = binPow(_num, _exponent / 2, _mod);
return _half * _half % _mod * (_exponent & 1 ? _num : 1) % _mod;
}
ll modularInverse(ll _x, ll _mod) { /// (a / b ) % mod
return binPow(_x, _mod - 2, _mod);
}
ll comb(ll _n, ll _k, ll _mod) { /// binom(a, b)
ll _ans = 1;
for (ll i = _n - _k + 1; i <= _n; i++)
_ans = _ans * i % _mod;
for (ll i = 2; i <= _k; i++)
_ans = _ans * modularInverse(i, _mod) % _mod;
return _ans;
}
/// math
const ld PI = acos(-1);
const ld rad = 180 / PI;
ld degsin(ll __alpha) { return sin(__alpha / rad); }
ld degcos(ll __alpha) { return cos(__alpha / rad); }
ld degtan(ll __alpha) { return tan(__alpha / rad); }
ld degasin(ll _sine) { return asin(_sine) * rad; }
ld degacos(ll _cosine) { return acos(_cosine) * rad; }
ld degatan(ll _tangent) { return atan(_tangent) * rad; }
ll gcd(ll _num1, ll _num2) {
if (_num2 == 0)
return _num1;
return gcd(_num2, _num1 % _num2);
}
ll lcm(ll _num1, ll _num2) { return _num1 * _num2 / gcd(_num1, _num2); }
ll divisors(ll _num) { /// number of divisors
if (_num == 0)
return 0;
ll _cnt = 1, _exp;
while (_num % 2 == 0) {
_cnt++;
_num /= 2;
}
for (ll _divisor = 3; _divisor * _divisor <= _num; _divisor += 2) {
_exp = 0;
while (_num % _divisor == 0) {
_exp++;
_num /= _divisor;
}
_cnt *= (_exp + 1);
}
if (_num > 1)
_cnt *= 2;
return _cnt;
}
ll eulerTotient(ll _num) { /// computes the euler totient function
if (_num == 0)
return 0;
ll _cnt = 1, _divisor = 2;
while (_num % _divisor == 0) {
_cnt *= _divisor;
_num /= _divisor;
}
_cnt /= _divisor;
_cnt *= (_divisor - 1);
for (ll _divisor = 3; _divisor * _divisor <= _num; _divisor += 2) {
if (_num % _divisor == 0) {
while (_num % _divisor == 0) {
_cnt *= _divisor;
_num /= _divisor;
}
_cnt /= _divisor;
_cnt *= (_divisor - 1);
}
}
if (_num > 1)
_cnt *= _num - 1;
return _cnt;
}
void sieve(bool *_startpos, bool *_endpos) {
/// erathostenes' sieve; 0 means the number is prime; 1 means the number is
/// composite
ll _maxnum = _endpos - _startpos;
for (ll i = 4; i < _maxnum; i += 2)
*(_startpos + i) = 1;
for (ll i = 3; i * i < _maxnum; i += 2)
if (*(_startpos + i) == 0)
for (ll j = i * i; j < _maxnum; j += i)
*(_startpos + j) = 1;
}
} // namespace andu2006
/// - end of namespaces and start of program
#define MOD 998244353
#define NMAX 11
struct str {
ll a, b, c, d;
} v[NMAX];
ll sol[NMAX], ans = 0, n, m, q;
void testt() {}
ll sum() {
ll s = 0;
for (ll i = 0; i < q; i++) {
if (sol[v[i].b - 1] - sol[v[i].a - 1] == v[i].c)
s += v[i].d;
}
return s;
}
void bkt(ll k) {
if (k == n) {
ans = max(ans, sum());
} else {
for (ll i = (k ? sol[k - 1] : 1); i <= m; i++) {
sol[k] = i;
bkt(k + 1);
}
}
}
int main() {
andu2006::fastIO();
cin >> n >> m >> q;
for (ll i = 0; i < q; i++)
cin >> v[i].a >> v[i].b >> v[i].c >> v[i].d;
bkt(0);
cout << ans;
andu2006::done();
}
| #include <bits/stdc++.h>
using namespace std;
namespace variables {
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
struct triple {
ll first, second, third;
};
} // namespace variables
using namespace variables;
void testt();
namespace andu2006 {
/// debugging
void debug(set<ll> _var, string _name = "") {
cerr << "Debug #set " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END] : "
<< "size = " << _var.size() << "\n\n";
}
void light_debug(set<ll> _var, string _name = "") {
cerr << "Debug #set " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END]\n\n";
}
void debug(map<ll, ll> _var, string _name = "") {
cerr << "Debug #map (size = " << _var.size() << ")\n";
for (auto it : _var)
cerr << _name << "[" << it.first << "] = " << it.second << '\n';
}
void light_debug(map<ll, ll> _var, string _name = "") {
cerr << "Debug #map \n";
for (auto it : _var)
cerr << _name << "[" << it.first << "] = " << it.second << '\n';
}
void debug(deque<ll> _var, string _name = "") {
cerr << "Debug #deque " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END] : "
<< "size = " << _var.size() << "\n\n";
}
void light_debug(deque<ll> _var, string _name = "") {
cerr << "Debug #deque " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END]\n\n";
}
void debug(vector<ll> _var, string _name = "") {
cerr << "Debug #vector " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END] : "
<< "size = " << _var.size() << "\n\n";
}
void light_debug(vector<ll> _var, string _name = "") {
cerr << "Debug #vector " << _name << " = [";
for (auto it : _var)
cerr << it << ",";
cerr << "END]\n\n";
}
void debug(string _var, string _name = "") {
cerr << "Debug #string " << _name << " = ";
cerr << '"' << _var << '"';
cerr << " : "
<< "size = " << _var.size() << "\n\n";
}
void light_debug(string _var, string _name = "") {
cerr << "Debug #string : " << _name << " = " << '"' << _var << '"' << "\n\n";
}
void debug(ll _var, string _name = "") {
cerr << "Debug #int64 : " << _name << " = " << _var << "\n\n";
}
void debug(ull _var, string _name = "") {
cerr << "Debug #uint64 : " << _name << " = " << _var << "\n\n";
}
void debug(uint _var, string _name = "") {
cerr << "Debug #uint32 : " << _name << " = " << _var << "\n\n";
}
void debug(int _var, string _name = "") {
cerr << "Debug #int32 : " << _name << " = " << _var << "\n\n";
}
void debug(char _var, string _name = "") {
cerr << "Debug #char : " << _name << " = '" << _var << "'\n\n";
}
void debug(bool _var, string _name = "") {
cerr << "Debug #Boolean : " << _name << " = " << (_var ? "1 True" : "0 False")
<< "\n\n";
}
/// misc
string emptyString;
vector<ll> emptyVector;
stack<ll> emptyStack;
queue<ll> emptyQueue;
void multipleTests() {
ll __test_count;
cin >> __test_count;
while (__test_count--)
testt();
}
void done() { exit(0); }
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void rseed() { srand(time(NULL)); }
void mreset(void *_pointer, uint _size) { memset(_pointer, 0, _size); }
/// compare functions
template <char compArg> bool compare(ll a, ll b) {
if (compArg == '<')
return a < b;
else if (compArg == '>')
return a > b;
return false;
}
template <char compArg1, char compArg2>
bool compair(pll a, pll b) { /// HAHA pun
return compare<compArg1>(a.first, b.first) ||
(a.first == b.first && compare<compArg2>(a.second, b.second));
}
template <char compArg1, char compArg2> struct objcompair {
bool operator()(pll a, pll b) {
return compare<compArg1>(a.first, b.first) ||
(a.first == b.first && compare<compArg2>(a.second, b.second));
}
};
/// binary functions
ll trailingZeroes(ll x) {
if (x == 0)
return 0;
return (x & 1 ? 0 : 1 + trailingZeroes(x / 2));
}
ll trailingOnes(ll x) {
if (x == 0)
return 0;
return (x & 1 ? 1 + trailingZeroes(x / 2) : 0);
}
ll trailingPowerOfTwo(ll x) { return 1ll << trailingZeroes(x); }
ll popCount(ll x) {
if (x == 0)
return x;
return 1 + popCount(x & -x);
}
ll log2(ll x) {
if (x == 0)
return -1;
return 1 + log2(x / 2);
}
ll getBit(ll x, ll pos) { return (x >> pos) & 1; }
ll cutLastOne(ll x) { return x & -x; }
/// modular functions; "mod" MUST BE PRIME
ll binPow(ll _num, ll _exponent, ll _mod) { /// a^b
if (_exponent == 0)
return 1;
ll _half = binPow(_num, _exponent / 2, _mod);
return _half * _half % _mod * (_exponent & 1 ? _num : 1) % _mod;
}
ll modularInverse(ll _x, ll _mod) { /// (a / b ) % mod
return binPow(_x, _mod - 2, _mod);
}
ll comb(ll _n, ll _k, ll _mod) { /// binom(a, b)
ll _ans = 1;
for (ll i = _n - _k + 1; i <= _n; i++)
_ans = _ans * i % _mod;
for (ll i = 2; i <= _k; i++)
_ans = _ans * modularInverse(i, _mod) % _mod;
return _ans;
}
/// math
const ld PI = acos(-1);
const ld rad = 180 / PI;
ld degsin(ll __alpha) { return sin(__alpha / rad); }
ld degcos(ll __alpha) { return cos(__alpha / rad); }
ld degtan(ll __alpha) { return tan(__alpha / rad); }
ld degasin(ll _sine) { return asin(_sine) * rad; }
ld degacos(ll _cosine) { return acos(_cosine) * rad; }
ld degatan(ll _tangent) { return atan(_tangent) * rad; }
ll gcd(ll _num1, ll _num2) {
if (_num2 == 0)
return _num1;
return gcd(_num2, _num1 % _num2);
}
ll lcm(ll _num1, ll _num2) { return _num1 * _num2 / gcd(_num1, _num2); }
ll divisors(ll _num) { /// number of divisors
if (_num == 0)
return 0;
ll _cnt = 1, _exp;
while (_num % 2 == 0) {
_cnt++;
_num /= 2;
}
for (ll _divisor = 3; _divisor * _divisor <= _num; _divisor += 2) {
_exp = 0;
while (_num % _divisor == 0) {
_exp++;
_num /= _divisor;
}
_cnt *= (_exp + 1);
}
if (_num > 1)
_cnt *= 2;
return _cnt;
}
ll eulerTotient(ll _num) { /// computes the euler totient function
if (_num == 0)
return 0;
ll _cnt = 1, _divisor = 2;
while (_num % _divisor == 0) {
_cnt *= _divisor;
_num /= _divisor;
}
_cnt /= _divisor;
_cnt *= (_divisor - 1);
for (ll _divisor = 3; _divisor * _divisor <= _num; _divisor += 2) {
if (_num % _divisor == 0) {
while (_num % _divisor == 0) {
_cnt *= _divisor;
_num /= _divisor;
}
_cnt /= _divisor;
_cnt *= (_divisor - 1);
}
}
if (_num > 1)
_cnt *= _num - 1;
return _cnt;
}
void sieve(bool *_startpos, bool *_endpos) {
/// erathostenes' sieve; 0 means the number is prime; 1 means the number is
/// composite
ll _maxnum = _endpos - _startpos;
for (ll i = 4; i < _maxnum; i += 2)
*(_startpos + i) = 1;
for (ll i = 3; i * i < _maxnum; i += 2)
if (*(_startpos + i) == 0)
for (ll j = i * i; j < _maxnum; j += i)
*(_startpos + j) = 1;
}
} // namespace andu2006
/// - end of namespaces and start of program
#define MOD 998244353
#define NMAX 100
struct str {
ll a, b, c, d;
} v[NMAX];
ll sol[NMAX], ans = 0, n, m, q;
void testt() {}
ll sum() {
ll s = 0;
for (ll i = 0; i < q; i++) {
if (sol[v[i].b - 1] - sol[v[i].a - 1] == v[i].c)
s += v[i].d;
}
return s;
}
void bkt(ll k) {
if (k == n) {
ans = max(ans, sum());
} else {
for (ll i = (k ? sol[k - 1] : 1); i <= m; i++) {
sol[k] = i;
bkt(k + 1);
}
}
}
int main() {
andu2006::fastIO();
cin >> n >> m >> q;
for (ll i = 0; i < q; i++)
cin >> v[i].a >> v[i].b >> v[i].c >> v[i].d;
bkt(0);
cout << ans;
andu2006::done();
}
| replace | 258 | 259 | 258 | 259 | 0 | |
p02695 | C++ | Runtime Error | #pragma region RegionDefs
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, l, r) for (int i = (l), i##_len = (r); i < i##_len; ++i)
#define all(x) begin(x), end(x)
using namespace std;
const int INF = 1e9;
template <class T = int> using V = vector<T>;
template <class T = int> using PQ = priority_queue<T>;
template <class T = int> using PQG = priority_queue<T, V<T>, greater<T>>;
typedef long long ll;
const ll MOD = 1000000007LL;
void in() {}
template <class Head, class... Tail> void in(Head &&head, Tail &&...tail) {
cin >> head;
in(move(tail)...);
}
#define IN(...) \
ll __VA_ARGS__; \
in(__VA_ARGS__)
// 時間が厳しいとき使う
#define INI(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define INS(T, ...) \
T __VA_ARGS__; \
in(__VA_ARGS__)
#define VIN(T, v, n) \
V<T> v(n); \
for (auto &_elem : v) \
cin >> _elem
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define y0 y__0
#define y1 y__1
#define j0 j__0
#define j1 j__1
#define YN(e) cout << ((e) ? "YES" : "NO") << endl
#define yn(e) cout << ((e) ? "Yes" : "No") << endl
#pragma endregion RegionDefs
template <class T>
bool combination_with_replacement_dfs(const vector<T> &seed, int size,
bool (*callback)(const vector<T> &),
int depth, vector<int> &used,
vector<T> &temp) {
if (depth == size)
return callback(temp);
for (int i = depth == 0 ? 0 : used[depth - 1]; i < seed.size(); ++i) {
temp[depth] = seed[i];
used[depth] = i;
if (combination_with_replacement_dfs(seed, size, callback, depth + 1, used,
temp))
return true;
}
return false;
}
template <class T>
bool combination_with_replacement_dfs(const vector<T> &seed, int size,
bool (*callback)(const vector<T> &)) {
vector<int> used(size);
vector<T> temp(size);
combination_with_replacement_dfs(seed, size, callback, 0, used, temp);
}
vector<tuple<ll, ll, ll, ll>> cond;
ll mx = 0;
// callbackがtrueを返すと、その時点で探索を終了する。
bool test_callback(const vector<ll> &comb) {
ll pts = 0;
for (auto &&t : cond) {
if (comb[get<1>(t)] - comb[get<0>(t)] == get<2>(t))
pts += get<3>(t);
}
chmax(mx, pts);
return false;
}
void solve() {
IN(n, m, q);
vector<ll> v(m);
rep(i, m) v[i] = i + 1;
rep(i, q) {
IN(a, b, c, d);
cond.emplace_back(a - 1, b - 1, c, d);
}
combination_with_replacement_dfs(v, n, test_callback);
cout << mx << endl;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
} | #pragma region RegionDefs
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, l, r) for (int i = (l), i##_len = (r); i < i##_len; ++i)
#define all(x) begin(x), end(x)
using namespace std;
const int INF = 1e9;
template <class T = int> using V = vector<T>;
template <class T = int> using PQ = priority_queue<T>;
template <class T = int> using PQG = priority_queue<T, V<T>, greater<T>>;
typedef long long ll;
const ll MOD = 1000000007LL;
void in() {}
template <class Head, class... Tail> void in(Head &&head, Tail &&...tail) {
cin >> head;
in(move(tail)...);
}
#define IN(...) \
ll __VA_ARGS__; \
in(__VA_ARGS__)
// 時間が厳しいとき使う
#define INI(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define INS(T, ...) \
T __VA_ARGS__; \
in(__VA_ARGS__)
#define VIN(T, v, n) \
V<T> v(n); \
for (auto &_elem : v) \
cin >> _elem
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define y0 y__0
#define y1 y__1
#define j0 j__0
#define j1 j__1
#define YN(e) cout << ((e) ? "YES" : "NO") << endl
#define yn(e) cout << ((e) ? "Yes" : "No") << endl
#pragma endregion RegionDefs
template <class T>
bool combination_with_replacement_dfs(const vector<T> &seed, int size,
bool (*callback)(const vector<T> &),
int depth, vector<int> &used,
vector<T> &temp) {
if (depth == size)
return callback(temp);
for (int i = depth == 0 ? 0 : used[depth - 1]; i < seed.size(); ++i) {
temp[depth] = seed[i];
used[depth] = i;
if (combination_with_replacement_dfs(seed, size, callback, depth + 1, used,
temp))
return true;
}
return false;
}
template <class T>
bool combination_with_replacement_dfs(const vector<T> &seed, int size,
bool (*callback)(const vector<T> &)) {
vector<int> used(size);
vector<T> temp(size);
return combination_with_replacement_dfs(seed, size, callback, 0, used, temp);
}
vector<tuple<ll, ll, ll, ll>> cond;
ll mx = 0;
// callbackがtrueを返すと、その時点で探索を終了する。
bool test_callback(const vector<ll> &comb) {
ll pts = 0;
for (auto &&t : cond) {
if (comb[get<1>(t)] - comb[get<0>(t)] == get<2>(t))
pts += get<3>(t);
}
chmax(mx, pts);
return false;
}
void solve() {
IN(n, m, q);
vector<ll> v(m);
rep(i, m) v[i] = i + 1;
rep(i, q) {
IN(a, b, c, d);
cond.emplace_back(a - 1, b - 1, c, d);
}
combination_with_replacement_dfs(v, n, test_callback);
cout << mx << endl;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
} | replace | 75 | 76 | 75 | 76 | 0 | |
p02695 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <limits>
// #include <type_traits>
// #include <boost/integer/common_factor_rt.hpp>
using namespace std;
int main() {
long ans, sum;
int i, j, N, M, Q;
int a[50], b[50], c[50];
long d[50] = {};
int e[10] = {};
bool can;
can = true;
// using T = int32_t;
ans = 0;
sum = 0;
cin >> N >> M >> Q;
for (i = 0; i < Q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
for (i = 0; i < N; i++) {
e[i] = 1;
}
for (;;) {
e[N - 1] += 1;
for (i = N - 1; i > -1; i--) {
if (e[i] == M + 1)
e[i - 1] += 1;
}
for (i = 1; i < N; i++) {
if (e[i] == M + 1)
e[i] = e[i - 1];
}
for (j = 0; j < Q; j++) {
if ((e[b[j] - 1] - e[a[j] - 1]) == c[j])
sum += d[j];
}
if (ans < sum)
ans = sum;
sum = 0;
// cout<<e[0]<<" "<<e[1]<<" "<<e[2]<<" "<<e[3]<<" "<<e[4]<<" "<<e[5]<<"
// "<<e[6]<<" "<<e[7]<<" "<<e[8]<<" "<<e[9]<<endl;
if (e[0] == M)
break;
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <limits>
// #include <type_traits>
// #include <boost/integer/common_factor_rt.hpp>
using namespace std;
int main() {
long ans, sum;
int i, j, N, M, Q;
int a[50], b[50], c[50];
long d[50] = {};
int e[10] = {};
bool can;
can = true;
// using T = int32_t;
ans = 0;
sum = 0;
cin >> N >> M >> Q;
for (i = 0; i < Q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
for (i = 0; i < N; i++) {
e[i] = 1;
}
for (;;) {
e[N - 1] += 1;
for (i = N - 1; i > -1; i--) {
if (e[i] == M + 1)
e[i - 1] += 1;
}
for (i = 1; i < N; i++) {
if (e[i] == M + 1)
e[i] = e[i - 1];
}
for (j = 0; j < Q; j++) {
if ((e[b[j] - 1] - e[a[j] - 1]) == c[j])
sum += d[j];
}
if (ans < sum)
ans = sum;
sum = 0;
// cout<<e[0]<<" "<<e[1]<<" "<<e[2]<<" "<<e[3]<<" "<<e[4]<<" "<<e[5]<<"
// "<<e[6]<<" "<<e[7]<<" "<<e[8]<<" "<<e[9]<<endl;
if (e[0] > (M / 2))
break;
}
cout << ans << endl;
return 0;
}
| replace | 51 | 52 | 51 | 52 | TLE | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int a[50], b[50], c[50], d[50];
int n, m, q;
int ans = 0;
int A[10];
void dfs(int dep, int pre) {
if (dep == n) {
int sum = 0;
for (int i = 0; i < q; i++) {
if (A[b[i] - 1] - A[a[i] - 1] == c[i])
sum += d[i];
}
ans = max(sum, ans);
return;
} else {
for (int i = pre; i < n - 1; i++) {
A[i] = i;
dfs(i + 1, pre);
}
}
}
int main() {
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
dfs(0, 1);
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int a[50], b[50], c[50], d[50];
int n, m, q;
int ans = 0;
int A[10];
void dfs(int dep, int pre) {
if (dep == n) {
int sum = 0;
for (int i = 0; i < q; i++) {
if (A[b[i] - 1] - A[a[i] - 1] == c[i])
sum += d[i];
}
ans = max(sum, ans);
return;
} else {
for (int i = pre; i <= m; i++) {
A[dep] = i;
dfs(dep + 1, i);
}
}
}
int main() {
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
dfs(0, 1);
cout << ans << endl;
}
| replace | 20 | 23 | 20 | 23 | -11 | |
p02695 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool nextvect(vector<int> &A, int N, int M) {
if (A[N - 1] < M)
A[N - 1]++;
else {
int i = N - 2;
while (i >= 0 && A[i] == M)
i--;
if (i == -1)
return false;
A[i]++;
for (int j = i + 1; j < N; j++)
A[j] = A[i];
}
}
int main() {
int N, M, Q;
cin >> N >> M >> Q;
vector<vector<int>> Queries(Q, vector<int>(4));
for (int i = 0; i < Q; i++)
cin >> Queries[i][0] >> Queries[i][1] >> Queries[i][2] >> Queries[i][3];
vector<int> A(N, 1);
int MAX = 0;
while (1) {
int sum = 0;
for (int i = 0; i < Q; i++)
if (A[Queries[i][1] - 1] - A[Queries[i][0] - 1] == Queries[i][2])
sum += Queries[i][3];
MAX = max(MAX, sum);
if (!nextvect(A, N, M))
break;
}
cout << MAX << endl;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool nextvect(vector<int> &A, int N, int M) {
if (A[N - 1] < M)
A[N - 1]++;
else {
int i = N - 2;
while (i >= 0 && A[i] == M)
i--;
if (i == -1)
return false;
A[i]++;
for (int j = i + 1; j < N; j++)
A[j] = A[i];
}
return true;
}
int main() {
int N, M, Q;
cin >> N >> M >> Q;
vector<vector<int>> Queries(Q, vector<int>(4));
for (int i = 0; i < Q; i++)
cin >> Queries[i][0] >> Queries[i][1] >> Queries[i][2] >> Queries[i][3];
vector<int> A(N, 1);
int MAX = 0;
while (1) {
int sum = 0;
for (int i = 0; i < Q; i++)
if (A[Queries[i][1] - 1] - A[Queries[i][0] - 1] == Queries[i][2])
sum += Queries[i][3];
MAX = max(MAX, sum);
if (!nextvect(A, N, M))
break;
}
cout << MAX << endl;
} | insert | 18 | 18 | 18 | 19 | 0 | |
p02695 | C++ | Time Limit Exceeded | #define _GLIBCXX_DEBUG
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using pss = pair<string, string>;
using pcc = pair<char, char>;
using pbb = pair<bool, bool>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using ti3 = tuple<int, int, int>;
using tl3 = tuple<ll, ll, ll>;
using td3 = tuple<double, double, double>;
using ts3 = tuple<string, string, string>;
using tc3 = tuple<char, char, char>;
using tb3 = tuple<bool, bool, bool>;
using ti4 = tuple<int, int, int, int>;
using tl4 = tuple<ll, ll, ll, ll>;
using td4 = tuple<double, double, double, double>;
using ts4 = tuple<string, string, string, string>;
using tc4 = tuple<char, char, char, char>;
using tb4 = tuple<bool, bool, bool, bool>;
using vi = vector<int>;
using vl = vector<ll>;
using vd = vector<double>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvd = vector<vd>;
using vvs = vector<vs>;
using vvc = vector<vc>;
using vvb = vector<vb>;
using vvvi = vector<vvi>;
using vvvl = vector<vvl>;
using vvvd = vector<vvd>;
using vvvs = vector<vvs>;
using vvvc = vector<vvc>;
using vvvb = vector<vvb>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
using vpss = vector<pss>;
using vpcc = vector<pcc>;
using vpbb = vector<pbb>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vti3 = vector<ti3>;
using vtl3 = vector<tl3>;
using vtd3 = vector<td3>;
using vts3 = vector<ts3>;
using vtc3 = vector<tc3>;
using vtb3 = vector<tb3>;
using vti4 = vector<ti4>;
using vtl4 = vector<tl4>;
using vtd4 = vector<td4>;
using vts4 = vector<ts4>;
using vtc4 = vector<tc4>;
using vtb4 = vector<tb4>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using msi = map<string, int>;
using mci = map<char, int>;
using mil = map<int, ll>;
using mli = map<ll, int>;
using si = set<int>;
using sl = set<ll>;
using sd = set<double>;
using ss = set<string>;
using sc = set<char>;
using sb = set<bool>;
using spii = set<pii>;
using spll = set<pll>;
using spdd = set<pdd>;
using spss = set<pss>;
using spcc = set<pcc>;
using spbb = set<pbb>;
using spil = set<pil>;
using spli = set<pli>;
using sti3 = set<ti3>;
using stl3 = set<tl3>;
using std3 = set<td3>;
using sts3 = set<ts3>;
using stc3 = set<tc3>;
using stb3 = set<tb3>;
#define rep0(TMS) for (int CNT = 0; CNT < (int)(TMS); CNT++)
#define rep(CNT, GOAL) for (int CNT = 0; CNT < (int)(GOAL); CNT++)
#define rep2(CNT, START, GOAL) \
for (int CNT = (int)(START); CNT < (int)(GOAL); CNT++)
#define rep3(CNT, START, GOAL) \
for (int CNT = (int)(START); CNT > (int)(GOAL); CNT--)
#define all(CONT) begin(CONT), end(CONT)
#define fr1(CONT) next(begin(CONT)), end(CONT)
#define itrep(ITR, CONT) for (auto ITR = begin(CONT); ITR != end(CONT); ITR++)
#define itrep1(ITR, CONT) \
for (auto ITR = next(begin(CONT)); ITR != end(CONT); ITR++)
#define maxel(CONT) *max_element(all(CONT))
#define minel(CONT) *min_element(all(CONT))
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> T sum(const vector<T> &VEC) {
return accumulate(all(VEC), 0.0);
}
template <typename T> vector<T> acm(const vector<T> &VEC) {
vector<T> RES(VEC.size() + 1);
rep(CNT, VEC.size()) RES[CNT + 1] = RES[CNT] + VEC[CNT];
return RES;
}
template <typename T> void fil(vector<T> &VEC, const int NUM, const T &VAL) {
VEC.assign(NUM, VAL);
}
template <typename T> void fil(vector<T> &VEC, const int NUM) {
VEC.assign(NUM, 0.0);
}
template <typename T>
void fil(vector<vector<T>> &VV, const int RNUM, const int CNUM, const T &VAL) {
fil(VV, RNUM, vector<T>());
rep(CNT, RNUM) fil(VV[CNT], CNUM, VAL);
}
template <typename T>
void fil(vector<vector<T>> &VV, const int RNUM, const int CNUM) {
fil(VV, RNUM, vector<T>());
rep(CNT, RNUM) fil(VV[CNT], CNUM);
}
template <typename T> void fil(vector<vector<T>> &VV, const int RNUM) {
fil(VV, RNUM, vector<T>());
}
void prec(const int &DIG) {
cerr << fixed << setprecision(DIG);
cout << fixed << setprecision(DIG);
}
template <typename T> void COUT(const T &ELEM) { cout << ELEM; }
template <typename T> void pout(const T &ELEM) {
COUT(ELEM);
cout << " ";
}
template <typename T, typename... Ts>
void pout(const T &FIRST, const Ts &...REST) {
pout(FIRST);
pout(REST...);
}
template <typename T> void print(T ELEM) {
COUT(ELEM);
cout << "\n";
}
template <typename T, typename... Ts>
void print(const T &FIRST, const Ts &...REST) {
print(FIRST);
print(REST...);
}
void CERR() { cerr << "\n"; }
template <typename T> void CERR(const T &ELEM) { cerr << ELEM; }
template <typename T, typename... Ts>
void CERR(const T &FIRST, const Ts &...REST) {
CERR(FIRST);
cerr << ", ";
CERR(REST...);
}
template <typename T1, typename T2> void CERR(const pair<T1, T2> &PAIR) {
cerr << "(";
CERR(PAIR.first);
cerr << ", ";
CERR(PAIR.second);
cerr << ")";
}
template <typename T1, typename T2, typename T3>
void CERR(const tuple<T1, T2, T3> &TUP3) {
cerr << "(";
CERR(get<0>(TUP3));
cerr << ", ";
CERR(get<1>(TUP3));
cerr << ", ";
CERR(get<2>(TUP3));
cerr << ")";
}
template <typename T1, typename T2, typename T3, typename T4>
void CERR(const tuple<T1, T2, T3, T4> &TUP4) {
cerr << "(";
CERR(get<0>(TUP4));
cerr << ", ";
CERR(get<1>(TUP4));
cerr << ", ";
CERR(get<2>(TUP4));
cerr << ", ";
CERR(get<3>(TUP4));
cerr << ")";
}
template <typename T> void CERR(const vector<T> &VEC) {
cerr << "{ ";
itrep(ITR, VEC) {
CERR(*ITR);
cerr << ", ";
}
cerr << "}";
}
template <typename T> void CERR1(const vector<T> &VEC) {
cerr << "{ ";
itrep1(ITR, VEC) {
CERR(*ITR);
cerr << ", ";
}
cerr << "}";
}
template <typename T> void CERR(const set<T> &SET) {
cerr << "{ ";
itrep(ITR, SET) {
CERR(*ITR);
cerr << ", ";
}
cerr << "}";
}
template <typename T1, typename T2> void CERR(const map<T1, T2> &MAP) {
cerr << "{ ";
itrep(ITR, MAP) {
CERR(*ITR);
cerr << ", ";
}
cerr << "}";
}
#define db(OBJ) \
cerr << #OBJ << ": "; \
CERR(OBJ); \
cerr << ", "
#define dbl(OBJ) \
cerr << #OBJ << ": "; \
CERR(OBJ); \
cerr << "\n"
#define db1(OBJ) \
cerr << #OBJ << ": "; \
CERR1(OBJ); \
cerr << "\n"
#define dbs(...) \
cerr << "(" << #__VA_ARGS__ << "): ("; \
CERR(__VA_ARGS__); \
cerr << ")\n"
#define dbvv(VV) \
cerr << #VV << ": {\n"; \
rep(CNT, VV.size()) { \
cerr << #VV << "[" << CNT << "]: "; \
CERR(VV[CNT]); \
cerr << ",\n"; \
} \
cerr << "}\n"
#define db01(VV) \
cerr << #VV << ": {\n"; \
rep(CNT, VV.size()) { \
cerr << #VV << "[" << CNT << "]: "; \
CERR1(VV[CNT]); \
cerr << ",\n"; \
} \
cerr << "}\n"
#define db10(VV) \
cerr << #VV << ": {\n"; \
rep2(CNT, 1, VV.size()) { \
cerr << #VV << "[" << CNT << "]: "; \
CERR(VV[CNT]); \
cerr << ",\n"; \
} \
cerr << "}\n"
#define db11(VV) \
cerr << #VV << ": {\n"; \
rep2(CNT, 1, VV.size()) { \
cerr << #VV << "[" << CNT << "]: "; \
CERR1(VV[CNT]); \
cerr << ",\n"; \
} \
cerr << "}\n"
#define YN(FLG) cout << (FLG ? "YES" : "NO") << "\n"
#define Yn(FLG) cout << (FLG ? "Yes" : "No") << "\n"
#define yn(FLG) cout << (FLG ? "yes" : "no") << "\n"
#define pcase(NUM) \
cout << "Case #" << NUM << ":" \
<< " "
#define pcasel(NUM) \
cout << "Case #" << NUM << ":" \
<< "\n"
// const ll INF = 1'000'000'000'000'000'007;
const int INF = 1'000'000'007;
const ll MOD = 1'000'000'007; // 998'2447'353;
int N, M, Q;
vi a, b, c, d;
int next_combination(int bitint) {
int x = bitint & -bitint; // bitの最下位の1 (Least Significant Bit)
int y = bitint + x; // bit + LSB
return (((bitint & ~y) / x) >> 1) | y; //
}
int solve(int bit) {
int res = 0;
vi A;
rep(i, M + N - 1) if (bit & (1 << i)) A.push_back(i - A.size());
rep(i, Q) if (A[b[i]] - A[a[i]] == c[i]) res += d[i];
dbl(A);
dbl(res);
return res;
}
int main() {
// 0-ind
cin >> N >> M >> Q;
fil(a, Q);
fil(b, Q);
fil(c, Q);
fil(d, Q);
rep(i, Q) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--;
b[i]--;
}
int ans = 0;
for (int bit = (1 << N) - 1; bit < (1 << (M + N - 1));
bit = next_combination(bit)) {
chmax(ans, solve(bit));
}
print(ans);
} | #define _GLIBCXX_DEBUG
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using pss = pair<string, string>;
using pcc = pair<char, char>;
using pbb = pair<bool, bool>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using ti3 = tuple<int, int, int>;
using tl3 = tuple<ll, ll, ll>;
using td3 = tuple<double, double, double>;
using ts3 = tuple<string, string, string>;
using tc3 = tuple<char, char, char>;
using tb3 = tuple<bool, bool, bool>;
using ti4 = tuple<int, int, int, int>;
using tl4 = tuple<ll, ll, ll, ll>;
using td4 = tuple<double, double, double, double>;
using ts4 = tuple<string, string, string, string>;
using tc4 = tuple<char, char, char, char>;
using tb4 = tuple<bool, bool, bool, bool>;
using vi = vector<int>;
using vl = vector<ll>;
using vd = vector<double>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvd = vector<vd>;
using vvs = vector<vs>;
using vvc = vector<vc>;
using vvb = vector<vb>;
using vvvi = vector<vvi>;
using vvvl = vector<vvl>;
using vvvd = vector<vvd>;
using vvvs = vector<vvs>;
using vvvc = vector<vvc>;
using vvvb = vector<vvb>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
using vpss = vector<pss>;
using vpcc = vector<pcc>;
using vpbb = vector<pbb>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vti3 = vector<ti3>;
using vtl3 = vector<tl3>;
using vtd3 = vector<td3>;
using vts3 = vector<ts3>;
using vtc3 = vector<tc3>;
using vtb3 = vector<tb3>;
using vti4 = vector<ti4>;
using vtl4 = vector<tl4>;
using vtd4 = vector<td4>;
using vts4 = vector<ts4>;
using vtc4 = vector<tc4>;
using vtb4 = vector<tb4>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using msi = map<string, int>;
using mci = map<char, int>;
using mil = map<int, ll>;
using mli = map<ll, int>;
using si = set<int>;
using sl = set<ll>;
using sd = set<double>;
using ss = set<string>;
using sc = set<char>;
using sb = set<bool>;
using spii = set<pii>;
using spll = set<pll>;
using spdd = set<pdd>;
using spss = set<pss>;
using spcc = set<pcc>;
using spbb = set<pbb>;
using spil = set<pil>;
using spli = set<pli>;
using sti3 = set<ti3>;
using stl3 = set<tl3>;
using std3 = set<td3>;
using sts3 = set<ts3>;
using stc3 = set<tc3>;
using stb3 = set<tb3>;
#define rep0(TMS) for (int CNT = 0; CNT < (int)(TMS); CNT++)
#define rep(CNT, GOAL) for (int CNT = 0; CNT < (int)(GOAL); CNT++)
#define rep2(CNT, START, GOAL) \
for (int CNT = (int)(START); CNT < (int)(GOAL); CNT++)
#define rep3(CNT, START, GOAL) \
for (int CNT = (int)(START); CNT > (int)(GOAL); CNT--)
#define all(CONT) begin(CONT), end(CONT)
#define fr1(CONT) next(begin(CONT)), end(CONT)
#define itrep(ITR, CONT) for (auto ITR = begin(CONT); ITR != end(CONT); ITR++)
#define itrep1(ITR, CONT) \
for (auto ITR = next(begin(CONT)); ITR != end(CONT); ITR++)
#define maxel(CONT) *max_element(all(CONT))
#define minel(CONT) *min_element(all(CONT))
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> T sum(const vector<T> &VEC) {
return accumulate(all(VEC), 0.0);
}
template <typename T> vector<T> acm(const vector<T> &VEC) {
vector<T> RES(VEC.size() + 1);
rep(CNT, VEC.size()) RES[CNT + 1] = RES[CNT] + VEC[CNT];
return RES;
}
template <typename T> void fil(vector<T> &VEC, const int NUM, const T &VAL) {
VEC.assign(NUM, VAL);
}
template <typename T> void fil(vector<T> &VEC, const int NUM) {
VEC.assign(NUM, 0.0);
}
template <typename T>
void fil(vector<vector<T>> &VV, const int RNUM, const int CNUM, const T &VAL) {
fil(VV, RNUM, vector<T>());
rep(CNT, RNUM) fil(VV[CNT], CNUM, VAL);
}
template <typename T>
void fil(vector<vector<T>> &VV, const int RNUM, const int CNUM) {
fil(VV, RNUM, vector<T>());
rep(CNT, RNUM) fil(VV[CNT], CNUM);
}
template <typename T> void fil(vector<vector<T>> &VV, const int RNUM) {
fil(VV, RNUM, vector<T>());
}
void prec(const int &DIG) {
cerr << fixed << setprecision(DIG);
cout << fixed << setprecision(DIG);
}
template <typename T> void COUT(const T &ELEM) { cout << ELEM; }
template <typename T> void pout(const T &ELEM) {
COUT(ELEM);
cout << " ";
}
template <typename T, typename... Ts>
void pout(const T &FIRST, const Ts &...REST) {
pout(FIRST);
pout(REST...);
}
template <typename T> void print(T ELEM) {
COUT(ELEM);
cout << "\n";
}
template <typename T, typename... Ts>
void print(const T &FIRST, const Ts &...REST) {
print(FIRST);
print(REST...);
}
void CERR() { cerr << "\n"; }
template <typename T> void CERR(const T &ELEM) { cerr << ELEM; }
template <typename T, typename... Ts>
void CERR(const T &FIRST, const Ts &...REST) {
CERR(FIRST);
cerr << ", ";
CERR(REST...);
}
template <typename T1, typename T2> void CERR(const pair<T1, T2> &PAIR) {
cerr << "(";
CERR(PAIR.first);
cerr << ", ";
CERR(PAIR.second);
cerr << ")";
}
template <typename T1, typename T2, typename T3>
void CERR(const tuple<T1, T2, T3> &TUP3) {
cerr << "(";
CERR(get<0>(TUP3));
cerr << ", ";
CERR(get<1>(TUP3));
cerr << ", ";
CERR(get<2>(TUP3));
cerr << ")";
}
template <typename T1, typename T2, typename T3, typename T4>
void CERR(const tuple<T1, T2, T3, T4> &TUP4) {
cerr << "(";
CERR(get<0>(TUP4));
cerr << ", ";
CERR(get<1>(TUP4));
cerr << ", ";
CERR(get<2>(TUP4));
cerr << ", ";
CERR(get<3>(TUP4));
cerr << ")";
}
template <typename T> void CERR(const vector<T> &VEC) {
cerr << "{ ";
itrep(ITR, VEC) {
CERR(*ITR);
cerr << ", ";
}
cerr << "}";
}
template <typename T> void CERR1(const vector<T> &VEC) {
cerr << "{ ";
itrep1(ITR, VEC) {
CERR(*ITR);
cerr << ", ";
}
cerr << "}";
}
template <typename T> void CERR(const set<T> &SET) {
cerr << "{ ";
itrep(ITR, SET) {
CERR(*ITR);
cerr << ", ";
}
cerr << "}";
}
template <typename T1, typename T2> void CERR(const map<T1, T2> &MAP) {
cerr << "{ ";
itrep(ITR, MAP) {
CERR(*ITR);
cerr << ", ";
}
cerr << "}";
}
#define db(OBJ) \
cerr << #OBJ << ": "; \
CERR(OBJ); \
cerr << ", "
#define dbl(OBJ) \
cerr << #OBJ << ": "; \
CERR(OBJ); \
cerr << "\n"
#define db1(OBJ) \
cerr << #OBJ << ": "; \
CERR1(OBJ); \
cerr << "\n"
#define dbs(...) \
cerr << "(" << #__VA_ARGS__ << "): ("; \
CERR(__VA_ARGS__); \
cerr << ")\n"
#define dbvv(VV) \
cerr << #VV << ": {\n"; \
rep(CNT, VV.size()) { \
cerr << #VV << "[" << CNT << "]: "; \
CERR(VV[CNT]); \
cerr << ",\n"; \
} \
cerr << "}\n"
#define db01(VV) \
cerr << #VV << ": {\n"; \
rep(CNT, VV.size()) { \
cerr << #VV << "[" << CNT << "]: "; \
CERR1(VV[CNT]); \
cerr << ",\n"; \
} \
cerr << "}\n"
#define db10(VV) \
cerr << #VV << ": {\n"; \
rep2(CNT, 1, VV.size()) { \
cerr << #VV << "[" << CNT << "]: "; \
CERR(VV[CNT]); \
cerr << ",\n"; \
} \
cerr << "}\n"
#define db11(VV) \
cerr << #VV << ": {\n"; \
rep2(CNT, 1, VV.size()) { \
cerr << #VV << "[" << CNT << "]: "; \
CERR1(VV[CNT]); \
cerr << ",\n"; \
} \
cerr << "}\n"
#define YN(FLG) cout << (FLG ? "YES" : "NO") << "\n"
#define Yn(FLG) cout << (FLG ? "Yes" : "No") << "\n"
#define yn(FLG) cout << (FLG ? "yes" : "no") << "\n"
#define pcase(NUM) \
cout << "Case #" << NUM << ":" \
<< " "
#define pcasel(NUM) \
cout << "Case #" << NUM << ":" \
<< "\n"
// const ll INF = 1'000'000'000'000'000'007;
const int INF = 1'000'000'007;
const ll MOD = 1'000'000'007; // 998'2447'353;
int N, M, Q;
vi a, b, c, d;
int next_combination(int bitint) {
int x = bitint & -bitint; // bitの最下位の1 (Least Significant Bit)
int y = bitint + x; // bit + LSB
return (((bitint & ~y) / x) >> 1) | y; //
}
int solve(int bit) {
int res = 0;
vi A;
rep(i, M + N - 1) if (bit & (1 << i)) A.push_back(i - A.size());
rep(i, Q) if (A[b[i]] - A[a[i]] == c[i]) res += d[i];
// dbl(A); dbl(res);
return res;
}
int main() {
// 0-ind
cin >> N >> M >> Q;
fil(a, Q);
fil(b, Q);
fil(c, Q);
fil(d, Q);
rep(i, Q) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--;
b[i]--;
}
int ans = 0;
for (int bit = (1 << N) - 1; bit < (1 << (M + N - 1));
bit = next_combination(bit)) {
chmax(ans, solve(bit));
}
print(ans);
} | replace | 310 | 312 | 310 | 311 | TLE | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define M 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
for (long blockTime = 0; \
(blockTime == 0 ? (blockTime = clock()) != 0 : false); \
debug("%s time : %.4fs", d, \
(double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 2e18
#define sm -1e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;
#define ordered_set \
tree<pii, null_type, less<pii>, rb_tree_tag, \
tree_order_statistics_node_update>
void pr_init() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
}
int a[11], b[11], c[11], d[11], n, m, q, an = -1;
void recur(int cur, int rm, vii vc) {
if (cur > m && rm != 0)
return;
if (rm == 0) {
int cur = 0;
forf(j, 0, q) {
int f = a[j] - 1, s = b[j] - 1;
if (vc[s] - vc[f] == c[j])
cur += d[j];
}
an = max(an, cur);
return;
}
recur(cur + 1, rm, vc);
vc.pb(cur);
recur(cur + 1, rm - 1, vc);
recur(cur, rm - 1, vc);
}
void solve() {
cin >> n >> m >> q;
forf(i, 0, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
vii emp;
recur(1, n, emp);
cout << an << endl;
}
int32_t main() {
pr_init();
fastio;
auto start = high_resolution_clock::now();
solve();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
// cout << "Time taken by function: "
// << duration.count() << " microseconds" << endl;
}
| #include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define M 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
for (long blockTime = 0; \
(blockTime == 0 ? (blockTime = clock()) != 0 : false); \
debug("%s time : %.4fs", d, \
(double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 2e18
#define sm -1e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;
#define ordered_set \
tree<pii, null_type, less<pii>, rb_tree_tag, \
tree_order_statistics_node_update>
void pr_init() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
}
int a[55], b[55], c[55], d[100001], n, m, q, an = -1;
void recur(int cur, int rm, vii vc) {
if (cur > m && rm != 0)
return;
if (rm == 0) {
int cur = 0;
forf(j, 0, q) {
int f = a[j] - 1, s = b[j] - 1;
if (vc[s] - vc[f] == c[j])
cur += d[j];
}
an = max(an, cur);
return;
}
recur(cur + 1, rm, vc);
vc.pb(cur);
recur(cur + 1, rm - 1, vc);
recur(cur, rm - 1, vc);
}
void solve() {
cin >> n >> m >> q;
forf(i, 0, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
vii emp;
recur(1, n, emp);
cout << an << endl;
}
int32_t main() {
pr_init();
fastio;
auto start = high_resolution_clock::now();
solve();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
// cout << "Time taken by function: "
// << duration.count() << " microseconds" << endl;
}
| replace | 55 | 56 | 55 | 56 | 0 | |
p02695 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define INF 1e9
#define MAXN 100005
#define MAXM 100005
#define MOD 1000000007
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define pii pair<int, int>
int ans = 0, N, M, Q;
vi A, B, C, D, num;
int checkReq(vi tmp) {
int res = 0;
rep(i, Q) {
if (tmp[B[i]] - tmp[A[i]] == C[i])
res += D[i];
}
return res;
}
bool dfs(vi tmp) {
int n = tmp.size();
if (n == N) {
// rep(i, N)
// cout << tmp[i+1] << " ";
// cout << endl;
ans = max(ans, checkReq(tmp));
} else {
tmp.push_back(tmp[n - 1]);
while (tmp[n] <= M) {
dfs(tmp);
tmp[n]++;
}
tmp.pop_back();
}
}
void solve() {
rep(i, M) {
num.push_back(i + 1);
dfs(num);
num.pop_back();
}
cout << ans << endl;
}
int main() {
cin >> N >> M >> Q;
A.resize(Q);
B.resize(Q);
C.resize(Q);
D.resize(Q);
rep(i, Q) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
--A[i], --B[i];
}
solve();
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define INF 1e9
#define MAXN 100005
#define MAXM 100005
#define MOD 1000000007
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define pii pair<int, int>
int ans = 0, N, M, Q;
vi A, B, C, D, num;
int checkReq(vi tmp) {
int res = 0;
rep(i, Q) {
if (tmp[B[i]] - tmp[A[i]] == C[i])
res += D[i];
}
return res;
}
void dfs(vi tmp) {
int n = tmp.size();
if (n == N) {
// rep(i, N)
// cout << tmp[i+1] << " ";
// cout << endl;
ans = max(ans, checkReq(tmp));
} else {
tmp.push_back(tmp[n - 1]);
while (tmp[n] <= M) {
dfs(tmp);
tmp[n]++;
}
tmp.pop_back();
}
}
void solve() {
rep(i, M) {
num.push_back(i + 1);
dfs(num);
num.pop_back();
}
cout << ans << endl;
}
int main() {
cin >> N >> M >> Q;
A.resize(Q);
B.resize(Q);
C.resize(Q);
D.resize(Q);
rep(i, Q) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
--A[i], --B[i];
}
solve();
}
| replace | 32 | 33 | 32 | 33 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef struct QRY {
int a;
int b;
int c;
int d;
} qry_t;
int main(void) {
int N, M, Q;
cin >> N >> M >> Q;
vector<qry_t> qry(Q);
for (int i = 0; i < Q; ++i) {
cin >> qry.at(i).a >> qry.at(i).b >> qry.at(i).c >> qry.at(i).d;
}
vector<vector<int>> A(1, vector<int>(M)), Am(0, vector<int>(M));
A.at(0).at(0) = 1;
/* 数列をDFSで列挙 */
for (int i = 1; i < N; i++) {
Am = A;
A.clear();
for (auto it = Am.begin(); it != Am.end(); ++it) {
for (int j = it->at(i - 1); j <= M; ++j) {
vector<int> a = *it;
a[i] = j;
A.push_back(a);
}
}
}
int ans = 0;
for (auto it = A.begin(); it != A.end(); ++it) {
int tmp = 0;
for (int i = 0; i < Q; ++i) {
if (it->at(qry.at(i).b - 1) - it->at(qry.at(i).a - 1) == qry.at(i).c) {
tmp += qry.at(i).d;
}
}
ans = max(tmp, ans);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef struct QRY {
int a;
int b;
int c;
int d;
} qry_t;
int main(void) {
int N, M, Q;
cin >> N >> M >> Q;
vector<qry_t> qry(Q);
for (int i = 0; i < Q; ++i) {
cin >> qry.at(i).a >> qry.at(i).b >> qry.at(i).c >> qry.at(i).d;
}
vector<vector<int>> A(1, vector<int>(N)), Am(0, vector<int>(N));
A.at(0).at(0) = 1;
/* 数列をDFSで列挙 */
for (int i = 1; i < N; i++) {
Am = A;
A.clear();
for (auto it = Am.begin(); it != Am.end(); ++it) {
for (int j = it->at(i - 1); j <= M; ++j) {
vector<int> a = *it;
a[i] = j;
A.push_back(a);
}
}
}
int ans = 0;
for (auto it = A.begin(); it != A.end(); ++it) {
int tmp = 0;
for (int i = 0; i < Q; ++i) {
if (it->at(qry.at(i).b - 1) - it->at(qry.at(i).a - 1) == qry.at(i).c) {
tmp += qry.at(i).d;
}
}
ans = max(tmp, ans);
}
cout << ans << endl;
return 0;
} | replace | 19 | 20 | 19 | 20 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
struct info {
int a, b, c, d;
};
int main() {
int n, m, q;
cin >> n >> m >> q;
vector<info> v(n);
for (int i = 0; i < q; i++) {
info in;
cin >> in.a >> in.b >> in.c >> in.d;
v[i] = in;
}
queue<vector<int>> que;
que.push({1});
int cur_mx = 0;
while (!que.empty()) {
vector<int> front = que.front();
que.pop();
int sz = front.size();
if (sz == n) {
int score = 0;
for (int i = 0; i < q; i++) {
int a = v[i].a;
int b = v[i].b;
int c = v[i].c;
int d = v[i].d;
if (front[b - 1] - front[a - 1] == c) {
score += d;
}
}
cur_mx = max(cur_mx, score);
} else {
int last = front[sz - 1];
for (int i = last; i <= m; i++) {
vector<int> nw = front;
nw.push_back(i);
que.push(nw);
}
}
}
cout << cur_mx;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
struct info {
int a, b, c, d;
};
int main() {
int n, m, q;
cin >> n >> m >> q;
vector<info> v(q);
for (int i = 0; i < q; i++) {
info in;
cin >> in.a >> in.b >> in.c >> in.d;
v[i] = in;
}
queue<vector<int>> que;
que.push({1});
int cur_mx = 0;
while (!que.empty()) {
vector<int> front = que.front();
que.pop();
int sz = front.size();
if (sz == n) {
int score = 0;
for (int i = 0; i < q; i++) {
int a = v[i].a;
int b = v[i].b;
int c = v[i].c;
int d = v[i].d;
if (front[b - 1] - front[a - 1] == c) {
score += d;
}
}
cur_mx = max(cur_mx, score);
} else {
int last = front[sz - 1];
for (int i = last; i <= m; i++) {
vector<int> nw = front;
nw.push_back(i);
que.push(nw);
}
}
}
cout << cur_mx;
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p02695 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i, a, N) for ((i) = (a); (i) < (N); (i)++)
#define rrp(i, N, a) for ((i) = (N)-1; (i) >= (a); (i)--)
#define llfor ll i, j
#define sc(a) cin >> a
#define pr(a) cout << a << endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\n")
#define pb(b) push_back(b)
#define all(a) a.begin(), a.end()
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define size(a, b) (a, vector<ll>(b))
/*
UnionFind:素集合系管理の構造体(union by size)
isSame(x, y): x と y が同じ集合にいるか。 計算量はならし O(α(n))
unite(x, y): x と y を同じ集合にする。計算量はならし O(α(n))
treeSize(x): x を含む集合の要素数。
*/
struct UnionFind {
vector<int> size, parents;
UnionFind() {}
UnionFind(int n) { // make n trees.
size.resize(n, 0);
parents.resize(n, 0);
for (int i = 0; i < n; i++)
makeTree(i);
}
void makeTree(int x) {
parents[x] = x; // the parent of x is x
size[x] = 1;
}
bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
bool unite(int x, int y) {
x = findRoot(x);
y = findRoot(y);
if (x == y)
return false;
if (size[x] > size[y]) {
parents[y] = x;
size[x] += size[y];
} else {
parents[x] = y;
size[y] += size[x];
}
return true;
}
int findRoot(int x) { // 木の根を探す,計算量削減のため低く
if (x != parents[x])
parents[x] = findRoot(parents[x]);
return parents[x];
}
int treeSize(int x) { return size[findRoot(x)]; }
};
/*mod*/ const int mod = 1e9 + 7;
/*pi*/ const double pi = acos(-1);
/*繰り上げ除算*/ ll cei(ll x, ll y) {
ll ans = x / y;
if (x % y != 0)
ans++;
return ans;
}
/*最大公約数*/ ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
/*最小公倍数*/ ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
/*n乗 1*/ ll llpow(ll x, ll n) {
ll ans = 1, i;
rep(i, 0, n) {
ans *= x;
ans %= mod;
}
return ans;
}
/*n乗 2*/ ll modpow(ll x, ll n) {
ll ans = x, i, tmp = x, t1 = 1;
i = 1;
while (i < n) {
if (i * 2 < n) {
ans *= ans;
ans %= mod;
i *= 2;
} else {
t1 = 1;
tmp = x;
while (t1 * 2 < n - i) {
tmp *= tmp;
tmp %= mod;
t1 *= 2;
}
ans *= tmp;
ans %= mod;
i += t1;
}
}
return ans;
}
/*階乗*/ ll fact(ll x) {
ll i, ans = 1;
rep(i, 0, x) {
ans *= (x - i);
ans %= mod;
}
return ans;
}
/*逆元*/ ll inv(ll x) { return modpow(x, mod - 2); }
/*nCr*/ ll ncr(ll n, ll r) {
ll a = fact(n), b = modpow(fact(n - r), mod - 2),
c = modpow(fact(r), mod - 2);
ll ans = a * b;
ans %= mod;
ans *= c;
ans %= mod;
return ans;
}
/*nPr*/ ll npr(ll n, ll r) {
ll a = fact(n), b = modpow(fact(n - r), mod - 2);
return (a * b) % mod;
}
/*primejudge*/ bool prime(ll a) {
if (a <= 1)
return false;
ll i;
for (i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
/*Fibonacci数列*/ ll fib(ll x) {
ll i, fibo[x + 10];
fibo[0] = 1;
fibo[1] = 1;
fibo[2] = 1;
rep(i, 3, x + 1) fibo[i] = fibo[i - 1] + fibo[i - 2];
return fibo[x];
}
/*桁数*/ ll dig(ll n) {
ll d = 0, tmp = n;
while (tmp / 10 > 0) {
tmp /= 10;
d++;
}
return d;
}
/*迷路移動*/ const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
llfor; ////////////////////////////////////////////////////////////
ll n, m, q, num[20], ans;
ll a[20], b[20], c[20], d[20];
void making(ll x) {
ll y1;
ll st = (x == 0 ? 1 : num[x - 1]);
rep(y1, st, m + 1) {
num[x] = y1;
if (x == n - 1) {
ll cnt = 0;
rep(i, 0, q) {
if (num[b[i]] - num[a[i]] == c[i])
cnt += d[i];
}
// rep(i,0,n)cout<<num[i]<<" ";pr(cnt);
ans = max(ans, cnt);
} else
making(x + 1);
}
}
int main() {
cin >> n >> m >> q;
rep(i, 0, q) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--;
b[i]--;
}
ans = 0;
making(0);
pr(ans);
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i, a, N) for ((i) = (a); (i) < (N); (i)++)
#define rrp(i, N, a) for ((i) = (N)-1; (i) >= (a); (i)--)
#define llfor ll i, j
#define sc(a) cin >> a
#define pr(a) cout << a << endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\n")
#define pb(b) push_back(b)
#define all(a) a.begin(), a.end()
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define size(a, b) (a, vector<ll>(b))
/*
UnionFind:素集合系管理の構造体(union by size)
isSame(x, y): x と y が同じ集合にいるか。 計算量はならし O(α(n))
unite(x, y): x と y を同じ集合にする。計算量はならし O(α(n))
treeSize(x): x を含む集合の要素数。
*/
struct UnionFind {
vector<int> size, parents;
UnionFind() {}
UnionFind(int n) { // make n trees.
size.resize(n, 0);
parents.resize(n, 0);
for (int i = 0; i < n; i++)
makeTree(i);
}
void makeTree(int x) {
parents[x] = x; // the parent of x is x
size[x] = 1;
}
bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
bool unite(int x, int y) {
x = findRoot(x);
y = findRoot(y);
if (x == y)
return false;
if (size[x] > size[y]) {
parents[y] = x;
size[x] += size[y];
} else {
parents[x] = y;
size[y] += size[x];
}
return true;
}
int findRoot(int x) { // 木の根を探す,計算量削減のため低く
if (x != parents[x])
parents[x] = findRoot(parents[x]);
return parents[x];
}
int treeSize(int x) { return size[findRoot(x)]; }
};
/*mod*/ const int mod = 1e9 + 7;
/*pi*/ const double pi = acos(-1);
/*繰り上げ除算*/ ll cei(ll x, ll y) {
ll ans = x / y;
if (x % y != 0)
ans++;
return ans;
}
/*最大公約数*/ ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
/*最小公倍数*/ ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
/*n乗 1*/ ll llpow(ll x, ll n) {
ll ans = 1, i;
rep(i, 0, n) {
ans *= x;
ans %= mod;
}
return ans;
}
/*n乗 2*/ ll modpow(ll x, ll n) {
ll ans = x, i, tmp = x, t1 = 1;
i = 1;
while (i < n) {
if (i * 2 < n) {
ans *= ans;
ans %= mod;
i *= 2;
} else {
t1 = 1;
tmp = x;
while (t1 * 2 < n - i) {
tmp *= tmp;
tmp %= mod;
t1 *= 2;
}
ans *= tmp;
ans %= mod;
i += t1;
}
}
return ans;
}
/*階乗*/ ll fact(ll x) {
ll i, ans = 1;
rep(i, 0, x) {
ans *= (x - i);
ans %= mod;
}
return ans;
}
/*逆元*/ ll inv(ll x) { return modpow(x, mod - 2); }
/*nCr*/ ll ncr(ll n, ll r) {
ll a = fact(n), b = modpow(fact(n - r), mod - 2),
c = modpow(fact(r), mod - 2);
ll ans = a * b;
ans %= mod;
ans *= c;
ans %= mod;
return ans;
}
/*nPr*/ ll npr(ll n, ll r) {
ll a = fact(n), b = modpow(fact(n - r), mod - 2);
return (a * b) % mod;
}
/*primejudge*/ bool prime(ll a) {
if (a <= 1)
return false;
ll i;
for (i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
/*Fibonacci数列*/ ll fib(ll x) {
ll i, fibo[x + 10];
fibo[0] = 1;
fibo[1] = 1;
fibo[2] = 1;
rep(i, 3, x + 1) fibo[i] = fibo[i - 1] + fibo[i - 2];
return fibo[x];
}
/*桁数*/ ll dig(ll n) {
ll d = 0, tmp = n;
while (tmp / 10 > 0) {
tmp /= 10;
d++;
}
return d;
}
/*迷路移動*/ const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
llfor; ////////////////////////////////////////////////////////////
ll n, m, q, num[100], ans;
ll a[100], b[100], c[100], d[100];
void making(ll x) {
ll y1;
ll st = (x == 0 ? 1 : num[x - 1]);
rep(y1, st, m + 1) {
num[x] = y1;
if (x == n - 1) {
ll cnt = 0;
rep(i, 0, q) {
if (num[b[i]] - num[a[i]] == c[i])
cnt += d[i];
}
// rep(i,0,n)cout<<num[i]<<" ";pr(cnt);
ans = max(ans, cnt);
} else
making(x + 1);
}
}
int main() {
cin >> n >> m >> q;
rep(i, 0, q) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--;
b[i]--;
}
ans = 0;
making(0);
pr(ans);
return 0;
} | replace | 171 | 173 | 171 | 173 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M, Q;
cin >> N >> M >> Q;
vector<int> a(Q);
vector<int> b(Q);
vector<int> c(Q);
vector<int> d(Q);
for (int i = 0; i < Q; i++) {
cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
}
int ans = 0;
int hozonnum = 0;
if (Q == 1) {
cout << d.at(0);
exit(0);
}
vector<int> hozon(N);
for (int i = 1; i <= M; i++) {
hozon.at(0) = i;
for (int j = i; j <= M; j++) {
hozon.at(1) = j;
for (int k = j; k <= M; k++) {
if (N == 2) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) == hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(2) = k;
for (int x = k; x <= M; x++) {
if (N == 3) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) == hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(3) = x;
for (int y = x; y <= M; y++) {
if (N == 4) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) == hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(4) = y;
for (int z = y; z <= M; z++) {
if (N == 5) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(5) = z;
for (int q = z; q <= M; q++) {
if (N == 6) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(6) = q;
for (int p = q; p <= M; p++) {
if (N == 7) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(7) = p;
for (int w = p; w <= M; w++) {
if (N == 8) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(8) = w;
for (int l = w; l <= M; l++) {
if (N == 9) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(9) = l;
if (N == 10) {
for (int g = 0; i < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
}
}
}
}
}
}
}
}
}
}
}
cout << hozonnum;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M, Q;
cin >> N >> M >> Q;
vector<int> a(Q);
vector<int> b(Q);
vector<int> c(Q);
vector<int> d(Q);
for (int i = 0; i < Q; i++) {
cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
}
int ans = 0;
int hozonnum = 0;
if (Q == 1) {
cout << d.at(0);
exit(0);
}
vector<int> hozon(N);
for (int i = 1; i <= M; i++) {
hozon.at(0) = i;
for (int j = i; j <= M; j++) {
hozon.at(1) = j;
for (int k = j; k <= M; k++) {
if (N == 2) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) == hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(2) = k;
for (int x = k; x <= M; x++) {
if (N == 3) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) == hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(3) = x;
for (int y = x; y <= M; y++) {
if (N == 4) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) == hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(4) = y;
for (int z = y; z <= M; z++) {
if (N == 5) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(5) = z;
for (int q = z; q <= M; q++) {
if (N == 6) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(6) = q;
for (int p = q; p <= M; p++) {
if (N == 7) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(7) = p;
for (int w = p; w <= M; w++) {
if (N == 8) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(8) = w;
for (int l = w; l <= M; l++) {
if (N == 9) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
break;
}
hozon.at(9) = l;
if (N == 10) {
for (int g = 0; g < Q; g++) {
if (hozon.at(b.at(g) - 1) ==
hozon.at(a.at(g) - 1) + c.at(g)) {
ans += d.at(g);
}
}
if (hozonnum < ans) {
hozonnum = ans;
}
ans = 0;
}
}
}
}
}
}
}
}
}
}
}
cout << hozonnum;
return 0;
} | replace | 150 | 151 | 150 | 151 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <string>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;
/*
for(i=0; i<N; i++)
cin >> a[i];
*/
int main(void) {
ll i, j, k;
ll N, M, Q;
cin >> N >> M >> Q;
ll a[11], b[11], c[11], d[11];
for (i = 0; i < Q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
ll ans = 0;
for (ll S = 0; S < 1 << M; S++) {
vector<ll> va;
for (i = 0; i < M; i++) {
if (S >> i & 1)
va.push_back(i);
}
for (ll T = 0; T < 1 << (N - 1); T++) {
vector<ll> vp;
vp.push_back(-1);
for (i = 0; i < N - 1; i++) {
if (T >> i & 1)
vp.push_back(i);
}
vp.push_back(N - 1);
if (vp.size() != va.size() + 1)
continue;
ll u[11] = {};
for (i = 0; i < vp.size() - 1; i++) {
for (j = vp[i] + 1; j <= vp[i + 1]; j++) {
u[j] = va[i];
}
}
ll t = 0;
for (i = 0; i < Q; i++) {
ll l = a[i] - 1;
ll r = b[i] - 1;
if (u[r] - u[l] == c[i])
t += d[i];
}
chmax(ans, t);
}
}
pt(ans);
}
| #include <bits/stdc++.h>
#include <string>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;
/*
for(i=0; i<N; i++)
cin >> a[i];
*/
int main(void) {
ll i, j, k;
ll N, M, Q;
cin >> N >> M >> Q;
ll a[55], b[55], c[55], d[55];
for (i = 0; i < Q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
ll ans = 0;
for (ll S = 0; S < 1 << M; S++) {
vector<ll> va;
for (i = 0; i < M; i++) {
if (S >> i & 1)
va.push_back(i);
}
for (ll T = 0; T < 1 << (N - 1); T++) {
vector<ll> vp;
vp.push_back(-1);
for (i = 0; i < N - 1; i++) {
if (T >> i & 1)
vp.push_back(i);
}
vp.push_back(N - 1);
if (vp.size() != va.size() + 1)
continue;
ll u[11] = {};
for (i = 0; i < vp.size() - 1; i++) {
for (j = vp[i] + 1; j <= vp[i + 1]; j++) {
u[j] = va[i];
}
}
ll t = 0;
for (i = 0; i < Q; i++) {
ll l = a[i] - 1;
ll r = b[i] - 1;
if (u[r] - u[l] == c[i])
t += d[i];
}
chmax(ans, t);
}
}
pt(ans);
}
| replace | 26 | 27 | 26 | 27 | 0 | |
p02695 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int n, m, q;
vector<vector<unordered_map<int, int>>> v;
void dfs(vector<int> &cur, int score, int &res) {
if ((int)cur.size() == n) {
res = max(res, score);
return;
}
int i = cur.empty() ? 1 : cur.back();
for (; i <= m; i++) {
cur.push_back(i);
int ns = score;
for (int j = 0; j < (int)cur.size() - 1; j++) {
ns += v[cur.size()][j + 1][i - cur[j]];
}
dfs(cur, ns, res);
cur.pop_back();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> q;
v = vector<vector<unordered_map<int, int>>>(
m + 1, vector<unordered_map<int, int>>(m + 1, unordered_map<int, int>()));
for (int i = 0; i < q; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
v[b][a][c] = d;
}
int res = 0;
vector<int> cur;
dfs(cur, 0, res);
cout << res << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int n, m, q;
vector<vector<unordered_map<int, int>>> v;
void dfs(vector<int> &cur, int score, int &res) {
if ((int)cur.size() == n) {
res = max(res, score);
return;
}
int i = cur.empty() ? 1 : cur.back();
for (; i <= m; i++) {
cur.push_back(i);
int ns = score;
for (int j = 0; j < (int)cur.size() - 1; j++) {
ns += v[cur.size()][j + 1][i - cur[j]];
}
dfs(cur, ns, res);
cur.pop_back();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> q;
v = vector<vector<unordered_map<int, int>>>(
n + 1, vector<unordered_map<int, int>>(n + 1, unordered_map<int, int>()));
for (int i = 0; i < q; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
v[b][a][c] = d;
}
int res = 0;
vector<int> cur;
dfs(cur, 0, res);
cout << res << endl;
return 0;
}
| replace | 31 | 32 | 31 | 32 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long int;
using vi = vector<ll>;
ll n, m, k;
ll ans = 0, sum = 0;
/*--------------------template--------------------*/
vi combs;
vi a(10), b(10), c(10), d(10);
void dfs(ll j, const ll &n, const ll &m, const ll &k, ll &ans) {
if (combs.size() == n) {
sum = 0;
REP(i, k) {
if (combs[b[i] - 1] - combs[a[i] - 1] == c[i])
sum += d[i];
}
ans = max(ans, sum);
combs.pop_back();
return;
}
for (ll jj = j; jj <= m; ++jj) {
combs.push_back(jj);
dfs(jj, n, m, k, ans);
}
combs.pop_back();
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
REP(i, k) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
dfs(1, n, m, k, ans);
cout << ans << endl;
}
| #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long int;
using vi = vector<ll>;
ll n, m, k;
ll ans = 0, sum = 0;
/*--------------------template--------------------*/
vi combs;
vi a(50), b(50), c(50), d(50);
void dfs(ll j, const ll &n, const ll &m, const ll &k, ll &ans) {
if (combs.size() == n) {
sum = 0;
REP(i, k) {
if (combs[b[i] - 1] - combs[a[i] - 1] == c[i])
sum += d[i];
}
ans = max(ans, sum);
combs.pop_back();
return;
}
for (ll jj = j; jj <= m; ++jj) {
combs.push_back(jj);
dfs(jj, n, m, k, ans);
}
combs.pop_back();
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
REP(i, k) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
dfs(1, n, m, k, ans);
cout << ans << endl;
}
| replace | 18 | 19 | 18 | 19 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
if (a > b)
a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
if (a < b)
a = b;
}
vector<ll> a(15), b(15), c(15), d(15);
ll N, M, Q;
vector<ll> A(15);
ll best = 0;
ll solve(ll cur, ll prev) {
if (cur == N) {
ll tmp = 0;
/*rep(i,N){
cout << A[i] << " ";
}
cout << endl;*/
rep(i, Q) {
if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
tmp += d[i];
}
}
cmax(best, tmp);
return 0;
} else {
REP(i, prev, M + 1) {
A[cur] = i;
solve(ll(cur + 1), ll(i));
}
return 0;
}
}
int main() {
// ll N,M,Q;
string S;
cin >> N >> M >> Q;
rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
solve(ll(0), ll(1));
cout << best << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
if (a > b)
a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
if (a < b)
a = b;
}
vector<ll> a(60), b(60), c(60), d(60);
ll N, M, Q;
vector<ll> A(15);
ll best = 0;
ll solve(ll cur, ll prev) {
if (cur == N) {
ll tmp = 0;
/*rep(i,N){
cout << A[i] << " ";
}
cout << endl;*/
rep(i, Q) {
if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
tmp += d[i];
}
}
cmax(best, tmp);
return 0;
} else {
REP(i, prev, M + 1) {
A[cur] = i;
solve(ll(cur + 1), ll(i));
}
return 0;
}
}
int main() {
// ll N,M,Q;
string S;
cin >> N >> M >> Q;
rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
solve(ll(0), ll(1));
cout << best << endl;
} | replace | 18 | 19 | 18 | 19 | 0 | |
p02695 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
int n, m, q;
int ans;
vector<int> a, b, c, d;
void dfs(vector<int> A) {
if (A.size() == n + 1) {
int now = 0;
rep(i, q) {
if (A[b[i]] - A[a[i]] == c[i])
now += d[i];
}
ans = max(ans, now);
}
A.push_back(A.back());
while (A.back() <= m) {
dfs(A);
A.back()++;
}
}
int main() {
cin >> n >> m >> q;
a = b = c = d = vector<int>(q);
rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
dfs(vector<int>(1, 1));
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
int n, m, q;
int ans;
vector<int> a, b, c, d;
void dfs(vector<int> A) {
if (A.size() == n + 1) {
int now = 0;
rep(i, q) {
if (A[b[i]] - A[a[i]] == c[i])
now += d[i];
}
ans = max(ans, now);
return;
}
A.push_back(A.back());
while (A.back() <= m) {
dfs(A);
A.back()++;
}
}
int main() {
cin >> n >> m >> q;
a = b = c = d = vector<int>(q);
rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
dfs(vector<int>(1, 1));
cout << ans << endl;
} | insert | 17 | 17 | 17 | 18 | TLE | |
p02695 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
int n, m, q;
vector<int> a, b, c, d;
int ans;
void dfs(vector<int> A) {
if (A.size() == n + 1) {
int now = 0;
for (int i = 0; i < q; i++) {
if (A[b[i]] - A[a[i]] == c[i])
now += d[i];
}
ans = max(ans, now);
}
A.push_back(A.back());
while (A.back() <= m) {
dfs(A);
A.back()++;
}
}
int main() {
cin >> n >> m >> q;
a = b = c = d = vector<int>(q);
for (int i = 0; i < q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
vector<int> A(1, 1);
dfs(A);
cout << ans << endl;
} | #include <iostream>
#include <vector>
using namespace std;
int n, m, q;
vector<int> a, b, c, d;
int ans;
void dfs(vector<int> A) {
if (A.size() == n + 1) {
int now = 0;
for (int i = 0; i < q; i++) {
if (A[b[i]] - A[a[i]] == c[i])
now += d[i];
}
ans = max(ans, now);
return;
}
A.push_back(A.back());
while (A.back() <= m) {
dfs(A);
A.back()++;
}
}
int main() {
cin >> n >> m >> q;
a = b = c = d = vector<int>(q);
for (int i = 0; i < q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
vector<int> A(1, 1);
dfs(A);
cout << ans << endl;
} | insert | 15 | 15 | 15 | 16 | TLE | |
p02695 | C++ | Runtime Error | #include <iostream>
using namespace std;
int a[10], b[10], c[10], d[10], e[10];
int n, m, q;
int ret = 0;
void dfs(int pos, int val) {
if (pos == n) {
int tmp = 0;
for (int i = 0; i < q; i++) {
tmp += d[i] * (e[b[i]] - e[a[i]] == c[i]);
}
ret = max(ret, tmp);
return;
}
for (int i = val; i <= m; i++) {
e[pos] = i;
dfs(pos + 1, i);
}
}
int main() {
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--, b[i]--;
}
dfs(0, 1);
cout << ret << endl;
return 0;
} | #include <iostream>
using namespace std;
int a[50], b[50], c[50], d[50], e[50];
int n, m, q;
int ret = 0;
void dfs(int pos, int val) {
if (pos == n) {
int tmp = 0;
for (int i = 0; i < q; i++) {
tmp += d[i] * (e[b[i]] - e[a[i]] == c[i]);
}
ret = max(ret, tmp);
return;
}
for (int i = val; i <= m; i++) {
e[pos] = i;
dfs(pos + 1, i);
}
}
int main() {
cin >> n >> m >> q;
for (int i = 0; i < q; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--, b[i]--;
}
dfs(0, 1);
cout << ret << endl;
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const ll mod = 1000000007;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;
void prvec(vector<ll> vec) { // for debug
ll n = vec.size();
cout << "------------------------------------\n";
rep(i, n) cout << i << " " << vec.at(i) << "\n";
cout << "------------------------------------\n";
}
void pr2d(vector<vector<ll>> vec) { // for debug
ll h = vec.size();
ll w = vec.at(0).size();
cout << "------------------------------------\n";
rep(i, h) {
rep(j, w) { cout << vec.at(i).at(j) << " "; }
cout << "\n";
}
cout << "------------------------------------\n";
}
int main() {
ll n, m, q;
cin >> n >> m >> q;
vector<vector<vector<ll>>> v(
m + 1, vector<vector<ll>>(
0, vector<ll>(0))); // v[a][b][c] 長さaの数列たちを入れる。
for (int i = 1; i <= m; i++) {
v.at(1).push_back({i});
}
for (int i = 1; i < n; i++) {
for (auto a : v.at(i)) { // 長さiの数列を全部持ってくる
ll am = a.back();
for (int b = am; b <= m; b++) {
auto c = a; // cに持ってきた数列をコピーする
c.emplace_back(b);
v.at(i + 1).emplace_back(c);
}
}
}
vector<vector<ll>> query(q, vector<ll>(4, 0));
rep(i, q) {
rep(j, 4) { cin >> query[i][j]; }
}
ll ans = 0;
for (auto a : v.at(n)) {
ll aans = 0;
rep(i, q) {
if (a.at(query[i][1] - 1) - a.at(query[i][0] - 1) == query[i][2])
aans += query[i][3];
}
ans = max(ans, aans);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const ll mod = 1000000007;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;
void prvec(vector<ll> vec) { // for debug
ll n = vec.size();
cout << "------------------------------------\n";
rep(i, n) cout << i << " " << vec.at(i) << "\n";
cout << "------------------------------------\n";
}
void pr2d(vector<vector<ll>> vec) { // for debug
ll h = vec.size();
ll w = vec.at(0).size();
cout << "------------------------------------\n";
rep(i, h) {
rep(j, w) { cout << vec.at(i).at(j) << " "; }
cout << "\n";
}
cout << "------------------------------------\n";
}
int main() {
ll n, m, q;
cin >> n >> m >> q;
vector<vector<vector<ll>>> v(
n + 1, vector<vector<ll>>(
0, vector<ll>(0))); // v[a][b][c] 長さaの数列たちを入れる。
for (int i = 1; i <= m; i++) {
v.at(1).push_back({i});
}
for (int i = 1; i < n; i++) {
for (auto a : v.at(i)) { // 長さiの数列を全部持ってくる
ll am = a.back();
for (int b = am; b <= m; b++) {
auto c = a; // cに持ってきた数列をコピーする
c.emplace_back(b);
v.at(i + 1).emplace_back(c);
}
}
}
vector<vector<ll>> query(q, vector<ll>(4, 0));
rep(i, q) {
rep(j, 4) { cin >> query[i][j]; }
}
ll ans = 0;
for (auto a : v.at(n)) {
ll aans = 0;
rep(i, q) {
if (a.at(query[i][1] - 1) - a.at(query[i][0] - 1) == query[i][2])
aans += query[i][3];
}
ans = max(ans, aans);
}
cout << ans << endl;
} | replace | 32 | 33 | 32 | 33 | 0 | |
p02695 | C++ | Runtime Error | // #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define mod (ll)(1e9 + 7)
#define FIX(a) ((a) % mod + mod) % mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000 // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define fi first
#define se second
#define pb push_back
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
vll sq(10, 1);
vll a(10), b(10), c(10), d(10);
ll solve(ll n, ll m, ll id, ll q) {
ll ret = 0;
if (id == n) {
REP(i, q) {
if (sq[b[i]] - sq[a[i]] == c[i]) {
ret += d[i];
}
}
return ret;
}
if (id == 0) {
ret = solve(n, m, 1, q);
} else {
for (ll i = sq[id - 1]; i <= m; i++) {
sq[id] = i;
ret = max(ret, solve(n, m, id + 1, q));
}
}
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, m, q;
cin >> n >> m >> q;
ll ans = 0;
REP(i, q) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--;
b[i]--;
}
ans = solve(n, m, 0, q);
cout << ans << endl;
return 0;
} | // #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define mod (ll)(1e9 + 7)
#define FIX(a) ((a) % mod + mod) % mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000 // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define fi first
#define se second
#define pb push_back
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
vll sq(10, 1);
vll a(50), b(50), c(50), d(50);
ll solve(ll n, ll m, ll id, ll q) {
ll ret = 0;
if (id == n) {
REP(i, q) {
if (sq[b[i]] - sq[a[i]] == c[i]) {
ret += d[i];
}
}
return ret;
}
if (id == 0) {
ret = solve(n, m, 1, q);
} else {
for (ll i = sq[id - 1]; i <= m; i++) {
sq[id] = i;
ret = max(ret, solve(n, m, id + 1, q));
}
}
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, m, q;
cin >> n >> m >> q;
ll ans = 0;
REP(i, q) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--;
b[i]--;
}
ans = solve(n, m, 0, q);
cout << ans << endl;
return 0;
} | replace | 31 | 32 | 31 | 32 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long int
using namespace std;
int q, ans = 0;
vector<int> e;
vector<int> f;
struct matter {
int a, b, c, d;
};
matter p[50];
void check() {
int val = 0;
for (int i = 0; i < q; i++) {
if ((f[p[i].b - 1] - f[p[i].a - 1]) == p[i].c)
val = val + p[i].d;
}
ans = max(ans, val);
}
void solve(int init, int k) {
if (k == 0) {
check();
return;
}
int prev = 0;
if (init != 0)
prev = init - 1;
for (int i = prev; i <= (e.size() - k); i++) {
f.push_back(e[i]);
solve(i + 1, k - 1);
f.pop_back();
}
}
int main() {
int n, m;
scanf("%d%d%d", &n, &m, &q);
for (int i = 0; i < q; i++)
scanf("%d%d%d%d", &p[i].a, &p[i].b, &p[i].c, &p[i].d);
for (int i = 0; i < m; i++)
e.push_back(i + 1);
solve(0, n);
printf("%d\n", ans);
return 0;
} | #include <bits/stdc++.h>
#define ll long long int
using namespace std;
int q, ans = 0;
vector<int> e;
vector<int> f;
struct matter {
int a, b, c, d;
};
matter p[50];
void check() {
int val = 0;
for (int i = 0; i < q; i++) {
if ((f[p[i].b - 1] - f[p[i].a - 1]) == p[i].c)
val = val + p[i].d;
}
ans = max(ans, val);
}
void solve(int init, int k) {
if (k == 0) {
check();
return;
}
int prev = 0;
if (init != 0)
prev = init - 1;
for (int i = prev; i < e.size(); i++) {
f.push_back(e[i]);
solve(i + 1, k - 1);
f.pop_back();
}
}
int main() {
int n, m;
scanf("%d%d%d", &n, &m, &q);
for (int i = 0; i < q; i++)
scanf("%d%d%d%d", &p[i].a, &p[i].b, &p[i].c, &p[i].d);
for (int i = 0; i < m; i++)
e.push_back(i + 1);
solve(0, n);
printf("%d\n", ans);
return 0;
} | replace | 26 | 27 | 26 | 27 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
long long int x[15], n, m, i, ans, a[15], b[15], c[15], d[15], q;
void bt(long long int p) {
long long int t, s;
if (p <= n) {
for (t = x[p - 1]; t < m + 1; t++) {
x[p] = t;
bt(p + 1);
}
return;
}
s = 0;
for (t = 1; t < q + 1; t++)
if (x[b[t]] - x[a[t]] == c[t])
s = s + d[t];
ans = max(ans, s);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
for (i = 1; i < q + 1; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
ans = 0;
x[0] = 1;
bt(1);
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
long long int x[15], n, m, i, ans, a[55], b[55], c[55], d[55], q;
void bt(long long int p) {
long long int t, s;
if (p <= n) {
for (t = x[p - 1]; t < m + 1; t++) {
x[p] = t;
bt(p + 1);
}
return;
}
s = 0;
for (t = 1; t < q + 1; t++)
if (x[b[t]] - x[a[t]] == c[t])
s = s + d[t];
ans = max(ans, s);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
for (i = 1; i < q + 1; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
ans = 0;
x[0] = 1;
bt(1);
cout << ans;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define umap unordered_map
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;
ll mod(ll x, ll y) {
if (x >= 0 || x % y == 0)
return x % y;
return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
if (x >= 0 || x % y == 0)
return x / y;
return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
if (x % y == 0)
return dv0(x, y);
return dv0(x, y) + 1;
} // rnd up
ll M = 1e9 + 7;
int quick_pow(int x, int p) {
int a = 1, po = x;
while (p) {
if (p & 1)
a = 1ll * a * po % M;
po = 1ll * po * po % M;
p >>= 1;
}
return a;
}
int cnt = 0;
int ans = 0;
vector<int> A[10];
void count(int N, int M, int Q, vector<int> a, vector<int> b, vector<int> c,
vector<int> d, int x) {
if (x == N) {
cnt = 0;
rep(i, Q) {
if (A[x].at(b.at(i) - 1) - A[x].at(a.at(i) - 1) == c.at(i)) {
cnt += d.at(i);
}
}
A[x] = {};
ans = max(ans, cnt);
} else if (x == 0) {
for (int j = 1; j <= M; j++) {
A[1] = {j};
count(N, M, Q, a, b, c, d, 1);
}
} else {
for (int j = A[x].at(x - 1); j <= M; j++) {
A[x + 1] = A[x];
A[x + 1].push_back(j);
count(N, M, Q, a, b, c, d, x + 1);
}
}
}
// Start from Here
int main() {
int N, M, Q;
cin >> N >> M >> Q;
vector<int> a, b, c, d;
rep(i, Q) {
int x, y, z, w;
cin >> x >> y >> z >> w;
a.push_back(x);
b.push_back(y);
c.push_back(z);
d.push_back(w);
}
count(N, M, Q, a, b, c, d, 0);
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define umap unordered_map
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;
ll mod(ll x, ll y) {
if (x >= 0 || x % y == 0)
return x % y;
return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
if (x >= 0 || x % y == 0)
return x / y;
return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
if (x % y == 0)
return dv0(x, y);
return dv0(x, y) + 1;
} // rnd up
ll M = 1e9 + 7;
int quick_pow(int x, int p) {
int a = 1, po = x;
while (p) {
if (p & 1)
a = 1ll * a * po % M;
po = 1ll * po * po % M;
p >>= 1;
}
return a;
}
int cnt = 0;
int ans = 0;
vector<int> A[11];
void count(int N, int M, int Q, vector<int> a, vector<int> b, vector<int> c,
vector<int> d, int x) {
if (x == N) {
cnt = 0;
rep(i, Q) {
if (A[x].at(b.at(i) - 1) - A[x].at(a.at(i) - 1) == c.at(i)) {
cnt += d.at(i);
}
}
A[x] = {};
ans = max(ans, cnt);
} else if (x == 0) {
for (int j = 1; j <= M; j++) {
A[1] = {j};
count(N, M, Q, a, b, c, d, 1);
}
} else {
for (int j = A[x].at(x - 1); j <= M; j++) {
A[x + 1] = A[x];
A[x + 1].push_back(j);
count(N, M, Q, a, b, c, d, x + 1);
}
}
}
// Start from Here
int main() {
int N, M, Q;
cin >> N >> M >> Q;
vector<int> a, b, c, d;
rep(i, Q) {
int x, y, z, w;
cin >> x >> y >> z >> w;
a.push_back(x);
b.push_back(y);
c.push_back(z);
d.push_back(w);
}
count(N, M, Q, a, b, c, d, 0);
cout << ans << endl;
} | replace | 37 | 38 | 37 | 38 | 0 | |
p02695 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define loop(i, a, n) for (ll i = a; i < n; i++)
#define loopm(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define Graph(n) vector<vector<ll>>(n, vector<ll>(0, 0))
#define tr(container, it) \
for (auto it = container.begin(); it != container.end(); it++)
#define SZ(x) ((int)(x).size())
typedef vector<long long> VI;
typedef long long ll;
typedef vector<vector<ll>> VII;
typedef pair<ll, ll> PII;
typedef double db;
struct quad {
ll a, b, c, d;
};
vector<quad> conditions;
ll m, n;
vector<ll> perm;
ll ans = 0;
ll rec(ll i, ll lvl) {
loop(j, i, m + 1) {
perm[lvl] = j;
if (lvl != n - 1) {
rec(j, lvl + 1);
} else {
ll thi = 0;
loop(f, 0, conditions.size()) {
if (perm[conditions[f].b - 1] - perm[conditions[f].a - 1] ==
conditions[f].c)
thi += conditions[f].d;
}
ans = max(ans, thi);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll q;
cin >> n >> m >> q;
conditions = vector<quad>(q);
perm = VI(n, 0);
loop(i, 0, q) {
cin >> conditions[i].a;
cin >> conditions[i].b;
cin >> conditions[i].c;
cin >> conditions[i].d;
}
rec(1, 0);
cout << ans << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
#define loop(i, a, n) for (ll i = a; i < n; i++)
#define loopm(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define Graph(n) vector<vector<ll>>(n, vector<ll>(0, 0))
#define tr(container, it) \
for (auto it = container.begin(); it != container.end(); it++)
#define SZ(x) ((int)(x).size())
typedef vector<long long> VI;
typedef long long ll;
typedef vector<vector<ll>> VII;
typedef pair<ll, ll> PII;
typedef double db;
struct quad {
ll a, b, c, d;
};
vector<quad> conditions;
ll m, n;
vector<ll> perm;
ll ans = 0;
void rec(ll i, ll lvl) {
loop(j, i, m + 1) {
perm[lvl] = j;
if (lvl != n - 1) {
rec(j, lvl + 1);
} else {
ll thi = 0;
loop(f, 0, conditions.size()) {
if (perm[conditions[f].b - 1] - perm[conditions[f].a - 1] ==
conditions[f].c)
thi += conditions[f].d;
}
ans = max(ans, thi);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll q;
cin >> n >> m >> q;
conditions = vector<quad>(q);
perm = VI(n, 0);
loop(i, 0, q) {
cin >> conditions[i].a;
cin >> conditions[i].b;
cin >> conditions[i].c;
cin >> conditions[i].d;
}
rec(1, 0);
cout << ans << "\n";
}
| replace | 25 | 26 | 25 | 26 | TLE | |
p02695 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
int *num_array;
int array_len;
int num_max;
int score_len;
vector<int> as;
vector<int> bs;
vector<int> cs;
vector<int> ds;
bool array_next(int index);
int main(void) {
cin >> array_len >> num_max >> score_len;
for (int i = 0; i < score_len; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
as.push_back(a - 1);
bs.push_back(b - 1);
cs.push_back(c);
ds.push_back(d);
}
num_array = new int[array_len];
for (int i = 0; i < array_len; i++) {
num_array[i] = 1;
}
num_array[array_len - 1] = 0;
int max_score = 0;
while (array_next(array_len - 1)) {
bool ascend = true;
for (int i = 0; i < array_len - 1; i++) {
if (num_array[i] > num_array[i + 1]) {
ascend = false;
break;
}
}
if (ascend) {
int current_sum = 0;
for (int i = 0; i < score_len; i++) {
if (num_array[bs[i]] - num_array[as[i]] == cs[i]) {
current_sum += ds[i];
}
}
if (current_sum > max_score) {
max_score = current_sum;
}
}
}
cout << max_score << endl;
return 0;
}
bool array_next(int index) {
if (num_array[index] == num_max) {
if (index == 0) {
return false;
}
num_array[index] = 1;
return array_next(index - 1);
}
num_array[index] += 1;
return true;
} | #include <iostream>
#include <vector>
using namespace std;
int *num_array;
int array_len;
int num_max;
int score_len;
vector<int> as;
vector<int> bs;
vector<int> cs;
vector<int> ds;
bool array_next(int index);
int main(void) {
cin >> array_len >> num_max >> score_len;
for (int i = 0; i < score_len; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
as.push_back(a - 1);
bs.push_back(b - 1);
cs.push_back(c);
ds.push_back(d);
}
num_array = new int[array_len];
for (int i = 0; i < array_len; i++) {
num_array[i] = 1;
}
num_array[array_len - 1] = 0;
int max_score = 0;
while (array_next(array_len - 1)) {
bool ascend = true;
for (int i = 0; i < array_len - 1; i++) {
if (num_array[i] > num_array[i + 1]) {
ascend = false;
break;
}
}
if (ascend) {
int current_sum = 0;
for (int i = 0; i < score_len; i++) {
if (num_array[bs[i]] - num_array[as[i]] == cs[i]) {
current_sum += ds[i];
}
}
if (current_sum > max_score) {
max_score = current_sum;
}
}
}
cout << max_score << endl;
return 0;
}
bool array_next(int index) {
if (num_array[index] == num_max) {
if (index == 0) {
return false;
}
bool cont = array_next(index - 1);
if (cont) {
for (int i = index; i < array_len; i++) {
num_array[i] = num_array[i - 1];
}
}
return cont;
}
num_array[index] += 1;
return true;
} | replace | 70 | 72 | 70 | 77 | TLE | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r) {
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)
const int MOD = 1e9 + 7;
int mod_pow(int a, int b, int M = MOD) {
int res = 1;
while (b) {
if (b & 1)
res = (res * a) % M;
a = (a * a) % M;
b >>= 1;
}
return res;
}
const int N = 12;
int a[N], b[N], c[N], d[N];
int n, m, q;
int res = 0;
vector<int> seq;
int go() {
int vv = 0;
f(i, q) {
if (seq[b[i] - 1] - seq[a[i] - 1] == c[i])
vv += d[i];
}
return vv;
}
void gen(int num, int last) {
if (num == 0) {
res = max(res, go());
return;
}
for (int i = last; i <= m; i++) {
seq.pb(i);
gen(num - 1, i);
seq.pop_back();
}
}
void solve() {
cin >> n >> m >> q;
f(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];
gen(n, 1);
cout << res;
}
signed main() {
fast;
int t = 1;
// cin >> t;
while (t--)
solve();
} | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r) {
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)
const int MOD = 1e9 + 7;
int mod_pow(int a, int b, int M = MOD) {
int res = 1;
while (b) {
if (b & 1)
res = (res * a) % M;
a = (a * a) % M;
b >>= 1;
}
return res;
}
const int N = 1200;
int a[N], b[N], c[N], d[N];
int n, m, q;
int res = 0;
vector<int> seq;
int go() {
int vv = 0;
f(i, q) {
if (seq[b[i] - 1] - seq[a[i] - 1] == c[i])
vv += d[i];
}
return vv;
}
void gen(int num, int last) {
if (num == 0) {
res = max(res, go());
return;
}
for (int i = last; i <= m; i++) {
seq.pb(i);
gen(num - 1, i);
seq.pop_back();
}
}
void solve() {
cin >> n >> m >> q;
f(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];
gen(n, 1);
cout << res;
}
signed main() {
fast;
int t = 1;
// cin >> t;
while (t--)
solve();
} | replace | 45 | 46 | 45 | 46 | 0 | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll, vl /*, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
#define rep2(i, n) for (ll i = 1; i <= ll(n); ++i)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); ++i)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); ++i)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
bool chmax(ll &a, ll b) {
if (b > a) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\n';
ll num = 1, ans = 0, cnt = 0, sum = 0;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n, m, q, watch;
cin >> n >> m >> q;
vl a(q), b(q), c(q), d(q);
rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
vl A(n + 1, 1);
while (A[0] == 1) {
sum = 0;
rep(i, q) if (A[b[i]] - A[a[i]] == c[i]) sum += d[i];
chmax(ans, sum);
watch = n;
while (A[watch] == m)
watch--;
A[watch]++;
for (ll i = n; i > watch; i--)
A[i] = A[watch];
}
cout << ans << newl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll, vl /*, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
#define rep2(i, n) for (ll i = 1; i <= ll(n); ++i)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); ++i)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); ++i)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
bool chmax(ll &a, ll b) {
if (b > a) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\n';
ll num = 1, ans = 0, cnt = 0, sum = 0;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n, m, q, watch;
cin >> n >> m >> q;
vl a(q), b(q), c(q), d(q);
rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
if (m == 1) {
rep(i, q) if (!c[i]) ans += d[i];
cout << ans << newl;
return 0;
}
vl A(n + 1, 1);
while (A[0] == 1) {
sum = 0;
rep(i, q) if (A[b[i]] - A[a[i]] == c[i]) sum += d[i];
chmax(ans, sum);
watch = n;
while (A[watch] == m)
watch--;
A[watch]++;
for (ll i = n; i > watch; i--)
A[i] = A[watch];
}
cout << ans << newl;
return 0;
} | insert | 46 | 46 | 46 | 52 | 0 | |
p02695 | C++ | Time Limit Exceeded | #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int n, m, p, ans = 0;
vector<int> e, b, c, d;
void dfs(vector<int> a) {
if (a.size() == n + 1) {
int now = 0;
rep(i, p) {
if (a[b[i]] - a[e[i]] == c[i])
now += d[i];
}
ans = max(ans, now);
}
a.push_back(a.back());
while (a.back() <= m) {
dfs(a);
a.back()++;
}
}
int main() {
cin >> n >> m >> p;
e = b = c = d = vector<int>(p);
rep(i, p) { cin >> e[i] >> b[i] >> c[i] >> d[i]; }
dfs(vector<int>(1, 1));
cout << ans;
}
| #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int n, m, p, ans = 0;
vector<int> e, b, c, d;
void dfs(vector<int> a) {
if (a.size() == n + 1) {
int now = 0;
rep(i, p) {
if (a[b[i]] - a[e[i]] == c[i])
now += d[i];
}
ans = max(ans, now);
return;
}
a.push_back(a.back());
while (a.back() <= m) {
dfs(a);
a.back()++;
}
}
int main() {
cin >> n >> m >> p;
e = b = c = d = vector<int>(p);
rep(i, p) { cin >> e[i] >> b[i] >> c[i] >> d[i]; }
dfs(vector<int>(1, 1));
cout << ans;
}
| insert | 33 | 33 | 33 | 34 | TLE | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
cerr << f << ": ";
dbg(s...);
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int n, m, q;
cin >> n >> m >> q;
VL a(n), b(n), c(n), d(n);
rep(i, q) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--;
b[i]--;
}
VI ks(n);
auto rec = [&](auto rec, int z, int pi) {
ll ans = 0;
if (z == n) {
ll sm = 0;
rep(i, q) {
if (ks[b[i]] - ks[a[i]] == c[i])
sm += d[i];
}
return sm;
}
rep2(i, pi, m) {
ks[z] = i;
ans = max(ans, rec(rec, z + 1, i));
}
return ans;
};
cout << rec(rec, 0, 0) << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
cerr << f << ": ";
dbg(s...);
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int n, m, q;
cin >> n >> m >> q;
VL a(q), b(q), c(q), d(q);
rep(i, q) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--;
b[i]--;
}
VI ks(n);
auto rec = [&](auto rec, int z, int pi) {
ll ans = 0;
if (z == n) {
ll sm = 0;
rep(i, q) {
if (ks[b[i]] - ks[a[i]] == c[i])
sm += d[i];
}
return sm;
}
rep2(i, pi, m) {
ks[z] = i;
ans = max(ans, rec(rec, z + 1, i));
}
return ans;
};
cout << rec(rec, 0, 0) << "\n";
return 0;
}
| replace | 25 | 26 | 25 | 26 | 0 | |
p02695 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define imx INT_MAX
#define imn INT_MIN
#define llmx LLONG_MAX
#define llmn LLONG_MIN
#define fi first
#define se second
#define pb push_back
#define f_s(a) sort(a.begin(), a.end())
#define b_s(a) sort(a.rbegin(), a.rend())
#define p(a) cout << a << "\n"
// #define l(i, )
// #define rl(i, a, b) for(int i = a; i < b; i++)
/////////////////////GCD//////////////////////
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
////////////////////SIEVE OF ERATOSTHENES//////////////////////
vector<int> sieveoferatosthenes(int n) {
vector<bool> prime(n + 1, true);
for (int i = 2; i <= sqrt(n); i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i) {
prime[j] = false;
}
}
}
vector<int> all_primes;
for (int i = 2; i <= n; i++) {
if (prime[i])
all_primes.pb(i);
}
return all_primes;
}
int rec(int level, int n, int a) {
if (level == n) {
return a;
}
rec(level + 1, n, a);
for (int i = a + 1; i <= n; i++) {
rec(level + 1, n, a + 1);
}
// ans.pb()
// rec(level+1, n, a+1);
}
int best = imn;
int q, m, n;
int rec_fn(int i, int pivot, vector<int> &arr, vector<vector<int>> &mat) {
// cout<<i<<endl;
// for(auto i: arr) cout<<i<<" ";
// cout<<endl;
if (i == n + 1) {
int sum = 0;
for (int k = 0; k < q; k++) {
if (arr[mat[k][1] - 1] - arr[mat[k][0] - 1] == mat[k][2])
sum += mat[k][3];
}
// cout<<sum<<endl;
best = max(best, sum);
// return;
return best;
}
for (int j = pivot; j <= m; j++) {
arr[i] = j;
rec_fn(i + 1, j, arr, mat);
best = max(rec_fn(i + 1, j, arr, mat), best);
// cout<<" best "<<best<<" j "<<j<<endl;
// return best;
}
return best;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// ll x;
// cin>>x;
// ll start =100;
// int i = 0;
// while(start < x)
// {
// start= (start+(start*1)/100);
// i++;
// }
// cout<<i<<endl;
// int a, b, c, d;
ll sum = 0, maxim = 0;
cin >> n >> m >> q;
// for(int vector<)
vector<vector<int>> mat(q + 1, vector<int>(4));
vector<int> ans(n + 1, imx);
for (int i = 0; i < q; i++)
for (int j = 0; j < 4; j++)
cin >> mat[i][j];
// for(int i = 0; i < n; i++)
// {
// for(int j = 0; j < 4; j++)
// {
// cout<<mat[i][j]<<" ";
// }
// cout<<endl;
// }
// rec_fn(0, 1, ans, mat);
// p(best);
int an = rec_fn(0, 1, ans, mat);
p(an);
// p(best);
// rec(0, n, 1);
// while(q--)
// {
// cin>>a>>b>>c>>d;
// }
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define imx INT_MAX
#define imn INT_MIN
#define llmx LLONG_MAX
#define llmn LLONG_MIN
#define fi first
#define se second
#define pb push_back
#define f_s(a) sort(a.begin(), a.end())
#define b_s(a) sort(a.rbegin(), a.rend())
#define p(a) cout << a << "\n"
// #define l(i, )
// #define rl(i, a, b) for(int i = a; i < b; i++)
/////////////////////GCD//////////////////////
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
////////////////////SIEVE OF ERATOSTHENES//////////////////////
vector<int> sieveoferatosthenes(int n) {
vector<bool> prime(n + 1, true);
for (int i = 2; i <= sqrt(n); i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i) {
prime[j] = false;
}
}
}
vector<int> all_primes;
for (int i = 2; i <= n; i++) {
if (prime[i])
all_primes.pb(i);
}
return all_primes;
}
int rec(int level, int n, int a) {
if (level == n) {
return a;
}
rec(level + 1, n, a);
for (int i = a + 1; i <= n; i++) {
rec(level + 1, n, a + 1);
}
// ans.pb()
// rec(level+1, n, a+1);
}
int best = imn;
int q, m, n;
int rec_fn(int i, int pivot, vector<int> &arr, vector<vector<int>> &mat) {
// cout<<i<<endl;
// for(auto i: arr) cout<<i<<" ";
// cout<<endl;
if (i == n + 1) {
int sum = 0;
for (int k = 0; k < q; k++) {
if (arr[mat[k][1] - 1] - arr[mat[k][0] - 1] == mat[k][2])
sum += mat[k][3];
}
// cout<<sum<<endl;
best = max(best, sum);
// return;
return best;
}
for (int j = pivot; j <= m; j++) {
arr[i] = j;
// rec_fn(i+1, j, arr, mat);//
best = max(rec_fn(i + 1, j, arr, mat), best);
// cout<<" best "<<best<<" j "<<j<<endl;
// return best;
}
return best;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// ll x;
// cin>>x;
// ll start =100;
// int i = 0;
// while(start < x)
// {
// start= (start+(start*1)/100);
// i++;
// }
// cout<<i<<endl;
// int a, b, c, d;
ll sum = 0, maxim = 0;
cin >> n >> m >> q;
// for(int vector<)
vector<vector<int>> mat(q + 1, vector<int>(4));
vector<int> ans(n + 1, imx);
for (int i = 0; i < q; i++)
for (int j = 0; j < 4; j++)
cin >> mat[i][j];
// for(int i = 0; i < n; i++)
// {
// for(int j = 0; j < 4; j++)
// {
// cout<<mat[i][j]<<" ";
// }
// cout<<endl;
// }
// rec_fn(0, 1, ans, mat);
// p(best);
int an = rec_fn(0, 1, ans, mat);
p(an);
// p(best);
// rec(0, n, 1);
// while(q--)
// {
// cin>>a>>b>>c>>d;
// }
} | replace | 79 | 80 | 79 | 80 | TLE | |
p02695 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long long INF = INT_MAX / 4;
const long long MOD = 1'000'000'007;
const double EPS = 1e-14;
const bool DEBUG = false;
const string YES = "YES";
const string NO = "NO";
const string Yes = "Yes";
const string No = "No";
template <class T> void debug(T head) {
if (DEBUG) {
cout << head << endl;
}
}
template <class Head, class... Body> void debug(Head head, Body... body) {
if (DEBUG) {
cout << head << " ";
debug(body...);
}
}
/////
class q_set {
public:
long a, b, c, d;
};
int N, M, Q;
vector<q_set> q;
long max_ans = 0;
long calc(vector<int> a) {
long ans = 0;
for (int i = 0; i < Q; ++i) {
if (a[q[i].b - 1] - a[q[i].a - 1] == q[i].c) {
ans += q[i].d;
}
}
// debug(a[0], a[1], a[2], ans);
if (max_ans < ans) {
max_ans = ans;
}
}
void dfs(int d, vector<int> a) {
if (d == N) {
if (a.size() == N) {
calc(a);
}
} else if (d == 0) {
for (int i = 1; i <= M; ++i) {
a.push_back(i);
dfs(d + 1, a);
a.pop_back();
}
} else {
for (int i = a[a.size() - 1]; i <= M; ++i) {
a.push_back(i);
dfs(d + 1, a);
a.pop_back();
}
}
}
void answer() {
int a, b, c, d;
cin >> N >> M >> Q;
for (int i = 0; i < Q; ++i) {
cin >> a >> b >> c >> d;
q.push_back({a, b, c, d});
}
vector<int> aa;
dfs(0, aa);
cout << max_ans << endl;
return;
}
/////
int main(int argc, char *argv[]) {
cin.tie(0);
ios::sync_with_stdio(0);
cout.precision(16);
answer();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long long INF = INT_MAX / 4;
const long long MOD = 1'000'000'007;
const double EPS = 1e-14;
const bool DEBUG = false;
const string YES = "YES";
const string NO = "NO";
const string Yes = "Yes";
const string No = "No";
template <class T> void debug(T head) {
if (DEBUG) {
cout << head << endl;
}
}
template <class Head, class... Body> void debug(Head head, Body... body) {
if (DEBUG) {
cout << head << " ";
debug(body...);
}
}
/////
class q_set {
public:
long a, b, c, d;
};
int N, M, Q;
vector<q_set> q;
long max_ans = 0;
void calc(vector<int> a) {
long ans = 0;
for (int i = 0; i < Q; ++i) {
if (a[q[i].b - 1] - a[q[i].a - 1] == q[i].c) {
ans += q[i].d;
}
}
// debug(a[0], a[1], a[2], ans);
if (max_ans < ans) {
max_ans = ans;
}
}
void dfs(int d, vector<int> a) {
if (d == N) {
if (a.size() == N) {
calc(a);
}
} else if (d == 0) {
for (int i = 1; i <= M; ++i) {
a.push_back(i);
dfs(d + 1, a);
a.pop_back();
}
} else {
for (int i = a[a.size() - 1]; i <= M; ++i) {
a.push_back(i);
dfs(d + 1, a);
a.pop_back();
}
}
}
void answer() {
int a, b, c, d;
cin >> N >> M >> Q;
for (int i = 0; i < Q; ++i) {
cin >> a >> b >> c >> d;
q.push_back({a, b, c, d});
}
vector<int> aa;
dfs(0, aa);
cout << max_ans << endl;
return;
}
/////
int main(int argc, char *argv[]) {
cin.tie(0);
ios::sync_with_stdio(0);
cout.precision(16);
answer();
return 0;
}
| replace | 40 | 41 | 40 | 41 | 0 | |
p02695 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
using edge = struct {
ll to;
ll cost;
};
struct point {
ll x;
ll y;
bool operator<(const point &p) const {
if (x == p.x)
return y < p.y;
return x < p.x;
}
};
struct undirected_edge {
ll from;
ll to;
ll cost;
bool operator<(const undirected_edge &ue) const { return cost < ue.cost; }
};
typedef string str;
typedef std::pair<ll, ll> pl;
typedef std::tuple<ll, ll, ll> tp3;
typedef std::tuple<ll, ll, ll, ll> tp4;
typedef std::map<string, ll> msl;
typedef std::map<char, ll> mcl;
typedef std::map<ll, ll> mll;
typedef std::vector<ll> vl;
typedef std::vector<vl> vl2;
typedef std::vector<vl2> vl3;
typedef std::vector<vl3> vl4;
typedef std::vector<bool> vb;
typedef std::vector<vb> vb2;
typedef std::vector<vb2> vb3;
typedef std::vector<vb3> vb4;
typedef std::vector<pl> vpl;
typedef std::vector<tp3> vtp3;
typedef std::vector<tp4> vtp4;
typedef std::vector<point> points;
// priority queue. Taking from the higher value. Don't forget calling !q.empty()
typedef std::priority_queue<ll> pq;
// priority queue. Taking from the lower value
typedef std::priority_queue<ll, vl, greater<ll>> pql;
typedef std::vector<vector<edge>> Graph;
const ll N_DIGITS = 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = MOD * MOD;
const long double EPS = 1e-9;
const long double PI = 3.14159265358979323846;
points dirs = {
{-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions
{1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal
{0, 0} // self
};
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--)
#define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--)
#define all(a) (a).begin(), (a).end()
#define parts(a, i) (a).begin() + (i), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define isUpper(c) ('a' - c > 0)
#define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9)
#define toLower(c) char((c) + 0x20)
#define toUpper(c) char((c)-0x20)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pr(a) cout << (a)
#define prl(a) cout << (a) << endl
#define prl2(a, b) cout << (a) << " " << (b) << endl
#define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define prl4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define prs(a) cout << (a) << " "
#define prs2(a, b) cout << (a) << " " << (b) << " "
#define prs3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " "
#define prs4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << " "
#define yn(condition) \
if ((condition)) \
prl("Yes"); \
else \
prl("No");
#define YN(condition) \
if ((condition)) \
prl("YES"); \
else \
prl("NO");
#define in1(a) cin >> (a)
#define in2(a, b) cin >> (a) >> (b)
#define in3(a, b, c) cin >> (a) >> (b) >> (c)
#define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d)
#define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e)
#define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f)
#define in7(a, b, c, d, e, f, g) \
cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g)
#define e1 first
#define e2 second
#define ctol(c) ll((c)) - ll('0')
#define ltos(n) to_string((n))
#define items(kv, v) for (auto &(kv) : (v))
#define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)]))
#define rsort(a, n) sort(a, a + n, greater<>())
#define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++)
#define cntchar(s, c) count(all((s)), c)
#define substring(s, start, end) s.substr((start), (end) - (start) + 1)
#define prl_nd(num, digits) \
cout << fixed << setprecision(digits) << (num) << endl;
#define XOR(a, b) (a) ^ (b)
#define prl_time(s) \
prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]");
#define char_to_str(c) string(1, (c))
#define DEBUG(x) cerr << #x << ": " << (x) << endl
#define DEBUG_VEC(v) \
cerr << #v << ": "; \
rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \
cerr << endl
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
struct MaxFlow {
struct F_edge {
ll to, rev, capacity;
F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {}
};
typedef vector<F_edge> F_edges;
vector<F_edges> graph;
ll n_vertex;
// level is the shortest path to get a given node from the source node.
vl level, iter;
MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); }
void add_edge(ll from, ll to, ll capacity) {
graph[from].pb({to, ll(graph[to].size()), capacity});
graph[to].pb({from, ll(graph[from].size()) - 1, 0});
}
void bfs(ll source) {
level = vl(n_vertex, -1);
level[source] = 0;
queue<ll> q;
q.push(source);
while (!q.empty()) {
ll vertex = q.front();
q.pop();
rep(i, graph[vertex].size()) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
// if the flow can be into the target node, implement below.
if (cap_target > 0 && level[target] < 0) {
level[target] = level[vertex] + 1;
q.push(target);
}
}
}
}
ll dfs(ll vertex, ll sink, ll flow) {
if (vertex == sink)
return flow;
for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
ll rev_target = graph[vertex][i].rev;
// if capasitiy is not full yet and target is farther,
// then assign current flow
if (cap_target > 0 && level[vertex] < level[target]) {
ll d = dfs(target, sink, min(cap_target, flow));
if (d > 0) { // if the flow successfully reaches the sink, reduce the
// flow from the capacity
graph[vertex][i].capacity -= d;
graph[target][rev_target].capacity += d;
return d;
}
}
}
return 0;
}
ll dinic(ll source, ll sink) {
// complexity O(EV^2)
ll flow = 0;
while (true) {
bfs(source);
// if there is no path leading to the sink, the maximum flow is 0.
if (level[sink] < 0)
return flow;
iter = vl(n_vertex, 0);
ll f;
while ((f = dfs(source, sink, INF)) > 0)
flow += f;
}
}
};
class UnionFind {
vl parents, set_size;
ll n_groups;
public:
UnionFind() {}
UnionFind(ll n) {
parents = set_size = vl(n);
n_groups = n;
rep(i, n) {
parents[i] = i;
set_size[i] = 1LL;
}
}
ll root_find(ll x) {
if (parents[x] == x)
return x;
return parents[x] = root_find(parents[x]);
}
void unite(ll x, ll y) {
// priority for x is larger than that of y
x = root_find(x);
y = root_find(y);
if (x == y)
return;
parents[y] = x, set_size[x] += set_size[y];
n_groups--;
}
bool is_same(ll x, ll y) { // connected or not
return root_find(x) == root_find(y);
}
ll size(ll x) { return set_size[root_find(x)]; }
ll union_count() const { return n_groups; }
};
struct Doubling { // ABC167D
ll n;
ll sz;
vl2 next;
/*
next[k + 1][i] := next[k][next[k][i]]
next[0][i] := edge[i]
e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1
a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times)
Let the function repeatedly input a[i] m times be f[m](a[i])
- get(i, x) returns f[x](a[i])
- lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j.
if not possible returns n.
*/
// edge[i]: the step size for one iteration
Doubling(vl &edge) : n(edge.size()), sz(62) {
next.resize(sz, vl(n, -1));
rep(i, n) next[0][i] = edge[i];
rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]];
}
ll get(ll i, ll x) {
ll ret = i;
rep(bit, sz) {
if (!(x >> bit & 1))
continue;
ret = next[bit][ret];
}
return ret;
}
ll lower_bound(ll i, ll j) {
ll cur = i, acc = 0;
revrep(wid, sz) {
if (next[wid][cur] < j) {
acc += 1LL << wid;
cur = next[wid][cur];
}
}
return min(n, acc + 1);
}
};
class LowestCommonAncestor {
public:
ll N, logN;
vl depth, len;
Graph tree;
vl2 parents;
LowestCommonAncestor(ll n, ll offset = -1, bool is_weighted = true) {
N = n;
logN = 0;
while (N > (1LL << logN))
logN++;
depth = vl(N);
len = vl(N);
parents = vl2(logN, vl(N));
tree = Graph(N);
input(N, offset, is_weighted);
init(0, -1, 0, 0);
build();
}
void add_edge(ll from, ll to, ll dist) {
tree[from].pb({to, dist});
tree[to].pb({from, dist});
}
void input(ll n, ll offset = -1, bool is_weighted = true) {
rep(i, n - 1) {
ll a, b, d = 1;
in2(a, b);
a += offset, b += offset;
if (is_weighted)
in1(d);
add_edge(a, b, d);
}
}
void init(ll source, ll parent, ll d, ll l) {
depth[source] = d;
parents[0][source] = parent;
len[source] = l;
rep(i, tree[source].size()) {
ll target = tree[source][i].to;
ll cost = tree[source][i].cost;
if (target == parent)
continue;
init(target, source, d + 1, cost + l);
}
}
void build() {
rep(k, logN - 1) rep(n, N) {
// if there is no parent, -1.
// otherwise, the parent of the parent is the parent.
if (parents[k][n] < 0)
parents[k + 1][n] = -1;
else
parents[k + 1][n] = parents[k][parents[k][n]];
}
}
ll query(ll u, ll v) {
if (depth[u] > depth[v])
swap(u, v);
rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v];
if (u == v)
return u;
revrep(k, logN) {
if (parents[k][u] != parents[k][v]) {
u = parents[k][u];
v = parents[k][v];
}
}
return parents[0][u];
}
ll distance(ll u, ll v) {
ll w = query(u, v);
return len[u] + len[v] - 2 * len[w];
}
};
struct BinaryIndexedTree {
ll n, ini;
vl dat;
BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){};
// x: 1001 1010 1100 1011 1101 1111
// x & - x: 0001 0010 0100 0001 0001 0001
// ->: 1010 1100 10000 1100 1100 10000
ll update_func(ll val, ll d) {
// if maximum -> max(val, dat)
// return max(val, d);
// if cumulative sum
return val + d;
}
ll query(ll i) {
/*
v[0] + v[1] + ... + v[i]
e.g.) i = 10101
itr1. 10101 -> 10100
itr2. 10100 -> 10000
itr3. 10000 -> 00000 (break)
*/
if (i < 0)
return ini;
ll ret = 0;
for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) {
ret = update_func(ret, dat[j]);
}
return ret;
}
ll query(ll l, ll r) {
// a[l] + a[l + 1] + ... + a[r - 1] + a[r]
return query(r) - query(l - 1);
}
ll lower_bound(ll key) {
// v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left]
if (key <= 0)
return 0;
ll left = 0, right = 1;
while (right <= n)
right *= 2;
for (ll i = right; i > 0; i /= 2) {
if (left + i <= n && dat[left + i - 1] < key) {
key -= dat[left + i - 1];
left += i;
}
}
return left;
}
void update(ll i, ll val) {
/*
e.g.) i = 10101, n = 11111
itr1. i: 10101, i+1: 10110 -> 10111
itr2. i: 10111, i+1: 11000 -> 11111 (break)
*/
if (i < 0)
return;
for (ll j = i; j < n; j |= j + 1) {
dat[j] = update_func(val, dat[j]);
}
}
};
struct SegmentTree {
ll n, ini, minimize;
vl dat;
// when seeking minimum
// ini = INF
// when seeking maximum
// ini = -INF
SegmentTree(ll n_, bool minimize_ = true) {
n = 1;
minimize = minimize_;
if (minimize)
ini = INF;
else
ini = -INF;
while (n < n_)
n *= 2;
dat.resize(2 * n - 1);
rep(i, 2 * n - 1) dat[i] = ini;
};
void update(ll idx, ll val) {
idx += n - 1;
if (minimize && dat[idx] <= val)
return;
if (!minimize && dat[idx] >= val)
return;
dat[idx] = val;
while (idx > 0) {
idx = (idx - 1) / 2;
// when seeking minimum
if (minimize)
dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]);
// when seeking maximum
else
dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]);
}
}
ll query(ll l, ll r) {
// ### NOTE ###
// the range is [l, r]
// l, l + 1, ..., r
r++; // to adjust to this method
return query_segment(l, r, 0, 0, n);
}
ll query_segment(ll a, ll b, ll idx, ll l, ll r) {
assert(a < b);
if (r <= a || b <= l)
return ini;
if (a <= l && r <= b)
return dat[idx];
else {
ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2);
ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r);
// when seeking minimum
if (minimize)
return min(seg1, seg2);
// when seeking maximum
else
return max(seg1, seg2);
}
}
};
template <class Target> class RerootingTreeDP {
public:
using T = typename Target::type;
struct DP_edge {
ll to, rev; // rev is the index to trace the source node.
T value; // objective value
};
private:
ll n;
void dfs_fwd(ll source, ll parent) {
ll par_idx = -1;
vector<T> values;
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent) {
par_idx = i;
continue;
}
dfs_fwd(e.to, source);
values.pb(e.value);
}
// If the parent != -1, update the value on edge from parent to source
if (par_idx != -1) {
ll src_idx = tree[source][par_idx].rev;
// update values on the edge from parent to source
tree[parent][src_idx].value = Target::merge(values);
}
}
void dfs_bwd(ll source, ll parent) {
vector<T> values;
for (auto &&e : tree[source])
values.pb(e.value);
values = Target::evaluate(values);
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent)
continue;
// tree[e.to][e.rev]: e.to -> source
tree[e.to][e.rev].value = values[i];
dfs_bwd(e.to, source);
}
}
public:
UnionFind uf;
vector<vector<DP_edge>> tree;
RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {}
void add_edge(ll u, ll v, T val) {
assert(!uf.is_same(u, v));
tree[u].pb({v, ll(tree[v].size()), val});
tree[v].pb({u, ll(tree[u].size()) - 1, val});
uf.unite(u, v);
}
void dp() {
vb visited(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_fwd(i, -1);
visited[uf.root_find(i)] = true;
}
visited.assign(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_bwd(i, -1);
visited[uf.root_find(i)] = true;
}
}
ll size() const { return tree.size(); }
};
// ABC160F is one example
// Modify the functions evaluate and merge based on given problems
struct Merger {
using type = ll;
static type merge(const vector<type> &value) {
// merge the result below the source node
// each value is from each child node of the source node
// value[i] := f(child i)
// Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ...,
// n_children)
ll ret = 1;
for (auto &&v : value)
ret += v;
return ret;
}
static vector<type> evaluate(const vector<type> &value) {
// value[i] := f(source -> child i)
// we would like to obtain f(child i -> source)
// child j (j != i) is the grandchildren of child i
// represent f(child i -> source) using f(source -> j) (j != i)
// L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i)
// R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children)
const ll n_children = value.size();
vl L(n_children + 1, 0), R(n_children + 1, 0);
rep(i, n_children) L[i + 1] = L[i] + value[i];
revrep(i, n_children) R[i] = R[i + 1] + value[i];
vl ret(n_children);
rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1;
return ret;
}
};
struct StronglyConnectedComponents {
ll n, n_cmp;
vl2 graph, graph_rev, dag, cmp;
vl order, visited, cmp_idx;
StronglyConnectedComponents() {}
StronglyConnectedComponents(ll sz)
: n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {}
void add_edge(ll from, ll to) {
graph[from].pb(to);
graph_rev[to].pb(from);
}
void input(ll m, ll offset = -1) {
ll a, b;
rep(i, m) {
in2(a, b);
add_edge(a + offset, b + offset);
}
}
ll operator[](ll k) { return cmp_idx[k]; }
void dfs_fwd(ll source) {
visited[source] = 1;
rep(i, graph[source].size()) {
ll target = graph[source][i];
if (!visited[target])
dfs_fwd(target);
}
order.pb(source);
}
void dfs_bwd(ll source, ll num) {
visited[source] = 1, cmp_idx[source] = num;
cmp[num].pb(source);
rep(i, graph_rev[source].size()) {
ll target = graph_rev[source][i];
if (!visited[target])
dfs_bwd(target, num);
}
}
ll build() {
fill(all(visited), 0);
order.clear();
rep(i, n) if (!visited[i]) dfs_fwd(i);
fill(all(visited), 0);
ll num = 0;
revrep(i, order.size()) {
if (!visited[order[i]]) {
dag.pb(vl());
cmp.pb(vl());
dfs_bwd(order[i], num++);
}
}
rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to])
dag[cmp_idx[i]]
.pb(cmp_idx[to]);
rep(i, num) {
sort(all(dag[i]));
dag[i].erase(unique(all(dag[i])), dag[i].end());
}
return n_cmp = num;
}
};
struct CombinationMemo {
ll sz, mod;
vl facts, facts_inv, minv;
CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) {
facts.resize(sz + 5);
facts_inv.resize(sz + 5);
minv.resize(sz + 5);
init();
}
void init() {
facts[0] = facts[1] = 1;
minv[1] = 1;
facts_inv[0] = facts_inv[1] = 1;
For(i, 2, sz + 3) {
facts[i] = (i * facts[i - 1]) % mod;
minv[i] = mod - minv[mod % i] * (mod / i) % mod;
facts_inv[i] = facts_inv[i - 1] * minv[i] % mod;
}
}
ll nCk(ll n, ll r) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll val = (facts[n] * facts_inv[n - r]) % mod;
val *= facts_inv[r];
return val % mod;
}
};
struct Grid2D {
Graph graph;
ll Width, Height;
Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); }
ll pos_to_idx(point p) { return p.y * Width + p.x; }
point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; }
bool undefined_region(point p, vb2 &block) {
if (p.x < 0 || p.x > Width - 1)
return true;
if (p.y < 0 || p.y > Height - 1)
return true;
if (block[p.x][p.y])
return true;
return false;
}
void build(vb2 &block, ll val = 1) {
rep(x, Width) rep(y, Height) {
point p = {x, y};
ll idx1 = pos_to_idx(p);
ll idx2;
if (block[x][y])
continue;
rep(i, 4) {
point d = dirs[i], nxt = {x + d.x, y + d.y};
idx2 = pos_to_idx(nxt);
if (!undefined_region(nxt, block))
graph[idx1].pb(
{idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd)
}
}
}
};
struct Cumulative2D {
vl2 cum;
ll w, h;
Cumulative2D(ll w, ll h) : w(w), h(h) { cum = vl2(w + 1, vl(h + 1, 0)); }
template <typename T> void build(vector<T> &vec) {
// never forget building
rep(x, w + 1) cum[x][0] = 0;
rep(y, h + 1) cum[0][y] = 0;
rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vec[x][y];
rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y];
}
ll func(ll x, ll y, ll dx, ll dy) {
// 1-indexed
if (x + dx > w || y + dy > h)
return -INF;
ll val = cum[x + dx][y + dy];
val += cum[x][y];
val -= cum[x][y + dy];
val -= cum[x + dx][y];
return val;
}
};
ll gcd(ll m, ll n) {
ll a = max(m, n);
ll b = min(m, n);
while (b != 1 && b != 0) {
a %= b;
swap(a, b);
}
return b == 1 ? 1 : a;
}
ll lcm(ll m, ll n) { return m / gcd(m, n) * n; }
ll power_mod(ll a, ll power, ll mod) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = (value * a) % mod;
a = (a * a) % mod;
power = power >> 1;
}
return value % mod;
}
ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); }
ll power_normal(ll a, ll power) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = value * a;
a = a * a;
power = power >> 1;
}
return value;
}
vl2 pascal_triangle(ll n) {
/*
Complexity: O(n^2)
The upper bound of n is nearly 50.
Parameters
----------
n; the size of returned vector
Returns
-------
comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i
*/
vl2 comb(n + 1, vl(n + 1));
comb[0][0] = 1;
For(i, 1, n + 1) rep(j, i + 1) {
comb[i][j] += comb[i - 1][j];
if (j > 0)
comb[i][j] += comb[i - 1][j - 1];
}
return comb;
}
ll combination(ll n, ll r, ll mod) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll numerator = 1;
ll denomenator = 1;
for (ll i = 0; i < r; i++) {
ll num = (n - i) % mod, den = (i + 1) % mod;
(numerator *= num) %= mod;
(denomenator *= modinv(den, mod)) %= mod;
}
return (numerator * denomenator) % mod;
}
ld log_combination(ll n, ll r) {
if (n == r && n == 0)
return 0;
else if (n <= 0 || r < 0 || r > n)
return -INF;
ld val = 0;
for (ll i = 0; i < r; i++) {
val += log(n - i);
val -= log(i + 1);
}
return val;
}
ll bin_search(ll key, ll A[], ll left, ll right) {
// return the index idx where A[idx] = key.
// A[left] is start and A[right] is end..
// In other words, A[right], not A[right - 1], must be defined.
while (right >= left) {
ll mid = left + (right - left) / 2;
if (A[mid] == key)
return mid;
else if (A[mid] > key)
right = mid - 1;
else if (A[mid] < key)
left = mid + 1;
}
return -1;
}
/*
// vs[lb - 1] < key <= vs[lb]
lb = lower_bound(all(vs), key);
// vs[ub - 1] <= key < vs[lb]
ub = upper_bound(all(vs), key);
ll bin_search_temp(ll left, ll right, callable judge){
while(right > left){
// when seeking lower bound
ll mid = (right + left) / 2;
if (judge(mid)) right = mid;
else left = mid + 1;
// when seeking upper bound
ll mid = (right + left + 1) / 2;
if (judge(mid)) left = mid;
else right = mid - 1;
}
return right;
}
trinary_search
// Care the value EPS!!! Compare to the condition
ld left = 0; ld right = p;
while(abs(right - left) > EPS){
ld left2 = (2 * left + right) / 3.0;
ld right2 = (left + 2 * right) / 3.0;
ld f1 = tak_func(left2);
ld f2 = tak_func(right2);
if (f1 <= f2) right = right2;
else if (f2 <= f1) left = left2;
}
*/
str bin_expression(ll n, ll dig) {
str s = "";
rep(i, dig) {
s += ltos(n % 2);
n /= 2;
}
reverse(all(s));
return s;
}
ll fact(ll n) {
if (n == 0)
return 1;
return n * fact(n - 1);
}
ll fact_mod(ll n, ll mod) {
if (n == 0)
return 1;
return n * fact_mod(n - 1, mod);
}
bool is_prime(ll n) {
if (n <= 1)
return false;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
mll prime_factorization(ll n) {
ll i = 2;
mll table;
while (i * i <= n) {
while (n % i == 0) {
table[i]++;
n /= i;
}
i++;
}
if (n > 1)
table[n] = 1;
return table;
}
vl divisor_table(ll n) {
vl table;
ll i = 1;
while (i * i <= n) {
if (n % i == 0) {
table.pb(i);
if (i * i != n)
table.pb(n / i);
}
i++;
}
sort(all(table));
return table;
}
ll next_combination(ll sub) {
/*
### Attention ###
if k is 0 or 1 and n is 0, it does not work well.
ll n, k; ll bit = (1 << k) - 1;
for (; bit < (1 << n); bit = next_combination(bit)){
bool ith = bit & (1 << i);
procedures...
}
sub & -sub: the binary which shares the last digit whose value is 1 in sub
sub + x : carry up the last digit
~y : the binary whose digits are 1 if y's digit is 0.
(sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub &
~y).
*/
ll x = sub & -sub, y = sub + x;
return (((sub & ~y) / x) >> 1) | y;
}
// just change the input if you want to change the target.
// If you want to check the common sequences in two strings,
// combine them. e.g. ABC150F
vl z_algorithm(str s) {
/*
Paramters
---------
str: the string of interest
Returns
-------
res[i] is the maximum number of K which satisfies
s[:K] == s[i:i + K]
for each i = 0, 1, 2, ..., n - 1.
*/
ll n = s.length();
vl res(n);
res[0] = n;
ll i1 = 1, i2 = 0;
while (i1 < n) {
/*
i1: the starting point
i2: the length of substring
*/
while (i1 + i2 < n && s[i2] == s[i1 + i2])
++i2;
res[i1] = i2;
if (i2 == 0) {
++i1;
continue;
}
ll i3 = 1;
// update the already seen points
while (i1 + i3 < n && i3 + res[i3] < i2) {
res[i1 + i3] = res[i3];
++i3;
}
// update up to i1 + i3 and the next possible minimum length is i2 - i3 (=
// res[i3])
i1 += i3, i2 -= i3;
}
return res;
}
ll big_number_mod(str s, ll mod) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth) % mod;
val %= mod;
tenth *= 10;
tenth %= mod;
idx++;
}
return val;
}
ll big_number_compare(str s1, str s2) {
if (s1.length() > s2.length())
return 1;
else if (s1.length() < s2.length())
return -1;
else if (s1 == s2)
return 0;
return 2 * (s1 > s2) - 1;
}
ll string_to_ll(str s) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth);
tenth *= 10;
idx++;
}
return val;
}
str reflected_string(str s) {
str t, u;
ll n = s.length();
t = s;
reverse(all(t));
u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1);
return u;
}
ld distance_between_point_line(point l_begin, point l_end, point p) {
ll xl1 = l_begin.x, yl1 = l_begin.y;
ll xl2 = l_end.x, yl2 = l_end.y;
ll xp = p.x, yp = p.y;
ll a = yl2 - yl1;
ll b = -xl2 + xl1;
ll c = -a * xl2 - b * yl2;
return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b));
}
bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) {
ll x1 = l1_begin.x, y1 = l1_begin.y;
ll x2 = l1_end.x, y2 = l1_end.y;
ll x3 = l2_begin.x, y3 = l2_begin.y;
ll x4 = l2_end.x, y4 = l2_end.y;
ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3);
ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4);
ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1);
ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2);
return val1 * val2 < 0 && val3 * val4 < 0;
}
ld space_of_triangle(point p1, point p2, point p3) {
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
ll v1 = x2 - x1;
ll u1 = y2 - y1;
ll v2 = x3 - x1;
ll u2 = y3 - y1;
ld s = ld(v1 * u2 - u1 * v2) / ld(2);
return abs(s);
}
pair<point, ll> center_2det_of_3points(point p1, point p2, point p3) {
// the center of circle on the given three points
// return the determinant value and the product of center points and 2 *
// determinant value
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
// center of 2 points
ll c2x_2 = x2 + x1, c2y_2 = y2 + y1;
ll c3x_2 = x3 + x1, c3y_2 = y3 + y1;
// vertical vector of 2 lines
ll b2y = x2 - x1, b2x = -y2 + y1;
ll b3y = x3 - x1, b3x = -y3 + y1;
ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x;
ll det = -b3y * b2x + b3x * b2y;
if (det == 0)
return {{INF, INF}, det};
ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2;
return {{cx_2det, cy_2det}, det};
}
ll inversion_number(vl a, ll a_max) {
/*
Paramters
---------
a: vector<ll>
All the elements must be non-negative.
Prefably the elements are compressed to reduce the computational cost.
a_max: ll
The maximum value of the vector a or the value bigger than the value
stated previously.
*/
BinaryIndexedTree bit(a_max + 1);
ll val = 0;
rep(i, a.size()) {
// i is the number of elements that have lower index than a[i].
// call the number of elements that have lower value than a[i]
// by subtracting these two, the residual number is the number of elements
// that have larger value
val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1
bit.update(a[i], 1);
}
return val;
}
template <typename T> vector<T> compress(vector<T> v) {
// sort and remove all the duplicated values
sort(all(v));
v.erase(unique(all(v)), v.end());
return v;
}
template <typename T> map<T, ll> dict(const vector<T> &v) {
map<T, ll> d;
rep(i, v.size()) d[v[i]] = i;
return d;
}
points compress2D(vl xs, vl ys) {
/*
NOTE
----
Add the corner points if required
*/
ll n = xs.size();
vl xcs = compress(xs), ycs = compress(ys);
map<ll, ll> xd = dict(xcs), yd = dict(ycs);
points ps(n);
rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]];
rep(i, n) ps[i] = {xs[i], ys[i]};
sort(all(ps));
return ps;
}
void GaussJordanBitVector(vl &bs) {
ll n = bs.size();
ll rank = 0;
ll j = 0;
revrep(i, N_DIGITS) {
for (j = rank; j < n; j++)
if (bs[j] & (1LL << i))
break;
if (j == n)
continue;
if (j > rank)
bs[rank] ^= bs[j];
for (j = rank + 1; j < n; j++)
bs[j] = min(bs[j], bs[j] ^ bs[rank]);
rank++;
}
}
ll kruskal(vector<undirected_edge> &es, ll n_vertex) {
// O(ElogE)
sort(all(es));
UnionFind uf(n_vertex);
ll min_cost = 0;
rep(i, es.size()) {
undirected_edge &e = es[i];
if (!uf.is_same(e.from, e.to)) {
min_cost += e.cost;
uf.unite(e.from, e.to);
}
}
return min_cost;
}
ll LongestIncreasedSequence(vl &v) {
ll n = v.size();
vl dp(n, INF);
rep(i, n) * lower_bound(all(dp), v[i]) = v[i];
return lower_bound(all(dp), INF) - dp.begin();
}
void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre,
bool trace = false) {
priority_queue<pl, vpl, greater<pl>> edge_costs;
ll n = graph.size();
dist = vl(n, INF);
if (trace)
vertex_pre = vl(n, -1);
dist[start] = 0;
edge_costs.push(pl(0, start));
while (!edge_costs.empty()) {
ll idx, cost;
tie(cost, idx) = edge_costs.top();
edge_costs.pop();
if (dist[idx] < cost)
continue;
for (auto e : graph[idx]) {
if (dist[e.to] > dist[idx] + e.cost) {
dist[e.to] = dist[idx] + e.cost;
if (trace)
vertex_pre[e.to] = idx;
edge_costs.push(pl(dist[e.to], e.to));
}
}
}
}
vl get_predecessor(ll g, vl &vertex_pre) {
vl path;
for (; g != -1; g = vertex_pre[g])
path.pb(g);
reverse(all(path));
return path;
}
void warshall_floyd(vl2 &dist) {
ll n = dist.size();
// Dont forget the initialization
// rep(i, n) rep(j, n) dist[i][j] = INF * (i != j);
rep(k, n) rep(i, n) rep(j, n) dist[i][j] =
min(dist[i][j], dist[i][k] + dist[k][j]);
}
// ABC061D
bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) {
rep(i, n) rep(v, n) rep(k, graph[v].size()) {
edge e = graph[v][k];
if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) {
dist[e.to] = -INF;
if (goal == -1)
return true;
else if (goal == e.to)
return true;
}
}
return false;
}
bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) {
// if there is a closed circuit, it returns false. (when goal == -1)
// if the distance to goal cannot be obtained, it returns false (when goal !=
// -1)
ll n = graph.size();
dist = vl(n, INF);
dist[start] = 0;
rep(i, n) rep(v, n) rep(k, graph[v].size()) {
edge e = graph[v][k];
if (dist[v] != INF && dist[e.to] > dist[v] + e.cost)
dist[e.to] = dist[v] + e.cost;
}
if (find_negative_cycle(n, goal, graph, dist))
return false;
return true;
}
void read_graph(ll n_vertex, ll n_edges, Graph &graph, ll cost = -1,
ll directed = false, ll offset = -1) {
graph.resize(n_vertex);
rep(i, n_edges) {
ll from, to, c = cost;
if (cost == -1)
in3(from, to, c);
else
in2(from, to);
from += offset, to += offset;
graph[from].pb({to, cost});
if (!directed)
graph[to].pb({from, cost});
}
}
void read_vector(ll n, vl &v, ll offset = 0) {
v.resize(n);
rep(i, n) {
in1(v[i]);
v[i] += offset;
}
}
/*
diameter of tree
Graph tree;
ll dM = 0, vM = 0, v2 = 0;
void dfs1(ll source, ll parent, ll d){
if (d > dM) dM = d, vM = source;
rep(i, tree[source].size()){
ll target = tree[source][i].to;
if (target == parent) continue;
dfs1(target, source, d + 1);
}
}
void dfs2(ll source, ll parent, ll d){
if (dM <= d) dM = d, v2 = source;
rep(i, tree[source].size()){
ll target = tree[source][i].to;
if (target == parent) continue;
dfs2(target, source, d + 1);
}
}
dfs(0, -1, 0);
dfs2(vM, -1, 0);
prl2(vM + 1, v2 + 1); // the two edges of tree
*/
/*
# 2. The usage of next_permutation and combination (factorial search)
ll a[8];
rep(i, 8) a[i] = i;
sort(a, a + 8);
do{
}while(next_permutation(a, a+n));
# 4. imos method
// used when we would like to count the number which
// shows how many times the numbers between l and r belongs to smt.
// This method is composed of three process.
ll n, m, s[MAX_M], l, r;
in2(n, m);
rep(i, m) s[i] = 0;
// 1st step
rep(i, n){
in3(l, r, c);
l--; r--; // if l starts from 1.
s[l] += c; s[r + 1] -= c;
}
// 2nd step
rep(i, m - 1) s[i + 1] += s[i];
// 3rd step: judgement...
#5. shakutori method (syakutori, two pointers technique)
// 1. strech right side while the condition is met.
// 2. renew the answer
// 3. increments left side
// 4. Back to 1. (l <= r must be satisfied all the time.)
ll l = 0; ll r = 0;
while (l < n){
r = max(r, l);
if (l == r) r++;
while(r < n && cond) r++;
answer += r - l; l++;
}
prl(answer);
#11. bfs ABC146D, ABC007C
1. first create a tree.
2. start searching from a node.
3. do some processes and push nodes connected with a given target node in
BFS.
4. repeat a series of procedure until queue is empty.
queue<pl> q;
void bfs(ll source, ll parents){
ll n_edge = graph[source].size();
if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1);
if (visited[source]) return;
visited[source] = true;
rep(idx, n_edge){
ll target = graph[source][idx].to;
if (target == parents) continue;
q.push(mp(target, source));
}
}
q.push(mp(sg.e1, -1));
while(!q.empty()){
pl source = q.front(); q.pop();
bfs(source.e1, source.e2);
}
# Cumulative sum (2 dimension)
ll func(ll x, ll y, ll dx, ll dy){
if (x + dx > w || y + dy > h) return - INF;
ll val = cum[x + dx][y + dy];
val += cum[x][y];
val -= cum[x][y + dy];
val -= cum[x + dx][y];
return val;
}
vl2 cum(w + 1, vl(h + 1, 0));
rep(x, w + 1) cum[x][0] = 0;
rep(y, h + 1) cum[0][y] = 0;
rep(y, h) rep(x, w)
cum[x + 1][y + 1] = cum[x][y + 1] + vs[x][y];
rep(x, w + 1) rep(y, h)
cum[x][y + 1] += cum[x][y];
*/
/*
# the operators regarding bit
& (AND), | (OR), ^ (XOR)
- (REVERSE), >> (SMALLER SHIFT)
<< (BIGGER SHIFT)
x1: 0000 0001 0010 0101 0110 0111 0111
x2: xxxx 0001 0011 0100 0101 1000 0110
x1 & x2: 0000 0001 0010 0100 0100 0000 0110
x: 1001 1010 1100 1011 1101 1111
x & - x: 0001 0010 0100 0001 0001 0001
sum: 1010 1100 10000 1100 1100 10000
x << y is x * 2 ** y
x >> y is rep(i, y) x = x // 2
####### Attention #######
1 << i and 1LL << i is different. If programs show WA, try switch to this one.
Let S be a bit sequence and i be a non-negative integer
S & (1 << i) -> if true, i in S
S | (1 << i) -> S union {i}
S & ~(1 << i) -> S - {i}
__builtin_popcount(S) -> the number of elements in S
S = 0 -> S is an empty set
__builtin_popcountl(i) -> the number of 1 in binary
S = (1 << n) - 1 -> S includes all the elements up to the n-th
#Conditional Operator
condition ? true : false;
#iterator
type declaration: auto
value reference: *itr
increment: itr++
decrement: itr--
substitution of value: *itr = smt
# inclusion-exclusion principle
|U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n
- 1) |^[i = 1 to n]Ai|
*/
const ll MAX_N = 200005;
bool okay = false;
ll answer = 0;
str s, t, u;
ll n, m, h, w, k, Q;
ll a, b, c, d, idx, cnt;
vl as, bs, cs, ds;
vb2 block;
Graph graph;
ll judge(vl &fixed_nums) {
cnt = 0;
rep(i, Q) {
a = as[i], b = bs[i], c = cs[i], d = ds[i];
if (fixed_nums[b] - fixed_nums[a] == c)
cnt += d;
}
return cnt;
}
/*
### Attention ###
if k is 0 or 1 and n is 0, it does not work well.
*/
void solve() {
as = bs = cs = ds = vl(Q);
rep(i, Q) in4(as[i], bs[i], cs[i], ds[i]);
rep(i, Q) as[i]--, bs[i]--;
vl nums(n);
ll bit = (1 << (m - 1)) - 1;
for (; bit < (1 << (n + m - 1)); bit = next_combination(bit)) {
ll cur = 1;
idx = 0;
rep(i, n + m - 1) {
if (bit & 1LL << i)
cur++;
else
nums[idx] = cur, idx++;
}
chmax(answer, judge(nums));
}
prl(answer);
}
int main(void) {
in3(n, m, Q);
// assert(n <= 30);
// assert(k <= 400);
solve();
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
using edge = struct {
ll to;
ll cost;
};
struct point {
ll x;
ll y;
bool operator<(const point &p) const {
if (x == p.x)
return y < p.y;
return x < p.x;
}
};
struct undirected_edge {
ll from;
ll to;
ll cost;
bool operator<(const undirected_edge &ue) const { return cost < ue.cost; }
};
typedef string str;
typedef std::pair<ll, ll> pl;
typedef std::tuple<ll, ll, ll> tp3;
typedef std::tuple<ll, ll, ll, ll> tp4;
typedef std::map<string, ll> msl;
typedef std::map<char, ll> mcl;
typedef std::map<ll, ll> mll;
typedef std::vector<ll> vl;
typedef std::vector<vl> vl2;
typedef std::vector<vl2> vl3;
typedef std::vector<vl3> vl4;
typedef std::vector<bool> vb;
typedef std::vector<vb> vb2;
typedef std::vector<vb2> vb3;
typedef std::vector<vb3> vb4;
typedef std::vector<pl> vpl;
typedef std::vector<tp3> vtp3;
typedef std::vector<tp4> vtp4;
typedef std::vector<point> points;
// priority queue. Taking from the higher value. Don't forget calling !q.empty()
typedef std::priority_queue<ll> pq;
// priority queue. Taking from the lower value
typedef std::priority_queue<ll, vl, greater<ll>> pql;
typedef std::vector<vector<edge>> Graph;
const ll N_DIGITS = 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = MOD * MOD;
const long double EPS = 1e-9;
const long double PI = 3.14159265358979323846;
points dirs = {
{-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions
{1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal
{0, 0} // self
};
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--)
#define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--)
#define all(a) (a).begin(), (a).end()
#define parts(a, i) (a).begin() + (i), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define isUpper(c) ('a' - c > 0)
#define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9)
#define toLower(c) char((c) + 0x20)
#define toUpper(c) char((c)-0x20)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pr(a) cout << (a)
#define prl(a) cout << (a) << endl
#define prl2(a, b) cout << (a) << " " << (b) << endl
#define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define prl4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define prs(a) cout << (a) << " "
#define prs2(a, b) cout << (a) << " " << (b) << " "
#define prs3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " "
#define prs4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << " "
#define yn(condition) \
if ((condition)) \
prl("Yes"); \
else \
prl("No");
#define YN(condition) \
if ((condition)) \
prl("YES"); \
else \
prl("NO");
#define in1(a) cin >> (a)
#define in2(a, b) cin >> (a) >> (b)
#define in3(a, b, c) cin >> (a) >> (b) >> (c)
#define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d)
#define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e)
#define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f)
#define in7(a, b, c, d, e, f, g) \
cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g)
#define e1 first
#define e2 second
#define ctol(c) ll((c)) - ll('0')
#define ltos(n) to_string((n))
#define items(kv, v) for (auto &(kv) : (v))
#define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)]))
#define rsort(a, n) sort(a, a + n, greater<>())
#define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++)
#define cntchar(s, c) count(all((s)), c)
#define substring(s, start, end) s.substr((start), (end) - (start) + 1)
#define prl_nd(num, digits) \
cout << fixed << setprecision(digits) << (num) << endl;
#define XOR(a, b) (a) ^ (b)
#define prl_time(s) \
prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]");
#define char_to_str(c) string(1, (c))
#define DEBUG(x) cerr << #x << ": " << (x) << endl
#define DEBUG_VEC(v) \
cerr << #v << ": "; \
rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \
cerr << endl
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
struct MaxFlow {
struct F_edge {
ll to, rev, capacity;
F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {}
};
typedef vector<F_edge> F_edges;
vector<F_edges> graph;
ll n_vertex;
// level is the shortest path to get a given node from the source node.
vl level, iter;
MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); }
void add_edge(ll from, ll to, ll capacity) {
graph[from].pb({to, ll(graph[to].size()), capacity});
graph[to].pb({from, ll(graph[from].size()) - 1, 0});
}
void bfs(ll source) {
level = vl(n_vertex, -1);
level[source] = 0;
queue<ll> q;
q.push(source);
while (!q.empty()) {
ll vertex = q.front();
q.pop();
rep(i, graph[vertex].size()) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
// if the flow can be into the target node, implement below.
if (cap_target > 0 && level[target] < 0) {
level[target] = level[vertex] + 1;
q.push(target);
}
}
}
}
ll dfs(ll vertex, ll sink, ll flow) {
if (vertex == sink)
return flow;
for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
ll rev_target = graph[vertex][i].rev;
// if capasitiy is not full yet and target is farther,
// then assign current flow
if (cap_target > 0 && level[vertex] < level[target]) {
ll d = dfs(target, sink, min(cap_target, flow));
if (d > 0) { // if the flow successfully reaches the sink, reduce the
// flow from the capacity
graph[vertex][i].capacity -= d;
graph[target][rev_target].capacity += d;
return d;
}
}
}
return 0;
}
ll dinic(ll source, ll sink) {
// complexity O(EV^2)
ll flow = 0;
while (true) {
bfs(source);
// if there is no path leading to the sink, the maximum flow is 0.
if (level[sink] < 0)
return flow;
iter = vl(n_vertex, 0);
ll f;
while ((f = dfs(source, sink, INF)) > 0)
flow += f;
}
}
};
class UnionFind {
vl parents, set_size;
ll n_groups;
public:
UnionFind() {}
UnionFind(ll n) {
parents = set_size = vl(n);
n_groups = n;
rep(i, n) {
parents[i] = i;
set_size[i] = 1LL;
}
}
ll root_find(ll x) {
if (parents[x] == x)
return x;
return parents[x] = root_find(parents[x]);
}
void unite(ll x, ll y) {
// priority for x is larger than that of y
x = root_find(x);
y = root_find(y);
if (x == y)
return;
parents[y] = x, set_size[x] += set_size[y];
n_groups--;
}
bool is_same(ll x, ll y) { // connected or not
return root_find(x) == root_find(y);
}
ll size(ll x) { return set_size[root_find(x)]; }
ll union_count() const { return n_groups; }
};
struct Doubling { // ABC167D
ll n;
ll sz;
vl2 next;
/*
next[k + 1][i] := next[k][next[k][i]]
next[0][i] := edge[i]
e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1
a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times)
Let the function repeatedly input a[i] m times be f[m](a[i])
- get(i, x) returns f[x](a[i])
- lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j.
if not possible returns n.
*/
// edge[i]: the step size for one iteration
Doubling(vl &edge) : n(edge.size()), sz(62) {
next.resize(sz, vl(n, -1));
rep(i, n) next[0][i] = edge[i];
rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]];
}
ll get(ll i, ll x) {
ll ret = i;
rep(bit, sz) {
if (!(x >> bit & 1))
continue;
ret = next[bit][ret];
}
return ret;
}
ll lower_bound(ll i, ll j) {
ll cur = i, acc = 0;
revrep(wid, sz) {
if (next[wid][cur] < j) {
acc += 1LL << wid;
cur = next[wid][cur];
}
}
return min(n, acc + 1);
}
};
class LowestCommonAncestor {
public:
ll N, logN;
vl depth, len;
Graph tree;
vl2 parents;
LowestCommonAncestor(ll n, ll offset = -1, bool is_weighted = true) {
N = n;
logN = 0;
while (N > (1LL << logN))
logN++;
depth = vl(N);
len = vl(N);
parents = vl2(logN, vl(N));
tree = Graph(N);
input(N, offset, is_weighted);
init(0, -1, 0, 0);
build();
}
void add_edge(ll from, ll to, ll dist) {
tree[from].pb({to, dist});
tree[to].pb({from, dist});
}
void input(ll n, ll offset = -1, bool is_weighted = true) {
rep(i, n - 1) {
ll a, b, d = 1;
in2(a, b);
a += offset, b += offset;
if (is_weighted)
in1(d);
add_edge(a, b, d);
}
}
void init(ll source, ll parent, ll d, ll l) {
depth[source] = d;
parents[0][source] = parent;
len[source] = l;
rep(i, tree[source].size()) {
ll target = tree[source][i].to;
ll cost = tree[source][i].cost;
if (target == parent)
continue;
init(target, source, d + 1, cost + l);
}
}
void build() {
rep(k, logN - 1) rep(n, N) {
// if there is no parent, -1.
// otherwise, the parent of the parent is the parent.
if (parents[k][n] < 0)
parents[k + 1][n] = -1;
else
parents[k + 1][n] = parents[k][parents[k][n]];
}
}
ll query(ll u, ll v) {
if (depth[u] > depth[v])
swap(u, v);
rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v];
if (u == v)
return u;
revrep(k, logN) {
if (parents[k][u] != parents[k][v]) {
u = parents[k][u];
v = parents[k][v];
}
}
return parents[0][u];
}
ll distance(ll u, ll v) {
ll w = query(u, v);
return len[u] + len[v] - 2 * len[w];
}
};
struct BinaryIndexedTree {
ll n, ini;
vl dat;
BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){};
// x: 1001 1010 1100 1011 1101 1111
// x & - x: 0001 0010 0100 0001 0001 0001
// ->: 1010 1100 10000 1100 1100 10000
ll update_func(ll val, ll d) {
// if maximum -> max(val, dat)
// return max(val, d);
// if cumulative sum
return val + d;
}
ll query(ll i) {
/*
v[0] + v[1] + ... + v[i]
e.g.) i = 10101
itr1. 10101 -> 10100
itr2. 10100 -> 10000
itr3. 10000 -> 00000 (break)
*/
if (i < 0)
return ini;
ll ret = 0;
for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) {
ret = update_func(ret, dat[j]);
}
return ret;
}
ll query(ll l, ll r) {
// a[l] + a[l + 1] + ... + a[r - 1] + a[r]
return query(r) - query(l - 1);
}
ll lower_bound(ll key) {
// v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left]
if (key <= 0)
return 0;
ll left = 0, right = 1;
while (right <= n)
right *= 2;
for (ll i = right; i > 0; i /= 2) {
if (left + i <= n && dat[left + i - 1] < key) {
key -= dat[left + i - 1];
left += i;
}
}
return left;
}
void update(ll i, ll val) {
/*
e.g.) i = 10101, n = 11111
itr1. i: 10101, i+1: 10110 -> 10111
itr2. i: 10111, i+1: 11000 -> 11111 (break)
*/
if (i < 0)
return;
for (ll j = i; j < n; j |= j + 1) {
dat[j] = update_func(val, dat[j]);
}
}
};
struct SegmentTree {
ll n, ini, minimize;
vl dat;
// when seeking minimum
// ini = INF
// when seeking maximum
// ini = -INF
SegmentTree(ll n_, bool minimize_ = true) {
n = 1;
minimize = minimize_;
if (minimize)
ini = INF;
else
ini = -INF;
while (n < n_)
n *= 2;
dat.resize(2 * n - 1);
rep(i, 2 * n - 1) dat[i] = ini;
};
void update(ll idx, ll val) {
idx += n - 1;
if (minimize && dat[idx] <= val)
return;
if (!minimize && dat[idx] >= val)
return;
dat[idx] = val;
while (idx > 0) {
idx = (idx - 1) / 2;
// when seeking minimum
if (minimize)
dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]);
// when seeking maximum
else
dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]);
}
}
ll query(ll l, ll r) {
// ### NOTE ###
// the range is [l, r]
// l, l + 1, ..., r
r++; // to adjust to this method
return query_segment(l, r, 0, 0, n);
}
ll query_segment(ll a, ll b, ll idx, ll l, ll r) {
assert(a < b);
if (r <= a || b <= l)
return ini;
if (a <= l && r <= b)
return dat[idx];
else {
ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2);
ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r);
// when seeking minimum
if (minimize)
return min(seg1, seg2);
// when seeking maximum
else
return max(seg1, seg2);
}
}
};
template <class Target> class RerootingTreeDP {
public:
using T = typename Target::type;
struct DP_edge {
ll to, rev; // rev is the index to trace the source node.
T value; // objective value
};
private:
ll n;
void dfs_fwd(ll source, ll parent) {
ll par_idx = -1;
vector<T> values;
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent) {
par_idx = i;
continue;
}
dfs_fwd(e.to, source);
values.pb(e.value);
}
// If the parent != -1, update the value on edge from parent to source
if (par_idx != -1) {
ll src_idx = tree[source][par_idx].rev;
// update values on the edge from parent to source
tree[parent][src_idx].value = Target::merge(values);
}
}
void dfs_bwd(ll source, ll parent) {
vector<T> values;
for (auto &&e : tree[source])
values.pb(e.value);
values = Target::evaluate(values);
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent)
continue;
// tree[e.to][e.rev]: e.to -> source
tree[e.to][e.rev].value = values[i];
dfs_bwd(e.to, source);
}
}
public:
UnionFind uf;
vector<vector<DP_edge>> tree;
RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {}
void add_edge(ll u, ll v, T val) {
assert(!uf.is_same(u, v));
tree[u].pb({v, ll(tree[v].size()), val});
tree[v].pb({u, ll(tree[u].size()) - 1, val});
uf.unite(u, v);
}
void dp() {
vb visited(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_fwd(i, -1);
visited[uf.root_find(i)] = true;
}
visited.assign(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_bwd(i, -1);
visited[uf.root_find(i)] = true;
}
}
ll size() const { return tree.size(); }
};
// ABC160F is one example
// Modify the functions evaluate and merge based on given problems
struct Merger {
using type = ll;
static type merge(const vector<type> &value) {
// merge the result below the source node
// each value is from each child node of the source node
// value[i] := f(child i)
// Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ...,
// n_children)
ll ret = 1;
for (auto &&v : value)
ret += v;
return ret;
}
static vector<type> evaluate(const vector<type> &value) {
// value[i] := f(source -> child i)
// we would like to obtain f(child i -> source)
// child j (j != i) is the grandchildren of child i
// represent f(child i -> source) using f(source -> j) (j != i)
// L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i)
// R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children)
const ll n_children = value.size();
vl L(n_children + 1, 0), R(n_children + 1, 0);
rep(i, n_children) L[i + 1] = L[i] + value[i];
revrep(i, n_children) R[i] = R[i + 1] + value[i];
vl ret(n_children);
rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1;
return ret;
}
};
struct StronglyConnectedComponents {
ll n, n_cmp;
vl2 graph, graph_rev, dag, cmp;
vl order, visited, cmp_idx;
StronglyConnectedComponents() {}
StronglyConnectedComponents(ll sz)
: n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {}
void add_edge(ll from, ll to) {
graph[from].pb(to);
graph_rev[to].pb(from);
}
void input(ll m, ll offset = -1) {
ll a, b;
rep(i, m) {
in2(a, b);
add_edge(a + offset, b + offset);
}
}
ll operator[](ll k) { return cmp_idx[k]; }
void dfs_fwd(ll source) {
visited[source] = 1;
rep(i, graph[source].size()) {
ll target = graph[source][i];
if (!visited[target])
dfs_fwd(target);
}
order.pb(source);
}
void dfs_bwd(ll source, ll num) {
visited[source] = 1, cmp_idx[source] = num;
cmp[num].pb(source);
rep(i, graph_rev[source].size()) {
ll target = graph_rev[source][i];
if (!visited[target])
dfs_bwd(target, num);
}
}
ll build() {
fill(all(visited), 0);
order.clear();
rep(i, n) if (!visited[i]) dfs_fwd(i);
fill(all(visited), 0);
ll num = 0;
revrep(i, order.size()) {
if (!visited[order[i]]) {
dag.pb(vl());
cmp.pb(vl());
dfs_bwd(order[i], num++);
}
}
rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to])
dag[cmp_idx[i]]
.pb(cmp_idx[to]);
rep(i, num) {
sort(all(dag[i]));
dag[i].erase(unique(all(dag[i])), dag[i].end());
}
return n_cmp = num;
}
};
struct CombinationMemo {
ll sz, mod;
vl facts, facts_inv, minv;
CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) {
facts.resize(sz + 5);
facts_inv.resize(sz + 5);
minv.resize(sz + 5);
init();
}
void init() {
facts[0] = facts[1] = 1;
minv[1] = 1;
facts_inv[0] = facts_inv[1] = 1;
For(i, 2, sz + 3) {
facts[i] = (i * facts[i - 1]) % mod;
minv[i] = mod - minv[mod % i] * (mod / i) % mod;
facts_inv[i] = facts_inv[i - 1] * minv[i] % mod;
}
}
ll nCk(ll n, ll r) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll val = (facts[n] * facts_inv[n - r]) % mod;
val *= facts_inv[r];
return val % mod;
}
};
struct Grid2D {
Graph graph;
ll Width, Height;
Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); }
ll pos_to_idx(point p) { return p.y * Width + p.x; }
point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; }
bool undefined_region(point p, vb2 &block) {
if (p.x < 0 || p.x > Width - 1)
return true;
if (p.y < 0 || p.y > Height - 1)
return true;
if (block[p.x][p.y])
return true;
return false;
}
void build(vb2 &block, ll val = 1) {
rep(x, Width) rep(y, Height) {
point p = {x, y};
ll idx1 = pos_to_idx(p);
ll idx2;
if (block[x][y])
continue;
rep(i, 4) {
point d = dirs[i], nxt = {x + d.x, y + d.y};
idx2 = pos_to_idx(nxt);
if (!undefined_region(nxt, block))
graph[idx1].pb(
{idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd)
}
}
}
};
struct Cumulative2D {
vl2 cum;
ll w, h;
Cumulative2D(ll w, ll h) : w(w), h(h) { cum = vl2(w + 1, vl(h + 1, 0)); }
template <typename T> void build(vector<T> &vec) {
// never forget building
rep(x, w + 1) cum[x][0] = 0;
rep(y, h + 1) cum[0][y] = 0;
rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vec[x][y];
rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y];
}
ll func(ll x, ll y, ll dx, ll dy) {
// 1-indexed
if (x + dx > w || y + dy > h)
return -INF;
ll val = cum[x + dx][y + dy];
val += cum[x][y];
val -= cum[x][y + dy];
val -= cum[x + dx][y];
return val;
}
};
ll gcd(ll m, ll n) {
ll a = max(m, n);
ll b = min(m, n);
while (b != 1 && b != 0) {
a %= b;
swap(a, b);
}
return b == 1 ? 1 : a;
}
ll lcm(ll m, ll n) { return m / gcd(m, n) * n; }
ll power_mod(ll a, ll power, ll mod) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = (value * a) % mod;
a = (a * a) % mod;
power = power >> 1;
}
return value % mod;
}
ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); }
ll power_normal(ll a, ll power) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = value * a;
a = a * a;
power = power >> 1;
}
return value;
}
vl2 pascal_triangle(ll n) {
/*
Complexity: O(n^2)
The upper bound of n is nearly 50.
Parameters
----------
n; the size of returned vector
Returns
-------
comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i
*/
vl2 comb(n + 1, vl(n + 1));
comb[0][0] = 1;
For(i, 1, n + 1) rep(j, i + 1) {
comb[i][j] += comb[i - 1][j];
if (j > 0)
comb[i][j] += comb[i - 1][j - 1];
}
return comb;
}
ll combination(ll n, ll r, ll mod) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll numerator = 1;
ll denomenator = 1;
for (ll i = 0; i < r; i++) {
ll num = (n - i) % mod, den = (i + 1) % mod;
(numerator *= num) %= mod;
(denomenator *= modinv(den, mod)) %= mod;
}
return (numerator * denomenator) % mod;
}
ld log_combination(ll n, ll r) {
if (n == r && n == 0)
return 0;
else if (n <= 0 || r < 0 || r > n)
return -INF;
ld val = 0;
for (ll i = 0; i < r; i++) {
val += log(n - i);
val -= log(i + 1);
}
return val;
}
ll bin_search(ll key, ll A[], ll left, ll right) {
// return the index idx where A[idx] = key.
// A[left] is start and A[right] is end..
// In other words, A[right], not A[right - 1], must be defined.
while (right >= left) {
ll mid = left + (right - left) / 2;
if (A[mid] == key)
return mid;
else if (A[mid] > key)
right = mid - 1;
else if (A[mid] < key)
left = mid + 1;
}
return -1;
}
/*
// vs[lb - 1] < key <= vs[lb]
lb = lower_bound(all(vs), key);
// vs[ub - 1] <= key < vs[lb]
ub = upper_bound(all(vs), key);
ll bin_search_temp(ll left, ll right, callable judge){
while(right > left){
// when seeking lower bound
ll mid = (right + left) / 2;
if (judge(mid)) right = mid;
else left = mid + 1;
// when seeking upper bound
ll mid = (right + left + 1) / 2;
if (judge(mid)) left = mid;
else right = mid - 1;
}
return right;
}
trinary_search
// Care the value EPS!!! Compare to the condition
ld left = 0; ld right = p;
while(abs(right - left) > EPS){
ld left2 = (2 * left + right) / 3.0;
ld right2 = (left + 2 * right) / 3.0;
ld f1 = tak_func(left2);
ld f2 = tak_func(right2);
if (f1 <= f2) right = right2;
else if (f2 <= f1) left = left2;
}
*/
str bin_expression(ll n, ll dig) {
str s = "";
rep(i, dig) {
s += ltos(n % 2);
n /= 2;
}
reverse(all(s));
return s;
}
ll fact(ll n) {
if (n == 0)
return 1;
return n * fact(n - 1);
}
ll fact_mod(ll n, ll mod) {
if (n == 0)
return 1;
return n * fact_mod(n - 1, mod);
}
bool is_prime(ll n) {
if (n <= 1)
return false;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
mll prime_factorization(ll n) {
ll i = 2;
mll table;
while (i * i <= n) {
while (n % i == 0) {
table[i]++;
n /= i;
}
i++;
}
if (n > 1)
table[n] = 1;
return table;
}
vl divisor_table(ll n) {
vl table;
ll i = 1;
while (i * i <= n) {
if (n % i == 0) {
table.pb(i);
if (i * i != n)
table.pb(n / i);
}
i++;
}
sort(all(table));
return table;
}
ll next_combination(ll sub) {
/*
### Attention ###
if k is 0 or 1 and n is 0, it does not work well.
ll n, k; ll bit = (1 << k) - 1;
for (; bit < (1 << n); bit = next_combination(bit)){
bool ith = bit & (1 << i);
procedures...
}
sub & -sub: the binary which shares the last digit whose value is 1 in sub
sub + x : carry up the last digit
~y : the binary whose digits are 1 if y's digit is 0.
(sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub &
~y).
*/
ll x = sub & -sub, y = sub + x;
if (x != 0)
return (((sub & ~y) / x) >> 1) | y;
else
return INF;
}
// just change the input if you want to change the target.
// If you want to check the common sequences in two strings,
// combine them. e.g. ABC150F
vl z_algorithm(str s) {
/*
Paramters
---------
str: the string of interest
Returns
-------
res[i] is the maximum number of K which satisfies
s[:K] == s[i:i + K]
for each i = 0, 1, 2, ..., n - 1.
*/
ll n = s.length();
vl res(n);
res[0] = n;
ll i1 = 1, i2 = 0;
while (i1 < n) {
/*
i1: the starting point
i2: the length of substring
*/
while (i1 + i2 < n && s[i2] == s[i1 + i2])
++i2;
res[i1] = i2;
if (i2 == 0) {
++i1;
continue;
}
ll i3 = 1;
// update the already seen points
while (i1 + i3 < n && i3 + res[i3] < i2) {
res[i1 + i3] = res[i3];
++i3;
}
// update up to i1 + i3 and the next possible minimum length is i2 - i3 (=
// res[i3])
i1 += i3, i2 -= i3;
}
return res;
}
ll big_number_mod(str s, ll mod) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth) % mod;
val %= mod;
tenth *= 10;
tenth %= mod;
idx++;
}
return val;
}
ll big_number_compare(str s1, str s2) {
if (s1.length() > s2.length())
return 1;
else if (s1.length() < s2.length())
return -1;
else if (s1 == s2)
return 0;
return 2 * (s1 > s2) - 1;
}
ll string_to_ll(str s) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth);
tenth *= 10;
idx++;
}
return val;
}
str reflected_string(str s) {
str t, u;
ll n = s.length();
t = s;
reverse(all(t));
u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1);
return u;
}
ld distance_between_point_line(point l_begin, point l_end, point p) {
ll xl1 = l_begin.x, yl1 = l_begin.y;
ll xl2 = l_end.x, yl2 = l_end.y;
ll xp = p.x, yp = p.y;
ll a = yl2 - yl1;
ll b = -xl2 + xl1;
ll c = -a * xl2 - b * yl2;
return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b));
}
bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) {
ll x1 = l1_begin.x, y1 = l1_begin.y;
ll x2 = l1_end.x, y2 = l1_end.y;
ll x3 = l2_begin.x, y3 = l2_begin.y;
ll x4 = l2_end.x, y4 = l2_end.y;
ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3);
ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4);
ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1);
ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2);
return val1 * val2 < 0 && val3 * val4 < 0;
}
ld space_of_triangle(point p1, point p2, point p3) {
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
ll v1 = x2 - x1;
ll u1 = y2 - y1;
ll v2 = x3 - x1;
ll u2 = y3 - y1;
ld s = ld(v1 * u2 - u1 * v2) / ld(2);
return abs(s);
}
pair<point, ll> center_2det_of_3points(point p1, point p2, point p3) {
// the center of circle on the given three points
// return the determinant value and the product of center points and 2 *
// determinant value
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
// center of 2 points
ll c2x_2 = x2 + x1, c2y_2 = y2 + y1;
ll c3x_2 = x3 + x1, c3y_2 = y3 + y1;
// vertical vector of 2 lines
ll b2y = x2 - x1, b2x = -y2 + y1;
ll b3y = x3 - x1, b3x = -y3 + y1;
ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x;
ll det = -b3y * b2x + b3x * b2y;
if (det == 0)
return {{INF, INF}, det};
ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2;
return {{cx_2det, cy_2det}, det};
}
ll inversion_number(vl a, ll a_max) {
/*
Paramters
---------
a: vector<ll>
All the elements must be non-negative.
Prefably the elements are compressed to reduce the computational cost.
a_max: ll
The maximum value of the vector a or the value bigger than the value
stated previously.
*/
BinaryIndexedTree bit(a_max + 1);
ll val = 0;
rep(i, a.size()) {
// i is the number of elements that have lower index than a[i].
// call the number of elements that have lower value than a[i]
// by subtracting these two, the residual number is the number of elements
// that have larger value
val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1
bit.update(a[i], 1);
}
return val;
}
template <typename T> vector<T> compress(vector<T> v) {
// sort and remove all the duplicated values
sort(all(v));
v.erase(unique(all(v)), v.end());
return v;
}
template <typename T> map<T, ll> dict(const vector<T> &v) {
map<T, ll> d;
rep(i, v.size()) d[v[i]] = i;
return d;
}
points compress2D(vl xs, vl ys) {
/*
NOTE
----
Add the corner points if required
*/
ll n = xs.size();
vl xcs = compress(xs), ycs = compress(ys);
map<ll, ll> xd = dict(xcs), yd = dict(ycs);
points ps(n);
rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]];
rep(i, n) ps[i] = {xs[i], ys[i]};
sort(all(ps));
return ps;
}
void GaussJordanBitVector(vl &bs) {
ll n = bs.size();
ll rank = 0;
ll j = 0;
revrep(i, N_DIGITS) {
for (j = rank; j < n; j++)
if (bs[j] & (1LL << i))
break;
if (j == n)
continue;
if (j > rank)
bs[rank] ^= bs[j];
for (j = rank + 1; j < n; j++)
bs[j] = min(bs[j], bs[j] ^ bs[rank]);
rank++;
}
}
ll kruskal(vector<undirected_edge> &es, ll n_vertex) {
// O(ElogE)
sort(all(es));
UnionFind uf(n_vertex);
ll min_cost = 0;
rep(i, es.size()) {
undirected_edge &e = es[i];
if (!uf.is_same(e.from, e.to)) {
min_cost += e.cost;
uf.unite(e.from, e.to);
}
}
return min_cost;
}
ll LongestIncreasedSequence(vl &v) {
ll n = v.size();
vl dp(n, INF);
rep(i, n) * lower_bound(all(dp), v[i]) = v[i];
return lower_bound(all(dp), INF) - dp.begin();
}
void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre,
bool trace = false) {
priority_queue<pl, vpl, greater<pl>> edge_costs;
ll n = graph.size();
dist = vl(n, INF);
if (trace)
vertex_pre = vl(n, -1);
dist[start] = 0;
edge_costs.push(pl(0, start));
while (!edge_costs.empty()) {
ll idx, cost;
tie(cost, idx) = edge_costs.top();
edge_costs.pop();
if (dist[idx] < cost)
continue;
for (auto e : graph[idx]) {
if (dist[e.to] > dist[idx] + e.cost) {
dist[e.to] = dist[idx] + e.cost;
if (trace)
vertex_pre[e.to] = idx;
edge_costs.push(pl(dist[e.to], e.to));
}
}
}
}
vl get_predecessor(ll g, vl &vertex_pre) {
vl path;
for (; g != -1; g = vertex_pre[g])
path.pb(g);
reverse(all(path));
return path;
}
void warshall_floyd(vl2 &dist) {
ll n = dist.size();
// Dont forget the initialization
// rep(i, n) rep(j, n) dist[i][j] = INF * (i != j);
rep(k, n) rep(i, n) rep(j, n) dist[i][j] =
min(dist[i][j], dist[i][k] + dist[k][j]);
}
// ABC061D
bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) {
rep(i, n) rep(v, n) rep(k, graph[v].size()) {
edge e = graph[v][k];
if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) {
dist[e.to] = -INF;
if (goal == -1)
return true;
else if (goal == e.to)
return true;
}
}
return false;
}
bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) {
// if there is a closed circuit, it returns false. (when goal == -1)
// if the distance to goal cannot be obtained, it returns false (when goal !=
// -1)
ll n = graph.size();
dist = vl(n, INF);
dist[start] = 0;
rep(i, n) rep(v, n) rep(k, graph[v].size()) {
edge e = graph[v][k];
if (dist[v] != INF && dist[e.to] > dist[v] + e.cost)
dist[e.to] = dist[v] + e.cost;
}
if (find_negative_cycle(n, goal, graph, dist))
return false;
return true;
}
void read_graph(ll n_vertex, ll n_edges, Graph &graph, ll cost = -1,
ll directed = false, ll offset = -1) {
graph.resize(n_vertex);
rep(i, n_edges) {
ll from, to, c = cost;
if (cost == -1)
in3(from, to, c);
else
in2(from, to);
from += offset, to += offset;
graph[from].pb({to, cost});
if (!directed)
graph[to].pb({from, cost});
}
}
void read_vector(ll n, vl &v, ll offset = 0) {
v.resize(n);
rep(i, n) {
in1(v[i]);
v[i] += offset;
}
}
/*
diameter of tree
Graph tree;
ll dM = 0, vM = 0, v2 = 0;
void dfs1(ll source, ll parent, ll d){
if (d > dM) dM = d, vM = source;
rep(i, tree[source].size()){
ll target = tree[source][i].to;
if (target == parent) continue;
dfs1(target, source, d + 1);
}
}
void dfs2(ll source, ll parent, ll d){
if (dM <= d) dM = d, v2 = source;
rep(i, tree[source].size()){
ll target = tree[source][i].to;
if (target == parent) continue;
dfs2(target, source, d + 1);
}
}
dfs(0, -1, 0);
dfs2(vM, -1, 0);
prl2(vM + 1, v2 + 1); // the two edges of tree
*/
/*
# 2. The usage of next_permutation and combination (factorial search)
ll a[8];
rep(i, 8) a[i] = i;
sort(a, a + 8);
do{
}while(next_permutation(a, a+n));
# 4. imos method
// used when we would like to count the number which
// shows how many times the numbers between l and r belongs to smt.
// This method is composed of three process.
ll n, m, s[MAX_M], l, r;
in2(n, m);
rep(i, m) s[i] = 0;
// 1st step
rep(i, n){
in3(l, r, c);
l--; r--; // if l starts from 1.
s[l] += c; s[r + 1] -= c;
}
// 2nd step
rep(i, m - 1) s[i + 1] += s[i];
// 3rd step: judgement...
#5. shakutori method (syakutori, two pointers technique)
// 1. strech right side while the condition is met.
// 2. renew the answer
// 3. increments left side
// 4. Back to 1. (l <= r must be satisfied all the time.)
ll l = 0; ll r = 0;
while (l < n){
r = max(r, l);
if (l == r) r++;
while(r < n && cond) r++;
answer += r - l; l++;
}
prl(answer);
#11. bfs ABC146D, ABC007C
1. first create a tree.
2. start searching from a node.
3. do some processes and push nodes connected with a given target node in
BFS.
4. repeat a series of procedure until queue is empty.
queue<pl> q;
void bfs(ll source, ll parents){
ll n_edge = graph[source].size();
if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1);
if (visited[source]) return;
visited[source] = true;
rep(idx, n_edge){
ll target = graph[source][idx].to;
if (target == parents) continue;
q.push(mp(target, source));
}
}
q.push(mp(sg.e1, -1));
while(!q.empty()){
pl source = q.front(); q.pop();
bfs(source.e1, source.e2);
}
# Cumulative sum (2 dimension)
ll func(ll x, ll y, ll dx, ll dy){
if (x + dx > w || y + dy > h) return - INF;
ll val = cum[x + dx][y + dy];
val += cum[x][y];
val -= cum[x][y + dy];
val -= cum[x + dx][y];
return val;
}
vl2 cum(w + 1, vl(h + 1, 0));
rep(x, w + 1) cum[x][0] = 0;
rep(y, h + 1) cum[0][y] = 0;
rep(y, h) rep(x, w)
cum[x + 1][y + 1] = cum[x][y + 1] + vs[x][y];
rep(x, w + 1) rep(y, h)
cum[x][y + 1] += cum[x][y];
*/
/*
# the operators regarding bit
& (AND), | (OR), ^ (XOR)
- (REVERSE), >> (SMALLER SHIFT)
<< (BIGGER SHIFT)
x1: 0000 0001 0010 0101 0110 0111 0111
x2: xxxx 0001 0011 0100 0101 1000 0110
x1 & x2: 0000 0001 0010 0100 0100 0000 0110
x: 1001 1010 1100 1011 1101 1111
x & - x: 0001 0010 0100 0001 0001 0001
sum: 1010 1100 10000 1100 1100 10000
x << y is x * 2 ** y
x >> y is rep(i, y) x = x // 2
####### Attention #######
1 << i and 1LL << i is different. If programs show WA, try switch to this one.
Let S be a bit sequence and i be a non-negative integer
S & (1 << i) -> if true, i in S
S | (1 << i) -> S union {i}
S & ~(1 << i) -> S - {i}
__builtin_popcount(S) -> the number of elements in S
S = 0 -> S is an empty set
__builtin_popcountl(i) -> the number of 1 in binary
S = (1 << n) - 1 -> S includes all the elements up to the n-th
#Conditional Operator
condition ? true : false;
#iterator
type declaration: auto
value reference: *itr
increment: itr++
decrement: itr--
substitution of value: *itr = smt
# inclusion-exclusion principle
|U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n
- 1) |^[i = 1 to n]Ai|
*/
const ll MAX_N = 200005;
bool okay = false;
ll answer = 0;
str s, t, u;
ll n, m, h, w, k, Q;
ll a, b, c, d, idx, cnt;
vl as, bs, cs, ds;
vb2 block;
Graph graph;
ll judge(vl &fixed_nums) {
cnt = 0;
rep(i, Q) {
a = as[i], b = bs[i], c = cs[i], d = ds[i];
if (fixed_nums[b] - fixed_nums[a] == c)
cnt += d;
}
return cnt;
}
/*
### Attention ###
if k is 0 or 1 and n is 0, it does not work well.
*/
void solve() {
as = bs = cs = ds = vl(Q);
rep(i, Q) in4(as[i], bs[i], cs[i], ds[i]);
rep(i, Q) as[i]--, bs[i]--;
vl nums(n);
ll bit = (1 << (m - 1)) - 1;
for (; bit < (1 << (n + m - 1)); bit = next_combination(bit)) {
ll cur = 1;
idx = 0;
rep(i, n + m - 1) {
if (bit & 1LL << i)
cur++;
else
nums[idx] = cur, idx++;
}
chmax(answer, judge(nums));
}
prl(answer);
}
int main(void) {
in3(n, m, Q);
// assert(n <= 30);
// assert(k <= 400);
solve();
return 0;
}
| replace | 1,129 | 1,130 | 1,129 | 1,134 | 0 | |
p02696 | Python | Time Limit Exceeded | A, B, N = map(int, input().split())
c = 0
t = -10
if N > 100000000:
for i in range(100000000, N + 1):
a = (i * A) // B - A * (i // B)
c = max(c, a)
if t > a or i > 1100000000:
print(c)
break
t = a
else:
print(c)
else:
for i in range(N + 1):
a = (i * A) // B - A * (i // B)
c = max(c, a)
if t > a or i > 100000000:
print(c)
break
t = a
else:
print(c)
| A, B, N = map(int, input().split())
X = min(N, B - 1)
print(A * X // B - A * (X // B))
| replace | 1 | 23 | 1 | 3 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define REP(i, d, n) for (long long i = (d); i < (n); ++i)
#define all(v) v.begin(), v.end()
using ll = long long;
int main() { // 言語変えろ言語変えろ言語変えろ言語変えろ言語変えろ
ll a, b, n;
cin >> a >> b >> n;
ll max = -999999;
ll ans;
rep(i, n) {
if (n - i < b) {
ans = n - i;
break;
}
}
max = ans * a / b - a * (ans / b);
cout << max << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define REP(i, d, n) for (long long i = (d); i < (n); ++i)
#define all(v) v.begin(), v.end()
using ll = long long;
int main() { // 言語変えろ言語変えろ言語変えろ言語変えろ言語変えろ
ll a, b, n;
cin >> a >> b >> n;
ll max = -999999;
ll ans;
if (b > n) {
ans = n;
} else {
ans = b - 1;
}
max = ans * a / b - a * (ans / b);
cout << max << endl;
}
| replace | 13 | 18 | 13 | 17 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const long long mod = 1e9 + 7;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll max = 0;
for (ll i = 0; i <= min(b - 1, n); i++) {
if ((a * i) / b > max) {
max = (a * i) / b;
}
}
cout << max;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const long long mod = 1e9 + 7;
int main() {
ll a, b, n;
cin >> a >> b >> n;
cout << a * min(n, b - 1) / b;
return 0;
} | replace | 15 | 22 | 15 | 16 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
// マクロ&定数&関数 ================================================
typedef unsigned int uint;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;
typedef vector<string> vstring;
typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<double, double>> vpdouble;
typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vpint> vvpint;
typedef vector<vpll> vvpll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<string>> vvstring;
typedef vector<vector<bool>> vvbool;
typedef vector<vector<vector<ll>>> vvvll;
const int c_YET = -1;
const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;4近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:8近傍 9:(0,0)を含む
const ll MOD = 1e9 + 7; // 10^9 + 7
const ll MAX = 1e9;
const double PI = 3.14159265358979323846264338327950288;
//---------------------------------------------------------------
// オーバーフローチェック
//---------------------------------------------------------------
bool is_overflow(ll a, ll b) { return ((a * b) / b != a); }
//---------------------------------------------------------------
// 約数列挙
//---------------------------------------------------------------
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
//---------------------------------------------------------------
// N以下のすべての素数を列挙する(エラトステネスの篩)
//---------------------------------------------------------------
vbool searchSosuu(ll N) {
vbool sosuu; // ←これをグローバル変数にして1度だけ実行する
for (ll i = 0; i < N; i++) {
sosuu.emplace_back(true);
}
sosuu[0] = false;
sosuu[1] = false;
for (ll i = 2; i < N; i++) {
if (sosuu[i]) {
for (ll j = 2; i * j < N; j++) {
sosuu[i * j] = false;
}
}
}
return sosuu;
}
//---------------------------------------------------------------
// 素因数分解 O(√N)
//---------------------------------------------------------------
vpll div_prime(ll n) {
vpll prime_factor;
for (ll i = 2; i * i <= n; i++) {
ll count = 0;
while (n % i == 0) {
count++;
n /= i;
}
if (count) {
pair<ll, ll> temp = {i, count};
prime_factor.emplace_back(temp);
}
}
if (n != 1) {
pair<ll, ll> temp = {n, 1};
prime_factor.emplace_back(temp);
}
return prime_factor;
}
//---------------------------------------------------------------
// 素数判定
//---------------------------------------------------------------
bool is_sosuu(ll N) {
if (N < 2) {
return false;
} else if (N == 2) {
return true;
} else if (N % 2 == 0) {
return false;
}
for (ll i = 3; i <= sqrt(N); i += 2) {
if (N % i == 0) {
return false;
}
}
return true;
}
//---------------------------------------------------------------
// 最大公約数(ユークリッドの互除法)
//---------------------------------------------------------------
ll gcd(ll a, ll b) {
if (a < b) {
ll tmp = a;
a = b;
b = tmp;
}
ll r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
//---------------------------------------------------------------
// 最小公倍数
//---------------------------------------------------------------
ll lcm(ll a, ll b) {
ll temp = gcd(a, b);
return temp * (a / temp) * (b / temp);
}
//---------------------------------------------------------------
// 階乗
//---------------------------------------------------------------
ll factorial(ll n) {
if (n <= 1) {
return 1;
}
return (n * (factorial(n - 1))) % MOD;
}
//---------------------------------------------------------------
// 高速コンビネーション計算(前処理:O(N) 計算:O(1))
//---------------------------------------------------------------
// テーブルを作る前処理
ll comb_const = 200005;
vll fac(comb_const), finv(comb_const), inv(comb_const);
bool COMineted = false;
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < comb_const; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
COMineted = true;
}
// 二項係数計算
ll COM(ll n, ll k) {
if (COMineted == false)
COMinit();
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
//---------------------------------------------------------------
// 繰り返し2乗法 base^sisuu
//---------------------------------------------------------------
ll RepeatSquaring(ll base, ll sisuu) {
if (sisuu < 0) {
cout << "RepeatSquaring: 指数が負です!" << endl;
return 0;
}
if (sisuu == 0)
return 1;
if (sisuu % 2 == 0) {
ll t = RepeatSquaring(base, sisuu / 2) % MOD;
return (t * t) % MOD;
}
return base * RepeatSquaring(base, sisuu - 1) % MOD;
}
//---------------------------------------------------------------
// 高速単発コンビネーション計算(O(logN))
//---------------------------------------------------------------
ll fast_com(ll a, ll b) {
ll bunshi = 1;
ll bunbo = 1;
for (ll i = 1; i <= b; i++) {
bunbo *= i;
bunbo %= MOD;
bunshi *= (a - i + 1);
bunshi %= MOD;
}
ll ret = bunshi * RepeatSquaring(bunbo, MOD - 2);
ret %= MOD;
while (ret < 0) {
ret += MOD;
}
return ret;
}
//---------------------------------------------------------------
// 2直線の交差判定(直線(x1, y1)->(x2, y2) と 直線(X1, Y1)->(X2, Y2))
//---------------------------------------------------------------
bool is_cross(ll x1, ll y1, ll x2, ll y2, ll X1, ll Y1, ll X2, ll Y2) {
ll dx_ai = X1 - x1;
ll dy_ai = Y1 - y1;
ll dx_bi = X1 - x2;
ll dy_bi = Y1 - y2;
ll dx_ai2 = X2 - x1;
ll dy_ai2 = Y2 - y1;
ll dx_bi2 = X2 - x2;
ll dy_bi2 = Y2 - y2;
ll si = dx_ai * dy_bi - dy_ai * dx_bi;
ll si2 = dx_ai2 * dy_bi2 - dy_ai2 * dx_bi2;
ll si3 = dx_ai * dy_ai2 - dy_ai * dx_ai2;
ll si4 = dx_bi * dy_bi2 - dy_bi * dx_bi2;
return (si * si2 < 0 && si3 * si4 < 0);
}
//---------------------------------------------------------------
// 最長増加部分列の長さ(O(NlogN))
//---------------------------------------------------------------
ll LSI(vll vec, ll size) {
vll lsi(size + 1); // 長さjを作った時の右端の最小値
for (ll i = 0; i <= size; i++) {
lsi[i] = LLINF;
}
lsi[0] = 0;
lsi[1] = vec[0];
for (ll i = 1; i < size; i++) {
// 初めてvec[i]の方が小さくなるところを探す
auto Iter = lower_bound(lsi.begin(), lsi.end(), vec[i]);
ll idx = Iter - lsi.begin();
if (idx > 0 && lsi[idx - 1] < vec[i]) // 条件文の前半怪しい
{
lsi[idx] = vec[i];
}
}
for (ll i = size; i >= 0; i--) {
if (lsi[i] < LLINF) {
return i;
}
}
}
//---------------------------------------------------------------
// 木の根からの深さ
//---------------------------------------------------------------
vll tree_depth(vvll edge, ll start_node, ll n_node) {
vll dist(n_node, LLINF);
dist[start_node] = 0;
stack<pll> S;
S.push({start_node, 0});
while (!S.empty()) {
ll node = S.top().first;
ll d = S.top().second;
dist[node] = d;
S.pop();
for (int i = 0; i < edge[node].size(); i++) {
if (dist[edge[node][i]] == LLINF) {
S.push({edge[node][i], d + 1});
}
}
}
return dist;
}
//---------------------------------------------------------------
// ワーシャルフロイド法(O(N^3)) 任意の2点間の最短距離
//---------------------------------------------------------------
vvll warshall_floyd(ll n, vvll d) {
for (int k = 0; k < n; k++) { // 経由する頂点
for (int i = 0; i < n; i++) { // 始点
for (int j = 0; j < n; j++) { // 終点
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
return d;
}
//---------------------------------------------------------------
// Union Find
//---------------------------------------------------------------
/*
UnionFind uf(要素の個数);
for(int i = 0;i < 関係の個数; i++)
{
uf.merge(A[i], B[i]);
}
nを含む集合の大きさ = uf.size(n)
nを含む集合の代表者 = uf.root(n)
集合の個数 = uf.n_group
*/
class UnionFind {
public:
vector<ll> par; // 各元の親を表す配列
vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)
ll n_group; // 集合の数
// Constructor
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
n_group = sz_;
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
n_group--;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
};
//========================================================================
int main() {
////==================================
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(30);
////==================================
ll A, B, N;
cin >> A >> B >> N;
ll ans = 0;
for (int i = 0; i <= min(N, ll(1000000)); i--) {
ans = max(ans, ll((i * A) / B) - A * ll(i / B));
}
for (int j = 1000; j < 1010; j++) {
for (int i = N - j * 10000000; i >= max(N - j * 10000000 - 10000000, ll(0));
i--) {
ans = max(ans, ll((i * A) / B) - A * ll(i / B));
}
}
cout << ans;
}
| #include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
// マクロ&定数&関数 ================================================
typedef unsigned int uint;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;
typedef vector<string> vstring;
typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<double, double>> vpdouble;
typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vpint> vvpint;
typedef vector<vpll> vvpll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<string>> vvstring;
typedef vector<vector<bool>> vvbool;
typedef vector<vector<vector<ll>>> vvvll;
const int c_YET = -1;
const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;4近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:8近傍 9:(0,0)を含む
const ll MOD = 1e9 + 7; // 10^9 + 7
const ll MAX = 1e9;
const double PI = 3.14159265358979323846264338327950288;
//---------------------------------------------------------------
// オーバーフローチェック
//---------------------------------------------------------------
bool is_overflow(ll a, ll b) { return ((a * b) / b != a); }
//---------------------------------------------------------------
// 約数列挙
//---------------------------------------------------------------
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
//---------------------------------------------------------------
// N以下のすべての素数を列挙する(エラトステネスの篩)
//---------------------------------------------------------------
vbool searchSosuu(ll N) {
vbool sosuu; // ←これをグローバル変数にして1度だけ実行する
for (ll i = 0; i < N; i++) {
sosuu.emplace_back(true);
}
sosuu[0] = false;
sosuu[1] = false;
for (ll i = 2; i < N; i++) {
if (sosuu[i]) {
for (ll j = 2; i * j < N; j++) {
sosuu[i * j] = false;
}
}
}
return sosuu;
}
//---------------------------------------------------------------
// 素因数分解 O(√N)
//---------------------------------------------------------------
vpll div_prime(ll n) {
vpll prime_factor;
for (ll i = 2; i * i <= n; i++) {
ll count = 0;
while (n % i == 0) {
count++;
n /= i;
}
if (count) {
pair<ll, ll> temp = {i, count};
prime_factor.emplace_back(temp);
}
}
if (n != 1) {
pair<ll, ll> temp = {n, 1};
prime_factor.emplace_back(temp);
}
return prime_factor;
}
//---------------------------------------------------------------
// 素数判定
//---------------------------------------------------------------
bool is_sosuu(ll N) {
if (N < 2) {
return false;
} else if (N == 2) {
return true;
} else if (N % 2 == 0) {
return false;
}
for (ll i = 3; i <= sqrt(N); i += 2) {
if (N % i == 0) {
return false;
}
}
return true;
}
//---------------------------------------------------------------
// 最大公約数(ユークリッドの互除法)
//---------------------------------------------------------------
ll gcd(ll a, ll b) {
if (a < b) {
ll tmp = a;
a = b;
b = tmp;
}
ll r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
//---------------------------------------------------------------
// 最小公倍数
//---------------------------------------------------------------
ll lcm(ll a, ll b) {
ll temp = gcd(a, b);
return temp * (a / temp) * (b / temp);
}
//---------------------------------------------------------------
// 階乗
//---------------------------------------------------------------
ll factorial(ll n) {
if (n <= 1) {
return 1;
}
return (n * (factorial(n - 1))) % MOD;
}
//---------------------------------------------------------------
// 高速コンビネーション計算(前処理:O(N) 計算:O(1))
//---------------------------------------------------------------
// テーブルを作る前処理
ll comb_const = 200005;
vll fac(comb_const), finv(comb_const), inv(comb_const);
bool COMineted = false;
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < comb_const; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
COMineted = true;
}
// 二項係数計算
ll COM(ll n, ll k) {
if (COMineted == false)
COMinit();
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
//---------------------------------------------------------------
// 繰り返し2乗法 base^sisuu
//---------------------------------------------------------------
ll RepeatSquaring(ll base, ll sisuu) {
if (sisuu < 0) {
cout << "RepeatSquaring: 指数が負です!" << endl;
return 0;
}
if (sisuu == 0)
return 1;
if (sisuu % 2 == 0) {
ll t = RepeatSquaring(base, sisuu / 2) % MOD;
return (t * t) % MOD;
}
return base * RepeatSquaring(base, sisuu - 1) % MOD;
}
//---------------------------------------------------------------
// 高速単発コンビネーション計算(O(logN))
//---------------------------------------------------------------
ll fast_com(ll a, ll b) {
ll bunshi = 1;
ll bunbo = 1;
for (ll i = 1; i <= b; i++) {
bunbo *= i;
bunbo %= MOD;
bunshi *= (a - i + 1);
bunshi %= MOD;
}
ll ret = bunshi * RepeatSquaring(bunbo, MOD - 2);
ret %= MOD;
while (ret < 0) {
ret += MOD;
}
return ret;
}
//---------------------------------------------------------------
// 2直線の交差判定(直線(x1, y1)->(x2, y2) と 直線(X1, Y1)->(X2, Y2))
//---------------------------------------------------------------
bool is_cross(ll x1, ll y1, ll x2, ll y2, ll X1, ll Y1, ll X2, ll Y2) {
ll dx_ai = X1 - x1;
ll dy_ai = Y1 - y1;
ll dx_bi = X1 - x2;
ll dy_bi = Y1 - y2;
ll dx_ai2 = X2 - x1;
ll dy_ai2 = Y2 - y1;
ll dx_bi2 = X2 - x2;
ll dy_bi2 = Y2 - y2;
ll si = dx_ai * dy_bi - dy_ai * dx_bi;
ll si2 = dx_ai2 * dy_bi2 - dy_ai2 * dx_bi2;
ll si3 = dx_ai * dy_ai2 - dy_ai * dx_ai2;
ll si4 = dx_bi * dy_bi2 - dy_bi * dx_bi2;
return (si * si2 < 0 && si3 * si4 < 0);
}
//---------------------------------------------------------------
// 最長増加部分列の長さ(O(NlogN))
//---------------------------------------------------------------
ll LSI(vll vec, ll size) {
vll lsi(size + 1); // 長さjを作った時の右端の最小値
for (ll i = 0; i <= size; i++) {
lsi[i] = LLINF;
}
lsi[0] = 0;
lsi[1] = vec[0];
for (ll i = 1; i < size; i++) {
// 初めてvec[i]の方が小さくなるところを探す
auto Iter = lower_bound(lsi.begin(), lsi.end(), vec[i]);
ll idx = Iter - lsi.begin();
if (idx > 0 && lsi[idx - 1] < vec[i]) // 条件文の前半怪しい
{
lsi[idx] = vec[i];
}
}
for (ll i = size; i >= 0; i--) {
if (lsi[i] < LLINF) {
return i;
}
}
}
//---------------------------------------------------------------
// 木の根からの深さ
//---------------------------------------------------------------
vll tree_depth(vvll edge, ll start_node, ll n_node) {
vll dist(n_node, LLINF);
dist[start_node] = 0;
stack<pll> S;
S.push({start_node, 0});
while (!S.empty()) {
ll node = S.top().first;
ll d = S.top().second;
dist[node] = d;
S.pop();
for (int i = 0; i < edge[node].size(); i++) {
if (dist[edge[node][i]] == LLINF) {
S.push({edge[node][i], d + 1});
}
}
}
return dist;
}
//---------------------------------------------------------------
// ワーシャルフロイド法(O(N^3)) 任意の2点間の最短距離
//---------------------------------------------------------------
vvll warshall_floyd(ll n, vvll d) {
for (int k = 0; k < n; k++) { // 経由する頂点
for (int i = 0; i < n; i++) { // 始点
for (int j = 0; j < n; j++) { // 終点
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
return d;
}
//---------------------------------------------------------------
// Union Find
//---------------------------------------------------------------
/*
UnionFind uf(要素の個数);
for(int i = 0;i < 関係の個数; i++)
{
uf.merge(A[i], B[i]);
}
nを含む集合の大きさ = uf.size(n)
nを含む集合の代表者 = uf.root(n)
集合の個数 = uf.n_group
*/
class UnionFind {
public:
vector<ll> par; // 各元の親を表す配列
vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)
ll n_group; // 集合の数
// Constructor
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
n_group = sz_;
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
n_group--;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
};
//========================================================================
int main() {
////==================================
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(30);
////==================================
ll A, B, N;
cin >> A >> B >> N;
ll X = min(B - 1, N);
cout << (A * X) / B - A * (X / B);
}
| replace | 428 | 446 | 428 | 431 | TLE | |
p02696 | C++ | Runtime Error | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ll a, b, n;
cin >> a >> b >> n;
vector<long double> ans(n, 0);
for (int i = 1; i <= n; i++)
ans[i - 1] = floor(a * i / b) - a * floor(i / b);
sort(ans.begin(), ans.end());
cout << floor(ans[n - 1]) << endl;
} | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ll a, b, n;
cin >> a >> b >> n;
if (b > n)
cout << floor(a * n / b) << endl;
else
cout << floor(a * (b - 1) / b) << endl;
} | replace | 9 | 17 | 9 | 13 | 0 | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll a, b, n;
cin >> a >> b >> n;
ll max_val = -1;
rep(x, n + 1) {
ll val = floor(a * x / b) - a * floor(x / b);
chmax(max_val, val);
}
cout << max_val << endl;
return 0;
} | #include <bits/stdc++.h>
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll a, b, n;
cin >> a >> b >> n;
auto f = [&](ll x) { return (ll)floor(a * x / (double)b); };
ll ans = f(min(b - 1, n));
cout << ans << endl;
return 0;
} | replace | 33 | 39 | 33 | 37 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
using namespace std;
int main() {
long long a, b, n;
cin >> a >> b >> n;
long long z = floor(a / b) - a * floor(1 / b);
for (int i = 2; i <= n; i++) {
int z1 = floor(a * i / b) - a * floor(i / b);
if (z1 > z)
z = z1;
}
cout << z << endl;
} | #include <cmath>
#include <iostream>
using namespace std;
int main() {
long long a, b, c;
cin >> a >> b >> c;
if (c >= b)
c = b - 1;
cout << floor(a * c / b) - a * floor(c / b) << endl;
} | replace | 5 | 14 | 5 | 10 | TLE | |
p02696 | C++ | Time Limit Exceeded | // ABC165D -
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int A, B, N;
cin >> A >> B >> N;
long long int ans = N * A / B - N / B * A;
for (long long int i = B - 1; i <= N; i += B) {
if ((i * A / B - i / B * A) >= ans) {
ans = i * A / B - i / B * A;
}
}
cout << ans << endl;
return 0;
}
| // ABC165D -
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int A, B, N;
cin >> A >> B >> N;
long long int ans = 0;
if (N >= B - 1) {
ans = A * (B - 1) / B;
} else {
ans = A * N / B;
}
cout << ans << endl;
return 0;
}
| replace | 9 | 14 | 9 | 14 | TLE | |
p02696 | C++ | Time Limit Exceeded | //----AUTHOR:kkdrummer----/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef unordered_set<ll> usll;
// typedef unordered_multiset<ll> umsll;
typedef multiset<ll> msll;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef priority_queue<ll> pqll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
// typedef unordered_multiset<int> umsi;
// typedef unordered_set<int> usi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef set<pi> spi;
typedef priority_queue<int> pqi;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ind_set;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
ind_setll;
#define in insert
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define be begin
#define en end
#define itr iterator
#define io \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mo 1000000007
#define inf 9223372036854775807
#define ninf -inf
#define ima 2147483647
#define imi -ima
#define oncnt __builtin_popcount
#define zerobegin __builtin_clz
#define zeroend __builtin_ctz
#define parity __builtin_parity
#define all(x) x.be(), x.en()
#define eps 1e-9
#define coutd cout << setprecision(10) << fixed
#define mems(dp, x) memset(dp, x, sizeof(dp))
const ld PI = 3.1415926535897932384626433832792884197169399375105820974944;
inline ll modpow(ll x, ll n) {
if (n == 0)
return 1;
if (n == 1)
return (x % mo);
ll u = (modpow(x, n / 2));
u = (u * u) % mo;
if (n % 2 != 0)
u = (u * x % mo) % mo;
return u;
}
inline ll modinv(ll x) { return modpow(x, mo - 2); }
inline ll mmul(ll a, ll b) {
if (a >= mo)
a = a % mo;
if (b >= mo)
b = b % mo;
if (a * b >= mo)
return (a * b) % mo;
return (a * b);
}
inline ll madd(ll a, ll b) {
if (a >= mo)
a = a % mo;
if (b >= mo)
b = b % mo;
if (a + b >= mo)
return (a + b) % mo;
return (a + b);
}
inline ll msub(ll a, ll b) {
if (a >= mo)
a = a % mo;
if (b >= mo)
b = b % mo;
return (((a - b) % mo + mo) % mo);
}
inline ll mdiv(ll a, ll bb) {
if (a >= mo)
a = a % mo;
ll b = modinv(bb);
if (b >= mo)
b = b % mo;
if (a * b >= mo)
return (a * b) % mo;
return (a * b);
}
inline ll gcd(ll a, ll b) { return __gcd(a, b); }
inline ll lcm(ll a, ll b) { return ((a * b) / gcd(a, b)); }
ll val(ll a, ll b, ll x) { return ((a * x) / b) - (a * (x / b)); }
int main() {
io int testcases = 1; // cin>>testcases;
while (testcases--) {
ll a, b, n;
cin >> a >> b >> n;
ll am = 0;
if (n < b)
cout << val(a, b, n);
else if ((n / b) < 10000000) {
for (ll i = b - 1; i <= n; i += b) {
ll vp = val(a, b, i);
if (am > vp)
break;
am = max(am, vp);
}
am = max(val(a, b, n), am);
cout << am;
}
else {
for (ll i = n - (n % b) - 1; i >= 1; i -= b) {
ll vp = val(a, b, i);
if (am > vp)
break;
am = max(am, vp);
}
am = max(val(a, b, n), am);
cout << am;
}
}
return 0;
}
| //----AUTHOR:kkdrummer----/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef unordered_set<ll> usll;
// typedef unordered_multiset<ll> umsll;
typedef multiset<ll> msll;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef priority_queue<ll> pqll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
// typedef unordered_multiset<int> umsi;
// typedef unordered_set<int> usi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef set<pi> spi;
typedef priority_queue<int> pqi;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ind_set;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
ind_setll;
#define in insert
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define be begin
#define en end
#define itr iterator
#define io \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define mo 1000000007
#define inf 9223372036854775807
#define ninf -inf
#define ima 2147483647
#define imi -ima
#define oncnt __builtin_popcount
#define zerobegin __builtin_clz
#define zeroend __builtin_ctz
#define parity __builtin_parity
#define all(x) x.be(), x.en()
#define eps 1e-9
#define coutd cout << setprecision(10) << fixed
#define mems(dp, x) memset(dp, x, sizeof(dp))
const ld PI = 3.1415926535897932384626433832792884197169399375105820974944;
inline ll modpow(ll x, ll n) {
if (n == 0)
return 1;
if (n == 1)
return (x % mo);
ll u = (modpow(x, n / 2));
u = (u * u) % mo;
if (n % 2 != 0)
u = (u * x % mo) % mo;
return u;
}
inline ll modinv(ll x) { return modpow(x, mo - 2); }
inline ll mmul(ll a, ll b) {
if (a >= mo)
a = a % mo;
if (b >= mo)
b = b % mo;
if (a * b >= mo)
return (a * b) % mo;
return (a * b);
}
inline ll madd(ll a, ll b) {
if (a >= mo)
a = a % mo;
if (b >= mo)
b = b % mo;
if (a + b >= mo)
return (a + b) % mo;
return (a + b);
}
inline ll msub(ll a, ll b) {
if (a >= mo)
a = a % mo;
if (b >= mo)
b = b % mo;
return (((a - b) % mo + mo) % mo);
}
inline ll mdiv(ll a, ll bb) {
if (a >= mo)
a = a % mo;
ll b = modinv(bb);
if (b >= mo)
b = b % mo;
if (a * b >= mo)
return (a * b) % mo;
return (a * b);
}
inline ll gcd(ll a, ll b) { return __gcd(a, b); }
inline ll lcm(ll a, ll b) { return ((a * b) / gcd(a, b)); }
ll val(ll a, ll b, ll x) { return ((a * x) / b) - (a * (x / b)); }
int main() {
io int testcases = 1; // cin>>testcases;
while (testcases--) {
ll a, b, n;
cin >> a >> b >> n;
ll am = 0;
if (n < b)
cout << val(a, b, n);
else if ((n / b) < 10000000) {
for (ll i = b - 1; i <= n; i += b) {
ll vp = val(a, b, i);
if (am > vp)
break;
am = max(am, vp);
}
am = max(val(a, b, n), am);
cout << am;
}
else {
cout << val(a, b, n);
}
}
return 0;
}
| replace | 132 | 140 | 132 | 133 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define etm \
cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define doub long double
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pdd pair<double, double>
#define pll pair<long long int, long long int>
#define vpl vector<pll>
#define vll vector<ll>
#define vb vector<bool>
#define vvbb vector<vb>
#define vi vector<int>
#define mi map<int, int>
#define mull map<ull, ull>
#define stp setprecision(20)
#define N 100005
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define repb(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
#define MOD 1000000007
#define ld long double
#define inf 1e18
#define mp make_pair
#define vpll vector<pair<ll, ll>>
#define vvpll vector<vector<pair<ll, ll>>>
#define vvll vector<vector<ll>>
#define vvii vector<vector<int>>
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define test \
ll T; \
cin >> T; \
while (T--)
#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define show(w, size) \
for (ll i = 0; i < size; i++) \
cout << w[i] << " ";
#define print(a) cout << a << "\n";
#define pqll priority_queue<ll>
#define mset(dp, no) memset(dp, no, sizeof(dp))
#define umll unordered_map<ll, ll>
#define mll map<ll, ll>
#define input(a, n) \
ll I; \
rep(I, 0, n - 1, 1) cin >> a[I];
#define countbit __builtin_popcount // Number of set bits .
#define fbo(k) find_by_order // K-th element in a set (counting from zero) .
#define ook(k) order_of_key // Number of items strictly smaller than k .
#define lb lower_bound
#define up upper_bound
#define in insert
// #define db(x) cout <<#x<<": "<<x<<'\n';
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
using namespace std;
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
ll gcd(ll a, ll b) {
if (a < b)
return gcd(b, a);
else if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll isPrime(ll n) {
ll p = (ll)sqrt(n);
rep(i, 2, p, 1) if (n % i == 0) return 0;
return 1;
} // reuturn 1 if prime
ll pow(ll b, ll e) {
if (e == 0)
return 1;
else if (e % 2 == 0) {
ll a = pow(b, e / 2);
return a * a;
} else {
ll a = pow(b, e / 2);
return b * a * a;
}
}
ll powm(ll x, ll y, ll m = MOD) {
x = x % m;
ll res = 1;
while (y) {
if (y & 1)
res = res * x;
res %= m;
y = y >> 1;
x = x * x;
x %= m;
}
return res;
}
ll ceil(long double a) {
ll b = a;
if (a == b) {
return b;
} else {
return b + 1;
}
}
ll floor(long double a) {
ll b = a;
return b;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll modInverse(ll a, ll m) { return powm(a, m - 2, m); }
bool issq(ll n) {
ll p = sqrt(n);
return p * p == n;
}
vll prime; // if i==prime[i] then prime otherwise smallest prime factor of that
// number
void sieve(ll n) {
prime.resize(n + 1, 1);
prime[0] = 0, prime[1] = 0;
for (ll i = 2; i * i <= n; i++)
if (prime[i])
for (ll j = i * i; j <= n; j += i)
prime[j] = 0;
}
ll extended_GCD(ll a, ll b, ll &x, ll &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll gcd = extended_GCD(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
ll mulInv(ll a, ll mod = 26) {
ll x, y;
extended_GCD(a, mod, x, y);
if (x < 0)
x += mod;
return x;
}
ll find(ll num[], ll rem[], ll k, ll prod) {
// Compute product of all numbers
ll result = 0;
// Apply above formula
for (ll i = 0; i < k; i++) {
ll pp = prod / num[i];
result += rem[i] * mulInv(pp, num[i]) * pp;
}
return result % prod;
}
ll nCr(ll n, ll k) {
ll res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (ll i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
class DSU {
public:
vll parent, size;
public:
DSU(ll n) {
parent.resize(n + 1);
size.resize(n + 1);
for (ll i = 1; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
public:
ll find_set(ll x) {
if (parent[x] == x) {
return x;
}
return parent[x] = find_set(parent[x]);
}
public:
void union_set(ll x, ll y) {
x = find_set(x);
y = find_set(y);
if (x != y) {
parent[y] = x;
}
}
};
bool cmp(pair<ll, ll> &p1, pair<ll, ll> &p2) {
if (p1.fi == p2.fi) {
return p1.se > p2.se;
}
return p1.fi > p2.fi;
}
bool isPalindrome(string s) {
ll i, j;
for (i = 0, j = s.length() - 1; i <= j; i++, j--) {
if (s[i] != s[j]) {
return 0;
}
}
return 1;
}
ll ToInt(char ch) { return ch - '0'; }
char ToChar(ll a) { return a + '0'; }
bool isSubSeq(string str1, string str2, ll m, ll n) {
// Base Cases
if (m == 0)
return true;
if (n == 0)
return false;
// If last characters of two strings are matching
if (str1[m - 1] == str2[n - 1])
return isSubSeq(str1, str2, m - 1, n - 1);
// If last characters are not matching
return isSubSeq(str1, str2, m, n - 1);
}
void printVectorPair(vpll v) {
for (ll i = 0; i < v.size(); i++) {
cout << v[i].fi << " " << v[i].se << "\n";
}
}
void modBigNumber(string num, ll m) {
// Store the modulus of big number
vector<int> vec;
ll mod = 0;
// Do step by step division
for (int i = 0; i < num.size(); i++) {
int digit = num[i] - '0';
mod = mod * 10 + digit;
int quo = mod / m;
if ((vec.size() != 0) || (quo != 0)) // to remove initiale zeros
vec.push_back(quo);
mod = mod % m;
}
// cout << "\nRemainder : " << mod << "\n";
// cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n";
return;
}
struct SegmentTree {
ll n;
vll v;
SegmentTree(ll size) {
n = 4 * size + 1;
v.resize(n, 0);
}
void build(ll ar[], ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return;
if (ipos == fpos)
v[pos] = ar[ipos];
else {
ll mid = (ipos + fpos) / 2;
build(ar, ipos, mid, pos * 2);
build(ar, mid + 1, fpos, pos * 2 + 1);
v[pos] = v[pos * 2] + v[pos * 2 + 1];
}
}
void update(ll index, ll val, ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return;
if (ipos == fpos)
v[pos] += val;
else {
v[pos] += val;
ll mid = (ipos + fpos) / 2;
if (mid >= index)
update(index, val, ipos, mid, pos * 2);
else
update(index, val, mid + 1, fpos, pos * 2 + 1);
}
}
ll get_sum(ll l, ll r, ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return 0;
if (l > r)
return 0;
ll mid = (ipos + fpos) / 2;
if ((l == ipos) && (r == fpos))
return v[pos];
else
return get_sum(l, min(mid, r), ipos, mid, pos * 2) +
get_sum(max(mid + 1, l), r, mid + 1, fpos, pos * 2 + 1);
}
};
struct BIT {
vector<ll> bitree;
ll n;
BIT(ll n) {
this->n = n;
bitree.resize(n + 1, 0);
}
void update(ll idx, ll val) {
idx++;
while (idx <= n) {
bitree[idx] += val;
idx += idx & (-idx);
}
}
ll Sum(ll idx) {
ll sum = 0;
idx++;
while (idx > 0) {
sum += bitree[idx];
idx -= idx & (-idx);
}
return sum;
}
};
ll sumofdigits(ll a) {
ll val = 0;
while (a > 0) {
val += a % 10;
a /= 10;
}
return val;
}
/*
ideas
*/
int main() {
fastio;
ll n, a, b, ans = LLONG_MIN;
cin >> a >> b >> n;
rep(i, 1, min(n, (ll)1e12), 1) {
ans = max(ans, ((a * i) / b) - (a * (i / b)));
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define etm \
cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define doub long double
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pdd pair<double, double>
#define pll pair<long long int, long long int>
#define vpl vector<pll>
#define vll vector<ll>
#define vb vector<bool>
#define vvbb vector<vb>
#define vi vector<int>
#define mi map<int, int>
#define mull map<ull, ull>
#define stp setprecision(20)
#define N 100005
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define repb(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
#define MOD 1000000007
#define ld long double
#define inf 1e18
#define mp make_pair
#define vpll vector<pair<ll, ll>>
#define vvpll vector<vector<pair<ll, ll>>>
#define vvll vector<vector<ll>>
#define vvii vector<vector<int>>
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define test \
ll T; \
cin >> T; \
while (T--)
#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define show(w, size) \
for (ll i = 0; i < size; i++) \
cout << w[i] << " ";
#define print(a) cout << a << "\n";
#define pqll priority_queue<ll>
#define mset(dp, no) memset(dp, no, sizeof(dp))
#define umll unordered_map<ll, ll>
#define mll map<ll, ll>
#define input(a, n) \
ll I; \
rep(I, 0, n - 1, 1) cin >> a[I];
#define countbit __builtin_popcount // Number of set bits .
#define fbo(k) find_by_order // K-th element in a set (counting from zero) .
#define ook(k) order_of_key // Number of items strictly smaller than k .
#define lb lower_bound
#define up upper_bound
#define in insert
// #define db(x) cout <<#x<<": "<<x<<'\n';
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
using namespace std;
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
ll gcd(ll a, ll b) {
if (a < b)
return gcd(b, a);
else if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll isPrime(ll n) {
ll p = (ll)sqrt(n);
rep(i, 2, p, 1) if (n % i == 0) return 0;
return 1;
} // reuturn 1 if prime
ll pow(ll b, ll e) {
if (e == 0)
return 1;
else if (e % 2 == 0) {
ll a = pow(b, e / 2);
return a * a;
} else {
ll a = pow(b, e / 2);
return b * a * a;
}
}
ll powm(ll x, ll y, ll m = MOD) {
x = x % m;
ll res = 1;
while (y) {
if (y & 1)
res = res * x;
res %= m;
y = y >> 1;
x = x * x;
x %= m;
}
return res;
}
ll ceil(long double a) {
ll b = a;
if (a == b) {
return b;
} else {
return b + 1;
}
}
ll floor(long double a) {
ll b = a;
return b;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll modInverse(ll a, ll m) { return powm(a, m - 2, m); }
bool issq(ll n) {
ll p = sqrt(n);
return p * p == n;
}
vll prime; // if i==prime[i] then prime otherwise smallest prime factor of that
// number
void sieve(ll n) {
prime.resize(n + 1, 1);
prime[0] = 0, prime[1] = 0;
for (ll i = 2; i * i <= n; i++)
if (prime[i])
for (ll j = i * i; j <= n; j += i)
prime[j] = 0;
}
ll extended_GCD(ll a, ll b, ll &x, ll &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll gcd = extended_GCD(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
ll mulInv(ll a, ll mod = 26) {
ll x, y;
extended_GCD(a, mod, x, y);
if (x < 0)
x += mod;
return x;
}
ll find(ll num[], ll rem[], ll k, ll prod) {
// Compute product of all numbers
ll result = 0;
// Apply above formula
for (ll i = 0; i < k; i++) {
ll pp = prod / num[i];
result += rem[i] * mulInv(pp, num[i]) * pp;
}
return result % prod;
}
ll nCr(ll n, ll k) {
ll res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (ll i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
class DSU {
public:
vll parent, size;
public:
DSU(ll n) {
parent.resize(n + 1);
size.resize(n + 1);
for (ll i = 1; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
public:
ll find_set(ll x) {
if (parent[x] == x) {
return x;
}
return parent[x] = find_set(parent[x]);
}
public:
void union_set(ll x, ll y) {
x = find_set(x);
y = find_set(y);
if (x != y) {
parent[y] = x;
}
}
};
bool cmp(pair<ll, ll> &p1, pair<ll, ll> &p2) {
if (p1.fi == p2.fi) {
return p1.se > p2.se;
}
return p1.fi > p2.fi;
}
bool isPalindrome(string s) {
ll i, j;
for (i = 0, j = s.length() - 1; i <= j; i++, j--) {
if (s[i] != s[j]) {
return 0;
}
}
return 1;
}
ll ToInt(char ch) { return ch - '0'; }
char ToChar(ll a) { return a + '0'; }
bool isSubSeq(string str1, string str2, ll m, ll n) {
// Base Cases
if (m == 0)
return true;
if (n == 0)
return false;
// If last characters of two strings are matching
if (str1[m - 1] == str2[n - 1])
return isSubSeq(str1, str2, m - 1, n - 1);
// If last characters are not matching
return isSubSeq(str1, str2, m, n - 1);
}
void printVectorPair(vpll v) {
for (ll i = 0; i < v.size(); i++) {
cout << v[i].fi << " " << v[i].se << "\n";
}
}
void modBigNumber(string num, ll m) {
// Store the modulus of big number
vector<int> vec;
ll mod = 0;
// Do step by step division
for (int i = 0; i < num.size(); i++) {
int digit = num[i] - '0';
mod = mod * 10 + digit;
int quo = mod / m;
if ((vec.size() != 0) || (quo != 0)) // to remove initiale zeros
vec.push_back(quo);
mod = mod % m;
}
// cout << "\nRemainder : " << mod << "\n";
// cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n";
return;
}
struct SegmentTree {
ll n;
vll v;
SegmentTree(ll size) {
n = 4 * size + 1;
v.resize(n, 0);
}
void build(ll ar[], ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return;
if (ipos == fpos)
v[pos] = ar[ipos];
else {
ll mid = (ipos + fpos) / 2;
build(ar, ipos, mid, pos * 2);
build(ar, mid + 1, fpos, pos * 2 + 1);
v[pos] = v[pos * 2] + v[pos * 2 + 1];
}
}
void update(ll index, ll val, ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return;
if (ipos == fpos)
v[pos] += val;
else {
v[pos] += val;
ll mid = (ipos + fpos) / 2;
if (mid >= index)
update(index, val, ipos, mid, pos * 2);
else
update(index, val, mid + 1, fpos, pos * 2 + 1);
}
}
ll get_sum(ll l, ll r, ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return 0;
if (l > r)
return 0;
ll mid = (ipos + fpos) / 2;
if ((l == ipos) && (r == fpos))
return v[pos];
else
return get_sum(l, min(mid, r), ipos, mid, pos * 2) +
get_sum(max(mid + 1, l), r, mid + 1, fpos, pos * 2 + 1);
}
};
struct BIT {
vector<ll> bitree;
ll n;
BIT(ll n) {
this->n = n;
bitree.resize(n + 1, 0);
}
void update(ll idx, ll val) {
idx++;
while (idx <= n) {
bitree[idx] += val;
idx += idx & (-idx);
}
}
ll Sum(ll idx) {
ll sum = 0;
idx++;
while (idx > 0) {
sum += bitree[idx];
idx -= idx & (-idx);
}
return sum;
}
};
ll sumofdigits(ll a) {
ll val = 0;
while (a > 0) {
val += a % 10;
a /= 10;
}
return val;
}
/*
ideas
*/
int main() {
fastio;
ll n, a, b, ans = LLONG_MIN;
cin >> a >> b >> n;
if (n >= b) {
cout << (a * (b - 1)) / b - a * ((b - 1) / b) << endl;
} else {
cout << (a * n) / b - a * (n / b) << endl;
}
return 0;
} | replace | 365 | 369 | 365 | 370 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define lson node << 1, st, mid
#define rson node << 1 | 1, mid + 1, ed
#define mem(a, x) memset(a, x, sizeof(a))
#define me(a) memset(a, 0, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) x & (-x)
#define up(i, x, y) for (long long i = x; i < y; i++)
#define down(i, x, y) for (long long i = x; i >= y; i--)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
typedef long long ll;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f;
const ll maxn = 1e6 + 5;
const ll N = 1e3 + 5;
const double pi = acos(-1.0);
const double eps = 1e-9;
using namespace std;
int main() {
// in;
// out;
ll a, b, n;
cin >> a >> b >> n;
ll x, y;
x = (ll)floor(n * a * 1.0 / b) - a * floor(n * 1.0 / b);
y = (ll)floor((n - 1) * a * 1.0 / b) - a * floor((n - 1) * 1.0 / b);
ll res = max(x, y);
ll cnt = -100;
if (n <= 1000000001)
cout << res << endl;
else {
for (int i = 2; i <= 1000000000; i++) {
ll z = floor((n - i) * a * 1.0 / b) - a * floor((n - i) * 1.0 / b);
cnt = max(cnt, z);
}
for (int i = 1; i <= 1000000000; i++) {
ll z = floor(i * a * 1.0 / b) - a * floor(i * 1.0 / b);
cnt = max(cnt, z);
}
cout << max(res, cnt) << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define lson node << 1, st, mid
#define rson node << 1 | 1, mid + 1, ed
#define mem(a, x) memset(a, x, sizeof(a))
#define me(a) memset(a, 0, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) x & (-x)
#define up(i, x, y) for (long long i = x; i < y; i++)
#define down(i, x, y) for (long long i = x; i >= y; i--)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
typedef long long ll;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f;
const ll maxn = 1e6 + 5;
const ll N = 1e3 + 5;
const double pi = acos(-1.0);
const double eps = 1e-9;
using namespace std;
int main() {
// in;
// out;
ll a, b, n;
cin >> a >> b >> n;
ll x;
if (b - 1 <= n)
x = (ll)floor((b - 1) * a * 1.0 / b) - a * floor((b - 1) * 1.0 / b);
else
x = (ll)floor(n * a * 1.0 / b) - a * floor(n * 1.0 / b);
cout << x << endl;
} | replace | 37 | 55 | 37 | 43 | TLE | |
p02696 | C++ | Time Limit Exceeded | // 失敗するからこそ
// そこから立ち向かって行く強さがあってそんな強さが本当の強さだと私は思うから
#ifdef DAIJOBU
#include "/home/v-o_o-v/deb.h"
#define deb(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#include <bits/stdc++.h>
#define deb(x...)
#endif
using namespace std;
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef long long lint;
typedef double ld;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
lint a, b, n;
cin >> a >> b >> n;
lint ans = 0;
for (int i = 1; i <= min(b, n); i++) {
lint now = (a * i) / b - a * (i / b);
ans = max(ans, now);
}
cout << ans;
return 0;
}
// Write Here
| // 失敗するからこそ
// そこから立ち向かって行く強さがあってそんな強さが本当の強さだと私は思うから
#ifdef DAIJOBU
#include "/home/v-o_o-v/deb.h"
#define deb(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#include <bits/stdc++.h>
#define deb(x...)
#endif
using namespace std;
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef long long lint;
typedef double ld;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
lint a, b, n;
cin >> a >> b >> n;
// lint ans = 0;
lint ans = (a * (min(b - 1, n))) / b - a * ((min(b - 1, n)) / b);
// for(int i = 1; i <= n; i++){
// lint now = (a * i) / b - a * (i / b);
// deb(now);
// ans = max(ans, now);
// }
cout << ans;
return 0;
}
// Write Here
| replace | 40 | 45 | 40 | 47 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// マクロ
// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) // 最大値を求める
#define MIN(x) *min_element(ALL(x)) // 最小値を求める
#define D()
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
#define MOD 10000007 // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first // pairの一つ目の要素
#define S second // pairの二つ目の要素
int main(void) {
long double A, B, N;
cin >> A >> B >> N;
long int max = 0, n = A;
if (A > B) {
n = B;
}
if (n >= N) {
for (long int i = 0; i <= N; ++i) {
// printf("%Lf\n",(floor(A*i/B)- A*floor(i/B)));
if (floor(A * i / B) - A * floor(i / B) > max) {
max = floor(A * i / B) - A * floor(i / B);
}
}
} else {
for (long int i = 0; i <= n; ++i) {
// printf("%Lf\n",(floor(A*i/B)- A*floor(i/B)));
if (floor(A * i / B) - A * floor(i / B) > max) {
max = floor(A * i / B) - A * floor(i / B);
}
}
for (long int i = N; i >= N - n * 1000000; --i) {
// printf("%Lf\n",(floor(A*i/B)- A*floor(i/B)));
if (floor(A * i / B) - A * floor(i / B) > max) {
max = floor(A * i / B) - A * floor(i / B);
}
}
}
/* for(long int i=0; i<=N; i = i + n){
printf("%Lf\n",(floor(A*i/B)- A*floor(i/B)));
if(floor(A*i/B)- A*floor(i/B) > max){
max = floor(A*i/B)- A*floor(i/B);
}
}
*/
printf("%ld\n", max);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// マクロ
// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) // 最大値を求める
#define MIN(x) *min_element(ALL(x)) // 最小値を求める
#define D()
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
#define MOD 10000007 // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first // pairの一つ目の要素
#define S second // pairの二つ目の要素
int main(void) {
long double A, B, N;
cin >> A >> B >> N;
long int max = 0, n = A;
if (A > B) {
n = B;
}
if (n >= N) {
for (long int i = 0; i <= N; ++i) {
// printf("%Lf\n",(floor(A*i/B)- A*floor(i/B)));
if (floor(A * i / B) - A * floor(i / B) > max) {
max = floor(A * i / B) - A * floor(i / B);
}
}
} else {
for (long int i = 0; i <= n; ++i) {
// printf("%Lf\n",(floor(A*i/B)- A*floor(i/B)));
if (floor(A * i / B) - A * floor(i / B) > max) {
max = floor(A * i / B) - A * floor(i / B);
}
}
if (B <= N) {
for (long int i = B; i >= B - n; --i) {
// printf("%Lf\n",(floor(A*i/B)- A*floor(i/B)));
if (floor(A * i / B) - A * floor(i / B) > max) {
max = floor(A * i / B) - A * floor(i / B);
}
}
}
for (long int i = N; i >= N - n; --i) {
// printf("%Lf\n",(floor(A*i/B)- A*floor(i/B)));
if (floor(A * i / B) - A * floor(i / B) > max) {
max = floor(A * i / B) - A * floor(i / B);
}
}
}
/* for(long int i=0; i<=N; i = i + n){
printf("%Lf\n",(floor(A*i/B)- A*floor(i/B)));
if(floor(A*i/B)- A*floor(i/B) > max){
max = floor(A*i/B)- A*floor(i/B);
}
}
*/
printf("%ld\n", max);
return 0;
} | replace | 49 | 50 | 49 | 58 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#include "math.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef vector<pair<ll, ll>> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i > (b); --i)
const int MOD = 1000000007;
const int MAX = 510000;
int main() {
ll a, b, n, ans = 0;
cin >> a >> b >> n;
for (ll i = b - 1; i <= n; i += b) {
ll tmp = ((a * i) / b) - a * (i / b);
ans = max(ans, tmp);
}
if (ans == 0) {
for (ll i = n; i <= n; i += b) {
ll tmp = ((a * i) / b) - a * (i / b);
ans = max(ans, tmp);
}
}
cout << ans << endl;
}
| #include "bits/stdc++.h"
#include "math.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef vector<pair<ll, ll>> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i > (b); --i)
const int MOD = 1000000007;
const int MAX = 510000;
int main() {
ll a, b, n, ans = 0;
cin >> a >> b >> n;
if (n / b == 0) {
ans = ((a * n) / b) - a * (n / b);
} else {
if (n % b == b - 1) {
ans = ((a * n) / b) - a * (n / b);
} else {
ll x = ((n / b) - 1) * b + b - 1;
ans = ((a * x) / b) - a * (x / b);
}
}
cout << ans << endl;
}
| replace | 20 | 28 | 20 | 28 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#include "math.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) int((a).size())
#define out(a) cout << (a) << endl;
const int INF = INT_MAX;
const int MAX = 510000;
const ll MOD = 1L << 32;
ll fac[MAX], finv[MAX], inv[MAX];
int main() {
ll a, b, n, ans = 0;
cin >> a >> b >> n;
ans = max(ans, ((n * a) / b) - (a * (n / b)));
if (n % b < b - 1) {
rep(i, 0, n / b) {
ll tmp = (i + 1) * b - 1;
ans = max(ans, ((tmp * a) / b) - (a * (tmp / b)));
}
} else {
rep(i, 0, n / b + 1) {
ll tmp = (i + 1) * b - 1;
ans = max(ans, ((tmp * a) / b) - (a * (tmp / b)));
}
}
cout << ans << endl;
}
| #include "bits/stdc++.h"
#include "math.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) int((a).size())
#define out(a) cout << (a) << endl;
const int INF = INT_MAX;
const int MAX = 510000;
const ll MOD = 1L << 32;
ll fac[MAX], finv[MAX], inv[MAX];
int main() {
ll a, b, n, ans = 0;
cin >> a >> b >> n;
ans = max(ans, ((n * a) / b) - (a * (n / b)));
if (n % b < b - 1) {
ll tmp = ((n / b)) * b - 1;
ans = max(ans, ((tmp * a) / b) - (a * (tmp / b)));
}
cout << ans << endl;
}
| replace | 26 | 35 | 26 | 28 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, c) for (long long i = 0; i < (long long)c; i++)
int main() {
long long a, b, n;
cin >> a >> b >> n;
long long ans = 0;
long long t;
rep(x, n + 1) {
t = floor(a * x / b) - a * floor(x / b);
ans = max(t, ans);
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, c) for (long long i = 0; i < (long long)c; i++)
int main() {
long long a, b, n;
cin >> a >> b >> n;
long long ans = 0;
long long t;
/* rep(x,n+1){
t=floor(a*x/b)-a*floor(x/b);
ans = max(t,ans);
}
*/
t = min(n, b - 1);
ans = floor(a * t / b);
cout << ans << endl;
return 0;
}
| replace | 8 | 12 | 8 | 15 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
int main() {
long long a, b, n, max;
cin >> a >> b >> n;
max = a / b - (1 / b) * a;
for (long long i = 2; i <= n; i++) {
if (((a * i) / b - (i / b) * a) > max) {
max = (a * i) / b - (i / b) * a;
}
}
cout << max << endl;
return 0;
}
| #include <iostream>
#include <vector>
using namespace std;
int main() {
long long a, b, n, max;
cin >> a >> b >> n;
if (a < b) {
if ((b - 1) < n) {
max = (a * (b - 1)) / b;
} else {
max = (a * n) / b;
}
} else {
max = (a * n) / b - a * (n / b);
long long temp = (n / b) * b - 1;
temp = (a * temp) / b - a * (temp / b);
if (temp > max) {
max = temp;
}
}
cout << max << endl;
return 0;
}
| replace | 8 | 12 | 8 | 20 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
long long int A, B, x;
cin >> A >> B >> x;
long long int ans = 0;
// ans = max(ans, (A*x)/B - A*(x/B));
// if (B<=1000000){
// for (int i=1; i<=A; i++){
// ans = max(ans, (A*x)/B - A*(x/B));
// }
// cout<<ans;
// }
// else{
for (int i = B; i <= B + x && i < 100000000000; i++) {
ans = max(ans, (A * i) / B - A * (i / B));
}
cout << ans;
// }
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
long long int A, B, N;
cin >> A >> B >> N;
long long int m = min(B - 1, N);
cout << (A * m) / B - A * (m / B);
return 0;
}
| replace | 8 | 24 | 8 | 13 | TLE | |
p02696 | C++ | Time Limit Exceeded | /*
No matter how long a night, dawn will always break.
Simular saying ... The darkest hour is just before the dawn.
*************************************** ************************8
**********************************
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define endl '\n'
#define sz(x) (int)(x).size()
#define fo(i, l, u) for (i = l; i < u; i++)
#define rfo(i, l, u) for (i = l; i >= u; i--)
#define allfo(s) for (auto it = (s).begin(); it != (s).end(); it++)
#define _init(b) memset(b, -1, sizeof(b))
#define _init0(b) memset(b, 0, sizeof(b))
#define MOD 1000000007
#define hell 998244353
#define output(x) cout << (x ? "YES" : "NO") << endl;
int gcd(int a, int b) {
if (a > b)
swap(a, b);
if (a == 0)
return b;
return gcd(b % a, a);
}
bool mod(double a, double b) { return a / b - floor(a / b); }
int f(int a, int b, int l, int r) {
if (l > r)
return 0;
if (l / b == r / b) {
return (a * r) / b - a * (r / b);
}
int mid = l + (r - l) / 2;
return max(f(a, b, l, mid), f(a, b, mid + 1, r));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int i, j, x, k, a, b;
cin >> a >> b >> x;
cout << f(a, b, 1, x);
return 0;
} | /*
No matter how long a night, dawn will always break.
Simular saying ... The darkest hour is just before the dawn.
*************************************** ************************8
**********************************
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define endl '\n'
#define sz(x) (int)(x).size()
#define fo(i, l, u) for (i = l; i < u; i++)
#define rfo(i, l, u) for (i = l; i >= u; i--)
#define allfo(s) for (auto it = (s).begin(); it != (s).end(); it++)
#define _init(b) memset(b, -1, sizeof(b))
#define _init0(b) memset(b, 0, sizeof(b))
#define MOD 1000000007
#define hell 998244353
#define output(x) cout << (x ? "YES" : "NO") << endl;
int gcd(int a, int b) {
if (a > b)
swap(a, b);
if (a == 0)
return b;
return gcd(b % a, a);
}
bool mod(double a, double b) { return a / b - floor(a / b); }
int f(int a, int b, int l, int r) {
if (l > r)
return 0;
if (l / b == r / b) {
return (a * r) / b - a * (r / b);
}
int mid = l + (r - l) / 2;
return max(f(a, b, l, mid), f(a, b, mid + 1, r));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int i, j, x, k, a, b;
cin >> a >> b >> x;
if (b < x) {
cout << (a * (b - 1)) / b << endl;
return 0;
}
cout << f(a, b, 1, x);
return 0;
} | insert | 57 | 57 | 57 | 61 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rep1(i, n) for (int i = 1; i <= (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define rrep1(i, n) for (int i = n; i >= 1; --i)
#define range(i, l, r) for (int i = l; i < (int)r; ++i)
#define rrange(i, l, r) for (int i = (int)r - 1; i >= (int)l; --i)
#define unless(a) if (!(a))
#define all(a) begin(a), end(a)
#define fst first
#define scd second
#define PB emplace_back
#define PPB pop_back
using vi = vector<int>;
using pii = pair<int, int>;
bool chmin(int &a, int b) { return a > b ? (a = b, true) : false; }
bool chmax(int &a, int b) { return a < b ? (a = b, true) : false; }
int read() {
int a;
scanf("%lld", &a);
return a;
}
constexpr int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }
int f(int a, int b) { return a / b; }
signed main() {
int A, B, N;
cin >> A >> B >> N;
int mx = 0;
range(i, 0, 1000 * A) {
if (N - i >= 0)
chmax(mx, f(A * (N - i), B) - A * f(N - i, B));
}
printf("%lld\n", mx);
}
| #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rep1(i, n) for (int i = 1; i <= (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define rrep1(i, n) for (int i = n; i >= 1; --i)
#define range(i, l, r) for (int i = l; i < (int)r; ++i)
#define rrange(i, l, r) for (int i = (int)r - 1; i >= (int)l; --i)
#define unless(a) if (!(a))
#define all(a) begin(a), end(a)
#define fst first
#define scd second
#define PB emplace_back
#define PPB pop_back
using vi = vector<int>;
using pii = pair<int, int>;
bool chmin(int &a, int b) { return a > b ? (a = b, true) : false; }
bool chmax(int &a, int b) { return a < b ? (a = b, true) : false; }
int read() {
int a;
scanf("%lld", &a);
return a;
}
constexpr int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }
int f(int a, int b) { return a / b; }
signed main() {
int A, B, N;
cin >> A >> B >> N;
int m = min(B - 1, N);
cout << A * m / B - A * (m / B) << endl;
}
| replace | 50 | 56 | 50 | 53 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tgmath.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rep(i, b, e) for (i = b; i < e; ++i)
#define repr(i, b, e) for (i = b; i >= e; --i)
#define ull unsigned long long int
#define ll long long int
#define pint pair<int, int>
#define M_DEBUG 1
#define PI 3.141592653589793
#define E '\n'
#define printt(x) cout << (x) << endl;
#define printa(x, n) \
for (int i = 0; i < n; i++) { \
cout << (x[i]) << "\n"; \
}
#define INF (1e18)
const ll MAX = 1000000000;
const ll MOD = 1e9 + 7;
ll a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll aabs(ll a) {
if (a < 0)
return -a;
return a;
}
// fast power
ll calc(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll d = calc(a, b / 2);
return (d * d) % MOD;
} else
return (a * calc(a, b - 1)) % MOD;
}
// fast combination
ll comb(ll n, ll a) {
ll b = 1, c = 1;
for (ll i = 1; i <= a; ++i) {
b = b * i % MOD;
c = c * (n - i + 1) % MOD;
}
return c * calc(b, MOD - 2) % MOD;
}
ll comb2(ll a) {
ll b = a / 2;
if (a % 2) {
b = (a - 1) / 2;
return (b * a);
}
return (b * (a - 1));
}
int dfs(int x, int y, int z) {
if (x == a && y == b && z == c)
return 1;
int ans = 0;
if (x != a)
ans += dfs(x + 1, y, z);
if (x >= y + 1 /* && y != b*/)
ans += dfs(x, y + 1, z);
if (y >= z + 1 /* && z != c*/)
ans += dfs(x, y, z + 1);
return ans;
}
uint64_t gcd_impl(uint64_t n, uint64_t m) {
for (int i = 0; i < 10; ++i) {
uint64_t t = n - m;
bool q = m > t;
n = q ? m : t;
m = q ? t : m;
if (m == 0) {
return n;
}
}
return gcd_impl(m, n % m);
}
uint64_t gcd(uint64_t n, uint64_t m) {
return n > m ? gcd_impl(n, m) : gcd_impl(m, n);
}
const int INF_ = 1e+7;
// 0-indexed
struct Node {
std::vector<int> nodes;
std::vector<int> edge_costs;
int num = 0;
int cost = INF_;
int link_out = 0;
int link_in = 0;
int pcost = 0;
};
class Graph {
public:
Graph(int n, bool directed);
~Graph();
void setGraph(int n, int node, int edge_cost);
void Dijkstra(int start);
void print();
std::vector<int> topologicalSort();
int longestPath();
int ans = 0;
private:
std::vector<Node> m_graph;
int m_size;
bool m_directed = false;
};
Graph::Graph(int n, bool directed) : m_size(n), m_directed(directed) {
m_graph = std::vector<Node>(m_size);
}
Graph::~Graph() {}
void Graph::setGraph(int n, int node, int edge_cost) {
--n;
--node;
++m_graph[node].link_in;
m_graph[n].nodes.emplace_back(node);
m_graph[n].edge_costs.emplace_back(edge_cost);
++m_graph[n].link_out;
}
void Graph::Dijkstra(int start) {
m_graph[start].cost = 0;
std::queue<int> q;
q.push(start);
while (!q.empty()) {
int qi = q.front();
q.pop();
for (int i = 0; i < m_graph[qi].nodes.size(); ++i) {
int qqi = m_graph[qi].nodes[i],
new_cost = m_graph[qi].edge_costs[i] + m_graph[qi].cost;
if (m_graph[qqi].cost > new_cost) {
m_graph[qqi].cost = new_cost;
q.push(qqi);
}
}
}
}
int Graph::longestPath() {
int mx = 0;
for (auto &&g : m_graph) {
if (g.pcost > mx)
mx = g.pcost;
}
return mx;
}
void Graph::print() {
for (int i = 0; i < m_size; ++i) {
std::cout << i + 1 << " : ";
for (int j = 0; j < m_graph[i].nodes.size(); ++j)
std::cout << m_graph[i].nodes[j] + 1 << ", ";
std::cout << std::endl;
}
for (int i = 0; i < m_size; ++i)
std::cout << i + 1 << " : " << m_graph[i].link_in << ", "
<< m_graph[i].link_out << std::endl;
}
std::vector<int> Graph::topologicalSort() {
std::queue<int> q;
for (int i = 0; i < m_size; ++i)
if (m_graph[i].link_in == 0) {
q.push(i);
m_graph[i].pcost = 1;
}
std::vector<int> topo;
std::vector<int> tmp(m_size);
for (int i = 0; i < m_size; ++i)
tmp[i] = m_graph[i].link_in;
while (!q.empty()) {
int i = q.front();
q.pop();
topo.emplace_back(i);
for (auto &&j : m_graph[i].nodes) {
m_graph[j].link_in--;
if (m_graph[j].link_in == 0) {
q.push(j);
m_graph[j].pcost = m_graph[i].pcost + 1;
}
}
}
for (int i = 0; i < m_size; ++i)
m_graph[i].link_in = tmp[i];
return topo;
}
struct HashPair {
// 注意 constがいる
template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
// first分をハッシュ化する
auto hash1 = hash<T1>{}(p.first);
// second分をハッシュ化する
auto hash2 = hash<T2>{}(p.second);
// 重複しないようにハッシュ処理
size_t seed = 0;
seed ^= hash1 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
seed ^= hash2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
};
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
/*
cin >> n >> m >> q;
vector<vector<ll> > vv(q, vector<ll>(4, 0));
rep (i, 0, q) {
cin >> vv[i][0] >> vv[i][1] >> vv[i][2] >> vv[i][3];
}
*/
cin >> a >> b >> n;
for (x = 0; x <= n; ++x) {
long long c = floor((double)a / (double)b * x);
// cout << c << " ";
long long d = floor((double)x / (double)b) * a;
// cout << d << endl;
e = c - d;
if (e < f)
break;
f = e;
}
cout << f << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tgmath.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rep(i, b, e) for (i = b; i < e; ++i)
#define repr(i, b, e) for (i = b; i >= e; --i)
#define ull unsigned long long int
#define ll long long int
#define pint pair<int, int>
#define M_DEBUG 1
#define PI 3.141592653589793
#define E '\n'
#define printt(x) cout << (x) << endl;
#define printa(x, n) \
for (int i = 0; i < n; i++) { \
cout << (x[i]) << "\n"; \
}
#define INF (1e18)
const ll MAX = 1000000000;
const ll MOD = 1e9 + 7;
ll a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll aabs(ll a) {
if (a < 0)
return -a;
return a;
}
// fast power
ll calc(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll d = calc(a, b / 2);
return (d * d) % MOD;
} else
return (a * calc(a, b - 1)) % MOD;
}
// fast combination
ll comb(ll n, ll a) {
ll b = 1, c = 1;
for (ll i = 1; i <= a; ++i) {
b = b * i % MOD;
c = c * (n - i + 1) % MOD;
}
return c * calc(b, MOD - 2) % MOD;
}
ll comb2(ll a) {
ll b = a / 2;
if (a % 2) {
b = (a - 1) / 2;
return (b * a);
}
return (b * (a - 1));
}
int dfs(int x, int y, int z) {
if (x == a && y == b && z == c)
return 1;
int ans = 0;
if (x != a)
ans += dfs(x + 1, y, z);
if (x >= y + 1 /* && y != b*/)
ans += dfs(x, y + 1, z);
if (y >= z + 1 /* && z != c*/)
ans += dfs(x, y, z + 1);
return ans;
}
uint64_t gcd_impl(uint64_t n, uint64_t m) {
for (int i = 0; i < 10; ++i) {
uint64_t t = n - m;
bool q = m > t;
n = q ? m : t;
m = q ? t : m;
if (m == 0) {
return n;
}
}
return gcd_impl(m, n % m);
}
uint64_t gcd(uint64_t n, uint64_t m) {
return n > m ? gcd_impl(n, m) : gcd_impl(m, n);
}
const int INF_ = 1e+7;
// 0-indexed
struct Node {
std::vector<int> nodes;
std::vector<int> edge_costs;
int num = 0;
int cost = INF_;
int link_out = 0;
int link_in = 0;
int pcost = 0;
};
class Graph {
public:
Graph(int n, bool directed);
~Graph();
void setGraph(int n, int node, int edge_cost);
void Dijkstra(int start);
void print();
std::vector<int> topologicalSort();
int longestPath();
int ans = 0;
private:
std::vector<Node> m_graph;
int m_size;
bool m_directed = false;
};
Graph::Graph(int n, bool directed) : m_size(n), m_directed(directed) {
m_graph = std::vector<Node>(m_size);
}
Graph::~Graph() {}
void Graph::setGraph(int n, int node, int edge_cost) {
--n;
--node;
++m_graph[node].link_in;
m_graph[n].nodes.emplace_back(node);
m_graph[n].edge_costs.emplace_back(edge_cost);
++m_graph[n].link_out;
}
void Graph::Dijkstra(int start) {
m_graph[start].cost = 0;
std::queue<int> q;
q.push(start);
while (!q.empty()) {
int qi = q.front();
q.pop();
for (int i = 0; i < m_graph[qi].nodes.size(); ++i) {
int qqi = m_graph[qi].nodes[i],
new_cost = m_graph[qi].edge_costs[i] + m_graph[qi].cost;
if (m_graph[qqi].cost > new_cost) {
m_graph[qqi].cost = new_cost;
q.push(qqi);
}
}
}
}
int Graph::longestPath() {
int mx = 0;
for (auto &&g : m_graph) {
if (g.pcost > mx)
mx = g.pcost;
}
return mx;
}
void Graph::print() {
for (int i = 0; i < m_size; ++i) {
std::cout << i + 1 << " : ";
for (int j = 0; j < m_graph[i].nodes.size(); ++j)
std::cout << m_graph[i].nodes[j] + 1 << ", ";
std::cout << std::endl;
}
for (int i = 0; i < m_size; ++i)
std::cout << i + 1 << " : " << m_graph[i].link_in << ", "
<< m_graph[i].link_out << std::endl;
}
std::vector<int> Graph::topologicalSort() {
std::queue<int> q;
for (int i = 0; i < m_size; ++i)
if (m_graph[i].link_in == 0) {
q.push(i);
m_graph[i].pcost = 1;
}
std::vector<int> topo;
std::vector<int> tmp(m_size);
for (int i = 0; i < m_size; ++i)
tmp[i] = m_graph[i].link_in;
while (!q.empty()) {
int i = q.front();
q.pop();
topo.emplace_back(i);
for (auto &&j : m_graph[i].nodes) {
m_graph[j].link_in--;
if (m_graph[j].link_in == 0) {
q.push(j);
m_graph[j].pcost = m_graph[i].pcost + 1;
}
}
}
for (int i = 0; i < m_size; ++i)
m_graph[i].link_in = tmp[i];
return topo;
}
struct HashPair {
// 注意 constがいる
template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
// first分をハッシュ化する
auto hash1 = hash<T1>{}(p.first);
// second分をハッシュ化する
auto hash2 = hash<T2>{}(p.second);
// 重複しないようにハッシュ処理
size_t seed = 0;
seed ^= hash1 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
seed ^= hash2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
};
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
/*
cin >> n >> m >> q;
vector<vector<ll> > vv(q, vector<ll>(4, 0));
rep (i, 0, q) {
cin >> vv[i][0] >> vv[i][1] >> vv[i][2] >> vv[i][3];
}
*/
cin >> a >> b >> n;
if (n >= b)
x = b - 1;
else
x = n;
c = floor((double)a / (double)b * x);
d = floor((double)x / (double)b) * a;
cout << c - d << endl;
return 0;
}
| replace | 287 | 299 | 287 | 296 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
int main() {
ll a, b, n, ans = 0;
cin >> a >> b >> n;
ll c = GCD(a, b);
ll d = n * c / (GCD(n, c));
rep(i, max((ll)0, d - a), min(n, d + a) + 1) ans =
max(ans, (a * i) / b - a * (i / b));
rep(i, 0, min(n, b)) ans = max(ans, (a * i) / b - a * (i / b));
ll aa = min((ll)n, b - 1);
ll bb = max((ll)0, b - 10000000);
irep(i, aa, bb) ans = max(ans, (a * i) / b - a * (i / b));
cout << ans;
}
| #include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
int main() {
ll a, b, n, ans = 0;
cin >> a >> b >> n;
ll c = GCD(a, b);
ll d = n * c / (GCD(n, c));
rep(i, max((ll)0, d - a), min(n, d + a) + 1) ans =
max(ans, (a * i) / b - a * (i / b));
// rep(i,0,min(n,b))ans=max(ans,(a*i)/b-a*(i/b));
ll aa = min((ll)n, b - 1);
ll bb = max((ll)0, b - 10000000);
irep(i, aa, bb) ans = max(ans, (a * i) / b - a * (i / b));
cout << ans;
}
| replace | 35 | 36 | 35 | 36 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define SUM(x) accumulate(ALL(x), 0)
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define MAXR 100000
ll solve(ll &a, ll &b, ll &x) {
ll res = (int)(a * x / b) - a * (int)(x / b);
return res;
}
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll x = b / a + 1;
ll ans = 0;
for (ll i = x; i <= min(b, n); i++) {
ans = max(ans, solve(a, b, i));
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define SUM(x) accumulate(ALL(x), 0)
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define MAXR 100000
ll solve(ll &a, ll &b, ll &x) {
ll res = (int)(a * x / b) - a * (int)(x / b);
return res;
}
int main() {
ll a, b, n;
cin >> a >> b >> n;
// ll x = b / a + 1;
// ll y = min(n, b);
// cout << max(solve(a, b, x), solve(a, b, y)) << endl;
ll a_ = 0;
if (b <= n)
a_ = b - 1;
else
a_ = n;
cout << solve(a, b, a_) << endl;
// ll ans = 0;
// for (ll i = x; i <= min(b, n); i ++) {
// ans = max(ans, solve(a, b, i));
// }
// cout << ans << endl;
// ll x = n;
// if (a < b) {
// if (b <= x) {
// ll u = b - 1;
// cout << solve(a, b, u) << endl;
// } else cout << solve(a, b, n) << endl;
// } else {
// if (b <= x) {
// ll y = b - 1;
// if (solve(a, b, x) < solve(a, b, y)) cout << solve(a, b, y) << endl;
// else cout << solve(a, b, x) << endl;
// }
// else cout << solve(a, b, x) << endl;
// }
return 0;
}
| replace | 29 | 35 | 29 | 60 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll a, b, n;
int main() {
cin >> a >> b >> n;
ll x;
ll ans;
if (n < b) {
ans = (a * n) / b - a * (n / b);
cout << ans;
return 0;
} else if (n == b) {
x = n - 1;
ans = (a * n) / b - a * (n / b);
ll ans1 = (a * x) / b - a * (x / b);
ans = max(ans, ans1);
cout << ans;
} else {
ll maxn = (a * n) / b - a * (n / b);
ll tmp;
ll num = n / b;
for (ll i = num; i >= 1; i--) {
ll mul = i * b - 1;
tmp = (a * mul) / b - a * (mul / b);
maxn = max(maxn, tmp);
}
ans = maxn;
cout << ans;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll a, b, n;
int main() {
cin >> a >> b >> n;
ll x;
ll ans;
if (n < b) {
ans = (a * n) / b - a * (n / b);
cout << ans;
return 0;
} else if (n == b) {
x = n - 1;
ans = (a * n) / b - a * (n / b);
ll ans1 = (a * x) / b - a * (x / b);
ans = max(ans, ans1);
cout << ans;
} else {
ll maxn = (a * n) / b - a * (n / b);
ll tmp;
ll num = n / b;
ll mod = 1e7;
if (num > mod) {
for (ll i = 1; i <= mod; i++) {
ll mul = i * b - 1;
tmp = (a * mul) / b - a * (mul / b);
maxn = max(maxn, tmp);
}
} else {
for (ll i = num; i >= 1; i--) {
ll mul = i * b - 1;
tmp = (a * mul) / b - a * (mul / b);
maxn = max(maxn, tmp);
}
}
ans = maxn;
cout << ans;
}
return 0;
} | replace | 23 | 27 | 23 | 36 | TLE | |
p02696 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost \
ios::sync_with_stdio(false); \
cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;
int main() {
boost;
ll a, b, n;
cin >> a >> b >> n;
ll ans = 0;
if (b > n)
ans = (a * n) / b;
else {
ll x = n / (b - 1);
x *= (b - 1);
ans = (a * x) / b - a * (x / b);
}
cout << ans;
}
| #include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost \
ios::sync_with_stdio(false); \
cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;
int main() {
boost;
ll a, b, n;
cin >> a >> b >> n;
ll ans = 0;
if (b == 1)
ans = 0;
else if (b > n)
ans = (a * n) / b;
else {
ll x = n / (b - 1);
x *= (b - 1);
ans = (a * x) / b - a * (x / b);
}
cout << ans;
}
| replace | 30 | 31 | 30 | 33 | 0 | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll k = min(b, n);
rep(i, k + 1) { ans = max(ans, (a * i) / b - a * (i / b)); }
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll k = min(b - 1, n);
ans = max(ans, (a * k) / b - a * (k / b));
cout << ans << endl;
return 0;
} | replace | 9 | 11 | 9 | 11 | TLE | |
p02696 | C++ | Runtime Error | #include <bits/stdc++.h>
#define lli long long int
#define uli unsigned long long int
#define rep(i, m, n) for (lli i = m; i < (n); i++)
#define repe(i, m, n) for (lli i = m; i <= (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((lli)(x).size())
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000 // 10^12
#define MOD 10000007 // 10^9+7
#define SORT(n) sort(n.begin(), n.end())
#define SORTR(n) sort(n.begin(), n.end(), greater<int>())
#define REV(n) reverse(n.begin(), n.end())
#define Vec(K, L, N, S) vector<L> K(N, S)
#define rt sqrt
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vlli = vector<lli>;
using vs = vector<string>;
using ll = long long;
// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
// os << '{';
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : "");
}
// os << '}';
return os;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
lli a, b, c, h, k, n, w, x, ans = 0, count = 0;
cin >> a >> b >> n;
set<lli> g;
vector<lli> f(n + 1);
cout << a * min(n, b - 1) / b << endl;
} | #include <bits/stdc++.h>
#define lli long long int
#define uli unsigned long long int
#define rep(i, m, n) for (lli i = m; i < (n); i++)
#define repe(i, m, n) for (lli i = m; i <= (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((lli)(x).size())
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000 // 10^12
#define MOD 10000007 // 10^9+7
#define SORT(n) sort(n.begin(), n.end())
#define SORTR(n) sort(n.begin(), n.end(), greater<int>())
#define REV(n) reverse(n.begin(), n.end())
#define Vec(K, L, N, S) vector<L> K(N, S)
#define rt sqrt
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vlli = vector<lli>;
using vs = vector<string>;
using ll = long long;
// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
// os << '{';
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : "");
}
// os << '}';
return os;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
lli a, b, c, h, k, n, w, x, ans = 0, count = 0;
cin >> a >> b >> n;
cout << a * min(n, b - 1) / b << endl;
} | delete | 43 | 45 | 43 | 43 | 0 | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define INF 100000000000
#define MOD 100000007 // 10^9+7
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll n, a, b, i, j;
cin >> a >> b >> n;
ll ans = 0LL;
ll tmp;
for (i = n; i >= n / b; i--) {
tmp = floor((a * i) / b) - a * (floor(i / b));
ans = max(ans, tmp);
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define INF 100000000000
#define MOD 100000007 // 10^9+7
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll n, a, b, i, j;
cin >> a >> b >> n;
ll x = n;
if (n >= b - 1)
x = b - 1;
ll ans = a * x / b - a * (x / b);
cout << ans << endl;
return 0;
}
| replace | 27 | 34 | 27 | 31 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long int ans = 0;
long int a, b, n, x;
cin >> a >> b >> n;
if (n < b) {
x = n;
} else if (n == b) {
x = b - 1;
} else {
x = b;
while (n > (x + b))
x += b;
x--;
}
ans = ((a * x) / b) - a * (x / b);
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long int ans = 0;
long int a, b, n, x;
cin >> a >> b >> n;
if (n < b) {
x = n;
} else if (n == b) {
x = b - 1;
} else {
x = (n / b) * b - 1;
}
ans = ((a * x) / b) - a * (x / b);
cout << ans << endl;
}
| replace | 13 | 17 | 13 | 14 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
template <class T> struct rge {
T b, e;
};
template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
template <class T> debug &operator<<(T x) {
cerr << boolalpha << x;
return *this;
}
template <class B, class C> debug &operator<<(pair<B, C> x) {
return *this << "(" << x.first << ", " << x.second << ")";
}
template <class T> debug &operator<<(rge<T> x) {
*this << "[";
for (auto it = x.b; it != x.e; it++) {
*this << ", " + 2 * (it == x.b) << *it;
}
return *this << "]";
}
template <class T> debug &operator<<(vector<T> x) {
return *this << range(x.begin(), x.end());
}
#else
template <class T> debug &operator<<(const T &) { return *this; }
#endif
};
#define nav(...) << "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
using ll = long long;
ll a, b, n;
ll f(ll x) { return (a * x) / b - a * (x / b); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> a >> b >> n;
ll ans = 0;
for (ll x = 0; x <= n;) {
ll lo = x, hi = n;
while (lo < hi) {
ll mid = lo + (hi - lo + 1) / 2;
if ((a * x) / b == (a * mid) / b) {
lo = mid;
} else {
hi = mid - 1;
}
}
//$ debug() nav(x);
ans = max(ans, f(x));
x = lo + 1;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
template <class T> struct rge {
T b, e;
};
template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
template <class T> debug &operator<<(T x) {
cerr << boolalpha << x;
return *this;
}
template <class B, class C> debug &operator<<(pair<B, C> x) {
return *this << "(" << x.first << ", " << x.second << ")";
}
template <class T> debug &operator<<(rge<T> x) {
*this << "[";
for (auto it = x.b; it != x.e; it++) {
*this << ", " + 2 * (it == x.b) << *it;
}
return *this << "]";
}
template <class T> debug &operator<<(vector<T> x) {
return *this << range(x.begin(), x.end());
}
#else
template <class T> debug &operator<<(const T &) { return *this; }
#endif
};
#define nav(...) << "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
using ll = long long;
ll a, b, n;
ll f(ll x) { return (a * x) / b - a * (x / b); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> a >> b >> n;
ll x = b * (n / b) - 1;
cout << max(f(n), f(x)) << endl;
}
| replace | 41 | 58 | 41 | 43 | TLE | |
p02696 | C++ | Time Limit Exceeded | /*
GOOD is not Enough
You've got to be GREAT.
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #include<boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define mod 1000000007
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define f(i, n) for (int i = 0; i < n; i++)
#define fp(i, k, n) for (int i = k; i <= n; i++)
#define fr(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define pii pair<int, int>
#define dbg(x) cout << (#x) << " is " << (x) << '\n'
#define F first
#define S second
#define all(a) a.begin(), a.end()
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
void solve() {
int a, b, n;
cin >> a >> b >> n;
int ans = 0;
int x = 0;
fr(i, n, 0) {
int res = ((a * i) / b) - (a * (i / b));
ans = max(res, ans);
x++;
if (x == 100000000)
break;
}
x = 0;
fp(i, 0, n) {
int res = ((a * i) / b) - (a * (i / b));
ans = max(res, ans);
x++;
if (x == 100000000)
break;
}
cout << ans << '\n';
return;
}
signed main() {
FAST int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
} | /*
GOOD is not Enough
You've got to be GREAT.
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #include<boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define mod 1000000007
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define f(i, n) for (int i = 0; i < n; i++)
#define fp(i, k, n) for (int i = k; i <= n; i++)
#define fr(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define pii pair<int, int>
#define dbg(x) cout << (#x) << " is " << (x) << '\n'
#define F first
#define S second
#define all(a) a.begin(), a.end()
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
void solve() {
int a, b, n;
cin >> a >> b >> n;
int ans = 0;
int x = 0;
x = min(n, b - 1);
cout << (a * x) / b << '\n';
return;
}
signed main() {
FAST int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
} | replace | 31 | 47 | 31 | 33 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll A, B, N;
cin >> A >> B >> N;
ll max = 0;
for (ll i = N; i >= 0; i--) {
ll tmp = floor((A * i) / B) - A * floor(i / B);
if (tmp > max) {
max = tmp;
}
}
cout << max << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll A, B, N;
cin >> A >> B >> N;
ll max = 0;
/*
for(ll i = N; i > 0; i--){
if()
ll tmp = floor((A * i)/B) - A*floor(i/B);
if(tmp > max){
max = tmp;
}
}
*/
ll tmp = floor((A * N) / B) - A * floor(N / B);
if (tmp > max) {
max = tmp;
}
if (N >= (B - 1)) {
ll tmp2 = 0;
tmp2 = floor((A * (B - 1)) / B) - A * floor((B - 1) / B);
if (tmp2 > max) {
max = tmp2;
}
}
cout << max << endl;
return 0;
} | replace | 12 | 16 | 12 | 32 | TLE | |
p02696 | C++ | Time Limit Exceeded | // g++ -std=gnu++14 a.cpp
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;
int main() {
ll A, B, N;
cin >> A >> B >> N;
ll ans = 0;
if (B > N) {
cout << (A * N) / B << endl;
return 0;
} else {
/*
for(int i = B-1;i <= N;i+=B){
ll tmp;
tmp = (A*i)/B - A*(i/B);
ans = max(ans,tmp);
}*/
ll i = B - 1;
while (i <= N) {
ll tmp = 0;
tmp = (A * i) / B - A * (i / B);
ans = max(ans, tmp);
i += B;
}
}
cout << ans << endl;
}
| // g++ -std=gnu++14 a.cpp
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;
int main() {
ll A, B, N;
cin >> A >> B >> N;
ll ans = 0;
if (B > N) {
cout << (A * N) / B << endl;
return 0;
} else {
/*
for(int i = B-1;i <= N;i+=B){
ll tmp;
tmp = (A*i)/B - A*(i/B);
ans = max(ans,tmp);
}*/
ll i = B - 1;
// while(i <= N){
ll tmp = 0;
tmp = (A * i) / B - A * (i / B);
ans = max(ans, tmp);
// i += B;
//}
}
cout << ans << endl;
}
| replace | 43 | 49 | 43 | 49 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll a, b, n;
cin >> a >> b >> n;
// 全探索
ll ans = 0;
for (ll x = 0; x <= n; ++x) {
ll fst = a * x / b;
ll scd = x / b;
scd *= a;
ll flr = fst - scd;
ans = max(ans, flr);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll x = min(b - 1, n);
int ans = int(a * x / b) - a * int(x / b);
// // 全探索
// ll ans = 0;
// for(ll x = 0; x <= n; ++x) {
// ll fst = a * x / b;
// ll scd = x / b;
// scd *= a;
// ll flr = fst - scd;
// ans = max(ans, flr);
// }
cout << ans << endl;
return 0;
} | replace | 10 | 19 | 10 | 21 | TLE | |
p02696 | C++ | Time Limit Exceeded | /*
--------DO NOT COPY I REQUEST YOU PLEASE--------
AUTHOR : Chandan Agrawal
College : Poornima College of Engg. jaipur, Raj
Mail : chandanagrawal23@gmail.com
██████╗ ██╗ ██╗ █████╗ ███╗ ██╗ ██████╗ █████╗ ███╗ ██╗
██╔════╝ ██║ ██║ ██╔══██╗ ████╗ ██║ ██╔══██╗ ██╔══██╗ ████╗ ██║
██║ ███████║ ███████║ ██╔██╗ ██║ ██║ ██║ ███████║ ██╔██╗ ██║
██║ ██╔══██║ ██╔══██║ ██║╚██╗██║ ██║ ██║ ██╔══██║ ██║╚██╗██║
╚██████╗ ██║ ██║ ██║ ██║ ██║ ╚████║ ██████╔╝ ██║ ██║ ██║ ╚████║
╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝
*/
#include <bits/stdc++.h>
using namespace std;
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#include <cstdio>
#define MAX 100050
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <unordered_map>
#define ll long long
#define ld long double
#define lli long long int
#define pb emplace_back
#define INF 1000000000
#define mod 1000000007
#define MOD 1000000007
// #define mp make_pair
#define loop(i, n) for (lli i = 0; i < n; i++)
#define loopitr(xt, vec) for (auto xt : vec)
#define FOR(i, a, b) for (lli i = a; i < b; i += 1)
#define loop_rev(i, n) for (lli i = n - 1; i >= 0; i--)
#define FOR_REV(i, a, b) for (lli i = a; i >= b; i--)
#define all(v) v.begin(), v.end()
// #define all(a) begin(a),end(a)
#define sz(x) int(x.size())
#define pii pair<int, int>
#define F first
#define S second
#define mii map<lli, lli>
#define vi vector<lli>
#define seti set<lli>
#define itr ::iterator it
#define WL(t) while (t--)
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) (a / gcd(a, b)) * b
#define abs(x) ((x < 0) ? -(x) : x)
#define print(x) printf("%lli\n", x);
#define print2(x, y) printf("%lli %lli\n", x, y);
#define print3(x, y, z) printf("%lli %lli %lli\n", x, y, z);
#define scan(x) scanf("%lld", &x);
#define scan2(x, y) scanf("%lld %lld", &x, &y);
#define scan3(x, y, z) scanf("%lld %lld %lld", &x, &y, &z);
#define scanarr(a, n) \
for (lli i = 0; i < n; i++) \
cin >> a[i];
#define scanvector(a, n) \
for (lli i = 0; i < n; i++) { \
lli x; \
cin >> x; \
a.push_back(x); \
}
#define printarr(a, n) \
for (lli i = 0; i < n; i++) \
printf("%lli ", a[i]); \
printf("\n");
#define printvector(vec) \
for (auto xt : vec) \
cout << xt << " "; \
cout << "\n";
#define printset(st) \
for (auto xt : st) \
cout << xt << " "; \
cout << "\n";
#define FD(N) fixed << setprecision(N)
#define endl '\n'
#define deb(x) cout << #x << " " << x << endl;
/*
ifstream cinn("i3.txt");
ofstream coutt("o3.txt");
*/
lli find_mod(string s) {
lli i, x = 0;
for (i = 0; i < s.length(); i++)
x = (x * 10 + s[i] - '0') % MOD;
return x;
}
bool prime[MAX + 1];
void sieve1() { // prime or not
for (int i = 0; i <= MAX; i++)
prime[i] = 1;
prime[0] = prime[1] = 0;
for (lli i = 4; i <= MAX; i += 2)
prime[i] = 0;
for (int p = 3; p * p <= MAX; p += 2) {
if (prime[p] == 1)
for (int i = p * 2; i <= MAX; i += p) {
prime[i] = 0;
}
}
}
int min_prime_div_of_n[MAX + 1];
void sieve2() { // smallest prime factor which divide a no.
memset(min_prime_div_of_n, 0, sizeof(min_prime_div_of_n));
min_prime_div_of_n[0] = min_prime_div_of_n[1] = 1;
for (int i = 2; i * i <= MAX; i++) {
if (min_prime_div_of_n[i] == 0) {
for (int j = i * 2; j < MAX; j += i) {
if (min_prime_div_of_n[j] == 0) {
min_prime_div_of_n[j] = i;
// cout<<min_prime_div_of_n[i]<<" ";}
}
}
}
}
for (int i = 2; i <= MAX; i++)
if (min_prime_div_of_n[i] == 0)
min_prime_div_of_n[i] = i;
// If the number is prime then it's // smallest prime factor is the number //
// itself
}
int max_prime_div_of_n[MAX + 1];
void sieve3() { // maximum prime factor which divide a no.
sieve1();
memset(max_prime_div_of_n, 0,
sizeof(max_prime_div_of_n)); // 0 stands for is_prime
max_prime_div_of_n[0] = max_prime_div_of_n[1] = 1;
for (int i = 2; i * i <= MAX; i++) {
if (max_prime_div_of_n[i] == 0) {
for (int j = i * 2; j < MAX; j += i) {
if (max_prime_div_of_n[j] == 0 || ((j % i) == 0 && prime[i] == 1)) {
max_prime_div_of_n[j] = i;
}
}
}
}
for (int i = 2; i <= 100; i++)
// If the number is prime then it's largest prime factor is the number
// itself
if (max_prime_div_of_n[i] == 0)
max_prime_div_of_n[i] = i;
// cout<<max_prime_div_of_n[i]<<" ";
}
lli sum_prime_fac[1000001];
void sieve4() { // prime or not
sum_prime_fac[1000001] = {0};
for (lli i = 2; i <= 1000000; i++) {
if (!sum_prime_fac[i]) {
for (lli j = i; j <= 1000000; j += i)
sum_prime_fac[j] += i;
}
}
}
bool is_even(lli n) { return (!(n & 2)); }
bool is_odd(lli n) { return (n & 2); }
// memset(array_name , initialized value , sizeof(data_type_of_array))
bool isPrime(lli n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n == 5)
return true;
if ((n % 2 == 0) || (n % 3 == 0) || (n % 5 == 0))
return false;
for (lli i = 5; i * i <= n; i += 6)
if ((n % i == 0) || (n % (i + 2) == 0))
return false;
return true;
}
lli reversenum(lli x) {
lli num = 0;
while (x > 0) {
num = num * 10 + x % 10;
x = x / 10;
}
return num;
}
bool ispalindrome(string s) {
string q = s;
reverse(q.begin(), q.end());
return (s == q);
}
bool isvowel(char x) {
return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u');
}
lli mceil(lli a, lli b) {
if (a % b == 0)
return (a / b);
else
return (a / b + 1);
}
lli mfloor(lli a, lli b) { return (a / b); }
lli sumarr(lli a[], lli n) {
lli sum = 0;
loop(i, n) sum += a[i];
return sum;
}
ll modmullong(ll a, ll b) {
ll res = 0;
a %= mod;
while (b) {
if (b & 1)
res = (res + a) % mod;
a = (2 * a) % mod;
b >>= 1;
}
return res;
}
ll modmul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll modmul2(ll a, ll b) {
return (((a % (mod - 1)) * (b % (mod - 1))) % (mod - 1));
}
ll modadd(ll a, ll b) { return ((a % mod) + (b % mod) + mod) % mod; }
ll modsub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
lli fastexpo(lli a, lli b) {
a = a % mod;
lli ans = 1;
while (b) {
if (b & 1)
ans = (ans * 1ll * a) % mod;
a = (a * 1ll * a) % mod;
b = b / 2;
}
return ans;
}
lli phi[MAX];
void euler_phi() { // euler totient function
loop(i, MAX - 1) phi[i + 1] = i + 1;
for (lli p = 2; p < MAX; p++) {
if (phi[p] == p) {
phi[p] = p - 1;
for (lli i = 2 * p; i < MAX; i += p)
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
lli myphi(lli n) {
lli result = n;
for (lli p = 2; p * p <= n; ++p) {
if (n % p == 0) {
while (n % p == 0)
n /= p;
result -= result / p;
}
}
if (n > 1)
result -= result / n;
return result;
}
void prime_factorise(lli n, map<lli, lli> &mp) {
while (n % 2 == 0) {
mp[2]++;
n /= 2;
}
for (lli i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
mp[i]++;
n /= i;
}
}
if (n > 1)
mp[n]++;
}
lli cntbits(lli n) {
lli cnt = 0;
while (n) {
cnt++;
n /= 2;
}
return cnt;
}
lli fact[100001];
void facto() {
fact[0] = 1;
for (lli i = 1; i <= 100000; i++)
fact[i] = modmul(fact[i - 1], i);
}
lli nCr(lli n, lli r) {
if (n < r)
return 0;
else {
r = min(r, n - r);
lli a = 1, b = 1;
for (lli i = 0; i < r; i++) {
a = (a * (n - i)) % mod;
b = (b * (i + 1)) % mod;
}
a = modmul(a, fastexpo(b, mod - 2));
return a;
}
}
bool ispower2(lli n) { return (n and (n & (n - 1)) == 0); }
// chandan1,2
void chandan1() { return; }
void chandan2() {
loop(i, 10) { lli x = 1; }
return (chandan1());
}
lli myfunc(lli x, lli y, lli z) { return ((x & z) * (y & z)); }
int main() {
fastIO lli t = 1;
// cin>>t;
// chandan2();
while (t--) {
lli a, b, n;
cin >> a >> b >> n;
lli maxi = -1;
for (lli i = max(0LL, n / 2 - 100000000); i <= min(n, n / 2 + 100000000);
i++) {
lli val = mfloor(a * i, b) - a * mfloor(i, b);
maxi = max(maxi, val);
}
cout << maxi;
}
return 0;
}
| /*
--------DO NOT COPY I REQUEST YOU PLEASE--------
AUTHOR : Chandan Agrawal
College : Poornima College of Engg. jaipur, Raj
Mail : chandanagrawal23@gmail.com
██████╗ ██╗ ██╗ █████╗ ███╗ ██╗ ██████╗ █████╗ ███╗ ██╗
██╔════╝ ██║ ██║ ██╔══██╗ ████╗ ██║ ██╔══██╗ ██╔══██╗ ████╗ ██║
██║ ███████║ ███████║ ██╔██╗ ██║ ██║ ██║ ███████║ ██╔██╗ ██║
██║ ██╔══██║ ██╔══██║ ██║╚██╗██║ ██║ ██║ ██╔══██║ ██║╚██╗██║
╚██████╗ ██║ ██║ ██║ ██║ ██║ ╚████║ ██████╔╝ ██║ ██║ ██║ ╚████║
╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝
*/
#include <bits/stdc++.h>
using namespace std;
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#include <cstdio>
#define MAX 100050
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <unordered_map>
#define ll long long
#define ld long double
#define lli long long int
#define pb emplace_back
#define INF 1000000000
#define mod 1000000007
#define MOD 1000000007
// #define mp make_pair
#define loop(i, n) for (lli i = 0; i < n; i++)
#define loopitr(xt, vec) for (auto xt : vec)
#define FOR(i, a, b) for (lli i = a; i < b; i += 1)
#define loop_rev(i, n) for (lli i = n - 1; i >= 0; i--)
#define FOR_REV(i, a, b) for (lli i = a; i >= b; i--)
#define all(v) v.begin(), v.end()
// #define all(a) begin(a),end(a)
#define sz(x) int(x.size())
#define pii pair<int, int>
#define F first
#define S second
#define mii map<lli, lli>
#define vi vector<lli>
#define seti set<lli>
#define itr ::iterator it
#define WL(t) while (t--)
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) (a / gcd(a, b)) * b
#define abs(x) ((x < 0) ? -(x) : x)
#define print(x) printf("%lli\n", x);
#define print2(x, y) printf("%lli %lli\n", x, y);
#define print3(x, y, z) printf("%lli %lli %lli\n", x, y, z);
#define scan(x) scanf("%lld", &x);
#define scan2(x, y) scanf("%lld %lld", &x, &y);
#define scan3(x, y, z) scanf("%lld %lld %lld", &x, &y, &z);
#define scanarr(a, n) \
for (lli i = 0; i < n; i++) \
cin >> a[i];
#define scanvector(a, n) \
for (lli i = 0; i < n; i++) { \
lli x; \
cin >> x; \
a.push_back(x); \
}
#define printarr(a, n) \
for (lli i = 0; i < n; i++) \
printf("%lli ", a[i]); \
printf("\n");
#define printvector(vec) \
for (auto xt : vec) \
cout << xt << " "; \
cout << "\n";
#define printset(st) \
for (auto xt : st) \
cout << xt << " "; \
cout << "\n";
#define FD(N) fixed << setprecision(N)
#define endl '\n'
#define deb(x) cout << #x << " " << x << endl;
/*
ifstream cinn("i3.txt");
ofstream coutt("o3.txt");
*/
lli find_mod(string s) {
lli i, x = 0;
for (i = 0; i < s.length(); i++)
x = (x * 10 + s[i] - '0') % MOD;
return x;
}
bool prime[MAX + 1];
void sieve1() { // prime or not
for (int i = 0; i <= MAX; i++)
prime[i] = 1;
prime[0] = prime[1] = 0;
for (lli i = 4; i <= MAX; i += 2)
prime[i] = 0;
for (int p = 3; p * p <= MAX; p += 2) {
if (prime[p] == 1)
for (int i = p * 2; i <= MAX; i += p) {
prime[i] = 0;
}
}
}
int min_prime_div_of_n[MAX + 1];
void sieve2() { // smallest prime factor which divide a no.
memset(min_prime_div_of_n, 0, sizeof(min_prime_div_of_n));
min_prime_div_of_n[0] = min_prime_div_of_n[1] = 1;
for (int i = 2; i * i <= MAX; i++) {
if (min_prime_div_of_n[i] == 0) {
for (int j = i * 2; j < MAX; j += i) {
if (min_prime_div_of_n[j] == 0) {
min_prime_div_of_n[j] = i;
// cout<<min_prime_div_of_n[i]<<" ";}
}
}
}
}
for (int i = 2; i <= MAX; i++)
if (min_prime_div_of_n[i] == 0)
min_prime_div_of_n[i] = i;
// If the number is prime then it's // smallest prime factor is the number //
// itself
}
int max_prime_div_of_n[MAX + 1];
void sieve3() { // maximum prime factor which divide a no.
sieve1();
memset(max_prime_div_of_n, 0,
sizeof(max_prime_div_of_n)); // 0 stands for is_prime
max_prime_div_of_n[0] = max_prime_div_of_n[1] = 1;
for (int i = 2; i * i <= MAX; i++) {
if (max_prime_div_of_n[i] == 0) {
for (int j = i * 2; j < MAX; j += i) {
if (max_prime_div_of_n[j] == 0 || ((j % i) == 0 && prime[i] == 1)) {
max_prime_div_of_n[j] = i;
}
}
}
}
for (int i = 2; i <= 100; i++)
// If the number is prime then it's largest prime factor is the number
// itself
if (max_prime_div_of_n[i] == 0)
max_prime_div_of_n[i] = i;
// cout<<max_prime_div_of_n[i]<<" ";
}
lli sum_prime_fac[1000001];
void sieve4() { // prime or not
sum_prime_fac[1000001] = {0};
for (lli i = 2; i <= 1000000; i++) {
if (!sum_prime_fac[i]) {
for (lli j = i; j <= 1000000; j += i)
sum_prime_fac[j] += i;
}
}
}
bool is_even(lli n) { return (!(n & 2)); }
bool is_odd(lli n) { return (n & 2); }
// memset(array_name , initialized value , sizeof(data_type_of_array))
bool isPrime(lli n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n == 5)
return true;
if ((n % 2 == 0) || (n % 3 == 0) || (n % 5 == 0))
return false;
for (lli i = 5; i * i <= n; i += 6)
if ((n % i == 0) || (n % (i + 2) == 0))
return false;
return true;
}
lli reversenum(lli x) {
lli num = 0;
while (x > 0) {
num = num * 10 + x % 10;
x = x / 10;
}
return num;
}
bool ispalindrome(string s) {
string q = s;
reverse(q.begin(), q.end());
return (s == q);
}
bool isvowel(char x) {
return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u');
}
lli mceil(lli a, lli b) {
if (a % b == 0)
return (a / b);
else
return (a / b + 1);
}
lli mfloor(lli a, lli b) { return (a / b); }
lli sumarr(lli a[], lli n) {
lli sum = 0;
loop(i, n) sum += a[i];
return sum;
}
ll modmullong(ll a, ll b) {
ll res = 0;
a %= mod;
while (b) {
if (b & 1)
res = (res + a) % mod;
a = (2 * a) % mod;
b >>= 1;
}
return res;
}
ll modmul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll modmul2(ll a, ll b) {
return (((a % (mod - 1)) * (b % (mod - 1))) % (mod - 1));
}
ll modadd(ll a, ll b) { return ((a % mod) + (b % mod) + mod) % mod; }
ll modsub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
lli fastexpo(lli a, lli b) {
a = a % mod;
lli ans = 1;
while (b) {
if (b & 1)
ans = (ans * 1ll * a) % mod;
a = (a * 1ll * a) % mod;
b = b / 2;
}
return ans;
}
lli phi[MAX];
void euler_phi() { // euler totient function
loop(i, MAX - 1) phi[i + 1] = i + 1;
for (lli p = 2; p < MAX; p++) {
if (phi[p] == p) {
phi[p] = p - 1;
for (lli i = 2 * p; i < MAX; i += p)
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
lli myphi(lli n) {
lli result = n;
for (lli p = 2; p * p <= n; ++p) {
if (n % p == 0) {
while (n % p == 0)
n /= p;
result -= result / p;
}
}
if (n > 1)
result -= result / n;
return result;
}
void prime_factorise(lli n, map<lli, lli> &mp) {
while (n % 2 == 0) {
mp[2]++;
n /= 2;
}
for (lli i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
mp[i]++;
n /= i;
}
}
if (n > 1)
mp[n]++;
}
lli cntbits(lli n) {
lli cnt = 0;
while (n) {
cnt++;
n /= 2;
}
return cnt;
}
lli fact[100001];
void facto() {
fact[0] = 1;
for (lli i = 1; i <= 100000; i++)
fact[i] = modmul(fact[i - 1], i);
}
lli nCr(lli n, lli r) {
if (n < r)
return 0;
else {
r = min(r, n - r);
lli a = 1, b = 1;
for (lli i = 0; i < r; i++) {
a = (a * (n - i)) % mod;
b = (b * (i + 1)) % mod;
}
a = modmul(a, fastexpo(b, mod - 2));
return a;
}
}
bool ispower2(lli n) { return (n and (n & (n - 1)) == 0); }
// chandan1,2
void chandan1() { return; }
void chandan2() {
loop(i, 10) { lli x = 1; }
return (chandan1());
}
lli myfunc(lli x, lli y, lli z) { return ((x & z) * (y & z)); }
int main() {
fastIO lli t = 1;
// cin>>t;
// chandan2();
while (t--) {
lli a, b, n;
cin >> a >> b >> n;
if (n < b) {
lli val = mfloor(a * n, b) - a * mfloor(n, b);
cout << val;
} else if (n == b) {
lli w = n - 1;
lli val = mfloor(a * w, b) - a * mfloor(w, b);
cout << val;
} else {
lli w = b - 1;
lli val = mfloor(a * w, b) - a * mfloor(w, b);
cout << val;
}
/*lli maxi=-1;
for(lli i=1;i<=n;i++){
lli val = mfloor(a*i , b) - a*mfloor(i,b);
//cout<<i<<" "<<val<<endl;
maxi = max(maxi,val);
}
cout<<endl<<maxi;*/
}
return 0;
}
| replace | 370 | 377 | 370 | 389 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// Shortening code
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef tuple<int, int, int> ti;
// Macros
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = a; i >= b; i--)
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
// Your code
ll a, b, n;
cin >> a >> b >> n;
ll max_val = 0;
for (ll i = 0; i <= n; i++) {
max_val = max(max_val, (ll)(floor(a * i / b) - a * floor(i / b)));
}
cout << max_val;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// Shortening code
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef tuple<int, int, int> ti;
// Macros
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = a; i >= b; i--)
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
// Your code
ll a, b, n;
cin >> a >> b >> n;
ll ind = min(b - 1, n);
cout << floor(a * ind / b);
return 0;
} | replace | 24 | 29 | 24 | 26 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pint pair<int, int>
#define pll pair<ll, ll>
#define vint vector<int>
#define vll vector<ll>
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll ans = 0;
for (ll x = b - 1; x <= n; x += b) {
ans = max(ans, (a * x) / b - a * (x / b));
}
ans = max(ans, (a * n) / b - a * (n / b));
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pint pair<int, int>
#define pll pair<ll, ll>
#define vint vector<int>
#define vll vector<ll>
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll ans = 0;
if (b == 1) {
cout << ans << endl;
return 0;
}
for (ll x = b - 1; x <= n; x += b) {
ans = max(ans, (a * x) / b - a * (x / b));
}
ans = max(ans, (a * n) / b - a * (n / b));
cout << ans << endl;
return 0;
} | insert | 17 | 17 | 17 | 21 | TLE | |
p02696 | C++ | Time Limit Exceeded | /**
* @copyright (c) 2020 Daisuke Hashimoto
*/
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using Pair = pair<int64_t, int64_t>;
// std::cout << std::setprecision(20) << 1.1 << endl;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int64_t A, B, N;
cin >> A >> B >> N;
int64_t max_val = INT64_MIN;
for (int64_t x = 0; x < B && x <= N; ++x) {
const int64_t val = (A * x) / B - A * (x / B);
if (val > max_val) {
max_val = val;
}
}
cout << max_val << endl;
return 0;
}
| /**
* @copyright (c) 2020 Daisuke Hashimoto
*/
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using Pair = pair<int64_t, int64_t>;
// std::cout << std::setprecision(20) << 1.1 << endl;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int64_t A, B, N;
cin >> A >> B >> N;
int64_t max_val = (A * min(B - 1, N)) / B;
cout << max_val << endl;
return 0;
}
| replace | 17 | 24 | 17 | 18 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
ll INF = 1 << 25;
int main() {
ll A, B, N;
cin >> A >> B >> N;
ll X = -1 * INF;
for (int i = 0; i <= N; i++) {
ll Y = (ll)(A * i / B) - (ll)A * (i / B);
chmax(X, Y);
}
cout << X << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
ll INF = 1 << 25;
int main() {
ll A, B, N;
cin >> A >> B >> N;
// ll X = -1*INF;
// for (int i=0;i<=N;i++) {
// ll Y = (ll)(A*i/B)-(ll)A*(i/B);
// // cout << i << " " << Y << endl;
// chmax(X, Y);
// }
// cout << X << endl;
ll Z = min(B - 1, N);
cout << (ll)(A * Z / B) - (ll)A * (Z / B) << endl;
return 0;
} | replace | 21 | 27 | 21 | 30 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define deb(x) cerr << #x << ": " << x << '\n';
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
using namespace std;
typedef long double ld;
typedef long long ll;
int main() {
ios;
ll A, B, N;
cin >> A >> B >> N;
ll ans = 0, best = 0;
rep1(x, 1e8 + 9e7) {
if (x > N)
break;
ans = ((A * x) / B) - (A * (x / B));
best = max(best, ans);
}
cout << best;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define deb(x) cerr << #x << ": " << x << '\n';
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
using namespace std;
typedef long double ld;
typedef long long ll;
int main() {
ios;
ll A, B, N;
cin >> A >> B >> N;
ll ans = 0, max = min(B - 1, N);
ans = ((A * max) / B) - (A * (max / B));
cout << ans;
return 0;
}
| replace | 16 | 24 | 16 | 19 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
typedef long long ll;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
using namespace std;
typedef pair<int, int> P;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll ans = 0;
ll prev = 0;
int flag = 0;
for (ll x = 1; x < n + 1; x++) {
prev = ans;
ans = floor(a * x / b) - (a * floor(x / b));
if (ans != 0)
flag = 1;
else if (flag == 1) {
cout << prev << endl;
return 0;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
typedef long long ll;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
using namespace std;
typedef pair<int, int> P;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll x = min(b - 1, n);
cout << floor(a * x / b) - (a * floor(x / b)) << endl;
return 0;
} | replace | 9 | 23 | 9 | 11 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
long long int a, b, n, v, i, max, tmp;
scanf("%lld %lld %lld", &a, &b, &n);
max = 0;
for (i = 1; i <= n && i <= b * 2; i++) {
v = floor(a * i * 1.00 / b) - a * floor(i / b);
if (v > max)
max = v;
}
printf("%lld\n", max);
return 0;
}
| #include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
long long int a, b, n, v, i, max, tmp;
scanf("%lld %lld %lld", &a, &b, &n);
max = 0;
if (n < b) {
max = floor(a * n * 1.00 / b);
} else {
max = floor(a * (b - 1) * 1.00 / b);
}
printf("%lld\n", max);
return 0;
}
| replace | 8 | 12 | 8 | 12 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int A;
long long int B, N;
cin >> A >> B >> N;
long long int output, num;
output = 0;
if (N > B) {
N = B;
}
for (int i = 1; i <= N; i++) {
num = floor((A * i) / B) - A * floor(i / B);
if (num >= output) {
output = num;
}
}
cout << output << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int A;
long long int B, N;
cin >> A >> B >> N;
if (N > B - 1)
N = B - 1;
cout << floor(A * N / B) - A * (N / B) << endl;
return 0;
} | replace | 8 | 22 | 8 | 11 | TLE | |
p02696 | C++ | Time Limit Exceeded | #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
using namespace std;
typedef long long ll;
void Main() {
ll a, b, n;
cin >> a >> b >> n;
int max = -1;
ll t = 0;
for (ll x = 1; x <= n; ++x) {
t = floor(a * x / b) - a * floor(x / b);
if (t > max) {
max = t;
}
}
cout << max << endl;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(15);
Main();
} | #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
using namespace std;
typedef long long ll;
void Main() {
ll a, b, n;
cin >> a >> b >> n;
int max = 0;
if (b > n) {
max = floor(a * n / b) - a * floor(n / b);
} else {
n = b - 1;
max = floor(a * n / b) - a * floor(n / b);
}
cout << max << endl;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(15);
Main();
} | replace | 17 | 24 | 17 | 24 | TLE | |
p02696 | C++ | Time Limit Exceeded | // Floor Function
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll a, b, n;
ll f(ll x) {
ll A = floor(a * x / b);
// cout << A << endl;
ll B = floor(x / b);
return A - a * B;
}
int main() {
cin >> a >> b >> n;
ll ans = 0;
for (ll i = 1; i <= n; i++) {
// cout << f(i) << endl;
ans = max(f(i), ans);
}
cout << ans << endl;
return 0;
}
| // Floor Function
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll a, b, n;
ll f(ll x) {
ll A = floor(a * x / b);
// cout << A << endl;
ll B = floor(x / b);
return A - a * B;
}
int main() {
cin >> a >> b >> n;
ll ans = 0;
if (b - 1 <= n) {
ans = f(b - 1);
} else {
ans = f(n);
}
cout << ans << endl;
return 0;
}
| replace | 18 | 21 | 18 | 22 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using u8t = std::uint64_t;
using i8t = std::int64_t;
#define F(I, N) for (i8t I = 0, I##_N = N; I < I##_N; I++)
#define FR(I, N) for (i8t I = N; I--;)
#define R(V) \
i8t V; \
std::cin >> V;
#define RV(V, N) \
std::vector<i8t> V; \
F(_i, N) { R(_x) V.push_back(_x); }
#define RL1(line) std::getline(std::cin, line);
#define RL(line) \
std::string line; \
RL1(line)
#define RM(V, L, C) \
std::vector<char> V; \
F(_i, L) { RL(_l) F(_j, C) V.push_back(_l[_j]); }
#define P(X) std::cout << (X) << std::endl;
i8t div_ceil(i8t a, i8t b) { return (a + b - 1) / b; }
int main() {
R(A) R(B) R(N);
i8t mx = 0;
for (i8t x = 1; x <= N;) {
mx = std::max(mx, A * x / B - A * (x / B));
auto next_floor1_num = (A * x / B + 1) * B;
auto next_floor2_num = (x / B + 1) * B;
auto x1 = div_ceil(next_floor1_num, A);
x = std::min(x1, next_floor2_num);
}
P(mx);
return 0;
}
| #include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using u8t = std::uint64_t;
using i8t = std::int64_t;
#define F(I, N) for (i8t I = 0, I##_N = N; I < I##_N; I++)
#define FR(I, N) for (i8t I = N; I--;)
#define R(V) \
i8t V; \
std::cin >> V;
#define RV(V, N) \
std::vector<i8t> V; \
F(_i, N) { R(_x) V.push_back(_x); }
#define RL1(line) std::getline(std::cin, line);
#define RL(line) \
std::string line; \
RL1(line)
#define RM(V, L, C) \
std::vector<char> V; \
F(_i, L) { RL(_l) F(_j, C) V.push_back(_l[_j]); }
#define P(X) std::cout << (X) << std::endl;
i8t div_ceil(i8t a, i8t b) { return (a + b - 1) / b; }
int main() {
R(A) R(B) R(N);
i8t mx = 0;
if (N >= B)
N = B;
for (i8t x = 1; x <= N;) {
mx = std::max(mx, A * x / B - A * (x / B));
auto next_floor1_num = (A * x / B + 1) * B;
auto next_floor2_num = (x / B + 1) * B;
auto x1 = div_ceil(next_floor1_num, A);
x = std::min(x1, next_floor2_num);
}
P(mx);
return 0;
}
| insert | 34 | 34 | 34 | 36 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int, int>::iterator mit;
typedef vector<int>::iterator vit;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;
#define _ % MOD
#define __ %= MOD
#define each(it, s) for (auto it = s.begin(); it != s.end(); ++it)
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.begin(), v.end(), greater<auto>())
#define fill(a) memset(a, 0, sizeof(a))
#define swap(a, b) \
{ \
a = a + b; \
b = a - b; \
a = a - b; \
}
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll power(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
} // modular exponent
int main() {
ios_base::sync_with_stdio(false); // don't use printf and scanf
cin.tie(NULL); // cout<<fixed<<setprecision
ll a, b, n;
cin >> a >> b >> n;
ll q = n / b;
// ll rem=n%b;
ll ans = 0;
ans = max(ans, (a * n) / b - a * (n / b));
repA(i, 1, q) { ans = max(ans, (a * ((ll)i * b - 1)) / b - a * ((ll)i - 1)); }
cout << ans << '\n';
return 0;
}
// JUST ASK YOURSELF DID YOU GIVE YOUR BEST ? ISSE ZYADA
// KUCH KAR BHI NAHI SAKTE !! ENJOY AND GIVE YOUR BEST!!
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long double ld;
typedef set<int>::iterator sit;
typedef map<int, int>::iterator mit;
typedef vector<int>::iterator vit;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;
#define _ % MOD
#define __ %= MOD
#define each(it, s) for (auto it = s.begin(); it != s.end(); ++it)
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.begin(), v.end(), greater<auto>())
#define fill(a) memset(a, 0, sizeof(a))
#define swap(a, b) \
{ \
a = a + b; \
b = a - b; \
a = a - b; \
}
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll power(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
} // modular exponent
int main() {
ios_base::sync_with_stdio(false); // don't use printf and scanf
cin.tie(NULL); // cout<<fixed<<setprecision
ll a, b, n;
cin >> a >> b >> n;
ll x = min(b - 1, n);
ll ans = (a * x) / b - a * (x / b);
cout << ans << '\n';
return 0;
}
// JUST ASK YOURSELF DID YOU GIVE YOUR BEST ? ISSE ZYADA
// KUCH KAR BHI NAHI SAKTE !! ENJOY AND GIVE YOUR BEST!!
| replace | 66 | 71 | 66 | 68 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int f(long long a, long long b, long long x) {
long long s = a * x / b;
long long t = a * (x / b);
return s - t;
}
int main() {
long long a, b, n;
cin >> a >> b >> n;
long long ans = 0;
for (long long i = n / 2; i <= n; i++) {
if (ans < f(a, b, i))
ans = f(a, b, i);
}
cout << ans << endl;
} | #include <iostream>
using namespace std;
int f(long long a, long long b, long long x) {
long long s = a * x / b;
long long t = a * (x / b);
return s - t;
}
int main() {
long long a, b, n;
cin >> a >> b >> n;
long long ans = 0;
if (b > n) {
ans = f(a, b, n);
} else {
ans = f(a, b, b - 1);
}
cout << ans << endl;
} | replace | 12 | 15 | 12 | 16 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
long long A, B, N;
long long dif(long long x) {
long long a = A * x;
a /= B;
long long b = x / B;
b *= A;
return a - b;
}
long long bi(long long mi, long long ma, long long target) {
if (ma - mi == 1) {
return dif(ma);
}
long long mid = (ma + mi) / 2;
long long midValue = dif(mid);
if (midValue >= target) {
return bi(mi, mid, target);
} else {
return bi(mid, ma, target);
}
}
int main() {
cin >> A >> B >> N;
long long valueN = dif(N);
long long x = N / B;
x *= B;
x -= 1;
if (x < 0) {
x = 0;
}
long long valueLastEdge = dif(x);
long long target = max(valueN, valueLastEdge);
cout << bi(0, min(N, B - 1), target);
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
long long A, B, N;
long long dif(long long x) {
long long a = A * x;
a /= B;
long long b = x / B;
b *= A;
return a - b;
}
long long bi(long long mi, long long ma, long long target) {
if (ma - mi == 1 || ma - mi == 0) {
return dif(ma);
}
long long mid = (ma + mi) / 2;
long long midValue = dif(mid);
if (midValue >= target) {
return bi(mi, mid, target);
} else {
return bi(mid, ma, target);
}
}
int main() {
cin >> A >> B >> N;
long long valueN = dif(N);
long long x = N / B;
x *= B;
x -= 1;
if (x < 0) {
x = 0;
}
long long valueLastEdge = dif(x);
long long target = max(valueN, valueLastEdge);
cout << bi(0, min(N, B - 1), target);
} | replace | 21 | 22 | 21 | 22 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
#define MOD(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, n) for (int i = 0; i < n; i++)
#define forr(i, a, b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
ll a, b, x, n, res;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> a >> b >> n;
for (int x = max(1ll, n - int(2e6)); x <= n; x++) {
res = max(res, (a * x) / b - a * (x / b));
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
#define MOD(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, n) for (int i = 0; i < n; i++)
#define forr(i, a, b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
ll a, b, x, n, res;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> a >> b >> n;
ll x = n - n % b - 1;
if (x < 1)
x = n;
cout << (a * x) / b - a * (x / b) << endl;
return 0;
}
| replace | 28 | 32 | 28 | 33 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define All(V) V.begin(), V.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7, INF = 1e9;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll ans = (ll)(floor(a * n) / b) - (ll)(a * floor(n / b));
for (ll i = n; i >= b - 1; i--) {
if (i > n)
break;
ans = max(ans, (ll)(floor(a * i) / b) - (ll)(a * floor(i / b)));
}
cout << ans << endl;
system("pause");
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define All(V) V.begin(), V.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7, INF = 1e9;
int main() {
ll a, b, n;
cin >> a >> b >> n;
cout << a * min(n, b - 1) / b << endl;
system("pause");
} | replace | 11 | 18 | 11 | 12 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(), (x).end()
#define f(i, a, b) for (int i = a; i < (signed)(b); ++i)
#define pc __builtin_popcount // counts number of 1's in bin(num)
#define pb push_back
#define mp make_pair
// debug templates
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N) \
cerr << #A << " : ["; \
for (int i = 0; i < N; i++) \
cerr << A[i] << " "; \
cerr << "]\n";
#define debuga2(A, N, M) \
cerr << #A << " : \n"; \
for (int i = 0; i < N; i++) { \
cerr << "["; \
for (int j = 0; j < M; ++j) \
cerr << A[i][j] << " "; \
cerr << "]\n"; \
}
#define debugp(p) \
cerr << #p << " : " \
<< "(" << (p).first << "," << (p).second << ")\n";
#define debugv(v) \
cerr << #v << " : " \
<< "["; \
for (int i = 0; i < (v).size(); i++) \
cerr << v[i] << " "; \
cerr << "]\n";
#define debugv2(v) \
cerr << #v << " : \n"; \
for (int i = 0; i < v.size(); i++) { \
cerr << "["; \
for (int j = 0; j < (v[0].size()); ++j) \
cerr << v[i][j] << " "; \
cerr << "]\n"; \
}
#define debugs(m) \
cerr << #m << " : [ "; \
for (auto itr = m.begin(); itr != m.end(); itr++) \
cerr << *itr << " "; \
cerr << "]\n";
#define debugm(m) \
cerr << #m << " : [ "; \
for (auto itr = m.begin(); itr != m.end(); itr++) \
cerr << "(" << itr->first << "," << itr->second << ") "; \
cerr << "]\n";
void solve() {
ll a, b, n;
cin >> a >> b >> n;
ll x = -1;
if (n < b) {
x = n;
} else {
for (ll i = n; i >= n - b; --i) {
if (i % b == b - 1) {
x = i;
break;
}
}
}
cout << (a * x) / b - a * (x / b);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(), (x).end()
#define f(i, a, b) for (int i = a; i < (signed)(b); ++i)
#define pc __builtin_popcount // counts number of 1's in bin(num)
#define pb push_back
#define mp make_pair
// debug templates
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N) \
cerr << #A << " : ["; \
for (int i = 0; i < N; i++) \
cerr << A[i] << " "; \
cerr << "]\n";
#define debuga2(A, N, M) \
cerr << #A << " : \n"; \
for (int i = 0; i < N; i++) { \
cerr << "["; \
for (int j = 0; j < M; ++j) \
cerr << A[i][j] << " "; \
cerr << "]\n"; \
}
#define debugp(p) \
cerr << #p << " : " \
<< "(" << (p).first << "," << (p).second << ")\n";
#define debugv(v) \
cerr << #v << " : " \
<< "["; \
for (int i = 0; i < (v).size(); i++) \
cerr << v[i] << " "; \
cerr << "]\n";
#define debugv2(v) \
cerr << #v << " : \n"; \
for (int i = 0; i < v.size(); i++) { \
cerr << "["; \
for (int j = 0; j < (v[0].size()); ++j) \
cerr << v[i][j] << " "; \
cerr << "]\n"; \
}
#define debugs(m) \
cerr << #m << " : [ "; \
for (auto itr = m.begin(); itr != m.end(); itr++) \
cerr << *itr << " "; \
cerr << "]\n";
#define debugm(m) \
cerr << #m << " : [ "; \
for (auto itr = m.begin(); itr != m.end(); itr++) \
cerr << "(" << itr->first << "," << itr->second << ") "; \
cerr << "]\n";
void solve() {
ll a, b, n;
cin >> a >> b >> n;
ll x = -1;
if (n < b) {
x = n;
} else {
ll off = n % b;
x = n - off - 1;
}
cout << (a * x) / b - a * (x / b);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
| replace | 63 | 69 | 63 | 65 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, n;
long long int res = 0;
cin >> a >> b >> n;
long long int i = 0;
while (i <= n && (i / b) < 1) {
long long int tmp = floor(a * i / b);
res = max(res, tmp);
++i;
}
cout << res << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, n;
long long int res = 0;
cin >> a >> b >> n;
long long int i = min(n, b - 1);
res = floor(a * i / b);
/*
while (i<=n && (i/b)<1){
long long int tmp=floor(a*i/b);
res=max(res,tmp);
++i;
}
*/
cout << res << endl;
} | replace | 7 | 15 | 7 | 16 | TLE | |
p02696 | C++ | Time Limit Exceeded | /**
* D - Floor Function
* https://atcoder.jp/contests/abc165/tasks/abc165_d
*/
#include <bits/stdc++.h>
using namespace std;
long long A, B, N;
long long calc(long long x) {
long long floor1 = A * x / B;
long long floor2 = x / B;
return floor1 - A * floor2;
}
int main() {
// 整数 A, B, N が与えられます。
cin >> A >> B >> N;
// N 以下の非負整数 x に対する
// floor(Ax/B)−A×floor(x/B)の最大値を求めてください。
// ただし、floor(t)とは、実数 t 以下の最大の整数のことを表します。
long long maxNum = 0;
for (long long x = 0; x <= N; x++) {
long long c = calc(x);
if (maxNum < c) {
maxNum = c;
}
}
cout << maxNum;
}
| /**
* D - Floor Function
* https://atcoder.jp/contests/abc165/tasks/abc165_d
*/
#include <bits/stdc++.h>
using namespace std;
long long A, B, N;
long long calc(long long x) {
long long floor1 = A * x / B;
long long floor2 = x / B;
return floor1 - A * floor2;
}
int main() {
// 整数 A, B, N が与えられます。
cin >> A >> B >> N;
// N 以下の非負整数 x に対する
// floor(Ax/B)−A×floor(x/B)の最大値を求めてください。
// ただし、floor(t)とは、実数 t 以下の最大の整数のことを表します。
long long keisu = N / B;
long long maxNum1 = calc(B * keisu - 1);
long long maxNum2 = calc(N);
cout << (maxNum1 > maxNum2 ? maxNum1 : maxNum2);
}
| replace | 22 | 31 | 22 | 26 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll A, B, N;
cin >> A >> B >> N;
ll Ans = 0;
for (int i = 0; i <= N; i++) {
ll num = (A * i / B) - A * (i / B);
Ans = max(Ans, num);
}
cout << Ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll A, B, N;
cin >> A >> B >> N;
ll ans;
if (N < B)
ans = (A * N / B);
else
ans = (A * (B - 1) / B);
cout << ans << endl;
return 0;
} | replace | 9 | 15 | 9 | 15 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll sum = 0, prevSum = -1;
for (ll x = 1; x <= n; x++) {
sum = floor((a * x) / b) - a * floor(x / b);
// cout<<x<<" "<<sum<<" "<<prevSum<<endl;
if (prevSum == -1) {
prevSum = sum;
} else {
if (sum < prevSum) {
break;
}
prevSum = sum;
}
}
// cout<<sum<<" "<<prevSum<<endl;
cout << prevSum << endl;
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ll a, b, n;
cin >> a >> b >> n;
if (n >= b)
cout << floor(a * (b - 1) / b) << endl;
else
cout << floor(a * (n) / b) << endl;
return 0;
}
| replace | 7 | 22 | 7 | 11 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18 + 1;
const ll DIV = 1000000007;
// #define TEST
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
#ifdef TEST
chrono::system_clock::time_point start, end;
start = chrono::system_clock::now();
#endif
ll A, B, N;
cin >> A >> B >> N;
ll max_num = 0;
if (N / B == 0) {
ll x = N;
ll ax_b = (A * x) / B;
ll x_b = x / B;
max_num = max(max_num, ax_b - A * x_b);
} else {
for (size_t i = 1; i <= N / B; i++) {
// ll ax_b = (A * i)/B;
// ll x_b = i / B;
ll x = i * B - 1;
ll ax_b = (A * x) / B;
ll x_b = x / B;
max_num = max(max_num, ax_b - A * x_b);
}
}
cout << max_num << endl;
#ifdef TEST
end = chrono::system_clock::now();
cerr << static_cast<double>(
chrono::duration_cast<chrono::microseconds>(end - start).count() /
1000.0)
<< "[ms]" << endl;
#endif
return 0;
} | #include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18 + 1;
const ll DIV = 1000000007;
// #define TEST
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
#ifdef TEST
chrono::system_clock::time_point start, end;
start = chrono::system_clock::now();
#endif
ll A, B, N;
cin >> A >> B >> N;
ll max_num = 0;
ll x = min(B - 1, N);
ll ax_b = (A * x) / B;
ll x_b = x / B;
cout << ax_b - A * x_b << endl;
#ifdef TEST
end = chrono::system_clock::now();
cerr << static_cast<double>(
chrono::duration_cast<chrono::microseconds>(end - start).count() /
1000.0)
<< "[ms]" << endl;
#endif
return 0;
} | replace | 28 | 44 | 28 | 32 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a, b;
cin >> a >> b >> n;
if (b > n) {
cout << a * n / b << endl;
return 0;
}
long long y = n / b;
priority_queue<long long> pq;
for (int i = 1; i < y + 1; i++) {
pq.push(b * i - 1);
}
pq.push(n);
long long max = 0;
while (!pq.empty()) {
long long mae = a * pq.top() / b;
long long ushi = pq.top() / b;
if (max < mae - ushi * a)
max = mae - ushi * a;
pq.pop();
}
cout << max << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a, b;
cin >> a >> b >> n;
if (b > n) {
cout << a * n / b << endl;
return 0;
}
cout << a * (b - 1) / b << endl;
return 0;
} | replace | 11 | 27 | 11 | 12 | TLE | |
p02696 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll res = 0;
for (int i = 0; i <= n; i++) {
ll tmp = floor(a * i / b) - a * floor(i / b);
res = max(res, tmp);
}
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
int main() {
ll a, b, n;
cin >> a >> b >> n;
ll x = n;
if (b <= n)
x = b - 1 ? b - 1 : 1;
ll res = floor(a * x / b) - a * floor(x / b);
cout << res << endl;
}
| replace | 10 | 15 | 10 | 14 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.