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
p03096
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #define rt register int #define l putchar('\n') #define ll long long #define r read() #define p 1000000007 using namespace std; inline ll read() { ll x = 0; char zf = 1; char ch = getchar(); while (ch != '-' && !isdigit(ch)) ch = getchar(); if (ch == '-') zf = -1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * zf; } void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + 48); } void writeln(const ll y) { write(y); putchar('\n'); } int i, j, k, m, n, x, y, z, cnt, sum; int a[100010], f[100010], la[100100], s[100010]; int main() { n = r; for (rt i = 1; i <= n; i++) { a[i] = r; if (a[i] == a[i - 1]) n--, i--; } f[0] = s[0] = 1; for (rt i = 1; i <= n; i++) { if (la[a[i]]) f[i] = s[la[a[i]]]; la[a[i]] = i; s[i] = (s[i - 1] + f[i]) % p; } cout << s[n]; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #define rt register int #define l putchar('\n') #define ll long long #define r read() #define p 1000000007 using namespace std; inline ll read() { ll x = 0; char zf = 1; char ch = getchar(); while (ch != '-' && !isdigit(ch)) ch = getchar(); if (ch == '-') zf = -1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * zf; } void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + 48); } void writeln(const ll y) { write(y); putchar('\n'); } int i, j, k, m, n, x, y, z, cnt, sum; int a[1000010], f[1000010], la[1001000], s[1000010]; int main() { n = r; for (rt i = 1; i <= n; i++) { a[i] = r; if (a[i] == a[i - 1]) n--, i--; } f[0] = s[0] = 1; for (rt i = 1; i <= n; i++) { if (la[a[i]]) f[i] = s[la[a[i]]]; la[a[i]] = i; s[i] = (s[i - 1] + f[i]) % p; } cout << s[n]; return 0; }
replace
35
36
35
36
0
p03096
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <tuple> #include <vector> #define INF 1000000009 #define LINF 1000000000000000009 #define double long double #define all(a) a.begin(), a.end() using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> PP; 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; } ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); } ll lcm(ll n, ll m) { return n / gcd(n, m) * m; } ll mod = 1000000007; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> p(n); vector<ll> dp(n); dp[0] = 1; p[a[0]] = 1; for (int i = 1; i < n; i++) { dp[i] = dp[i - 1]; if (a[i] != a[i - 1]) { dp[i] += p[a[i]]; dp[i] %= mod; p[a[i]] = dp[i]; } } cout << dp[n - 1] << endl; }
#include <algorithm> #include <bitset> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <tuple> #include <vector> #define INF 1000000009 #define LINF 1000000000000000009 #define double long double #define all(a) a.begin(), a.end() using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> PP; 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; } ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); } ll lcm(ll n, ll m) { return n / gcd(n, m) * m; } ll mod = 1000000007; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> p(300000); vector<ll> dp(n); dp[0] = 1; p[a[0]] = 1; for (int i = 1; i < n; i++) { dp[i] = dp[i - 1]; if (a[i] != a[i - 1]) { dp[i] += p[a[i]]; dp[i] %= mod; p[a[i]] = dp[i]; } } cout << dp[n - 1] << endl; }
replace
45
46
45
46
0
p03096
C++
Runtime Error
// i ll be the king #include <bits/stdc++.h> #define ll long long int #define sync \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define input(arr, n) \ for (ll i1 = 0; i1 < n; i1++) \ cin >> arr[i1] #define rep(n) for (ll i2 = 0; i2 < n; i2++) #define vmp(v, a, b) v.push_back(make_pair(a, b)) #define si(a) scanf("%lld", &a) #define pi(a) printf("%lld", a) #define aset(a, n, k) \ for (ll i3 = 0; i3 < n; i3++) \ a[i3] = k; #define mod 1000000007 #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> using namespace std; int main() { sync; ll n; cin >> n; ll a[n]; input(a, n); map<ll, ll> mp; ll ans = 0, val = 0; vector<ll> arr; for (ll i = 0; i < n - 1; i++) if (a[i] != a[i + 1]) arr.push_back(a[i]); arr.push_back(a[n - 1]); for (ll i = 0; i < n; i++) { val = ans; if (mp[arr[i]]) ans = (ans + mp[arr[i]]) % mod; mp[arr[i]] = (mp[arr[i]] + 1 + val) % mod; } cout << ans + 1; }
// i ll be the king #include <bits/stdc++.h> #define ll long long int #define sync \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define input(arr, n) \ for (ll i1 = 0; i1 < n; i1++) \ cin >> arr[i1] #define rep(n) for (ll i2 = 0; i2 < n; i2++) #define vmp(v, a, b) v.push_back(make_pair(a, b)) #define si(a) scanf("%lld", &a) #define pi(a) printf("%lld", a) #define aset(a, n, k) \ for (ll i3 = 0; i3 < n; i3++) \ a[i3] = k; #define mod 1000000007 #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> using namespace std; int main() { sync; ll n; cin >> n; ll a[n]; input(a, n); map<ll, ll> mp; ll ans = 0, val = 0; vector<ll> arr; for (ll i = 0; i < n - 1; i++) if (a[i] != a[i + 1]) arr.push_back(a[i]); arr.push_back(a[n - 1]); for (ll i = 0; i < arr.size(); i++) { val = ans; if (mp[arr[i]]) ans = (ans + mp[arr[i]]) % mod; mp[arr[i]] = (mp[arr[i]] + 1 + val) % mod; } cout << ans + 1; }
replace
37
38
37
38
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vb = vector<bool>; using vc = vector<char>; using vs = vector<string>; using vvi = vector<vector<int>>; using vvc = vector<vector<char>>; using pii = pair<int, int>; #define fix10 cout << fixed << setprecision(10); #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define MOD 1000000007 int main() { int n; cin >> n; vector<int> clr(n); rep(i, n) cin >> clr.at(i); vector<long> dp(n + 1, 0); vector<long> hs(n + 1, 0); dp.at(0) = 1; hs.at(clr.at(0)) = 1; for (int i = 1; i <= n; i++) { int c = clr.at(i - 1); if (i != n) { if (clr.at(i - 1) == clr.at(i)) continue; } dp.at(i) = hs.at(c) % MOD; if (i == n) continue; hs.at(clr.at(i)) += dp.at(i); hs.at(clr.at(i)) %= MOD; } cout << dp.at(n) % MOD << endl; }
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vb = vector<bool>; using vc = vector<char>; using vs = vector<string>; using vvi = vector<vector<int>>; using vvc = vector<vector<char>>; using pii = pair<int, int>; #define fix10 cout << fixed << setprecision(10); #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define MOD 1000000007 int main() { int n; cin >> n; vector<int> clr(n); rep(i, n) cin >> clr.at(i); vector<long> dp(n + 1, 0); vector<long> hs(200001, 0); dp.at(0) = 1; hs.at(clr.at(0)) = 1; for (int i = 1; i <= n; i++) { int c = clr.at(i - 1); if (i != n) { if (clr.at(i - 1) == clr.at(i)) continue; } dp.at(i) = hs.at(c) % MOD; if (i == n) continue; hs.at(clr.at(i)) += dp.at(i); hs.at(clr.at(i)) %= MOD; } cout << dp.at(n) % MOD << endl; }
replace
19
20
19
20
0
p03096
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; const int mod = 1e+9 + 7; // dpテーブルは 1-indexed ll dp[200005]; int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> c(n); vector<vector<int>> pos(n); // 色がどの位置にあるか (0-indexed) for (int i = 0; i < n; ++i) { cin >> c[i]; --c[i]; pos[c[i]].push_back(i); } dp[0] = 1; for (int i = 0; i < n; ++i) { // 何も操作しない dp[i + 1] = (dp[i + 1] + dp[i]) % mod; // 今見ている c[i] が何番目であるか int id = lower_bound(pos[c[i]].begin(), pos[c[i]].end(), i) - pos[c[i]].begin(); if (id > 0) { // 同じ色が隣り合っていないか確認 if (i - pos[c[i]][id - 1] <= 1) continue; // 反転操作 dp[i + 1] = (dp[i + 1] + dp[pos[c[i]][id - 1] + 1]) % mod; } } cout << dp[n] << endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; const int mod = 1e+9 + 7; // dpテーブルは 1-indexed ll dp[200005]; int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> c(n); vector<vector<int>> pos(200005); // 色がどの位置にあるか (0-indexed) for (int i = 0; i < n; ++i) { cin >> c[i]; --c[i]; pos[c[i]].push_back(i); } dp[0] = 1; for (int i = 0; i < n; ++i) { // 何も操作しない dp[i + 1] = (dp[i + 1] + dp[i]) % mod; // 今見ている c[i] が何番目であるか int id = lower_bound(pos[c[i]].begin(), pos[c[i]].end(), i) - pos[c[i]].begin(); if (id > 0) { // 同じ色が隣り合っていないか確認 if (i - pos[c[i]][id - 1] <= 1) continue; // 反転操作 dp[i + 1] = (dp[i + 1] + dp[pos[c[i]][id - 1] + 1]) % mod; } } cout << dp[n] << endl; }
replace
18
19
18
19
0
p03096
C++
Runtime Error
#include <cstdio> #define mod 1000000007 #define maxn 100005 int n, val[maxn], dp[maxn], la[maxn], pos[maxn]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &val[i]); la[i] = pos[val[i]], pos[val[i]] = i; } dp[0] = 1; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; if (val[i] != val[i - 1] && la[i]) dp[i] = (dp[i] + dp[la[i]]) % mod; } printf("%d\n", dp[n]); }
#include <cstdio> #define mod 1000000007 #define maxn 200005 int n, val[maxn], dp[maxn], la[maxn], pos[maxn]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &val[i]); la[i] = pos[val[i]], pos[val[i]] = i; } dp[0] = 1; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; if (val[i] != val[i - 1] && la[i]) dp[i] = (dp[i] + dp[la[i]]) % mod; } printf("%d\n", dp[n]); }
replace
2
3
2
3
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // types typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ld, ld> pdd; typedef vector<ll> vll; // macros #define ALL(a) a.begin(), a.end() #define SZ(a) ((int)a.size()) #define FI first #define SE second #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 FOR(i, a, b) for (int i = (a); i < (b); i++) #define PB push_back #define EB emplace_back #define MP(a, b) make_pair(a, b) #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin()) #define Decimal fixed << setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL // constants const int inf = 1e9; const ll linf = 1LL << 50; const double eps = 1e-10; const int MOD = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; vll c(n); REP(i, n) { cin >> c[i]; c[i]--; } vll dp(n + 1, 0); dp[0] = 1; vll color(n + 1, -1); REP(i, n) { ll x = color[c[i]]; if (x == -1) { dp[i + 1] = dp[i]; } else { if (x == i - 1) dp[i + 1] = dp[i]; else dp[i + 1] = (dp[x + 1] + dp[i]) % MOD; } color[c[i]] = i; } cout << dp[n] << endl; }
#include <bits/stdc++.h> using namespace std; // types typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ld, ld> pdd; typedef vector<ll> vll; // macros #define ALL(a) a.begin(), a.end() #define SZ(a) ((int)a.size()) #define FI first #define SE second #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 FOR(i, a, b) for (int i = (a); i < (b); i++) #define PB push_back #define EB emplace_back #define MP(a, b) make_pair(a, b) #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin()) #define Decimal fixed << setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL // constants const int inf = 1e9; const ll linf = 1LL << 50; const double eps = 1e-10; const int MOD = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; vll c(n); REP(i, n) { cin >> c[i]; c[i]--; } vll dp(n + 1, 0); dp[0] = 1; vll color(2 * 1e5 + 1, -1); REP(i, n) { ll x = color[c[i]]; if (x == -1) { dp[i + 1] = dp[i]; } else { if (x == i - 1) dp[i + 1] = dp[i]; else dp[i + 1] = (dp[x + 1] + dp[i]) % MOD; } color[c[i]] = i; } cout << dp[n] << endl; }
replace
52
53
52
53
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++) #define all(v) v.begin(), v.end() #define sz(v) v.size() #define INF 100000000000000 // 10^14 template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } 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 { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } vector<mint> dp(200001, 0); vector<mint> dpc(200000, 0); int main() { ll N; cin >> N; vector<ll> C(N + 1); C.at(0) = -1; rep(i, N) cin >> C.at(i + 1); dp.at(0) = 1; rep(i, N) { if (C.at(i) != C.at(i + 1)) { dpc.at(C.at(i + 1)) += dp.at(i); } dp.at(i + 1) = dpc.at(C.at(i + 1)); } cout << dp.at(N) << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++) #define all(v) v.begin(), v.end() #define sz(v) v.size() #define INF 100000000000000 // 10^14 template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } 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 { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } vector<mint> dp(200001, 0); vector<mint> dpc(200000, 0); int main() { ll N; cin >> N; vector<ll> C(N + 1); C.at(0) = -1; rep(i, N) cin >> C.at(i + 1); rep(i, N) { C.at(i + 1)--; } dp.at(0) = 1; rep(i, N) { if (C.at(i) != C.at(i + 1)) { dpc.at(C.at(i + 1)) += dp.at(i); } dp.at(i + 1) = dpc.at(C.at(i + 1)); } cout << dp.at(N) << endl; }
insert
75
75
75
76
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #define DEBUG_MODE #define endl '\n' #ifdef DEBUG_MODE #define DEBUG(X) debug_func(X, #X) #define DEBUG_ENDL endl << flush #define DEBUG_SEPARATOR_LINE cout << "=================\n" #else #define DEBUG(X) 0 #define DEBUG_ENDL 0 #define DEBUG_SEPARATOR_LINE 0 #endif #define ALL(V) (V).begin(), (V).end() #define ALLR(V) (V).rbegin(), (V).rend() #define DEBUG_ENDL_S(S) ((S).size() ? "\n" : "") << flush; template <typename T> using V = vector<T>; template <typename T> using VV = V<V<T>>; template <typename T, typename U> using P = pair<T, U>; using ll = int64_t; using PLL = P<ll, ll>; template <typename T> const T &var_min(const T &t) { return t; } template <typename T> const T &var_max(const T &t) { return t; } template <typename Head, typename... Tail> const Head &var_min(const Head &head, const Tail &...tail) { return min(head, var_min(tail...)); } template <typename Head, typename... Tail> const Head &var_max(const Head &head, const Tail &...tail) { return max(head, var_max(tail...)); } template <typename T, typename... Tail> void chmin(T &t, const Tail &...tail) { t = var_min(t, tail...); } template <typename T, typename... Tail> void chmax(T &t, const Tail &...tail) { t = var_max(t, tail...); } void debug_func_preffix(const string &s) { if (s.size()) cout << s << " = "; } template <typename T> void debug_func(const T &t, const string &s = "") { debug_func_preffix(s); cout << t << DEBUG_ENDL_S(s); } template <typename T, typename U> void debug_func(const P<T, U> &p, const string &s = "") { debug_func_preffix(s); cout << "("; debug_func(p.first); cout << ", "; debug_func(p.second); cout << ")" << DEBUG_ENDL_S(s); } template <typename T> void debug_func(const V<T> &v, const string &s = "") { for (ll i = 0; i < v.size(); i++) { string t = s + "[" + to_string(i) + "]"; debug_func(v[i], t); } } void init_io() { cin.tie(0); ios_base::sync_with_stdio(false); cout << fixed << setprecision(30); } const ll MOD = 1e9 + 7; int main() { init_io(); ll N; cin >> N; V<ll> dp(N + 1); ll pre = -1; V<ll> prec(N, -1); dp[0] = 1; for (ll i = 0; i < N; i++) { ll e; cin >> e; e--; if (e == pre) { dp[i + 1] = dp[i]; continue; } if (prec[e] == -1) { pre = e; prec[e] = i; dp[i + 1] = dp[i]; continue; } DEBUG(i); DEBUG(e + 1); DEBUG(prec[e]); DEBUG(dp[prec[e]]); dp[i + 1] = (dp[prec[e] + 1] + dp[i]) % MOD; prec[e] = i; pre = e; } DEBUG(dp); cout << dp[N] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; // #define DEBUG_MODE #define endl '\n' #ifdef DEBUG_MODE #define DEBUG(X) debug_func(X, #X) #define DEBUG_ENDL endl << flush #define DEBUG_SEPARATOR_LINE cout << "=================\n" #else #define DEBUG(X) 0 #define DEBUG_ENDL 0 #define DEBUG_SEPARATOR_LINE 0 #endif #define ALL(V) (V).begin(), (V).end() #define ALLR(V) (V).rbegin(), (V).rend() #define DEBUG_ENDL_S(S) ((S).size() ? "\n" : "") << flush; template <typename T> using V = vector<T>; template <typename T> using VV = V<V<T>>; template <typename T, typename U> using P = pair<T, U>; using ll = int64_t; using PLL = P<ll, ll>; template <typename T> const T &var_min(const T &t) { return t; } template <typename T> const T &var_max(const T &t) { return t; } template <typename Head, typename... Tail> const Head &var_min(const Head &head, const Tail &...tail) { return min(head, var_min(tail...)); } template <typename Head, typename... Tail> const Head &var_max(const Head &head, const Tail &...tail) { return max(head, var_max(tail...)); } template <typename T, typename... Tail> void chmin(T &t, const Tail &...tail) { t = var_min(t, tail...); } template <typename T, typename... Tail> void chmax(T &t, const Tail &...tail) { t = var_max(t, tail...); } void debug_func_preffix(const string &s) { if (s.size()) cout << s << " = "; } template <typename T> void debug_func(const T &t, const string &s = "") { debug_func_preffix(s); cout << t << DEBUG_ENDL_S(s); } template <typename T, typename U> void debug_func(const P<T, U> &p, const string &s = "") { debug_func_preffix(s); cout << "("; debug_func(p.first); cout << ", "; debug_func(p.second); cout << ")" << DEBUG_ENDL_S(s); } template <typename T> void debug_func(const V<T> &v, const string &s = "") { for (ll i = 0; i < v.size(); i++) { string t = s + "[" + to_string(i) + "]"; debug_func(v[i], t); } } void init_io() { cin.tie(0); ios_base::sync_with_stdio(false); cout << fixed << setprecision(30); } const ll MOD = 1e9 + 7; int main() { init_io(); ll N; cin >> N; V<ll> dp(N + 1); ll pre = -1; V<ll> prec(2e5 + 10, -1); dp[0] = 1; for (ll i = 0; i < N; i++) { ll e; cin >> e; e--; if (e == pre) { dp[i + 1] = dp[i]; continue; } if (prec[e] == -1) { pre = e; prec[e] = i; dp[i + 1] = dp[i]; continue; } DEBUG(i); DEBUG(e + 1); DEBUG(prec[e]); DEBUG(dp[prec[e]]); dp[i + 1] = (dp[prec[e] + 1] + dp[i]) % MOD; prec[e] = i; pre = e; } DEBUG(dp); cout << dp[N] << endl; return 0; }
replace
75
76
75
76
0
p03096
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cctype> #include <chrono> #define _USE_MATH_DEFINES #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <tuple> #include <vector> using namespace std; #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() const int INF = 0x3f3f3f3f, MOD = 1000000007; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; /*-----------------------------------------*/ int main() { cin.tie(0); ios::sync_with_stdio(false); // freopen("input.txt", "r", stdin); int n; cin >> n; vector<long long> memo(n + 1, 0); vector<int> c; int mae = -1; REP(i, n) { int cc; cin >> cc; if (mae != cc) { c.emplace_back(cc); mae = cc; } } n = c.size(); long long pre = 1; REP(i, n) { (memo[c[i]] += pre) %= MOD; pre = memo[c[i]]; } cout << pre << '\n'; return 0; }
#include <algorithm> #include <cassert> #include <cctype> #include <chrono> #define _USE_MATH_DEFINES #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <tuple> #include <vector> using namespace std; #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() const int INF = 0x3f3f3f3f, MOD = 1000000007; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; /*-----------------------------------------*/ int main() { cin.tie(0); ios::sync_with_stdio(false); // freopen("input.txt", "r", stdin); int n; cin >> n; vector<long long> memo(200001, 0); vector<int> c; int mae = -1; REP(i, n) { int cc; cin >> cc; if (mae != cc) { c.emplace_back(cc); mae = cc; } } n = c.size(); long long pre = 1; REP(i, n) { (memo[c[i]] += pre) %= MOD; pre = memo[c[i]]; } cout << pre << '\n'; return 0; }
replace
36
37
36
37
0
p03096
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <deque> #include <float.h> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <tuple> #include <unordered_map> #include <vector> using namespace std; #define int long long #define I32_MAX 2147483647 #define I64_MAX 9223372036854775807LL #define I64_MAX2 1223372036854775807LL #define INF I64_MAX2 #define MOD 1000000007 #define MEM_SIZE 10000 #define DEBUG_OUT true #define ALL(x) (x).begin(), (x).end() // int DP[MEM_SIZE][MEM_SIZE] = {0}; // int GMEM[MEM_SIZE][MEM_SIZE] = {0}; template <typename T> void DEBUG(T e) { if (DEBUG_OUT == false) return; std::cout << e << " "; } template <typename T> void DEBUG(const std::vector<T> &v) { if (DEBUG_OUT == false) return; for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) { if (DEBUG_OUT == false) return; for (const auto &v : vv) { DEBUG(v); } } template <class T, class... Ts> void DEBUG(T d, Ts... e) { if (DEBUG_OUT == false) return; DEBUG(d); DEBUG(e...); }; template <class T> void corner(bool flg, T hoge) { if (flg) { cout << hoge << endl; exit(0); } } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } 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); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) { for (auto &e : u) fill_v<T>(e, v...); } // FUNCTION // BELOW void solve(void) { int N; cin >> N; vector<int> vec(N); for (int i = 0; i < N; i++) { cin >> vec[i]; } int DP = 1; vector<int> data(100111, 0); for (int i = 0; i < N; i++) { if (i != 0 && vec[i] == vec[i - 1]) continue; int x = vec[i]; data[x] = data[x] + DP; data[x] %= MOD; DP = data[x]; // DEBUG(">>",i,x,DP); } cout << DP % MOD << endl; return; } int32_t main(int32_t argc, const char *argv[]) { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout << std::fixed; std::cout << std::setprecision(9); solve(); return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <deque> #include <float.h> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <tuple> #include <unordered_map> #include <vector> using namespace std; #define int long long #define I32_MAX 2147483647 #define I64_MAX 9223372036854775807LL #define I64_MAX2 1223372036854775807LL #define INF I64_MAX2 #define MOD 1000000007 #define MEM_SIZE 10000 #define DEBUG_OUT true #define ALL(x) (x).begin(), (x).end() // int DP[MEM_SIZE][MEM_SIZE] = {0}; // int GMEM[MEM_SIZE][MEM_SIZE] = {0}; template <typename T> void DEBUG(T e) { if (DEBUG_OUT == false) return; std::cout << e << " "; } template <typename T> void DEBUG(const std::vector<T> &v) { if (DEBUG_OUT == false) return; for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) { if (DEBUG_OUT == false) return; for (const auto &v : vv) { DEBUG(v); } } template <class T, class... Ts> void DEBUG(T d, Ts... e) { if (DEBUG_OUT == false) return; DEBUG(d); DEBUG(e...); }; template <class T> void corner(bool flg, T hoge) { if (flg) { cout << hoge << endl; exit(0); } } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } 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); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) { for (auto &e : u) fill_v<T>(e, v...); } // FUNCTION // BELOW void solve(void) { int N; cin >> N; vector<int> vec(N); for (int i = 0; i < N; i++) { cin >> vec[i]; } int DP = 1; vector<int> data(10000111, 0); for (int i = 0; i < N; i++) { if (i != 0 && vec[i] == vec[i - 1]) continue; int x = vec[i]; data[x] = data[x] + DP; data[x] %= MOD; DP = data[x]; // DEBUG(">>",i,x,DP); } cout << DP % MOD << endl; return; } int32_t main(int32_t argc, const char *argv[]) { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout << std::fixed; std::cout << std::setprecision(9); solve(); return 0; }
replace
111
112
111
112
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 int main() { long Nc; cin >> Nc; vector<long> C; for (long i = 0; i < Nc; i++) { long c; cin >> c; c--; if (i == 0 || c != C[C.size() - 1]) C.push_back(c); } long N = C.size(); vector<long> DP(N, 1), L(Nc, 0); for (long i = 0; i < N; i++) { if (i != 0) DP[i] = (DP[i - 1] + L[C[i]]) % MOD; L[C[i]] = (L[C[i]] + (i > 0 ? DP[i - 1] : 1)) % MOD; } cout << DP[N - 1] % MOD; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 int main() { long Nc; cin >> Nc; vector<long> C; for (long i = 0; i < Nc; i++) { long c; cin >> c; c--; if (i == 0 || c != C[C.size() - 1]) C.push_back(c); } long N = C.size(); Nc = 200000; vector<long> DP(N, 1), L(Nc, 0); for (long i = 0; i < N; i++) { if (i != 0) DP[i] = (DP[i - 1] + L[C[i]]) % MOD; L[C[i]] = (L[C[i]] + (i > 0 ? DP[i - 1] : 1)) % MOD; } cout << DP[N - 1] % MOD; }
insert
15
15
15
16
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; typedef long long ll; // -9,223,372,036,854,775,808 から 9,223,372,036,854,775,807 template <class T> using vec = std::vector<T>; template <class T> using vec2 = vec<vec<T>>; template <class T> using vec3 = vec<vec<vec<T>>>; // #define DEBUG struct GCDResult { long long x; long long y; long long res; }; GCDResult gcd(long long a, long long b) { if (a < 0) { auto sub = gcd(-a, b); return GCDResult{-sub.x, sub.y, sub.res}; } if (b < 0) { auto sub = gcd(a, -b); return GCDResult{sub.x, -sub.y, sub.res}; } if (a < b) { auto sub = gcd(b, a); return GCDResult{sub.y, sub.x, sub.res}; } // a >= b AND a >= 0 AND b >= 0 if (b == 0) { return GCDResult{1LL, 0LL, a}; } // a >= b AND a > 0 AND b > 0 auto sub = gcd(b, a % b); return GCDResult{sub.y, sub.x - sub.y * (a / b), sub.res}; } // Requires ATC_GCD const int COM_MAX = 510000; template <long long p> class Zp { long long content; // for comb() static array<long long, COM_MAX> fac; static array<long long, COM_MAX> finv; static array<long long, COM_MAX> inv_; static bool is_initialized_com_table; // By rng_2 (See https://drken1215.hatenablog.com/entry/2018/06/08/210000) // テーブルを作る前処理 static void COMinit() { auto MOD = p; auto MAX = COM_MAX; fac.at(0) = fac.at(1) = 1; finv.at(0) = finv.at(1) = 1; inv_.at(1) = 1; for (int i = 2; i < MAX; i++) { fac.at(i) = fac.at(i - 1) * i % MOD; inv_.at(i) = MOD - inv_.at(MOD % i) * (MOD / i) % MOD; finv.at(i) = finv.at(i - 1) * inv_.at(i) % MOD; } is_initialized_com_table = true; } // 二項係数計算 static long long COM(int n, int k) { auto MOD = p; auto MAX = COM_MAX; if (!is_initialized_com_table) { assert(false); } if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac.at(n) * (finv.at(k) * finv.at(n - k) % MOD) % MOD; } public: Zp(long long content_) { if (content_ >= 0) { content = content_ % p; } else { long long temp = p; while (temp < -content_) { temp *= p; } content = (temp + content_) % p; } } bool operator==(const Zp<p> &rhs) const { return this->content == rhs.content; } bool operator!=(const Zp<p> &rhs) const { return !(*this == rhs); } Zp<p> operator+(const Zp<p> &rhs) const { return Zp<p>(this->content + rhs.content); } Zp<p> operator-() const { return Zp<p>(p - this->content); } Zp<p> operator-(const Zp<p> &rhs) const { return Zp<p>(this->content - rhs.content + p); } Zp<p> operator*(const Zp<p> &rhs) const { return Zp<p>(this->content * rhs.content); } Zp<p> inv() const { if (this->content == 0) { assert(false); } auto res = gcd(this->content, p); return Zp<p>(res.x); } Zp<p> operator/(const Zp<p> &rhs) const { if (rhs.content == 0) { assert(false); } return (*this) * rhs.inv(); } Zp<p> pow(int n) { auto res = Zp<p>(1); Zp<p> mult = (*this); while (n > 0) { if (n & 1 == 1) { res = res * mult; } n >>= 1; mult = mult * mult; } return res; } void operator+=(const Zp<p> &rhs) { *this = *this + rhs; } void operator-=(const Zp<p> &rhs) { *this = *this - rhs; } void operator*=(const Zp<p> &rhs) { *this = *this * rhs; } void operator/=(const Zp<p> &rhs) { *this = *this / rhs; } long long raw() { return this->content; } static void initializeCom() { COMinit(); } static Zp<p> com(int n, int k) { return Zp<p>(COM(n, k)); } }; template <long long p> bool Zp<p>::is_initialized_com_table = false; template <long long p> array<long long, COM_MAX> Zp<p>::fac; template <long long p> array<long long, COM_MAX> Zp<p>::finv; template <long long p> array<long long, COM_MAX> Zp<p>::inv_; struct Seq { ll len; ll col; }; void solve(long long N, std::vector<long long> C) { // shift for (auto &iter : C) { iter--; } // かたまりをかぞえる vec<Seq> seqs; seqs.push_back(Seq{0, C.at(0)}); for (int i = 0; i < N; i++) { if (C.at(i) == seqs.back().col) { seqs.back().len++; } else { seqs.push_back(Seq{1, C.at(i)}); } } // かたまりの個数をかぞえる vec<ll> cnt(200'000); for (auto iter : seqs) { cnt.at(iter.col)++; } vec<Zp<MOD>> resAt(seqs.size(), Zp<MOD>(0)); ll INF = 11234567LL; resAt.at(0) = Zp<MOD>(1); vec<ll> lastAppeared(seqs.size(), INF); lastAppeared.at(seqs.at(0).col) = 0; for (int i = 1; i < seqs.size(); i++) { auto res = resAt.at(i - 1); if (lastAppeared.at(seqs.at(i).col) != INF) { res += resAt.at(lastAppeared.at(seqs.at(i).col)); } resAt.at(i) = res; lastAppeared.at(seqs.at(i).col) = i; } cout << resAt.back().raw() << 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> C(N); for (int i = 0; i < N; i++) { scanf("%lld", &C[i]); } solve(N, std::move(C)); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; typedef long long ll; // -9,223,372,036,854,775,808 から 9,223,372,036,854,775,807 template <class T> using vec = std::vector<T>; template <class T> using vec2 = vec<vec<T>>; template <class T> using vec3 = vec<vec<vec<T>>>; // #define DEBUG struct GCDResult { long long x; long long y; long long res; }; GCDResult gcd(long long a, long long b) { if (a < 0) { auto sub = gcd(-a, b); return GCDResult{-sub.x, sub.y, sub.res}; } if (b < 0) { auto sub = gcd(a, -b); return GCDResult{sub.x, -sub.y, sub.res}; } if (a < b) { auto sub = gcd(b, a); return GCDResult{sub.y, sub.x, sub.res}; } // a >= b AND a >= 0 AND b >= 0 if (b == 0) { return GCDResult{1LL, 0LL, a}; } // a >= b AND a > 0 AND b > 0 auto sub = gcd(b, a % b); return GCDResult{sub.y, sub.x - sub.y * (a / b), sub.res}; } // Requires ATC_GCD const int COM_MAX = 510000; template <long long p> class Zp { long long content; // for comb() static array<long long, COM_MAX> fac; static array<long long, COM_MAX> finv; static array<long long, COM_MAX> inv_; static bool is_initialized_com_table; // By rng_2 (See https://drken1215.hatenablog.com/entry/2018/06/08/210000) // テーブルを作る前処理 static void COMinit() { auto MOD = p; auto MAX = COM_MAX; fac.at(0) = fac.at(1) = 1; finv.at(0) = finv.at(1) = 1; inv_.at(1) = 1; for (int i = 2; i < MAX; i++) { fac.at(i) = fac.at(i - 1) * i % MOD; inv_.at(i) = MOD - inv_.at(MOD % i) * (MOD / i) % MOD; finv.at(i) = finv.at(i - 1) * inv_.at(i) % MOD; } is_initialized_com_table = true; } // 二項係数計算 static long long COM(int n, int k) { auto MOD = p; auto MAX = COM_MAX; if (!is_initialized_com_table) { assert(false); } if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac.at(n) * (finv.at(k) * finv.at(n - k) % MOD) % MOD; } public: Zp(long long content_) { if (content_ >= 0) { content = content_ % p; } else { long long temp = p; while (temp < -content_) { temp *= p; } content = (temp + content_) % p; } } bool operator==(const Zp<p> &rhs) const { return this->content == rhs.content; } bool operator!=(const Zp<p> &rhs) const { return !(*this == rhs); } Zp<p> operator+(const Zp<p> &rhs) const { return Zp<p>(this->content + rhs.content); } Zp<p> operator-() const { return Zp<p>(p - this->content); } Zp<p> operator-(const Zp<p> &rhs) const { return Zp<p>(this->content - rhs.content + p); } Zp<p> operator*(const Zp<p> &rhs) const { return Zp<p>(this->content * rhs.content); } Zp<p> inv() const { if (this->content == 0) { assert(false); } auto res = gcd(this->content, p); return Zp<p>(res.x); } Zp<p> operator/(const Zp<p> &rhs) const { if (rhs.content == 0) { assert(false); } return (*this) * rhs.inv(); } Zp<p> pow(int n) { auto res = Zp<p>(1); Zp<p> mult = (*this); while (n > 0) { if (n & 1 == 1) { res = res * mult; } n >>= 1; mult = mult * mult; } return res; } void operator+=(const Zp<p> &rhs) { *this = *this + rhs; } void operator-=(const Zp<p> &rhs) { *this = *this - rhs; } void operator*=(const Zp<p> &rhs) { *this = *this * rhs; } void operator/=(const Zp<p> &rhs) { *this = *this / rhs; } long long raw() { return this->content; } static void initializeCom() { COMinit(); } static Zp<p> com(int n, int k) { return Zp<p>(COM(n, k)); } }; template <long long p> bool Zp<p>::is_initialized_com_table = false; template <long long p> array<long long, COM_MAX> Zp<p>::fac; template <long long p> array<long long, COM_MAX> Zp<p>::finv; template <long long p> array<long long, COM_MAX> Zp<p>::inv_; struct Seq { ll len; ll col; }; void solve(long long N, std::vector<long long> C) { // shift for (auto &iter : C) { iter--; } // かたまりをかぞえる vec<Seq> seqs; seqs.push_back(Seq{0, C.at(0)}); for (int i = 0; i < N; i++) { if (C.at(i) == seqs.back().col) { seqs.back().len++; } else { seqs.push_back(Seq{1, C.at(i)}); } } // かたまりの個数をかぞえる vec<ll> cnt(200'000); for (auto iter : seqs) { cnt.at(iter.col)++; } vec<Zp<MOD>> resAt(seqs.size(), Zp<MOD>(0)); ll INF = 11234567LL; resAt.at(0) = Zp<MOD>(1); vec<ll> lastAppeared(200'000, INF); lastAppeared.at(seqs.at(0).col) = 0; for (int i = 1; i < seqs.size(); i++) { auto res = resAt.at(i - 1); if (lastAppeared.at(seqs.at(i).col) != INF) { res += resAt.at(lastAppeared.at(seqs.at(i).col)); } resAt.at(i) = res; lastAppeared.at(seqs.at(i).col) = i; } cout << resAt.back().raw() << 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> C(N); for (int i = 0; i < N; i++) { scanf("%lld", &C[i]); } solve(N, std::move(C)); return 0; }
replace
196
197
196
197
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define dump(...) \ do { \ print_vars(cout << "# " << #__VA_ARGS__ << '=', __VA_ARGS__); \ cout << endl; \ } while (0) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define all(c) begin(c), end(c) #define mp make_pair #define mt make_tuple using uint = unsigned; using ll = long long; using ull = unsigned long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vd = vector<double>; using vvd = vector<vd>; using vs = vector<string>; template <typename T, typename U> bool chmin(T &a, const U &b) { return a > b ? (a = b, 1) : 0; } template <typename T, typename U> bool chmax(T &a, const U &b) { return a < b ? (a = b, 1) : 0; } void print_vars(ostream &) {} template <typename Car, typename... Cdr> void print_vars(ostream &os, const Car &car, const Cdr &...cdr) { print_vars(os << car << (sizeof...(cdr) ? "," : ""), cdr...); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '(' << p.first << ',' << p.second << ')'; } template <int I, typename Tuple> void print_tuple(ostream &, const Tuple &) {} template <int I, typename Car, typename... Cdr, typename Tuple> void print_tuple(ostream &os, const Tuple &t) { os << get<I>(t) << (sizeof...(Cdr) ? "," : ""); print_tuple<I + 1, Cdr...>(os, t); } template <typename... Args> ostream &operator<<(ostream &os, const tuple<Args...> &t) { print_tuple<0, Args...>(os << '(', t); return os << ')'; } template <typename Ch, typename Tr, typename C> basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) { os << '['; for (auto i = begin(c); i != end(c); ++i) os << (i == begin(c) ? "" : " ") << *i; return os << ']'; } constexpr int INF = 1e9; constexpr int MOD = 1e9 + 7; constexpr double EPS = 1e-9; template <uint M> struct modint { uint val; modint(uint x = 0) : val((x % M + M) % M) {} modint pow(uint r) const { ll a = val, x = 1; for (; r; r >>= 1) { if (r & 1) (x *= a) %= M; (a *= a) %= M; } return x; } modint inv() const { ll x = 1; for (ll a = val, b = M, u = 0; b;) { ll t = a / b; swap(a -= b * t, b); swap(x -= u * t, u); } return (x + M) % M; } modint &operator=(const modint &x) & { val = x.val; return *this; } modint &operator+=(const modint &x) & { if ((val += x.val) >= M) val -= M; return *this; } modint &operator-=(const modint &x) & { if ((val += M - x.val) >= M) val -= M; return *this; } modint &operator*=(const modint &x) & { val = (ll)val * x.val % M; return *this; } modint &operator/=(const modint &x) & { val = (ll)val * x.inv().val % M; return *this; } explicit operator uint() const { return val; } }; template <uint M> bool operator==(const modint<M> &a, const modint<M> &b) { return a.val == b.val; } template <uint M> bool operator!=(const modint<M> &a, const modint<M> &b) { return a.val != b.val; } template <uint M> modint<M> operator+(modint<M> a, const modint<M> &b) { a += b; return a; } template <uint M> modint<M> operator-(modint<M> a, const modint<M> &b) { a -= b; return a; } template <uint M> modint<M> operator*(modint<M> a, const modint<M> &b) { a *= b; return a; } template <uint M> modint<M> operator/(modint<M> a, const modint<M> &b) { a /= b; return a; } template <uint M> ostream &operator<<(ostream &os, const modint<M> &x) { return os << x.val; } using mint = modint<MOD>; int main() { #ifndef _GLIBCXX_DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); constexpr char endl = '\n'; #endif for (int n; cin >> n && n;) { vi cs(n); rep(i, n) cin >> cs[i]; vector<mint> dp(n + 1); dp[0] = 1; vi prev(n, -1); rep(i, n) { dp[i + 1] = dp[i]; if (prev[cs[i]] != -1 && prev[cs[i]] < i - 1) dp[i + 1] += dp[prev[cs[i]] + 1]; prev[cs[i]] = i; } cout << dp.back() << endl; } }
#include <bits/stdc++.h> using namespace std; #define dump(...) \ do { \ print_vars(cout << "# " << #__VA_ARGS__ << '=', __VA_ARGS__); \ cout << endl; \ } while (0) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define all(c) begin(c), end(c) #define mp make_pair #define mt make_tuple using uint = unsigned; using ll = long long; using ull = unsigned long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vd = vector<double>; using vvd = vector<vd>; using vs = vector<string>; template <typename T, typename U> bool chmin(T &a, const U &b) { return a > b ? (a = b, 1) : 0; } template <typename T, typename U> bool chmax(T &a, const U &b) { return a < b ? (a = b, 1) : 0; } void print_vars(ostream &) {} template <typename Car, typename... Cdr> void print_vars(ostream &os, const Car &car, const Cdr &...cdr) { print_vars(os << car << (sizeof...(cdr) ? "," : ""), cdr...); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '(' << p.first << ',' << p.second << ')'; } template <int I, typename Tuple> void print_tuple(ostream &, const Tuple &) {} template <int I, typename Car, typename... Cdr, typename Tuple> void print_tuple(ostream &os, const Tuple &t) { os << get<I>(t) << (sizeof...(Cdr) ? "," : ""); print_tuple<I + 1, Cdr...>(os, t); } template <typename... Args> ostream &operator<<(ostream &os, const tuple<Args...> &t) { print_tuple<0, Args...>(os << '(', t); return os << ')'; } template <typename Ch, typename Tr, typename C> basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) { os << '['; for (auto i = begin(c); i != end(c); ++i) os << (i == begin(c) ? "" : " ") << *i; return os << ']'; } constexpr int INF = 1e9; constexpr int MOD = 1e9 + 7; constexpr double EPS = 1e-9; template <uint M> struct modint { uint val; modint(uint x = 0) : val((x % M + M) % M) {} modint pow(uint r) const { ll a = val, x = 1; for (; r; r >>= 1) { if (r & 1) (x *= a) %= M; (a *= a) %= M; } return x; } modint inv() const { ll x = 1; for (ll a = val, b = M, u = 0; b;) { ll t = a / b; swap(a -= b * t, b); swap(x -= u * t, u); } return (x + M) % M; } modint &operator=(const modint &x) & { val = x.val; return *this; } modint &operator+=(const modint &x) & { if ((val += x.val) >= M) val -= M; return *this; } modint &operator-=(const modint &x) & { if ((val += M - x.val) >= M) val -= M; return *this; } modint &operator*=(const modint &x) & { val = (ll)val * x.val % M; return *this; } modint &operator/=(const modint &x) & { val = (ll)val * x.inv().val % M; return *this; } explicit operator uint() const { return val; } }; template <uint M> bool operator==(const modint<M> &a, const modint<M> &b) { return a.val == b.val; } template <uint M> bool operator!=(const modint<M> &a, const modint<M> &b) { return a.val != b.val; } template <uint M> modint<M> operator+(modint<M> a, const modint<M> &b) { a += b; return a; } template <uint M> modint<M> operator-(modint<M> a, const modint<M> &b) { a -= b; return a; } template <uint M> modint<M> operator*(modint<M> a, const modint<M> &b) { a *= b; return a; } template <uint M> modint<M> operator/(modint<M> a, const modint<M> &b) { a /= b; return a; } template <uint M> ostream &operator<<(ostream &os, const modint<M> &x) { return os << x.val; } using mint = modint<MOD>; int main() { #ifndef _GLIBCXX_DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); constexpr char endl = '\n'; #endif for (int n; cin >> n && n;) { vi cs(n); rep(i, n) cin >> cs[i]; vector<mint> dp(n + 1); dp[0] = 1; vi prev(*max_element(all(cs)) + 1, -1); rep(i, n) { dp[i + 1] = dp[i]; if (prev[cs[i]] != -1 && prev[cs[i]] < i - 1) dp[i + 1] += dp[prev[cs[i]] + 1]; prev[cs[i]] = i; } cout << dp.back() << endl; } }
replace
156
157
156
157
0
p03096
C++
Runtime Error
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; const int MOD = 1000000007; typedef long long ll; typedef pair<ll, ll> p; const int INF = (1 << 28); const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define INF 2e9 #define ALL(v) v.begin(), v.end() int n; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; vector<ll> count(n + 1, 0); ll dp[n + 1]; dp[0] = 1; int before = -1; for (int i = 1; i <= n; i++) { int temp; cin >> temp; dp[i] = dp[i - 1]; if (before == temp) continue; before = temp; // if(count[temp].size()!=0){ /* for(auto iter=count[temp].begin();iter!=count[temp].end();iter++){ if(i-*iter>1){ dp[i]+=dp[*iter-1]; dp[i]%=MOD; } } */ dp[i] += count[temp]; dp[i] %= MOD; //} count[temp] += dp[i - 1]; count[temp] %= MOD; } cout << dp[n] << "\n"; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; const int MOD = 1000000007; typedef long long ll; typedef pair<ll, ll> p; const int INF = (1 << 28); const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define INF 2e9 #define ALL(v) v.begin(), v.end() int n; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; vector<ll> count(2 * 100000, 0); ll dp[n + 1]; dp[0] = 1; int before = -1; for (int i = 1; i <= n; i++) { int temp; cin >> temp; dp[i] = dp[i - 1]; if (before == temp) continue; before = temp; // if(count[temp].size()!=0){ /* for(auto iter=count[temp].begin();iter!=count[temp].end();iter++){ if(i-*iter>1){ dp[i]+=dp[*iter-1]; dp[i]%=MOD; } } */ dp[i] += count[temp]; dp[i] %= MOD; //} count[temp] += dp[i - 1]; count[temp] %= MOD; } cout << dp[n] << "\n"; }
replace
43
44
43
44
0
p03096
C++
Runtime Error
#if 1 #include <algorithm> #include <climits> #include <cmath> #include <functional> #include <iostream> #include <numeric> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define int long long constexpr int MOD = 1000000007; constexpr int INF = 1145141919810; #define LOADVEC(type, name, N) \ std::vector<type> name(N); \ for (int nnn = 0; nnn < N; ++nnn) { \ cin >> name[nnn]; \ } #define LOADVEC2(type, name0, name1, N) \ std::vector<type> name0(N), name1(N); \ for (int nnn = 0; nnn < N; ++nnn) { \ cin >> name0[nnn]; \ cin >> name1[nnn]; \ } #define LOAD(type, name) \ type name; \ cin >> name; signed main() { ios::sync_with_stdio(false); LOAD(int, N); LOADVEC(int, C0, N); std::vector<int> C; C.reserve(N); int last = -1; for (int i = 0; i < N; ++i) { if (last != C0[i]) C.push_back(C0[i]); last = C0[i]; } std::vector<int> res(C.size()); std::vector<int> lastColor(N, -1); int lastColor2 = -1; res[0] = 1; lastColor[C[0]] = 0; for (int i = 1; i < C.size(); ++i) { if (lastColor[C[i]] == -1) { res[i] = res[i - 1]; } else { res[i] = res[i - 1] + res[lastColor[C[i]]]; res[i] %= MOD; } lastColor[C[i]] = i; } cout << res.back(); return 0; } #endif
#if 1 #include <algorithm> #include <climits> #include <cmath> #include <functional> #include <iostream> #include <numeric> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define int long long constexpr int MOD = 1000000007; constexpr int INF = 1145141919810; #define LOADVEC(type, name, N) \ std::vector<type> name(N); \ for (int nnn = 0; nnn < N; ++nnn) { \ cin >> name[nnn]; \ } #define LOADVEC2(type, name0, name1, N) \ std::vector<type> name0(N), name1(N); \ for (int nnn = 0; nnn < N; ++nnn) { \ cin >> name0[nnn]; \ cin >> name1[nnn]; \ } #define LOAD(type, name) \ type name; \ cin >> name; signed main() { ios::sync_with_stdio(false); LOAD(int, N); LOADVEC(int, C0, N); std::vector<int> C; C.reserve(N); int last = -1; for (int i = 0; i < N; ++i) { if (last != C0[i]) C.push_back(C0[i]); last = C0[i]; } std::vector<int> res(C.size()); std::vector<int> lastColor(300000, -1); int lastColor2 = -1; res[0] = 1; lastColor[C[0]] = 0; for (int i = 1; i < C.size(); ++i) { if (lastColor[C[i]] == -1) { res[i] = res[i - 1]; } else { res[i] = res[i - 1] + res[lastColor[C[i]]]; res[i] %= MOD; } lastColor[C[i]] = i; } cout << res.back(); return 0; } #endif
replace
48
49
48
49
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int add(int &x, int y) { x += y; if (x >= mod) { x -= mod; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } a.erase(unique(a.begin(), a.end()), a.end()); n = (int)a.size(); vector<int> dp(n, 0); dp[a[0]] = 1; for (int i = 1; i < n; i++) { add(dp[a[i]], dp[a[i - 1]]); } cout << dp[a[n - 1]] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int add(int &x, int y) { x += y; if (x >= mod) { x -= mod; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } a.erase(unique(a.begin(), a.end()), a.end()); n = (int)a.size(); vector<int> dp((int)2e5, 0); dp[a[0]] = 1; for (int i = 1; i < n; i++) { add(dp[a[i]], dp[a[i - 1]]); } cout << dp[a[n - 1]] << '\n'; return 0; }
replace
24
25
24
25
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> #define Set(a, b) memset(a, b, sizeof(a)) using namespace std; const int N = 1e5 + 10; const int mod = 1e9 + 7; template <typename T> inline void init(T &x) { x = 0; char ch = getchar(); bool t = 0; for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') t = 1; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch - 48); if (t) x = -x; return; } typedef long long ll; typedef unsigned long long ull; template <typename T> inline void Inc(T &x, int y) { x += y; if (x >= mod) x -= mod; return; } template <typename T> inline void Dec(T &x, int y) { x -= y; if (x < 0) x += mod; return; } template <typename T> inline int fpow(int x, T k) { int ret = 1; for (; k; k >>= 1, x = (ll)x * x % mod) if (k & 1) ret = (ll)ret * x % mod; return ret; } int Sum(int x, int y) { x += y; if (x >= mod) return x - mod; return x; } int Dif(int x, int y) { x -= y; if (x < 0) return x + mod; return x; } int dp[N]; int C[N]; int n; int lst[N]; int main() { init(n); dp[1] = 1; for (int i = 1; i <= n; ++i) init(C[i]); lst[C[1]] = 1; for (int i = 2; i <= n; ++i) { if (C[i - 1] == C[i]) dp[i] = dp[i - 1]; else dp[i] = Sum(dp[i - 1], dp[lst[C[i]]]); lst[C[i]] = i; } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> #define Set(a, b) memset(a, b, sizeof(a)) using namespace std; const int N = 2e5 + 10; const int mod = 1e9 + 7; template <typename T> inline void init(T &x) { x = 0; char ch = getchar(); bool t = 0; for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') t = 1; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch - 48); if (t) x = -x; return; } typedef long long ll; typedef unsigned long long ull; template <typename T> inline void Inc(T &x, int y) { x += y; if (x >= mod) x -= mod; return; } template <typename T> inline void Dec(T &x, int y) { x -= y; if (x < 0) x += mod; return; } template <typename T> inline int fpow(int x, T k) { int ret = 1; for (; k; k >>= 1, x = (ll)x * x % mod) if (k & 1) ret = (ll)ret * x % mod; return ret; } int Sum(int x, int y) { x += y; if (x >= mod) return x - mod; return x; } int Dif(int x, int y) { x -= y; if (x < 0) return x + mod; return x; } int dp[N]; int C[N]; int n; int lst[N]; int main() { init(n); dp[1] = 1; for (int i = 1; i <= n; ++i) init(C[i]); lst[C[1]] = 1; for (int i = 2; i <= n; ++i) { if (C[i - 1] == C[i]) dp[i] = dp[i - 1]; else dp[i] = Sum(dp[i - 1], dp[lst[C[i]]]); lst[C[i]] = i; } cout << dp[n] << endl; return 0; }
replace
3
4
3
4
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #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 REPR(i, n) for (int i = n; i >= 0; i--) #define FOREACH(x, a) for (auto &(x) : (a)) #define VECCIN(x) \ for (auto &youso_ : (x)) \ cin >> youso_ #define mp make_pair #define bitcnt __builtin_popcount #define All(a) (a).begin(), (a).end() template <typename T = long long> inline T IN() { T x; cin >> x; return (x); } inline void CIN() {} template <class Head, class... Tail> inline void CIN(Head &&head, Tail &&...tail) { cin >> head; CIN(move(tail)...); } #define CINT(...) \ int __VA_ARGS__; \ CIN(__VA_ARGS__) #define LCIN(...) \ ll __VA_ARGS__; \ CIN(__VA_ARGS__) #define SCIN(...) \ string __VA_ARGS__; \ CIN(__VA_ARGS__) #define Yes(a) cout << (a ? "Yes" : "No") << "\n" #define YES(a) cout << (a ? "YES" : "NO") << "\n" typedef long long ll; typedef unsigned long long ul; typedef vector<int> V; typedef vector<ll> VL; typedef pair<int, int> P; typedef pair<ll, ll> PL; typedef priority_queue<int> PQ; typedef priority_queue<int, V, greater<int>> PQG; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int N; vector<V> cidx; V c; VL dp; int main() { cin >> N; c.resize(N); cidx.resize(N); REP(i, N) { CINT(C); c[i] = C; cidx[C].push_back(i); } dp.resize(N + 1); dp[0] = 1; REP(i, N) { (dp[i + 1] += dp[i]) %= MOD; int color = c[i]; int it = distance(cidx[color].begin(), lower_bound(All(cidx[color]), i)); if (it > 0 && i - cidx[color][it - 1] > 1) { (dp[i + 1] += dp[cidx[color][it - 1] + 1]) %= MOD; } } cout << dp[N] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #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 REPR(i, n) for (int i = n; i >= 0; i--) #define FOREACH(x, a) for (auto &(x) : (a)) #define VECCIN(x) \ for (auto &youso_ : (x)) \ cin >> youso_ #define mp make_pair #define bitcnt __builtin_popcount #define All(a) (a).begin(), (a).end() template <typename T = long long> inline T IN() { T x; cin >> x; return (x); } inline void CIN() {} template <class Head, class... Tail> inline void CIN(Head &&head, Tail &&...tail) { cin >> head; CIN(move(tail)...); } #define CINT(...) \ int __VA_ARGS__; \ CIN(__VA_ARGS__) #define LCIN(...) \ ll __VA_ARGS__; \ CIN(__VA_ARGS__) #define SCIN(...) \ string __VA_ARGS__; \ CIN(__VA_ARGS__) #define Yes(a) cout << (a ? "Yes" : "No") << "\n" #define YES(a) cout << (a ? "YES" : "NO") << "\n" typedef long long ll; typedef unsigned long long ul; typedef vector<int> V; typedef vector<ll> VL; typedef pair<int, int> P; typedef pair<ll, ll> PL; typedef priority_queue<int> PQ; typedef priority_queue<int, V, greater<int>> PQG; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int N; vector<V> cidx; V c; VL dp; int main() { cin >> N; c.resize(N); cidx.resize(int(2 * 10e5)); REP(i, N) { CINT(C); c[i] = C; cidx[C].push_back(i); } dp.resize(N + 1); dp[0] = 1; REP(i, N) { (dp[i + 1] += dp[i]) %= MOD; int color = c[i]; int it = distance(cidx[color].begin(), lower_bound(All(cidx[color]), i)); if (it > 0 && i - cidx[color][it - 1] > 1) { (dp[i + 1] += dp[cidx[color][it - 1] + 1]) %= MOD; } } cout << dp[N] << "\n"; return 0; }
replace
59
60
59
60
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i < n; i++) #define all(v) (v).begin(), (v).end() #define decimal fixed << setprecision(20) #define fastcin() \ cin.tie(0); \ ios::sync_with_stdio(false) using namespace std; using LL = long long; const int INF = 1e9; const LL LLINF = 1e16; template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } friend ostream &operator<<(ostream &os, const ModInt<mod> &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt<mod> &a) { long long x; is >> x; a = ModInt<mod>(x); return (is); } }; const int mod = 1e9 + 7; using modint = ModInt<mod>; int main() { int n; cin >> n; vector<int> c(1); cin >> c[0]; rep1(i, n) { int x; cin >> x; if (x != c[c.size() - 1]) c.push_back(x); } vector<modint> mk(100010, 0); modint ans = 1; rep(i, c.size()) { ans += mk[c[i]]; mk[c[i]] = ans; } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i < n; i++) #define all(v) (v).begin(), (v).end() #define decimal fixed << setprecision(20) #define fastcin() \ cin.tie(0); \ ios::sync_with_stdio(false) using namespace std; using LL = long long; const int INF = 1e9; const LL LLINF = 1e16; template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } friend ostream &operator<<(ostream &os, const ModInt<mod> &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt<mod> &a) { long long x; is >> x; a = ModInt<mod>(x); return (is); } }; const int mod = 1e9 + 7; using modint = ModInt<mod>; int main() { int n; cin >> n; vector<int> c(1); cin >> c[0]; rep1(i, n) { int x; cin >> x; if (x != c[c.size() - 1]) c.push_back(x); } vector<modint> mk(200010, 0); modint ans = 1; rep(i, c.size()) { ans += mk[c[i]]; mk[c[i]] = ans; } cout << ans << endl; }
replace
94
95
94
95
0
p03096
C++
Runtime Error
// #pragma GCC optimize ("-O3") #include <bits/stdc++.h> using namespace std; //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } __initon; // 衝突対策 #define ws ___ws //@必須構造 struct T { int f, s, t; T() { f = -1, s = -1, t = -1; } T(int f, int s, int t) : f(f), s(s), t(t) {} bool operator<(const T &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 T &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 T &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; } int operator[](int i) { assert(i < 3); return i == 0 ? f : i == 1 ? s : t; } }; struct F { int a, b, c, d; F() { a = -1, b = -1, c = -1, d = -1; } F(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {} bool operator<(const F &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 F &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 F &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } int operator[](int i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; T mt(int a, int b, int c) { return T(a, b, c); } //@マクロ省略系 型,構造 #define int long long #define ll long long #define double long double #define ull unsigned long long using dou = double; using itn = int; using str = string; using bo = bool; using P = pair<ll, ll>; #define fi first #define se second #define vec vector #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == // マクロ省略系 コンテナ using vi = vector<int>; #define _overloadvvi(_1, _2, _3, _4, name, ...) name #define vvi1(a) vec<vi> a #define vvi2(a, b) vec<vi> a(b) #define vvi3(a, b, c) vec<vi> a(b, vi(c)) #define vvi4(a, b, c, d) vec<vi> a(b, vi(c, d)) #define vvi(...) _overloadvvi(__VA_ARGS__, vvi4, vvi3, vvi2, vvi1)(__VA_ARGS__) using vl = vector<ll>; #define _overloadvvl(_1, _2, _3, _4, name, ...) name #define vvl1(a) vec<vl> a #define vvl2(a, b) vec<vl> a(b) #define vvl3(a, b, c) vec<vl> a(b, vl(c)) #define vvl4(a, b, c, d) vec<vl> a(b, vl(c, d)) #define vvl(...) _overloadvvl(__VA_ARGS__, vvl4, vvl3, vvl2, vvl1)(__VA_ARGS__) using vb = vector<bool>; #define _overloadvvb(_1, _2, _3, _4, name, ...) name #define vvb1(a) vec<vb> a #define vvb2(a, b) vec<vb> a(b) #define vvb3(a, b, c) vec<vb> a(b, vb(c)) #define vvb4(a, b, c, d) vec<vb> a(b, vb(c, d)) #define vvb(...) _overloadvvb(__VA_ARGS__, vvb4, vvb3, vvb2, vvb1)(__VA_ARGS__) using vs = vector<string>; #define _overloadvvs(_1, _2, _3, _4, name, ...) name #define vvs1(a) vec<vs> a #define vvs2(a, b) vec<vs> a(b) #define vvs3(a, b, c) vec<vs> a(b, vs(c)) #define vvs4(a, b, c, d) vec<vs> a(b, vs(c, d)) #define vvs(...) _overloadvvs(__VA_ARGS__, vvs4, vvs3, vvs2, vvs1)(__VA_ARGS__) using vd = vector<double>; #define _overloadvvd(_1, _2, _3, _4, name, ...) name #define vvd1(a) vec<vd> a #define vvd2(a, b) vec<vd> a(b) #define vvd3(a, b, c) vec<vd> a(b, vd(c)) #define vvd4(a, b, c, d) vec<vd> a(b, vd(c, d)) #define vvd(...) _overloadvvd(__VA_ARGS__, vvd4, vvd3, vvd2, vvd1)(__VA_ARGS__) using vc = vector<char>; #define _overloadvvc(_1, _2, _3, _4, name, ...) name #define vvc1(a) vec<vc> a #define vvc2(a, b) vec<vc> a(b) #define vvc3(a, b, c) vec<vc> a(b, vc(c)) #define vvc4(a, b, c, d) vec<vc> a(b, vc(c, d)) #define vvc(...) _overloadvvc(__VA_ARGS__, vvc4, vvc3, vvc2, vvc1)(__VA_ARGS__) using vp = vector<P>; #define _overloadvvp(_1, _2, _3, _4, name, ...) name #define vvp1(a) vec<vp> a #define vvp2(a, b) vec<vp> a(b) #define vvp3(a, b, c) vec<vp> a(b, vp(c)) #define vvp4(a, b, c, d) vec<vp> a(b, vp(c, d)) using vt = vector<T>; #define _overloadvvt(_1, _2, _3, _4, name, ...) name #define vvt1(a) vec<vt> a #define vvt2(a, b) vec<vt> a(b) #define vvt3(a, b, c) vec<vt> a(b, vt(c)) #define vvt4(a, b, c, d) vec<vt> a(b, vt(c, d)) #define v3i(a, b, c, d) vector<vector<vi>> a(b, vector<vi>(c, vi(d))) #define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d))) #define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d))) #define _vvi vector<vi> #define _vvl vector<vl> #define _vvb vector<vb> #define _vvs vector<vs> #define _vvd vector<vd> #define _vvc vector<vc> #define _vvp vector<vp> #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<int, int>; using mapd = map<dou, int>; using mapc = map<char, int>; using maps = map<str, int>; using seti = set<int>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<int>; #define bset bitset #define uset unordered_set #define mset multiset #define umap unordered_map #define umapi unordered_map<int, int> #define umapp unordered_map<P, int> #define mmap multimap // マクロ 繰り返し #define _overloadrep(_1, _2, _3, _4, name, ...) name #define _rep(i, n) for (int i = 0, _lim = n; i < _lim; i++) #define repi(i, m, n) for (int i = m, _lim = n; i < _lim; i++) #define repadd(i, m, n, ad) for (int i = m, _lim = n; i < _lim; i += ad) #define rep(...) _overloadrep(__VA_ARGS__, repadd, repi, _rep, )(__VA_ARGS__) #define _rer(i, n) for (int i = n; i >= 0; i--) #define reri(i, m, n) for (int i = m, _lim = n; i >= _lim; i--) #define rerdec(i, m, n, dec) for (int i = m, _lim = n; i >= _lim; i -= dec) #define rer(...) _overloadrep(__VA_ARGS__, rerdec, reri, _rer, )(__VA_ARGS__) #define fora(a, b) for (auto &&a : b) #define forg(gi, ve) \ for (int gi = 0, f, t, c; gi < ve.size() && (f = ve[gi].from, t = ve[gi].to, \ c = ve[gi].cost, true); \ gi++) #define fort(gi, ve) \ for (int gi = 0, f, t, c; \ gi < ve.size() && (gi += (ve[gi].to == p)) < ve.size() && \ (f = ve[gi].from, t = ve[gi].to, c = ve[gi].cost, true); \ gi++) // マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const int inf = (int)1e9 + 100; const ll linf = (ll)1e18 + 100; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; ll ma = numeric_limits<ll>::min(); ll mi = numeric_limits<ll>::max(); const int y4[] = {-1, 1, 0, 0}; const int x4[] = {0, 0, -1, 1}; const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; // マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((int)(a).size()) #define rs resize #define mp make_pair #define pb push_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() //@拡張系 こう出来るべきというもの // 埋め込み 存在を意識せずに機能を増やされているもの 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 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; } ostream &operator<<(ostream &os, P p) { os << p.fi << " " << p.se << endl; 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 (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < vec[0].size(); j++) { os << vec[i][j]; } os << endl; } return os; } template <typename V, typename H> void resize(vector<V> &vec, const H head) { vec.resize(head); } template <typename V, typename H, typename... T> void resize(vector<V> &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } template <typename T, typename _Pr> bool all_of(const vector<T> &vec, _Pr pred) { return std::all_of(vec.begin(), vec.end(), pred); } template <typename T, typename _Pr> bool any_of(const vector<T> &vec, _Pr pred) { return std::any_of(vec.begin(), vec.end(), pred); } template <typename T, typename _Pr> bool none_of(const vector<T> &vec, _Pr pred) { return std::none_of(vec.begin(), vec.end(), pred); } template <typename T, typename _Pr> const typename vector<T>::const_iterator find_if(const vector<T> &vec, _Pr pred) { return std::find_if(vec.begin(), vec.end(), pred); } template <typename T> bool contains(const vector<T> &vec, const T &val) { return std::find(vec.begin(), vec.end(), val) != vec.end(); } template <typename T, typename _Pr> bool contains_if(const vector<T> &vec, _Pr pred) { return std::find_if(vec.begin(), vec.end(), pred) != vec.end(); } template <class T> void replace(vector<T> &a, T key, T v) { replace(a.begin(), a.end(), key, v); } 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)); } template <class T> T pop(set<T> &set) { T res = *set.begin(); set.erase(set.find(res)); return res; } template <class T> T pop(mset<T> &set) { T res = *set.begin(); set.erase(set.find(res)); return res; } template <class T> T popBack(set<T> &set) { T res = *set.rbegin(); set.erase(set.find(res)); return res; } template <class T> T popBack(mset<T> &set) { T res = *set.rbegin(); set.erase(set.find(res)); return res; } inline void sort(string &a) { sort(a.begin(), a.end()); } template <class T> inline void sort(vector<T> &a) { sort(a.begin(), a.end()); }; template <class T> inline void sort(vector<T> &a, int len) { sort(a.begin(), a.begin() + len); }; template <class T, class F> inline void sort(vector<T> &a, F f) { sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); }); }; enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; inline void sort(vector<P> &a, ___pcomparator type) { switch (type) { case fisi: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); break; case fisd: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); break; case fdsi: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); break; case fdsd: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); break; case sifi: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); break; case sifd: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); break; case sdfi: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); break; case sdfd: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); break; } }; template <class T> inline void rsort(vector<T> &a) { sort(a.begin(), a.end(), greater<T>()); }; template <class T> inline void rsort(vector<T> &a, int len) { sort(a.begin(), a.begin() + len, greater<T>()); }; template <class U, class F> inline void rsort(vector<U> &a, F f) { sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); }); }; template <class U> inline void sortp(vector<U> &a, vector<U> &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n) c.eb(a[i], b[i]); sort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; ; } }; // F = T<T> // 例えばreturn p.fi + p.se; template <class U, class F> inline void sortp(vector<U> &a, vector<U> &b, F f) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n) c.eb(a[i], b[i]); sort(c, f); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template <class U, class F> inline void sortp(vector<U> &a, vector<U> &b, char type) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n) c.eb(a[i], b[i]); sort(c, type); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template <class U> inline void rsortp(vector<U> &a, vector<U> &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n) c.eb(a[i], b[i]); rsort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template <class U, class F> inline void rsortp(vector<U> &a, vector<U> &b, F f) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n) c.eb(a[i], b[i]); rsort(c, f); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template <class U> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n) r.eb(a[i], b[i], c[i]); sort(r); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template <class U, class F> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n) r.eb(a[i], b[i], c[i]); sort(r, f); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template <class U, class F> inline void rsortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n) r.eb(a[i], b[i], c[i]); rsort(r, f); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template <class T> inline void sort2(vector<vector<T>> &a) { for (int i = 0, n = a.size(); i < n; i++) sort(a[i]); } template <class T> inline void rsort2(vector<vector<T>> &a) { for (int 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 V, typename T> void fill(V &xx, const T vall) { xx = vall; } template <typename V, typename T> void fill(vector<V> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } //@汎用便利関数 入力 template <typename T = int> T in() { T x; cin >> x; return (x); } string sin() { return in<string>(); } double din() { return in<double>(); } ll lin() { return in<ll>(); } #define na(a, n) \ a.resize(n); \ rep(i, n) cin >> a[i]; #define nao(a, n) \ a.resize(n + 1); \ rep(i, n) cin >> a[i + 1]; #define nad(a, n) \ a.resize(n); \ rep(i, n) cin >> a[i], a[i]--; #define na2(a, b, n) \ a.resize(n), b.resize(n); \ rep(i, n) cin >> a[i] >> b[i]; #define na2d(a, b, n) \ a.resize(n), b.resize(n); \ rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; #define na3(a, b, c, n) \ a.resize(n), b.resize(n), c.resize(n); \ rep(i, n) cin >> a[i] >> b[i] >> c[i]; #define na3d(a, b, c, 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 nt(a, h, w) \ resize(a, h, w); \ rep(hi, h) rep(wi, w) cin >> a[hi][wi]; #define ntd(a, h, w) \ rs(a, h, w); \ rep(hi, h) rep(wi, w) cin >> a[hi][wi], a[hi][wi]--; #define ntp(a, h, w) \ fill(a, '#'); \ rep(hi, 1, h + 1) rep(wi, 1, w + 1) cin >> a[hi][wi]; // 汎用便利関数 出力 template <class T> void out(T x) { typeid(x) == typeid(double) ? cout << fixed << setprecision(10) << x << endl : cout << x << endl; } template <class T> void out(vector<T> a) { fora(v, a) cout << v << endl; } // デバッグ #define sp << " " << #define debugName(VariableName) #VariableName #define _deb1(x) cerr << debugName(x) << " = " << x << endl #define _deb2(x, y) \ cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \ << endl #define _deb3(x, y, z) \ cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \ << ", " debugName(z) << " = " << z << endl #define _deb4(x, y, z, a) \ cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \ << ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \ << a << endl #define _deb5(x, y, z, a, b) \ cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \ << ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \ << a << ", " << debugName(b) << " = " << b << endl #define _overloadebug(_1, _2, _3, _4, _5, name, ...) name #define debug(...) \ _overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__) #define deb(...) \ _overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__) #define debugline(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n' // よく使うクラス、構造体 class UnionFind { public: vi par, rank, sizes; int n, trees; UnionFind(int n) : n(n), trees(n) { par.resize(n), rank.resize(n), sizes.resize(n); rep(i, n) par[i] = i, sizes[i] = 1; } int root(int x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } int find(int x) { return root(x); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); trees--; par[y] = x; sizes[x] += sizes[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return sizes[root(x)]; } // 順不同 umapなので vec<vi> sets() { vec<vi> res(trees); umap<int, vi> map; rep(i, n) map[root(i)].push_back(i); int i = 0; for (auto &&p : map) { int r = p.fi; res[i].push_back(r); for (auto &&v : p.se) { if (r == v) continue; res[i].push_back(v); } i++; } return res; } }; // MOD関連 ll MOD = (int)1e9 + 7; class mint { public: int x; mint() : x(0) {} mint(signed y) : x(y >= 0 ? y % MOD : MOD - (-y) % MOD) {} mint(int y) : x(y >= 0 ? y % MOD : MOD - (-y) % MOD) {} static int _mpow(int v, ll a) { ll x = v, n = a, res = 1; while (n) { if (n & 1) res = (res * x) % MOD; x = (x * x) % MOD; n >>= 1; } return res; } // Arithmetic Oprators mint &operator+=(mint that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } mint &operator-=(mint that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } mint &operator*=(mint that) { x = 1LL * x * that.x % MOD; return *this; } mint &operator/=(const mint &that); mint &operator^=(const mint &that) { this->x = _mpow(x, that.x); return *this; } mint &operator%=(mint that) { x %= that.x; return *this; } mint &operator+=(const int that) { return *this += mint(that); } mint &operator-=(const int that) { return *this -= mint(that); } mint &operator*=(const int that) { return *this *= mint(that); } mint &operator/=(const int that); mint &operator^=(const int that) { this->x = _mpow(x, that); return *this; } mint &operator%=(const int that) { return *this %= mint(that); } mint &operator+=(const signed that) { return *this += mint(that); } mint &operator-=(const signed that) { return *this -= mint(that); } mint &operator*=(const signed that) { return *this *= mint(that); } mint &operator/=(const signed that); mint &operator^=(const signed that) { this->x = _mpow(x, that); return *this; } mint &operator%=(const signed that) { return *this %= mint(that); } // Comparators bool operator<(mint that) { return x < that.x; } bool operator>(mint that) { return x > that.x; } bool operator<=(mint that) { return x <= that.x; } bool operator>=(mint that) { return x >= that.x; } bool operator!=(mint that) { return x != that.x; } bool operator==(mint that) { return x == that.x; } bool operator!=(int that) { return x != that; } bool operator==(int that) { return x == that; } bool operator!=(signed that) { return x != that; } bool operator==(signed that) { return x == that; } // Utilities unsigned getval() const { return x; } operator int() { return x; } mint operator+(mint that) const { return mint(*this) += that; } mint operator-(mint that) const { return mint(*this) -= that; } mint operator*(mint that) const { return mint(*this) *= that; } mint operator%(mint that) const { return mint(*this) %= that; } mint operator+(const int that) const { return mint(*this) += that; } mint operator-(const int that) const { return mint(*this) -= that; } mint operator*(const int that) const { return mint(*this) *= that; } mint operator%(const int that) const { return mint(*this) %= that; } mint operator=(const int that) { return *this = mint(that); } mint operator+(const signed that) const { return mint(*this) += that; } mint operator-(const signed that) const { return mint(*this) -= that; } mint operator*(const signed that) const { return mint(*this) *= that; } mint operator%(const signed that) const { return mint(*this) %= that; } mint operator=(const signed that) { return *this = mint(that); } mint operator++() { x++; return *this; } mint operator++(signed) { auto ret = *this; x++; return ret; } friend void operator+=(ll &a, const mint &b) { a = mint(a % MOD + b.x); } friend void operator-=(ll &a, const mint &b) { a = mint(a % MOD - b.x); } friend void operator*=(ll &a, const mint &b) { a = mint(a % MOD * b.x); } friend void operator/=(ll &a, const mint &b); friend mint operator+(const ll a, const mint &b) { return mint(a % MOD + b.x); } friend mint operator-(const ll a, const mint &b) { return mint(a % MOD - b.x); } friend mint operator*(const ll a, const mint &b) { return mint(a % MOD * b.x); } friend mint operator^(const ll a, const mint &b) { return _mpow(a, b.x); } }; mint itom(int v) { mint res; res.x = v; return res; } const int setModMax = 510000; vector<mint> fac, finv, inv; void setMod(int m = MOD) { fac.resize(setModMax); finv.resize(setModMax); inv.resize(setModMax); MOD = m; fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < setModMax; i++) { fac[i] = fac[i - 1].x * i % MOD; inv[i] = MOD - inv[MOD % i].x * (MOD / i) % MOD; finv[i] = finv[i - 1].x * inv[i].x % MOD; } } mint mpow(int v, ll a) { return mint::_mpow(v, a); } mint com(ll n, ll r) { if (n < r || n < 0 || r < 0) return 0; if (fac.size() == 0) setMod(); return fac[n] * finv[r] * finv[n - r]; } mint ncr(ll n, ll r) { return com(n, r); } // n人にr個を配るとき、同じ人に何個配っても良い場合 mint nhr(ll n, ll r) { return com(n + r - 1, r); } // 拡張ユークリッドの互除法 mint minv(ll a) { if (fac[0] == 0) setMod(); if (a < setModMax) return inv[a]; a %= MOD; ll b = MOD, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return x; } mint &mint::operator/=(const mint &that) { return *this *= minv(that.x); } mint &mint::operator/=(const ll a) { return *this *= minv(a); } mint &mint::operator/=(const signed a) { return *this *= minv(a); } void operator/=(ll &a, const mint &b) { a = (a * minv(b.x)).x; } using PM = pair<mint, mint>; using vm = vector<mint>; #define _overloadvvm(_1, _2, _3, _4, name, ...) name #define vvm1(a) vec<vm> a #define vvm2(a, b) vec<vm> a(b) #define vvm3(a, b, c) vec<vm> a(b, vm(c)) #define vvm4(a, b, c, d) vec<vm> a(b, vm(c, d)) #define vvm(...) _overloadvvm(__VA_ARGS__, vvm4, vvm3, vvm2, vvm1)(__VA_ARGS__) vb isPrime; vi primes; void setPrime() { isPrime.resize(4010101); fill(isPrime, true); int len = sizeof(isPrime) / sizeof(isPrime[0]); isPrime[0] = isPrime[1] = false; for (int i = 2; i <= sqrt(len) + 5; ++i) { if (!isPrime[i]) continue; for (int j = 2; i * j < len; ++j) { isPrime[i * j] = false; } } rep(i, len) if (isPrime[i]) primes.pb(i); } // 幾何 Pをcomplexとして扱う bool eq(double a, double b) { return fabs(a - b) < eps; } using C = complex<double>; C rot(C &a, dou th) { return a * C(cos(th), sin(th)); } dou inpro(C &a, C &b) { return real(a * conj(b)); } // 90度回転させて内積が0なら平行 bool line(C a, C b, C c) { C ab = b - a; C ac = c - a; // 複素数の掛け算は回転 ab *= C(0, 1); return eq(inpro(ab, ac), 0); } bool line(P a, P b, P c) { return line(C(a.fi, a.se), C(b.fi, b.se), C(c.fi, c.se)); } bool line(int xa, int ya, int xb, int yb, int xc, int yc) { C a = C(xa, ya); C b = C(xb, yb); C c = C(xc, yc); return line(a, b, c); } // 便利関数 // テスト用 char ranc() { return (char)('a' + rand() % 26); } int rand(int min, int 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(int n, int min, int max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } // 単調増加 vi ranvi(int n, int min, int 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(int n, int min, int 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]); } } // 便利 汎用 template <typename V, typename T> int find(vector<V> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename V, typename T> P find(vector<vector<V>> &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 V, typename U> T find(vector<vector<vector<V>>> &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); } template <typename V, typename T> int count(V &a, const T k) { return a == k; } template <typename V, typename T> int count(vector<V> &a, const T k) { int ret = 0; fora(v, a) ret += count(v, k); return ret; } template <typename V> int count_odd(V &a) { return a % 2; } template <typename V> int count_odd(vector<V> &a) { int ret = 0; fora(v, a) ret += count_odd(v); return ret; } template <typename V> int count_even(V &a) { return a % 2 == 0; } template <typename V> int count_even(vector<V> &a) { int ret = 0; fora(v, a) ret += count_even(v); return ret; } // algorythm void iota(vector<int> &ve, int s, int n) { ve.resize(n); iota(all(ve), s); } vi iota(int s, int n) { vi ve(n); iota(all(ve), s); return ve; } // 便利 数学 int mod(int a, int m) { return (a % m + m) % m; } int pow(int a) { return a * a; }; ll fact(int v) { return v <= 1 ? 1 : v * fact(v - 1); } ll comi(int n, int 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]; } 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 tle() { while (inf) cout << inf << endl; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll gcd(vi b) { ll res = b[0]; for (auto &&v : b) res = gcd(v, res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template <class T> vector<T> rev(vector<T> &a) { vector<T> ret = a; reverse(all(ret)); return ret; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else return (a + b - 1) / b; } 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 / 2; } ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; } vi divisors(int v) { vi res; for (int i = 1; i <= sqrt(v); ++i) { if (v % i == 0) { res.pb(i); if (i != v / i) res.pb(v / i); } } return res; } vi factorization(int v) { int tv = v; vi res; if (isPrime.size() == 0) setPrime(); for (auto &&p : primes) { if (v % p == 0) res.push_back(p); while (v % p == 0) { v /= p; } if (v == 1 || p * p > tv) break; } if (v > 1) res.pb(v); return res; } unordered_map<int, int> factorizationMap(int v) { int tv = v; unordered_map<int, int> res; if (isPrime.size() == 0) setPrime(); for (auto &&p : primes) { while (v % p == 0) { res[p]++; v /= p; } if (v == 1 || p * p > tv) break; } if (v > 1) res[v]++; return res; } int get(int a, int keta) { return (a / (int)pow(10, keta)) % 10; } int keta(int v) { int cou = 0; while (v) { cou++, v %= 10; } return cou; } int dsum(int v) { int ret = 0; for (; v; v /= 10) ret += v % 10; return ret; } int sumd(int v) { return dsum(v); } // 変換系 template <class T, class U> vector<T> keys(vector<pair<T, U>> a) { vector<T> res; for (auto &&k : a) res.pb(k.fi); return res; } template <class T, class U> vector<U> keys(map<T, U> a) { vector<U> res; for (auto &&k : a) res.pb(k.fi); return res; } template <class T, class U> vector<U> keys(umap<T, U> a) { vector<U> res; for (auto &&k : a) res.pb(k.fi); return res; } template <class T, class U> vector<U> values(vector<pair<T, U>> a) { vector<U> res; for (auto &&k : a) res.pb(k.se); return res; } template <class T, class U> vector<T> values(map<T, U> a) { vector<T> res; for (auto &&k : a) res.pb(k.se); return res; } template <class T, class U> vector<T> values(umap<T, U> a) { vector<T> res; for (auto &&k : a) res.pb(k.se); return res; } vi list(int a) { vi res; while (a) { res.insert(res.begin(), a % 10); a /= 10; } return res; } template <class T, class U> bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class U> bool chmax(const U &b) { return chmax(ma, b); } template <class T, class U> bool chmin(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class U> bool chmin(const U &b) { return chmin(mi, b); } #define chmi chmin #define chma chmax 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 min(vector<T> a, int n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> a, int s, int 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> T max(vector<T> a, int n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> a, int s, int 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, max(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, int len = -1) { if (len == -1) len = v.size(); T res = 0; chmin(len, v.size()); rep(i, len) res += v[i]; return res; } template <class T> T sum(vector<vector<T>> &v, int h = -1, int w = -1) { if (h == -1) h = v.size(); if (w == -1) w = v[0].size(); T res = 0; chmin(h, v.size()); chmin(w, v[0].size()); rep(i, h) rep(j, w) res += v[i][j]; return res; } P sump(vp &v, int len = -1) { if (len == -1) len = v.size(); P res = {0, 0}; chmin(len, v.size()); rep(i, len) { res.fi += v[i].fi; res.se += v[i].se; } return res; } /// 要素が0の時、返り値は0か1か template <class T> T mul(vector<T> &v, int len = -1) { if (len == -1) len = v.size(); T res = 1; chmin(len, v.size()); rep(i, len) res *= v[i]; return res; } void clear(PQ &q) { while (q.size()) q.pop(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(int size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(int h, int w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (int i = 1; i <= 2 * h + 1; i++) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; } // 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; } template <class T> vector<T> imomi(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chmin(ret[i + 1], ret[i]); return ret; } template <class T> struct ruic { const vector<T> rui; ruic(vector<T> &ru) : rui(ru) {} T operator()(int l, int r) { assert(l <= r); return rui[r] - rui[l]; } T operator[](int i) { return rui[i]; } }; template <class T> struct rruic { const T *rrui; rruic(T *ru) : rrui(ru) {} // n-1から-1へ T operator()(int l, int r) { assert(l >= r); return rrui[r] - rrui[l]; } T operator[](int i) { return rrui[i]; } }; // template<class T> vector<T> ruiv(vector<T> &a) template <class T> ruic<T> ruiv(vector<T> &a) { vector<T> res(a.size() + 1); rep(i, a.size()) res[i + 1] = res[i] + a[i]; return ruic<T>(res); } 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; } // template<class T> T *rrui(vector<T> &a) { // 右から左にかけての半開区間 (-1 n-1] template <class T> rruic<T> rrui(vector<T> &a) { int 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) { int 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> void inc(vector<T> &a) { for (auto &u : a) inc(u, 1); } 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 T> void dec(vector<T> &a) { for (auto &u : a) dec(u, 1); } inline bool inside(int h, int w, int H, int W) { return h >= 0 && w >= 0 && h < H && w < W; } 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() ll goldd(ll left, ll right, function<ll(ll)> 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; } ll goldt(ll left, ll right, function<ll(ll)> 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; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmin(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()) chmax(res, *max_element(all(a[i]))); return res; } bool bget(ll m, int keta) { return (m >> keta) & 1; } int bget(ll m, int keta, int sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } inline ll bit(int n) { return (1LL << (n)); } inline ll bit(int n, int sinsuu) { return (ll)pow(sinsuu, n); } int bcou(ll m) { return __builtin_popcount(m & 0xFFFFFFFF) + __builtin_popcount(m >> 32); } // 初期化は0を渡す ll nextComb(ll &mask, int n, int 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を返す vl bitCombList(int n, int r) { vl res; int m = 0; while (nextComb(m, n, r)) { res.pb(m); } return res; } // 大文字小文字を区別する int altoiaZ(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a' + 26; } char itoalaZ(int i) { if (i < 26) return 'A' + i; return 'a' + i - 26; } // aもAも0を返す 基本小文字 int altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } char itoal(int i) { return 'a' + i; } int ctoi(char c) { return c - '0'; } char itoc(int i) { return i + '0'; } int vtoi(vi &v) { int 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(int i) { vi res; while (i) { res.pb(i % 10); i /= 10; } rev(res); return res; } vector<vector<int>> ctoi(vector<vector<char>> s, char c) { int n = sz(s), m = sz(s[0]); vector<vector<int>> res(n, vector<int>(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()); void compress(vi &a) { vi b; int len = a.size(); for (int i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (int i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } void compress(int a[], int len) { vi b; for (int i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (int 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 ans(a) \ cout << a << endl; \ continue; #define poll(a) \ q.front(); \ q.pop() #define dpoll(a) \ q.front(); \ q.pop_front() #define pollLast(a) \ q.back(); \ q.pop_back() #define pollBack(a) \ q.back(); \ q.pop_back() template <class T> inline void fin(T s) { cout << s << endl, exit(0); } template <class T> struct edge { int from, to; T cost; int id; int type; edge(int f, int t, T c = 1, int id = -1, int ty = -1) : from(f), to(t), cost(c), id(id), type(ty) {} bool operator<(const edge &b) const { return cost < b.cost; } bool operator>(const edge &b) const { return cost > b.cost; } }; template <typename T> class graph { protected: vector<bool> _used; public: vector<vector<edge<T>>> g; vector<edge<T>> edges; int n; graph(int n) : n(n) { g.resize(n), _used.resize(n); } void clear() { g.clear(), edges.clear(); } void resize(int n) { this->n = n; g.resize(n); _used.resize(n); } int size() { return g.size(); } vector<edge<T>> &operator[](int i) { return g[i]; } virtual void add(int from, int to, T cost, int id, int ty) = 0; virtual bool used(edge<T> &e) = 0; virtual bool used(int id) = 0; virtual void del(edge<T> &e) = 0; virtual void del(int id) = 0; }; template <typename T = ll> class digraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; using graph<T>::_used; digraph(int n) : graph<T>(n) {} void add(int f, int t, T cost = 1, int id = -1, int ty = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digraph add"); deb(f, t, cost, id, ty); ole(); } if (id == -1) id = edges.size(); g[f].emplace_back(f, t, cost, id, ty); edges.emplace_back(f, t, cost, id, ty); } bool used(edge<T> &e) { return _used[e.id]; } bool used(int id) { return _used[id]; } void del(edge<T> &e) { _used[e.id] = _used[e.id ^ 1] = 1; } void del(int id) { _used[id] = _used[id ^ 1] = 1; } }; template <class T = int> class undigraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; using graph<T>::_used; undigraph(int n) : graph<T>(n) {} void add(int f, int t, T cost = 1, int id = -1, int ty = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("undigraph add"); deb(f, t, cost, id, ty); ole(); } if (id == -1) id = edges.size(); g[f].emplace_back(f, t, cost, id, ty); g[t].emplace_back(t, f, cost, id + 1, ty); edges.emplace_back(f, t, cost, id, ty); edges.emplace_back(t, f, cost, id + 1, ty); } void add(edge<T> &e) { int f = e.from, t = e.to, ty = e.type; T cost = e.cost; add(f, t, cost, ty); } bool used(edge<T> &e) { return _used[e.id]; } bool used(int id) { return _used[id]; } void del(edge<T> &e) { _used[e.id] = _used[e.id ^ 1] = 1; } void del(int id) { _used[id] = _used[id ^ 1] = 1; } }; template <class T> vector<T> dijkstra(const graph<T> &g, int s, int cant_arrive = -1) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } T initValue = MAX(T); vector<T> dis(g.n, initValue); priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; dis[s] = 0; q.emplace(0, s); while (q.size()) { T nowc = q.top().fi; int i = q.top().se; q.pop(); if (dis[i] != nowc) continue; for (auto &&e : g.g[i]) { int to = e.to; T cost = nowc + e.cost; if (dis[to] > cost) { dis[to] = cost; q.emplace(dis[to], to); } } } // 基本、たどり着かないなら-1 if (cant_arrive == -1) for (auto &&d : dis) if (d == initValue) d = -1; return dis; } template <class T> vector<vector<T>> warshall(const graph<T> &g, int cant_arrive = -1) { int n = g.n; vector<vector<T>> dis(n, vector<T>(n, linf)); fora(e, g.edges) chmin(dis[e.from][e.to], e.cost); rep(i, n) dis[i][i] = 0; rep(k, n) rep(i, n) rep(j, n) chmin(dis[i][j], dis[i][k] + dis[k][j]); // 基本、たどり着かないなら-1 if (cant_arrive == -1) rep(i, n) rep(j, n) if (dis[i][j] == linf) dis[i][j] = -1; return dis; } template <class T = int> class tree : public undigraph<T> { public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; using undigraph<T>::_used; vi dep; vi disv; private: bool never = 1; int root = -1; vector<vector<int>> par; bool costallone; void dfs(int v, int p, int d) { dep[v] = d; par[0][v] = p; int lim = (*this)[v].size(); for (int i = 0; i < lim; i++) { int t = g[v][i].to; if (t == p) con; dfs(t, v, d + 1); } } void built() { never = 0; n = g.size(); par.assign(30, vi(n)); dep.resize(n); costallone = 1; fora(e, edges) if (e.cost != 1) costallone = 0; dfs(root, -1, 0); rep(k, par.size() - 1) { rep(i, n) { if (par[k][i] == -1) par[k + 1][i] = -1; else par[k + 1][i] = par[k][par[k][i]]; } } if (costallone) disv = dep; else disv = dijkstra(*this, root); } int _lca(int u, int v) { if (dep[u] > dep[v]) swap(u, v); rep(k, par.size()) { if ((dep[u] - dep[v]) >> k & 1) { v = par[k][v]; } } if (u == v) return u; rer(k, par.size() - 1) { if (par[k][u] != par[k][v]) { u = par[k][u]; v = par[k][v]; } } return par[0][u]; } int _dis(int u, int v) { int p = _lca(u, v); return disv[u] + disv[v] - disv[p] * 2; } public: tree(int n, int root = 0) : undigraph<T>(n), root(root) {} bool leaf(int v) { return sz(g[v]) == 1 && v != root; } int dis(int u, int v) { if (never) { built(); } return _dis(u, v); } int lca(int u, int v) { if (never) { built(); } return _lca(u, v); } }; // 辺によりメモリを大量消費 // よってedgesを消している // 頂点10^6でメモリを190MB(制限の8割)使う // 軽量化のため、辺を持たないbig gridクラスがあってもいいかもしれない // template <class T = int> class grid_k6 : public undigraph<T> { public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; using undigraph<T>::_used; int H, W; int eid = 0; void add(int f, int t, T cost = 1, int id = -1, int ty = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("grid_k6 add"); deb(f, t, cost, id, ty); ole(); } g[f].emplace_back(f, t, cost, eid++, ty); g[t].emplace_back(t, f, cost, eid++, ty); } int getid(int h, int w) { if (!inside(h, w, H, W)) return -1; return W * h + w; } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } grid_k6(int H, int W) : H(H), W(W), undigraph<T>(H * W) { rep(h, H) { rep(w, W) { int f = getid(h, w); if (w + 1 < W) add(f, getid(h, w + 1)); if (h + 1 < H) add(f, getid(h + 1, w)); } } } grid_k6(_vvc ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall) con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } void add(int fh, int fw, int th, int tw) { add(getid(fh, fw), getid(th, tw)); } }; // 左上から右下に移動できる template <class T = int> class digrid_k6 : public digraph<T> { public: using digraph<T>::g; using digraph<T>::n; using digraph<T>::edges; using digraph<T>::_used; int H, W; int eid = 0; void add(int f, int t, T cost = 1, int id = -1, int ty = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digrid_k6 add"); deb(f, t, cost, id, ty); ole(); } g[f].emplace_back(f, t, cost, eid++, ty); } int getid(int h, int w) { if (!inside(h, w, H, W)) return -1; return W * h + w; } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } digrid_k6(int H, int W) : H(H), W(W), digraph<T>(H * W) { rep(h, H) { rep(w, W) { int f = getid(h, w); if (w + 1 < W) add(f, getid(h, w + 1)); if (h + 1 < H) add(f, getid(h + 1, w)); } } } digrid_k6(_vvc ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), digraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall) con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } void add(int fh, int fw, int th, int tw) { add(getid(fh, fw), getid(th, tw)); } }; template <class T> bool nibu(const graph<T> &g) { if (g.edges.size() == 0) return true; UnionFind uf(g.n * 2); for (auto &&e : g.edges) uf.unite(e.from, e.to + g.n), uf.unite(e.from + g.n, e.to); rep(i, g.n) if (uf.same(i, i + g.n)) return 0; return 1; } // 二部グラフを色分けした際の頂点数を返す template <class T> vp nibug(graph<T> &g) { vp cg; if (!nibu(g)) { debugline("nibu"); ole(); } int _n = g.size(); vb _was(_n); queue<P> q; rep(i, _n) { if (_was[i]) continue; q.push(mp(i, 1)); _was[i] = 1; int red = 0; int coun = 0; while (q.size()) { int now = q.front().fi; int col = q.front().se; red += col; coun++; q.pop(); forg(gi, g[now]) { if (_was[t]) continue; q.push(mp(t, col ^ 1)); _was[t] = 1; } } cg.push_back(mp(red, coun - red)); } return cg; } // 機能拡張 vp vtop(vi &a, vi &b) { vp res(sz(a)); rep(i, sz(a)) res[i] = mp(a[i], b[i]); return res; } void ptov(vp &p, vi &a, vi &b) { a.resize(sz(p)); b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].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 <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } 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, class U> set<T> &operator+=(set<T> &a, U v) { a.insert(v); return a; } 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.pb(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) { a.pb(b); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, initializer_list<U> v) { for (auto &&va : v) a.pb(va); 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(); } 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; rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <typename T> void remove(vector<T> &v, unsigned int i) { v.erase(v.begin() + i); } template <typename T> void remove(vector<T> &v, unsigned int s, unsigned int e) { v.erase(v.begin() + s, v.begin() + e); } template <typename T> void removen(vector<T> &v, unsigned int s, unsigned int n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T> void erase(vector<T> &v, unsigned int i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned int s, unsigned int e) { v.erase(v.begin() + s, v.begin() + e); } template <typename T> void erasen(vector<T> &v, unsigned int s, unsigned int n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned int 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 int i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T, typename U> void insert(vector<T> &v, initializer_list<U> list) { for (auto &&va : list) v.pb(va); } template <typename T, typename U> void insert(vector<T> &v, unsigned int i, initializer_list<U> 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); } template <typename T> void insert(set<T> &v, initializer_list<T> list) { for (auto &&va : list) v.insert(va); } // 閉路がなければtrue bool topo(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n) for (auto &&e : g[i]) nyu[e.to]++; queue<int> st; rep(i, n) if (nyu[i] == 0) st.push(i); while (st.size()) { int v = st.front(); st.pop(); res.pb(v); fora(e, g[v]) if (--nyu[e.to] == 0) st.push(e.to); } return res.size() == n; } // 辞書順最小トポロジカルソート bool topos(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n) for (auto &&e : g[i]) nyu[e.to]++; // 小さい順 priority_queue<int, vector<int>, greater<int>> q; rep(i, n) if (nyu[i] == 0) q.push(i); while (q.size()) { int i = q.top(); q.pop(); res.pb(i); fora(e, g[i]) if (--nyu[e.to] == 0) q.push(e.to); } return res.size() == n; } vector<string> split(const string a, const char deli) { string b = a + deli; int 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) { string b = a + deli; int l = 0, r = 0, n = b.size(), dn = deli.size(); vector<string> res; rep(i, n) { if (i + dn <= n && b.substr(i, i + dn) == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); i += dn - 1; l = i + 1; } } 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); } int n, m, k, d, H, W, x, y, z, q; int cou; vi a, b, c; vvi(s, 0, 0); vvc(ba, 0, 0); vp p; void solve() { cin >> n; nao(a, n); unique(a); n = sz(a); setMod(); vm ad(n + 1); vm dp(n + 1); dp[0] = 1; rep(i, 1, n) { int v = a[i]; dp[i] += dp[i - 1] + ad[v]; ad[v] += dp[i - 1]; } cout << dp[n - 1] << endl; } int my(int n, vi a) { return 0; } int sister(int n, vi a) { return 0; } signed main() { solve(); #define _arg n, a // cin>>n; // na(a,n); // my(_arg); // cout << my(_arg) << endl; #ifdef _DEBUG bool bad = 0; for (int i = 0, ok = 1; i < k5 && ok; i++) { int n = rand(1, 3); vi a = ranv(m, 1, 10); int myres = my(_arg); int res = sister(_arg); ok = myres == res; if (!ok) { cout << n << endl; cout << a << endl; cout << "正解 : " << res << endl; cout << "出力 : " << myres << endl; bad = 1; break; } } #endif return 0; };
// #pragma GCC optimize ("-O3") #include <bits/stdc++.h> using namespace std; //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } __initon; // 衝突対策 #define ws ___ws //@必須構造 struct T { int f, s, t; T() { f = -1, s = -1, t = -1; } T(int f, int s, int t) : f(f), s(s), t(t) {} bool operator<(const T &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 T &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 T &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; } int operator[](int i) { assert(i < 3); return i == 0 ? f : i == 1 ? s : t; } }; struct F { int a, b, c, d; F() { a = -1, b = -1, c = -1, d = -1; } F(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {} bool operator<(const F &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 F &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 F &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } int operator[](int i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; T mt(int a, int b, int c) { return T(a, b, c); } //@マクロ省略系 型,構造 #define int long long #define ll long long #define double long double #define ull unsigned long long using dou = double; using itn = int; using str = string; using bo = bool; using P = pair<ll, ll>; #define fi first #define se second #define vec vector #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == // マクロ省略系 コンテナ using vi = vector<int>; #define _overloadvvi(_1, _2, _3, _4, name, ...) name #define vvi1(a) vec<vi> a #define vvi2(a, b) vec<vi> a(b) #define vvi3(a, b, c) vec<vi> a(b, vi(c)) #define vvi4(a, b, c, d) vec<vi> a(b, vi(c, d)) #define vvi(...) _overloadvvi(__VA_ARGS__, vvi4, vvi3, vvi2, vvi1)(__VA_ARGS__) using vl = vector<ll>; #define _overloadvvl(_1, _2, _3, _4, name, ...) name #define vvl1(a) vec<vl> a #define vvl2(a, b) vec<vl> a(b) #define vvl3(a, b, c) vec<vl> a(b, vl(c)) #define vvl4(a, b, c, d) vec<vl> a(b, vl(c, d)) #define vvl(...) _overloadvvl(__VA_ARGS__, vvl4, vvl3, vvl2, vvl1)(__VA_ARGS__) using vb = vector<bool>; #define _overloadvvb(_1, _2, _3, _4, name, ...) name #define vvb1(a) vec<vb> a #define vvb2(a, b) vec<vb> a(b) #define vvb3(a, b, c) vec<vb> a(b, vb(c)) #define vvb4(a, b, c, d) vec<vb> a(b, vb(c, d)) #define vvb(...) _overloadvvb(__VA_ARGS__, vvb4, vvb3, vvb2, vvb1)(__VA_ARGS__) using vs = vector<string>; #define _overloadvvs(_1, _2, _3, _4, name, ...) name #define vvs1(a) vec<vs> a #define vvs2(a, b) vec<vs> a(b) #define vvs3(a, b, c) vec<vs> a(b, vs(c)) #define vvs4(a, b, c, d) vec<vs> a(b, vs(c, d)) #define vvs(...) _overloadvvs(__VA_ARGS__, vvs4, vvs3, vvs2, vvs1)(__VA_ARGS__) using vd = vector<double>; #define _overloadvvd(_1, _2, _3, _4, name, ...) name #define vvd1(a) vec<vd> a #define vvd2(a, b) vec<vd> a(b) #define vvd3(a, b, c) vec<vd> a(b, vd(c)) #define vvd4(a, b, c, d) vec<vd> a(b, vd(c, d)) #define vvd(...) _overloadvvd(__VA_ARGS__, vvd4, vvd3, vvd2, vvd1)(__VA_ARGS__) using vc = vector<char>; #define _overloadvvc(_1, _2, _3, _4, name, ...) name #define vvc1(a) vec<vc> a #define vvc2(a, b) vec<vc> a(b) #define vvc3(a, b, c) vec<vc> a(b, vc(c)) #define vvc4(a, b, c, d) vec<vc> a(b, vc(c, d)) #define vvc(...) _overloadvvc(__VA_ARGS__, vvc4, vvc3, vvc2, vvc1)(__VA_ARGS__) using vp = vector<P>; #define _overloadvvp(_1, _2, _3, _4, name, ...) name #define vvp1(a) vec<vp> a #define vvp2(a, b) vec<vp> a(b) #define vvp3(a, b, c) vec<vp> a(b, vp(c)) #define vvp4(a, b, c, d) vec<vp> a(b, vp(c, d)) using vt = vector<T>; #define _overloadvvt(_1, _2, _3, _4, name, ...) name #define vvt1(a) vec<vt> a #define vvt2(a, b) vec<vt> a(b) #define vvt3(a, b, c) vec<vt> a(b, vt(c)) #define vvt4(a, b, c, d) vec<vt> a(b, vt(c, d)) #define v3i(a, b, c, d) vector<vector<vi>> a(b, vector<vi>(c, vi(d))) #define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d))) #define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d))) #define _vvi vector<vi> #define _vvl vector<vl> #define _vvb vector<vb> #define _vvs vector<vs> #define _vvd vector<vd> #define _vvc vector<vc> #define _vvp vector<vp> #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<int, int>; using mapd = map<dou, int>; using mapc = map<char, int>; using maps = map<str, int>; using seti = set<int>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<int>; #define bset bitset #define uset unordered_set #define mset multiset #define umap unordered_map #define umapi unordered_map<int, int> #define umapp unordered_map<P, int> #define mmap multimap // マクロ 繰り返し #define _overloadrep(_1, _2, _3, _4, name, ...) name #define _rep(i, n) for (int i = 0, _lim = n; i < _lim; i++) #define repi(i, m, n) for (int i = m, _lim = n; i < _lim; i++) #define repadd(i, m, n, ad) for (int i = m, _lim = n; i < _lim; i += ad) #define rep(...) _overloadrep(__VA_ARGS__, repadd, repi, _rep, )(__VA_ARGS__) #define _rer(i, n) for (int i = n; i >= 0; i--) #define reri(i, m, n) for (int i = m, _lim = n; i >= _lim; i--) #define rerdec(i, m, n, dec) for (int i = m, _lim = n; i >= _lim; i -= dec) #define rer(...) _overloadrep(__VA_ARGS__, rerdec, reri, _rer, )(__VA_ARGS__) #define fora(a, b) for (auto &&a : b) #define forg(gi, ve) \ for (int gi = 0, f, t, c; gi < ve.size() && (f = ve[gi].from, t = ve[gi].to, \ c = ve[gi].cost, true); \ gi++) #define fort(gi, ve) \ for (int gi = 0, f, t, c; \ gi < ve.size() && (gi += (ve[gi].to == p)) < ve.size() && \ (f = ve[gi].from, t = ve[gi].to, c = ve[gi].cost, true); \ gi++) // マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const int inf = (int)1e9 + 100; const ll linf = (ll)1e18 + 100; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; ll ma = numeric_limits<ll>::min(); ll mi = numeric_limits<ll>::max(); const int y4[] = {-1, 1, 0, 0}; const int x4[] = {0, 0, -1, 1}; const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; // マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((int)(a).size()) #define rs resize #define mp make_pair #define pb push_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() //@拡張系 こう出来るべきというもの // 埋め込み 存在を意識せずに機能を増やされているもの 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 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; } ostream &operator<<(ostream &os, P p) { os << p.fi << " " << p.se << endl; 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 (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < vec[0].size(); j++) { os << vec[i][j]; } os << endl; } return os; } template <typename V, typename H> void resize(vector<V> &vec, const H head) { vec.resize(head); } template <typename V, typename H, typename... T> void resize(vector<V> &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } template <typename T, typename _Pr> bool all_of(const vector<T> &vec, _Pr pred) { return std::all_of(vec.begin(), vec.end(), pred); } template <typename T, typename _Pr> bool any_of(const vector<T> &vec, _Pr pred) { return std::any_of(vec.begin(), vec.end(), pred); } template <typename T, typename _Pr> bool none_of(const vector<T> &vec, _Pr pred) { return std::none_of(vec.begin(), vec.end(), pred); } template <typename T, typename _Pr> const typename vector<T>::const_iterator find_if(const vector<T> &vec, _Pr pred) { return std::find_if(vec.begin(), vec.end(), pred); } template <typename T> bool contains(const vector<T> &vec, const T &val) { return std::find(vec.begin(), vec.end(), val) != vec.end(); } template <typename T, typename _Pr> bool contains_if(const vector<T> &vec, _Pr pred) { return std::find_if(vec.begin(), vec.end(), pred) != vec.end(); } template <class T> void replace(vector<T> &a, T key, T v) { replace(a.begin(), a.end(), key, v); } 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)); } template <class T> T pop(set<T> &set) { T res = *set.begin(); set.erase(set.find(res)); return res; } template <class T> T pop(mset<T> &set) { T res = *set.begin(); set.erase(set.find(res)); return res; } template <class T> T popBack(set<T> &set) { T res = *set.rbegin(); set.erase(set.find(res)); return res; } template <class T> T popBack(mset<T> &set) { T res = *set.rbegin(); set.erase(set.find(res)); return res; } inline void sort(string &a) { sort(a.begin(), a.end()); } template <class T> inline void sort(vector<T> &a) { sort(a.begin(), a.end()); }; template <class T> inline void sort(vector<T> &a, int len) { sort(a.begin(), a.begin() + len); }; template <class T, class F> inline void sort(vector<T> &a, F f) { sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); }); }; enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; inline void sort(vector<P> &a, ___pcomparator type) { switch (type) { case fisi: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); break; case fisd: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); break; case fdsi: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); break; case fdsd: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); break; case sifi: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); break; case sifd: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); break; case sdfi: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); break; case sdfd: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); break; } }; template <class T> inline void rsort(vector<T> &a) { sort(a.begin(), a.end(), greater<T>()); }; template <class T> inline void rsort(vector<T> &a, int len) { sort(a.begin(), a.begin() + len, greater<T>()); }; template <class U, class F> inline void rsort(vector<U> &a, F f) { sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); }); }; template <class U> inline void sortp(vector<U> &a, vector<U> &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n) c.eb(a[i], b[i]); sort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; ; } }; // F = T<T> // 例えばreturn p.fi + p.se; template <class U, class F> inline void sortp(vector<U> &a, vector<U> &b, F f) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n) c.eb(a[i], b[i]); sort(c, f); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template <class U, class F> inline void sortp(vector<U> &a, vector<U> &b, char type) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n) c.eb(a[i], b[i]); sort(c, type); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template <class U> inline void rsortp(vector<U> &a, vector<U> &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n) c.eb(a[i], b[i]); rsort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template <class U, class F> inline void rsortp(vector<U> &a, vector<U> &b, F f) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n) c.eb(a[i], b[i]); rsort(c, f); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template <class U> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n) r.eb(a[i], b[i], c[i]); sort(r); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template <class U, class F> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n) r.eb(a[i], b[i], c[i]); sort(r, f); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template <class U, class F> inline void rsortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n) r.eb(a[i], b[i], c[i]); rsort(r, f); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template <class T> inline void sort2(vector<vector<T>> &a) { for (int i = 0, n = a.size(); i < n; i++) sort(a[i]); } template <class T> inline void rsort2(vector<vector<T>> &a) { for (int 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 V, typename T> void fill(V &xx, const T vall) { xx = vall; } template <typename V, typename T> void fill(vector<V> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } //@汎用便利関数 入力 template <typename T = int> T in() { T x; cin >> x; return (x); } string sin() { return in<string>(); } double din() { return in<double>(); } ll lin() { return in<ll>(); } #define na(a, n) \ a.resize(n); \ rep(i, n) cin >> a[i]; #define nao(a, n) \ a.resize(n + 1); \ rep(i, n) cin >> a[i + 1]; #define nad(a, n) \ a.resize(n); \ rep(i, n) cin >> a[i], a[i]--; #define na2(a, b, n) \ a.resize(n), b.resize(n); \ rep(i, n) cin >> a[i] >> b[i]; #define na2d(a, b, n) \ a.resize(n), b.resize(n); \ rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; #define na3(a, b, c, n) \ a.resize(n), b.resize(n), c.resize(n); \ rep(i, n) cin >> a[i] >> b[i] >> c[i]; #define na3d(a, b, c, 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 nt(a, h, w) \ resize(a, h, w); \ rep(hi, h) rep(wi, w) cin >> a[hi][wi]; #define ntd(a, h, w) \ rs(a, h, w); \ rep(hi, h) rep(wi, w) cin >> a[hi][wi], a[hi][wi]--; #define ntp(a, h, w) \ fill(a, '#'); \ rep(hi, 1, h + 1) rep(wi, 1, w + 1) cin >> a[hi][wi]; // 汎用便利関数 出力 template <class T> void out(T x) { typeid(x) == typeid(double) ? cout << fixed << setprecision(10) << x << endl : cout << x << endl; } template <class T> void out(vector<T> a) { fora(v, a) cout << v << endl; } // デバッグ #define sp << " " << #define debugName(VariableName) #VariableName #define _deb1(x) cerr << debugName(x) << " = " << x << endl #define _deb2(x, y) \ cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \ << endl #define _deb3(x, y, z) \ cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \ << ", " debugName(z) << " = " << z << endl #define _deb4(x, y, z, a) \ cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \ << ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \ << a << endl #define _deb5(x, y, z, a, b) \ cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \ << ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \ << a << ", " << debugName(b) << " = " << b << endl #define _overloadebug(_1, _2, _3, _4, _5, name, ...) name #define debug(...) \ _overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__) #define deb(...) \ _overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__) #define debugline(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n' // よく使うクラス、構造体 class UnionFind { public: vi par, rank, sizes; int n, trees; UnionFind(int n) : n(n), trees(n) { par.resize(n), rank.resize(n), sizes.resize(n); rep(i, n) par[i] = i, sizes[i] = 1; } int root(int x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } int find(int x) { return root(x); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); trees--; par[y] = x; sizes[x] += sizes[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return sizes[root(x)]; } // 順不同 umapなので vec<vi> sets() { vec<vi> res(trees); umap<int, vi> map; rep(i, n) map[root(i)].push_back(i); int i = 0; for (auto &&p : map) { int r = p.fi; res[i].push_back(r); for (auto &&v : p.se) { if (r == v) continue; res[i].push_back(v); } i++; } return res; } }; // MOD関連 ll MOD = (int)1e9 + 7; class mint { public: int x; mint() : x(0) {} mint(signed y) : x(y >= 0 ? y % MOD : MOD - (-y) % MOD) {} mint(int y) : x(y >= 0 ? y % MOD : MOD - (-y) % MOD) {} static int _mpow(int v, ll a) { ll x = v, n = a, res = 1; while (n) { if (n & 1) res = (res * x) % MOD; x = (x * x) % MOD; n >>= 1; } return res; } // Arithmetic Oprators mint &operator+=(mint that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } mint &operator-=(mint that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } mint &operator*=(mint that) { x = 1LL * x * that.x % MOD; return *this; } mint &operator/=(const mint &that); mint &operator^=(const mint &that) { this->x = _mpow(x, that.x); return *this; } mint &operator%=(mint that) { x %= that.x; return *this; } mint &operator+=(const int that) { return *this += mint(that); } mint &operator-=(const int that) { return *this -= mint(that); } mint &operator*=(const int that) { return *this *= mint(that); } mint &operator/=(const int that); mint &operator^=(const int that) { this->x = _mpow(x, that); return *this; } mint &operator%=(const int that) { return *this %= mint(that); } mint &operator+=(const signed that) { return *this += mint(that); } mint &operator-=(const signed that) { return *this -= mint(that); } mint &operator*=(const signed that) { return *this *= mint(that); } mint &operator/=(const signed that); mint &operator^=(const signed that) { this->x = _mpow(x, that); return *this; } mint &operator%=(const signed that) { return *this %= mint(that); } // Comparators bool operator<(mint that) { return x < that.x; } bool operator>(mint that) { return x > that.x; } bool operator<=(mint that) { return x <= that.x; } bool operator>=(mint that) { return x >= that.x; } bool operator!=(mint that) { return x != that.x; } bool operator==(mint that) { return x == that.x; } bool operator!=(int that) { return x != that; } bool operator==(int that) { return x == that; } bool operator!=(signed that) { return x != that; } bool operator==(signed that) { return x == that; } // Utilities unsigned getval() const { return x; } operator int() { return x; } mint operator+(mint that) const { return mint(*this) += that; } mint operator-(mint that) const { return mint(*this) -= that; } mint operator*(mint that) const { return mint(*this) *= that; } mint operator%(mint that) const { return mint(*this) %= that; } mint operator+(const int that) const { return mint(*this) += that; } mint operator-(const int that) const { return mint(*this) -= that; } mint operator*(const int that) const { return mint(*this) *= that; } mint operator%(const int that) const { return mint(*this) %= that; } mint operator=(const int that) { return *this = mint(that); } mint operator+(const signed that) const { return mint(*this) += that; } mint operator-(const signed that) const { return mint(*this) -= that; } mint operator*(const signed that) const { return mint(*this) *= that; } mint operator%(const signed that) const { return mint(*this) %= that; } mint operator=(const signed that) { return *this = mint(that); } mint operator++() { x++; return *this; } mint operator++(signed) { auto ret = *this; x++; return ret; } friend void operator+=(ll &a, const mint &b) { a = mint(a % MOD + b.x); } friend void operator-=(ll &a, const mint &b) { a = mint(a % MOD - b.x); } friend void operator*=(ll &a, const mint &b) { a = mint(a % MOD * b.x); } friend void operator/=(ll &a, const mint &b); friend mint operator+(const ll a, const mint &b) { return mint(a % MOD + b.x); } friend mint operator-(const ll a, const mint &b) { return mint(a % MOD - b.x); } friend mint operator*(const ll a, const mint &b) { return mint(a % MOD * b.x); } friend mint operator^(const ll a, const mint &b) { return _mpow(a, b.x); } }; mint itom(int v) { mint res; res.x = v; return res; } const int setModMax = 510000; vector<mint> fac, finv, inv; void setMod(int m = MOD) { fac.resize(setModMax); finv.resize(setModMax); inv.resize(setModMax); MOD = m; fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < setModMax; i++) { fac[i] = fac[i - 1].x * i % MOD; inv[i] = MOD - inv[MOD % i].x * (MOD / i) % MOD; finv[i] = finv[i - 1].x * inv[i].x % MOD; } } mint mpow(int v, ll a) { return mint::_mpow(v, a); } mint com(ll n, ll r) { if (n < r || n < 0 || r < 0) return 0; if (fac.size() == 0) setMod(); return fac[n] * finv[r] * finv[n - r]; } mint ncr(ll n, ll r) { return com(n, r); } // n人にr個を配るとき、同じ人に何個配っても良い場合 mint nhr(ll n, ll r) { return com(n + r - 1, r); } // 拡張ユークリッドの互除法 mint minv(ll a) { if (fac[0] == 0) setMod(); if (a < setModMax) return inv[a]; a %= MOD; ll b = MOD, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return x; } mint &mint::operator/=(const mint &that) { return *this *= minv(that.x); } mint &mint::operator/=(const ll a) { return *this *= minv(a); } mint &mint::operator/=(const signed a) { return *this *= minv(a); } void operator/=(ll &a, const mint &b) { a = (a * minv(b.x)).x; } using PM = pair<mint, mint>; using vm = vector<mint>; #define _overloadvvm(_1, _2, _3, _4, name, ...) name #define vvm1(a) vec<vm> a #define vvm2(a, b) vec<vm> a(b) #define vvm3(a, b, c) vec<vm> a(b, vm(c)) #define vvm4(a, b, c, d) vec<vm> a(b, vm(c, d)) #define vvm(...) _overloadvvm(__VA_ARGS__, vvm4, vvm3, vvm2, vvm1)(__VA_ARGS__) vb isPrime; vi primes; void setPrime() { isPrime.resize(4010101); fill(isPrime, true); int len = sizeof(isPrime) / sizeof(isPrime[0]); isPrime[0] = isPrime[1] = false; for (int i = 2; i <= sqrt(len) + 5; ++i) { if (!isPrime[i]) continue; for (int j = 2; i * j < len; ++j) { isPrime[i * j] = false; } } rep(i, len) if (isPrime[i]) primes.pb(i); } // 幾何 Pをcomplexとして扱う bool eq(double a, double b) { return fabs(a - b) < eps; } using C = complex<double>; C rot(C &a, dou th) { return a * C(cos(th), sin(th)); } dou inpro(C &a, C &b) { return real(a * conj(b)); } // 90度回転させて内積が0なら平行 bool line(C a, C b, C c) { C ab = b - a; C ac = c - a; // 複素数の掛け算は回転 ab *= C(0, 1); return eq(inpro(ab, ac), 0); } bool line(P a, P b, P c) { return line(C(a.fi, a.se), C(b.fi, b.se), C(c.fi, c.se)); } bool line(int xa, int ya, int xb, int yb, int xc, int yc) { C a = C(xa, ya); C b = C(xb, yb); C c = C(xc, yc); return line(a, b, c); } // 便利関数 // テスト用 char ranc() { return (char)('a' + rand() % 26); } int rand(int min, int 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(int n, int min, int max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } // 単調増加 vi ranvi(int n, int min, int 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(int n, int min, int 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]); } } // 便利 汎用 template <typename V, typename T> int find(vector<V> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename V, typename T> P find(vector<vector<V>> &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 V, typename U> T find(vector<vector<vector<V>>> &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); } template <typename V, typename T> int count(V &a, const T k) { return a == k; } template <typename V, typename T> int count(vector<V> &a, const T k) { int ret = 0; fora(v, a) ret += count(v, k); return ret; } template <typename V> int count_odd(V &a) { return a % 2; } template <typename V> int count_odd(vector<V> &a) { int ret = 0; fora(v, a) ret += count_odd(v); return ret; } template <typename V> int count_even(V &a) { return a % 2 == 0; } template <typename V> int count_even(vector<V> &a) { int ret = 0; fora(v, a) ret += count_even(v); return ret; } // algorythm void iota(vector<int> &ve, int s, int n) { ve.resize(n); iota(all(ve), s); } vi iota(int s, int n) { vi ve(n); iota(all(ve), s); return ve; } // 便利 数学 int mod(int a, int m) { return (a % m + m) % m; } int pow(int a) { return a * a; }; ll fact(int v) { return v <= 1 ? 1 : v * fact(v - 1); } ll comi(int n, int 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]; } 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 tle() { while (inf) cout << inf << endl; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll gcd(vi b) { ll res = b[0]; for (auto &&v : b) res = gcd(v, res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template <class T> vector<T> rev(vector<T> &a) { vector<T> ret = a; reverse(all(ret)); return ret; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else return (a + b - 1) / b; } 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 / 2; } ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; } vi divisors(int v) { vi res; for (int i = 1; i <= sqrt(v); ++i) { if (v % i == 0) { res.pb(i); if (i != v / i) res.pb(v / i); } } return res; } vi factorization(int v) { int tv = v; vi res; if (isPrime.size() == 0) setPrime(); for (auto &&p : primes) { if (v % p == 0) res.push_back(p); while (v % p == 0) { v /= p; } if (v == 1 || p * p > tv) break; } if (v > 1) res.pb(v); return res; } unordered_map<int, int> factorizationMap(int v) { int tv = v; unordered_map<int, int> res; if (isPrime.size() == 0) setPrime(); for (auto &&p : primes) { while (v % p == 0) { res[p]++; v /= p; } if (v == 1 || p * p > tv) break; } if (v > 1) res[v]++; return res; } int get(int a, int keta) { return (a / (int)pow(10, keta)) % 10; } int keta(int v) { int cou = 0; while (v) { cou++, v %= 10; } return cou; } int dsum(int v) { int ret = 0; for (; v; v /= 10) ret += v % 10; return ret; } int sumd(int v) { return dsum(v); } // 変換系 template <class T, class U> vector<T> keys(vector<pair<T, U>> a) { vector<T> res; for (auto &&k : a) res.pb(k.fi); return res; } template <class T, class U> vector<U> keys(map<T, U> a) { vector<U> res; for (auto &&k : a) res.pb(k.fi); return res; } template <class T, class U> vector<U> keys(umap<T, U> a) { vector<U> res; for (auto &&k : a) res.pb(k.fi); return res; } template <class T, class U> vector<U> values(vector<pair<T, U>> a) { vector<U> res; for (auto &&k : a) res.pb(k.se); return res; } template <class T, class U> vector<T> values(map<T, U> a) { vector<T> res; for (auto &&k : a) res.pb(k.se); return res; } template <class T, class U> vector<T> values(umap<T, U> a) { vector<T> res; for (auto &&k : a) res.pb(k.se); return res; } vi list(int a) { vi res; while (a) { res.insert(res.begin(), a % 10); a /= 10; } return res; } template <class T, class U> bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class U> bool chmax(const U &b) { return chmax(ma, b); } template <class T, class U> bool chmin(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class U> bool chmin(const U &b) { return chmin(mi, b); } #define chmi chmin #define chma chmax 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 min(vector<T> a, int n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> a, int s, int 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> T max(vector<T> a, int n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> a, int s, int 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, max(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, int len = -1) { if (len == -1) len = v.size(); T res = 0; chmin(len, v.size()); rep(i, len) res += v[i]; return res; } template <class T> T sum(vector<vector<T>> &v, int h = -1, int w = -1) { if (h == -1) h = v.size(); if (w == -1) w = v[0].size(); T res = 0; chmin(h, v.size()); chmin(w, v[0].size()); rep(i, h) rep(j, w) res += v[i][j]; return res; } P sump(vp &v, int len = -1) { if (len == -1) len = v.size(); P res = {0, 0}; chmin(len, v.size()); rep(i, len) { res.fi += v[i].fi; res.se += v[i].se; } return res; } /// 要素が0の時、返り値は0か1か template <class T> T mul(vector<T> &v, int len = -1) { if (len == -1) len = v.size(); T res = 1; chmin(len, v.size()); rep(i, len) res *= v[i]; return res; } void clear(PQ &q) { while (q.size()) q.pop(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(int size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(int h, int w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (int i = 1; i <= 2 * h + 1; i++) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; } // 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; } template <class T> vector<T> imomi(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chmin(ret[i + 1], ret[i]); return ret; } template <class T> struct ruic { const vector<T> rui; ruic(vector<T> &ru) : rui(ru) {} T operator()(int l, int r) { assert(l <= r); return rui[r] - rui[l]; } T operator[](int i) { return rui[i]; } }; template <class T> struct rruic { const T *rrui; rruic(T *ru) : rrui(ru) {} // n-1から-1へ T operator()(int l, int r) { assert(l >= r); return rrui[r] - rrui[l]; } T operator[](int i) { return rrui[i]; } }; // template<class T> vector<T> ruiv(vector<T> &a) template <class T> ruic<T> ruiv(vector<T> &a) { vector<T> res(a.size() + 1); rep(i, a.size()) res[i + 1] = res[i] + a[i]; return ruic<T>(res); } 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; } // template<class T> T *rrui(vector<T> &a) { // 右から左にかけての半開区間 (-1 n-1] template <class T> rruic<T> rrui(vector<T> &a) { int 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) { int 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> void inc(vector<T> &a) { for (auto &u : a) inc(u, 1); } 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 T> void dec(vector<T> &a) { for (auto &u : a) dec(u, 1); } inline bool inside(int h, int w, int H, int W) { return h >= 0 && w >= 0 && h < H && w < W; } 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() ll goldd(ll left, ll right, function<ll(ll)> 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; } ll goldt(ll left, ll right, function<ll(ll)> 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; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmin(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()) chmax(res, *max_element(all(a[i]))); return res; } bool bget(ll m, int keta) { return (m >> keta) & 1; } int bget(ll m, int keta, int sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } inline ll bit(int n) { return (1LL << (n)); } inline ll bit(int n, int sinsuu) { return (ll)pow(sinsuu, n); } int bcou(ll m) { return __builtin_popcount(m & 0xFFFFFFFF) + __builtin_popcount(m >> 32); } // 初期化は0を渡す ll nextComb(ll &mask, int n, int 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を返す vl bitCombList(int n, int r) { vl res; int m = 0; while (nextComb(m, n, r)) { res.pb(m); } return res; } // 大文字小文字を区別する int altoiaZ(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a' + 26; } char itoalaZ(int i) { if (i < 26) return 'A' + i; return 'a' + i - 26; } // aもAも0を返す 基本小文字 int altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } char itoal(int i) { return 'a' + i; } int ctoi(char c) { return c - '0'; } char itoc(int i) { return i + '0'; } int vtoi(vi &v) { int 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(int i) { vi res; while (i) { res.pb(i % 10); i /= 10; } rev(res); return res; } vector<vector<int>> ctoi(vector<vector<char>> s, char c) { int n = sz(s), m = sz(s[0]); vector<vector<int>> res(n, vector<int>(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()); void compress(vi &a) { vi b; int len = a.size(); for (int i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (int i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } void compress(int a[], int len) { vi b; for (int i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (int 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 ans(a) \ cout << a << endl; \ continue; #define poll(a) \ q.front(); \ q.pop() #define dpoll(a) \ q.front(); \ q.pop_front() #define pollLast(a) \ q.back(); \ q.pop_back() #define pollBack(a) \ q.back(); \ q.pop_back() template <class T> inline void fin(T s) { cout << s << endl, exit(0); } template <class T> struct edge { int from, to; T cost; int id; int type; edge(int f, int t, T c = 1, int id = -1, int ty = -1) : from(f), to(t), cost(c), id(id), type(ty) {} bool operator<(const edge &b) const { return cost < b.cost; } bool operator>(const edge &b) const { return cost > b.cost; } }; template <typename T> class graph { protected: vector<bool> _used; public: vector<vector<edge<T>>> g; vector<edge<T>> edges; int n; graph(int n) : n(n) { g.resize(n), _used.resize(n); } void clear() { g.clear(), edges.clear(); } void resize(int n) { this->n = n; g.resize(n); _used.resize(n); } int size() { return g.size(); } vector<edge<T>> &operator[](int i) { return g[i]; } virtual void add(int from, int to, T cost, int id, int ty) = 0; virtual bool used(edge<T> &e) = 0; virtual bool used(int id) = 0; virtual void del(edge<T> &e) = 0; virtual void del(int id) = 0; }; template <typename T = ll> class digraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; using graph<T>::_used; digraph(int n) : graph<T>(n) {} void add(int f, int t, T cost = 1, int id = -1, int ty = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digraph add"); deb(f, t, cost, id, ty); ole(); } if (id == -1) id = edges.size(); g[f].emplace_back(f, t, cost, id, ty); edges.emplace_back(f, t, cost, id, ty); } bool used(edge<T> &e) { return _used[e.id]; } bool used(int id) { return _used[id]; } void del(edge<T> &e) { _used[e.id] = _used[e.id ^ 1] = 1; } void del(int id) { _used[id] = _used[id ^ 1] = 1; } }; template <class T = int> class undigraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; using graph<T>::_used; undigraph(int n) : graph<T>(n) {} void add(int f, int t, T cost = 1, int id = -1, int ty = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("undigraph add"); deb(f, t, cost, id, ty); ole(); } if (id == -1) id = edges.size(); g[f].emplace_back(f, t, cost, id, ty); g[t].emplace_back(t, f, cost, id + 1, ty); edges.emplace_back(f, t, cost, id, ty); edges.emplace_back(t, f, cost, id + 1, ty); } void add(edge<T> &e) { int f = e.from, t = e.to, ty = e.type; T cost = e.cost; add(f, t, cost, ty); } bool used(edge<T> &e) { return _used[e.id]; } bool used(int id) { return _used[id]; } void del(edge<T> &e) { _used[e.id] = _used[e.id ^ 1] = 1; } void del(int id) { _used[id] = _used[id ^ 1] = 1; } }; template <class T> vector<T> dijkstra(const graph<T> &g, int s, int cant_arrive = -1) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } T initValue = MAX(T); vector<T> dis(g.n, initValue); priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; dis[s] = 0; q.emplace(0, s); while (q.size()) { T nowc = q.top().fi; int i = q.top().se; q.pop(); if (dis[i] != nowc) continue; for (auto &&e : g.g[i]) { int to = e.to; T cost = nowc + e.cost; if (dis[to] > cost) { dis[to] = cost; q.emplace(dis[to], to); } } } // 基本、たどり着かないなら-1 if (cant_arrive == -1) for (auto &&d : dis) if (d == initValue) d = -1; return dis; } template <class T> vector<vector<T>> warshall(const graph<T> &g, int cant_arrive = -1) { int n = g.n; vector<vector<T>> dis(n, vector<T>(n, linf)); fora(e, g.edges) chmin(dis[e.from][e.to], e.cost); rep(i, n) dis[i][i] = 0; rep(k, n) rep(i, n) rep(j, n) chmin(dis[i][j], dis[i][k] + dis[k][j]); // 基本、たどり着かないなら-1 if (cant_arrive == -1) rep(i, n) rep(j, n) if (dis[i][j] == linf) dis[i][j] = -1; return dis; } template <class T = int> class tree : public undigraph<T> { public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; using undigraph<T>::_used; vi dep; vi disv; private: bool never = 1; int root = -1; vector<vector<int>> par; bool costallone; void dfs(int v, int p, int d) { dep[v] = d; par[0][v] = p; int lim = (*this)[v].size(); for (int i = 0; i < lim; i++) { int t = g[v][i].to; if (t == p) con; dfs(t, v, d + 1); } } void built() { never = 0; n = g.size(); par.assign(30, vi(n)); dep.resize(n); costallone = 1; fora(e, edges) if (e.cost != 1) costallone = 0; dfs(root, -1, 0); rep(k, par.size() - 1) { rep(i, n) { if (par[k][i] == -1) par[k + 1][i] = -1; else par[k + 1][i] = par[k][par[k][i]]; } } if (costallone) disv = dep; else disv = dijkstra(*this, root); } int _lca(int u, int v) { if (dep[u] > dep[v]) swap(u, v); rep(k, par.size()) { if ((dep[u] - dep[v]) >> k & 1) { v = par[k][v]; } } if (u == v) return u; rer(k, par.size() - 1) { if (par[k][u] != par[k][v]) { u = par[k][u]; v = par[k][v]; } } return par[0][u]; } int _dis(int u, int v) { int p = _lca(u, v); return disv[u] + disv[v] - disv[p] * 2; } public: tree(int n, int root = 0) : undigraph<T>(n), root(root) {} bool leaf(int v) { return sz(g[v]) == 1 && v != root; } int dis(int u, int v) { if (never) { built(); } return _dis(u, v); } int lca(int u, int v) { if (never) { built(); } return _lca(u, v); } }; // 辺によりメモリを大量消費 // よってedgesを消している // 頂点10^6でメモリを190MB(制限の8割)使う // 軽量化のため、辺を持たないbig gridクラスがあってもいいかもしれない // template <class T = int> class grid_k6 : public undigraph<T> { public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; using undigraph<T>::_used; int H, W; int eid = 0; void add(int f, int t, T cost = 1, int id = -1, int ty = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("grid_k6 add"); deb(f, t, cost, id, ty); ole(); } g[f].emplace_back(f, t, cost, eid++, ty); g[t].emplace_back(t, f, cost, eid++, ty); } int getid(int h, int w) { if (!inside(h, w, H, W)) return -1; return W * h + w; } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } grid_k6(int H, int W) : H(H), W(W), undigraph<T>(H * W) { rep(h, H) { rep(w, W) { int f = getid(h, w); if (w + 1 < W) add(f, getid(h, w + 1)); if (h + 1 < H) add(f, getid(h + 1, w)); } } } grid_k6(_vvc ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall) con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } void add(int fh, int fw, int th, int tw) { add(getid(fh, fw), getid(th, tw)); } }; // 左上から右下に移動できる template <class T = int> class digrid_k6 : public digraph<T> { public: using digraph<T>::g; using digraph<T>::n; using digraph<T>::edges; using digraph<T>::_used; int H, W; int eid = 0; void add(int f, int t, T cost = 1, int id = -1, int ty = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digrid_k6 add"); deb(f, t, cost, id, ty); ole(); } g[f].emplace_back(f, t, cost, eid++, ty); } int getid(int h, int w) { if (!inside(h, w, H, W)) return -1; return W * h + w; } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } digrid_k6(int H, int W) : H(H), W(W), digraph<T>(H * W) { rep(h, H) { rep(w, W) { int f = getid(h, w); if (w + 1 < W) add(f, getid(h, w + 1)); if (h + 1 < H) add(f, getid(h + 1, w)); } } } digrid_k6(_vvc ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), digraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall) con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } void add(int fh, int fw, int th, int tw) { add(getid(fh, fw), getid(th, tw)); } }; template <class T> bool nibu(const graph<T> &g) { if (g.edges.size() == 0) return true; UnionFind uf(g.n * 2); for (auto &&e : g.edges) uf.unite(e.from, e.to + g.n), uf.unite(e.from + g.n, e.to); rep(i, g.n) if (uf.same(i, i + g.n)) return 0; return 1; } // 二部グラフを色分けした際の頂点数を返す template <class T> vp nibug(graph<T> &g) { vp cg; if (!nibu(g)) { debugline("nibu"); ole(); } int _n = g.size(); vb _was(_n); queue<P> q; rep(i, _n) { if (_was[i]) continue; q.push(mp(i, 1)); _was[i] = 1; int red = 0; int coun = 0; while (q.size()) { int now = q.front().fi; int col = q.front().se; red += col; coun++; q.pop(); forg(gi, g[now]) { if (_was[t]) continue; q.push(mp(t, col ^ 1)); _was[t] = 1; } } cg.push_back(mp(red, coun - red)); } return cg; } // 機能拡張 vp vtop(vi &a, vi &b) { vp res(sz(a)); rep(i, sz(a)) res[i] = mp(a[i], b[i]); return res; } void ptov(vp &p, vi &a, vi &b) { a.resize(sz(p)); b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].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 <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } 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, class U> set<T> &operator+=(set<T> &a, U v) { a.insert(v); return a; } 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.pb(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) { a.pb(b); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, initializer_list<U> v) { for (auto &&va : v) a.pb(va); 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(); } 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; rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <typename T> void remove(vector<T> &v, unsigned int i) { v.erase(v.begin() + i); } template <typename T> void remove(vector<T> &v, unsigned int s, unsigned int e) { v.erase(v.begin() + s, v.begin() + e); } template <typename T> void removen(vector<T> &v, unsigned int s, unsigned int n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T> void erase(vector<T> &v, unsigned int i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned int s, unsigned int e) { v.erase(v.begin() + s, v.begin() + e); } template <typename T> void erasen(vector<T> &v, unsigned int s, unsigned int n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned int 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 int i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T, typename U> void insert(vector<T> &v, initializer_list<U> list) { for (auto &&va : list) v.pb(va); } template <typename T, typename U> void insert(vector<T> &v, unsigned int i, initializer_list<U> 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); } template <typename T> void insert(set<T> &v, initializer_list<T> list) { for (auto &&va : list) v.insert(va); } // 閉路がなければtrue bool topo(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n) for (auto &&e : g[i]) nyu[e.to]++; queue<int> st; rep(i, n) if (nyu[i] == 0) st.push(i); while (st.size()) { int v = st.front(); st.pop(); res.pb(v); fora(e, g[v]) if (--nyu[e.to] == 0) st.push(e.to); } return res.size() == n; } // 辞書順最小トポロジカルソート bool topos(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n) for (auto &&e : g[i]) nyu[e.to]++; // 小さい順 priority_queue<int, vector<int>, greater<int>> q; rep(i, n) if (nyu[i] == 0) q.push(i); while (q.size()) { int i = q.top(); q.pop(); res.pb(i); fora(e, g[i]) if (--nyu[e.to] == 0) q.push(e.to); } return res.size() == n; } vector<string> split(const string a, const char deli) { string b = a + deli; int 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) { string b = a + deli; int l = 0, r = 0, n = b.size(), dn = deli.size(); vector<string> res; rep(i, n) { if (i + dn <= n && b.substr(i, i + dn) == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); i += dn - 1; l = i + 1; } } 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); } int n, m, k, d, H, W, x, y, z, q; int cou; vi a, b, c; vvi(s, 0, 0); vvc(ba, 0, 0); vp p; void solve() { cin >> n; nao(a, n); unique(a); n = sz(a); setMod(); vm ad(k5 * 2); vm dp(k5 * 2); dp[0] = 1; rep(i, 1, n) { int v = a[i]; dp[i] += dp[i - 1] + ad[v]; ad[v] += dp[i - 1]; } cout << dp[n - 1] << endl; } int my(int n, vi a) { return 0; } int sister(int n, vi a) { return 0; } signed main() { solve(); #define _arg n, a // cin>>n; // na(a,n); // my(_arg); // cout << my(_arg) << endl; #ifdef _DEBUG bool bad = 0; for (int i = 0, ok = 1; i < k5 && ok; i++) { int n = rand(1, 3); vi a = ranv(m, 1, 10); int myres = my(_arg); int res = sister(_arg); ok = myres == res; if (!ok) { cout << n << endl; cout << a << endl; cout << "正解 : " << res << endl; cout << "出力 : " << myres << endl; bad = 1; break; } } #endif return 0; };
replace
2,318
2,320
2,318
2,320
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int a[100007]; int b[100007]; long long dp[100007]; long long sum[100007]; const long long mod = 1e9 + 7; int main() { dp[0] = 1; int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int cnt = 0; for (int i = 1; i <= n; i++) { if (a[i] != a[i - 1]) b[++cnt] = a[i]; } for (int i = 1; i <= cnt; i++) { dp[i] = dp[i - 1]; dp[i] = (dp[i] + sum[b[i]]) % mod; sum[b[i]] = (sum[b[i]] + dp[i - 1]) % mod; } printf("%lld", dp[cnt]); return 0; }
#include <bits/stdc++.h> using namespace std; int a[200007]; int b[200007]; long long dp[200007]; long long sum[200007]; const long long mod = 1e9 + 7; int main() { dp[0] = 1; int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int cnt = 0; for (int i = 1; i <= n; i++) { if (a[i] != a[i - 1]) b[++cnt] = a[i]; } for (int i = 1; i <= cnt; i++) { dp[i] = dp[i - 1]; dp[i] = (dp[i] + sum[b[i]]) % mod; sum[b[i]] = (sum[b[i]] + dp[i - 1]) % mod; } printf("%lld", dp[cnt]); return 0; }
replace
2
6
2
6
0
p03096
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using namespace placeholders; using LL = long long; using ULL = unsigned long long; using VI = vector<int>; using VVI = vector<vector<int>>; using VS = vector<string>; using ISS = istringstream; using OSS = ostringstream; using PII = pair<int, int>; using VPII = vector<pair<int, int>>; template <typename T = int> using VT = vector<T>; template <typename T = int> using VVT = vector<vector<T>>; template <typename T = int> using LIM = numeric_limits<T>; template <typename T> inline istream &operator>>(istream &s, vector<T> &v) { for (T &t : v) { s >> t; } return s; } template <typename T> inline ostream &operator<<(ostream &s, const vector<T> &v) { for (int i = 0; i < int(v.size()); ++i) { s << (" " + !i) << v[i]; } return s; } void in_impl(){}; template <typename T, typename... TS> void in_impl(T &head, TS &...tail) { cin >> head; in_impl(tail...); } #define IN(T, ...) \ T __VA_ARGS__; \ in_impl(__VA_ARGS__); template <typename T> struct getv_fmt; template <> struct getv_fmt<int> { static constexpr const char *fmt = "%d"; }; template <> struct getv_fmt<long long> { static constexpr const char *fmt = "%lld"; }; template <typename T> void getv(std::vector<T> &v) { for_each(begin(v), end(v), [](T &a) { scanf(getv_fmt<T>::fmt, &a); }); }; template <typename T> inline T fromString(const string &s) { T res; istringstream iss(s); iss >> res; return res; } template <typename T> inline string toString(const T &a) { ostringstream oss; oss << a; return oss.str(); } #define NUMBERED(name, number) NUMBERED2(name, number) #define NUMBERED2(name, number) name##_##number #define REP1(n) REP2(NUMBERED(REP_COUNTER, __LINE__), n) #define REP2(i, n) REP3(i, 0, n) #define REP3(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define GET_REP(a, b, c, F, ...) F #define REP(...) GET_REP(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__) #define FOR(e, c) for (auto &&e : c) #define ALL(c) begin(c), end(c) #define AALL(a) \ (remove_all_extents<decltype(a)>::type *)a, \ (remove_all_extents<decltype(a)>::type *)a + \ sizeof(a) / sizeof(remove_all_extents<decltype(a)>::type) #define DRANGE(c, p) begin(c), begin(c) + (p), end(c) #define SZ(v) ((int)(v).size()) #define EXIST(c, e) ((c).find(e) != (c).end()) template <typename T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } template <typename T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } #define PB push_back #define EM emplace #define EB emplace_back #define BI back_inserter #define MP make_pair #define fst first #define snd second #define DUMP(x) cerr << #x << " = " << (x) << endl constexpr int MOD = 1'000'000'007; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(12) << fixed; IN(int, N); VI A(N); cin >> A; transform(ALL(A), begin(A), bind(minus<int>(), _1, 1)); VI nexts(N); { VVI occurences(N); REP(i, N) { occurences[A[i]].PB(i); } FOR(row, occurences) { REP(i, SZ(row) - 1) { nexts[row[i]] = row[i + 1]; } } } static int dp[1 << 18]; // dp[ position of leftmost ] := # of ways dp[0] = 1; REP(i, N) { (dp[i + 1] += dp[i]) %= MOD; if (i + 1 < nexts[i]) { (dp[nexts[i]] += dp[i]) %= MOD; } } cout << dp[N] << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using namespace placeholders; using LL = long long; using ULL = unsigned long long; using VI = vector<int>; using VVI = vector<vector<int>>; using VS = vector<string>; using ISS = istringstream; using OSS = ostringstream; using PII = pair<int, int>; using VPII = vector<pair<int, int>>; template <typename T = int> using VT = vector<T>; template <typename T = int> using VVT = vector<vector<T>>; template <typename T = int> using LIM = numeric_limits<T>; template <typename T> inline istream &operator>>(istream &s, vector<T> &v) { for (T &t : v) { s >> t; } return s; } template <typename T> inline ostream &operator<<(ostream &s, const vector<T> &v) { for (int i = 0; i < int(v.size()); ++i) { s << (" " + !i) << v[i]; } return s; } void in_impl(){}; template <typename T, typename... TS> void in_impl(T &head, TS &...tail) { cin >> head; in_impl(tail...); } #define IN(T, ...) \ T __VA_ARGS__; \ in_impl(__VA_ARGS__); template <typename T> struct getv_fmt; template <> struct getv_fmt<int> { static constexpr const char *fmt = "%d"; }; template <> struct getv_fmt<long long> { static constexpr const char *fmt = "%lld"; }; template <typename T> void getv(std::vector<T> &v) { for_each(begin(v), end(v), [](T &a) { scanf(getv_fmt<T>::fmt, &a); }); }; template <typename T> inline T fromString(const string &s) { T res; istringstream iss(s); iss >> res; return res; } template <typename T> inline string toString(const T &a) { ostringstream oss; oss << a; return oss.str(); } #define NUMBERED(name, number) NUMBERED2(name, number) #define NUMBERED2(name, number) name##_##number #define REP1(n) REP2(NUMBERED(REP_COUNTER, __LINE__), n) #define REP2(i, n) REP3(i, 0, n) #define REP3(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define GET_REP(a, b, c, F, ...) F #define REP(...) GET_REP(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__) #define FOR(e, c) for (auto &&e : c) #define ALL(c) begin(c), end(c) #define AALL(a) \ (remove_all_extents<decltype(a)>::type *)a, \ (remove_all_extents<decltype(a)>::type *)a + \ sizeof(a) / sizeof(remove_all_extents<decltype(a)>::type) #define DRANGE(c, p) begin(c), begin(c) + (p), end(c) #define SZ(v) ((int)(v).size()) #define EXIST(c, e) ((c).find(e) != (c).end()) template <typename T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } template <typename T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } #define PB push_back #define EM emplace #define EB emplace_back #define BI back_inserter #define MP make_pair #define fst first #define snd second #define DUMP(x) cerr << #x << " = " << (x) << endl constexpr int MOD = 1'000'000'007; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(12) << fixed; IN(int, N); VI A(N); cin >> A; transform(ALL(A), begin(A), bind(minus<int>(), _1, 1)); VI nexts(N); { VVI occurences(1 << 18); REP(i, N) { occurences[A[i]].PB(i); } FOR(row, occurences) { REP(i, SZ(row) - 1) { nexts[row[i]] = row[i + 1]; } } } static int dp[1 << 18]; // dp[ position of leftmost ] := # of ways dp[0] = 1; REP(i, N) { (dp[i + 1] += dp[i]) %= MOD; if (i + 1 < nexts[i]) { (dp[nexts[i]] += dp[i]) %= MOD; } } cout << dp[N] << endl; return 0; }
replace
143
144
143
144
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> #define mp make_pair #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair<ll, ll>; const ll INF = 1LL << 30; const ll LINF = 1LL << 60; const double eps = 1e-9; const ll MOD = 1000000007LL; template <typename T> void chmin(T &a, T b) { a = min(a, b); }; template <typename T> void chmax(T &a, T b) { a = max(a, b); }; 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); }; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int main() { int n; cin >> n; vector<int> c(n + 10), b(100010, -1); for (int i = 1; i <= n; i++) cin >> c[i]; vector<ll> dp(n + 10); dp[0] = 1; for (int i = 1; i <= n; i++) { dp[i] += dp[i - 1]; if (b[c[i]] != -1 && b[c[i]] != i - 1) { dp[i] += dp[b[c[i]]]; } b[c[i]] = i; dp[i] %= MOD; } cout << dp[n] << endl; }
#include <bits/stdc++.h> #define mp make_pair #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair<ll, ll>; const ll INF = 1LL << 30; const ll LINF = 1LL << 60; const double eps = 1e-9; const ll MOD = 1000000007LL; template <typename T> void chmin(T &a, T b) { a = min(a, b); }; template <typename T> void chmax(T &a, T b) { a = max(a, b); }; 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); }; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int main() { int n; cin >> n; vector<int> c(n + 10), b(200010, -1); for (int i = 1; i <= n; i++) cin >> c[i]; vector<ll> dp(n + 10); dp[0] = 1; for (int i = 1; i <= n; i++) { dp[i] += dp[i - 1]; if (b[c[i]] != -1 && b[c[i]] != i - 1) { dp[i] += dp[b[c[i]]]; } b[c[i]] = i; dp[i] %= MOD; } cout << dp[n] << endl; }
replace
30
31
30
31
0
p03096
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define MOD 1000000007 int N; vector<int> C; vector<int> memo; vector<int> nearest; vector<int> ind; int f(int i) { if (i >= nearest.size()) return 1; if (memo[i] != -1) return memo[i]; if (nearest[i] == -1) { return memo[i] = f(i + 1) % MOD; } else { return memo[i] = (f(nearest[i]) + f(i + 1)) % MOD; } } int main() { cin >> N; C.resize(N); for (auto &e : C) cin >> e; C.erase(unique(C.begin(), C.end()), C.end()); ind.assign(N + 1, -1); nearest.assign(C.size(), -1); for (int i = C.size() - 1; i >= 0; i--) { if (ind[C[i]] != -1) nearest[i] = ind[C[i]]; ind[C[i]] = i; } memo.assign(C.size(), -1); ll ans = f(0); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define MOD 1000000007 int N; vector<int> C; vector<int> memo; vector<int> nearest; vector<int> ind; int f(int i) { if (i >= nearest.size()) return 1; if (memo[i] != -1) return memo[i]; if (nearest[i] == -1) { return memo[i] = f(i + 1) % MOD; } else { return memo[i] = (f(nearest[i]) + f(i + 1)) % MOD; } } int main() { cin >> N; C.resize(N); for (auto &e : C) cin >> e; C.erase(unique(C.begin(), C.end()), C.end()); ind.assign(200000 + 1, -1); nearest.assign(C.size(), -1); for (int i = C.size() - 1; i >= 0; i--) { if (ind[C[i]] != -1) nearest[i] = ind[C[i]]; ind[C[i]] = i; } memo.assign(C.size(), -1); ll ans = f(0); cout << ans << endl; }
replace
31
32
31
32
0
p03097
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fr(i, n) for (int i = 0; i < (n); ++i) #define foor(i, a, b) for (int i = (a); i <= (b); ++i) #define rf(i, n) for (int i = (n); i--;) #define roof(i, b, a) for (int i = (b); i >= (a); --i) #define elsif else if #define all(x) x.begin(), x.end() #define Sort(x) sort(all(x)) #define Reverse(x) reverse(all(x)) #define PQ priority_queue #define NP(x) next_permutation(all(x)) #define M_PI 3.14159265358979323846 #define popcount __builtin_popcount using namespace std; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<int> vi; typedef vector<vi> vvi; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef unsigned long long ull; typedef vector<ull> vu; typedef vector<vu> vvu; typedef double dbl; typedef vector<dbl> vd; typedef vector<vd> vvd; typedef string str; typedef vector<str> vs; typedef vector<vs> vvs; typedef pair<int, int> pii; typedef vector<pii> vpii; typedef map<int, int> mii; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef map<ll, ll> mll; typedef pair<dbl, dbl> pdd; typedef vector<pdd> vpdd; typedef map<dbl, dbl> mdd; typedef pair<str, str> pss; typedef vector<pss> vpss; typedef map<str, str> mss; typedef pair<int, ll> pil; typedef vector<pil> vpil; typedef map<int, ll> mil; typedef pair<ll, int> pli; typedef vector<pli> vpli; typedef map<ll, int> mli; typedef pair<dbl, int> pdi; typedef vector<pdi> vpdi; typedef map<dbl, int> mdi; template <typename T> vector<T> &operator<<(vector<T> &v, const T t) { v.push_back(t); return v; } template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) { m.insert(t); return m; } template <typename T> set<T> &operator<<(set<T> &s, const T t) { s.insert(t); return s; } template <typename T> stack<T> &operator<<(stack<T> &s, const T t) { s.push(t); return s; } template <typename T> stack<T> &operator>>(stack<T> &s, T &t) { t = s.top(); s.pop(); return s; } template <typename T> queue<T> &operator<<(queue<T> &q, const T t) { q.push(t); return q; } template <typename T> queue<T> &operator>>(queue<T> &q, T &t) { t = q.front(); q.pop(); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) { q.push(t); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator>>(PQ<T, vector<T>, U> &q, T &t) { t = q.top(); q.pop(); return q; } template <typename T, typename U> istream &operator>>(istream &s, pair<T, U> &p) { return s >> p.first >> p.second; } template <typename T> istream &operator>>(istream &s, vector<T> &v) { fr(i, v.size()) { s >> v[i]; } return s; } template <typename T, typename U> ostream &operator<<(ostream &s, const pair<T, U> p) { return s << p.first << " " << p.second; } // template<typename T>ostream&operator<<(ostream&s,const vector<T>v){for(auto // a:v){s<<a<<endl;}return s;} template <typename T> ostream &operator<<(ostream &s, const vector<T> v) { fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; } return s; } template <typename T> ostream &operator<<(ostream &s, const deque<T> d) { fr(i, d.size()) { i ? s << " " << d[i] : s << d[i]; } return s; } template <typename T> _Bit_reference operator&=(_Bit_reference b, T t) { return b = b & t; } template <typename T> _Bit_reference operator^=(_Bit_reference b, T t) { return b = b ^ t; } template <typename T> _Bit_reference operator|=(_Bit_reference b, T t) { return b = b | t; } template <typename T, typename U> pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { return {a.first + b.first, a.second + b.second}; } template <typename T, typename U> pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { return {a.first - b.first, a.second - b.second}; } void print(void) { cout << endl; } template <typename T> void print(T t) { cout << t << endl; } template <typename T, typename... U> void print(T &&t, U &&...u) { cout << t << " "; print(forward<U>(u)...); } bool YN(bool b) { print(b ? "YES" : "NO"); return b; } bool PI(bool b) { print(b ? "POSSIBLE" : "IMPOSSIBLE"); return b; } bool Yn(bool b) { print(b ? "Yes" : "No"); return b; } bool Pi(bool b) { print(b ? "Possible" : "Impossible"); return b; } bool yn(bool b) { print(b ? "yes" : "no"); return b; } bool pi(bool b) { print(b ? "possible" : "impossible"); return b; } const int MD = 1e9 + 7; template <typename T> str to_string(const T &n) { ostringstream s; s << n; return s.str(); } template <typename T> T &chmax(T &a, T b) { return a = max(a, b); } template <typename T> T &chmin(T &a, T b) { return a = min(a, b); } template <typename T, typename U> vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> &E, const U s, const T inf) { using P = pair<T, U>; vector<P> d; fr(i, E.size()) { d << P{inf, i}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } template <typename T, typename U> map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s, const T inf) { using P = pair<T, U>; map<U, P> d; for (pair<U, vector<P>> e : E) { d[e.first] = P{inf, e.first}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } ll maxflow(vector<mil> &E, int s, int t) { ll z = 0; vi b(E.size(), -1); for (int i = 0;; ++i) { static auto dfs = [&](int v, ll f, auto &dfs) -> ll { if (v == t) return f; b[v] = i; for (auto &p : E[v]) { if (b[p.first] < i && p.second) { if (ll r = dfs(p.first, min(f, p.second), dfs)) { p.second -= r; E[p.first][v] += r; return r; } } } return 0; }; ll x = dfs(s, ll(1e18), dfs); z += x; if (x == 0) return z; } } template <typename T> T distsq(pair<T, T> a, pair<T, T> b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } template <typename T> T max(const vector<T> a) { T m = a[0]; for (T e : a) { m = max(m, e); } return m; } template <typename T> T min(const vector<T> a) { T m = a[0]; for (T e : a) { m = min(m, e); } return m; } template <typename T> T gcd(const T a, const T b) { return a ? gcd(b % a, a) : b; } template <typename T> T gcd(const vector<T> a) { T g = a[0]; for (T e : a) { g = gcd(g, e); } return g; } template <typename T> vector<T> LIS(const vector<T> A) { vector<T> B; for (T a : A) { auto it = lower_bound(all(B), a); if (it == B.end()) { B << a; } else { *it = a; } } return B; } template <typename T> vector<T> LCS(vector<T> A, vector<T> B) { int N = A.size(), M = B.size(); vector<vector<pair<int, pii>>> d(N + 1, vector<pair<int, pii>>(M + 1)); fr(i, N) { fr(j, M) { if (A[i] == B[j]) { d[i + 1][j + 1] = {d[i][j].first + 1, {i, j}}; } else { d[i + 1][j + 1] = max(d[i][j + 1], d[i + 1][j]); } } } vector<T> r; for (pii p = {N, M}; d[p.first][p.second].first; p = d[p.first][p.second].second) { r << A[d[p.first][p.second].second.first]; } Reverse(r); return r; } str LCS(str S, str T) { vector<char> s = LCS(vector<char>(S.begin(), S.end()), vector<char>(T.begin(), T.end())); return str(s.begin(), s.end()); } template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) { if (V.size() <= 3) { return V; } Sort(V); rf(i, V.size() - 1) V << V[i]; vector<pair<T, T>> r; for (pair<T, T> p : V) { int s = r.size(); while (s >= 2 && (p.second - r[s - 1].second) * (p.first - r[s - 2].first) < (p.second - r[s - 2].second) * (p.first - r[s - 1].first)) { r.pop_back(); --s; } r << p; } r.pop_back(); return r; } class UnionFind { vi p, r, s; public: UnionFind(int N) { p = r = vi(N); s = vi(N, 1); fr(i, N) { p[i] = i; } } int find(int i) { return p[i] = p[i] == i ? i : find(p[i]); } void unite(int a, int b) { if ((a = find(a)) != (b = find(b))) { if (r[a] > r[b]) { swap(a, b); } s[b] += s[a]; r[p[a] = b] += r[a] == r[b]; } } bool same(int a, int b) { return find(a) == find(b); } int size(int x) { return s[find(x)]; } }; ll strmod(const str &s, const int m) { ll x = 0; fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; } return x; } vvl mul(const vvl &A, const vvl &B, const int m) { vvl C; fr(y, A.size()) { C << vl(B[y].size()); } fr(y, C.size()) { fr(x, C[y].size()) { fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; } } } return C; } vvl pow(const vvl &A, const ll n, const int m) { vvl B; fr(y, A.size()) { B << vl(A.size()); } if (n == 0) { fr(i, B.size()) { B[i][i] = 1; } } elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); } else { vvl C = pow(A, n / 2, m); B = mul(C, C, m); } return B; } ll pow(const ll a, const ll n, const int m) { ll t; return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) * (t = pow(a, n >> 1, m), t * t % m) % m : !!a; } ll inv(const ll x, const int p) { return pow(x, p - 2, p); } ll inv(const ll x) { return inv(x, MD); } vpll fact(const int n, const int p) { vpll v(n + 1); v[0].first = 1; foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; } v[n].second = inv(v[n].first, p); roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; } return v; } class Combination { const vpll f; const int M; public: Combination(int n, int m) : f(fact(n, m)), M(m) {} Combination(int n) : Combination(n, MD) {} ll P(int n, int k) { return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M; } ll C(int n, int k) { return P(n, k) * f[k].second % M; } ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); } }; ll C2(const int n) { return (ll)n * ~-n / 2; } ll sum(const vi a) { ll s = 0; for (int e : a) { s += e; } return s; } ll sum(const vl a) { ll s = 0; for (ll e : a) { s += e; } return s; } template <typename T> int MSB(T N) { int r = -1; for (; N > 0; N /= 2) { ++r; } return r; } template <typename T> class SegmentTree { vector<T> S; T (*const op)(T a, T b); const T zero; const int B; public: SegmentTree(int N, T (*f)(T a, T b), const T zero) : S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero), B(1 << MSB(N - 1) + 1) {} SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero) : SegmentTree(v.size(), f, zero) { fr(i, v.size()) { S[S.size() / 2 + i] = v[i]; } roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); } } T calc(int l, int r) { l += B; r += B; if (l > r) { return zero; } if (l == r) { return S[l]; } T L = S[l], R = S[r]; for (; l / 2 < r / 2; l /= 2, r /= 2) { if (l % 2 == 0) { L = op(L, S[l + 1]); } if (r % 2 == 1) { R = op(S[r - 1], R); } } return op(L, R); } void replace(int i, T x) { for (S[i += B] = x; i != 1; i /= 2) { if (i % 2) { S[i / 2] = op(S[i - 1], S[i]); } else { S[i / 2] = op(S[i], S[i + 1]); } } } void add(int i, T x) { replace(i, op(S[B + i], x)); } T top() { return S[1]; } }; ll BITsum(vl &B, int i) { ll z = 0; while (i > 0) { z += B[i]; i -= i & -i; } return z; } void BITadd(vl &B, int i, ll x) { while (i < B.size()) { B[i] += x; i += i & -i; } } ll fib(const ll n, const int m) { ll a, b, c, d, A, B, C, D; a = 1; b = 0; c = 0; d = 1; rf(i, 63) { A = a * a + b * c; B = a * b + b * d; C = c * a + d * c; D = c * b + d * d; if (n >> i & 1) { a = A; b = B; c = C; d = D; A = a + b; B = a; C = c + d; D = c; } a = A % m; b = B % m; c = C % m; d = D % m; } return b; } vi primes(int n) { vb b(n + 1); vi p; foor(i, 2, n) { if (!b[i]) { p << i; for (int j = 2 * i; j <= n; j += i) { b[j] = true; } } } return p; } vb isprime(const int n) { vb v(n + 1, true); v[0] = v[1] = false; foor(i, 2, n) { if (v[i]) { for (int j = 2 * i; j <= n; j += i) { v[j] = false; } } } return v; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N, A, B; cin >> N >> A >> B; vi g(1 << N); fr(i, 1 << N) { g[i ^ i >> 1] = i; } if (YN(g[A] % 2 != g[B] % 2)) { vi v; if (g[A ^ B] % 4 == 3) { fr(i, g[A ^ B]) { v << (i ^ i >> 1); } for (int i = v.size() + 2; i != (1 << N) + 1; i += vi{0, 1, 3, 0}[i % 4]) { v << (i ^ i >> 1); } for (int i = (1 << N) - 1; v.size() != (1 << N); i -= vi{1, 0, 0, 3}[i % 4]) { v << (i ^ i >> 1); } } else { fr(i, g[A ^ B]) { v << (i ^ i >> 1); } for (int i = v.size() + 2; i != (1 << N); i += vi{3, 0, 0, 1}[i % 4]) { v << (i ^ i >> 1); } for (int i = (1 << N) - 2; v.size() != (1 << N); i -= vi{0, 3, 1, 0}[i % 4]) { v << (i ^ i >> 1); } } fr(i, 1 << N) { v[i] ^= A; } print(v); } return 0; }
#include <bits/stdc++.h> #define fr(i, n) for (int i = 0; i < (n); ++i) #define foor(i, a, b) for (int i = (a); i <= (b); ++i) #define rf(i, n) for (int i = (n); i--;) #define roof(i, b, a) for (int i = (b); i >= (a); --i) #define elsif else if #define all(x) x.begin(), x.end() #define Sort(x) sort(all(x)) #define Reverse(x) reverse(all(x)) #define PQ priority_queue #define NP(x) next_permutation(all(x)) #define M_PI 3.14159265358979323846 #define popcount __builtin_popcount using namespace std; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<int> vi; typedef vector<vi> vvi; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef unsigned long long ull; typedef vector<ull> vu; typedef vector<vu> vvu; typedef double dbl; typedef vector<dbl> vd; typedef vector<vd> vvd; typedef string str; typedef vector<str> vs; typedef vector<vs> vvs; typedef pair<int, int> pii; typedef vector<pii> vpii; typedef map<int, int> mii; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef map<ll, ll> mll; typedef pair<dbl, dbl> pdd; typedef vector<pdd> vpdd; typedef map<dbl, dbl> mdd; typedef pair<str, str> pss; typedef vector<pss> vpss; typedef map<str, str> mss; typedef pair<int, ll> pil; typedef vector<pil> vpil; typedef map<int, ll> mil; typedef pair<ll, int> pli; typedef vector<pli> vpli; typedef map<ll, int> mli; typedef pair<dbl, int> pdi; typedef vector<pdi> vpdi; typedef map<dbl, int> mdi; template <typename T> vector<T> &operator<<(vector<T> &v, const T t) { v.push_back(t); return v; } template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) { m.insert(t); return m; } template <typename T> set<T> &operator<<(set<T> &s, const T t) { s.insert(t); return s; } template <typename T> stack<T> &operator<<(stack<T> &s, const T t) { s.push(t); return s; } template <typename T> stack<T> &operator>>(stack<T> &s, T &t) { t = s.top(); s.pop(); return s; } template <typename T> queue<T> &operator<<(queue<T> &q, const T t) { q.push(t); return q; } template <typename T> queue<T> &operator>>(queue<T> &q, T &t) { t = q.front(); q.pop(); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) { q.push(t); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator>>(PQ<T, vector<T>, U> &q, T &t) { t = q.top(); q.pop(); return q; } template <typename T, typename U> istream &operator>>(istream &s, pair<T, U> &p) { return s >> p.first >> p.second; } template <typename T> istream &operator>>(istream &s, vector<T> &v) { fr(i, v.size()) { s >> v[i]; } return s; } template <typename T, typename U> ostream &operator<<(ostream &s, const pair<T, U> p) { return s << p.first << " " << p.second; } // template<typename T>ostream&operator<<(ostream&s,const vector<T>v){for(auto // a:v){s<<a<<endl;}return s;} template <typename T> ostream &operator<<(ostream &s, const vector<T> v) { fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; } return s; } template <typename T> ostream &operator<<(ostream &s, const deque<T> d) { fr(i, d.size()) { i ? s << " " << d[i] : s << d[i]; } return s; } template <typename T> _Bit_reference operator&=(_Bit_reference b, T t) { return b = b & t; } template <typename T> _Bit_reference operator^=(_Bit_reference b, T t) { return b = b ^ t; } template <typename T> _Bit_reference operator|=(_Bit_reference b, T t) { return b = b | t; } template <typename T, typename U> pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { return {a.first + b.first, a.second + b.second}; } template <typename T, typename U> pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { return {a.first - b.first, a.second - b.second}; } void print(void) { cout << endl; } template <typename T> void print(T t) { cout << t << endl; } template <typename T, typename... U> void print(T &&t, U &&...u) { cout << t << " "; print(forward<U>(u)...); } bool YN(bool b) { print(b ? "YES" : "NO"); return b; } bool PI(bool b) { print(b ? "POSSIBLE" : "IMPOSSIBLE"); return b; } bool Yn(bool b) { print(b ? "Yes" : "No"); return b; } bool Pi(bool b) { print(b ? "Possible" : "Impossible"); return b; } bool yn(bool b) { print(b ? "yes" : "no"); return b; } bool pi(bool b) { print(b ? "possible" : "impossible"); return b; } const int MD = 1e9 + 7; template <typename T> str to_string(const T &n) { ostringstream s; s << n; return s.str(); } template <typename T> T &chmax(T &a, T b) { return a = max(a, b); } template <typename T> T &chmin(T &a, T b) { return a = min(a, b); } template <typename T, typename U> vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> &E, const U s, const T inf) { using P = pair<T, U>; vector<P> d; fr(i, E.size()) { d << P{inf, i}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } template <typename T, typename U> map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s, const T inf) { using P = pair<T, U>; map<U, P> d; for (pair<U, vector<P>> e : E) { d[e.first] = P{inf, e.first}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } ll maxflow(vector<mil> &E, int s, int t) { ll z = 0; vi b(E.size(), -1); for (int i = 0;; ++i) { static auto dfs = [&](int v, ll f, auto &dfs) -> ll { if (v == t) return f; b[v] = i; for (auto &p : E[v]) { if (b[p.first] < i && p.second) { if (ll r = dfs(p.first, min(f, p.second), dfs)) { p.second -= r; E[p.first][v] += r; return r; } } } return 0; }; ll x = dfs(s, ll(1e18), dfs); z += x; if (x == 0) return z; } } template <typename T> T distsq(pair<T, T> a, pair<T, T> b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } template <typename T> T max(const vector<T> a) { T m = a[0]; for (T e : a) { m = max(m, e); } return m; } template <typename T> T min(const vector<T> a) { T m = a[0]; for (T e : a) { m = min(m, e); } return m; } template <typename T> T gcd(const T a, const T b) { return a ? gcd(b % a, a) : b; } template <typename T> T gcd(const vector<T> a) { T g = a[0]; for (T e : a) { g = gcd(g, e); } return g; } template <typename T> vector<T> LIS(const vector<T> A) { vector<T> B; for (T a : A) { auto it = lower_bound(all(B), a); if (it == B.end()) { B << a; } else { *it = a; } } return B; } template <typename T> vector<T> LCS(vector<T> A, vector<T> B) { int N = A.size(), M = B.size(); vector<vector<pair<int, pii>>> d(N + 1, vector<pair<int, pii>>(M + 1)); fr(i, N) { fr(j, M) { if (A[i] == B[j]) { d[i + 1][j + 1] = {d[i][j].first + 1, {i, j}}; } else { d[i + 1][j + 1] = max(d[i][j + 1], d[i + 1][j]); } } } vector<T> r; for (pii p = {N, M}; d[p.first][p.second].first; p = d[p.first][p.second].second) { r << A[d[p.first][p.second].second.first]; } Reverse(r); return r; } str LCS(str S, str T) { vector<char> s = LCS(vector<char>(S.begin(), S.end()), vector<char>(T.begin(), T.end())); return str(s.begin(), s.end()); } template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) { if (V.size() <= 3) { return V; } Sort(V); rf(i, V.size() - 1) V << V[i]; vector<pair<T, T>> r; for (pair<T, T> p : V) { int s = r.size(); while (s >= 2 && (p.second - r[s - 1].second) * (p.first - r[s - 2].first) < (p.second - r[s - 2].second) * (p.first - r[s - 1].first)) { r.pop_back(); --s; } r << p; } r.pop_back(); return r; } class UnionFind { vi p, r, s; public: UnionFind(int N) { p = r = vi(N); s = vi(N, 1); fr(i, N) { p[i] = i; } } int find(int i) { return p[i] = p[i] == i ? i : find(p[i]); } void unite(int a, int b) { if ((a = find(a)) != (b = find(b))) { if (r[a] > r[b]) { swap(a, b); } s[b] += s[a]; r[p[a] = b] += r[a] == r[b]; } } bool same(int a, int b) { return find(a) == find(b); } int size(int x) { return s[find(x)]; } }; ll strmod(const str &s, const int m) { ll x = 0; fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; } return x; } vvl mul(const vvl &A, const vvl &B, const int m) { vvl C; fr(y, A.size()) { C << vl(B[y].size()); } fr(y, C.size()) { fr(x, C[y].size()) { fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; } } } return C; } vvl pow(const vvl &A, const ll n, const int m) { vvl B; fr(y, A.size()) { B << vl(A.size()); } if (n == 0) { fr(i, B.size()) { B[i][i] = 1; } } elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); } else { vvl C = pow(A, n / 2, m); B = mul(C, C, m); } return B; } ll pow(const ll a, const ll n, const int m) { ll t; return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) * (t = pow(a, n >> 1, m), t * t % m) % m : !!a; } ll inv(const ll x, const int p) { return pow(x, p - 2, p); } ll inv(const ll x) { return inv(x, MD); } vpll fact(const int n, const int p) { vpll v(n + 1); v[0].first = 1; foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; } v[n].second = inv(v[n].first, p); roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; } return v; } class Combination { const vpll f; const int M; public: Combination(int n, int m) : f(fact(n, m)), M(m) {} Combination(int n) : Combination(n, MD) {} ll P(int n, int k) { return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M; } ll C(int n, int k) { return P(n, k) * f[k].second % M; } ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); } }; ll C2(const int n) { return (ll)n * ~-n / 2; } ll sum(const vi a) { ll s = 0; for (int e : a) { s += e; } return s; } ll sum(const vl a) { ll s = 0; for (ll e : a) { s += e; } return s; } template <typename T> int MSB(T N) { int r = -1; for (; N > 0; N /= 2) { ++r; } return r; } template <typename T> class SegmentTree { vector<T> S; T (*const op)(T a, T b); const T zero; const int B; public: SegmentTree(int N, T (*f)(T a, T b), const T zero) : S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero), B(1 << MSB(N - 1) + 1) {} SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero) : SegmentTree(v.size(), f, zero) { fr(i, v.size()) { S[S.size() / 2 + i] = v[i]; } roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); } } T calc(int l, int r) { l += B; r += B; if (l > r) { return zero; } if (l == r) { return S[l]; } T L = S[l], R = S[r]; for (; l / 2 < r / 2; l /= 2, r /= 2) { if (l % 2 == 0) { L = op(L, S[l + 1]); } if (r % 2 == 1) { R = op(S[r - 1], R); } } return op(L, R); } void replace(int i, T x) { for (S[i += B] = x; i != 1; i /= 2) { if (i % 2) { S[i / 2] = op(S[i - 1], S[i]); } else { S[i / 2] = op(S[i], S[i + 1]); } } } void add(int i, T x) { replace(i, op(S[B + i], x)); } T top() { return S[1]; } }; ll BITsum(vl &B, int i) { ll z = 0; while (i > 0) { z += B[i]; i -= i & -i; } return z; } void BITadd(vl &B, int i, ll x) { while (i < B.size()) { B[i] += x; i += i & -i; } } ll fib(const ll n, const int m) { ll a, b, c, d, A, B, C, D; a = 1; b = 0; c = 0; d = 1; rf(i, 63) { A = a * a + b * c; B = a * b + b * d; C = c * a + d * c; D = c * b + d * d; if (n >> i & 1) { a = A; b = B; c = C; d = D; A = a + b; B = a; C = c + d; D = c; } a = A % m; b = B % m; c = C % m; d = D % m; } return b; } vi primes(int n) { vb b(n + 1); vi p; foor(i, 2, n) { if (!b[i]) { p << i; for (int j = 2 * i; j <= n; j += i) { b[j] = true; } } } return p; } vb isprime(const int n) { vb v(n + 1, true); v[0] = v[1] = false; foor(i, 2, n) { if (v[i]) { for (int j = 2 * i; j <= n; j += i) { v[j] = false; } } } return v; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N, A, B; cin >> N >> A >> B; if (N == 1) { YN(true); print(A ? "1 0" : "0 1"); return 0; } vi g(1 << N); fr(i, 1 << N) { g[i ^ i >> 1] = i; } if (YN(g[A] % 2 != g[B] % 2)) { vi v; if (g[A ^ B] % 4 == 3) { fr(i, g[A ^ B]) { v << (i ^ i >> 1); } for (int i = v.size() + 2; i != (1 << N) + 1; i += vi{0, 1, 3, 0}[i % 4]) { v << (i ^ i >> 1); } for (int i = (1 << N) - 1; v.size() != (1 << N); i -= vi{1, 0, 0, 3}[i % 4]) { v << (i ^ i >> 1); } } else { fr(i, g[A ^ B]) { v << (i ^ i >> 1); } for (int i = v.size() + 2; i != (1 << N); i += vi{3, 0, 0, 1}[i % 4]) { v << (i ^ i >> 1); } for (int i = (1 << N) - 2; v.size() != (1 << N); i -= vi{0, 3, 1, 0}[i % 4]) { v << (i ^ i >> 1); } } fr(i, 1 << N) { v[i] ^= A; } print(v); } return 0; }
insert
548
548
548
553
TLE
p03097
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n; int dfs(int pos, int cnt, int a, int bit, vector<int> &tmp) { int keta = (pos + cnt) % n; if (cnt == 0) { if ((bit >> cnt) & 1) return a; else { a ^= (1 << keta); tmp.push_back(a); return a; } } else { if ((bit >> cnt) & 1) return dfs(pos, cnt - 1, a, bit, tmp); else { a = dfs(pos, cnt - 1, a, bit, tmp); a ^= (1 << keta); tmp.push_back(a); a = dfs(pos, cnt - 1, a, bit, tmp); return a; } } } void solve(int a, int b, int bit, vector<int> &v) { if (a == b) { v.push_back(b); return; } for (int i = 0; i < n; i++) { if (((a ^ b) >> i) & 1) { bit ^= (1 << i); for (int j = 0; j < n; j++) { vector<int> tmp; tmp.push_back(a); int x = dfs(j, 0, a, bit, tmp); if (x != b) { v.insert(v.end(), tmp.begin(), tmp.end()); solve(x ^ (1 << i), b, bit, v); return; } } } } } int main() { int a, b; cin >> n >> a >> b; if ((__builtin_popcount(a ^ b) & 1) == 0) { cout << "NO" << endl; return 0; } vector<int> v; int bit = 0; solve(a, b, bit, v); cout << "YES" << endl; for (int i = 0; i < (1 << n); i++) { cout << v[i] << ((i + 1 == (1 << n)) ? "\n" : " "); } return 0; }
#include <bits/stdc++.h> using namespace std; int n; int dfs(int pos, int cnt, int a, int bit, vector<int> &tmp) { int keta = (pos + cnt) % n; if (cnt == 0) { if ((bit >> cnt) & 1) return a; else { a ^= (1 << keta); tmp.push_back(a); return a; } } else { if ((bit >> cnt) & 1) return dfs(pos, cnt - 1, a, bit, tmp); else { a = dfs(pos, cnt - 1, a, bit, tmp); a ^= (1 << keta); tmp.push_back(a); a = dfs(pos, cnt - 1, a, bit, tmp); return a; } } } void solve(int a, int b, int bit, vector<int> &v) { if (a == b) { v.push_back(b); return; } for (int i = 0; i < n; i++) { if (((a ^ b) >> i) & 1) { bit ^= (1 << i); for (int j = 0; j < n; j++) { vector<int> tmp; tmp.push_back(a); int x = dfs(j, n - 1, a, bit, tmp); if (x != b) { v.insert(v.end(), tmp.begin(), tmp.end()); solve(x ^ (1 << i), b, bit, v); return; } } } } } int main() { int a, b; cin >> n >> a >> b; if ((__builtin_popcount(a ^ b) & 1) == 0) { cout << "NO" << endl; return 0; } vector<int> v; int bit = 0; solve(a, b, bit, v); cout << "YES" << endl; for (int i = 0; i < (1 << n); i++) { cout << v[i] << ((i + 1 == (1 << n)) ? "\n" : " "); } return 0; }
replace
37
38
37
38
0
p03097
C++
Runtime Error
/* ЗАПУСКАЕМ ░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░ ▄███▀░◐░░░▌░░░░░░░ ░░░░▌░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▌░░░░░▐▄▄░░░░░ ░░░░▌░░░░▄▀▒▒▀▀▀▀▄ ░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄ ░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄ ░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄ ░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄ ░░░░░░░░░░░▌▌░▌▌░░░░░ ░░░░░░░░░░░▌▌░▌▌░░░░░ ░░░░░░░░░▄▄▌▌▄▌▌░░░░░ */ #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chkmax(T1 &a, T2 b) { if (a < b) a = b; } #define files(FILENAME) \ read(FILENAME); \ write(FILENAME) #define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin) #define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout) #define all(c) (c).begin(), (c).end() #define sz(c) (int)(c).size() #define left left228 #define right right228 #define y1 y1228 #define mp make_pair #define pb push_back #define y2 y2228 const string FILENAME = "input"; vector<int> get(int a, int b, int n) { if (n == 1) { // cout << a << ' ' << b << endl; if (a + b != 1) { cout << "NO\n"; exit(0); } vector<int> res; res.pb(a); res.pb(b); return res; } int a1 = 0, b1 = 0; int pos = -1; vector<int> g; for (int i = 0; i < n; i++) { if ((a ^ b) & (1 << i)) { pos = i; int uk = 0; for (int j = 0; j < n; j++) { if (i != j) { if (a & (1 << j)) { a1 += 1 << uk; } if (b & (1 << j)) { b1 += 1 << uk; } g.pb(j); uk++; } } break; } } vector<int> res1 = get(a1, a1 ^ 1, n - 1); vector<int> res2 = get(a1 ^ 1, b1, n - 1); vector<int> res; for (auto x : res1) { int nx = 0; for (int f = 0; f < n - 1; f++) { if (x & (1 << f)) { nx += 1 << g[f]; } } if (a & (1 << pos)) { nx += 1 << pos; } res.pb(nx); } for (auto x : res2) { int nx = 0; for (int f = 0; f < n - 1; f++) { if (x & (1 << f)) { nx += 1 << g[f]; } } if (b & (1 << pos)) { nx += 1 << pos; } res.pb(nx); } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // read(FILENAME); int n, a, b; cin >> n >> a >> b; vector<int> res = get(a, b, n); cout << "YES\n"; for (auto x : res) { cout << x << ' '; } cout << '\n'; return 0; }
/* ЗАПУСКАЕМ ░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░ ▄███▀░◐░░░▌░░░░░░░ ░░░░▌░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▌░░░░░▐▄▄░░░░░ ░░░░▌░░░░▄▀▒▒▀▀▀▀▄ ░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄ ░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄ ░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄ ░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄ ░░░░░░░░░░░▌▌░▌▌░░░░░ ░░░░░░░░░░░▌▌░▌▌░░░░░ ░░░░░░░░░▄▄▌▌▄▌▌░░░░░ */ #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chkmax(T1 &a, T2 b) { if (a < b) a = b; } #define files(FILENAME) \ read(FILENAME); \ write(FILENAME) #define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin) #define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout) #define all(c) (c).begin(), (c).end() #define sz(c) (int)(c).size() #define left left228 #define right right228 #define y1 y1228 #define mp make_pair #define pb push_back #define y2 y2228 const string FILENAME = "input"; vector<int> get(int a, int b, int n) { if (n == 1) { // cout << a << ' ' << b << endl; if (a + b != 1) { cout << "NO\n"; exit(0); } vector<int> res; res.pb(a); res.pb(b); return res; } int a1 = 0, b1 = 0; int pos = -1; vector<int> g; for (int i = 0; i < n; i++) { if ((a ^ b) & (1 << i)) { pos = i; int uk = 0; for (int j = 0; j < n; j++) { if (i != j) { if (a & (1 << j)) { a1 += 1 << uk; } if (b & (1 << j)) { b1 += 1 << uk; } g.pb(j); uk++; } } break; } } if (pos == -1) { cout << "NO\n"; exit(0); } vector<int> res1 = get(a1, a1 ^ 1, n - 1); vector<int> res2 = get(a1 ^ 1, b1, n - 1); vector<int> res; for (auto x : res1) { int nx = 0; for (int f = 0; f < n - 1; f++) { if (x & (1 << f)) { nx += 1 << g[f]; } } if (a & (1 << pos)) { nx += 1 << pos; } res.pb(nx); } for (auto x : res2) { int nx = 0; for (int f = 0; f < n - 1; f++) { if (x & (1 << f)) { nx += 1 << g[f]; } } if (b & (1 << pos)) { nx += 1 << pos; } res.pb(nx); } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // read(FILENAME); int n, a, b; cin >> n >> a >> b; vector<int> res = get(a, b, n); cout << "YES\n"; for (auto x : res) { cout << x << ' '; } cout << '\n'; return 0; }
insert
103
103
103
107
0
p03097
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } #ifdef __LOCAL #define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n' #define debugArray(x, n) \ cerr << __LINE__ << ": " << #x << " = {"; \ for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \ cerr << ((hoge) ? "," : "") << x[hoge]; \ cerr << "}" << '\n' #define debugMatrix(x, h, w) \ cerr << __LINE__ << ": " << #x << " =\n"; \ for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) { \ cerr << ((hoge ? " {" : "{{")); \ for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga)) \ cerr << ((fuga ? ", " : "")) << x[hoge][fuga]; \ cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n'; \ } #else #define debug(x) (void(0)) #define debugArray(x, n) (void(0)) #define debugMatrix(x, h, w) (void(0)) #endif signed main() { cin.tie(0); ios::sync_with_stdio(0); int N, A, B; cin >> N >> A >> B; if (!(__builtin_popcount(A ^ B) & 1)) { cout << "NO" << '\n'; return 0; } auto f = [](int x, int i) { return (x >> (i + 1)) * (1 << i) ^ (x % (1 << i)); }; auto g = [](int x, int i, bool b) { return (x >> (i)) * (1 << (i + 1)) ^ (b << i) ^ (x % (1 << i)); }; function<vector<int>(int, int, int)> rec = [&](int n, int a, int b) { if (n == 1) return vector<int>({a, b}); vector<int> ret(1 << n); for (int i = 0; i < n; i++) { if (((a ^ b) >> i) & 1) { int aa = f(a, i); int bb = f(b, i); auto l = rec(n - 1, aa, aa ^ 1); auto r = rec(n - 1, aa ^ 1, bb); for (int j = 0; j < (1 << (n - 1)); j++) { ret[j] = g(l[j], i, (a >> i) & 1); ret[j + (1 << (n - 1))] = g(r[j], i, (b >> i) & 1); } } } return ret; }; auto ans = rec(N, A, B); cout << "YES" << '\n'; for (int i = 0; i < (1 << N); i++) cout << (i ? " " : "") << ans[i]; cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } #ifdef __LOCAL #define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n' #define debugArray(x, n) \ cerr << __LINE__ << ": " << #x << " = {"; \ for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \ cerr << ((hoge) ? "," : "") << x[hoge]; \ cerr << "}" << '\n' #define debugMatrix(x, h, w) \ cerr << __LINE__ << ": " << #x << " =\n"; \ for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) { \ cerr << ((hoge ? " {" : "{{")); \ for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga)) \ cerr << ((fuga ? ", " : "")) << x[hoge][fuga]; \ cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n'; \ } #else #define debug(x) (void(0)) #define debugArray(x, n) (void(0)) #define debugMatrix(x, h, w) (void(0)) #endif signed main() { cin.tie(0); ios::sync_with_stdio(0); int N, A, B; cin >> N >> A >> B; if (!(__builtin_popcount(A ^ B) & 1)) { cout << "NO" << '\n'; return 0; } auto f = [](int x, int i) { return (x >> (i + 1)) * (1 << i) ^ (x % (1 << i)); }; auto g = [](int x, int i, bool b) { return (x >> (i)) * (1 << (i + 1)) ^ (b << i) ^ (x % (1 << i)); }; function<vector<int>(int, int, int)> rec = [&](int n, int a, int b) { if (n == 1) return vector<int>({a, b}); vector<int> ret(1 << n); for (int i = 0; i < n; i++) { if (((a ^ b) >> i) & 1) { int aa = f(a, i); int bb = f(b, i); auto l = rec(n - 1, aa, aa ^ 1); auto r = rec(n - 1, aa ^ 1, bb); for (int j = 0; j < (1 << (n - 1)); j++) { ret[j] = g(l[j], i, (a >> i) & 1); ret[j + (1 << (n - 1))] = g(r[j], i, (b >> i) & 1); } break; } } return ret; }; auto ans = rec(N, A, B); cout << "YES" << '\n'; for (int i = 0; i < (1 << N); i++) cout << (i ? " " : "") << ans[i]; cout << '\n'; return 0; }
insert
57
57
57
58
TLE
p03097
C++
Runtime Error
/** * author: tourist * created: 16.03.2019 15:29:11 **/ #include <bits/stdc++.h> using namespace std; int remove_bit(int x, int j) { int low = x & ((1 << j) - 1); return ((x >> (j + 1)) << j) + low; } int insert_bit(int x, int j, int v) { int low = x & ((1 << j) - 1); return ((x >> j) << (j + 1)) + (v << j) + low; } vector<int> solve(int n, int a, int b) { if (n == 1) { return {a, b}; } for (int j = 0; j < n; j++) { if ((a ^ b) & (1 << j)) { int aa = remove_bit(a, j); int bb = remove_bit(b, j); for (int u = 0; u < (1 << n); u++) { int uu = remove_bit(u, j); if (uu != aa && uu != bb) { vector<int> p1 = solve(n - 1, aa, uu); vector<int> p2 = solve(n - 1, uu, bb); for (int &x : p1) { x = insert_bit(x, j, (a >> j) & 1); } for (int &x : p2) { x = insert_bit(x, j, (b >> j) & 1); } p1.insert(p1.end(), p2.begin(), p2.end()); return p1; } } } } assert(false); return {}; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, a, b; cin >> n >> a >> b; if (__builtin_popcount(a ^ b) % 2 == 0) { cout << "NO" << '\n'; return 0; } cout << "YES" << '\n'; vector<int> p = solve(n, a, b); set<int> val(p.begin(), p.end()); assert((int)val.size() == (1 << n)); for (int i = 0; i < (1 << n); i++) { assert(0 <= p[i] && p[i] < (1 << n)); } for (int i = 0; i < (1 << n) - 1; i++) { assert(__builtin_popcount(p[i] ^ p[i + 1]) == 1); } for (int i = 0; i < (1 << n); i++) { if (i > 0) { cout << " "; } cout << p[i]; } cout << '\n'; return 0; }
/** * author: tourist * created: 16.03.2019 15:29:11 **/ #include <bits/stdc++.h> using namespace std; int remove_bit(int x, int j) { int low = x & ((1 << j) - 1); return ((x >> (j + 1)) << j) + low; } int insert_bit(int x, int j, int v) { int low = x & ((1 << j) - 1); return ((x >> j) << (j + 1)) + (v << j) + low; } vector<int> solve(int n, int a, int b) { if (n == 1) { return {a, b}; } for (int j = 0; j < n; j++) { if ((a ^ b) & (1 << j)) { int aa = remove_bit(a, j); int bb = remove_bit(b, j); for (int u = 0; u < (1 << n); u++) { int uu = remove_bit(u, j); if (uu != aa && uu != bb) { if (__builtin_popcount(aa ^ uu) % 2 == 0) { continue; } if (__builtin_popcount(bb ^ uu) % 2 == 0) { continue; } vector<int> p1 = solve(n - 1, aa, uu); vector<int> p2 = solve(n - 1, uu, bb); for (int &x : p1) { x = insert_bit(x, j, (a >> j) & 1); } for (int &x : p2) { x = insert_bit(x, j, (b >> j) & 1); } p1.insert(p1.end(), p2.begin(), p2.end()); return p1; } } } } assert(false); return {}; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, a, b; cin >> n >> a >> b; if (__builtin_popcount(a ^ b) % 2 == 0) { cout << "NO" << '\n'; return 0; } cout << "YES" << '\n'; vector<int> p = solve(n, a, b); set<int> val(p.begin(), p.end()); assert((int)val.size() == (1 << n)); for (int i = 0; i < (1 << n); i++) { assert(0 <= p[i] && p[i] < (1 << n)); } for (int i = 0; i < (1 << n) - 1; i++) { assert(__builtin_popcount(p[i] ^ p[i + 1]) == 1); } for (int i = 0; i < (1 << n); i++) { if (i > 0) { cout << " "; } cout << p[i]; } cout << '\n'; return 0; }
insert
29
29
29
35
0
p03097
C++
Runtime Error
#include <bits/stdc++.h> typedef long long LL; #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) \ int(X); \ scanf("%d", &X) #define DRII(X, Y) \ int X, Y; \ scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) \ int X, Y, Z; \ scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET \ int ___T, case_n = 1; \ scanf("%d ", &___T); \ while (___T-- > 0) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define PII pair<int, int> #define VI vector<int> #define VL vector<long long> #define VPII vector<pair<int, int>> #define PLL pair<long long, long long> #define VPLL vector<pair<long long, long long>> #define F first #define S second using namespace std; VI difs(0); VI same(0); VI gendiff(int n) { if (n == 1) { VI v(2, 0); v[1] = 1 << difs[0]; return v; } VI base = gendiff(n - 2); int sb = base.size(); int st = 0; VI res(0); int j = 0; int nn = 1 << difs[n - 1]; int nn2 = 1 << difs[n - 2]; REP(i, sb) { res.PB(base[i]); } REP(i, sb) { res.PB(base[sb - 1 - i] + nn); } VI t = {nn + nn2, nn2}; int last = 0; while (j < sb) { res.PB(base[j] + t[st]); if ((st + 1) % 2 == last) { j++; last = st; } else { st++; st %= 2; } } return res; } VI gensame(int n) { if (n == 1) { VI v(2, 0); v[1] = (1 << same[0]); return v; } VI base = gensame(n - 1); int sb = base.size(); int st = 0; VI res(0); int j = 0; int nn = 1 << same[n - 1]; REP(i, sb) { res.PB(base[i]); } REP(i, sb) { res.PB(base[sb - 1 - i] + nn); } return res; } int main() { DRIII(n, a, b); int na = a; int nb = b; int kk = 0; while (kk < n) { if (na % 2 != nb % 2) { difs.PB(kk); } else { same.PB(kk); } na >>= 1; nb >>= 1; kk++; } /* for (auto i:difs) {cout<<i<<" ";} cout<<endl; for (auto i:same) {cout<<i<<" ";} cout<<endl; for (auto i:gensame(same.size())) {cout<<i<<" ";} cout<<endl; for (auto i:gendiff(difs.size())) {cout<<i<<" ";} cout<<endl;*/ if (difs.size() % 2) { printf("YES\n"); VI ss = gensame(same.size()); VI sd = gendiff(difs.size()); VI res; REP(i, sd.size()) { if (i % 2) { for (int j = ss.size() - 1; j >= 0; j--) { res.PB((ss[j] + sd[i]) ^ a); } } else { REP(j, ss.size()) { res.PB((ss[j] + sd[i]) ^ a); } } } REP(i, res.size()) { printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' '); } } else { printf("NO\n"); } }
#include <bits/stdc++.h> typedef long long LL; #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) \ int(X); \ scanf("%d", &X) #define DRII(X, Y) \ int X, Y; \ scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) \ int X, Y, Z; \ scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET \ int ___T, case_n = 1; \ scanf("%d ", &___T); \ while (___T-- > 0) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define PII pair<int, int> #define VI vector<int> #define VL vector<long long> #define VPII vector<pair<int, int>> #define PLL pair<long long, long long> #define VPLL vector<pair<long long, long long>> #define F first #define S second using namespace std; VI difs(0); VI same(0); VI gendiff(int n) { if (n == 1) { VI v(2, 0); v[1] = 1 << difs[0]; return v; } VI base = gendiff(n - 2); int sb = base.size(); int st = 0; VI res(0); int j = 0; int nn = 1 << difs[n - 1]; int nn2 = 1 << difs[n - 2]; REP(i, sb) { res.PB(base[i]); } REP(i, sb) { res.PB(base[sb - 1 - i] + nn); } VI t = {nn + nn2, nn2}; int last = 0; while (j < sb) { res.PB(base[j] + t[st]); if ((st + 1) % 2 == last) { j++; last = st; } else { st++; st %= 2; } } return res; } VI gensame(int n) { if (n == 0) { VI v(1, 0); return v; } if (n == 1) { VI v(2, 0); v[1] = (1 << same[0]); return v; } VI base = gensame(n - 1); int sb = base.size(); int st = 0; VI res(0); int j = 0; int nn = 1 << same[n - 1]; REP(i, sb) { res.PB(base[i]); } REP(i, sb) { res.PB(base[sb - 1 - i] + nn); } return res; } int main() { DRIII(n, a, b); int na = a; int nb = b; int kk = 0; while (kk < n) { if (na % 2 != nb % 2) { difs.PB(kk); } else { same.PB(kk); } na >>= 1; nb >>= 1; kk++; } /* for (auto i:difs) {cout<<i<<" ";} cout<<endl; for (auto i:same) {cout<<i<<" ";} cout<<endl; for (auto i:gensame(same.size())) {cout<<i<<" ";} cout<<endl; for (auto i:gendiff(difs.size())) {cout<<i<<" ";} cout<<endl;*/ if (difs.size() % 2) { printf("YES\n"); VI ss = gensame(same.size()); VI sd = gendiff(difs.size()); VI res; REP(i, sd.size()) { if (i % 2) { for (int j = ss.size() - 1; j >= 0; j--) { res.PB((ss[j] + sd[i]) ^ a); } } else { REP(j, ss.size()) { res.PB((ss[j] + sd[i]) ^ a); } } } REP(i, res.size()) { printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' '); } } else { printf("NO\n"); } }
insert
73
73
73
77
0
p03097
C++
Runtime Error
#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 __gnu_pbds; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<long long, long long>; #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007LL; // constexpr long long MOD = 998244353LL; template <class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true; } return false; } void print() { cout << "\n"; } template <class T, class... Args> void print(const T &x, const Args &...args) { cout << x << " "; print(args...); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// using bit = bitset<7>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, A, B; cin >> N >> A >> B; if (__builtin_popcount(A) % 2 == __builtin_popcount(B) % 2) { cout << "NO" << ln; return 0; } cout << "YES" << ln; auto dfs = [](auto &&self, int n, bit a, bit b) -> vector<bit> { if (!n) { vector<bit> ret = {a, b}; return ret; } if (a.test(n) ^ b.test(n)) { auto c = a; c.flip(0); auto P = self(self, n - 1, a, c); c.flip(n); auto Q = self(self, n - 1, c, b); vector<bit> ret; int L = P.size(); rep(i, L) ret.emplace_back(P[i]); rep(i, L) ret.emplace_back(Q[i]); return ret; } else { auto P = self(self, n - 1, a, b); a.flip(n); auto c = P[1]; c.flip(n); auto Q = self(self, n - 1, a, c); vector<bit> ret; int L = P.size(); ret.emplace_back(P[0]); rep(i, L) ret.emplace_back(Q[i]); for (int i = 1; i < L; i++) ret.emplace_back(P[i]); return ret; } }; bit x(A); bit y(B); auto ans = dfs(dfs, N - 1, x, y); for (auto i : ans) cout << i.to_ullong() << " "; cout << ln; }
#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 __gnu_pbds; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<long long, long long>; #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007LL; // constexpr long long MOD = 998244353LL; template <class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true; } return false; } void print() { cout << "\n"; } template <class T, class... Args> void print(const T &x, const Args &...args) { cout << x << " "; print(args...); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// using bit = bitset<17>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, A, B; cin >> N >> A >> B; if (__builtin_popcount(A) % 2 == __builtin_popcount(B) % 2) { cout << "NO" << ln; return 0; } cout << "YES" << ln; auto dfs = [](auto &&self, int n, bit a, bit b) -> vector<bit> { if (!n) { vector<bit> ret = {a, b}; return ret; } if (a.test(n) ^ b.test(n)) { auto c = a; c.flip(0); auto P = self(self, n - 1, a, c); c.flip(n); auto Q = self(self, n - 1, c, b); vector<bit> ret; int L = P.size(); rep(i, L) ret.emplace_back(P[i]); rep(i, L) ret.emplace_back(Q[i]); return ret; } else { auto P = self(self, n - 1, a, b); a.flip(n); auto c = P[1]; c.flip(n); auto Q = self(self, n - 1, a, c); vector<bit> ret; int L = P.size(); ret.emplace_back(P[0]); rep(i, L) ret.emplace_back(Q[i]); for (int i = 1; i < L; i++) ret.emplace_back(P[i]); return ret; } }; bit x(A); bit y(B); auto ans = dfs(dfs, N - 1, x, y); for (auto i : ans) cout << i.to_ullong() << " "; cout << ln; }
replace
36
37
36
37
0
p03097
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; const int N = 1e6; int n, A, B; int a[N]; int count(int x) { int sum = 0; while (x) { sum += (x & 1); x >>= 1; } return sum; } void work(int *a, int n, int A, int B) { if (!n) { a[0] = A; return; } if (n == 1) { a[0] = A; a[1] = B; return; } int sig = 0; int AA, BB; int x; while (1) { while (((1 << sig) & A) == ((1 << sig) & B)) sig++; AA = (A & ((1 << (sig)) - 1)) + ((A >> (sig + 1)) << (sig)); BB = ((B & ((1 << (sig)) - 1)) + ((B >> (sig + 1)) << (sig))); x = 1; while ((AA ^ x) == BB && x <= (1 << (n - 2))) x <<= 1; if (x <= (1 << (n - 2))) break; } work(a, n - 1, AA, (AA ^ x)); work(a + (1 << (n - 1)), n - 1, (AA ^ x), BB); for (int i = 0; i < (1 << (n - 1)); i++) a[i] = ((a[i] >> (sig)) << (sig + 1)) + (a[i] & ((1 << (sig)) - 1)) + (A & (1 << sig)); for (int i = (1 << (n - 1)); i < (1 << n); i++) a[i] = ((a[i] >> (sig)) << (sig + 1)) + (a[i] & ((1 << (sig)) - 1)) + (B & (1 << sig)); } int main() { scanf("%d %d %d", &n, &A, &B); if (count(A) == count(B)) { printf("NO"); return 0; } work(a, n, A, B); printf("YES\n"); for (int i = 0; i < (1 << n); i++) printf("%d ", a[i]); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; const int N = 1e6; int n, A, B; int a[N]; int count(int x) { int sum = 0; while (x) { sum += (x & 1); x >>= 1; } return sum; } void work(int *a, int n, int A, int B) { if (!n) { a[0] = A; return; } if (n == 1) { a[0] = A; a[1] = B; return; } int sig = 0; int AA, BB; int x; while (1) { while (((1 << sig) & A) == ((1 << sig) & B)) sig++; AA = (A & ((1 << (sig)) - 1)) + ((A >> (sig + 1)) << (sig)); BB = ((B & ((1 << (sig)) - 1)) + ((B >> (sig + 1)) << (sig))); x = 1; while ((AA ^ x) == BB && x <= (1 << (n - 2))) x <<= 1; if (x <= (1 << (n - 2))) break; } work(a, n - 1, AA, (AA ^ x)); work(a + (1 << (n - 1)), n - 1, (AA ^ x), BB); for (int i = 0; i < (1 << (n - 1)); i++) a[i] = ((a[i] >> (sig)) << (sig + 1)) + (a[i] & ((1 << (sig)) - 1)) + (A & (1 << sig)); for (int i = (1 << (n - 1)); i < (1 << n); i++) a[i] = ((a[i] >> (sig)) << (sig + 1)) + (a[i] & ((1 << (sig)) - 1)) + (B & (1 << sig)); } int main() { scanf("%d %d %d", &n, &A, &B); if ((count(A) & 1) == (count(B) & 1)) { printf("NO"); return 0; } work(a, n, A, B); printf("YES\n"); for (int i = 0; i < (1 << n); i++) printf("%d ", a[i]); return 0; }
replace
55
56
55
56
TLE
p03097
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define int long long bool check(vector<int> v) { for (int i = 0; i + 1 < (int)v.size(); i++) { if (__builtin_popcount(v[i] ^ v[i + 1]) != 1) { return false; } } return true; } vector<int> genka(int n, int a, int b) { if (n == 1) { return {a, b}; } assert(__builtin_popcount(a ^ b) & 1); int ca = (a >> (n - 1)) & 1; int cb = (b >> (n - 1)) & 1; if (ca != cb) { int wb = 0; if (ca) { a ^= (1 << (n - 1)); b ^= (1 << (n - 1)); wb = 1; } for (int mid = 0; mid < (1 << (n - 1)); mid++) { if (__builtin_popcount(mid ^ a) % 2 == 0) continue; if (__builtin_popcount((1 << (n - 1)) ^ mid ^ b) % 2 == 0) continue; { vector<int> f = genka(n - 1, a, mid); vector<int> s = genka(n - 1, mid, b ^ (1 << (n - 1))); for (auto t : s) { f.push_back(t ^ (1 << (n - 1))); } if (wb) { for (auto &t : f) { t ^= (1 << (n - 1)); } } return f; } } } else { int wb = 0; if (ca) { a ^= (1 << (n - 1)); b ^= (1 << (n - 1)); wb = 1; } vector<int> f = genka(n - 1, a, b); { int i = 0; int x = f[i]; int y = f[i + 1]; vector<int> s = genka(n - 1, x, y); vector<int> ans = {f[0]}; for (auto t : s) { ans.push_back(t ^ (1 << (n - 1))); } for (int j = 1; j < (int)f.size(); j++) { ans.push_back(f[j]); } if (wb) { for (auto &t : ans) { t ^= (1 << (n - 1)); } } return ans; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, a, b; cin >> n >> a >> b; int dt = a ^ b; if (!(__builtin_popcount(dt) & 1)) { cout << "NO\n"; } cout << "YES\n"; vector<int> ans = genka(n, a, b); for (auto t : ans) { cout << t << ' '; } cout << endl; // for (int i = 0; i < (1 << n); i++) { // for (int j = 0; j < (1 << n); j++) { // if (__builtin_popcount(i ^ j) & 1) genka(n, i, j); // } // } }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define int long long bool check(vector<int> v) { for (int i = 0; i + 1 < (int)v.size(); i++) { if (__builtin_popcount(v[i] ^ v[i + 1]) != 1) { return false; } } return true; } vector<int> genka(int n, int a, int b) { if (n == 1) { return {a, b}; } assert(__builtin_popcount(a ^ b) & 1); int ca = (a >> (n - 1)) & 1; int cb = (b >> (n - 1)) & 1; if (ca != cb) { int wb = 0; if (ca) { a ^= (1 << (n - 1)); b ^= (1 << (n - 1)); wb = 1; } for (int mid = 0; mid < (1 << (n - 1)); mid++) { if (__builtin_popcount(mid ^ a) % 2 == 0) continue; if (__builtin_popcount((1 << (n - 1)) ^ mid ^ b) % 2 == 0) continue; { vector<int> f = genka(n - 1, a, mid); vector<int> s = genka(n - 1, mid, b ^ (1 << (n - 1))); for (auto t : s) { f.push_back(t ^ (1 << (n - 1))); } if (wb) { for (auto &t : f) { t ^= (1 << (n - 1)); } } return f; } } } else { int wb = 0; if (ca) { a ^= (1 << (n - 1)); b ^= (1 << (n - 1)); wb = 1; } vector<int> f = genka(n - 1, a, b); { int i = 0; int x = f[i]; int y = f[i + 1]; vector<int> s = genka(n - 1, x, y); vector<int> ans = {f[0]}; for (auto t : s) { ans.push_back(t ^ (1 << (n - 1))); } for (int j = 1; j < (int)f.size(); j++) { ans.push_back(f[j]); } if (wb) { for (auto &t : ans) { t ^= (1 << (n - 1)); } } return ans; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, a, b; cin >> n >> a >> b; int dt = a ^ b; if (!(__builtin_popcount(dt) & 1)) { cout << "NO\n"; exit(0); } cout << "YES\n"; vector<int> ans = genka(n, a, b); for (auto t : ans) { cout << t << ' '; } cout << endl; // for (int i = 0; i < (1 << n); i++) { // for (int j = 0; j < (1 << n); j++) { // if (__builtin_popcount(i ^ j) & 1) genka(n, i, j); // } // } }
insert
107
107
107
108
0
p03097
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int pc(int i) { return __builtin_popcount(i); } int arr[1 << 17]; int rmbit(unsigned n, int i) { return ((n << (32 - i)) >> (32 - i)) | (((n >> (i + 1))) << i); } int addbit(unsigned n, int i, bool p) { unsigned t = (n >> i) << (i + 1), s = (n << (32 - i)) >> (32 - i); t |= s; if (p) t |= (1 << i); return t; } void f(int n, int a, int b, int l) { if (n == 1) { arr[l] = a; arr[l + 1] = b; return; } for (int i = 0; i < n; i++) { if ((a & (1 << i)) == (b & (1 << i))) continue; int ap = rmbit(a, i), bp = rmbit(b, i), c = ap ^ 1; f(n - 1, ap, c, l); f(n - 1, c, bp, l + (1 << (n - 1))); for (int j = l; j < l + (1 << n); j++) arr[j] = addbit(arr[j], i, j < (l + (1 << (n - 1))) ? a & (1 << i) : b & (1 << i)); return; } assert(0); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, a, b; cin >> n >> a >> b; if ((pc(a) & 1) == (pc(b) & 1)) { return !(cout << "NO"); } cout << "YES\n"; f(n, a, b, 0); for (int i = 0; i < (1 << n); i++) { cout << arr[i] << ' '; } }
#include <bits/stdc++.h> using namespace std; int pc(int i) { return __builtin_popcount(i); } int arr[1 << 17]; int getl(int n, int i) { return (n >> (i + 1)) << (i + 1); } int getr(int n, int i) { return n ^ ((n >> i) << i); } int rmbit(int n, int i) { return (getl(n, i) >> 1) | getr(n, i); } int addbit(int n, int i, bool p) { return ((n >> i) << (i + 1)) | getr(n, i) | (p ? (1 << i) : 0); } void f(int n, int a, int b, int l) { if (n == 1) { arr[l] = a; arr[l + 1] = b; return; } for (int i = 0; i < n; i++) { if ((a & (1 << i)) == (b & (1 << i))) continue; int ap = rmbit(a, i), bp = rmbit(b, i), c = ap ^ 1; f(n - 1, ap, c, l); f(n - 1, c, bp, l + (1 << (n - 1))); for (int j = l; j < l + (1 << n); j++) arr[j] = addbit(arr[j], i, j < (l + (1 << (n - 1))) ? a & (1 << i) : b & (1 << i)); return; } assert(0); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, a, b; cin >> n >> a >> b; if ((pc(a) & 1) == (pc(b) & 1)) { return !(cout << "NO"); } cout << "YES\n"; f(n, a, b, 0); for (int i = 0; i < (1 << n); i++) { cout << arr[i] << ' '; } }
replace
4
13
4
9
0
p03097
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #define lowbit(_A) ((_A) & (-(_A))) int n; int cnt; int fr[166666]; int ne[333333]; int to[333333]; void add(int a, int b) { ne[++cnt] = fr[a], fr[a] = cnt, to[cnt] = b; ne[++cnt] = fr[b], fr[b] = cnt, to[cnt] = a; } void dfs(int S, int T, int s, int cnt) { if (cnt == n - 1) { add(S, T); return; } for (int i = 0; i < n; i++) { if (s & (1 << i)) continue; if ((S & (1 << i)) != (T & (1 << i))) { for (int j = 0, tmp, tmp2; j < n; j++) { if ((s & (1 << j)) || j == i) continue; else { if (S & (1 << j)) tmp = S & ((1 << n) - 1 - (1 << j)); else tmp = S | (1 << j); dfs(S, tmp, s | (1 << i), cnt + 1); if (S & (1 << i)) tmp2 = tmp & (((1 << n) - 1 - (1 << i))); else tmp2 = tmp | (1 << i); dfs(tmp2, T, s | (1 << i), cnt + 1); add(tmp, tmp2); } } break; } } } int A, B; int na, nb; void out(int a, int f) { printf("%d ", a); for (int i = fr[a]; i; i = ne[i]) if (to[i] != f) out(to[i], a); } int main() { scanf("%d", &n); scanf("%d %d", &A, &B); int tmp = A; while (tmp) tmp -= lowbit(tmp), na++; tmp = B; while (tmp) tmp -= lowbit(tmp), nb++; if (na % 2 == nb % 2) return !printf("NO\n"); dfs(A, B, 0, 0); puts("YES"); out(A, -2); printf("\n"); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #define lowbit(_A) ((_A) & (-(_A))) int n; int cnt; int fr[166666]; int ne[333333]; int to[333333]; void add(int a, int b) { ne[++cnt] = fr[a], fr[a] = cnt, to[cnt] = b; ne[++cnt] = fr[b], fr[b] = cnt, to[cnt] = a; } void dfs(int S, int T, int s, int cnt) { if (cnt == n - 1) { add(S, T); return; } for (int i = 0; i < n; i++) { if (s & (1 << i)) continue; if ((S & (1 << i)) != (T & (1 << i))) { for (int j = 0, tmp, tmp2; j < n; j++) { if ((s & (1 << j)) || j == i) continue; else { if (S & (1 << j)) tmp = S & ((1 << n) - 1 - (1 << j)); else tmp = S | (1 << j); dfs(S, tmp, s | (1 << i), cnt + 1); if (S & (1 << i)) tmp2 = tmp & (((1 << n) - 1 - (1 << i))); else tmp2 = tmp | (1 << i); dfs(tmp2, T, s | (1 << i), cnt + 1); add(tmp, tmp2); break; } } break; } } } int A, B; int na, nb; void out(int a, int f) { printf("%d ", a); for (int i = fr[a]; i; i = ne[i]) if (to[i] != f) out(to[i], a); } int main() { scanf("%d", &n); scanf("%d %d", &A, &B); int tmp = A; while (tmp) tmp -= lowbit(tmp), na++; tmp = B; while (tmp) tmp -= lowbit(tmp), nb++; if (na % 2 == nb % 2) return !printf("NO\n"); dfs(A, B, 0, 0); puts("YES"); out(A, -2); printf("\n"); return 0; }
insert
40
40
40
41
0
p03097
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; void print(int x) { for (int i = 7; i >= 0; i--) cout << (x >> i & 1); cout << endl; } int main() { int N, A, B; cin >> N >> A >> B; auto count = [&](int x) { int r = 0; for (int i = 0; i < N; i++) r += x >> i & 1; return r; }; int C = A ^ B; if (count(C) % 2 != 1) { cout << "NO" << endl; return 0; } vector<int> T; for (int i = 0; i < 1 << N; i++) T.push_back(i ^ i >> 1); for (int i = 0; i < count(C) / 2; i++) { bool ok = false; int c = ((1 << N) >> (i * 2 + 1)); for (int j = (1 << N) - c - 3; j <= (1 << N) - 2; j++) { if (count(T[j] ^ T[(1 << N) - 1]) == 1 && count(T[j + 1]) == i * 2 + 3) { reverse(T.begin() + j + 1, T.end()); ok = true; break; } } if (!ok) { *(int *)0 = 0; cout << "NO" << endl; return 0; } } vector<int> B0, B1; for (int i = 0; i < N; i++) if (C >> i & 1) B1.push_back(i); else B0.push_back(i); vector<int> M; int b0 = 0; int b1 = 0; for (int i = 0; i < N; i++) if (T[(1 << N) - 1] >> i & 1) M.push_back(B1[b1++]); else M.push_back(B0[b0++]); cout << "YES" << endl; bool f = true; for (int t : T) { int t2 = 0; for (int i = 0; i < N; i++) t2 |= (t >> i & 1) << M[i]; t2 ^= A; cout << (f ? "" : " ") << t2; f = false; } cout << endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; void print(int x) { for (int i = 7; i >= 0; i--) cout << (x >> i & 1); cout << endl; } int main() { int N, A, B; cin >> N >> A >> B; auto count = [&](int x) { int r = 0; for (int i = 0; i < N; i++) r += x >> i & 1; return r; }; int C = A ^ B; if (count(C) % 2 != 1) { cout << "NO" << endl; return 0; } vector<int> T; for (int i = 0; i < 1 << N; i++) T.push_back(i ^ i >> 1); for (int i = 0; i < count(C) / 2; i++) { int t = 1 << (N - i * 2 - 1); reverse(T.end() - t + 1, T.end()); reverse(T.end() - t + 2, T.end() - t / 2 + 1); reverse(T.end() - t / 2 + 1, T.end()); } vector<int> B0, B1; for (int i = 0; i < N; i++) if (C >> i & 1) B1.push_back(i); else B0.push_back(i); vector<int> M; int b0 = 0; int b1 = 0; for (int i = 0; i < N; i++) if (T[(1 << N) - 1] >> i & 1) M.push_back(B1[b1++]); else M.push_back(B0[b0++]); cout << "YES" << endl; bool f = true; for (int t : T) { int t2 = 0; for (int i = 0; i < N; i++) t2 |= (t >> i & 1) << M[i]; t2 ^= A; cout << (f ? "" : " ") << t2; f = false; } cout << endl; }
replace
33
47
33
37
0
p03097
C++
Runtime Error
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> typedef long long ll; using namespace std; #ifndef LOCAL #define debug(x) ; #else #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } #endif #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 200010 int n; int conv[20]; int ff(int x) { int r[20], res = 0; for (int i = 0; i < n; i++) r[conv[i]] = x & (1 << i); for (int i = 0; i < n; i++) res += (1 << i) * !!(r[i]); // cerr << x << " -> " << res << endl; return res; } int main() { int a, b; cin >> n >> a >> b; if (__builtin_popcount(a ^ b) % 2 == 0) { cout << "NO" << endl; return 0; } int pp = __builtin_popcount(a ^ b); puts("YES"); if (n == 1) { printf("%d %d\n", a, b); return 0; } vector<int> vec[5]; vec[0].push_back(0); vec[0].push_back(1); for (int i = 0; i < 4; i++) { for (int p = 0; p < vec[i].size(); p++) { for (int q = 0; q < vec[i].size(); q++) { int r; if (p % 2 == 0) r = q; else r = vec[i].size() - q - 1; vec[i + 1].push_back((vec[i][p] << (1 << i)) + vec[i][r]); } } } n--; vector<int> vec1; for (int i = 0; i < 5; i++) { if (n & (1 << i)) { if (vec1.size() == 0) { vec1 = vec[i]; continue; } vector<int> vec2; for (int p = 0; p < vec[i].size(); p++) { for (int q = 0; q < vec1.size(); q++) { int r; if (p % 2 == 0) r = q; else r = vec1.size() - q - 1; vec2.push_back((vec1[r] << (1 << i)) + vec[i][p]); } } vec1 = vec2; } } int y = -1, x = -1; for (int i = 0; i < n; i++) { int q; if (i % 2 == 0) { q = __builtin_popcount(vec1[0] ^ vec1[i]) + 1; } else { q = __builtin_popcount(vec1[0] ^ vec1[i]); } if (q == pp) { y = i % 2 == 0; x = i; break; } } assert(y != -1); vector<int> vec2; for (int i = 0; i < x; i++) { if (i % 2 == 0) vec2.push_back(vec1[i] * 2 + 0); vec2.push_back(vec1[i] * 2 + 1); if (i % 2 == 1) vec2.push_back(vec1[i] * 2 + 0); } for (int i = x; i < vec1.size(); i++) { if (y == 0) vec2.push_back(vec1[i] * 2 + 1); if (y == 1) vec2.push_back(vec1[i] * 2 + 0); } for (int i = vec1.size() - 1; i >= x; i--) { if (y == 0) vec2.push_back(vec1[i] * 2 + 0); if (y == 1) vec2.push_back(vec1[i] * 2 + 1); } n++; set<int> t1, t2; for (int i = 0; i < n; i++) { int m = vec2.size(); int p = vec2[0] & (1 << i); int q = vec2[m - 1] & (1 << i); int r = a & (1 << i); int s = b & (1 << i); if (p != q) t1.insert(i); if (r != s) t2.insert(i); } int cur = 0; for (int i = 0; i < n; i++) { if (t1.find(i) != t1.end()) continue; while (t2.find(cur) != t2.end()) cur++; conv[i] = cur++; } auto it1 = t1.begin(); auto it2 = t2.begin(); while (it1 != t1.end()) { conv[*it1] = *it2; it1++; it2++; } bool f[20]; int tmp = ff(vec2[0]); for (int i = 0; i < n; i++) { int p = tmp & (1 << i); int q = a & (1 << i); f[i] = p ^ q; } int prev = -1; debug(vec2.size()); for (int i = 0; i < vec2.size(); i++) { int res = ff(vec2[i]); for (int j = 0; j < n; j++) { if (f[j]) res ^= 1 << j; } printf("%d ", res); assert(prev == -1 || __builtin_popcount(res ^ prev) == 1); prev = res; if (i == 0) assert(a == res); } assert(b == prev); puts(""); sort(vec1.begin(), vec1.end()); for (int i = 0; i < n; i++) { assert(vec1[i] == i); } return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> typedef long long ll; using namespace std; #ifndef LOCAL #define debug(x) ; #else #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } #endif #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 200010 int n; int conv[20]; int ff(int x) { int r[20], res = 0; for (int i = 0; i < n; i++) r[conv[i]] = x & (1 << i); for (int i = 0; i < n; i++) res += (1 << i) * !!(r[i]); // cerr << x << " -> " << res << endl; return res; } int main() { int a, b; cin >> n >> a >> b; if (__builtin_popcount(a ^ b) % 2 == 0) { cout << "NO" << endl; return 0; } int pp = __builtin_popcount(a ^ b); puts("YES"); if (n == 1) { printf("%d %d\n", a, b); return 0; } vector<int> vec[5]; vec[0].push_back(0); vec[0].push_back(1); for (int i = 0; i < 4; i++) { for (int p = 0; p < vec[i].size(); p++) { for (int q = 0; q < vec[i].size(); q++) { int r; if (p % 2 == 0) r = q; else r = vec[i].size() - q - 1; vec[i + 1].push_back((vec[i][p] << (1 << i)) + vec[i][r]); } } } n--; vector<int> vec1; for (int i = 0; i < 5; i++) { if (n & (1 << i)) { if (vec1.size() == 0) { vec1 = vec[i]; continue; } vector<int> vec2; for (int p = 0; p < vec[i].size(); p++) { for (int q = 0; q < vec1.size(); q++) { int r; if (p % 2 == 0) r = q; else r = vec1.size() - q - 1; vec2.push_back((vec1[r] << (1 << i)) + vec[i][p]); } } vec1 = vec2; } } int y = -1, x = -1; for (int i = 0; i < vec1.size(); i++) { int q; if (i % 2 == 0) { q = __builtin_popcount(vec1[0] ^ vec1[i]) + 1; } else { q = __builtin_popcount(vec1[0] ^ vec1[i]); } if (q == pp) { y = i % 2 == 0; x = i; break; } } assert(y != -1); vector<int> vec2; for (int i = 0; i < x; i++) { if (i % 2 == 0) vec2.push_back(vec1[i] * 2 + 0); vec2.push_back(vec1[i] * 2 + 1); if (i % 2 == 1) vec2.push_back(vec1[i] * 2 + 0); } for (int i = x; i < vec1.size(); i++) { if (y == 0) vec2.push_back(vec1[i] * 2 + 1); if (y == 1) vec2.push_back(vec1[i] * 2 + 0); } for (int i = vec1.size() - 1; i >= x; i--) { if (y == 0) vec2.push_back(vec1[i] * 2 + 0); if (y == 1) vec2.push_back(vec1[i] * 2 + 1); } n++; set<int> t1, t2; for (int i = 0; i < n; i++) { int m = vec2.size(); int p = vec2[0] & (1 << i); int q = vec2[m - 1] & (1 << i); int r = a & (1 << i); int s = b & (1 << i); if (p != q) t1.insert(i); if (r != s) t2.insert(i); } int cur = 0; for (int i = 0; i < n; i++) { if (t1.find(i) != t1.end()) continue; while (t2.find(cur) != t2.end()) cur++; conv[i] = cur++; } auto it1 = t1.begin(); auto it2 = t2.begin(); while (it1 != t1.end()) { conv[*it1] = *it2; it1++; it2++; } bool f[20]; int tmp = ff(vec2[0]); for (int i = 0; i < n; i++) { int p = tmp & (1 << i); int q = a & (1 << i); f[i] = p ^ q; } int prev = -1; debug(vec2.size()); for (int i = 0; i < vec2.size(); i++) { int res = ff(vec2[i]); for (int j = 0; j < n; j++) { if (f[j]) res ^= 1 << j; } printf("%d ", res); assert(prev == -1 || __builtin_popcount(res ^ prev) == 1); prev = res; if (i == 0) assert(a == res); } assert(b == prev); puts(""); sort(vec1.begin(), vec1.end()); for (int i = 0; i < n; i++) { assert(vec1[i] == i); } return 0; }
replace
134
135
134
135
0
p03097
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { #ifdef wxh010910 freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, a, b; cin >> n >> a >> b; int k = __builtin_popcount(a ^ b); if (k % 2 == 0) { cout << "NO" << "\n"; return 0; } cout << "YES" << "\n"; vector<vector<int>> to_one(n + 1); to_one[1] = {0, 1}; for (int i = 2; i <= n; ++i) { for (auto x : to_one[i - 1]) { to_one[i].push_back(x << 1); } reverse(to_one[i - 1].begin(), to_one[i - 1].end()); for (auto x : to_one[i - 1]) { to_one[i].push_back(x << 1 | 1); } reverse(to_one[i - 1].begin(), to_one[i - 1].end()); } vector<vector<int>> to_all(k + 1); to_all[1] = {0, 1}; for (int i = 3; i <= k; i += 2) { for (auto x : to_all[i - 2]) { to_all[i].push_back(x); } for (auto x : to_one[i - 2]) { to_all[i].push_back(x ^ ((1 << (i - 1)) - 1)); } for (auto x : to_one[i - 1]) { to_all[i].push_back(x ^ ((1 << i) - 2)); } } vector<int> ans; for (auto x : to_all[k]) { for (auto y : to_one[n - k]) { ans.push_back(y << k | x); } reverse(to_one[n - k].begin(), to_one[n - k].end()); } vector<int> to_bit(n); int ptr = 0; for (int i = 0; i < n; ++i) { if ((a ^ b) >> i & 1) { to_bit[ptr++] = i; } } for (int i = 0; i < n; ++i) { if (!((a ^ b) >> i & 1)) { to_bit[ptr++] = i; } } for (int i = 0; i < 1 << n; ++i) { int real_ans = 0; for (int j = 0; j < n; ++j) { if (ans[i] >> j & 1) { real_ans |= 1 << to_bit[j]; } } ans[i] = real_ans; } for (int i = 0; i < 1 << n; ++i) { if (i) { cout << " "; } cout << (ans[i] ^ a); } cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { #ifdef wxh010910 freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, a, b; cin >> n >> a >> b; int k = __builtin_popcount(a ^ b); if (k % 2 == 0) { cout << "NO" << "\n"; return 0; } cout << "YES" << "\n"; vector<vector<int>> to_one(n + 1); to_one[0] = {0}; to_one[1] = {0, 1}; for (int i = 2; i <= n; ++i) { for (auto x : to_one[i - 1]) { to_one[i].push_back(x << 1); } reverse(to_one[i - 1].begin(), to_one[i - 1].end()); for (auto x : to_one[i - 1]) { to_one[i].push_back(x << 1 | 1); } reverse(to_one[i - 1].begin(), to_one[i - 1].end()); } vector<vector<int>> to_all(k + 1); to_all[1] = {0, 1}; for (int i = 3; i <= k; i += 2) { for (auto x : to_all[i - 2]) { to_all[i].push_back(x); } for (auto x : to_one[i - 2]) { to_all[i].push_back(x ^ ((1 << (i - 1)) - 1)); } for (auto x : to_one[i - 1]) { to_all[i].push_back(x ^ ((1 << i) - 2)); } } vector<int> ans; for (auto x : to_all[k]) { for (auto y : to_one[n - k]) { ans.push_back(y << k | x); } reverse(to_one[n - k].begin(), to_one[n - k].end()); } vector<int> to_bit(n); int ptr = 0; for (int i = 0; i < n; ++i) { if ((a ^ b) >> i & 1) { to_bit[ptr++] = i; } } for (int i = 0; i < n; ++i) { if (!((a ^ b) >> i & 1)) { to_bit[ptr++] = i; } } for (int i = 0; i < 1 << n; ++i) { int real_ans = 0; for (int j = 0; j < n; ++j) { if (ans[i] >> j & 1) { real_ans |= 1 << to_bit[j]; } } ans[i] = real_ans; } for (int i = 0; i < 1 << n; ++i) { if (i) { cout << " "; } cout << (ans[i] ^ a); } cout << "\n"; return 0; }
insert
22
22
22
23
0
p03097
C++
Runtime Error
#include <bits/stdc++.h> #define maxn 100005 using namespace std; inline int read() { int x = 0, c; while (!isdigit(c = getchar())) ; while (x = x * 10 + c - '0', isdigit(c = getchar())) ; return x; } int cnt[1 << 18], ans[18][1 << 17], n, A, B; void solve(int k, int A, int B, int *a, int l, int r) { if (k == 1) { a[l] = A; a[r] = B; return; } int C = A ^ B, x = C & (-C), mid = (l + r) >> 1; int nA = A % x + A / (x << 1) * x, nB = B % x + B / (x << 1) * x; solve(k - 1, nA, nA ^ nB, ans[k - 1], l, mid); solve(k - 1, nA ^ nB, nB, ans[k - 1], mid + 1, r); for (int i = l; i <= mid; i++) a[i] = ans[k - 1][i] % x + ans[k - 1][i] / x * (x << 1) + ((A & x) ? x : 0); for (int i = mid + 1; i <= r; i++) a[i] = ans[k - 1][i] % x + ans[k - 1][i] / x * (x << 1) + ((B & x) ? x : 0); } int main() { n = read(); A = read(); B = read(); int N = 1 << n; N--; for (int i = 1; i <= N; i++) cnt[i] = cnt[i >> 1] + (i & 1); if (cnt[A ^ B] & 1) { printf("YES\n"); solve(n, A, B, ans[n], 0, N); for (int i = 0; i <= N; i++) printf("%d ", ans[n][i]); return 0; } printf("NO\n"); }
#include <bits/stdc++.h> #define maxn 100005 using namespace std; inline int read() { int x = 0, c; while (!isdigit(c = getchar())) ; while (x = x * 10 + c - '0', isdigit(c = getchar())) ; return x; } int cnt[1 << 18], ans[18][1 << 17], n, A, B; void solve(int k, int A, int B, int *a, int l, int r) { if (k == 1) { a[l] = A; a[r] = B; return; } int C = A ^ B, x = C & (-C), mid = (l + r) >> 1; int nA = A % x + A / (x << 1) * x, nB = B % x + B / (x << 1) * x; solve(k - 1, nA, nA ^ 1, ans[k - 1], l, mid); solve(k - 1, nA ^ 1, nB, ans[k - 1], mid + 1, r); for (int i = l; i <= mid; i++) a[i] = ans[k - 1][i] % x + ans[k - 1][i] / x * (x << 1) + ((A & x) ? x : 0); for (int i = mid + 1; i <= r; i++) a[i] = ans[k - 1][i] % x + ans[k - 1][i] / x * (x << 1) + ((B & x) ? x : 0); } int main() { n = read(); A = read(); B = read(); int N = 1 << n; N--; for (int i = 1; i <= N; i++) cnt[i] = cnt[i >> 1] + (i & 1); if (cnt[A ^ B] & 1) { printf("YES\n"); solve(n, A, B, ans[n], 0, N); for (int i = 0; i <= N; i++) printf("%d ", ans[n][i]); return 0; } printf("NO\n"); }
replace
20
22
20
22
0
p03097
C++
Runtime Error
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back using namespace std; typedef long long ll; typedef pair<int, int> ii; const int maxn = 1 << 17; int N, A, B; void go(int s, int t, int mask) { if (__builtin_popcount(mask) == N - 1) { cout << s << ' ' << t << ' '; return; } int c = -1, e = -1; for (int i = 0; i < N; ++i) if (!(mask >> i & 1)) { if (c == -1 && (s >> i & 1) != (t >> i & 1)) c = i; else e = i; } go(s, s ^ (1 << c), mask ^ (1 << c)); go(s ^ (1 << c) ^ (1 << e), t, mask ^ (1 << c)); } signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("A.INP", "r", stdin); freopen("A.OUT", "w", stdout); #endif // LOCAL cin >> N >> A >> B; if (__builtin_popcount(A) % 2 == __builtin_popcount(B) % 2) { cout << "NO"; } else { cout << "YES\n"; go(A, B, 0); } }
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back using namespace std; typedef long long ll; typedef pair<int, int> ii; const int maxn = 1 << 17; int N, A, B; void go(int s, int t, int mask) { if (__builtin_popcount(mask) == N - 1) { cout << s << ' ' << t << ' '; return; } int c = -1, e = -1; for (int i = 0; i < N; ++i) if (!(mask >> i & 1)) { if (c == -1 && (s >> i & 1) != (t >> i & 1)) c = i; else e = i; } go(s, s ^ (1 << e), mask ^ (1 << c)); go(s ^ (1 << e) ^ (1 << c), t, mask ^ (1 << c)); } signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("A.INP", "r", stdin); freopen("A.OUT", "w", stdout); #endif // LOCAL cin >> N >> A >> B; if (__builtin_popcount(A) % 2 == __builtin_popcount(B) % 2) { cout << "NO"; } else { cout << "YES\n"; go(A, B, 0); } }
replace
29
31
29
31
0
p03097
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; int n, a, b; vector<int> ret; void dfs(int l, int r, int mask) { if (mask & (mask - 1) == 0) { ret.pb(l); ret.pb(r); return; } int x = l ^ r; int tb = x & -x, tc = mask ^ tb; int td = tc & -tc; dfs(l, l ^ td, tc); dfs(l ^ tb ^ td, r, tc); } int main() { cin >> n >> a >> b; if (__builtin_popcount(a) % 2 == __builtin_popcount(b) % 2) { cout << "NO" << endl; return 0; } dfs(a, b, (1 << n) - 1); cout << "YES" << endl; rep(i, ret.size()) { cout << ret[i] << " "; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; int n, a, b; vector<int> ret; void dfs(int l, int r, int mask) { if ((mask & (mask - 1)) == 0) { ret.pb(l); ret.pb(r); return; } int x = l ^ r; int tb = x & -x, tc = mask ^ tb; int td = tc & -tc; dfs(l, l ^ td, tc); dfs(l ^ tb ^ td, r, tc); } int main() { cin >> n >> a >> b; if (__builtin_popcount(a) % 2 == __builtin_popcount(b) % 2) { cout << "NO" << endl; return 0; } dfs(a, b, (1 << n) - 1); cout << "YES" << endl; rep(i, ret.size()) { cout << ret[i] << " "; } cout << endl; return 0; }
replace
11
12
11
12
0
p03097
C++
Runtime Error
#include <bitset> #include <iostream> #include <vector> using namespace std; int n; int a, b; void dfs(vector<int> &ans, int a, int b, int cn, int mask) { if (cn == 1) { ans.push_back(a); ans.push_back(b); return; } int c, d; for (int i = 0; i < n; i++) { if (!(mask & (1 << i)) && ((a ^ b) & (1 << i))) { c = i; break; } } for (int i = 0; i < n; i++) { if (!(mask & (1 << i)) && i != c) { d = i; break; } } dfs(ans, a, a ^ (1 << d), n - 1, mask + (1 << c)); dfs(ans, a ^ (1 << c) ^ (1 << d), b, n - 1, mask + (1 << c)); } int main() { cin >> n >> a >> b; int tmp = a; int cnt_a, cnt_b; while (tmp) { if (tmp & 1) cnt_a++; tmp >>= 1; } tmp = b; while (tmp) { if (tmp & 1) cnt_b++; tmp >>= 1; } if ((cnt_a % 2) == (cnt_b % 2)) { cout << "NO"; return 0; } cout << "YES" << endl; vector<int> ans; dfs(ans, a, b, n, 0); for (int i : ans) cout << i << ' '; }
#include <bitset> #include <iostream> #include <vector> using namespace std; int n; int a, b; void dfs(vector<int> &ans, int a, int b, int cn, int mask) { if (cn == 1) { ans.push_back(a); ans.push_back(b); return; } int c, d; for (int i = 0; i < n; i++) { if (!(mask & (1 << i)) && ((a ^ b) & (1 << i))) { c = i; break; } } for (int i = 0; i < n; i++) { if (!(mask & (1 << i)) && i != c) { d = i; break; } } dfs(ans, a, a ^ (1 << d), cn - 1, mask + (1 << c)); dfs(ans, a ^ (1 << c) ^ (1 << d), b, cn - 1, mask + (1 << c)); } int main() { cin >> n >> a >> b; int tmp = a; int cnt_a, cnt_b; while (tmp) { if (tmp & 1) cnt_a++; tmp >>= 1; } tmp = b; while (tmp) { if (tmp & 1) cnt_b++; tmp >>= 1; } if ((cnt_a % 2) == (cnt_b % 2)) { cout << "NO"; return 0; } cout << "YES" << endl; vector<int> ans; dfs(ans, a, b, n, 0); for (int i : ans) cout << i << ' '; }
replace
32
34
32
34
0
p03097
C++
Runtime Error
#pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") #include <algorithm> #include <assert.h> #include <bitset> #include <cfloat> #include <complex> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stack> #include <string.h> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define REP(i, n) for (int i = 1; i <= (n); i++) #define int long long #define ll long long #define P pair<int, int> #define prique(T) priority_queue<T, vector<T>, greater<T>> #define all(V) V.begin(), V.end() #ifdef int constexpr int INF = LLONG_MAX / 10; #else constexpr int INF = INT_MAX / 10; #endif constexpr double eps = 1e-9; template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } using namespace std; inline ll gcd(ll a, ll b) { while (b) { ll c = a; a = b; b = c % b; } return a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } bool isprime(ll n) { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } ll mypow(ll a, ll b) { if (!b) return 1; if (b & 1) return mypow(a, b - 1) * a; int memo = mypow(a, b >> 1); return memo * memo; } int n, a, b; int bit[1 << 16]; void func(int m, int e, int c, int d, int l, int r) { bit[l] = c; bit[r - 1] = d; if (m == 1) return; int dbit; rep(i, n) { if ((e & (1 << i)) && (c & (1 << i)) != (d & (1 << i))) { dbit = i; break; } } int md = c; rep(i, n) { if ((e & (1 << i)) && i != dbit) { md ^= (1 << i); break; } } func(m - 1, e - (1 << dbit), c, md, l, (l + r) / 2); func(m - 1, e - (1 << dbit), md, d, (l + r) / 2, r); for (int i = l; i < (l + r) / 2; i++) { if (bit[i] & (1 << dbit)) bit[i] -= 1 << dbit; bit[i] += c & (1 << dbit); } for (int i = (l + r) / 2; i < r; i++) { if (bit[i] & (1 << dbit)) bit[i] -= 1 << dbit; bit[i] += d & (1 << dbit); } } signed main() { cin >> n >> a >> b; int cnt = 0; if ((bitset<17>(a).count() & 1) == (bitset<17>(b).count() & 1)) { puts("NO"); return 0; } puts("YES"); func(n, (1 << n) - 1, a, b, 0, (1 << n)); rep(i, (1 << n) - 1) cout << bit[i] << " "; cout << bit[(1 << n) - 1] << endl; return 0; }
#pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") #include <algorithm> #include <assert.h> #include <bitset> #include <cfloat> #include <complex> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stack> #include <string.h> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define REP(i, n) for (int i = 1; i <= (n); i++) #define int long long #define ll long long #define P pair<int, int> #define prique(T) priority_queue<T, vector<T>, greater<T>> #define all(V) V.begin(), V.end() #ifdef int constexpr int INF = LLONG_MAX / 10; #else constexpr int INF = INT_MAX / 10; #endif constexpr double eps = 1e-9; template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } using namespace std; inline ll gcd(ll a, ll b) { while (b) { ll c = a; a = b; b = c % b; } return a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } bool isprime(ll n) { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } ll mypow(ll a, ll b) { if (!b) return 1; if (b & 1) return mypow(a, b - 1) * a; int memo = mypow(a, b >> 1); return memo * memo; } int n, a, b; int bit[1 << 17]; void func(int m, int e, int c, int d, int l, int r) { bit[l] = c; bit[r - 1] = d; if (m == 1) return; int dbit; rep(i, n) { if ((e & (1 << i)) && (c & (1 << i)) != (d & (1 << i))) { dbit = i; break; } } int md = c; rep(i, n) { if ((e & (1 << i)) && i != dbit) { md ^= (1 << i); break; } } func(m - 1, e - (1 << dbit), c, md, l, (l + r) / 2); func(m - 1, e - (1 << dbit), md, d, (l + r) / 2, r); for (int i = l; i < (l + r) / 2; i++) { if (bit[i] & (1 << dbit)) bit[i] -= 1 << dbit; bit[i] += c & (1 << dbit); } for (int i = (l + r) / 2; i < r; i++) { if (bit[i] & (1 << dbit)) bit[i] -= 1 << dbit; bit[i] += d & (1 << dbit); } } signed main() { cin >> n >> a >> b; int cnt = 0; if ((bitset<17>(a).count() & 1) == (bitset<17>(b).count() & 1)) { puts("NO"); return 0; } puts("YES"); func(n, (1 << n) - 1, a, b, 0, (1 << n)); rep(i, (1 << n) - 1) cout << bit[i] << " "; cout << bit[(1 << n) - 1] << endl; return 0; }
replace
82
83
82
83
0
p03097
C++
Time Limit Exceeded
#include <bits/stdc++.h> // iostream is too mainstream #include <cstdio> // bitch please #include <algorithm> #include <cmath> #include <cstdlib> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <time.h> #include <vector> #define dibs reserve #define OVER9000 1234567890 #define ALL_THE(CAKE, LIE) \ for (auto LIE = CAKE.begin(); LIE != CAKE.end(); LIE++) #define tisic 47 #define soclose 1e-8 #define chocolate win // so much chocolate #define patkan 9 #define ff first #define ss second #define abs(x) (((x) < 0) ? -(x) : (x)) #define uint unsigned int #define dbl long double #define pi 3.14159265358979323846 using namespace std; // mylittledoge using cat = long long; #ifdef DONLINE_JUDGE // palindromic tree is better than splay tree! #define lld I64d #endif int popcnt(int x) { int ret = 0; for (int i = 0; i < 17; i++) ret += (x >> i) & 1; return ret; } void build(int mask, int A, int B, int mask_val, int l, int r, vector<int> &ans) { if (l + 1 == r) { // A == B ans[l] = mask_val; return; } int dif = A ^ B, c = (l + r) / 2; for (int i = 0; i < 17; i++) if ((dif >> i) & 1) { if (mask == (1 << i)) { build(0, 0, 0, A | mask_val, l, c, ans); build(0, 0, 0, B | mask_val, c, r, ans); break; } for (int j = 0; j < 17; j++) if (j != i) if ((mask >> j) & 1) { int C = (A - (A & (1 << i))) ^ (1 << j); build(mask - (1 << i), A - (A & (1 << i)), C, mask_val | (A & (1 << i)), l, c, ans); build(mask - (1 << i), C, B - (B & (1 << i)), mask_val | (B & (1 << i)), c, r, ans); } break; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int N, A, B; cin >> N >> A >> B; if (popcnt(A ^ B) % 2 == 0) { cout << "NO\n"; return 0; } vector<int> ans(1 << N); build((1 << N) - 1, A, B, 0, 0, 1 << N, ans); cout << "YES\n"; for (int i = 0; i < (1 << N); i++) cout << ans[i] << ((i == (1 << N) - 1) ? "\n" : " "); return 0; } // look at my code // my code is amazing
#include <bits/stdc++.h> // iostream is too mainstream #include <cstdio> // bitch please #include <algorithm> #include <cmath> #include <cstdlib> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <time.h> #include <vector> #define dibs reserve #define OVER9000 1234567890 #define ALL_THE(CAKE, LIE) \ for (auto LIE = CAKE.begin(); LIE != CAKE.end(); LIE++) #define tisic 47 #define soclose 1e-8 #define chocolate win // so much chocolate #define patkan 9 #define ff first #define ss second #define abs(x) (((x) < 0) ? -(x) : (x)) #define uint unsigned int #define dbl long double #define pi 3.14159265358979323846 using namespace std; // mylittledoge using cat = long long; #ifdef DONLINE_JUDGE // palindromic tree is better than splay tree! #define lld I64d #endif int popcnt(int x) { int ret = 0; for (int i = 0; i < 17; i++) ret += (x >> i) & 1; return ret; } void build(int mask, int A, int B, int mask_val, int l, int r, vector<int> &ans) { if (l + 1 == r) { // A == B ans[l] = mask_val; return; } int dif = A ^ B, c = (l + r) / 2; for (int i = 0; i < 17; i++) if ((dif >> i) & 1) { if (mask == (1 << i)) { build(0, 0, 0, A | mask_val, l, c, ans); build(0, 0, 0, B | mask_val, c, r, ans); break; } for (int j = 0; j < 17; j++) if (j != i) if ((mask >> j) & 1) { int C = (A - (A & (1 << i))) ^ (1 << j); build(mask - (1 << i), A - (A & (1 << i)), C, mask_val | (A & (1 << i)), l, c, ans); build(mask - (1 << i), C, B - (B & (1 << i)), mask_val | (B & (1 << i)), c, r, ans); break; } break; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int N, A, B; cin >> N >> A >> B; if (popcnt(A ^ B) % 2 == 0) { cout << "NO\n"; return 0; } vector<int> ans(1 << N); build((1 << N) - 1, A, B, 0, 0, 1 << N, ans); cout << "YES\n"; for (int i = 0; i < (1 << N); i++) cout << ans[i] << ((i == (1 << N) - 1) ? "\n" : " "); return 0; } // look at my code // my code is amazing
insert
70
70
70
71
TLE
p03097
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; typedef pair<double, double> pdd; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef vector<pii> vii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; // const int mod = ; vi solve(int n, int a, int b) { if (n == 1) { assert(a != b); return {a, b}; } assert(a >= 0 && b >= 0 && max(a, b) < (1 << n)); if (a % 2 == b % 2) { vi v1 = solve(n - 1, a / 2, b / 2); vi v2 = solve(n - 1, a / 2, v1[1]); for (int &x : v2) x = 2 * x + (1 - a % 2); v2.insert(v2.begin(), a); for (int i = 1; i < v1.size(); ++i) { v2.push_back(v1[i] * 2 + a % 2); } return v2; } else { int neig = (a / 2) ^ 1; vi v1 = solve(n - 1, a / 2, neig); vi v2 = solve(n - 1, neig, b); for (int &x : v1) x = 2 * x + a % 2; for (int x : v2) v1.push_back(x * 2 + b % 2); return v1; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n, a, b; cin >> n >> a >> b; if (__builtin_popcount(a ^ b) % 2 == 0) { cout << "NO\n"; return 0; } cout << "YES\n"; vi v = solve(n, a, b); for (int i = 0; i + 1 < v.size(); ++i) { assert(__builtin_popcount(v[i] ^ v[i + 1]) == 1); } for (int x : v) { cout << x << ' '; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; typedef pair<double, double> pdd; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef vector<pii> vii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; // const int mod = ; vi solve(int n, int a, int b) { if (n == 1) { assert(a != b); return {a, b}; } assert(a >= 0 && b >= 0 && max(a, b) < (1 << n)); if (a % 2 == b % 2) { vi v1 = solve(n - 1, a / 2, b / 2); vi v2 = solve(n - 1, a / 2, v1[1]); for (int &x : v2) x = 2 * x + (1 - a % 2); v2.insert(v2.begin(), a); for (int i = 1; i < v1.size(); ++i) { v2.push_back(v1[i] * 2 + a % 2); } return v2; } else { int neig = (a / 2) ^ 1; vi v1 = solve(n - 1, a / 2, neig); vi v2 = solve(n - 1, neig, b / 2); for (int &x : v1) x = 2 * x + a % 2; for (int x : v2) v1.push_back(x * 2 + b % 2); return v1; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n, a, b; cin >> n >> a >> b; if (__builtin_popcount(a ^ b) % 2 == 0) { cout << "NO\n"; return 0; } cout << "YES\n"; vi v = solve(n, a, b); for (int i = 0; i + 1 < v.size(); ++i) { assert(__builtin_popcount(v[i] ^ v[i + 1]) == 1); } for (int x : v) { cout << x << ' '; } cout << endl; return 0; }
replace
35
36
35
36
0
p03097
C++
Runtime Error
#include <cassert> #include <iostream> #include <vector> using namespace std; int N, A, B; vector<int> ans; vector<int> dfs(int start, int goal, int len) { if (len == 1) return (vector<int>){start, goal}; int x = 0; for (int i = 0; i < len; i++) { if ((start ^ goal) & (1 << i)) { x = i; break; } } int a = 0, b = 0; for (int i = 0; i < len; i++) { if (i < x) { if (start & (1 << i)) a += (1 << i); if (goal & (1 << i)) b += (1 << i); } if (i > x) { if (start & (1 << i)) a += (1 << (i - 1)); if (goal & (1 << i)) b += (1 << (i - 1)); } } int c = -1; for (int i = 0; i < len - 1; i++) { if ((a ^ (1 << i)) != b) { c = a ^ (1 << i); break; } } assert(c != -1); vector<int> q = dfs(a, c, len - 1), r = dfs(c, b, len - 1), res; for (auto n : q) { int now = (start & (1 << x) ? (1 << x) : 0); for (int i = 0; i < len - 1; i++) { if (i < x && (n & (1 << i))) now += (1 << i); if (i >= x && (n & (1 << i))) now += (1 << (i + 1)); } res.push_back(now); } for (auto n : r) { int now = (goal & (1 << x) ? (1 << x) : 0); for (int i = 0; i < len - 1; i++) { if (i < x && (n & (1 << i))) now += (1 << i); if (i >= x && (n & (1 << i))) now += (1 << (i + 1)); } res.push_back(now); } return res; } int main() { cin >> N >> A >> B; if (__builtin_popcount(A) % 2 == __builtin_popcount(B)) { cout << "NO" << endl; return 0; } vector<int> ans = dfs(A, B, N); cout << "YES" << endl; for (auto x : ans) cout << x << " "; cout << endl; }
#include <cassert> #include <iostream> #include <vector> using namespace std; int N, A, B; vector<int> ans; vector<int> dfs(int start, int goal, int len) { if (len == 1) return (vector<int>){start, goal}; int x = 0; for (int i = 0; i < len; i++) { if ((start ^ goal) & (1 << i)) { x = i; break; } } int a = 0, b = 0; for (int i = 0; i < len; i++) { if (i < x) { if (start & (1 << i)) a += (1 << i); if (goal & (1 << i)) b += (1 << i); } if (i > x) { if (start & (1 << i)) a += (1 << (i - 1)); if (goal & (1 << i)) b += (1 << (i - 1)); } } int c = -1; for (int i = 0; i < len - 1; i++) { if ((a ^ (1 << i)) != b) { c = a ^ (1 << i); break; } } assert(c != -1); vector<int> q = dfs(a, c, len - 1), r = dfs(c, b, len - 1), res; for (auto n : q) { int now = (start & (1 << x) ? (1 << x) : 0); for (int i = 0; i < len - 1; i++) { if (i < x && (n & (1 << i))) now += (1 << i); if (i >= x && (n & (1 << i))) now += (1 << (i + 1)); } res.push_back(now); } for (auto n : r) { int now = (goal & (1 << x) ? (1 << x) : 0); for (int i = 0; i < len - 1; i++) { if (i < x && (n & (1 << i))) now += (1 << i); if (i >= x && (n & (1 << i))) now += (1 << (i + 1)); } res.push_back(now); } return res; } int main() { cin >> N >> A >> B; if (__builtin_popcount(A) % 2 == __builtin_popcount(B) % 2) { cout << "NO" << endl; return 0; } vector<int> ans = dfs(A, B, N); cout << "YES" << endl; for (auto x : ans) cout << x << " "; cout << endl; }
replace
67
68
67
68
0
p03097
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (a); i >= (b); i--) #define REP(i, n) for (int i = (0); i < (n); i++) #define fi first #define se second #define pb push_back #define mp make_pair using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef long long ll; int n; vi solve(int nn, int l, int r, int bak) { // printf("%d %d %d %d\n",nn,l,r,bak); if (nn == 1) return {l & bak, r & bak}; vi L, R, res; REP(i, n) if ((bak >> i & 1) && ((l >> i & 1) ^ (r >> i & 1))) { int vl = (l >> i & 1), vr = (r >> i & 1), fr = 0; REP(j, n) if ((bak >> j & 1) && (j != i)) fr = 1 << j; L = solve(nn - 1, l, l ^ fr, bak ^ (1 << i)); R = solve(nn - 1, l ^ fr, r, bak ^ (1 << i)); res.resize(1 << nn); int semi = 1 << (nn - 1); REP(j, semi) res[j] = L[j] ^ (vl << i); REP(j, semi) res[j + semi] = R[j] ^ (vr << i); } return res; } int main() { int l, r; cin >> n >> l >> r; if (__builtin_popcount(l ^ r) % 2 == 0) return puts("NO"), 0; vi ans = solve(n, l, r, (1 << n) - 1); puts("YES"); for (auto x : ans) printf("%d ", x); return 0; }
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (a); i >= (b); i--) #define REP(i, n) for (int i = (0); i < (n); i++) #define fi first #define se second #define pb push_back #define mp make_pair using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef long long ll; int n; vi solve(int nn, int l, int r, int bak) { // printf("%d %d %d %d\n",nn,l,r,bak); if (nn == 1) return {l & bak, r & bak}; vi L, R, res; REP(i, n) if ((bak >> i & 1) && ((l >> i & 1) ^ (r >> i & 1))) { int vl = (l >> i & 1), vr = (r >> i & 1), fr = 0; REP(j, n) if ((bak >> j & 1) && (j != i)) fr = 1 << j; L = solve(nn - 1, l, l ^ fr, bak ^ (1 << i)); R = solve(nn - 1, l ^ fr, r, bak ^ (1 << i)); res.resize(1 << nn); int semi = 1 << (nn - 1); REP(j, semi) res[j] = L[j] ^ (vl << i); REP(j, semi) res[j + semi] = R[j] ^ (vr << i); break; } return res; } int main() { int l, r; cin >> n >> l >> r; if (__builtin_popcount(l ^ r) % 2 == 0) return puts("NO"), 0; vi ans = solve(n, l, r, (1 << n) - 1); puts("YES"); for (auto x : ans) printf("%d ", x); return 0; }
insert
30
30
30
31
TLE
p03098
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <vector> #define fo(i, a, b) for (int i = a; i <= b; i++) #define fd(i, a, b) for (int i = a; i >= b; i--) using namespace std; const int N = 1e5 + 5; typedef vector<int> vec; int n, k, c[N]; bool vis[N]; vec a[7], A, p, q; vec inv(vec a) { vec b; b.resize(n); fo(i, 0, n - 1) b[a[i]] = i; return b; } vec mult(vec a, vec b) { vec c; c.resize(n); fo(i, 0, n - 1) c[i] = a[b[i]]; return c; } vec pwr(vec a, int t) { vec b; b.resize(n); fo(i, 0, n - 1) vis[i] = 0; fo(i, 0, n - 1) if (!vis[i]) { if (a[i] == i) { b[i] = i; continue; } int x = i, m = 0; do { vis[c[m++] = x] = 1; x = a[x]; } while (x != i); fo(j, 0, m - 1) b[c[j]] = c[(j + t) % m]; } return b; } int main() { scanf("%d%d", &n, &k); p.resize(n); q.resize(n); fo(i, 0, n - 1) scanf("%d", &p[i]), p[i]--; fo(i, 0, n - 1) scanf("%d", &q[i]), q[i]--; a[1] = p; a[2] = q; if (n <= 6) { fo(i, 3, n) a[i] = mult(a[i - 1], inv(a[i - 2])); fo(i, 0, n - 1) printf("%d ", a[n][i] + 1); return 0; } fo(i, 3, 6) a[i] = mult(a[i - 1], inv(a[i - 2])); A.resize(n); fo(i, 0, n - 1) A[i] = i; A = mult(A, q); A = mult(A, inv(p)); A = mult(A, inv(q)); A = mult(A, p); A = pwr(A, (k - 1) / 6); vec an = a[k % 6]; an = mult(A, an); an = mult(an, inv(A)); fo(i, 0, n - 1) printf("%d ", an[i] + 1); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <vector> #define fo(i, a, b) for (int i = a; i <= b; i++) #define fd(i, a, b) for (int i = a; i >= b; i--) using namespace std; const int N = 1e5 + 5; typedef vector<int> vec; int n, k, c[N]; bool vis[N]; vec a[7], A, p, q; vec inv(vec a) { vec b; b.resize(n); fo(i, 0, n - 1) b[a[i]] = i; return b; } vec mult(vec a, vec b) { vec c; c.resize(n); fo(i, 0, n - 1) c[i] = a[b[i]]; return c; } vec pwr(vec a, int t) { vec b; b.resize(n); fo(i, 0, n - 1) vis[i] = 0; fo(i, 0, n - 1) if (!vis[i]) { if (a[i] == i) { b[i] = i; continue; } int x = i, m = 0; do { vis[c[m++] = x] = 1; x = a[x]; } while (x != i); fo(j, 0, m - 1) b[c[j]] = c[(j + t) % m]; } return b; } int main() { scanf("%d%d", &n, &k); p.resize(n); q.resize(n); fo(i, 0, n - 1) scanf("%d", &p[i]), p[i]--; fo(i, 0, n - 1) scanf("%d", &q[i]), q[i]--; a[1] = p; a[2] = q; if (n <= 6) { fo(i, 3, n) a[i] = mult(a[i - 1], inv(a[i - 2])); fo(i, 0, n - 1) printf("%d ", a[n][i] + 1); return 0; } fo(i, 3, 6) a[i] = mult(a[i - 1], inv(a[i - 2])); A.resize(n); fo(i, 0, n - 1) A[i] = i; A = mult(A, q); A = mult(A, inv(p)); A = mult(A, inv(q)); A = mult(A, p); A = pwr(A, (k - 1) / 6); vec an = a[(k - 1) % 6 + 1]; an = mult(A, an); an = mult(an, inv(A)); fo(i, 0, n - 1) printf("%d ", an[i] + 1); return 0; }
replace
70
71
70
71
0
p03098
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #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 all(x) (x).begin(), (x).end() #define uniq(x) (x).erase(unique(all(x)), (x).end()); #define bit(n) (1LL << (n)) #define dump(x) cerr << #x " = " << (x) << endl using vint = vector<int>; using vvint = vector<vint>; using pint = pair<int, int>; using vpint = vector<pint>; constexpr double pi = 3.1415926535897932384626433832795028; constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } inline int lcm(int a, int b) { return a / gcd(a, b) * b; } template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; template <typename T1, typename T2> inline bool chmax(T1 &a, const T2 &b) { if (a < b) { a = b; return true; } return false; } template <typename T1, typename T2> inline bool chmin(T1 &a, const T2 &b) { if (b < a) { a = b; return true; } return false; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) { os << "(" << rhs.first << ", " << rhs.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) { os << "{"; for (auto itr = rhs.begin(); itr != rhs.end(); itr++) { os << *itr << (next(itr) != rhs.end() ? ", " : ""); } os << "}"; return os; } struct Setup { static constexpr int PREC = 20; Setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } setup; struct Permutation { const int n; std::vector<int> data; Permutation(int n) : n(n), data(n) { for (int i = 0; i < n; i++) { data[i] = i; } } Permutation(std::vector<int> &src) : n(src.size()), data(src) {} bool next() { return std::next_permutation(data.begin(), data.end()); } bool prev() { return std::prev_permutation(data.begin(), data.end()); } bool operator==(const Permutation &rhs) const { return data == rhs.data; } bool operator!=(const Permutation &rhs) const { return data != rhs.data; } int operator[](int i) const { return data[i]; } Permutation &operator*=(const Permutation &rhs) { std::vector<int> tmp(data); for (int i = 0; i < n; i++) { data[i] = tmp[rhs[i]]; } return *this; } Permutation &operator/=(const Permutation &rhs) { return *this *= rhs.inv(); } Permutation operator*(const Permutation &rhs) const { return Permutation(*this) *= rhs; } Permutation operator/(const Permutation &rhs) const { return Permutation(*this) /= rhs; } Permutation inv() const { std::vector<int> ret(n); for (int i = 0; i < n; i++) { ret[data[i]] = i; } return ret; } Permutation pow(long long m) const { std::vector<int> ret(n); std::vector<bool> used(n); for (int i = 0; i < n; i++) { if (used[i]) { continue; } std::vector<int> cyc; int cur = i; do { used[cur] = true; cyc.push_back(cur); cur = data[cur]; } while (cur != i); for (int j = 0; j < cyc.size(); j++) { ret[cyc[j]] = cyc[(j + m) % cyc.size()]; } } return ret; } friend std::ostream &operator<<(std::ostream &os, const Permutation &rhs) { os << "{"; for (int i = 0; i < rhs.n; i++) { os << rhs[i] << (i + 1 != rhs.n ? ", " : ""); } os << "}"; return os; } }; int N, K; signed main() { cin >> N >> K; vint p(N), q(N); rep(i, N) { cin >> p[i], q[i]--; } rep(i, N) { cin >> q[i], q[i]--; } Permutation P(p), Q(q); Permutation O(N); int k = (K - 1) % 6; if (k == 0) { O *= P; } else if (k == 1) { O *= Q; } else if (k == 2) { O *= Q / P; } else if (k == 3) { O *= Q / P / Q; } else if (k == 4) { O *= Q / P / Q * P / Q; } else if (k == 5) { O *= Q / P / Q * P * P / Q; } Permutation S = (Q / P / Q * P).pow((K - 1) / 6); Permutation A = S * O / S; rep(i, N) { cout << A[i] + 1 << endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #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 all(x) (x).begin(), (x).end() #define uniq(x) (x).erase(unique(all(x)), (x).end()); #define bit(n) (1LL << (n)) #define dump(x) cerr << #x " = " << (x) << endl using vint = vector<int>; using vvint = vector<vint>; using pint = pair<int, int>; using vpint = vector<pint>; constexpr double pi = 3.1415926535897932384626433832795028; constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } inline int lcm(int a, int b) { return a / gcd(a, b) * b; } template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; template <typename T1, typename T2> inline bool chmax(T1 &a, const T2 &b) { if (a < b) { a = b; return true; } return false; } template <typename T1, typename T2> inline bool chmin(T1 &a, const T2 &b) { if (b < a) { a = b; return true; } return false; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) { os << "(" << rhs.first << ", " << rhs.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) { os << "{"; for (auto itr = rhs.begin(); itr != rhs.end(); itr++) { os << *itr << (next(itr) != rhs.end() ? ", " : ""); } os << "}"; return os; } struct Setup { static constexpr int PREC = 20; Setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } setup; struct Permutation { const int n; std::vector<int> data; Permutation(int n) : n(n), data(n) { for (int i = 0; i < n; i++) { data[i] = i; } } Permutation(std::vector<int> &src) : n(src.size()), data(src) {} bool next() { return std::next_permutation(data.begin(), data.end()); } bool prev() { return std::prev_permutation(data.begin(), data.end()); } bool operator==(const Permutation &rhs) const { return data == rhs.data; } bool operator!=(const Permutation &rhs) const { return data != rhs.data; } int operator[](int i) const { return data[i]; } Permutation &operator*=(const Permutation &rhs) { std::vector<int> tmp(data); for (int i = 0; i < n; i++) { data[i] = tmp[rhs[i]]; } return *this; } Permutation &operator/=(const Permutation &rhs) { return *this *= rhs.inv(); } Permutation operator*(const Permutation &rhs) const { return Permutation(*this) *= rhs; } Permutation operator/(const Permutation &rhs) const { return Permutation(*this) /= rhs; } Permutation inv() const { std::vector<int> ret(n); for (int i = 0; i < n; i++) { ret[data[i]] = i; } return ret; } Permutation pow(long long m) const { std::vector<int> ret(n); std::vector<bool> used(n); for (int i = 0; i < n; i++) { if (used[i]) { continue; } std::vector<int> cyc; int cur = i; do { used[cur] = true; cyc.push_back(cur); cur = data[cur]; } while (cur != i); for (int j = 0; j < cyc.size(); j++) { ret[cyc[j]] = cyc[(j + m) % cyc.size()]; } } return ret; } friend std::ostream &operator<<(std::ostream &os, const Permutation &rhs) { os << "{"; for (int i = 0; i < rhs.n; i++) { os << rhs[i] << (i + 1 != rhs.n ? ", " : ""); } os << "}"; return os; } }; int N, K; signed main() { cin >> N >> K; vint p(N), q(N); rep(i, N) { cin >> p[i], p[i]--; } rep(i, N) { cin >> q[i], q[i]--; } Permutation P(p), Q(q); Permutation O(N); int k = (K - 1) % 6; if (k == 0) { O *= P; } else if (k == 1) { O *= Q; } else if (k == 2) { O *= Q / P; } else if (k == 3) { O *= Q / P / Q; } else if (k == 4) { O *= Q / P / Q * P / Q; } else if (k == 5) { O *= Q / P / Q * P * P / Q; } Permutation S = (Q / P / Q * P).pow((K - 1) / 6); Permutation A = S * O / S; rep(i, N) { cout << A[i] + 1 << endl; } }
replace
126
127
126
127
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p03099
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> using namespace std; int S = 0, T; int N; struct edge { int s, t, cap; long long v; int nxt; } e[50005]; int e_cnt, last[405]; void addedge(int u, int v, int cap, long long l) { e[e_cnt] = (edge){u, v, cap, l, last[u]}; last[u] = e_cnt++; e[e_cnt] = (edge){v, u, 0, -l, last[v]}; last[v] = e_cnt++; } long long d[405]; int fa[405]; bool vis[405]; #define LIM -(1000000000000001ll) bool dij() { for (int i = 0; i <= T; i++) d[i] = LIM; memset(vis, 0, sizeof(vis)); d[S] = 0; for (int i = 0; i <= T; i++) { int x = -1; for (int j = 0; j <= T; j++) if (!vis[j] && (x == -1 || d[j] > d[x])) x = j; if (x == -1 || d[x] == LIM) break; vis[x] = 1; for (int i = last[x]; i != -1; i = e[i].nxt) if (!vis[e[i].t] && e[i].cap && d[e[i].t] < d[x] + e[i].v) { // assert(e[i].v<=0); d[e[i].t] = d[x] + e[i].v; fa[e[i].t] = i; } } return d[T] > LIM; } pair<int, long long> solve() { int flow = 0; long long cost = 0; long long r = 0; while (dij()) { flow++; int u = T; while (u != S) { e[fa[u]].cap = 0; e[fa[u] ^ 1].cap = 1; u = e[fa[u]].s; } r += d[T]; cost += r; for (int i = 0; i < e_cnt; i++) e[i].v = d[e[i].s] + e[i].v - d[e[i].t]; } return make_pair(flow, cost); } int x[85], y[85]; long long v[85]; int t[325], a[325], b[325]; int L[85][2], R[85][2]; int main() { freopen("e.in", "r", stdin); int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d%lld", &x[i], &y[i], &v[i]); } int m; scanf("%d", &m); for (int i = 1; i <= m; i++) { char c = getchar(); while (c < 'A' || c > 'Z') c = getchar(); if (c == 'L') t[i] = 0; if (c == 'R') t[i] = 1; if (c == 'D') t[i] = 2; if (c == 'U') t[i] = 3; scanf("%d%d", &a[i], &b[i]); } long long ans = 0; for (int supy = 1; supy <= n; supy++) { memset(R, 63, sizeof(R)); for (int j = 1; j <= m; j++) if (b[j] < supy) { if (t[j] & 1) R[supy - b[j]][t[j] >> 1] = min(R[supy - b[j]][t[j] >> 1], a[j] - 1); else L[b[j] + 1][t[j] >> 1] = max(L[b[j] + 1][t[j] >> 1], a[j] + 1); } for (int i = 2; i <= supy; i++) for (int j = 0; j < 2; j++) L[i][j] = max(L[i][j], L[i - 1][j]); for (int i = supy - 1; i > 0; i--) for (int j = 0; j < 2; j++) R[i][j] = min(R[i][j], R[i + 1][j]); // for(int i=1;i<=supy;i++)printf("%d %d\n",L[i][0],R[i][0]); // for(int i=1;i<=supy;i++)printf("%d %d\n",L[i][1],R[i][1]); T = 2 * supy + 2 * n + 1; e_cnt = 0; memset(last, -1, sizeof(last)); for (int i = 1; i <= supy; i++) addedge(S, i, 1, 0), addedge(i + supy, T, 1, 0); for (int i = 1; i <= n; i++) addedge(i + 2 * supy, i + n + 2 * supy, 1, v[i] - 1000000000000000ll); for (int i = 1; i <= supy; i++) for (int j = 1; j <= n; j++) if (x[j] >= L[i][0] && x[j] <= R[i][0]) addedge(i, j + 2 * supy, 1, 0); for (int i = 1; i <= supy; i++) for (int j = 1; j <= n; j++) if (y[j] >= L[i][1] && y[j] <= R[i][1]) addedge(j + n + 2 * supy, i + supy, 1, 0); pair<int, long long> res = solve(); // printf("%d %d %lld\n",supy,res.first,res.second); res.second += supy * 1000000000000000ll; if (res.first == supy) ans = max(ans, res.second); } printf("%lld\n", ans); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> using namespace std; int S = 0, T; int N; struct edge { int s, t, cap; long long v; int nxt; } e[50005]; int e_cnt, last[405]; void addedge(int u, int v, int cap, long long l) { e[e_cnt] = (edge){u, v, cap, l, last[u]}; last[u] = e_cnt++; e[e_cnt] = (edge){v, u, 0, -l, last[v]}; last[v] = e_cnt++; } long long d[405]; int fa[405]; bool vis[405]; #define LIM -(1000000000000001ll) bool dij() { for (int i = 0; i <= T; i++) d[i] = LIM; memset(vis, 0, sizeof(vis)); d[S] = 0; for (int i = 0; i <= T; i++) { int x = -1; for (int j = 0; j <= T; j++) if (!vis[j] && (x == -1 || d[j] > d[x])) x = j; if (x == -1 || d[x] == LIM) break; vis[x] = 1; for (int i = last[x]; i != -1; i = e[i].nxt) if (!vis[e[i].t] && e[i].cap && d[e[i].t] < d[x] + e[i].v) { // assert(e[i].v<=0); d[e[i].t] = d[x] + e[i].v; fa[e[i].t] = i; } } return d[T] > LIM; } pair<int, long long> solve() { int flow = 0; long long cost = 0; long long r = 0; while (dij()) { flow++; int u = T; while (u != S) { e[fa[u]].cap = 0; e[fa[u] ^ 1].cap = 1; u = e[fa[u]].s; } r += d[T]; cost += r; for (int i = 0; i < e_cnt; i++) e[i].v = d[e[i].s] + e[i].v - d[e[i].t]; } return make_pair(flow, cost); } int x[85], y[85]; long long v[85]; int t[325], a[325], b[325]; int L[85][2], R[85][2]; int main() { // freopen("e.in","r",stdin); int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d%lld", &x[i], &y[i], &v[i]); } int m; scanf("%d", &m); for (int i = 1; i <= m; i++) { char c = getchar(); while (c < 'A' || c > 'Z') c = getchar(); if (c == 'L') t[i] = 0; if (c == 'R') t[i] = 1; if (c == 'D') t[i] = 2; if (c == 'U') t[i] = 3; scanf("%d%d", &a[i], &b[i]); } long long ans = 0; for (int supy = 1; supy <= n; supy++) { memset(R, 63, sizeof(R)); for (int j = 1; j <= m; j++) if (b[j] < supy) { if (t[j] & 1) R[supy - b[j]][t[j] >> 1] = min(R[supy - b[j]][t[j] >> 1], a[j] - 1); else L[b[j] + 1][t[j] >> 1] = max(L[b[j] + 1][t[j] >> 1], a[j] + 1); } for (int i = 2; i <= supy; i++) for (int j = 0; j < 2; j++) L[i][j] = max(L[i][j], L[i - 1][j]); for (int i = supy - 1; i > 0; i--) for (int j = 0; j < 2; j++) R[i][j] = min(R[i][j], R[i + 1][j]); // for(int i=1;i<=supy;i++)printf("%d %d\n",L[i][0],R[i][0]); // for(int i=1;i<=supy;i++)printf("%d %d\n",L[i][1],R[i][1]); T = 2 * supy + 2 * n + 1; e_cnt = 0; memset(last, -1, sizeof(last)); for (int i = 1; i <= supy; i++) addedge(S, i, 1, 0), addedge(i + supy, T, 1, 0); for (int i = 1; i <= n; i++) addedge(i + 2 * supy, i + n + 2 * supy, 1, v[i] - 1000000000000000ll); for (int i = 1; i <= supy; i++) for (int j = 1; j <= n; j++) if (x[j] >= L[i][0] && x[j] <= R[i][0]) addedge(i, j + 2 * supy, 1, 0); for (int i = 1; i <= supy; i++) for (int j = 1; j <= n; j++) if (y[j] >= L[i][1] && y[j] <= R[i][1]) addedge(j + n + 2 * supy, i + supy, 1, 0); pair<int, long long> res = solve(); // printf("%d %d %lld\n",supy,res.first,res.second); res.second += supy * 1000000000000000ll; if (res.first == supy) ans = max(ans, res.second); } printf("%lld\n", ans); return 0; }
replace
69
70
69
70
TLE
p03099
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define N 710 #define M 200010 #define int long long #define INF 4611686018427387904LL inline int read() { int x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x; } struct Point { int x, y, val; } p[N]; struct opt { char ch; int x, y; } a[N]; int tot = 1; int fir[N], nex[M], got[M], tak[M], cst[M]; inline void AddEdge(int x, int y, int z, int w) { nex[++tot] = fir[x], fir[x] = tot, got[tot] = y, tak[tot] = z, cst[tot] = w; } inline void Add(int x, int y, int z, int w) { AddEdge(x, y, z, w), AddEdge(y, x, 0, -w); } int xl[N], xr[N], yl[N], yr[N]; int vis[N], dis[N], pre[N], que[N]; inline int spfa(int s, int t) { int l = 1, r = 1; for (int i = 1; i <= t; i++) vis[i] = false, dis[i] = -INF, pre[i] = 0; que[1] = s, dis[s] = 0; while (l <= r) { int x = que[l]; for (int i = fir[x]; i; i = nex[i]) { int y = got[i], w = cst[i]; if (!tak[i] || dis[x] + w <= dis[y]) continue; dis[y] = dis[x] + w, pre[y] = i; if (!vis[y]) vis[y] = true, que[++r] = y; } ++l, vis[x] = false; } return dis[t] != -INF; } inline int getflow(int s, int t) { int flow = INF; for (int i = t; i != s; i = got[pre[i] ^ 1]) { flow = min(flow, tak[pre[i]]); } return flow; } inline void update(int s, int t, int flow, int &Maxflow, int &Mincost) { Maxflow += flow, Mincost += flow * dis[t]; for (int i = t; i != s; i = got[pre[i] ^ 1]) { tak[pre[i]] -= flow, tak[pre[i] ^ 1] += flow; } } inline int solve(int s, int t) { int Maxflow = 0, Mincost = 0; while (spfa(s, t)) { update(s, t, getflow(s, t), Maxflow, Mincost); } return Mincost; } signed main() { int n = read(); for (int i = 1; i <= n; i++) p[i].x = read(), p[i].y = read(), p[i].val = read(); int m = read(), res = 0; for (int i = 1; i <= m; i++) { char ch = getchar(); while (ch != 'U' && ch != 'D' && ch != 'L' && ch != 'R') ch = getchar(); a[i].ch = ch, a[i].x = read(), a[i].y = read(); } for (int k = 1; k <= n; k++) { tot = 1, memset(fir, 0, sizeof(fir)); int s = (k << 1) + (n << 1) + 1, t = s + 1; for (int i = 1; i <= k; i++) Add(s, i, 1, 0); for (int i = k + 2 * n + 1; i <= 2 * k + 2 * n; i++) Add(i, t, 1, 0); for (int i = k + 1; i <= k + n; i++) Add(i, i + n, 1, p[i - k].val); for (int i = 1; i <= k; i++) xl[i] = yl[i] = 0, xr[i] = yr[i] = INF; for (int i = 1; i <= m; i++) { if (a[i].ch == 'U') for (int j = 1; j <= k - a[i].y; j++) yr[j] = min(yr[j], a[i].x - 1); if (a[i].ch == 'D') for (int j = a[i].y + 1; j <= k; j++) yl[j] = max(yl[j], a[i].x + 1); if (a[i].ch == 'L') for (int j = a[i].y + 1; j <= k; j++) xl[j] = max(xl[j], a[i].x + 1); if (a[i].ch == 'R') for (int j = 1; j <= k - a[i].y; j++) xr[j] = min(xr[j], a[i].x - 1); } for (int i = 1; i <= k; i++) for (int j = 1; j <= n; j++) if (xl[i] <= p[j].x && p[j].x <= xr[i]) Add(i, j + k, 1, 0); for (int i = 1; i <= k; i++) for (int j = 1; j <= n; j++) if (yl[i] <= p[j].y && p[j].y <= yr[i]) Add(j + k + n, i + k + 2 * n, 1, 0); res = max(res, solve(s, t)); } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define N 2010 #define M 200010 #define int long long #define INF 4611686018427387904LL inline int read() { int x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x; } struct Point { int x, y, val; } p[N]; struct opt { char ch; int x, y; } a[N]; int tot = 1; int fir[N], nex[M], got[M], tak[M], cst[M]; inline void AddEdge(int x, int y, int z, int w) { nex[++tot] = fir[x], fir[x] = tot, got[tot] = y, tak[tot] = z, cst[tot] = w; } inline void Add(int x, int y, int z, int w) { AddEdge(x, y, z, w), AddEdge(y, x, 0, -w); } int xl[N], xr[N], yl[N], yr[N]; int vis[N], dis[N], pre[N], que[N]; inline int spfa(int s, int t) { int l = 1, r = 1; for (int i = 1; i <= t; i++) vis[i] = false, dis[i] = -INF, pre[i] = 0; que[1] = s, dis[s] = 0; while (l <= r) { int x = que[l]; for (int i = fir[x]; i; i = nex[i]) { int y = got[i], w = cst[i]; if (!tak[i] || dis[x] + w <= dis[y]) continue; dis[y] = dis[x] + w, pre[y] = i; if (!vis[y]) vis[y] = true, que[++r] = y; } ++l, vis[x] = false; } return dis[t] != -INF; } inline int getflow(int s, int t) { int flow = INF; for (int i = t; i != s; i = got[pre[i] ^ 1]) { flow = min(flow, tak[pre[i]]); } return flow; } inline void update(int s, int t, int flow, int &Maxflow, int &Mincost) { Maxflow += flow, Mincost += flow * dis[t]; for (int i = t; i != s; i = got[pre[i] ^ 1]) { tak[pre[i]] -= flow, tak[pre[i] ^ 1] += flow; } } inline int solve(int s, int t) { int Maxflow = 0, Mincost = 0; while (spfa(s, t)) { update(s, t, getflow(s, t), Maxflow, Mincost); } return Mincost; } signed main() { int n = read(); for (int i = 1; i <= n; i++) p[i].x = read(), p[i].y = read(), p[i].val = read(); int m = read(), res = 0; for (int i = 1; i <= m; i++) { char ch = getchar(); while (ch != 'U' && ch != 'D' && ch != 'L' && ch != 'R') ch = getchar(); a[i].ch = ch, a[i].x = read(), a[i].y = read(); } for (int k = 1; k <= n; k++) { tot = 1, memset(fir, 0, sizeof(fir)); int s = (k << 1) + (n << 1) + 1, t = s + 1; for (int i = 1; i <= k; i++) Add(s, i, 1, 0); for (int i = k + 2 * n + 1; i <= 2 * k + 2 * n; i++) Add(i, t, 1, 0); for (int i = k + 1; i <= k + n; i++) Add(i, i + n, 1, p[i - k].val); for (int i = 1; i <= k; i++) xl[i] = yl[i] = 0, xr[i] = yr[i] = INF; for (int i = 1; i <= m; i++) { if (a[i].ch == 'U') for (int j = 1; j <= k - a[i].y; j++) yr[j] = min(yr[j], a[i].x - 1); if (a[i].ch == 'D') for (int j = a[i].y + 1; j <= k; j++) yl[j] = max(yl[j], a[i].x + 1); if (a[i].ch == 'L') for (int j = a[i].y + 1; j <= k; j++) xl[j] = max(xl[j], a[i].x + 1); if (a[i].ch == 'R') for (int j = 1; j <= k - a[i].y; j++) xr[j] = min(xr[j], a[i].x - 1); } for (int i = 1; i <= k; i++) for (int j = 1; j <= n; j++) if (xl[i] <= p[j].x && p[j].x <= xr[i]) Add(i, j + k, 1, 0); for (int i = 1; i <= k; i++) for (int j = 1; j <= n; j++) if (yl[i] <= p[j].y && p[j].y <= yr[i]) Add(j + k + n, i + k + 2 * n, 1, 0); res = max(res, solve(s, t)); } cout << res << endl; return 0; }
replace
2
3
2
3
TLE
p03099
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, a, b) for (register int i = (a); i <= (b); ++i) #define per(i, a, b) for (register int i = (a); i >= (b); --i) #define loop(it, v) for (auto it = v.begin(); it != v.end(); it++) #define cont(i, x) for (register int i = head[x]; i; i = edge[i].nex) #define clr(a) memset(a, 0, sizeof(a)) #define ass(a, cnt) memset(a, cnt, sizeof(a)) #define cop(a, b) memcpy(a, b, sizeof(a)) #define lowbit(x) (x & -x) #define all(x) x.begin(), x.end() #define SC(t, x) static_cast<t>(x) #define ub upper_bound #define lb lower_bound #define pqueue priority_queue #define mp make_pair #define pb push_back #define pof pop_front #define pob pop_back #define fi first #define se second #define y1 y1_ #define Pi acos(-1.0) #define iv inline void #define enter putchar('\n') #define siz(x) ((int)x.size()) #define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout) typedef double db; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vii; typedef queue<int> qi; typedef queue<pii> qii; typedef set<int> si; typedef map<int, int> mii; typedef map<string, int> msi; const int maxn = 200 + 5; const int inf = 0x3f3f3f3f; const int iinf = 1 << 30; const ll linf = 2e18; const ll mod = 998244353; const double eps = 1e-7; template <class T = int> T read() { T f = 1, a = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { a = (a << 3) + (a << 1) + ch - '0'; ch = getchar(); } return a * f; } #define int ll #define inf linf namespace mcmf { const int maxm = 10 * maxn; int n, s, t; struct node { int nex, to, val, cost; } edge[maxm]; int head[maxn], tot = 1; void add_path(int u, int v, int w, int c) { edge[++tot] = (node){head[u], v, w, c}; head[u] = tot; } void add_flow(int u, int v, int w, int c) { add_path(u, v, w, c); add_path(v, u, 0, -c); } int dist[maxn], pre[maxn], lim[maxn], vis[maxn]; bool spfa() { rep(i, 1, n) dist[i] = inf; qi q; q.push(s), dist[s] = 0, lim[s] = inf, lim[t] = 0; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; cont(i, now) { int to = edge[i].to, val = edge[i].val, cost = edge[i].cost; if (!val || dist[to] <= dist[now] + cost) continue; dist[to] = dist[now] + cost, lim[to] = min(lim[now], val), pre[to] = i ^ 1; if (!vis[to]) q.push(to), vis[to] = 1; } } return lim[t]; } int maxflow, mincost; void updata() { maxflow += lim[t]; for (int i = t; i != s; i = edge[pre[i]].to) { edge[pre[i]].val += lim[t], edge[pre[i] ^ 1].val -= lim[t]; mincost += lim[t] * edge[pre[i] ^ 1].cost; } } void clear() { rep(i, 1, n) head[i] = 0; tot = 1; maxflow = mincost = 0; } } // namespace mcmf int n, x[maxn], y[maxn], w[maxn], ans; int m, t[maxn], a[maxn], b[maxn], pos[maxn]; int minn[maxn], maxx[maxn]; void solve(int sum) { // puts("------------solve-------------"); // printf("sum : %lld\n", sum); mcmf ::n = 2 * (sum + n + 1); rep(i, 1, sum << 1) maxx[i] = inf, minn[i] = 0; rep(i, 1, m) { if (b[i] >= sum) continue; if (t[i] == 1) minn[b[i] + 1] = max(minn[b[i] + 1], a[i] + 1); if (t[i] == 2) maxx[sum - b[i]] = min(maxx[sum - b[i]], a[i] - 1); if (t[i] == 3) minn[b[i] + 1 + sum] = max(minn[b[i] + 1 + sum], a[i] + 1); if (t[i] == 4) maxx[sum - b[i] + sum] = min(maxx[sum - b[i] + sum], a[i] - 1); } rep(i, 2, sum) minn[i] = max(minn[i], minn[i - 1]); per(i, sum - 1, 1) maxx[i] = min(maxx[i], maxx[i + 1]); rep(i, sum + 2, sum + sum) minn[i] = max(minn[i], minn[i - 1]); per(i, sum + sum - 1, sum + 1) maxx[i] = min(maxx[i], maxx[i + 1]); // rep(i, 1, sum) // { // printf("i : %lld\n", i); // puts("x : "); // printf("%lld %lld\n", minn[i], maxx[i]); // puts("y : "); // printf("%lld %lld\n", minn[sum + i], maxx[sum + i]); // } // puts("add_limit success"); mcmf ::s = mcmf ::n, mcmf ::t = mcmf ::n - 1; rep(i, 1, sum) mcmf ::add_flow(mcmf ::s, i, 1, 0), mcmf ::add_flow(sum + n + n + i, mcmf ::t, 1, 0); rep(i, 1, n) { mcmf ::add_flow(sum + i, sum + n + i, 1, -w[i]); pos[i] = mcmf ::tot; rep(j, 1, sum) { if (x[i] >= minn[j] && x[i] <= maxx[j]) mcmf ::add_flow(j, sum + i, 1, 0); if (y[i] >= minn[j + sum] && y[i] <= maxx[j + sum]) mcmf ::add_flow(sum + n + i, sum + n + n + j, 1, 0); } } // puts("build_graph success"); while (mcmf ::spfa()) mcmf ::updata(); ans = min(ans, mcmf ::mincost); // printf("mincost : %lld\n", mcmf :: mincost); // puts("choose : "); // rep(i, 1, n) if(mcmf :: edge[pos[i]].val == 1) printf("%lld ", i); // enter; mcmf ::clear(); return; } signed main() { scanf("%lld", &n); rep(i, 1, n) scanf("%lld %lld %lld", &x[i], &y[i], &w[i]); scanf("%lld", &m); rep(i, 1, m) { char tmp; scanf("%s", &tmp); if (tmp == 'L') t[i] = 1; if (tmp == 'R') t[i] = 2; if (tmp == 'D') t[i] = 3; if (tmp == 'U') t[i] = 4; scanf("%lld %lld", &a[i], &b[i]); } rep(i, 1, n) solve(i); printf("%lld\n", -ans); return 0; } /* 7 1 3 6 1 5 9 3 1 8 4 3 8 6 2 9 5 4 11 5 7 10 4 L 3 1 R 2 3 D 5 3 U 4 2 */
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, a, b) for (register int i = (a); i <= (b); ++i) #define per(i, a, b) for (register int i = (a); i >= (b); --i) #define loop(it, v) for (auto it = v.begin(); it != v.end(); it++) #define cont(i, x) for (register int i = head[x]; i; i = edge[i].nex) #define clr(a) memset(a, 0, sizeof(a)) #define ass(a, cnt) memset(a, cnt, sizeof(a)) #define cop(a, b) memcpy(a, b, sizeof(a)) #define lowbit(x) (x & -x) #define all(x) x.begin(), x.end() #define SC(t, x) static_cast<t>(x) #define ub upper_bound #define lb lower_bound #define pqueue priority_queue #define mp make_pair #define pb push_back #define pof pop_front #define pob pop_back #define fi first #define se second #define y1 y1_ #define Pi acos(-1.0) #define iv inline void #define enter putchar('\n') #define siz(x) ((int)x.size()) #define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout) typedef double db; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vii; typedef queue<int> qi; typedef queue<pii> qii; typedef set<int> si; typedef map<int, int> mii; typedef map<string, int> msi; const int maxn = 2e5 + 5; const int inf = 0x3f3f3f3f; const int iinf = 1 << 30; const ll linf = 2e18; const ll mod = 998244353; const double eps = 1e-7; template <class T = int> T read() { T f = 1, a = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { a = (a << 3) + (a << 1) + ch - '0'; ch = getchar(); } return a * f; } #define int ll #define inf linf namespace mcmf { const int maxm = 10 * maxn; int n, s, t; struct node { int nex, to, val, cost; } edge[maxm]; int head[maxn], tot = 1; void add_path(int u, int v, int w, int c) { edge[++tot] = (node){head[u], v, w, c}; head[u] = tot; } void add_flow(int u, int v, int w, int c) { add_path(u, v, w, c); add_path(v, u, 0, -c); } int dist[maxn], pre[maxn], lim[maxn], vis[maxn]; bool spfa() { rep(i, 1, n) dist[i] = inf; qi q; q.push(s), dist[s] = 0, lim[s] = inf, lim[t] = 0; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; cont(i, now) { int to = edge[i].to, val = edge[i].val, cost = edge[i].cost; if (!val || dist[to] <= dist[now] + cost) continue; dist[to] = dist[now] + cost, lim[to] = min(lim[now], val), pre[to] = i ^ 1; if (!vis[to]) q.push(to), vis[to] = 1; } } return lim[t]; } int maxflow, mincost; void updata() { maxflow += lim[t]; for (int i = t; i != s; i = edge[pre[i]].to) { edge[pre[i]].val += lim[t], edge[pre[i] ^ 1].val -= lim[t]; mincost += lim[t] * edge[pre[i] ^ 1].cost; } } void clear() { rep(i, 1, n) head[i] = 0; tot = 1; maxflow = mincost = 0; } } // namespace mcmf int n, x[maxn], y[maxn], w[maxn], ans; int m, t[maxn], a[maxn], b[maxn], pos[maxn]; int minn[maxn], maxx[maxn]; void solve(int sum) { // puts("------------solve-------------"); // printf("sum : %lld\n", sum); mcmf ::n = 2 * (sum + n + 1); rep(i, 1, sum << 1) maxx[i] = inf, minn[i] = 0; rep(i, 1, m) { if (b[i] >= sum) continue; if (t[i] == 1) minn[b[i] + 1] = max(minn[b[i] + 1], a[i] + 1); if (t[i] == 2) maxx[sum - b[i]] = min(maxx[sum - b[i]], a[i] - 1); if (t[i] == 3) minn[b[i] + 1 + sum] = max(minn[b[i] + 1 + sum], a[i] + 1); if (t[i] == 4) maxx[sum - b[i] + sum] = min(maxx[sum - b[i] + sum], a[i] - 1); } rep(i, 2, sum) minn[i] = max(minn[i], minn[i - 1]); per(i, sum - 1, 1) maxx[i] = min(maxx[i], maxx[i + 1]); rep(i, sum + 2, sum + sum) minn[i] = max(minn[i], minn[i - 1]); per(i, sum + sum - 1, sum + 1) maxx[i] = min(maxx[i], maxx[i + 1]); // rep(i, 1, sum) // { // printf("i : %lld\n", i); // puts("x : "); // printf("%lld %lld\n", minn[i], maxx[i]); // puts("y : "); // printf("%lld %lld\n", minn[sum + i], maxx[sum + i]); // } // puts("add_limit success"); mcmf ::s = mcmf ::n, mcmf ::t = mcmf ::n - 1; rep(i, 1, sum) mcmf ::add_flow(mcmf ::s, i, 1, 0), mcmf ::add_flow(sum + n + n + i, mcmf ::t, 1, 0); rep(i, 1, n) { mcmf ::add_flow(sum + i, sum + n + i, 1, -w[i]); pos[i] = mcmf ::tot; rep(j, 1, sum) { if (x[i] >= minn[j] && x[i] <= maxx[j]) mcmf ::add_flow(j, sum + i, 1, 0); if (y[i] >= minn[j + sum] && y[i] <= maxx[j + sum]) mcmf ::add_flow(sum + n + i, sum + n + n + j, 1, 0); } } // puts("build_graph success"); while (mcmf ::spfa()) mcmf ::updata(); ans = min(ans, mcmf ::mincost); // printf("mincost : %lld\n", mcmf :: mincost); // puts("choose : "); // rep(i, 1, n) if(mcmf :: edge[pos[i]].val == 1) printf("%lld ", i); // enter; mcmf ::clear(); return; } signed main() { scanf("%lld", &n); rep(i, 1, n) scanf("%lld %lld %lld", &x[i], &y[i], &w[i]); scanf("%lld", &m); rep(i, 1, m) { char tmp; scanf("%s", &tmp); if (tmp == 'L') t[i] = 1; if (tmp == 'R') t[i] = 2; if (tmp == 'D') t[i] = 3; if (tmp == 'U') t[i] = 4; scanf("%lld %lld", &a[i], &b[i]); } rep(i, 1, n) solve(i); printf("%lld\n", -ans); return 0; } /* 7 1 3 6 1 5 9 3 1 8 4 3 8 6 2 9 5 4 11 5 7 10 4 L 3 1 R 2 3 D 5 3 U 4 2 */
replace
56
57
56
57
0
p03099
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define sqr(x) ((x) * (x)) #define fz1(i, n) for ((i) = 1; (i) <= (n); (i)++) #define fd1(i, n) for ((i) = (n); (i) >= 1; (i)--) #define fz0g(i, n) for ((i) = 0; (i) <= (n); (i)++) #define fd0g(i, n) for ((i) = (n); (i) >= 0; (i)--) #define fz0k(i, n) for ((i) = 0; (i) < (n); (i)++) #define fd0k(i, n) for ((i) = (int)((n)-1); (i) >= 0; (i)--) #define fz(i, x, y) for ((i) = (x); (i) <= (y); (i)++) #define fd(i, y, x) for ((i) = (y); (i) >= (x); (i)--) #define fzin fz1(i, n) #define fzim fz1(i, m) #define fzjn fz1(j, n) #define fzjm fz1(j, m) #define ff(c, itr) \ for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) #define rdst(st, len) \ { \ static char ss[len]; \ scanf(" %s", ss); \ (st) = ss; \ } // #define T_ int using namespace std; // struct bit{T_ arr[1000005];int lowbit(int x){return x&(-x);}void add(int x,T_ // y){for (int i=x;i<=n;i+=lowbit(i)) arr[i]+=y;}T_ query(int x){T_ s=0;for (int // i=x;i>0;i-=lowbit(i)) s+=arr[i];return s;}}; struct seg{struct treedot{int // l,r;T_ sum,add;void update(T_ x){sum+=(r-l+1)*x; add+=x;}}c[800005];void // pushdown(int id){if // (c[id].add){c[id*2].update(c[id].add);c[id*2+1].update(c[id].add);c[id].add=0;}}void // pushup(int id){c[id].sum=c[id*2].sum+c[id*2+1].sum;}void build(int id,int // l,int r){if (l>r) return;c[id].l=l;c[id].r=r;c[id].sum=0;c[id].add=0;if // (l==r) c[id].sum=0;else{int // mid=l+(r-l)/2;build(id*2,l,mid);build(id*2+1,mid+1,r);pushup(id);}}void // update(int id,int le,int ri,T_ x){if (le>c[id].r||ri<c[id].l) return;if // (le<=c[id].l&&c[id].r<=ri) // c[id].update(x);else{pushdown(id);update(id*2,le,ri,x);update(id*2+1,le,ri,x);pushup(id);}}T_ // query(int id,int le,int ri){if (ri<c[id].l||c[id].r<le) return 0;if // (le<=c[id].l&&c[id].r<=ri) return c[id].sum;T_ // ans=0;pushdown(id);ans+=query(id*2,le,ri);ans+=query(id*2+1,le,ri);pushup(id);return // ans;}}; struct bit_2d{T_ a[2005][2005];int lowbit(int x){return x&(-x);}void // add(int x,int y,T_ z){int i,j;for (i=x;i<=2000;i+=lowbit(i)){for // (j=y;j<=2000;j+=lowbit(j)){a[i][j]+=z;}}}T_ query(int x,int y){int i,j;T_ // s=0;for (i=x;i;i-=lowbit(i)){for (j=y;j;j-=lowbit(j)){s+=a[i][j];}}return // s;}T_ query(int x,int y,int xx,int yy){return // query(xx,yy)-query(x-1,yy)-query(xx,y-1)+query(x-1,y-1);}}; int n, m, i, j, k, cnt, a[325], b[325], s = 0, t = 321, l[325], r[325], u[325], d[325], x, y, inq[325], vis[325]; vector<int> allx, ally; long long v[325], cst, ans, dis[325]; char c[325], ca[325], cb[325]; struct ii { int y, z; long long d; } e[10005]; vector<int> bi[10005]; void init() { int i; fz0g(i, 321) bi[i].clear(); cnt = 0; } void add(int x, int y, int z, long long d) { cnt++; bi[x].push_back(cnt * 2 - 2); bi[y].push_back(cnt * 2 - 1); e[cnt * 2 - 2] = (ii){y, z, d}; e[cnt * 2 - 1] = (ii){x, 0, -d}; } bool bfs(int s, int t) { memset(dis, -0x18, sizeof(dis)); dis[s] = 0; queue<int> qx; qx.push(s); inq[s] = 1; while (!qx.empty()) { int x = qx.front(); qx.pop(); inq[x] = 0; ff(bi[x], it) if (e[*it].z) { int y = e[*it].y; long long z = dis[x] + e[*it].d; if (dis[y] < z) { dis[y] = z; if (!inq[y]) { inq[y] = 1; qx.push(y); } } } } return dis[t] > dis[324]; } int dfs(int x, int tt, int flw) { if (!flw) return 0; if (x == tt) { vis[x] = 1; return flw; } if (vis[x]) return 0; vis[x] = 1; int ans = 0; ff(bi[x], it) { // cerr<<e[*it].y<<' '<<e[*it].z<<endl; if (e[*it].z && dis[e[*it].y] == dis[x] + e[*it].d) { int t = dfs(e[*it].y, tt, min(flw, e[*it].z)); cst += t * e[*it].d; ans += t; flw -= t; e[*it].z -= t; e[(*it) ^ 1].z += t; if (!flw) break; } } return ans; } bool dinic(int s, int t, int aa) { int ans = 0; while (bfs(s, t)) { do { memset(vis, 0, sizeof(vis)); ans += dfs(s, t, aa); } while (vis[t]); } if (ans == aa) return 1; return 0; } int main() { scanf("%d", &n); fz1(i, n) { scanf("%d%d%lld", &a[i], &b[i], &v[i]); // allx.push_back(a[i]);ally.push_back(b[i]); } // sort(allx.begin(),allx.end());allx.resize(unique(allx.begin(),allx.end())-allx.begin());sort(ally.begin(),ally.end());ally.resize(unique(ally.begin(),ally.end())-ally.begin()); /*fz1(i,n){ a[i]=upper_bound(allx.begin(),allx.end(),a[i])-allx.begin(); b[i]=upper_bound(ally.begin(),ally.end(),b[i])-ally.begin(); }*/ scanf("%d", &m); fz1(i, m) { scanf(" %c%d%d", &c[i], &ca[i], &cb[i]); } ans = 0; fz1(i, n) { cst = 0; init(); fz1(j, i) { l[i] = d[i] = 0; r[i] = u[i] = 0x18181818; } fz1(j, m) if (cb[j] < i) { if (c[j] == 'L') l[cb[j] + 1] = max(l[cb[j] + 1], ca[j] + 1); if (c[j] == 'R') r[i - cb[j]] = min(r[i - cb[j]], ca[j] - 1); if (c[j] == 'D') d[cb[j] + 1] = max(d[cb[j] + 1], ca[j] + 1); if (c[j] == 'U') u[i - cb[j]] = min(u[i - cb[j]], ca[j] - 1); } fz(j, 2, i) { l[j] = max(l[j], l[j - 1]); d[j] = max(d[j], d[j - 1]); } fd(j, i - 1, 1) { r[j] = min(r[j], r[j + 1]); u[j] = min(u[j], u[j + 1]); } fz1(j, i) fz1(k, n) { if (l[j] <= a[k] && a[k] <= r[j]) { add(j, n + k, 1, 0); } if (d[j] <= b[k] && b[k] <= u[j]) { add(n + n + k, n + n + n + j, 1, 0); } } fz1(j, i) { add(s, j, 1, 0); add(n + n + n + j, t, 1, 0); } fz1(j, n) { add(j + n, j + n + n, 1, v[j]); } if (dinic(s, t, i)) ans = max(ans, cst); } printf("%lld\n", ans); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define sqr(x) ((x) * (x)) #define fz1(i, n) for ((i) = 1; (i) <= (n); (i)++) #define fd1(i, n) for ((i) = (n); (i) >= 1; (i)--) #define fz0g(i, n) for ((i) = 0; (i) <= (n); (i)++) #define fd0g(i, n) for ((i) = (n); (i) >= 0; (i)--) #define fz0k(i, n) for ((i) = 0; (i) < (n); (i)++) #define fd0k(i, n) for ((i) = (int)((n)-1); (i) >= 0; (i)--) #define fz(i, x, y) for ((i) = (x); (i) <= (y); (i)++) #define fd(i, y, x) for ((i) = (y); (i) >= (x); (i)--) #define fzin fz1(i, n) #define fzim fz1(i, m) #define fzjn fz1(j, n) #define fzjm fz1(j, m) #define ff(c, itr) \ for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) #define rdst(st, len) \ { \ static char ss[len]; \ scanf(" %s", ss); \ (st) = ss; \ } // #define T_ int using namespace std; // struct bit{T_ arr[1000005];int lowbit(int x){return x&(-x);}void add(int x,T_ // y){for (int i=x;i<=n;i+=lowbit(i)) arr[i]+=y;}T_ query(int x){T_ s=0;for (int // i=x;i>0;i-=lowbit(i)) s+=arr[i];return s;}}; struct seg{struct treedot{int // l,r;T_ sum,add;void update(T_ x){sum+=(r-l+1)*x; add+=x;}}c[800005];void // pushdown(int id){if // (c[id].add){c[id*2].update(c[id].add);c[id*2+1].update(c[id].add);c[id].add=0;}}void // pushup(int id){c[id].sum=c[id*2].sum+c[id*2+1].sum;}void build(int id,int // l,int r){if (l>r) return;c[id].l=l;c[id].r=r;c[id].sum=0;c[id].add=0;if // (l==r) c[id].sum=0;else{int // mid=l+(r-l)/2;build(id*2,l,mid);build(id*2+1,mid+1,r);pushup(id);}}void // update(int id,int le,int ri,T_ x){if (le>c[id].r||ri<c[id].l) return;if // (le<=c[id].l&&c[id].r<=ri) // c[id].update(x);else{pushdown(id);update(id*2,le,ri,x);update(id*2+1,le,ri,x);pushup(id);}}T_ // query(int id,int le,int ri){if (ri<c[id].l||c[id].r<le) return 0;if // (le<=c[id].l&&c[id].r<=ri) return c[id].sum;T_ // ans=0;pushdown(id);ans+=query(id*2,le,ri);ans+=query(id*2+1,le,ri);pushup(id);return // ans;}}; struct bit_2d{T_ a[2005][2005];int lowbit(int x){return x&(-x);}void // add(int x,int y,T_ z){int i,j;for (i=x;i<=2000;i+=lowbit(i)){for // (j=y;j<=2000;j+=lowbit(j)){a[i][j]+=z;}}}T_ query(int x,int y){int i,j;T_ // s=0;for (i=x;i;i-=lowbit(i)){for (j=y;j;j-=lowbit(j)){s+=a[i][j];}}return // s;}T_ query(int x,int y,int xx,int yy){return // query(xx,yy)-query(x-1,yy)-query(xx,y-1)+query(x-1,y-1);}}; int n, m, i, j, k, cnt, a[325], b[325], s = 0, t = 321, l[325], r[325], u[325], d[325], x, y, inq[325], vis[325]; vector<int> allx, ally; long long v[325], cst, ans, dis[325]; char c[325], ca[325], cb[325]; struct ii { int y, z; long long d; } e[1000005]; vector<int> bi[325]; void init() { int i; fz0g(i, 321) bi[i].clear(); cnt = 0; } void add(int x, int y, int z, long long d) { cnt++; bi[x].push_back(cnt * 2 - 2); bi[y].push_back(cnt * 2 - 1); e[cnt * 2 - 2] = (ii){y, z, d}; e[cnt * 2 - 1] = (ii){x, 0, -d}; } bool bfs(int s, int t) { memset(dis, -0x18, sizeof(dis)); dis[s] = 0; queue<int> qx; qx.push(s); inq[s] = 1; while (!qx.empty()) { int x = qx.front(); qx.pop(); inq[x] = 0; ff(bi[x], it) if (e[*it].z) { int y = e[*it].y; long long z = dis[x] + e[*it].d; if (dis[y] < z) { dis[y] = z; if (!inq[y]) { inq[y] = 1; qx.push(y); } } } } return dis[t] > dis[324]; } int dfs(int x, int tt, int flw) { if (!flw) return 0; if (x == tt) { vis[x] = 1; return flw; } if (vis[x]) return 0; vis[x] = 1; int ans = 0; ff(bi[x], it) { // cerr<<e[*it].y<<' '<<e[*it].z<<endl; if (e[*it].z && dis[e[*it].y] == dis[x] + e[*it].d) { int t = dfs(e[*it].y, tt, min(flw, e[*it].z)); cst += t * e[*it].d; ans += t; flw -= t; e[*it].z -= t; e[(*it) ^ 1].z += t; if (!flw) break; } } return ans; } bool dinic(int s, int t, int aa) { int ans = 0; while (bfs(s, t)) { do { memset(vis, 0, sizeof(vis)); ans += dfs(s, t, aa); } while (vis[t]); } if (ans == aa) return 1; return 0; } int main() { scanf("%d", &n); fz1(i, n) { scanf("%d%d%lld", &a[i], &b[i], &v[i]); // allx.push_back(a[i]);ally.push_back(b[i]); } // sort(allx.begin(),allx.end());allx.resize(unique(allx.begin(),allx.end())-allx.begin());sort(ally.begin(),ally.end());ally.resize(unique(ally.begin(),ally.end())-ally.begin()); /*fz1(i,n){ a[i]=upper_bound(allx.begin(),allx.end(),a[i])-allx.begin(); b[i]=upper_bound(ally.begin(),ally.end(),b[i])-ally.begin(); }*/ scanf("%d", &m); fz1(i, m) { scanf(" %c%d%d", &c[i], &ca[i], &cb[i]); } ans = 0; fz1(i, n) { cst = 0; init(); fz1(j, i) { l[i] = d[i] = 0; r[i] = u[i] = 0x18181818; } fz1(j, m) if (cb[j] < i) { if (c[j] == 'L') l[cb[j] + 1] = max(l[cb[j] + 1], ca[j] + 1); if (c[j] == 'R') r[i - cb[j]] = min(r[i - cb[j]], ca[j] - 1); if (c[j] == 'D') d[cb[j] + 1] = max(d[cb[j] + 1], ca[j] + 1); if (c[j] == 'U') u[i - cb[j]] = min(u[i - cb[j]], ca[j] - 1); } fz(j, 2, i) { l[j] = max(l[j], l[j - 1]); d[j] = max(d[j], d[j - 1]); } fd(j, i - 1, 1) { r[j] = min(r[j], r[j + 1]); u[j] = min(u[j], u[j + 1]); } fz1(j, i) fz1(k, n) { if (l[j] <= a[k] && a[k] <= r[j]) { add(j, n + k, 1, 0); } if (d[j] <= b[k] && b[k] <= u[j]) { add(n + n + k, n + n + n + j, 1, 0); } } fz1(j, i) { add(s, j, 1, 0); add(n + n + n + j, t, 1, 0); } fz1(j, n) { add(j + n, j + n + n, 1, v[j]); } if (dinic(s, t, i)) ans = max(ans, cst); } printf("%lld\n", ans); return 0; }
replace
66
68
66
68
0
p03099
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define Fail puts("NO"), exit(0); #define eps 1e-10 #define maxn 505 #define maxm 100010 #define inf 1000000007 #define mod 1000000007 #define pi acos(-1) #define mp(x, y) make_pair(x, y) #define fi first #define se second using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int, int> pii; typedef bitset<maxn> bit; ll read() { ll x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m, s, t; struct P { int x, y; ll v; } p[85]; struct X { char d; int a, b; } co[325]; int Lx[85], Rx[85], Ly[85], Ry[85]; int head[325], nxt[14010], to[14010], c[14010], tot; ll v[14010]; void add(int x, int y, int z, ll u) { tot++; nxt[tot] = head[x]; head[x] = tot; to[tot] = y; c[tot] = z; v[tot] = u; } void addx(int x, int y, int z, ll u) { add(x, y, z, u); add(y, x, 0, -u); } ll ans, res, dis[325]; int vis[325], pre[325], pre_num[325]; queue<int> q; int spfa() { for (int i = 1; i <= t; i++) dis[i] = -1e18; dis[s] = 0; q.push(s); vis[s] = 1; while (q.size()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = head[now]; i; i = nxt[i]) { if (dis[to[i]] < dis[now] + v[i] && c[i]) { dis[to[i]] = dis[now] + v[i]; pre[to[i]] = now; pre_num[to[i]] = i; if (!vis[to[i]]) q.push(to[i]), vis[to[i]] = 1; } } } if (dis[t] == -1e18) return 0; int di = inf; for (int i = t; i != s; i = pre[i]) di = min(di, c[pre_num[i]]); for (int i = t; i != s; i = pre[i]) c[pre_num[i]] -= di, c[pre_num[i] ^ 1] += di; ans += dis[t] * di; return di; } int main() { n = read(); for (int i = 1; i <= n; i++) p[i].x = read(), p[i].y = read(), p[i].v = read(); m = read(); for (int i = 1; i <= m; i++) co[i].d = getchar(), co[i].a = read(), co[i].b = read(); for (int k = 1; k <= n; k++) { tot = 1; memset(head, 0, sizeof(head)); s = 2 * k + 2 * n + 1; t = s + 1; for (int i = 1; i <= k; i++) addx(s, i, 1, 0); for (int i = k + 2 * n + 1; i <= 2 * n + 2 * k; i++) addx(i, t, 1, 0); for (int i = k + 1; i <= k + n; i++) addx(i, i + n, 1, p[i - k].v); for (int i = 1; i <= k; i++) Lx[i] = Ly[i] = 0, Rx[i] = Ry[i] = inf; for (int i = 1; i <= m; i++) { if (co[i].d == 'U') { for (int j = 1; j <= k - co[i].b; j++) Ry[j] = min(Ry[j], co[i].a - 1); } if (co[i].d == 'D') { for (int j = co[i].b + 1; j <= k; j++) Ly[j] = max(Ly[j], co[i].a + 1); } if (co[i].d == 'L') { for (int j = co[i].b + 1; j <= k; j++) Lx[j] = max(Lx[j], co[i].a + 1); } if (co[i].d == 'R') { for (int j = 1; j <= k - co[i].b; j++) Rx[j] = min(Rx[j], co[i].a - 1); } } for (int i = 1; i <= k; i++) for (int j = 1; j <= n; j++) if (Lx[i] <= p[j].x && p[j].x <= Rx[i]) addx(i, k + j, 1, 0); for (int i = 1; i <= k; i++) for (int j = 1; j <= n; j++) if (Ly[i] <= p[j].y && p[j].y <= Ry[i]) addx(k + n + j, k + 2 * n + i, 1, 0); ans = 0; while (spfa()) { }; res = max(res, ans); } cout << res << endl; return 0; }
#include <bits/stdc++.h> #define Fail puts("NO"), exit(0); #define eps 1e-10 #define maxn 505 #define maxm 100010 #define inf 1000000007 #define mod 1000000007 #define pi acos(-1) #define mp(x, y) make_pair(x, y) #define fi first #define se second using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int, int> pii; typedef bitset<maxn> bit; ll read() { ll x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m, s, t; struct P { int x, y; ll v; } p[85]; struct X { char d; int a, b; } co[325]; int Lx[85], Rx[85], Ly[85], Ry[85]; int head[325], nxt[140010], to[140010], c[140010], tot; ll v[140010]; void add(int x, int y, int z, ll u) { tot++; nxt[tot] = head[x]; head[x] = tot; to[tot] = y; c[tot] = z; v[tot] = u; } void addx(int x, int y, int z, ll u) { add(x, y, z, u); add(y, x, 0, -u); } ll ans, res, dis[325]; int vis[325], pre[325], pre_num[325]; queue<int> q; int spfa() { for (int i = 1; i <= t; i++) dis[i] = -1e18; dis[s] = 0; q.push(s); vis[s] = 1; while (q.size()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = head[now]; i; i = nxt[i]) { if (dis[to[i]] < dis[now] + v[i] && c[i]) { dis[to[i]] = dis[now] + v[i]; pre[to[i]] = now; pre_num[to[i]] = i; if (!vis[to[i]]) q.push(to[i]), vis[to[i]] = 1; } } } if (dis[t] == -1e18) return 0; int di = inf; for (int i = t; i != s; i = pre[i]) di = min(di, c[pre_num[i]]); for (int i = t; i != s; i = pre[i]) c[pre_num[i]] -= di, c[pre_num[i] ^ 1] += di; ans += dis[t] * di; return di; } int main() { n = read(); for (int i = 1; i <= n; i++) p[i].x = read(), p[i].y = read(), p[i].v = read(); m = read(); for (int i = 1; i <= m; i++) co[i].d = getchar(), co[i].a = read(), co[i].b = read(); for (int k = 1; k <= n; k++) { tot = 1; memset(head, 0, sizeof(head)); s = 2 * k + 2 * n + 1; t = s + 1; for (int i = 1; i <= k; i++) addx(s, i, 1, 0); for (int i = k + 2 * n + 1; i <= 2 * n + 2 * k; i++) addx(i, t, 1, 0); for (int i = k + 1; i <= k + n; i++) addx(i, i + n, 1, p[i - k].v); for (int i = 1; i <= k; i++) Lx[i] = Ly[i] = 0, Rx[i] = Ry[i] = inf; for (int i = 1; i <= m; i++) { if (co[i].d == 'U') { for (int j = 1; j <= k - co[i].b; j++) Ry[j] = min(Ry[j], co[i].a - 1); } if (co[i].d == 'D') { for (int j = co[i].b + 1; j <= k; j++) Ly[j] = max(Ly[j], co[i].a + 1); } if (co[i].d == 'L') { for (int j = co[i].b + 1; j <= k; j++) Lx[j] = max(Lx[j], co[i].a + 1); } if (co[i].d == 'R') { for (int j = 1; j <= k - co[i].b; j++) Rx[j] = min(Rx[j], co[i].a - 1); } } for (int i = 1; i <= k; i++) for (int j = 1; j <= n; j++) if (Lx[i] <= p[j].x && p[j].x <= Rx[i]) addx(i, k + j, 1, 0); for (int i = 1; i <= k; i++) for (int j = 1; j <= n; j++) if (Ly[i] <= p[j].y && p[j].y <= Ry[i]) addx(k + n + j, k + 2 * n + i, 1, 0); ans = 0; while (spfa()) { }; res = max(res, ans); } cout << res << endl; return 0; }
replace
41
43
41
43
TLE
p03099
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #define fo(i, j, l) for (int i = j; i <= l; ++i) #define fd(i, j, l) for (int i = j; i >= l; --i) using namespace std; typedef long long ll; const ll N = 600, M = N * 10, maxn = 1e18; bool bz[N]; int x[N], y[N]; ll v[N]; int la[N], lb[M], ne[M], le[M]; ll cost[M]; int fr[N], f1[N]; int n, m, oo, S, T; char c[N]; int a[N], b[N], d[M]; int p[N][2], q[N][2]; ll dis[N]; ll rec; inline ll max(ll a, ll b) { return a > b ? a : b; } inline int min(int a, int b) { return a < b ? a : b; } inline void llb(int a, int b, int c, ll co) { ne[++oo] = la[a]; la[a] = oo; lb[oo] = b; le[oo] = c; cost[oo] = co; } inline void gg(int a, int b, int c, ll d) { llb(a, b, c, d); llb(b, a, 0, -d); } inline bool flow() { fo(i, 1, T) dis[i] = maxn; dis[S] = 0; int l = 0, r = 1; d[1] = S; bz[S] = true; while (l < r) { int o = d[++l]; for (int y = la[o]; y; y = ne[y]) if (dis[o] - cost[y] < dis[lb[y]] && le[y] == 1) { dis[lb[y]] = dis[o] - cost[y]; fr[lb[y]] = o; f1[lb[y]] = y; if (!bz[lb[y]]) d[++r] = lb[y], bz[lb[y]] = true; } bz[o] = false; } if (dis[T] == maxn) return false; rec = 0; for (int u = T; u != S; u = fr[u]) { int k = f1[u]; le[k] = 0; le[k ^ 1] = 1; rec = rec + cost[k]; } return true; } int main() { scanf("%d", &n); fo(i, 1, n) scanf("%d%d%lld", &x[i], &y[i], &v[i]); scanf("%d", &m); fo(i, 1, m) { char ch = ' '; for (; ch != 'L' && ch != 'R' && ch != 'D' && ch != 'U'; ch = getchar()) ; c[i] = ch; scanf("%d%d", &a[i], &b[i]); } ll ans = 0; fo(i, 1, n) { fo(l, 1, i) p[l][0] = 1, p[l][1] = 100; fo(l, 1, i) q[l][0] = 1, q[l][1] = 100; fo(l, 1, m) if (c[l] == 'L' && b[l] < i) { fo(j, b[l] + 1, i) p[j][0] = max(p[j][0], a[l] + 1); } else if (c[l] == 'R' && b[l] < i) { fo(j, 1, i - b[l]) p[j][1] = min(p[j][1], a[l] - 1); } else if (c[l] == 'U' && b[l] < i) { fo(j, 1, i - b[l]) q[j][1] = min(q[j][1], a[l] - 1); } else if (c[l] == 'D' && b[l] < i) { fo(j, b[l] + 1, i) q[j][0] = max(q[j][0], a[l] + 1); } int ok = 0; fo(l, 1, i) if (p[l][0] > p[l][1] || q[l][0] > q[l][1]) { ok = 1; break; } if (ok) continue; oo = 1; S = 2 * i + 200 + 1, T = S + 1; fo(j, 1, T) la[j] = 0; fo(l, 1, i) gg(S, l, 1, 0); fo(l, 1, i) gg(i + 200 + l, T, 1, 0); fo(j, 1, i) fo(l, p[j][0], p[j][1]) gg(j, i + l, 1, 0); fo(j, 1, i) fo(l, q[j][0], q[j][1]) gg(100 + i + l, 200 + i + j, 1, 0); fo(l, 1, n) gg(i + x[l], i + 100 + y[l], 1, v[l]); ll da = 0; int cs = 0; while (flow()) { da = da + rec; ++cs; } if (cs == i) ans = max(ans, da); } cout << ans; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #define fo(i, j, l) for (int i = j; i <= l; ++i) #define fd(i, j, l) for (int i = j; i >= l; --i) using namespace std; typedef long long ll; const ll N = 1000, M = N * 50, maxn = 1e18; bool bz[N]; int x[N], y[N]; ll v[N]; int la[N], lb[M], ne[M], le[M]; ll cost[M]; int fr[N], f1[N]; int n, m, oo, S, T; char c[N]; int a[N], b[N], d[M]; int p[N][2], q[N][2]; ll dis[N]; ll rec; inline ll max(ll a, ll b) { return a > b ? a : b; } inline int min(int a, int b) { return a < b ? a : b; } inline void llb(int a, int b, int c, ll co) { ne[++oo] = la[a]; la[a] = oo; lb[oo] = b; le[oo] = c; cost[oo] = co; } inline void gg(int a, int b, int c, ll d) { llb(a, b, c, d); llb(b, a, 0, -d); } inline bool flow() { fo(i, 1, T) dis[i] = maxn; dis[S] = 0; int l = 0, r = 1; d[1] = S; bz[S] = true; while (l < r) { int o = d[++l]; for (int y = la[o]; y; y = ne[y]) if (dis[o] - cost[y] < dis[lb[y]] && le[y] == 1) { dis[lb[y]] = dis[o] - cost[y]; fr[lb[y]] = o; f1[lb[y]] = y; if (!bz[lb[y]]) d[++r] = lb[y], bz[lb[y]] = true; } bz[o] = false; } if (dis[T] == maxn) return false; rec = 0; for (int u = T; u != S; u = fr[u]) { int k = f1[u]; le[k] = 0; le[k ^ 1] = 1; rec = rec + cost[k]; } return true; } int main() { scanf("%d", &n); fo(i, 1, n) scanf("%d%d%lld", &x[i], &y[i], &v[i]); scanf("%d", &m); fo(i, 1, m) { char ch = ' '; for (; ch != 'L' && ch != 'R' && ch != 'D' && ch != 'U'; ch = getchar()) ; c[i] = ch; scanf("%d%d", &a[i], &b[i]); } ll ans = 0; fo(i, 1, n) { fo(l, 1, i) p[l][0] = 1, p[l][1] = 100; fo(l, 1, i) q[l][0] = 1, q[l][1] = 100; fo(l, 1, m) if (c[l] == 'L' && b[l] < i) { fo(j, b[l] + 1, i) p[j][0] = max(p[j][0], a[l] + 1); } else if (c[l] == 'R' && b[l] < i) { fo(j, 1, i - b[l]) p[j][1] = min(p[j][1], a[l] - 1); } else if (c[l] == 'U' && b[l] < i) { fo(j, 1, i - b[l]) q[j][1] = min(q[j][1], a[l] - 1); } else if (c[l] == 'D' && b[l] < i) { fo(j, b[l] + 1, i) q[j][0] = max(q[j][0], a[l] + 1); } int ok = 0; fo(l, 1, i) if (p[l][0] > p[l][1] || q[l][0] > q[l][1]) { ok = 1; break; } if (ok) continue; oo = 1; S = 2 * i + 200 + 1, T = S + 1; fo(j, 1, T) la[j] = 0; fo(l, 1, i) gg(S, l, 1, 0); fo(l, 1, i) gg(i + 200 + l, T, 1, 0); fo(j, 1, i) fo(l, p[j][0], p[j][1]) gg(j, i + l, 1, 0); fo(j, 1, i) fo(l, q[j][0], q[j][1]) gg(100 + i + l, 200 + i + j, 1, 0); fo(l, 1, n) gg(i + x[l], i + 100 + y[l], 1, v[l]); ll da = 0; int cs = 0; while (flow()) { da = da + rec; ++cs; } if (cs == i) ans = max(ans, da); } cout << ans; }
replace
10
11
10
11
0
p03099
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ref(i, x, y) for (int i = x; i <= y; ++i) #define def(i, x, y) for (int i = x; i >= y; --i) #define fi first #define se second #define int long long #define pb push_back typedef long long LL; const int N = 221; int n, m; int lx[2][N], ly[2][N]; struct dot { int x, y, v, id; } p[N], px[N], py[N]; bool cmpx(dot a, dot b) { return a.x < b.x; } bool cmpy(dot a, dot b) { return a.y < b.y; } void smin(int &a, int b) { if (b < a) a = b; } void smax(int &a, int b) { if (b > a) a = b; } namespace eden { int w[N][N], c[N][N], op, ed; void init() { memset(w, 0, sizeof w); memset(c, 0, sizeof c); } void add(int x, int y, int ww, int cc) { w[x][y] = ww; w[y][x] = 0; c[x][y] = cc; c[y][x] = -cc; } queue<int> q; bool vis[N]; int dis[N], pre[N]; bool spfa() { memset(vis, 0, sizeof vis); memset(pre, 0, sizeof pre); while (!q.empty()) q.pop(); ref(i, 1, ed) dis[i] = (LL)1e18; dis[op] = 0; q.push(op); vis[op] = 1; while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; ref(v, 1, ed) if (w[u][v] && dis[u] + c[u][v] < dis[v]) { dis[v] = dis[u] + c[u][v]; pre[v] = u; if (!vis[v]) vis[v] = 1, q.push(v); } } return (dis[ed] < (LL)1e18); } pair<int, int> run() { int fw = 0, res = 0; while (spfa()) { fw++; res += dis[ed]; for (int i = ed; i != op; i = pre[i]) w[pre[i]][i]--, w[i][pre[i]]++; } return make_pair(fw, res); } } // namespace eden signed main() { cin >> n; ref(i, 1, n) cin >> p[i].x >> p[i].y >> p[i].v; ref(i, 1, n) p[i].id = i; ref(i, 1, n) px[i] = p[i]; ref(i, 1, n) py[i] = p[i]; sort(px + 1, px + n + 1, cmpx); sort(py + 1, py + n + 1, cmpy); cin >> m; ref(i, 1, n) lx[0][i] = ly[0][i] = n + 1; ref(i, 1, n) lx[1][i] = ly[1][i] = 0; ref(i, 1, m) { char c; int a, b; cin >> c >> a >> b; if (c == 'L') { int pos = 0; ref(j, 1, n) if (px[j].x <= a) pos = j; if (pos > b) smin(lx[0][pos - b], pos); } if (c == 'R') { int pos = n + 1; def(j, n, 1) if (px[j].x >= a) pos = j; if (n + 1 - pos > b) smax(lx[1][n + 1 - pos - b], pos); } if (c == 'D') { int pos = 0; ref(j, 1, n) if (py[j].y <= a) pos = j; if (pos > b) smin(ly[0][pos - b], pos); } if (c == 'U') { int pos = n + 1; def(j, n, 1) if (py[j].y >= a) pos = j; if (n + 1 - pos > b) smax(ly[1][n + 1 - pos - b], pos); } } def(i, n - 1, 1) smin(lx[0][i], lx[0][i + 1]); def(i, n - 1, 1) smin(ly[0][i], ly[0][i + 1]); def(i, n - 1, 1) smax(lx[1][i], lx[1][i + 1]); def(i, n - 1, 1) smax(ly[1][i], ly[1][i + 1]); int ans = 0, sum = 0; ref(i, 1, n) ans += p[i].v, sum += p[i].v; ref(S, 0, n) { if (S < n) { if (lx[0][S + 1] <= n || ly[0][S + 1] <= n) continue; if (lx[1][S + 1] >= 1 || ly[1][S + 1] >= 1) continue; } bool fg = 1; ref(i, 1, S) if (lx[1][S + 1 - i] > lx[0][i]) { fg = 0; break; } ref(i, 1, S) if (ly[1][S + 1 - i] > ly[0][i]) { fg = 0; break; } if (!fg) continue; eden::init(); eden::op = n + n + n + n + 1; eden::ed = eden::op + 1; ref(i, 1, S) ref(j, max(1LL, lx[1][S + 1 - i]), min(n, lx[0][i])) eden::add(i, n + px[j].id, 1, 0); ref(i, 1, n) eden::add(n + i, n + n + i, 1, p[i].v); ref(i, 1, S) ref(j, max(1LL, ly[1][S + 1 - i]), min(n, ly[0][i])) eden::add(n + n + py[j].id, n + n + n + i, 1, 0); ref(i, 1, S) eden::add(eden::op, i, 1, 0); ref(i, 1, S) eden::add(n + n + n + i, eden::ed, 1, 0); pair<int, int> pp = eden::run(); if (pp.fi == S) ans = min(ans, pp.se); } cout << sum - ans << endl; }
#include <bits/stdc++.h> using namespace std; #define ref(i, x, y) for (int i = x; i <= y; ++i) #define def(i, x, y) for (int i = x; i >= y; --i) #define fi first #define se second #define int long long #define pb push_back typedef long long LL; const int N = 421; int n, m; int lx[2][N], ly[2][N]; struct dot { int x, y, v, id; } p[N], px[N], py[N]; bool cmpx(dot a, dot b) { return a.x < b.x; } bool cmpy(dot a, dot b) { return a.y < b.y; } void smin(int &a, int b) { if (b < a) a = b; } void smax(int &a, int b) { if (b > a) a = b; } namespace eden { int w[N][N], c[N][N], op, ed; void init() { memset(w, 0, sizeof w); memset(c, 0, sizeof c); } void add(int x, int y, int ww, int cc) { w[x][y] = ww; w[y][x] = 0; c[x][y] = cc; c[y][x] = -cc; } queue<int> q; bool vis[N]; int dis[N], pre[N]; bool spfa() { memset(vis, 0, sizeof vis); memset(pre, 0, sizeof pre); while (!q.empty()) q.pop(); ref(i, 1, ed) dis[i] = (LL)1e18; dis[op] = 0; q.push(op); vis[op] = 1; while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; ref(v, 1, ed) if (w[u][v] && dis[u] + c[u][v] < dis[v]) { dis[v] = dis[u] + c[u][v]; pre[v] = u; if (!vis[v]) vis[v] = 1, q.push(v); } } return (dis[ed] < (LL)1e18); } pair<int, int> run() { int fw = 0, res = 0; while (spfa()) { fw++; res += dis[ed]; for (int i = ed; i != op; i = pre[i]) w[pre[i]][i]--, w[i][pre[i]]++; } return make_pair(fw, res); } } // namespace eden signed main() { cin >> n; ref(i, 1, n) cin >> p[i].x >> p[i].y >> p[i].v; ref(i, 1, n) p[i].id = i; ref(i, 1, n) px[i] = p[i]; ref(i, 1, n) py[i] = p[i]; sort(px + 1, px + n + 1, cmpx); sort(py + 1, py + n + 1, cmpy); cin >> m; ref(i, 1, n) lx[0][i] = ly[0][i] = n + 1; ref(i, 1, n) lx[1][i] = ly[1][i] = 0; ref(i, 1, m) { char c; int a, b; cin >> c >> a >> b; if (c == 'L') { int pos = 0; ref(j, 1, n) if (px[j].x <= a) pos = j; if (pos > b) smin(lx[0][pos - b], pos); } if (c == 'R') { int pos = n + 1; def(j, n, 1) if (px[j].x >= a) pos = j; if (n + 1 - pos > b) smax(lx[1][n + 1 - pos - b], pos); } if (c == 'D') { int pos = 0; ref(j, 1, n) if (py[j].y <= a) pos = j; if (pos > b) smin(ly[0][pos - b], pos); } if (c == 'U') { int pos = n + 1; def(j, n, 1) if (py[j].y >= a) pos = j; if (n + 1 - pos > b) smax(ly[1][n + 1 - pos - b], pos); } } def(i, n - 1, 1) smin(lx[0][i], lx[0][i + 1]); def(i, n - 1, 1) smin(ly[0][i], ly[0][i + 1]); def(i, n - 1, 1) smax(lx[1][i], lx[1][i + 1]); def(i, n - 1, 1) smax(ly[1][i], ly[1][i + 1]); int ans = 0, sum = 0; ref(i, 1, n) ans += p[i].v, sum += p[i].v; ref(S, 0, n) { if (S < n) { if (lx[0][S + 1] <= n || ly[0][S + 1] <= n) continue; if (lx[1][S + 1] >= 1 || ly[1][S + 1] >= 1) continue; } bool fg = 1; ref(i, 1, S) if (lx[1][S + 1 - i] > lx[0][i]) { fg = 0; break; } ref(i, 1, S) if (ly[1][S + 1 - i] > ly[0][i]) { fg = 0; break; } if (!fg) continue; eden::init(); eden::op = n + n + n + n + 1; eden::ed = eden::op + 1; ref(i, 1, S) ref(j, max(1LL, lx[1][S + 1 - i]), min(n, lx[0][i])) eden::add(i, n + px[j].id, 1, 0); ref(i, 1, n) eden::add(n + i, n + n + i, 1, p[i].v); ref(i, 1, S) ref(j, max(1LL, ly[1][S + 1 - i]), min(n, ly[0][i])) eden::add(n + n + py[j].id, n + n + n + i, 1, 0); ref(i, 1, S) eden::add(eden::op, i, 1, 0); ref(i, 1, S) eden::add(n + n + n + i, eden::ed, 1, 0); pair<int, int> pp = eden::run(); if (pp.fi == S) ans = min(ans, pp.se); } cout << sum - ans << endl; }
replace
9
10
9
10
0
p03099
C++
Runtime Error
#include <bits/stdc++.h> #define N 100 #define M 400 #define V 200 #define E 1000 #define INF 1000000000000000000LL using namespace std; int n; struct jewel { int x, y; long long v; } J[N]; struct table { int n, a[N]; void add(int x) { a[++n] = x; } void su() { sort(a + 1, a + n + 1); n = unique(a + 1, a + n + 1) - a - 1; } int lst(int x) { return upper_bound(a + 1, a + n + 1, x) - a - 1; } int nxt(int x) { return lower_bound(a + 1, a + n + 1, x) - a; } } X, Y; int m; struct cond { char t; int a, b; } C[M]; int e, head[V], nxt[E], to[E], cap[E], op[E]; long long val[E]; void add(int x, int y, int c, long long v) { nxt[++e] = head[x], to[head[x] = e] = y, cap[e] = c, val[e] = v, op[e] = e + 1; nxt[++e] = head[y], to[head[y] = e] = x, cap[e] = 0, val[e] = -v, op[e] = e - 1; } int S, T, from[V], from_edge[V]; long long f[V]; bool vis[V]; queue<int> q; long long spfa() { for (int i = 0; i <= X.n + Y.n + 3; ++i) f[i] = -INF * 2; f[S] = 0, vis[S] = 1, q.push(S); while (!q.empty()) { int x = q.front(); vis[x] = 0, q.pop(); for (int i = head[x]; i; i = nxt[i]) if (cap[i] && f[x] + val[i] > f[to[i]]) { f[to[i]] = f[x] + val[i]; from[to[i]] = x, from_edge[to[i]] = i; if (!vis[to[i]]) vis[to[i]] = 1, q.push(to[i]); } } return f[T]; } int flow; long long cost; void augment() { int delta = N; for (int i = T; i != S; i = from[i]) delta = min(delta, cap[from_edge[i]]); flow += delta; for (int i = T; i != S; i = from[i]) { cap[from_edge[i]] -= delta; cap[op[from_edge[i]]] += delta; if (val[from_edge[i]] == -INF) continue; cost += val[from_edge[i]] * delta; } } int R_min[N], R_max[N], U_min[N], U_max[N]; int in[V]; long long solve(int k) { int s = 0, t = X.n + 1, ss = X.n + Y.n + 2, tt = X.n + Y.n + 3; e = 0; for (int i = 0; i <= X.n + Y.n + 3; ++i) head[i] = 0; for (int i = 1; i <= X.n + 1; ++i) R_min[i] = 0, R_max[i] = N; for (int i = 1; i <= Y.n + 1; ++i) U_min[i] = 0, U_max[i] = N; R_max[X.n + 1] = U_max[Y.n + 1] = 0; R_min[1] = R_max[1] = k; for (int i = 1; i <= m; ++i) if (C[i].t == 'L') R_min[C[i].a + 1] = max(R_min[C[i].a + 1], k - C[i].b); else if (C[i].t == 'R') R_max[C[i].a] = min(R_max[C[i].a], C[i].b); else if (C[i].t == 'D') U_min[C[i].a + 1] = max(U_min[C[i].a + 1], k - C[i].b); else U_max[C[i].a] = min(U_max[C[i].a], C[i].b); for (int i = 1; i <= X.n + 1; ++i) if (R_min[i] > R_max[i]) return 0; for (int i = 1; i <= Y.n + 1; ++i) if (U_min[i] > U_max[i]) return 0; for (int i = 0; i <= X.n + Y.n + 1; ++i) in[i] = 0; for (int i = 1; i <= X.n; ++i) { add(i - 1, i, R_max[i] - R_min[i], 0); in[i] += R_min[i], in[i - 1] -= R_min[i]; } for (int i = 1; i <= n; ++i) add(X.nxt(J[i].x), t + Y.nxt(J[i].y), 1, J[i].v); for (int i = 1; i <= Y.n; ++i) { add(t + i, t + i - 1, U_max[i] - U_min[i], 0); in[t + i] -= U_min[i], in[t + i - 1] += U_min[i]; } int total = 0; for (int i = 0; i <= X.n + Y.n + 1; ++i) if (in[i] > 0) total += in[i], add(ss, i, in[i], 0); else if (in[i] < 0) add(i, tt, -in[i], 0); add(t, s, N, -INF); S = ss, T = tt, flow = 0, cost = 0; while (spfa() > -INF * 2) augment(); if (flow < total) return 0; S = s, T = t; head[s] = nxt[head[s]]; head[e] = nxt[head[t]]; while (spfa() > 0) augment(); return cost; } int main() { cin >> n; X.n = Y.n = 0; X.add(0), Y.add(0); X.add(101), Y.add(101); for (int i = 1; i <= n; ++i) { cin >> J[i].x >> J[i].y >> J[i].v; X.add(J[i].x), Y.add(J[i].y); } X.su(), Y.su(); cin >> m; for (int i = 1; i <= m; ++i) { char ch = getchar(); while (ch != 'L' && ch != 'R' && ch != 'D' && ch != 'U') ch = getchar(); C[i].t = ch; cin >> C[i].a >> C[i].b; if (ch == 'L') C[i].a = X.lst(C[i].a); else if (ch == 'R') C[i].a = X.nxt(C[i].a); else if (ch == 'D') C[i].a = Y.lst(C[i].a); else C[i].a = Y.nxt(C[i].a); } long long ans = 0; for (int i = 1; i <= n; ++i) ans = max(ans, solve(i)); cout << ans << endl; }
#include <bits/stdc++.h> #define N 1000 #define M 1000 #define V 1000 #define E 10000 #define INF 1000000000000000000LL using namespace std; int n; struct jewel { int x, y; long long v; } J[N]; struct table { int n, a[N]; void add(int x) { a[++n] = x; } void su() { sort(a + 1, a + n + 1); n = unique(a + 1, a + n + 1) - a - 1; } int lst(int x) { return upper_bound(a + 1, a + n + 1, x) - a - 1; } int nxt(int x) { return lower_bound(a + 1, a + n + 1, x) - a; } } X, Y; int m; struct cond { char t; int a, b; } C[M]; int e, head[V], nxt[E], to[E], cap[E], op[E]; long long val[E]; void add(int x, int y, int c, long long v) { nxt[++e] = head[x], to[head[x] = e] = y, cap[e] = c, val[e] = v, op[e] = e + 1; nxt[++e] = head[y], to[head[y] = e] = x, cap[e] = 0, val[e] = -v, op[e] = e - 1; } int S, T, from[V], from_edge[V]; long long f[V]; bool vis[V]; queue<int> q; long long spfa() { for (int i = 0; i <= X.n + Y.n + 3; ++i) f[i] = -INF * 2; f[S] = 0, vis[S] = 1, q.push(S); while (!q.empty()) { int x = q.front(); vis[x] = 0, q.pop(); for (int i = head[x]; i; i = nxt[i]) if (cap[i] && f[x] + val[i] > f[to[i]]) { f[to[i]] = f[x] + val[i]; from[to[i]] = x, from_edge[to[i]] = i; if (!vis[to[i]]) vis[to[i]] = 1, q.push(to[i]); } } return f[T]; } int flow; long long cost; void augment() { int delta = N; for (int i = T; i != S; i = from[i]) delta = min(delta, cap[from_edge[i]]); flow += delta; for (int i = T; i != S; i = from[i]) { cap[from_edge[i]] -= delta; cap[op[from_edge[i]]] += delta; if (val[from_edge[i]] == -INF) continue; cost += val[from_edge[i]] * delta; } } int R_min[N], R_max[N], U_min[N], U_max[N]; int in[V]; long long solve(int k) { int s = 0, t = X.n + 1, ss = X.n + Y.n + 2, tt = X.n + Y.n + 3; e = 0; for (int i = 0; i <= X.n + Y.n + 3; ++i) head[i] = 0; for (int i = 1; i <= X.n + 1; ++i) R_min[i] = 0, R_max[i] = N; for (int i = 1; i <= Y.n + 1; ++i) U_min[i] = 0, U_max[i] = N; R_max[X.n + 1] = U_max[Y.n + 1] = 0; R_min[1] = R_max[1] = k; for (int i = 1; i <= m; ++i) if (C[i].t == 'L') R_min[C[i].a + 1] = max(R_min[C[i].a + 1], k - C[i].b); else if (C[i].t == 'R') R_max[C[i].a] = min(R_max[C[i].a], C[i].b); else if (C[i].t == 'D') U_min[C[i].a + 1] = max(U_min[C[i].a + 1], k - C[i].b); else U_max[C[i].a] = min(U_max[C[i].a], C[i].b); for (int i = 1; i <= X.n + 1; ++i) if (R_min[i] > R_max[i]) return 0; for (int i = 1; i <= Y.n + 1; ++i) if (U_min[i] > U_max[i]) return 0; for (int i = 0; i <= X.n + Y.n + 1; ++i) in[i] = 0; for (int i = 1; i <= X.n; ++i) { add(i - 1, i, R_max[i] - R_min[i], 0); in[i] += R_min[i], in[i - 1] -= R_min[i]; } for (int i = 1; i <= n; ++i) add(X.nxt(J[i].x), t + Y.nxt(J[i].y), 1, J[i].v); for (int i = 1; i <= Y.n; ++i) { add(t + i, t + i - 1, U_max[i] - U_min[i], 0); in[t + i] -= U_min[i], in[t + i - 1] += U_min[i]; } int total = 0; for (int i = 0; i <= X.n + Y.n + 1; ++i) if (in[i] > 0) total += in[i], add(ss, i, in[i], 0); else if (in[i] < 0) add(i, tt, -in[i], 0); add(t, s, N, -INF); S = ss, T = tt, flow = 0, cost = 0; while (spfa() > -INF * 2) augment(); if (flow < total) return 0; S = s, T = t; head[s] = nxt[head[s]]; head[e] = nxt[head[t]]; while (spfa() > 0) augment(); return cost; } int main() { cin >> n; X.n = Y.n = 0; X.add(0), Y.add(0); X.add(101), Y.add(101); for (int i = 1; i <= n; ++i) { cin >> J[i].x >> J[i].y >> J[i].v; X.add(J[i].x), Y.add(J[i].y); } X.su(), Y.su(); cin >> m; for (int i = 1; i <= m; ++i) { char ch = getchar(); while (ch != 'L' && ch != 'R' && ch != 'D' && ch != 'U') ch = getchar(); C[i].t = ch; cin >> C[i].a >> C[i].b; if (ch == 'L') C[i].a = X.lst(C[i].a); else if (ch == 'R') C[i].a = X.nxt(C[i].a); else if (ch == 'D') C[i].a = Y.lst(C[i].a); else C[i].a = Y.nxt(C[i].a); } long long ans = 0; for (int i = 1; i <= n; ++i) ans = max(ans, solve(i)); cout << ans << endl; }
replace
1
5
1
5
0
p03099
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> #define ll long long using namespace std; const int INF = 2147483647; struct Edge { int to, nxt; int flow; ll cost; } e[10005]; int n, m, s, t, edgenum, head[1005], f[1005], pre[1005], x[85], y[85], a[325], b[325], l1[85], r1[85], l2[85], r2[85]; ll dis[1005], val[85]; bool flag[1005]; char ti[325]; queue<int> q; void add(int u, int v, int f, ll c) { e[++edgenum].cost = c; e[edgenum].flow = f; e[edgenum].to = v; e[edgenum].nxt = head[u]; head[u] = edgenum; } bool SPFA() { for (int i = 1; i <= t; i++) dis[i] = 1e18, flag[i] = 0, f[i] = 0; dis[s] = 0; f[s] = INF; flag[s] = 1; q.push(s); while (!q.empty()) { int node = q.front(); q.pop(); flag[node] = 0; for (int hd = head[node]; hd; hd = e[hd].nxt) { int to = e[hd].to; if (flag[to]) continue; if (e[hd].flow == 0) continue; if (dis[to] > dis[node] + e[hd].cost) { dis[to] = dis[node] + e[hd].cost; f[to] = min(f[node], e[hd].flow); pre[to] = hd; q.push(to); } } } return dis[t] < 0; } ll EK() { ll ans = 0; while (SPFA()) { ans += f[t] * dis[t]; int x = t; while (x) { e[pre[x]].flow -= f[t]; e[pre[x] ^ 1].flow += f[t]; x = e[pre[x] ^ 1].to; } } return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d%lld", &x[i], &y[i], &val[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) { while (ti[i] < 'A' || ti[i] > 'Z') ti[i] = (char)getchar(); scanf("%d%d", &b[i], &a[i]); } ll ans = 0; for (int c = 1; c <= n; c++) { for (int i = 1; i <= c; i++) l1[i] = l2[i] = 1, r1[i] = r2[i] = 100; for (int i = 1; i <= m; i++) { if (ti[i] == 'L') if (a[i] + 1 <= c) l1[a[i] + 1] = max(l1[a[i] + 1], b[i] + 1); if (ti[i] == 'R') if (a[i] + 1 <= c) r1[c - a[i]] = min(r1[c - a[i]], b[i] - 1); if (ti[i] == 'D') if (a[i] + 1 <= c) l2[a[i] + 1] = max(l2[a[i] + 1], b[i] + 1); if (ti[i] == 'U') if (a[i] + 1 <= c) r2[c - a[i]] = min(r2[c - a[i]], b[i] - 1); } for (int i = 2; i <= c; i++) { l1[i] = max(l1[i], l1[i - 1]); l2[i] = max(l2[i], l2[i - 1]); } for (int i = c - 1; i >= 1; i--) { r1[i] = min(r1[i], r1[i + 1]); r2[i] = min(r2[i], r2[i + 1]); } bool flag = 1; for (int i = 1; i <= c; i++) { if (l1[i] > r1[i]) flag = 0; if (l2[i] > r2[i]) flag = 0; } if (!flag) continue; s = 2 * c + 2 * n + 1, t = 2 * c + 2 * n + 2; edgenum = 1; for (int i = 1; i <= t; i++) head[i] = 0; for (int i = 1; i <= c; i++) { add(s, i + 2 * n, 1, 0); add(i + 2 * n, s, 0, 0); add(i + 2 * n + c, t, 1, 0); add(t, i + 2 * n + c, 0, 0); for (int j = 1; j <= n; j++) { if (x[j] >= l1[i] && x[j] <= r1[i]) { add(i + 2 * n, j, 1, 0); add(j, i + 2 * n, 0, 0); } if (y[j] >= l2[i] && y[j] <= r2[i]) { add(j + n, i + 2 * n + c, 1, 0); add(i + 2 * n + c, j + n, 0, 0); } } } for (int i = 1; i <= n; i++) { add(i, i + n, 1, -val[i]); add(i + n, i, 0, val[i]); } ans = max(ans, -EK()); } printf("%lld\n", ans); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> #define ll long long using namespace std; const int INF = 2147483647; struct Edge { int to, nxt; int flow; ll cost; } e[100005]; int n, m, s, t, edgenum, head[1005], f[1005], pre[1005], x[85], y[85], a[325], b[325], l1[85], r1[85], l2[85], r2[85]; ll dis[1005], val[85]; bool flag[1005]; char ti[325]; queue<int> q; void add(int u, int v, int f, ll c) { e[++edgenum].cost = c; e[edgenum].flow = f; e[edgenum].to = v; e[edgenum].nxt = head[u]; head[u] = edgenum; } bool SPFA() { for (int i = 1; i <= t; i++) dis[i] = 1e18, flag[i] = 0, f[i] = 0; dis[s] = 0; f[s] = INF; flag[s] = 1; q.push(s); while (!q.empty()) { int node = q.front(); q.pop(); flag[node] = 0; for (int hd = head[node]; hd; hd = e[hd].nxt) { int to = e[hd].to; if (flag[to]) continue; if (e[hd].flow == 0) continue; if (dis[to] > dis[node] + e[hd].cost) { dis[to] = dis[node] + e[hd].cost; f[to] = min(f[node], e[hd].flow); pre[to] = hd; q.push(to); } } } return dis[t] < 0; } ll EK() { ll ans = 0; while (SPFA()) { ans += f[t] * dis[t]; int x = t; while (x) { e[pre[x]].flow -= f[t]; e[pre[x] ^ 1].flow += f[t]; x = e[pre[x] ^ 1].to; } } return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d%lld", &x[i], &y[i], &val[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) { while (ti[i] < 'A' || ti[i] > 'Z') ti[i] = (char)getchar(); scanf("%d%d", &b[i], &a[i]); } ll ans = 0; for (int c = 1; c <= n; c++) { for (int i = 1; i <= c; i++) l1[i] = l2[i] = 1, r1[i] = r2[i] = 100; for (int i = 1; i <= m; i++) { if (ti[i] == 'L') if (a[i] + 1 <= c) l1[a[i] + 1] = max(l1[a[i] + 1], b[i] + 1); if (ti[i] == 'R') if (a[i] + 1 <= c) r1[c - a[i]] = min(r1[c - a[i]], b[i] - 1); if (ti[i] == 'D') if (a[i] + 1 <= c) l2[a[i] + 1] = max(l2[a[i] + 1], b[i] + 1); if (ti[i] == 'U') if (a[i] + 1 <= c) r2[c - a[i]] = min(r2[c - a[i]], b[i] - 1); } for (int i = 2; i <= c; i++) { l1[i] = max(l1[i], l1[i - 1]); l2[i] = max(l2[i], l2[i - 1]); } for (int i = c - 1; i >= 1; i--) { r1[i] = min(r1[i], r1[i + 1]); r2[i] = min(r2[i], r2[i + 1]); } bool flag = 1; for (int i = 1; i <= c; i++) { if (l1[i] > r1[i]) flag = 0; if (l2[i] > r2[i]) flag = 0; } if (!flag) continue; s = 2 * c + 2 * n + 1, t = 2 * c + 2 * n + 2; edgenum = 1; for (int i = 1; i <= t; i++) head[i] = 0; for (int i = 1; i <= c; i++) { add(s, i + 2 * n, 1, 0); add(i + 2 * n, s, 0, 0); add(i + 2 * n + c, t, 1, 0); add(t, i + 2 * n + c, 0, 0); for (int j = 1; j <= n; j++) { if (x[j] >= l1[i] && x[j] <= r1[i]) { add(i + 2 * n, j, 1, 0); add(j, i + 2 * n, 0, 0); } if (y[j] >= l2[i] && y[j] <= r2[i]) { add(j + n, i + 2 * n + c, 1, 0); add(i + 2 * n + c, j + n, 0, 0); } } } for (int i = 1; i <= n; i++) { add(i, i + n, 1, -val[i]); add(i + n, i, 0, val[i]); } ans = max(ans, -EK()); } printf("%lld\n", ans); return 0; }
replace
11
12
11
12
0
p03099
C++
Runtime Error
#include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const ll N = 1e4 + 10, inf = 1e18; ll w[N], ne[N], la[N], len[N], cst[N], t = 1; ll d[N], dep[N], s[N], ans, S, T, vis[N], cur[N]; void alink(ll x, ll y, ll z, ll l) { w[++t] = y; ne[t] = la[x]; la[x] = t; len[t] = z; cst[t] = l; } void link(ll x, ll y, ll z, ll l) { alink(x, y, z, l); alink(y, x, 0, -l); } void clear() { for (ll i = 1; i <= T; i++) la[i] = 0; t = 1; } ll spfa() { for (ll i = 1; i <= T; i++) s[i] = -inf, vis[i] = 0; ll l = 0, r = 1; s[S] = 0, d[1] = S, vis[S] = 1; while (l < r) { ll x = d[++l]; for (ll y = la[x]; y; y = ne[y]) { ll z = w[y]; if (s[z] < s[x] + cst[y] && len[y]) { s[z] = s[x] + cst[y]; if (!vis[z]) { d[++r] = z; vis[z] = 1; } } } vis[x] = 0; } l = 0, r = 1; vis[S] = 1; while (l < r) { ll x = d[++l]; cur[x] = la[x]; for (ll y = la[x]; y; y = ne[y]) { ll z = w[y]; if (s[z] == s[x] + cst[y] && len[y] && !vis[z]) { dep[z] = dep[x] + 1; vis[z] = 1; d[++r] = z; } } } return vis[T]; } ll flow(ll x, ll val) { if (x == T) return val; ll v1 = val; for (ll y = cur[x]; y; cur[x] = y = ne[y]) { ll z = w[y]; if (dep[z] != dep[x] + 1 || !len[y] || s[z] != s[x] + cst[y]) continue; ll use = flow(z, min(len[y], val)); val -= use; len[y] -= use; len[y ^ 1] += use; ans += use * cst[y]; if (!val) return v1; } return v1 - val; } ll n, px[N], py[N], val[N]; ll L[N], R[N], U[N], D[N]; ll ty[N], lim[N], lc[N], k; ll z1[N], z2[N], c1[N], c2[N]; int main() { cin >> n; for (ll i = 1; i <= n; i++) scanf("%lld%lld%lld", &px[i], &py[i], &val[i]); cin >> k; for (ll i = 1; i <= k; i++) { scanf("\n"); ty[i] = getchar(); scanf("%lld%lld", &lc[i], &lim[i]); } t = 0; for (ll i = 1; i <= n; i++) z1[i] = ++t, z2[i] = ++t, L[i] = 0, R[i] = 100, U[i] = 100, D[i] = 0, c1[i] = ++t, c2[i] = ++t; S = ++t, T = ++t; t = 1; ll max1 = 0; for (ll K = 1; K <= n; K++) { for (ll i = 1; i <= n; i++) L[i] = 0, R[i] = 100, U[i] = 100, D[i] = 0; ans = 0; clear(); for (ll i = 1; i <= k; i++) { if (ty[i] == 'L') { for (ll j = lim[i] + 1; j <= K; j++) L[j] = max(L[j], lc[i] + 1); } if (ty[i] == 'R') { for (ll j = 1; j <= K - lim[i]; j++) R[j] = min(R[j], lc[i] - 1); } if (ty[i] == 'U') { for (ll j = 1; j <= K - lim[i]; j++) U[j] = min(U[j], lc[i] - 1); } if (ty[i] == 'D') { for (ll j = lim[i] + 1; j <= K; j++) D[j] = max(D[j], lc[i] + 1); } } for (ll i = 1; i <= K; i++) link(S, z1[i], 1, 0), link(z2[i], T, 1, 0); for (ll i = 1; i <= K; i++) { for (ll j = 1; j <= n; j++) { if (L[i] <= px[j] && px[j] <= R[i]) link(z1[i], c1[j], 1, 0); if (D[i] <= py[j] && py[j] <= U[i]) link(c2[j], z2[i], 1, 0); } } for (ll i = 1; i <= n; i++) link(c1[i], c2[i], 1, val[i]); while (spfa()) { if (s[T] > 0) flow(S, 1 << 30); else break; } max1 = max(max1, ans); } cout << max1 << endl; }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const ll N = 1005, inf = 1e18, M = 1e6; ll w[M], ne[M], la[M], len[M], cst[M], t = 1; ll d[M], dep[M], s[M], ans, S, T, vis[M], cur[M]; void alink(ll x, ll y, ll z, ll l) { w[++t] = y; ne[t] = la[x]; la[x] = t; len[t] = z; cst[t] = l; } void link(ll x, ll y, ll z, ll l) { alink(x, y, z, l); alink(y, x, 0, -l); } void clear() { for (ll i = 1; i <= T; i++) la[i] = 0; t = 1; } ll spfa() { for (ll i = 1; i <= T; i++) s[i] = -inf, vis[i] = 0; ll l = 0, r = 1; s[S] = 0, d[1] = S, vis[S] = 1; while (l < r) { ll x = d[++l]; for (ll y = la[x]; y; y = ne[y]) { ll z = w[y]; if (s[z] < s[x] + cst[y] && len[y]) { s[z] = s[x] + cst[y]; if (!vis[z]) { d[++r] = z; vis[z] = 1; } } } vis[x] = 0; } l = 0, r = 1; vis[S] = 1; while (l < r) { ll x = d[++l]; cur[x] = la[x]; for (ll y = la[x]; y; y = ne[y]) { ll z = w[y]; if (s[z] == s[x] + cst[y] && len[y] && !vis[z]) { dep[z] = dep[x] + 1; vis[z] = 1; d[++r] = z; } } } return vis[T]; } ll flow(ll x, ll val) { if (x == T) return val; ll v1 = val; for (ll y = cur[x]; y; cur[x] = y = ne[y]) { ll z = w[y]; if (dep[z] != dep[x] + 1 || !len[y] || s[z] != s[x] + cst[y]) continue; ll use = flow(z, min(len[y], val)); val -= use; len[y] -= use; len[y ^ 1] += use; ans += use * cst[y]; if (!val) return v1; } return v1 - val; } ll n, px[N], py[N], val[N]; ll L[N], R[N], U[N], D[N]; ll ty[N], lim[N], lc[N], k; ll z1[N], z2[N], c1[N], c2[N]; int main() { cin >> n; for (ll i = 1; i <= n; i++) scanf("%lld%lld%lld", &px[i], &py[i], &val[i]); cin >> k; for (ll i = 1; i <= k; i++) { scanf("\n"); ty[i] = getchar(); scanf("%lld%lld", &lc[i], &lim[i]); } t = 0; for (ll i = 1; i <= n; i++) z1[i] = ++t, z2[i] = ++t, L[i] = 0, R[i] = 100, U[i] = 100, D[i] = 0, c1[i] = ++t, c2[i] = ++t; S = ++t, T = ++t; t = 1; ll max1 = 0; for (ll K = 1; K <= n; K++) { for (ll i = 1; i <= n; i++) L[i] = 0, R[i] = 100, U[i] = 100, D[i] = 0; ans = 0; clear(); for (ll i = 1; i <= k; i++) { if (ty[i] == 'L') { for (ll j = lim[i] + 1; j <= K; j++) L[j] = max(L[j], lc[i] + 1); } if (ty[i] == 'R') { for (ll j = 1; j <= K - lim[i]; j++) R[j] = min(R[j], lc[i] - 1); } if (ty[i] == 'U') { for (ll j = 1; j <= K - lim[i]; j++) U[j] = min(U[j], lc[i] - 1); } if (ty[i] == 'D') { for (ll j = lim[i] + 1; j <= K; j++) D[j] = max(D[j], lc[i] + 1); } } for (ll i = 1; i <= K; i++) link(S, z1[i], 1, 0), link(z2[i], T, 1, 0); for (ll i = 1; i <= K; i++) { for (ll j = 1; j <= n; j++) { if (L[i] <= px[j] && px[j] <= R[i]) link(z1[i], c1[j], 1, 0); if (D[i] <= py[j] && py[j] <= U[i]) link(c2[j], z2[i], 1, 0); } } for (ll i = 1; i <= n; i++) link(c1[i], c2[i], 1, val[i]); while (spfa()) { if (s[T] > 0) flow(S, 1 << 30); else break; } max1 = max(max1, ans); } cout << max1 << endl; }
replace
5
8
5
8
0
p03099
C++
Runtime Error
#include <bits/stdc++.h> #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 MP make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; const LL INF = 0x3f3f3f3f3f3f3f; namespace flow { const int MAXN = 400; struct edge { int to, cap; LL cost; }; int n, m; edge ed[30005]; vector<int> G[405]; void clear() { m = 0; rep(i, n) G[i].clear(); } void add_edge(int u, int v, int fl, LL co) { ed[m].to = v; ed[m].cap = fl; ed[m].cost = co; G[u].push_back(m); m++; ed[m].to = u; ed[m].cap = 0; ed[m].cost = -co; G[v].push_back(m); m++; } LL h[405]; LL pat[405]; int prev[405]; bool vis[405]; bool dijk(int s, int t) { rep(i, n) { pat[i] = INF; vis[i] = false; } pat[s] = 0; priority_queue<pair<LL, int>> que; que.push(MP(0, s)); while (!que.empty()) { int cur = que.top().second; que.pop(); if (vis[cur]) continue; vis[cur] = true; rep(i, G[cur].size()) { int ce = G[cur][i]; if (ed[ce].cap == 0) continue; if (pat[ed[ce].to] > pat[cur] + ed[ce].cost) { pat[ed[ce].to] = pat[cur] + ed[ce].cost; prev[ed[ce].to] = ce; que.push(MP(-pat[ed[ce].to], ed[ce].to)); } } } return pat[t] != INF; } LL mcf(int s, int t, int mf) { rep(i, n) h[i] = 0; LL ans = 0; while (dijk(s, t)) { // in this occasion flow must be 1 int cur = t, cf = 1; while (cur != s) { ed[prev[cur]].cap -= cf; ed[prev[cur] ^ 1].cap += cf; cur = ed[prev[cur] ^ 1].to; } rep(i, m) ed[i].cost += pat[ed[i ^ 1].to] - pat[ed[i].to]; rep(i, m) h[i] += pat[i]; ans += h[t] * cf; mf--; if (mf == 0) return ans; } return -1; } } // namespace flow int n, x[85], y[85]; LL v[85]; int m, a[325], b[325]; char t[325]; LL ans; int minx[85], maxx[85], miny[85], maxy[85]; LL solve(int tot) { rep(i, n) { minx[i] = miny[i] = -INF; maxx[i] = maxy[i] = INF; } rep(i, m) if (b[i] < tot) { if (t[i] == 'L') minx[b[i]] = max(minx[b[i]], a[i] + 1); else if (t[i] == 'R') maxx[tot - b[i] - 1] = min(maxx[tot - b[i] - 1], a[i] - 1); else if (t[i] == 'D') miny[b[i]] = max(miny[b[i]], a[i] + 1); else if (t[i] == 'U') maxy[tot - b[i] - 1] = min(maxy[tot - b[i] - 1], a[i] - 1); } rep(i, tot - 1) { minx[i + 1] = max(minx[i], minx[i + 1]); miny[i + 1] = max(miny[i], miny[i + 1]); } for (int i = tot - 1; i > 0; i--) { maxx[i - 1] = min(maxx[i - 1], maxx[i]); maxy[i - 1] = min(maxy[i - 1], maxy[i]); } flow::n = 2 + tot * 2 + n * 2; flow::clear(); rep(i, tot) flow::add_edge(tot * 2 + n * 2, i, 1, 0); rep(i, tot) rep(j, n) if (x[j] >= minx[i] && x[j] <= maxx[i]) flow::add_edge(i, j + tot * 2, 1, 0); rep(i, n) flow::add_edge(i + tot * 2, i + tot * 2 + n, 1, INF - v[i]); rep(i, tot) rep(j, n) if (y[j] >= miny[i] && y[j] <= maxy[i]) flow::add_edge(j + tot * 2 + n, i + tot, 1, 0); rep(i, tot) flow::add_edge(i + tot, tot * 2 + n * 2 + 1, 1, 0); LL ret = flow::mcf(flow::n - 2, flow::n - 1, tot); return ret == -1 ? -1 : INF * tot - ret; } int main() { cin >> n; rep(i, n) cin >> x[i] >> y[i] >> v[i]; cin >> m; rep(i, m) cin >> t[i] >> a[i] >> b[i]; for (int t = 0; t <= n; t++) ans = max(ans, solve(t)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #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 MP make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; const LL INF = 0x3f3f3f3f3f3f3f; namespace flow { const int MAXN = 400; struct edge { int to, cap; LL cost; }; int n, m; edge ed[30005]; vector<int> G[405]; void clear() { m = 0; rep(i, n) G[i].clear(); } void add_edge(int u, int v, int fl, LL co) { ed[m].to = v; ed[m].cap = fl; ed[m].cost = co; G[u].push_back(m); m++; ed[m].to = u; ed[m].cap = 0; ed[m].cost = -co; G[v].push_back(m); m++; } LL h[405]; LL pat[405]; int prev[405]; bool vis[405]; bool dijk(int s, int t) { rep(i, n) { pat[i] = INF; vis[i] = false; } pat[s] = 0; priority_queue<pair<LL, int>> que; que.push(MP(0, s)); while (!que.empty()) { int cur = que.top().second; que.pop(); if (vis[cur]) continue; vis[cur] = true; rep(i, G[cur].size()) { int ce = G[cur][i]; if (ed[ce].cap == 0) continue; if (pat[ed[ce].to] > pat[cur] + ed[ce].cost) { pat[ed[ce].to] = pat[cur] + ed[ce].cost; prev[ed[ce].to] = ce; que.push(MP(-pat[ed[ce].to], ed[ce].to)); } } } return pat[t] != INF; } LL mcf(int s, int t, int mf) { rep(i, n) h[i] = 0; LL ans = 0; while (dijk(s, t)) { // in this occasion flow must be 1 int cur = t, cf = 1; while (cur != s) { ed[prev[cur]].cap -= cf; ed[prev[cur] ^ 1].cap += cf; cur = ed[prev[cur] ^ 1].to; } rep(i, m) ed[i].cost += pat[ed[i ^ 1].to] - pat[ed[i].to]; rep(i, n) h[i] += pat[i]; ans += h[t] * cf; mf--; if (mf == 0) return ans; } return -1; } } // namespace flow int n, x[85], y[85]; LL v[85]; int m, a[325], b[325]; char t[325]; LL ans; int minx[85], maxx[85], miny[85], maxy[85]; LL solve(int tot) { rep(i, n) { minx[i] = miny[i] = -INF; maxx[i] = maxy[i] = INF; } rep(i, m) if (b[i] < tot) { if (t[i] == 'L') minx[b[i]] = max(minx[b[i]], a[i] + 1); else if (t[i] == 'R') maxx[tot - b[i] - 1] = min(maxx[tot - b[i] - 1], a[i] - 1); else if (t[i] == 'D') miny[b[i]] = max(miny[b[i]], a[i] + 1); else if (t[i] == 'U') maxy[tot - b[i] - 1] = min(maxy[tot - b[i] - 1], a[i] - 1); } rep(i, tot - 1) { minx[i + 1] = max(minx[i], minx[i + 1]); miny[i + 1] = max(miny[i], miny[i + 1]); } for (int i = tot - 1; i > 0; i--) { maxx[i - 1] = min(maxx[i - 1], maxx[i]); maxy[i - 1] = min(maxy[i - 1], maxy[i]); } flow::n = 2 + tot * 2 + n * 2; flow::clear(); rep(i, tot) flow::add_edge(tot * 2 + n * 2, i, 1, 0); rep(i, tot) rep(j, n) if (x[j] >= minx[i] && x[j] <= maxx[i]) flow::add_edge(i, j + tot * 2, 1, 0); rep(i, n) flow::add_edge(i + tot * 2, i + tot * 2 + n, 1, INF - v[i]); rep(i, tot) rep(j, n) if (y[j] >= miny[i] && y[j] <= maxy[i]) flow::add_edge(j + tot * 2 + n, i + tot, 1, 0); rep(i, tot) flow::add_edge(i + tot, tot * 2 + n * 2 + 1, 1, 0); LL ret = flow::mcf(flow::n - 2, flow::n - 1, tot); return ret == -1 ? -1 : INF * tot - ret; } int main() { cin >> n; rep(i, n) cin >> x[i] >> y[i] >> v[i]; cin >> m; rep(i, m) cin >> t[i] >> a[i] >> b[i]; for (int t = 0; t <= n; t++) ans = max(ans, solve(t)); cout << ans << endl; return 0; }
replace
85
86
85
86
0
p03100
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, m, q, mod, fa[600010], id[50010][3][2], x = 0, g = 0, z; bool can_a[2][50010]; struct edge { int u, v, w; } e[50010]; int find(int a) { return fa[a] == a ? a : fa[a] = find(fa[a]); } int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } void merge(int u, int v) { int fau = find(u), fav = find(v); if (fau == fav) return; fa[fav] = fau; } int main() { scanf("%d %d %d %d", &n, &m, &q, &mod); for (int i = 1; i <= n; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 2; k++) id[i][j][k] = ++x, fa[x] = x; for (int i = 1; i <= m; i++) scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w), g = gcd(abs(e[i].w - e[1].w), g); if (!g) g = mod; mod = gcd(mod, 3 * g), z = e[1].w % g; for (int i = 1; i <= m; i++) { for (int j = 0; j < 3; j++) for (int k = 0; k < 2; k++) { merge(id[e[i].u][j][k], id[e[i].v][(2 * j + e[i].w / g) % 3][k ^ 1]); merge(id[e[i].v][j][k], id[e[i].u][(2 * j + e[i].w / g) % 3][k ^ 1]); } } for (int i = 0, j = z, ed = mod * 2; i < ed; i++, j = 2 * j % mod) can_a[i & 1][j] = 1; for (int i = 1, s, t, r; i <= q; i++) { scanf("%d %d %d", &s, &t, &r); bool flag = false; for (int j = 0; j < 3; j++) for (int k = 0; k < 2; k++) if (find(id[t][0][0]) == find(id[s][j][k])) flag |= can_a[k][(r + z + (3 - j) * g) % mod]; puts(flag ? "YES" : "NO"); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, q, mod, fa[600010], id[50010][3][2], x = 0, g = 0, z; bool can_a[2][1000010]; struct edge { int u, v, w; } e[50010]; int find(int a) { return fa[a] == a ? a : fa[a] = find(fa[a]); } int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } void merge(int u, int v) { int fau = find(u), fav = find(v); if (fau == fav) return; fa[fav] = fau; } int main() { scanf("%d %d %d %d", &n, &m, &q, &mod); for (int i = 1; i <= n; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 2; k++) id[i][j][k] = ++x, fa[x] = x; for (int i = 1; i <= m; i++) scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w), g = gcd(abs(e[i].w - e[1].w), g); if (!g) g = mod; mod = gcd(mod, 3 * g), z = e[1].w % g; for (int i = 1; i <= m; i++) { for (int j = 0; j < 3; j++) for (int k = 0; k < 2; k++) { merge(id[e[i].u][j][k], id[e[i].v][(2 * j + e[i].w / g) % 3][k ^ 1]); merge(id[e[i].v][j][k], id[e[i].u][(2 * j + e[i].w / g) % 3][k ^ 1]); } } for (int i = 0, j = z, ed = mod * 2; i < ed; i++, j = 2 * j % mod) can_a[i & 1][j] = 1; for (int i = 1, s, t, r; i <= q; i++) { scanf("%d %d %d", &s, &t, &r); bool flag = false; for (int j = 0; j < 3; j++) for (int k = 0; k < 2; k++) if (find(id[t][0][0]) == find(id[s][j][k])) flag |= can_a[k][(r + z + (3 - j) * g) % mod]; puts(flag ? "YES" : "NO"); } return 0; }
replace
3
4
3
4
0
p03100
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int Maxn = 50005; int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } vector<tuple<int, int, int>> edge; int fa[6 * Maxn], chk[2][1000005]; int getid(int x, int p, int q) { return 6 * (x - 1) + 3 * p + q; } int Getfa(int x) { return fa[x] = (fa[x] == x ? x : Getfa(fa[x])); } int main() { int n, m, q, mod, g = 0; scanf("%d%d%d%d", &n, &m, &q, &mod); for (int i = 1; i <= 6 * n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { static int lst = -1; int x, y, v; scanf("%d%d%d", &x, &y, &v); edge.emplace_back(x, y, v); if (~lst) g = gcd(g, abs(v - lst)); lst = v; } mod = gcd(mod, 3 * g); for (int i = 0; i < 2 * mod; i++) { static int j = get<2>(edge[0]) % g; chk[i & 1][j] = 1; j = 2 * j % mod; } for (auto &e : edge) { int x, y, v; tie(x, y, v) = e; for (int p = 0; p < 2; p++) for (int q = 0; q < 3; q++) { fa[Getfa(getid(x, p, q))] = Getfa(getid(y, p ^ 1, (2 * q + (v - v % g) / g) % 3)); fa[Getfa(getid(y, p, q))] = Getfa(getid(x, p ^ 1, (2 * q + (v - v % g) / g) % 3)); } } while (q--) { int x, y, v, succ = 0; scanf("%d%d%d", &x, &y, &v); for (int p = 0; p < 2 && !succ; p++) for (int q = 0; q < 3 && !succ; q++) if (Getfa(getid(y, 0, 0)) == Getfa(getid(x, p, q))) if (chk[p][(v + (3 - q) * g + get<2>(edge[0]) % g) % mod]) { succ = true; puts("YES"); break; } if (!succ) puts("NO"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int Maxn = 50005; int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } vector<tuple<int, int, int>> edge; int fa[6 * Maxn], chk[2][1000005]; int getid(int x, int p, int q) { return 6 * (x - 1) + 3 * p + q; } int Getfa(int x) { return fa[x] = (fa[x] == x ? x : Getfa(fa[x])); } int main() { int n, m, q, mod, g = 0; scanf("%d%d%d%d", &n, &m, &q, &mod); for (int i = 1; i <= 6 * n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { static int lst = -1; int x, y, v; scanf("%d%d%d", &x, &y, &v); edge.emplace_back(x, y, v); if (~lst) g = gcd(g, abs(v - lst)); lst = v; } if (g == 0) g = mod; mod = gcd(mod, 3 * g); for (int i = 0; i < 2 * mod; i++) { static int j = get<2>(edge[0]) % g; chk[i & 1][j] = 1; j = 2 * j % mod; } for (auto &e : edge) { int x, y, v; tie(x, y, v) = e; for (int p = 0; p < 2; p++) for (int q = 0; q < 3; q++) { fa[Getfa(getid(x, p, q))] = Getfa(getid(y, p ^ 1, (2 * q + (v - v % g) / g) % 3)); fa[Getfa(getid(y, p, q))] = Getfa(getid(x, p ^ 1, (2 * q + (v - v % g) / g) % 3)); } } while (q--) { int x, y, v, succ = 0; scanf("%d%d%d", &x, &y, &v); for (int p = 0; p < 2 && !succ; p++) for (int q = 0; q < 3 && !succ; q++) if (Getfa(getid(y, 0, 0)) == Getfa(getid(x, p, q))) if (chk[p][(v + (3 - q) * g + get<2>(edge[0]) % g) % mod]) { succ = true; puts("YES"); break; } if (!succ) puts("NO"); } return 0; }
insert
22
22
22
24
0
p03100
C++
Runtime Error
#include <bits/stdc++.h> #define mp make_pair #define mt make_tuple #define pb push_back #define fi first #define se second #define sz(a) int(a.size()) #define clr(a) memset(a, 0, sizeof(a)) #define all(a) a.begin(), a.end() using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, int> pli; const int inf = 1e9; const ll Inf = 1e18; int mod; template <typename T = int> T gi() { T x = 0, o = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') o = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * o; } template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }; template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }; int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; } int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; } void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); } void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.fi) + "," + to_string(p.se) + ")"; } template <typename T> string to_string(T v) { int fst = 1; string ret = "{"; for (auto x : v) { if (!fst) ret += ","; fst = 0, ret += to_string(x); } ret += "}"; return ret; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << " " << to_string(H); dbg_out(T...); } #define dbg(...) cerr << "{" << #__VA_ARGS__ << "}:", dbg_out(__VA_ARGS__) template <typename T> int qpow(int a, T b) { int ret = 1; while (b) { if (b & 1) ret = 1ll * ret * a % mod; a = 1ll * a * a % mod, b >>= 1; } return ret; } const int N = 1e6 + 10; namespace dsu { int fa[N]; int getf(int x) { return x == fa[x] ? x : fa[x] = getf(fa[x]); } bool merge(int x, int y) { x = getf(x), y = getf(y); if (x == y) return 0; fa[y] = x; return 1; } void init(int n) { for (int i = 0; i < n; i++) fa[i] = i; } bool connected(int x, int y) { return getf(x) == getf(y); } } // namespace dsu int n, m, q, g = 0, z, a[N], b[N], c[N]; bool ispw[2][N]; int id(int u, int p, int q) { return 6 * u + 2 * q + p; } int main() { n = gi(), m = gi(), q = gi(), mod = gi(); for (int i = 1; i <= m; i++) { a[i] = gi() - 1, b[i] = gi() - 1, c[i] = gi(); g = __gcd(g, c[i] - c[1]); } if (!g) g = mod; mod = __gcd(mod, 3 * g); z = c[1] % g; dsu::init(6 * n); for (int i = 1; i <= m; i++) { int w = (c[i] - z) / g % 3; for (int p = 0; p < 2; p++) for (int q = 0; q < 3; q++) { dsu::merge(id(a[i], p, q), id(b[i], p ^ 1, (2 * q + w) % 3)); dsu::merge(id(b[i], p, q), id(a[i], p ^ 1, (2 * q + w) % 3)); } } for (int i = 0, j = z % mod; !ispw[i & 1][j]; i++, j = 2ll * j % mod) ispw[i & 1][j] = 1; while (q--) { int s = gi() - 1, t = gi() - 1, r = gi(); bool ans = 0; for (int p = 0; p < 2; p++) for (int q = 0; q < 3; q++) { if (dsu::connected(id(t, 0, 0), id(s, p, q))) { if (ispw[p & 1][(r + z - q * g + 3 * g) % mod]) ans = 1; } } puts(ans ? "YES" : "NO"); } return 0; }
#include <bits/stdc++.h> #define mp make_pair #define mt make_tuple #define pb push_back #define fi first #define se second #define sz(a) int(a.size()) #define clr(a) memset(a, 0, sizeof(a)) #define all(a) a.begin(), a.end() using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, int> pli; const int inf = 1e9; const ll Inf = 1e18; int mod; template <typename T = int> T gi() { T x = 0, o = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') o = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * o; } template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }; template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }; int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; } int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; } void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); } void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.fi) + "," + to_string(p.se) + ")"; } template <typename T> string to_string(T v) { int fst = 1; string ret = "{"; for (auto x : v) { if (!fst) ret += ","; fst = 0, ret += to_string(x); } ret += "}"; return ret; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << " " << to_string(H); dbg_out(T...); } #define dbg(...) cerr << "{" << #__VA_ARGS__ << "}:", dbg_out(__VA_ARGS__) template <typename T> int qpow(int a, T b) { int ret = 1; while (b) { if (b & 1) ret = 1ll * ret * a % mod; a = 1ll * a * a % mod, b >>= 1; } return ret; } const int N = 1e6 + 10; namespace dsu { int fa[N]; int getf(int x) { return x == fa[x] ? x : fa[x] = getf(fa[x]); } bool merge(int x, int y) { x = getf(x), y = getf(y); if (x == y) return 0; fa[y] = x; return 1; } void init(int n) { for (int i = 0; i < n; i++) fa[i] = i; } bool connected(int x, int y) { return getf(x) == getf(y); } } // namespace dsu int n, m, q, g = 0, z, a[N], b[N], c[N]; bool ispw[2][N]; int id(int u, int p, int q) { return 6 * u + 2 * q + p; } int main() { n = gi(), m = gi(), q = gi(), mod = gi(); for (int i = 1; i <= m; i++) { a[i] = gi() - 1, b[i] = gi() - 1, c[i] = gi(); g = __gcd(g, abs(c[i] - c[1])); } if (!g) g = mod; mod = __gcd(mod, 3 * g); z = c[1] % g; dsu::init(6 * n); for (int i = 1; i <= m; i++) { int w = (c[i] - z) / g % 3; for (int p = 0; p < 2; p++) for (int q = 0; q < 3; q++) { dsu::merge(id(a[i], p, q), id(b[i], p ^ 1, (2 * q + w) % 3)); dsu::merge(id(b[i], p, q), id(a[i], p ^ 1, (2 * q + w) % 3)); } } for (int i = 0, j = z % mod; !ispw[i & 1][j]; i++, j = 2ll * j % mod) ispw[i & 1][j] = 1; while (q--) { int s = gi() - 1, t = gi() - 1, r = gi(); bool ans = 0; for (int p = 0; p < 2; p++) for (int q = 0; q < 3; q++) { if (dsu::connected(id(t, 0, 0), id(s, p, q))) { if (ispw[p & 1][(r + z - q * g + 3 * g) % mod]) ans = 1; } } puts(ans ? "YES" : "NO"); } return 0; }
replace
103
104
103
104
0
p03100
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; const int Q = 1 << 19; int fa[Q]; int gf(int x) { return fa[x] == x ? x : (fa[x] = gf(fa[x])); } int X[Q], Y[Q], Z[Q]; int MOD; int id(int x, int y, int z) { return (x - 1) * 6 + y * 2 + z; } void Merge(int x, int y) { x = gf(x), y = gf(y); fa[x] = y; } int had[2][1 << 21]; void Deal(int x, int y, int z) { for (int q = 0; q < 3; q++) for (int p = 0; p < 2; p++) Merge(id(x, q, p), id(y, ((q << 1) + z) % 3, p ^ 1)); } int main() { int n, m, q, gg = -1; scanf("%d%d%d%d", &n, &m, &q, &MOD); for (int i = 1; i <= (n + 1) * 6; i++) fa[i] = i; for (int i = 1; i <= m; i++) { scanf("%d%d%d", &X[i], &Y[i], &Z[i]); if (i > 1) { int v = abs(Z[i] - Z[1]); gg = gg < 0 ? v : __gcd(gg, v); } } // gg=max(gg,1); MOD = __gcd(MOD, 3 * gg); int zz = Z[1] % gg; for (int i = 1; i <= m; i++) { int x = X[i], y = Y[i], z = (Z[i] - zz) / gg % 3; Deal(x, y, z), Deal(y, x, z); } for (int i = 0, j = zz % MOD; i < (MOD << 1); i++, j = (j << 1) % MOD) had[i & 1][j] = 1; while (q--) { int s, t, r, als = 0; scanf("%d%d%d", &s, &t, &r); for (int q = 0; q < 3; q++) for (int p = 0; p < 2; p++) if (gf(id(t, 0, 0)) == gf(id(s, q, p)) && had[p][(r + zz + (3 - q) * gg) % MOD]) { als = 1; break; } puts(als ? "YES" : "NO"); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; const int Q = 1 << 19; int fa[Q]; int gf(int x) { return fa[x] == x ? x : (fa[x] = gf(fa[x])); } int X[Q], Y[Q], Z[Q]; int MOD; int id(int x, int y, int z) { return (x - 1) * 6 + y * 2 + z; } void Merge(int x, int y) { x = gf(x), y = gf(y); fa[x] = y; } int had[2][1 << 21]; void Deal(int x, int y, int z) { for (int q = 0; q < 3; q++) for (int p = 0; p < 2; p++) Merge(id(x, q, p), id(y, ((q << 1) + z) % 3, p ^ 1)); } int main() { int n, m, q, gg = -1; scanf("%d%d%d%d", &n, &m, &q, &MOD); for (int i = 1; i <= (n + 1) * 6; i++) fa[i] = i; for (int i = 1; i <= m; i++) { scanf("%d%d%d", &X[i], &Y[i], &Z[i]); if (i > 1) { int v = abs(Z[i] - Z[1]); gg = gg < 0 ? v : __gcd(gg, v); } } if (!gg) gg = MOD; MOD = __gcd(MOD, 3 * gg); int zz = Z[1] % gg; for (int i = 1; i <= m; i++) { int x = X[i], y = Y[i], z = (Z[i] - zz) / gg % 3; Deal(x, y, z), Deal(y, x, z); } for (int i = 0, j = zz % MOD; i < (MOD << 1); i++, j = (j << 1) % MOD) had[i & 1][j] = 1; while (q--) { int s, t, r, als = 0; scanf("%d%d%d", &s, &t, &r); for (int q = 0; q < 3; q++) for (int p = 0; p < 2; p++) if (gf(id(t, 0, 0)) == gf(id(s, q, p)) && had[p][(r + zz + (3 - q) * gg) % MOD]) { als = 1; break; } puts(als ? "YES" : "NO"); } return 0; }
replace
33
34
33
35
0
p03100
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #define N (500010) #define M () #define inf (0x7f7f7f7f) #define rg register int #define Label puts("NAIVE") #define spa print(' ') #define ent print('\n') #define rand() (((rand()) << (15)) ^ (rand())) #define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout) typedef long double ld; typedef long long LL; typedef unsigned long long ull; using namespace std; namespace fastIO1 { inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; return (s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin), (s == t ? -1 : *s++) : *s++); } template <class T> inline void read(T &x) { static bool iosig; static char c; for (iosig = false, c = read(); !isdigit(c); c = read()) { if (c == '-') iosig = true; if (c == -1) return; } for (x = 0; isdigit(c); c = read()) x = ((x + (x << 2)) << 1) + (c ^ '0'); if (iosig) x = -x; } inline char readc(char &c) { for (c = read(); !isalpha(c) && !isdigit(c); c = read()) if (c == -1) return 0; } const int OUT_LEN = 10000000; char obuf[OUT_LEN], *ooh = obuf; inline void print(char c) { if (ooh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf; *ooh++ = c; } template <class T> inline void print(T x) { static int buf[30], cnt; if (x == 0) print('0'); else { if (x < 0) print('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) print((char)buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); } } // namespace fastIO1 namespace fastIO2 { template <class T> inline void read(T &x) { static bool iosig; static char c; for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) { if (c == '-') iosig = true; if (c == -1) return; } for (x = 0; isdigit(c); c = getchar()) x = ((x + (x << 2)) << 1) + (c ^ '0'); if (iosig) x = -x; } } // namespace fastIO2 using namespace fastIO1; struct edge { int x, y, c; } e[N]; int n, m, g, P, Q, fa[N], pos[N][2][3], res, tot; bool can[1000010][2]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int ask(int x) { return fa[x] == x ? x : fa[x] = ask(fa[x]); } void unite(int x, int y) { x = ask(x), y = ask(y); if (x != y) fa[x] = y; } int main() { freopen("01.txt", "r", stdin); freopen("my.out", "w", stdout); read(n), read(m), read(Q), read(P), g = P; for (int i = 1; i <= m; i++) read(e[i].x), read(e[i].y), read(e[i].c), g = gcd(g, (int)abs(e[i].c - e[1].c)); // cout<<g<<endl; P = gcd(P, 3 * g), res = e[1].c % g; for (int i = 1; i <= n * 6; i++) fa[i] = i; for (int i = 0, x = res; i <= P; i++, x = (x + x) % P) can[x][i & 1] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= 1; j++) for (int k = 0; k <= 2; k++) pos[i][j][k] = ++tot; for (int i = 1; i <= m; i++) { int w = (e[i].c - res) % P / g, x = e[i].x, y = e[i].y; for (int j = 0; j <= 1; j++) for (int k = 0; k <= 2; k++) unite(pos[x][j][k], pos[y][j ^ 1][(2 * k + w) % 3]); } while (Q--) { int x, y, z; bool fl = 0; read(x), read(y), read(z); for (int i = 0; i <= 1; i++) for (int j = 0; j <= 2; j++) fl |= (ask(pos[y][0][0]) == ask(pos[x][i][j])) && can[(z + res + (3 - j) * g) % P][i]; puts(fl ? "YES" : "NO"); } }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #define N (500010) #define M () #define inf (0x7f7f7f7f) #define rg register int #define Label puts("NAIVE") #define spa print(' ') #define ent print('\n') #define rand() (((rand()) << (15)) ^ (rand())) #define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout) typedef long double ld; typedef long long LL; typedef unsigned long long ull; using namespace std; namespace fastIO1 { inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; return (s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin), (s == t ? -1 : *s++) : *s++); } template <class T> inline void read(T &x) { static bool iosig; static char c; for (iosig = false, c = read(); !isdigit(c); c = read()) { if (c == '-') iosig = true; if (c == -1) return; } for (x = 0; isdigit(c); c = read()) x = ((x + (x << 2)) << 1) + (c ^ '0'); if (iosig) x = -x; } inline char readc(char &c) { for (c = read(); !isalpha(c) && !isdigit(c); c = read()) if (c == -1) return 0; } const int OUT_LEN = 10000000; char obuf[OUT_LEN], *ooh = obuf; inline void print(char c) { if (ooh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf; *ooh++ = c; } template <class T> inline void print(T x) { static int buf[30], cnt; if (x == 0) print('0'); else { if (x < 0) print('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) print((char)buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); } } // namespace fastIO1 namespace fastIO2 { template <class T> inline void read(T &x) { static bool iosig; static char c; for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) { if (c == '-') iosig = true; if (c == -1) return; } for (x = 0; isdigit(c); c = getchar()) x = ((x + (x << 2)) << 1) + (c ^ '0'); if (iosig) x = -x; } } // namespace fastIO2 using namespace fastIO1; struct edge { int x, y, c; } e[N]; int n, m, g, P, Q, fa[N], pos[N][2][3], res, tot; bool can[1000010][2]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int ask(int x) { return fa[x] == x ? x : fa[x] = ask(fa[x]); } void unite(int x, int y) { x = ask(x), y = ask(y); if (x != y) fa[x] = y; } int main() { // freopen("01.txt","r",stdin); // freopen("my.out","w",stdout); read(n), read(m), read(Q), read(P), g = P; for (int i = 1; i <= m; i++) read(e[i].x), read(e[i].y), read(e[i].c), g = gcd(g, (int)abs(e[i].c - e[1].c)); // cout<<g<<endl; P = gcd(P, 3 * g), res = e[1].c % g; for (int i = 1; i <= n * 6; i++) fa[i] = i; for (int i = 0, x = res; i <= P; i++, x = (x + x) % P) can[x][i & 1] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= 1; j++) for (int k = 0; k <= 2; k++) pos[i][j][k] = ++tot; for (int i = 1; i <= m; i++) { int w = (e[i].c - res) % P / g, x = e[i].x, y = e[i].y; for (int j = 0; j <= 1; j++) for (int k = 0; k <= 2; k++) unite(pos[x][j][k], pos[y][j ^ 1][(2 * k + w) % 3]); } while (Q--) { int x, y, z; bool fl = 0; read(x), read(y), read(z); for (int i = 0; i <= 1; i++) for (int j = 0; j <= 2; j++) fl |= (ask(pos[y][0][0]) == ask(pos[x][i][j])) && can[(z + res + (3 - j) * g) % P][i]; puts(fl ? "YES" : "NO"); } }
replace
101
103
101
103
-8
p03100
C++
Runtime Error
#include <stdio.h> int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } int a[65536], b[65536], c[65536]; int fa1[1048576], fa2[1048576]; int findroot1(int x) { if (fa1[x] == x) { return x; } return fa1[x] = findroot1(fa1[x]); } int findroot2(int x) { if (fa2[x] == x) { return x; } return fa2[x] = findroot2(fa2[x]); } int main() { int n, m, q, mod; scanf("%d%d%d%d", &n, &m, &q, &mod); int g = mod; for (int i = 0; i < m; i++) { scanf("%d%d%d", &a[i], &b[i], &c[i]); if (i) { g = gcd(g, c[i] - c[i - 1]); } } mod = gcd(mod, g * 3); for (int i = 0; i < m; i++) { c[i] %= mod; } for (int i = 0; i < mod; i++) { fa1[i] = i; } for (int i = 0; i < mod; i++) { int x = findroot1(i); int y = findroot1(i * 4 % mod); fa1[x] = y; } int dd = mod / g; for (int i = 1; i <= n * dd * 2; i++) { fa2[i] = i; } int st = c[0] % g; for (int i = 0; i < m; i++) { c[i] -= st; c[i] /= g; for (int j = 0; j < 2; j++) { for (int k = 0; k < dd; k++) { int x = findroot2(a[i] + j * n + k * n * 2); int y = findroot2(b[i] + (j ^ 1) * n + ((k + k + c[i]) % dd) * n * 2); fa2[x] = y; x = findroot2(b[i] + j * n + k * n * 2); y = findroot2(a[i] + (j ^ 1) * n + ((k + k + c[i]) % dd) * n * 2); fa2[x] = y; } } } while (q--) { int s, t, r; scanf("%d%d%d", &s, &t, &r); r = (r + st) % mod; bool ok = false; for (int i = 0; i < 2; i++) { for (int j = 0; j < dd; j++) { if (findroot2(s + i * n + j * n * 2) == findroot2(t)) { if (findroot1(((st << i) + j * g) % mod) == findroot1(r)) { ok = true; } } } } if (ok) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }
#include <stdio.h> int gcd(int a, int b) { if (a < 0) { a = -a; } if (b < 0) { b = -b; } if (b == 0) { return a; } return gcd(b, a % b); } int a[65536], b[65536], c[65536]; int fa1[1048576], fa2[1048576]; int findroot1(int x) { if (fa1[x] == x) { return x; } return fa1[x] = findroot1(fa1[x]); } int findroot2(int x) { if (fa2[x] == x) { return x; } return fa2[x] = findroot2(fa2[x]); } int main() { int n, m, q, mod; scanf("%d%d%d%d", &n, &m, &q, &mod); int g = mod; for (int i = 0; i < m; i++) { scanf("%d%d%d", &a[i], &b[i], &c[i]); if (i) { g = gcd(g, c[i] - c[i - 1]); } } mod = gcd(mod, g * 3); for (int i = 0; i < m; i++) { c[i] %= mod; } for (int i = 0; i < mod; i++) { fa1[i] = i; } for (int i = 0; i < mod; i++) { int x = findroot1(i); int y = findroot1(i * 4 % mod); fa1[x] = y; } int dd = mod / g; for (int i = 1; i <= n * dd * 2; i++) { fa2[i] = i; } int st = c[0] % g; for (int i = 0; i < m; i++) { c[i] -= st; c[i] /= g; for (int j = 0; j < 2; j++) { for (int k = 0; k < dd; k++) { int x = findroot2(a[i] + j * n + k * n * 2); int y = findroot2(b[i] + (j ^ 1) * n + ((k + k + c[i]) % dd) * n * 2); fa2[x] = y; x = findroot2(b[i] + j * n + k * n * 2); y = findroot2(a[i] + (j ^ 1) * n + ((k + k + c[i]) % dd) * n * 2); fa2[x] = y; } } } while (q--) { int s, t, r; scanf("%d%d%d", &s, &t, &r); r = (r + st) % mod; bool ok = false; for (int i = 0; i < 2; i++) { for (int j = 0; j < dd; j++) { if (findroot2(s + i * n + j * n * 2) == findroot2(t)) { if (findroot1(((st << i) + j * g) % mod) == findroot1(r)) { ok = true; } } } } if (ok) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }
insert
2
2
2
8
0
p03100
C++
Runtime Error
#include <algorithm> #include <cstdio> using namespace std; int gi() { int x = 0, w = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') w = 0, ch = getchar(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return w ? x : -x; } const int N = 1e6 + 5; int n, m, q, mod, a[N], b[N], c[N], g, z, fa[N], chk[2][N]; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } void link(int x, int y) { fa[find(x)] = find(y); } int id(int u, int x, int y) { return u * 6 + x * 2 + y; } int main() { n = gi(); m = gi(); q = gi(); mod = gi(); for (int i = 1; i <= m; ++i) { a[i] = gi(), b[i] = gi(), c[i] = gi(); g = __gcd(g, c[i] - c[1]); } if (!g) g = mod; mod = __gcd(mod, 3 * g); z = c[1] % g; for (int i = 0; i < n * 6; ++i) fa[i] = i; for (int i = 1; i <= m; ++i) { int u = a[i] - 1, v = b[i] - 1, w = (c[i] - z) / g % 3; for (int x = 0; x < 3; ++x) { link(id(u, x, 0), id(v, (2 * x + w) % 3, 1)); link(id(u, x, 1), id(v, (2 * x + w) % 3, 0)); link(id(v, x, 0), id(u, (2 * x + w) % 3, 1)); link(id(v, x, 1), id(u, (2 * x + w) % 3, 0)); } } for (int i = 0, j = z; i < mod << 1; ++i, j = (j << 1) % mod) chk[i & 1][j] = 1; while (q--) { int s = gi() - 1, t = gi() - 1, r = gi(), res = 0; for (int x = 0; x < 3; ++x) for (int y = 0; y < 2; ++y) if (find(id(t, 0, 0)) == find(id(s, x, y))) res |= chk[y][(r + z + (3 - x) * g) % mod]; puts(res ? "YES" : "NO"); } return 0; }
#include <algorithm> #include <cstdio> using namespace std; int gi() { int x = 0, w = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') w = 0, ch = getchar(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return w ? x : -x; } const int N = 1e6 + 5; int n, m, q, mod, a[N], b[N], c[N], g, z, fa[N], chk[2][N]; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } void link(int x, int y) { fa[find(x)] = find(y); } int id(int u, int x, int y) { return u * 6 + x * 2 + y; } int main() { n = gi(); m = gi(); q = gi(); mod = gi(); for (int i = 1; i <= m; ++i) { a[i] = gi(), b[i] = gi(), c[i] = gi(); g = __gcd(g, abs(c[i] - c[1])); } if (!g) g = mod; mod = __gcd(mod, 3 * g); z = c[1] % g; for (int i = 0; i < n * 6; ++i) fa[i] = i; for (int i = 1; i <= m; ++i) { int u = a[i] - 1, v = b[i] - 1, w = (c[i] - z) / g % 3; for (int x = 0; x < 3; ++x) { link(id(u, x, 0), id(v, (2 * x + w) % 3, 1)); link(id(u, x, 1), id(v, (2 * x + w) % 3, 0)); link(id(v, x, 0), id(u, (2 * x + w) % 3, 1)); link(id(v, x, 1), id(u, (2 * x + w) % 3, 0)); } } for (int i = 0, j = z; i < mod << 1; ++i, j = (j << 1) % mod) chk[i & 1][j] = 1; while (q--) { int s = gi() - 1, t = gi() - 1, r = gi(), res = 0; for (int x = 0; x < 3; ++x) for (int y = 0; y < 2; ++y) if (find(id(t, 0, 0)) == find(id(s, x, y))) res |= chk[y][(r + z + (3 - x) * g) % mod]; puts(res ? "YES" : "NO"); } return 0; }
replace
26
27
26
27
0
p03100
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define M 50010 using namespace std; template <typename T> void read(T &x) { x = 0; char c = getchar(); int fh = 1; while (!isdigit(c)) { if (c == '-') fh = -1; c = getchar(); } while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= fh; } struct edge { int x, y, z; } a[M]; int m, n, q, mod, g, bi[1000010], num, f[M * 6], z, x, y, r; int gcd(int x, int y) { if (y == 0) return x; if (x % y == 0) return y; return gcd(y, x % y); } int qf(int x) { if (f[x] != x) f[x] = qf(f[x]); return f[x]; } void lian(int x, int y) { int nx = qf(x), ny = qf(y); if (nx != ny) f[nx] = ny; } int que(int x, int y, int z) { return (y * 3 + z) * n + x; } int main() { read(n); read(m); read(q); read(mod); for (int i = 1; i <= m; i++) { read(a[i].x); read(a[i].y); read(a[i].z); g = gcd(g, a[i].z - a[1].z); } if (!g) g = mod; mod = gcd(g * 3, mod); z = a[1].z % g; for (int i = 0; i < mod; i++) bi[i] = 0; for (int i = 0; i < mod; i++) if (!bi[i]) { num++; for (int j = i; !bi[j]; j = j * 4 % mod) bi[j] = num; } for (int i = 1; i <= m; i++) a[i].z -= z; for (int i = 1; i <= n * 6; i++) f[i] = i; for (int i = 1; i <= m; i++) for (int j = 0; j <= 1; j++) for (int p = 0; p <= 2; p++) { lian(que(a[i].x, j, p), que(a[i].y, j ^ 1, (p * 2 + a[i].z / g) % 3)); lian(que(a[i].y, j, p), que(a[i].x, j ^ 1, (p * 2 + a[i].z / g) % 3)); } for (int i = 1; i <= q; i++) { read(x); read(y); read(r); bool vi = 0; for (int j = 0; j <= 1; j++) for (int k = 0; k <= 2; k++) if (qf(que(y, 1, 0)) == qf(que(x, j, k)) && bi[(r + z) % mod] == bi[((2 - j) * z + k * g) % mod]) vi = 1; if (vi) puts("YES"); else puts("NO"); } return 0; }
#include <bits/stdc++.h> #define ll long long #define M 50010 using namespace std; template <typename T> void read(T &x) { x = 0; char c = getchar(); int fh = 1; while (!isdigit(c)) { if (c == '-') fh = -1; c = getchar(); } while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= fh; } struct edge { int x, y, z; } a[M]; int m, n, q, mod, g, bi[1000010], num, f[M * 6], z, x, y, r; int gcd(int x, int y) { if (y == 0) return x; if (x % y == 0) return y; return gcd(y, x % y); } int qf(int x) { if (f[x] != x) f[x] = qf(f[x]); return f[x]; } void lian(int x, int y) { int nx = qf(x), ny = qf(y); if (nx != ny) f[nx] = ny; } int que(int x, int y, int z) { return (y * 3 + z) * n + x; } int main() { read(n); read(m); read(q); read(mod); for (int i = 1; i <= m; i++) { read(a[i].x); read(a[i].y); read(a[i].z); g = gcd(g, abs(a[i].z - a[1].z)); } if (!g) g = mod; mod = gcd(g * 3, mod); z = a[1].z % g; for (int i = 0; i < mod; i++) bi[i] = 0; for (int i = 0; i < mod; i++) if (!bi[i]) { num++; for (int j = i; !bi[j]; j = j * 4 % mod) bi[j] = num; } for (int i = 1; i <= m; i++) a[i].z -= z; for (int i = 1; i <= n * 6; i++) f[i] = i; for (int i = 1; i <= m; i++) for (int j = 0; j <= 1; j++) for (int p = 0; p <= 2; p++) { lian(que(a[i].x, j, p), que(a[i].y, j ^ 1, (p * 2 + a[i].z / g) % 3)); lian(que(a[i].y, j, p), que(a[i].x, j ^ 1, (p * 2 + a[i].z / g) % 3)); } for (int i = 1; i <= q; i++) { read(x); read(y); read(r); bool vi = 0; for (int j = 0; j <= 1; j++) for (int k = 0; k <= 2; k++) if (qf(que(y, 1, 0)) == qf(que(x, j, k)) && bi[(r + z) % mod] == bi[((2 - j) * z + k * g) % mod]) vi = 1; if (vi) puts("YES"); else puts("NO"); } return 0; }
replace
48
49
48
49
0
p03100
C++
Runtime Error
#include <bits/stdc++.h> #pragma GCC optimize("O2,unroll-loops") // #pragma GCC optimize("no-stack-protector,fast-math") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef pair<ll, ll> pll; #define debug(x) cerr << #x << '=' << (x) << endl; #define debugp(x) \ cerr << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl; #define debug2(x, y) \ cerr << "{" << #x << ", " << #y << "} = {" << (x) << ", " << (y) << "}" \ << endl; #define debugv(v) \ { \ cerr << #v << " : "; \ for (auto x : v) \ cerr << x << ' '; \ cerr << endl; \ } #define all(x) x.begin(), x.end() #define pb push_back #define kill(x) return cout << x << '\n', 0; const int MAXN = 300010; int n, m, q, u, v, x, y, t, r, mod; int par[MAXN]; int A[MAXN], B[MAXN], C[MAXN], G, Z; bool good[1000000]; int getpar(int x) { return (par[x] == x ? x : par[x] = getpar(par[x])); } inline void join(int x, int y) { par[getpar(x)] = getpar(y); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); iota(par, par + MAXN, 0); cin >> n >> m >> q >> mod; for (int i = 1; i <= m; i++) cin >> A[i] >> B[i] >> C[i], G = __gcd(G, C[i] - C[1]); G = __gcd(G, mod); mod = __gcd(mod, 3 * G); Z = C[1] % G; for (int i = 1; i <= m; i++) { int u = A[i], v = B[i], c = C[i]; for (int s : {0, 1, 2}) for (int t : {0, 1}) { join(u * 6 + 2 * s + t, v * 6 + 2 * ((2 * s + c / G) % 3) + (t ^ 1)); join(v * 6 + 2 * s + t, u * 6 + 2 * ((2 * s + c / G) % 3) + (t ^ 1)); } } int val = Z; while (!good[val]) { good[val] = 1; val = val * 4 % mod; } while (q--) { cin >> u >> v >> r; bool ans = 0; for (int s : {0, 1, 2}) for (int t : {0, 1}) if (getpar(6 * v) == getpar(6 * u + 2 * s + t)) { int R = (r + Z - s * G) % mod; if (R < 0) R += mod; if (t) R = R * (mod + 1ll) / 2 % mod; ans |= good[R]; } cout << (ans ? "YES\n" : "NO\n"); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O2,unroll-loops") // #pragma GCC optimize("no-stack-protector,fast-math") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef pair<ll, ll> pll; #define debug(x) cerr << #x << '=' << (x) << endl; #define debugp(x) \ cerr << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl; #define debug2(x, y) \ cerr << "{" << #x << ", " << #y << "} = {" << (x) << ", " << (y) << "}" \ << endl; #define debugv(v) \ { \ cerr << #v << " : "; \ for (auto x : v) \ cerr << x << ' '; \ cerr << endl; \ } #define all(x) x.begin(), x.end() #define pb push_back #define kill(x) return cout << x << '\n', 0; const int MAXN = 300010; int n, m, q, u, v, x, y, t, r, mod; int par[MAXN]; int A[MAXN], B[MAXN], C[MAXN], G, Z; bool good[1000000]; int getpar(int x) { return (par[x] == x ? x : par[x] = getpar(par[x])); } inline void join(int x, int y) { par[getpar(x)] = getpar(y); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); iota(par, par + MAXN, 0); cin >> n >> m >> q >> mod; for (int i = 1; i <= m; i++) cin >> A[i] >> B[i] >> C[i], G = __gcd(G, C[i] - C[1]); G = __gcd(G, mod); if (G < 0) G *= -1; mod = __gcd(mod, 3 * G); Z = C[1] % G; for (int i = 1; i <= m; i++) { int u = A[i], v = B[i], c = C[i]; for (int s : {0, 1, 2}) for (int t : {0, 1}) { join(u * 6 + 2 * s + t, v * 6 + 2 * ((2 * s + c / G) % 3) + (t ^ 1)); join(v * 6 + 2 * s + t, u * 6 + 2 * ((2 * s + c / G) % 3) + (t ^ 1)); } } int val = Z; while (!good[val]) { good[val] = 1; val = val * 4 % mod; } while (q--) { cin >> u >> v >> r; bool ans = 0; for (int s : {0, 1, 2}) for (int t : {0, 1}) if (getpar(6 * v) == getpar(6 * u + 2 * s + t)) { int R = (r + Z - s * G) % mod; if (R < 0) R += mod; if (t) R = R * (mod + 1ll) / 2 % mod; ans |= good[R]; } cout << (ans ? "YES\n" : "NO\n"); } return 0; }
insert
49
49
49
51
0
p03100
C++
Runtime Error
#include <algorithm> #include <cstdio> using namespace std; int n, m, Q, Mod, T, UF[301000], Z[50100]; struct point { int a, b, c; } w[101000]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int Find(int a) { if (a == UF[a]) return a; return UF[a] = Find(UF[a]); } void Merge(int a, int b) { a = Find(a), b = Find(b); if (a != b) UF[a] = b; } int main() { int i, a, b, c, j, k; scanf("%d%d%d%d", &n, &m, &Q, &Mod); T = Mod; for (i = 0; i < m; i++) { scanf("%d%d%d", &w[i].a, &w[i].b, &w[i].c); w[i].a--, w[i].b--; w[m + i] = w[i]; swap(w[m + i].a, w[m + i].b); if (i) T = gcd(T, abs(w[i].c - w[i - 1].c)); } m *= 2; Mod = gcd(Mod, 3 * T); int A = Mod / T; int rr = w[0].c % T; for (i = 0; i < m; i++) { w[i].c -= rr; } for (i = 0; i < (A * 2) * n; i++) UF[i] = i; for (i = 0; i < m; i++) { a = w[i].a, b = w[i].b, c = (w[i].c % Mod) / T; for (j = 0; j < A; j++) { Merge(a * (A * 2) + j, b * (A * 2) + A + (2 * j + c) % A); Merge(a * (A * 2) + A + j, b * (A * 2) + (2 * j + c) % A); } } int cnt = 0; for (i = 0; i < Mod; i++) { if (Z[i]) continue; cnt++; int x = i; while (!Z[x]) { Z[x] = cnt; x = x * 4 % Mod; } } while (Q--) { scanf("%d%d%d", &a, &b, &c); a--, b--; for (j = 0; j < (A * 2); j++) { if (Find(b * (A * 2)) == Find(a * (A * 2) + j)) { int t = (rr * (j / A + 1) + (j % A) * T) % Mod; if (Z[t] == Z[(c + rr) % Mod]) { puts("YES"); break; } } } if (j == (A * 2)) puts("NO"); } }
#include <algorithm> #include <cstdio> using namespace std; int n, m, Q, Mod, T, UF[6001000], Z[1000100]; struct point { int a, b, c; } w[101000]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int Find(int a) { if (a == UF[a]) return a; return UF[a] = Find(UF[a]); } void Merge(int a, int b) { a = Find(a), b = Find(b); if (a != b) UF[a] = b; } int main() { int i, a, b, c, j, k; scanf("%d%d%d%d", &n, &m, &Q, &Mod); T = Mod; for (i = 0; i < m; i++) { scanf("%d%d%d", &w[i].a, &w[i].b, &w[i].c); w[i].a--, w[i].b--; w[m + i] = w[i]; swap(w[m + i].a, w[m + i].b); if (i) T = gcd(T, abs(w[i].c - w[i - 1].c)); } m *= 2; Mod = gcd(Mod, 3 * T); int A = Mod / T; int rr = w[0].c % T; for (i = 0; i < m; i++) { w[i].c -= rr; } for (i = 0; i < (A * 2) * n; i++) UF[i] = i; for (i = 0; i < m; i++) { a = w[i].a, b = w[i].b, c = (w[i].c % Mod) / T; for (j = 0; j < A; j++) { Merge(a * (A * 2) + j, b * (A * 2) + A + (2 * j + c) % A); Merge(a * (A * 2) + A + j, b * (A * 2) + (2 * j + c) % A); } } int cnt = 0; for (i = 0; i < Mod; i++) { if (Z[i]) continue; cnt++; int x = i; while (!Z[x]) { Z[x] = cnt; x = x * 4 % Mod; } } while (Q--) { scanf("%d%d%d", &a, &b, &c); a--, b--; for (j = 0; j < (A * 2); j++) { if (Find(b * (A * 2)) == Find(a * (A * 2) + j)) { int t = (rr * (j / A + 1) + (j % A) * T) % Mod; if (Z[t] == Z[(c + rr) % Mod]) { puts("YES"); break; } } } if (j == (A * 2)) puts("NO"); } }
replace
3
4
3
4
0
p03100
C++
Runtime Error
#include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define SZ(x) ((int)x.size()) #define L(i, u) for (register int i = head[u]; i; i = nxt[i]) #define rep(i, a, b) for (register int i = (a); i <= (b); i++) #define per(i, a, b) for (register int i = (a); i >= (b); i--) using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> Pii; typedef vector<int> Vi; template <class T> inline void read(T &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } x *= f; } template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; } template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; } inline ui R() { static ui seed = 416; return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; } const int N = 300500; int n, m, q, mo, fa[N], A[N], B[N], C[N], g, z; bool ok[2][1020000]; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } void merge(int x, int y) { x = find(x); y = find(y); fa[x] = y; } int getid(int x, int p, int q) { // 01 012 return 6 * (x - 1) + p * 3 + q + 1; } int main() { read(n); read(m); read(q); read(mo); rep(i, 1, m) { read(A[i]); read(B[i]); read(C[i]); g = gcd(g, C[i] - C[1]); } g = gcd(g, mo); mo = gcd(3 * g, mo); z = C[1] % g; rep(i, 1, n * 6) fa[i] = i; for (int x = z % mo, cnt = 0; cnt <= mo; cnt++) { ok[0][x] = 1; x = 2LL * x % mo; ok[1][x] = 1; x = 2LL * x % mo; } rep(i, 1, m) { int u = A[i], v = B[i], w = (C[i] - z) / g; rep(p, 0, 1) rep(q, 0, 2) merge(getid(u, p, q), getid(v, p ^ 1, (2 * q + w) % 3)); swap(u, v); rep(p, 0, 1) rep(q, 0, 2) merge(getid(u, p, q), getid(v, p ^ 1, (2 * q + w) % 3)); } while (q--) { int s, t, r; read(s); read(t); read(r); bool res = 0; rep(p, 0, 1) rep(q, 0, 2) if (find(getid(t, 0, 0)) == find(getid(s, p, q))) { res |= ok[p][((r + z - q * g) % mo + mo) % mo]; // if(ok[p][((r+z-q*g)%mo+mo)%mo])cerr<<p<<' //'<<((r+z-q*g)%mo+mo)%mo<<endl; } printf("%s\n", res ? "YES" : "NO"); } return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define SZ(x) ((int)x.size()) #define L(i, u) for (register int i = head[u]; i; i = nxt[i]) #define rep(i, a, b) for (register int i = (a); i <= (b); i++) #define per(i, a, b) for (register int i = (a); i >= (b); i--) using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> Pii; typedef vector<int> Vi; template <class T> inline void read(T &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } x *= f; } template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; } template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; } inline ui R() { static ui seed = 416; return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; } const int N = 300500; int n, m, q, mo, fa[N], A[N], B[N], C[N], g, z; bool ok[2][1020000]; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } void merge(int x, int y) { x = find(x); y = find(y); fa[x] = y; } int getid(int x, int p, int q) { // 01 012 return 6 * (x - 1) + p * 3 + q + 1; } int main() { read(n); read(m); read(q); read(mo); rep(i, 1, m) { read(A[i]); read(B[i]); read(C[i]); g = gcd(g, C[i] - C[1] > 0 ? C[i] - C[1] : -(C[i] - C[1])); } g = gcd(g, mo); mo = gcd(3 * g, mo); z = C[1] % g; rep(i, 1, n * 6) fa[i] = i; for (int x = z % mo, cnt = 0; cnt <= mo; cnt++) { ok[0][x] = 1; x = 2LL * x % mo; ok[1][x] = 1; x = 2LL * x % mo; } rep(i, 1, m) { int u = A[i], v = B[i], w = (C[i] - z) / g; rep(p, 0, 1) rep(q, 0, 2) merge(getid(u, p, q), getid(v, p ^ 1, (2 * q + w) % 3)); swap(u, v); rep(p, 0, 1) rep(q, 0, 2) merge(getid(u, p, q), getid(v, p ^ 1, (2 * q + w) % 3)); } while (q--) { int s, t, r; read(s); read(t); read(r); bool res = 0; rep(p, 0, 1) rep(q, 0, 2) if (find(getid(t, 0, 0)) == find(getid(s, p, q))) { res |= ok[p][((r + z - q * g) % mo + mo) % mo]; // if(ok[p][((r+z-q*g)%mo+mo)%mo])cerr<<p<<' //'<<((r+z-q*g)%mo+mo)%mo<<endl; } printf("%s\n", res ? "YES" : "NO"); } return 0; }
replace
56
57
56
57
0
p03100
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #define fo(i, a, b) for (int i = a; i <= b; i++) #define fd(i, a, b) for (int i = a; i >= b; i--) using namespace std; const int N = 1e6 + 5; int n, m, q, mod, s, t, r, u[N], v[N], c[N], g, fa[N]; bool can[2][N]; int Id(int x, int y, int z) { return (x - 1) * 6 + y * 3 + z; } int get(int x) { return fa[x] == x ? x : fa[x] = get(fa[x]); } void link(int x, int y) { x = get(x); y = get(y); if (x == y) return; fa[x] = y; } int main() { scanf("%d%d%d%d", &n, &m, &q, &mod); fo(i, 1, m) { scanf("%d%d%d", &u[i], &v[i], &c[i]); g = __gcd(g, c[i] - c[1]); } if (!g) g = mod; mod = __gcd(mod, 3 * g); int z = c[1] % g; fo(i, 0, 6 * n) fa[i] = i; fo(i, 1, m) { int w = (c[i] - z) / g; fo(p, 0, 1) fo(q, 0, 2) { link(Id(u[i], p, q), Id(v[i], 1 - p, (2 * q + w) % 3)); link(Id(v[i], p, q), Id(u[i], 1 - p, (2 * q + w) % 3)); } } for (int i = 0, j = z; i < mod << 1; i++, (j <<= 1) %= mod) can[i & 1][j] = 1; for (; q; q--) { scanf("%d%d%d", &s, &t, &r); int ret = 0; fo(p, 0, 1) fo(q, 0, 2) if (get(Id(t, 0, 0)) == get(Id(s, p, q))) ret |= can[p][(r + z + (3 - q) * g) % mod]; puts(ret ? "YES" : "NO"); } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #define fo(i, a, b) for (int i = a; i <= b; i++) #define fd(i, a, b) for (int i = a; i >= b; i--) using namespace std; const int N = 1e6 + 5; int n, m, q, mod, s, t, r, u[N], v[N], c[N], g, fa[N]; bool can[2][N]; int Id(int x, int y, int z) { return (x - 1) * 6 + y * 3 + z; } int get(int x) { return fa[x] == x ? x : fa[x] = get(fa[x]); } void link(int x, int y) { x = get(x); y = get(y); if (x == y) return; fa[x] = y; } int main() { scanf("%d%d%d%d", &n, &m, &q, &mod); fo(i, 1, m) { scanf("%d%d%d", &u[i], &v[i], &c[i]); g = __gcd(g, abs(c[i] - c[1])); } if (!g) g = mod; mod = __gcd(mod, 3 * g); int z = c[1] % g; fo(i, 0, 6 * n) fa[i] = i; fo(i, 1, m) { int w = (c[i] - z) / g; fo(p, 0, 1) fo(q, 0, 2) { link(Id(u[i], p, q), Id(v[i], 1 - p, (2 * q + w) % 3)); link(Id(v[i], p, q), Id(u[i], 1 - p, (2 * q + w) % 3)); } } for (int i = 0, j = z; i < mod << 1; i++, (j <<= 1) %= mod) can[i & 1][j] = 1; for (; q; q--) { scanf("%d%d%d", &s, &t, &r); int ret = 0; fo(p, 0, 1) fo(q, 0, 2) if (get(Id(t, 0, 0)) == get(Id(s, p, q))) ret |= can[p][(r + z + (3 - q) * g) % mod]; puts(ret ? "YES" : "NO"); } return 0; }
replace
27
28
27
28
0
p03100
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int read() { int res = 0, w = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') c = getchar(), w = -1; while (isdigit(c)) res = (res << 1) + (res << 3) + c - '0', c = getchar(); return res * w; } struct Dsu { vector<int> fa; void init(int n) { fa.resize(n + 3); for (int i = 1; i <= n; i++) fa[i] = i; } int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } bool merge(int x, int y) { x = find(x), y = find(y); if (x == y) return 0; fa[x] = y; return 1; } } dsu; const int N = 5e5 + 10; int n, m, q, Mod; int u[N], v[N], w[N]; int main() { n = read(), m = read(), q = read(), Mod = read(); int g = Mod; for (int i = 1; i <= m; i++) { u[i] = read(), v[i] = read(), w[i] = read(); if (i != 1) { g = __gcd(g, abs(w[i] - w[1])); } } if ((Mod / g) % 3 == 0) Mod = g * 3; else Mod = g; int z = w[1] % g; dsu.init((n + 2) * 6); for (int i = 1; i <= m; i++) { int p = (w[i] - z) / g % 3; for (int j = 0; j < 3; j++) { dsu.merge(u[i] * 6 + j * 2 + 0, v[i] * 6 + (2 * j + p) % 3 * 2 + 1); dsu.merge(u[i] * 6 + j * 2 + 1, v[i] * 6 + (2 * j + p) % 3 * 2 + 0); dsu.merge(v[i] * 6 + j * 2 + 0, u[i] * 6 + (2 * j + p) % 3 * 2 + 1); dsu.merge(v[i] * 6 + j * 2 + 1, u[i] * 6 + (2 * j + p) % 3 * 2 + 0); } } vector<vector<bool>> can(2, vector<bool>(g)); for (int i = 0, j = z; i < g * 2; i++, j = j * 2 % Mod) { can[i & 1][j] = 1; } while (q--) { int s = read(), t = read(), r = read(); bool ans = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { if (dsu.find(t * 6) == dsu.find(s * 6 + i * 2 + j)) { if (can[j][(r + z + (3 - i) * g) % Mod]) { ans = true; } } } } if (ans) puts("YES"); else puts("NO"); } return 0; }
#include <bits/stdc++.h> using namespace std; int read() { int res = 0, w = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') c = getchar(), w = -1; while (isdigit(c)) res = (res << 1) + (res << 3) + c - '0', c = getchar(); return res * w; } struct Dsu { vector<int> fa; void init(int n) { fa.resize(n + 3); for (int i = 1; i <= n; i++) fa[i] = i; } int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } bool merge(int x, int y) { x = find(x), y = find(y); if (x == y) return 0; fa[x] = y; return 1; } } dsu; const int N = 5e5 + 10; int n, m, q, Mod; int u[N], v[N], w[N]; int main() { n = read(), m = read(), q = read(), Mod = read(); int g = Mod; for (int i = 1; i <= m; i++) { u[i] = read(), v[i] = read(), w[i] = read(); if (i != 1) { g = __gcd(g, abs(w[i] - w[1])); } } if ((Mod / g) % 3 == 0) Mod = g * 3; else Mod = g; int z = w[1] % g; dsu.init((n + 2) * 6); for (int i = 1; i <= m; i++) { int p = (w[i] - z) / g % 3; for (int j = 0; j < 3; j++) { dsu.merge(u[i] * 6 + j * 2 + 0, v[i] * 6 + (2 * j + p) % 3 * 2 + 1); dsu.merge(u[i] * 6 + j * 2 + 1, v[i] * 6 + (2 * j + p) % 3 * 2 + 0); dsu.merge(v[i] * 6 + j * 2 + 0, u[i] * 6 + (2 * j + p) % 3 * 2 + 1); dsu.merge(v[i] * 6 + j * 2 + 1, u[i] * 6 + (2 * j + p) % 3 * 2 + 0); } } vector<vector<bool>> can(2, vector<bool>(Mod)); for (int i = 0, j = z; i < g * 2; i++, j = j * 2 % Mod) { can[i & 1][j] = 1; } while (q--) { int s = read(), t = read(), r = read(); bool ans = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { if (dsu.find(t * 6) == dsu.find(s * 6 + i * 2 + j)) { if (can[j][(r + z + (3 - i) * g) % Mod]) { ans = true; } } } } if (ans) puts("YES"); else puts("NO"); } return 0; }
replace
65
66
65
66
0
p03100
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 5; const int MAXP = 1e6 + 5; typedef long long ll; template <typename T> void chkmax(T &x, T y) { x = max(x, y); } template <typename T> void chkmin(T &x, T y) { x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } int n, m, q, P, f[MAXN]; int tot, point[MAXN][2][3]; int x[MAXN], y[MAXN], w[MAXN]; int find(int x) { if (f[x] == x) return x; else return f[x] = find(f[x]); } void merge(int x, int y) { f[find(x)] = find(y); } bool same(int x, int y) { return find(x) == find(y); } bool vis[2][MAXP]; int main() { read(n), read(m), read(q), read(P); int g = P; for (int i = 1; i <= m; i++) { read(x[i]), read(y[i]), read(w[i]); g = __gcd(g, w[i] - w[1]); } P = __gcd(3 * g, P); for (int i = 1; i <= n; i++) for (int p = 0; p <= 1; p++) for (int q = 0; q <= 2; q++) { point[i][p][q] = ++tot; f[tot] = tot; } int z = w[1] % g; for (int x = z, i = 0; i <= P; i++, x = 2 * x % P) vis[i & 1][x] = true; for (int i = 1; i <= m; i++) { int tx = x[i], ty = y[i]; int tw = (w[i] - z) % P / g; for (int p = 0; p <= 1; p++) for (int q = 0; q <= 2; q++) merge(point[tx][p][q], point[ty][p ^ 1][(2 * q + tw) % 3]); } for (int i = 1; i <= q; i++) { bool res = false; int s, t, r; read(s), read(t), read(r); for (int p = 0; p <= 1; p++) for (int q = 0; q <= 2; q++) res |= same(point[t][0][0], point[s][p][q]) && vis[p][(r + z + (3 - q) * g) % P]; if (res) puts("YES"); else puts("NO"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 5; const int MAXP = 1e6 + 5; typedef long long ll; template <typename T> void chkmax(T &x, T y) { x = max(x, y); } template <typename T> void chkmin(T &x, T y) { x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } int n, m, q, P, f[MAXN]; int tot, point[MAXN][2][3]; int x[MAXN], y[MAXN], w[MAXN]; int find(int x) { if (f[x] == x) return x; else return f[x] = find(f[x]); } void merge(int x, int y) { f[find(x)] = find(y); } bool same(int x, int y) { return find(x) == find(y); } bool vis[2][MAXP]; int main() { read(n), read(m), read(q), read(P); int g = P; for (int i = 1; i <= m; i++) { read(x[i]), read(y[i]), read(w[i]); g = __gcd(g, abs(w[i] - w[1])); } P = __gcd(3 * g, P); for (int i = 1; i <= n; i++) for (int p = 0; p <= 1; p++) for (int q = 0; q <= 2; q++) { point[i][p][q] = ++tot; f[tot] = tot; } int z = w[1] % g; for (int x = z, i = 0; i <= P; i++, x = 2 * x % P) vis[i & 1][x] = true; for (int i = 1; i <= m; i++) { int tx = x[i], ty = y[i]; int tw = (w[i] - z) % P / g; for (int p = 0; p <= 1; p++) for (int q = 0; q <= 2; q++) merge(point[tx][p][q], point[ty][p ^ 1][(2 * q + tw) % 3]); } for (int i = 1; i <= q; i++) { bool res = false; int s, t, r; read(s), read(t), read(r); for (int p = 0; p <= 1; p++) for (int q = 0; q <= 2; q++) res |= same(point[t][0][0], point[s][p][q]) && vis[p][(r + z + (3 - q) * g) % P]; if (res) puts("YES"); else puts("NO"); } return 0; }
replace
35
36
35
36
0
p03101
Python
Runtime Error
H, W = int(input().split()) h, w = int(input().split()) print((H - h) * (W - w)) H, W = map(int, input().split()) h, w = map(int, input().split()) print((H - h) * (W - w))
H, W = map(int, input().split()) h, w = map(int, input().split()) print((H - h) * (W - w))
delete
0
4
0
0
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03101/Python/s085592284.py", line 1, in <module> H, W = int(input().split()) TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
p03101
Python
Runtime Error
"""A - White Cells https://atcoder.jp/contests/abc121/tasks/abc121_a >>> main([3, 2], [2, 1]) 1 >>> main([5, 5], [2, 3]) 6 >>> main([2, 4], [2, 4]) 0""" def main(amount, portion): print((amount[0] - portion[0]) * (amount[1] - portion[1])) if __name__ == "__main__": amount = map(int, input().split(" ")) portion = map(int, input().split(" ")) main(amount, portion)
"""A - White Cells https://atcoder.jp/contests/abc121/tasks/abc121_a >>> main([3, 2], [2, 1]) 1 >>> main([5, 5], [2, 3]) 6 >>> main([2, 4], [2, 4]) 0""" def main(amount, portion): print((amount[0] - portion[0]) * (amount[1] - portion[1])) if __name__ == "__main__": amount = list(map(int, input().split(" "))) portion = list(map(int, input().split(" "))) main(amount, portion)
replace
16
18
16
18
TypeError: 'map' object is not subscriptable
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03101/Python/s794256981.py", line 20, in <module> main(amount, portion) File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03101/Python/s794256981.py", line 13, in main print((amount[0] - portion[0]) * (amount[1] - portion[1])) TypeError: 'map' object is not subscriptable
p03101
C++
Time Limit Exceeded
/* author : s@if */ #include <bits/stdc++.h> using namespace std; #define NIL -1 #define fi first #define sec second #define MAX INT_MAX #define INF 99999999 #define ll long long #define PI acos(-1.0) #define MOD 1000000007 #define PLL pair<ll, ll> #define PII pair<int, int> #define triplell pair<pair<ll, ll>, ll> #define triple pair<pair<int, int>, int> #define For(i, n) for (int i = 0; i < (int)n; i++) #define Forn(i, a, b) for (int i = (int)a; i <= (int)b; i++) #define forba(i, a, b) for (int i = (int)b; i >= (int)a; i--) int dx[] = {+1, -1, +0, +0}; int dy[] = {+0, +0, +1, -1}; int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1}; int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; int hr[] = {-2, -2, -1, +1, +2, +2, -1, +1}; int hc[] = {+1, -1, +2, +2, -1, +1, -2, -2}; const int MAXN = (int)1e5 + 9; const int N = 100000; int main() { /* freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); */ int i, j, k, l, m, n, p, q, r, tc, t; int h, w, h1, w1; cin >> h >> w; cin >> h1 >> w1; h -= h1; w -= w1; cout << h * w << endl; main(); return 0; }
/* author : s@if */ #include <bits/stdc++.h> using namespace std; #define NIL -1 #define fi first #define sec second #define MAX INT_MAX #define INF 99999999 #define ll long long #define PI acos(-1.0) #define MOD 1000000007 #define PLL pair<ll, ll> #define PII pair<int, int> #define triplell pair<pair<ll, ll>, ll> #define triple pair<pair<int, int>, int> #define For(i, n) for (int i = 0; i < (int)n; i++) #define Forn(i, a, b) for (int i = (int)a; i <= (int)b; i++) #define forba(i, a, b) for (int i = (int)b; i >= (int)a; i--) int dx[] = {+1, -1, +0, +0}; int dy[] = {+0, +0, +1, -1}; int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1}; int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; int hr[] = {-2, -2, -1, +1, +2, +2, -1, +1}; int hc[] = {+1, -1, +2, +2, -1, +1, -2, -2}; const int MAXN = (int)1e5 + 9; const int N = 100000; int main() { /* freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); */ int i, j, k, l, m, n, p, q, r, tc, t; int h, w, h1, w1; cin >> h >> w; cin >> h1 >> w1; h -= h1; w -= w1; cout << h * w << endl; // main(); return 0; }
replace
53
54
53
54
TLE
p03101
Python
Runtime Error
H, W, h, w = map(int, input().split()) print((H - h) * (W - w))
H, W = map(int, input().split()) h, w = map(int, input().split()) print((H - h) * (W - w))
replace
0
1
0
2
ValueError: not enough values to unpack (expected 4, got 2)
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03101/Python/s695121527.py", line 1, in <module> H, W, h, w = map(int, input().split()) ValueError: not enough values to unpack (expected 4, got 2)
p03101
C++
Runtime Error
#include <iostream> using namespace std; int main() { int H, W, h, w; cin >> H; cin >> W; cin >> h; cin >> w; return (H - h) * (W - w); }
#include <iostream> using namespace std; int main() { int H, W, h, w; cin >> H; cin >> W; cin >> h; cin >> w; cout << (H - h) * (W - w); return 0; }
replace
11
12
11
13
1
p03101
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } sort(a.begin(), a.end()); bool s = true; int cnt = 0, ans = 0; while (s) { for (int i = 0; i < 100000; i++) { if (cnt < m) { ans = ans + a[i] * b[i]; cnt += b[i]; } if (cnt = m) { bool s = false; break; } } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { int H, W, h, w; cin >> H >> W >> h >> w; cout << (H - h) * (W - w) << endl; }
replace
4
26
4
8
TLE
p03101
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int H, W, h, w; cin >> H >> W; cin >> h >> w; int all = H * W; all = all - W * h - H * w + h * w; cout << all << endl; return all; }
#include <iostream> #include <vector> using namespace std; int main() { int H, W, h, w; cin >> H >> W; cin >> h >> w; int all = H * W; all = all - W * h - H * w + h * w; cout << all << endl; return 0; }
replace
13
14
13
14
1
p03101
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; #define X first #define Y second #define PB push_back #define eps 1e-9 #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (int)(n); ++i) const int maxn = 1e6 + 7; inline int read() { int ret = 0, c, f = 1; for (c = getchar(); !(isdigit(c) || c == '-'); c = getchar()) ; if (c == '-') f = -1, c = getchar(); for (; isdigit(c); c = getchar()) ret = ret * 10 + c - '0'; if (f < 0) ret = -ret; return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("i.txt", "r", stdin); freopen("o.txt", "w", stdout); #endif int a, b; cin >> a >> b; int ans = a * b; int x, y; cin >> x >> y; cout << (a - x) * (b - y); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; #define X first #define Y second #define PB push_back #define eps 1e-9 #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (int)(n); ++i) const int maxn = 1e6 + 7; inline int read() { int ret = 0, c, f = 1; for (c = getchar(); !(isdigit(c) || c == '-'); c = getchar()) ; if (c == '-') f = -1, c = getchar(); for (; isdigit(c); c = getchar()) ret = ret * 10 + c - '0'; if (f < 0) ret = -ret; return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // #ifndef ONLINE_JUDGE // freopen("i.txt", "r", stdin); // freopen("o.txt", "w", stdout); // #endif int a, b; cin >> a >> b; int ans = a * b; int x, y; cin >> x >> y; cout << (a - x) * (b - y); }
replace
31
35
31
35
0
p03101
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FastRead \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long int #define FOR(i, a, n) for (int i = (a); i <= (n); ++i) #define RFOR(i, a, n) for (int i = (n); i >= (a); --i) #define FI(i, n) for (int i = 0; i < (n); ++i) #define ZERO(a) memset((a), 0, sizeof((a))) #define f first #define s second #define pb push_back #define mk make_pair #define all(g) g.begin(), g.end() int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } // #include <ext/pb_ds/assoc_container.hpp> // Common file // #include <ext/pb_ds/tree_policy.hpp> // Including // tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll, // null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // ordered_set; // I am questioning life and universe and // everything else after looking at this void solve() { ll H, W, h, w; cin >> H >> W >> h >> w; ll ans = H * W - h * W - w * H + h * w; cout << ans << endl; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif FastRead; int t; t = 1; // cin>>t; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; #define FastRead \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long int #define FOR(i, a, n) for (int i = (a); i <= (n); ++i) #define RFOR(i, a, n) for (int i = (n); i >= (a); --i) #define FI(i, n) for (int i = 0; i < (n); ++i) #define ZERO(a) memset((a), 0, sizeof((a))) #define f first #define s second #define pb push_back #define mk make_pair #define all(g) g.begin(), g.end() int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } // #include <ext/pb_ds/assoc_container.hpp> // Common file // #include <ext/pb_ds/tree_policy.hpp> // Including // tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll, // null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // ordered_set; // I am questioning life and universe and // everything else after looking at this void solve() { ll H, W, h, w; cin >> H >> W >> h >> w; ll ans = H * W - h * W - w * H + h * w; cout << ans << endl; } signed main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif FastRead; int t; t = 1; // cin>>t; while (t--) solve(); }
replace
38
42
38
42
0
p03101
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(void) { //* int a, b, c, d; cin >> a >> b >> c >> d; //*/ /* vector<int> vec(3); cin >> vec.at(0) >> vec.at(1) >> vec.at(2) ; sort(vec.begin(), vec.end()); //*/ /* if( b / a < c ){ cout << b / a << endl; } else { cout << c << endl; } //*/ cout << (a - c) / (b - d) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { //* int a, b, c, d; cin >> a >> b >> c >> d; //*/ /* vector<int> vec(3); cin >> vec.at(0) >> vec.at(1) >> vec.at(2) ; sort(vec.begin(), vec.end()); //*/ /* if( b / a < c ){ cout << b / a << endl; } else { cout << c << endl; } //*/ cout << (a - c) * (b - d) << endl; return 0; }
replace
24
25
24
25
0
p03101
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int H, W, h, w; cin >> H >> W; cin >> h >> w; int all = H * W; all = all - W * h - H * w + h * w; cout << all; return all; }
#include <iostream> #include <vector> using namespace std; int main() { int H, W, h, w; cin >> H >> W; cin >> h >> w; int all = H * W; all = all - W * h - H * w + h * w; cout << all << endl; return 0; }
replace
12
14
12
14
1
p03102
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; /*{{{*/ // template #define rep(i, n) for (int i = 0; i < (int)(n); i++) constexpr int INF = numeric_limits<int>::max() / 2; constexpr long long LINF = numeric_limits<long long>::max() / 3; #define mp make_pair #define pb push_back #define eb emplace_back #define fi first #define se second #define all(v) (v).begin(), (v).end() #define sz(x) (int)(x).size() #define debug(x) cerr << #x << ":" << x << endl #define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl // struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_; struct Double { double d; explicit Double(double x) : d(x) {} }; ostream &operator<<(ostream &os, const Double x) { os << fixed << setprecision(20) << x.d; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } 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> ostream &operator<<(ostream &os, const set<T> &st) { os << "{"; for (T v : st) os << v << ","; os << "}"; return os; } template <typename T, typename U> inline void chmax(T &x, U y) { if (y > x) x = y; } template <typename T, typename U> inline void chmin(T &x, U y) { if (y < x) x = y; } typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } // constexpr double eps = 1e-14; constexpr double eps = 1e-10; constexpr ll mod = 1e9 + 7; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; /*}}}*/ int main() { int n, m; cin >> n >> m; int c; cin >> c; vector<int> b(n); vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < m; i++) cin >> b[i]; rep(i, n) { rep(j, m) { cin >> a[i][j]; } } ll ans = 0; rep(i, n) { ll p = c; rep(j, m) p += a[i][j] * b[j]; if (p > 0) ans++; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; /*{{{*/ // template #define rep(i, n) for (int i = 0; i < (int)(n); i++) constexpr int INF = numeric_limits<int>::max() / 2; constexpr long long LINF = numeric_limits<long long>::max() / 3; #define mp make_pair #define pb push_back #define eb emplace_back #define fi first #define se second #define all(v) (v).begin(), (v).end() #define sz(x) (int)(x).size() #define debug(x) cerr << #x << ":" << x << endl #define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl // struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_; struct Double { double d; explicit Double(double x) : d(x) {} }; ostream &operator<<(ostream &os, const Double x) { os << fixed << setprecision(20) << x.d; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } 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> ostream &operator<<(ostream &os, const set<T> &st) { os << "{"; for (T v : st) os << v << ","; os << "}"; return os; } template <typename T, typename U> inline void chmax(T &x, U y) { if (y > x) x = y; } template <typename T, typename U> inline void chmin(T &x, U y) { if (y < x) x = y; } typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } // constexpr double eps = 1e-14; constexpr double eps = 1e-10; constexpr ll mod = 1e9 + 7; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; /*}}}*/ int main() { int n, m; cin >> n >> m; int c; cin >> c; vector<int> b(m); vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < m; i++) cin >> b[i]; rep(i, n) { rep(j, m) { cin >> a[i][j]; } } ll ans = 0; rep(i, n) { ll p = c; rep(j, m) p += a[i][j] * b[j]; if (p > 0) ans++; } cout << ans << endl; }
replace
74
75
74
75
0
p03102
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n, m, c; cin >> n >> m >> c; vector<int> b(m); for (int i = 0; i < m; i++) { cin >> b.at(i); } vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; i < m; j++) { cin >> a[i][j]; } } int ans = 0; for (int i = 0; i < n; i++) { int sum = 0; for (int j = 0; j < m; j++) { sum += a[i][j] * b[j]; } sum += c; if (sum > 0) { ans++; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, c; cin >> n >> m >> c; vector<int> b(m); for (int i = 0; i < m; i++) { cin >> b.at(i); } vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } int ans = 0; for (int i = 0; i < n; i++) { int sum = 0; for (int j = 0; j < m; j++) { sum += a[i][j] * b[j]; } sum += c; if (sum > 0) { ans++; } } cout << ans << endl; }
replace
12
13
12
13
TLE
p03102
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int n, m, c; cin >> n >> m >> c; vector<int> b(1); for (int i = 0; i < m; i++) { cin >> b[i]; } vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } int y = 0; for (int i = 0; i < n; i++) { long long int x = 0; for (int j = 0; j < m; j++) { x += a[i][j] * b[j]; } x += c; if (x > 0) { y++; } } cout << y << endl; return 0; }
#include <iostream> #include <vector> using namespace std; int main() { int n, m, c; cin >> n >> m >> c; vector<int> b(m); for (int i = 0; i < m; i++) { cin >> b[i]; } vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } int y = 0; for (int i = 0; i < n; i++) { long long int x = 0; for (int j = 0; j < m; j++) { x += a[i][j] * b[j]; } x += c; if (x > 0) { y++; } } cout << y << endl; return 0; }
replace
8
9
8
9
0
p03102
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; int main() { int n, m, c; cin >> n >> m >> c; vector<int> b(n); rep(i, m) cin >> b[i]; int ans = 0; rep(i, n) { int sum = 0; rep(j, m) { int a; cin >> a; sum += a * b[j]; } if (sum > -c) ans++; } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; int main() { int n, m, c; cin >> n >> m >> c; vector<int> b(m); rep(i, m) cin >> b[i]; int ans = 0; rep(i, n) { int sum = 0; rep(j, m) { int a; cin >> a; sum += a * b[j]; } if (sum > -c) ans++; } cout << ans << endl; }
replace
10
11
10
11
0
p03102
C++
Runtime Error
#include <iostream> #include <numeric> #include <vector> using namespace std; int main() { int n, m, c, ans = 0; cin >> n >> m >> c; vector<int> b(m); vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < m; i++) cin >> b[i]; 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++) if (inner_product(begin(a[n]), end(a[n]), begin(b), c) > 0) ans++; cout << ans << endl; }
#include <iostream> #include <numeric> #include <vector> using namespace std; int main() { int n, m, c, ans = 0; cin >> n >> m >> c; vector<int> b(m); vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < m; i++) cin >> b[i]; 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++) if (inner_product(begin(a[i]), end(a[i]), begin(b), c) > 0) ans++; cout << ans << endl; }
replace
16
19
16
18
-11
p03102
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, M, C; cin >> N >> M >> C; vector<int> B(M); int sum = 0; for (int i = 0; i < M; i++) { cin >> B.at(i); } int x = 0; for (int j = 0; j < N; j++) { int sum = 0; vector<int> S; for (int i = 0; i < M; i++) { cin >> S.at(i); sum += S.at(i) * B.at(i); if (i == M - 1 && sum + C > 0) { x++; } } } cout << x << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, M, C; cin >> N >> M >> C; vector<int> B(M); int sum = 0; for (int i = 0; i < M; i++) { cin >> B.at(i); } int x = 0; for (int j = 0; j < N; j++) { int sum = 0; vector<int> S(M); for (int i = 0; i < M; i++) { cin >> S.at(i); sum += S.at(i) * B.at(i); if (i == M - 1 && sum + C > 0) { x++; } } } cout << x << endl; }
replace
19
20
19
20
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
p03102
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; int main() { int N, M; int C; cin >> N >> M >> C; vector<int> B(N); rep(i, M) cin >> B[i]; int ans = 0; rep(i, N) { ll S = 0; rep(j, M) { int A; cin >> A; S += A * B[j]; } S += C; if (S > 0) ans++; } 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; int main() { int N, M; int C; cin >> N >> M >> C; vector<int> B(M); rep(i, M) cin >> B[i]; int ans = 0; rep(i, N) { ll S = 0; rep(j, M) { int A; cin >> A; S += A * B[j]; } S += C; if (S > 0) ans++; } cout << ans << endl; return 0; }
replace
9
10
9
10
0
p03102
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; using Int = long long; int main() { Int N, M, C, ans, count = 0; cin >> N >> M >> C; vector<Int> B(M); for (Int i = 0; i < M; i++) { cin >> B[i]; } vector<vector<int>> A(M); for (Int i = 0; i < N; i++) { A[i].resize(M); } for (Int i = 0; i < N; i++) { for (Int j = 0; j < M; j++) { cin >> A[i][j]; } } ans = 0; for (Int i = 0; i < N; i++) { for (Int j = 0; j < M; j++) { ans += A[i][j] * B[j]; } if (ans + C > 0) count++; ans = 0; } cout << count << endl; return 0; }
#include <iostream> #include <vector> using namespace std; using Int = long long; int main() { Int N, M, C, ans, count = 0; cin >> N >> M >> C; vector<Int> B(M); for (Int i = 0; i < M; i++) { cin >> B[i]; } vector<vector<int>> A(N); for (Int i = 0; i < N; i++) { A[i].resize(M); } for (Int i = 0; i < N; i++) { for (Int j = 0; j < M; j++) { cin >> A[i][j]; } } ans = 0; for (Int i = 0; i < N; i++) { for (Int j = 0; j < M; j++) { ans += A[i][j] * B[j]; } if (ans + C > 0) count++; ans = 0; } cout << count << endl; return 0; }
replace
14
15
14
15
0
p03102
C++
Runtime Error
#include <iostream> using namespace std; int main() { long long m, n, c, b[5], a, sum = 0, count = 0; cin >> n >> m >> c; for (int i = 0; i < m; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { sum = 0; for (int j = 0; j < m; j++) { cin >> a; sum += b[j] * a; } if (sum + c > 0) { count++; } } cout << count << '\n'; return 0; }
#include <iostream> using namespace std; int main() { int m, n, c, b[100], a, sum = 0, count = 0; cin >> n >> m >> c; for (int i = 0; i < m; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { sum = 0; for (int j = 0; j < m; j++) { cin >> a; sum += b[j] * a; } if (sum + c > 0) { count++; } } cout << count << '\n'; return 0; }
replace
4
5
4
5
0
p03102
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main(void) { int n, m, c, ans = 0; cin >> n >> m >> c; vector<int> b(n); for (int i = 0; i < m; i++) cin >> b[i]; for (int i = 0; i < n; i++) { int a, sum = 0; for (int j = 0; j < m; j++) cin >> a, sum += a * b[j]; ans += (sum + c > 0); } cout << ans << endl; }
#include <iostream> #include <vector> using namespace std; int main(void) { int n, m, c, ans = 0; cin >> n >> m >> c; vector<int> b(m); for (int i = 0; i < m; i++) cin >> b[i]; for (int i = 0; i < n; i++) { int a, sum = 0; for (int j = 0; j < m; j++) cin >> a, sum += a * b[j]; ans += (sum + c > 0); } cout << ans << endl; }
replace
8
9
8
9
0
p03102
C++
Runtime Error
#include <algorithm> #include <functional> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <tuple> #include <vector> #define PI 3.14159265359 #define INF 100100100100100100 #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, a, n) for (int i = a; i < n; i++) #define in(a) cin >> a #define out(a) cout << a << endl using namespace std; typedef long long ll; typedef pair<ll, ll> PLL; int main() { int ans = 0; int n, m, c; cin >> n >> m >> c; vector<int> b(n); rep(i, m) { cin >> b[i]; } rep(i, n) { ll sum = 0; rep(j, m) { int a; cin >> a; sum += a * b[j]; } sum += c; if (sum > 0) { ans++; } } cout << ans << endl; return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <tuple> #include <vector> #define PI 3.14159265359 #define INF 100100100100100100 #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, a, n) for (int i = a; i < n; i++) #define in(a) cin >> a #define out(a) cout << a << endl using namespace std; typedef long long ll; typedef pair<ll, ll> PLL; int main() { int ans = 0; int n, m, c; cin >> n >> m >> c; vector<int> b(m); rep(i, m) { cin >> b[i]; } rep(i, n) { ll sum = 0; rep(j, m) { int a; cin >> a; sum += a * b[j]; } sum += c; if (sum > 0) { ans++; } } cout << ans << endl; return 0; }
replace
26
27
26
27
0
p03102
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { int n, m, c; cin >> n >> m >> c; vector<int> v(n); for (int i = 0; i < m; i++) cin >> v[i]; int res = 0; for (int i = 0; i < n; i++) { int sum = 0; for (int j = 0; j < m; j++) { int a; cin >> a; sum += a * v[j]; } if (sum + c > 0) res++; } cout << res; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { int n, m, c; cin >> n >> m >> c; vector<int> v(m); for (int i = 0; i < m; i++) cin >> v[i]; int res = 0; for (int i = 0; i < n; i++) { int sum = 0; for (int j = 0; j < m; j++) { int a; cin >> a; sum += a * v[j]; } if (sum + c > 0) res++; } cout << res; return 0; }
replace
7
8
7
8
0
p03102
C++
Runtime Error
#include <bits/stdc++.h> #define MOD (long long)(1E9 + 7) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int n, m, c, cnt = 0; cin >> n >> m >> c; vector<int> b(n); rep(i, m) { cin >> b[i]; } rep(i, n) { int a, t = 0; rep(j, m) { cin >> a; t += a * b[j]; } t += c; if (t > 0) cnt++; } cout << cnt << endl; }
#include <bits/stdc++.h> #define MOD (long long)(1E9 + 7) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int n, m, c, cnt = 0; cin >> n >> m >> c; vector<int> b(m); rep(i, m) { cin >> b[i]; } rep(i, n) { int a, t = 0; rep(j, m) { cin >> a; t += a * b[j]; } t += c; if (t > 0) cnt++; } cout << cnt << endl; }
replace
9
10
9
10
0
p03102
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <forward_list> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <utility> #include <vector> #define ll long long int #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int mx4[] = {0, 1, 0, -1}; int my4[] = {1, 0, -1, 0}; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int main() { int n, m, c; cin >> n >> m >> c; vector<int> b(m); int A[m][m]; rep(i, m) cin >> b[i]; rep(i, n) { rep(j, m) { int a; cin >> a; A[i][j] = a; } } int cnt = 0, check; rep(i, n) { check = 0; rep(j, m) { check += A[i][j] * b[j]; } if (check + c > 0) cnt++; } cout << cnt << endl; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <forward_list> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <utility> #include <vector> #define ll long long int #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int mx4[] = {0, 1, 0, -1}; int my4[] = {1, 0, -1, 0}; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int main() { int n, m, c; cin >> n >> m >> c; vector<int> b(m); int A[n][m]; rep(i, m) cin >> b[i]; rep(i, n) { rep(j, m) { int a; cin >> a; A[i][j] = a; } } int cnt = 0, check; rep(i, n) { check = 0; rep(j, m) { check += A[i][j] * b[j]; } if (check + c > 0) cnt++; } cout << cnt << endl; }
replace
28
29
28
29
0