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
p02838
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); const ll mod = 1e9 + 7; int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i]; // bits[i][j][0or1] := i番目以降の数にj桁目が0or1になる数がいくつあるか vector<vector<vector<ll>>> bits(n, vector<vector<ll>>(60, vector<ll>(2))); for (ll i = n - 1; i >= 0; i--) { for (ll j = 0; j < 60; j++) { if (i < n - 1) { bits[i][j][0] = bits[i + 1][j][0]; bits[i][j][1] = bits[i + 1][j][1]; } if (a[i] & (1ll << j)) { bits[i][j][1]++; } else { bits[i][j][0]++; } } } ll ans = 0; for (ll i = 0; i < n - 1; i++) { for (ll j = 0; j < 60; j++) { if (a[i] & (1ll << j)) { for (int k = 0; k < bits[i + 1][j][0]; k++) { ans += (1ll << j); ans %= mod; } // ans += ((1ll << j) % mod) * bits[i + 1][j][0]; // ans %= mod; } else { for (int k = 0; k < bits[i + 1][j][1]; k++) { ans += (1ll << j); ans %= mod; } // ans += ((1ll << j) % mod) * bits[i + 1][j][1]; // ans %= mod; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); const ll mod = 1e9 + 7; int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i]; // bits[i][j][0or1] := i番目以降の数にj桁目が0or1になる数がいくつあるか vector<vector<vector<ll>>> bits(n, vector<vector<ll>>(60, vector<ll>(2))); for (ll i = n - 1; i >= 0; i--) { for (ll j = 0; j < 60; j++) { if (i < n - 1) { bits[i][j][0] = bits[i + 1][j][0]; bits[i][j][1] = bits[i + 1][j][1]; } if (a[i] & (1ll << j)) { bits[i][j][1]++; } else { bits[i][j][0]++; } } } ll ans = 0; // for (ll i = 0; i < n - 1; i++) { // for (ll j = 0; j < 60; j++) { // if (a[i] & (1ll << j)) { // ans += ((1ll << j) % mod) * bits[i + 1][j][0]; // ans %= mod; // } else { // ans += ((1ll << j) % mod) * bits[i + 1][j][1]; // ans %= mod; // } // } // } for (ll j = 0; j < 60; j++) { ans += (1ll << j) % mod * bits[0][j][0] % mod * bits[0][j][1] % mod; ans %= mod; } cout << ans << endl; return 0; }
replace
35
53
35
49
TLE
p02838
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define endl "\n" #define REP(i, a, n) for (int i = a; i < n; ++i) #define REPR(i, a, n) for (int i = a; i > n; --i) #define RUP(a, b) ((a + b - 1) / (b)) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define mt make_tuple // #define MOD 1000000007 #define INF LLONG_MAX / 2 typedef long long ll; typedef pair<int, int> Pii; typedef tuple<int, int, int> Tiii; typedef vector<int> Vi; typedef vector<Vi> VVi; typedef vector<Pii> VPii; typedef vector<string> Vs; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void YesNo(T a) { cout << (a ? "Yes" : "No") << endl; } template <class T> void YESNO(T a) { cout << (a ? "YES" : "NO") << endl; } void vin(Vi &v) { REP(i, 0, (v).size()) cin >> v[i]; } void vin(Vi &v, Vi &v2) { REP(i, 0, (v).size()) cin >> v[i] >> v2[i]; } void vout(Vi &v) { for (int i = 0; i < (v).size(); i++) cout << v[i] << " "; cout << endl; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } void uniq(Vi &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } void accum(Vi &v) { REP(i, 1, (v).size()) v[i] += v[i - 1]; } bool comp(Pii a, Pii b) { if (a.second != b.second) return a.second < b.second; else return a.first < b.first; } const long long MOD = 1000000007; void solve(long long n, std::vector<long long> a) { int ans = 0; REP(i, 0, 61) { int checkbit = (1ll << i); int cnt0 = 0, cnt1 = 0; REP(j, 0, n) { if (a[j] & checkbit) cnt1++; else cnt0++; } // cout<<i<<" "<<cnt0<<" "<<cnt1<<endl; REP(j, 0, cnt0 * cnt1) ans = (ans + checkbit) % MOD; // cout<<ans<<endl; } cout << ans << endl; } // Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You // use the default template now. You can remove this line by using your custom // template) signed main() { long long N; scanf("%lld", &N); std::vector<long long> A(N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); } solve(N, std::move(A)); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define endl "\n" #define REP(i, a, n) for (int i = a; i < n; ++i) #define REPR(i, a, n) for (int i = a; i > n; --i) #define RUP(a, b) ((a + b - 1) / (b)) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define mt make_tuple // #define MOD 1000000007 #define INF LLONG_MAX / 2 typedef long long ll; typedef pair<int, int> Pii; typedef tuple<int, int, int> Tiii; typedef vector<int> Vi; typedef vector<Vi> VVi; typedef vector<Pii> VPii; typedef vector<string> Vs; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void YesNo(T a) { cout << (a ? "Yes" : "No") << endl; } template <class T> void YESNO(T a) { cout << (a ? "YES" : "NO") << endl; } void vin(Vi &v) { REP(i, 0, (v).size()) cin >> v[i]; } void vin(Vi &v, Vi &v2) { REP(i, 0, (v).size()) cin >> v[i] >> v2[i]; } void vout(Vi &v) { for (int i = 0; i < (v).size(); i++) cout << v[i] << " "; cout << endl; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } void uniq(Vi &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } void accum(Vi &v) { REP(i, 1, (v).size()) v[i] += v[i - 1]; } bool comp(Pii a, Pii b) { if (a.second != b.second) return a.second < b.second; else return a.first < b.first; } const long long MOD = 1000000007; void solve(long long n, std::vector<long long> a) { int ans = 0; REP(i, 0, 61) { int checkbit = (1ll << i); int cnt0 = 0, cnt1 = 0; REP(j, 0, n) { if (a[j] & checkbit) cnt1++; else cnt0++; } // cout<<i<<" "<<cnt0<<" "<<cnt1<<endl; // REP(j,0,cnt0*cnt1) ans = (ans+checkbit)%MOD; ans = (ans + (((cnt0 * cnt1) % MOD) * (checkbit % MOD)) % MOD) % MOD; // cout<<ans<<endl; } cout << ans << endl; } // Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You // use the default template now. You can remove this line by using your custom // template) signed main() { long long N; scanf("%lld", &N); std::vector<long long> A(N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); } solve(N, std::move(A)); return 0; }
replace
80
81
80
82
TLE
p02838
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 typedef long long ll; typedef pair<int, int> P; ll dis[100000] = {}; int main() { int n, i, j; ll a, tem, res = 0, count[60][2] = {}; cin >> n; for (i = 0; i < n; i++) { cin >> a; tem = 1; for (j = 0; j < 60; j++) { count[j][(int)((tem & a) > 0)]++; tem <<= 1; } } tem = 1; for (i = 0; i < n; i++) { res += ((tem % MOD) * (count[i][0] * count[i][1] % MOD)) % MOD; res %= MOD; tem <<= 1; } cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 typedef long long ll; typedef pair<int, int> P; ll dis[100000] = {}; int main() { int n, i, j; ll a, tem, res = 0, count[60][2] = {}; cin >> n; for (i = 0; i < n; i++) { cin >> a; tem = 1; for (j = 0; j < 60; j++) { count[j][(int)((tem & a) > 0)]++; tem <<= 1; } } tem = 1; for (i = 0; i < 60; i++) { res += ((tem % MOD) * (count[i][0] * count[i][1] % MOD)) % MOD; res %= MOD; tem <<= 1; } cout << res; return 0; }
replace
21
22
21
22
0
p02838
C++
Runtime Error
/************************************************************ > File Name: d.cpp > Author: TSwiftie > Mail: 2224273204@qq.com > Created Time: Fri 20 Dec 2019 03:02:31 AM CST ************************************************************/ #pragma GCC optimize(2) #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> // #include <unordered_map> #define lowbit(x) (x & -x) #define lc (o << 1) #define rc (o << 1 | 1) #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 0x3f3f3f3f; const int MAXN = 1e5 + 5; const int MAXM = 2e5 + 5; const int MOD = 1e9 + 7; const int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; const double PI = acos(-1.0); const double EXP = 1e-8; ll n; ll a[MAXN]; int bit[65]; int main(void) { IOS; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; for (int j = 0; j < 62; j++) if (a[i] & (1ll << j)) bit[j]++; } ll ans = 0; for (int i = 0; i < 62; i++) ans = (ans + ((bit[i] * (n - bit[i]) % MOD) * ((1ll << i) % MOD)) % MOD) % MOD; cout << ans << endl; return 0; }
/************************************************************ > File Name: d.cpp > Author: TSwiftie > Mail: 2224273204@qq.com > Created Time: Fri 20 Dec 2019 03:02:31 AM CST ************************************************************/ #pragma GCC optimize(2) #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> // #include <unordered_map> #define lowbit(x) (x & -x) #define lc (o << 1) #define rc (o << 1 | 1) #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 0x3f3f3f3f; const int MAXN = 3e5 + 5; const int MAXM = 2e5 + 5; const int MOD = 1e9 + 7; const int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; const double PI = acos(-1.0); const double EXP = 1e-8; ll n; ll a[MAXN]; int bit[65]; int main(void) { IOS; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; for (int j = 0; j < 62; j++) if (a[i] & (1ll << j)) bit[j]++; } ll ans = 0; for (int i = 0; i < 62; i++) ans = (ans + ((bit[i] * (n - bit[i]) % MOD) * ((1ll << i) % MOD)) % MOD) % MOD; cout << ans << endl; return 0; }
replace
34
35
34
35
0
p02838
C++
Time Limit Exceeded
#include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { int N; cin >> N; ll *a = new ll[N]; for (int i = 0; i < N; i++) cin >> a[i]; ll mod = 1e9 + 7; vector<vector<int>> b(N, vector<int>(60)); for (int i = 0; i < N; i++) { for (int j = 0; j < 60; j++) { b[i][j] = a[i] % 2; a[i] /= 2; } } /* for (int i = 0; i < N; i++) { for (int j = 0; j < 60; j++) cout << b[i][j]; cout << endl; } */ vector<vector<ll>> p(N, vector<ll>(60)), q(N, vector<ll>(60)); for (int k = 0; k < 60; k++) { p[N - 1][k] = (1 - b[N - 1][k]); q[N - 1][k] = b[N - 1][k]; for (int i = N - 1; i > 0; i--) { p[i - 1][k] = p[i][k] + (1 - b[i - 1][k]); q[i - 1][k] = q[i][k] + b[i - 1][k]; } } /* cout << endl; for (int i = 0; i < N; i++) { for (int k = 0; k < 60; k++) cout << p[i][k]; cout << endl; } */ ll ans = 0, tmp = 1; for (int k = 0; k < 60; k++) { for (int i = 0; i < N - 1; i++) { if (b[i][k] == 0) ans += tmp * q[i + 1][k] % mod; else ans += tmp * p[i + 1][k] % mod; ans %= mod; } tmp *= 2; tmp %= mod; } cout << ans << endl; return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { int N; cin >> N; ll *a = new ll[N]; for (int i = 0; i < N; i++) cin >> a[i]; ll mod = 1e9 + 7; vector<vector<int>> b(N, vector<int>(60)); for (int i = 0; i < N; i++) { for (int j = 0; j < 60; j++) { b[i][j] = a[i] % 2; a[i] /= 2; } } /* for (int i = 0; i < N; i++) { for (int j = 0; j < 60; j++) cout << b[i][j]; cout << endl; } */ vector<vector<ll>> p(N, vector<ll>(60)), q(N, vector<ll>(60)); for (int k = 0; k < 60; k++) { p[N - 1][k] = (1 - b[N - 1][k]); q[N - 1][k] = b[N - 1][k]; for (int i = N - 1; i > 0; i--) { p[i - 1][k] = p[i][k] + (1 - b[i - 1][k]); q[i - 1][k] = q[i][k] + b[i - 1][k]; } } /* cout << endl; for (int i = 0; i < N; i++) { for (int k = 0; k < 60; k++) cout << p[i][k]; cout << endl; } */ ll ans = 0, tmp = 1; for (int k = 0; k < 60; k++) { ans += tmp * p[0][k] % mod * q[0][k] % mod; ans %= mod; tmp *= 2; tmp %= mod; } cout << ans << endl; return 0; }
replace
53
60
53
55
TLE
p02838
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long using pi = pair<int, int>; #define mp make_pair const int mod = 1e9 + 7; signed main() { int n; cin >> n; vector<int> a(n); for (auto &i : a) cin >> i; int suma = 0, land = 0; for (auto i : a) suma = (suma + i) % mod; int res = 0; for (auto i : a) for (auto j : a) land = (land + (i & j)) % mod; res = (n * suma - land) % mod; cout << res; }
#include <bits/stdc++.h> using namespace std; #define int long using pi = pair<int, int>; #define mp make_pair const int mod = 1e9 + 7; signed main() { int n; cin >> n; vector<int> a(n); for (auto &i : a) cin >> i; int suma = 0, land = 0; for (auto i : a) suma = (suma + i) % mod; int res = 0; bool ref = true; int p = 1, cnt, ps = 0; while (ref) { ref = false; cnt = ps = 0; for (auto &i : a) if (i) { ref = true; if (i & 1) ++cnt; i >>= 1; } ps = cnt % mod * cnt % mod * p % mod; land = (land + ps) % mod; p <<= 1; p %= mod; } res = suma % mod * n % mod; res = (res - land) % mod; while (res < 0) res += mod; cout << res % mod << endl; }
replace
17
22
17
40
TLE
p02838
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1e9 + 7, MAX_N = 30006; int N; ll A[MAX_N]; ll bit0[70], bit1[70]; // how many 0's and 1's have we seen at bit i void solve() { ll x = A[N - 1]; for (int i = 0; i < 62; ++i) { if (x >> i & 1) bit1[i]++; else bit0[i]++; } ll res = 0; for (int i = N - 2; i >= 0; --i) { ll x = A[i]; for (int j = 0; j < 62; ++j) { if (x >> j & 1) res = (res + (bit0[j] % MOD) * ((1LL << j) % MOD)) % MOD, bit1[j] += 1; else res = (res + (bit1[j] % MOD) * ((1LL << j) % MOD)) % MOD, bit0[j] += 1; } } cout << res << "\n"; } int main() { cin >> N; for (int i = 0; i < N; ++i) cin >> A[i]; solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1e9 + 7, MAX_N = 300006; int N; ll A[MAX_N]; ll bit0[70], bit1[70]; // how many 0's and 1's have we seen at bit i void solve() { ll x = A[N - 1]; for (int i = 0; i < 62; ++i) { if (x >> i & 1) bit1[i]++; else bit0[i]++; } ll res = 0; for (int i = N - 2; i >= 0; --i) { ll x = A[i]; for (int j = 0; j < 62; ++j) { if (x >> j & 1) res = (res + (bit0[j] % MOD) * ((1LL << j) % MOD)) % MOD, bit1[j] += 1; else res = (res + (bit1[j] % MOD) * ((1LL << j) % MOD)) % MOD, bit0[j] += 1; } } cout << res << "\n"; } int main() { cin >> N; for (int i = 0; i < N; ++i) cin >> A[i]; solve(); return 0; }
replace
4
5
4
5
0
p02838
C++
Runtime Error
#include <bits/stdc++.h> #define f first #define s second #define MOD 1000000007 #define PMOD 998244353 #define pb(x) push_back(x) using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, ll> plii; typedef pair<int, pii> piii; const int INF = 1e9 + 10; const ll LINF = 1LL * INF * INF; const int MAXN = 2e5 + 10; const int MAXM = 5e3 + 10; priority_queue<int> pq; vector<vector<int>> graph; queue<int> que; ll A[MAXN]; ll cal[110]; int main() { ll n, m, k, a, b, x, y, q; int sum = 0; int cnt = 0; int mx = 0; int mn = INF; ll cur = 0, idx = -1; int tc; ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; cur = A[i]; for (int j = 0; j < 60; j++) { cal[j] += (cur & 1); cur >>= 1; } } ll res = 0; ll bi = 1; for (int i = 0; i < 60; i++) { cur = cal[i] * (n - cal[i]); cur %= MOD; res += (bi * cur); res %= MOD; bi <<= 1; if (bi >= MOD) bi -= MOD; } cout << res << "\n"; return 0; }
#include <bits/stdc++.h> #define f first #define s second #define MOD 1000000007 #define PMOD 998244353 #define pb(x) push_back(x) using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, ll> plii; typedef pair<int, pii> piii; const int INF = 1e9 + 10; const ll LINF = 1LL * INF * INF; const int MAXN = 3e5 + 10; const int MAXM = 5e3 + 10; priority_queue<int> pq; vector<vector<int>> graph; queue<int> que; ll A[MAXN]; ll cal[110]; int main() { ll n, m, k, a, b, x, y, q; int sum = 0; int cnt = 0; int mx = 0; int mn = INF; ll cur = 0, idx = -1; int tc; ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; cur = A[i]; for (int j = 0; j < 60; j++) { cal[j] += (cur & 1); cur >>= 1; } } ll res = 0; ll bi = 1; for (int i = 0; i < 60; i++) { cur = cal[i] * (n - cal[i]); cur %= MOD; res += (bi * cur); res %= MOD; bi <<= 1; if (bi >= MOD) bi -= MOD; } cout << res << "\n"; return 0; }
replace
14
15
14
15
0
p02838
Python
Time Limit Exceeded
N = int(input()) A = list(map(int, input().split())) result = 0 for i in range(60): on = sum([(1 << i) & a for a in A]) >> i result += (on * (N - on)) << i result %= (10**9) + 7 print(result)
N = int(input()) A = list(map(int, input().split())) result = 0 for i in range(60): target = 1 << i on = sum([target & a for a in A]) >> i result += (on * (N - on)) << i result %= (10**9) + 7 print(result)
replace
5
6
5
7
TLE
p02838
Python
Runtime Error
def solve(string): n, *a = map(int, string.split()) a = ("{:060b}".format(_a) for _a in a) m = 10**9 + 7 ans = 0 for s in map(str.count("1"), zip(*a)): ans <<= 1 ans += s * (n - s) ans %= m return str(ans) if __name__ == "__main__": import sys print(solve(sys.stdin.read().strip()))
def solve(string): n, *a = map(int, string.split()) a = ("{:060b}".format(_a) for _a in a) m = 10**9 + 7 ans = 0 for s in map(lambda x: x.count("1"), zip(*a)): ans <<= 1 ans += s * (n - s) ans %= m return str(ans) if __name__ == "__main__": import sys print(solve(sys.stdin.read().strip()))
replace
5
6
5
6
TypeError: count() takes at least 1 argument (0 given)
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02838/Python/s739463429.py", line 15, in <module> print(solve(sys.stdin.read().strip())) File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02838/Python/s739463429.py", line 6, in solve for s in map(str.count("1"), zip(*a)): TypeError: count() takes at least 1 argument (0 given)
p02838
Python
Time Limit Exceeded
#!/usr/bin/env python3 n, *a = map(int, open(0).read().split()) b = [0] * 60 ans = 0 for i in range(n): for j in range(60): ans += 2**j * (i - b[j] if a[i] >> j & 1 else b[j]) ans %= 10**9 + 7 for j in range(len(bin(a[i])) - 2): b[j] += a[i] >> j & 1 print(ans)
#!/usr/bin/env python3 n, *a = map(int, open(0).read().split()) b = [0] * 60 ans = 0 for i in range(n): for j in range(60): ans += (i - b[j] if a[i] >> j & 1 else b[j]) << j ans %= 10**9 + 7 for j in range(60): b[j] += a[i] >> j & 1 print(ans)
replace
6
9
6
9
TLE
p02838
Python
Runtime Error
import numpy as np N = int(input()) A = np.fromstring(input(), np.int64, " ") result = 0 for bit in range(60): c = int((A & 1).sum()) A >>= 1 result = (result + c * (N - c) * (1 << bit)) % 1000000007 print(result)
import numpy as np N = int(input()) A = np.fromstring(input(), dtype=np.int64, sep=" ") result = 0 for bit in range(60): c = int((A & 1).sum()) A >>= 1 result = (result + c * (N - c) * (1 << bit)) % 1000000007 print(result)
replace
3
4
3
4
TLE
p02838
Python
Time Limit Exceeded
def main(): MOD = 10**9 + 7 N = int(input()) (*a,) = map(int, input().split()) b = [0] * 60 for x in a: for j in range(60): b[j] += (x & 1) > 0 x >>= 1 t = 1 ans = 0 for j in range(60): ans = (ans + b[j] * (N - b[j]) * t) % MOD t <<= 1 print(ans) if __name__ == "__main__": main() # import sys # # sys.setrecursionlimit(10 ** 7) # # input = sys.stdin.readline # rstrip() # int(input()) # map(int, input().split())
def main(): MOD = 10**9 + 7 N = int(input()) (*a,) = map(int, input().split()) b = [0] * 60 for x in a: for j in range(60): b[j] += (x & 1) > 0 x >>= 1 t = 1 ans = 0 for j in range(60): ans = (ans + b[j] * (N - b[j]) * t) % MOD t <<= 1 print(ans) if __name__ == "__main__": main()
delete
22
31
22
22
TLE
p02838
Python
Time Limit Exceeded
MOD = 10**9 + 7 N = int(input()) A = list(map(int, input().split())) ans = 0 for i in range(N - 1): for j in range(i + 1, N): ans += A[i] ^ A[j] ans %= MOD print(ans)
MOD = 10**9 + 7 N = int(input()) A = list(map(int, input().split())) ans = 0 m = 1 for i in range(60): bits = sum((a & m for a in A)) bits //= m ans += bits * (N - bits) * m ans %= MOD m <<= 1 print(ans % MOD)
replace
5
10
5
13
TLE
p02838
Python
Time Limit Exceeded
n, *a = map(int, open(0).read().split()) # すべての組み合わせ # あるビットについて、そのビットが立つ回数 mod = 10**9 + 7 ans = 0 for i in range(60): cnt = 0 for j in range(n): cnt += a[j] >> i & 1 ans = (ans + (1 << i) * cnt * (n - cnt) % mod) % mod print(ans)
n, *a = map(int, open(0).read().split()) mod = 10**9 + 7 ans = 0 for i in range(60): cnt = 0 for j in range(n): cnt += a[j] >> i & 1 ans = (ans + (1 << i) * cnt * (n - cnt) % mod) % mod print(ans)
delete
1
3
1
1
TLE
p02838
Python
Time Limit Exceeded
import numpy as np N = int(input()) A = np.array(list(map(int, input().split()))) ans = 0 MOD = 10**9 + 7 for digit in range(60): one = sum((A >> digit) & 1) ans += (one * (N - one) * pow(2, digit, MOD)) % MOD ans %= MOD print(ans)
import numpy as np N = int(input()) A = np.array(list(map(int, input().split()))) ans = 0 MOD = 10**9 + 7 for digit in range(60): one = np.count_nonzero((A >> digit) & 1) ans += (one * (N - one) * pow(2, digit, MOD)) % MOD ans %= MOD print(ans)
replace
8
9
8
9
TLE
p02838
Python
Time Limit Exceeded
from collections import Counter import sys input = sys.stdin.readline def main(): mod = 10**9 + 7 n = int(input()) a = list(map(int, input().split())) ans = 0 a_dict = Counter(a) for i in range(n): a_dict[a[i]] -= 1 for j in a_dict: ans += a_dict[j] * (a[i] ^ j) print(ans % mod) if __name__ == "__main__": main()
n = int(input()) a = map(int, input().split()) mod = 10**9 + 7 p = [0] * 60 for a_ in a: for i in range(60): p[i] += (a_ >> i) & 1 ans = 0 for i in range(60): sn = (n - p[i]) * p[i] ans = (ans + (sn << i)) % mod print(ans)
replace
0
21
0
12
TLE
p02838
Python
Time Limit Exceeded
import itertools MOD = 10**9 + 7 N = int(input()) A = [int(i) for i in input().split()] def main(): ans = sum([a ^ b for (a, b) in itertools.combinations(A, 2)]) % MOD print(ans) if __name__ == "__main__": main()
import itertools MOD = 10**9 + 7 N = int(input()) A = [int(i) for i in input().split()] def main(): count_1 = [0] * 61 for i in range(60): tmp = 0 for j, a in enumerate(A): tmp += (a >> i) & 1 count_1[i] += tmp ans = 0 for i, c1 in enumerate(count_1): c0 = len(A) - c1 ans += c0 * c1 * 2**i ans %= MOD print(ans) if __name__ == "__main__": main()
replace
8
9
8
19
TLE
p02838
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define endl '\n' template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(long long x_) { if ((x = x_ % mod + mod) >= mod) x -= mod; } ModInt &operator+=(ModInt rhs) { if ((x += rhs.x) >= mod) x -= mod; return *this; } ModInt &operator-=(ModInt rhs) { if ((x -= rhs.x) < 0) x += mod; return *this; } ModInt &operator*=(ModInt rhs) { x = (unsigned long long)x * rhs.x % mod; return *this; } ModInt &operator/=(ModInt rhs) { x = (unsigned long long)x * rhs.inv().x % mod; return *this; } ModInt operator-() const { return -x < 0 ? mod - x : -x; } ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; } ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; } ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; } ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; } bool operator==(ModInt rhs) const { return x == rhs.x; } bool operator!=(ModInt rhs) const { return x != rhs.x; } ModInt inv() const { return pow(*this, mod - 2); } friend ostream &operator<<(ostream &s, ModInt<mod> a) { s << a.x; return s; } friend istream &operator>>(istream &s, ModInt<mod> &a) { s >> a.x; return s; } }; using mint = ModInt<(int)1e9 + 7>; int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); long long n; mint ans = 0; cin >> n; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; for (long long i = 0; i < 60LL; i++) { long long cnt1 = 0, cnt0 = 0; for (auto &mask : v) { if (mask >> i & 1) cnt1++; else cnt0++; } mint total = (1LL << i); ans += (total * cnt1 * cnt0); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define endl '\n' template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(long long x_) { if ((x = x_ % mod + mod) >= mod) x -= mod; } ModInt &operator+=(ModInt rhs) { if ((x += rhs.x) >= mod) x -= mod; return *this; } ModInt &operator-=(ModInt rhs) { if ((x -= rhs.x) < 0) x += mod; return *this; } ModInt &operator*=(ModInt rhs) { x = (unsigned long long)x * rhs.x % mod; return *this; } ModInt &operator/=(ModInt rhs) { x = (unsigned long long)x * rhs.inv().x % mod; return *this; } ModInt operator-() const { return -x < 0 ? mod - x : -x; } ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; } ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; } ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; } ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; } bool operator==(ModInt rhs) const { return x == rhs.x; } bool operator!=(ModInt rhs) const { return x != rhs.x; } ModInt inv() const { return pow(*this, mod - 2); } friend ostream &operator<<(ostream &s, ModInt<mod> a) { s << a.x; return s; } friend istream &operator>>(istream &s, ModInt<mod> &a) { s >> a.x; return s; } }; using mint = ModInt<(int)1e9 + 7>; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); long long n; mint ans = 0; cin >> n; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; for (long long i = 0; i < 60LL; i++) { long long cnt1 = 0, cnt0 = 0; for (auto &mask : v) { if (mask >> i & 1) cnt1++; else cnt0++; } mint total = (1LL << i); ans += (total * cnt1 * cnt0); } cout << ans << endl; }
delete
48
52
48
48
0
p02838
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define MAX 1000001 #define pb push_back #define mk make_pair #define ll long long #define pii pair<int, int> ll M = 1000000000 + 7; ll sum[200005][65]; ll pot[63]; int main() { pot[0] = 1LL; for (int i = 1; i < 62; i++) { pot[i] = (pot[i - 1] * 2LL) % M; } // ios_base::sync_with_stdio(false); // cin.tie(0); int n; cin >> n; vector<ll> arr; ll temp; for (int i = 0; i < n; i++) { cin >> temp; arr.pb(temp); } for (int i = 0; i < n; i++) { vector<int> many; for (int x = 0; x < 61; x++) { if (((1LL << x) & arr[i]) == (1LL << x)) { many.pb(1LL); } else { many.pb(0); } } for (int x = 0; x < 61; x++) { if (i != 0) { sum[i][x] = sum[i - 1][x] + many[x]; } else { sum[i][x] = many[x]; } } } ll ans = 0; for (int i = 1; i < n; i++) { for (int x = 0; x < 61; x++) { // cout << "sum[i][x] = " << i << " " << x << " " << sum[i][x] << endl; if (((1LL << x) & arr[i]) == (1LL << x)) { ll times = (1LL * i) - sum[i - 1][x]; ll add = (times * pot[x]) % M; // cout << "times = " << times << endl; // cout << "add= " << add << endl; ans = (ans + add) % M; } else { ll times = sum[i - 1][x]; ll add = (times * pot[x]) % M; // cout << "times = " << times << endl; // cout << "add= " << add << endl; ans = (ans + add) % M; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define MAX 1000001 #define pb push_back #define mk make_pair #define ll long long #define pii pair<int, int> ll M = 1000000000 + 7; ll sum[300005][65]; ll pot[63]; int main() { pot[0] = 1LL; for (int i = 1; i < 62; i++) { pot[i] = (pot[i - 1] * 2LL) % M; } // ios_base::sync_with_stdio(false); // cin.tie(0); int n; cin >> n; vector<ll> arr; ll temp; for (int i = 0; i < n; i++) { cin >> temp; arr.pb(temp); } for (int i = 0; i < n; i++) { vector<int> many; for (int x = 0; x < 61; x++) { if (((1LL << x) & arr[i]) == (1LL << x)) { many.pb(1LL); } else { many.pb(0); } } for (int x = 0; x < 61; x++) { if (i != 0) { sum[i][x] = sum[i - 1][x] + many[x]; } else { sum[i][x] = many[x]; } } } ll ans = 0; for (int i = 1; i < n; i++) { for (int x = 0; x < 61; x++) { // cout << "sum[i][x] = " << i << " " << x << " " << sum[i][x] << endl; if (((1LL << x) & arr[i]) == (1LL << x)) { ll times = (1LL * i) - sum[i - 1][x]; ll add = (times * pot[x]) % M; // cout << "times = " << times << endl; // cout << "add= " << add << endl; ans = (ans + add) % M; } else { ll times = sum[i - 1][x]; ll add = (times * pot[x]) % M; // cout << "times = " << times << endl; // cout << "add= " << add << endl; ans = (ans + add) % M; } } } cout << ans << endl; return 0; }
replace
10
11
10
11
127
/tmp/09f76a7b-374c-43cf-b420-2f757c70abf0.out: error while loading shared libraries: libstdc++.so.6: failed to map segment from shared object
p02838
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; // template<typename T> using ordered_set = tree<T, null_type, less<T>, // rb_tree_tag, tree_order_statistics_node_update>; // class Timer { // clock_t start; // string name; // public: // Timer() {name = "";start = clock();} // Timer(string s){name = s;start = clock();} // ~Timer() {fprintf(stderr, "%s: %.3gs\n", name.c_str(), 1.0*(clock() - // start) / CLOCKS_PER_SEC);} // }; // #define isValid(a, b) ((a) >= 0 && (a) < (b)) // int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; // int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; #define all(x) (x).begin(), (x).end() #define pb push_back #define eb emplace_back #define um unordered_map #define us unordered_set #define endl '\n' #define what_is(x) \ cerr << fixed << setprecision(2) << boolalpha << #x << " = " << x << endl const double EPS = 1e-9; const double PI = acos(-1.0); template <typename T> inline T sq(T a) { return a * a; } template <typename T1, typename T2> inline pair<T1, T2> mp(T1 a, T2 b) { return make_pair(a, b); } template <typename T1, typename T2> inline T1 safeMod(T1 a, T2 m) { return (a % m + m) % m; } /*handling negative sign of remainder*/ template <typename T1, typename T2> inline bool isEq(T1 a, T2 b) { return abs(a - b) < EPS; } template <typename T1, typename T2, typename T3> inline bool isEq(T1 a, T2 b, T3 eps) { return abs(a - b) < eps; } template <typename T> inline bool isKthBitOn(T n, int k) { assert(n <= numeric_limits<T>::max()); assert(k <= numeric_limits<T>::digits); T ONE = 1; return bool((n & (ONE << k))); } template <typename T> inline void setKthBit(T &n, int k) { assert(n <= numeric_limits<T>::max()); assert(k <= numeric_limits<T>::digits); T ONE = 1; n = (n | (ONE << k)); } // mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count()); // template<typename T> inline T nextRand(T lo, T hi){return // uniform_int_distribution<T>(lo, hi)(rng);} #define toRad(deg) ((deg)*PI / 180.0) #define toDeg(rad) ((rad)*180.0 / PI) typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; const int oo = 0x3f3f3f3f; const int MAX = 200010; const int MOD = 1000000007; const int precision = 10; int sum[MAX][64]; ll binexp(ll b, int p) { ll ret = 1; while (p) { if (p & 1) ret = (ret * b) % MOD; b = (b * b) % MOD; p >>= 1; } return ret; } int main() { // assert(freopen("in.txt", "r", stdin)); // assert(freopen("out.txt", "w", stdout)); ios_base::sync_with_stdio(false); cin.tie(NULL); // cout << fixed << setprecision(precision); int n; cin >> n; vector<ll> v(n); for (ll &x : v) cin >> x; for (int i = 0; i < n; i++) { for (int j = 0; j < 62; j++) { if (isKthBitOn(v[i], j)) sum[i][j] = 1; } } vector<int> bitsum(62); for (int j = 0; j < 62; j++) { int ss = 0; for (int i = 0; i < n; i++) { ss += sum[i][j]; } bitsum[j] = ss; } ll ans = 0; ll div = binexp(2, MOD - 2); for (int i = 0; i < n; i++) { for (int j = 0; j < 62; j++) { if (isKthBitOn(v[i], j)) { ans = (ans + (1LL << j) % MOD * (n - bitsum[j]) % MOD) % MOD; } else { ans = (ans + (1LL << j) % MOD * bitsum[j] % MOD) % MOD; } } } cout << ans * div % MOD << endl; return 0; }
#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; // template<typename T> using ordered_set = tree<T, null_type, less<T>, // rb_tree_tag, tree_order_statistics_node_update>; // class Timer { // clock_t start; // string name; // public: // Timer() {name = "";start = clock();} // Timer(string s){name = s;start = clock();} // ~Timer() {fprintf(stderr, "%s: %.3gs\n", name.c_str(), 1.0*(clock() - // start) / CLOCKS_PER_SEC);} // }; // #define isValid(a, b) ((a) >= 0 && (a) < (b)) // int dr[] = {0, -1, -1, -1, 0, 1, 1, 1}; // int dc[] = {1, 1, 0, -1, -1, -1, 0, 1}; #define all(x) (x).begin(), (x).end() #define pb push_back #define eb emplace_back #define um unordered_map #define us unordered_set #define endl '\n' #define what_is(x) \ cerr << fixed << setprecision(2) << boolalpha << #x << " = " << x << endl const double EPS = 1e-9; const double PI = acos(-1.0); template <typename T> inline T sq(T a) { return a * a; } template <typename T1, typename T2> inline pair<T1, T2> mp(T1 a, T2 b) { return make_pair(a, b); } template <typename T1, typename T2> inline T1 safeMod(T1 a, T2 m) { return (a % m + m) % m; } /*handling negative sign of remainder*/ template <typename T1, typename T2> inline bool isEq(T1 a, T2 b) { return abs(a - b) < EPS; } template <typename T1, typename T2, typename T3> inline bool isEq(T1 a, T2 b, T3 eps) { return abs(a - b) < eps; } template <typename T> inline bool isKthBitOn(T n, int k) { assert(n <= numeric_limits<T>::max()); assert(k <= numeric_limits<T>::digits); T ONE = 1; return bool((n & (ONE << k))); } template <typename T> inline void setKthBit(T &n, int k) { assert(n <= numeric_limits<T>::max()); assert(k <= numeric_limits<T>::digits); T ONE = 1; n = (n | (ONE << k)); } // mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count()); // template<typename T> inline T nextRand(T lo, T hi){return // uniform_int_distribution<T>(lo, hi)(rng);} #define toRad(deg) ((deg)*PI / 180.0) #define toDeg(rad) ((rad)*180.0 / PI) typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; const int oo = 0x3f3f3f3f; const int MAX = 300010; const int MOD = 1000000007; const int precision = 10; int sum[MAX][64]; ll binexp(ll b, int p) { ll ret = 1; while (p) { if (p & 1) ret = (ret * b) % MOD; b = (b * b) % MOD; p >>= 1; } return ret; } int main() { // assert(freopen("in.txt", "r", stdin)); // assert(freopen("out.txt", "w", stdout)); ios_base::sync_with_stdio(false); cin.tie(NULL); // cout << fixed << setprecision(precision); int n; cin >> n; vector<ll> v(n); for (ll &x : v) cin >> x; for (int i = 0; i < n; i++) { for (int j = 0; j < 62; j++) { if (isKthBitOn(v[i], j)) sum[i][j] = 1; } } vector<int> bitsum(62); for (int j = 0; j < 62; j++) { int ss = 0; for (int i = 0; i < n; i++) { ss += sum[i][j]; } bitsum[j] = ss; } ll ans = 0; ll div = binexp(2, MOD - 2); for (int i = 0; i < n; i++) { for (int j = 0; j < 62; j++) { if (isKthBitOn(v[i], j)) { ans = (ans + (1LL << j) % MOD * (n - bitsum[j]) % MOD) % MOD; } else { ans = (ans + (1LL << j) % MOD * bitsum[j] % MOD) % MOD; } } } cout << ans * div % MOD << endl; return 0; }
replace
77
78
77
78
0
p02838
C++
Runtime Error
#include "bits/stdc++.h" #pragma GCC optimize "03" using namespace std; #define int long long int #define pb push_back #define pii pair<int, int> #define fi first #define se second #define rep(i, a, b) for (int i = a; i < b; ++i) #define IOS \ ios::sync_with_stdio(false); \ cin.tie(NULL); #ifndef LOCAL #define dbg(...) ; #define endl '\n' #endif const int inf = 1e15; const int MOD = 1e9 + 7; const int N = 2e5 + 5; int a[N], sum[N]; signed main() { IOS; int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; int ans = 0; for (int i = 0; i < 60; i++) { int o = 0, z = 0; for (int j = 1; j <= n; j++) { if (a[j] & (1ll << i)) o++; else z++; } ans += ((o * z) % MOD * ((1ll << i) % MOD)) % MOD; ans %= MOD; } cout << ans << endl; return 0; }
#include "bits/stdc++.h" #pragma GCC optimize "03" using namespace std; #define int long long int #define pb push_back #define pii pair<int, int> #define fi first #define se second #define rep(i, a, b) for (int i = a; i < b; ++i) #define IOS \ ios::sync_with_stdio(false); \ cin.tie(NULL); #ifndef LOCAL #define dbg(...) ; #define endl '\n' #endif const int inf = 1e15; const int MOD = 1e9 + 7; const int N = 3e5 + 5; int a[N], sum[N]; signed main() { IOS; int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; int ans = 0; for (int i = 0; i < 60; i++) { int o = 0, z = 0; for (int j = 1; j <= n; j++) { if (a[j] & (1ll << i)) o++; else z++; } ans += ((o * z) % MOD * ((1ll << i) % MOD)) % MOD; ans %= MOD; } cout << ans << endl; return 0; }
replace
20
21
20
21
0
p02838
C++
Time Limit Exceeded
#include <iostream> using namespace std; long a[300010], s[64][300010][2]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; for (int j = 0; j < 61; ++j) { int k = (a[i] >> j) & 1; if (k == 0) { s[j][i + 1][0] = s[j][i][0] + 1; s[j][i + 1][1] = s[j][i][1]; } else { s[j][i + 1][0] = s[j][i][0]; s[j][i + 1][1] = s[j][i][1] + 1; } } } const long mod = 1e9 + 7; long res = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < 61; ++j) { long t = 0; int x = (a[i] >> j) & 1; t += s[j][n][!x] - s[j][i][!x]; t %= mod; for (int k = 0; k < j; ++k) (t *= 2) %= mod; (res += t) %= mod; } } cout << res << endl; }
#include <iostream> using namespace std; long a[300010], s[64][300010][2]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; for (int j = 0; j < 61; ++j) { int k = (a[i] >> j) & 1; if (k == 0) { s[j][i + 1][0] = s[j][i][0] + 1; s[j][i + 1][1] = s[j][i][1]; } else { s[j][i + 1][0] = s[j][i][0]; s[j][i + 1][1] = s[j][i][1] + 1; } } } const long mod = 1e9 + 7; long res = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < 61; ++j) { long t = 0; int x = (a[i] >> j) & 1; t += s[j][n][!x] - s[j][i][!x]; t %= mod; (t *= (1L << j) % mod) %= mod; (res += t) %= mod; } } cout << res << endl; }
replace
29
31
29
30
TLE
p02838
C++
Runtime Error
// #include <bits/stdc++.h> #include <algorithm> #include <cmath> #include <iomanip> #include <ios> #include <iostream> #include <map> #include <numeric> #include <queue> #include <stack> // #include <windows.h> using namespace std; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = V<VV<T>>; template <class T1, class T2> using P = pair<T1, T2>; using I = int; using D = double; using B = bool; using C = char; using S = string; using LL = long long; using LD = long double; using ULL = unsigned long long; using PII = P<I, I>; using VPII = V<PII>; using PLL = P<LL, LL>; using VPLL = V<PLL>; using VI = V<I>; using VVI = VV<I>; using VLL = V<LL>; using VVLL = VV<LL>; using VC = V<C>; using VVC = VV<C>; using VS = V<S>; using VVS = VV<S>; using VB = V<B>; using VVB = VV<B>; #define REP(type, i, n) for (type i = 0; i < (type)(n); ++i) #define REP2(type, i, m, n) for (type i = (m); i < (type)(n); ++i) #define REPR(type, i, n) for (type i = (n)-1; i >= 0; --i) #define REPR2(type, i, m, n) for (type i = (n)-1; i >= (m); --i) #define REPx(x, a) for (auto x : a) #define ALL(a) a.begin(), a.end() #define SORT(a) sort(ALL(a)) #define SORTR(a, type) sort(ALL(a), greater<type>()) #define SORTF(a, comp) sort(ALL(a), comp) #define REVERSE(a) reverse(ALL(a)) #define SIZE(a, type) ((type)(a).size()) #define bit_search(bit, n) REP(LL, bit, 1LL << (n)) #define bit_check(bit, i) ((bit >> (i)) & 1) #define setpre(n) fixed << setprecision((n)) #define UNIQUE(a) \ do { \ SORT(a); \ (a).erase(unique(ALL(a)), (a).end()); \ } while (0) #define MAX(a) *max_element(ALL(a)) #define MIN(a) *min_element(ALL(a)) #define bisect_left(a, x) lower_bound(ALL(a), (x)) - a.begin() #define bisect_right(a, x) upper_bound(ALL(a), (x)) - a.begin() #define INPUT(a) REPx(&x, a) cin >> x; #define INPUT2(a) REPx(&x, a) INPUT(x); #define INPUTP(a) REPx(&x, a) cin >> x.first >> x.second; #define ENDL cout << endl; const int INF = 2e9; // const LL INF = 9e18; const LL MOD = 1e9 + 7; template <class T> using PRIORITY_QUEUE = priority_queue<T, V<T>, greater<T>>; 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; } template <class T> inline void debug1(V<T> A) { REP(int, i, SIZE(A, int)) { if (A[i] == INF) cout << "I "; else cout << A[i] << " "; } ENDL } template <class T> inline void debug2(VV<T> A) { REP(int, i, SIZE(A, int)) { REP(int, j, SIZE(A[i], int)) { if (A[i][j] == INF) cout << "I "; else cout << A[i][j] << " "; } ENDL } } template <class T> string D2BC(T d) { string res = ""; while (d > 0) { if (d & 1) res += "1"; else res += "0"; d >>= 1; } REVERSE(res); return res; } // modをとる整数型 const int mod = 1e9 + 7; struct mint { LL x; mint(LL _x = 0) : x((_x % mod + mod) % mod) {} mint operator-() const { mint tmp(-x); return tmp; } mint &operator+=(const mint &a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint &a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint &a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint &a) const { mint tmp(x); tmp += a.x; return tmp; } mint operator-(const mint &a) const { mint tmp(x); tmp -= a.x; return tmp; } mint operator*(const mint &a) const { mint tmp(x); tmp *= a.x; return tmp; } mint pow(LL b) const { mint res(1), a = *this; while (b > 0) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; } mint inv() const { return pow(mod - 2); } mint &operator/=(const mint &a) { return *this *= a.inv(); } mint operator/(const mint &a) const { mint tmp(x); tmp /= a; return tmp; } }; istream &operator>>(istream &is, mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; VLL A(n); INPUT(A); int digit = int(log2(MAX(A))) + 1; VVI ct_1(n + 1, VI(digit, 0)), ct_0(n + 1, VI(digit, 1)); REP(int, i, digit) ct_0[0][i] = 0; REP2(int, i, 1, n + 1) { VI tmp_1(digit, 0), tmp_0(digit, 1); LL j = A[i - 1], c = 0; while (j) { if (j & 1) { ++tmp_1[c]; --tmp_0[c]; } j >>= 1; ++c; } REP(int, j, digit) { ct_1[i][j] = tmp_1[j] + ct_1[i - 1][j]; ct_0[i][j] = tmp_0[j] + ct_0[i - 1][j]; } } mint ans = 0; REP2(int, i, 1, n) { VI tmp_1(digit), tmp_0(digit); REP(int, j, digit) { tmp_1[j] = ct_1[n][j] - ct_1[i][j]; tmp_0[j] = ct_0[n][j] - ct_0[i][j]; } LL j = A[i - 1], c = 0; while (j) { if (j & 1) tmp_1[c] = tmp_0[c]; j >>= 1; ++c; } REP(int, i, digit) { mint x = 2; ans += x.pow(i) * tmp_1[i]; } } cout << ans << endl; return 0; }
// #include <bits/stdc++.h> #include <algorithm> #include <cmath> #include <iomanip> #include <ios> #include <iostream> #include <map> #include <numeric> #include <queue> #include <stack> // #include <windows.h> using namespace std; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = V<VV<T>>; template <class T1, class T2> using P = pair<T1, T2>; using I = int; using D = double; using B = bool; using C = char; using S = string; using LL = long long; using LD = long double; using ULL = unsigned long long; using PII = P<I, I>; using VPII = V<PII>; using PLL = P<LL, LL>; using VPLL = V<PLL>; using VI = V<I>; using VVI = VV<I>; using VLL = V<LL>; using VVLL = VV<LL>; using VC = V<C>; using VVC = VV<C>; using VS = V<S>; using VVS = VV<S>; using VB = V<B>; using VVB = VV<B>; #define REP(type, i, n) for (type i = 0; i < (type)(n); ++i) #define REP2(type, i, m, n) for (type i = (m); i < (type)(n); ++i) #define REPR(type, i, n) for (type i = (n)-1; i >= 0; --i) #define REPR2(type, i, m, n) for (type i = (n)-1; i >= (m); --i) #define REPx(x, a) for (auto x : a) #define ALL(a) a.begin(), a.end() #define SORT(a) sort(ALL(a)) #define SORTR(a, type) sort(ALL(a), greater<type>()) #define SORTF(a, comp) sort(ALL(a), comp) #define REVERSE(a) reverse(ALL(a)) #define SIZE(a, type) ((type)(a).size()) #define bit_search(bit, n) REP(LL, bit, 1LL << (n)) #define bit_check(bit, i) ((bit >> (i)) & 1) #define setpre(n) fixed << setprecision((n)) #define UNIQUE(a) \ do { \ SORT(a); \ (a).erase(unique(ALL(a)), (a).end()); \ } while (0) #define MAX(a) *max_element(ALL(a)) #define MIN(a) *min_element(ALL(a)) #define bisect_left(a, x) lower_bound(ALL(a), (x)) - a.begin() #define bisect_right(a, x) upper_bound(ALL(a), (x)) - a.begin() #define INPUT(a) REPx(&x, a) cin >> x; #define INPUT2(a) REPx(&x, a) INPUT(x); #define INPUTP(a) REPx(&x, a) cin >> x.first >> x.second; #define ENDL cout << endl; const int INF = 2e9; // const LL INF = 9e18; const LL MOD = 1e9 + 7; template <class T> using PRIORITY_QUEUE = priority_queue<T, V<T>, greater<T>>; 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; } template <class T> inline void debug1(V<T> A) { REP(int, i, SIZE(A, int)) { if (A[i] == INF) cout << "I "; else cout << A[i] << " "; } ENDL } template <class T> inline void debug2(VV<T> A) { REP(int, i, SIZE(A, int)) { REP(int, j, SIZE(A[i], int)) { if (A[i][j] == INF) cout << "I "; else cout << A[i][j] << " "; } ENDL } } template <class T> string D2BC(T d) { string res = ""; while (d > 0) { if (d & 1) res += "1"; else res += "0"; d >>= 1; } REVERSE(res); return res; } // modをとる整数型 const int mod = 1e9 + 7; struct mint { LL x; mint(LL _x = 0) : x((_x % mod + mod) % mod) {} mint operator-() const { mint tmp(-x); return tmp; } mint &operator+=(const mint &a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint &a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint &a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint &a) const { mint tmp(x); tmp += a.x; return tmp; } mint operator-(const mint &a) const { mint tmp(x); tmp -= a.x; return tmp; } mint operator*(const mint &a) const { mint tmp(x); tmp *= a.x; return tmp; } mint pow(LL b) const { mint res(1), a = *this; while (b > 0) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; } mint inv() const { return pow(mod - 2); } mint &operator/=(const mint &a) { return *this *= a.inv(); } mint operator/(const mint &a) const { mint tmp(x); tmp /= a; return tmp; } }; istream &operator>>(istream &is, mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; VLL A(n); INPUT(A); int digit; if (MAX(A) == 0) digit = 1; else digit = int(log2(MAX(A))) + 1; VVI ct_1(n + 1, VI(digit, 0)), ct_0(n + 1, VI(digit, 1)); REP(int, i, digit) ct_0[0][i] = 0; REP2(int, i, 1, n + 1) { VI tmp_1(digit, 0), tmp_0(digit, 1); LL j = A[i - 1], c = 0; while (j) { if (j & 1) { ++tmp_1[c]; --tmp_0[c]; } j >>= 1; ++c; } REP(int, j, digit) { ct_1[i][j] = tmp_1[j] + ct_1[i - 1][j]; ct_0[i][j] = tmp_0[j] + ct_0[i - 1][j]; } } mint ans = 0; REP2(int, i, 1, n) { VI tmp_1(digit), tmp_0(digit); REP(int, j, digit) { tmp_1[j] = ct_1[n][j] - ct_1[i][j]; tmp_0[j] = ct_0[n][j] - ct_0[i][j]; } LL j = A[i - 1], c = 0; while (j) { if (j & 1) tmp_1[c] = tmp_0[c]; j >>= 1; ++c; } REP(int, i, digit) { mint x = 2; ans += x.pow(i) * tmp_1[i]; } } cout << ans << endl; return 0; }
replace
189
190
189
194
0
p02838
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long const int mod = 1e9 + 7; int n; int a[100001]; int cnt[201][2]; signed main() { scanf("%lld", &n); long long ans = 0; for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); for (int j = 0; j <= 60; j++) ans += 1ll * (1ll << j) % mod * cnt[j][((a[i] >> j) & 1) ^ 1] % mod, ans %= mod; for (int j = 0; j <= 60; j++) cnt[j][((a[i] >> j) & 1)]++; } printf("%lld", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long const int mod = 1e9 + 7; int n; int a[1000001]; int cnt[201][2]; signed main() { scanf("%lld", &n); long long ans = 0; for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); for (int j = 0; j <= 60; j++) ans += 1ll * (1ll << j) % mod * cnt[j][((a[i] >> j) & 1) ^ 1] % mod, ans %= mod; for (int j = 0; j <= 60; j++) cnt[j][((a[i] >> j) & 1)]++; } printf("%lld", ans); return 0; }
replace
5
6
5
6
0
p02838
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; void solve(long long N, std::vector<long long> A) { const long long MOD = 1000000007; long long maxA = *max_element(A.begin(), A.end()); long long k = floor(log2(maxA)) + 1; // cout << "k " << k << endl; long long res = 0; for (size_t j = 0; j < k; j++) { long long n_ones = 0; for (size_t i = 0; i < N; i++) { n_ones += (A[i] >> j) & (1); } long long res_k = n_ones * (N - n_ones); for (size_t l = 0; l < j; l++) { res_k = (2 * res_k) % MOD; } res += res_k; } cout << (res % MOD) << endl; } // Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You // use the default template now. You can remove this line by using your custom // template) int main() { long long N; scanf("%lld", &N); std::vector<long long> A(N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); } solve(N, std::move(A)); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; void solve(long long N, std::vector<long long> A) { const long long MOD = 1000000007; long long maxA = *max_element(A.begin(), A.end()); long long k = floor(log2(maxA)) + 1; // cout << "k " << k << endl; long long res = 0; for (size_t j = 0; j < 60; j++) { long long n_ones = 0; for (size_t i = 0; i < N; i++) { n_ones += (A[i] >> j) & (1); } long long res_k = n_ones * (N - n_ones); for (size_t l = 0; l < j; l++) { res_k = (2 * res_k) % MOD; } res += res_k; } cout << (res % MOD) << endl; } // Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You // use the default template now. You can remove this line by using your custom // template) int main() { long long N; scanf("%lld", &N); std::vector<long long> A(N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); } solve(N, std::move(A)); return 0; }
replace
13
14
13
14
TLE
p02838
C++
Runtime Error
// IOI 2021 #include <bits/stdc++.h> using namespace std; #define endl '\n' #define ends ' ' #define die(x) return cout << x << endl, 0 #define all(v) v.begin(), v.end() #define sz(x) (int)(x.size()) #define debug(x) cerr << #x << ": " << x << endl #define debugP(p) \ cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll INF = 1e9; const ll MOD = 1e9 + 7; //////////////////////////////////////////////////////////////////// const int N = 3e5 + 5, LOG = 6e1 + 1; int A[LOG]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { ll x; cin >> x; for (int j = 0; j < LOG; j++) A[j] += (x >> j) & 1; } int ans = 0; for (int i = 0; i < LOG; i++) ans = (ans + 1LL * A[i] * (n - A[i]) % MOD * (1LL << i) % MOD) % MOD; if (ans < 0) assert(0); cout << ans << endl; return 0; }
// IOI 2021 #include <bits/stdc++.h> using namespace std; #define endl '\n' #define ends ' ' #define die(x) return cout << x << endl, 0 #define all(v) v.begin(), v.end() #define sz(x) (int)(x.size()) #define debug(x) cerr << #x << ": " << x << endl #define debugP(p) \ cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll INF = 1e9; const ll MOD = 1e9 + 7; //////////////////////////////////////////////////////////////////// const int N = 3e5 + 5, LOG = 6e1 + 1; int A[LOG]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { ll x; cin >> x; for (int j = 0; j < LOG; j++) A[j] += (x >> j) & 1; } int ans = 0; for (int i = 0; i < LOG; i++) ans = (ans + (1LL * A[i] * (n - A[i]) % MOD) * ((1LL << i) % MOD) % MOD) % MOD; cout << ans << endl; return 0; }
replace
40
43
40
42
0
p02838
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define sz(a) int((a).size()) #define endl '\n' using namespace std; typedef vector<int> vi; typedef pair<int, int> ii; typedef long long ll; const int maxn = 1e5 + 10; const int mod = 1e9 + 7; ll v[maxn]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif int n; ll ans = 0; cin >> n; for (int i = 0; i < n; ++i) { cin >> v[i]; } for (int i = 0; i < 60; ++i) { ll cnt = 0; for (int j = 0; j < n; ++j) { if ((v[j] & (1LL << i))) cnt++; } ll one_bit = (cnt * (n - cnt)) % mod; ll to_dec = (((1LL << i) % mod) * one_bit) % mod; ans = (ans + to_dec) % mod; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define pb push_back #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define sz(a) int((a).size()) #define endl '\n' using namespace std; typedef vector<int> vi; typedef pair<int, int> ii; typedef long long ll; const int maxn = 3e5 + 10; const int mod = 1e9 + 7; ll v[maxn]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif int n; ll ans = 0; cin >> n; for (int i = 0; i < n; ++i) { cin >> v[i]; } for (int i = 0; i < 60; ++i) { ll cnt = 0; for (int j = 0; j < n; ++j) { if ((v[j] & (1LL << i))) cnt++; } ll one_bit = (cnt * (n - cnt)) % mod; ll to_dec = (((1LL << i) % mod) * one_bit) % mod; ans = (ans + to_dec) % mod; } cout << ans << endl; return 0; }
replace
15
16
15
16
0
p02838
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 200100; const ll MOD = 1e9 + 7; int n; ll arr[MAXN], ans; ll dig[100]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &arr[i]); for (int i = 1; i <= n; i++) { for (int j = 0; j <= 61; j++) dig[j] += (bool)(arr[i] & (1ll << j)); } for (int i = 0; i <= 61; i++) ans = (ans + (((dig[i] * (n - dig[i])) % MOD) * ((1ll << i) % MOD)) % MOD) % MOD; printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 300100; const ll MOD = 1e9 + 7; int n; ll arr[MAXN], ans; ll dig[100]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &arr[i]); for (int i = 1; i <= n; i++) { for (int j = 0; j <= 61; j++) dig[j] += (bool)(arr[i] & (1ll << j)); } for (int i = 0; i <= 61; i++) ans = (ans + (((dig[i] * (n - dig[i])) % MOD) * ((1ll << i) % MOD)) % MOD) % MOD; printf("%lld\n", ans); return 0; }
replace
3
4
3
4
0
p02838
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++) #define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++) #define rrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--) #define all(x) (x).begin(), (x).end() #define sz(x) ((ll)(x).size()) #define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end()) struct mint { const int mod = 1e9 + 7; long long x; mint(long long x = 0) : x((x % mod + mod) % mod) {} mint &operator=(const long long a) { x = a % mod; return *this; } mint &operator=(const mint a) { x = a.x % mod; return *this; } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint &operator-=(const mint a) { if ((x += (mod - a.x)) >= mod) x -= mod; return *this; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); // std::ifstream in("input.txt"); // std::cin.rdbuf(in.rdbuf()); ll n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; mint ans = 0; rep(i, n - 1) { reps(ii, i + 1, n) { ans += a[i] ^ a[ii]; } } cout << ans.x << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++) #define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++) #define rrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--) #define all(x) (x).begin(), (x).end() #define sz(x) ((ll)(x).size()) #define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end()) struct mint { const int mod = 1e9 + 7; long long x; mint(long long x = 0) : x((x % mod + mod) % mod) {} mint &operator=(const long long a) { x = a % mod; return *this; } mint &operator=(const mint a) { x = a.x % mod; return *this; } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint &operator-=(const mint a) { if ((x += (mod - a.x)) >= mod) x -= mod; return *this; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); // std::ifstream in("input.txt"); // std::cin.rdbuf(in.rdbuf()); ll n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; mint ans = 0LL; ll f = 0LL; bool updated; do { updated = false; vector<ll> cnt(2, 0LL); rep(i, n) { if (a[i] > 0LL) updated = true; cnt[a[i] % 2LL]++; a[i] >>= 1LL; } mint tmp = 1; tmp *= (1LL << f); tmp *= cnt[0]; tmp *= cnt[1]; ans += tmp; // printf("cnt[0]%lld, cnt[1]=%lld\n", cnt[0], cnt[1]); f++; } while (updated); cout << ans.x << endl; return 0; }
replace
88
92
88
108
TLE
p02838
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define gcd __gcd #define F first #define B begin() #define E end() #define ln length() #define sz size() #define S second #define em empty() #define mp make_pair #define pb push_back #define pq priority_queue #define um unordered_map #define us unordered_set #define lb lower_bound #define ub upper_bound #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) #define sbits(a) __builtin_popcount(a) #define lcm(a, b) ((a * b) / gcd(a, b)) #define nani(x) cerr << #x << " is " << x << endl; #define adde(g, u, v) g[u].pb(v), g[v].pb(u); #define addew(g, u, v, w) g[u].pb({v, w}), g[v].pb({u, w}); #define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define inf 10000000000 #define mod 1000000007 typedef long long ll; const int N = 1e5 + 5; ll A[N]; ll s[61], u[61]; int main() { fast; // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int n; cin >> n; for (int i = 0; i < n; i++) cin >> A[i]; for (int j = 0; j < n; j++) { for (ll i = 0; i < 61; i++) { if (A[j] & (1LL << i)) s[i]++; else u[i]++; } } ll ans = 0; for (ll i = 0; i < 61; i++) { ans = (ans + (((s[i] * u[i]) % mod) * ((1LL << i) % mod)) % mod) % mod; } cout << ans; return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define gcd __gcd #define F first #define B begin() #define E end() #define ln length() #define sz size() #define S second #define em empty() #define mp make_pair #define pb push_back #define pq priority_queue #define um unordered_map #define us unordered_set #define lb lower_bound #define ub upper_bound #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) #define sbits(a) __builtin_popcount(a) #define lcm(a, b) ((a * b) / gcd(a, b)) #define nani(x) cerr << #x << " is " << x << endl; #define adde(g, u, v) g[u].pb(v), g[v].pb(u); #define addew(g, u, v, w) g[u].pb({v, w}), g[v].pb({u, w}); #define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define inf 10000000000 #define mod 1000000007 typedef long long ll; const int N = 3e5 + 5; ll A[N]; ll s[61], u[61]; int main() { fast; // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int n; cin >> n; for (int i = 0; i < n; i++) cin >> A[i]; for (int j = 0; j < n; j++) { for (ll i = 0; i < 61; i++) { if (A[j] & (1LL << i)) s[i]++; else u[i]++; } } ll ans = 0; for (ll i = 0; i < 61; i++) { ans = (ans + (((s[i] * u[i]) % mod) * ((1LL << i) % mod)) % mod) % mod; } cout << ans; return 0; }
replace
47
48
47
48
0
p02838
C++
Runtime Error
#include <iostream> using namespace std; long long sum = 0, a[3001], beki[61]; int A[300001][60] = {0}, B[300001][60] = {0}; int main() { int N; cin >> N; for (int i = 1; i <= N; i++) { cin >> a[i]; for (int j = 0; j < 60; j++) { A[i][j] = A[i - 1][j] + ((a[i] >> j) & 1); B[i][j] = B[i - 1][j] + 1 - ((a[i] >> j) & 1); } } beki[0] = 1; for (int i = 1; i <= 60; i++) { beki[i] = beki[i - 1] * 2; beki[i] %= 1000000007; } for (int i = 1; i <= N - 1; i++) { for (int j = 0; j < 60; j++) { if (((a[i] >> j) & 1) == 0) sum += ((A[N][j] - A[i][j]) * beki[j]) % 1000000007; else if (((a[i] >> j) & 1) > 0) sum += ((B[N][j] - B[i][j]) * beki[j]) % 1000000007; sum %= 1000000007; } } cout << sum; return 0; }
#include <iostream> using namespace std; long long sum = 0, a[300001], beki[61]; int A[300001][60] = {0}, B[300001][60] = {0}; int main() { int N; cin >> N; for (int i = 1; i <= N; i++) { cin >> a[i]; for (int j = 0; j < 60; j++) { A[i][j] = A[i - 1][j] + ((a[i] >> j) & 1); B[i][j] = B[i - 1][j] + 1 - ((a[i] >> j) & 1); } } beki[0] = 1; for (int i = 1; i <= 60; i++) { beki[i] = beki[i - 1] * 2; beki[i] %= 1000000007; } for (int i = 1; i <= N - 1; i++) { for (int j = 0; j < 60; j++) { if (((a[i] >> j) & 1) == 0) sum += ((A[N][j] - A[i][j]) * beki[j]) % 1000000007; else if (((a[i] >> j) & 1) > 0) sum += ((B[N][j] - B[i][j]) * beki[j]) % 1000000007; sum %= 1000000007; } } cout << sum; return 0; }
replace
2
3
2
3
-11
p02838
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <bitset> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> using namespace std; typedef unsigned long long UL; typedef long long LL; typedef long double LD; #define LP(i, a, b) for (LL i = int(a); i < int(b); i++) #define LPE(i, a, b) for (LL i = int(a); i <= int(b); i++) typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<vector<PII>> WAL; typedef vector<vector<int>> SAL; #define Ep 1e-8 #define INF 1e9 #define LINF 1e18 LL const MOD = 1e9 + 7; LL const MaxSize = 3e5 + 5; // LL const MOD = 998244353; /* #https://atcoder.jp/contests/abc147/tasks/abc147_d #Xor Sum 4 """ iterate over bits 1. calculate how many bits we have on that position 2. for each 1, we need to add num 0s * num 1s * 2s 3. sum them up 1. python version gets TLE due to the IO 2. in c++ version, all needs to be LL */ LL N, a[MaxSize]; int main() { ios_base::sync_with_stdio(false); freopen("/Users/georgeli/A_1.in", "r", stdin); cin >> N; LP(i, 0, N) { cin >> a[i]; } LL ans = 0; int maxS = 61; LL base = 1; LP(shift, 0, maxS) { // instead of spending time calculating tight boudary, // relax it a bit // LL base = 1 << shift; base %= MOD; //!!!! this overflow is what got me, also, shift does not work //!well in 32 bit+ cases LL n1 = 0; LP(i, 0, N) { if ((a[i] >> shift) & 1) // this is what got me: I didn't use & 1, shift // itself is an assignment which returns no value n1 += 1; } // cout << "n1: " << n1 << endl; LL part = n1 * (N - n1); // cout << shift << " " << part << " " << ans << endl; part %= MOD; part *= base; part %= MOD; ans += part; ans %= MOD; base *= 2L; } cout << ans; return 0; }
#include <algorithm> #include <assert.h> #include <bitset> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> using namespace std; typedef unsigned long long UL; typedef long long LL; typedef long double LD; #define LP(i, a, b) for (LL i = int(a); i < int(b); i++) #define LPE(i, a, b) for (LL i = int(a); i <= int(b); i++) typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<vector<PII>> WAL; typedef vector<vector<int>> SAL; #define Ep 1e-8 #define INF 1e9 #define LINF 1e18 LL const MOD = 1e9 + 7; LL const MaxSize = 3e5 + 5; // LL const MOD = 998244353; /* #https://atcoder.jp/contests/abc147/tasks/abc147_d #Xor Sum 4 """ iterate over bits 1. calculate how many bits we have on that position 2. for each 1, we need to add num 0s * num 1s * 2s 3. sum them up 1. python version gets TLE due to the IO 2. in c++ version, all needs to be LL */ LL N, a[MaxSize]; int main() { ios_base::sync_with_stdio(false); // freopen("/Users/georgeli/A_1.in", "r", stdin); cin >> N; LP(i, 0, N) { cin >> a[i]; } LL ans = 0; int maxS = 61; LL base = 1; LP(shift, 0, maxS) { // instead of spending time calculating tight boudary, // relax it a bit // LL base = 1 << shift; base %= MOD; //!!!! this overflow is what got me, also, shift does not work //!well in 32 bit+ cases LL n1 = 0; LP(i, 0, N) { if ((a[i] >> shift) & 1) // this is what got me: I didn't use & 1, shift // itself is an assignment which returns no value n1 += 1; } // cout << "n1: " << n1 << endl; LL part = n1 * (N - n1); // cout << shift << " " << part << " " << ans << endl; part %= MOD; part *= base; part %= MOD; ans += part; ans %= MOD; base *= 2L; } cout << ans; return 0; }
replace
53
54
53
54
0
p02838
C++
Runtime Error
#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 FORR(i, m, n) for (int i = m; i >= n; i--) #define ALL(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll INF = 1e15; const ll MOD = 1e9 + 7; ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } int main() { ll n; cin >> n; vector<ll> a(n); ll mx = -1; REP(i, n) { cin >> a[i]; mx = max(mx, a[i]); } ll cnt = 0; while (mx > 0) { cnt++; mx /= 2; } vector<map<ll, ll>> mp(70); REP(j, n) { ll tmp = 0; REP(i, cnt) { ll b = a[j] % 2; mp[i][b]++; a[j] /= 2; tmp++; } } ll ans = 0; ll next = 0; ll i = 0; while (true) { ll c = mp[i][0] * mp[i][1]; ans += mod_pow(2, i, MOD) * ((next + c) % 2); ans %= MOD; next = (next + c) / 2; if (next == 0) break; i++; } cout << ans << endl; return 0; }
#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 FORR(i, m, n) for (int i = m; i >= n; i--) #define ALL(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll INF = 1e15; const ll MOD = 1e9 + 7; ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } int main() { ll n; cin >> n; vector<ll> a(n); ll mx = -1; REP(i, n) { cin >> a[i]; mx = max(mx, a[i]); } ll cnt = 0; while (mx > 0) { cnt++; mx /= 2; } vector<map<ll, ll>> mp(1000); REP(j, n) { ll tmp = 0; REP(i, cnt) { ll b = a[j] % 2; mp[i][b]++; a[j] /= 2; tmp++; } } ll ans = 0; ll next = 0; ll i = 0; while (true) { ll c = mp[i][0] * mp[i][1]; ans += mod_pow(2, i, MOD) * ((next + c) % 2); ans %= MOD; next = (next + c) / 2; if (next == 0) break; i++; } cout << ans << endl; return 0; }
replace
41
42
41
42
0
p02838
C++
Runtime Error
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) using namespace std; long long mod = 1000000000 + 7; int main() { int N; cin >> N; vector<long long> A(N); rep(i, N) cin >> A[i]; long long ans = 0; long long twoPow = 1; rep(i, 60) { long long count0 = 0; long long count1 = 0; rep(j, N) { if (A[j] & (1LL << i)) count1++; else count0++; } ans += (count0 * count1) % mod * twoPow % mod; ans %= ans % mod; twoPow = (twoPow * 2) % mod; } cout << ans; }
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) using namespace std; long long mod = 1000000000 + 7; int main() { int N; cin >> N; vector<long long> A(N); rep(i, N) cin >> A[i]; long long ans = 0; long long twoPow = 1; rep(i, 60) { long long count0 = 0; long long count1 = 0; rep(j, N) { if (A[j] & (1LL << i)) count1++; else count0++; } ans += (count0 * count1) % mod * twoPow % mod; ans %= mod; twoPow = (twoPow * 2) % mod; } cout << ans; }
replace
27
28
27
28
-8
p02838
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define ll long long int #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n; i >= 0; i--) #define REP(i, s, t) for (int i = s; i <= t; i++) #define RREP(i, s, t) for (int i = s; i >= t; i--) #define dump(x) cerr << #x << " = " << (x) << endl; #define INF 2000000000 #define mod 1000000007 #define INF2 1000000000000000000 #define int long long int F[2100010]; int Mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; } void fact(void) { F[0] = 1; F[1] = 1; for (int i = 2; i <= 2100000; i++) { F[i] = Mul(i, F[i - 1]); } } int square(int x) { return (x * x) % mod; } int power(int x, int y) { if (y == 0) return 1; else if (y == 1) return x % mod; else if (y % 2 == 0) return square(power(x, y / 2)) % mod; else return square(power(x, y / 2)) * x % mod; } int Div(int a, int b) { return Mul(a, power(b, mod - 2)); } int P(int n, int r) { if (n < r || n < 0 || r < 0) return 0; return Div(F[n], F[n - r]); } int C(int n, int r) { if (n < r || n < 0 || r < 0) return 0; int ret; ret = Div(F[n], Mul(F[n - r], F[r])); return ret; } int H(int n, int r) { if (n == 0 && r == 0) return 1; return C(n + r - 1, r); } int K[64]; signed main(void) { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int A[30010]; rep(i, N) cin >> A[i]; int sum = 0; rep(i, N) { int num = A[i]; int cnt = 0; while (num > 0) { K[cnt] += num % 2; num /= 2; cnt++; } } // rep(i, 4) cout << "*" << K[i] << endl; int ans = 0; rep(i, N - 1) { int cnt = 0; rep(j, 62) { if (A[i] % 2 == 1) { ans = (ans + Mul((1LL << j), (N - i) - K[j])) % mod; } else { ans = (ans + Mul((1LL << j), K[j])) % mod; } K[j] -= A[i] % 2; A[i] /= 2; // cout << i << " "<< ans << endl; } } cout << ans << endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define ll long long int #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n; i >= 0; i--) #define REP(i, s, t) for (int i = s; i <= t; i++) #define RREP(i, s, t) for (int i = s; i >= t; i--) #define dump(x) cerr << #x << " = " << (x) << endl; #define INF 2000000000 #define mod 1000000007 #define INF2 1000000000000000000 #define int long long int F[2100010]; int Mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; } void fact(void) { F[0] = 1; F[1] = 1; for (int i = 2; i <= 2100000; i++) { F[i] = Mul(i, F[i - 1]); } } int square(int x) { return (x * x) % mod; } int power(int x, int y) { if (y == 0) return 1; else if (y == 1) return x % mod; else if (y % 2 == 0) return square(power(x, y / 2)) % mod; else return square(power(x, y / 2)) * x % mod; } int Div(int a, int b) { return Mul(a, power(b, mod - 2)); } int P(int n, int r) { if (n < r || n < 0 || r < 0) return 0; return Div(F[n], F[n - r]); } int C(int n, int r) { if (n < r || n < 0 || r < 0) return 0; int ret; ret = Div(F[n], Mul(F[n - r], F[r])); return ret; } int H(int n, int r) { if (n == 0 && r == 0) return 1; return C(n + r - 1, r); } int K[64]; signed main(void) { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int A[300010]; rep(i, N) cin >> A[i]; int sum = 0; rep(i, N) { int num = A[i]; int cnt = 0; while (num > 0) { K[cnt] += num % 2; num /= 2; cnt++; } } // rep(i, 4) cout << "*" << K[i] << endl; int ans = 0; rep(i, N - 1) { int cnt = 0; rep(j, 62) { if (A[i] % 2 == 1) { ans = (ans + Mul((1LL << j), (N - i) - K[j])) % mod; } else { ans = (ans + Mul((1LL << j), K[j])) % mod; } K[j] -= A[i] % 2; A[i] /= 2; // cout << i << " "<< ans << endl; } } cout << ans << endl; return 0; }
replace
62
63
62
63
0
p02838
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; // #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i)) #define rep(i, e) for (int(i) = 0; (i) < (e); ++(i)) #define all(x) x.begin(), x.end() int main() { int n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<vector<ll>> sum(n + 1, vector<ll>(60, 0)); rep(i, n) rep(j, 60) { if ((a[i] >> j) & 1) ++sum[i + 1][j]; sum[i + 1][j] += sum[i][j]; } /*rep(i, n)rep(j, 60) { sum[i + 1][j] += sum[i][j]; }*/ ll MOD = 1e9 + 7; ll ans = 0; rep(i, n) rep(j, 60) { ll num = j; ll tmp = powl(2LL, num); tmp %= MOD; ll x; if ((a[i] >> j) & 1) x = n - i - 1 - (sum[n][j] - sum[i + 1][j]); else x = sum[n][j] - sum[i + 1][j]; ans += x * tmp; ans %= MOD; } cout << ans << endl; }
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; // #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i)) #define rep(i, e) for (int(i) = 0; (i) < (e); ++(i)) #define all(x) x.begin(), x.end() int main() { int n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<vector<ll>> sum(n + 1, vector<ll>(60, 0)); rep(i, n) rep(j, 60) { if ((a[i] >> j) & 1) ++sum[i + 1][j]; sum[i + 1][j] += sum[i][j]; } ll MOD = 1e9 + 7; ll ans = 0; rep(i, n) rep(j, 60) { ll num = j; ll tmp = powl(2LL, num); tmp %= MOD; ll x; if ((a[i] >> j) & 1) x = n - i - 1 - (sum[n][j] - sum[i + 1][j]); else x = sum[n][j] - sum[i + 1][j]; ans += x * tmp; ans %= MOD; } cout << ans << endl; }
delete
29
33
29
29
TLE
p02838
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; using vl = vector<ll>; /* short */ #define pb push_back #define mp make_pair #define Fi first #define Se second #define ALL(v) begin(v), end(v) #define RALL(v) rbegin(v), rend(v) /* REPmacro */ #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FORR(i, a, b) for (int i = (a); i >= (b); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define FOREACH(x, a) for (auto x : a) /* exchange */ #define CHMIN(a, b) (a) = min((ll)(a), (ll)(b)) #define CHMAX(a, b) (a) = max((ll)(a), (ll)(b)) /* function */ #define IN(x) cin >> x #define DEBUG(x) cerr << (x) << " " #define LN() cerr << "\n" #define PRINT(x) cout << (x) << endl #define BR cout << endl /* const */ const int ARRAY = 100005; const int INF = 1001001001; // 10^9 const ll LINF = 1001001001001001001; // 10^18 const int MOD = 1e9 + 7; ll N = 0; ll A[30005]; ll di[60]; ll t; ll ret = 0; void input() { IN(N); REP(i, N) { IN(t); A[i] = t; } } void solve() { REP(j, 60) { ll x = 0; REP(i, N) { x += (A[i] >> j) & 1; } // DEBUG(x); ll p = x * (N - x) % MOD; REP(k, j) p = p * 2 % MOD; ret += p; ret %= MOD; } PRINT(ret); } int main(void) { input(); solve(); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; using vl = vector<ll>; /* short */ #define pb push_back #define mp make_pair #define Fi first #define Se second #define ALL(v) begin(v), end(v) #define RALL(v) rbegin(v), rend(v) /* REPmacro */ #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FORR(i, a, b) for (int i = (a); i >= (b); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define FOREACH(x, a) for (auto x : a) /* exchange */ #define CHMIN(a, b) (a) = min((ll)(a), (ll)(b)) #define CHMAX(a, b) (a) = max((ll)(a), (ll)(b)) /* function */ #define IN(x) cin >> x #define DEBUG(x) cerr << (x) << " " #define LN() cerr << "\n" #define PRINT(x) cout << (x) << endl #define BR cout << endl /* const */ const int ARRAY = 100005; const int INF = 1001001001; // 10^9 const ll LINF = 1001001001001001001; // 10^18 const int MOD = 1e9 + 7; ll N = 0; ll A[300005]; ll di[60]; ll t; ll ret = 0; void input() { IN(N); REP(i, N) { IN(t); A[i] = t; } } void solve() { REP(j, 60) { ll x = 0; REP(i, N) { x += (A[i] >> j) & 1; } // DEBUG(x); ll p = x * (N - x) % MOD; REP(k, j) p = p * 2 % MOD; ret += p; ret %= MOD; } PRINT(ret); } int main(void) { input(); solve(); }
replace
38
39
38
39
0
p02838
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD 1000000007 template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } int main() { int n; cin >> n; ll A[n]; for (int i = 0; i < n; i++) cin >> A[i]; int sum = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { sum += (A[i] ^ A[j]) % MOD; sum %= MOD; } } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD 1000000007 template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } int main() { int n; cin >> n; ll A[n]; for (int i = 0; i < n; i++) cin >> A[i]; ll sum = 0; ll cnt[61]; for (ll b = 0; b < 61; b++) cnt[b] = 0; for (ll i = 0; i < n; i++) for (ll b = 0; b < 61; b++) if (A[i] & (1ull << b)) cnt[b]++; for (ll b = 0; b < 61; b++) { ll m = ((cnt[b] * (n - cnt[b]) % MOD) * ((1ull << b) % MOD)) % MOD; sum += m; sum %= MOD; } cout << sum << endl; return 0; }
replace
22
29
22
34
TLE
p02838
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll n; ll a[1 << 18]; ll sum[1 << 18][65]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; ll tmp = a[i]; for (int j = 0; j < 60; j++) { sum[i][j] = tmp & 1; tmp >>= 1; } } for (int i = n; i >= 0; i--) { for (int j = 0; j < 60; j++) { sum[i][j] += sum[i + 1][j]; } } ll ans = 0; ll mod = 1e9 + 7; for (int i = 0; i < n; i++) { for (int j = 0; j < 60; j++) { ans += (1LL << j) % mod * ((a[i] >> j) & 1 ? n - i - 1 - sum[i + 1][j] : sum[i + 1][j]) % mod; ans %= mod; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll n; ll a[1 << 19]; ll sum[1 << 19][65]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; ll tmp = a[i]; for (int j = 0; j < 60; j++) { sum[i][j] = tmp & 1; tmp >>= 1; } } for (int i = n; i >= 0; i--) { for (int j = 0; j < 60; j++) { sum[i][j] += sum[i + 1][j]; } } ll ans = 0; ll mod = 1e9 + 7; for (int i = 0; i < n; i++) { for (int j = 0; j < 60; j++) { ans += (1LL << j) % mod * ((a[i] >> j) & 1 ? n - i - 1 - sum[i + 1][j] : sum[i + 1][j]) % mod; ans %= mod; } } cout << ans << endl; return 0; }
replace
4
6
4
6
-11
p02838
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; #define INF 10e17 #define rep(i, n) for (long long i = 0; i < n; i++) #define repr(i, n, m) for (long long i = m; i < n; i++) #define END cout << endl #define MOD 1000000007 #define pb push_back #define sorti(x) sort(x.begin(), x.end()) #define sortd(x) sort(x.begin(), x.end(), std::greater<long long>()) #define debug(x) std::cerr << (x) << std::endl; #define roll(x) \ for (auto &&itr : x) { \ debug(itr); \ } template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t; } template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t; } ll digit[100010][70][2]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); vector<ll> two(70, 0); ll tw = 1; ll cnt = 0; while (cnt < 62) { two[cnt] = tw % MOD; tw *= 2; cnt++; } ll n; cin >> n; vector<ll> a(n); rep(i, n) { cin >> a[i]; ll cp = a[i]; for (int t = 0; t < 70; ++t) { if (cp & 1) { digit[i + 1][t][1] = (digit[i][t][1] + two[t]) % MOD; digit[i + 1][t][0] = (digit[i][t][0]) % MOD; } else { digit[i + 1][t][1] = (digit[i][t][1]) % MOD; digit[i + 1][t][0] = (digit[i][t][0] + two[t]) % MOD; } cp >>= 1; } } /*for (int i = 0; i < n+1; ++i) { for (int j = 0; j < 3; ++j) { cout << digit[i][j][0] << " "; //cout << digit[i][j][1] << " "; } cout << endl; }*/ ll ans = 0; for (int i = 0; i < n; ++i) { ll cp = a[i]; for (int t = 0; t < 70; ++t) { ll one = MOD + digit[n][t][1] - digit[i][t][1]; ll zero = MOD + digit[n][t][0] - digit[i][t][0]; // cout << one << " " << zero << endl; if (cp & 1) { ans += zero % MOD; } else { ans += one % MOD; } cp >>= 1; } } cout << ans % MOD << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; #define INF 10e17 #define rep(i, n) for (long long i = 0; i < n; i++) #define repr(i, n, m) for (long long i = m; i < n; i++) #define END cout << endl #define MOD 1000000007 #define pb push_back #define sorti(x) sort(x.begin(), x.end()) #define sortd(x) sort(x.begin(), x.end(), std::greater<long long>()) #define debug(x) std::cerr << (x) << std::endl; #define roll(x) \ for (auto &&itr : x) { \ debug(itr); \ } template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t; } template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t; } ll digit[300010][70][2]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); vector<ll> two(70, 0); ll tw = 1; ll cnt = 0; while (cnt < 62) { two[cnt] = tw % MOD; tw *= 2; cnt++; } ll n; cin >> n; vector<ll> a(n); rep(i, n) { cin >> a[i]; ll cp = a[i]; for (int t = 0; t < 70; ++t) { if (cp & 1) { digit[i + 1][t][1] = (digit[i][t][1] + two[t]) % MOD; digit[i + 1][t][0] = (digit[i][t][0]) % MOD; } else { digit[i + 1][t][1] = (digit[i][t][1]) % MOD; digit[i + 1][t][0] = (digit[i][t][0] + two[t]) % MOD; } cp >>= 1; } } /*for (int i = 0; i < n+1; ++i) { for (int j = 0; j < 3; ++j) { cout << digit[i][j][0] << " "; //cout << digit[i][j][1] << " "; } cout << endl; }*/ ll ans = 0; for (int i = 0; i < n; ++i) { ll cp = a[i]; for (int t = 0; t < 70; ++t) { ll one = MOD + digit[n][t][1] - digit[i][t][1]; ll zero = MOD + digit[n][t][0] - digit[i][t][0]; // cout << one << " " << zero << endl; if (cp & 1) { ans += zero % MOD; } else { ans += one % MOD; } cp >>= 1; } } cout << ans % MOD << endl; }
replace
41
42
41
42
-11
p02838
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using i64 = long long; using u64 = unsigned long long; #define dbg(x) cerr << "[" #x << " : " << x << "]\n" const int mod = (int)1e9 + 7; int main() { int n; scanf("%d", &n); i64 a[400000]; vector<int> cnt(65, 0); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); for (i64 j = 0; j < 62; j++) { cnt[j] += ((a[i] & (1LL << j)) > 0); } } vector<i64> powers(65, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < 62; j++) { if (a[i] & (1LL << j)) { cnt[j]--; powers[j] += n - i - 1 - cnt[j]; } else { powers[j] += cnt[j]; } } } i64 ans = 0; for (int i = 0; i < 61; i++) { i64 p = (i64)pow(2, i); p %= mod; ans = ans + (p * (powers[i]) % mod) % mod; assert(ans >= 0); ans %= mod; } printf("%lld\n", ans); }
#include <bits/stdc++.h> using namespace std; using i64 = long long; using u64 = unsigned long long; #define dbg(x) cerr << "[" #x << " : " << x << "]\n" const int mod = (int)1e9 + 7; int main() { int n; scanf("%d", &n); i64 a[400000]; vector<int> cnt(65, 0); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); for (i64 j = 0; j < 62; j++) { cnt[j] += ((a[i] & (1LL << j)) > 0); } } vector<i64> powers(65, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < 62; j++) { if (a[i] & (1LL << j)) { cnt[j]--; powers[j] += n - i - 1 - cnt[j]; } else { powers[j] += cnt[j]; } } } i64 ans = 0; for (int i = 0; i < 61; i++) { i64 tmp = powers[i]; for (int j = 1; j <= i; j++) { tmp *= 2; tmp %= mod; } ans += tmp; ans %= mod; } printf("%lld\n", ans); }
replace
34
38
34
40
0
p02838
C++
Time Limit Exceeded
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> #include <vector> using namespace std; using ll = long long; using pll = pair<ll, ll>; using vl = vector<ll>; using vll = vector<vl>; using vpll = vector<pll>; int ctoi(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; default: return 0; } } bool pairCompare(const pll firstElof, pll secondElof) { return firstElof.first > secondElof.first; } ll nod(ll F) { ll keta = 1; while (F / 10 > 0) { keta++; F /= 10; } return keta; } ll gcd(ll x, ll y) { ll r; if (x < y) { swap(x, y); } while (y > 0) { r = x % y; x = y; y = r; } return x; } ll lcm(ll x, ll y) { return x * y / gcd(x, y); } ll isPrime(ll x) { ll i; if (x < 2) { return 0; } else if (x == 2) { return 1; } else if (x % 2 == 0) { return 0; } else { for (i = 3; i * i <= x; i += 2) { if (x % i == 0) { return 0; } } return 1; } } void eratos(vl isPrime) { //(注)isPrimeのサイズはN+1にする!実際にはmain内に配置して使用 ll i, j; for (i = 0; i < isPrime.size(); i++) { isPrime[i] = 1; } isPrime[0] = 0; isPrime[1] = 0; for (i = 2; i * i <= isPrime.size() - 1; i++) { if (isPrime[i] == 1) { j = i * 2; while (j <= isPrime.size() - 1) { isPrime[j] = 0; j = j + i; } } } } ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } ll alphabet(char C) { ll b = 0; ll key = 0; if (C == 'a') { return b; } b++; if (C == 'b') { return b; } b++; if (C == 'c') { return b; } b++; if (C == 'd') { return b; } b++; if (C == 'e') { return b; } b++; if (C == 'f') { return b; } b++; if (C == 'g') { return b; } b++; if (C == 'h') { return b; } b++; if (C == 'i') { return b; } b++; if (C == 'j') { return b; } b++; if (C == 'k') { return b; } b++; if (C == 'l') { return b; } b++; if (C == 'm') { return b; } b++; if (C == 'n') { return b; } b++; if (C == 'o') { return b; } b++; if (C == 'p') { return b; } b++; if (C == 'q') { return b; } b++; if (C == 'r') { return b; } b++; if (C == 's') { return b; } b++; if (C == 't') { return b; } b++; if (C == 'u') { return b; } b++; if (C == 'v') { return b; } b++; if (C == 'w') { return b; } b++; if (C == 'x') { return b; } b++; if (C == 'y') { return b; } b++; if (C == 'z') { return b; } return -1; } void bitSearch(ll n) { // 実際にはコピーして中身を改変して使う ll i, j; for (i = 0; i < pow(2, n); i++) { ll p = i; for (j = 0; j < n; j++) { cout << p % 2; p /= 2; } cout << endl; } } void dfsx(ll now, ll C) { stack<ll> S; S.push(now); // color[now] = C; // while (!S.empty()) { ll u = S.top(); S.pop(); // for (ll i = 0; i < road[u].size(); i++) { // ll v = road[u][i]; // if (color[v] == 0) { // dfs(v, C); //} //} //} } void bfs(ll now) { // 中身は毎回書き換えて使用 queue<ll> Q; Q.push(now); ll u; while (!Q.empty()) { u = Q.front(); Q.pop(); // ll v=; // Q.push(v); } } void dijkstra(ll s) { priority_queue<pll> PQ; // vl D(N), color(N); // for (i = 0; i < N; i++) { // D[i] = INF; // color[i] = 0; // } // D[s] = 0; PQ.push(make_pair(0, s)); // color[0] = 1; ll q = -1; while (!PQ.empty()) { pll f = PQ.top(); PQ.pop(); ll u = f.second; // color[u] = 2; // if (D[u] < f.first * q) { // continue; // } // for (j = 0; j < path[u].size(); j++) { // ll v = path[u][j].first; // if (color[v] == 2) { // continue; // } // if (D[v] > D[u] + path[u][j].second) { // D[v] = D[u] + path[u][j].second; // color[v] = 1; // PQ.push(make_pair(D[v] * q, v)); // } //} } // cout << (D[N - 1] == INF ? -1 : D[N - 1]); } class UnionFind { public: vl rank, par, siz; UnionFind() {} UnionFind(ll N) { rank.resize(N, 0); par.resize(N, 0); siz.resize(N, 1); init(N); } void init(ll N) { for (ll i = 0; i < N; i++) { par[i] = i; rank[i] = 0; } } ll find(ll x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(ll x, ll y) { x = find(x); y = find(y); if (x != y) { if (rank[x] < rank[y]) { par[x] = y; siz[y] += siz[x]; } else { par[y] = x; siz[x] += siz[y]; if (rank[x] == rank[y]) rank[x]++; } } } bool issame(ll x, ll y) { return find(x) == find(y); } ll size(ll x) { return siz[find(x)]; } }; //**定義場所**// ll i, j, k, l, m, n, N, M, K, H, W, L; string S, T; ll MOD = 1000000007; ll ans = 0; ll INF = 9999999999999999; //***********// int main() { cin >> N; vl A(N); vl C(60); for (i = 0; i < 60; i++) { C[i] = 0; } for (i = 0; i < N; i++) { cin >> A[i]; ll c = 0; ll a = A[i]; while (a > 0) { if (a % 2) { C[c]++; } a /= 2; c++; } } for (i = 0; i < 60; i++) { ll p = 1; for (j = 0; j < i; j++) { p = (p * 2) % MOD; } for (j = 0; j < C[i]; j++) { for (k = 0; k < (N - C[i]); k++) { ans = (ans + p) % MOD; } } } cout << ans; }
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> #include <vector> using namespace std; using ll = long long; using pll = pair<ll, ll>; using vl = vector<ll>; using vll = vector<vl>; using vpll = vector<pll>; int ctoi(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; default: return 0; } } bool pairCompare(const pll firstElof, pll secondElof) { return firstElof.first > secondElof.first; } ll nod(ll F) { ll keta = 1; while (F / 10 > 0) { keta++; F /= 10; } return keta; } ll gcd(ll x, ll y) { ll r; if (x < y) { swap(x, y); } while (y > 0) { r = x % y; x = y; y = r; } return x; } ll lcm(ll x, ll y) { return x * y / gcd(x, y); } ll isPrime(ll x) { ll i; if (x < 2) { return 0; } else if (x == 2) { return 1; } else if (x % 2 == 0) { return 0; } else { for (i = 3; i * i <= x; i += 2) { if (x % i == 0) { return 0; } } return 1; } } void eratos(vl isPrime) { //(注)isPrimeのサイズはN+1にする!実際にはmain内に配置して使用 ll i, j; for (i = 0; i < isPrime.size(); i++) { isPrime[i] = 1; } isPrime[0] = 0; isPrime[1] = 0; for (i = 2; i * i <= isPrime.size() - 1; i++) { if (isPrime[i] == 1) { j = i * 2; while (j <= isPrime.size() - 1) { isPrime[j] = 0; j = j + i; } } } } ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } ll alphabet(char C) { ll b = 0; ll key = 0; if (C == 'a') { return b; } b++; if (C == 'b') { return b; } b++; if (C == 'c') { return b; } b++; if (C == 'd') { return b; } b++; if (C == 'e') { return b; } b++; if (C == 'f') { return b; } b++; if (C == 'g') { return b; } b++; if (C == 'h') { return b; } b++; if (C == 'i') { return b; } b++; if (C == 'j') { return b; } b++; if (C == 'k') { return b; } b++; if (C == 'l') { return b; } b++; if (C == 'm') { return b; } b++; if (C == 'n') { return b; } b++; if (C == 'o') { return b; } b++; if (C == 'p') { return b; } b++; if (C == 'q') { return b; } b++; if (C == 'r') { return b; } b++; if (C == 's') { return b; } b++; if (C == 't') { return b; } b++; if (C == 'u') { return b; } b++; if (C == 'v') { return b; } b++; if (C == 'w') { return b; } b++; if (C == 'x') { return b; } b++; if (C == 'y') { return b; } b++; if (C == 'z') { return b; } return -1; } void bitSearch(ll n) { // 実際にはコピーして中身を改変して使う ll i, j; for (i = 0; i < pow(2, n); i++) { ll p = i; for (j = 0; j < n; j++) { cout << p % 2; p /= 2; } cout << endl; } } void dfsx(ll now, ll C) { stack<ll> S; S.push(now); // color[now] = C; // while (!S.empty()) { ll u = S.top(); S.pop(); // for (ll i = 0; i < road[u].size(); i++) { // ll v = road[u][i]; // if (color[v] == 0) { // dfs(v, C); //} //} //} } void bfs(ll now) { // 中身は毎回書き換えて使用 queue<ll> Q; Q.push(now); ll u; while (!Q.empty()) { u = Q.front(); Q.pop(); // ll v=; // Q.push(v); } } void dijkstra(ll s) { priority_queue<pll> PQ; // vl D(N), color(N); // for (i = 0; i < N; i++) { // D[i] = INF; // color[i] = 0; // } // D[s] = 0; PQ.push(make_pair(0, s)); // color[0] = 1; ll q = -1; while (!PQ.empty()) { pll f = PQ.top(); PQ.pop(); ll u = f.second; // color[u] = 2; // if (D[u] < f.first * q) { // continue; // } // for (j = 0; j < path[u].size(); j++) { // ll v = path[u][j].first; // if (color[v] == 2) { // continue; // } // if (D[v] > D[u] + path[u][j].second) { // D[v] = D[u] + path[u][j].second; // color[v] = 1; // PQ.push(make_pair(D[v] * q, v)); // } //} } // cout << (D[N - 1] == INF ? -1 : D[N - 1]); } class UnionFind { public: vl rank, par, siz; UnionFind() {} UnionFind(ll N) { rank.resize(N, 0); par.resize(N, 0); siz.resize(N, 1); init(N); } void init(ll N) { for (ll i = 0; i < N; i++) { par[i] = i; rank[i] = 0; } } ll find(ll x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(ll x, ll y) { x = find(x); y = find(y); if (x != y) { if (rank[x] < rank[y]) { par[x] = y; siz[y] += siz[x]; } else { par[y] = x; siz[x] += siz[y]; if (rank[x] == rank[y]) rank[x]++; } } } bool issame(ll x, ll y) { return find(x) == find(y); } ll size(ll x) { return siz[find(x)]; } }; //**定義場所**// ll i, j, k, l, m, n, N, M, K, H, W, L; string S, T; ll MOD = 1000000007; ll ans = 0; ll INF = 9999999999999999; //***********// int main() { cin >> N; vl A(N); vl C(60); for (i = 0; i < 60; i++) { C[i] = 0; } for (i = 0; i < N; i++) { cin >> A[i]; ll c = 0; ll a = A[i]; while (a > 0) { if (a % 2) { C[c]++; } a /= 2; c++; } } for (i = 0; i < 60; i++) { ll p = 1; for (j = 0; j < i; j++) { p = (p * 2) % MOD; } for (j = 0; j < C[i]; j++) { ans = (ans + p * (N - C[i])) % MOD; } } cout << ans; }
replace
369
372
369
370
TLE
p02838
C++
Runtime Error
/// ببِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #include <bits/stdc++.h> #define off exit(0) #define cn continue #define rt return #define sc scanf #define pr printf #define pb push_back #define pf push_front #define p_f pop_front #define p_b pop_back #define ff first #define ss second #define vec vector #define gcd __gcd #define pi acos(-1) #define ll long long #define ld long double #define N 2256 #define M 1000000007 #define I 1000000000000000000 #define pdd pair<ld, ld> #define pll pair<ll, ll> #define pii pair<int, int> #define rep(a, b, c, d) for (int a = b; a <= c; a += d) #define rvv(a, b, c, d) for (int a = b; a >= c; a -= d) #define en printf("\n") #define sqr(a) a *a #define all(c) c.begin(), c.end() #define srt(c) sort(all(c)) #define rev(c) reverse(all(c)) #define srtr(c) sort(c.rbegin(), c.rend()) #define numcounts __builtin_popcount #define mxn(x, y, z) max(x, min(y, z)) #define mnx(x, y, z) min(x, max(y, z)) const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, -1, 1}; using namespace std; ll ttt = 1; ll n, a[N], ans, cnt; void solve() { cin >> n; for (ll i = 0; i < n; i++) cin >> a[i]; for (ll i = 0; i < 61; i++) { cnt = 0; for (ll j = 0; j < n; j++) if (a[j] & (1LL << i)) cnt++; ans += (((cnt * (n - cnt)) % M) * ((1LL << i) % M)) % M; ans %= M; } cout << ans; } int main() { // sc( "%I64d", &ttt ); for (int i = 0; i < ttt; i++) solve(); }
/// ببِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #include <bits/stdc++.h> #define off exit(0) #define cn continue #define rt return #define sc scanf #define pr printf #define pb push_back #define pf push_front #define p_f pop_front #define p_b pop_back #define ff first #define ss second #define vec vector #define gcd __gcd #define pi acos(-1) #define ll long long #define ld long double #define N 300256 #define M 1000000007 #define I 1000000000000000000 #define pdd pair<ld, ld> #define pll pair<ll, ll> #define pii pair<int, int> #define rep(a, b, c, d) for (int a = b; a <= c; a += d) #define rvv(a, b, c, d) for (int a = b; a >= c; a -= d) #define en printf("\n") #define sqr(a) a *a #define all(c) c.begin(), c.end() #define srt(c) sort(all(c)) #define rev(c) reverse(all(c)) #define srtr(c) sort(c.rbegin(), c.rend()) #define numcounts __builtin_popcount #define mxn(x, y, z) max(x, min(y, z)) #define mnx(x, y, z) min(x, max(y, z)) const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, -1, 1}; using namespace std; ll ttt = 1; ll n, a[N], ans, cnt; void solve() { cin >> n; for (ll i = 0; i < n; i++) cin >> a[i]; for (ll i = 0; i < 61; i++) { cnt = 0; for (ll j = 0; j < n; j++) if (a[j] & (1LL << i)) cnt++; ans += (((cnt * (n - cnt)) % M) * ((1LL << i) % M)) % M; ans %= M; } cout << ans; } int main() { // sc( "%I64d", &ttt ); for (int i = 0; i < ttt; i++) solve(); }
replace
18
19
18
19
0
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using Edge = pair<int, int>; using Graph = vector<vector<Edge>>; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) const ll MOD = 1000000007; const ll nmax = 8; const ll INF = 1e9; bool graph[nmax][nmax]; vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF)); void warshall_floyd(ll n) { for (size_t i = 0; i < n; i++) { for (size_t j = 0; j < n; j++) { for (size_t k = 0; k < n; k++) { dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]); } } } } class UnionFind { public: vector<ll> Parent; UnionFind(ll N) { Parent = vector<ll>(N, -1); } ll find(ll A) { if (Parent[A] < 0) return A; return Parent[A] = find(Parent[A]); } ll size(ll A) { return -Parent[find(A)]; } bool Union(ll A, ll B) { A = find(A); B = find(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } ll mulMod(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); } ll powMod(ll a, ll p) { if (p == 0) { return 1; } else if (p % 2 == 0) { ll half = powMod(a, p / 2); return mulMod(half, half); } else { return mulMod(powMod(a, p - 1), a); } } ll ceil(ll a, ll b) { return (a + b - 1) / b; } void solve(long long H, long long W, std::vector<std::vector<long long>> A, std::vector<std::vector<long long>> B) {} int main() { long long H; scanf("%lld", &H); long long W; scanf("%lld", &W); bool dp[H][W][H + W * 80 + 10]; std::fill(dp[0][0], dp[H][0], false); std::vector<std::vector<long long>> sub(H, std::vector<long long>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ll tmp; cin >> tmp; sub[i][j] = tmp; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ll tmp; cin >> tmp; sub[i][j] = abs(sub[i][j] - tmp); } } dp[0][0][sub[0][0]] = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k < (H + W) * 80 + 1; k++) { if (dp[i][j][k]) { if (i != H - 1) { dp[i + 1][j][k + sub[i + 1][j]] = true; dp[i + 1][j][abs(k - sub[i + 1][j])] = true; } if (j != W - 1) { dp[i][j + 1][k + sub[i][j + 1]] = true; dp[i][j + 1][abs(k - sub[i][j + 1])] = true; } } } } } for (int k = 0; k < 12810; k++) { if (dp[H - 1][W - 1][k]) { cout << k << endl; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Edge = pair<int, int>; using Graph = vector<vector<Edge>>; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) const ll MOD = 1000000007; const ll nmax = 8; const ll INF = 1e9; bool graph[nmax][nmax]; vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF)); void warshall_floyd(ll n) { for (size_t i = 0; i < n; i++) { for (size_t j = 0; j < n; j++) { for (size_t k = 0; k < n; k++) { dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]); } } } } class UnionFind { public: vector<ll> Parent; UnionFind(ll N) { Parent = vector<ll>(N, -1); } ll find(ll A) { if (Parent[A] < 0) return A; return Parent[A] = find(Parent[A]); } ll size(ll A) { return -Parent[find(A)]; } bool Union(ll A, ll B) { A = find(A); B = find(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } ll mulMod(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); } ll powMod(ll a, ll p) { if (p == 0) { return 1; } else if (p % 2 == 0) { ll half = powMod(a, p / 2); return mulMod(half, half); } else { return mulMod(powMod(a, p - 1), a); } } ll ceil(ll a, ll b) { return (a + b - 1) / b; } void solve(long long H, long long W, std::vector<std::vector<long long>> A, std::vector<std::vector<long long>> B) {} int main() { long long H; scanf("%lld", &H); long long W; scanf("%lld", &W); bool dp[80][80][(H + W) * 80 + 10]; std::fill(dp[0][0], dp[H][0], false); std::vector<std::vector<long long>> sub(H, std::vector<long long>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ll tmp; cin >> tmp; sub[i][j] = tmp; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ll tmp; cin >> tmp; sub[i][j] = abs(sub[i][j] - tmp); } } dp[0][0][sub[0][0]] = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k < (H + W) * 80 + 1; k++) { if (dp[i][j][k]) { if (i != H - 1) { dp[i + 1][j][k + sub[i + 1][j]] = true; dp[i + 1][j][abs(k - sub[i + 1][j])] = true; } if (j != W - 1) { dp[i][j + 1][k + sub[i][j + 1]] = true; dp[i][j + 1][abs(k - sub[i][j + 1])] = true; } } } } } for (int k = 0; k < 12810; k++) { if (dp[H - 1][W - 1][k]) { cout << k << endl; break; } } return 0; }
replace
87
88
87
88
0
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define li long long int #define rep(i, to) for (li i = 0; i < ((li)(to)); i++) #define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++) #define MOD 1000000007 li h, w; li a[88][88]; li b[88][88]; bitset<80 * 160 * 2 + 1> dp[88][88]; int main(void) { li offset = 80 * 160; cin >> h >> w; rep(i, h) { rep(j, w) { cin >> a[i][j]; } } rep(i, h) { rep(j, w) { cin >> b[i][j]; } } li lim = 0; rep(i, h) { rep(j, w) { li geta = min(a[i][j], b[i][j]); a[i][j] -= geta; b[i][j] -= geta; lim += a[i][j]; lim += b[i][j]; } } dp[0][0][a[0][0] - b[0][0] + offset] = true; dp[0][0][-a[0][0] + b[0][0] + offset] = true; // first line repp(i, 1, w) { li val = abs(a[0][i] - b[0][i]); dp[0][i] |= dp[0][i - 1] << val; dp[0][i] |= dp[0][i - 1] >> val; } repp(i, 1, h) { rep(j, lim + 1) { li val = abs(a[i][0] - b[i][0]); dp[i][0] |= dp[i - 1][0] << val; dp[i][0] |= dp[i - 1][0] >> val; } repp(j, 1, w) { li now = abs(a[i][j] - b[i][j]); dp[i][j] |= dp[i - 1][j] << now; dp[i][j] |= dp[i - 1][j] >> now; dp[i][j] |= dp[i][j - 1] << now; dp[i][j] |= dp[i][j - 1] >> now; } } li res = 1 << 30; rep(i, 80 * 160 * 2 + 1) { if (dp[h - 1][w - 1][i]) { res = min(res, abs(offset - i)); } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define li long long int #define rep(i, to) for (li i = 0; i < ((li)(to)); i++) #define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++) #define MOD 1000000007 li h, w; li a[88][88]; li b[88][88]; bitset<80 * 160 * 2 + 1> dp[88][88]; int main(void) { li offset = 80 * 160; cin >> h >> w; rep(i, h) { rep(j, w) { cin >> a[i][j]; } } rep(i, h) { rep(j, w) { cin >> b[i][j]; } } li lim = 0; rep(i, h) { rep(j, w) { li geta = min(a[i][j], b[i][j]); a[i][j] -= geta; b[i][j] -= geta; lim += a[i][j]; lim += b[i][j]; } } dp[0][0][a[0][0] - b[0][0] + offset] = true; dp[0][0][-a[0][0] + b[0][0] + offset] = true; // first line repp(i, 1, w) { li val = abs(a[0][i] - b[0][i]); dp[0][i] |= dp[0][i - 1] << val; dp[0][i] |= dp[0][i - 1] >> val; } repp(i, 1, h) { { li val = abs(a[i][0] - b[i][0]); dp[i][0] |= dp[i - 1][0] << val; dp[i][0] |= dp[i - 1][0] >> val; } repp(j, 1, w) { li now = abs(a[i][j] - b[i][j]); dp[i][j] |= dp[i - 1][j] << now; dp[i][j] |= dp[i - 1][j] >> now; dp[i][j] |= dp[i][j - 1] << now; dp[i][j] |= dp[i][j - 1] >> now; } } li res = 1 << 30; rep(i, 80 * 160 * 2 + 1) { if (dp[h - 1][w - 1][i]) { res = min(res, abs(offset - i)); } } cout << res << endl; return 0; }
replace
44
45
44
45
TLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; typedef pair<int, int> P; #define bit(n) (1LL << (n)) // #define int long long #define all(v) v.begin(), v.end() #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i < n; i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++) template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); } template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); } #define mod (ll)(1e9 + 7) const long long INF = 1LL << 60; signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); ll h, w; cin >> h >> w; vvll a(h, vll(w)); rep(y, h) { rep(x, w) { cin >> a[y][x]; } } rep(y, h) { rep(x, w) { ll tmp; cin >> tmp; a[y][x] = abs(a[y][x] - tmp); } } const int max_val = 80 * h * w; using vvvi = vector<vvi>; vvvi dp(h, vvi(w, vi(max_val + 1))); using vvb = vector<vb>; vvb visited(h, vb(w)); queue<P> que; dp[0][0][a[0][0]] = 1; que.emplace(0, 1); que.emplace(1, 0); while (!que.empty()) { auto q = que.front(); que.pop(); int x = q.first; int y = q.second; if (x > 0) { for (int i = 0; i <= max_val; i++) { dp[y][x][i + a[y][x]] |= dp[y][x - 1][i]; dp[y][x][abs(i - a[y][x])] |= dp[y][x - 1][i]; } } if (y > 0) { for (int i = 0; i <= max_val; i++) { dp[y][x][i + a[y][x]] |= dp[y - 1][x][i]; dp[y][x][abs(i - a[y][x])] |= dp[y - 1][x][i]; } } if (y < h - 1) { if (!visited[y + 1][x]) { visited[y + 1][x] = true; que.emplace(x, y + 1); } } if (x < w - 1) { if (!visited[y][x + 1]) { visited[y][x + 1] = true; que.emplace(x + 1, y); } } } int ans = 123456; for (int i = 0; i <= max_val; i++) { if (dp[h - 1][w - 1][i]) { ans = i; break; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; typedef pair<int, int> P; #define bit(n) (1LL << (n)) // #define int long long #define all(v) v.begin(), v.end() #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i < n; i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++) template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); } template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); } #define mod (ll)(1e9 + 7) const long long INF = 1LL << 60; signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); ll h, w; cin >> h >> w; vvll a(h, vll(w)); rep(y, h) { rep(x, w) { cin >> a[y][x]; } } rep(y, h) { rep(x, w) { ll tmp; cin >> tmp; a[y][x] = abs(a[y][x] - tmp); } } const int max_val = 80 * (h + w); using vvvi = vector<vvi>; vvvi dp(h, vvi(w, vi(max_val + 1))); using vvb = vector<vb>; vvb visited(h, vb(w)); queue<P> que; dp[0][0][a[0][0]] = 1; que.emplace(0, 1); que.emplace(1, 0); while (!que.empty()) { auto q = que.front(); que.pop(); int x = q.first; int y = q.second; if (x > 0) { for (int i = 0; i <= max_val; i++) { dp[y][x][i + a[y][x]] |= dp[y][x - 1][i]; dp[y][x][abs(i - a[y][x])] |= dp[y][x - 1][i]; } } if (y > 0) { for (int i = 0; i <= max_val; i++) { dp[y][x][i + a[y][x]] |= dp[y - 1][x][i]; dp[y][x][abs(i - a[y][x])] |= dp[y - 1][x][i]; } } if (y < h - 1) { if (!visited[y + 1][x]) { visited[y + 1][x] = true; que.emplace(x, y + 1); } } if (x < w - 1) { if (!visited[y][x + 1]) { visited[y][x + 1] = true; que.emplace(x + 1, y); } } } int ans = 123456; for (int i = 0; i <= max_val; i++) { if (dp[h - 1][w - 1][i]) { ans = i; break; } } cout << ans << endl; return 0; }
replace
55
56
55
56
0
p02839
C++
Memory Limit Exceeded
#include <bits/stdc++.h> #define int long long #define pb push_back #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define sz(a) int((a).size()) #define endl '\n' using namespace std; typedef vector<int> vi; typedef pair<int, int> ii; typedef long long ll; const int maxn = 85; const int INF = 1e9 + 10; ii g[maxn][maxn]; int dp[maxn][maxn][3 * maxn * maxn]; int w, h; int get_ans(int i, int j, int somaa, int somab, int diff) { if (i > h || j > w) return INF; if (dp[i][j][diff] != -1) return dp[i][j][diff]; if (i == h && j == w) { int s1 = (somaa + g[i][j].ff); int s2 = (somaa + g[i][j].ss); int s3 = (somab + g[i][j].ff); int s4 = (somab + g[i][j].ss); return min(abs(s1 - s4), abs(s2 - s3)); } int ans1 = get_ans(i + 1, j, somaa + g[i][j].ff, somab + g[i][j].ss, abs((somaa + g[i][j].ff) - (somab + g[i][j].ss))); int ans2 = get_ans(i + 1, j, somaa + g[i][j].ss, somab + g[i][j].ff, abs((somaa + g[i][j].ss) - (somab + g[i][j].ff))); int ans3 = get_ans(i, j + 1, somaa + g[i][j].ff, somab + g[i][j].ss, abs((somaa + g[i][j].ff) - (somab + g[i][j].ss))); int ans4 = get_ans(i, j + 1, somaa + g[i][j].ss, somab + g[i][j].ff, abs((somaa + g[i][j].ss) - (somab + g[i][j].ff))); return dp[i][j][diff] = min({ans1, ans2, ans3, ans4}); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif cin >> h >> w; for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { cin >> g[i][j].ff; } } for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { cin >> g[i][j].ss; } } memset(dp, -1, sizeof dp); cout << get_ans(1, 1, 0, 0, 0) << endl; return 0; }
#include <bits/stdc++.h> #define int long long #define pb push_back #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define sz(a) int((a).size()) #define endl '\n' using namespace std; typedef vector<int> vi; typedef pair<int, int> ii; typedef long long ll; const int maxn = 85; const int INF = 1e9 + 10; ii g[maxn][maxn]; int dp[maxn][maxn][2 * maxn * maxn]; int w, h; int get_ans(int i, int j, int somaa, int somab, int diff) { if (i > h || j > w) return INF; if (dp[i][j][diff] != -1) return dp[i][j][diff]; if (i == h && j == w) { int s1 = (somaa + g[i][j].ff); int s2 = (somaa + g[i][j].ss); int s3 = (somab + g[i][j].ff); int s4 = (somab + g[i][j].ss); return min(abs(s1 - s4), abs(s2 - s3)); } int ans1 = get_ans(i + 1, j, somaa + g[i][j].ff, somab + g[i][j].ss, abs((somaa + g[i][j].ff) - (somab + g[i][j].ss))); int ans2 = get_ans(i + 1, j, somaa + g[i][j].ss, somab + g[i][j].ff, abs((somaa + g[i][j].ss) - (somab + g[i][j].ff))); int ans3 = get_ans(i, j + 1, somaa + g[i][j].ff, somab + g[i][j].ss, abs((somaa + g[i][j].ff) - (somab + g[i][j].ss))); int ans4 = get_ans(i, j + 1, somaa + g[i][j].ss, somab + g[i][j].ff, abs((somaa + g[i][j].ss) - (somab + g[i][j].ff))); return dp[i][j][diff] = min({ans1, ans2, ans3, ans4}); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif cin >> h >> w; for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { cin >> g[i][j].ff; } } for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { cin >> g[i][j].ss; } } memset(dp, -1, sizeof dp); cout << get_ans(1, 1, 0, 0, 0) << endl; return 0; }
replace
20
21
20
21
MLE
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using vl = vector<ll>; #define rep(i, n) for (ll i = 0; i < n; i++) #define all(i) i.begin(), i.end() template <class T, class U> bool cmax(T &a, U b) { if (a < b) { a = b; return true; } else return false; } template <class T, class U> bool cmin(T &a, U b) { if (a > b) { a = b; return true; } else return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll h, w; cin >> h >> w; vector<vl> a(h, vl(w)); vector<vl> b(h, vl(w)); rep(i, h) { rep(j, w) { cin >> a[i][j]; } } rep(i, h) { rep(j, w) { cin >> b[i][j]; } } vector<vl> c(h, vl(w)); rep(i, h) { rep(j, w) { c[i][j] = a[i][j] - b[i][j]; // cerr << c[i][j] << " "; } // cerr << endl; } //( h+ w) * max(ai) * 2 * 2(+-) ll ma = (80 + 80) * 80 * 2 * 2 * 2; ll ze = ma / 2; ma *= 2; ma += 1000; vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(ma))); dp[0][0][c[0][0] + ze] = true; rep(i, h) { rep(j, w) { for (ll k = -ze; k <= ze; k++) { if (i - 1 >= 0 && k + c[i][j] + ze >= 0) { if (dp[i - 1][j][k + c[i][j] + ze]) dp[i][j][k + ze] = 1; } if (i - 1 >= 0 && k - c[i][j] + ze >= 0) { if (dp[i - 1][j][k - c[i][j] + ze]) dp[i][j][k + ze] = 1; } if (j - 1 >= 0 && k + c[i][j] + ze >= 0) { if (dp[i][j - 1][k + c[i][j] + ze]) dp[i][j][k + ze] = 1; } if (j - 1 >= 0 && k - c[i][j] + ze >= 0) { if (dp[i][j - 1][k - c[i][j] + ze]) dp[i][j][k + ze] = 1; } } } } ll ans = 1e20; for (ll k = -ze; k <= ze; k++) { if (dp[h - 1][w - 1][k + ze]) { cmin(ans, abs(k)); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using vl = vector<ll>; #define rep(i, n) for (ll i = 0; i < n; i++) #define all(i) i.begin(), i.end() template <class T, class U> bool cmax(T &a, U b) { if (a < b) { a = b; return true; } else return false; } template <class T, class U> bool cmin(T &a, U b) { if (a > b) { a = b; return true; } else return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll h, w; cin >> h >> w; vector<vl> a(h, vl(w)); vector<vl> b(h, vl(w)); rep(i, h) { rep(j, w) { cin >> a[i][j]; } } rep(i, h) { rep(j, w) { cin >> b[i][j]; } } vector<vl> c(h, vl(w)); rep(i, h) { rep(j, w) { c[i][j] = a[i][j] - b[i][j]; // cerr << c[i][j] << " "; } // cerr << endl; } //( h+ w) * max(ai) * 2 * 2(+-) ll ma = (80 + 80) * 80 * 2 * 2; ll ze = ma / 2; ma *= 2; ma += 1000; vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(ma))); dp[0][0][c[0][0] + ze] = true; rep(i, h) { rep(j, w) { for (ll k = -ze; k <= ze; k++) { if (i - 1 >= 0 && k + c[i][j] + ze >= 0) { if (dp[i - 1][j][k + c[i][j] + ze]) dp[i][j][k + ze] = 1; } if (i - 1 >= 0 && k - c[i][j] + ze >= 0) { if (dp[i - 1][j][k - c[i][j] + ze]) dp[i][j][k + ze] = 1; } if (j - 1 >= 0 && k + c[i][j] + ze >= 0) { if (dp[i][j - 1][k + c[i][j] + ze]) dp[i][j][k + ze] = 1; } if (j - 1 >= 0 && k - c[i][j] + ze >= 0) { if (dp[i][j - 1][k - c[i][j] + ze]) dp[i][j][k + ze] = 1; } } } } ll ans = 1e20; for (ll k = -ze; k <= ze; k++) { if (dp[h - 1][w - 1][k + ze]) { cmin(ans, abs(k)); } } cout << ans << endl; }
replace
49
50
49
50
TLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> #define FOR(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<int, int> pii; const int K = 15000; const int N = 80; int h, w; int a[N][N], b[N][N]; bool dp[N][N][2 * K]; int put(int x) { return x + K; } int get(int x) { return x - K; } int main() { scanf("%d%d", &h, &w); FOR(i, h) FOR(j, w) scanf("%d", &a[i][j]); FOR(i, h) FOR(j, w) scanf("%d", &b[i][j]); dp[0][0][put(a[0][0] - b[0][0])] = dp[0][0][put(b[0][0] - a[0][0])] = true; FOR(i, h) FOR(j, w) { if (!i && !j) continue; int raz = a[i][j] - b[i][j]; FOR(di, 2) FOR(dj, 2) { if (di == dj) continue; FOR(u, (2 * K)) { if (!dp[i - di][j - dj][u]) continue; dp[i][j][u + raz] = dp[i][j][u - raz] = true; } } } int mini = 1e9; FOR(u, (2 * K)) { if (!dp[h - 1][w - 1][u]) continue; mini = min(mini, abs(get(u))); } printf("%d\n", mini); return 0; }
#include <bits/stdc++.h> #define FOR(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<int, int> pii; const int K = 15000; const int N = 80; int h, w; int a[N][N], b[N][N]; bool dp[N][N][2 * K]; int put(int x) { return x + K; } int get(int x) { return x - K; } int main() { scanf("%d%d", &h, &w); FOR(i, h) FOR(j, w) scanf("%d", &a[i][j]); FOR(i, h) FOR(j, w) scanf("%d", &b[i][j]); dp[0][0][put(a[0][0] - b[0][0])] = dp[0][0][put(b[0][0] - a[0][0])] = true; FOR(i, h) FOR(j, w) { if (!i && !j) continue; int raz = a[i][j] - b[i][j]; FOR(di, 2) FOR(dj, 2) { if (di == dj) continue; if (i - di < 0 || j - dj < 0) continue; FOR(u, (2 * K)) { if (!dp[i - di][j - dj][u]) continue; dp[i][j][u + raz] = dp[i][j][u - raz] = true; } } } int mini = 1e9; FOR(u, (2 * K)) { if (!dp[h - 1][w - 1][u]) continue; mini = min(mini, abs(get(u))); } printf("%d\n", mini); return 0; }
insert
32
32
32
34
-11
p02839
C++
Memory Limit Exceeded
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <locale> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef long long LL; typedef vector<int> VI; typedef vector<bool> VB; typedef vector<char> VC; typedef vector<double> VD; typedef vector<string> VS; typedef vector<LL> VLL; typedef vector<VI> VVI; typedef vector<VB> VVB; typedef vector<VS> VVS; typedef vector<VLL> VVLL; typedef vector<VVI> VVVI; typedef vector<VVLL> VVVLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<int, string> PIS; typedef pair<string, int> PSI; typedef pair<string, string> PSS; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<VPII> VVPII; typedef vector<VPLL> VVPLL; typedef vector<VS> VVS; typedef map<int, int> MII; typedef map<LL, LL> MLL; typedef map<string, int> MSI; typedef map<int, string> MIS; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #define EACH(i, arr) \ for (typeof((arr).begin()) i = (arr).begin(); i != (arr).end(); ++i) #define EXIST(str, e) ((str).find(e) != (str).end()) #define COUNT(arr, v) count((arr).begin(), (arr).end(), v) #define SEARCH(v, w) search((v).begin(), (v).end(), (w).begin(), (w).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define REVERSE(c) reverse((c).begin(), (c).end()) #define ROTATE_LEFT(arr, c) \ rotate((arr).begin(), (arr).begin() + (c), (arr).end()) #define ROTATE_RIGHT(arr, c) \ rotate((arr).rbegin(), (arr).rbegin() + (c), (arr).rend()) #define SUMI(arr) accumulate((arr).begin(), (arr).end(), 0) #define SUMD(arr) accumulate((arr).begin(), (arr).end(), 0.) #define SUMLL(arr) accumulate((arr).begin(), (arr).end(), 0LL) #define MULD(arr) \ accumulate((arr).begin(), (arr).end(), 1., multiplies<double>()) #define UB(arr, n) upper_bound((arr).begin(), (arr).end(), n) #define LB(arr, n) lower_bound((arr).begin(), (arr).end(), n) #define PB push_back #define MP make_pair #define ft first #define sd second // input output //------------------------------------------ #define GL(s) getline(cin, (s)) #define INIT() \ std::ios::sync_with_stdio(false); \ std::cin.tie(0) #define OUT(d) std::cout << (d) #define OUT_L(d) std::cout << (d) << endl #define FOUT(n, data) std::cout << std::fixed << std::setprecision(n) << (data) #define FOUT_L(n, data) \ std::cout << std::fixed << std::setprecision(n) << (data) << "\n" #define EL() printf("\n") #define SHOW_VECTOR(v) \ { \ std::cerr << #v << "\t:"; \ for (const auto &xxx : v) { \ std::cerr << xxx << " "; \ } \ std::cerr << "\n"; \ } #define SHOW_MAP(v) \ { \ std::cerr << #v << endl; \ for (const auto &xxx : v) { \ std::cerr << xxx.first << " " << xxx.second << "\n"; \ } \ } #define Yes() printf("Yes\n") #define No() printf("No\n") #define YES() printf("YES\n") #define NO() printf("NO\n") #define Yay() printf("Yay!\n") #define Nnn() printf(":(\n") #define CE(x, y) ((x + y - 1) / (y)) template <typename T1, typename T2> istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &x : v) in >> x; return in; } template <typename T1, typename T2> ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { out << "[" << p.first << ", " << p.second << "]" << "\n"; return out; } template <class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } return false; } template <class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; } // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) for (int i = n - 1; i >= 0; i--) #define FORLL(i, a, b) for (LL i = LL(a); i < LL(b); ++i) #define RFORLL(i, a, b) for (LL i = LL(b) - 1; i >= LL(a); --i) #define REPLL(i, n) for (LL i = 0; i < LL(n); ++i) #define RREPLL(i, n) for (LL i = LL(n) - 1; i >= 0; --i) #define FOREACH(x, arr) for (auto &(x) : (arr)) //------------------------------------------ //------------------------------------------ constexpr LL MAX = 20000; int main() { LL H, W; cin >> H >> W; VVLL A(H, VLL(W)); VVLL B(H, VLL(W)); cin >> A >> B; REP(i, H) { REP(j, W) { if (A[i][j] < B[i][j]) { swap(A[i][j], B[i][j]); } } } VVVLL dp(H, VVLL(W, VLL(MAX, 0))); dp[0][0][A[0][0] - B[0][0]] = 1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k < MAX; k++) { if (dp[i][j][k]) { if (i < H - 1) { for (int l = 0; l < 2; l++) { LL to = k + (A[i + 1][j] - B[i + 1][j]); to = abs(to); if (to >= 0) { dp[i + 1][j][to] = 1; } swap(A[i + 1][j], B[i + 1][j]); } } if (j < W - 1) { for (int l = 0; l < 2; l++) { LL to = k + (A[i][j + 1] - B[i][j + 1]); to = abs(to); if (to >= 0) { dp[i][j + 1][to] = 1; } swap(A[i][j + 1], B[i][j + 1]); } } } } } } LL ans = 1e18; for (LL i = 0; i < MAX; i++) { if (dp[H - 1][W - 1][i]) { ans = min(ans, i); } } cout << ans << endl; return 0; }
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <locale> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef long long LL; typedef vector<int> VI; typedef vector<bool> VB; typedef vector<char> VC; typedef vector<double> VD; typedef vector<string> VS; typedef vector<LL> VLL; typedef vector<VI> VVI; typedef vector<VB> VVB; typedef vector<VS> VVS; typedef vector<VLL> VVLL; typedef vector<VVI> VVVI; typedef vector<VVLL> VVVLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<int, string> PIS; typedef pair<string, int> PSI; typedef pair<string, string> PSS; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<VPII> VVPII; typedef vector<VPLL> VVPLL; typedef vector<VS> VVS; typedef map<int, int> MII; typedef map<LL, LL> MLL; typedef map<string, int> MSI; typedef map<int, string> MIS; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #define EACH(i, arr) \ for (typeof((arr).begin()) i = (arr).begin(); i != (arr).end(); ++i) #define EXIST(str, e) ((str).find(e) != (str).end()) #define COUNT(arr, v) count((arr).begin(), (arr).end(), v) #define SEARCH(v, w) search((v).begin(), (v).end(), (w).begin(), (w).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define REVERSE(c) reverse((c).begin(), (c).end()) #define ROTATE_LEFT(arr, c) \ rotate((arr).begin(), (arr).begin() + (c), (arr).end()) #define ROTATE_RIGHT(arr, c) \ rotate((arr).rbegin(), (arr).rbegin() + (c), (arr).rend()) #define SUMI(arr) accumulate((arr).begin(), (arr).end(), 0) #define SUMD(arr) accumulate((arr).begin(), (arr).end(), 0.) #define SUMLL(arr) accumulate((arr).begin(), (arr).end(), 0LL) #define MULD(arr) \ accumulate((arr).begin(), (arr).end(), 1., multiplies<double>()) #define UB(arr, n) upper_bound((arr).begin(), (arr).end(), n) #define LB(arr, n) lower_bound((arr).begin(), (arr).end(), n) #define PB push_back #define MP make_pair #define ft first #define sd second // input output //------------------------------------------ #define GL(s) getline(cin, (s)) #define INIT() \ std::ios::sync_with_stdio(false); \ std::cin.tie(0) #define OUT(d) std::cout << (d) #define OUT_L(d) std::cout << (d) << endl #define FOUT(n, data) std::cout << std::fixed << std::setprecision(n) << (data) #define FOUT_L(n, data) \ std::cout << std::fixed << std::setprecision(n) << (data) << "\n" #define EL() printf("\n") #define SHOW_VECTOR(v) \ { \ std::cerr << #v << "\t:"; \ for (const auto &xxx : v) { \ std::cerr << xxx << " "; \ } \ std::cerr << "\n"; \ } #define SHOW_MAP(v) \ { \ std::cerr << #v << endl; \ for (const auto &xxx : v) { \ std::cerr << xxx.first << " " << xxx.second << "\n"; \ } \ } #define Yes() printf("Yes\n") #define No() printf("No\n") #define YES() printf("YES\n") #define NO() printf("NO\n") #define Yay() printf("Yay!\n") #define Nnn() printf(":(\n") #define CE(x, y) ((x + y - 1) / (y)) template <typename T1, typename T2> istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &x : v) in >> x; return in; } template <typename T1, typename T2> ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { out << "[" << p.first << ", " << p.second << "]" << "\n"; return out; } template <class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } return false; } template <class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; } // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) for (int i = n - 1; i >= 0; i--) #define FORLL(i, a, b) for (LL i = LL(a); i < LL(b); ++i) #define RFORLL(i, a, b) for (LL i = LL(b) - 1; i >= LL(a); --i) #define REPLL(i, n) for (LL i = 0; i < LL(n); ++i) #define RREPLL(i, n) for (LL i = LL(n) - 1; i >= 0; --i) #define FOREACH(x, arr) for (auto &(x) : (arr)) //------------------------------------------ //------------------------------------------ constexpr LL MAX = 13000; int main() { LL H, W; cin >> H >> W; VVLL A(H, VLL(W)); VVLL B(H, VLL(W)); cin >> A >> B; REP(i, H) { REP(j, W) { if (A[i][j] < B[i][j]) { swap(A[i][j], B[i][j]); } } } VVVLL dp(H, VVLL(W, VLL(MAX, 0))); dp[0][0][A[0][0] - B[0][0]] = 1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k < MAX; k++) { if (dp[i][j][k]) { if (i < H - 1) { for (int l = 0; l < 2; l++) { LL to = k + (A[i + 1][j] - B[i + 1][j]); to = abs(to); if (to >= 0) { dp[i + 1][j][to] = 1; } swap(A[i + 1][j], B[i + 1][j]); } } if (j < W - 1) { for (int l = 0; l < 2; l++) { LL to = k + (A[i][j + 1] - B[i][j + 1]); to = abs(to); if (to >= 0) { dp[i][j + 1][to] = 1; } swap(A[i][j + 1], B[i][j + 1]); } } } } } } LL ans = 1e18; for (LL i = 0; i < MAX; i++) { if (dp[H - 1][W - 1][i]) { ans = min(ans, i); } } cout << ans << endl; return 0; }
replace
180
181
180
181
MLE
p02839
C++
Runtime Error
//***Little by little one travels far*** #include <bits/stdc++.h> using namespace std; #define mp make_pair #define mt make_tuple #define fi first #define se second #define pb push_back #define ll long long #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep(i, n) for (i = 0; i < n; i++) #define forn(i, n) for (ll i = 0; i < (ll)(n); ++i) #define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i) #define ford(i, n) for (ll i = (ll)(n)-1; i >= 0; --i) #define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i) #define fora(it, x) for (auto it : x) #define PI 3.14159265 #define sync \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define endl "\n" typedef pair<ll, ll> pii; typedef vector<ll> vi; typedef vector<pii> vpi; typedef vector<vi> vvi; typedef long long i64; typedef vector<i64> vi64; typedef vector<vi64> vvi64; typedef pair<i64, i64> pi64; typedef long double ld; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } ll a[81][81]; ll b[81][81]; int main() { ll h, w; cin >> h >> w; forn(i, h) forn(j, w) cin >> a[i][j]; forn(i, h) forn(j, w) cin >> b[i][j]; ll dp[h][w][80 * (h + w) + 1]; forn(i, h) forn(j, w) forn(k, (ll)80 * (h + w) + 1) dp[i][j][k] = 0; dp[0][0][abs(a[0][0] - b[0][0])] = 1; forn(i, h) forn(j, w) forn(k, (ll)80 * (h + w) + 1) { if ((i - 1) >= 0) dp[i][j][k] |= (dp[i - 1][j][k + abs(a[i][j] - b[i][j])] | dp[i - 1][j][abs(k - abs(a[i][j] - b[i][j]))]); if ((j - 1) >= 0) dp[i][j][k] |= (dp[i][j - 1][k + abs(a[i][j] - b[i][j])] | dp[i][j - 1][abs(k - abs(a[i][j] - b[i][j]))]); // cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl; } ll m = 80 * (h + w); forn(k, (ll)80 * (h + w) + 1) if (dp[h - 1][w - 1][k] == 1) m = min(m, k); cout << m; }
//***Little by little one travels far*** #include <bits/stdc++.h> using namespace std; #define mp make_pair #define mt make_tuple #define fi first #define se second #define pb push_back #define ll long long #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep(i, n) for (i = 0; i < n; i++) #define forn(i, n) for (ll i = 0; i < (ll)(n); ++i) #define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i) #define ford(i, n) for (ll i = (ll)(n)-1; i >= 0; --i) #define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i) #define fora(it, x) for (auto it : x) #define PI 3.14159265 #define sync \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define endl "\n" typedef pair<ll, ll> pii; typedef vector<ll> vi; typedef vector<pii> vpi; typedef vector<vi> vvi; typedef long long i64; typedef vector<i64> vi64; typedef vector<vi64> vvi64; typedef pair<i64, i64> pi64; typedef long double ld; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } ll a[81][81]; ll b[81][81]; int main() { ll h, w; cin >> h >> w; forn(i, h) forn(j, w) cin >> a[i][j]; forn(i, h) forn(j, w) cin >> b[i][j]; bool dp[h][w][80 * (h + w) + 1]; forn(i, h) forn(j, w) forn(k, (ll)80 * (h + w) + 1) dp[i][j][k] = 0; dp[0][0][abs(a[0][0] - b[0][0])] = 1; forn(i, h) forn(j, w) forn(k, (ll)80 * (h + w) + 1) { if ((i - 1) >= 0) dp[i][j][k] |= (dp[i - 1][j][k + abs(a[i][j] - b[i][j])] | dp[i - 1][j][abs(k - abs(a[i][j] - b[i][j]))]); if ((j - 1) >= 0) dp[i][j][k] |= (dp[i][j - 1][k + abs(a[i][j] - b[i][j])] | dp[i][j - 1][abs(k - abs(a[i][j] - b[i][j]))]); // cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl; } ll m = 80 * (h + w); forn(k, (ll)80 * (h + w) + 1) if (dp[h - 1][w - 1][k] == 1) m = min(m, k); cout << m; }
replace
49
50
49
50
0
p02839
C++
Runtime Error
// https://atcoder.jp/contests/abc147/tasks/abc147_e #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++) #define DUMP(a, n) REP(_i, n) printf("%d%c", a[_i], _i + 1 == n ? '\n' : ' ') #define DUMP2D(a, n, m) \ REP(_i, n) REP(_j, m) printf("%d%c", a[_i][_j], _j + 1 == m ? '\n' : ' '); \ puts("") const int H_MAX = 80; const int W_MAX = 80; const int A_IJ_MAX = 80; const int B_IJ_MAX = 80; const int BS_SIZE = 6400; int H, W; int A[H_MAX][W_MAX]; int B[H_MAX][W_MAX]; bitset<BS_SIZE> dp0[W_MAX]; bitset<BS_SIZE> dp1[W_MAX]; bitset<BS_SIZE> wk; bitset<BS_SIZE> &move_bs(bitset<BS_SIZE> &bs, int v) { wk.reset(); REP(i, BS_SIZE) if (bs[i]) { wk.set(i + v); wk.set(abs(i - v)); } return wk; } void solve() { auto cur = dp0; auto nxt = dp1; REP(i, H) { REP(j, W) { nxt[j].reset(); int v = abs(A[i][j] - B[i][j]); if (i == 0 && j == 0) nxt[j].set(v); if (i > 0) nxt[j] |= move_bs(cur[j], v); if (j > 0) nxt[j] |= move_bs(nxt[j - 1], v); // printf("[%d, %d] ", i, j); // REP(k, BS_SIZE) if (nxt[j][k]) printf("%d,", k); // puts(""); } swap(cur, nxt); } REP(i, BS_SIZE) { if (cur[W - 1][i]) { printf("%d\n", i); return; } } } void input() { scanf("%d%d", &H, &W); REP(i, H) REP(j, W) scanf("%d", &A[i][j]); REP(i, H) REP(j, W) scanf("%d", &B[i][j]); } int main() { input(); solve(); return 0; }
// https://atcoder.jp/contests/abc147/tasks/abc147_e #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++) #define DUMP(a, n) REP(_i, n) printf("%d%c", a[_i], _i + 1 == n ? '\n' : ' ') #define DUMP2D(a, n, m) \ REP(_i, n) REP(_j, m) printf("%d%c", a[_i][_j], _j + 1 == m ? '\n' : ' '); \ puts("") const int H_MAX = 80; const int W_MAX = 80; const int A_IJ_MAX = 80; const int B_IJ_MAX = 80; const int BS_SIZE = 6400; int H, W; int A[H_MAX][W_MAX]; int B[H_MAX][W_MAX]; bitset<BS_SIZE> dp0[W_MAX]; bitset<BS_SIZE> dp1[W_MAX]; bitset<BS_SIZE> wk; bitset<BS_SIZE> &move_bs(bitset<BS_SIZE> &bs, int v) { wk.reset(); REP(i, BS_SIZE) if (bs[i]) { if (i + v < BS_SIZE) wk.set(i + v); if (abs(i - v) < BS_SIZE) wk.set(abs(i - v)); } return wk; } void solve() { auto cur = dp0; auto nxt = dp1; REP(i, H) { REP(j, W) { nxt[j].reset(); int v = abs(A[i][j] - B[i][j]); if (i == 0 && j == 0) nxt[j].set(v); if (i > 0) nxt[j] |= move_bs(cur[j], v); if (j > 0) nxt[j] |= move_bs(nxt[j - 1], v); // printf("[%d, %d] ", i, j); // REP(k, BS_SIZE) if (nxt[j][k]) printf("%d,", k); // puts(""); } swap(cur, nxt); } REP(i, BS_SIZE) { if (cur[W - 1][i]) { printf("%d\n", i); return; } } } void input() { scanf("%d%d", &H, &W); REP(i, H) REP(j, W) scanf("%d", &A[i][j]); REP(i, H) REP(j, W) scanf("%d", &B[i][j]); } int main() { input(); solve(); return 0; }
replace
30
32
30
34
0
p02839
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, N) for (int i = 0; i < N; i++) #define rep2(i, N) for (int i = 1; i <= N; i++) using namespace std; long long INF = 1000000000000000000; long long mod = 1000000007; using namespace std; int a[85][85]; int b[85][85]; int main() { int h, w; cin >> h >> w; rep2(i, h) { rep2(k, w) { cin >> a[i][k]; } } rep2(i, h) { rep2(k, w) { cin >> b[i][k]; } } long long size = 2 * 80 * 160 + 1; bool dp[size][h + 1][h + 1]; rep(i, size) rep2(y, h) rep2(x, w) dp[i][y][x] = false; dp[abs(a[1][1] - b[1][1])][1][1] = true; rep2(y, h) { rep2(x, w) { for (int i = 0; i < size; i++) { if (!dp[i][y][x]) continue; if (x + 1 <= w) { int p = abs(a[y][x + 1] - b[y][x + 1]); dp[abs(i - p)][y][x + 1] = true; dp[i + p][y][x + 1] = true; } if (y + 1 <= h) { int p = abs(a[y + 1][x] - b[y + 1][x]); dp[abs(i - p)][y + 1][x] = true; dp[i + p][y + 1][x] = true; } } } } for (int i = 0; i < size; i++) { if (dp[i][h][w]) { cout << i << endl; break; } } return 0; } /* */
#include <bits/stdc++.h> #define rep(i, N) for (int i = 0; i < N; i++) #define rep2(i, N) for (int i = 1; i <= N; i++) using namespace std; long long INF = 1000000000000000000; long long mod = 1000000007; using namespace std; int a[85][85]; int b[85][85]; int main() { int h, w; cin >> h >> w; rep2(i, h) { rep2(k, w) { cin >> a[i][k]; } } rep2(i, h) { rep2(k, w) { cin >> b[i][k]; } } long long size = 2 * 80 * 160 + 1; bool dp[size][h + 1][w + 1]; rep(i, size) rep2(y, h) rep2(x, w) dp[i][y][x] = false; dp[abs(a[1][1] - b[1][1])][1][1] = true; rep2(y, h) { rep2(x, w) { for (int i = 0; i < size; i++) { if (!dp[i][y][x]) continue; if (x + 1 <= w) { int p = abs(a[y][x + 1] - b[y][x + 1]); dp[abs(i - p)][y][x + 1] = true; dp[i + p][y][x + 1] = true; } if (y + 1 <= h) { int p = abs(a[y + 1][x] - b[y + 1][x]); dp[abs(i - p)][y + 1][x] = true; dp[i + p][y + 1][x] = true; } } } } for (int i = 0; i < size; i++) { if (dp[i][h][w]) { cout << i << endl; break; } } return 0; } /* */
replace
24
25
24
25
0
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define modulo 1000000007 #define mod(mod_x) ((((long long)mod_x + modulo)) % modulo) #define Inf 10000000000000000 int main() { int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)); vector<vector<int>> B(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> B[i][j]; } } vector<vector<bitset<14000>>> flag(H, vector<bitset<14000>>(W)); flag[0][0][7000 + A[0][0] - B[0][0]] = true; flag[0][0][7000 - A[0][0] + B[0][0]] = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k < 14000; k++) { if (flag[i][j][k] == false) continue; if (i != H - 1) { int a = A[i + 1][j] - B[i + 1][j]; int b = -a; if (a > 0) { flag[i + 1][j] |= (flag[i][j] << a); } else { flag[i + 1][j] |= (flag[i][j] >> (-a)); } if (b > 0) { flag[i + 1][j] |= (flag[i][j] << b); } else { flag[i + 1][j] |= (flag[i][j] >> (-b)); } } if (j != W - 1) { int a = A[i][j + 1] - B[i][j + 1]; int b = -a; if (a > 0) { flag[i][j + 1] |= (flag[i][j] << a); } else { flag[i][j + 1] |= (flag[i][j] >> (-a)); } if (b > 0) { flag[i][j + 1] |= (flag[i][j] << b); } else { flag[i][j + 1] |= (flag[i][j] >> (-b)); } } } } } int ans = 10000000; for (int i = 0; i < 14000; i++) { if (flag[H - 1][W - 1][i]) { ans = min(ans, abs(7000 - i)); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define modulo 1000000007 #define mod(mod_x) ((((long long)mod_x + modulo)) % modulo) #define Inf 10000000000000000 int main() { int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)); vector<vector<int>> B(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> B[i][j]; } } vector<vector<bitset<14000>>> flag(H, vector<bitset<14000>>(W)); flag[0][0][7000 + A[0][0] - B[0][0]] = true; flag[0][0][7000 - A[0][0] + B[0][0]] = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (i != H - 1) { int a = abs(A[i + 1][j] - B[i + 1][j]); flag[i + 1][j] |= (flag[i][j] << a); flag[i + 1][j] |= (flag[i][j] >> a); } if (j != W - 1) { int a = abs(A[i][j + 1] - B[i][j + 1]); flag[i][j + 1] |= (flag[i][j] << a); flag[i][j + 1] |= (flag[i][j] >> a); } } } int ans = 10000000; for (int i = 0; i < 14000; i++) { if (flag[H - 1][W - 1][i]) { ans = min(ans, abs(7000 - i)); } } cout << ans << endl; return 0; }
replace
32
63
32
41
TLE
p02839
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #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 all(a) (a).begin(), (a).end() #define bit(k) (1LL << (k)) #define SUM(v) accumulate(all(v), 0LL) typedef pair<int, int> i_i; typedef pair<ll, ll> l_l; // template <class T> using vec = vector<T>; // template <class T> using vvec = vector<vec<T>>; template <class T> vector<T> vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); } struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; 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 TOSTRING(x) string(#x) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; for (auto _ : v) os << _ << ", "; os << "]"; return os; }; template <typename T> ostream &operator<<(ostream &os, set<T> &st) { os << "("; for (auto _ : st) { os << _ << ", "; } os << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "{" << p.first << ", " << p.second << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &mp) { os << "["; for (auto _ : mp) { os << _ << ", "; } os << "]" << endl; return os; } #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG #define dbg(...) dump_func(__VA_ARGS__) #define dump(...) \ DUMPOUT << string(#__VA_ARGS__) << ": "; \ dump_func(__VA_ARGS__) #else #define dbg(...) #define dump(...) #endif const int INF = (ll)1e9; const ll INFLL = (ll)1e18 + 1; const ll MOD = 1000000007; // const ll MOD = 998244353; const long double PI = acos(-1.0); /* const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const string dir = "DRUL"; */ int main() { int H, W; cin >> H >> W; // vvec<int> A(H, vec<int>(W)); // vvec<int> B(H, vec<int>(W)); auto A = vec<int>(H, W); auto B = vec<int>(H, W); cin >> A >> B; const int M = 100000; auto dp = vec<int>(H, W, M); dp[0][0][abs(A[0][0] - B[0][0])] = 1; rep(i, H) rep(j, W) rep(k, M) { if (dp[i][j][k]) { if (i + 1 < H) { dp[i + 1][j][k + abs(A[i + 1][j] - B[i + 1][j])] = 1; dp[i + 1][j][abs(k - abs(A[i + 1][j] - B[i + 1][j]))] = 1; } if (j + 1 < W) { dp[i][j + 1][k + abs(A[i][j + 1] - B[i][j + 1])] = 1; dp[i][j + 1][abs(k - abs(A[i][j + 1] - B[i][j + 1]))] = 1; } } } int ans = INF; rep(k, M) { if (dp[H - 1][W - 1][k]) chmin(ans, k); } cout << ans << endl; } /* dp[x座標][y座標][そのマスをどっちにぬったか][達成される差] */
#include <bits/stdc++.h> using namespace std; typedef long long ll; #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 all(a) (a).begin(), (a).end() #define bit(k) (1LL << (k)) #define SUM(v) accumulate(all(v), 0LL) typedef pair<int, int> i_i; typedef pair<ll, ll> l_l; // template <class T> using vec = vector<T>; // template <class T> using vvec = vector<vec<T>>; template <class T> vector<T> vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); } struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; 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 TOSTRING(x) string(#x) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; for (auto _ : v) os << _ << ", "; os << "]"; return os; }; template <typename T> ostream &operator<<(ostream &os, set<T> &st) { os << "("; for (auto _ : st) { os << _ << ", "; } os << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "{" << p.first << ", " << p.second << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &mp) { os << "["; for (auto _ : mp) { os << _ << ", "; } os << "]" << endl; return os; } #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG #define dbg(...) dump_func(__VA_ARGS__) #define dump(...) \ DUMPOUT << string(#__VA_ARGS__) << ": "; \ dump_func(__VA_ARGS__) #else #define dbg(...) #define dump(...) #endif const int INF = (ll)1e9; const ll INFLL = (ll)1e18 + 1; const ll MOD = 1000000007; // const ll MOD = 998244353; const long double PI = acos(-1.0); /* const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const string dir = "DRUL"; */ int main() { int H, W; cin >> H >> W; // vvec<int> A(H, vec<int>(W)); // vvec<int> B(H, vec<int>(W)); auto A = vec<int>(H, W); auto B = vec<int>(H, W); cin >> A >> B; const int M = 10000 * 2; auto dp = vec<int>(H, W, M); dp[0][0][abs(A[0][0] - B[0][0])] = 1; rep(i, H) rep(j, W) rep(k, M) { if (dp[i][j][k]) { if (i + 1 < H) { dp[i + 1][j][k + abs(A[i + 1][j] - B[i + 1][j])] = 1; dp[i + 1][j][abs(k - abs(A[i + 1][j] - B[i + 1][j]))] = 1; } if (j + 1 < W) { dp[i][j + 1][k + abs(A[i][j + 1] - B[i][j + 1])] = 1; dp[i][j + 1][abs(k - abs(A[i][j + 1] - B[i][j + 1]))] = 1; } } } int ans = INF; rep(k, M) { if (dp[H - 1][W - 1][k]) chmin(ans, k); } cout << ans << endl; } /* dp[x座標][y座標][そのマスをどっちにぬったか][達成される差] */
replace
117
118
117
118
MLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, m; int a[101][101], b[101][101]; bitset<20001> f[81][81]; int main(int argc, char **argv) { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> b[i][j]; f[1][1].set(10000); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int v1 = a[i][j] - b[i][j]; int v2 = b[i][j] - a[i][j]; if (v1 >= 0) { f[i + 1][j] |= (f[i][j] << v1); f[i][j + 1] |= (f[i][j] << v1); } else { v1 = -v1; f[i + 1][j] |= (f[i][j] >> v1); f[i][j + 1] |= (f[i][j] >> v1); } if (v2 >= 0) { f[i + 1][j] |= (f[i][j] << v2); f[i][j + 1] |= (f[i][j] << v2); } else { v2 = -v2; f[i + 1][j] |= (f[i][j] >> v2); f[i][j + 1] |= (f[i][j] >> v2); } } int ans = 1e9; for (int i = 0; i <= 20000; i++) if (f[n][m].test(i)) { int v1 = i + a[n][m] - b[n][m]; int v2 = i + b[n][m] - a[n][m]; ans = min(ans, min(abs(v1 - 10000), abs(v2 - 10000))); } cout << ans; }
#include <bits/stdc++.h> using namespace std; int n, m; int a[101][101], b[101][101]; bitset<20001> f[101][101]; int main(int argc, char **argv) { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> b[i][j]; f[1][1].set(10000); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int v1 = a[i][j] - b[i][j]; int v2 = b[i][j] - a[i][j]; if (v1 >= 0) { f[i + 1][j] |= (f[i][j] << v1); f[i][j + 1] |= (f[i][j] << v1); } else { v1 = -v1; f[i + 1][j] |= (f[i][j] >> v1); f[i][j + 1] |= (f[i][j] >> v1); } if (v2 >= 0) { f[i + 1][j] |= (f[i][j] << v2); f[i][j + 1] |= (f[i][j] << v2); } else { v2 = -v2; f[i + 1][j] |= (f[i][j] >> v2); f[i][j + 1] |= (f[i][j] >> v2); } } int ans = 1e9; for (int i = 0; i <= 20000; i++) if (f[n][m].test(i)) { int v1 = i + a[n][m] - b[n][m]; int v2 = i + b[n][m] - a[n][m]; ans = min(ans, min(abs(v1 - 10000), abs(v2 - 10000))); } cout << ans; }
replace
6
7
6
7
0
p02839
C++
Runtime Error
#pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; using ll = long long int; #define debugos cout #define debug(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ debugos << (v) << endl; \ } #define debugv(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ for (auto e : (v)) { \ debugos << e << " "; \ } \ debugos << endl; \ } #define debuga(m, w) \ { \ printf("L%d %s > ", __LINE__, #m); \ for (int x = 0; x < (w); x++) { \ debugos << (m)[x] << " "; \ } \ debugos << endl; \ } #define debugaa(m, h, w) \ { \ printf("L%d %s >\n", __LINE__, #m); \ for (int y = 0; y < (h); y++) { \ for (int x = 0; x < (w); x++) { \ debugos << (m)[y][x] << " "; \ } \ debugos << endl; \ } \ } #define all(v) (v).begin(), (v).end() #define repeat(cnt, l) \ for (auto cnt = remove_reference<decltype(l)>::type(); (cnt) < (l); ++(cnt)) #define rrepeat(cnt, l) for (auto cnt = (l)-1; 0 <= (cnt); --(cnt)) #define iterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); ++(cnt)) #define diterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); --(cnt)) const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L; inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } } template <typename T1, typename T2> inline ostream &operator<<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; } template <typename Vec> inline ostream &_ostream_vecprint(ostream &os, const Vec &a) { os << '['; for (const auto &e : a) os << ' ' << e << ' '; os << ']'; return os; } template <typename T> inline ostream &operator<<(ostream &o, const vector<T> &v) { return _ostream_vecprint(o, v); } template <typename T, size_t S> inline ostream &operator<<(ostream &o, const array<T, S> &v) { return _ostream_vecprint(o, v); } template <typename T> inline T &chmax(T &to, const T &val) { return to = max(to, val); } template <typename T> inline T &chmin(T &to, const T &val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(code); } mt19937_64 randdev(8901016); template <typename T, typename Random = decltype(randdev), typename enable_if<is_integral<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_int_distribution<T>(l, h)(rand); } template <typename T, typename Random = decltype(randdev), typename enable_if<is_floating_point<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_real_distribution<T>(l, h)(rand); } #if defined(_WIN32) || defined(_WIN64) #define getc_x _getc_nolock #define putc_x _putc_nolock #elif defined(__GNUC__) #define getc_x getc_unlocked #define putc_x putc_unlocked #else #define getc_x getc #define putc_x putc #endif #define isvisiblechar(c) (0x21 <= (c) && (c) <= 0x7E) class MaiScanner { FILE *fp_; public: inline MaiScanner(FILE *fp) : fp_(fp) {} template <typename T> void input_integer(T &var) noexcept { var = 0; T sign = 1; int cc = getc_x(fp_); for (; cc < '0' || '9' < cc; cc = getc_x(fp_)) if (cc == '-') sign = -1; for (; '0' <= cc && cc <= '9'; cc = getc_x(fp_)) var = (var << 3) + (var << 1) + cc - '0'; var = var * sign; } inline int c() noexcept { return getc_x(fp_); } inline MaiScanner &operator>>(int &var) noexcept { input_integer<int>(var); return *this; } inline MaiScanner &operator>>(long long &var) noexcept { input_integer<long long>(var); return *this; } inline MaiScanner &operator>>(string &var) { int cc = getc_x(fp_); for (; !isvisiblechar(cc); cc = getc_x(fp_)) ; for (; isvisiblechar(cc); cc = getc_x(fp_)) var.push_back(cc); return *this; } template <typename IT> inline void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; class MaiPrinter { FILE *fp_; public: inline MaiPrinter(FILE *fp) : fp_(fp) {} template <typename T> void output_integer(T var) noexcept { if (var == 0) { putc_x('0', fp_); return; } if (var < 0) putc_x('-', fp_), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putc_x(stack[--stack_p], fp_); } inline MaiPrinter &operator<<(char c) noexcept { putc_x(c, fp_); return *this; } inline MaiPrinter &operator<<(int var) noexcept { output_integer<int>(var); return *this; } inline MaiPrinter &operator<<(long long var) noexcept { output_integer<long long>(var); return *this; } inline MaiPrinter &operator<<(char *str_p) noexcept { while (*str_p) putc_x(*(str_p++), fp_); return *this; } inline MaiPrinter &operator<<(const string &str) { const char *p = str.c_str(); const char *l = p + str.size(); while (p < l) putc_x(*p++, fp_); return *this; } template <typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; } }; MaiScanner scanner(stdin); MaiPrinter printer(stdout); // int H, W; array<int, 2> AB[88][88]; // constexpr int inf = 1e9; int main() { scanner >> H >> W; repeat(i, H) { repeat(j, W) { int a; scanner >> a; AB[i][j][0] = a; } } repeat(i, H) { repeat(j, W) { int a; scanner >> a; AB[i][j][1] = a; } } constexpr int K = 5000; vector<bitset<K>> dp1(W); vector<bitset<K>> dp2(W); dp1[0][0] = 1; repeat(i, H) { for (auto &e : dp2) e.reset(); repeat(e, K) { if (!dp1[0][e]) continue; constexpr int j = 0; auto ab = abs(AB[i][j][0] - AB[i][j][1]); dp2[j][e + ab] = 1; dp2[j][abs(e - ab)] = 1; } iterate(j, 1, W) { repeat(e, K) { if (!dp2[j - 1][e]) continue; auto ab = abs(AB[i][j][0] - AB[i][j][1]); dp2[j][e + ab] = 1; dp2[j][abs(e - ab)] = 1; } repeat(e, K) { if (!dp1[j][e]) continue; auto ab = abs(AB[i][j][0] - AB[i][j][1]); dp2[j][e + ab] = 1; dp2[j][abs(e - ab)] = 1; } } // cout << dp2[W-1].size() << endl; dp1.swap(dp2); } int best = 1e9; repeat(i, K) { if (dp1[W - 1][i]) { best = i; break; } } cout << best << endl; return 0; }
#pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; using ll = long long int; #define debugos cout #define debug(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ debugos << (v) << endl; \ } #define debugv(v) \ { \ printf("L%d %s > ", __LINE__, #v); \ for (auto e : (v)) { \ debugos << e << " "; \ } \ debugos << endl; \ } #define debuga(m, w) \ { \ printf("L%d %s > ", __LINE__, #m); \ for (int x = 0; x < (w); x++) { \ debugos << (m)[x] << " "; \ } \ debugos << endl; \ } #define debugaa(m, h, w) \ { \ printf("L%d %s >\n", __LINE__, #m); \ for (int y = 0; y < (h); y++) { \ for (int x = 0; x < (w); x++) { \ debugos << (m)[y][x] << " "; \ } \ debugos << endl; \ } \ } #define all(v) (v).begin(), (v).end() #define repeat(cnt, l) \ for (auto cnt = remove_reference<decltype(l)>::type(); (cnt) < (l); ++(cnt)) #define rrepeat(cnt, l) for (auto cnt = (l)-1; 0 <= (cnt); --(cnt)) #define iterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); ++(cnt)) #define diterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); --(cnt)) const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L; inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } } template <typename T1, typename T2> inline ostream &operator<<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; } template <typename Vec> inline ostream &_ostream_vecprint(ostream &os, const Vec &a) { os << '['; for (const auto &e : a) os << ' ' << e << ' '; os << ']'; return os; } template <typename T> inline ostream &operator<<(ostream &o, const vector<T> &v) { return _ostream_vecprint(o, v); } template <typename T, size_t S> inline ostream &operator<<(ostream &o, const array<T, S> &v) { return _ostream_vecprint(o, v); } template <typename T> inline T &chmax(T &to, const T &val) { return to = max(to, val); } template <typename T> inline T &chmin(T &to, const T &val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(code); } mt19937_64 randdev(8901016); template <typename T, typename Random = decltype(randdev), typename enable_if<is_integral<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_int_distribution<T>(l, h)(rand); } template <typename T, typename Random = decltype(randdev), typename enable_if<is_floating_point<T>::value>::type * = nullptr> inline T rand(T l, T h, Random &rand = randdev) { return uniform_real_distribution<T>(l, h)(rand); } #if defined(_WIN32) || defined(_WIN64) #define getc_x _getc_nolock #define putc_x _putc_nolock #elif defined(__GNUC__) #define getc_x getc_unlocked #define putc_x putc_unlocked #else #define getc_x getc #define putc_x putc #endif #define isvisiblechar(c) (0x21 <= (c) && (c) <= 0x7E) class MaiScanner { FILE *fp_; public: inline MaiScanner(FILE *fp) : fp_(fp) {} template <typename T> void input_integer(T &var) noexcept { var = 0; T sign = 1; int cc = getc_x(fp_); for (; cc < '0' || '9' < cc; cc = getc_x(fp_)) if (cc == '-') sign = -1; for (; '0' <= cc && cc <= '9'; cc = getc_x(fp_)) var = (var << 3) + (var << 1) + cc - '0'; var = var * sign; } inline int c() noexcept { return getc_x(fp_); } inline MaiScanner &operator>>(int &var) noexcept { input_integer<int>(var); return *this; } inline MaiScanner &operator>>(long long &var) noexcept { input_integer<long long>(var); return *this; } inline MaiScanner &operator>>(string &var) { int cc = getc_x(fp_); for (; !isvisiblechar(cc); cc = getc_x(fp_)) ; for (; isvisiblechar(cc); cc = getc_x(fp_)) var.push_back(cc); return *this; } template <typename IT> inline void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; class MaiPrinter { FILE *fp_; public: inline MaiPrinter(FILE *fp) : fp_(fp) {} template <typename T> void output_integer(T var) noexcept { if (var == 0) { putc_x('0', fp_); return; } if (var < 0) putc_x('-', fp_), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putc_x(stack[--stack_p], fp_); } inline MaiPrinter &operator<<(char c) noexcept { putc_x(c, fp_); return *this; } inline MaiPrinter &operator<<(int var) noexcept { output_integer<int>(var); return *this; } inline MaiPrinter &operator<<(long long var) noexcept { output_integer<long long>(var); return *this; } inline MaiPrinter &operator<<(char *str_p) noexcept { while (*str_p) putc_x(*(str_p++), fp_); return *this; } inline MaiPrinter &operator<<(const string &str) { const char *p = str.c_str(); const char *l = p + str.size(); while (p < l) putc_x(*p++, fp_); return *this; } template <typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; } }; MaiScanner scanner(stdin); MaiPrinter printer(stdout); // int H, W; array<int, 2> AB[88][88]; // constexpr int inf = 1e9; int main() { scanner >> H >> W; repeat(i, H) { repeat(j, W) { int a; scanner >> a; AB[i][j][0] = a; } } repeat(i, H) { repeat(j, W) { int a; scanner >> a; AB[i][j][1] = a; } } constexpr int K = 7500; vector<bitset<K>> dp1(W); vector<bitset<K>> dp2(W); dp1[0][0] = 1; repeat(i, H) { for (auto &e : dp2) e.reset(); repeat(e, K) { if (!dp1[0][e]) continue; constexpr int j = 0; auto ab = abs(AB[i][j][0] - AB[i][j][1]); dp2[j][e + ab] = 1; dp2[j][abs(e - ab)] = 1; } iterate(j, 1, W) { repeat(e, K) { if (!dp2[j - 1][e]) continue; auto ab = abs(AB[i][j][0] - AB[i][j][1]); dp2[j][e + ab] = 1; dp2[j][abs(e - ab)] = 1; } repeat(e, K) { if (!dp1[j][e]) continue; auto ab = abs(AB[i][j][0] - AB[i][j][1]); dp2[j][e + ab] = 1; dp2[j][abs(e - ab)] = 1; } } // cout << dp2[W-1].size() << endl; dp1.swap(dp2); } int best = 1e9; repeat(i, K) { if (dp1[W - 1][i]) { best = i; break; } } cout << best << endl; return 0; }
replace
224
225
224
225
0
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) // [a, b) #define reprev(i, n) reprrev(i, 0, n) typedef long long ll; typedef unsigned long long ull; 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; } const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } ll power(ll x, ll n) { if (n == 0) return 1; ll res = power(x, n / 2); if (n % 2 == 0) return res * res; else return res * res * x; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; // 線分 ab の偏角 返り値は[-π, π] double argument(const pair<double, double> &a, const pair<double, double> &b) { double ax = a.first, ay = a.second, bx = b.first, by = b.second; return atan2(by - ay, bx - ax); } /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vector<int>> a(H + 5, vector<int>(W + 5, 0)), b(H + 5, vector<int>(W + 5, 0)); repr(i, 1, H + 1) repr(j, 1, W + 1) cin >> a[i][j]; repr(i, 1, H + 1) repr(j, 1, W + 1) cin >> b[i][j]; vector<vector<vector<bool>>> dp( H + 5, vector<vector<bool>>(W + 5, vector<bool>(H * W * 81, false))); dp[1][1][abs(a[1][1] - b[1][1])] = true; repr(i, 1, H + 1) repr(j, 1, W + 1) rep(k, H * W * 81) if (dp[i][j][k]) { if (i + 1 <= H) { dp[i + 1][j][abs(k + a[i + 1][j] - b[i + 1][j])] = true; dp[i + 1][j][abs(k - a[i + 1][j] + b[i + 1][j])] = true; } if (j + 1 <= W) { dp[i][j + 1][abs(k + a[i][j + 1] - b[i][j + 1])] = true; dp[i][j + 1][abs(k + b[i][j + 1] - a[i][j + 1])] = true; } } rep(k, H * W * 81) if (dp[H][W][k]) { cout << k << endl; return 0; } return 0; }
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) // [a, b) #define reprev(i, n) reprrev(i, 0, n) typedef long long ll; typedef unsigned long long ull; 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; } const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } ll power(ll x, ll n) { if (n == 0) return 1; ll res = power(x, n / 2); if (n % 2 == 0) return res * res; else return res * res * x; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; // 線分 ab の偏角 返り値は[-π, π] double argument(const pair<double, double> &a, const pair<double, double> &b) { double ax = a.first, ay = a.second, bx = b.first, by = b.second; return atan2(by - ay, bx - ax); } /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vector<int>> a(H + 5, vector<int>(W + 5, 0)), b(H + 5, vector<int>(W + 5, 0)); repr(i, 1, H + 1) repr(j, 1, W + 1) cin >> a[i][j]; repr(i, 1, H + 1) repr(j, 1, W + 1) cin >> b[i][j]; vector<vector<vector<bool>>> dp( H + 5, vector<vector<bool>>(W + 5, vector<bool>(H * W * 81, false))); dp[1][1][abs(a[1][1] - b[1][1])] = true; repr(i, 1, H + 1) repr(j, 1, W + 1) rep(k, (H + W) * 81) if (dp[i][j][k]) { if (i + 1 <= H) { dp[i + 1][j][abs(k + a[i + 1][j] - b[i + 1][j])] = true; dp[i + 1][j][abs(k - a[i + 1][j] + b[i + 1][j])] = true; } if (j + 1 <= W) { dp[i][j + 1][abs(k + a[i][j + 1] - b[i][j + 1])] = true; dp[i][j + 1][abs(k + b[i][j + 1] - a[i][j + 1])] = true; } } rep(k, H * W * 81) if (dp[H][W][k]) { cout << k << endl; return 0; } return 0; }
replace
88
89
88
89
TLE
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int z = 80 * 80 * 80 + 2; bool dp[81][81][z]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n, m; cin >> n >> m; vector<vector<int>> v(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> v[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int a; cin >> a; v[i][j] = abs(a - v[i][j]); } } dp[0][0][v[0][0]] = true; for (int i = 1; i < m; i++) { for (int j = 0; j < z; j++) { int a = v[0][i]; dp[0][i][j] = dp[0][i - 1][abs(j - a)] | dp[0][i - 1][j + a]; } } for (int i = 1; i < n; i++) { for (int j = 0; j < z; j++) { int a = v[i][0]; dp[i][0][j] = dp[i - 1][0][abs(j - a)] | dp[i - 1][0][j + a]; } } for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { int a = v[i][j]; for (int k = 0; k < z; k++) { dp[i][j][k] = dp[i - 1][j][abs(k - a)] | dp[i - 1][j][k + a] | dp[i][j - 1][abs(k - a)] | dp[i][j - 1][k + a]; } } } for (int i = 0; i < z; i++) { if (dp[n - 1][m - 1][i]) { cout << i << '\n'; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int z = 80 * 80 * 2; bool dp[81][81][z]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n, m; cin >> n >> m; vector<vector<int>> v(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> v[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int a; cin >> a; v[i][j] = abs(a - v[i][j]); } } dp[0][0][v[0][0]] = true; for (int i = 1; i < m; i++) { for (int j = 0; j < z; j++) { int a = v[0][i]; dp[0][i][j] = dp[0][i - 1][abs(j - a)] | dp[0][i - 1][j + a]; } } for (int i = 1; i < n; i++) { for (int j = 0; j < z; j++) { int a = v[i][0]; dp[i][0][j] = dp[i - 1][0][abs(j - a)] | dp[i - 1][0][j + a]; } } for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { int a = v[i][j]; for (int k = 0; k < z; k++) { dp[i][j][k] = dp[i - 1][j][abs(k - a)] | dp[i - 1][j][k + a] | dp[i][j - 1][abs(k - a)] | dp[i][j - 1][k + a]; } } } for (int i = 0; i < z; i++) { if (dp[n - 1][m - 1][i]) { cout << i << '\n'; break; } } return 0; }
replace
4
5
4
5
TLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD (long long int)(1e9 + 7) #define ll long long int #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define REP(i, n) for (int i = n - 1; i >= 0; i--) #define REPS(i, n) for (int i = n; i > 0; i--) #define FOR(i, a, b) for (int i = a; i < (int)(b); i++) #define ALL(x) (x).begin(), (x).end() #define RALL(a) (a).rbegin(), (a).rend() #define CLR(a) memset((a), 0, sizeof(a)) #define PB push_back #define MP make_pair #define SP << " " << const int INF = 1001001001; const ll LINF = 100100100100100100; const double EPS = 1e-10; const long double PI = acos(-1.0L); typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VL; #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) __attribute__((constructor)) void initial() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } signed main() { int h, w; cin >> h >> w; int a[h][w], b[h][w]; const int max_k = h * w * 80 + 5; rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) cin >> b[i][j]; bool dp[h][w][max_k]; memset(dp, false, sizeof(dp)); const int dh[2] = {1, 0}, dw[2] = {0, 1}; dp[0][0][abs(a[0][0] - b[0][0])] = true; rep(now_h, h) rep(now_w, w) { rep(i, 2) { int next_h = now_h + dh[i], next_w = now_w + dw[i]; if (next_h >= h || next_w >= w) continue; int d = abs(a[next_h][next_w] - b[next_h][next_w]); rep(k, max_k) { if (!dp[now_h][now_w][k]) continue; if (k + d < max_k) dp[next_h][next_w][k + d] = true; if (abs(k - d) < max_k) dp[next_h][next_w][abs(k - d)] = true; } } } int ans = max_k; rep(k, max_k) { if (dp[h - 1][w - 1][k]) return cout << k << endl, 0; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD (long long int)(1e9 + 7) #define ll long long int #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define REP(i, n) for (int i = n - 1; i >= 0; i--) #define REPS(i, n) for (int i = n; i > 0; i--) #define FOR(i, a, b) for (int i = a; i < (int)(b); i++) #define ALL(x) (x).begin(), (x).end() #define RALL(a) (a).rbegin(), (a).rend() #define CLR(a) memset((a), 0, sizeof(a)) #define PB push_back #define MP make_pair #define SP << " " << const int INF = 1001001001; const ll LINF = 100100100100100100; const double EPS = 1e-10; const long double PI = acos(-1.0L); typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VL; #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) __attribute__((constructor)) void initial() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } signed main() { int h, w; cin >> h >> w; int a[h][w], b[h][w]; const int max_k = (h + w) * 80 + 5; rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) cin >> b[i][j]; bool dp[h][w][max_k]; memset(dp, false, sizeof(dp)); const int dh[2] = {1, 0}, dw[2] = {0, 1}; dp[0][0][abs(a[0][0] - b[0][0])] = true; rep(now_h, h) rep(now_w, w) { rep(i, 2) { int next_h = now_h + dh[i], next_w = now_w + dw[i]; if (next_h >= h || next_w >= w) continue; int d = abs(a[next_h][next_w] - b[next_h][next_w]); rep(k, max_k) { if (!dp[now_h][now_w][k]) continue; if (k + d < max_k) dp[next_h][next_w][k + d] = true; if (abs(k - d) < max_k) dp[next_h][next_w][abs(k - d)] = true; } } } int ans = max_k; rep(k, max_k) { if (dp[h - 1][w - 1][k]) return cout << k << endl, 0; } cout << ans << endl; return 0; }
replace
36
37
36
37
0
p02839
C++
Runtime Error
/** * Created by hiramekun at 20:44 on 2019-12-08. */ #include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vb = vector<bool>; using P = pair<ll, ll>; template <typename T> using pq = priority_queue<T>; template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>; template <typename T, typename K> using ump = unordered_map<T, K>; const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ll mod = 1000000007; const ll inf = ll(1e9); const ll e5 = ll(1e5); const ll ll_inf = ll(1e9) * ll(1e9); #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--) #define each(i, mp) for (auto &i : mp) #define eb emplace_back #define F first #define S second #define all(obj) (obj).begin(), (obj).end() template <class T> ostream &operator<<(ostream &out, const vector<T> &list) { ll n = list.size(); rep(i, n) out << list[i] << ' '; return out; } template <class T> istream &operator>>(istream &in, vector<T> &list) { ll n = list.size(); rep(i, n) in >> list[i]; return in; } template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> &list) { ll n = list.size(); rep(i, n) out << list[i] << '\n'; return out; } /* ------------- ANSWER ------------- */ /* ---------------------------------- */ void solve() { ll h, w; cin >> h >> w; vvl a(h, vl(w)), b(h, vl(w)); rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) cin >> b[i][j]; // a < b にする rep(i, h) { rep(j, w) { P p = minmax(a[i][j], b[i][j]); a[i][j] = p.first, b[i][j] = p.second; } } ll max_diff = 85 * (h + w); bool dp[w][h][max_diff]; rep(i, w) rep(j, h) rep(l, max_diff) dp[i][j][l] = false; dp[0][0][b[0][0] - a[0][0]] = true; rep(i, h) { rep(j, w) { rep(l, max_diff) { if (dp[i][j][l]) { if (i + 1 <= h - 1) { vl candi{ l + b[i + 1][j] - a[i + 1][j], l - b[i + 1][j] + a[i + 1][j], -l + b[i + 1][j] - a[i + 1][j], -l - b[i + 1][j] + a[i + 1][j]}; each(e, candi) if (e >= 0) dp[i + 1][j][e] = true; } if (j + 1 <= w - 1) { vl candi{ l + b[i][j + 1] - a[i][j + 1], l - b[i][j + 1] + a[i][j + 1], -l + b[i][j + 1] - a[i][j + 1], -l - b[i][j + 1] + a[i][j + 1]}; each(e, candi) if (e >= 0) dp[i][j + 1][e] = true; } } } } } ll ans = ll_inf; rep(i, max_diff) { if (dp[h - 1][w - 1][i]) ans = min(ans, i); } cout << ans << '\n'; } int main() { #ifdef MY_DEBUG while (true) { #endif solve(); #ifdef MY_DEBUG } #endif return 0; }
/** * Created by hiramekun at 20:44 on 2019-12-08. */ #include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vb = vector<bool>; using P = pair<ll, ll>; template <typename T> using pq = priority_queue<T>; template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>; template <typename T, typename K> using ump = unordered_map<T, K>; const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ll mod = 1000000007; const ll inf = ll(1e9); const ll e5 = ll(1e5); const ll ll_inf = ll(1e9) * ll(1e9); #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--) #define each(i, mp) for (auto &i : mp) #define eb emplace_back #define F first #define S second #define all(obj) (obj).begin(), (obj).end() template <class T> ostream &operator<<(ostream &out, const vector<T> &list) { ll n = list.size(); rep(i, n) out << list[i] << ' '; return out; } template <class T> istream &operator>>(istream &in, vector<T> &list) { ll n = list.size(); rep(i, n) in >> list[i]; return in; } template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> &list) { ll n = list.size(); rep(i, n) out << list[i] << '\n'; return out; } /* ------------- ANSWER ------------- */ /* ---------------------------------- */ void solve() { ll h, w; cin >> h >> w; vvl a(h, vl(w)), b(h, vl(w)); rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) cin >> b[i][j]; // a < b にする rep(i, h) { rep(j, w) { P p = minmax(a[i][j], b[i][j]); a[i][j] = p.first, b[i][j] = p.second; } } ll max_diff = 85 * (h + w); bool dp[h][w][max_diff]; rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false; dp[0][0][b[0][0] - a[0][0]] = true; rep(i, h) { rep(j, w) { rep(l, max_diff) { if (dp[i][j][l]) { if (i + 1 <= h - 1) { vl candi{ l + b[i + 1][j] - a[i + 1][j], l - b[i + 1][j] + a[i + 1][j], -l + b[i + 1][j] - a[i + 1][j], -l - b[i + 1][j] + a[i + 1][j]}; each(e, candi) if (e >= 0) dp[i + 1][j][e] = true; } if (j + 1 <= w - 1) { vl candi{ l + b[i][j + 1] - a[i][j + 1], l - b[i][j + 1] + a[i][j + 1], -l + b[i][j + 1] - a[i][j + 1], -l - b[i][j + 1] + a[i][j + 1]}; each(e, candi) if (e >= 0) dp[i][j + 1][e] = true; } } } } } ll ans = ll_inf; rep(i, max_diff) { if (dp[h - 1][w - 1][i]) ans = min(ans, i); } cout << ans << '\n'; } int main() { #ifdef MY_DEBUG while (true) { #endif solve(); #ifdef MY_DEBUG } #endif return 0; }
replace
67
69
67
69
0
p02839
C++
Runtime Error
// problem: "https://atcoder.jp/contests/abc147/tasks/abc147_e" #include <bits/stdc++.h> #define p 6401 using namespace std; int minD(vector<vector<int>> &A, vector<vector<int>> &B, int h, int w) { vector<vector<int>> D(h, vector<int>(w, 0)); int i, j; for (i = 0; i != h; i++) for (j = 0; j != w; j++) D[i][j] = abs(A[i][j] - B[i][j]); vector<vector<vector<bool>>> V( h, vector<vector<bool>>(w, vector<bool>(p, false))); int k; V[0][0][D[0][0]] = true; for (i = 0; i != h; i++) { for (j = 0; j != w; j++) { if (i - 1 >= 0) { for (k = 0; k != p; k++) { if (V[i - 1][j][k]) { V[i][j][abs(D[i][j] - k)] = true; V[i][j][abs(D[i][j] + k)] = true; } } } if (j - 1 >= 0) { for (k = 0; k != p; k++) { if (V[i][j - 1][k]) { V[i][j][abs(D[i][j] - k)] = true; V[i][j][abs(D[i][j] + k)] = true; } } } } } for (k = 0; k != p; k++) if (V[h - 1][w - 1][k]) break; return k; } int main() { int h, w; cin >> h >> w; int i, j; vector<vector<int>> A(h, vector<int>(w, 0)); for (i = 0; i != h; i++) { for (j = 0; j != w; j++) { int x; cin >> x; A[i][j] = x; } } vector<vector<int>> B(h, vector<int>(w, 0)); for (i = 0; i != h; i++) { for (j = 0; j != w; j++) { int x; cin >> x; B[i][j] = x; } } cout << minD(A, B, h, w); // define return 0; }
// problem: "https://atcoder.jp/contests/abc147/tasks/abc147_e" #include <bits/stdc++.h> #define p 6401 * 2 using namespace std; int minD(vector<vector<int>> &A, vector<vector<int>> &B, int h, int w) { vector<vector<int>> D(h, vector<int>(w, 0)); int i, j; for (i = 0; i != h; i++) for (j = 0; j != w; j++) D[i][j] = abs(A[i][j] - B[i][j]); vector<vector<vector<bool>>> V( h, vector<vector<bool>>(w, vector<bool>(p, false))); int k; V[0][0][D[0][0]] = true; for (i = 0; i != h; i++) { for (j = 0; j != w; j++) { if (i - 1 >= 0) { for (k = 0; k != p; k++) { if (V[i - 1][j][k]) { V[i][j][abs(D[i][j] - k)] = true; V[i][j][abs(D[i][j] + k)] = true; } } } if (j - 1 >= 0) { for (k = 0; k != p; k++) { if (V[i][j - 1][k]) { V[i][j][abs(D[i][j] - k)] = true; V[i][j][abs(D[i][j] + k)] = true; } } } } } for (k = 0; k != p; k++) if (V[h - 1][w - 1][k]) break; return k; } int main() { int h, w; cin >> h >> w; int i, j; vector<vector<int>> A(h, vector<int>(w, 0)); for (i = 0; i != h; i++) { for (j = 0; j != w; j++) { int x; cin >> x; A[i][j] = x; } } vector<vector<int>> B(h, vector<int>(w, 0)); for (i = 0; i != h; i++) { for (j = 0; j != w; j++) { int x; cin >> x; B[i][j] = x; } } cout << minD(A, B, h, w); // define return 0; }
replace
4
5
4
5
0
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // Define using ll = long long; using ull = unsigned long long; using ld = long double; template <class T> using pvector = vector<pair<T, T>>; template <class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>; constexpr const ll dx[4] = {1, 0, -1, 0}; constexpr const ll dy[4] = {0, 1, 0, -1}; constexpr const ll MOD = 1e9 + 7; constexpr const ll mod = 998244353; constexpr const ll INF = 1LL << 60; constexpr const ll inf = 1 << 30; constexpr const char rt = '\n'; constexpr const char sp = ' '; #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back #define elif else if #define all(a, v, ...) \ ([&](decltype((v)) w) { return (a)(begin(w), end(w), ##__VA_ARGS__); })(v) #define fi first #define se second 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; } // Debug #define debug(...) \ { \ cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \ for (auto &&X : {__VA_ARGS__}) \ cerr << "[" << X << "] "; \ cerr << rt; \ } #define dump(a, h, w) \ { \ cerr << __LINE__ << ": " << #a << " = [" << rt; \ rep(i, h) { \ rep(j, w) cerr << a[i][j] << sp; \ cerr << rt; \ } \ cerr << "]" << rt; \ } #define vdump(a, n) \ { \ cerr << __LINE__ << ": " << #a << " = ["; \ rep(i, n) if (i) cerr << sp << a[i]; \ else cerr << a[i]; \ cerr << "]" << rt; \ } // Loop #define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i) #define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i) #define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i) #define each(i, a) for (auto &&i : a) // Stream #define fout(n) cout << fixed << setprecision(n) struct io { io() { cin.tie(nullptr), ios::sync_with_stdio(false); } } io; // Speed #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // Math inline constexpr ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; } inline constexpr ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; } inline constexpr ll modulo(const ll n, const ll m = MOD) { ll k = n % m; return k + m * (k < 0); } inline constexpr ll chmod(ll &n, const ll m = MOD) { n %= m; return n += m * (n < 0); } inline constexpr ll mpow(ll a, ll n, const ll m = MOD) { ll r = 1; rep(i, 64) { if (n & (1LL << i)) r *= a; chmod(r, m); a *= a; chmod(a, m); } return r; } inline ll inv(const ll n, const ll m = MOD) { ll a = n, b = m, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return modulo(x, m); } bool DP[80][80][20000 + 1]; signed main() { ll H, W; cin >> H >> W; ll A[H][W], B[H][W]; rep(i, H) rep(j, W) cin >> A[i][j]; rep(i, H) rep(j, W) cin >> B[i][j]; rep(i, H + 1) rep(j, W + 1) rep(k, 20000 + 1) DP[i][j][k] = 0; DP[0][0][10000] = 1; rep(i, H) rep(j, W) rep(k, 20000 + 1) { if (i + 1 <= H - 1) { if (0 <= k + A[i][j] - B[i][j] && k + A[i][j] - B[i][j] <= 20000) { DP[i + 1][j][k + A[i][j] - B[i][j]] |= DP[i][j][k]; } if (0 <= k + B[i][j] - A[i][j] && k + B[i][j] - A[i][j] <= 20000) { DP[i + 1][j][k + B[i][j] - A[i][j]] |= DP[i][j][k]; } } if (j + 1 <= W - 1) { if (0 <= k + A[i][j] - B[i][j] && k + A[i][j] - B[i][j] <= 20000) { DP[i][j + 1][k + A[i][j] - B[i][j]] |= DP[i][j][k]; } if (0 <= k + B[i][j] - A[i][j] && k + B[i][j] - A[i][j] <= 20000) { DP[i][j + 1][k + B[i][j] - A[i][j]] |= DP[i][j][k]; } } } bool get[20001] = {}; rep(i, 20000 + 1) { if (abs(i + A[H - 1][W - 1] - B[H - 1][W - 1] - 10000) <= 20000) { get[abs(i + A[H - 1][W - 1] - B[H - 1][W - 1] - 10000)] |= DP[H - 1][W - 1][i]; } if (abs(i + B[H - 1][W - 1] - A[H - 1][W - 1] - 10000) <= 20000) { get[abs(i + B[H - 1][W - 1] - A[H - 1][W - 1] - 10000)] |= DP[H - 1][W - 1][i]; } } ll res = INF; rep(i, 20000) if (get[i]) chmin(res, i); cout << res << rt; } // -g -D_GLIBCXX_DEBUG -fsanitize=undefined
#include <bits/stdc++.h> using namespace std; // Define using ll = long long; using ull = unsigned long long; using ld = long double; template <class T> using pvector = vector<pair<T, T>>; template <class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>; constexpr const ll dx[4] = {1, 0, -1, 0}; constexpr const ll dy[4] = {0, 1, 0, -1}; constexpr const ll MOD = 1e9 + 7; constexpr const ll mod = 998244353; constexpr const ll INF = 1LL << 60; constexpr const ll inf = 1 << 30; constexpr const char rt = '\n'; constexpr const char sp = ' '; #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back #define elif else if #define all(a, v, ...) \ ([&](decltype((v)) w) { return (a)(begin(w), end(w), ##__VA_ARGS__); })(v) #define fi first #define se second 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; } // Debug #define debug(...) \ { \ cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \ for (auto &&X : {__VA_ARGS__}) \ cerr << "[" << X << "] "; \ cerr << rt; \ } #define dump(a, h, w) \ { \ cerr << __LINE__ << ": " << #a << " = [" << rt; \ rep(i, h) { \ rep(j, w) cerr << a[i][j] << sp; \ cerr << rt; \ } \ cerr << "]" << rt; \ } #define vdump(a, n) \ { \ cerr << __LINE__ << ": " << #a << " = ["; \ rep(i, n) if (i) cerr << sp << a[i]; \ else cerr << a[i]; \ cerr << "]" << rt; \ } // Loop #define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i) #define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i) #define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i) #define each(i, a) for (auto &&i : a) // Stream #define fout(n) cout << fixed << setprecision(n) struct io { io() { cin.tie(nullptr), ios::sync_with_stdio(false); } } io; // Speed #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // Math inline constexpr ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; } inline constexpr ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; } inline constexpr ll modulo(const ll n, const ll m = MOD) { ll k = n % m; return k + m * (k < 0); } inline constexpr ll chmod(ll &n, const ll m = MOD) { n %= m; return n += m * (n < 0); } inline constexpr ll mpow(ll a, ll n, const ll m = MOD) { ll r = 1; rep(i, 64) { if (n & (1LL << i)) r *= a; chmod(r, m); a *= a; chmod(a, m); } return r; } inline ll inv(const ll n, const ll m = MOD) { ll a = n, b = m, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return modulo(x, m); } bool DP[80][80][20000 + 1]; signed main() { ll H, W; cin >> H >> W; ll A[H][W], B[H][W]; rep(i, H) rep(j, W) cin >> A[i][j]; rep(i, H) rep(j, W) cin >> B[i][j]; rep(i, H) rep(j, W) rep(k, 20000 + 1) DP[i][j][k] = 0; DP[0][0][10000] = 1; rep(i, H) rep(j, W) rep(k, 20000 + 1) { if (i + 1 <= H - 1) { if (0 <= k + A[i][j] - B[i][j] && k + A[i][j] - B[i][j] <= 20000) { DP[i + 1][j][k + A[i][j] - B[i][j]] |= DP[i][j][k]; } if (0 <= k + B[i][j] - A[i][j] && k + B[i][j] - A[i][j] <= 20000) { DP[i + 1][j][k + B[i][j] - A[i][j]] |= DP[i][j][k]; } } if (j + 1 <= W - 1) { if (0 <= k + A[i][j] - B[i][j] && k + A[i][j] - B[i][j] <= 20000) { DP[i][j + 1][k + A[i][j] - B[i][j]] |= DP[i][j][k]; } if (0 <= k + B[i][j] - A[i][j] && k + B[i][j] - A[i][j] <= 20000) { DP[i][j + 1][k + B[i][j] - A[i][j]] |= DP[i][j][k]; } } } bool get[20001] = {}; rep(i, 20000 + 1) { if (abs(i + A[H - 1][W - 1] - B[H - 1][W - 1] - 10000) <= 20000) { get[abs(i + A[H - 1][W - 1] - B[H - 1][W - 1] - 10000)] |= DP[H - 1][W - 1][i]; } if (abs(i + B[H - 1][W - 1] - A[H - 1][W - 1] - 10000) <= 20000) { get[abs(i + B[H - 1][W - 1] - A[H - 1][W - 1] - 10000)] |= DP[H - 1][W - 1][i]; } } ll res = INF; rep(i, 20000) if (get[i]) chmin(res, i); cout << res << rt; } // -g -D_GLIBCXX_DEBUG -fsanitize=undefined
replace
130
131
130
131
-11
p02839
C++
Runtime Error
#include <bits/stdc++.h> #define N 85 #define maxSum (2 * N * N) using namespace std; const int del = maxSum + 105; int n, m; int a[N][N]; bool f[2][N][maxSum * 2 + 500]; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) cin >> a[i][j]; for (int i = 1; i <= n; ++i) for (int j = 1, d; j <= m; ++j) cin >> d, a[i][j] -= d; int u = 0, v = 1; f[0][1][a[1][1] + del] = true; for (int i = 2; i < n + m; ++i) { memset(f[v], 0, sizeof f[v]); for (int x = 1, y; x <= n; ++x) { y = i - x; if (y < 1 || y > m) continue; if (x + 1 <= n) { for (int o = -maxSum; o <= maxSum; ++o) if (f[u][x][o + del]) { f[v][x + 1][o + a[x + 1][y] + del] = true; f[v][x + 1][o - a[x + y][y] + del] = true; } } if (y + 1 <= m) { for (int o = -maxSum; o <= maxSum; ++o) if (f[u][x][o + del]) { f[v][x][o + a[x][y + 1] + del] = true; f[v][x][o - a[x][y + 1] + del] = true; } } } swap(u, v); } int ans = INT_MAX; for (int i = -maxSum; i <= maxSum; ++i) if (f[u][n][i + del] && abs(i) < ans) ans = abs(i); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define N 85 #define maxSum (2 * N * N) using namespace std; const int del = maxSum + 105; int n, m; int a[N][N]; bool f[2][N][maxSum * 2 + 500]; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) cin >> a[i][j]; for (int i = 1; i <= n; ++i) for (int j = 1, d; j <= m; ++j) cin >> d, a[i][j] -= d; int u = 0, v = 1; f[0][1][a[1][1] + del] = true; for (int i = 2; i < n + m; ++i) { memset(f[v], 0, sizeof f[v]); for (int x = 1, y; x <= n; ++x) { y = i - x; if (y < 1 || y > m) continue; if (x + 1 <= n) { for (int o = -maxSum; o <= maxSum; ++o) if (f[u][x][o + del]) { f[v][x + 1][o + a[x + 1][y] + del] = true; f[v][x + 1][o - a[x + 1][y] + del] = true; } } if (y + 1 <= m) { for (int o = -maxSum; o <= maxSum; ++o) if (f[u][x][o + del]) { f[v][x][o + a[x][y + 1] + del] = true; f[v][x][o - a[x][y + 1] + del] = true; } } } swap(u, v); } int ans = INT_MAX; for (int i = -maxSum; i <= maxSum; ++i) if (f[u][n][i + del] && abs(i) < ans) ans = abs(i); cout << ans << endl; return 0; }
replace
34
35
34
35
0
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vector<int>> aij(H, vector<int>(W)); vector<vector<int>> bij(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int a; cin >> a; aij.at(i).at(j) = a; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int b; cin >> b; bij.at(i).at(j) = b; } } vector<vector<vector<bool>>> dp( H, vector<vector<bool>>(W, vector<bool>(6401, false))); dp.at(0).at(0).at(abs(aij.at(0).at(0) - bij.at(0).at(0))) = true; for (int i = 1; i < H + W - 1; i++) { int currentH = i + 1; int currentW = -1; while (currentH >= 0) { currentH--; currentW++; if (currentH >= H || currentH < 0 || currentW >= W || currentW < 0) { continue; } int ababs = abs(aij.at(currentH).at(currentW) - bij.at(currentH).at(currentW)); for (int j = 0; j < max(i * 80 + 1, 6321); j++) { if (currentH > 0) { dp.at(currentH).at(currentW).at(j) = dp.at(currentH).at(currentW).at(j) | dp.at(currentH - 1).at(currentW).at(abs(j - ababs)); dp.at(currentH).at(currentW).at(j) = dp.at(currentH).at(currentW).at(j) | dp.at(currentH - 1).at(currentW).at(j + ababs); } if (currentW > 0) { dp.at(currentH).at(currentW).at(j) = dp.at(currentH).at(currentW).at(j) | dp.at(currentH).at(currentW - 1).at(abs(j - ababs)); dp.at(currentH).at(currentW).at(j) = dp.at(currentH).at(currentW).at(j) | dp.at(currentH).at(currentW - 1).at(j + ababs); } } } } for (int i = 0; i < 81; i++) { if (dp.at(H - 1).at(W - 1).at(i)) { cout << i << endl; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vector<int>> aij(H, vector<int>(W)); vector<vector<int>> bij(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int a; cin >> a; aij.at(i).at(j) = a; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int b; cin >> b; bij.at(i).at(j) = b; } } vector<vector<vector<bool>>> dp( H, vector<vector<bool>>(W, vector<bool>(6401, false))); dp.at(0).at(0).at(abs(aij.at(0).at(0) - bij.at(0).at(0))) = true; for (int i = 1; i < H + W - 1; i++) { int currentH = i + 1; int currentW = -1; while (currentH >= 0) { currentH--; currentW++; if (currentH >= H || currentH < 0 || currentW >= W || currentW < 0) { continue; } int ababs = abs(aij.at(currentH).at(currentW) - bij.at(currentH).at(currentW)); for (int j = 0; j < min((i + 1) * 80 + 1, 6321); j++) { if (currentH > 0) { dp.at(currentH).at(currentW).at(j) = dp.at(currentH).at(currentW).at(j) | dp.at(currentH - 1).at(currentW).at(abs(j - ababs)); dp.at(currentH).at(currentW).at(j) = dp.at(currentH).at(currentW).at(j) | dp.at(currentH - 1).at(currentW).at(j + ababs); } if (currentW > 0) { dp.at(currentH).at(currentW).at(j) = dp.at(currentH).at(currentW).at(j) | dp.at(currentH).at(currentW - 1).at(abs(j - ababs)); dp.at(currentH).at(currentW).at(j) = dp.at(currentH).at(currentW).at(j) | dp.at(currentH).at(currentW - 1).at(j + ababs); } } } } for (int i = 0; i < 81; i++) { if (dp.at(H - 1).at(W - 1).at(i)) { cout << i << endl; break; } } return 0; }
replace
40
41
40
41
0
p02839
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const int INF = 1e9; const int MAXC = 80; int main() { int h, w; cin >> h >> w; vector<vector<int>> a(h, vector<int>(w)); vector<vector<int>> b(h, vector<int>(w)); rep(i, h) { rep(j, w) cin >> a[i][j]; } rep(i, h) { rep(j, w) cin >> b[i][j]; } vector<vector<int>> c(h, vector<int>(w)); rep(i, h) rep(j, w) c[i][j] = abs(a[i][j] - b[i][j]); int MAX = MAXC * h * w; bool dp[h][w][MAX]; rep(i, h) rep(j, w) rep(k, MAX) dp[i][j][k] = false; dp[0][0][c[0][0]] = true; rep(i, h) { rep(j, w) { rep(k, MAX) { int n = c[i][j]; if (i - 1 >= 0 && abs(k - n) < MAX) dp[i][j][abs(k - n)] |= dp[i - 1][j][k]; if (j - 1 >= 0 && abs(k - n) < MAX) dp[i][j][abs(k - n)] |= dp[i][j - 1][k]; if (i - 1 >= 0 && k + n < MAX) dp[i][j][k + n] |= dp[i - 1][j][k]; if (j - 1 >= 0 && k + n < MAX) dp[i][j][k + n] |= dp[i][j - 1][k]; } } } rep(k, MAX) { if (dp[h - 1][w - 1][k]) { cout << k << endl; return 0; } } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const int INF = 1e9; const int MAXC = 80; int main() { int h, w; cin >> h >> w; vector<vector<int>> a(h, vector<int>(w)); vector<vector<int>> b(h, vector<int>(w)); rep(i, h) { rep(j, w) cin >> a[i][j]; } rep(i, h) { rep(j, w) cin >> b[i][j]; } vector<vector<int>> c(h, vector<int>(w)); rep(i, h) rep(j, w) c[i][j] = abs(a[i][j] - b[i][j]); int MAX = MAXC * (h + w); bool dp[h][w][MAX]; rep(i, h) rep(j, w) rep(k, MAX) dp[i][j][k] = false; dp[0][0][c[0][0]] = true; rep(i, h) { rep(j, w) { rep(k, MAX) { int n = c[i][j]; if (i - 1 >= 0 && abs(k - n) < MAX) dp[i][j][abs(k - n)] |= dp[i - 1][j][k]; if (j - 1 >= 0 && abs(k - n) < MAX) dp[i][j][abs(k - n)] |= dp[i][j - 1][k]; if (i - 1 >= 0 && k + n < MAX) dp[i][j][k + n] |= dp[i - 1][j][k]; if (j - 1 >= 0 && k + n < MAX) dp[i][j][k + n] |= dp[i][j - 1][k]; } } } rep(k, MAX) { if (dp[h - 1][w - 1][k]) { cout << k << endl; return 0; } } }
replace
20
21
20
21
0
p02839
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 83; const int M = 6405; #define ll long long int n, m, DP[M][N][N], b[2 * N][N], a[N][N]; void in() { scanf("%d%d", &n, &m); for (int i = 1; i <= n * 2; i++) for (int j = 1; j <= m; j++) scanf("%d", &b[i][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) a[i][j] = abs(b[i][j] - b[i + n][j]); } void _DP() { DP[0][1][1] = 1; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) for (int l = 0; l <= 6400; l++) { if (!DP[l][i][j]) continue; DP[l + a[i][j]][i + 1][j] = DP[abs(l - a[i][j])][i + 1][j] = DP[l + a[i][j]][i][j + 1] = DP[abs(l - a[i][j])][i][j + 1] = 1; } } void print() { for (int i = 0; i <= 6400; i++) if (DP[i][n + 1][m] || DP[i][n][m + 1]) { cout << i; break; } } int main() { in(); _DP(); print(); }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 100; const int M = 8000; #define ll long long int n, m, DP[M][N][N], b[2 * N][N], a[N][N]; void in() { scanf("%d%d", &n, &m); for (int i = 1; i <= n * 2; i++) for (int j = 1; j <= m; j++) scanf("%d", &b[i][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) a[i][j] = abs(b[i][j] - b[i + n][j]); } void _DP() { DP[0][1][1] = 1; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) for (int l = 0; l <= 6400; l++) { if (!DP[l][i][j]) continue; DP[l + a[i][j]][i + 1][j] = DP[abs(l - a[i][j])][i + 1][j] = DP[l + a[i][j]][i][j + 1] = DP[abs(l - a[i][j])][i][j + 1] = 1; } } void print() { for (int i = 0; i <= 6400; i++) if (DP[i][n + 1][m] || DP[i][n][m + 1]) { cout << i; break; } } int main() { in(); _DP(); print(); }
replace
8
10
8
10
-11
p02839
C++
Memory Limit Exceeded
#include <bits/stdc++.h> #define pt(sth) cout << sth << "\n" #define chmax(a, b) \ { \ if (a < b) \ a = b; \ } #define chmin(a, b) \ { \ if (a > b) \ a = b; \ } #define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD using namespace std; typedef long long ll; typedef pair<ll, ll> P; static const ll INF = 1e18; static const ll MOD = 1e9 + 7; static const ll MAX = 101010; /* for(i=0; i<N; i++) cin >> a[i]; */ ll dp[88][88][88 * (88 + 88) * 2]; int main(void) { ll i, j, k, l; ll H, W; cin >> H >> W; ll a[88][88], b[88][88], c[88][88]; ll BD = 82 * (82 + 82); for (i = 0; i < H; i++) for (j = 0; j < W; j++) cin >> a[i][j]; for (i = 0; i < H; i++) for (j = 0; j < W; j++) cin >> b[i][j]; for (i = 0; i < H; i++) for (j = 0; j < W; j++) c[i][j] = abs(a[i][j] - b[i][j]); memset(dp, 0, sizeof(dp)); dp[0][0][BD + c[0][0]] = 1; dp[0][0][BD - c[0][0]] = 1; for (i = 0; i < H; i++) { for (j = 0; j < W; j++) { for (k = 0; k < 85 * (85 + 85) * 2; k++) { if (dp[i][j][k]) { chmax(dp[i + 1][j][k + c[i + 1][j]], dp[i][j][k]); chmax(dp[i + 1][j][k - c[i + 1][j]], dp[i][j][k]); chmax(dp[i][j + 1][k + c[i][j + 1]], dp[i][j][k]); chmax(dp[i][j + 1][k - c[i][j + 1]], dp[i][j][k]); } } } } ll ans = INF; for (k = 0; k < 85 * (85 + 85) * 2; k++) { if (dp[H - 1][W - 1][k]) chmin(ans, abs(k - BD)); } pt(ans); }
#include <bits/stdc++.h> #define pt(sth) cout << sth << "\n" #define chmax(a, b) \ { \ if (a < b) \ a = b; \ } #define chmin(a, b) \ { \ if (a > b) \ a = b; \ } #define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD using namespace std; typedef long long ll; typedef pair<ll, ll> P; static const ll INF = 1e18; static const ll MOD = 1e9 + 7; static const ll MAX = 101010; /* for(i=0; i<N; i++) cin >> a[i]; */ int dp[88][88][88 * (88 + 88) * 2]; int main(void) { ll i, j, k, l; ll H, W; cin >> H >> W; ll a[88][88], b[88][88], c[88][88]; ll BD = 82 * (82 + 82); for (i = 0; i < H; i++) for (j = 0; j < W; j++) cin >> a[i][j]; for (i = 0; i < H; i++) for (j = 0; j < W; j++) cin >> b[i][j]; for (i = 0; i < H; i++) for (j = 0; j < W; j++) c[i][j] = abs(a[i][j] - b[i][j]); memset(dp, 0, sizeof(dp)); dp[0][0][BD + c[0][0]] = 1; dp[0][0][BD - c[0][0]] = 1; for (i = 0; i < H; i++) { for (j = 0; j < W; j++) { for (k = 0; k < 85 * (85 + 85) * 2; k++) { if (dp[i][j][k]) { chmax(dp[i + 1][j][k + c[i + 1][j]], dp[i][j][k]); chmax(dp[i + 1][j][k - c[i + 1][j]], dp[i][j][k]); chmax(dp[i][j + 1][k + c[i][j + 1]], dp[i][j][k]); chmax(dp[i][j + 1][k - c[i][j + 1]], dp[i][j][k]); } } } } ll ans = INF; for (k = 0; k < 85 * (85 + 85) * 2; k++) { if (dp[H - 1][W - 1][k]) chmin(ans, abs(k - BD)); } pt(ans); }
replace
25
26
25
26
MLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MX = 13000, N = 80; bitset<2 * MX> pos[N][N]; int a[N][N], b[N][N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> b[i][j]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int d = abs(a[i][j] - b[i][j]); if (i == 1 && j == 1) pos[i][j][MX + d] = 1; if (i > 1) pos[i][j] |= (pos[i - 1][j] << d) | (pos[i - 1][j] >> d); if (j > 1) pos[i][j] |= (pos[i][j - 1] << d) | (pos[i][j - 1] >> d); // cout<<i<<" "<<j<<" "<<d<<" ---> "; // for (int x=0; x<2*MX; x++) // if (pos[i][j][x]) cout<<x-MX<<" "; cout<<endl; } } for (int i = 0; i < MX; i++) { if (pos[n][m][MX + i] || pos[n][m][MX - i]) { cout << i << endl; break; } } }
#include <bits/stdc++.h> using namespace std; const int MX = 13000, N = 85; bitset<2 * MX> pos[N][N]; int a[N][N], b[N][N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> b[i][j]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int d = abs(a[i][j] - b[i][j]); if (i == 1 && j == 1) pos[i][j][MX + d] = 1; if (i > 1) pos[i][j] |= (pos[i - 1][j] << d) | (pos[i - 1][j] >> d); if (j > 1) pos[i][j] |= (pos[i][j - 1] << d) | (pos[i][j - 1] >> d); // cout<<i<<" "<<j<<" "<<d<<" ---> "; // for (int x=0; x<2*MX; x++) // if (pos[i][j][x]) cout<<x-MX<<" "; cout<<endl; } } for (int i = 0; i < MX; i++) { if (pos[n][m][MX + i] || pos[n][m][MX - i]) { cout << i << endl; break; } } }
replace
3
4
3
4
0
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; int a[h][w], b[h][w]; int diff[h][w]; for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { cin >> a[x][y]; } } for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { cin >> b[x][y]; } } for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { diff[x][y] = abs(a[x][y] - b[x][y]); } } int x = 0, y = 0; int base = 80 * 80; int lim = base * 2; int dp[h][w][lim + 1]; for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { for (int d = 0; d <= lim; d++) { dp[x][y][d] = 0; } } } dp[0][0][diff[0][0]] = 1; for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { for (int d = 0; d <= lim; d++) { if (x != 0) { dp[x][y][d] = max(dp[x][y][d], dp[x - 1][y][abs(d - diff[x][y])]); if (d + diff[x][y] <= lim) { dp[x][y][d] = max(dp[x][y][d], dp[x - 1][y][d + diff[x][y]]); } } if (y != 0) { dp[x][y][d] = max(dp[x][y][d], dp[x][y - 1][abs(d - diff[x][y])]); if (d + diff[x][y] <= lim) { dp[x][y][d] = max(dp[x][y][d], dp[x][y - 1][d + diff[x][y]]); } } } } } for (int d = 0; d <= base; d++) { if (dp[h - 1][w - 1][d] == 1) { cout << d << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; int a[h][w], b[h][w]; int diff[h][w]; for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { cin >> a[x][y]; } } for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { cin >> b[x][y]; } } for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { diff[x][y] = abs(a[x][y] - b[x][y]); } } int x = 0, y = 0; int base = 80 * 80; int lim = base * 2; vector<vector<vector<int>>> dp( h, vector<vector<int>>(w, vector<int>(lim + 1, 0))); dp[0][0][diff[0][0]] = 1; for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { for (int d = 0; d <= lim; d++) { if (x != 0) { dp[x][y][d] = max(dp[x][y][d], dp[x - 1][y][abs(d - diff[x][y])]); if (d + diff[x][y] <= lim) { dp[x][y][d] = max(dp[x][y][d], dp[x - 1][y][d + diff[x][y]]); } } if (y != 0) { dp[x][y][d] = max(dp[x][y][d], dp[x][y - 1][abs(d - diff[x][y])]); if (d + diff[x][y] <= lim) { dp[x][y][d] = max(dp[x][y][d], dp[x][y - 1][d + diff[x][y]]); } } } } } for (int d = 0; d <= base; d++) { if (dp[h - 1][w - 1][d] == 1) { cout << d << endl; return 0; } } }
replace
26
34
26
28
0
p02839
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef uint64_t u64; typedef int64_t s64; typedef uint32_t u32; typedef int32_t s32; typedef vector<s32> vs32; typedef vector<u32> vu32; typedef vector<s64> vs64; typedef vector<u64> vu64; const double PI = 3.14159265358979323846; #define MAX(x, y) ((x) < (y) ? (y) : (x)) #define MIN(x, y) ((x) > (y) ? (y) : (x)) #define rep(i, N) for (int i = 0; i < N; ++i) #define CEIL(x, y) (((x) + (y)-1) / (y)) #define MOD 1000000007ULL int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vs32> A(H, vs32(W)); vector<vs32> B(H, vs32(W)); rep(h, H) rep(w, W) cin >> A[h][w]; rep(h, H) rep(w, W) cin >> B[h][w]; vector<vector<vector<bool>>> dp( H, vector<vector<bool>>(W, vector<bool>((H + W) * 80 + 1, false))); dp[0][0][abs(A[0][0] - B[0][0])] = true; for (int h = 0; h < H; ++h) { for (int w = 0; w < W; ++w) { for (int k = 0; k <= (H + W) * 80; ++k) { int d = abs(A[h][w] - B[h][w]); if (h > 0) { dp[h][w][abs(k - d)] = dp[h][w][abs(k - d)] || dp[h - 1][w][k]; dp[h][w][abs(k + d)] = dp[h][w][abs(k - d)] || dp[h - 1][w][k]; } if (w > 0) { dp[h][w][abs(k - d)] = dp[h][w][abs(k - d)] || dp[h][w - 1][k]; dp[h][w][abs(k + d)] = dp[h][w][abs(k + d)] || dp[h][w - 1][k]; } } } } int ans; for (ans = 0; ans <= (H + W) * 80 && !dp[H - 1][W - 1][ans]; ++ans) ; cout << ans << "\n"; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef uint64_t u64; typedef int64_t s64; typedef uint32_t u32; typedef int32_t s32; typedef vector<s32> vs32; typedef vector<u32> vu32; typedef vector<s64> vs64; typedef vector<u64> vu64; const double PI = 3.14159265358979323846; #define MAX(x, y) ((x) < (y) ? (y) : (x)) #define MIN(x, y) ((x) > (y) ? (y) : (x)) #define rep(i, N) for (int i = 0; i < N; ++i) #define CEIL(x, y) (((x) + (y)-1) / (y)) #define MOD 1000000007ULL int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vs32> A(H, vs32(W)); vector<vs32> B(H, vs32(W)); rep(h, H) rep(w, W) cin >> A[h][w]; rep(h, H) rep(w, W) cin >> B[h][w]; vector<vector<vector<bool>>> dp( H, vector<vector<bool>>(W, vector<bool>((H + W) * 80 + 1, false))); dp[0][0][abs(A[0][0] - B[0][0])] = true; for (int h = 0; h < H; ++h) { for (int w = 0; w < W; ++w) { for (int k = 0; k <= (H + W) * 80; ++k) { if (dp[h][w][k]) { if (h < H - 1) { int d = abs(A[h + 1][w] - B[h + 1][w]); dp[h + 1][w][abs(k - d)] = true; dp[h + 1][w][abs(k + d)] = true; } if (w < W - 1) { int d = abs(A[h][w + 1] - B[h][w + 1]); dp[h][w + 1][abs(k - d)] = true; dp[h][w + 1][abs(k + d)] = true; } } } } } int ans; for (ans = 0; ans <= (H + W) * 80 && !dp[H - 1][W - 1][ans]; ++ans) ; cout << ans << "\n"; return 0; }
replace
54
62
54
65
0
p02839
C++
Runtime Error
/** * Created by hiramekun at 20:44 on 2019-12-08. */ #include <bits/stdc++.h> #pragma optimize("", off) using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) int main() { ll h, w; cin >> h >> w; vector<bool> v; v[0]; vector<int> v2; v2[0]; int a[h][w], b[h][w]; rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) cin >> b[i][j]; ll max_diff = 2 * 80 * 160; bool dp[h][w][max_diff]; rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false; ll mid = max_diff / 2; dp[0][0][b[0][0] - a[0][0] + mid] = true; dp[0][0][-b[0][0] + a[0][0] + mid] = true; rep(i, h) { rep(j, w) { rep(l, max_diff) { if (i + 1 <= h - 1) { assert(l - b[i + 1][j] + a[i + 1][j] >= 0); assert(l + b[i + 1][j] - a[i + 1][j] >= 0); dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] |= dp[i][j][l]; dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] |= dp[i][j][l]; } if (j + 1 <= w - 1) { dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] |= dp[i][j][l]; dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] |= dp[i][j][l]; } } } } ll ans = 1e18; rep(i, max_diff) { if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - mid)); } cout << ans << '\n'; }
/** * Created by hiramekun at 20:44 on 2019-12-08. */ #include <bits/stdc++.h> #pragma optimize("", off) using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) int main() { ll h, w; cin >> h >> w; vector<bool> v; v[0]; vector<int> v2; v2[0]; int a[h][w], b[h][w]; rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) cin >> b[i][j]; ll max_diff = 2 * 80 * 160; bool dp[h][w][max_diff]; rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false; ll mid = max_diff / 2; dp[0][0][b[0][0] - a[0][0] + mid] = true; dp[0][0][-b[0][0] + a[0][0] + mid] = true; rep(i, h) { rep(j, w) { rep(l, max_diff) { if (i + 1 <= h - 1) { dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] |= dp[i][j][l]; dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] |= dp[i][j][l]; } if (j + 1 <= w - 1) { dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] |= dp[i][j][l]; dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] |= dp[i][j][l]; } } } } ll ans = 1e18; rep(i, max_diff) { if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - mid)); } cout << ans << '\n'; }
replace
32
34
32
33
-6
8e2bdc06-1693-4b1a-92cf-025663b1b3b6.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02839/C++/s516123002.cpp:34: int main(): Assertion `l + b[i + 1][j] - a[i + 1][j] >= 0' failed.
p02839
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <string> #define ll int using namespace std; const ll l = 1000; const ll r = 19000; ll f[9][9][20000], a[90][90]; ll n, m, i, j, k, t, x, y, ans = 1e9; ll read() { ll f = 0, c = 1; char ch = getchar(); while ((ch < '0') || (ch > '9')) { if (ch == '-') c = -1; ch = getchar(); } while ((ch >= '0') && (ch <= '9')) { f = f * 10 + ch - '0'; ch = getchar(); } return c * f; } void dp(ll x, ll y) { // cout<<a[x][y]<<" "<<x<<" "<<y<<" "<<f[0][1][10000]<<endl; for (ll i = l; i < r; ++i) { // cout<<f[x][y][i]; // cout<<i<<" "<<a[x][y]<<" "<<x<<" "<<y<<" "<<f[x][y][i]<<endl; f[x][y][i] = f[x - 1][y][i - a[x][y]] | f[x - 1][y][i + a[x][y]] | f[x][y - 1][i - a[x][y]] | f[x][y - 1][i + a[x][y]]; /* f[x][y][i]|=f[x-1][y][i-a[x][y]]; cout<<"fa"; f[x][y][i]|=f[x-1][y][i+a[x][y]]; cout<<"fa"; f[x][y][i]|=f[x][y-1][i-a[x][y]]; cout<<"fa"; f[x][y][i]|=f[x][y-1][i+a[x][y]]; cout<<"fa";*/ // cout<<i<<" "<<f[x][y][i]<<endl; } } int main() { // freopen("test.in","r",stdin); // freopen("test.out","w",stdout); f[0][1][10000] = 1; n = read(); m = read(); for (ll i = 1; i <= n; ++i) { for (ll j = 1; j <= m; ++j) { a[i][j] = read(); } } for (ll i = 1; i <= n; ++i) { for (ll j = 1; j <= m; ++j) { a[i][j] = abs(a[i][j] - read()); // cout<<a[i][j]<<" "; } // cout<<endl; } for (ll i = 1; i <= n; ++i) { for (ll j = 1; j <= m; ++j) { dp(i, j); } } for (ll i = l; i <= r; ++i) { if (f[n][m][i]) ans = min(ans, abs(i - 10000)); } cout << ans; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <string> #define ll int using namespace std; const ll l = 1000; const ll r = 19000; ll f[90][90][20000], a[90][90]; ll n, m, i, j, k, t, x, y, ans = 1e9; ll read() { ll f = 0, c = 1; char ch = getchar(); while ((ch < '0') || (ch > '9')) { if (ch == '-') c = -1; ch = getchar(); } while ((ch >= '0') && (ch <= '9')) { f = f * 10 + ch - '0'; ch = getchar(); } return c * f; } void dp(ll x, ll y) { // cout<<a[x][y]<<" "<<x<<" "<<y<<" "<<f[0][1][10000]<<endl; for (ll i = l; i < r; ++i) { // cout<<f[x][y][i]; // cout<<i<<" "<<a[x][y]<<" "<<x<<" "<<y<<" "<<f[x][y][i]<<endl; f[x][y][i] = f[x - 1][y][i - a[x][y]] | f[x - 1][y][i + a[x][y]] | f[x][y - 1][i - a[x][y]] | f[x][y - 1][i + a[x][y]]; /* f[x][y][i]|=f[x-1][y][i-a[x][y]]; cout<<"fa"; f[x][y][i]|=f[x-1][y][i+a[x][y]]; cout<<"fa"; f[x][y][i]|=f[x][y-1][i-a[x][y]]; cout<<"fa"; f[x][y][i]|=f[x][y-1][i+a[x][y]]; cout<<"fa";*/ // cout<<i<<" "<<f[x][y][i]<<endl; } } int main() { // freopen("test.in","r",stdin); // freopen("test.out","w",stdout); f[0][1][10000] = 1; n = read(); m = read(); for (ll i = 1; i <= n; ++i) { for (ll j = 1; j <= m; ++j) { a[i][j] = read(); } } for (ll i = 1; i <= n; ++i) { for (ll j = 1; j <= m; ++j) { a[i][j] = abs(a[i][j] - read()); // cout<<a[i][j]<<" "; } // cout<<endl; } for (ll i = 1; i <= n; ++i) { for (ll j = 1; j <= m; ++j) { dp(i, j); } } for (ll i = l; i <= r; ++i) { if (f[n][m][i]) ans = min(ans, abs(i - 10000)); } cout << ans; return 0; }
replace
12
13
12
13
0
p02839
C++
Memory Limit Exceeded
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define repi(i, a, b) for (int i = (ll)(a); i < (ll)(b); i++) #define rrepi(i, a, b) for (int i = (ll)(b); i >= (ll)(a); i--) #define all(x) (x).begin(), (x).end() 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 (b < a) { a = b; return 1; } return 0; } typedef long long ll; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); ll h, w; cin >> h >> w; vector<vector<ll>> a(h, vector<ll>(w)); ll geta = 12800, sz = geta * 2 + 7; vector<vector<vector<ll>>> dp(h, vector<vector<ll>>(w, vector<ll>(sz))); auto tdp = dp; auto b = a; rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) cin >> b[i][j]; auto isin = [&](ll y, ll x) { if (y >= 0 && y < h && x >= 0 && x < w) return 1; return 0; }; ll d = abs(a[0][0] - b[0][0]); dp[0][0][geta - d] = 1; dp[0][0][geta + d] = 1; rep(i, h) rep(j, w) { if (i == 0 && j == 0) continue; ll d = abs(a[i][j] - b[i][j]); if (isin(i - 1, j)) { rep(k, sz) { if (dp[i - 1][j][k] == 1) { dp[i][j][k - d] = 1; dp[i][j][k + d] = 1; } } } if (isin(i, j - 1)) { rep(k, sz) { if (dp[i][j - 1][k]) { dp[i][j][k - d] = 1; dp[i][j][k + d] = 1; } } } } ll res = 1e9 + 7; rep(i, sz) { if (dp[h - 1][w - 1][i]) chmin(res, abs(i - geta)); } cout << res << endl; return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <cstdint> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define repi(i, a, b) for (int i = (ll)(a); i < (ll)(b); i++) #define rrepi(i, a, b) for (int i = (ll)(b); i >= (ll)(a); i--) #define all(x) (x).begin(), (x).end() 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 (b < a) { a = b; return 1; } return 0; } typedef long long ll; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); ll h, w; cin >> h >> w; vector<vector<ll>> a(h, vector<ll>(w)); ll geta = 12800, sz = geta * 2 + 7; vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(sz))); auto tdp = dp; auto b = a; rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) cin >> b[i][j]; auto isin = [&](ll y, ll x) { if (y >= 0 && y < h && x >= 0 && x < w) return 1; return 0; }; ll d = abs(a[0][0] - b[0][0]); dp[0][0][geta - d] = 1; dp[0][0][geta + d] = 1; rep(i, h) rep(j, w) { if (i == 0 && j == 0) continue; ll d = abs(a[i][j] - b[i][j]); if (isin(i - 1, j)) { rep(k, sz) { if (dp[i - 1][j][k] == 1) { dp[i][j][k - d] = 1; dp[i][j][k + d] = 1; } } } if (isin(i, j - 1)) { rep(k, sz) { if (dp[i][j - 1][k]) { dp[i][j][k - d] = 1; dp[i][j][k + d] = 1; } } } } ll res = 1e9 + 7; rep(i, sz) { if (dp[h - 1][w - 1][i]) chmin(res, abs(i - geta)); } cout << res << endl; return 0; }
replace
51
52
51
52
MLE
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; using namespace std; int h, w; int ma[80][80], mb[80][80], ch[80][80]; bool ans[80][80][16000]; int main() { cin >> h >> w; rep(i, h) rep(j, w) cin >> ma[i][j]; rep(i, h) rep(j, w) cin >> mb[i][j]; rep(i, h) rep(j, w) ch[i][j] = abs(ma[i][j] - mb[i][j]); ans[0][0][ch[0][0]] = true; rep(i, h) rep(j, w) rep(k, 640000) { if (ans[i][j][k]) { if (i + 1 < h) { ans[i + 1][j][k + ch[i + 1][j]] = true; ans[i + 1][j][abs(k - ch[i + 1][j])] = true; } if (j + 1 < w) { ans[i][j + 1][k + ch[i][j + 1]] = true; ans[i][j + 1][abs(k - ch[i][j + 1])] = true; } } } rep(k, 640000) if (ans[h - 1][w - 1][k]) { cout << k << endl; return 0; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; using namespace std; int h, w; int ma[80][80], mb[80][80], ch[80][80]; bool ans[80][80][16000]; int main() { cin >> h >> w; rep(i, h) rep(j, w) cin >> ma[i][j]; rep(i, h) rep(j, w) cin >> mb[i][j]; rep(i, h) rep(j, w) ch[i][j] = abs(ma[i][j] - mb[i][j]); ans[0][0][ch[0][0]] = true; rep(i, h) rep(j, w) rep(k, 16000) { if (ans[i][j][k]) { if (i + 1 < h) { ans[i + 1][j][k + ch[i + 1][j]] = true; ans[i + 1][j][abs(k - ch[i + 1][j])] = true; } if (j + 1 < w) { ans[i][j + 1][k + ch[i][j + 1]] = true; ans[i][j + 1][abs(k - ch[i][j + 1])] = true; } } } rep(k, 640000) if (ans[h - 1][w - 1][k]) { cout << k << endl; return 0; } }
replace
17
18
17
18
TLE
p02839
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> v1; typedef vector<v1> v2; typedef vector<vector<ll>> graph; const ll INF = 1ll << 50; const ll mod = 1000000007; ll h, w; ll a[81][81]; ll b[81][81]; ll offset = 81 * 160; ll dp[81][81][81 * 160 * 2]; ll ans = INF; int main() { cin >> h >> w; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { cin >> a[i][j]; } } for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { cin >> b[i][j]; } } dp[0][0][(a[0][0] - b[0][0]) + offset] = 1; dp[0][0][(b[0][0] - a[0][0]) + offset] = 1; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { if (i + 1 < h) { for (ll k = 0; k < 81 * 160 * 2; k++) { if (dp[i][j][k] == 1) { dp[i + 1][j][k + (a[i + 1][j] - b[i + 1][j])] = 1; dp[i + 1][j][k + (b[i + 1][j] - a[i + 1][j])] = 1; } } } if (j + 1 < w) { for (ll k = 0; k < 81 * 160 * 2; k++) { if (dp[i][j][k] == 1) { dp[i][j + 1][k + (a[i][j + 1] - b[i][j + 1])] = 1; dp[i][j + 1][k + (b[i][j + 1] - a[i][j + 1])] = 1; } } } } } // for(ll i = 0;i < h;i++){ // for(ll j = 0;j < w;j++){ // cerr << dp[i][j][offset+0]; // } // cerr << endl; // } for (ll k = 0; k < 81 * 160 * 2; k++) { if (dp[h - 1][w - 1][k]) { ans = min(ans, abs(k - offset)); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> v1; typedef vector<v1> v2; typedef vector<vector<ll>> graph; const ll INF = 1ll << 50; const ll mod = 1000000007; ll h, w; ll a[81][81]; ll b[81][81]; ll offset = 81 * 160; char dp[81][81][81 * 160 * 2]; ll ans = INF; int main() { cin >> h >> w; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { cin >> a[i][j]; } } for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { cin >> b[i][j]; } } dp[0][0][(a[0][0] - b[0][0]) + offset] = 1; dp[0][0][(b[0][0] - a[0][0]) + offset] = 1; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { if (i + 1 < h) { for (ll k = 0; k < 81 * 160 * 2; k++) { if (dp[i][j][k] == 1) { dp[i + 1][j][k + (a[i + 1][j] - b[i + 1][j])] = 1; dp[i + 1][j][k + (b[i + 1][j] - a[i + 1][j])] = 1; } } } if (j + 1 < w) { for (ll k = 0; k < 81 * 160 * 2; k++) { if (dp[i][j][k] == 1) { dp[i][j + 1][k + (a[i][j + 1] - b[i][j + 1])] = 1; dp[i][j + 1][k + (b[i][j + 1] - a[i][j + 1])] = 1; } } } } } // for(ll i = 0;i < h;i++){ // for(ll j = 0;j < w;j++){ // cerr << dp[i][j][offset+0]; // } // cerr << endl; // } for (ll k = 0; k < 81 * 160 * 2; k++) { if (dp[h - 1][w - 1][k]) { ans = min(ans, abs(k - offset)); } } cout << ans << endl; }
replace
16
17
16
17
MLE
p02839
C++
Memory Limit Exceeded
#include <bits/stdc++.h> #define endl enjoy_codeforces using lint = long long; int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); lint h, w; std::cin >> h >> w; std::vector<std::vector<lint>> a(h, std::vector<lint>(w)); for (lint i = 0; i < h; i++) { for (lint j = 0; j < w; j++) { std::cin >> a.at(i).at(j); } } for (lint i = 0; i < h; i++) { for (lint j = 0; j < w; j++) { lint x; std::cin >> x; a.at(i).at(j) = std::abs(a.at(i).at(j) - x); } } lint max = 20000; std::vector<std::vector<std::vector<lint>>> dp( h, std::vector<std::vector<lint>>(w, std::vector<lint>(max + 1, false))); dp.at(0).at(0).at(a.at(0).at(0)) = true; for (lint i = 0; i < h; i++) { for (lint j = 0; j < w; j++) { lint x = a.at(i).at(j); if (i) { for (lint k = 0; k <= max; k++) { dp.at(i).at(j).at(k) |= dp.at(i - 1).at(j).at(std::abs(k - x)) || k + x <= max && dp.at(i - 1).at(j).at(k + x); } } if (j) { for (lint k = 0; k <= max; k++) { dp.at(i).at(j).at(k) |= dp.at(i).at(j - 1).at(std::abs(k - x)) || k + x <= max && dp.at(i).at(j - 1).at(k + x); } } } } for (int i = 0;; i++) { if (dp.at(h - 1).at(w - 1).at(i)) { std::cout << i << '\n'; return 0; } } }
#include <bits/stdc++.h> #define endl enjoy_codeforces using lint = long long; int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); lint h, w; std::cin >> h >> w; std::vector<std::vector<lint>> a(h, std::vector<lint>(w)); for (lint i = 0; i < h; i++) { for (lint j = 0; j < w; j++) { std::cin >> a.at(i).at(j); } } for (lint i = 0; i < h; i++) { for (lint j = 0; j < w; j++) { lint x; std::cin >> x; a.at(i).at(j) = std::abs(a.at(i).at(j) - x); } } lint max = 80 * 80 * 2 + 100; std::vector<std::vector<std::vector<lint>>> dp( h, std::vector<std::vector<lint>>(w, std::vector<lint>(max + 1, false))); dp.at(0).at(0).at(a.at(0).at(0)) = true; for (lint i = 0; i < h; i++) { for (lint j = 0; j < w; j++) { lint x = a.at(i).at(j); if (i) { for (lint k = 0; k <= max; k++) { dp.at(i).at(j).at(k) |= dp.at(i - 1).at(j).at(std::abs(k - x)) || k + x <= max && dp.at(i - 1).at(j).at(k + x); } } if (j) { for (lint k = 0; k <= max; k++) { dp.at(i).at(j).at(k) |= dp.at(i).at(j - 1).at(std::abs(k - x)) || k + x <= max && dp.at(i).at(j - 1).at(k + x); } } } } for (int i = 0;; i++) { if (dp.at(h - 1).at(w - 1).at(i)) { std::cout << i << '\n'; return 0; } } }
replace
21
22
21
22
MLE
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int a[85][85], b[85][85]; bool dp[85][85][12800 + 12800 + 1]; int main() { int h, w; scanf("%d%d", &h, &w); int mid = 128000; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) scanf("%d", &a[i][j]); for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) scanf("%d", &b[i][j]); dp[1][1][mid + a[1][1] - b[1][1]] = 1; dp[1][1][mid + b[1][1] - a[1][1]] = 1; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (i == 1 && j == 1) continue; if (i > 1) { for (int k = 0; k <= mid + mid; k++) { if (!dp[i - 1][j][k]) continue; dp[i][j][k + a[i][j] - b[i][j]] = 1; dp[i][j][k + b[i][j] - a[i][j]] = 1; } } if (j > 1) { for (int k = 0; k <= mid + mid; k++) { if (!dp[i][j - 1][k]) continue; dp[i][j][k + a[i][j] - b[i][j]] = 1; dp[i][j][k + b[i][j] - a[i][j]] = 1; } } } } int ans = 1e9; for (int k = 0; k <= mid + mid; k++) if (dp[h][w][k]) ans = min(ans, abs(k - mid)); printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; int a[85][85], b[85][85]; bool dp[85][85][12800 + 12800 + 1]; int main() { int h, w; scanf("%d%d", &h, &w); int mid = 12800; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) scanf("%d", &a[i][j]); for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) scanf("%d", &b[i][j]); dp[1][1][mid + a[1][1] - b[1][1]] = 1; dp[1][1][mid + b[1][1] - a[1][1]] = 1; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (i == 1 && j == 1) continue; if (i > 1) { for (int k = 0; k <= mid + mid; k++) { if (!dp[i - 1][j][k]) continue; dp[i][j][k + a[i][j] - b[i][j]] = 1; dp[i][j][k + b[i][j] - a[i][j]] = 1; } } if (j > 1) { for (int k = 0; k <= mid + mid; k++) { if (!dp[i][j - 1][k]) continue; dp[i][j][k + a[i][j] - b[i][j]] = 1; dp[i][j][k + b[i][j] - a[i][j]] = 1; } } } } int ans = 1e9; for (int k = 0; k <= mid + mid; k++) if (dp[h][w][k]) ans = min(ans, abs(k - mid)); printf("%d\n", ans); }
replace
7
8
7
8
TLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> const int N = 67; const int M = 6400; int n, m, A[N][N], B[N][N]; std::bitset<M * 2 + 1> dp[N][N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", A[i] + j); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", B[i] + j); dp[0][1].set(M); dp[1][0].set(M); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int d = std::abs(A[i][j] - B[i][j]); dp[i][j] |= (dp[i - 1][j] << d) | (dp[i - 1][j] >> d); dp[i][j] |= (dp[i][j - 1] << d) | (dp[i][j - 1] >> d); } int ans = 1e9; for (int i = 0; i <= 2 * M; i++) if (dp[n][m].test(i)) ans = std::min(ans, std::abs(i - M)); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> const int N = 88; const int M = 6400; int n, m, A[N][N], B[N][N]; std::bitset<M * 2 + 1> dp[N][N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", A[i] + j); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", B[i] + j); dp[0][1].set(M); dp[1][0].set(M); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int d = std::abs(A[i][j] - B[i][j]); dp[i][j] |= (dp[i - 1][j] << d) | (dp[i - 1][j] >> d); dp[i][j] |= (dp[i][j - 1] << d) | (dp[i][j - 1] >> d); } int ans = 1e9; for (int i = 0; i <= 2 * M; i++) if (dp[n][m].test(i)) ans = std::min(ans, std::abs(i - M)); printf("%d\n", ans); return 0; }
replace
2
3
2
3
0
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, j, n) for (int i = j; i < n; i++) #define all(x) (x).begin(), (x).end() #define pii pair<int, int> #define pll pair<ll, ll> #define vc vector #define a first #define b second #define pb pusu_back #define INF (1000000000) #define MOD (1000000007) #define MAX (5000000) ///////////////////////////////////////////////////////// class XY { public: ll x, y; XY() { x = y = 0; } XY(ll u, ll v) { x = u; y = v; } }; template <typename T1, typename T2> void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> void chmax(T1 &a, T2 b) { if (a < b) a = b; } template <typename T1, typename T2> ll mypow(T1 a, T2 n) { if (n == 0) return 1; if (n == 1) return a; if (n % 2) return a * mypow(a, n - 1); ll tmp = mypow(a, n / 2); return tmp * tmp; } template <typename T> int BS(vector<T> &V, int left, int right, T key) { int mid = (left + right) / 2; if (V[mid] <= key) left = mid; else right = mid; if (right - mid == 1) return left; else return BS(V, left, right, key); } ll comb(ll n, ll r) { ll res = 1; rep(i, 0, r) { res *= n - i; res /= i + 1; } return res; } template <typename T> T euclid(T a, T b) { if (b == 0) return a; else return euclid(b, a % b); } ///////////////////////////////////////////////////////// // MOD of Combination /* vector<ll> fact(MAX, 0), fact_inv(MAX, 0); ll pow_mod(ll a, ll b){ ll res = 1; while(b > 0){ if(b & 1) res = res * a % MOD; a = a * a % MOD; b >>= 1; } return res; } void prepare_comb_mod(ll n){ fact[0] = 1LL; for(ll i = 0; i < n; i++) fact[i+1] = fact[i] * (i + 1) % MOD; fact_inv[n] = pow_mod(fact[n], MOD - 2); for(ll i = n - 1; i >= 0; i--) fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD; } ll comb_mod(ll n, ll r){ return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD; } */ ///////////////////////////////////////////////////////// // Union-Find Tree template <class T> struct UFT { vector<int> par; vector<int> rank; vector<T> diff_weight; UFT(int n = 1, T w = 0) { init(n, w); } void init(int n = 1, T w = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); rep(i, 0, n) { par[i] = i; rank[i] = 0; diff_weight[i] = w; } } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } bool unite(int x, int y, int w = 0) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) { par[x] = y; diff_weight[x] = -w; } else { par[y] = x; diff_weight[y] = w; if (rank[x] == rank[y]) rank[x]++; } return true; } bool same(int x, int y) { return root(x) == root(y); } T weight(int x) { root(x); return diff_weight[x]; } T diff(int x, int y) { return weight(y) - weight(x); } }; ///////////////////////////////////////////////////////// ll digit(ll N) { return log10(N) + 1; } ///////////////////////////////////////////////////////// void ans(bool b) { if (b) cout << "yes" << endl; else cout << "no" << endl; } void Ans(bool b) { if (b) cout << "Yes" << endl; else cout << "No" << endl; } void ANS(bool b) { if (b) cout << "YES" << endl; else cout << "NO" << endl; } void out(string S) { cout << S << endl; } void out(int N) { cout << N << endl; } void out(ll N) { cout << N << endl; } ///////////////////////////////////////////////////////// void Main() { int num = 600000; int H, W; cin >> H >> W; vc<vc<int>> A(H, vc<int>(W)), B(H, vc<int>(W)), T(H, vc<int>(W)); rep(i, 0, H) rep(j, 0, W) cin >> A[i][j]; rep(i, 0, H) rep(j, 0, W) cin >> B[i][j]; rep(i, 0, H) rep(j, 0, W) T[i][j] = abs(A[i][j] - B[i][j]); vc<vc<vc<bool>>> dp(H, vc<vc<bool>>(W, vc<bool>(num, false))); dp[0][0][T[0][0]] = true; rep(i, 0, H) { rep(j, 0, W) { rep(k, 0, num) { if (i > 0 && (dp[i - 1][j][abs(k - T[i][j])] || dp[i - 1][j][abs(k + T[i][j])])) { dp[i][j][k] = true; } if (j > 0 && (dp[i][j - 1][abs(k - T[i][j])] || dp[i][j - 1][abs(k + T[i][j])])) { dp[i][j][k] = true; } } } } rep(i, 0, num) { if (dp[H - 1][W - 1][i]) { out(i); break; } } } ///////////////////////////////////////////////////////// int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << std::fixed << std::setprecision(15); Main(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, j, n) for (int i = j; i < n; i++) #define all(x) (x).begin(), (x).end() #define pii pair<int, int> #define pll pair<ll, ll> #define vc vector #define a first #define b second #define pb pusu_back #define INF (1000000000) #define MOD (1000000007) #define MAX (5000000) ///////////////////////////////////////////////////////// class XY { public: ll x, y; XY() { x = y = 0; } XY(ll u, ll v) { x = u; y = v; } }; template <typename T1, typename T2> void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> void chmax(T1 &a, T2 b) { if (a < b) a = b; } template <typename T1, typename T2> ll mypow(T1 a, T2 n) { if (n == 0) return 1; if (n == 1) return a; if (n % 2) return a * mypow(a, n - 1); ll tmp = mypow(a, n / 2); return tmp * tmp; } template <typename T> int BS(vector<T> &V, int left, int right, T key) { int mid = (left + right) / 2; if (V[mid] <= key) left = mid; else right = mid; if (right - mid == 1) return left; else return BS(V, left, right, key); } ll comb(ll n, ll r) { ll res = 1; rep(i, 0, r) { res *= n - i; res /= i + 1; } return res; } template <typename T> T euclid(T a, T b) { if (b == 0) return a; else return euclid(b, a % b); } ///////////////////////////////////////////////////////// // MOD of Combination /* vector<ll> fact(MAX, 0), fact_inv(MAX, 0); ll pow_mod(ll a, ll b){ ll res = 1; while(b > 0){ if(b & 1) res = res * a % MOD; a = a * a % MOD; b >>= 1; } return res; } void prepare_comb_mod(ll n){ fact[0] = 1LL; for(ll i = 0; i < n; i++) fact[i+1] = fact[i] * (i + 1) % MOD; fact_inv[n] = pow_mod(fact[n], MOD - 2); for(ll i = n - 1; i >= 0; i--) fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD; } ll comb_mod(ll n, ll r){ return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD; } */ ///////////////////////////////////////////////////////// // Union-Find Tree template <class T> struct UFT { vector<int> par; vector<int> rank; vector<T> diff_weight; UFT(int n = 1, T w = 0) { init(n, w); } void init(int n = 1, T w = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); rep(i, 0, n) { par[i] = i; rank[i] = 0; diff_weight[i] = w; } } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } bool unite(int x, int y, int w = 0) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) { par[x] = y; diff_weight[x] = -w; } else { par[y] = x; diff_weight[y] = w; if (rank[x] == rank[y]) rank[x]++; } return true; } bool same(int x, int y) { return root(x) == root(y); } T weight(int x) { root(x); return diff_weight[x]; } T diff(int x, int y) { return weight(y) - weight(x); } }; ///////////////////////////////////////////////////////// ll digit(ll N) { return log10(N) + 1; } ///////////////////////////////////////////////////////// void ans(bool b) { if (b) cout << "yes" << endl; else cout << "no" << endl; } void Ans(bool b) { if (b) cout << "Yes" << endl; else cout << "No" << endl; } void ANS(bool b) { if (b) cout << "YES" << endl; else cout << "NO" << endl; } void out(string S) { cout << S << endl; } void out(int N) { cout << N << endl; } void out(ll N) { cout << N << endl; } ///////////////////////////////////////////////////////// void Main() { int num = 12810; int H, W; cin >> H >> W; vc<vc<int>> A(H, vc<int>(W)), B(H, vc<int>(W)), T(H, vc<int>(W)); rep(i, 0, H) rep(j, 0, W) cin >> A[i][j]; rep(i, 0, H) rep(j, 0, W) cin >> B[i][j]; rep(i, 0, H) rep(j, 0, W) T[i][j] = abs(A[i][j] - B[i][j]); vc<vc<vc<bool>>> dp(H, vc<vc<bool>>(W, vc<bool>(num, false))); dp[0][0][T[0][0]] = true; rep(i, 0, H) { rep(j, 0, W) { rep(k, 0, num) { if (i > 0 && (dp[i - 1][j][abs(k - T[i][j])] || dp[i - 1][j][abs(k + T[i][j])])) { dp[i][j][k] = true; } if (j > 0 && (dp[i][j - 1][abs(k - T[i][j])] || dp[i][j - 1][abs(k + T[i][j])])) { dp[i][j][k] = true; } } } } rep(i, 0, num) { if (dp[H - 1][W - 1][i]) { out(i); break; } } } ///////////////////////////////////////////////////////// int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << std::fixed << std::setprecision(15); Main(); }
replace
200
201
200
201
TLE
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; const int ac = 83, mod = 1e9; bool A[ac][ac][ac * ac * 2]; ll B[ac][ac]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> B[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int x; cin >> x; B[i][j] -= x; } A[0][1][ac * ac] = 1; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { for (int k = max(0ll, B[i][j]); k < ac * ac * 2 - max(0ll, B[i][j]); k++) { cerr << k - B[i][j] << ' '; if (A[i - 1][j][k - B[i][j]]) A[i][j][k] = 1; if (A[i - 1][j][k + B[i][j]]) A[i][j][k] = 1; if (A[i][j - 1][k - B[i][j]]) A[i][j][k] = 1; if (A[i][j - 1][k + B[i][j]]) A[i][j][k] = 1; } } int b = 1e9; for (int i = 0; i < ac * ac * 2; i++) if (A[n][m][i]) b = min(b, abs(i - ac * ac)); cout << b << '\n'; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int ac = 83, mod = 1e9; bool A[ac][ac][ac * ac * 2]; ll B[ac][ac]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> B[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int x; cin >> x; B[i][j] -= x; } A[0][1][ac * ac] = 1; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { for (int k = max(0ll, B[i][j]); k < ac * ac * 2 - max(0ll, B[i][j]); k++) { if (A[i - 1][j][k - B[i][j]]) A[i][j][k] = 1; if (A[i - 1][j][k + B[i][j]]) A[i][j][k] = 1; if (A[i][j - 1][k - B[i][j]]) A[i][j][k] = 1; if (A[i][j - 1][k + B[i][j]]) A[i][j][k] = 1; } } int b = 1e9; for (int i = 0; i < ac * ac * 2; i++) if (A[n][m][i]) b = min(b, abs(i - ac * ac)); cout << b << '\n'; }
delete
27
28
27
27
TLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define INF LONG_MAX #define MOD 1000000007 #define rng(a) a.begin(), a.end() #define rrng(a) a.end(), a.begin() int main() { ios::sync_with_stdio(false); cin.tie(0); int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)), B(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> B[i][j]; } } vector<vector<vector<int>>> dp( H, vector<vector<int>>(W, vector<int>((80 + 80) * 80 + 1))); dp[0][0][A[0][0] - B[0][0]] = true; dp[0][0][-A[0][0] + B[0][0]] = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k <= (80 + 80) * 80; k++) { if (i + 1 < H) { dp[i + 1][j][k] |= dp[i][j][k + abs(A[i + 1][j] - B[i + 1][j])]; dp[i + 1][j][k] |= dp[i][j][abs(k - abs(A[i + 1][j] - B[i + 1][j]))]; } if (j + 1 < W) { dp[i][j + 1][k] |= dp[i][j][k + abs(A[i][j + 1] - B[i][j + 1])]; dp[i][j + 1][k] |= dp[i][j][abs(k - abs(A[i][j + 1] - B[i][j + 1]))]; } } } } for (int k = 0; k <= (80 + 80) * 80; k++) { if (dp[H - 1][W - 1][k]) { cout << k; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define INF LONG_MAX #define MOD 1000000007 #define rng(a) a.begin(), a.end() #define rrng(a) a.end(), a.begin() int main() { ios::sync_with_stdio(false); cin.tie(0); int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)), B(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> B[i][j]; } } vector<vector<vector<int>>> dp( H, vector<vector<int>>(W, vector<int>((80 + 80) * 80 + 1))); dp[0][0][abs(A[0][0] - B[0][0])] = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k <= (80 + 80) * 80; k++) { if (i + 1 < H) { dp[i + 1][j][k] |= dp[i][j][k + abs(A[i + 1][j] - B[i + 1][j])]; dp[i + 1][j][k] |= dp[i][j][abs(k - abs(A[i + 1][j] - B[i + 1][j]))]; } if (j + 1 < W) { dp[i][j + 1][k] |= dp[i][j][k + abs(A[i][j + 1] - B[i][j + 1])]; dp[i][j + 1][k] |= dp[i][j][abs(k - abs(A[i][j + 1] - B[i][j + 1]))]; } } } } for (int k = 0; k <= (80 + 80) * 80; k++) { if (dp[H - 1][W - 1][k]) { cout << k; break; } } return 0; }
replace
31
33
31
32
-6
free(): invalid pointer
p02839
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; const int INF = 1 << 30; const ll LLINF = 1LL << 62; int mod = 1000000007; int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); int base = 80 * 80; int H, W; cin >> H >> W; pair<int, int> nums[H][W]; rep(i, H) rep(j, W) cin >> nums[i][j].first; rep(i, H) rep(j, W) cin >> nums[i][j].second; vector<bool> dp[H][W]; rep(i, H) rep(j, W) dp[i][j].resize(base * 2 + 1, false); dp[0][0][base + nums[0][0].first - nums[0][0].second] = true; dp[0][0][base + nums[0][0].second - nums[0][0].first] = true; int sz = base * 2; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (i + 1 < H) { int fst = nums[i + 1][j].first, snd = nums[i + 1][j].second; for (int k = 0; k <= sz; k++) { if (dp[i][j][k]) { dp[i + 1][j][k + (fst - snd)] = true; dp[i + 1][j][k + (snd - fst)] = true; } } } if (j + 1 < W) { int fst = nums[i][j + 1].first, snd = nums[i][j + 1].second; for (int k = 0; k <= sz; k++) { if (dp[i][j][k]) { dp[i][j + 1][k + (fst - snd)] = true; dp[i][j + 1][k + (snd - fst)] = true; } } } } } int ans = mod; for (int i = 0; i <= sz; i++) { if (dp[H - 1][W - 1][i]) ans = min(ans, abs(i - base)); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; const int INF = 1 << 30; const ll LLINF = 1LL << 62; int mod = 1000000007; int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); int base = 80 * 160; int H, W; cin >> H >> W; pair<int, int> nums[H][W]; rep(i, H) rep(j, W) cin >> nums[i][j].first; rep(i, H) rep(j, W) cin >> nums[i][j].second; vector<bool> dp[H][W]; rep(i, H) rep(j, W) dp[i][j].resize(base * 2 + 1, false); dp[0][0][base + nums[0][0].first - nums[0][0].second] = true; dp[0][0][base + nums[0][0].second - nums[0][0].first] = true; int sz = base * 2; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (i + 1 < H) { int fst = nums[i + 1][j].first, snd = nums[i + 1][j].second; for (int k = 0; k <= sz; k++) { if (dp[i][j][k]) { dp[i + 1][j][k + (fst - snd)] = true; dp[i + 1][j][k + (snd - fst)] = true; } } } if (j + 1 < W) { int fst = nums[i][j + 1].first, snd = nums[i][j + 1].second; for (int k = 0; k <= sz; k++) { if (dp[i][j][k]) { dp[i][j + 1][k + (fst - snd)] = true; dp[i][j + 1][k + (snd - fst)] = true; } } } } } int ans = mod; for (int i = 0; i <= sz; i++) { if (dp[H - 1][W - 1][i]) ans = min(ans, abs(i - base)); } cout << ans << endl; return 0; }
replace
15
16
15
16
0
p02839
C++
Runtime Error
// pragma // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define YOU using #define DONT namespace #define SAY std YOU DONT SAY; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef vector<int> vi; typedef vector<pii> vii; typedef vector<pll> vll; #define REPP(i, a, b, d) for (int i = a; i <= b; i += d) #define REP(i, a, b) REPP(i, a, b, 1) #define REVV(i, a, b, d) for (int i = a; i >= b; i -= d) #define REV(i, a, b) REVV(i, a, b, 1) #define FOR(i, a) for (int i = 0; i < a; i++) #define FORD(i, a) for (int i = (int)a - 1; i >= 0; i--) #define pb push_back #define F first #define S second const int OO = 1e9; const ll INF = 1e18; const int irand(int lo, int hi) { return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo; } const ll lrand(ll lo, ll hi) { return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo; } #define getc getchar template <typename T> T getnum() { int sign = 1; T ret = 0; char c; do { c = getc(); } while (c == ' ' || c == '\n'); if (c == '-') sign = -1; else ret = c - '0'; while (1) { c = getc(); if (c < '0' || c > '9') break; ret = 10 * ret + c - '0'; } return sign * ret; } inline void ini(int &x) { x = getnum<int>(); } inline void scani(int &x) { scanf("%d", &x); } // end of macro const int N = 5; int h, w; int arr[N][N]; bitset<4 * N * N + 5> btt[N][N]; // offset by 2*N*N const int OFFSET = 2 * N * N; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define endl '\n' cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(10); cin >> h >> w; FOR(i, h) { FOR(j, w) { cin >> arr[i][j]; } } FOR(i, h) { FOR(j, w) { int x; cin >> x; arr[i][j] -= x; arr[i][j] = abs(arr[i][j]); } } btt[0][0][arr[0][0] + OFFSET] = 1; FOR(i, h) { FOR(j, w) { if (i + 1 < h) { auto curr = btt[i][j]; curr = (curr >> arr[i + 1][j]) | (curr << arr[i + 1][j]); btt[i + 1][j] |= curr; } if (j + 1 < w) { auto curr = btt[i][j]; curr = (curr >> arr[i][j + 1]) | (curr << arr[i][j + 1]); btt[i][j + 1] |= curr; } // printf("%d %d\n",i,j); // FOR(k,4*N*N+5){ // if(btt[i][j][k] == 1)printf("%d ",k - //OFFSET); }printf("\n"); } } int ans = OO; // cout << btt[h-1][w-1] << endl; FOR(i, 4 * N * N + 5) { if (btt[h - 1][w - 1][i] == 1) { // printf("%d\n",i - OFFSET); ans = min(ans, abs(i - OFFSET)); } } cout << ans << endl; return 0; }
// pragma // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define YOU using #define DONT namespace #define SAY std YOU DONT SAY; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef vector<int> vi; typedef vector<pii> vii; typedef vector<pll> vll; #define REPP(i, a, b, d) for (int i = a; i <= b; i += d) #define REP(i, a, b) REPP(i, a, b, 1) #define REVV(i, a, b, d) for (int i = a; i >= b; i -= d) #define REV(i, a, b) REVV(i, a, b, 1) #define FOR(i, a) for (int i = 0; i < a; i++) #define FORD(i, a) for (int i = (int)a - 1; i >= 0; i--) #define pb push_back #define F first #define S second const int OO = 1e9; const ll INF = 1e18; const int irand(int lo, int hi) { return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo; } const ll lrand(ll lo, ll hi) { return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo; } #define getc getchar template <typename T> T getnum() { int sign = 1; T ret = 0; char c; do { c = getc(); } while (c == ' ' || c == '\n'); if (c == '-') sign = -1; else ret = c - '0'; while (1) { c = getc(); if (c < '0' || c > '9') break; ret = 10 * ret + c - '0'; } return sign * ret; } inline void ini(int &x) { x = getnum<int>(); } inline void scani(int &x) { scanf("%d", &x); } // end of macro const int N = 80; int h, w; int arr[N][N]; bitset<4 * N * N + 5> btt[N][N]; // offset by 2*N*N const int OFFSET = 2 * N * N; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define endl '\n' cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(10); cin >> h >> w; FOR(i, h) { FOR(j, w) { cin >> arr[i][j]; } } FOR(i, h) { FOR(j, w) { int x; cin >> x; arr[i][j] -= x; arr[i][j] = abs(arr[i][j]); } } btt[0][0][arr[0][0] + OFFSET] = 1; FOR(i, h) { FOR(j, w) { if (i + 1 < h) { auto curr = btt[i][j]; curr = (curr >> arr[i + 1][j]) | (curr << arr[i + 1][j]); btt[i + 1][j] |= curr; } if (j + 1 < w) { auto curr = btt[i][j]; curr = (curr >> arr[i][j + 1]) | (curr << arr[i][j + 1]); btt[i][j + 1] |= curr; } // printf("%d %d\n",i,j); // FOR(k,4*N*N+5){ // if(btt[i][j][k] == 1)printf("%d ",k - //OFFSET); }printf("\n"); } } int ans = OO; // cout << btt[h-1][w-1] << endl; FOR(i, 4 * N * N + 5) { if (btt[h - 1][w - 1][i] == 1) { // printf("%d\n",i - OFFSET); ans = min(ans, abs(i - OFFSET)); } } cout << ans << endl; return 0; }
replace
73
74
73
74
0
p02839
C++
Memory Limit Exceeded
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define rng(a) a.begin(), a.end() #define ina(n, a) \ cin >> n; \ for (int i = 1; i <= n; i++) \ cin >> a[i] #define sz(x) (int)(x).size() #define se second #define fi first #define prev coyhhhhhhyoc #define next sdNNNmNNNNNNNmds #define y0 hNNNNy_yNNNNNN_sNh #define y1 mNNNNNdtdNNNNNNtsNNm #define yn mNNNNNNNNy___smNNNms #define tm oooooosyysooooot #define read tyhyt #define rank ytmNmo #define index yyy #define pb push_back #define pcnt __builtin_popcountll #define rrep(i, a, b) for (int i = (b); i >= (a); i--) #define rall(x, a) for (auto x : a) #define MOD 1000000007 #define endl "\n" typedef long long ll; using namespace std; const int N = 88, X = 22222; int a[N][N], b[N][N]; int dp[N][N][2 * X]; int n, m; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; rep(i, 1, n) { rep(j, 1, m) { cin >> a[i][j]; } } rep(i, 1, n) { rep(j, 1, m) { cin >> b[i][j]; } } dp[0][1][X] = true; rep(i, 1, n) { rep(j, 1, m) { int diff = a[i][j] - b[i][j]; rep(px, 0, 2 * X - 1) { int x = px + diff; if (x >= 0 && x < 2 * X && dp[i - 1][j][px] || dp[i][j - 1][px]) { dp[i][j][x] = true; } x = px - diff; if (x >= 0 && x < 2 * X && dp[i - 1][j][px] || dp[i][j - 1][px]) { dp[i][j][x] = true; } } } } int ans = 1e9; rep(x, 0, 2 * X - 1) { if (dp[n][m][x]) { ans = min(ans, abs(x - X)); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define rng(a) a.begin(), a.end() #define ina(n, a) \ cin >> n; \ for (int i = 1; i <= n; i++) \ cin >> a[i] #define sz(x) (int)(x).size() #define se second #define fi first #define prev coyhhhhhhyoc #define next sdNNNmNNNNNNNmds #define y0 hNNNNy_yNNNNNN_sNh #define y1 mNNNNNdtdNNNNNNtsNNm #define yn mNNNNNNNNy___smNNNms #define tm oooooosyysooooot #define read tyhyt #define rank ytmNmo #define index yyy #define pb push_back #define pcnt __builtin_popcountll #define rrep(i, a, b) for (int i = (b); i >= (a); i--) #define rall(x, a) for (auto x : a) #define MOD 1000000007 #define endl "\n" typedef long long ll; using namespace std; const int N = 88, X = 12721; int a[N][N], b[N][N]; int dp[N][N][2 * X]; int n, m; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; rep(i, 1, n) { rep(j, 1, m) { cin >> a[i][j]; } } rep(i, 1, n) { rep(j, 1, m) { cin >> b[i][j]; } } dp[0][1][X] = true; rep(i, 1, n) { rep(j, 1, m) { int diff = a[i][j] - b[i][j]; rep(px, 0, 2 * X - 1) { int x = px + diff; if (x >= 0 && x < 2 * X && dp[i - 1][j][px] || dp[i][j - 1][px]) { dp[i][j][x] = true; } x = px - diff; if (x >= 0 && x < 2 * X && dp[i - 1][j][px] || dp[i][j - 1][px]) { dp[i][j][x] = true; } } } } int ans = 1e9; rep(x, 0, 2 * X - 1) { if (dp[n][m][x]) { ans = min(ans, abs(x - X)); } } cout << ans << endl; return 0; }
replace
28
29
28
29
MLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; const long long INF = 1e17; int main() { ll H, W; cin >> H >> W; vector<vector<ll>> a(H, vector<ll>(W)); vector<vector<ll>> b(H, vector<ll>(W)); vector<vector<ll>> c(H, vector<ll>(W)); rep(i, H) rep(j, W) cin >> a[i][j]; rep(i, H) rep(j, W) cin >> b[i][j]; rep(i, H) rep(j, W) c[i][j] = b[i][j] - a[i][j]; vector<vector<vector<bool>>> dp( H, vector<vector<bool>>(W, vector<bool>(82 * (H + W), false))); dp[0][0][abs(c[0][0])] = true; rep(i, H) rep(j, W) rep(k, 82 * (H + W)) { if (i < H - 1 && dp[i][j][k]) { dp[i + 1][j][k + abs(c[i + 1][j])] = true; dp[i + 1][j][abs(k - abs(c[i + 1][j]))] = true; } if (i < W - 1 && dp[i][j][k]) { dp[i][j + 1][k + abs(c[i][j + 1])] = true; dp[i][j + 1][abs(k - abs(c[i][j + 1]))] = true; } } ll ans = INF; rep(i, 82 * (H + W)) { if (dp[H - 1][W - 1][i]) ans = i < ans ? i : ans; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; const long long INF = 1e17; int main() { ll H, W; cin >> H >> W; vector<vector<ll>> a(H, vector<ll>(W)); vector<vector<ll>> b(H, vector<ll>(W)); vector<vector<ll>> c(H, vector<ll>(W)); rep(i, H) rep(j, W) cin >> a[i][j]; rep(i, H) rep(j, W) cin >> b[i][j]; rep(i, H) rep(j, W) c[i][j] = b[i][j] - a[i][j]; vector<vector<vector<bool>>> dp( H, vector<vector<bool>>(W, vector<bool>(82 * (H + W), false))); dp[0][0][abs(c[0][0])] = true; rep(i, H) rep(j, W) rep(k, 82 * (H + W)) { if (i < H - 1 && dp[i][j][k]) { dp[i + 1][j][k + abs(c[i + 1][j])] = true; dp[i + 1][j][abs(k - abs(c[i + 1][j]))] = true; } if (j < W - 1 && dp[i][j][k]) { dp[i][j + 1][k + abs(c[i][j + 1])] = true; dp[i][j + 1][abs(k - abs(c[i][j + 1]))] = true; } } ll ans = INF; rep(i, 82 * (H + W)) { if (dp[H - 1][W - 1][i]) ans = i < ans ? i : ans; } cout << ans << endl; return 0; }
replace
24
25
24
25
-11
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define x first #define y second #define fastio ios_base::sync_with_stdio(0), cin.tie(0) using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef pair<ll, ll> pll; int n, m; int a[80][80]; int b[80][80]; bool cache[80][80][12805]; const int dx[] = {1, 0}; const int dy[] = {0, 1}; bool f(int x, int y, int k) { cache[x][y][k] = true; for (int d = 0; d < 2; d++) { int nx = x + dx[d], ny = y + dy[d]; if (!(0 <= nx && nx < n && 0 <= ny && ny < m)) continue; int t = abs(a[nx][ny] - b[nx][ny]); f(nx, ny, k + t); f(nx, ny, abs(k - t)); } } int main() { fastio; cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> b[i][j]; f(0, 0, abs(a[0][0] - b[0][0])); for (int i = 0; i <= 12800; i++) if (cache[n - 1][m - 1][i]) { cout << i << '\n'; return 0; } }
#include <bits/stdc++.h> #define x first #define y second #define fastio ios_base::sync_with_stdio(0), cin.tie(0) using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef pair<ll, ll> pll; int n, m; int a[80][80]; int b[80][80]; bool cache[80][80][12805]; const int dx[] = {1, 0}; const int dy[] = {0, 1}; void f(int x, int y, int k) { if (cache[x][y][k]) return; cache[x][y][k] = true; for (int d = 0; d < 2; d++) { int nx = x + dx[d], ny = y + dy[d]; if (!(0 <= nx && nx < n && 0 <= ny && ny < m)) continue; int t = abs(a[nx][ny] - b[nx][ny]); f(nx, ny, k + t); f(nx, ny, abs(k - t)); } } int main() { fastio; cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> b[i][j]; f(0, 0, abs(a[0][0] - b[0][0])); for (int i = 0; i <= 12800; i++) if (cache[n - 1][m - 1][i]) { cout << i << '\n'; return 0; } }
replace
18
19
18
21
TLE
p02839
C++
Memory Limit Exceeded
// #undef _DEBUG // #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long #define ll long long auto start_time = system_clock::now(); #define debugName(VariableName) #VariableName /*@formatter:off*/ template <class T> struct mvec { vector<T> v; int n; //+-n mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} T &operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #ifdef _DEBUG template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[0]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef _DEBUG // 区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapip unordered_map<ll, P> #else // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> #endif struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } // 衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 */ } bool operator==(const T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } //@マクロ省略系 型,構造 #define double long double #define pow powl #define ull unsigned long long using dou = double; using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using mvp = mvec<P>; using mvt = mvec<T>; #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define wh while #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](auto &p) { return p right; } // マクロ省略系 コンテナ using vi = vector<ll>; using vb = vector<bool>; using vs = vector<string>; using vd = vector<double>; using vc = vector<char>; using vp = vector<P>; using vt = vector<T>; #define V vector #define o_vvt(o1, o2, o3, o4, name, ...) name #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vvi(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) #define vvt(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__) #define vv(type, ...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__) template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__) #define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__) #define vns(name, ...) auto name = make_v<string>(__VA_ARGS__) #define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__) #define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__) #define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__) #define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> // マクロ 繰り返し #define o_rep(o1, o2, o3, o4, name, ...) name #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) #define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) #define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) #define reps2(i, j, n) \ for (ll i = 0, reps2lim = n; i < reps2lim; ++i) \ for (ll j = 0; j < reps2lim; ++j) #define reps3(i, j, k, n) \ for (ll i = 0, reps3lim = n; i < reps3lim; ++i) \ for (ll j = 0; j < reps3lim; ++j) \ for (ll k = 0; k < reps3lim; ++k) #define reps4(i, j, k, l, n) \ for (ll i = 0, reps4lim = n; i < reps4lim; ++i) \ for (ll j = 0; j < reps4lim; ++j) \ for (ll k = 0; k < reps4lim; ++k) \ for (ll l = 0; l < reps4lim; ++l) #define o_reps(o1, o2, o3, o4, o5, name, ...) name #define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, rep2, )(__VA_ARGS__) #define repss(i, j, k, a, b, c) \ for (ll i = 0; i < a; ++i) \ for (ll j = 0; j < b; ++j) \ for (ll k = 0; k < c; ++k) #define repv(i, j, A) rep(i, sz(A)) rep(j, sz(A[0])) #define fora(a, b) for (auto &&a : b) // インデックスを前後含めて走査 #define fori(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) // vectorの中身を先頭から見る #define foriv(i, v, d) \ int i = 0; \ for (auto prev = d[sz(d) - 1], next = d[1], v = d[0]; i < sz(d); \ i++, prev = v, v = next, next = (i >= sz(d) - 1 ? d[0] : d[i + 1])) #define form(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it).fi < r; ++it) #define forit(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;) // マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const char infc = '{'; const string infs = "{"; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; // マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの // 埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T, typename U> ostream &operator<<(ostream &os, vector<pair<T, U>> &vec) { for (ll i = 0; i < vec.size(); ++i) { os << vec[i]; if (i != vec.size() - 1) os << endl; } return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // template<typename W, typename H> void resize(vector<W> &vec, const H head) { // vec.resize(head); } template<typename W, typename H, typename ... T> void // resize(vector<W> &vec, const H &head, const T ... tail) {vec.resize(head);for // (auto &v: vec)resize(v, tail...);} template <typename T, typename F> bool all_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool all_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (!all_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool any_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool any_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (any_of2(v[i], f)) return true; } return false; } template <typename T, typename F> bool none_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool none_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (none_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool find_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll find_if2(vector<T> &v, F f) { rep(i, sz(v)) { if (find_if2(v[i], f)) return i; } return sz(v); } template <typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll rfind_if2(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (rfind_if2(v[i], f)) return i; } return -1; } template <class T> bool contains(string &s, const T &v) { return s.find(v) != string::npos; } template <typename T> bool contains(vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template <typename T, typename F> bool contains_if2(vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template <typename T, typename F> ll count_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll count_if2(vector<T> &vec, F f) { ll ret = 0; fora(v, vec) ret += count_if2(v, f); return ret; } template <typename T, typename F> void for_each2(T &v, F f) { f(v); } template <typename T, typename F> void for_each2(vector<T> &vec, F f) { fora(v, vec) for_each2(v, f); } template <typename W> ll count_od(vector<W> &a) { return count_if2(a, [](ll v) { return v & 1; }); } template <typename W> ll count_ev(vector<W> &a) { return count_if2(a, [](ll v) { return !(v & 1); }); } // 削除された要素の数を返す template <typename T, typename F> int erase_if2(vector<T> &v, F f) { vector<T> nv; int cou = 0; rep(i, sz(v)) { if (f(v[i])) { cou++; } else { nv.push_back(v[i]); } } v = nv; return cou; } template <typename T, typename F> int erase_if2(vector<vector<T>> &v, F f) { int cou = 0; rep(i, sz(v)) { cou += erase_if2(v[i], f); } return cou; } #define all_of(a, right) all_of2(a, lam(right)) #define all_of_f(a, f) all_of2(a, f) #define any_of(a, right) any_of2(a, lam(right)) #define any_of_f(a, f) any_of2(a, f) #define none_of(a, right) none_of2(a, lam(right)) #define none_of_f(a, f) none_of2(a, f) #define find_if(a, right) find_if2(a, lam(right)) #define find_if_f(a, f) find_if2(a, f) #define rfind_if(a, right) rfind_if2(a, lam(right)) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if(a, right) contains_if2(a, lam(right)) #define contains_if_f(a, f) contains_if2(a, f) #define count_if(a, right) count_if2(a, lam(right)) #define count_if_f(a, f) count_if2(a, f) #define for_each(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each_f(a, f) \ do { \ fora(v, a) { f(v); } \ } while (0) #define erase_if(a, right) erase_if2(a, lam(right)) #define erase_if_f(a, f) erase_if2(a, f) template <class T, class U> void replace(vector<T> &a, T key, U v) { replace(a.begin(), a.end(), key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> // 例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) { rep(i, N) a[i] = v; } template <typename A, size_t N, size_t O, typename T> void fill(A (&a)[N][O], const T &v) { rep(i, N) rep(j, O) a[i][j] = v; } template <typename A, size_t N, size_t O, size_t P, typename T> void fill(A (&a)[N][O][P], const T &v) { rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T> void fill(A (&a)[N][O][P][Q], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, typename T> void fill(A (&a)[N][O][P][Q][R], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S, typename T> void fill(A (&a)[N][O][P][Q][R][S], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) a[i][j][k][l][m][n] = v; } template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define o_din(o1, o2, o3, o4, o5, o6, name, ...) name #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define o_dins(o1, o2, o3, o4, o5, o6, name, ...) name #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ o_dins(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define o_dind(o1, o2, o3, o4, name, ...) name #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #ifdef _DEBUG template <class T> void err2(T &&head) { cerr << head; } template <class T, class... U> void err2(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); } template <class T, class... U> void err(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); cerr << "" << endl; } template <class T> void err(T &&head) { cerr << head << endl; } void err() { cerr << "" << endl; } template <class T> string out_m2(vector<T> &a, ll W = inf) { stringstream ss; if (W == inf) W = min(sz(a), 12ll); if (sz(a) == 0) return ss.str(); rep(i, W) { ss << a[i] << " "; } return ss.str(); } template <class T> string out_m2(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1) ss << " *|"; else ss << " " << key << "|"; rep(w, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3) ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]), 12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } template <class T> string out_m2(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } template <class T> string out_m(vector<T> &a, ll W = inf) { stringstream ss; if (W == inf) W = min(sz(a), 12ll); if (sz(a) == 0) return ss.str(); rep(i, W) { ss << a[i] << " "; } ss << "" << endl; return ss.str(); } template <class T> string out_m(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1) ss << " *|"; else ss << " " << key << "|"; rep(w, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3) ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]), 12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } ss << endl; return ss.str(); } template <class T> string out_m(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 5; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m(a[i], W, U, i); } ss << endl; return ss.str(); } string out_m(int a) { stringstream ss; ss << a << endl; return ss.str(); } template <class T> string out_m(T &a) { stringstream ss; ss << a << endl; return ss.str(); } template <class T> void outv(vector<T> &a, ll W = inf) { cout << out_m(a, W) << endl; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) { cout << out_m(a, H, W, key) << endl; } template <class T> void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) { cout << out_m(a, H, W, U) << endl; } #else template <class T> void outv(vector<T> &a, ll W = inf) { rep(i, min(W, sz(a))) { cout << a[i] << " "; } cout << "" << endl; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) { rep(i, min(H, sz(a))) { outv(a[i], W); } } template <class T> void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) { ; } #define err(...) ; #endif template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } // テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--; } #define dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; // デバッグ #define sp << " " << #define deb1(x) debugName(x) << " = " << out_m2(x) #define deb2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__) #define deb3(x, ...) deb1(x) << ", " << deb2(__VA_ARGS__) #define deb4(x, ...) deb1(x) << ", " << deb3(__VA_ARGS__) #define deb5(x, ...) deb1(x) << ", " << deb4(__VA_ARGS__) #define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__) #define deb7(x, ...) deb1(x) << ", " << deb6(__VA_ARGS__) #define deb8(x, ...) deb1(x) << ", " << deb7(__VA_ARGS__) #define deb9(x, ...) deb1(x) << ", " << deb8(__VA_ARGS__) #define deb10(x, ...) deb1(x) << ", " << deb9(__VA_ARGS__) #define o_ebug(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name #ifdef _DEBUG #define deb(...) \ cerr << o_ebug(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb4, deb3, \ deb2, deb1)(__VA_ARGS__) \ << endl #else #define deb(...) ; #endif #define debugline(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n' //@formatter:off // よく使うクラス、構造体 struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; // 連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } // つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} // エラー void ole() { #ifdef _DEBUG debugline("ole"); exit(0); #endif string a = "a"; rep(i, 30) a += a; rep(i, 1 << 17) cout << a << endl; cout << "OLE 出力長制限超過" << endl; exit(0); } void re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } // 便利関数 // テスト用 char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } // 単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } // 便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } vp run_length(vi &a) { vp ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } vector<pair<char, ll>> run_length(string &a) { vector<pair<char, ll>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } template <class F> ll mgr(ll ok, ll ng, F f) { if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } else while (ok - ng > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } // エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template <class T> void rev(vector<T> &a) { reverse(all(a)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } // 変換系 //[v] := iとなるようなvectorを返す // 存在しない物は-1 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> T min(T a, signed b) { return a < b ? a : b; } template <class T> T max(T a, signed b) { return a < b ? b : a; } template <class T> T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret // += sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // imoは0-indexed // ruiは1-indexed template <class T> vector<T> imo(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } // kと同じものの数 template <class T, class U> vi imo(vector<T> &a, U k) { vector<T> ret = a; rep(i, sz(ret)) ret[i] = a[i] == k; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } template <class T> vector<T> imox(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i]; return ret; } // 漸化的に最小を持つ template <class T> vector<T> imi(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chmi(ret[i - 1], ret[i]); return ret; } template <class T> vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chma(ret[i - 1], ret[i]); return ret; } template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } }; template <class T> struct rruic { const T *rrui; rruic(T *ru) : rrui(ru) {} //[r , l) : [3 , -1)等 T operator()(ll r, ll l) { assert(r >= l); return rrui[l] - rrui[r]; } T operator()(int l) { return rrui[l]; } T operator[](ll i) { return rrui[i - 1] - rrui[i]; } }; template <class T> ostream &operator<<(ostream &os, ruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } template <class T> vector<T> ruiv(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i]; return ret; } template <class T> ruiC<T> ruic() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return ret; } vector<ll> ruiv(string &a) { if (sz(a) == 0) return vi(1); ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0; vector<ll> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i] - dec; return ret; } ruiC<ll> ruic(string &a) { vector<ll> ret = ruiv(a); return ruiC<ll>(ret); } // kと同じものの数 template <class T, class U> vi ruiv(T &a, U k) { vi ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k); return ret; } template <class T, class U> ruiC<ll> ruic(T &a, U k) { vi ret = ruiv(a, k); return ruiC<ll>(ret); } // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { const vector<T> rui; ruixC(vector<T> &ru) : rui(ru) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruiXc "; deb(l, r); assert(0); } return rui[r] ^ rui[l]; } T operator[](ll i) { return rui[i]; } T back() { return rui.back(); } ll size() { return rui.size(); } }; template <class T> vector<T> ruix(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i]; return ret; } template <class T> ruixC<ll> ruixc(vector<T> &a) { vi ret = ruix(a); return ruixC<ll>(ret); } template <class T> vector<T> ruim(vector<T> &a) { vector<T> res(a.size() + 1, 1); rep(i, a.size()) res[i + 1] = res[i] * a[i]; return res; } // 漸化的に最小を1indexで持つ template <class T> vector<T> ruimi(vector<T> &a) { ll n = sz(a); vector<T> ret(n + 1); rep(i, 1, n) { ret[i] = a[i - 1]; chmi(ret[i + 1], ret[i]); } return ret; } // template<class T> T *rrui(vector<T> &a) { // 右から左にかけての半開区間 (-1 n-1] template <class T> rruic<T> rrui(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; rer(i, len - 1) res[i - 1] = res[i] + a[i]; return rruic<T>(res); } // 掛け算 template <class T> T *rruim(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; res[len - 1] = 1; rer(i, len - 1) res[i - 1] = res[i] * a[i]; return res; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T> void dec(vector<T> &a) { for (auto &u : a) dec(u, 1); } template <class T, class U> void dec(vector<T> &a, vector<U> &b) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(const vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() // 添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); // オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); // オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす ll bit(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } #define bcou __builtin_popcountll // 最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } // 最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } // 初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } // 基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } // 要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define lowerBound(a, v) (*lower_bound(all(a), v)) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define upperBound(a, v) (*upper_bound(all(a), v)) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rlowerBound(a, v) *(--(upper_bound(all(a), v))) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) #define rupperBound(a, v) *(--(lower_bound(all(a), v))) #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) // 狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } // 存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T> void fin(T s) { cout << s << endl, exit(0); } // 便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } ll mod(ll a, ll m) { return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_rem__kr(ll a, ll r, ll v) { if (r >= v) return r; return (v - r + a - 1) / a * a + r; } // 第何項か ll lower_rem_i__kr(ll a, ll r, ll v) { if (r >= v) return 0; return (v - r + a - 1) / a; } ll upper_rem__kr(ll a, ll r, ll v) { return lower_rem__kr(a, r, v + 1); } ll upper_rem_i__kr(ll a, ll r, ll v) { return lower_rem_i__kr(a, r, v + 1); } // v * v >= aとなる最小のvを返す ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll)std::sqrt(a); while (res * res < a) ++res; return res; } double log(double e, double x) { return log(x) / log(e); } ll sig(ll t) { return ((1 + t) * t) >> 1; } ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } // 機能拡張 template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } void yn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; } void Yn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; } void YN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } void fyn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; exit(0); } void fYn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; exit(0); } void fYN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; exit(0); } void Possible(bool a) { if (a) cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { if (a) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } template <typename T> class fixed_point : T { public: explicit constexpr fixed_point(T &&t) noexcept : T(std::forward<T>(t)) {} template <typename... Args> constexpr decltype(auto) operator()(Args &&...args) const { return T::operator()(*this, std::forward<Args>(args)...); } }; template <typename T> static inline constexpr decltype(auto) fix(T &&t) noexcept { return fixed_point<T>{std::forward<T>(t)}; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; //@formatter:on // gra mll pr // 上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] // 右、上が正 constexpr ll y4[] = {1, -1, 0, 0}; constexpr ll x4[] = {0, 0, -1, 1}; constexpr ll y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; vvc(ba); ll N, M, H, W; vi A, B, C; // 継承と一から核のどっちが早いか void solve() { in(H, W); vvi(A); vvi(B); nt(A, H, W); nt(B, H, W); int lim = 13000; vv(mvec<int>) dp; resize(dp, H, W, lim); dp[0][0][(A[0][0] - B[0][0])] = true; dp[0][0][(B[0][0] - A[0][0])] = true; rep(h, H) { rep(w, W) { rep(s, -lim + 1, lim) { if (dp[h][w][s] == 0) con; if (h + 1 < H) { int a = A[h + 1][w], b = B[h + 1][w]; dp[h + 1][w][(s + a - b)] = true; dp[h + 1][w][(s + b - a)] = true; } if (w + 1 < W) { int a = A[h][w + 1], b = B[h][w + 1]; dp[h][w + 1][(s + a - b)] = true; dp[h][w + 1][(s + b - a)] = true; } } } } int best = linf; rep(s, -lim + 1, lim) { if (dp[H - 1][W - 1][s]) { chmi(best, abs(s)); } } cout << best << endl; ; } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
// #undef _DEBUG // #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long #define ll long long auto start_time = system_clock::now(); #define debugName(VariableName) #VariableName /*@formatter:off*/ template <class T> struct mvec { vector<T> v; int n; //+-n mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} T &operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #ifdef _DEBUG template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[0]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef _DEBUG // 区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapip unordered_map<ll, P> #else // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> #endif struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } // 衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 */ } bool operator==(const T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } //@マクロ省略系 型,構造 #define double long double #define pow powl #define ull unsigned long long using dou = double; using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using mvp = mvec<P>; using mvt = mvec<T>; #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define wh while #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](auto &p) { return p right; } // マクロ省略系 コンテナ using vi = vector<ll>; using vb = vector<bool>; using vs = vector<string>; using vd = vector<double>; using vc = vector<char>; using vp = vector<P>; using vt = vector<T>; #define V vector #define o_vvt(o1, o2, o3, o4, name, ...) name #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vvi(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) #define vvt(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__) #define vv(type, ...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__) template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__) #define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__) #define vns(name, ...) auto name = make_v<string>(__VA_ARGS__) #define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__) #define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__) #define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__) #define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> // マクロ 繰り返し #define o_rep(o1, o2, o3, o4, name, ...) name #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) #define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) #define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) #define reps2(i, j, n) \ for (ll i = 0, reps2lim = n; i < reps2lim; ++i) \ for (ll j = 0; j < reps2lim; ++j) #define reps3(i, j, k, n) \ for (ll i = 0, reps3lim = n; i < reps3lim; ++i) \ for (ll j = 0; j < reps3lim; ++j) \ for (ll k = 0; k < reps3lim; ++k) #define reps4(i, j, k, l, n) \ for (ll i = 0, reps4lim = n; i < reps4lim; ++i) \ for (ll j = 0; j < reps4lim; ++j) \ for (ll k = 0; k < reps4lim; ++k) \ for (ll l = 0; l < reps4lim; ++l) #define o_reps(o1, o2, o3, o4, o5, name, ...) name #define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, rep2, )(__VA_ARGS__) #define repss(i, j, k, a, b, c) \ for (ll i = 0; i < a; ++i) \ for (ll j = 0; j < b; ++j) \ for (ll k = 0; k < c; ++k) #define repv(i, j, A) rep(i, sz(A)) rep(j, sz(A[0])) #define fora(a, b) for (auto &&a : b) // インデックスを前後含めて走査 #define fori(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) // vectorの中身を先頭から見る #define foriv(i, v, d) \ int i = 0; \ for (auto prev = d[sz(d) - 1], next = d[1], v = d[0]; i < sz(d); \ i++, prev = v, v = next, next = (i >= sz(d) - 1 ? d[0] : d[i + 1])) #define form(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it).fi < r; ++it) #define forit(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;) // マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const char infc = '{'; const string infs = "{"; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; // マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの // 埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T, typename U> ostream &operator<<(ostream &os, vector<pair<T, U>> &vec) { for (ll i = 0; i < vec.size(); ++i) { os << vec[i]; if (i != vec.size() - 1) os << endl; } return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // template<typename W, typename H> void resize(vector<W> &vec, const H head) { // vec.resize(head); } template<typename W, typename H, typename ... T> void // resize(vector<W> &vec, const H &head, const T ... tail) {vec.resize(head);for // (auto &v: vec)resize(v, tail...);} template <typename T, typename F> bool all_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool all_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (!all_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool any_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool any_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (any_of2(v[i], f)) return true; } return false; } template <typename T, typename F> bool none_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool none_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (none_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool find_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll find_if2(vector<T> &v, F f) { rep(i, sz(v)) { if (find_if2(v[i], f)) return i; } return sz(v); } template <typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll rfind_if2(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (rfind_if2(v[i], f)) return i; } return -1; } template <class T> bool contains(string &s, const T &v) { return s.find(v) != string::npos; } template <typename T> bool contains(vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template <typename T, typename F> bool contains_if2(vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template <typename T, typename F> ll count_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll count_if2(vector<T> &vec, F f) { ll ret = 0; fora(v, vec) ret += count_if2(v, f); return ret; } template <typename T, typename F> void for_each2(T &v, F f) { f(v); } template <typename T, typename F> void for_each2(vector<T> &vec, F f) { fora(v, vec) for_each2(v, f); } template <typename W> ll count_od(vector<W> &a) { return count_if2(a, [](ll v) { return v & 1; }); } template <typename W> ll count_ev(vector<W> &a) { return count_if2(a, [](ll v) { return !(v & 1); }); } // 削除された要素の数を返す template <typename T, typename F> int erase_if2(vector<T> &v, F f) { vector<T> nv; int cou = 0; rep(i, sz(v)) { if (f(v[i])) { cou++; } else { nv.push_back(v[i]); } } v = nv; return cou; } template <typename T, typename F> int erase_if2(vector<vector<T>> &v, F f) { int cou = 0; rep(i, sz(v)) { cou += erase_if2(v[i], f); } return cou; } #define all_of(a, right) all_of2(a, lam(right)) #define all_of_f(a, f) all_of2(a, f) #define any_of(a, right) any_of2(a, lam(right)) #define any_of_f(a, f) any_of2(a, f) #define none_of(a, right) none_of2(a, lam(right)) #define none_of_f(a, f) none_of2(a, f) #define find_if(a, right) find_if2(a, lam(right)) #define find_if_f(a, f) find_if2(a, f) #define rfind_if(a, right) rfind_if2(a, lam(right)) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if(a, right) contains_if2(a, lam(right)) #define contains_if_f(a, f) contains_if2(a, f) #define count_if(a, right) count_if2(a, lam(right)) #define count_if_f(a, f) count_if2(a, f) #define for_each(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each_f(a, f) \ do { \ fora(v, a) { f(v); } \ } while (0) #define erase_if(a, right) erase_if2(a, lam(right)) #define erase_if_f(a, f) erase_if2(a, f) template <class T, class U> void replace(vector<T> &a, T key, U v) { replace(a.begin(), a.end(), key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> // 例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) { rep(i, N) a[i] = v; } template <typename A, size_t N, size_t O, typename T> void fill(A (&a)[N][O], const T &v) { rep(i, N) rep(j, O) a[i][j] = v; } template <typename A, size_t N, size_t O, size_t P, typename T> void fill(A (&a)[N][O][P], const T &v) { rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T> void fill(A (&a)[N][O][P][Q], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, typename T> void fill(A (&a)[N][O][P][Q][R], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S, typename T> void fill(A (&a)[N][O][P][Q][R][S], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) a[i][j][k][l][m][n] = v; } template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define o_din(o1, o2, o3, o4, o5, o6, name, ...) name #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define o_dins(o1, o2, o3, o4, o5, o6, name, ...) name #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ o_dins(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define o_dind(o1, o2, o3, o4, name, ...) name #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #ifdef _DEBUG template <class T> void err2(T &&head) { cerr << head; } template <class T, class... U> void err2(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); } template <class T, class... U> void err(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); cerr << "" << endl; } template <class T> void err(T &&head) { cerr << head << endl; } void err() { cerr << "" << endl; } template <class T> string out_m2(vector<T> &a, ll W = inf) { stringstream ss; if (W == inf) W = min(sz(a), 12ll); if (sz(a) == 0) return ss.str(); rep(i, W) { ss << a[i] << " "; } return ss.str(); } template <class T> string out_m2(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1) ss << " *|"; else ss << " " << key << "|"; rep(w, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3) ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]), 12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } template <class T> string out_m2(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } template <class T> string out_m(vector<T> &a, ll W = inf) { stringstream ss; if (W == inf) W = min(sz(a), 12ll); if (sz(a) == 0) return ss.str(); rep(i, W) { ss << a[i] << " "; } ss << "" << endl; return ss.str(); } template <class T> string out_m(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1) ss << " *|"; else ss << " " << key << "|"; rep(w, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3) ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]), 12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } ss << endl; return ss.str(); } template <class T> string out_m(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 5; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m(a[i], W, U, i); } ss << endl; return ss.str(); } string out_m(int a) { stringstream ss; ss << a << endl; return ss.str(); } template <class T> string out_m(T &a) { stringstream ss; ss << a << endl; return ss.str(); } template <class T> void outv(vector<T> &a, ll W = inf) { cout << out_m(a, W) << endl; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) { cout << out_m(a, H, W, key) << endl; } template <class T> void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) { cout << out_m(a, H, W, U) << endl; } #else template <class T> void outv(vector<T> &a, ll W = inf) { rep(i, min(W, sz(a))) { cout << a[i] << " "; } cout << "" << endl; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) { rep(i, min(H, sz(a))) { outv(a[i], W); } } template <class T> void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) { ; } #define err(...) ; #endif template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } // テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--; } #define dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; // デバッグ #define sp << " " << #define deb1(x) debugName(x) << " = " << out_m2(x) #define deb2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__) #define deb3(x, ...) deb1(x) << ", " << deb2(__VA_ARGS__) #define deb4(x, ...) deb1(x) << ", " << deb3(__VA_ARGS__) #define deb5(x, ...) deb1(x) << ", " << deb4(__VA_ARGS__) #define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__) #define deb7(x, ...) deb1(x) << ", " << deb6(__VA_ARGS__) #define deb8(x, ...) deb1(x) << ", " << deb7(__VA_ARGS__) #define deb9(x, ...) deb1(x) << ", " << deb8(__VA_ARGS__) #define deb10(x, ...) deb1(x) << ", " << deb9(__VA_ARGS__) #define o_ebug(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name #ifdef _DEBUG #define deb(...) \ cerr << o_ebug(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb4, deb3, \ deb2, deb1)(__VA_ARGS__) \ << endl #else #define deb(...) ; #endif #define debugline(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n' //@formatter:off // よく使うクラス、構造体 struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; // 連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } // つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} // エラー void ole() { #ifdef _DEBUG debugline("ole"); exit(0); #endif string a = "a"; rep(i, 30) a += a; rep(i, 1 << 17) cout << a << endl; cout << "OLE 出力長制限超過" << endl; exit(0); } void re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } // 便利関数 // テスト用 char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } // 単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } // 便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } vp run_length(vi &a) { vp ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } vector<pair<char, ll>> run_length(string &a) { vector<pair<char, ll>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } template <class F> ll mgr(ll ok, ll ng, F f) { if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } else while (ok - ng > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } // エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template <class T> void rev(vector<T> &a) { reverse(all(a)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } // 変換系 //[v] := iとなるようなvectorを返す // 存在しない物は-1 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> T min(T a, signed b) { return a < b ? a : b; } template <class T> T max(T a, signed b) { return a < b ? b : a; } template <class T> T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret // += sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // imoは0-indexed // ruiは1-indexed template <class T> vector<T> imo(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } // kと同じものの数 template <class T, class U> vi imo(vector<T> &a, U k) { vector<T> ret = a; rep(i, sz(ret)) ret[i] = a[i] == k; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } template <class T> vector<T> imox(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i]; return ret; } // 漸化的に最小を持つ template <class T> vector<T> imi(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chmi(ret[i - 1], ret[i]); return ret; } template <class T> vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chma(ret[i - 1], ret[i]); return ret; } template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } }; template <class T> struct rruic { const T *rrui; rruic(T *ru) : rrui(ru) {} //[r , l) : [3 , -1)等 T operator()(ll r, ll l) { assert(r >= l); return rrui[l] - rrui[r]; } T operator()(int l) { return rrui[l]; } T operator[](ll i) { return rrui[i - 1] - rrui[i]; } }; template <class T> ostream &operator<<(ostream &os, ruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } template <class T> vector<T> ruiv(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i]; return ret; } template <class T> ruiC<T> ruic() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return ret; } vector<ll> ruiv(string &a) { if (sz(a) == 0) return vi(1); ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0; vector<ll> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i] - dec; return ret; } ruiC<ll> ruic(string &a) { vector<ll> ret = ruiv(a); return ruiC<ll>(ret); } // kと同じものの数 template <class T, class U> vi ruiv(T &a, U k) { vi ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k); return ret; } template <class T, class U> ruiC<ll> ruic(T &a, U k) { vi ret = ruiv(a, k); return ruiC<ll>(ret); } // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { const vector<T> rui; ruixC(vector<T> &ru) : rui(ru) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruiXc "; deb(l, r); assert(0); } return rui[r] ^ rui[l]; } T operator[](ll i) { return rui[i]; } T back() { return rui.back(); } ll size() { return rui.size(); } }; template <class T> vector<T> ruix(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i]; return ret; } template <class T> ruixC<ll> ruixc(vector<T> &a) { vi ret = ruix(a); return ruixC<ll>(ret); } template <class T> vector<T> ruim(vector<T> &a) { vector<T> res(a.size() + 1, 1); rep(i, a.size()) res[i + 1] = res[i] * a[i]; return res; } // 漸化的に最小を1indexで持つ template <class T> vector<T> ruimi(vector<T> &a) { ll n = sz(a); vector<T> ret(n + 1); rep(i, 1, n) { ret[i] = a[i - 1]; chmi(ret[i + 1], ret[i]); } return ret; } // template<class T> T *rrui(vector<T> &a) { // 右から左にかけての半開区間 (-1 n-1] template <class T> rruic<T> rrui(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; rer(i, len - 1) res[i - 1] = res[i] + a[i]; return rruic<T>(res); } // 掛け算 template <class T> T *rruim(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; res[len - 1] = 1; rer(i, len - 1) res[i - 1] = res[i] * a[i]; return res; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T> void dec(vector<T> &a) { for (auto &u : a) dec(u, 1); } template <class T, class U> void dec(vector<T> &a, vector<U> &b) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(const vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() // 添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); // オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); // オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす ll bit(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } #define bcou __builtin_popcountll // 最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } // 最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } // 初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } // 基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } // 要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define lowerBound(a, v) (*lower_bound(all(a), v)) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define upperBound(a, v) (*upper_bound(all(a), v)) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rlowerBound(a, v) *(--(upper_bound(all(a), v))) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) #define rupperBound(a, v) *(--(lower_bound(all(a), v))) #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) // 狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } // 存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T> void fin(T s) { cout << s << endl, exit(0); } // 便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } ll mod(ll a, ll m) { return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_rem__kr(ll a, ll r, ll v) { if (r >= v) return r; return (v - r + a - 1) / a * a + r; } // 第何項か ll lower_rem_i__kr(ll a, ll r, ll v) { if (r >= v) return 0; return (v - r + a - 1) / a; } ll upper_rem__kr(ll a, ll r, ll v) { return lower_rem__kr(a, r, v + 1); } ll upper_rem_i__kr(ll a, ll r, ll v) { return lower_rem_i__kr(a, r, v + 1); } // v * v >= aとなる最小のvを返す ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll)std::sqrt(a); while (res * res < a) ++res; return res; } double log(double e, double x) { return log(x) / log(e); } ll sig(ll t) { return ((1 + t) * t) >> 1; } ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } // 機能拡張 template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } void yn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; } void Yn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; } void YN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } void fyn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; exit(0); } void fYn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; exit(0); } void fYN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; exit(0); } void Possible(bool a) { if (a) cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { if (a) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } template <typename T> class fixed_point : T { public: explicit constexpr fixed_point(T &&t) noexcept : T(std::forward<T>(t)) {} template <typename... Args> constexpr decltype(auto) operator()(Args &&...args) const { return T::operator()(*this, std::forward<Args>(args)...); } }; template <typename T> static inline constexpr decltype(auto) fix(T &&t) noexcept { return fixed_point<T>{std::forward<T>(t)}; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; //@formatter:on // gra mll pr // 上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] // 右、上が正 constexpr ll y4[] = {1, -1, 0, 0}; constexpr ll x4[] = {0, 0, -1, 1}; constexpr ll y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; vvc(ba); ll N, M, H, W; vi A, B, C; // 継承と一から核のどっちが早いか void solve() { in(H, W); vvi(A); vvi(B); nt(A, H, W); nt(B, H, W); int lim = 13000; vv(mvec<signed>) dp; resize(dp, H, W, lim); dp[0][0][(A[0][0] - B[0][0])] = true; dp[0][0][(B[0][0] - A[0][0])] = true; rep(h, H) { rep(w, W) { rep(s, -lim + 1, lim) { if (dp[h][w][s] == 0) con; if (h + 1 < H) { int a = A[h + 1][w], b = B[h + 1][w]; dp[h + 1][w][(s + a - b)] = true; dp[h + 1][w][(s + b - a)] = true; } if (w + 1 < W) { int a = A[h][w + 1], b = B[h][w + 1]; dp[h][w + 1][(s + a - b)] = true; dp[h][w + 1][(s + b - a)] = true; } } } } int best = linf; rep(s, -lim + 1, lim) { if (dp[H - 1][W - 1][s]) { chmi(best, abs(s)); } } cout << best << endl; ; } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
replace
3,924
3,925
3,924
3,925
MLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define ff first #define ss second #define mp make_pair #define pp pair<ll, ll> #define pq_min priority_queue<ll, vector<ll>, greater<ll>> #define pq_max priority_queue<ll> #define forx(i, a, b) for (ll i = 0; i < a; i++) #define ld long double const ll mod = 1e9 + 7; const ll N = 15; ll power(ll a, ll b) { a = a % mod; ll ans = 1; while (b > 0) { if (b % 2 == 1) { ans = (ans * a) % mod; } b = b / 2; a = (a * a) % mod; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); srand(time(NULL)); ll n, m; cin >> n >> m; ll a[n][m]; ll b[n][m]; for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) { cin >> a[i][j]; } } for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) { cin >> b[i][j]; } } ll ans[n][m][13000]; memset(ans, 0, sizeof(ans)); ll z = abs(a[0][0] - b[0][0]); ans[0][0][z] = 1; for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) { ll f = abs(a[i][j] - b[i][j]); if (i > 0) { for (ll k = 0; k < 13000; k++) { if (ans[i - 1][j][k] == 1) { ans[i][j][k + f] = 1; ans[i][j][abs(k - f)] = 1; } } } if (j > 0) { for (ll k = 0; k < 13000; k++) { if (ans[i][j - 1][k] == 1) { ans[i][j][k + f] = 1; ans[i][j][abs(k - f)] = 1; } } } } } for (ll i = 0; i < 13000; i++) { if (ans[n - 1][m - 1][i] == 1) { cout << i << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define ff first #define ss second #define mp make_pair #define pp pair<ll, ll> #define pq_min priority_queue<ll, vector<ll>, greater<ll>> #define pq_max priority_queue<ll> #define forx(i, a, b) for (ll i = 0; i < a; i++) #define ld long double const ll mod = 1e9 + 7; const ll N = 15; ll power(ll a, ll b) { a = a % mod; ll ans = 1; while (b > 0) { if (b % 2 == 1) { ans = (ans * a) % mod; } b = b / 2; a = (a * a) % mod; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); srand(time(NULL)); ll n, m; cin >> n >> m; ll a[n][m]; ll b[n][m]; for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) { cin >> a[i][j]; } } for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) { cin >> b[i][j]; } } bool ans[n][m][13000]; memset(ans, 0, sizeof(ans)); ll z = abs(a[0][0] - b[0][0]); ans[0][0][z] = 1; for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) { ll f = abs(a[i][j] - b[i][j]); if (i > 0) { for (ll k = 0; k < 13000; k++) { if (ans[i - 1][j][k] == 1) { ans[i][j][k + f] = 1; ans[i][j][abs(k - f)] = 1; } } } if (j > 0) { for (ll k = 0; k < 13000; k++) { if (ans[i][j - 1][k] == 1) { ans[i][j][k + f] = 1; ans[i][j][abs(k - f)] = 1; } } } } } for (ll i = 0; i < 13000; i++) { if (ans[n - 1][m - 1][i] == 1) { cout << i << endl; return 0; } } }
replace
47
48
47
48
0
p02839
Python
Runtime Error
H, W = map(int, input().split()) M = (H + W) * 80 A = [tuple(map(int, input().split())) for _ in range(H)] B = [tuple(map(int, input().split())) for _ in range(H)] dp = [[0] * W for _ in range(H)] dp[0][0] = 1 << (M - abs(A[0][0] - B[0][0])) for h in range(H): for w, (a, b) in enumerate(zip(A[h], B[h])): d = abs(a - b) if d == 0: continue mask = 0 if h > 0: mask |= dp[h - 1][w] << d mask |= dp[h - 1][w] >> d if w > 0: mask |= dp[h][w - 1] << d mask |= dp[h][w - 1] >> d dp[h][w] |= mask state = dp[-1][-1] nums = [] for digit, b in enumerate(bin(state)[2:][::-1]): if b == "1": nums.append(digit - M) ans = min(abs(n) for n in nums) print(ans)
H, W = map(int, input().split()) M = (H + W) * 80 A = [tuple(map(int, input().split())) for _ in range(H)] B = [tuple(map(int, input().split())) for _ in range(H)] dp = [[0] * W for _ in range(H)] dp[0][0] = 1 << (M - abs(A[0][0] - B[0][0])) for h in range(H): for w, (a, b) in enumerate(zip(A[h], B[h])): d = abs(a - b) mask = 0 if h > 0: mask |= dp[h - 1][w] << d mask |= dp[h - 1][w] >> d if w > 0: mask |= dp[h][w - 1] << d mask |= dp[h][w - 1] >> d dp[h][w] |= mask state = dp[-1][-1] nums = [] for digit, b in enumerate(bin(state)[2:][::-1]): if b == "1": nums.append(digit - M) ans = min(abs(n) for n in nums) print(ans)
delete
12
14
12
12
0
p02839
Python
Time Limit Exceeded
H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] B = [list(map(int, input().split())) for _ in range(H)] Z = sum(sum(a) for a in A) + sum(sum(b) for b in B) + 10 dp = [[0] * (W + 1) for _ in range(H + 1)] dp[0][0] = 1 << Z for h in range(H): for w in range(W): d = dp[h][w] k = abs(A[h][w] - B[h][w]) dp[h + 1][w] |= (d << k) | (d >> k) dp[h][w + 1] |= (d << k) | (d >> k) ans = 10**18 for a in (dp[H][W - 1], dp[H - 1][W]): for d in range(Z * 2 + 10): if a & (1 << d) != 0: ans = min(ans, abs(d - Z)) print(ans)
H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] B = [list(map(int, input().split())) for _ in range(H)] Z = 1000 dp = [[0] * (W + 1) for _ in range(H + 1)] dp[0][0] = 1 << Z for h in range(H): for w in range(W): d = dp[h][w] k = abs(A[h][w] - B[h][w]) dp[h + 1][w] |= (d << k) | (d >> k) dp[h][w + 1] |= (d << k) | (d >> k) ans = 10**18 for a in (dp[H][W - 1], dp[H - 1][W]): for d in range(Z * 2 + 10): if a & (1 << d) != 0: ans = min(ans, abs(d - Z)) print(ans)
replace
4
5
4
5
TLE
p02839
Python
Time Limit Exceeded
def main(): B_MAX = (80 + 80 - 1) * 80 H, W = list(map(int, input().split(" "))) A = [list(map(int, input().split(" "))) for _ in range(H)] B = [list(map(int, input().split(" "))) for _ in range(H)] dp = [[[0 for _ in range(B_MAX + 1)] for _ in range(W)] for _ in range(H)] dp[0][0][abs(A[0][0] - B[0][0])] = 1 max_b = abs(A[0][0] - B[0][0]) for h in range(H): for w in range(W): if h == 0 and w == 0: continue d = abs(A[h][w] - B[h][w]) max_b += d for b in range(min(B_MAX, max_b) + 1): v = 0 if h > 0: v = max(v, dp[h - 1][w][abs(b - d)]) if b + d <= B_MAX: v = max(v, dp[h - 1][w][b + d]) if w > 0: v = max(v, dp[h][w - 1][abs(b - d)]) if b + d <= B_MAX: v = max(v, dp[h][w - 1][b + d]) dp[h][w][b] = v ans = 0 while dp[H - 1][W - 1][ans] == 0: ans += 1 print(ans) if __name__ == "__main__": main()
def main(): B_MAX = (80 + 80 - 1) * 80 // 2 H, W = list(map(int, input().split(" "))) A = [list(map(int, input().split(" "))) for _ in range(H)] B = [list(map(int, input().split(" "))) for _ in range(H)] dp = [[[0 for _ in range(B_MAX + 1)] for _ in range(W)] for _ in range(H)] dp[0][0][abs(A[0][0] - B[0][0])] = 1 max_b = abs(A[0][0] - B[0][0]) for h in range(H): for w in range(W): if h == 0 and w == 0: continue d = abs(A[h][w] - B[h][w]) max_b += d for b in range(min(B_MAX, max_b) + 1): v = 0 if h > 0: v = max(v, dp[h - 1][w][abs(b - d)]) if b + d <= B_MAX: v = max(v, dp[h - 1][w][b + d]) if w > 0: v = max(v, dp[h][w - 1][abs(b - d)]) if b + d <= B_MAX: v = max(v, dp[h][w - 1][b + d]) dp[h][w][b] = v ans = 0 while dp[H - 1][W - 1][ans] == 0: ans += 1 print(ans) if __name__ == "__main__": main()
replace
1
2
1
2
TLE
p02839
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <string> #include <vector> // #include<cmath> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int abs(int x) { if (x > 0) return x; else return -x; } int dp[80][80][12801]; int main() { int h, w; cin >> h >> w; // h = 80; // w = 80; int a[h][w], b[h][w]; rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) cin >> b[i][j]; // rep(i, h)rep(j,w) a[i][j] = 80; // rep(i, h)rep(j,w) b[i][j] = 80; int mx = 1280001; rep(i, mx + 1) { if (abs(a[0][0] - b[0][0]) == i) { dp[0][0][i] = 1; } else { dp[0][0][i] = 0; } } rep(i, h) rep(j, w) rep(k, mx + 1) { int delta1 = abs(k - (a[i][j] - b[i][j])); int delta2 = abs(k + (a[i][j] - b[i][j])); if (delta1 > mx) { delta1 = delta2; } if (delta2 > mx) { delta2 = delta1; } if (i > 0 && j > 0) { if (dp[i - 1][j][delta1] == 1 || dp[i][j - 1][delta1] == 1 || dp[i - 1][j][delta2] == 1 || dp[i][j - 1][delta2] == 1) { dp[i][j][k] = 1; } else { dp[i][j][k] = 0; } } else if (i > 0 && j == 0) { if (dp[i - 1][j][delta1] == 1 || dp[i - 1][j][delta2] == 1) { dp[i][j][k] = 1; } else { dp[i][j][k] = 0; } } else if (i == 0 && j > 0) { if (dp[i][j - 1][delta1] == 1 || dp[i][j - 1][delta2] == 1) { dp[i][j][k] = 1; } else { dp[i][j][k] = 0; } } } rep(i, mx + 1) { if (dp[h - 1][w - 1][i] == 1) { cout << i << endl; return 0; } } }
#include <algorithm> #include <iostream> #include <string> #include <vector> // #include<cmath> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int abs(int x) { if (x > 0) return x; else return -x; } int dp[80][80][12801]; int main() { int h, w; cin >> h >> w; // h = 80; // w = 80; int a[h][w], b[h][w]; rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) cin >> b[i][j]; // rep(i, h)rep(j,w) a[i][j] = 80; // rep(i, h)rep(j,w) b[i][j] = 80; int mx = (h + w) * 80; rep(i, mx + 1) { if (abs(a[0][0] - b[0][0]) == i) { dp[0][0][i] = 1; } else { dp[0][0][i] = 0; } } rep(i, h) rep(j, w) rep(k, mx + 1) { int delta1 = abs(k - (a[i][j] - b[i][j])); int delta2 = abs(k + (a[i][j] - b[i][j])); if (delta1 > mx) { delta1 = delta2; } if (delta2 > mx) { delta2 = delta1; } if (i > 0 && j > 0) { if (dp[i - 1][j][delta1] == 1 || dp[i][j - 1][delta1] == 1 || dp[i - 1][j][delta2] == 1 || dp[i][j - 1][delta2] == 1) { dp[i][j][k] = 1; } else { dp[i][j][k] = 0; } } else if (i > 0 && j == 0) { if (dp[i - 1][j][delta1] == 1 || dp[i - 1][j][delta2] == 1) { dp[i][j][k] = 1; } else { dp[i][j][k] = 0; } } else if (i == 0 && j > 0) { if (dp[i][j - 1][delta1] == 1 || dp[i][j - 1][delta2] == 1) { dp[i][j][k] = 1; } else { dp[i][j][k] = 0; } } } rep(i, mx + 1) { if (dp[h - 1][w - 1][i] == 1) { cout << i << endl; return 0; } } }
replace
28
29
28
29
TLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAX = 1280; int main() { int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)); auto B = A; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> B[i][j]; A[i][j] = abs(A[i][j] - B[i][j]); } } vector<vector<vector<bool>>> dp( H, vector<vector<bool>>(W, vector<bool>(MAX + 1))); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (i == 0 && j == 0) { dp[i][j][A[i][j]] = true; continue; } if (0 <= i - 1) { for (int k = 0; k <= MAX; k++) if (dp[i - 1][j][k]) { dp[i][j][(k + A[i][j])] = true; dp[i][j][abs(k - A[i][j])] = true; } } if (0 <= j - 1) { for (int k = 0; k <= MAX; k++) if (dp[i][j - 1][k]) { dp[i][j][(k + A[i][j])] = true; dp[i][j][abs(k - A[i][j])] = true; } } } } int res = INT_MAX; for (int i = 0; i <= MAX; i++) if (dp[H - 1][W - 1][i]) { res = i; break; } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 12800; int main() { int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)); auto B = A; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> B[i][j]; A[i][j] = abs(A[i][j] - B[i][j]); } } vector<vector<vector<bool>>> dp( H, vector<vector<bool>>(W, vector<bool>(MAX + 1))); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (i == 0 && j == 0) { dp[i][j][A[i][j]] = true; continue; } if (0 <= i - 1) { for (int k = 0; k <= MAX; k++) if (dp[i - 1][j][k]) { dp[i][j][(k + A[i][j])] = true; dp[i][j][abs(k - A[i][j])] = true; } } if (0 <= j - 1) { for (int k = 0; k <= MAX; k++) if (dp[i][j - 1][k]) { dp[i][j][(k + A[i][j])] = true; dp[i][j][abs(k - A[i][j])] = true; } } } } int res = INT_MAX; for (int i = 0; i <= MAX; i++) if (dp[H - 1][W - 1][i]) { res = i; break; } cout << res << endl; return 0; }
replace
3
4
3
4
0
p02839
C++
Runtime Error
// https://atcoder.jp/contests/abc147/tasks/abc147_e #include "algorithm" #include "bitset" #include "cmath" #include "functional" #include "iomanip" #include "iostream" #include "map" #include "numeric" #include "queue" #include "set" #include "string" #include "vector" #define rep(i, to) for (ll i = 0; i < (to); ++i) #define rep1(i, to) for (ll i = 1; i <= (to); ++i) #define repf(i, from, to) for (ll i = from; i < (to); ++i) #define repr(i, from) for (ll i = from - 1; i >= 0; --i) #define all(vec) vec.begin(), vec.end() #define fi first #define se second using namespace std; typedef long long ll; template <typename T> using V = vector<T>; using VL = V<ll>; using VVL = V<VL>; template <typename T, typename U> using P = pair<T, U>; using PL = P<ll, ll>; using VPL = V<PL>; template <typename T> inline bool chmax(T &a, T b); template <typename T> inline bool chmin(T &a, T b); void print_ints(vector<ll> v); template <typename T> void drop(T a); const ll INF = 1e18; void solve() { ll H, W; cin >> H >> W; VVL as(H, VL(W)); rep(h, H) rep(w, W) cin >> as[h][w]; VVL bs(H, VL(W)); rep(h, H) rep(w, W) cin >> bs[h][w]; ll abs_max = 0; rep(h, H) rep(w, W) { chmax(abs_max, abs(as[h][w] - bs[h][w])); } ll abs_sum_max = (W + H - 1) * abs_max; ll res = INF; // dp[h][w][k]: h, w までの経路で偏りが k となるか // k はマイナスも扱うために abs_sum_max を足した値としておく V<V<V<bool>>> dp(H, V<V<bool>>(W, V<bool>(2 * abs_sum_max + 1))); dp[0][0][abs_sum_max + as[0][0] - bs[0][0]] = true; dp[0][0][abs_sum_max + bs[0][0] - as[0][0]] = true; // 配る DP rep(h, H) rep(w, W) rep(k, 2 * abs_sum_max + 1) { if (!dp[h][w][k]) continue; rep(h_diff, 2) rep(w_diff, 2) { if (h_diff == w_diff) continue; if (h + h_diff >= H || w + w_diff >= W) continue; ll a = as[h + h_diff][w + w_diff]; ll b = bs[h + h_diff][w + w_diff]; dp[h + h_diff][w + w_diff][k + a - b] = true; dp[h + h_diff][w + w_diff][k + b - a] = true; } } rep(k, abs_sum_max + 1) if (dp[H - 1][W - 1][abs_sum_max + k]) drop(k); } // bitset void solve2() { ll H, W; cin >> H >> W; VVL as(H, VL(W)); rep(h, H) rep(w, W) cin >> as[h][w]; VVL bs(H, VL(W)); rep(h, H) rep(w, W) cin >> bs[h][w]; ll abs_max = 0; rep(h, H) rep(w, W) { chmax(abs_max, abs(as[h][w] - bs[h][w])); } ll abs_sum_max = (W + H - 1) * abs_max; ll res = INF; // dp[h][w][k]: h, w までの経路で偏りが k となるか // k はマイナスも扱うために abs_sum_max を足した値としておく // (80 + 80 - 1) * 80 = 12720 using bits = bitset<12720>; V<V<bits>> dp(H, V<bits>(W, bits())); dp[0][0][abs_sum_max + as[0][0] - bs[0][0]] = true; dp[0][0][abs_sum_max + bs[0][0] - as[0][0]] = true; // 配る DP rep(h, H) rep(w, W) rep(k, 2 * abs_sum_max + 1) { if (!dp[h][w][k]) continue; rep(h_diff, 2) rep(w_diff, 2) { if (h_diff == w_diff) continue; if (h + h_diff >= H || w + w_diff >= W) continue; ll a = as[h + h_diff][w + w_diff]; ll b = bs[h + h_diff][w + w_diff]; dp[h + h_diff][w + w_diff][k + a - b] = true; dp[h + h_diff][w + w_diff][k + b - a] = true; } } rep(k, abs_sum_max + 1) if (dp[H - 1][W - 1][abs_sum_max + k]) drop(k); } struct exit_exception : public std::exception { const char *what() const throw() { return "Exited"; } }; #ifndef TEST int main() { cin.tie(0); ios::sync_with_stdio(false); try { solve2(); } catch (exit_exception &e) { } return 0; } #endif template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void print_ints(vector<ll> v) { rep(i, v.size()) { if (i > 0) { cout << " "; } cout << v[i]; } cout << endl; } template <typename T> void drop(T res) { cout << res << endl; throw exit_exception(); }
// https://atcoder.jp/contests/abc147/tasks/abc147_e #include "algorithm" #include "bitset" #include "cmath" #include "functional" #include "iomanip" #include "iostream" #include "map" #include "numeric" #include "queue" #include "set" #include "string" #include "vector" #define rep(i, to) for (ll i = 0; i < (to); ++i) #define rep1(i, to) for (ll i = 1; i <= (to); ++i) #define repf(i, from, to) for (ll i = from; i < (to); ++i) #define repr(i, from) for (ll i = from - 1; i >= 0; --i) #define all(vec) vec.begin(), vec.end() #define fi first #define se second using namespace std; typedef long long ll; template <typename T> using V = vector<T>; using VL = V<ll>; using VVL = V<VL>; template <typename T, typename U> using P = pair<T, U>; using PL = P<ll, ll>; using VPL = V<PL>; template <typename T> inline bool chmax(T &a, T b); template <typename T> inline bool chmin(T &a, T b); void print_ints(vector<ll> v); template <typename T> void drop(T a); const ll INF = 1e18; void solve() { ll H, W; cin >> H >> W; VVL as(H, VL(W)); rep(h, H) rep(w, W) cin >> as[h][w]; VVL bs(H, VL(W)); rep(h, H) rep(w, W) cin >> bs[h][w]; ll abs_max = 0; rep(h, H) rep(w, W) { chmax(abs_max, abs(as[h][w] - bs[h][w])); } ll abs_sum_max = (W + H - 1) * abs_max; ll res = INF; // dp[h][w][k]: h, w までの経路で偏りが k となるか // k はマイナスも扱うために abs_sum_max を足した値としておく V<V<V<bool>>> dp(H, V<V<bool>>(W, V<bool>(2 * abs_sum_max + 1))); dp[0][0][abs_sum_max + as[0][0] - bs[0][0]] = true; dp[0][0][abs_sum_max + bs[0][0] - as[0][0]] = true; // 配る DP rep(h, H) rep(w, W) rep(k, 2 * abs_sum_max + 1) { if (!dp[h][w][k]) continue; rep(h_diff, 2) rep(w_diff, 2) { if (h_diff == w_diff) continue; if (h + h_diff >= H || w + w_diff >= W) continue; ll a = as[h + h_diff][w + w_diff]; ll b = bs[h + h_diff][w + w_diff]; dp[h + h_diff][w + w_diff][k + a - b] = true; dp[h + h_diff][w + w_diff][k + b - a] = true; } } rep(k, abs_sum_max + 1) if (dp[H - 1][W - 1][abs_sum_max + k]) drop(k); } // bitset void solve2() { ll H, W; cin >> H >> W; VVL as(H, VL(W)); rep(h, H) rep(w, W) cin >> as[h][w]; VVL bs(H, VL(W)); rep(h, H) rep(w, W) cin >> bs[h][w]; ll abs_max = 0; rep(h, H) rep(w, W) { chmax(abs_max, abs(as[h][w] - bs[h][w])); } ll abs_sum_max = (W + H - 1) * abs_max; ll res = INF; // dp[h][w][k]: h, w までの経路で偏りが k となるか // k はマイナスも扱うために abs_sum_max を足した値としておく using bits = bitset<2 * (80 + 80 - 1) * 80 - 1>; V<V<bits>> dp(H, V<bits>(W, bits())); dp[0][0][abs_sum_max + as[0][0] - bs[0][0]] = true; dp[0][0][abs_sum_max + bs[0][0] - as[0][0]] = true; // 配る DP rep(h, H) rep(w, W) rep(k, 2 * abs_sum_max + 1) { if (!dp[h][w][k]) continue; rep(h_diff, 2) rep(w_diff, 2) { if (h_diff == w_diff) continue; if (h + h_diff >= H || w + w_diff >= W) continue; ll a = as[h + h_diff][w + w_diff]; ll b = bs[h + h_diff][w + w_diff]; dp[h + h_diff][w + w_diff][k + a - b] = true; dp[h + h_diff][w + w_diff][k + b - a] = true; } } rep(k, abs_sum_max + 1) if (dp[H - 1][W - 1][abs_sum_max + k]) drop(k); } struct exit_exception : public std::exception { const char *what() const throw() { return "Exited"; } }; #ifndef TEST int main() { cin.tie(0); ios::sync_with_stdio(false); try { solve2(); } catch (exit_exception &e) { } return 0; } #endif template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void print_ints(vector<ll> v) { rep(i, v.size()) { if (i > 0) { cout << " "; } cout << v[i]; } cout << endl; } template <typename T> void drop(T res) { cout << res << endl; throw exit_exception(); }
replace
101
103
101
102
0
p02839
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef uint64_t u64; typedef int64_t s64; typedef uint32_t u32; typedef int32_t s32; typedef vector<s32> vs32; typedef vector<u32> vu32; typedef vector<s64> vs64; typedef vector<u64> vu64; const double PI = 3.14159265358979323846; #define MAX(x, y) ((x) < (y) ? (y) : (x)) #define MIN(x, y) ((x) > (y) ? (y) : (x)) #define rep(i, N) for (int i = 0; i < N; ++i) #define CEIL(x, y) (((x) + (y)-1) / (y)) #define MOD 1000000007ULL int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vs32> A(H, vs32(W)); vector<vs32> B(H, vs32(W)); int m = 0; rep(h, H) rep(w, W) cin >> A[h][w]; rep(h, H) rep(w, W) { cin >> B[h][w]; m = MAX(m, abs(A[h][w] - B[h][w])); } vector<vector<vector<bool>>> dp( H, vector<vector<bool>>(W, vector<bool>((H + W) * m + 1, false))); dp[0][0][abs(A[0][0] - B[0][0])] = true; for (int h = 0; h < H; ++h) { for (int w = 0; w < W; ++w) { for (int k = 0; k <= (H + W) * 80; ++k) { if (dp[h][w][k]) { if (h < H - 1) { int d = abs(A[h + 1][w] - B[h + 1][w]); dp[h + 1][w][abs(k - d)] = true; dp[h + 1][w][abs(k + d)] = true; } if (w < W - 1) { int d = abs(A[h][w + 1] - B[h][w + 1]); dp[h][w + 1][abs(k - d)] = true; dp[h][w + 1][abs(k + d)] = true; } } } } } int ans; for (ans = 0; ans <= (H + W) * m && !dp[H - 1][W - 1][ans]; ++ans) ; cout << ans << "\n"; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef uint64_t u64; typedef int64_t s64; typedef uint32_t u32; typedef int32_t s32; typedef vector<s32> vs32; typedef vector<u32> vu32; typedef vector<s64> vs64; typedef vector<u64> vu64; const double PI = 3.14159265358979323846; #define MAX(x, y) ((x) < (y) ? (y) : (x)) #define MIN(x, y) ((x) > (y) ? (y) : (x)) #define rep(i, N) for (int i = 0; i < N; ++i) #define CEIL(x, y) (((x) + (y)-1) / (y)) #define MOD 1000000007ULL int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vs32> A(H, vs32(W)); vector<vs32> B(H, vs32(W)); int m = 0; rep(h, H) rep(w, W) cin >> A[h][w]; rep(h, H) rep(w, W) { cin >> B[h][w]; m = MAX(m, abs(A[h][w] - B[h][w])); } vector<vector<vector<bool>>> dp( H, vector<vector<bool>>(W, vector<bool>((H + W) * m + 1, false))); dp[0][0][abs(A[0][0] - B[0][0])] = true; for (int h = 0; h < H; ++h) { for (int w = 0; w < W; ++w) { for (int k = 0; k <= (H + W) * m; ++k) { if (dp[h][w][k]) { if (h < H - 1) { int d = abs(A[h + 1][w] - B[h + 1][w]); dp[h + 1][w][abs(k - d)] = true; dp[h + 1][w][abs(k + d)] = true; } if (w < W - 1) { int d = abs(A[h][w + 1] - B[h][w + 1]); dp[h][w + 1][abs(k - d)] = true; dp[h][w + 1][abs(k + d)] = true; } } } } } int ans; for (ans = 0; ans <= (H + W) * m && !dp[H - 1][W - 1][ans]; ++ans) ; cout << ans << "\n"; return 0; }
replace
57
58
57
58
-6
munmap_chunk(): invalid pointer
p02839
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &o) { out << "(" << o.first << "," << o.second << ")"; return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> &V) { for (int i = 0; i < V.size(); i++) { out << V[i]; if (i != V.size() - 1) out << " "; } return out; } template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> &Mat) { for (int i = 0; i < Mat.size(); i++) { if (i != 0) out << endl; out << Mat[i]; } return out; } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> &mp) { out << "{ "; for (auto it = mp.begin(); it != mp.end(); it++) { out << it->first << ":" << it->second; if (mp.size() - 1 != distance(mp.begin(), it)) out << ", "; } out << " }"; return out; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } /* <url:> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ template <class Type> Type solve(Type res = Type()) { int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)); vector<vector<int>> B(H, vector<int>(W)); for (auto &vec : A) for (auto &in : vec) cin >> in; for (auto &vec : B) for (auto &in : vec) cin >> in; int base = 7000; auto dp = make_v<int>(H + 1, W + 1, base * 2); dp[0][0][base] = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int v1 = A[i][j], v2 = B[i][j]; for (int k = 0; k < 2 * base; k++) { if (dp[i][j][k]) { dp[i + 1][j][k + v1 - v2] = true; dp[i + 1][j][k + v2 - v1] = true; dp[i][j + 1][k + v1 - v2] = true; dp[i][j + 1][k + v2 - v1] = true; } } } } res = INF; for (int k = 0; k < 2 * base; k++) { if (dp[H][W - 1][k]) { res = min(res, abs((ll)k - base)); } } return res; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); // solve<ll>(0); cout << fixed << setprecision(12) << solve<ll>() << endl; return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &o) { out << "(" << o.first << "," << o.second << ")"; return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> &V) { for (int i = 0; i < V.size(); i++) { out << V[i]; if (i != V.size() - 1) out << " "; } return out; } template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> &Mat) { for (int i = 0; i < Mat.size(); i++) { if (i != 0) out << endl; out << Mat[i]; } return out; } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> &mp) { out << "{ "; for (auto it = mp.begin(); it != mp.end(); it++) { out << it->first << ":" << it->second; if (mp.size() - 1 != distance(mp.begin(), it)) out << ", "; } out << " }"; return out; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } /* <url:> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ template <class Type> Type solve(Type res = Type()) { int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)); vector<vector<int>> B(H, vector<int>(W)); for (auto &vec : A) for (auto &in : vec) cin >> in; for (auto &vec : B) for (auto &in : vec) cin >> in; int base = 13000; auto dp = make_v<int>(H + 1, W + 1, base * 2); dp[0][0][base] = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int v1 = A[i][j], v2 = B[i][j]; for (int k = 0; k < 2 * base; k++) { if (dp[i][j][k]) { dp[i + 1][j][k + v1 - v2] = true; dp[i + 1][j][k + v2 - v1] = true; dp[i][j + 1][k + v1 - v2] = true; dp[i][j + 1][k + v2 - v1] = true; } } } } res = INF; for (int k = 0; k < 2 * base; k++) { if (dp[H][W - 1][k]) { res = min(res, abs((ll)k - base)); } } return res; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); // solve<ll>(0); cout << fixed << setprecision(12) << solve<ll>() << endl; return 0; }
replace
76
77
76
77
0
p02839
C++
Runtime Error
#include <algorithm> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define nmax_def 15000 #define nmax_def_2 7500 #define Inf 2000000000 #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; vector<vector<vb>> DP; int main() { int H, W; cin >> H >> W; vector<vi> A(H); vector<vi> B(H); vector<vi> difflist(H); rep(i, H) { difflist[i].resize(W); A[i].resize(W); B[i].resize(W); } rep(i, H) { rep(j, W) { cin >> A[i][j]; } } rep(i, H) { rep(j, W) { cin >> B[i][j]; } } rep(i, H) { rep(j, W) { difflist[i][j] = A[i][j] - B[i][j]; } } DP.resize(H + 1); rep(i, H + 1) { DP[i].resize(W + 1); rep(j, W + 1) { DP[i][j].resize(nmax_def); rep(k, nmax_def) { DP[i][j][k] = false; } } } DP[1][1][difflist[0][0] + nmax_def_2] = true; // 左上が(1, 1)で差はdifflist[0][0]->(要素では)+=7500 DP[1][1][-difflist[0][0] + nmax_def_2] = true; // 左上が(1, 1)で差は-difflist[0][0]->(要素では)+=7500 rep(i, H) { rep(j, W) { rep(k, nmax_def) { if (DP[i + 1][j][k]) { DP[i + 1][j + 1][k + difflist[i][j]] = true; DP[i + 1][j + 1][k - difflist[i][j]] = true; } if (DP[i][j + 1][k]) { DP[i + 1][j + 1][k + difflist[i][j]] = true; DP[i + 1][j + 1][k - difflist[i][j]] = true; } } } } int ans = Inf; rep(k, nmax_def) { if (DP[H][W][k]) { int anstmp = abs(k - nmax_def_2); ans = min(ans, anstmp); } } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define nmax_def 30000 #define nmax_def_2 15000 #define Inf 2000000000 #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; vector<vector<vb>> DP; int main() { int H, W; cin >> H >> W; vector<vi> A(H); vector<vi> B(H); vector<vi> difflist(H); rep(i, H) { difflist[i].resize(W); A[i].resize(W); B[i].resize(W); } rep(i, H) { rep(j, W) { cin >> A[i][j]; } } rep(i, H) { rep(j, W) { cin >> B[i][j]; } } rep(i, H) { rep(j, W) { difflist[i][j] = A[i][j] - B[i][j]; } } DP.resize(H + 1); rep(i, H + 1) { DP[i].resize(W + 1); rep(j, W + 1) { DP[i][j].resize(nmax_def); rep(k, nmax_def) { DP[i][j][k] = false; } } } DP[1][1][difflist[0][0] + nmax_def_2] = true; // 左上が(1, 1)で差はdifflist[0][0]->(要素では)+=7500 DP[1][1][-difflist[0][0] + nmax_def_2] = true; // 左上が(1, 1)で差は-difflist[0][0]->(要素では)+=7500 rep(i, H) { rep(j, W) { rep(k, nmax_def) { if (DP[i + 1][j][k]) { DP[i + 1][j + 1][k + difflist[i][j]] = true; DP[i + 1][j + 1][k - difflist[i][j]] = true; } if (DP[i][j + 1][k]) { DP[i + 1][j + 1][k + difflist[i][j]] = true; DP[i + 1][j + 1][k - difflist[i][j]] = true; } } } } int ans = Inf; rep(k, nmax_def) { if (DP[H][W][k]) { int anstmp = abs(k - nmax_def_2); ans = min(ans, anstmp); } } cout << ans << endl; return 0; }
replace
10
12
10
12
0
p02839
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)n; i++) #define all(c) (c).begin(), (c).end() #define pb push_back #define dbg(...) \ do { \ cerr << __LINE__ << ": "; \ dbgprint(#__VA_ARGS__, __VA_ARGS__); \ } while (0); using namespace std; namespace std { template <class S, class T> struct hash<pair<S, T>> { size_t operator()(const pair<S, T> &p) const { return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second); } }; template <class T> struct hash<vector<T>> { size_t operator()(const vector<T> &v) const { size_t h = 0; for (auto i : v) h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1; return h; } }; } // namespace std template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "[ "; rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", "); return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { os << "{ "; for (const auto &i : v) os << i << ", "; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const map<T, U> &v) { os << "{"; for (const auto &i : v) os << " " << i.first << ": " << i.second << ","; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.first << ", " << p.second << ")"; } void dbgprint(const string &fmt) { cerr << endl; } template <class H, class... T> void dbgprint(const string &fmt, const H &h, const T &...r) { cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " "; dbgprint(fmt.substr(fmt.find(",") + 1), r...); } typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; const int inf = (int)1e9; const double INF = 1e12, EPS = 1e-9; int main() { cin.tie(0); cin.sync_with_stdio(0); int h, w; cin >> h >> w; vector<vi> d(h, vi(w)); rep(it, 2) rep(i, h) rep(j, w) { d[i][j] *= -1; int x; cin >> x; d[i][j] += x; } const int MX = 13000; const int GETA = MX / 2; vector<vector<bitset<MX>>> dp(h, vector<bitset<MX>>(w)); dp[0][0][d[0][0] + GETA] = 1; dp[0][0][-d[0][0] + GETA] = 1; rep(i, h) rep(j, w) rep(k, MX) if (dp[i][j][k]) for (int s = -1; s < 2; s += 2) { if (i + 1 < h) dp[i + 1][j][k + s * d[i + 1][j]] = 1; if (j + 1 < w) dp[i][j + 1][k + s * d[i][j + 1]] = 1; } int ans = inf; rep(i, MX) if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - GETA)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)n; i++) #define all(c) (c).begin(), (c).end() #define pb push_back #define dbg(...) \ do { \ cerr << __LINE__ << ": "; \ dbgprint(#__VA_ARGS__, __VA_ARGS__); \ } while (0); using namespace std; namespace std { template <class S, class T> struct hash<pair<S, T>> { size_t operator()(const pair<S, T> &p) const { return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second); } }; template <class T> struct hash<vector<T>> { size_t operator()(const vector<T> &v) const { size_t h = 0; for (auto i : v) h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1; return h; } }; } // namespace std template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "[ "; rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", "); return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { os << "{ "; for (const auto &i : v) os << i << ", "; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const map<T, U> &v) { os << "{"; for (const auto &i : v) os << " " << i.first << ": " << i.second << ","; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.first << ", " << p.second << ")"; } void dbgprint(const string &fmt) { cerr << endl; } template <class H, class... T> void dbgprint(const string &fmt, const H &h, const T &...r) { cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " "; dbgprint(fmt.substr(fmt.find(",") + 1), r...); } typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; const int inf = (int)1e9; const double INF = 1e12, EPS = 1e-9; int main() { cin.tie(0); cin.sync_with_stdio(0); int h, w; cin >> h >> w; vector<vi> d(h, vi(w)); rep(it, 2) rep(i, h) rep(j, w) { d[i][j] *= -1; int x; cin >> x; d[i][j] += x; } const int MX = 26000; const int GETA = MX / 2; vector<vector<bitset<MX>>> dp(h, vector<bitset<MX>>(w)); dp[0][0][d[0][0] + GETA] = 1; dp[0][0][-d[0][0] + GETA] = 1; rep(i, h) rep(j, w) rep(k, MX) if (dp[i][j][k]) for (int s = -1; s < 2; s += 2) { if (i + 1 < h) dp[i + 1][j][k + s * d[i + 1][j]] = 1; if (j + 1 < w) dp[i][j + 1][k + s * d[i][j + 1]] = 1; } int ans = inf; rep(i, MX) if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - GETA)); cout << ans << endl; return 0; }
replace
74
75
74
75
0