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
p02889
C++
Time Limit Exceeded
typedef long long ll; #include <bits/stdc++.h> using namespace std; ll n, m, l; vector<vector<pair<ll, ll>>> edges(400); vector<vector<bool>> reals(400, vector<bool>(400, false)); vector<vector<ll>> costs(400, vector<ll>(400, -1)); void saiki(ll from, ll now, ll gas) { for (auto e : edges[now]) { ll to = e.first; ll cost = e.second; if (cost <= gas && gas - cost > costs[from][to]) { reals[from][to] = true; costs[from][to] = gas - cost; saiki(from, to, gas - cost); } } } int main() { std::cin >> n >> m >> l; for (int i = 0; i < m; i++) { ll a, b, c; std::cin >> a >> b >> c; a--; b--; edges[a].push_back({b, c}); edges[b].push_back({a, c}); } for (int i = 0; i < n; i++) { saiki(i, i, l); } vector<vector<ll>> dis(400, vector<ll>(400, 1e18)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { dis[i][j] = 0; } else if (reals[i][j]) { dis[i][j] = 1; // std::cout << i<< " "<<j << std::endl; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]); // std::cout << dis[j][k] <<" "<<j<<" "<<k << std::endl; } } } ll q; std::cin >> q; for (int i = 0; i < q; i++) { ll s, t; std::cin >> s >> t; s--; t--; if (dis[s][t] == 1e18) { std::cout << -1 << std::endl; } else { std::cout << dis[s][t] - 1 << std::endl; } } }
typedef long long ll; #include <bits/stdc++.h> using namespace std; ll n, m, l; vector<vector<pair<ll, ll>>> edges(400); vector<vector<bool>> reals(400, vector<bool>(400, false)); vector<vector<ll>> costs(400, vector<ll>(400, -1)); void saiki(ll from, ll now, ll gas) { for (auto e : edges[now]) { ll to = e.first; ll cost = e.second; if (cost <= gas && gas - cost > costs[from][to]) { reals[from][to] = true; costs[from][to] = gas - cost; saiki(from, to, gas - cost); } } } int main() { std::cin >> n >> m >> l; for (int i = 0; i < m; i++) { ll a, b, c; std::cin >> a >> b >> c; a--; b--; edges[a].push_back({b, c}); edges[b].push_back({a, c}); } for (int i = 0; i < n; i++) { priority_queue<pair<ll, ll>> que; que.push({l, i}); while (!que.empty()) { auto qt = que.top(); que.pop(); ll now = qt.second; ll gas = qt.first; for (auto e : edges[now]) { ll to = e.first; ll cost = e.second; if (cost <= gas && gas - cost > costs[i][to]) { reals[i][to] = true; costs[i][to] = gas - cost; que.push({gas - cost, to}); } } } } vector<vector<ll>> dis(400, vector<ll>(400, 1e18)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { dis[i][j] = 0; } else if (reals[i][j]) { dis[i][j] = 1; // std::cout << i<< " "<<j << std::endl; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]); // std::cout << dis[j][k] <<" "<<j<<" "<<k << std::endl; } } } ll q; std::cin >> q; for (int i = 0; i < q; i++) { ll s, t; std::cin >> s >> t; s--; t--; if (dis[s][t] == 1e18) { std::cout << -1 << std::endl; } else { std::cout << dis[s][t] - 1 << std::endl; } } }
replace
34
35
34
52
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, m) for (long long i = 0; i < m; i++) #define per(i, m) for (long long i = m - 1; i >= 0; i--) #define FOR(i, n, m) for (long long i = n; i < m; i++) #define ROF(i, n, m) for (long long i = m - 1; i >= n; i--) #define SORT(v, n) \ do { \ sort(v, v + n); \ reverse(v, v + n); \ } while (0) #define all(x) (x).begin(), (x).end() #define MP make_pair #define MT make_tuple #define EPS (1e-7) #define INF (1e18) #define PI (acos(-1)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using namespace std; typedef long long ll; const ll MOD = 1000000007; typedef pair<int, int> P; typedef pair<ll, ll> LP; ll POW(ll x, ll n) { x %= MOD; if (n == 0) return 1; if (n % 2 == 0) return POW(x * x, n / 2) % MOD; return x % MOD * POW(x, n - 1) % MOD; } ll POW2(ll x, ll n) { if (n == 0) return 1; if (n % 2 == 0) return POW2(x * x, n / 2); return x * POW2(x, n - 1); } ll POW3(ll x, ll n, ll m) { x %= m; if (n == 0) return 1; if (n % 2 == 0) return POW3(x * x, n / 2, m) % m; return x * POW3(x, n - 1, m) % m; } ll gcd(ll u, ll v) { ll r; while (0 != v) { r = u % v; u = v; v = r; } return u; } ll lcm(ll u, ll v) { return u / gcd(u, v) * v; } ll KAI(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI(m - 1) % MOD; } ll KAI2(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI2(m - 1); } ll extGCD(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } inline ll mod(ll a, ll m) { return (a % m + m) % m; } ll modinv(ll a) { ll x, y; extGCD(a, MOD, x, y); return mod(x, MOD); } ll COM(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI(m) % MOD * modinv(KAI(n) % MOD * KAI(m - n) % MOD) % MOD; } ll COM2(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI2(m) / KAI2(n) / KAI2(m - n); } ll DEC(ll x, ll m, ll n) { return x % POW2(m, n + 1) / POW2(m, n); } ll keta(ll x, ll n) { if (x == 0) return 0; return keta(x / n, n) + 1; } ll DIV(ll x, ll n) { if (x == 0) return 0; return x / n + DIV(x / n, n); } ll ORD(ll x, ll n) { if (x == 0) return INF; if (x % n != 0) return 0; return 1 + ORD(x / n, n); } ll SUP(ll x, ll n) { if (x == 0) return 0; if (x % n != 0) return x; return SUP(x / n, n); } ll SGS(ll x, ll y, ll m) { if (y == 0) return 0; if (y % 2 == 0) { return (1 + POW3(x, y / 2, m)) * SGS(x, y / 2, m) % m; } return (1 + x * SGS(x, y - 1, m)) % m; } ll SSGS(ll x, ll y, ll m) { if (y == 0) return 0; if (y == 1) return 1; if (y % 2 == 0) { return (SSGS(x, y / 2, m) * (POW3(x, y / 2, m) + 1) % m + SGS(x, y / 2, m) * y / 2 % m) % m; } return (SSGS(x, y - 1, m) * x % m + y) % m; } void shuffle(ll array[], ll size) { for (ll i = 0; i < size; i++) { ll j = rand() % size; ll t = array[i]; array[i] = array[j]; array[j] = t; } } ll SQRT(ll n) { ll ok, ng, mid; ng = n + 1; if (303700500 < ng) ng = 303700500; ok = 0; while (abs(ok - ng) > 1) { mid = (ok + ng) / 2; if (mid * mid <= n) { ok = mid; } else { ng = mid; } } return ok; } struct UnionFind { vector<int> par; vector<int> sizes; UnionFind(int n) : par(n), sizes(n, 1) { rep(i, n) par[i] = i; } int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sizes[x] < sizes[y]) swap(x, y); par[y] = x; sizes[x] += sizes[y]; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sizes[find(x)]; } }; map<int64_t, int> prime_factor(int64_t n) { map<int64_t, int> ret; for (int64_t i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } struct edge { ll to, cost; }; struct graph { ll V; vector<vector<edge>> G; vector<ll> d; graph(ll n) { init(n); } void init(ll n) { V = n; G.resize(V); d.resize(V); rep(i, V) { d[i] = INF; } } void add_edge(ll s, ll t, ll cost) { edge e; e.to = t, e.cost = cost; G[s].push_back(e); } void dijkstra(ll s) { rep(i, V) { d[i] = INF; } d[s] = 0; priority_queue<LP, vector<LP>, greater<LP>> que; que.push(LP(0, s)); while (!que.empty()) { LP p = que.top(); que.pop(); ll v = p.second; if (d[v] < p.first) continue; for (auto e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(LP(d[e.to], e.to)); } } } } }; ll d[310][310]; void warshall_floyd(ll n) { rep(i, n) rep(j, n) rep(k, n) d[j][k] = min(d[j][k], d[j][i] + d[i][k]); } struct bit { ll m; vector<ll> b; bit(ll i) { m = i; b.resize(m + 1); } ll num(ll i) { return b[i]; } ll sum(ll i) { ll s = 0; while (i > 0) { s += b[i]; i -= i & -i; } return s; } void add(ll i, ll x) { while (i <= m) { b[i] += x; i += i & -i; } } }; ll n, m, l, a[100000], b[100000], c[100000], q, s[100000], t[100000], ans[310][310]; vector<vector<ll>> p(400); queue<LP> que; void bfs(ll c) { que.push(make_pair(c, 0)); while (que.size()) { LP f = que.front(); que.pop(); ans[c][f.first] = min(f.second, ans[c][f.first]); rep(i, p[f.first].size()) { if (ans[c][p[f.first][i]] == INF) que.push(make_pair(p[f.first][i], f.second + 1)); } } return; } int main() { cin >> n >> m >> l; rep(i, m) cin >> a[i] >> b[i] >> c[i]; rep(i, m) { a[i]--; b[i]--; } cin >> q; rep(i, q) cin >> s[i] >> t[i]; rep(i, q) { s[i]--; t[i]--; } rep(i, n) rep(j, n) d[i][j] = INF; rep(i, n) d[i][i] = 0; rep(i, m) { d[a[i]][b[i]] = c[i]; d[b[i]][a[i]] = c[i]; } warshall_floyd(n); rep(i, n) { rep(j, n) { if (d[i][j] <= l) p[i].push_back(j); } } rep(i, 305) rep(j, 305) ans[i][j] = INF; rep(i, q) { bfs(s[i]); } rep(i, q) { if (ans[s[i]][t[i]] == INF) printf("-1\n"); else printf("%lld\n", ans[s[i]][t[i]] - 1); } }
#include <bits/stdc++.h> #define rep(i, m) for (long long i = 0; i < m; i++) #define per(i, m) for (long long i = m - 1; i >= 0; i--) #define FOR(i, n, m) for (long long i = n; i < m; i++) #define ROF(i, n, m) for (long long i = m - 1; i >= n; i--) #define SORT(v, n) \ do { \ sort(v, v + n); \ reverse(v, v + n); \ } while (0) #define all(x) (x).begin(), (x).end() #define MP make_pair #define MT make_tuple #define EPS (1e-7) #define INF (1e18) #define PI (acos(-1)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using namespace std; typedef long long ll; const ll MOD = 1000000007; typedef pair<int, int> P; typedef pair<ll, ll> LP; ll POW(ll x, ll n) { x %= MOD; if (n == 0) return 1; if (n % 2 == 0) return POW(x * x, n / 2) % MOD; return x % MOD * POW(x, n - 1) % MOD; } ll POW2(ll x, ll n) { if (n == 0) return 1; if (n % 2 == 0) return POW2(x * x, n / 2); return x * POW2(x, n - 1); } ll POW3(ll x, ll n, ll m) { x %= m; if (n == 0) return 1; if (n % 2 == 0) return POW3(x * x, n / 2, m) % m; return x * POW3(x, n - 1, m) % m; } ll gcd(ll u, ll v) { ll r; while (0 != v) { r = u % v; u = v; v = r; } return u; } ll lcm(ll u, ll v) { return u / gcd(u, v) * v; } ll KAI(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI(m - 1) % MOD; } ll KAI2(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI2(m - 1); } ll extGCD(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } inline ll mod(ll a, ll m) { return (a % m + m) % m; } ll modinv(ll a) { ll x, y; extGCD(a, MOD, x, y); return mod(x, MOD); } ll COM(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI(m) % MOD * modinv(KAI(n) % MOD * KAI(m - n) % MOD) % MOD; } ll COM2(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI2(m) / KAI2(n) / KAI2(m - n); } ll DEC(ll x, ll m, ll n) { return x % POW2(m, n + 1) / POW2(m, n); } ll keta(ll x, ll n) { if (x == 0) return 0; return keta(x / n, n) + 1; } ll DIV(ll x, ll n) { if (x == 0) return 0; return x / n + DIV(x / n, n); } ll ORD(ll x, ll n) { if (x == 0) return INF; if (x % n != 0) return 0; return 1 + ORD(x / n, n); } ll SUP(ll x, ll n) { if (x == 0) return 0; if (x % n != 0) return x; return SUP(x / n, n); } ll SGS(ll x, ll y, ll m) { if (y == 0) return 0; if (y % 2 == 0) { return (1 + POW3(x, y / 2, m)) * SGS(x, y / 2, m) % m; } return (1 + x * SGS(x, y - 1, m)) % m; } ll SSGS(ll x, ll y, ll m) { if (y == 0) return 0; if (y == 1) return 1; if (y % 2 == 0) { return (SSGS(x, y / 2, m) * (POW3(x, y / 2, m) + 1) % m + SGS(x, y / 2, m) * y / 2 % m) % m; } return (SSGS(x, y - 1, m) * x % m + y) % m; } void shuffle(ll array[], ll size) { for (ll i = 0; i < size; i++) { ll j = rand() % size; ll t = array[i]; array[i] = array[j]; array[j] = t; } } ll SQRT(ll n) { ll ok, ng, mid; ng = n + 1; if (303700500 < ng) ng = 303700500; ok = 0; while (abs(ok - ng) > 1) { mid = (ok + ng) / 2; if (mid * mid <= n) { ok = mid; } else { ng = mid; } } return ok; } struct UnionFind { vector<int> par; vector<int> sizes; UnionFind(int n) : par(n), sizes(n, 1) { rep(i, n) par[i] = i; } int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sizes[x] < sizes[y]) swap(x, y); par[y] = x; sizes[x] += sizes[y]; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sizes[find(x)]; } }; map<int64_t, int> prime_factor(int64_t n) { map<int64_t, int> ret; for (int64_t i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } struct edge { ll to, cost; }; struct graph { ll V; vector<vector<edge>> G; vector<ll> d; graph(ll n) { init(n); } void init(ll n) { V = n; G.resize(V); d.resize(V); rep(i, V) { d[i] = INF; } } void add_edge(ll s, ll t, ll cost) { edge e; e.to = t, e.cost = cost; G[s].push_back(e); } void dijkstra(ll s) { rep(i, V) { d[i] = INF; } d[s] = 0; priority_queue<LP, vector<LP>, greater<LP>> que; que.push(LP(0, s)); while (!que.empty()) { LP p = que.top(); que.pop(); ll v = p.second; if (d[v] < p.first) continue; for (auto e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(LP(d[e.to], e.to)); } } } } }; ll d[310][310]; void warshall_floyd(ll n) { rep(i, n) rep(j, n) rep(k, n) d[j][k] = min(d[j][k], d[j][i] + d[i][k]); } struct bit { ll m; vector<ll> b; bit(ll i) { m = i; b.resize(m + 1); } ll num(ll i) { return b[i]; } ll sum(ll i) { ll s = 0; while (i > 0) { s += b[i]; i -= i & -i; } return s; } void add(ll i, ll x) { while (i <= m) { b[i] += x; i += i & -i; } } }; ll n, m, l, a[100000], b[100000], c[100000], q, s[100000], t[100000], ans[310][310]; vector<vector<ll>> p(400); queue<LP> que; void bfs(ll c) { que.push(make_pair(c, 0)); while (que.size()) { LP f = que.front(); que.pop(); if (ans[c][f.first] <= f.second) continue; ans[c][f.first] = f.second; rep(i, p[f.first].size()) { if (ans[c][p[f.first][i]] == INF) que.push(make_pair(p[f.first][i], f.second + 1)); } } return; } int main() { cin >> n >> m >> l; rep(i, m) cin >> a[i] >> b[i] >> c[i]; rep(i, m) { a[i]--; b[i]--; } cin >> q; rep(i, q) cin >> s[i] >> t[i]; rep(i, q) { s[i]--; t[i]--; } rep(i, n) rep(j, n) d[i][j] = INF; rep(i, n) d[i][i] = 0; rep(i, m) { d[a[i]][b[i]] = c[i]; d[b[i]][a[i]] = c[i]; } warshall_floyd(n); rep(i, n) { rep(j, n) { if (d[i][j] <= l) p[i].push_back(j); } } rep(i, 305) rep(j, 305) ans[i][j] = INF; rep(i, q) { bfs(s[i]); } rep(i, q) { if (ans[s[i]][t[i]] == INF) printf("-1\n"); else printf("%lld\n", ans[s[i]][t[i]] - 1); } }
replace
321
322
321
324
TLE
p02889
C++
Runtime Error
///*****************************??????????????????? #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define Khela \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define ll long long int #define llu long long unsigned int #define pf printf #define sf scanf #define f first #define s second #define pb push_back #define mk make_pair #define pii pair<int, int> int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1}; int dx4[] = {0, 0, 1, -1}; int dy4[] = {1, -1, 0, 0}; typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } const ll Max = 1e18 + 100; const int sz = 111; ll dist[2][sz][sz]; void Floyad(ll n, ll l, ll tar) { for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (dist[tar][i][k] + dist[tar][k][j] < dist[tar][i][j]) dist[tar][i][j] = dist[tar][i][k] + dist[tar][k][j]; } } } } int main() { Khela ll a, b, c, i, j, k, q, p, x, y, ct, ct1, m, l, r, x1, y1, mn, h, sum1, in, z, mid, n, mx; char ch; double d; string str1, str2, str; int t, cs; cin >> n >> m >> l; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) dist[0][i][j] = dist[1][i][j] = 0; else dist[0][i][j] = dist[1][i][j] = Max; } } for (i = 1; i <= m; i++) { cin >> p >> q >> c; dist[0][p][q] = dist[0][q][p] = c; } Floyad(n, l, 0ll); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (i == j) dist[1][i][j] = 0; else dist[1][i][j] = dist[1][j][i] = ((dist[0][i][j] <= l) ? 1ll : Max); } } Floyad(n, l, 1ll); cin >> x; while (x--) { cin >> p >> q; if (dist[1][p][q] >= Max) cout << "-1" << endl; else { cout << dist[1][p][q] - 1 << endl; } } } /* help taken from: https://codeforces.com/blog/entry/70703 */
///*****************************??????????????????? #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define Khela \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define ll long long int #define llu long long unsigned int #define pf printf #define sf scanf #define f first #define s second #define pb push_back #define mk make_pair #define pii pair<int, int> int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1}; int dx4[] = {0, 0, 1, -1}; int dy4[] = {1, -1, 0, 0}; typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } const ll Max = 1e18 + 100; const int sz = 310; ll dist[2][sz][sz]; void Floyad(ll n, ll l, ll tar) { for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (dist[tar][i][k] + dist[tar][k][j] < dist[tar][i][j]) dist[tar][i][j] = dist[tar][i][k] + dist[tar][k][j]; } } } } int main() { Khela ll a, b, c, i, j, k, q, p, x, y, ct, ct1, m, l, r, x1, y1, mn, h, sum1, in, z, mid, n, mx; char ch; double d; string str1, str2, str; int t, cs; cin >> n >> m >> l; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) dist[0][i][j] = dist[1][i][j] = 0; else dist[0][i][j] = dist[1][i][j] = Max; } } for (i = 1; i <= m; i++) { cin >> p >> q >> c; dist[0][p][q] = dist[0][q][p] = c; } Floyad(n, l, 0ll); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (i == j) dist[1][i][j] = 0; else dist[1][i][j] = dist[1][j][i] = ((dist[0][i][j] <= l) ? 1ll : Max); } } Floyad(n, l, 1ll); cin >> x; while (x--) { cin >> p >> q; if (dist[1][p][q] >= Max) cout << "-1" << endl; else { cout << dist[1][p][q] - 1 << endl; } } } /* help taken from: https://codeforces.com/blog/entry/70703 */
replace
33
34
33
34
0
p02889
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define f1(a, b, c) for (int c = a; c <= b; c++) #define f2(a, b, c) for (int c = a; c >= b; c--) #define f3(a, b, c) for (int c = a; c; c = b) #define so1(a, n) sort(a + 1, a + n + 1, mycmp); #define so2(a, n) sort(a + 1, a + n + 1); #define ll long long #define itn int #define ubt int const int twx = 20000 + 100; const int inf = 0x3f3f3f3f; ll read() { ll sum = 0; ll flag = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') { flag = -1; } c = getchar(); } while (c >= '0' && c <= '9') { sum = ((sum * 10) + c - '0'); c = getchar(); } return sum * flag; } int n, m, l; struct LV { int y; int Next; int v; } a[twx << 1]; int Link[twx]; int len; int d[twx]; int v[twx]; int f[301][301]; priority_queue<pair<int, int>> q; void dijkstra(int s) { memset(v, 0, sizeof v); memset(d, 0x3f, sizeof d); q.push(make_pair(0, s)); d[s] = 0; while (!q.empty()) { int x = q.top().second; q.pop(); if (v[x]) { continue; } v[x] = 1; f3(Link[x], a[i].Next, i) { int y = a[i].y; int v = a[i].v; if (d[y] > d[x] + v && d[x] + v <= l) { d[y] = d[x] + v; f[s][y] = 0; q.push(make_pair(-d[y], y)); } } } } void floyed() { f1(1, n, k) { f1(1, n, i) { f1(1, n, j) { f[i][j] = min(f[i][j], f[i][k] + f[k][j] + 1); } } } } void Insert(int x, int y, int z) { a[++len].y = y; a[len].v = z; a[len].Next = Link[x]; Link[x] = len; } void init() { n = read(); m = read(); l = read(); f1(1, m, i) { int x = read(); int y = read(); int z = read(); Insert(x, y, z); Insert(y, x, z); } memset(f, 0x3f, sizeof f); f1(1, n, i) { dijkstra(i); } int qq = read(); floyed(); while (qq--) { int x = read(); int y = read(); printf("%d\n", f[x][y] == inf ? -1 : f[x][y]); } } void work() {} void print() {} int main() { init(); return 0; }
#include <bits/stdc++.h> using namespace std; #define f1(a, b, c) for (int c = a; c <= b; c++) #define f2(a, b, c) for (int c = a; c >= b; c--) #define f3(a, b, c) for (int c = a; c; c = b) #define so1(a, n) sort(a + 1, a + n + 1, mycmp); #define so2(a, n) sort(a + 1, a + n + 1); #define ll long long #define itn int #define ubt int const int twx = 100000 + 100; const int inf = 0x3f3f3f3f; ll read() { ll sum = 0; ll flag = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') { flag = -1; } c = getchar(); } while (c >= '0' && c <= '9') { sum = ((sum * 10) + c - '0'); c = getchar(); } return sum * flag; } int n, m, l; struct LV { int y; int Next; int v; } a[twx << 1]; int Link[twx]; int len; int d[twx]; int v[twx]; int f[301][301]; priority_queue<pair<int, int>> q; void dijkstra(int s) { memset(v, 0, sizeof v); memset(d, 0x3f, sizeof d); q.push(make_pair(0, s)); d[s] = 0; while (!q.empty()) { int x = q.top().second; q.pop(); if (v[x]) { continue; } v[x] = 1; f3(Link[x], a[i].Next, i) { int y = a[i].y; int v = a[i].v; if (d[y] > d[x] + v && d[x] + v <= l) { d[y] = d[x] + v; f[s][y] = 0; q.push(make_pair(-d[y], y)); } } } } void floyed() { f1(1, n, k) { f1(1, n, i) { f1(1, n, j) { f[i][j] = min(f[i][j], f[i][k] + f[k][j] + 1); } } } } void Insert(int x, int y, int z) { a[++len].y = y; a[len].v = z; a[len].Next = Link[x]; Link[x] = len; } void init() { n = read(); m = read(); l = read(); f1(1, m, i) { int x = read(); int y = read(); int z = read(); Insert(x, y, z); Insert(y, x, z); } memset(f, 0x3f, sizeof f); f1(1, n, i) { dijkstra(i); } int qq = read(); floyed(); while (qq--) { int x = read(); int y = read(); printf("%d\n", f[x][y] == inf ? -1 : f[x][y]); } } void work() {} void print() {} int main() { init(); return 0; }
replace
10
11
10
11
0
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, j) for (int i = 0; i < j; i++) #define all(obj) (obj).begin(), (obj).end() #define rall(obj) (obj).rbegin(), (obj).rend() typedef long long int ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<pii> vpii; pair<ll, ll> alldp[303][303]; bool allused[303][303]; vector<pair<ll, ll>> toc[303]; const ll inf = (1LL << 50); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; int n, m, l; cin >> n >> m >> l; rep(i, m) { int a, b, c; cin >> a >> b >> c; a--, b--; toc[a].emplace_back(b, c); toc[b].emplace_back(a, c); } rep(i, n) { int s = i; bool *used = (allused[i]); pair<ll, ll> *dp = (alldp[i]); rep(k, n) dp[k] = make_pair(inf, inf); rep(j, n) { used[j] = false; } auto comp = [](const tuple<ll, ll, ll> &a, const tuple<ll, ll, ll> &b) { ll a1, a2, a3, b1, b2, b3; tie(a1, a2, a3) = a; tie(b1, b2, b3) = b; if (a1 == b1) { return b2 > a2; } else return a1 > b1; }; priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll>>, decltype(comp)> que(comp); que.emplace(0, l, s); while (!que.empty()) { int x, y, z; tie(x, y, z) = que.top(); que.pop(); if (used[z]) continue; used[z] = true; dp[z] = make_pair(x, y); for (auto &p : toc[z]) { int dist = p.second; int j = p.first; int nx = x, ny = y; if (dist > l) continue; else if (dist > ny) { nx++; ny = l; } ny -= dist; que.emplace(nx, ny, j); } } } int q; cin >> q; rep(i, q) { int s, t; cin >> s >> t; s--, t--; if (alldp[s][t].second == inf) { cout << -1 << endl; } else { cout << alldp[s][t].first << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, j) for (int i = 0; i < j; i++) #define all(obj) (obj).begin(), (obj).end() #define rall(obj) (obj).rbegin(), (obj).rend() typedef long long int ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<pii> vpii; pair<ll, ll> alldp[303][303]; bool allused[303][303]; vector<pair<ll, ll>> toc[303]; const ll inf = (1LL << 50); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; int n, m, l; cin >> n >> m >> l; rep(i, m) { int a, b, c; cin >> a >> b >> c; a--, b--; toc[a].emplace_back(b, c); toc[b].emplace_back(a, c); } rep(i, n) { int s = i; bool *used = (allused[i]); pair<ll, ll> *dp = (alldp[i]); rep(k, n) dp[k] = make_pair(inf, inf); rep(j, n) { used[j] = false; } auto comp = [](const tuple<ll, ll, ll> &a, const tuple<ll, ll, ll> &b) { ll a1, a2, a3, b1, b2, b3; tie(a1, a2, a3) = a; tie(b1, b2, b3) = b; if (a1 == b1) { return b2 > a2; } else return a1 > b1; }; priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll>>, decltype(comp)> que(comp); que.emplace(0, l, s); while (!que.empty()) { int x, y, z; tie(x, y, z) = que.top(); que.pop(); if (used[z]) continue; used[z] = true; dp[z] = make_pair(x, y); for (auto &p : toc[z]) { int dist = p.second; int j = p.first; if (used[j]) continue; int nx = x, ny = y; if (dist > l) continue; else if (dist > ny) { nx++; ny = l; } ny -= dist; que.emplace(nx, ny, j); } } } int q; cin >> q; rep(i, q) { int s, t; cin >> s >> t; s--, t--; if (alldp[s][t].second == inf) { cout << -1 << endl; } else { cout << alldp[s][t].first << endl; } } return 0; }
insert
71
71
71
73
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define debug(x) \ cout << "DEBUG" \ << " " << #x << ":" << x << '\n' // ↓0-originか1-originでn回繰り返し #define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-origin昇順 #define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++) // 1-origin昇順 #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) // 0-origin降順 #define rrep1(i, n) for (int i = ((int)(n)); i >= 1; i--) // 1-origin降順 // rep2 -> 第二引数 m から n 回繰り返し ex) m=5 n=3 なら i=5,i=6,i=7 まで #define rep2(i, m, n) for (int i = ((int)(m)); i < ((int)(n)) + ((int)(m)); i++) #define SIZE(x) ((int)((x).size())) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<vi> vii; // 2次元配列 typedef vector<ll> vll; typedef vector<string> vs; template <class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return 1; } return 0; } template <class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; } const ll inf = numeric_limits<ll>::max(); /*GRAPH_TEMPLATE=============================================*/ template <typename T> class Edge { public: int srch; // searched?探索状態記録「null = -1」 int to; // 辺の行き先 T cost; // 辺の重み Edge(int t, T w) : srch(-1), to(t), cost(w) {} Edge(int src, int t, T w) : srch(src), to(t), cost(w) {} }; template <typename T> using W_Graph = vector<vector<Edge<T>>>; // 重み付きグラフ using Graph = vector<vector<int>>; // 通常グラフ template <typename T> using Matrix = vector<vector<T>>; // 隣接行列 /*FUNCs=================================================*/ /*MAIN==================================================*/ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); // cin cout 高速化 // MAKING GRAPH int n, m, l; cin >> n >> m >> l; W_Graph<ll> G(n); rep(i, m) { int a, b; ll c; cin >> a >> b >> c; a--; b--; if (c > l) continue; // このケースのルートは通れないので枝刈り G[a].emplace_back(b, c); G[b].emplace_back(a, c); } // MAKING Table with dijkstra using P = pair<ll, ll>; // ★優先度的にcnt,distの順! <charging_cnt, dist> vector<vector<P>> rec(n, vector<P>(n, make_pair(inf, inf))); // recording using TP = pair<P, int>; // <cnt, dist, id> //優先度的にcnt,distの順! priority_queue<TP, vector<TP>, greater<TP>> que; for (int i = 0; i < n; ++i) { rec[i][i] = make_pair(0LL, 0LL); que.emplace(rec[i][i], i); while (que.size()) { auto p = que.top(); que.pop(); ll cnt = p.first.first; ll dist = p.first.second; int id = p.second; // if(dist > rec[i][id].second) continue; //いるかな? for (auto &edge : G[id]) { ll new_dist = dist + edge.cost; ll new_cnt = cnt; // 補給の処理 if (new_dist > l) { new_dist = edge.cost; new_cnt++; } P next_p = make_pair(new_cnt, new_dist); if (chmin(rec[i][edge.to], next_p)) { que.emplace(rec[i][edge.to], edge.to); } } } } // ANSWER to Query int q; cin >> q; rep(i, q) { int s, t; cin >> s >> t; --s; --t; if (rec[s][t].first == inf) cout << -1 << '\n'; else cout << rec[s][t].first << '\n'; } }
#include <bits/stdc++.h> using namespace std; #define debug(x) \ cout << "DEBUG" \ << " " << #x << ":" << x << '\n' // ↓0-originか1-originでn回繰り返し #define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-origin昇順 #define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++) // 1-origin昇順 #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) // 0-origin降順 #define rrep1(i, n) for (int i = ((int)(n)); i >= 1; i--) // 1-origin降順 // rep2 -> 第二引数 m から n 回繰り返し ex) m=5 n=3 なら i=5,i=6,i=7 まで #define rep2(i, m, n) for (int i = ((int)(m)); i < ((int)(n)) + ((int)(m)); i++) #define SIZE(x) ((int)((x).size())) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<vi> vii; // 2次元配列 typedef vector<ll> vll; typedef vector<string> vs; template <class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return 1; } return 0; } template <class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; } const ll inf = numeric_limits<ll>::max(); /*GRAPH_TEMPLATE=============================================*/ template <typename T> class Edge { public: int srch; // searched?探索状態記録「null = -1」 int to; // 辺の行き先 T cost; // 辺の重み Edge(int t, T w) : srch(-1), to(t), cost(w) {} Edge(int src, int t, T w) : srch(src), to(t), cost(w) {} }; template <typename T> using W_Graph = vector<vector<Edge<T>>>; // 重み付きグラフ using Graph = vector<vector<int>>; // 通常グラフ template <typename T> using Matrix = vector<vector<T>>; // 隣接行列 /*FUNCs=================================================*/ /*MAIN==================================================*/ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); // cin cout 高速化 // MAKING GRAPH int n, m, l; cin >> n >> m >> l; W_Graph<ll> G(n); rep(i, m) { int a, b; ll c; cin >> a >> b >> c; a--; b--; if (c > l) continue; // このケースのルートは通れないので枝刈り G[a].emplace_back(b, c); G[b].emplace_back(a, c); } // MAKING Table with dijkstra using P = pair<ll, ll>; // ★優先度的にcnt,distの順! <charging_cnt, dist> vector<vector<P>> rec(n, vector<P>(n, make_pair(inf, inf))); // recording using TP = pair<P, int>; // <cnt, dist, id> //優先度的にcnt,distの順! priority_queue<TP, vector<TP>, greater<TP>> que; for (int i = 0; i < n; ++i) { rec[i][i] = make_pair(0LL, 0LL); que.emplace(rec[i][i], i); while (que.size()) { auto p = que.top(); que.pop(); ll cnt = p.first.first; ll dist = p.first.second; int id = p.second; if (dist > rec[i][id].second) continue; // やっぱいるね。うん for (auto &edge : G[id]) { ll new_dist = dist + edge.cost; ll new_cnt = cnt; // 補給の処理 if (new_dist > l) { new_dist = edge.cost; new_cnt++; } P next_p = make_pair(new_cnt, new_dist); if (chmin(rec[i][edge.to], next_p)) { que.emplace(rec[i][edge.to], edge.to); } } } } // ANSWER to Query int q; cin >> q; rep(i, q) { int s, t; cin >> s >> t; --s; --t; if (rec[s][t].first == inf) cout << -1 << '\n'; else cout << rec[s][t].first << '\n'; } }
replace
89
90
89
91
TLE
p02889
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() typedef long long ll; typedef pair<ll, ll> P; typedef vector<ll> vec; typedef vector<vec> mat; int main() { ll n, m, l, d[201][201], q, inf = 1e18; cin >> n >> m >> l; int a, b, c; rep(i, n) rep(j, n) d[i][j] = inf; rep(i, m) cin >> a >> b >> c, d[a - 1][b - 1] = c, d[b - 1][a - 1] = c; rep(i, n) d[i][i] = 0; rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); rep(i, n) rep(j, n) { if (i == j) d[i][j] = 0; else if (d[i][j] <= l) d[i][j] = 1; else d[i][j] = inf; } rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); cin >> q; rep(i, q) { int s, t; cin >> s >> t; if (d[s - 1][t - 1] == inf) cout << -1 << "\n"; else cout << d[s - 1][t - 1] - 1 << "\n"; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() typedef long long ll; typedef pair<ll, ll> P; typedef vector<ll> vec; typedef vector<vec> mat; int main() { ll n, m, l, d[301][301], q, inf = 1e18; cin >> n >> m >> l; int a, b, c; rep(i, n) rep(j, n) d[i][j] = inf; rep(i, m) cin >> a >> b >> c, d[a - 1][b - 1] = c, d[b - 1][a - 1] = c; rep(i, n) d[i][i] = 0; rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); rep(i, n) rep(j, n) { if (i == j) d[i][j] = 0; else if (d[i][j] <= l) d[i][j] = 1; else d[i][j] = inf; } rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); cin >> q; rep(i, q) { int s, t; cin >> s >> t; if (d[s - 1][t - 1] == inf) cout << -1 << "\n"; else cout << d[s - 1][t - 1] - 1 << "\n"; } }
replace
9
10
9
10
0
p02889
C++
Runtime Error
#include <algorithm> #include <climits> #include <iostream> #include <vector> using namespace std; static const int MAX = 100; static const long long INFTY = (1LL << 50); int n; long long d[MAX][MAX]; long long d_[MAX][MAX]; void floyd() { for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { if (d[i][k] == INFTY) continue; for (int j = 0; j < n; j++) { if (d[k][j] == INFTY) continue; d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } } void floyd_() { for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { if (d_[i][k] == INFTY) continue; for (int j = 0; j < n; j++) { if (d_[k][j] == INFTY) continue; d_[i][j] = min(d_[i][j], d_[i][k] + d_[k][j]); } } } } int main() { int e, u, v, c, l; cin >> n >> e >> l; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = ((i == j) ? 0 : INFTY); } } for (int i = 0; i < e; i++) { cin >> u >> v >> c; d[u - 1][v - 1] = c; d[v - 1][u - 1] = c; } floyd(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (d[i][j] <= l) { d_[i][j] = 1; } else { d_[i][j] = INFTY; } } } floyd_(); int q, s, t; cin >> q; for (int i = 0; i < q; i++) { cin >> s >> t; if (d_[s - 1][t - 1] == INFTY) { cout << -1 << endl; } else { cout << d_[s - 1][t - 1] - 1 << endl; } } return 0; }
#include <algorithm> #include <climits> #include <iostream> #include <vector> using namespace std; static const int MAX = 300; static const long long INFTY = 9223372036854775807; int n; long long d[MAX][MAX]; long long d_[MAX][MAX]; void floyd() { for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { if (d[i][k] == INFTY) continue; for (int j = 0; j < n; j++) { if (d[k][j] == INFTY) continue; d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } } void floyd_() { for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { if (d_[i][k] == INFTY) continue; for (int j = 0; j < n; j++) { if (d_[k][j] == INFTY) continue; d_[i][j] = min(d_[i][j], d_[i][k] + d_[k][j]); } } } } int main() { int e, u, v, c, l; cin >> n >> e >> l; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = ((i == j) ? 0 : INFTY); } } for (int i = 0; i < e; i++) { cin >> u >> v >> c; d[u - 1][v - 1] = c; d[v - 1][u - 1] = c; } floyd(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (d[i][j] <= l) { d_[i][j] = 1; } else { d_[i][j] = INFTY; } } } floyd_(); int q, s, t; cin >> q; for (int i = 0; i < q; i++) { cin >> s >> t; if (d_[s - 1][t - 1] == INFTY) { cout << -1 << endl; } else { cout << d_[s - 1][t - 1] - 1 << endl; } } return 0; }
replace
6
8
6
8
0
p02889
C++
Time Limit Exceeded
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; #define MOD 1000000007 #define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++) #define REP(i, n) rep(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; const long long INF = 1LL << 60; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string &s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge( from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph &G, ll s, ll t) // O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = INF; } } REP(i, G.size()) { REP(j, G[i].size()) { d[i][G[i][j].to] = G[i][j].cap; } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, vector<int> &order) { // トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { // xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { // xの集合のサイズを返す return -data[root(x)]; } int numSet() { // 集合の数を返す return num; } }; class SumSegTree { private: ll _sum(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return 0; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return s1 + s2; } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) SumSegTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, 0); } // 場所i(0-indexed)にxを足す void add(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] += x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] += x; } } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: ll _find(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return INF; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return min(s1, s2); } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) RmqTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, INF); } // 場所i(0-indexed)をxにする void update(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] = x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } // 区間[a,b)の最小値。ノードk=[l,r)に着目している。 ll find(ll a, ll b) { return _find(a, b, 0, 0, n); } }; // 約数求める //約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } vector<ll> lis_fast(const vector<ll> &a) { // 最長部分増加列 const ll n = a.size(); vector<ll> A(n, INT_MAX); vector<ll> id(n); for (int i = 0; i < n; ++i) { id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i])); A[id[i]] = a[i]; } ll m = *max_element(id.begin(), id.end()); vector<ll> b(m + 1); for (int i = n - 1; i >= 0; --i) if (id[i] == m) b[m--] = a[i]; return b; } bool z_algorithm(string &str, vector<int> &z, ll s) { // s&tを渡してtにsが含まれるかを返す const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } if (z[i] == s) return true; } return false; } bool z_algorithm(string &str, vector<int> &z) { // z[i]==|s|のときstr[i]からsが含まれる const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } } return true; } // ローリングハッシュ // 二分探索で LCP を求める機能つき struct RollingHash { static const int base1 = 1007, base2 = 2009; static const int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; // construct RollingHash(const string &S) { int n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } // get hash of S[left:right] inline pair<long long, long long> get(int l, int r) const { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } // get lcp of S[a:] and T[b:] inline int getLCP(int a, int b) const { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } }; ll mod_pow(ll x, ll n, ll mod) { ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } // nCrとか class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll nCr(ll n, ll r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll n, ll _mod) { mod = _mod; fact.resize(n + 1); fact[0] = 1; REP(i, n) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n + 1); REP(i, n + 1) { inv[i] = mod_inv(fact[i]); } } }; ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } // gcd ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } Matrix mIdentity(ll n) { Matrix A(n, Array(n)); for (int i = 0; i < n; ++i) A[i][i] = 1; return A; } Matrix mMul(const Matrix &A, const Matrix &B) { Matrix C(A.size(), Array(B[0].size())); for (int i = 0; i < C.size(); ++i) for (int j = 0; j < C[i].size(); ++j) for (int k = 0; k < A[i].size(); ++k) (C[i][j] += (A[i][k] % MOD) * (B[k][j] % MOD)) %= MOD; return C; } // O( n^3 log e ) Matrix mPow(const Matrix &A, ll e) { return e == 0 ? mIdentity(A.size()) : e % 2 == 0 ? mPow(mMul(A, A), e / 2) : mMul(A, mPow(A, e - 1)); } template <class T> class RectangleSum { public: vector<vector<T>> sum; T GetSum(int left, int right, int top, int bottom) { //[left, right], [top, bottom] T res = sum[bottom][right]; if (left > 0) res -= sum[bottom][left - 1]; if (top > 0) res -= sum[top - 1][right]; if (left > 0 && top > 0) res += sum[top - 1][left - 1]; return res; } RectangleSum(const vector<vector<T>> &s, int h, int w) { sum.resize(h); for (int i = 0; i < h; i++) sum[i].resize(w, 0); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { sum[y][x] = s[y][x]; if (y > 0) sum[y][x] += sum[y - 1][x]; if (x > 0) sum[y][x] += sum[y][x - 1]; if (y > 0 && x > 0) sum[y][x] -= sum[y - 1][x - 1]; } } } }; // NTT ll _garner(Array &xs, Array &mods) { int M = xs.size(); Array coeffs(M, 1), constants(M, 0); for (int i = 0; i < M - 1; ++i) { ll mod_i = mods[i]; // coffs[i] * v + constants[i] == mr[i].val (mod mr[i].first) を解く ll v = (xs[i] - constants[i] + mod_i) % mod_i; v = (v * mod_pow(coeffs[i], mod_i - 2, mod_i)) % mod_i; for (int j = i + 1; j < M; j++) { ll mod_j = mods[j]; constants[j] = (constants[j] + coeffs[j] * v) % mod_j; coeffs[j] = (coeffs[j] * mod_i) % mod_j; } } return constants.back(); } template <typename T> inline void bit_reverse(vector<T> &a) { int n = a.size(); int i = 0; for (int j = 1; j < n - 1; ++j) { for (int k = n >> 1; k > (i ^= k); k >>= 1) ; if (j < i) swap(a[i], a[j]); } } template <long long mod, long long primitive_root> class NTT { public: long long get_mod() { return mod; } void _ntt(vector<long long> &a, int sign) { const int n = a.size(); assert((n ^ (n & -n)) == 0); // n = 2^k const long long g = primitive_root; // g is primitive root of mod long long tmp = (mod - 1) * mod_pow(n, mod - 2, mod) % mod; // -1/n long long h = mod_pow(g, tmp, mod); // ^n√g if (sign == -1) h = mod_pow(h, mod - 2, mod); bit_reverse(a); for (int m = 1; m < n; m <<= 1) { const int m2 = 2 * m; long long _base = mod_pow(h, n / m2, mod); long long _w = 1; for (int x = 0; x < m; ++x) { for (int s = x; s < n; s += m2) { long long u = a[s]; long long d = (a[s + m] * _w) % mod; a[s] = (u + d) % mod; a[s + m] = (u - d + mod) % mod; } _w = (_w * _base) % mod; } } } void ntt(vector<long long> &input) { _ntt(input, 1); } void intt(vector<long long> &input) { _ntt(input, -1); const long long n_inv = mod_pow(input.size(), mod - 2, mod); for (auto &x : input) x = (x * n_inv) % mod; } // 畳み込み演算を行う vector<long long> convolution(const vector<long long> &a, const vector<long long> &b) { int result_size = a.size() + b.size() - 1; int n = 1; while (n < result_size) n <<= 1; vector<long long> _a = a, _b = b; _a.resize(n, 0); _b.resize(n, 0); ntt(_a); ntt(_b); for (int i = 0; i < n; ++i) _a[i] = (_a[i] * _b[i]) % mod; intt(_a); _a.resize(result_size); return _a; } }; vector<long long> convolution_ntt(vector<long long> &a, vector<long long> &b, long long mod = 1224736769LL) { for (auto &x : a) x %= mod; for (auto &x : b) x %= mod; ll maxval = max(a.size(), b.size()) * *max_element(a.begin(), a.end()) * *max_element(b.begin(), b.end()); if (maxval < 1224736769) { NTT<1224736769, 3> ntt3; return ntt3.convolution(a, b); } NTT<167772161, 3> ntt1; NTT<469762049, 3> ntt2; NTT<1224736769, 3> ntt3; vector<long long> x1 = ntt1.convolution(a, b); vector<long long> x2 = ntt2.convolution(a, b); vector<long long> x3 = ntt3.convolution(a, b); vector<long long> ret(x1.size()); vector<long long> mods{167772161, 469762049, 1224736769, mod}; for (int i = 0; i < x1.size(); ++i) { vector<long long> xs{x1[i], x2[i], x3[i], 0}; ret[i] = _garner(xs, mods); } return ret; } int popcount3(int x) { x = (x & 0x55555555) + (x >> 1 & 0x55555555); x = (x & 0x33333333) + (x >> 2 & 0x33333333); x = (x & 0x0F0F0F0F) + (x >> 4 & 0x0F0F0F0F); x = (x & 0x00FF00FF) + (x >> 8 & 0x00FF00FF); x = (x & 0x0000FFFF) + (x >> 16 & 0x0000FFFF); return x; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; ll m, l; cin >> m >> l; Graph graph(n); REP(i, m) { ll a, b, c; cin >> a >> b >> c; a--; b--; add_edge(graph, a, b, c, true, c); } Matrix ans(n, Array(n, INF)); REP(i, n) { priority_queue<pair<ll, P>, vector<pair<ll, P>>, greater<pair<ll, P>>> que; que.push(make_pair(0, make_pair(i, l))); Array cnt(n, INF); Array rest(n, 0); rest[i] = l; cnt[i] = 0; while (que.size()) { auto temp = que.top(); que.pop(); ll v = temp.second.first; ll lll = temp.second.second; ll c = temp.first; if (cnt[v] < c) continue; REP(i, graph[v].size()) { ll to = graph[v][i].to; ll cap = graph[v][i].cap; if (cap <= lll && cnt[to] > c) { cnt[to] = c; rest[to] = lll - cap; que.push(make_pair(c, make_pair(to, lll - cap))); } else if (cap <= lll && cnt[to] == c && rest[to] < lll - cap) { rest[to] = lll - cap; que.push(make_pair(c, make_pair(to, lll - cap))); } else if (cap <= l && cnt[to] > c + 1) { cnt[to] = c + 1; rest[to] = l - cap; que.push(make_pair(c + 1, make_pair(to, l - cap))); } else if (cap <= l && cnt[to] == c + 1 && rest[to] < l - cap) { rest[to] = l - cap; que.push(make_pair(c + 1, make_pair(to, l - cap))); } } } swap(ans[i], cnt); } ll q; cin >> q; REP(i, q) { ll s, t; cin >> s >> t; s--; t--; if (ans[s][t] == INF) cout << -1 << endl; else cout << ans[s][t] << endl; } return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; #define MOD 1000000007 #define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++) #define REP(i, n) rep(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; const long long INF = 1LL << 60; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string &s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge( from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph &G, ll s, ll t) // O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = INF; } } REP(i, G.size()) { REP(j, G[i].size()) { d[i][G[i][j].to] = G[i][j].cap; } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, vector<int> &order) { // トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { // xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { // xの集合のサイズを返す return -data[root(x)]; } int numSet() { // 集合の数を返す return num; } }; class SumSegTree { private: ll _sum(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return 0; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return s1 + s2; } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) SumSegTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, 0); } // 場所i(0-indexed)にxを足す void add(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] += x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] += x; } } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: ll _find(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return INF; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return min(s1, s2); } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) RmqTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, INF); } // 場所i(0-indexed)をxにする void update(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] = x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } // 区間[a,b)の最小値。ノードk=[l,r)に着目している。 ll find(ll a, ll b) { return _find(a, b, 0, 0, n); } }; // 約数求める //約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } vector<ll> lis_fast(const vector<ll> &a) { // 最長部分増加列 const ll n = a.size(); vector<ll> A(n, INT_MAX); vector<ll> id(n); for (int i = 0; i < n; ++i) { id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i])); A[id[i]] = a[i]; } ll m = *max_element(id.begin(), id.end()); vector<ll> b(m + 1); for (int i = n - 1; i >= 0; --i) if (id[i] == m) b[m--] = a[i]; return b; } bool z_algorithm(string &str, vector<int> &z, ll s) { // s&tを渡してtにsが含まれるかを返す const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } if (z[i] == s) return true; } return false; } bool z_algorithm(string &str, vector<int> &z) { // z[i]==|s|のときstr[i]からsが含まれる const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } } return true; } // ローリングハッシュ // 二分探索で LCP を求める機能つき struct RollingHash { static const int base1 = 1007, base2 = 2009; static const int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; // construct RollingHash(const string &S) { int n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } // get hash of S[left:right] inline pair<long long, long long> get(int l, int r) const { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } // get lcp of S[a:] and T[b:] inline int getLCP(int a, int b) const { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } }; ll mod_pow(ll x, ll n, ll mod) { ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } // nCrとか class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll nCr(ll n, ll r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll n, ll _mod) { mod = _mod; fact.resize(n + 1); fact[0] = 1; REP(i, n) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n + 1); REP(i, n + 1) { inv[i] = mod_inv(fact[i]); } } }; ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } // gcd ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } Matrix mIdentity(ll n) { Matrix A(n, Array(n)); for (int i = 0; i < n; ++i) A[i][i] = 1; return A; } Matrix mMul(const Matrix &A, const Matrix &B) { Matrix C(A.size(), Array(B[0].size())); for (int i = 0; i < C.size(); ++i) for (int j = 0; j < C[i].size(); ++j) for (int k = 0; k < A[i].size(); ++k) (C[i][j] += (A[i][k] % MOD) * (B[k][j] % MOD)) %= MOD; return C; } // O( n^3 log e ) Matrix mPow(const Matrix &A, ll e) { return e == 0 ? mIdentity(A.size()) : e % 2 == 0 ? mPow(mMul(A, A), e / 2) : mMul(A, mPow(A, e - 1)); } template <class T> class RectangleSum { public: vector<vector<T>> sum; T GetSum(int left, int right, int top, int bottom) { //[left, right], [top, bottom] T res = sum[bottom][right]; if (left > 0) res -= sum[bottom][left - 1]; if (top > 0) res -= sum[top - 1][right]; if (left > 0 && top > 0) res += sum[top - 1][left - 1]; return res; } RectangleSum(const vector<vector<T>> &s, int h, int w) { sum.resize(h); for (int i = 0; i < h; i++) sum[i].resize(w, 0); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { sum[y][x] = s[y][x]; if (y > 0) sum[y][x] += sum[y - 1][x]; if (x > 0) sum[y][x] += sum[y][x - 1]; if (y > 0 && x > 0) sum[y][x] -= sum[y - 1][x - 1]; } } } }; // NTT ll _garner(Array &xs, Array &mods) { int M = xs.size(); Array coeffs(M, 1), constants(M, 0); for (int i = 0; i < M - 1; ++i) { ll mod_i = mods[i]; // coffs[i] * v + constants[i] == mr[i].val (mod mr[i].first) を解く ll v = (xs[i] - constants[i] + mod_i) % mod_i; v = (v * mod_pow(coeffs[i], mod_i - 2, mod_i)) % mod_i; for (int j = i + 1; j < M; j++) { ll mod_j = mods[j]; constants[j] = (constants[j] + coeffs[j] * v) % mod_j; coeffs[j] = (coeffs[j] * mod_i) % mod_j; } } return constants.back(); } template <typename T> inline void bit_reverse(vector<T> &a) { int n = a.size(); int i = 0; for (int j = 1; j < n - 1; ++j) { for (int k = n >> 1; k > (i ^= k); k >>= 1) ; if (j < i) swap(a[i], a[j]); } } template <long long mod, long long primitive_root> class NTT { public: long long get_mod() { return mod; } void _ntt(vector<long long> &a, int sign) { const int n = a.size(); assert((n ^ (n & -n)) == 0); // n = 2^k const long long g = primitive_root; // g is primitive root of mod long long tmp = (mod - 1) * mod_pow(n, mod - 2, mod) % mod; // -1/n long long h = mod_pow(g, tmp, mod); // ^n√g if (sign == -1) h = mod_pow(h, mod - 2, mod); bit_reverse(a); for (int m = 1; m < n; m <<= 1) { const int m2 = 2 * m; long long _base = mod_pow(h, n / m2, mod); long long _w = 1; for (int x = 0; x < m; ++x) { for (int s = x; s < n; s += m2) { long long u = a[s]; long long d = (a[s + m] * _w) % mod; a[s] = (u + d) % mod; a[s + m] = (u - d + mod) % mod; } _w = (_w * _base) % mod; } } } void ntt(vector<long long> &input) { _ntt(input, 1); } void intt(vector<long long> &input) { _ntt(input, -1); const long long n_inv = mod_pow(input.size(), mod - 2, mod); for (auto &x : input) x = (x * n_inv) % mod; } // 畳み込み演算を行う vector<long long> convolution(const vector<long long> &a, const vector<long long> &b) { int result_size = a.size() + b.size() - 1; int n = 1; while (n < result_size) n <<= 1; vector<long long> _a = a, _b = b; _a.resize(n, 0); _b.resize(n, 0); ntt(_a); ntt(_b); for (int i = 0; i < n; ++i) _a[i] = (_a[i] * _b[i]) % mod; intt(_a); _a.resize(result_size); return _a; } }; vector<long long> convolution_ntt(vector<long long> &a, vector<long long> &b, long long mod = 1224736769LL) { for (auto &x : a) x %= mod; for (auto &x : b) x %= mod; ll maxval = max(a.size(), b.size()) * *max_element(a.begin(), a.end()) * *max_element(b.begin(), b.end()); if (maxval < 1224736769) { NTT<1224736769, 3> ntt3; return ntt3.convolution(a, b); } NTT<167772161, 3> ntt1; NTT<469762049, 3> ntt2; NTT<1224736769, 3> ntt3; vector<long long> x1 = ntt1.convolution(a, b); vector<long long> x2 = ntt2.convolution(a, b); vector<long long> x3 = ntt3.convolution(a, b); vector<long long> ret(x1.size()); vector<long long> mods{167772161, 469762049, 1224736769, mod}; for (int i = 0; i < x1.size(); ++i) { vector<long long> xs{x1[i], x2[i], x3[i], 0}; ret[i] = _garner(xs, mods); } return ret; } int popcount3(int x) { x = (x & 0x55555555) + (x >> 1 & 0x55555555); x = (x & 0x33333333) + (x >> 2 & 0x33333333); x = (x & 0x0F0F0F0F) + (x >> 4 & 0x0F0F0F0F); x = (x & 0x00FF00FF) + (x >> 8 & 0x00FF00FF); x = (x & 0x0000FFFF) + (x >> 16 & 0x0000FFFF); return x; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; ll m, l; cin >> m >> l; Graph graph(n); REP(i, m) { ll a, b, c; cin >> a >> b >> c; a--; b--; add_edge(graph, a, b, c, true, c); } Matrix ans(n, Array(n, INF)); REP(i, n) { priority_queue<pair<ll, P>, vector<pair<ll, P>>, greater<pair<ll, P>>> que; que.push(make_pair(0, make_pair(i, l))); Array cnt(n, INF); Array rest(n, 0); rest[i] = l; cnt[i] = 0; while (que.size()) { auto temp = que.top(); que.pop(); ll v = temp.second.first; ll lll = temp.second.second; ll c = temp.first; if (cnt[v] < c) continue; if (cnt[v] == c && rest[v] > lll) continue; REP(i, graph[v].size()) { ll to = graph[v][i].to; ll cap = graph[v][i].cap; if (cap <= lll && cnt[to] > c) { cnt[to] = c; rest[to] = lll - cap; que.push(make_pair(c, make_pair(to, lll - cap))); } else if (cap <= lll && cnt[to] == c && rest[to] < lll - cap) { rest[to] = lll - cap; que.push(make_pair(c, make_pair(to, lll - cap))); } else if (cap <= l && cnt[to] > c + 1) { cnt[to] = c + 1; rest[to] = l - cap; que.push(make_pair(c + 1, make_pair(to, l - cap))); } else if (cap <= l && cnt[to] == c + 1 && rest[to] < l - cap) { rest[to] = l - cap; que.push(make_pair(c + 1, make_pair(to, l - cap))); } } } swap(ans[i], cnt); } ll q; cin >> q; REP(i, q) { ll s, t; cin >> s >> t; s--; t--; if (ans[s][t] == INF) cout << -1 << endl; else cout << ans[s][t] << endl; } return 0; }
replace
760
761
760
762
TLE
p02889
C++
Runtime Error
// C++ 14 #include <algorithm> #include <iostream> #include <list> #include <math.h> #include <queue> #include <stack> #include <vector> #define ll long long #define Int int #define loop(x, start, end) for (Int x = start; x < end; x++) #define loopdown(x, start, end) for (int x = start; x > end; x--) #define span(a, x, y) a.begin() + x, a.begin() + y #define span_all(a) a.begin(), a.end() #define len(x) (x.size()) #define last(x) (*(x.end() - 1)) using namespace std; #define MAX_N 301 #define MAX_Q ((301 * 300) / 2) #define MAX_C 1000000001 Int N, M, Q; vector<Int> S(MAX_Q, -1), T(MAX_Q, -1); ll L; ll G[MAX_N][MAX_N]; ll COUNT[MAX_N][MAX_N]; void apsp(ll g[MAX_N][MAX_N]) { loop(k, 0, N) { loop(u, 0, N) { if (g[u][k] == MAX_C) continue; loop(v, 0, N) { if (g[k][v] == MAX_C) continue; g[u][v] = min(g[u][v], g[u][k] + g[k][v]); } } } } void dump(ll g[MAX_N][MAX_N]) { loop(n, 0, N) { loop(m, 0, N) { cout << g[n][m] << " "; } cout << endl; } } void solve() { apsp(G); loop(n, 0, N) { loop(m, 0, N) { if (G[n][m] <= L) COUNT[n][m] = 1; } } apsp(COUNT); loop(q, 0, Q) { if (COUNT[S[q]][T[q]] == MAX_C) cout << -1 << endl; else cout << COUNT[S[q]][T[q]] - 1 << endl; } } int main() { Int a, b; ll c; cin >> N >> M >> L; loop(n, 0, N) { loop(m, 0, N) { G[n][m] = COUNT[n][m] = (n == m) ? 0 : MAX_C; } } loop(m, 0, M) { cin >> a >> b >> c; G[a - 1][b - 1] = c; G[b - 1][a - 1] = c; } cin >> Q; loop(q, 0, Q) { cin >> S[q] >> T[q]; S[q]--; T[q]--; } solve(); }
// C++ 14 #include <algorithm> #include <iostream> #include <list> #include <math.h> #include <queue> #include <stack> #include <vector> #define ll long long #define Int int #define loop(x, start, end) for (Int x = start; x < end; x++) #define loopdown(x, start, end) for (int x = start; x > end; x--) #define span(a, x, y) a.begin() + x, a.begin() + y #define span_all(a) a.begin(), a.end() #define len(x) (x.size()) #define last(x) (*(x.end() - 1)) using namespace std; #define MAX_N 301 #define MAX_Q (301 * 300) #define MAX_C 1000000001 Int N, M, Q; vector<Int> S(MAX_Q, -1), T(MAX_Q, -1); ll L; ll G[MAX_N][MAX_N]; ll COUNT[MAX_N][MAX_N]; void apsp(ll g[MAX_N][MAX_N]) { loop(k, 0, N) { loop(u, 0, N) { if (g[u][k] == MAX_C) continue; loop(v, 0, N) { if (g[k][v] == MAX_C) continue; g[u][v] = min(g[u][v], g[u][k] + g[k][v]); } } } } void dump(ll g[MAX_N][MAX_N]) { loop(n, 0, N) { loop(m, 0, N) { cout << g[n][m] << " "; } cout << endl; } } void solve() { apsp(G); loop(n, 0, N) { loop(m, 0, N) { if (G[n][m] <= L) COUNT[n][m] = 1; } } apsp(COUNT); loop(q, 0, Q) { if (COUNT[S[q]][T[q]] == MAX_C) cout << -1 << endl; else cout << COUNT[S[q]][T[q]] - 1 << endl; } } int main() { Int a, b; ll c; cin >> N >> M >> L; loop(n, 0, N) { loop(m, 0, N) { G[n][m] = COUNT[n][m] = (n == m) ? 0 : MAX_C; } } loop(m, 0, M) { cin >> a >> b >> c; G[a - 1][b - 1] = c; G[b - 1][a - 1] = c; } cin >> Q; loop(q, 0, Q) { cin >> S[q] >> T[q]; S[q]--; T[q]--; } solve(); }
replace
21
22
21
22
0
p02889
C++
Time Limit Exceeded
#if 1 #include <bits/stdc++.h> using namespace std; int n, m, l; int d[305][305]; vector<pair<int, int>> v[305]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> l; int x, y, z; for (int i = 1; i <= m; i++) { cin >> x >> y >> z; v[x].push_back({y, z}); v[y].push_back({x, z}); } priority_queue<pair<pair<int, int>, int>> q; int c[305]; for (int i = 1; i <= n; i++) { q.push({{0, 0}, i}); memset(c, 0, sizeof(c)); while (!q.empty()) { x = -q.top().first.first; y = -q.top().first.second; z = q.top().second; q.pop(); if (c[z]) continue; c[z] = 1; d[i][z] = x; for (auto k : v[z]) { if (k.second > l) continue; if (y + k.second > l) { q.push({{-x - 1, -k.second}, k.first}); } else q.push({{-x, -y - k.second}, k.first}); } } for (int j = 1; j <= n; j++) { if (c[j] == 0) d[i][j] = -1; } } int qq; cin >> qq; while (qq--) { cin >> x >> y; cout << d[x][y] << '\n'; } } #endif
#if 1 #include <bits/stdc++.h> using namespace std; int n, m, l; int d[305][305]; vector<pair<int, int>> v[305]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> l; int x, y, z; for (int i = 1; i <= m; i++) { cin >> x >> y >> z; v[x].push_back({y, z}); v[y].push_back({x, z}); } priority_queue<pair<pair<int, int>, int>> q; int c[305]; for (int i = 1; i <= n; i++) { q.push({{0, 0}, i}); memset(c, 0, sizeof(c)); while (!q.empty()) { x = -q.top().first.first; y = -q.top().first.second; z = q.top().second; q.pop(); if (c[z]) continue; c[z] = 1; d[i][z] = x; for (auto k : v[z]) { if (c[k.first]) continue; if (k.second > l) continue; if (y + k.second > l) { q.push({{-x - 1, -k.second}, k.first}); } else q.push({{-x, -y - k.second}, k.first}); } } for (int j = 1; j <= n; j++) { if (c[j] == 0) d[i][j] = -1; } } int qq; cin >> qq; while (qq--) { cin >> x >> y; cout << d[x][y] << '\n'; } } #endif
insert
33
33
33
35
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main(void) { // 5+ 1854- int N, M; long long L; cin >> N >> M >> L; vector<vector<int>> Load_to(N, vector<int>()); vector<vector<int>> Load_cost(N, vector<int>()); for (int i = 0; i < M; i++) { int a, b, c; cin >> a >> b >> c; a--; b--; Load_to[a].push_back(b); Load_cost[a].push_back(c); Load_to[b].push_back(a); Load_cost[b].push_back(c); } // Map vector<vector<int>> CostMap(N, vector<int>(N, -1)); // 一回で行ける距離を判定 for (int i = 0; i < N; i++) { priority_queue<pair<int, int>> que; que.push(make_pair(L, i)); while (true) { if (que.empty()) { break; } pair<int, int> S = que.top(); que.pop(); int tank = S.first; int from = S.second; if (CostMap[i][from] == 0) { continue; } CostMap[i][from] = 0; for (int j = 0; j < Load_to[from].size(); j++) { int to = Load_to[from][j]; int rest = tank - Load_cost[from][j]; if (Load_cost[from][j] > tank) { continue; } if (tank != 0) { que.push(make_pair(rest, to)); } } } } int t = 0; while (true) { bool check = false; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i == j) { CostMap[i][j] = 0; continue; } if (CostMap[i][j] == t) { for (int k = 0; k < N; k++) { if (CostMap[j][k] != -1) { int cost = CostMap[i][j] + CostMap[j][k] + 1; if (CostMap[i][k] == -1 || (CostMap[i][k] != -1 && CostMap[i][k] > cost)) { CostMap[i][k] = cost; check = true; } } } } } } if (!check) { break; } t++; } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int s, t; cin >> s >> t; s--; t--; cout << CostMap[s][t] << endl; } /* debacker for (int i = 0; i < N; i++){ for(int j=0;j<N;j++){ cout<<CostMap[i][j] <<" "; } cout<<endl; } */ return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { // 5+ 1854- int N, M; long long L; cin >> N >> M >> L; vector<vector<int>> Load_to(N, vector<int>()); vector<vector<int>> Load_cost(N, vector<int>()); for (int i = 0; i < M; i++) { int a, b, c; cin >> a >> b >> c; a--; b--; Load_to[a].push_back(b); Load_cost[a].push_back(c); Load_to[b].push_back(a); Load_cost[b].push_back(c); } // Map vector<vector<int>> CostMap(N, vector<int>(N, -1)); // 一回で行ける距離を判定 for (int i = 0; i < N; i++) { priority_queue<pair<int, int>> que; que.push(make_pair(L, i)); while (true) { if (que.empty()) { break; } pair<int, int> S = que.top(); que.pop(); int tank = S.first; int from = S.second; if (CostMap[i][from] == 0) { continue; } CostMap[i][from] = 0; for (int j = 0; j < Load_to[from].size(); j++) { int to = Load_to[from][j]; int rest = tank - Load_cost[from][j]; if (Load_cost[from][j] > tank || CostMap[i][to] == 0) { continue; } if (tank != 0) { que.push(make_pair(rest, to)); } } } } int t = 0; while (true) { bool check = false; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i == j) { CostMap[i][j] = 0; continue; } if (CostMap[i][j] == t) { for (int k = 0; k < N; k++) { if (CostMap[j][k] != -1) { int cost = CostMap[i][j] + CostMap[j][k] + 1; if (CostMap[i][k] == -1 || (CostMap[i][k] != -1 && CostMap[i][k] > cost)) { CostMap[i][k] = cost; check = true; } } } } } } if (!check) { break; } t++; } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int s, t; cin >> s >> t; s--; t--; cout << CostMap[s][t] << endl; } /* debacker for (int i = 0; i < N; i++){ for(int j=0;j<N;j++){ cout<<CostMap[i][j] <<" "; } cout<<endl; } */ return 0; }
replace
48
49
48
49
TLE
p02889
C++
Runtime Error
#include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; const long MAXV = 101; const long MAXD = (long)1e12; long V; void warshall_floyd(vector<vector<long>> &d) { for (long k = 1; k <= V; k++) { for (long i = 1; i <= V; i++) { for (long j = 1; j <= V; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } return; } int main() { long N, M, L, Q; cin >> N >> M >> L; vector<vector<long>> d(MAXV, vector<long>(MAXV, MAXD)); vector<vector<long>> f(MAXV, vector<long>(MAXV, MAXD)); V = N; vector<long> ans; for (long i = 0; i < M; i++) { long a, b, c; cin >> a >> b >> c; d[a][b] = c; d[b][a] = c; } warshall_floyd(d); for (long i = 1; i <= V; i++) { for (long j = 1; j <= V; j++) { if (d[i][j] <= L) { f[i][j] = 1; f[j][i] = 1; } } } warshall_floyd(f); cin >> Q; vector<long> s(Q), t(Q), r(Q); for (long i = 0; i < Q; i++) { long result; cin >> s[i] >> t[i]; /* if( d[s[i]][t[i]]<MAXD ){ r[i] = d[s[i]][t[i]]; }else{ r[i] = -1; } */ if (f[s[i]][t[i]] >= MAXD) { result = -1; ans.push_back(result); } else { result = f[s[i]][t[i]] - 1; ans.push_back(result); } } for (long i = 0; i < Q; i++) { // cout << "i s t d[s][t] r[i]" << i << " " << s[i] << " " << t[i] << " " << // r[i] << endl; cout << ans[i] << endl; } return 0; }
#include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; const long MAXV = 301; const long MAXD = (long)1e12; long V; void warshall_floyd(vector<vector<long>> &d) { for (long k = 1; k <= V; k++) { for (long i = 1; i <= V; i++) { for (long j = 1; j <= V; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } return; } int main() { long N, M, L, Q; cin >> N >> M >> L; vector<vector<long>> d(MAXV, vector<long>(MAXV, MAXD)); vector<vector<long>> f(MAXV, vector<long>(MAXV, MAXD)); V = N; vector<long> ans; for (long i = 0; i < M; i++) { long a, b, c; cin >> a >> b >> c; d[a][b] = c; d[b][a] = c; } warshall_floyd(d); for (long i = 1; i <= V; i++) { for (long j = 1; j <= V; j++) { if (d[i][j] <= L) { f[i][j] = 1; f[j][i] = 1; } } } warshall_floyd(f); cin >> Q; vector<long> s(Q), t(Q), r(Q); for (long i = 0; i < Q; i++) { long result; cin >> s[i] >> t[i]; /* if( d[s[i]][t[i]]<MAXD ){ r[i] = d[s[i]][t[i]]; }else{ r[i] = -1; } */ if (f[s[i]][t[i]] >= MAXD) { result = -1; ans.push_back(result); } else { result = f[s[i]][t[i]] - 1; ans.push_back(result); } } for (long i = 0; i < Q; i++) { // cout << "i s t d[s][t] r[i]" << i << " " << s[i] << " " << t[i] << " " << // r[i] << endl; cout << ans[i] << endl; } return 0; }
replace
6
7
6
7
0
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long int ll; // template {{{ 0 // using {{{ 1 using ll = long long int; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vii = vector<pii>; using vll = vector<pll>; // }}} 1 // definition {{{ 1 // scaning {{{ 2 #define Scd(x) scanf("%d", &x) #define Scd2(x, y) scanf("%d%d", &x, &y) #define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define Scll(x) scanf("%lld", &x) #define Scll2(x, y) scanf("%lld%lld", &x, &y) #define Scll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z) #define Scc(c) scanf("%c", &c); #define Scs(s) scanf("%s", s); #define Scstr(s) scanf("%s", &s); // }}} 2 // constants {{{ 2 #define EPS (1e-7) #define INF (2e9) #define PI (acos(-1)) // }}} 2 // systems {{{ 2 #define Repe(x, y, z) for (ll x = z; x < y; x++) #define Rep(x, y) Repe(x, y, 0) #define RRepe(x, y, z) for (ll x = y - z - 1; x >= 0; x--) #define RRep(x, y) RRepe(x, y, 0) // }}} 2 // output {{{ 2 #define YesNo(a) (a) ? printf("Yes\n") : printf("No\n") #define YESNO(a) (a) ? printf("YES\n") : printf("NO\n") // }}} 2 // }}} 1 // input {{{ 1 // }}} 1 // }}} 0 struct edge { int to, dis; }; int main() { int N, M, L; Scd3(N, M, L); vector<vector<edge>> e(N); vector<vll> t(N, vll(N, {INF, INF})); // t[kyuyu][used] Rep(i, N) t[i][i] = {0, 0}; int a, b, c; Rep(i, M) { Scd3(a, b, c); a--, b--; if (c > L) continue; e[a].push_back({b, c}); e[b].push_back({a, c}); } priority_queue<pair<pll, int>> q; Rep(i, N) { q.push({{0, 0}, i}); while (q.size()) { int from = q.top().second; pll dist = q.top().first; q.pop(); if (t[i][from] < dist) continue; for (edge tt : e[from]) { int to = tt.to; int di = tt.dis; pll old = t[i][to]; pll ne; if (dist.second + di > L) { ne = {dist.first + 1, di}; } else { ne = {dist.first, dist.second + di}; } if (ne < old) { t[i][to] = ne; q.push({ne, to}); } } } } // Rep(i,N) Rep(j,N){ // printf ("%lld,%lld%c", t[i][j].first, t[i][j].second, j == N-1 ? '\n' : // ' ' ); // } int Q; Scd(Q); Rep(i, Q) { Scd2(a, b); a--, b--; printf("%lld\n", t[a][b].first == INF ? -1 : t[a][b].first); } // Rep(k,N) Rep(i,N) Rep(j,N){ // d[i][j] = min( d[i][j] , d[i][k] + d[k][j] ); // } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; // template {{{ 0 // using {{{ 1 using ll = long long int; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vii = vector<pii>; using vll = vector<pll>; // }}} 1 // definition {{{ 1 // scaning {{{ 2 #define Scd(x) scanf("%d", &x) #define Scd2(x, y) scanf("%d%d", &x, &y) #define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define Scll(x) scanf("%lld", &x) #define Scll2(x, y) scanf("%lld%lld", &x, &y) #define Scll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z) #define Scc(c) scanf("%c", &c); #define Scs(s) scanf("%s", s); #define Scstr(s) scanf("%s", &s); // }}} 2 // constants {{{ 2 #define EPS (1e-7) #define INF (2e9) #define PI (acos(-1)) // }}} 2 // systems {{{ 2 #define Repe(x, y, z) for (ll x = z; x < y; x++) #define Rep(x, y) Repe(x, y, 0) #define RRepe(x, y, z) for (ll x = y - z - 1; x >= 0; x--) #define RRep(x, y) RRepe(x, y, 0) // }}} 2 // output {{{ 2 #define YesNo(a) (a) ? printf("Yes\n") : printf("No\n") #define YESNO(a) (a) ? printf("YES\n") : printf("NO\n") // }}} 2 // }}} 1 // input {{{ 1 // }}} 1 // }}} 0 struct edge { int to, dis; }; int main() { int N, M, L; Scd3(N, M, L); vector<vector<edge>> e(N); vector<vll> t(N, vll(N, {INF, INF})); // t[kyuyu][used] Rep(i, N) t[i][i] = {0, 0}; int a, b, c; Rep(i, M) { Scd3(a, b, c); a--, b--; if (c > L) continue; e[a].push_back({b, c}); e[b].push_back({a, c}); } priority_queue<pair<pll, int>, vector<pair<pll, int>>, greater<pair<pll, int>>> q; Rep(i, N) { q.push({{0, 0}, i}); while (q.size()) { int from = q.top().second; pll dist = q.top().first; q.pop(); if (t[i][from] < dist) continue; for (edge tt : e[from]) { int to = tt.to; int di = tt.dis; pll old = t[i][to]; pll ne; if (dist.second + di > L) { ne = {dist.first + 1, di}; } else { ne = {dist.first, dist.second + di}; } if (ne < old) { t[i][to] = ne; q.push({ne, to}); } } } } // Rep(i,N) Rep(j,N){ // printf ("%lld,%lld%c", t[i][j].first, t[i][j].second, j == N-1 ? '\n' : // ' ' ); // } int Q; Scd(Q); Rep(i, Q) { Scd2(a, b); a--, b--; printf("%lld\n", t[a][b].first == INF ? -1 : t[a][b].first); } // Rep(k,N) Rep(i,N) Rep(j,N){ // d[i][j] = min( d[i][j] , d[i][k] + d[k][j] ); // } return 0; }
replace
70
71
70
73
TLE
p02890
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef long double ld; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; #define REP(i, m, n) for (ll i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define F first #define S second constexpr char ln = '\n'; const int mx = 100010; const ll mod = 1e9 + 7; int main() { ll n; cin >> n; vector<int> a(n); map<int, int> mp; rep(i, n) { cin >> a[i]; mp[a[i]]++; } vector<ll> b; for (auto it : mp) { b.emplace_back(it.S); } sort(b.begin(), b.end()); ll m = b.size(); vector<ll> r(n + 1, 0); rep(i, n) { r[i + 1] = r[i] + b[i]; } REP(k, 1, n + 1) { ll ok = -1, ng = n + 1, mid = (ok + ng) / 2; while (ng - ok > 1) { mid = (ok + ng) / 2; ll pos = lower_bound(b.begin(), b.end(), mid) - b.begin(); ll cnt = r[pos] + mid * (m - pos); if (cnt >= mid * k) { ok = mid; } else { ng = mid; } } ll ans = ok; cout << ans << ln; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef long double ld; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; #define REP(i, m, n) for (ll i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define F first #define S second constexpr char ln = '\n'; const int mx = 100010; const ll mod = 1e9 + 7; int main() { ll n; cin >> n; vector<int> a(n); map<int, int> mp; rep(i, n) { cin >> a[i]; mp[a[i]]++; } vector<ll> b; for (auto it : mp) { b.emplace_back(it.S); } sort(b.begin(), b.end()); ll m = b.size(); vector<ll> r(n + 1, 0); rep(i, n) { r[i + 1] = r[i] + (i < m ? b[i] : 0); } REP(k, 1, n + 1) { ll ok = -1, ng = n + 1, mid = (ok + ng) / 2; while (ng - ok > 1) { mid = (ok + ng) / 2; ll pos = lower_bound(b.begin(), b.end(), mid) - b.begin(); ll cnt = r[pos] + mid * (m - pos); if (cnt >= mid * k) { ok = mid; } else { ng = mid; } } ll ans = ok; cout << ans << ln; } return 0; }
replace
33
34
33
34
0
p02890
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; #define int long long int MOD = 1000000007; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> A(N); map<int, int> mp; for (int i = 0; i < N; i++) { cin >> A[i]; mp[A[i]]++; } A.clear(); for (auto m : mp) { A.push_back(m.second); } while (A.size() < N) A.push_back(0); sort(A.begin(), A.end()); int cur = 0; int sum = 0; vector<int> res(N + 1, 0); for (int i = 1; i <= N; i++) { while (A[cur] < i) { sum += A[cur]; cur++; } int t = i * (N - cur) + sum; // cerr << i << " " << t / i << endl; res[t / i] = i; } for (int i = N - 1; i > 0; i--) { res[i] = max(res[i], res[i + 1]); } for (int i = 0; i < N; i++) { cout << res[i + 1] << endl; } }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; #define int long long int MOD = 1000000007; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> A(N); map<int, int> mp; for (int i = 0; i < N; i++) { cin >> A[i]; mp[A[i]]++; } A.clear(); for (auto m : mp) { A.push_back(m.second); } while (A.size() < N) A.push_back(0); sort(A.begin(), A.end()); int cur = 0; int sum = 0; vector<int> res(N + 1, 0); for (int i = 1; i <= N; i++) { while (cur < N && A[cur] < i) { sum += A[cur]; cur++; } int t = i * (N - cur) + sum; // cerr << i << " " << t / i << endl; res[t / i] = i; } for (int i = N - 1; i > 0; i--) { res[i] = max(res[i], res[i + 1]); } for (int i = 0; i < N; i++) { cout << res[i + 1] << endl; } }
replace
35
36
35
36
0
p02890
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define ignore ignore #define pb emplace_back #define mt make_tuple #define gcd __gcd // Input #define in(a) scanf("%d", &a) #define in2(a, b) scanf("%d%d", &a, &b) #define in3(a, b, c) scanf("%d%d%d", &a, &b, &c) #define in4(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d) #define inp(p) in2(p.F, p.S) #define llin(a) cin >> a #define llin2(a, b) cin >> a >> b #define llin3(a, b, c) cin >> a >> b >> c #define inl(a) scanf("%lld", &a) #define read(v, i, n) \ for (i = 0; i < n; i++) \ in(v[i]) #define twodread(mat, i, j, n, m) \ rep(i, n) { rep(j, m) in(mat[i][j]); } #define sc(ch) scanf("%c", &ch) #define sstr(str) scanf("%s", str) // Output #define pr(a) printf("%d ", a) #define pr2(a, b) printf("%d %d\n", a, b) #define pr3(a, b, c) printf("%d %d %d\n", a, b, c) #define prp(p) pr2(p.F, p.S) #define out(a) printf("%d\n", a) #define outl(a) printf("%lld\n", a) #define llpr(a) cout << a << " " #define llpr2(a, b) cout << a << " " << b << " " #define llout(a) cout << a << "\n" #define pinttwod(mat, i, j, n, m) \ rep(i, n) { \ rep(j, m) pr(mat[i][j]); \ lin; \ } #define plltwod(mat, i, j, n, m) \ rep(i, n) { \ rep(j, m) llpr(mat[i][j]); \ lin; \ } #define byes printf("YES\n") #define bno printf("NO\n") #define yes printf("Yes\n") #define no printf("No\n") #define lin printf("\n") #define test(q) cout << "Case #" << q << ": " // Iterator #define rep(i, n) for (i = 0; i < n; ++i) #define fone(i, n) for (i = 1; i <= n; ++i) #define rrep(i, n) for (i = n - 1; i >= 0; --i) #define lp(i, a, b) for (i = a; i < b; ++i) #define rlp(i, a, b) for (i = a; i >= b; --i) #define all(vec) vec.begin(), vec.end() #define lower(v, k) lower_bound(v.begin(), v.end(), k) - v.begin() #define upper(v, k) upper_bound(v.begin(), v.end(), k) - v.begin() #define tf(mytuple) get<0>(mytuple) #define ts(mytuple) get<1>(mytuple) #define tt(mytuple) get<2>(mytuple) // function #define sz(x) (int)x.size() #define inrange(i, a, b) (i >= a && i <= b) #define FLUSH fflush(stdout) #define precision(x) cout << setprecision(x) << fixed #define remax(a, b) a = max(a, b) #define remin(a, b) a = min(a, b) #define middle() ((l + h) / 2) #define lchild(p) 2 * p #define rchild(p) 2 * p + 1 #define lseg l, m, 2 * p #define rseg m + 1, h, 2 * p + 1 #define bhardo(mat, i, j, n, m, t) \ rep(i, n) { rep(j, m) mat[i][j] = t; } #define bitcount(x) __builtin_popcount(x) #define bitcountll(x) __builtin_popcountll(x) #define biton(mask, i) ((mask >> i) & 1) #define bitoff(mask, i) (!((mask >> i) & 1)) #define toggle(mask, i) (mask ^= (1 << (i))) #define mul(p, q) ((ll)(p) * (ll)(q)) // Debug #define dbg(v, i, n) \ for (i = 0; i < n; i++) \ pr(v[i]); \ lin #define ck printf("continue\n") #define debug(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << "\n"; err(++it, args...); } // Data Type #define ll long long int #define ii pair<int, int> #define pli pair<ll, int> #define lii pair<ll, ll> #define triple tuple<int, int, int> #define vi vector<int> #define vll vector<ll> #define vii vector<pair<int, int>> #define vvi vector<vector<int>> #define viii vector<pair<pair<int, int>, int>> #define vvii vector<vector<pair<int, int>>> // Constant const double PI = acos(-1); const double eps = (1e-9); const ll INF = 2e18; const int M = (1e9) + 7; // const int M= 998244353; const int N = (3e5) + 500; // check the limit, man /** Have you worked on Simplification of Idea? Ok! let's code, check whether it is correct or not? */ struct binaryindextree { int sum[N]; void update(int j, int x) { if (j == 0) return; for (; j < N; j += j & (-j)) sum[j] += x; } int query(int j) { int x = 0; for (; j > 0; j -= (j) & (-j)) x += sum[j]; return x; } }; int ar[N]; void solve() { int n, i, a, k; in(n); rep(i, n) in(a), ar[a]++; vi v; rep(i, N) if (ar[i] > 0) v.pb(ar[i]); binaryindextree cnt, total; for (auto a : v) cnt.update(a, 1), total.update(a, a); int sum, new_sum; int m = sz(v); fone(k, n) { if (k > m) out(0); else { sum = (n / k); while (1) { new_sum = total.query(sum) + (m - cnt.query(sum)) * sum; new_sum /= k; if (new_sum == sum) break; sum = new_sum; } out(sum); } } } int main() { int t = 1; // in(t); while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define ignore ignore #define pb emplace_back #define mt make_tuple #define gcd __gcd // Input #define in(a) scanf("%d", &a) #define in2(a, b) scanf("%d%d", &a, &b) #define in3(a, b, c) scanf("%d%d%d", &a, &b, &c) #define in4(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d) #define inp(p) in2(p.F, p.S) #define llin(a) cin >> a #define llin2(a, b) cin >> a >> b #define llin3(a, b, c) cin >> a >> b >> c #define inl(a) scanf("%lld", &a) #define read(v, i, n) \ for (i = 0; i < n; i++) \ in(v[i]) #define twodread(mat, i, j, n, m) \ rep(i, n) { rep(j, m) in(mat[i][j]); } #define sc(ch) scanf("%c", &ch) #define sstr(str) scanf("%s", str) // Output #define pr(a) printf("%d ", a) #define pr2(a, b) printf("%d %d\n", a, b) #define pr3(a, b, c) printf("%d %d %d\n", a, b, c) #define prp(p) pr2(p.F, p.S) #define out(a) printf("%d\n", a) #define outl(a) printf("%lld\n", a) #define llpr(a) cout << a << " " #define llpr2(a, b) cout << a << " " << b << " " #define llout(a) cout << a << "\n" #define pinttwod(mat, i, j, n, m) \ rep(i, n) { \ rep(j, m) pr(mat[i][j]); \ lin; \ } #define plltwod(mat, i, j, n, m) \ rep(i, n) { \ rep(j, m) llpr(mat[i][j]); \ lin; \ } #define byes printf("YES\n") #define bno printf("NO\n") #define yes printf("Yes\n") #define no printf("No\n") #define lin printf("\n") #define test(q) cout << "Case #" << q << ": " // Iterator #define rep(i, n) for (i = 0; i < n; ++i) #define fone(i, n) for (i = 1; i <= n; ++i) #define rrep(i, n) for (i = n - 1; i >= 0; --i) #define lp(i, a, b) for (i = a; i < b; ++i) #define rlp(i, a, b) for (i = a; i >= b; --i) #define all(vec) vec.begin(), vec.end() #define lower(v, k) lower_bound(v.begin(), v.end(), k) - v.begin() #define upper(v, k) upper_bound(v.begin(), v.end(), k) - v.begin() #define tf(mytuple) get<0>(mytuple) #define ts(mytuple) get<1>(mytuple) #define tt(mytuple) get<2>(mytuple) // function #define sz(x) (int)x.size() #define inrange(i, a, b) (i >= a && i <= b) #define FLUSH fflush(stdout) #define precision(x) cout << setprecision(x) << fixed #define remax(a, b) a = max(a, b) #define remin(a, b) a = min(a, b) #define middle() ((l + h) / 2) #define lchild(p) 2 * p #define rchild(p) 2 * p + 1 #define lseg l, m, 2 * p #define rseg m + 1, h, 2 * p + 1 #define bhardo(mat, i, j, n, m, t) \ rep(i, n) { rep(j, m) mat[i][j] = t; } #define bitcount(x) __builtin_popcount(x) #define bitcountll(x) __builtin_popcountll(x) #define biton(mask, i) ((mask >> i) & 1) #define bitoff(mask, i) (!((mask >> i) & 1)) #define toggle(mask, i) (mask ^= (1 << (i))) #define mul(p, q) ((ll)(p) * (ll)(q)) // Debug #define dbg(v, i, n) \ for (i = 0; i < n; i++) \ pr(v[i]); \ lin #define ck printf("continue\n") #define debug(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << "\n"; err(++it, args...); } // Data Type #define ll long long int #define ii pair<int, int> #define pli pair<ll, int> #define lii pair<ll, ll> #define triple tuple<int, int, int> #define vi vector<int> #define vll vector<ll> #define vii vector<pair<int, int>> #define vvi vector<vector<int>> #define viii vector<pair<pair<int, int>, int>> #define vvii vector<vector<pair<int, int>>> // Constant const double PI = acos(-1); const double eps = (1e-9); const ll INF = 2e18; const int M = (1e9) + 7; // const int M= 998244353; const int N = (3e5) + 5000; // check the limit, man /** Have you worked on Simplification of Idea? Ok! let's code, check whether it is correct or not? */ struct binaryindextree { int sum[N]; void update(int j, int x) { if (j == 0) return; for (; j < N; j += j & (-j)) sum[j] += x; } int query(int j) { int x = 0; for (; j > 0; j -= (j) & (-j)) x += sum[j]; return x; } }; int ar[N]; void solve() { int n, i, a, k; in(n); rep(i, n) in(a), ar[a]++; vi v; rep(i, N) if (ar[i] > 0) v.pb(ar[i]); binaryindextree cnt, total; for (auto a : v) cnt.update(a, 1), total.update(a, a); int sum, new_sum; int m = sz(v); fone(k, n) { if (k > m) out(0); else { sum = (n / k); while (1) { new_sum = total.query(sum) + (m - cnt.query(sum)) * sum; new_sum /= k; if (new_sum == sum) break; sum = new_sum; } out(sum); } } } int main() { int t = 1; // in(t); while (t--) solve(); }
replace
121
122
121
122
0
p02890
C++
Runtime Error
#include <algorithm> #include <iostream> #include <utility> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> c(n); for (int i = 0; i < n; i++) { int a; cin >> a; c[a - 1]++; } sort(c.rbegin(), c.rend()); vector<int> d(n); int j = n - 1; int tot = 0; for (int i = 1; i <= n; i++) { while (j >= 0 && c[j] <= i) { tot += c[j]; j--; } d[i] = ((j + 1) * i + tot) / i; } for (int i = 1; i <= n; i++) { int ok = 0, ng = n + 1; while (ng - ok > 1) { int m = (ok + ng) / 2; if (d[m] >= i) { ok = m; } else { ng = m; } } cout << ok << endl; } }
#include <algorithm> #include <iostream> #include <utility> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> c(n); for (int i = 0; i < n; i++) { int a; cin >> a; c[a - 1]++; } sort(c.rbegin(), c.rend()); vector<int> d(n + 1); int j = n - 1; int tot = 0; for (int i = 1; i <= n; i++) { while (j >= 0 && c[j] <= i) { tot += c[j]; j--; } d[i] = ((j + 1) * i + tot) / i; } for (int i = 1; i <= n; i++) { int ok = 0, ng = n + 1; while (ng - ok > 1) { int m = (ok + ng) / 2; if (d[m] >= i) { ok = m; } else { ng = m; } } cout << ok << endl; } }
replace
22
23
22
23
0
p02890
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define ignore ignore #define pb emplace_back #define mt make_tuple #define gcd __gcd // Input #define in(a) scanf("%d", &a) #define in2(a, b) scanf("%d%d", &a, &b) #define in3(a, b, c) scanf("%d%d%d", &a, &b, &c) #define in4(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d) #define inp(p) in2(p.F, p.S) #define llin(a) cin >> a #define llin2(a, b) cin >> a >> b #define llin3(a, b, c) cin >> a >> b >> c #define inl(a) scanf("%lld", &a) #define read(v, i, n) \ for (i = 0; i < n; i++) \ in(v[i]) #define twodread(mat, i, j, n, m) \ rep(i, n) { rep(j, m) in(mat[i][j]); } #define sc(ch) scanf("%c", &ch) #define sstr(str) scanf("%s", str) // Output #define pr(a) printf("%d ", a) #define pr2(a, b) printf("%d %d\n", a, b) #define pr3(a, b, c) printf("%d %d %d\n", a, b, c) #define prp(p) pr2(p.F, p.S) #define out(a) printf("%d\n", a) #define outl(a) printf("%lld\n", a) #define llpr(a) cout << a << " " #define llpr2(a, b) cout << a << " " << b << " " #define llout(a) cout << a << "\n" #define pinttwod(mat, i, j, n, m) \ rep(i, n) { \ rep(j, m) pr(mat[i][j]); \ lin; \ } #define plltwod(mat, i, j, n, m) \ rep(i, n) { \ rep(j, m) llpr(mat[i][j]); \ lin; \ } #define byes printf("YES\n") #define bno printf("NO\n") #define yes printf("Yes\n") #define no printf("No\n") #define lin printf("\n") #define test(q) cout << "Case #" << q << ": " // Iterator #define rep(i, n) for (i = 0; i < n; ++i) #define fone(i, n) for (i = 1; i <= n; ++i) #define rrep(i, n) for (i = n - 1; i >= 0; --i) #define lp(i, a, b) for (i = a; i < b; ++i) #define rlp(i, a, b) for (i = a; i >= b; --i) #define all(vec) vec.begin(), vec.end() #define lower(v, k) lower_bound(v.begin(), v.end(), k) - v.begin() #define upper(v, k) upper_bound(v.begin(), v.end(), k) - v.begin() #define tf(mytuple) get<0>(mytuple) #define ts(mytuple) get<1>(mytuple) #define tt(mytuple) get<2>(mytuple) // function #define sz(x) (int)x.size() #define inrange(i, a, b) (i >= a && i <= b) #define FLUSH fflush(stdout) #define precision(x) cout << setprecision(x) << fixed #define remax(a, b) a = max(a, b) #define remin(a, b) a = min(a, b) #define middle() ((l + h) / 2) #define lchild(p) 2 * p #define rchild(p) 2 * p + 1 #define lseg l, m, 2 * p #define rseg m + 1, h, 2 * p + 1 #define bhardo(mat, i, j, n, m, t) \ rep(i, n) { rep(j, m) mat[i][j] = t; } #define bitcount(x) __builtin_popcount(x) #define bitcountll(x) __builtin_popcountll(x) #define biton(mask, i) ((mask >> i) & 1) #define bitoff(mask, i) (!((mask >> i) & 1)) #define toggle(mask, i) (mask ^= (1 << (i))) #define mul(p, q) ((ll)(p) * (ll)(q)) // Debug #define dbg(v, i, n) \ for (i = 0; i < n; i++) \ pr(v[i]); \ lin #define ck printf("continue\n") #define debug(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << "\n"; err(++it, args...); } // Data Type #define ll long long int #define ii pair<int, int> #define pli pair<ll, int> #define lii pair<ll, ll> #define triple tuple<int, int, int> #define vi vector<int> #define vll vector<ll> #define vii vector<pair<int, int>> #define vvi vector<vector<int>> #define viii vector<pair<pair<int, int>, int>> #define vvii vector<vector<pair<int, int>>> // Constant const double PI = acos(-1); const double eps = (1e-9); const ll INF = 2e18; const int M = (1e9) + 7; // const int M= 998244353; const int N = (3e5) + 3; // check the limit, man /** Have you worked on Simplification of Idea? Ok! let's code, check whether it is correct or not? */ struct binaryindextree { int sum[N]; void update(int j, int x) { if (j == 0) return; for (; j < N; j += j & (-j)) sum[j] += x; } int query(int j) { int x = 0; for (; j > 0; j -= (j) & (-j)) x += sum[j]; return x; } }; int cnt[N]; void solve() { int n, i, a, k; in(n); rep(i, n) in(a), cnt[a]++; vi v; rep(i, N) if (cnt[i] > 0) v.pb(cnt[i]); binaryindextree cnt, total; for (auto a : v) cnt.update(a, 1), total.update(a, a); int sum, new_sum; int m = sz(v); fone(k, n) { if (k > m) out(0); else { sum = (n / k); while (1) { new_sum = total.query(sum) + (m - cnt.query(sum)) * sum; new_sum /= k; if (new_sum == sum) break; sum = new_sum; } out(sum); } } } int main() { int t = 1; // in(t); while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define ignore ignore #define pb emplace_back #define mt make_tuple #define gcd __gcd // Input #define in(a) scanf("%d", &a) #define in2(a, b) scanf("%d%d", &a, &b) #define in3(a, b, c) scanf("%d%d%d", &a, &b, &c) #define in4(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d) #define inp(p) in2(p.F, p.S) #define llin(a) cin >> a #define llin2(a, b) cin >> a >> b #define llin3(a, b, c) cin >> a >> b >> c #define inl(a) scanf("%lld", &a) #define read(v, i, n) \ for (i = 0; i < n; i++) \ in(v[i]) #define twodread(mat, i, j, n, m) \ rep(i, n) { rep(j, m) in(mat[i][j]); } #define sc(ch) scanf("%c", &ch) #define sstr(str) scanf("%s", str) // Output #define pr(a) printf("%d ", a) #define pr2(a, b) printf("%d %d\n", a, b) #define pr3(a, b, c) printf("%d %d %d\n", a, b, c) #define prp(p) pr2(p.F, p.S) #define out(a) printf("%d\n", a) #define outl(a) printf("%lld\n", a) #define llpr(a) cout << a << " " #define llpr2(a, b) cout << a << " " << b << " " #define llout(a) cout << a << "\n" #define pinttwod(mat, i, j, n, m) \ rep(i, n) { \ rep(j, m) pr(mat[i][j]); \ lin; \ } #define plltwod(mat, i, j, n, m) \ rep(i, n) { \ rep(j, m) llpr(mat[i][j]); \ lin; \ } #define byes printf("YES\n") #define bno printf("NO\n") #define yes printf("Yes\n") #define no printf("No\n") #define lin printf("\n") #define test(q) cout << "Case #" << q << ": " // Iterator #define rep(i, n) for (i = 0; i < n; ++i) #define fone(i, n) for (i = 1; i <= n; ++i) #define rrep(i, n) for (i = n - 1; i >= 0; --i) #define lp(i, a, b) for (i = a; i < b; ++i) #define rlp(i, a, b) for (i = a; i >= b; --i) #define all(vec) vec.begin(), vec.end() #define lower(v, k) lower_bound(v.begin(), v.end(), k) - v.begin() #define upper(v, k) upper_bound(v.begin(), v.end(), k) - v.begin() #define tf(mytuple) get<0>(mytuple) #define ts(mytuple) get<1>(mytuple) #define tt(mytuple) get<2>(mytuple) // function #define sz(x) (int)x.size() #define inrange(i, a, b) (i >= a && i <= b) #define FLUSH fflush(stdout) #define precision(x) cout << setprecision(x) << fixed #define remax(a, b) a = max(a, b) #define remin(a, b) a = min(a, b) #define middle() ((l + h) / 2) #define lchild(p) 2 * p #define rchild(p) 2 * p + 1 #define lseg l, m, 2 * p #define rseg m + 1, h, 2 * p + 1 #define bhardo(mat, i, j, n, m, t) \ rep(i, n) { rep(j, m) mat[i][j] = t; } #define bitcount(x) __builtin_popcount(x) #define bitcountll(x) __builtin_popcountll(x) #define biton(mask, i) ((mask >> i) & 1) #define bitoff(mask, i) (!((mask >> i) & 1)) #define toggle(mask, i) (mask ^= (1 << (i))) #define mul(p, q) ((ll)(p) * (ll)(q)) // Debug #define dbg(v, i, n) \ for (i = 0; i < n; i++) \ pr(v[i]); \ lin #define ck printf("continue\n") #define debug(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << "\n"; err(++it, args...); } // Data Type #define ll long long int #define ii pair<int, int> #define pli pair<ll, int> #define lii pair<ll, ll> #define triple tuple<int, int, int> #define vi vector<int> #define vll vector<ll> #define vii vector<pair<int, int>> #define vvi vector<vector<int>> #define viii vector<pair<pair<int, int>, int>> #define vvii vector<vector<pair<int, int>>> // Constant const double PI = acos(-1); const double eps = (1e-9); const ll INF = 2e18; const int M = (1e9) + 7; // const int M= 998244353; const int N = (4e5) + 3; // check the limit, man /** Have you worked on Simplification of Idea? Ok! let's code, check whether it is correct or not? */ struct binaryindextree { int sum[N]; void update(int j, int x) { if (j == 0) return; for (; j < N; j += j & (-j)) sum[j] += x; } int query(int j) { int x = 0; for (; j > 0; j -= (j) & (-j)) x += sum[j]; return x; } }; int cnt[N]; void solve() { int n, i, a, k; in(n); rep(i, n) in(a), cnt[a]++; vi v; rep(i, N) if (cnt[i] > 0) v.pb(cnt[i]); binaryindextree cnt, total; for (auto a : v) cnt.update(a, 1), total.update(a, a); int sum, new_sum; int m = sz(v); fone(k, n) { if (k > m) out(0); else { sum = (n / k); while (1) { new_sum = total.query(sum) + (m - cnt.query(sum)) * sum; new_sum /= k; if (new_sum == sum) break; sum = new_sum; } out(sum); } } } int main() { int t = 1; // in(t); while (t--) solve(); }
replace
121
122
121
122
0
p02890
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep2(i, a, b) for (ll i = a; i < b; ++i) const ll MOD = 1000000007; const ll INF = 1e9; const ll IINF = 1e18; const double EPS = 1e-8; const double pi = acos(-1); template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int N; cin >> N; map<int, int> m; rep(i, N) { int a; cin >> a; m[a]++; } vector<int> cnt; for (auto x : m) { cnt.push_back(x.second); } sort(cnt.begin(), cnt.end()); int s = cnt.size(); vector<int> cnt2(s); cnt2[0] = 0; rep(i, s) { cnt2[i + 1] = cnt2[i] + cnt[i]; } rep(i, N) { int lb = -1, ub = N + 1; while (ub - lb > 1) { int mid = (ub + lb) / 2; int x = lower_bound(cnt.begin(), cnt.end(), mid) - cnt.begin(); int sum = cnt2[x] + mid * (s - x); if (sum >= (i + 1) * mid) { lb = mid; } else { ub = mid; } } cout << lb << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep2(i, a, b) for (ll i = a; i < b; ++i) const ll MOD = 1000000007; const ll INF = 1e9; const ll IINF = 1e18; const double EPS = 1e-8; const double pi = acos(-1); template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int N; cin >> N; map<int, int> m; rep(i, N) { int a; cin >> a; m[a]++; } vector<int> cnt; for (auto x : m) { cnt.push_back(x.second); } sort(cnt.begin(), cnt.end()); int s = cnt.size(); vector<int> cnt2(s + 1); cnt2[0] = 0; rep(i, s) { cnt2[i + 1] = cnt2[i] + cnt[i]; } rep(i, N) { int lb = -1, ub = N + 1; while (ub - lb > 1) { int mid = (ub + lb) / 2; int x = lower_bound(cnt.begin(), cnt.end(), mid) - cnt.begin(); int sum = cnt2[x] + mid * (s - x); if (sum >= (i + 1) * mid) { lb = mid; } else { ub = mid; } } cout << lb << endl; } }
replace
43
44
43
44
0
p02890
C++
Runtime Error
#pragma GCC optimize("-O3") #include <bits/stdc++.h> using namespace std; #define endl '\n' void solve(); signed main() { #ifdef Sprinkle clock_t clock_start = clock(); assert(freopen("test.txt", "r", stdin)); #else ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); #endif solve(); #ifdef Sprinkle cerr << "Running Time: " << ((clock() - clock_start) * 1.0 / CLOCKS_PER_SEC) << "s\n"; #endif } #ifdef Sprinkle void err() { cerr << "\n"; } template <typename T, typename... U> void err(const T &hd, const U &...tl) { cerr << hd << (sizeof...(tl) ? ", " : ""); err(tl...); } #define debug(x...) \ cerr << "" #x " = "; \ err(x) #else #define debug(...) 0 #endif int n; int a[300005], qian[300005]; int ans[300005]; void solve() { int n, k; cin >> n; for (int i = 0; i < n; ++i) { cin >> k; a[k]++; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) { qian[i] = qian[i - 1] + a[i]; } a[n + 1] = n + 1; int t = 1; for (int i = 1; i <= n; ++i) { while (a[t] * (t - i) <= qian[t - 1]) t++; ans[n - i + 1] = qian[t - 1] / (t - i); } for (int i = 1; i <= n; ++i) { cout << ans[i] << endl; } }
#pragma GCC optimize("-O3") #include <bits/stdc++.h> using namespace std; #define endl '\n' void solve(); signed main() { #ifdef Sprinkle clock_t clock_start = clock(); assert(freopen("test.txt", "r", stdin)); #else ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); #endif solve(); #ifdef Sprinkle cerr << "Running Time: " << ((clock() - clock_start) * 1.0 / CLOCKS_PER_SEC) << "s\n"; #endif } #ifdef Sprinkle void err() { cerr << "\n"; } template <typename T, typename... U> void err(const T &hd, const U &...tl) { cerr << hd << (sizeof...(tl) ? ", " : ""); err(tl...); } #define debug(x...) \ cerr << "" #x " = "; \ err(x) #else #define debug(...) 0 #endif int n; int a[300005], qian[300005]; int ans[300005]; void solve() { int n, k; cin >> n; for (int i = 0; i < n; ++i) { cin >> k; a[k]++; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) { qian[i] = qian[i - 1] + a[i]; } a[n + 1] = n + 1; int t = 1; for (int i = 1; i <= n; ++i) { while (t < n + 1 && 1ll * a[t] * (t - i) <= qian[t - 1]) t++; ans[n - i + 1] = qian[t - 1] / (t - i); } for (int i = 1; i <= n; ++i) { cout << ans[i] << endl; } }
replace
47
48
47
48
0
p02890
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int N; int A[30010]; vector<int> V; int ans[300010]; int main() { cin >> N; for (int i = 0; i < N; i++) { int a; cin >> a; A[a]++; } for (int i = 1; i <= N; i++) { V.push_back(A[i]); } sort(V.begin(), V.end()); int tmp = N; int sum = 0; int idx = 0; for (int i = 0; i < N; i++) { while (V.size() != idx && V[idx] == i) { idx++; tmp--; } sum += tmp; ans[sum / (i + 1)] = i + 1; } int MAX = 0; for (int i = N; i > 0; i--) { MAX = max(MAX, ans[i]); ans[i] = MAX; } for (int i = 1; i <= N; i++) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int N; int A[300010]; vector<int> V; int ans[300010]; int main() { cin >> N; for (int i = 0; i < N; i++) { int a; cin >> a; A[a]++; } for (int i = 1; i <= N; i++) { V.push_back(A[i]); } sort(V.begin(), V.end()); int tmp = N; int sum = 0; int idx = 0; for (int i = 0; i < N; i++) { while (V.size() != idx && V[idx] == i) { idx++; tmp--; } sum += tmp; ans[sum / (i + 1)] = i + 1; } int MAX = 0; for (int i = N; i > 0; i--) { MAX = max(MAX, ans[i]); ans[i] = MAX; } for (int i = 1; i <= N; i++) { cout << ans[i] << endl; } return 0; }
replace
4
5
4
5
0
p02890
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 <string> #include <utility> #include <vector> using namespace std; int read() { int xx = 0, ff = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') ff = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { xx = xx * 10 + ch - '0'; ch = getchar(); } return xx * ff; } long long READ() { long long xx = 0, ff = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') ff = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { xx = xx * 10 + ch - '0'; ch = getchar(); } return xx * ff; } char one() { char ch = getchar(); while (ch == ' ' || ch == '\n') ch = getchar(); return ch; } const int maxn = 100010; int N, a[maxn], b[maxn], c[maxn], d[maxn], e[maxn]; int main() { // freopen("in","r",stdin); N = read(); for (int i = 1; i <= N; i++) a[read()]++; for (int i = 1; i < maxn; i++) b[a[i]]++; // b:occur times for (int i = 1; i <= N; i++) { c[i] = c[i - 1] + b[i] * i; // c:sum of occur times <= i d[i] = d[i - 1] + b[i]; // d:prefix sum } for (int i = 1; i <= N; i++) e[i] = c[i] / i + d[N] - d[i]; for (int i = 1, j = N; i <= N; i++) { for (; j; j--) if (e[j] >= i) { printf("%d\n", j); break; } if (!j) printf("%d\n", j); } 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 <string> #include <utility> #include <vector> using namespace std; int read() { int xx = 0, ff = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') ff = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { xx = xx * 10 + ch - '0'; ch = getchar(); } return xx * ff; } long long READ() { long long xx = 0, ff = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') ff = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { xx = xx * 10 + ch - '0'; ch = getchar(); } return xx * ff; } char one() { char ch = getchar(); while (ch == ' ' || ch == '\n') ch = getchar(); return ch; } const int maxn = 300010; int N, a[maxn], b[maxn], c[maxn], d[maxn], e[maxn]; int main() { // freopen("in","r",stdin); N = read(); for (int i = 1; i <= N; i++) a[read()]++; for (int i = 1; i < maxn; i++) b[a[i]]++; // b:occur times for (int i = 1; i <= N; i++) { c[i] = c[i - 1] + b[i] * i; // c:sum of occur times <= i d[i] = d[i - 1] + b[i]; // d:prefix sum } for (int i = 1; i <= N; i++) e[i] = c[i] / i + d[N] - d[i]; for (int i = 1, j = N; i <= N; i++) { for (; j; j--) if (e[j] >= i) { printf("%d\n", j); break; } if (!j) printf("%d\n", j); } return 0; }
replace
49
50
49
50
0
p02890
C++
Time Limit Exceeded
#include <cassert> #include <iostream> #include <list> #include <map> using namespace std; int main() { int n; cin >> n; map<int, int> m; for (int i = 0; i < n; i++) { int a; cin >> a; m[a]++; } map<int, int> m2; for (auto &x : m) m2[x.second]++; int sz = 0; for (auto &x : m2) sz += x.second; for (int t = 1; t <= n; t++) { list<pair<int, int>> tmp(m2.begin(), m2.end()); int ans = 0, cursz = sz; // cerr << "t = " << t << endl; while (cursz >= t) { // for (auto &x : tmp) cerr << x.first << ' ' << x.second << endl; // cerr << "ans = " << ans << endl; auto it = prev(tmp.end()); int t1 = t; for (;;) { if (t1 > it->second) t1 -= it->second, it--; else { if (it->first == 1) { if (it->second == t1) it = tmp.erase(it); else { it->second -= t1; it++; } cursz -= t1; } else { if (it == tmp.begin() || (prev(it)->first != it->first - 1)) { tmp.insert(it, {it->first - 1, t1}); } else prev(it)->second += t1; it->second -= t1; it++; } while (it != tmp.end()) { assert(it->first != 1); if (it != tmp.begin() && prev(it)->first == it->first - 1) { prev(it)->second += it->second; it = tmp.erase(it); } else { it->first--; it++; } } break; } } ans++; } cout << ans << endl; } }
#include <cassert> #include <iostream> #include <list> #include <map> using namespace std; int main() { int n; cin >> n; map<int, int> m; for (int i = 0; i < n; i++) { int a; cin >> a; m[a]++; } map<int, int> m2; for (auto &x : m) m2[x.second]++; int sz = 0; for (auto &x : m2) sz += x.second; for (int t = 1; t <= n; t++) { if (sz < t) { cout << 0 << endl; continue; } list<pair<int, int>> tmp(m2.begin(), m2.end()); int ans = 0, cursz = sz; // cerr << "t = " << t << endl; while (cursz >= t) { // for (auto &x : tmp) cerr << x.first << ' ' << x.second << endl; // cerr << "ans = " << ans << endl; auto it = prev(tmp.end()); int t1 = t; for (;;) { if (t1 > it->second) t1 -= it->second, it--; else { if (it->first == 1) { if (it->second == t1) it = tmp.erase(it); else { it->second -= t1; it++; } cursz -= t1; } else { if (it == tmp.begin() || (prev(it)->first != it->first - 1)) { tmp.insert(it, {it->first - 1, t1}); } else prev(it)->second += t1; it->second -= t1; it++; } while (it != tmp.end()) { assert(it->first != 1); if (it != tmp.begin() && prev(it)->first == it->first - 1) { prev(it)->second += it->second; it = tmp.erase(it); } else { it->first--; it++; } } break; } } ans++; } cout << ans << endl; } }
insert
21
21
21
25
TLE
p02890
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = (3e5) + 5; int n, a[N]; int freq[N]; int ac[N]; int f(int x, int pos) { while (pos >= 0) { if (freq[pos] < x) break; pos--; } return (pos >= 0 ? ac[pos] : 0) + x * (n - pos - 1); } int main() { // claudio van scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); freq[a[i] - 1]++; } sort(freq, freq + n); partial_sum(freq, freq + n, ac); int ans = n, pos = n - 1; for (int k = 1; k <= n; ++k) { while (ans > 0) { if (k <= f(ans, pos) / ans) break; ans--; } printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = (3e5) + 5; int n, a[N]; int freq[N]; int ac[N]; int f(int x, int &pos) { while (pos >= 0) { if (freq[pos] < x) break; pos--; } return (pos >= 0 ? ac[pos] : 0) + x * (n - pos - 1); } int main() { // claudio van scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); freq[a[i] - 1]++; } sort(freq, freq + n); partial_sum(freq, freq + n, ac); int ans = n, pos = n - 1; for (int k = 1; k <= n; ++k) { while (ans > 0) { if (k <= f(ans, pos) / ans) break; ans--; } printf("%d\n", ans); } return 0; }
replace
9
10
9
10
TLE
p02890
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; class Solution { public: vector<int> solve(vector<int> &A) { sort(A.begin(), A.end()); int n = A.size(); vector<int> res(n, 0); vector<int> B; for (int i = 0; i < n; ++i) { int cnt = 1; while (i + 1 < n && A[i + 1] == A[i]) { ++i; ++cnt; } B.push_back(cnt); } sort(B.begin(), B.end()); int m = B.size(); vector<int> acc(m + 1, 0); for (int i = 0; i < m; ++i) { acc[i + 1] = acc[i] + B[i]; } auto count = [&](const vector<int> &B, int K) { int n = B.size(); int total = accumulate(B.begin(), B.end(), 0); int lo = 0, hi = total; while (lo < hi) { auto mi = (lo + hi + 1) / 2; auto it = lower_bound(B.begin(), B.end(), mi); int idx = it - B.begin(); int sum = acc[idx] + (n - idx) * mi; int cnt = sum / K; // cout << mi << " " << cnt << " " << sum << " " << hi << endl; if (cnt >= mi) { lo = mi; } else { hi = mi - 1; } } return lo; }; for (int i = 0; i < m; ++i) { res[i] = count(B, i + 1); } return res; } }; int main(int argc, char **argv) { 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]; } Solution sol; auto res = sol.solve(A); for (auto r : res) { cout << r << "\n"; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; class Solution { public: vector<int> solve(vector<int> &A) { sort(A.begin(), A.end()); int n = A.size(); vector<int> res(n, 0); vector<int> B; for (int i = 0; i < n; ++i) { int cnt = 1; while (i + 1 < n && A[i + 1] == A[i]) { ++i; ++cnt; } B.push_back(cnt); } sort(B.begin(), B.end()); int m = B.size(); vector<int> acc(m + 1, 0); for (int i = 0; i < m; ++i) { acc[i + 1] = acc[i] + B[i]; } auto count = [&](const vector<int> &B, int K) { int n = B.size(); int lo = 0, hi = acc[n]; while (lo < hi) { auto mi = (lo + hi + 1) / 2; auto it = lower_bound(B.begin(), B.end(), mi); int idx = it - B.begin(); int sum = acc[idx] + (n - idx) * mi; int cnt = sum / K; // cout << mi << " " << cnt << " " << sum << " " << hi << endl; if (cnt >= mi) { lo = mi; } else { hi = mi - 1; } } return lo; }; for (int i = 0; i < m; ++i) { res[i] = count(B, i + 1); } return res; } }; int main(int argc, char **argv) { 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]; } Solution sol; auto res = sol.solve(A); for (auto r : res) { cout << r << "\n"; } return 0; }
replace
51
53
51
53
TLE
p02890
C++
Runtime Error
#include <stdio.h> #include <stdlib.h> #include <iomanip> #include <iostream> #include <sstream> #include <stdint.h> #include <string.h> #define _USE_MATH_DEFINES #include <math.h> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <algorithm> #include <bitset> #include <chrono> #include <functional> #include <random> #define sqr(x) (x) * (x) typedef unsigned int u32; typedef int i32; typedef unsigned long long int u64; typedef long long int i64; typedef uint16_t u16; typedef int16_t i16; typedef uint8_t u8; typedef int8_t i8; using namespace std; using namespace std::chrono; const i64 mod = 1000000007ll; const i64 smod = 998244353ll; const i64 inf = 10000000000000007ll; const double eps = 1e-10; // del const i64 MAXN = 30005; i64 n; i64 m; i64 a[MAXN]; i64 c[MAXN]; i64 ps[MAXN]; bool f(i64 k, i64 t) { i64 i = lower_bound(c + 1, c + m + 1, -t) - c; i64 s = ps[i - 1] - t * (i - 1); return s <= n - (k * t); } i64 bin(i64 k, i64 l, i64 r) { if (l == r) { return l; } if (l + 1 == r) { if (f(k, r)) { return r; } else { return l; } } i64 t = (l + r) / 2; if (f(k, t)) { return bin(k, t, r); } else { return bin(k, l, t); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed); cin >> n; for (i64 i = 0; i < n; i++) { cin >> a[i]; } for (i64 i = 0; i < n; i++) { c[a[i]]++; } sort(c + 1, c + n + 1, greater<i64>()); for (i64 i = 1; i <= n; i++) { ps[i] = ps[i - 1] + c[i]; if (c[i] != 0) { m++; } } for (i64 i = 1; i <= n; i++) { c[i] *= -1; } // i64 tt = f(3, 2); // cerr << tt << endl; stringstream ss; for (i64 k = 1; k <= n; k++) { i64 R = bin(k, 0, n / k); ss << R << endl; } cout << ss.str(); return 0; }
#include <stdio.h> #include <stdlib.h> #include <iomanip> #include <iostream> #include <sstream> #include <stdint.h> #include <string.h> #define _USE_MATH_DEFINES #include <math.h> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <algorithm> #include <bitset> #include <chrono> #include <functional> #include <random> #define sqr(x) (x) * (x) typedef unsigned int u32; typedef int i32; typedef unsigned long long int u64; typedef long long int i64; typedef uint16_t u16; typedef int16_t i16; typedef uint8_t u8; typedef int8_t i8; using namespace std; using namespace std::chrono; const i64 mod = 1000000007ll; const i64 smod = 998244353ll; const i64 inf = 10000000000000007ll; const double eps = 1e-10; // del const i64 MAXN = 300005; i64 n; i64 m; i64 a[MAXN]; i64 c[MAXN]; i64 ps[MAXN]; bool f(i64 k, i64 t) { i64 i = lower_bound(c + 1, c + m + 1, -t) - c; i64 s = ps[i - 1] - t * (i - 1); return s <= n - (k * t); } i64 bin(i64 k, i64 l, i64 r) { if (l == r) { return l; } if (l + 1 == r) { if (f(k, r)) { return r; } else { return l; } } i64 t = (l + r) / 2; if (f(k, t)) { return bin(k, t, r); } else { return bin(k, l, t); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed); cin >> n; for (i64 i = 0; i < n; i++) { cin >> a[i]; } for (i64 i = 0; i < n; i++) { c[a[i]]++; } sort(c + 1, c + n + 1, greater<i64>()); for (i64 i = 1; i <= n; i++) { ps[i] = ps[i - 1] + c[i]; if (c[i] != 0) { m++; } } for (i64 i = 1; i <= n; i++) { c[i] *= -1; } // i64 tt = f(3, 2); // cerr << tt << endl; stringstream ss; for (i64 k = 1; k <= n; k++) { i64 R = bin(k, 0, n / k); ss << R << endl; } cout << ss.str(); return 0; }
replace
50
51
50
51
0
p02890
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 3e5 + 7; int a[maxn]; vector<int> ans; vector<int> vec; vector<int> rem; long double f(int u, int k) { long double right = (long double)rem[u - 1] / (long double)(k - u); return right; } int SanFen(int l, int r, int k) // 找凸点 { while (l < r - 1) { int mid = (l + r) / 2; int mmid = (mid + r) / 2; if (f(mid, k) > f(mmid, k)) l = mid; else r = mmid; } return f(l, k) > f(r, k) ? l : r; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a[x]++; } for (int i = 1; i < maxn; i++) if (a[i]) vec.push_back(a[i]); sort(vec.begin(), vec.end(), greater<int>()); for (int i = vec.size(); i <= n; i++) vec.push_back(0); rem.push_back(n - vec[0]); for (int i = 1; i < n; i++) rem.push_back(rem[i - 1] - vec[i]); ans.push_back(n); for (int kpl = 2; kpl <= n; kpl++) { int k = kpl; int l = SanFen(1, k, k); if (l >= k) l = k - 1; if (l <= 0) l = 1; int cnt = rem[l] / (k - l - 1); cnt = min(cnt, rem[l - 1] / (k - l)); cnt = min(cnt, rem[l + 1] / (k - l - 2)); cnt = min(cnt, n / k); ans.push_back(cnt); } for (auto u : ans) cout << u << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 3e5 + 7; int a[maxn]; vector<int> ans; vector<int> vec; vector<int> rem; long double f(int u, int k) { long double right = (long double)rem[u - 1] / (long double)(k - u); return right; } int SanFen(int l, int r, int k) // 找凸点 { while (l < r - 1) { int mid = (l + r) / 2; int mmid = (mid + r) / 2; if (f(mid, k) > f(mmid, k)) l = mid; else r = mmid; } return f(l, k) > f(r, k) ? l : r; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a[x]++; } for (int i = 1; i < maxn; i++) if (a[i]) vec.push_back(a[i]); sort(vec.begin(), vec.end(), greater<int>()); for (int i = vec.size(); i <= n; i++) vec.push_back(0); rem.push_back(n - vec[0]); for (int i = 1; i < n; i++) rem.push_back(rem[i - 1] - vec[i]); ans.push_back(n); for (int kpl = 2; kpl <= n; kpl++) { int k = kpl; int l = SanFen(1, k, k); if (l >= k) l = k - 1; if (l <= 0) l = 1; int cnt = rem[l - 1] / (k - l); if (l >= 0 && k != l + 1) cnt = min(cnt, rem[l] / (k - l - 1)); if (l + 1 < k - 1) cnt = min(cnt, rem[l + 1] / (k - l - 2)); cnt = min(cnt, n / k); ans.push_back(cnt); } for (auto u : ans) cout << u << endl; }
replace
50
53
50
55
-8
p02890
C++
Runtime Error
#include <bits/stdc++.h> #define fr first #define se second using namespace std; const long long N = 2e5 + 7; const long long inf = 1e9 + 7; const long long mod = 1e9 + 7; int n; int a[N]; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; a[x]++; } sort(a + 1, a + n + 1); vector<int> p(n + 1, 0); for (int i = 1; i <= n; i++) { p[i] = p[i - 1] + a[i]; } int res = n; for (int k = 1; k <= n; k++) { while (true) { int l = 0, r = n; while (l < r) { int m = (l + r) / 2; if (a[m + 1] < res) { l = m + 1; } else { r = m; } } int s = p[l] + (n - l) * res; if (s >= k * res) { break; } res--; } cout << res << "\n"; } }
#include <bits/stdc++.h> #define fr first #define se second using namespace std; const long long N = 3e5 + 7; const long long inf = 1e9 + 7; const long long mod = 1e9 + 7; int n; int a[N]; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; a[x]++; } sort(a + 1, a + n + 1); vector<int> p(n + 1, 0); for (int i = 1; i <= n; i++) { p[i] = p[i - 1] + a[i]; } int res = n; for (int k = 1; k <= n; k++) { while (true) { int l = 0, r = n; while (l < r) { int m = (l + r) / 2; if (a[m + 1] < res) { l = m + 1; } else { r = m; } } int s = p[l] + (n - l) * res; if (s >= k * res) { break; } res--; } cout << res << "\n"; } }
replace
7
8
7
8
0
p02890
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; int main() { int N; cin >> N; vector<int> A(N); map<int, int> cnt; for (int i = 0; i < N; i++) { cin >> A[i]; cnt[A[i]] += 1; } vector<int> all; for (auto p : cnt) { all.emplace_back(p.second); } sort(all.begin(), all.end()); int K = all.size(); vector<int> sum(K + 1); for (int i = 0; i <= K; i++) { sum[i + 1] = sum[i] + all[i]; } for (int i = 1; i <= N; i++) { int lb = 0, ub = N / i + 1; while (ub - lb > 1) { int m = (lb + ub) / 2; int p = lower_bound(all.begin(), all.end(), m) - all.begin(); (sum[p] + (K - p) * m < m * i ? ub : lb) = m; } printf("%d\n", lb); } return 0; }
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; int main() { int N; cin >> N; vector<int> A(N); map<int, int> cnt; for (int i = 0; i < N; i++) { cin >> A[i]; cnt[A[i]] += 1; } vector<int> all; for (auto p : cnt) { all.emplace_back(p.second); } sort(all.begin(), all.end()); int K = all.size(); vector<int> sum(K + 1); for (int i = 0; i < K; i++) { sum[i + 1] = sum[i] + all[i]; } for (int i = 1; i <= N; i++) { int lb = 0, ub = N / i + 1; while (ub - lb > 1) { int m = (lb + ub) / 2; int p = lower_bound(all.begin(), all.end(), m) - all.begin(); (sum[p] + (K - p) * m < m * i ? ub : lb) = m; } printf("%d\n", lb); } return 0; }
replace
20
21
20
21
0
p02890
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define N 100005 int sum[N], cc[N], number[N]; void update(int *pen, int ind, int v) { while (ind < N) { pen[ind] += v; ind += ind & (-ind); } } int query(int *pen, int ind) { int ret = 0; while (ind > 0) { ret += pen[ind]; ind = ind & (ind - 1); } return ret; } void solve() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); number[a]++; } int numType = 0; for (int i = 1; i <= n; ++i) { if (number[i] > 0) { update(sum, number[i], number[i]); update(cc, number[i], 1); ++numType; } } for (int k = 1; k <= n; ++k) { int l = 0, r = n / k, mid; while (l < r) { mid = (l + r + 1) / 2; int kCC = query(cc, mid); long long qsum = query(sum, mid); qsum += (long long)(numType - kCC) * mid; if (qsum >= mid * k) { l = mid; } else { r = mid - 1; } } printf("%d ", l); } putchar('\n'); } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define N 300005 int sum[N], cc[N], number[N]; void update(int *pen, int ind, int v) { while (ind < N) { pen[ind] += v; ind += ind & (-ind); } } int query(int *pen, int ind) { int ret = 0; while (ind > 0) { ret += pen[ind]; ind = ind & (ind - 1); } return ret; } void solve() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); number[a]++; } int numType = 0; for (int i = 1; i <= n; ++i) { if (number[i] > 0) { update(sum, number[i], number[i]); update(cc, number[i], 1); ++numType; } } for (int k = 1; k <= n; ++k) { int l = 0, r = n / k, mid; while (l < r) { mid = (l + r + 1) / 2; int kCC = query(cc, mid); long long qsum = query(sum, mid); qsum += (long long)(numType - kCC) * mid; if (qsum >= mid * k) { l = mid; } else { r = mid - 1; } } printf("%d ", l); } putchar('\n'); } int main() { solve(); return 0; }
replace
2
3
2
3
0
p02890
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; #define LL long long #define MAXN 100000 int n, cnt[MAXN + 5], ans[MAXN + 5]; LL sum[MAXN + 5]; LL read() { LL x = 0, F = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') F = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * F; } int main() { n = read(); for (int i = 1; i <= n; i++) { int x = read(); cnt[x]++, sum[cnt[x]]++; } for (int i = 1; i <= n; i++) sum[i] += sum[i - 1]; int t = 0; for (int k = n; k >= 1; k--) { while (t < n && sum[t + 1] >= 1LL * k * (t + 1)) t++; ans[k] = t; } for (int i = 1; i <= n; i++) printf("%d\n", ans[i]); }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; #define LL long long #define MAXN 300000 int n, cnt[MAXN + 5], ans[MAXN + 5]; LL sum[MAXN + 5]; LL read() { LL x = 0, F = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') F = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * F; } int main() { n = read(); for (int i = 1; i <= n; i++) { int x = read(); cnt[x]++, sum[cnt[x]]++; } for (int i = 1; i <= n; i++) sum[i] += sum[i - 1]; int t = 0; for (int k = n; k >= 1; k--) { while (t < n && sum[t + 1] >= 1LL * k * (t + 1)) t++; ans[k] = t; } for (int i = 1; i <= n; i++) printf("%d\n", ans[i]); }
replace
5
6
5
6
0
p02890
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define LLI long long int #define FOR(v, a, b) for (LLI v = (a); v < (b); ++v) #define FORE(v, a, b) for (LLI v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for (LLI v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define ITR(it, c) for (auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for (auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c, x) ((c).find(x) != (c).end()) #define fst first #define snd second #define popcount __builtin_popcount #define UNIQ(v) (v).erase(unique(ALL(v)), (v).end()) #define bit(i) (1LL << (i)) #ifdef DEBUG #include <misc/C++/Debug.cpp> #else #define dump(...) ((void)0) #endif #define gcd __gcd using namespace std; template <class T> constexpr T lcm(T m, T n) { return m / gcd(m, n) * n; } template <typename I> void join(ostream &ost, I s, I t, string d = " ") { for (auto i = s; i != t; ++i) { if (i != s) ost << d; ost << *i; } ost << endl; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &a : v) is >> a; return is; } template <typename T, typename U> bool chmin(T &a, const U &b) { return (a > b ? a = b, true : false); } template <typename T, typename U> bool chmax(T &a, const U &b) { return (a < b ? a = b, true : false); } template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v) { fill((U *)a, (U *)(a + N), v); } struct Init { Init() { cin.tie(0); ios::sync_with_stdio(false); } } init; template <typename T, typename Container = vector<T>> vector<pair<T, int64_t>> run_length_encode(const Container &v) { vector<pair<T, int64_t>> ret; for (auto &x : v) { if (ret.empty()) ret.push_back({x, 1}); else if (ret.back().fst == x) ++ret.back().snd; else ret.push_back({x, 1}); } return ret; } int main() { int N; while (cin >> N) { vector<LLI> A(N); cin >> A; REP(i, N) A[i] -= 1; sort(ALL(A)); vector<int> count(N); REP(i, N) { count[A[i]] += 1; } sort(RALL(count)); while (count.back() == 0) { count.pop_back(); } dump(count); auto count2 = run_length_encode<int>(count); dump(count2); FORE(k, 1, N) { auto check = [&](int x) { LLI s = 0; for (auto &p : count2) { s += min(x, p.fst) * p.snd; } return s >= (LLI)x * k; }; int lb = -1, ub = N + 1; while (abs(lb - ub) > 1) { int mid = (lb + ub) / 2; if (check(mid)) { lb = mid; } else { ub = mid; } } cout << lb << endl; } } return 0; }
#include <bits/stdc++.h> #define LLI long long int #define FOR(v, a, b) for (LLI v = (a); v < (b); ++v) #define FORE(v, a, b) for (LLI v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for (LLI v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define ITR(it, c) for (auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for (auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c, x) ((c).find(x) != (c).end()) #define fst first #define snd second #define popcount __builtin_popcount #define UNIQ(v) (v).erase(unique(ALL(v)), (v).end()) #define bit(i) (1LL << (i)) #ifdef DEBUG #include <misc/C++/Debug.cpp> #else #define dump(...) ((void)0) #endif #define gcd __gcd using namespace std; template <class T> constexpr T lcm(T m, T n) { return m / gcd(m, n) * n; } template <typename I> void join(ostream &ost, I s, I t, string d = " ") { for (auto i = s; i != t; ++i) { if (i != s) ost << d; ost << *i; } ost << endl; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &a : v) is >> a; return is; } template <typename T, typename U> bool chmin(T &a, const U &b) { return (a > b ? a = b, true : false); } template <typename T, typename U> bool chmax(T &a, const U &b) { return (a < b ? a = b, true : false); } template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v) { fill((U *)a, (U *)(a + N), v); } struct Init { Init() { cin.tie(0); ios::sync_with_stdio(false); } } init; template <typename T, typename Container = vector<T>> vector<pair<T, int64_t>> run_length_encode(const Container &v) { vector<pair<T, int64_t>> ret; for (auto &x : v) { if (ret.empty()) ret.push_back({x, 1}); else if (ret.back().fst == x) ++ret.back().snd; else ret.push_back({x, 1}); } return ret; } int main() { int N; while (cin >> N) { vector<LLI> A(N); cin >> A; REP(i, N) A[i] -= 1; sort(ALL(A)); vector<int> count(N); REP(i, N) { count[A[i]] += 1; } sort(RALL(count)); while (count.back() == 0) { count.pop_back(); } dump(count); auto count2 = run_length_encode<int>(count); dump(count2); FORE(k, 1, N) { auto check = [&](int x) { LLI s = 0; for (auto &p : count2) { s += min(x, p.fst) * p.snd; } return s >= (LLI)x * k; }; int lb = -1, ub = N / k + 1; while (abs(lb - ub) > 1) { int mid = (lb + ub) / 2; if (check(mid)) { lb = mid; } else { ub = mid; } } cout << lb << endl; } } return 0; }
replace
113
114
113
114
TLE
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(void) { string S; cin >> S; long long N; cin >> N; vector<int> Chars; int num = 1; for (int i = 1; i < S.size(); i++) { if (S[i] == S[i - 1]) { num++; if (i == S.size() - 1) { Chars.push_back(num); } } else { Chars.push_back(num); num = 1; } } long long result = 0; for (int i = 1; i < Chars.size() - 1; i++) { result += Chars[i] / 2; } if (S.front() != S.back()) { result += Chars.front() / 2; result += Chars.back() / 2; result *= N; } else { if (Chars.size() == 1) { result += Chars.front() * N; result /= 2; } else { int add = (Chars.front() + Chars.back()) / 2; result *= N; result += (N - 1) * add; result += Chars.front() / 2; result += Chars.back() / 2; } } cout << result << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { string S; cin >> S; long long N; cin >> N; if (S.size() == 1) { int result = N / 2; cout << result << endl; return 0; } vector<int> Chars; int num = 1; for (int i = 1; i < S.size(); i++) { if (S[i] == S[i - 1]) { num++; if (i == S.size() - 1) { Chars.push_back(num); } } else { Chars.push_back(num); num = 1; } } long long result = 0; for (int i = 1; i < Chars.size() - 1; i++) { result += Chars[i] / 2; } if (S.front() != S.back()) { result += Chars.front() / 2; result += Chars.back() / 2; result *= N; } else { if (Chars.size() == 1) { result += Chars.front() * N; result /= 2; } else { int add = (Chars.front() + Chars.back()) / 2; result *= N; result += (N - 1) * add; result += Chars.front() / 2; result += Chars.back() / 2; } } cout << result << endl; return 0; }
insert
9
9
9
15
0
p02891
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define ll long long int #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n; i >= 0; i--) #define REP(i, s, t) for (int i = s; i <= t; i++) #define RREP(i, s, t) for (int i = s; i >= t; i--) #define dump(x) cerr << #x << " = " << (x) << endl; #define INF 2000000000 #define mod 1000000007 #define INF2 1000000000000000000 #define int long long signed main(void) { cin.tie(0); ios::sync_with_stdio(false); string S; int K; cin >> S >> K; int ans1 = 0, ans2 = 0, ans3 = 0; string SS = S + S; string SSS = S + S + S; rep(i, S.length() - 1) { if (S[i] == S[i + 1]) { ans1++; S[i + 1] = '0'; } } rep(i, SS.length() - 1) { if (SS[i] == SS[i + 1]) { ans2++; SS[i + 1] = '0'; } } rep(i, SSS.length() - 1) { if (SSS[i] == SSS[i + 1]) { ans3++; SSS[i + 1] = '0'; } } int increase = ans2 - ans1; int increase2 = ans3 - ans1; // if (increase2 != increase * 2) { // cout << increase << " " << increase2 << endl; // cout << S << endl; // cout << SS << endl; // cout << SSS << endl; // } assert(increase2 == increase * 2); cout << ans1 + increase * (K - 1) << endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define ll long long int #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n; i >= 0; i--) #define REP(i, s, t) for (int i = s; i <= t; i++) #define RREP(i, s, t) for (int i = s; i >= t; i--) #define dump(x) cerr << #x << " = " << (x) << endl; #define INF 2000000000 #define mod 1000000007 #define INF2 1000000000000000000 #define int long long signed main(void) { cin.tie(0); ios::sync_with_stdio(false); string S; int K; cin >> S >> K; int ans1 = 0, ans2 = 0, ans3 = 0; string SS = S + S; string SSS = S + S + S; rep(i, S.length() - 1) { if (S[i] == S[i + 1]) { ans1++; S[i + 1] = '0'; } } rep(i, SS.length() - 1) { if (SS[i] == SS[i + 1]) { ans2++; SS[i + 1] = '0'; } } rep(i, SSS.length() - 1) { if (SSS[i] == SSS[i + 1]) { ans3++; SSS[i + 1] = '0'; } } int increase = ans2 - ans1; int increase2 = ans3 - ans1; if (increase2 != increase * 2) { // cout << ans1 << " " << increase << " " << increase2 << endl; cout << ans1 + increase * (K / 2) + (ans3 - ans2) * ((K - 1) / 2) << endl; return 0; } // assert(increase2 == increase * 2); cout << ans1 + increase * (K - 1) << endl; return 0; }
replace
42
49
42
49
0
p02891
C++
Time Limit Exceeded
#pragma GCC optimize("-O3") #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> p32; typedef pair<ll, ll> p64; typedef pair<double, double> pdd; typedef vector<ll> v64; typedef vector<int> v32; typedef vector<vector<int>> vv32; typedef vector<vector<ll>> vv64; typedef vector<p64> vp64; typedef vector<p32> vp32; ll MOD = 998244353; ll NUM = 1e9 + 7; #define forn(i, e) for (ll i = 0; i < e; i++) #define forsn(i, s, e) for (ll i = s; i < e; i++) #define rforn(i, s) for (ll i = s; i >= 0; i--) #define rforsn(i, s, e) for (ll i = s; i >= e; i--) #define ln "\n" #define dbg(x) cout << #x << " = " << x << ln #define mp make_pair #define pb push_back #define ff first #define ss second #define INF 1e18 #define fast_cin() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define all(x) (x).begin(), (x).end() #define sz(x) ((ll)(x).size()) #define zer ll(0) int main() { fast_cin(); string s; cin >> s; ll k; cin >> k; ll init = 0; ll cnt1 = 0, cnt2 = 0; while (init < s.size()) { if (s[0] == s[init]) { init++; cnt1++; } else { break; } } bool flg = 0; if (init == s.size()) { flg = 1; } ll end = s.size() - 1; while (end >= 0) { if (s[s.size() - 1] == s[end]) { end--; cnt2++; } else { break; } } if (flg) { cout << k * s.size() / 2 << '\n'; } else { if (s[0] == s[s.size() - 1]) { ll ans = 0; ll i = init; while (i <= end) { ll j = i; ll cnt = 0; while (j < s.size() && s[j] == s[init]) { j++; cnt++; } ans += cnt / 2; i = j; } ans *= k; ans += cnt1 / 2; ans += cnt2 / 2; ans += (k - 1) * ((cnt1 + cnt2) / 2); cout << ans << '\n'; } else { init = 0; ll ans = 0; while (init < s.size()) { ll j = init; ll cnt = 0; while (j < s.size() && s[j] == s[init]) { j++; cnt++; } ans += cnt / 2; init = j; } cout << ans * k << '\n'; } } return 0; }
#pragma GCC optimize("-O3") #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> p32; typedef pair<ll, ll> p64; typedef pair<double, double> pdd; typedef vector<ll> v64; typedef vector<int> v32; typedef vector<vector<int>> vv32; typedef vector<vector<ll>> vv64; typedef vector<p64> vp64; typedef vector<p32> vp32; ll MOD = 998244353; ll NUM = 1e9 + 7; #define forn(i, e) for (ll i = 0; i < e; i++) #define forsn(i, s, e) for (ll i = s; i < e; i++) #define rforn(i, s) for (ll i = s; i >= 0; i--) #define rforsn(i, s, e) for (ll i = s; i >= e; i--) #define ln "\n" #define dbg(x) cout << #x << " = " << x << ln #define mp make_pair #define pb push_back #define ff first #define ss second #define INF 1e18 #define fast_cin() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define all(x) (x).begin(), (x).end() #define sz(x) ((ll)(x).size()) #define zer ll(0) int main() { fast_cin(); string s; cin >> s; ll k; cin >> k; ll init = 0; ll cnt1 = 0, cnt2 = 0; while (init < s.size()) { if (s[0] == s[init]) { init++; cnt1++; } else { break; } } bool flg = 0; if (init == s.size()) { flg = 1; } ll end = s.size() - 1; while (end >= 0) { if (s[s.size() - 1] == s[end]) { end--; cnt2++; } else { break; } } if (flg) { cout << k * s.size() / 2 << '\n'; } else { if (s[0] == s[s.size() - 1]) { ll ans = 0; ll i = init; while (i <= end) { ll j = i; ll cnt = 0; while (j < s.size() && s[j] == s[i]) { j++; cnt++; } ans += cnt / 2; i = j; } ans *= k; ans += cnt1 / 2; ans += cnt2 / 2; ans += (k - 1) * ((cnt1 + cnt2) / 2); cout << ans << '\n'; } else { init = 0; ll ans = 0; while (init < s.size()) { ll j = init; ll cnt = 0; while (j < s.size() && s[j] == s[init]) { j++; cnt++; } ans += cnt / 2; init = j; } cout << ans * k << '\n'; } } return 0; }
replace
74
75
74
75
TLE
p02891
Python
Time Limit Exceeded
def count_changes(s, k, prev=None): count = 0 for _ in range(k): for cur in s: if cur == prev: count += 1 prev = None else: prev = cur return count, prev if __name__ == "__main__": s = input() k = int(input()) print(count_changes(s, k)[0])
def count_changes(s, k, prev=None): count = 0 for _ in range(k): for cur in s: if cur == prev: count += 1 prev = None else: prev = cur return count, prev if __name__ == "__main__": s = input() k = int(input()) if len(set(s)) == 1: print((len(s) * k) // 2) exit() if k <= 10**8: print(count_changes(s, k)[0]) else: base_count, prev = count_changes(s, 3) d, rest = divmod(k, 3) res = base_count * d if rest: res += count_changes(s, rest, prev)[0] elif s[0] == prev: res += d - 1 print(res)
replace
15
16
15
29
TLE
p02891
C++
Runtime Error
#include <bits/stdc++.h> #define inf 1000000000 #define ll long long using namespace std; int main() { int len, locate, locate1, flag = 1; ll k, ans = 0, ans1 = 0, ans2 = 0, ans3 = 0; char a[105]; scanf("%s%lld", a, &k); len = strlen(a); if (k == 1) { for (int i = 0; i < len; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i < len) { i++; } i--; ans += (i - tmp1 + 1) / 2; } printf("%lld", ans); return 0; } for (int i = len; i <= 2 * len - 1; i++) a[i] = a[i - len]; for (int i = 0; i < 2 * len; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i < 2 * len) { i++; } i--; if (tmp1 <= len - 1 && i >= len) { locate = tmp1 - 1, locate1 = i - len + 1; ans1 = (i - tmp1 + 1) / 2; flag = 0; break; } } if (!flag) { if (locate == -1 && locate1 == len) printf("%lld", k * len / 2); else { for (int i = 0; i <= locate; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i <= locate) { i++; } i--; ans += (i - tmp1 + 1) / 2; } for (int i = locate1; i < len; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i < len) { i++; } i--; ans2 += (i - tmp1 + 1) / 2; } for (int i = locate1; i <= locate; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i <= locate) { i++; } i--; ans3 += (i - tmp1 + 1) / 2; } printf("%lld", ans3 * (k - 2) + ans1 * (k - 1) + ans + ans2); } } else { for (int i = 0; i < len; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i < len) { i++; } i--; ans += (i - tmp1 + 1) / 2; } printf("%lld", ans * k); } return 0; }
#include <bits/stdc++.h> #define inf 1000000000 #define ll long long using namespace std; int main() { int len, locate, locate1, flag = 1; ll k, ans = 0, ans1 = 0, ans2 = 0, ans3 = 0; char a[205]; scanf("%s%lld", a, &k); len = strlen(a); if (k == 1) { for (int i = 0; i < len; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i < len) { i++; } i--; ans += (i - tmp1 + 1) / 2; } printf("%lld", ans); return 0; } for (int i = len; i <= 2 * len - 1; i++) a[i] = a[i - len]; for (int i = 0; i < 2 * len; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i < 2 * len) { i++; } i--; if (tmp1 <= len - 1 && i >= len) { locate = tmp1 - 1, locate1 = i - len + 1; ans1 = (i - tmp1 + 1) / 2; flag = 0; break; } } if (!flag) { if (locate == -1 && locate1 == len) printf("%lld", k * len / 2); else { for (int i = 0; i <= locate; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i <= locate) { i++; } i--; ans += (i - tmp1 + 1) / 2; } for (int i = locate1; i < len; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i < len) { i++; } i--; ans2 += (i - tmp1 + 1) / 2; } for (int i = locate1; i <= locate; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i <= locate) { i++; } i--; ans3 += (i - tmp1 + 1) / 2; } printf("%lld", ans3 * (k - 2) + ans1 * (k - 1) + ans + ans2); } } else { for (int i = 0; i < len; i++) { int tmp1 = i; char tmp = a[i]; while (a[i] == tmp && i < len) { i++; } i--; ans += (i - tmp1 + 1) / 2; } printf("%lld", ans * k); } return 0; }
replace
7
8
7
8
0
p02891
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string s; cin >> s; long long k; cin >> k; int s_length = (int)s.length(); int tmp = 0; int buf = 0; vector<int> array(s_length, 0); long long ans = 0; if (s[0] == s[s_length - 1]) buf = 1; for (int i = 1; i < s_length; i++) { if (s[i] == s[i - 1]) { array[i] = array[i - 1] + 1; } if (array[i] % 2 == 1) ans++; } if (buf != 1) { ans = ans * k; cout << ans << endl; } long long ans2 = 0; long long ans3 = 0; long long pic = 0; if (buf == 1) { char s2[s_length * 2], s3[s_length * 2]; for (int i = 0; i < s_length; i++) { s3[i] = s[i]; s3[i + s_length] = s[i]; s3[i + s_length * 2] = s[i]; } vector<int> array3(s_length * 3, 0); for (int i = 1; i < s_length * 3; i++) { if (s3[i] == s3[i - 1]) { array3[i] = array3[i - 1] + 1; } if (i < s_length * 2 && array3[i] % 2 == 1) ans2++; if (array3[i] % 2 == 1) ans3++; ; } long long res = ans3 - ans; if (k == 1) pic = ans; if (k == 2) pic = ans2; if (k == 3) pic = ans3; if (k > 3) { if (k % 2 == 0) { pic = ans2 + res * (k / 2 - 1); } else { pic = ans + res * (k - 1) / 2; } } cout << pic << endl; } // cout<<s_length<<endl; return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string s; cin >> s; long long k; cin >> k; int s_length = (int)s.length(); int tmp = 0; int buf = 0; vector<int> array(s_length, 0); long long ans = 0; if (s[0] == s[s_length - 1]) buf = 1; for (int i = 1; i < s_length; i++) { if (s[i] == s[i - 1]) { array[i] = array[i - 1] + 1; } if (array[i] % 2 == 1) ans++; } if (buf != 1) { ans = ans * k; cout << ans << endl; } long long ans2 = 0; long long ans3 = 0; long long pic = 0; if (buf == 1) { char s2[s_length * 2], s3[s_length * 3]; for (int i = 0; i < s_length; i++) { s3[i] = s[i]; s3[i + s_length] = s[i]; s3[i + s_length * 2] = s[i]; } vector<int> array3(s_length * 3, 0); for (int i = 1; i < s_length * 3; i++) { if (s3[i] == s3[i - 1]) { array3[i] = array3[i - 1] + 1; } if (i < s_length * 2 && array3[i] % 2 == 1) ans2++; if (array3[i] % 2 == 1) ans3++; ; } long long res = ans3 - ans; if (k == 1) pic = ans; if (k == 2) pic = ans2; if (k == 3) pic = ans3; if (k > 3) { if (k % 2 == 0) { pic = ans2 + res * (k / 2 - 1); } else { pic = ans + res * (k - 1) / 2; } } cout << pic << endl; } // cout<<s_length<<endl; return 0; }
replace
32
33
32
33
0
p02891
C++
Runtime Error
// https://yamakasa.net/atcoder-agc-039-a/ #include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for (ll i = 0; i < (ll)n; i++) int main() { string S; ll K; cin >> S >> K; ll n = S.length(); if (n == 1) { cout << K / 2 << "\n"; return 0; } ll cnt = 1; REP(i, n - 1) { if (S[i] == S[i + 1]) { cnt++; } } if (cnt == n) { cout << K * n / 2 << endl; return 0; } ll l = 0; ll r = 0; for (int i = 0; i < n; i++) { if (S[0] == S[i]) { l++; } else { break; } } if (l == n) { cout << n * K / 2 << "\n"; return 0; } for (int i = n - 1; i >= 0; i--) { if (S[n - 1] == S[i]) { r++; } else { break; } } if (l + r == n) { return -1; } ll ans = 0; ll c = 1; for (int i = l; i < n - r; i++) { if (S[i] == S[i + 1]) { c++; } else { ans += c / 2 * K; c = 1; } } ans += c / 2 * (K - 1); if (S[0] == S[n - 1]) { ans += l / 2 + (l + r) / 2 * (K - 1) + r / 2; } else { ans += l / 2 * K + r / 2 * K; } // cout << l << " " << r << "\n"; cout << ans << "\n"; return 0; }
// https://yamakasa.net/atcoder-agc-039-a/ #include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for (ll i = 0; i < (ll)n; i++) int main() { string S; ll K; cin >> S >> K; ll n = S.length(); if (n == 1) { cout << K / 2 << "\n"; return 0; } ll cnt = 1; REP(i, n - 1) { if (S[i] == S[i + 1]) { cnt++; } } if (cnt == n) { cout << K * n / 2 << endl; return 0; } ll l = 0; ll r = 0; for (int i = 0; i < n; i++) { if (S[0] == S[i]) { l++; } else { break; } } if (l == n) { cout << n * K / 2 << "\n"; return 0; } for (int i = n - 1; i >= 0; i--) { if (S[n - 1] == S[i]) { r++; } else { break; } } if (l + r == n) { // S[0] != S[n - 1] // aaaaa nnnnnnnみたいなの ll ans = l / 2 * K + r / 2 * K; cout << ans << "\n"; return 0; } ll ans = 0; ll c = 1; for (int i = l; i < n - r; i++) { if (S[i] == S[i + 1]) { c++; } else { ans += c / 2 * K; c = 1; } } ans += c / 2 * (K - 1); if (S[0] == S[n - 1]) { ans += l / 2 + (l + r) / 2 * (K - 1) + r / 2; } else { ans += l / 2 * K + r / 2 * K; } // cout << l << " " << r << "\n"; cout << ans << "\n"; return 0; }
replace
45
46
45
50
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; string s; long long k; int main() { cin >> s >> k; int n = s.size(); if (n == 1) { cout << k / 2 << endl; return 0; } if (n == 2) { cout << (s[0] == s[1]) * k << endl; return 0; } long long a = 0, b = 0; while (s[a] == s[n - 1] && a < n) a++; while (b < n && s[0] == s[n - b - 1]) b++; long long cnt = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) cnt++, i++; } if (a == n) { assert(0); } cout << cnt * k - ((a / 2) + (b / 2) - ((a + b) / 2)) * (k - 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string s; long long k; int main() { cin >> s >> k; int n = s.size(); if (n == 1) { cout << k / 2 << endl; return 0; } if (n == 2) { cout << (s[0] == s[1]) * k << endl; return 0; } long long a = 0, b = 0; while (s[a] == s[n - 1] && a < n) a++; while (b < n && s[0] == s[n - b - 1]) b++; long long cnt = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) cnt++, i++; } if (a == n) { cout << n * k / 2 << endl; return 0; } cout << cnt * k - ((a / 2) + (b / 2) - ((a + b) / 2)) * (k - 1) << endl; return 0; }
replace
28
29
28
30
0
p02891
C++
Runtime Error
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <string> #include <vector> #define repl(i, l, r) for (ll i = l; i < r; i++) #define rep(i, n) repl(i, 0, n) using namespace std; using ll = long long; int main() { string S; ll K, N; cin >> S >> K; N = S.size(); vector<ll> c; ll tmp = 1; repl(i, 1, N) { if (S[i - 1] == S[i]) { tmp++; if (i == N - 1) { c.push_back(tmp); } } else { c.push_back(tmp); tmp = 1; } } ll ans = 0; ll n = c.size(); rep(i, n) { ans += c[i] / 2; } ans = ans * K; if (S.front() == S.back()) { ans = ans - (c.front() / 2 + c.back() / 2 - (c.front() + c.back()) / 2) * (K - 1); } if (n == 1) { ans = c.front() * K / 2; } cout << ans; return 0; }
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <string> #include <vector> #define repl(i, l, r) for (ll i = l; i < r; i++) #define rep(i, n) repl(i, 0, n) using namespace std; using ll = long long; int main() { string S; ll K, N; cin >> S >> K; N = S.size(); vector<ll> c; ll tmp = 1; repl(i, 1, N) { if (S[i - 1] == S[i]) { tmp++; if (i == N - 1) { c.push_back(tmp); } } else { c.push_back(tmp); tmp = 1; } } if (N == 1) { c.push_back(1); } ll ans = 0; ll n = c.size(); rep(i, n) { ans += c[i] / 2; } ans = ans * K; if (S.front() == S.back()) { ans = ans - (c.front() / 2 + c.back() / 2 - (c.front() + c.back()) / 2) * (K - 1); } if (n == 1) { ans = c.front() * K / 2; } cout << ans; return 0; }
insert
39
39
39
43
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define drep(i, n) for (int i = (n - 1); i >= 0; i--) #define all(v) (v).begin(), (v).end() template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } typedef pair<int, int> P; typedef long long ll; const int INF = 1001001001; const ll LINF = 1001002003004005006ll; const ll MOD = 1e9 + 7; int main() { string s; ll k; cin >> s >> k; vector<int> chrs; int num = 1; rep(i, s.size()) { if (i) { if (s[i - 1] == s[i]) { num++; } else { chrs.push_back(num); num = 1; } } } chrs.push_back(num); ll cnt = 0; // sの最初と最後の文字種を除いたs内の操作回数 rep(i, chrs.size() - 2) cnt += chrs[i + 1] / 2; ll ans = 0; if (chrs.size() == 1) { // 全結合 ans = (k * s.size()) / 2; } else if (s.front() == s.back()) { // 結合あり ans = cnt * k + chrs.front() / 2 + chrs.back() / 2 + ((chrs.front() + chrs.back()) / 2) * (k - 1); } else { // 結合なし ans = (cnt + chrs.front() / 2 + chrs.back() / 2) * k; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define drep(i, n) for (int i = (n - 1); i >= 0; i--) #define all(v) (v).begin(), (v).end() template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } typedef pair<int, int> P; typedef long long ll; const int INF = 1001001001; const ll LINF = 1001002003004005006ll; const ll MOD = 1e9 + 7; int main() { string s; ll k; cin >> s >> k; vector<int> chrs; int num = 1; rep(i, s.size()) { if (i) { if (s[i - 1] == s[i]) { num++; } else { chrs.push_back(num); num = 1; } } } chrs.push_back(num); ll cnt = 0; // sの最初と最後の文字種を除いたs内の操作回数 rep(i, int(chrs.size()) - 2) cnt += chrs[i + 1] / 2; ll ans = 0; if (chrs.size() == 1) { // 全結合 ans = (k * s.size()) / 2; } else if (s.front() == s.back()) { // 結合あり ans = cnt * k + chrs.front() / 2 + chrs.back() / 2 + ((chrs.front() + chrs.back()) / 2) * (k - 1); } else { // 結合なし ans = (cnt + chrs.front() / 2 + chrs.back() / 2) * k; } cout << ans << endl; return 0; }
replace
45
46
45
46
0
p02891
C++
Runtime Error
#include <cctype> #include <iostream> #include <string> using namespace std; char c[105]; long long a[105]; int main() { long long sum = 0; long long res = 0; long long m = 0, n; long long cnt = 0; char s; while (s = getchar()) { if (s == '\n') break; c[++m] = s; } cin >> n; if (m == 1) { cout << n / 2; return 0; } for (int i = 1; i < m; i++) { if (c[i] == c[i + 1]) { cnt++; } else { a[i - cnt] = cnt; cnt = 0; } } if (c[m] == c[m - 1]) { a[m - cnt] = cnt; } // for (int i = 1; i <= m; i++) // cout << a[i] << endl; for (int i = 1; i <= m; i++) { if (a[i] != 0) { res += (a[i] + 1) / 2; } } if (a[m - cnt] + 1 == m) { cout << n * m / 2; return 0; } if (c[1] == c[m]) { if ((a[m - cnt] + 1) % 2 == 0) { cout << res * n; return 0; } else { if ((a[1] + 1) % 2 == 0) { return res * n; } cout << (res + 1) * n - 1; return 0; } } cout << res * n; return 0; }
#include <cctype> #include <iostream> #include <string> using namespace std; char c[105]; long long a[105]; int main() { long long sum = 0; long long res = 0; long long m = 0, n; long long cnt = 0; char s; while (s = getchar()) { if (s == '\n') break; c[++m] = s; } cin >> n; if (m == 1) { cout << n / 2; return 0; } for (int i = 1; i < m; i++) { if (c[i] == c[i + 1]) { cnt++; } else { a[i - cnt] = cnt; cnt = 0; } } if (c[m] == c[m - 1]) { a[m - cnt] = cnt; } // for (int i = 1; i <= m; i++) // cout << a[i] << endl; for (int i = 1; i <= m; i++) { if (a[i] != 0) { res += (a[i] + 1) / 2; } } if (a[m - cnt] + 1 == m) { cout << n * m / 2; return 0; } if (c[1] == c[m]) { if ((a[m - cnt] + 1) % 2 == 0) { cout << res * n; return 0; } else { if (a[1] % 2 == 1) { cout << res * n; return 0; } cout << (res + 1) * n - 1; return 0; } } cout << res * n; return 0; }
replace
50
52
50
53
0
p02891
Python
Runtime Error
from itertools import groupby s = input() k = int(input()) a = [sum(1 for _ in g) for _, g in groupby(s)] if s[0] == s[-1]: a[0] += a.pop() print(sum(x // 2 for x in a) * k)
from itertools import groupby s = input() k = int(input()) a = [sum(1 for _ in g) for _, g in groupby(s)] if len(a) == 1: print(len(s) * k // 2) else: x, y = a[0], a[-1] b = (x + y) // 2 if s[0] == s[-1] else x // 2 + y // 2 print(x // 2 + y // 2 + sum(x // 2 for x in a[1:-1]) * k + b * (k - 1))
replace
5
8
5
11
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; const int INF = 1e10; const int ZERO = 0; signed main() { string S; cin >> S; int K; cin >> K; bool s = true; for (int i = 0; i < S.size(); i++) { if (S.at(i) != S.at(i + 1)) { s = false; } } if (S.size() == 1) { cout << K / 2 << endl; } else if (s == true) { cout << S.size() * K / 2 << endl; } else { bool cnt = false; int count = 1; int A = 0; int B = 0; for (int i = 0; i < S.size() - 1; i++) { if (S.at(i) != S.at(i + 1) && cnt == true) { A += count / 2; count = 1; cnt = false; } if (S.at(i) == S.at(i + 1)) { cnt = true; count++; } } if (cnt == true) { A += count / 2; cnt = false; } S = S + S; count = 1; for (int i = 0; i < S.size() - 1; i++) { if (S.at(i) != S.at(i + 1) && cnt == true) { B += count / 2; count = 1; cnt = false; } if (S.at(i) == S.at(i + 1)) { cnt = true; count++; } } if (cnt == true) { B += count / 2; } B = B - A; cout << A + B * (K - 1) << endl; } }
#include <bits/stdc++.h> #define int long long using namespace std; const int INF = 1e10; const int ZERO = 0; signed main() { string S; cin >> S; int K; cin >> K; bool s = true; for (int i = 0; i < S.size() - 1; i++) { if (S.at(i) != S.at(i + 1)) { s = false; } } if (S.size() == 1) { cout << K / 2 << endl; } else if (s == true) { cout << S.size() * K / 2 << endl; } else { bool cnt = false; int count = 1; int A = 0; int B = 0; for (int i = 0; i < S.size() - 1; i++) { if (S.at(i) != S.at(i + 1) && cnt == true) { A += count / 2; count = 1; cnt = false; } if (S.at(i) == S.at(i + 1)) { cnt = true; count++; } } if (cnt == true) { A += count / 2; cnt = false; } S = S + S; count = 1; for (int i = 0; i < S.size() - 1; i++) { if (S.at(i) != S.at(i + 1) && cnt == true) { B += count / 2; count = 1; cnt = false; } if (S.at(i) == S.at(i + 1)) { cnt = true; count++; } } if (cnt == true) { B += count / 2; } B = B - A; cout << A + B * (K - 1) << endl; } }
replace
12
13
12
13
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 5) >= this->size() (which is 5)
p02891
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef pair<int, int> pii; const int INF = 1l << 60; #define u_b upper_bound #define l_b lower_bound int f(string S) { S.push_back('!'); int res = 0; for (int i = 1; i < S.length(); ++i) { if (S[i] == S[i - 1]) { res += 1; S[i] = S[i + 1] + 1; } } return res; } signed main() { string S; int K; cin >> S >> K; if (S.length() == 1) { cout << K / 2 << endl; return 0; } else if (S.length() == 2) { if (S[0] == S[1]) { cout << K << endl; return 0; } else { cout << 0 << endl; return 0; } } else if (S.length() == 3) { if (S[0] == S[1] && S[1] == S[2]) { cout << 3 * K / 2; } else { if (S[0] == S[1] || S[1] == S[2]) { cout << K << endl; } else if (S[0] == S[2]) { cout << K - 1 << endl; } else { cout << 0 << endl; } } return 0; } int a3 = f(S + S + S); int a2 = f(S + S); int a1 = f(S); assert(a3 == 2 * (a2 - a1) + a1); cout << (a3 - a1) * ((K - 1) / 2) + (a2 - a1) * ((K - 1) % 2) + a1 << endl; }
#include <bits/stdc++.h> #define int long long using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef pair<int, int> pii; const int INF = 1l << 60; #define u_b upper_bound #define l_b lower_bound int f(string S) { S.push_back('!'); int res = 0; for (int i = 1; i < S.length(); ++i) { if (S[i] == S[i - 1]) { res += 1; S[i] = S[i + 1] + 1; } } return res; } signed main() { string S; int K; cin >> S >> K; if (S.length() == 1) { cout << K / 2 << endl; return 0; } else if (S.length() == 2) { if (S[0] == S[1]) { cout << K << endl; return 0; } else { cout << 0 << endl; return 0; } } else if (S.length() == 3) { if (S[0] == S[1] && S[1] == S[2]) { cout << 3 * K / 2; } else { if (S[0] == S[1] || S[1] == S[2]) { cout << K << endl; } else if (S[0] == S[2]) { cout << K - 1 << endl; } else { cout << 0 << endl; } } return 0; } int a3 = f(S + S + S); int a2 = f(S + S); int a1 = f(S); cout << (a3 - a1) * ((K - 1) / 2) + (a2 - a1) * ((K - 1) % 2) + a1 << endl; }
delete
54
55
54
54
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define endl "\n" #define REP(i, a, n) for (int i = a; i < n; ++i) #define REPR(i, a, n) for (int i = a; i > n; --i) #define RUP(a, b) ((a + b - 1) / (b)) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define mt make_tuple #define MOD 1000000007 #define INF LLONG_MAX / 2 typedef long long ll; typedef pair<int, int> Pii; typedef tuple<int, int, int> Tiii; typedef vector<int> Vi; typedef vector<Vi> VVi; typedef vector<Pii> VPii; typedef vector<string> Vs; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void YesNo(T a) { cout << (a ? "Yes" : "No") << endl; } template <class T> void YESNO(T a) { cout << (a ? "YES" : "NO") << endl; } void vin(Vi &v) { REP(i, 0, (v).size()) cin >> v[i]; } void vin(Vi &v, Vi &v2) { REP(i, 0, (v).size()) cin >> v[i] >> v2[i]; } void vout(Vi &v) { for (int i = 0; i < (v).size(); i++) cout << v[i] << " "; cout << endl; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } void uniq(Vi &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } void accum(Vi &v) { REP(i, 1, (v).size()) v[i] += v[i - 1]; } bool comp(Pii a, Pii b) { if (a.second != b.second) return a.second < b.second; else return a.first < b.first; } void solve(std::string s, long long k) { Vi a; int now = 1, ans = 0; REP(i, 1, s.size()) { if (s[i] == s[i - 1]) { now++; } else { a.pb(now); now = 1; } if (i == s.size() - 1) a.pb(now); } if (a.size() == 1) { cout << (s.size() * k) / 2 << endl; return; } if (s[0] != s[s.size() - 1]) REP(i, 0, a.size()) ans += (a[i] / 2) * k; else { REP(i, 1, a.size() - 1) ans += (a[i] / 2) * k; ans += a[0] / 2; ans += a[a.size() - 1] / 2; ans += (a[0] + a[a.size() - 1]) / 2 * (k - 1); } cout << ans << endl; } // Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools (tips: You // use the default template now. You can remove this line by using your custom // template) signed main() { std::string S; std::cin >> S; long long K; scanf("%lld", &K); solve(S, K); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define endl "\n" #define REP(i, a, n) for (int i = a; i < n; ++i) #define REPR(i, a, n) for (int i = a; i > n; --i) #define RUP(a, b) ((a + b - 1) / (b)) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define mt make_tuple #define MOD 1000000007 #define INF LLONG_MAX / 2 typedef long long ll; typedef pair<int, int> Pii; typedef tuple<int, int, int> Tiii; typedef vector<int> Vi; typedef vector<Vi> VVi; typedef vector<Pii> VPii; typedef vector<string> Vs; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void YesNo(T a) { cout << (a ? "Yes" : "No") << endl; } template <class T> void YESNO(T a) { cout << (a ? "YES" : "NO") << endl; } void vin(Vi &v) { REP(i, 0, (v).size()) cin >> v[i]; } void vin(Vi &v, Vi &v2) { REP(i, 0, (v).size()) cin >> v[i] >> v2[i]; } void vout(Vi &v) { for (int i = 0; i < (v).size(); i++) cout << v[i] << " "; cout << endl; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } void uniq(Vi &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } void accum(Vi &v) { REP(i, 1, (v).size()) v[i] += v[i - 1]; } bool comp(Pii a, Pii b) { if (a.second != b.second) return a.second < b.second; else return a.first < b.first; } void solve(std::string s, long long k) { Vi a; int now = 1, ans = 0; if (s.size() == 1) { cout << k / 2 << endl; return; } REP(i, 1, s.size()) { if (s[i] == s[i - 1]) { now++; } else { a.pb(now); now = 1; } if (i == s.size() - 1) a.pb(now); } if (a.size() == 1) { cout << (s.size() * k) / 2 << endl; return; } if (s[0] != s[s.size() - 1]) REP(i, 0, a.size()) ans += (a[i] / 2) * k; else { REP(i, 1, a.size() - 1) ans += (a[i] / 2) * k; ans += a[0] / 2; ans += a[a.size() - 1] / 2; ans += (a[0] + a[a.size() - 1]) / 2 * (k - 1); } cout << ans << endl; } // Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools (tips: You // use the default template now. You can remove this line by using your custom // template) signed main() { std::string S; std::cin >> S; long long K; scanf("%lld", &K); solve(S, K); return 0; }
insert
69
69
69
73
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #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 repd(i, n) for (int i = (int)(n - 1); i >= 0; --i) #define repds(i, n) for (int i = (int)(n); i > 0; --i) #define loop(i, x, n) for (int i = (int)(x); i < (n); ++i) #define loops(i, x, n) for (int i = (int)(x); i <= (n); ++i) #define loopd(i, x, n) for (int i = (int)(x); i > (n); --i) #define loopds(i, x, n) for (int i = (int)(x); i >= (n); --i) #define itrep(i, s) for (auto i = begin(s); i != end(s); ++i) #define itrepd(i, s) for (auto i = --end(s); i != begin(s); --i) #define all(f, x, ...) \ [&](decltype(x) &whole) { \ return (f)(begin(whole), end(whole), ##__VA_ARGS__); \ }(x) #define rall(f, x, ...) \ [&](decltype(x) &whole) { \ return (f)(rbegin(whole), rend(whole), ##__VA_ARGS__); \ }(x) using ll = long long; using ld = long double; constexpr ll inf = static_cast<ll>(1e17); constexpr int iinf = static_cast<int>(1e9); constexpr double dinf = 1e10; constexpr ld ldinf = 1e17; constexpr ll mod = static_cast<ll>(1e9 + 7); std::ostream &endn(std::ostream &os) { return os.put(os.widen('\n')); } template <class T> constexpr int sz(const T &a) { return (int)a.size(); } template <class T, class... Args> constexpr T mins(T &a, Args... args) { return a = min<T>({a, args...}); } template <class T, class... Args> constexpr T maxs(T &a, Args... args) { return a = max<T>({a, args...}); } string s; ll k; void solve() { cin >> s >> k; int n = sz(s); char b = ' '; ll res = 1; vector<ll> vec; rep(i, n - 1) { if (s[i] == s[i + 1]) ++res; else { vec.emplace_back(res); res = 1; } } if (res != 1) vec.emplace_back(res); ll ans = 0; auto m = sz(vec); if (m == 1) ans = vec[0] * k / 2; else if (s[0] == s[n - 1]) { loop(i, 1, m - 1) ans += vec[i] / 2; ans *= k; ans += (vec[0] + vec[m - 1]) / 2 * (k - 1) + vec[0] / 2 + vec[m - 1] / 2; } else { rep(i, m) ans += vec[i] / 2; ans *= k; } cout << ans << endn; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); cout << flush; return 0; }
#include <bits/stdc++.h> using namespace std; #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 repd(i, n) for (int i = (int)(n - 1); i >= 0; --i) #define repds(i, n) for (int i = (int)(n); i > 0; --i) #define loop(i, x, n) for (int i = (int)(x); i < (n); ++i) #define loops(i, x, n) for (int i = (int)(x); i <= (n); ++i) #define loopd(i, x, n) for (int i = (int)(x); i > (n); --i) #define loopds(i, x, n) for (int i = (int)(x); i >= (n); --i) #define itrep(i, s) for (auto i = begin(s); i != end(s); ++i) #define itrepd(i, s) for (auto i = --end(s); i != begin(s); --i) #define all(f, x, ...) \ [&](decltype(x) &whole) { \ return (f)(begin(whole), end(whole), ##__VA_ARGS__); \ }(x) #define rall(f, x, ...) \ [&](decltype(x) &whole) { \ return (f)(rbegin(whole), rend(whole), ##__VA_ARGS__); \ }(x) using ll = long long; using ld = long double; constexpr ll inf = static_cast<ll>(1e17); constexpr int iinf = static_cast<int>(1e9); constexpr double dinf = 1e10; constexpr ld ldinf = 1e17; constexpr ll mod = static_cast<ll>(1e9 + 7); std::ostream &endn(std::ostream &os) { return os.put(os.widen('\n')); } template <class T> constexpr int sz(const T &a) { return (int)a.size(); } template <class T, class... Args> constexpr T mins(T &a, Args... args) { return a = min<T>({a, args...}); } template <class T, class... Args> constexpr T maxs(T &a, Args... args) { return a = max<T>({a, args...}); } string s; ll k; void solve() { cin >> s >> k; int n = sz(s); char b = ' '; ll res = 1; vector<ll> vec; rep(i, n - 1) { if (s[i] == s[i + 1]) ++res; else { vec.emplace_back(res); res = 1; } } vec.emplace_back(res); ll ans = 0; auto m = sz(vec); if (m == 1) ans = vec[0] * k / 2; else if (s[0] == s[n - 1]) { loop(i, 1, m - 1) ans += vec[i] / 2; ans *= k; ans += (vec[0] + vec[m - 1]) / 2 * (k - 1) + vec[0] / 2 + vec[m - 1] / 2; } else { rep(i, m) ans += vec[i] / 2; ans *= k; } cout << ans << endn; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); cout << flush; return 0; }
replace
54
56
54
55
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string S; long long K; cin >> S >> K; vector<int> len; int cnt = 1; for (int i = 1; i < S.size(); ++i) { if (S[i] == S[i - 1]) ++cnt; if (S[i] != S[i - 1] || i + 1 == S.size()) { len.push_back(cnt); cnt = 1; } } if (len.size() && len[0] == S.size()) { cout << K * S.size() / 2 << endl; return 0; } long long res = 0; for (int n : len) res += n / 2; if (S[0] != S[S.size() - 1]) cout << res * K << endl; else cout << res * K - (*begin(len) / 2 + *rbegin(len) / 2 - (*begin(len) + *rbegin(len)) / 2) * (K - 1) << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string S; long long K; cin >> S >> K; vector<int> len; int cnt = 1; if (S.size() == 1) len.push_back(1); for (int i = 1; i < S.size(); ++i) { if (S[i] == S[i - 1]) ++cnt; if (S[i] != S[i - 1] || i + 1 == S.size()) { len.push_back(cnt); cnt = 1; } } if (len.size() && len[0] == S.size()) { cout << K * S.size() / 2 << endl; return 0; } long long res = 0; for (int n : len) res += n / 2; if (S[0] != S[S.size() - 1]) cout << res * K << endl; else cout << res * K - (*begin(len) / 2 + *rbegin(len) / 2 - (*begin(len) + *rbegin(len)) / 2) * (K - 1) << endl; }
insert
10
10
10
12
0
p02891
C++
Runtime Error
#include <iostream> #include <sstream> #include <stdio.h> #define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cmath> #include <ctype.h> #include <limits> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> typedef long long int lli; #define rep(i, s, N) for (int i = s; i < N; i++) #define MOD 1000000007 #define more(a, b) (((a) > (b)) ? (a) : (b)) #define less(a, b) (((a) < (b)) ? (a) : (b)) using namespace std; int main(void) { string S; cin >> S; lli K; cin >> K; char tmp = S[0]; vector<int> cnt; int a = 1; rep(i, 1, S.length()) { if (tmp != S[i]) { cnt.push_back(a); a = 1; tmp = S[i]; } else a++; if (i == S.length() - 1) cnt.push_back(a); } // rep(i, 0, cnt.size())cout << cnt[i] << " " << endl; if (cnt.size() == 1) { cout << K * cnt[0] / 2 << endl; return 0; } lli ans = 0; rep(i, 0, cnt.size()) ans += cnt[i] / 2; ans *= K; if (S[0] == S[S.length() - 1]) ans += ((cnt[0] + cnt[cnt.size() - 1]) / 2 - cnt[0] / 2 - cnt[cnt.size() - 1] / 2) * (K - 1); cout << ans << endl; return 0; }
#include <iostream> #include <sstream> #include <stdio.h> #define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cmath> #include <ctype.h> #include <limits> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> typedef long long int lli; #define rep(i, s, N) for (int i = s; i < N; i++) #define MOD 1000000007 #define more(a, b) (((a) > (b)) ? (a) : (b)) #define less(a, b) (((a) < (b)) ? (a) : (b)) using namespace std; int main(void) { string S; cin >> S; lli K; cin >> K; char tmp = S[0]; vector<int> cnt; int a = 0; rep(i, 0, S.length()) { if (tmp != S[i]) { cnt.push_back(a); a = 1; tmp = S[i]; } else a++; if (i == S.length() - 1) cnt.push_back(a); } // rep(i, 0, cnt.size())cout << cnt[i] << " " << endl; if (cnt.size() == 1) { cout << K * cnt[0] / 2 << endl; return 0; } lli ans = 0; rep(i, 0, cnt.size()) ans += cnt[i] / 2; ans *= K; if (S[0] == S[S.length() - 1]) ans += ((cnt[0] + cnt[cnt.size() - 1]) / 2 - cnt[0] / 2 - cnt[cnt.size() - 1] / 2) * (K - 1); cout << ans << endl; return 0; }
replace
32
34
32
34
0
p02891
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #ifdef DEBUG #include "./Lib/debug.hpp" #else #define dump(...) template <class T> inline auto d_val(T a, T b) { return a; } #endif /* (=^o^=) */ #define int ll /* macro */ #define FOR(i, b, e) for (ll i = (ll)(b); i < (ll)(e); ++i) #define RFOR(i, b, e) for (ll i = (ll)(e - 1); i >= (ll)(b); --i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define REPC(x, c) for (const auto &x : (c)) #define REPI2(it, b, e) for (auto it = (b); it != (e); ++it) #define REPI(it, c) REPI2(it, (c).begin(), (c).end()) #define RREPI(it, c) REPI2(it, (c).rbegin(), (c).rend()) #define REPI_ERACE2(it, b, e) for (auto it = (b); it != (e);) #define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end()) #define ALL(x) (x).begin(), (x).end() #define cauto const auto & /* macro func */ #define SORT(x) \ do { \ sort(ALL(x)); \ } while (false) #define RSORT(x) \ do { \ sort((x).rbegin(), (x).rend()); \ } while (false) #define UNIQUE(v) \ do { \ v.erase(unique(v.begin(), v.end()), v.end()); \ } while (false) #define MAX(x, y) \ do { \ x = std::max(x, y); \ } while (false) #define MIN(x, y) \ do { \ x = std::min(x, y); \ } while (false) #define BR \ do { \ cout << "\n"; \ } while (false) /* type define */ using ll = long long; using PAIR = std::pair<ll, ll>; using VS = std::vector<std::string>; using VL = std::vector<long long>; using VVL = std::vector<VL>; using VVVL = std::vector<VVL>; using VD = std::vector<double>; template <class T> using V = std::vector<T>; /* using std */ using std::cout; constexpr char endl = '\n'; using std::bitset; using std::cin; using std::list; using std::map; using std::multimap; using std::multiset; using std::pair; using std::priority_queue; using std::queue; using std::set; using std::sort; using std::stack; using std::string; using std::unordered_map; using std::unordered_multimap; using std::unordered_multiset; using std::unordered_set; using std::vector; /* constant value */ constexpr ll MOD = 1000000007; // constexpr ll MOD = 998244353; /* Initial processing */ struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; } _Preprocessing; /* Remove the source of the bug */ signed pow(signed, signed) { assert(false); return -1; } /* define hash */ namespace std { template <> class hash<std::pair<ll, ll>> { public: size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(1000000000 * x.first + x.second); } }; } // namespace std /* input */ template <class T> std::istream &operator>>(std::istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } //============================================================================================= signed main() { string str; cin >> str; ll n; cin >> n; ll cnt = 0; ll sz = str.size(); VL ans; ans.reserve(1e6); ll t = 1; bool bf = false; bool ok = false; REP(i, sz - 1) { if (str[i] == str[i + 1]) { if (bf) { bf = false; continue; } ++cnt; bf = true; if (i == sz - 2) { ok = true; } } else { bf = false; } } if (str[0] != str[sz - 1]) { ok = true; } ans.emplace_back(cnt); while (!ok) { ++t; REP(i, sz) { if (str[(i - 1 + sz) % sz] == str[i]) { if (bf) { bf = false; continue; } ++cnt; bf = true; if (i == sz - 1) { ok = true; } } else { bf = false; } } ans.emplace_back(cnt); } ll p = cnt * (n / t); dump(p); ll c = (n % t) - 1; if (c >= 0) { p += ans[c]; } cout << p << endl; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #ifdef DEBUG #include "./Lib/debug.hpp" #else #define dump(...) template <class T> inline auto d_val(T a, T b) { return a; } #endif /* (=^o^=) */ #define int ll /* macro */ #define FOR(i, b, e) for (ll i = (ll)(b); i < (ll)(e); ++i) #define RFOR(i, b, e) for (ll i = (ll)(e - 1); i >= (ll)(b); --i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define REPC(x, c) for (const auto &x : (c)) #define REPI2(it, b, e) for (auto it = (b); it != (e); ++it) #define REPI(it, c) REPI2(it, (c).begin(), (c).end()) #define RREPI(it, c) REPI2(it, (c).rbegin(), (c).rend()) #define REPI_ERACE2(it, b, e) for (auto it = (b); it != (e);) #define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end()) #define ALL(x) (x).begin(), (x).end() #define cauto const auto & /* macro func */ #define SORT(x) \ do { \ sort(ALL(x)); \ } while (false) #define RSORT(x) \ do { \ sort((x).rbegin(), (x).rend()); \ } while (false) #define UNIQUE(v) \ do { \ v.erase(unique(v.begin(), v.end()), v.end()); \ } while (false) #define MAX(x, y) \ do { \ x = std::max(x, y); \ } while (false) #define MIN(x, y) \ do { \ x = std::min(x, y); \ } while (false) #define BR \ do { \ cout << "\n"; \ } while (false) /* type define */ using ll = long long; using PAIR = std::pair<ll, ll>; using VS = std::vector<std::string>; using VL = std::vector<long long>; using VVL = std::vector<VL>; using VVVL = std::vector<VVL>; using VD = std::vector<double>; template <class T> using V = std::vector<T>; /* using std */ using std::cout; constexpr char endl = '\n'; using std::bitset; using std::cin; using std::list; using std::map; using std::multimap; using std::multiset; using std::pair; using std::priority_queue; using std::queue; using std::set; using std::sort; using std::stack; using std::string; using std::unordered_map; using std::unordered_multimap; using std::unordered_multiset; using std::unordered_set; using std::vector; /* constant value */ constexpr ll MOD = 1000000007; // constexpr ll MOD = 998244353; /* Initial processing */ struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; } _Preprocessing; /* Remove the source of the bug */ signed pow(signed, signed) { assert(false); return -1; } /* define hash */ namespace std { template <> class hash<std::pair<ll, ll>> { public: size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(1000000000 * x.first + x.second); } }; } // namespace std /* input */ template <class T> std::istream &operator>>(std::istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } //============================================================================================= signed main() { string str; cin >> str; ll n; cin >> n; ll cnt = 0; ll sz = str.size(); VL ans; ans.reserve(1e6); ll t = 1; bool bf = false; bool ok = false; REP(i, sz - 1) { if (str[i] == str[i + 1]) { if (bf) { bf = false; continue; } ++cnt; bf = true; if (i == sz - 2) { ok = true; } } else { bf = false; } } if (str[0] != str[sz - 1]) { ok = true; } ans.emplace_back(cnt); while (!ok) { ++t; REP(i, sz) { if (str[(i - 1 + sz) % sz] == str[i]) { if (bf) { bf = false; continue; } ++cnt; bf = true; if (i == sz - 1) { ok = true; } } else { bf = false; } } ans.emplace_back(cnt); if (t == n) { cout << cnt << endl; return 0; } if (t == 1e3) { dump("in"); ll f = ans[0]; ll def = ans[101] - ans[100]; cout << f + def * (n - 1) << endl; return 0; } } ll p = cnt * (n / t); dump(p); ll c = (n % t) - 1; if (c >= 0) { p += ans[c]; } cout << p << endl; }
insert
185
185
185
196
TLE
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long char S[105]; int K, las = 1; vector<int> vec; signed main() { cin >> (S + 1) >> K; int n = strlen(S + 1); for (int i = 2; i <= n; ++i) if (S[i] != S[i - 1]) vec.push_back(i - las), las = i; else if (i == n) vec.push_back(i - las + 1); // for(auto v:vec) cout<<v<<endl; if (vec.size() == 1) return cout << n * K / 2, 0; if (S[1] == S[n]) { int ans = 0; for (int i = 1; i < vec.size() - 1; ++i) ans += vec[i] / 2; ans *= K; ans += (vec.front() + vec.back()) / 2 * (K - 1); ans += vec.front() / 2 + vec.back() / 2; cout << ans; } else { int ans = 0; for (auto v : vec) ans += v / 2 * K; cout << ans; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long char S[105]; int K, las = 1; vector<int> vec; signed main() { cin >> (S + 1) >> K; int n = strlen(S + 1); if (n == 1) return cout << n * K / 2, 0; for (int i = 2; i <= n; ++i) if (S[i] != S[i - 1]) vec.push_back(i - las), las = i; else if (i == n) vec.push_back(i - las + 1); // for(auto v:vec) cout<<v<<endl; if (vec.size() == 1) return cout << n * K / 2, 0; if (S[1] == S[n]) { int ans = 0; for (int i = 1; i < vec.size() - 1; ++i) ans += vec[i] / 2; ans *= K; ans += (vec.front() + vec.back()) / 2 * (K - 1); ans += vec.front() / 2 + vec.back() / 2; cout << ans; } else { int ans = 0; for (auto v : vec) ans += v / 2 * K; cout << ans; } return 0; }
insert
9
9
9
11
0
p02891
C++
Runtime Error
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <deque> #include <iomanip> #include <ios> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> #define itn int #define retrun return #define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) using namespace std; typedef long long llong; llong gcd(llong a, llong b) { if (a < b) std::swap(a, b); if (a % b == 0) return b; else return gcd(b, a % b); } llong lcm(llong a, llong b) { llong g = gcd(a, b); return a * b / g; } // ここまで共通 string S; llong K; llong ANS = 0; vector<llong> A; int main() { cin >> S; cin >> K; llong tmp = 1; for (int i = 1; i < S.length(); i++) { if (S[i - 1] == S[i]) { tmp++; if (i == S.length() - 1) { A.push_back(tmp); } } else { A.push_back(tmp); tmp = 1; } } if (A.size() == 1) { ANS = (A[0] * K) / 2; } else if (S[0] == S[S.length() - 1]) { llong a = A[0]; llong b = A[A.size() - 1]; A[0] += A[A.size() - 1]; A.pop_back(); for (int i = 0; i < A.size(); i++) { ANS += A[i] / 2; } ANS *= K; ANS -= A[0] / 2; ANS += a / 2 + b / 2; } else { for (int i = 0; i < A.size(); i++) { ANS += A[i] / 2; } ANS *= K; } cout << ANS << endl; return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <deque> #include <iomanip> #include <ios> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> #define itn int #define retrun return #define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) using namespace std; typedef long long llong; llong gcd(llong a, llong b) { if (a < b) std::swap(a, b); if (a % b == 0) return b; else return gcd(b, a % b); } llong lcm(llong a, llong b) { llong g = gcd(a, b); return a * b / g; } // ここまで共通 string S; llong K; llong ANS = 0; vector<llong> A; int main() { cin >> S; cin >> K; llong tmp = 1; if (S.length() == 1) { A.push_back(1); } for (int i = 1; i < S.length(); i++) { if (S[i - 1] == S[i]) { tmp++; if (i == S.length() - 1) { A.push_back(tmp); } } else { A.push_back(tmp); tmp = 1; } } if (A.size() == 1) { ANS = (A[0] * K) / 2; } else if (S[0] == S[S.length() - 1]) { llong a = A[0]; llong b = A[A.size() - 1]; A[0] += A[A.size() - 1]; A.pop_back(); for (int i = 0; i < A.size(); i++) { ANS += A[i] / 2; } ANS *= K; ANS -= A[0] / 2; ANS += a / 2 + b / 2; } else { for (int i = 0; i < A.size(); i++) { ANS += A[i] / 2; } ANS *= K; } cout << ANS << endl; return 0; }
insert
45
45
45
48
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> int main() { std::string s[3]; long long k, a[3] = {}; std::cin >> s[0] >> k; for (size_t i = 0; i < 2; ++i) s[i + 1] = s[i] + s[0]; for (size_t i = 0; i < 3; ++i) for (size_t j = 1; j < s[i].length(); ++j) if (s[i][j] == s[i][j - 1]) ++a[i], ++j; std::cout << (k & 1 ? a[0] : a[1]) + (k - 1) / 2 * (a[2] - a[0]) << std::endl; assert(a[0] + a[1] == a[2]); }
#include <bits/stdc++.h> int main() { std::string s[3]; long long k, a[3] = {}; std::cin >> s[0] >> k; for (size_t i = 0; i < 2; ++i) s[i + 1] = s[i] + s[0]; for (size_t i = 0; i < 3; ++i) for (size_t j = 1; j < s[i].length(); ++j) if (s[i][j] == s[i][j - 1]) ++a[i], ++j; std::cout << (k & 1 ? a[0] : a[1]) + (k - 1) / 2 * (a[2] - a[0]) << std::endl; assert(a[0] + a[1] <= a[2]); }
replace
12
13
12
13
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { string S; ll K, ans, l = 1; cin >> S >> K; vector<pair<char, ll>> v; for (ll i = 0; i < S.length(); i++) { if (i == 0) continue; if (S[i] == S[i - 1]) l++; else { v.push_back(make_pair(S[i - 1], l)); l = 1; } if (i == S.length() - 1) v.push_back(make_pair(S[i], l)); } ans = 0; if (v.size() == 1) ans = v[0].second * K / 2; else { for (ll i = 1; i < v.size() - 1; i++) { ans += v[i].second / 2 * K; } if (v[v.size() - 1].first == v[0].first) { ans += (v[0].second + v[v.size() - 1].second) / 2 * (K - 1); ans += v[v.size() - 1].second / 2; ans += v[0].second / 2; } else { ans += v[v.size() - 1].second / 2 * K; ans += v[0].second / 2 * K; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { string S; ll K, ans, l = 1; cin >> S >> K; vector<pair<char, ll>> v; for (ll i = 0; i < S.length(); i++) { if (i == 0) continue; if (S[i] == S[i - 1]) l++; else { v.push_back(make_pair(S[i - 1], l)); l = 1; } if (i == S.length() - 1) v.push_back(make_pair(S[i], l)); } if (S.length() == 1) v.push_back(make_pair(S[0], 1)); ans = 0; if (v.size() == 1) ans = v[0].second * K / 2; else { for (ll i = 1; i < v.size() - 1; i++) { ans += v[i].second / 2 * K; } if (v[v.size() - 1].first == v[0].first) { ans += (v[0].second + v[v.size() - 1].second) / 2 * (K - 1); ans += v[v.size() - 1].second / 2; ans += v[0].second / 2; } else { ans += v[v.size() - 1].second / 2 * K; ans += v[0].second / 2 * K; } } cout << ans << endl; }
insert
21
21
21
23
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; char d[100004]; long long k, cnt = 0; vector<long long> v; int main() { scanf("%s", d); scanf("%lld", &k); char bef = d[0]; int cc = 1; for (long long i = 1; i < strlen(d); i++) { if (bef == d[i]) cc++; else { v.push_back(cc); cc = 1; bef = d[i]; } } if (cc != 1) v.push_back(cc); /* for (int i=0; i<v.size(); i++) printf("%d ",v[i]); printf("\n"); */ if (v.size() == 1) { printf("%lld", (v[0] * k) / 2); } else if (d[strlen(d) - 1] == d[0] && k > 1) { for (long long i = 1; i < v.size() - 1; i++) cnt += v[i] / 2; cnt = cnt * k; cnt += (v[0] + v[v.size() - 1]) / 2 * (k - 1); cnt += v[0] / 2 + v[v.size() - 1] / 2; printf("%lld", cnt); } else { for (int i = 0; i < v.size(); i++) cnt += v[i] / 2; printf("%lld", cnt * k); } return 0; }
#include <bits/stdc++.h> using namespace std; char d[100004]; long long k, cnt = 0; vector<long long> v; int main() { scanf("%s", d); scanf("%lld", &k); char bef = d[0]; int cc = 1; for (long long i = 1; i < strlen(d); i++) { if (bef == d[i]) cc++; else { v.push_back(cc); cc = 1; bef = d[i]; } } if (cc != 1) v.push_back(cc); /* for (int i=0; i<v.size(); i++) printf("%d ",v[i]); printf("\n"); */ if (v.size() == 1) { printf("%lld", (v[0] * k) / 2); } else if (v.size() == 0 && strlen(d) > 1) { if (d[0] == d[strlen(d) - 1]) printf("%lld", k - 1); else printf("0"); } else if (v.size() == 0 && strlen(d) == 1) { printf("%lld", k / 2); } else if (d[strlen(d) - 1] == d[0] && k > 1) { for (long long i = 1; i < v.size() - 1; i++) cnt += v[i] / 2; cnt = cnt * k; cnt += (v[0] + v[v.size() - 1]) / 2 * (k - 1); cnt += v[0] / 2 + v[v.size() - 1] / 2; printf("%lld", cnt); } else { for (int i = 0; i < v.size(); i++) cnt += v[i] / 2; printf("%lld", cnt * k); } return 0; }
insert
28
28
28
35
0
p02891
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> using namespace std; using ll = long long; using ld = long double; const int INF = 1e9; const double EPS = 1e-9; const ll MOD = 1e9 + 7; int main() { std::string S; std::cin >> S; long long K; scanf("%lld", &K); vector<pair<char, int>> v; int sum = 0; for (int i = 0; i < S.size() - 1; ++i) { if (S[i] == S[i + 1]) { sum++; } else { v.emplace_back(S[i], sum + 1); sum = 0; } } if (sum > 0) v.emplace_back(S[S.size() - 1], sum + 1); ll ans = 0; if (v.size() == 1) { ans += v[0].second * K / 2; } else { for (int i = 1; i < v.size() - 1; ++i) { ans += v[i].second / 2 * K; } if (v[0].first == v[v.size() - 1].first) { ans += v[0].second / 2 + v[v.size() - 1].second / 2; ans += (v[0].second + v[v.size() - 1].second) / 2 * (K - 1); } else { ans += v[0].second / 2 * K + v[v.size() - 1].second / 2 * K; } } cout << ans << endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> using namespace std; using ll = long long; using ld = long double; const int INF = 1e9; const double EPS = 1e-9; const ll MOD = 1e9 + 7; int main() { std::string S; std::cin >> S; long long K; scanf("%lld", &K); vector<pair<char, int>> v; int sum = 0; for (int i = 0; i < S.size() - 1; ++i) { if (S[i] == S[i + 1]) { sum++; } else { v.emplace_back(S[i], sum + 1); sum = 0; } } if (sum > 0) v.emplace_back(S[S.size() - 1], sum + 1); ll ans = 0; if (v.empty()) ans = K / 2; else if (v.size() == 1) { ans += v[0].second * K / 2; } else { for (int i = 1; i < v.size() - 1; ++i) { ans += v[i].second / 2 * K; } if (v[0].first == v[v.size() - 1].first) { ans += v[0].second / 2 + v[v.size() - 1].second / 2; ans += (v[0].second + v[v.size() - 1].second) / 2 * (K - 1); } else { ans += v[0].second / 2 * K + v[v.size() - 1].second / 2 * K; } } cout << ans << endl; return 0; }
replace
56
57
56
59
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef __int128 ll; typedef pair<int, int> pii; typedef vector<int> vi; // const ll mod = 3006703054056749LL; // const ll mod = 1000000009; const ll mod = 31443539979727LL; ll modpow(ll b, ll e) { ll ans = 1; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } vector<ll> BerlekampMassey(vector<ll> s) { int n = sz(s), L = 0, m = 0; vector<ll> C(n), B(n), T; C[0] = B[0] = 1; ll b = 1; rep(i, 0, n) { ++m; ll d = s[i] % mod; rep(j, 1, L + 1) d = (d + C[j] * s[i - j]) % mod; if (!d) continue; T = C; ll coef = d * modpow(b, mod - 2) % mod; rep(j, m, n) C[j] = (C[j] - coef * B[j - m]) % mod; if (2 * L > i) continue; L = i + 1 - L; B = T; b = d; m = 0; } C.resize(L + 1); C.erase(C.begin()); trav(x, C) x = (mod - x) % mod; return C; } typedef vector<ll> Poly; ll linearRec(Poly S, Poly tr, ll k) { int n = sz(tr); auto combine = [&](Poly a, Poly b) { Poly res(n * 2 + 1); rep(i, 0, n + 1) rep(j, 0, n + 1) res[i + j] = (res[i + j] + a[i] * b[j]) % mod; for (int i = 2 * n; i > n; --i) rep(j, 0, n) res[i - 1 - j] = (res[i - 1 - j] + res[i] * tr[j]) % mod; res.resize(n + 1); return res; }; Poly pol(n + 1), e(pol); pol[0] = e[1] = 1; for (++k; k; k /= 2) { if (k % 2) pol = combine(pol, e); e = combine(e, e); } ll res = 0; rep(i, 0, n) res = (res + pol[i + 1] * S[i]) % mod; return res; } int slv(string s) { char lst = '#'; int cnt = 0; trav(c, s) { if (c == lst) { ++cnt; lst = '#'; } else lst = c; } return cnt; } #ifdef LOCAL #include "pretty_debug.h" #else #define debug(...) // ignore #endif int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); string s; int k; cin >> s >> k; vector<ll> S(50); string t = s; rep(i, 0, 50) { S[i] = slv(t); t += s; } vector<ll> tr = BerlekampMassey(S); debug(vector<long long>(all(tr))); ll ans = linearRec(S, tr, k - 1); cout << (long long)(ans) << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef __int128 ll; typedef pair<int, int> pii; typedef vector<int> vi; // const ll mod = 3006703054056749LL; // const ll mod = 1000000009; const ll mod = 31443539979727LL; ll modpow(ll b, ll e) { ll ans = 1; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } vector<ll> BerlekampMassey(vector<ll> s) { int n = sz(s), L = 0, m = 0; vector<ll> C(n), B(n), T; C[0] = B[0] = 1; ll b = 1; rep(i, 0, n) { ++m; ll d = s[i] % mod; rep(j, 1, L + 1) d = (d + C[j] * s[i - j]) % mod; if (!d) continue; T = C; ll coef = d * modpow(b, mod - 2) % mod; rep(j, m, n) C[j] = (C[j] - coef * B[j - m]) % mod; if (2 * L > i) continue; L = i + 1 - L; B = T; b = d; m = 0; } C.resize(L + 1); C.erase(C.begin()); trav(x, C) x = (mod - x) % mod; return C; } typedef vector<ll> Poly; ll linearRec(Poly S, Poly tr, ll k) { int n = sz(tr); auto combine = [&](Poly a, Poly b) { Poly res(n * 2 + 1); rep(i, 0, n + 1) rep(j, 0, n + 1) res[i + j] = (res[i + j] + a[i] * b[j]) % mod; for (int i = 2 * n; i > n; --i) rep(j, 0, n) res[i - 1 - j] = (res[i - 1 - j] + res[i] * tr[j]) % mod; res.resize(n + 1); return res; }; Poly pol(n + 1), e(pol); pol[0] = e[1] = 1; for (++k; k; k /= 2) { if (k % 2) pol = combine(pol, e); e = combine(e, e); } ll res = 0; rep(i, 0, n) res = (res + pol[i + 1] * S[i]) % mod; return res; } int slv(string s) { char lst = '#'; int cnt = 0; trav(c, s) { if (c == lst) { ++cnt; lst = '#'; } else lst = c; } return cnt; } #ifdef LOCAL #include "pretty_debug.h" #else #define debug(...) // ignore #endif int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); string s; int k; cin >> s >> k; vector<ll> S(50); string t = s; rep(i, 0, 50) { S[i] = slv(t); t += s; } vector<ll> tr = BerlekampMassey(S); debug(vector<long long>(all(tr))); ll ans = tr.empty() ? 0 : linearRec(S, tr, k - 1); cout << (long long)(ans) << endl; }
replace
114
115
114
115
0
p02891
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long typedef long long LL; template <class T> inline void read(T &x) { x = 0; char c = getchar(); bool f = 0; for (; !isdigit(c); c = getchar()) f ^= c == '-'; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); x = f ? -x : x; } template <class T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } T y = 1; int len = 1; for (; y <= x / 10; y *= 10) ++len; for (; len; --len, x %= y, y /= 10) putchar(x / y + 48); } string s; int k, len = 1; LL ans; signed main() { freopen("data.in", "r", stdin); freopen("my.out", "w", stdout); cin >> s; read(k); if (k == 1 || s[0] != s[(int)s.size() - 1]) { for (int i = 1; i < (int)s.size(); ++i) if (s[i] == s[i - 1]) ++len; else { ans += len / 2; len = 1; } ans += len / 2; write(ans * k); putchar('\n'); } else { int l, r; for (l = 1; s[l] == s[l - 1] && l < (int)s.size(); ++l) ; for (r = s.size() - 1; s[r] == s[r - 1] && r; --r) ; if (l >= r) { write((int)s.size() * k / 2); putchar('\n'); } else { for (int i = l + 1; i < r; ++i) if (s[i] == s[i - 1]) ++len; else { ans += len / 2; len = 1; } ans += len / 2; write(ans * k + l / 2 + ((int)s.size() - r) / 2 + ((int)s.size() - r + l) / 2 * (k - 1)); putchar('\n'); } } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long typedef long long LL; template <class T> inline void read(T &x) { x = 0; char c = getchar(); bool f = 0; for (; !isdigit(c); c = getchar()) f ^= c == '-'; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); x = f ? -x : x; } template <class T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } T y = 1; int len = 1; for (; y <= x / 10; y *= 10) ++len; for (; len; --len, x %= y, y /= 10) putchar(x / y + 48); } string s; int k, len = 1; LL ans; signed main() { cin >> s; read(k); if (k == 1 || s[0] != s[(int)s.size() - 1]) { for (int i = 1; i < (int)s.size(); ++i) if (s[i] == s[i - 1]) ++len; else { ans += len / 2; len = 1; } ans += len / 2; write(ans * k); putchar('\n'); } else { int l, r; for (l = 1; s[l] == s[l - 1] && l < (int)s.size(); ++l) ; for (r = s.size() - 1; s[r] == s[r - 1] && r; --r) ; if (l >= r) { write((int)s.size() * k / 2); putchar('\n'); } else { for (int i = l + 1; i < r; ++i) if (s[i] == s[i - 1]) ++len; else { ans += len / 2; len = 1; } ans += len / 2; write(ans * k + l / 2 + ((int)s.size() - r) / 2 + ((int)s.size() - r + l) / 2 * (k - 1)); putchar('\n'); } } return 0; }
delete
34
36
34
34
TLE
p02891
C++
Runtime Error
#include <bits/stdc++.h> #include <bitset> #include <chrono> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <random> #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define REP(i, a, b) for (int i = int(a); i <= int(b); i++) #define MD 1000000007 #define MOD2 1000000009 #define INF 2000000000 #define DESPACITO 1000000000000000 #define E 998244353 #define pb push_back #define mp make_pair #define double long double #define pdd pair<double, double> #define ll long long using namespace std; // using namespace __gnu_pbds; // typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> pbds; int main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string s; ll k; cin >> s; cin >> k; int n = s.length(); int l = 0; REP(i, 0, s.length() - 1) { if (s[i] == s[0]) l++; else break; } int r = 0; for (int i = s.length() - 1; i >= 0; i--) { if (s[i] == s[s.length() - 1]) r++; else break; } if (l - 1 >= n - r) { cout << (1LL * k * (s.length())) / 2; return 0; } ll x = 1; ll ans = 0; ll ct = 0; REP(i, 1, s.length() - 1) { if (s[i] == s[i - 1]) x++; else { ct += (x / 2); x = 1; } } ct += (x / 2); if (s[s.length() - 1] == s[0]) { ct -= (l / 2); ct -= (r / 2); ll val = (l + r); ll an = val / 2; an *= (k - 1); ans += an; ans += (ct * k); ans += (l / 2); ans += (r / 2); } else ans = (ct * k); cout << ans; }
#include <bits/stdc++.h> #include <bitset> #include <chrono> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <random> #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define REP(i, a, b) for (int i = int(a); i <= int(b); i++) #define MD 1000000007 #define MOD2 1000000009 #define INF 2000000000 #define DESPACITO 1000000000000000 #define E 998244353 #define pb push_back #define mp make_pair #define double long double #define pdd pair<double, double> #define ll long long using namespace std; // using namespace __gnu_pbds; // typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> pbds; int main() { IOS; string s; ll k; cin >> s; cin >> k; int n = s.length(); int l = 0; REP(i, 0, s.length() - 1) { if (s[i] == s[0]) l++; else break; } int r = 0; for (int i = s.length() - 1; i >= 0; i--) { if (s[i] == s[s.length() - 1]) r++; else break; } if (l - 1 >= n - r) { cout << (1LL * k * (s.length())) / 2; return 0; } ll x = 1; ll ans = 0; ll ct = 0; REP(i, 1, s.length() - 1) { if (s[i] == s[i - 1]) x++; else { ct += (x / 2); x = 1; } } ct += (x / 2); if (s[s.length() - 1] == s[0]) { ct -= (l / 2); ct -= (r / 2); ll val = (l + r); ll an = val / 2; an *= (k - 1); ans += an; ans += (ct * k); ans += (l / 2); ans += (r / 2); } else ans = (ct * k); cout << ans; }
delete
32
36
32
32
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, t, u; cin >> s; t = s + s; u = s + s + s; int64_t K, count1 = 0, count2 = 0, N, count3 = 0; cin >> K; N = s.size(); if (N == 1) { cout << K / 2 << endl; return 0; } for (int i = 0; i < N - 1; i++) { if (s.at(i) == s.at(i + 1)) { count1++; i++; } } for (int i = 0; i < 2 * N - 1; i++) { if (t.at(i) == t.at(i + 1)) { count2++; i++; } } for (int i = 0; i < 3 * N - 1; i++) { if (u.at(i) == u.at(i + 1)) { count3++; i++; } } if (2 * count2 - count1 == count3) { cout << (count2 - count1) * (K - 1) + count1 << endl; } else { return 1; } }
#include <bits/stdc++.h> using namespace std; int main() { string s, t, u; cin >> s; t = s + s; u = s + s + s; int64_t K, count1 = 0, count2 = 0, N, count3 = 0; cin >> K; N = s.size(); if (N == 1) { cout << K / 2 << endl; return 0; } for (int i = 0; i < N - 1; i++) { if (s.at(i) == s.at(i + 1)) { count1++; i++; } } for (int i = 0; i < 2 * N - 1; i++) { if (t.at(i) == t.at(i + 1)) { count2++; i++; } } for (int i = 0; i < 3 * N - 1; i++) { if (u.at(i) == u.at(i + 1)) { count3++; i++; } } if (2 * count2 - count1 == count3) { cout << (count2 - count1) * (K - 1) + count1 << endl; } else { int a = count2 - count1, b = count3 - count2; if (K % 2 == 0) { cout << count1 + K / 2 * a + (K / 2 - 1) * b << endl; } else { cout << count1 + K / 2 * (a + b) << endl; } } }
replace
36
37
36
42
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #include <map> int main() { string S; long long K; cin >> S >> K; string SS = S + S; vector<int> X(109, 0); long long flag = -1; long long x = 1; long long point = 0; for (int i = 1; i < SS.size(); i++) { if (SS[i - 1] == SS[i]) { x += 1; if (i == S.size()) { flag = point; } } else { X[point] = x; point += 1; x = 1; } if (i == SS.size() - 1) { X[point] = x; } } long long ans = 0; if (flag > 0) { ans += X[0] / 2; for (int i = 1; i < X.size() - 1; i++) { if (X[i + 1] == 0) { ans += X[i] / 2; break; } else if (i < flag) { ans += (X[i] / 2) * K; } else if (i == flag) { ans += (X[i] / 2) * (K - 1); } else { continue; } } } else if (flag == 0) { ans = S.size() * K / 2; } else { for (int i = 0; i < X.size(); i++) { if (X[i] == 0) { break; } else { ans += (X[i] / 2) * K; } } ans /= 2; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #include <map> int main() { string S; long long K; cin >> S >> K; string SS = S + S; vector<int> X(210, 0); long long flag = -1; long long x = 1; long long point = 0; for (int i = 1; i < SS.size(); i++) { if (SS[i - 1] == SS[i]) { x += 1; if (i == S.size()) { flag = point; } } else { X[point] = x; point += 1; x = 1; } if (i == SS.size() - 1) { X[point] = x; } } long long ans = 0; if (flag > 0) { ans += X[0] / 2; for (int i = 1; i < X.size() - 1; i++) { if (X[i + 1] == 0) { ans += X[i] / 2; break; } else if (i < flag) { ans += (X[i] / 2) * K; } else if (i == flag) { ans += (X[i] / 2) * (K - 1); } else { continue; } } } else if (flag == 0) { ans = S.size() * K / 2; } else { for (int i = 0; i < X.size(); i++) { if (X[i] == 0) { break; } else { ans += (X[i] / 2) * K; } } ans /= 2; } cout << ans << endl; }
replace
10
11
10
11
0
p02891
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i)) using namespace std; int main() { string s; ll k; cin >> s >> k; char prev = s[0]; ll cnt = 1; vector<ll> n; for (ll i = 1; i < s.size(); i++) { if (s[i] == prev) { cnt++; if (i == s.size() - 1) n.push_back(cnt); } else { n.push_back(cnt); cnt = 1; if (i == s.size() - 1) n.push_back(cnt); } prev = s[i]; } bool c = (s[0] == s[s.size() - 1]); ll ans = 0; if (c) { if (n.size() == 1) { cout << n[0] * k / 2 << endl; } else { ll aida = n[0] + n[n.size() - 1]; ans += (aida / 2) * (k - 1); ans += n[0] / 2; ans += n[n.size() - 1] / 2; for (ll i = 1; i < n.size() - 1; i++) { ans += (n[i] / 2) * k; } cout << ans << endl; } } else { rep(i, n.size()) { ans += n[i] / 2; } cout << ans * k << endl; } }
#include <bits/stdc++.h> #define ll long long #define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i)) using namespace std; int main() { string s; ll k; cin >> s >> k; if (s.size() == 1) { cout << k / 2 << endl; return 0; } char prev = s[0]; ll cnt = 1; vector<ll> n; for (ll i = 1; i < s.size(); i++) { if (s[i] == prev) { cnt++; if (i == s.size() - 1) n.push_back(cnt); } else { n.push_back(cnt); cnt = 1; if (i == s.size() - 1) n.push_back(cnt); } prev = s[i]; } bool c = (s[0] == s[s.size() - 1]); ll ans = 0; if (c) { if (n.size() == 1) { cout << n[0] * k / 2 << endl; } else { ll aida = n[0] + n[n.size() - 1]; ans += (aida / 2) * (k - 1); ans += n[0] / 2; ans += n[n.size() - 1] / 2; for (ll i = 1; i < n.size() - 1; i++) { ans += (n[i] / 2) * k; } cout << ans << endl; } } else { rep(i, n.size()) { ans += n[i] / 2; } cout << ans * k << endl; } }
insert
9
9
9
13
0
p02891
C++
Time Limit Exceeded
#include <bits/stdc++.h> // Defination {{{ #define PI std::pair<int, int> #define mk std::make_pair #define reg register #define ll long long #define rep(i, a, b) for (reg int i = a; i <= b; ++i) #define per(i, a, b) for (reg int i = a; i >= b; --i) #define pb push_back #define debug(...) fprintf(stderr, __VA_ARGS__) // }}} template <typename T> T max(T a, T b) { return a > b ? a : b; } template <typename T> T min(T a, T b) { return a < b ? a : b; } template <typename T> void read(T &x) { x = 0; reg char ch = getchar(); reg int f = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); x *= f; } #define N 105 char s[N << 1]; int k; int main() { #ifndef ONLINE_JUDGE freopen("A.in", "r", stdin); freopen("A.out", "w", stdout); #endif scanf("%s", s + 1); read(k); int n = strlen(s + 1); if (n == 1) { printf("%d\n", k / 2); return 0; } if (k == 1) { int ans = 0, tot = 0; rep(i, 1, n) { if (s[i] != s[i - 1] && i != 1) { ans += tot / 2; tot = 0; } ++tot; } ans += tot / 2; printf("%d\n", ans); return 0; } int check = 0; rep(i, 2, n) if (s[i] != s[i - 1]) check = 1; if (!check) { printf("%lld\n", 1ll * n * k / 2); return 0; } rep(i, 1, n) s[i + n] = s[i]; ll ans = 0, tot = 0, res = 0; rep(i, 1, n) { if (s[i] != s[i - 1] && i != 1) { ans += tot / 2; tot = 0; } ++tot; } int pos = n - tot + 1; tot = 0; rep(i, pos, n * 2) { if (s[i] != s[i - 1] && i != 1) { res += tot / 2; tot = 0; } ++tot; } res = res * (k - 1); ans += tot / 2 + res; printf("%lld\n", ans); return 0; } /* _____ ____ _____ |__ / | _ \ |__ / / / | |_) | / / / /_ | _ < / /_ /____| |_| \_\ /____| _____________________________________ < A.cpp is created by zrz who is weak > ------------------------------------- \ ^__^ \ (^^)\_______ (__)\ )\/\ U ||----w | || || */
#include <bits/stdc++.h> // Defination {{{ #define PI std::pair<int, int> #define mk std::make_pair #define reg register #define ll long long #define rep(i, a, b) for (reg int i = a; i <= b; ++i) #define per(i, a, b) for (reg int i = a; i >= b; --i) #define pb push_back #define debug(...) fprintf(stderr, __VA_ARGS__) // }}} template <typename T> T max(T a, T b) { return a > b ? a : b; } template <typename T> T min(T a, T b) { return a < b ? a : b; } template <typename T> void read(T &x) { x = 0; reg char ch = getchar(); reg int f = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); x *= f; } #define N 105 char s[N << 1]; int k; int main() { #ifndef ONLINE_JUDGE // freopen("A.in", "r", stdin); // freopen("A.out", "w", stdout); #endif scanf("%s", s + 1); read(k); int n = strlen(s + 1); if (n == 1) { printf("%d\n", k / 2); return 0; } if (k == 1) { int ans = 0, tot = 0; rep(i, 1, n) { if (s[i] != s[i - 1] && i != 1) { ans += tot / 2; tot = 0; } ++tot; } ans += tot / 2; printf("%d\n", ans); return 0; } int check = 0; rep(i, 2, n) if (s[i] != s[i - 1]) check = 1; if (!check) { printf("%lld\n", 1ll * n * k / 2); return 0; } rep(i, 1, n) s[i + n] = s[i]; ll ans = 0, tot = 0, res = 0; rep(i, 1, n) { if (s[i] != s[i - 1] && i != 1) { ans += tot / 2; tot = 0; } ++tot; } int pos = n - tot + 1; tot = 0; rep(i, pos, n * 2) { if (s[i] != s[i - 1] && i != 1) { res += tot / 2; tot = 0; } ++tot; } res = res * (k - 1); ans += tot / 2 + res; printf("%lld\n", ans); return 0; } /* _____ ____ _____ |__ / | _ \ |__ / / / | |_) | / / / /_ | _ < / /_ /____| |_| \_\ /____| _____________________________________ < A.cpp is created by zrz who is weak > ------------------------------------- \ ^__^ \ (^^)\_______ (__)\ )\/\ U ||----w | || || */
replace
35
37
35
37
TLE
p02892
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define erep(i, a, n) for (int i = a; i <= n; i++) typedef long long ll; #define int long long #define vint vector<int> #define vvint vector<vector<int>> #define vstring vector<string> #define vdouble vector<double> #define vll vector<ll>: #define vbool vector<bool> #define INF 1101010101010101010 #define MOD 1000000007 #define P pair<int, int> template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int color[300]; vvint to(300); bool dfs(int x, int c) { color[x] = c; for (auto p : to[x]) { if (color[p] == c) return false; if (color[p] == 0 && !dfs(p, -c)) return false; } return true; } signed main() { int n; cin >> n; vstring mass(n); rep(i, 0, n) rep(j, 0, n) cin >> mass[i][j]; vvint dist(n, vint(n)); rep(i, 0, n) { rep(j, 0, n) { if (i != j) dist[i][j] = INF; if (mass[i][j] == '1') { to[i].push_back(j); dist[i][j] = 1; } } } if (!dfs(0, 1)) { cout << -1 << endl; return 0; } rep(i, 0, n) { rep(j, 0, n) { rep(s, 0, n) { dist[j][s] = min(dist[j][s], dist[j][i] + dist[i][s]); } } } int ans = 0; rep(i, 0, n) rep(j, 0, n) ans = max(ans, dist[i][j]); cout << ans + 1 << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define erep(i, a, n) for (int i = a; i <= n; i++) typedef long long ll; #define int long long #define vint vector<int> #define vvint vector<vector<int>> #define vstring vector<string> #define vdouble vector<double> #define vll vector<ll>: #define vbool vector<bool> #define INF 1101010101010101010 #define MOD 1000000007 #define P pair<int, int> template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int color[300]; vvint to(300); bool dfs(int x, int c) { color[x] = c; for (auto p : to[x]) { if (color[p] == c) return false; if (color[p] == 0 && !dfs(p, -c)) return false; } return true; } signed main() { int n; cin >> n; vstring mass(n); rep(i, 0, n) cin >> mass[i]; vvint dist(n, vint(n)); rep(i, 0, n) { rep(j, 0, n) { if (i != j) dist[i][j] = INF; if (mass[i][j] == '1') { to[i].push_back(j); dist[i][j] = 1; } } } if (!dfs(0, 1)) { cout << -1 << endl; return 0; } rep(i, 0, n) { rep(j, 0, n) { rep(s, 0, n) { dist[j][s] = min(dist[j][s], dist[j][i] + dist[i][s]); } } } int ans = 0; rep(i, 0, n) rep(j, 0, n) ans = max(ans, dist[i][j]); cout << ans + 1 << endl; }
replace
49
50
49
50
0
p02892
Python
Runtime Error
import numpy as np import sys from scipy.sparse.csgraph import floyd_warshall, csgraph_from_dense N = int(input()) S = np.frombuffer(sys.stdin.buffer.read(), dtype="S1").reshape(N, -1)[:, :N].astype(int) G = csgraph_from_dense(S) F = floyd_warshall(G).astype(int) F2 = F % 2 I0 = np.where(F2[0] == 0) I1 = np.where(F2[0] == 1) if (S[I0][:, I0] == 0).all() and (S[I1][:, I1] == 0).all(): print(F.max() + 1) exit() print(-1)
import numpy as np import sys from scipy.sparse.csgraph import floyd_warshall, csgraph_from_dense N = int(sys.stdin.buffer.readline()) S = np.frombuffer(sys.stdin.buffer.read(), dtype="S1").reshape(N, -1)[:, :N].astype(int) G = csgraph_from_dense(S) F = floyd_warshall(G).astype(int) F2 = F % 2 I0 = np.where(F2[0] == 0) I1 = np.where(F2[0] == 1) if (S[I0][:, I0] == 0).all() and (S[I1][:, I1] == 0).all(): print(F.max() + 1) exit() print(-1)
replace
4
5
4
5
TLE
p02892
Python
Runtime Error
from collections import deque n = int(input()) s = [input() for i in range(n)] q = deque([0]) d = [float("inf")] * n d[0] = 0 while q: p = q.popleft() for i, c in enumerate(s[p]): if c == "1" and d[i] == float("inf"): d[i] = d[p] + 1 q.append(i) m = 0 for i in range(n): if d[m] < d[i]: m = i q = deque([m]) d = [float("inf")] * n d[m] = 0 while q: p = q.popleft() for i, c in enumerate(s[p]): if c == "1": if d[i] == float("inf"): d[i] = d[p] + 1 q.append(i) elif d[i] != d[p] + 1 and d[i] != d[p] - 1: print(-1) exit(1) m = 0 for i in range(n): m = max(m, d[i]) print(m + 1)
from collections import deque n = int(input()) s = [input() for i in range(n)] ans = -2 for k in range(n): q = deque([k]) d = [float("inf")] * n d[k] = 0 while q: flg = 0 p = q.popleft() for i, c in enumerate(s[p]): if c == "1": if d[i] == float("inf"): d[i] = d[p] + 1 q.append(i) elif d[i] != d[p] + 1 and d[i] != d[p] - 1: flg = 1 break if flg: break if flg: continue for i in range(n): ans = max(ans, d[i]) print(ans + 1)
replace
4
34
4
27
0
p02892
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 110; char s[N][N]; int stg[N]; int n; int main() { cin >> n; for (int i = 1; i <= n; i++) { scanf("%s", s[i] + 1); } int ret = -1; for (int S = 1; S <= n; S++) { queue<int> q; q.push(S); for (int i = 1; i <= n; i++) stg[i] = 0; stg[S] = 1; int ans = 1; while (q.size()) { int x = q.front(); q.pop(); ans = max(ans, stg[x]); for (int j = 1; j <= n; j++) if (s[x][j] == '1') { if (!stg[j]) stg[j] = stg[x] + 1, q.push(j); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) if (s[i][j] == '1') { if (abs(stg[i] - stg[j]) != 1) ans = -1; } } ret = max(ret, ans); } cout << ret << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 210; char s[N][N]; int stg[N]; int n; int main() { cin >> n; for (int i = 1; i <= n; i++) { scanf("%s", s[i] + 1); } int ret = -1; for (int S = 1; S <= n; S++) { queue<int> q; q.push(S); for (int i = 1; i <= n; i++) stg[i] = 0; stg[S] = 1; int ans = 1; while (q.size()) { int x = q.front(); q.pop(); ans = max(ans, stg[x]); for (int j = 1; j <= n; j++) if (s[x][j] == '1') { if (!stg[j]) stg[j] = stg[x] + 1, q.push(j); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) if (s[i][j] == '1') { if (abs(stg[i] - stg[j]) != 1) ans = -1; } } ret = max(ret, ans); } cout << ret << endl; }
replace
2
3
2
3
0
p02892
C++
Runtime Error
#include <iostream> using namespace std; int n, i, ii, j, x, nr, p, u, ok; int v[105], c[105]; char a[105][105]; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i] + 1; } nr = -1; for (ii = 1; ii <= n; ii++) { for (i = 1; i <= n; i++) { v[i] = 0; } v[ii] = p = u = 1; c[1] = ii; ok = 1; while (p <= u) { x = c[p]; for (i = 1; i <= n; i++) { if (a[x][i] == '1') { if (v[i] == 0) { v[i] = 1 + v[x]; c[++u] = i; } else { if (v[i] != v[x] - 1 && v[i] != v[x] + 1) { ok = 0; } } } } p++; } if (ok == 1) { for (i = 1; i <= n; i++) { nr = max(nr, v[i]); } } } cout << nr; }
#include <iostream> using namespace std; int n, i, ii, j, x, nr, p, u, ok; int v[205], c[205]; char a[205][205]; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i] + 1; } nr = -1; for (ii = 1; ii <= n; ii++) { for (i = 1; i <= n; i++) { v[i] = 0; } v[ii] = p = u = 1; c[1] = ii; ok = 1; while (p <= u) { x = c[p]; for (i = 1; i <= n; i++) { if (a[x][i] == '1') { if (v[i] == 0) { v[i] = 1 + v[x]; c[++u] = i; } else { if (v[i] != v[x] - 1 && v[i] != v[x] + 1) { ok = 0; } } } } p++; } if (ok == 1) { for (i = 1; i <= n; i++) { nr = max(nr, v[i]); } } } cout << nr; }
replace
3
5
3
5
0
p02892
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define pii pair<int, int> #define pld pair<ld, ld> #define all(v) v.begin(), v.end() #define F first #define S second #define mod 1000000007 #define pb push_back #define MP make_pair #define mset(x) memset(x, 0, sizeof(x)); #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define inf 1000000000000007 const int N = 206; ll n, dp[N][N], col[N]; string a[N]; void run() { ll i, j, k, ans = 0; queue<ll> q; cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; a[i] = '0' + a[i]; for (j = 1; j <= n; j++) if (a[i][j] == '1') dp[i][j] = 1; else if (i == j) dp[i][i] = 0; else dp[i][j] = 5000; } q.push(1); col[1] = 1; while (!q.empty()) { i = q.front(); q.pop(); for (j = 1; j <= n; j++) { if (a[i][j] == '1') { if (col[j] == 0) { col[j] = -col[i]; q.push(j); } else if (col[j] == col[i]) { cout << -1; return; } } } } cout << 1 / 0; // for(k=1;k<=n;k++) // { // for(i=1;i<=n;i++) // { // for(j=1;j<=n;j++) // { // dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]); // if(dp[i][j]<5000) ans=max(ans,dp[i][j]); // } // } // } // cout<<ans+1; } void setup() { ios; // cout << setprecision(20); // #ifndef ONLINE_JUDGE // freopen("input", "r", stdin); // freopen("output", "w", stdout); // #endif } signed main() { setup(); int tests = 1; // cin >> tests; for (int i = 1; i <= tests; i++) run(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define pii pair<int, int> #define pld pair<ld, ld> #define all(v) v.begin(), v.end() #define F first #define S second #define mod 1000000007 #define pb push_back #define MP make_pair #define mset(x) memset(x, 0, sizeof(x)); #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define inf 1000000000000007 const int N = 206; ll n, dp[N][N], col[N]; string a[N]; void run() { ll i, j, k, ans = 0; queue<ll> q; cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; a[i] = '0' + a[i]; for (j = 1; j <= n; j++) if (a[i][j] == '1') dp[i][j] = 1; else if (i == j) dp[i][i] = 0; else dp[i][j] = 5000; } q.push(1); col[1] = 1; while (!q.empty()) { i = q.front(); q.pop(); for (j = 1; j <= n; j++) { if (a[i][j] == '1') { if (col[j] == 0) { col[j] = -col[i]; q.push(j); } else if (col[j] == col[i]) { cout << -1; return; } } } } for (k = 1; k <= n; k++) { for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); } } } for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) if (dp[i][j] < 5000) ans = max(ans, dp[i][j]); cout << ans + 1; } void setup() { ios; // cout << setprecision(20); // #ifndef ONLINE_JUDGE // freopen("input", "r", stdin); // freopen("output", "w", stdout); // #endif } signed main() { setup(); int tests = 1; // cin >> tests; for (int i = 1; i <= tests; i++) run(); return 0; }
replace
53
66
53
66
-8
p02892
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define pb push_back #define sz(x) ((ll)x.size()) #define all(x) x.begin(), x.end() typedef pair<int, int> pii; typedef vector<int> vi; typedef long long ll; typedef unsigned long long ul; const int mn = 1e5 + 5; const int mod = 1e9 + 7; void bye() { puts("-1"); exit(0); } int n; char s[205][205]; stack<int> st; int vis[205]; int pos[205]; void dfs(int x, int fa) { if (vis[x] == -1) { if ((sz(st) - pos[x]) % 2) { bye(); } return; } vis[x] = -1; pos[x] = sz(st); st.push(x); for (int i = 1; i <= n; i++) { if (i != fa && s[x][i] == '1') dfs(i, x); } st.pop(); vis[x] = 1; } int dist[205][205]; int main() { // cin.sync_with_stdio(0); #ifdef LOCAL freopen("../1.txt", "r", stdin); // freopen("my.out", "w", stdout); size_t bg = clock(); #endif scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); dfs(1, 0); memset(dist, 1, sizeof dist); for (int i = 1; i <= n; i++) { dist[i][i] = 0; for (int j = 1; j <= n; j++) { if (s[i][j] == '1') dist[i][j] = 1; } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dist[i][j] = min(dist[i][k] + dist[k][j], dist[i][j]); } } } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (dist[i][j] < 1e7) ans = max(dist[i][j], ans); } } printf("%d\n", ans + 1); #ifdef LOCAL size_t ed = clock(); printf("time: %f\n", (double)(ed - bg) / CLOCKS_PER_SEC); #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define sz(x) ((ll)x.size()) #define all(x) x.begin(), x.end() typedef pair<int, int> pii; typedef vector<int> vi; typedef long long ll; typedef unsigned long long ul; const int mn = 1e5 + 5; const int mod = 1e9 + 7; void bye() { puts("-1"); exit(0); } int n; char s[205][205]; stack<int> st; int vis[205]; int pos[205]; void dfs(int x, int fa) { if (vis[x] == -1) { if ((sz(st) - pos[x]) % 2) { bye(); } return; } vis[x] = -1; pos[x] = sz(st); st.push(x); for (int i = 1; i <= n; i++) { if (i != fa && s[x][i] == '1' && vis[i] != 1) dfs(i, x); } st.pop(); vis[x] = 1; } int dist[205][205]; int main() { // cin.sync_with_stdio(0); #ifdef LOCAL freopen("../1.txt", "r", stdin); // freopen("my.out", "w", stdout); size_t bg = clock(); #endif scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); dfs(1, 0); memset(dist, 1, sizeof dist); for (int i = 1; i <= n; i++) { dist[i][i] = 0; for (int j = 1; j <= n; j++) { if (s[i][j] == '1') dist[i][j] = 1; } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dist[i][j] = min(dist[i][k] + dist[k][j], dist[i][j]); } } } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (dist[i][j] < 1e7) ans = max(dist[i][j], ans); } } printf("%d\n", ans + 1); #ifdef LOCAL size_t ed = clock(); printf("time: %f\n", (double)(ed - bg) / CLOCKS_PER_SEC); #endif return 0; }
replace
38
39
38
39
TLE
p02892
C++
Runtime Error
/**/ #include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> typedef long long LL; using namespace std; int n, col[205], tot, head[205]; int dp[205][205]; char s[205][205]; struct node { int v, next; } a[55 * 55 * 2]; bool dfs(int x, int p) { col[x] = p; for (int i = head[x]; i != -1; i = a[i].next) { int v = a[i].v; if (col[v] == p) return true; if (!col[v] && dfs(v, p ^ 1)) return true; } return false; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); scanf("%d", &n); for (int i = 1; i <= n; i++) head[i] = -1; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) dp[i][j] = 0x3f3f3f3f; for (int i = 1; i <= n; i++) { scanf("%s", s[i] + 1); for (int j = 1; j <= n; j++) { if (s[i][j] == '1') a[tot] = node{j, head[i]}, head[i] = tot++, dp[i][j] = 1; } } for (int i = 1; i <= n; i++) dp[i][i] = 0; if (dfs(1, 2)) { printf("-1\n"); return 0; } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); } } } int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans = max(ans, dp[i][j]); printf("%d\n", ans + 1); return 0; } /**/
/**/ #include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> typedef long long LL; using namespace std; int n, col[205], tot, head[205]; int dp[205][205]; char s[205][205]; struct node { int v, next; } a[205 * 205]; bool dfs(int x, int p) { col[x] = p; for (int i = head[x]; i != -1; i = a[i].next) { int v = a[i].v; if (col[v] == p) return true; if (!col[v] && dfs(v, p ^ 1)) return true; } return false; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); scanf("%d", &n); for (int i = 1; i <= n; i++) head[i] = -1; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) dp[i][j] = 0x3f3f3f3f; for (int i = 1; i <= n; i++) { scanf("%s", s[i] + 1); for (int j = 1; j <= n; j++) { if (s[i][j] == '1') a[tot] = node{j, head[i]}, head[i] = tot++, dp[i][j] = 1; } } for (int i = 1; i <= n; i++) dp[i][i] = 0; if (dfs(1, 2)) { printf("-1\n"); return 0; } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); } } } int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans = max(ans, dp[i][j]); printf("%d\n", ans + 1); return 0; } /**/
replace
23
24
23
24
0
p02892
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #define mk make_pair #define eb emplace_back #define eps 1e-8 #define fi first #define se second #define all(x) (x).begin(), (x).end() // #define int long long using namespace std; typedef long double ld; typedef unsigned int ui; typedef pair<int, int> pii; typedef unsigned long long ull; typedef vector<int> vii; typedef vector<long double> vd; const int inf = 1e9; const int M = 1e9 + 7; //__int128 const int maxn = 202; string s[maxn]; int vis[maxn]; int n; int bfs(int u) { queue<int> q; q.push(u); int res = 0; vis[u] = 1; while (!q.empty()) { int x = q.front(); q.pop(); res = max(res, vis[x]); for (int i = 0; i < n; ++i) { if (s[x][i] == '1') { if (!vis[i]) { q.push(i); vis[i] = vis[x] + 1; } else if (vis[i] == vis[x]) { return -1; } } } } return res; } signed main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; std::vector<pii> d(n); for (int i = 0; i < n; ++i) { cin >> s[i]; } int res = -1; for (int i = 0; i < n; ++i) { memset(vis, 0, sizeof(vis)); res = max(res, bfs(i)); } cout << res << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #define mk make_pair #define eb emplace_back #define eps 1e-8 #define fi first #define se second #define all(x) (x).begin(), (x).end() // #define int long long using namespace std; typedef long double ld; typedef unsigned int ui; typedef pair<int, int> pii; typedef unsigned long long ull; typedef vector<int> vii; typedef vector<long double> vd; const int inf = 1e9; const int M = 1e9 + 7; //__int128 const int maxn = 202; string s[maxn]; int vis[maxn]; int n; int bfs(int u) { queue<int> q; q.push(u); int res = 0; vis[u] = 1; while (!q.empty()) { int x = q.front(); q.pop(); res = max(res, vis[x]); for (int i = 0; i < n; ++i) { if (s[x][i] == '1') { if (!vis[i]) { q.push(i); vis[i] = vis[x] + 1; } else if (vis[i] == vis[x]) { return -1; } } } } return res; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; std::vector<pii> d(n); for (int i = 0; i < n; ++i) { cin >> s[i]; } int res = -1; for (int i = 0; i < n; ++i) { memset(vis, 0, sizeof(vis)); res = max(res, bfs(i)); } cout << res << endl; return 0; }
delete
58
62
58
58
0
p02892
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int dis[105][105], e[105][105]; signed main() { int n; char ch; cin >> n; memset(dis, 0x3f, sizeof(dis)); for (int i = 1; i <= n; ++i) dis[i][i] = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { cin >> ch; if (ch == '1') e[i][j] = dis[i][j] = 1; } for (int k = 1; k <= n; ++k) for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); int ans = -1; for (int u = 1; u <= n; ++u) { int fl = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (e[i][j] && abs(dis[u][i] - dis[u][j]) != 1) fl = 1; if (fl) break; for (int i = 1; i <= n; ++i) ans = max(ans, dis[u][i] + 1); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int dis[205][205], e[205][205]; signed main() { int n; char ch; cin >> n; memset(dis, 0x3f, sizeof(dis)); for (int i = 1; i <= n; ++i) dis[i][i] = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { cin >> ch; if (ch == '1') e[i][j] = dis[i][j] = 1; } for (int k = 1; k <= n; ++k) for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); int ans = -1; for (int u = 1; u <= n; ++u) { int fl = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (e[i][j] && abs(dis[u][i] - dis[u][j]) != 1) fl = 1; if (fl) break; for (int i = 1; i <= n; ++i) ans = max(ans, dis[u][i] + 1); } cout << ans; return 0; }
replace
2
3
2
3
0
p02892
C++
Time Limit Exceeded
// https://atcoder.jp/contests/agc039/tasks/agc039_b #include "algorithm" #include "iostream" #include "queue" #include "set" #include "vector" #define rep(i, from, to) for (ll i = from; i < (to); ++i) using namespace std; typedef long long ll; template <typename T> using V = vector<T>; template <typename T> inline bool chmax(T &a, T b); template <typename T> inline bool chmin(T &a, T b); void print_ints(vector<ll> v); template <typename T> void drop(T a); ll N; V<V<ll>> G; V<ll> colors; // 0: 未着色 V<V<ll>> ds; V<ll> group_ids; bool check_bio(ll v, ll color) { colors[v] = color; for (auto to : G[v]) { if (colors[to] == color) { return false; } if (colors[to] == 0 && !check_bio(to, -color)) { return false; } } return true; } bool satisfied(ll s) { queue<pair<ll, ll>> q; // f: edge, s: group group_ids[s] = 0; for (auto u : G[s]) q.emplace(u, 1); while (!q.empty()) { auto next = q.front(); q.pop(); auto u = next.first; auto g = next.second; if (group_ids[u] != -1 && group_ids[u] != g) { return false; } group_ids[u] = g; for (auto v : G[u]) { if (group_ids[v] == -1) q.emplace(v, g + 1); } } return true; } void solve() { cin >> N; G.assign(N, V<ll>()); rep(i, 0, N) { string e; cin >> e; rep(j, 0, N) { if (e[j] == '1') { G[i].push_back(j); } } } // { // colors.assign(N, 0); // if (!check_bio(0, 1)) { // drop(-1); // } // } { ll INF = 1 << 30; ds.assign(N, V<ll>(N, INF)); rep(i, 0, N) ds[i][i] = 0; rep(i, 0, N) { for (auto to : G[i]) { ds[i][to] = 1; ds[to][i] = 1; } } // ワーシャルフロイド rep(k, 0, N) rep(i, 0, N) rep(j, 0, N) { ds[i][j] = min(ds[i][j], ds[i][k] + ds[k][j]); } } ll max_d = 0; rep(i, 0, N) rep(j, 0, N) chmax(max_d, ds[i][j]); { pair<ll, ll> max_pair; rep(i, 0, N) rep(j, 0, N) if (ds[i][j] == max_d) max_pair = make_pair(i, j); group_ids.assign(N, -1); if (!satisfied(max_pair.first)) { drop(-1); } } cout << max_d + 1 << endl; } struct exit_exception : public std::exception { const char *what() const throw() { return "Exited"; } }; #ifndef TEST int main() { cin.tie(0); ios::sync_with_stdio(false); try { solve(); } catch (exit_exception &e) { } return 0; } #endif template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void print_ints(vector<ll> v) { rep(i, 0, v.size()) { if (i > 0) { cout << " "; } cout << v[i]; } cout << endl; } template <typename T> void drop(T res) { cout << res << endl; throw exit_exception(); }
// https://atcoder.jp/contests/agc039/tasks/agc039_b #include "algorithm" #include "iostream" #include "queue" #include "set" #include "vector" #define rep(i, from, to) for (ll i = from; i < (to); ++i) using namespace std; typedef long long ll; template <typename T> using V = vector<T>; template <typename T> inline bool chmax(T &a, T b); template <typename T> inline bool chmin(T &a, T b); void print_ints(vector<ll> v); template <typename T> void drop(T a); ll N; V<V<ll>> G; V<ll> colors; // 0: 未着色 V<V<ll>> ds; V<ll> group_ids; bool check_bio(ll v, ll color) { colors[v] = color; for (auto to : G[v]) { if (colors[to] == color) { return false; } if (colors[to] == 0 && !check_bio(to, -color)) { return false; } } return true; } bool satisfied(ll s) { queue<pair<ll, ll>> q; // f: edge, s: group group_ids[s] = 0; for (auto u : G[s]) q.emplace(u, 1); while (!q.empty()) { auto next = q.front(); q.pop(); auto u = next.first; auto g = next.second; if (group_ids[u] != -1) { if (group_ids[u] != g) { return false; } else { continue; } } group_ids[u] = g; for (auto v : G[u]) { if (group_ids[v] == -1) q.emplace(v, g + 1); } } return true; } void solve() { cin >> N; G.assign(N, V<ll>()); rep(i, 0, N) { string e; cin >> e; rep(j, 0, N) { if (e[j] == '1') { G[i].push_back(j); } } } // { // colors.assign(N, 0); // if (!check_bio(0, 1)) { // drop(-1); // } // } { ll INF = 1 << 30; ds.assign(N, V<ll>(N, INF)); rep(i, 0, N) ds[i][i] = 0; rep(i, 0, N) { for (auto to : G[i]) { ds[i][to] = 1; ds[to][i] = 1; } } // ワーシャルフロイド rep(k, 0, N) rep(i, 0, N) rep(j, 0, N) { ds[i][j] = min(ds[i][j], ds[i][k] + ds[k][j]); } } ll max_d = 0; rep(i, 0, N) rep(j, 0, N) chmax(max_d, ds[i][j]); { pair<ll, ll> max_pair; rep(i, 0, N) rep(j, 0, N) if (ds[i][j] == max_d) max_pair = make_pair(i, j); group_ids.assign(N, -1); if (!satisfied(max_pair.first)) { drop(-1); } } cout << max_d + 1 << endl; } struct exit_exception : public std::exception { const char *what() const throw() { return "Exited"; } }; #ifndef TEST int main() { cin.tie(0); ios::sync_with_stdio(false); try { solve(); } catch (exit_exception &e) { } return 0; } #endif template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void print_ints(vector<ll> v) { rep(i, 0, v.size()) { if (i > 0) { cout << " "; } cout << v[i]; } cout << endl; } template <typename T> void drop(T res) { cout << res << endl; throw exit_exception(); }
replace
52
54
52
58
TLE
p02892
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stdio.h> #include <string> #include <unordered_map> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define repn(i, n) for (int i = 1; i <= n; i++) #define repr(e, x) for (auto &e : x) using namespace std; typedef long long ll; typedef long double ld; // typedef pair<int,int> P; int const INF = 1001001001; ll const LINF = 1001001001001001001; ll const MOD = 1000000007; int N; string s[200]; int main() { cin >> N; rep(i, N) cin >> s[i]; int ans = -1; rep(start, N) { vector<int> num(N, 0); int v = 1; num[start] = v; vector<int> tmp; queue<int> q; q.push(start); q.push(-1); while (!q.empty()) { int p = q.front(); q.pop(); // cout<<p<<endl; if (p == -2) break; if (p == -1) { // repr(e,tmp) cout<<e<<' ';cout<<endl; repr(e1, tmp) { repr(e2, tmp) { if (s[e1][e2] == '1') { // cout<<"break"<<endl; goto a; } } } if (!q.empty()) { v++; repr(e, tmp) { num[e] = v; } tmp.clear(); q.push(-1); } else { break; } continue; } rep(i, N) { if (s[p][i] == '1' && num[i] == 0) { q.push(i); tmp.push_back(i); } } } // cout<<start<<' '<<v<<endl; // rep(i,N) cout<<num[i]<<' ';cout<<endl; ans = max(ans, v); a:; } cout << ans << endl; }
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stdio.h> #include <string> #include <unordered_map> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define repn(i, n) for (int i = 1; i <= n; i++) #define repr(e, x) for (auto &e : x) using namespace std; typedef long long ll; typedef long double ld; // typedef pair<int,int> P; int const INF = 1001001001; ll const LINF = 1001001001001001001; ll const MOD = 1000000007; int N; string s[200]; int main() { cin >> N; rep(i, N) cin >> s[i]; int ans = -1; rep(start, N) { vector<int> num(N, 0); int v = 1; num[start] = v; vector<int> tmp; queue<int> q; q.push(start); q.push(-1); while (!q.empty()) { int p = q.front(); q.pop(); // cout<<p<<endl; if (p == -2) break; if (p == -1) { // repr(e,tmp) cout<<e<<' ';cout<<endl; repr(e1, tmp) { repr(e2, tmp) { if (s[e1][e2] == '1') { // cout<<"break"<<endl; goto a; } } } if (!q.empty()) { v++; repr(e, tmp) { num[e] = v; } tmp.clear(); q.push(-1); } else { break; } continue; } rep(i, N) { if (s[p][i] == '1' && num[i] == 0) { num[i] = INF; q.push(i); tmp.push_back(i); } } } // cout<<start<<' '<<v<<endl; // rep(i,N) cout<<num[i]<<' ';cout<<endl; ans = max(ans, v); a:; } cout << ans << endl; }
insert
66
66
66
67
TLE
p02892
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #pragma GCC optimize("Ofast") using namespace std; const long long INF = 20000000000; // setprecision(2) << const int MOD = 1000000007; long long max(long long a, long long b) { if (a < b) return b; else return a; } long long min(long long a, long long b) { if (a > b) return b; else return a; } long long gcd(long long a, long long b) { if (a < b) swap(a, b); if (a % b == 0) return b; else return gcd(a % b, b); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long com(long long n, long long k) {} long long getDigit(long long n) { if (n == 1) return 1; else return log10(n) + 1; } class G { long long node; long long w; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long N; cin >> N; string Gs[N + 1]; int G[N][N]; for (int i = 0; i < N; i++) { cin >> Gs[i]; for (int j = 0; j < N; j++) { G[i][j] = (int)(Gs[i][j] - '0'); } } // cout<<G[0][1]; // 2graph or not stack<long long> que; vector<int> color(N + 2, -1); vector<bool> ex(N + 2, false); // 0 not 1 black 2 white color[0] = 1; que.push(0); while (!que.empty()) { int u = que.top(); que.pop(); ex[u] = true; // cout<<u<<endl; for (int i = 0; i < N; i++) { if (G[i][u] == 1) { if (ex[i] == false) que.push(i); if (color[i] == -1) { color[i] = !color[u]; continue; } if (color[i] == color[u]) { cout << -1 << endl; return 0; } } } } return -1; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #pragma GCC optimize("Ofast") using namespace std; const long long INF = 20000000000; // setprecision(2) << const int MOD = 1000000007; long long max(long long a, long long b) { if (a < b) return b; else return a; } long long min(long long a, long long b) { if (a > b) return b; else return a; } long long gcd(long long a, long long b) { if (a < b) swap(a, b); if (a % b == 0) return b; else return gcd(a % b, b); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long com(long long n, long long k) {} long long getDigit(long long n) { if (n == 1) return 1; else return log10(n) + 1; } class G { long long node; long long w; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long N; cin >> N; string Gs[N + 1]; int G[N][N]; for (int i = 0; i < N; i++) { cin >> Gs[i]; for (int j = 0; j < N; j++) { G[i][j] = (int)(Gs[i][j] - '0'); } } // cout<<G[0][1]; // 2graph or not stack<long long> que; vector<int> color(N + 2, -1); vector<bool> ex(N + 2, false); // 0 not 1 black 2 white color[0] = 1; que.push(0); while (!que.empty()) { int u = que.top(); que.pop(); ex[u] = true; // cout<<u<<endl; for (int i = 0; i < N; i++) { if (G[i][u] == 1) { if (ex[i] == false) que.push(i); if (color[i] == -1) { color[i] = !color[u]; continue; } if (color[i] == color[u]) { cout << -1 << endl; return 0; } } } } // long long d[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { d[i][j] = G[i][j]; if (G[i][j] != 1) { d[i][j] = INF; } if (i == j) d[i][j] = 0; } } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } long long ans = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { // cout<<d[i][j]; ans = max(ans, d[i][j]); } } cout << ans + 1 << endl; }
replace
92
93
92
121
255
p02892
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 105; typedef long long LL; char ch; int a[N][N], n, clr[N], ok, dis[N][N], ans; void Dfs(int u, int col) { clr[u] = col; for (int i = 1; i <= n; i++) if (a[u][i] == 1) { if (clr[i]) { if (clr[i] == col) ok = 1; continue; } Dfs(i, 3 - col); } } int main() { cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { cin >> ch; if (ch == '1') a[i][j] = 1; else { if (i == j) a[i][j] = 0; else a[i][j] = 1e9; } } Dfs(1, 1); if (ok) { cout << -1 << endl; return 0; } for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (a[i][k] + a[k][j] < a[i][j]) a[i][j] = a[i][k] + a[k][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans = max(ans, a[i][j]); cout << ans + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 205; typedef long long LL; char ch; int a[N][N], n, clr[N], ok, dis[N][N], ans; void Dfs(int u, int col) { clr[u] = col; for (int i = 1; i <= n; i++) if (a[u][i] == 1) { if (clr[i]) { if (clr[i] == col) ok = 1; continue; } Dfs(i, 3 - col); } } int main() { cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { cin >> ch; if (ch == '1') a[i][j] = 1; else { if (i == j) a[i][j] = 0; else a[i][j] = 1e9; } } Dfs(1, 1); if (ok) { cout << -1 << endl; return 0; } for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (a[i][k] + a[k][j] < a[i][j]) a[i][j] = a[i][k] + a[k][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans = max(ans, a[i][j]); cout << ans + 1 << endl; return 0; }
replace
3
4
3
4
0
p02892
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdint> #include <cstdio> #include <ctime> #include <deque> #include <float.h> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using ll = long long; unsigned euclidean_gcd(unsigned a, unsigned b) { if (a < b) return euclidean_gcd(b, a); unsigned r; while ((r = a % b)) { a = b; b = r; } return b; } ll ll_gcd(ll a, ll b) { if (a < b) return ll_gcd(b, a); ll r; while ((r = a % b)) { a = b; b = r; } return b; } class UnionFind { public: vector<ll> par; vector<ll> siz; UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); for (ll i = 0; i < sz_; ++i) par[i] = i; } ll root(ll x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } }; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int main() { ll n; cin >> n; vector<vector<int>> z(n); UnionFind u(n * 2); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < n; j++) { if (s[j] == '1') { z[i].push_back(j); u.merge(i, j + n); } } } int ansi = 0; for (int i = 0; i < n; i++) { if (u.issame(i, i + n)) ansi = -1; } if (ansi == -1) cout << ansi << endl; else { int anss = 0; for (int i = 0; i < n; i++) { vector<bool> seen(n, false); vector<int> ans(n); queue<int> q; q.push(i); while (!q.empty()) { int h = q.front(); seen[h] = true; for (int i = 0; i < z[h].size(); i++) { if (!seen[z[h][i]]) { q.push(z[h][i]); ans[z[h][i]] = ans[h] + 1; } } q.pop(); } for (int j = 0; j < n; j++) { anss = max(anss, ans[j] + 1); } } cout << anss << endl; } }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdint> #include <cstdio> #include <ctime> #include <deque> #include <float.h> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using ll = long long; unsigned euclidean_gcd(unsigned a, unsigned b) { if (a < b) return euclidean_gcd(b, a); unsigned r; while ((r = a % b)) { a = b; b = r; } return b; } ll ll_gcd(ll a, ll b) { if (a < b) return ll_gcd(b, a); ll r; while ((r = a % b)) { a = b; b = r; } return b; } class UnionFind { public: vector<ll> par; vector<ll> siz; UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); for (ll i = 0; i < sz_; ++i) par[i] = i; } ll root(ll x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } }; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int main() { ll n; cin >> n; vector<vector<int>> z(n); UnionFind u(n * 2); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < n; j++) { if (s[j] == '1') { z[i].push_back(j); u.merge(i, j + n); } } } int ansi = 0; for (int i = 0; i < n; i++) { if (u.issame(i, i + n)) ansi = -1; } if (ansi == -1) cout << ansi << endl; else { int anss = 0; for (int i = 0; i < n; i++) { vector<bool> seen(n, false); vector<int> ans(n); queue<int> q; q.push(i); while (!q.empty()) { int h = q.front(); seen[h] = true; for (int i = 0; i < z[h].size(); i++) { if (!seen[z[h][i]]) { seen[z[h][i]] = true; q.push(z[h][i]); ans[z[h][i]] = ans[h] + 1; } } q.pop(); } for (int j = 0; j < n; j++) { anss = max(anss, ans[j] + 1); } } cout << anss << endl; } }
insert
135
135
135
136
TLE
p02892
C++
Time Limit Exceeded
#include <algorithm> #include <assert.h> #include <climits> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <vector> using namespace std; template <typename T> inline bool sign(T A) { return (A > 0) - (A < 0); } #define REP(i, n) for (int i = 0; i < (n); ++i) #define SREP(i, s, n) \ for (int i = s; (n - i) * sign(n - s) > 0; i += sign(n - s)) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef unsigned long long ull; template <typename T> T gcd(T a, T b) { if (a < b) gcd(b, a); if (b == 1) return 1; T r; while ((r = a % b)) { a = b; b = r; } return b; } bool _less(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } template <template <class, class, class...> class C, typename K, typename V, typename... Args> V map_get(const C<K, V, Args...> &m, K const &key, const V &defval) { typename C<K, V, Args...>::const_iterator it = m.find(key); if (it == m.end()) return defval; return it->second; } #define MOD (1'000'000'000 + 7 #define INF (ll(1e18)) int main(void) { ll N; cin >> N; vector<string> S(N); REP(i, N) { cin >> S[i]; } ll maxk = -1; REP(i, N) { queue<pair<ll, ll>> q; vector<ll> v(N, -1); ll k = -1; q.push(make_pair(i, 1)); while (q.size()) { auto j = q.front(); q.pop(); if (v[j.first] >= 0 && v[j.first] != j.second) { k = -1; break; } v[j.first] = j.second; k = j.second; REP(l, N) { if (S[j.first][l] == '1') { if (v[l] < 0) { q.push(make_pair(l, k + 1)); } else if (v[l] != k - 1 && v[l] != k + 1) { k = -1; break; } } } if (k < 0) break; } maxk = max(maxk, k); } cout << maxk << endl; }
#include <algorithm> #include <assert.h> #include <climits> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <vector> using namespace std; template <typename T> inline bool sign(T A) { return (A > 0) - (A < 0); } #define REP(i, n) for (int i = 0; i < (n); ++i) #define SREP(i, s, n) \ for (int i = s; (n - i) * sign(n - s) > 0; i += sign(n - s)) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef unsigned long long ull; template <typename T> T gcd(T a, T b) { if (a < b) gcd(b, a); if (b == 1) return 1; T r; while ((r = a % b)) { a = b; b = r; } return b; } bool _less(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } template <template <class, class, class...> class C, typename K, typename V, typename... Args> V map_get(const C<K, V, Args...> &m, K const &key, const V &defval) { typename C<K, V, Args...>::const_iterator it = m.find(key); if (it == m.end()) return defval; return it->second; } #define MOD (1'000'000'000 + 7 #define INF (ll(1e18)) int main(void) { ll N; cin >> N; vector<string> S(N); REP(i, N) { cin >> S[i]; } ll maxk = -1; REP(i, N) { queue<pair<ll, ll>> q; vector<ll> v(N, -1); ll k = -1; q.push(make_pair(i, 1)); while (q.size()) { auto j = q.front(); q.pop(); if (v[j.first] >= 0 && v[j.first] != j.second) { k = -1; break; } else if (v[j.first] == j.second) { continue; } v[j.first] = j.second; k = j.second; REP(l, N) { if (S[j.first][l] == '1') { if (v[l] < 0) { q.push(make_pair(l, k + 1)); } else if (v[l] != k - 1 && v[l] != k + 1) { k = -1; break; } } } if (k < 0) break; } maxk = max(maxk, k); } cout << maxk << endl; }
insert
68
68
68
70
TLE
p02892
C++
Runtime Error
#include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; const int MAX_N = 1e3 + 10; int N, fl, ans; int col[MAX_N]; int cap; int head[MAX_N], to[MAX_N], nxt[MAX_N]; queue<int> que; inline int Abs(int x) { return x < 0 ? -x : x; } void BFS(int x) { memset(col, 0, sizeof col); col[x] = 1; que.push(x); while (!que.empty()) { int u = que.front(); que.pop(); ans = max(ans, col[u]); for (int i = head[u]; i; i = nxt[i]) { if (!col[to[i]]) { col[to[i]] = col[u] + 1; que.push(to[i]); } else if (Abs(col[to[i]] - col[u]) != 1) fl = 1; } if (fl) break; } } inline void add(int x, int y) { to[++cap] = y, nxt[cap] = head[x], head[x] = cap; } int main() { scanf("%d", &N); int x; for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { scanf("%1d", &x); if (x) add(i, j), add(j, i); } } for (int i = 1; i <= N; ++i) { BFS(i); if (fl) return puts("-1"), 0; } printf("%d\n", ans); return 0; }
#include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; const int MAX_N = 1e5 + 10; int N, fl, ans; int col[MAX_N]; int cap; int head[MAX_N], to[MAX_N], nxt[MAX_N]; queue<int> que; inline int Abs(int x) { return x < 0 ? -x : x; } void BFS(int x) { memset(col, 0, sizeof col); col[x] = 1; que.push(x); while (!que.empty()) { int u = que.front(); que.pop(); ans = max(ans, col[u]); for (int i = head[u]; i; i = nxt[i]) { if (!col[to[i]]) { col[to[i]] = col[u] + 1; que.push(to[i]); } else if (Abs(col[to[i]] - col[u]) != 1) fl = 1; } if (fl) break; } } inline void add(int x, int y) { to[++cap] = y, nxt[cap] = head[x], head[x] = cap; } int main() { scanf("%d", &N); int x; for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { scanf("%1d", &x); if (x) add(i, j), add(j, i); } } for (int i = 1; i <= N; ++i) { BFS(i); if (fl) return puts("-1"), 0; } printf("%d\n", ans); return 0; }
replace
7
8
7
8
0
p02892
C++
Runtime Error
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author parsa bahrami */ #pragma GCC optimize("O2") #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; typedef long long int ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<ld, ld> pld; typedef pair<string, string> pss; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define sz(x) (ll) x.size() #define jjoin(x) \ for (auto i : x) \ cout << i << endl; #define all(x) (x).begin(), (x).end() #define F first #define S second #define Mp make_pair #define sep ' ' #define error(x) cerr << #x << " = " << x << endl #define fast_io \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define set_random \ mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); #define kill(x) return cout << x << endl, 0; #define Hkill(x) \ cout << x << endl; \ exit(0); #define endl '\n' ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } const ll MAXN = 2e5 + 10; const ll MAXA = 101; const ll INF = 8e18; const ll MOD = 998244353; // 1e9 + 7 const ld PI = 4 * atan((ld)1); bool adj[MAXA][MAXA]; ll T[MAXN], H[MAXN], n; void dfs(ll v) { for (ll u = 1; u <= n; u++) { if (adj[u][v] && T[u] == INF) { T[u] = T[v] ^ 1; dfs(u); } else if (T[u] == T[v] && adj[u][v]) { // cout << T[u] << sep << 'u' << sep << T[v] << sep << 'v' << endl; Hkill(-1); } } } ll bfs(ll v) { fill(H, H + MAXN, 0); queue<ll> Q; Q.push(v); H[v] = 1; while (sz(Q)) { ll u = Q.front(); Q.pop(); for (ll ver = 1; ver <= n; ver++) { if (H[ver] == 0 && adj[ver][u]) { H[ver] = H[u] + 1; Q.push(ver); } } } return *max_element(H, H + MAXN); } int main() { fast_io; set_random; cin >> n; for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= n; j++) { char c; cin >> c; adj[i][j] = c - '0'; } } fill(T, T + MAXN, INF); T[1] = 1; dfs(1); ll ans = 0; for (ll i = 1; i <= n; i++) { ans = max(ans, bfs(i)); } cout << ans << endl; return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author parsa bahrami */ #pragma GCC optimize("O2") #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; typedef long long int ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<ld, ld> pld; typedef pair<string, string> pss; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define sz(x) (ll) x.size() #define jjoin(x) \ for (auto i : x) \ cout << i << endl; #define all(x) (x).begin(), (x).end() #define F first #define S second #define Mp make_pair #define sep ' ' #define error(x) cerr << #x << " = " << x << endl #define fast_io \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define set_random \ mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); #define kill(x) return cout << x << endl, 0; #define Hkill(x) \ cout << x << endl; \ exit(0); #define endl '\n' ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } const ll MAXN = 2e5 + 10; const ll MAXA = 210; const ll INF = 8e18; const ll MOD = 998244353; // 1e9 + 7 const ld PI = 4 * atan((ld)1); bool adj[MAXA][MAXA]; ll T[MAXN], H[MAXN], n; void dfs(ll v) { for (ll u = 1; u <= n; u++) { if (adj[u][v] && T[u] == INF) { T[u] = T[v] ^ 1; dfs(u); } else if (T[u] == T[v] && adj[u][v]) { // cout << T[u] << sep << 'u' << sep << T[v] << sep << 'v' << endl; Hkill(-1); } } } ll bfs(ll v) { fill(H, H + MAXN, 0); queue<ll> Q; Q.push(v); H[v] = 1; while (sz(Q)) { ll u = Q.front(); Q.pop(); for (ll ver = 1; ver <= n; ver++) { if (H[ver] == 0 && adj[ver][u]) { H[ver] = H[u] + 1; Q.push(ver); } } } return *max_element(H, H + MAXN); } int main() { fast_io; set_random; cin >> n; for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= n; j++) { char c; cin >> c; adj[i][j] = c - '0'; } } fill(T, T + MAXN, INF); T[1] = 1; dfs(1); ll ans = 0; for (ll i = 1; i <= n; i++) { ans = max(ans, bfs(i)); } cout << ans << endl; return 0; }
replace
54
55
54
55
0
p02892
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, m, n) for (int i = (m); i < (int)(n); i++) #define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--) #define rep(i, n) REP(i, 0, n) #define rrep(i, n) RREP(i, 0, n) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define fi first #define se second #define debug(...) \ { \ cerr << "[L" << __LINE__ << "] "; \ _debug(__VA_ARGS__); \ } template <typename T> string join(const vector<T> &v, string del = ", ") { stringstream s; for (auto x : v) s << del << x; return s.str().substr(del.size()); } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { if (v.size()) o << "[" << join(v) << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const vector<vector<T>> &vv) { int l = vv.size(); if (l) { o << endl; rep(i, l) o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : ""); } return o; } inline void _debug() { cerr << endl; } template <class First, class... Rest> void _debug(const First &first, const Rest &...rest) { cerr << first << " "; _debug(rest...); } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; const double PI = (1 * acos(0.0)); const double EPS = 1e-9; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3fLL; const ll mod = 1e9 + 7; inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); } vi g[300]; vi cost; bool dfs(int v, int c = 1) { cost[v] = c; for (auto u : g[v]) { if (cost[u] == 0 && !dfs(v, c + 1)) return false; if (abs(cost[v] - cost[u]) % 2 == 0) return false; } return true; } int bfs(int s) { int res = 0; queue<int> Q; Q.emplace(s); cost[s] = 1; while (!Q.empty()) { int v = Q.front(); Q.pop(); res = max(res, cost[v]); for (auto u : g[v]) { if (cost[u] == 0) { Q.emplace(u); cost[u] = cost[v] + 1; } } } return res; } int main() { ios_base::sync_with_stdio(0); // finput("./input"); int n; cin >> n; vector<string> s(n); rep(i, n) cin >> s[i]; cost = vi(n); rep(i, n) rep(j, n) { if (s[i][j] == '1') g[i].push_back(j); } fill(all(cost), 0); if (!dfs(0)) { cout << -1 << endl; } else { int ans = 0; rep(i, n) { fill(all(cost), 0); ans = max(ans, bfs(i)); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, m, n) for (int i = (m); i < (int)(n); i++) #define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--) #define rep(i, n) REP(i, 0, n) #define rrep(i, n) RREP(i, 0, n) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define fi first #define se second #define debug(...) \ { \ cerr << "[L" << __LINE__ << "] "; \ _debug(__VA_ARGS__); \ } template <typename T> string join(const vector<T> &v, string del = ", ") { stringstream s; for (auto x : v) s << del << x; return s.str().substr(del.size()); } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { if (v.size()) o << "[" << join(v) << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const vector<vector<T>> &vv) { int l = vv.size(); if (l) { o << endl; rep(i, l) o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : ""); } return o; } inline void _debug() { cerr << endl; } template <class First, class... Rest> void _debug(const First &first, const Rest &...rest) { cerr << first << " "; _debug(rest...); } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; const double PI = (1 * acos(0.0)); const double EPS = 1e-9; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3fLL; const ll mod = 1e9 + 7; inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); } vi g[300]; vi cost; bool dfs(int v, int c = 1) { cost[v] = c; for (auto u : g[v]) { if (cost[u] == 0 && !dfs(u, c + 1)) return false; if (abs(cost[v] - cost[u]) % 2 == 0) return false; } return true; } int bfs(int s) { int res = 0; queue<int> Q; Q.emplace(s); cost[s] = 1; while (!Q.empty()) { int v = Q.front(); Q.pop(); res = max(res, cost[v]); for (auto u : g[v]) { if (cost[u] == 0) { Q.emplace(u); cost[u] = cost[v] + 1; } } } return res; } int main() { ios_base::sync_with_stdio(0); // finput("./input"); int n; cin >> n; vector<string> s(n); rep(i, n) cin >> s[i]; cost = vi(n); rep(i, n) rep(j, n) { if (s[i][j] == '1') g[i].push_back(j); } fill(all(cost), 0); if (!dfs(0)) { cout << -1 << endl; } else { int ans = 0; rep(i, n) { fill(all(cost), 0); ans = max(ans, bfs(i)); } cout << ans << endl; } return 0; }
replace
66
67
66
67
-11
p02892
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; typedef long long int ll; typedef pair<int, int> Pii; const ll mod = 1000000007; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<string> S(N); for (auto &x : S) cin >> x; int best = 0; for (int i = 0; i < N; i++) { queue<Pii> que; que.emplace(i, 1); vector<int> depth(N, 0); vector<bool> visited(N, false); while (!que.empty()) { auto now = que.front(); que.pop(); visited[now.first] = true; depth[now.first] = now.second; for (int j = 0; j < N; j++) { if (S[now.first][j] == '0') continue; else if (visited[j]) { if (depth[now.first] - 1 != depth[j]) { cout << -1 << endl; // impossible return 0; } } else { que.emplace(j, now.second + 1); } } } for (int j = 0; j < N; j++) { if (depth[j] > best) { best = depth[j]; } } } cout << best << endl; return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; typedef long long int ll; typedef pair<int, int> Pii; const ll mod = 1000000007; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<string> S(N); for (auto &x : S) cin >> x; int best = 0; for (int i = 0; i < N; i++) { queue<Pii> que; que.emplace(i, 1); vector<int> depth(N, 0); vector<bool> visited(N, false); while (!que.empty()) { auto now = que.front(); que.pop(); if (visited[now.first]) continue; visited[now.first] = true; depth[now.first] = now.second; for (int j = 0; j < N; j++) { if (S[now.first][j] == '0') continue; else if (visited[j]) { if (depth[now.first] - 1 != depth[j]) { cout << -1 << endl; // impossible return 0; } } else { que.emplace(j, now.second + 1); } } } for (int j = 0; j < N; j++) { if (depth[j] > best) { best = depth[j]; } } } cout << best << endl; return 0; }
insert
33
33
33
35
TLE
p02892
C++
Time Limit Exceeded
#ifdef LOCAL #pragma GCC optimize("O0") #else #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx") #endif #include <algorithm> #include <bitset> #include <cmath> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; using VI = vector<int>; using VVI = vector<vector<int>>; using VLL = vector<ll>; using VVLL = vector<vector<ll>>; using VB = vector<bool>; using VVB = vector<vector<bool>>; using PII = pair<int, int>; template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; const int INF = 1e9 + 7; const ll INF_LL = (ll)1e18 + 7; #define __overload3(_1, _2, _3, name, ...) name #define rep(...) \ __overload3(__VA_ARGS__, repFromUntil, repUntil, repeat)(__VA_ARGS__) #define repeat(times) repFromUntil(__name##__LINE__, 0, times) #define repUntil(name, times) repFromUntil(name, 0, times) #define repFromUntil(name, from, until) \ for (int name = from, name##__until = (until); name < name##__until; name++) #define repr(...) \ __overload3(__VA_ARGS__, reprFromUntil, reprUntil, repeat)(__VA_ARGS__) #define reprUntil(name, times) reprFromUntil(name, 0, times) #define reprFromUntil(name, from, until) \ for (int name = until - 1, name##__from = (from); name >= name##__from; \ name--) #define EXIT(out) \ do { \ OUT(out); \ exit(0); \ } while (0) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define _1 first #define _2 second #define debug(v) \ do { \ debugos << "L" << __LINE__ << " " << #v << " > "; \ debugos << (v) << newl; \ } while (0) #define debugv(v) \ do { \ debugos << "L" << __LINE__ << " " << #v << " > "; \ for (auto e : (v)) { \ debugos << e << " "; \ } \ debugos << newl; \ } while (0) #define debuga(m, w) \ do { \ debugos << "L" << __LINE__ << " " << #m << " > "; \ for (int x = 0; x < (w); x++) { \ debugos << (m)[x] << " "; \ } \ debugos << newl; \ } while (0) #define debugaa(m, h, w) \ do { \ debugos << "L" << __LINE__ << " " << #m << " > \n"; \ for (int y = 0; y < (h); y++) { \ for (int x = 0; x < (w); x++) { \ debugos << (m)[y][x] << " "; \ } \ debugos << newl; \ } \ } while (0) #define newl "\n" constexpr int dr[] = {1, -1, 0, 0}; // LRUD constexpr int dc[] = {0, 0, 1, -1}; bool inside(int r, int c, int H, int W) { return 0 <= r and r < H and 0 <= c and c < W; } template <typename T> bool chmin(T &var, T x) { if (var > x) { var = x; return true; } else return false; } template <typename T> bool chmax(T &var, T x) { if (var < x) { var = x; return true; } else return false; } template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); } ll power(ll e, int t, ll mod = INF_LL) { ll res = 1; while (t) { if (t & 1) res = (res * e) % mod; t >>= 1; e = (e * e) % mod; } return res; } template <typename T> T divceil(T, T); template <typename T> T divfloor(T m, T d) { if (sgn(m) * sgn(d) >= 0) return m / d; else return -divceil(abs(m), abs(d)); } template <typename T> T divceil(T m, T d) { if (m >= 0 and d > 0) return (m + d - 1) / d; else if (m < 0 and d < 0) return divceil(-m, -d); else return -divfloor(abs(m), abs(d)); } template <typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } string operator*(const string &s, int times) { string res = ""; rep(times) res += s; return res; } class MyScanner { public: int offset = 0; template <typename T> void input_integer(T &var) { var = 0; T sign = 1; int cc = getchar(); for (; cc < '0' || '9' < cc; cc = getchar()) if (cc == '-') sign = -1; for (; '0' <= cc && cc <= '9'; cc = getchar()) var = (var << 3) + (var << 1) + cc - '0'; var = var * sign; var += offset; } int c() { char c; while (c = getchar(), c == ' ' or c == '\n') ; return c; } MyScanner &operator>>(char &var) { var = c(); return *this; } MyScanner &operator>>(int &var) { input_integer<int>(var); return *this; } MyScanner &operator>>(ll &var) { input_integer<ll>(var); return *this; } MyScanner &operator>>(string &var) { var = ""; int cc = getchar(); for (; !isvisiblechar(cc); cc = getchar()) ; for (; isvisiblechar(cc); cc = getchar()) var.push_back(cc); return *this; } template <typename T> operator T() { T x; *this >> x; return x; } template <typename T> void operator()(T &t) { *this >> t; } template <typename T, typename... Ts> void operator()(T &t, Ts &...ts) { *this >> t; this->operator()(ts...); } template <typename Iter> void iter(Iter first, Iter last) { while (first != last) *this >> *first, first++; } VI vi(int n) { VI res(n); iter(all(res)); return res; } VVI vvi(int n, int m) { VVI res(n); rep(i, n) res[i] = vi(m); return res; } VLL vll(int n) { VLL res(n); iter(all(res)); return res; } VVLL vvll(int n, int m) { VVLL res(n); rep(i, n) res[i] = vll(m); return res; } template <typename T> vector<T> v(int n) { vector<T> res(n); iter(all(res)); return res; } private: int isvisiblechar(int c) { return 0x21 <= c && c <= 0x7E; } } IN, IN1{-1}; class MyPrinter { public: template <typename T> void output_integer(T var) { if (var == 0) { putchar('0'); return; } if (var < 0) putchar('-'), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putchar(stack[--stack_p]); } MyPrinter &operator<<(char c) { putchar(c); return *this; } MyPrinter &operator<<(double x) { printf("%.10f\n", x); return *this; } template <typename T> MyPrinter &operator<<(T var) { output_integer<T>(var); return *this; } MyPrinter &operator<<(char *str_p) { while (*str_p) putchar(*(str_p++)); return *this; } MyPrinter &operator<<(const char *str_p) { while (*str_p) putchar(*(str_p++)); return *this; } MyPrinter &operator<<(const string &str) { const char *p = str.c_str(); const char *l = p + str.size(); while (p < l) putchar(*p++); return *this; } template <typename T> void operator()(T x) { *this << x << newl; } template <typename T, typename... Ts> void operator()(T x, Ts... xs) { *this << x << " "; this->operator()(xs...); } template <typename Iter> void iter(Iter s, Iter t) { if (s == t) *this << "\n"; else { for (; s != t; s++) { *this << *s << " \n"[next(s, 1) == t]; } } } template <typename Range> void range(const Range &r) { iter(begin(r), end(r)); } } OUT; class DebugPrint { public: template <typename T> DebugPrint &operator<<(const T &v) { #ifdef LOCAL cerr << v; #endif return *this; } } debugos; #pragma GCC diagnostic ignored "-Wshadow" template <typename T, typename Cmb, typename Upd> struct segtree { const int n; const T unit; const Cmb cmb; const Upd upd; vector<T> data; segtree(int n = 0, T unit = T(), Cmb cmb = Cmb(), Upd upd = Upd()) : n(n), unit(unit), cmb(cmb), upd(upd), data(n << 1, unit) { build(); } template <typename Iter> segtree(Iter first, Iter last, int n, T unit = T(), Cmb cmb = Cmb(), Upd upd = Upd()) : n(n), unit(unit), cmb(cmb), upd(upd), data(n << 1) { assign(first, last); } void build() { repr(i, n) data[i] = cmb(data[i << 1], data[i << 1 | 1]); } template <typename Iter> void assign(Iter first, Iter last) { copy(first, last, data.begin() + n); build(); } void modify(int l, T v) { l += n; data[l] = upd(data[l], v); for (; l > 1; l >>= 1) data[l >> 1] = cmb(data[l & (~1)], data[l | 1]); } T query(int l, int r) { if (l == r) return unit; if (l + 1 == r) return data[l + n]; T resl = data[l += n], resr = data[(r += n) - 1]; for (l++, r--; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = cmb(resl, data[l++]); if (r & 1) resr = cmb(data[--r], resr); } return cmb(resl, resr); } }; #pragma GCC diagnostic warning "-Wshadow" template <typename T> struct minT { T operator()(T a, T b) const { return min(a, b); } }; template <typename T> struct maxT { T operator()(T a, T b) const { return max(a, b); } }; template <typename T> struct assign { T operator()(T a, T b) const { return b; } }; template <typename T, typename Upd = assign<T>> using RangeMin = segtree<T, minT<T>, Upd>; template <typename T, typename Upd = assign<T>> using RangeMax = segtree<T, maxT<T>, maxT<T>>; template <typename T, typename U> ostream &operator<<(ostream &out, pair<T, U> var) { return out << var.first << " " << var.second; } template <typename Tuple, size_t I, size_t N, enable_if_t<I == N> * = nullptr> ostream &tuple_impl(ostream &out, Tuple var) { return out; } template <typename Tuple, size_t I, size_t N, enable_if_t<I != N> * = nullptr> ostream &tuple_impl(ostream &out, Tuple var) { out << get<I>(var) << " "; return tuple_impl<Tuple, I + 1, N>(out, var); } template <typename... Ts> ostream &operator<<(ostream &out, tuple<Ts...> var) { return tuple_impl<tuple<Ts...>, 0, sizeof...(Ts)>(out, var); } template <typename T, typename U> MyPrinter &operator<<(MyPrinter &out, pair<T, U> var) { return out << var.first << " " << var.second; } template <typename Tuple, size_t I, size_t N, enable_if_t<I == N> * = nullptr> MyPrinter &tuple_impl(MyPrinter &out, Tuple var) { return out; } template <typename Tuple, size_t I, size_t N, enable_if_t<I != N> * = nullptr> MyPrinter &tuple_impl(MyPrinter &out, Tuple var) { out << get<I>(var) << " "; return tuple_impl<Tuple, I + 1, N>(out, var); } template <typename... Ts> MyPrinter &operator<<(MyPrinter &out, tuple<Ts...> var) { return tuple_impl<tuple<Ts...>, 0, sizeof...(Ts)>(out, var); } template <typename T, typename U> MyScanner &operator>>(MyScanner &in, pair<T, U> &var) { return in >> var.first >> var.second; } template <typename... Ts> MyScanner &operator>>(MyScanner &in, tuple<Ts...> &var) { return tuple_impl<tuple<Ts...>, 0, sizeof...(Ts)>(in, var); } template <typename Tuple, size_t I, size_t N, enable_if_t<I == N> * = nullptr> MyScanner &tuple_impl(MyScanner &in, Tuple &var) { return in; } template <typename Tuple, size_t I, size_t N, enable_if_t<I != N> * = nullptr> MyScanner &tuple_impl(MyScanner &in, Tuple &var) { in >> get<I>(var); return tuple_impl<Tuple, I + 1, N>(in, var); } #pragma GCC diagnostic ignored "-Wshadow" struct union_find { int n; vector<int> par, rank, sz; union_find(int n) : n(n), par(n), rank(n), sz(n) { iota(all(par), 0); fill(all(sz), 1); } int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); } void merge(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); par[y] = x; if (rank[x] == rank[y]) rank[x]++; sz[x] += sz[y]; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return sz[root(x)]; } }; #pragma GCC diagnostic warning "-Wshadow" int main() { int n = IN; auto S = IN.v<string>(n); VI dist(n), par(n); VB done(n); stack<int> s; s.push(0); dist[0] = 0; par[0] = -1; done[0] = true; while (not s.empty()) { int v = s.top(); s.pop(); rep(next, n) if (S[v][next] == '1' and next != par[v]) { if (done[next]) { if (dist[v] % 2 == dist[next] % 2) EXIT(-1); continue; } dist[next] = dist[v] + 1; par[next] = v; s.push(next); } } auto d = make_v(n, n, INF); rep(i, n) d[i][i] = 0; rep(i, n) rep(j, n) if (S[i][j] == '1') d[i][j] = 1; rep(k, n) rep(i, n) rep(j, n) chmin(d[i][j], d[i][k] + d[k][j]); int D = 0; rep(i, n) rep(j, n) chmax(D, d[i][j]); OUT(D + 1); }
#ifdef LOCAL #pragma GCC optimize("O0") #else #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx") #endif #include <algorithm> #include <bitset> #include <cmath> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; using VI = vector<int>; using VVI = vector<vector<int>>; using VLL = vector<ll>; using VVLL = vector<vector<ll>>; using VB = vector<bool>; using VVB = vector<vector<bool>>; using PII = pair<int, int>; template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; const int INF = 1e9 + 7; const ll INF_LL = (ll)1e18 + 7; #define __overload3(_1, _2, _3, name, ...) name #define rep(...) \ __overload3(__VA_ARGS__, repFromUntil, repUntil, repeat)(__VA_ARGS__) #define repeat(times) repFromUntil(__name##__LINE__, 0, times) #define repUntil(name, times) repFromUntil(name, 0, times) #define repFromUntil(name, from, until) \ for (int name = from, name##__until = (until); name < name##__until; name++) #define repr(...) \ __overload3(__VA_ARGS__, reprFromUntil, reprUntil, repeat)(__VA_ARGS__) #define reprUntil(name, times) reprFromUntil(name, 0, times) #define reprFromUntil(name, from, until) \ for (int name = until - 1, name##__from = (from); name >= name##__from; \ name--) #define EXIT(out) \ do { \ OUT(out); \ exit(0); \ } while (0) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define _1 first #define _2 second #define debug(v) \ do { \ debugos << "L" << __LINE__ << " " << #v << " > "; \ debugos << (v) << newl; \ } while (0) #define debugv(v) \ do { \ debugos << "L" << __LINE__ << " " << #v << " > "; \ for (auto e : (v)) { \ debugos << e << " "; \ } \ debugos << newl; \ } while (0) #define debuga(m, w) \ do { \ debugos << "L" << __LINE__ << " " << #m << " > "; \ for (int x = 0; x < (w); x++) { \ debugos << (m)[x] << " "; \ } \ debugos << newl; \ } while (0) #define debugaa(m, h, w) \ do { \ debugos << "L" << __LINE__ << " " << #m << " > \n"; \ for (int y = 0; y < (h); y++) { \ for (int x = 0; x < (w); x++) { \ debugos << (m)[y][x] << " "; \ } \ debugos << newl; \ } \ } while (0) #define newl "\n" constexpr int dr[] = {1, -1, 0, 0}; // LRUD constexpr int dc[] = {0, 0, 1, -1}; bool inside(int r, int c, int H, int W) { return 0 <= r and r < H and 0 <= c and c < W; } template <typename T> bool chmin(T &var, T x) { if (var > x) { var = x; return true; } else return false; } template <typename T> bool chmax(T &var, T x) { if (var < x) { var = x; return true; } else return false; } template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); } ll power(ll e, int t, ll mod = INF_LL) { ll res = 1; while (t) { if (t & 1) res = (res * e) % mod; t >>= 1; e = (e * e) % mod; } return res; } template <typename T> T divceil(T, T); template <typename T> T divfloor(T m, T d) { if (sgn(m) * sgn(d) >= 0) return m / d; else return -divceil(abs(m), abs(d)); } template <typename T> T divceil(T m, T d) { if (m >= 0 and d > 0) return (m + d - 1) / d; else if (m < 0 and d < 0) return divceil(-m, -d); else return -divfloor(abs(m), abs(d)); } template <typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } string operator*(const string &s, int times) { string res = ""; rep(times) res += s; return res; } class MyScanner { public: int offset = 0; template <typename T> void input_integer(T &var) { var = 0; T sign = 1; int cc = getchar(); for (; cc < '0' || '9' < cc; cc = getchar()) if (cc == '-') sign = -1; for (; '0' <= cc && cc <= '9'; cc = getchar()) var = (var << 3) + (var << 1) + cc - '0'; var = var * sign; var += offset; } int c() { char c; while (c = getchar(), c == ' ' or c == '\n') ; return c; } MyScanner &operator>>(char &var) { var = c(); return *this; } MyScanner &operator>>(int &var) { input_integer<int>(var); return *this; } MyScanner &operator>>(ll &var) { input_integer<ll>(var); return *this; } MyScanner &operator>>(string &var) { var = ""; int cc = getchar(); for (; !isvisiblechar(cc); cc = getchar()) ; for (; isvisiblechar(cc); cc = getchar()) var.push_back(cc); return *this; } template <typename T> operator T() { T x; *this >> x; return x; } template <typename T> void operator()(T &t) { *this >> t; } template <typename T, typename... Ts> void operator()(T &t, Ts &...ts) { *this >> t; this->operator()(ts...); } template <typename Iter> void iter(Iter first, Iter last) { while (first != last) *this >> *first, first++; } VI vi(int n) { VI res(n); iter(all(res)); return res; } VVI vvi(int n, int m) { VVI res(n); rep(i, n) res[i] = vi(m); return res; } VLL vll(int n) { VLL res(n); iter(all(res)); return res; } VVLL vvll(int n, int m) { VVLL res(n); rep(i, n) res[i] = vll(m); return res; } template <typename T> vector<T> v(int n) { vector<T> res(n); iter(all(res)); return res; } private: int isvisiblechar(int c) { return 0x21 <= c && c <= 0x7E; } } IN, IN1{-1}; class MyPrinter { public: template <typename T> void output_integer(T var) { if (var == 0) { putchar('0'); return; } if (var < 0) putchar('-'), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putchar(stack[--stack_p]); } MyPrinter &operator<<(char c) { putchar(c); return *this; } MyPrinter &operator<<(double x) { printf("%.10f\n", x); return *this; } template <typename T> MyPrinter &operator<<(T var) { output_integer<T>(var); return *this; } MyPrinter &operator<<(char *str_p) { while (*str_p) putchar(*(str_p++)); return *this; } MyPrinter &operator<<(const char *str_p) { while (*str_p) putchar(*(str_p++)); return *this; } MyPrinter &operator<<(const string &str) { const char *p = str.c_str(); const char *l = p + str.size(); while (p < l) putchar(*p++); return *this; } template <typename T> void operator()(T x) { *this << x << newl; } template <typename T, typename... Ts> void operator()(T x, Ts... xs) { *this << x << " "; this->operator()(xs...); } template <typename Iter> void iter(Iter s, Iter t) { if (s == t) *this << "\n"; else { for (; s != t; s++) { *this << *s << " \n"[next(s, 1) == t]; } } } template <typename Range> void range(const Range &r) { iter(begin(r), end(r)); } } OUT; class DebugPrint { public: template <typename T> DebugPrint &operator<<(const T &v) { #ifdef LOCAL cerr << v; #endif return *this; } } debugos; #pragma GCC diagnostic ignored "-Wshadow" template <typename T, typename Cmb, typename Upd> struct segtree { const int n; const T unit; const Cmb cmb; const Upd upd; vector<T> data; segtree(int n = 0, T unit = T(), Cmb cmb = Cmb(), Upd upd = Upd()) : n(n), unit(unit), cmb(cmb), upd(upd), data(n << 1, unit) { build(); } template <typename Iter> segtree(Iter first, Iter last, int n, T unit = T(), Cmb cmb = Cmb(), Upd upd = Upd()) : n(n), unit(unit), cmb(cmb), upd(upd), data(n << 1) { assign(first, last); } void build() { repr(i, n) data[i] = cmb(data[i << 1], data[i << 1 | 1]); } template <typename Iter> void assign(Iter first, Iter last) { copy(first, last, data.begin() + n); build(); } void modify(int l, T v) { l += n; data[l] = upd(data[l], v); for (; l > 1; l >>= 1) data[l >> 1] = cmb(data[l & (~1)], data[l | 1]); } T query(int l, int r) { if (l == r) return unit; if (l + 1 == r) return data[l + n]; T resl = data[l += n], resr = data[(r += n) - 1]; for (l++, r--; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = cmb(resl, data[l++]); if (r & 1) resr = cmb(data[--r], resr); } return cmb(resl, resr); } }; #pragma GCC diagnostic warning "-Wshadow" template <typename T> struct minT { T operator()(T a, T b) const { return min(a, b); } }; template <typename T> struct maxT { T operator()(T a, T b) const { return max(a, b); } }; template <typename T> struct assign { T operator()(T a, T b) const { return b; } }; template <typename T, typename Upd = assign<T>> using RangeMin = segtree<T, minT<T>, Upd>; template <typename T, typename Upd = assign<T>> using RangeMax = segtree<T, maxT<T>, maxT<T>>; template <typename T, typename U> ostream &operator<<(ostream &out, pair<T, U> var) { return out << var.first << " " << var.second; } template <typename Tuple, size_t I, size_t N, enable_if_t<I == N> * = nullptr> ostream &tuple_impl(ostream &out, Tuple var) { return out; } template <typename Tuple, size_t I, size_t N, enable_if_t<I != N> * = nullptr> ostream &tuple_impl(ostream &out, Tuple var) { out << get<I>(var) << " "; return tuple_impl<Tuple, I + 1, N>(out, var); } template <typename... Ts> ostream &operator<<(ostream &out, tuple<Ts...> var) { return tuple_impl<tuple<Ts...>, 0, sizeof...(Ts)>(out, var); } template <typename T, typename U> MyPrinter &operator<<(MyPrinter &out, pair<T, U> var) { return out << var.first << " " << var.second; } template <typename Tuple, size_t I, size_t N, enable_if_t<I == N> * = nullptr> MyPrinter &tuple_impl(MyPrinter &out, Tuple var) { return out; } template <typename Tuple, size_t I, size_t N, enable_if_t<I != N> * = nullptr> MyPrinter &tuple_impl(MyPrinter &out, Tuple var) { out << get<I>(var) << " "; return tuple_impl<Tuple, I + 1, N>(out, var); } template <typename... Ts> MyPrinter &operator<<(MyPrinter &out, tuple<Ts...> var) { return tuple_impl<tuple<Ts...>, 0, sizeof...(Ts)>(out, var); } template <typename T, typename U> MyScanner &operator>>(MyScanner &in, pair<T, U> &var) { return in >> var.first >> var.second; } template <typename... Ts> MyScanner &operator>>(MyScanner &in, tuple<Ts...> &var) { return tuple_impl<tuple<Ts...>, 0, sizeof...(Ts)>(in, var); } template <typename Tuple, size_t I, size_t N, enable_if_t<I == N> * = nullptr> MyScanner &tuple_impl(MyScanner &in, Tuple &var) { return in; } template <typename Tuple, size_t I, size_t N, enable_if_t<I != N> * = nullptr> MyScanner &tuple_impl(MyScanner &in, Tuple &var) { in >> get<I>(var); return tuple_impl<Tuple, I + 1, N>(in, var); } #pragma GCC diagnostic ignored "-Wshadow" struct union_find { int n; vector<int> par, rank, sz; union_find(int n) : n(n), par(n), rank(n), sz(n) { iota(all(par), 0); fill(all(sz), 1); } int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); } void merge(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); par[y] = x; if (rank[x] == rank[y]) rank[x]++; sz[x] += sz[y]; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return sz[root(x)]; } }; #pragma GCC diagnostic warning "-Wshadow" int main() { int n = IN; auto S = IN.v<string>(n); VI m(n, -1); m[0] = 0; rep(n) rep(i, n) if (m[i] != -1) rep(j, n) if (S[i][j] == '1') { if (m[i] == m[j]) EXIT(-1); m[j] = not m[i]; } auto d = make_v(n, n, INF); rep(i, n) d[i][i] = 0; rep(i, n) rep(j, n) if (S[i][j] == '1') d[i][j] = 1; rep(k, n) rep(i, n) rep(j, n) chmin(d[i][j], d[i][k] + d[k][j]); int D = 0; rep(i, n) rep(j, n) chmax(D, d[i][j]); OUT(D + 1); }
replace
466
486
466
472
TLE
p02892
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using ld = long double; #define fs first #define sc second #define pb push_back #define mp make_pair #define mt make_tuple #define eb emplace_back #define all(v) (v).begin(), (v).end() #define siz(v) (ll)(v).size() #define rep(i, a, n) for (ll i = a; i < (ll)(n); i++) #define repr(i, a, n) for (ll i = n - 1; (ll)a <= i; i--) #define lb lower_bound #define ub upper_bound typedef pair<int, int> P; typedef pair<ll, ll> PL; const ll mod = 1000000007; const ll INF = 1000000099; const ll LINF = (ll)(1e18 + 99); vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1}; template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <typename T> T mpow(T a, T n) { T res = 1; for (; n; n >>= 1) { if (n & 1) res = res * a; a = a * a; } return res; } vector<string> v(200); template <typename T> class UnionFind { public: // 以下公開 // 親の番号を格納する。親である場合は-(その集合のサイズ) vector<T> Parent; // 作るときはParentの値を全て-1にする // こうすると全てバラバラになる UnionFind(T N) { Parent = vector<T>(N, -1); } // Aがどのグループに属しているか調べる T root(T A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } // 代入しておくことで参照を速くする // 自分のいるグループの頂点数を調べる T size(T A) { return -Parent[root(A)]; } // 親である場合は-(その集合のサイズ)なので、Aの親の // AとBをくっ付ける bool connect(T A, T B) { // AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける A = root(A); B = root(B); if (A == B) { // すでにくっついてるからくっ付けない return false; } // 大きい方(A)に小さいほう(B)をくっ付けたい // 大小が逆だったらひっくり返しちゃう。 if (size(A) < size(B)) swap(A, B); // Aのサイズを更新する Parent[A] += Parent[B]; // Bの親をAに変更する Parent[B] = A; return true; } bool same(T A, T B) { return root(A) == root(B); } }; ll dp[110][110] = {}; signed main() { ll n, ans = -1; cin >> n; UnionFind<ll> u(2 * n); rep(i, 0, n) { cin >> v[i]; rep(j, 0, n) { if (v[i][j] == '1') { u.connect(i, j + n); u.connect(i + n, j); dp[i][j] = 1; } else { if (i != j) dp[i][j] = INF; } } } rep(i, 0, n) { if (u.same(i, i + n)) { cout << -1 << endl; return 0; } } rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); } rep(i, 0, n) rep(j, 0, n) { ans = max(ans, dp[i][j]); } cout << ans + 1 << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using ld = long double; #define fs first #define sc second #define pb push_back #define mp make_pair #define mt make_tuple #define eb emplace_back #define all(v) (v).begin(), (v).end() #define siz(v) (ll)(v).size() #define rep(i, a, n) for (ll i = a; i < (ll)(n); i++) #define repr(i, a, n) for (ll i = n - 1; (ll)a <= i; i--) #define lb lower_bound #define ub upper_bound typedef pair<int, int> P; typedef pair<ll, ll> PL; const ll mod = 1000000007; const ll INF = 1000000099; const ll LINF = (ll)(1e18 + 99); vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1}; template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <typename T> T mpow(T a, T n) { T res = 1; for (; n; n >>= 1) { if (n & 1) res = res * a; a = a * a; } return res; } vector<string> v(200); template <typename T> class UnionFind { public: // 以下公開 // 親の番号を格納する。親である場合は-(その集合のサイズ) vector<T> Parent; // 作るときはParentの値を全て-1にする // こうすると全てバラバラになる UnionFind(T N) { Parent = vector<T>(N, -1); } // Aがどのグループに属しているか調べる T root(T A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } // 代入しておくことで参照を速くする // 自分のいるグループの頂点数を調べる T size(T A) { return -Parent[root(A)]; } // 親である場合は-(その集合のサイズ)なので、Aの親の // AとBをくっ付ける bool connect(T A, T B) { // AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける A = root(A); B = root(B); if (A == B) { // すでにくっついてるからくっ付けない return false; } // 大きい方(A)に小さいほう(B)をくっ付けたい // 大小が逆だったらひっくり返しちゃう。 if (size(A) < size(B)) swap(A, B); // Aのサイズを更新する Parent[A] += Parent[B]; // Bの親をAに変更する Parent[B] = A; return true; } bool same(T A, T B) { return root(A) == root(B); } }; ll dp[210][210] = {}; signed main() { ll n, ans = -1; cin >> n; UnionFind<ll> u(2 * n); rep(i, 0, n) { cin >> v[i]; rep(j, 0, n) { if (v[i][j] == '1') { u.connect(i, j + n); u.connect(i + n, j); dp[i][j] = 1; } else { if (i != j) dp[i][j] = INF; } } } rep(i, 0, n) { if (u.same(i, i + n)) { cout << -1 << endl; return 0; } } rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); } rep(i, 0, n) rep(j, 0, n) { ans = max(ans, dp[i][j]); } cout << ans + 1 << endl; }
replace
84
85
84
85
0
p02892
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #define REP(i, a, n) for (int i = a; i <= n; ++i) #define PER(i, a, n) for (int i = n; i >= a; --i) #define hr putchar(10) #define pb push_back #define lc (o << 1) #define rc (lc | 1) #define mid ((l + r) >> 1) #define ls lc, l, mid #define rs rc, mid + 1, r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) \ ({ \ REP(__i, 1, n) cout << a[__i] << ','; \ hr; \ }) using namespace std; typedef long long ll; typedef pair<int, int> pii; const int P = 1e9 + 7, INF = 0x3f3f3f3f; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll qpow(ll a, ll n) { ll r = 1 % P; for (a %= P; n; a = a * a % P, n >>= 1) if (n & 1) r = r * a % P; return r; } ll inv(ll x) { return x <= 1 ? 1 : inv(P % x) * (P - P / x) % P; } inline int rd() { int x = 0; char p = getchar(); while (p < '0' || p > '9') p = getchar(); while (p >= '0' && p <= '9') x = x * 10 + p - '0', p = getchar(); return x; } // head const int N = 10; int n; char s[N]; bitset<N> a[N], now, t, vis; int main() { scanf("%d", &n); REP(i, 0, n - 1) { scanf("%s", s); REP(j, 0, n - 1) a[i][j] = s[j] - '0'; } int ans = -1; REP(i, 0, n - 1) { int tot = 1, ret = 0, ok = 1; now.reset(); vis.reset(); now.set(i); vis.set(i); while (1) { ++ret; t.reset(); REP(i, 0, n - 1) if (now[i]) t |= a[i]; if ((t & now).any()) { ok = 0; break; } if (vis.count() == n) break; vis |= t; now = t; } if (ok) { ans = max(ans, ret); } } printf("%d\n", ans); }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #define REP(i, a, n) for (int i = a; i <= n; ++i) #define PER(i, a, n) for (int i = n; i >= a; --i) #define hr putchar(10) #define pb push_back #define lc (o << 1) #define rc (lc | 1) #define mid ((l + r) >> 1) #define ls lc, l, mid #define rs rc, mid + 1, r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) \ ({ \ REP(__i, 1, n) cout << a[__i] << ','; \ hr; \ }) using namespace std; typedef long long ll; typedef pair<int, int> pii; const int P = 1e9 + 7, INF = 0x3f3f3f3f; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll qpow(ll a, ll n) { ll r = 1 % P; for (a %= P; n; a = a * a % P, n >>= 1) if (n & 1) r = r * a % P; return r; } ll inv(ll x) { return x <= 1 ? 1 : inv(P % x) * (P - P / x) % P; } inline int rd() { int x = 0; char p = getchar(); while (p < '0' || p > '9') p = getchar(); while (p >= '0' && p <= '9') x = x * 10 + p - '0', p = getchar(); return x; } // head const int N = 210; int n; char s[N]; bitset<N> a[N], now, t, vis; int main() { scanf("%d", &n); REP(i, 0, n - 1) { scanf("%s", s); REP(j, 0, n - 1) a[i][j] = s[j] - '0'; } int ans = -1; REP(i, 0, n - 1) { int tot = 1, ret = 0, ok = 1; now.reset(); vis.reset(); now.set(i); vis.set(i); while (1) { ++ret; t.reset(); REP(i, 0, n - 1) if (now[i]) t |= a[i]; if ((t & now).any()) { ok = 0; break; } if (vis.count() == n) break; vis |= t; now = t; } if (ok) { ans = max(ans, ret); } } printf("%d\n", ans); }
replace
55
56
55
56
0
p02892
C++
Time Limit Exceeded
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; using ll = long long; using datas = pair<ll, ll>; using ddatas = pair<double, double>; using tdata = pair<ll, datas>; using vec = vector<ll>; using mat = vector<vec>; using pvec = vector<datas>; using pmat = vector<pvec>; #define For(i, a, b) for (i = a; i < b; i++) #define bFor(i, a, b) for (i = a; i >= b; i--) #define rep(i, N) For(i, 0, N) #define rep1(i, N) For(i, 1, N) #define brep(i, N) bFor(i, N - 1, 0) #define all(v) (v).begin(), (v).end() #define allr(v) (v).rbegin(), (v).rend() #define vsort(v) sort(all(v)) #define vrsort(v) sort(allr(v)) #define endl "\n" #define pb push_back #define output(v) \ do { \ bool f = 0; \ for (auto outi : v) { \ cout << (f ? " " : "") << outi; \ f = 1; \ } \ cout << endl; \ } while (0) const ll mod = 1000000007; const ll inf = 1LL << 60; template <class T> inline bool chmax(T &a, T b) { bool x = a < b; if (x) a = b; return x; } template <class T> inline bool chmin(T &a, T b) { bool x = a > b; if (x) a = b; return x; } double distance(ddatas &x, ddatas &y) { double a = x.first - y.first, b = x.second - y.second; return sqrt(a * a + b * b); } ll modinv(ll a) { ll b = mod, u = 1, v = 0, t; while (b) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return (u + mod) % mod; } ll moddevide(ll a, ll b) { return (a * modinv(b)) % mod; } vec modncrlistp, modncrlistm; ll modncr(ll n, ll r) { ll i, size = modncrlistp.size(); if (size <= n) { modncrlistp.resize(n + 1); modncrlistm.resize(n + 1); if (!size) { modncrlistp[0] = modncrlistm[0] = 1; size++; } For(i, size, n + 1) { modncrlistp[i] = modncrlistp[i - 1] * i % mod; modncrlistm[i] = modinv(modncrlistp[i]); } } return modncrlistp[n] * modncrlistm[r] % mod * modncrlistm[n - r] % mod; } ll modpow(ll a, ll n) { ll res = 1; while (n) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } ll gcd(ll a, ll b) { if (!b) return a; return (a % b == 0) ? b : gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll countdigits(ll n) { ll ans = 0; while (n) { n /= 10; ans++; } return ans; } ll sumdigits(ll n) { ll ans = 0; while (n) { ans += n % 10; n /= 10; } return ans; } int main() { ll i, j, k, n, ans = 0; cin >> n; string s; mat G(n), g(n, vec(n)); vec color(n, -1); rep(i, n) { cin >> s; rep(j, n) { g[i][j] = (s[j] == '1') ? 1 : inf; if (g[i][j] == 1) { G[i].pb(j); } } g[i][i] = 0; } color[0] = 0; queue<datas> que; for (auto i : G[0]) { que.push(datas(i, 1)); } while (!que.empty()) { auto x = que.front(); que.pop(); if (color[x.first] != -1 && color[x.first] != x.second) { cout << -1 << endl; return 0; } color[x.first] = x.second; for (auto i : G[x.first]) { if (color[i] != -1) { if (color[i] == x.second) { cout << -1 << endl; return 0; } } else { que.push(datas(i, 1 - x.second)); } } } rep(k, n) { rep(i, n) { rep(j, n) { g[i][j] = min(g[i][j], g[i][k] + g[k][j]); } } } rep(i, n) { ll res = *max_element(all(g[i])); ans = max(res, ans); } cout << ans + 1 << endl; return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; using ll = long long; using datas = pair<ll, ll>; using ddatas = pair<double, double>; using tdata = pair<ll, datas>; using vec = vector<ll>; using mat = vector<vec>; using pvec = vector<datas>; using pmat = vector<pvec>; #define For(i, a, b) for (i = a; i < b; i++) #define bFor(i, a, b) for (i = a; i >= b; i--) #define rep(i, N) For(i, 0, N) #define rep1(i, N) For(i, 1, N) #define brep(i, N) bFor(i, N - 1, 0) #define all(v) (v).begin(), (v).end() #define allr(v) (v).rbegin(), (v).rend() #define vsort(v) sort(all(v)) #define vrsort(v) sort(allr(v)) #define endl "\n" #define pb push_back #define output(v) \ do { \ bool f = 0; \ for (auto outi : v) { \ cout << (f ? " " : "") << outi; \ f = 1; \ } \ cout << endl; \ } while (0) const ll mod = 1000000007; const ll inf = 1LL << 60; template <class T> inline bool chmax(T &a, T b) { bool x = a < b; if (x) a = b; return x; } template <class T> inline bool chmin(T &a, T b) { bool x = a > b; if (x) a = b; return x; } double distance(ddatas &x, ddatas &y) { double a = x.first - y.first, b = x.second - y.second; return sqrt(a * a + b * b); } ll modinv(ll a) { ll b = mod, u = 1, v = 0, t; while (b) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return (u + mod) % mod; } ll moddevide(ll a, ll b) { return (a * modinv(b)) % mod; } vec modncrlistp, modncrlistm; ll modncr(ll n, ll r) { ll i, size = modncrlistp.size(); if (size <= n) { modncrlistp.resize(n + 1); modncrlistm.resize(n + 1); if (!size) { modncrlistp[0] = modncrlistm[0] = 1; size++; } For(i, size, n + 1) { modncrlistp[i] = modncrlistp[i - 1] * i % mod; modncrlistm[i] = modinv(modncrlistp[i]); } } return modncrlistp[n] * modncrlistm[r] % mod * modncrlistm[n - r] % mod; } ll modpow(ll a, ll n) { ll res = 1; while (n) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } ll gcd(ll a, ll b) { if (!b) return a; return (a % b == 0) ? b : gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll countdigits(ll n) { ll ans = 0; while (n) { n /= 10; ans++; } return ans; } ll sumdigits(ll n) { ll ans = 0; while (n) { ans += n % 10; n /= 10; } return ans; } int main() { ll i, j, k, n, ans = 0; cin >> n; string s; mat G(n), g(n, vec(n)); vec color(n, -1); rep(i, n) { cin >> s; rep(j, n) { g[i][j] = (s[j] == '1') ? 1 : inf; if (g[i][j] == 1) { G[i].pb(j); } } g[i][i] = 0; } color[0] = 0; queue<datas> que; for (auto i : G[0]) { que.push(datas(i, 1)); } while (!que.empty()) { auto x = que.front(); que.pop(); if (color[x.first] != -1) { if (color[x.first] != x.second) { cout << -1 << endl; return 0; } continue; } color[x.first] = x.second; for (auto i : G[x.first]) { if (color[i] != -1) { if (color[i] == x.second) { cout << -1 << endl; return 0; } } else { que.push(datas(i, 1 - x.second)); } } } rep(k, n) { rep(i, n) { rep(j, n) { g[i][j] = min(g[i][j], g[i][k] + g[k][j]); } } } rep(i, n) { ll res = *max_element(all(g[i])); ans = max(res, ans); } cout << ans + 1 << endl; return 0; }
replace
144
147
144
150
TLE
p02892
C++
Runtime Error
#include <bits/stdc++.h> #define ADD(a, b) a = (a + ll(b)) % mod #define MUL(a, b) a = (a * ll(b)) % mod #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define rep(i, a, b) for (int i = int(a); i < int(b); i++) #define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--) #define all(a) (a).begin(), (a).end() #define sz(v) (int)(v).size() #define pb push_back #define sec second #define fst first #define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<int, pi> ppi; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vl> mat; typedef complex<double> comp; void Debug() { cerr << '\n'; } template <class FIRST, class... REST> void Debug(FIRST arg, REST... rest) { cerr << arg << " "; Debug(rest...); } template <class T> ostream &operator<<(ostream &out, const vector<T> &v) { out << "["; if (!v.empty()) { rep(i, 0, sz(v) - 1) out << v[i] << ", "; out << v.back(); } out << "]"; return out; } template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &v) { out << "(" << v.first << ", " << v.second << ")"; return out; } const int MAX_N = 500010; const int MAX_V = 100010; const double eps = 1e-6; const ll mod = 1000000007; const int inf = (1 << 30) - 1; const ll linf = 1LL << 60; const double PI = 3.14159265358979323846; mt19937 rng; // use it by rng() % mod, shuffle(all(vec), rng) /////////////////////////////////////////////////////////////////////////////////////////////////// int N; vector<int> G[210]; int d[110]; void solve() { cin >> N; rep(i, 0, N) { rep(j, 0, N) { char a; cin >> a; if (a == '1') G[i].pb(j); } } int res = 0; rep(q, 0, N) { fill(d, d + N, inf); d[q] = 0; queue<int> que; que.push(q); while (!que.empty()) { int v = que.front(); que.pop(); rep(i, 0, sz(G[v])) { int n = G[v][i]; if (d[n] == inf) { d[n] = d[v] + 1; que.push(n); } else { if (d[n] % 2 == d[v] % 2) { cout << -1 << "\n"; return; } } } } rep(i, 0, N) { MAX(res, d[i] + 1); } } cout << res << "\n"; } uint32_t rd() { uint32_t res; #ifdef __MINGW32__ asm volatile("rdrand %0" : "=a"(res)::"cc"); #else res = std::random_device()(); #endif return res; } int main() { #ifndef LOCAL ios::sync_with_stdio(false); cin.tie(0); #endif cout << fixed; cout.precision(20); cerr << fixed; cerr.precision(6); rng.seed(rd()); #ifdef LOCAL // freopen("in.txt", "wt", stdout); //for tester if (!freopen("in.txt", "rt", stdin)) return 1; #endif solve(); cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0; }
#include <bits/stdc++.h> #define ADD(a, b) a = (a + ll(b)) % mod #define MUL(a, b) a = (a * ll(b)) % mod #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define rep(i, a, b) for (int i = int(a); i < int(b); i++) #define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--) #define all(a) (a).begin(), (a).end() #define sz(v) (int)(v).size() #define pb push_back #define sec second #define fst first #define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<int, pi> ppi; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vl> mat; typedef complex<double> comp; void Debug() { cerr << '\n'; } template <class FIRST, class... REST> void Debug(FIRST arg, REST... rest) { cerr << arg << " "; Debug(rest...); } template <class T> ostream &operator<<(ostream &out, const vector<T> &v) { out << "["; if (!v.empty()) { rep(i, 0, sz(v) - 1) out << v[i] << ", "; out << v.back(); } out << "]"; return out; } template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &v) { out << "(" << v.first << ", " << v.second << ")"; return out; } const int MAX_N = 500010; const int MAX_V = 100010; const double eps = 1e-6; const ll mod = 1000000007; const int inf = (1 << 30) - 1; const ll linf = 1LL << 60; const double PI = 3.14159265358979323846; mt19937 rng; // use it by rng() % mod, shuffle(all(vec), rng) /////////////////////////////////////////////////////////////////////////////////////////////////// int N; vector<int> G[210]; int d[210]; void solve() { cin >> N; rep(i, 0, N) { rep(j, 0, N) { char a; cin >> a; if (a == '1') G[i].pb(j); } } int res = 0; rep(q, 0, N) { fill(d, d + N, inf); d[q] = 0; queue<int> que; que.push(q); while (!que.empty()) { int v = que.front(); que.pop(); rep(i, 0, sz(G[v])) { int n = G[v][i]; if (d[n] == inf) { d[n] = d[v] + 1; que.push(n); } else { if (d[n] % 2 == d[v] % 2) { cout << -1 << "\n"; return; } } } } rep(i, 0, N) { MAX(res, d[i] + 1); } } cout << res << "\n"; } uint32_t rd() { uint32_t res; #ifdef __MINGW32__ asm volatile("rdrand %0" : "=a"(res)::"cc"); #else res = std::random_device()(); #endif return res; } int main() { #ifndef LOCAL ios::sync_with_stdio(false); cin.tie(0); #endif cout << fixed; cout.precision(20); cerr << fixed; cerr.precision(6); rng.seed(rd()); #ifdef LOCAL // freopen("in.txt", "wt", stdout); //for tester if (!freopen("in.txt", "rt", stdin)) return 1; #endif solve(); cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0; }
replace
54
55
54
55
0
Time: 0.031144 s.
p02892
C++
Runtime Error
#include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; const int N = 202; vector<int> g[N]; int col[N]; bool dfs(int v, int c, vector<int> &comp) { comp.push_back(v); col[v] = c; for (int u : g[v]) { if (col[u] == -1) { if (!dfs(u, c ^ 1, comp)) return false; } else { if (col[u] == col[v]) return false; } } return true; } bool used[N]; int d[N]; int bfs(int s, vector<int> &vert) { for (int v : vert) { used[v] = false; d[v] = N; } d[s] = 0; used[s] = true; queue<int> Q; Q.push(s); while (!Q.empty()) { int v = Q.front(); Q.pop(); for (int u : g[v]) { if (!used[u]) { d[u] = d[v] + 1; used[u] = true; Q.push(u); } } } int ans = 0; for (int v : vert) ans = max(ans, d[v] + 1); return ans; } char s[N]; int main() { freopen("input.txt", "r", stdin); int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", s); for (int j = 0; j < n; ++j) { if (s[j] == '1') g[i].push_back(j); } } for (int v = 0; v < n; ++v) col[v] = -1; int ans = 0; vector<int> comp; for (int v = 0; v < n; ++v) { if (col[v] == -1) { comp.clear(); if (!dfs(v, 0, comp)) { printf("-1"); return 0; } int cur = 0; for (int u : comp) cur = max(cur, bfs(u, comp)); ans += cur; } } printf("%d", ans); }
#include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; const int N = 202; vector<int> g[N]; int col[N]; bool dfs(int v, int c, vector<int> &comp) { comp.push_back(v); col[v] = c; for (int u : g[v]) { if (col[u] == -1) { if (!dfs(u, c ^ 1, comp)) return false; } else { if (col[u] == col[v]) return false; } } return true; } bool used[N]; int d[N]; int bfs(int s, vector<int> &vert) { for (int v : vert) { used[v] = false; d[v] = N; } d[s] = 0; used[s] = true; queue<int> Q; Q.push(s); while (!Q.empty()) { int v = Q.front(); Q.pop(); for (int u : g[v]) { if (!used[u]) { d[u] = d[v] + 1; used[u] = true; Q.push(u); } } } int ans = 0; for (int v : vert) ans = max(ans, d[v] + 1); return ans; } char s[N]; int main() { // freopen("input.txt", "r", stdin); int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", s); for (int j = 0; j < n; ++j) { if (s[j] == '1') g[i].push_back(j); } } for (int v = 0; v < n; ++v) col[v] = -1; int ans = 0; vector<int> comp; for (int v = 0; v < n; ++v) { if (col[v] == -1) { comp.clear(); if (!dfs(v, 0, comp)) { printf("-1"); return 0; } int cur = 0; for (int u : comp) cur = max(cur, bfs(u, comp)); ans += cur; } } printf("%d", ans); }
replace
60
61
60
61
0
p02892
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using lint = long long int; long long int INF = 1001001001001001LL; int inf = 1000000007; long long int MOD = 1000000007LL; double PI = 3.1415926535897932; template <typename T1, typename T2> inline void chmin(T1 &a, const T2 &b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, const T2 &b) { if (a < b) a = b; } #define ALL(a) a.begin(), a.end() #define RALL(a) a.rbegin(), a.rend() /* do your best */ int solve(int startNode, vector<vector<int>> &g) { // startNode から幅優先探索をして,どれだけ進めるかを返す. // ダメなら -1 をかえす int n = g.size(); bool ok = true; int ret = 0; queue<tuple<int, int, int>> que; // cur, par, cost vector<int> used(n, -1); que.push({startNode, -1, 1}); while (!que.empty()) { int cur, par, cost; tie(cur, par, cost) = que.front(); que.pop(); used[cur] = cost; ret = max(ret, cost); for (auto nxt : g[cur]) { if (used[nxt] != -1 and cost - 1 != used[nxt]) { ok = false; break; } if (!ok) break; if (used[nxt] == -1) que.push({nxt, cur, cost + 1}); } } if (!ok) return -1; else return ret; } int main() { // 幅優先探索をするが,途中で探索済みに遷移しようとしたら out // これをすべての頂点に対してやる int n; cin >> n; vector<vector<int>> g(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { char c; cin >> c; if (c == '1') { g[i].push_back(j); } } } int ans = -1; for (int i = 0; i < n; i++) { ans = max(ans, solve(i, g)); } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using lint = long long int; long long int INF = 1001001001001001LL; int inf = 1000000007; long long int MOD = 1000000007LL; double PI = 3.1415926535897932; template <typename T1, typename T2> inline void chmin(T1 &a, const T2 &b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, const T2 &b) { if (a < b) a = b; } #define ALL(a) a.begin(), a.end() #define RALL(a) a.rbegin(), a.rend() /* do your best */ int solve(int startNode, vector<vector<int>> &g) { // startNode から幅優先探索をして,どれだけ進めるかを返す. // ダメなら -1 をかえす int n = g.size(); bool ok = true; int ret = 0; queue<tuple<int, int, int>> que; // cur, par, cost vector<int> used(n, -1); que.push({startNode, -1, 1}); while (!que.empty()) { int cur, par, cost; tie(cur, par, cost) = que.front(); que.pop(); if (used[cur] != -1) { continue; } used[cur] = cost; ret = max(ret, cost); for (auto nxt : g[cur]) { if (used[nxt] != -1 and cost - 1 != used[nxt]) { ok = false; break; } if (!ok) break; if (used[nxt] == -1) que.push({nxt, cur, cost + 1}); } } if (!ok) return -1; else return ret; } int main() { // 幅優先探索をするが,途中で探索済みに遷移しようとしたら out // これをすべての頂点に対してやる int n; cin >> n; vector<vector<int>> g(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { char c; cin >> c; if (c == '1') { g[i].push_back(j); } } } int ans = -1; for (int i = 0; i < n; i++) { ans = max(ans, solve(i, g)); } cout << ans << endl; return 0; }
insert
60
60
60
63
TLE
p02892
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template <class T> inline bool chmax(T &x, T y) { if (x < y) { x = y; return true; } return false; } template <class T> inline bool chmin(T &x, T y) { if (x > y) { x = y; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> G(N, vector<int>(N)); for (int i = 0; i < N; ++i) { string S; cin >> S; for (int j = 0; j < N; ++j) { G[i][j] = S[j] - '0'; } } vector<int> color(N, -1); auto is_bipartite = [&](auto &&self, int u = 0, int c = 0) -> bool { color[u] = c; for (int v = 0; v < N; ++v) { if (!G[u][v]) continue; if (color[v] == -1 && !self(self, v, c ^ 1)) return false; else if (color[v] == c) return false; } return true; }; if (!is_bipartite(is_bipartite)) { cout << -1 << endl; return 0; } int ans = -1; for (int u = 0; u < N; ++u) { for (int v = 0; v < N; ++v) color[v] = -1; bool ok = true; int max_number = 1; queue<P> que; que.emplace(u, 1); while (!que.empty()) { int cur = que.front().first, c = que.front().second; que.pop(); color[cur] = c; chmax(max_number, c); for (int nxt = 0; nxt < N; ++nxt) { if (!G[cur][nxt]) continue; if (color[nxt] == -1) que.emplace(nxt, c + 1); else if (color[nxt] != c - 1 && color[nxt] != c + 1) { ok = false; break; } } if (!ok) break; } if (ok) chmax(ans, max_number); } cout << ans << endl; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template <class T> inline bool chmax(T &x, T y) { if (x < y) { x = y; return true; } return false; } template <class T> inline bool chmin(T &x, T y) { if (x > y) { x = y; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> G(N, vector<int>(N)); for (int i = 0; i < N; ++i) { string S; cin >> S; for (int j = 0; j < N; ++j) { G[i][j] = S[j] - '0'; } } vector<int> color(N, -1); auto is_bipartite = [&](auto &&self, int u = 0, int c = 0) -> bool { color[u] = c; for (int v = 0; v < N; ++v) { if (!G[u][v]) continue; if (color[v] == -1 && !self(self, v, c ^ 1)) return false; else if (color[v] == c) return false; } return true; }; if (!is_bipartite(is_bipartite)) { cout << -1 << endl; return 0; } int ans = -1; for (int u = 0; u < N; ++u) { for (int v = 0; v < N; ++v) color[v] = -1; bool ok = true; int max_number = 1; queue<P> que; que.emplace(u, 1); while (!que.empty()) { int cur = que.front().first, c = que.front().second; que.pop(); if (color[cur] != -1) continue; color[cur] = c; chmax(max_number, c); for (int nxt = 0; nxt < N; ++nxt) { if (!G[cur][nxt]) continue; if (color[nxt] == -1) que.emplace(nxt, c + 1); else if (color[nxt] != c - 1 && color[nxt] != c + 1) { ok = false; break; } } if (!ok) break; } if (ok) chmax(ans, max_number); } cout << ans << endl; }
insert
88
88
88
90
TLE
p02892
C++
Runtime Error
#include <cstdio> #include <iostream> #define N 9 using namespace std; int n, ans, d[N][N]; int main() { int i, j, k, t; cin >> n; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { scanf("%1d", &t); d[i][j] = (t == 1 || i == j ? t : 1e9); } for (k = 0; k < n; k++) for (i = 0; i < n; i++) for (j = 0; j < n; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } for (i = 0; i < n; i++) for (j = 0; j < n; j++) { ans = max(ans, d[i][j]); for (k = 0; k < n; k++) { if ((d[i][k] + d[k][j] + d[i][j]) % 2) return puts("-1") * 0; } } cout << ans + 1; return 0; }
#include <cstdio> #include <iostream> #define N 205 using namespace std; int n, ans, d[N][N]; int main() { int i, j, k, t; cin >> n; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { scanf("%1d", &t); d[i][j] = (t == 1 || i == j ? t : 1e9); } for (k = 0; k < n; k++) for (i = 0; i < n; i++) for (j = 0; j < n; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } for (i = 0; i < n; i++) for (j = 0; j < n; j++) { ans = max(ans, d[i][j]); for (k = 0; k < n; k++) { if ((d[i][k] + d[k][j] + d[i][j]) % 2) return puts("-1") * 0; } } cout << ans + 1; return 0; }
replace
2
3
2
3
0
p02892
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ri register int #define fi first #define se second #define pb push_back using namespace std; const int rlen = 1 << 18 | 1; char buf[rlen], *ib = buf, *ob = buf; #define gc() \ (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)), ib == ob) \ ? -1 \ : *ib++) inline int read() { int ans = 0; char ch = gc(); while (!isdigit(ch)) ch = gc(); while (isdigit(ch)) ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc(); return ans; } typedef pair<int, int> pii; typedef long long ll; typedef unsigned long long Ull; inline int Read(char *s) { int top = 0; char ch = gc(); while (!isdigit(ch)) ch = gc(); while (isdigit(ch)) s[++top] = ch, ch = gc(); return top; } char s[205]; int n, dep[205]; bool vs[205]; vector<int> e[205]; void check(int p) { vs[p] = 1; for (ri i = 0, v; i < e[p].size(); ++i) { if (vs[v = e[p][i]]) { if ((dep[v] + dep[p]) & 1) continue; puts("-1"); exit(0); } dep[v] = dep[p] + 1; check(v); } } int main() { #ifdef ldxcaicai freopen("lx.in", "r", stdin); #endif n = read(); for (ri i = 1; i <= n; ++i) { Read(s); for (ri j = 1; j <= n; ++j) if (s[j] == '1') e[i].pb(j); } check(1); int mx = 0; for (ri s = 1; s <= n; ++s) { memset(dep, 0, sizeof(dep)); memset(vs, 0, sizeof(vs)); queue<int> q; q.push(s), dep[s] = 1; while (q.size()) { int x = q.front(); q.pop(); vs[x] = 1; for (ri i = 0, v; i < e[x].size(); ++i) { if (vs[v = e[x][i]]) continue; dep[v] = dep[x] + 1; q.push(v); } } for (ri i = 1; i <= n; ++i) mx = max(mx, dep[i]); } cout << mx; return 0; }
#include <bits/stdc++.h> #define ri register int #define fi first #define se second #define pb push_back using namespace std; const int rlen = 1 << 18 | 1; char buf[rlen], *ib = buf, *ob = buf; #define gc() \ (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)), ib == ob) \ ? -1 \ : *ib++) inline int read() { int ans = 0; char ch = gc(); while (!isdigit(ch)) ch = gc(); while (isdigit(ch)) ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc(); return ans; } typedef pair<int, int> pii; typedef long long ll; typedef unsigned long long Ull; inline int Read(char *s) { int top = 0; char ch = gc(); while (!isdigit(ch)) ch = gc(); while (isdigit(ch)) s[++top] = ch, ch = gc(); return top; } char s[205]; int n, dep[205]; bool vs[205]; vector<int> e[205]; void check(int p) { vs[p] = 1; for (ri i = 0, v; i < e[p].size(); ++i) { if (vs[v = e[p][i]]) { if ((dep[v] + dep[p]) & 1) continue; puts("-1"); exit(0); } dep[v] = dep[p] + 1; check(v); } } int main() { #ifdef ldxcaicai freopen("lx.in", "r", stdin); #endif n = read(); for (ri i = 1; i <= n; ++i) { Read(s); for (ri j = 1; j <= n; ++j) if (s[j] == '1') e[i].pb(j); } check(1); int mx = 0; for (ri s = 1; s <= n; ++s) { memset(dep, 0, sizeof(dep)); memset(vs, 0, sizeof(vs)); queue<int> q; q.push(s), dep[s] = 1; while (q.size()) { int x = q.front(); q.pop(); vs[x] = 1; for (ri i = 0, v; i < e[x].size(); ++i) { if (vs[v = e[x][i]]) continue; dep[v] = dep[x] + 1; vs[v] = 1; q.push(v); } } for (ri i = 1; i <= n; ++i) mx = max(mx, dep[i]); } cout << mx; return 0; }
insert
76
76
76
77
TLE
p02892
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define REP(i, n) for (ll i = 0; i < n; i++) #define FOR(i, n1, n2) for (ll i = n1; i < n2; i++) #define bFOR(i, n1, n2) for (ll i = n1; i >= n2; i--) #define speed_up \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); typedef long long int ll; typedef pair<ll, ll> Pi; typedef tuple<ll, ll, ll> Tu; const int INF = (ll)(1LL << 30) - 1; const double INFd = 100000000000.0; const double PI = 3.14151926535; const ll INFl = (ll)9223372036854775807 / 2; const int MAX = 10000; const ll MOD = (ll)1e9 + 7; const ll tMOD = (ll)998244353; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } // int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0}; int mdx[8] = {0, 1, 0, -1, 1, 1, -1, -1}, mdy[8] = {-1, 0, 1, 0, 1, -1, 1, -1}; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } int n; int d[210][210]; int dd[210]; vector<int> v[210]; void warshall_floyd() { for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } bool tree_make() { queue<int> q; q.push(0); dd[0] = -1; while (q.size()) { int t = q.front(); q.pop(); for (int i = 0; i < v[t].size(); i++) { if (dd[v[t][i]] == 0) { dd[v[t][i]] = dd[t] * -1; q.push(dd[v[t][i]]); } else { if (dd[t] == dd[v[t][i]]) return false; } } } return true; } int main() { cin >> n; REP(i, n) { REP(j, n) { char s; cin >> s; if (i == j) continue; if (s == '1') { d[i][j] = 1; d[j][i] = 1; v[i].push_back(j); v[j].push_back(i); } else { d[i][j] = INF; d[j][i] = INF; } } } if (!tree_make()) { cout << -1 << endl; return 0; } warshall_floyd(); int ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (d[i][j] != INF) ans = max(ans, d[i][j] + 1); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define REP(i, n) for (ll i = 0; i < n; i++) #define FOR(i, n1, n2) for (ll i = n1; i < n2; i++) #define bFOR(i, n1, n2) for (ll i = n1; i >= n2; i--) #define speed_up \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); typedef long long int ll; typedef pair<ll, ll> Pi; typedef tuple<ll, ll, ll> Tu; const int INF = (ll)(1LL << 30) - 1; const double INFd = 100000000000.0; const double PI = 3.14151926535; const ll INFl = (ll)9223372036854775807 / 2; const int MAX = 10000; const ll MOD = (ll)1e9 + 7; const ll tMOD = (ll)998244353; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } // int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0}; int mdx[8] = {0, 1, 0, -1, 1, 1, -1, -1}, mdy[8] = {-1, 0, 1, 0, 1, -1, 1, -1}; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } int n; int d[210][210]; int dd[210]; vector<int> v[210]; void warshall_floyd() { for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } bool tree_make() { queue<int> q; q.push(0); dd[0] = -1; while (q.size()) { int t = q.front(); q.pop(); for (int i = 0; i < v[t].size(); i++) { if (dd[v[t][i]] == 0) { dd[v[t][i]] = dd[t] * (-1); q.push(v[t][i]); } else { if (dd[t] == dd[v[t][i]]) return false; } } } return true; } int main() { cin >> n; REP(i, n) { REP(j, n) { char s; cin >> s; if (i == j) continue; if (s == '1') { d[i][j] = 1; d[j][i] = 1; v[i].push_back(j); v[j].push_back(i); } else { d[i][j] = INF; d[j][i] = INF; } } } if (!tree_make()) { cout << -1 << endl; return 0; } warshall_floyd(); int ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (d[i][j] != INF) ans = max(ans, d[i][j] + 1); } cout << ans << endl; return 0; }
replace
51
53
51
53
0
p02893
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int N, dp[200009], bit[200009], p2[200009], v[200009]; char initSir[200009]; const int mod = 998244353; int add(int x, int y) { int ans = x + y; if (ans >= mod) ans -= mod; return ans; } int subtract(int x, int y) { if (x >= y) return x - y; return x - y + mod; } int mul(int x, int y) { return 1LL * x * y % mod; } void adto(int &x, int y) { x += y; if (x >= mod) x -= mod; } int power(int a, int b) { int p = 1; for (int i = 0; (1 << i) <= b; i++) { if (b & (1 << i)) p = mul(p, a); a = mul(a, a); } return p; } int fac[2000009], inv[2000009]; void Prec(int lim) { fac[0] = inv[0] = 1; for (int i = 1; i <= lim; i++) fac[i] = mul(fac[i - 1], i); inv[lim] = power(fac[lim], mod - 2); for (int i = lim - 1; i >= 0; i--) inv[i] = mul(inv[i + 1], i + 1); } int comb(int N, int K) { if (K > N || N < 0 || K < 0) return 0; int ans = mul(fac[N], inv[N - K]); ans = mul(ans, inv[K]); return ans; } int gcd(int a, int b) { int r; while (b) r = a % b, a = b, b = r; return a; } bool isBig = 0; int getCount(int P) { int D = gcd(P, N); /// number of cycles if ((P / D) % 2 == 1) return 0; if (isBig) return p2[D]; int ans = 0; for (int i = N - 1; i >= N - D; i--) if (bit[i]) adto(ans, p2[D - (N - i)]); for (int i = N - 1; i >= N - D; i--) { v[i] = bit[i]; for (int step = 1, j = i; step < N / D; step++, j = (j + P) % N) v[(j + P) % N] = v[j] ^ (j + P >= N); } int i = N - 1; while (i >= 0 && v[i] == bit[i]) i--; if (i >= 0 && bit[i] == 1) adto(ans, 1); return ans; } void add1() { int i = 0; while (bit[i] == 1) bit[i] = 0, i++; bit[i] = 1; isBig = (i == N); } int main() { // freopen ("input", "r", stdin); // freopen ("output", "w", stdout); scanf("%d\n%s", &N, initSir); for (int i = 0; i < N; i++) bit[i] = initSir[N - 1 - i] - '0'; add1(); p2[0] = 1; for (int i = 1; i <= N; i++) p2[i] = add(p2[i - 1], p2[i - 1]); int ans = 0; for (int i = 0; i <= N; i++) if (bit[i]) adto(dp[2 * N], p2[i]); /// all dp is referring to numbers strictly smaller than X for (int i = 1; i < 2 * N; i++) if ((2 * N) % i == 0) { // printf ("%d: %d\n", i, getCount (i)); adto(dp[i], getCount(i)); for (int j = i + i; j <= 2 * N; j += i) dp[j] = subtract(dp[j], dp[i]); } for (int i = 1; i <= 2 * N; i++) if ((2 * N) % i == 0) adto(ans, mul(dp[i], i)); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int N, dp[400009], bit[200009], p2[200009], v[200009]; char initSir[200009]; const int mod = 998244353; int add(int x, int y) { int ans = x + y; if (ans >= mod) ans -= mod; return ans; } int subtract(int x, int y) { if (x >= y) return x - y; return x - y + mod; } int mul(int x, int y) { return 1LL * x * y % mod; } void adto(int &x, int y) { x += y; if (x >= mod) x -= mod; } int power(int a, int b) { int p = 1; for (int i = 0; (1 << i) <= b; i++) { if (b & (1 << i)) p = mul(p, a); a = mul(a, a); } return p; } int fac[2000009], inv[2000009]; void Prec(int lim) { fac[0] = inv[0] = 1; for (int i = 1; i <= lim; i++) fac[i] = mul(fac[i - 1], i); inv[lim] = power(fac[lim], mod - 2); for (int i = lim - 1; i >= 0; i--) inv[i] = mul(inv[i + 1], i + 1); } int comb(int N, int K) { if (K > N || N < 0 || K < 0) return 0; int ans = mul(fac[N], inv[N - K]); ans = mul(ans, inv[K]); return ans; } int gcd(int a, int b) { int r; while (b) r = a % b, a = b, b = r; return a; } bool isBig = 0; int getCount(int P) { int D = gcd(P, N); /// number of cycles if ((P / D) % 2 == 1) return 0; if (isBig) return p2[D]; int ans = 0; for (int i = N - 1; i >= N - D; i--) if (bit[i]) adto(ans, p2[D - (N - i)]); for (int i = N - 1; i >= N - D; i--) { v[i] = bit[i]; for (int step = 1, j = i; step < N / D; step++, j = (j + P) % N) v[(j + P) % N] = v[j] ^ (j + P >= N); } int i = N - 1; while (i >= 0 && v[i] == bit[i]) i--; if (i >= 0 && bit[i] == 1) adto(ans, 1); return ans; } void add1() { int i = 0; while (bit[i] == 1) bit[i] = 0, i++; bit[i] = 1; isBig = (i == N); } int main() { // freopen ("input", "r", stdin); // freopen ("output", "w", stdout); scanf("%d\n%s", &N, initSir); for (int i = 0; i < N; i++) bit[i] = initSir[N - 1 - i] - '0'; add1(); p2[0] = 1; for (int i = 1; i <= N; i++) p2[i] = add(p2[i - 1], p2[i - 1]); int ans = 0; for (int i = 0; i <= N; i++) if (bit[i]) adto(dp[2 * N], p2[i]); /// all dp is referring to numbers strictly smaller than X for (int i = 1; i < 2 * N; i++) if ((2 * N) % i == 0) { // printf ("%d: %d\n", i, getCount (i)); adto(dp[i], getCount(i)); for (int j = i + i; j <= 2 * N; j += i) dp[j] = subtract(dp[j], dp[i]); } for (int i = 1; i <= 2 * N; i++) if ((2 * N) % i == 0) adto(ans, mul(dp[i], i)); printf("%d\n", ans); return 0; }
replace
4
5
4
5
0
p02893
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdint> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; using ll = long long; #define fst first #define snd second /* clang-format off */ template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; }; template <class T> struct _vec<T, 0> { using type = T; }; template <class T, size_t D> using vec = typename _vec<T, D>::type; template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); } template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); } template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; } template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; } /* clang-format on */ template <std::uint_fast64_t Modulus> class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(const modint rhs) noexcept { return *this *= ~rhs; } constexpr modint power(u64 exp) const noexcept { modint v = 1, x = *this; while (exp) { if (exp & 1) { v *= x; } x *= x; exp >>= 1; } return v; } constexpr modint operator~() const noexcept { return power(Modulus - 2); } }; vector<int> divisors(int N) { vector<int> res; for (int i = 1; i * i <= N; i++) { if (N % i == 0) { res.push_back(i); if (N / i != i) res.push_back(N / i); } } sort(res.begin(), res.end()); return res; } using mint = modint<998244353>; struct edge_t { int to; int w; }; mint pw2[200000 + 10]; mint solve(int N, const vector<int> &X, int L) { vec<edge_t, 2> G(N); for (int i = 0; i < N; i++) { int j = (i + L) % N; int w = ((i + L) / N) % 2; G[i].push_back({j, w}); } vector<int> id(N, -1), color(N, -1); vec<int, 2> group; for (int i = 0; i < N; i++) { if (id[i] != -1) continue; int v = i, c = 0, k = group.size(); group.push_back({}); while (id[v] == -1) { id[v] = k; color[v] = c; group[k].push_back(v); assert(G[v].size() == 1); int nv = G[v][0].to; int w = G[v][0].w; v = nv; c ^= w; } assert(v == i); if (c) return 0; } mint res = 0; int rem = group.size(); vector<int> bit(N, -1); for (int i = N - 1; i >= 0; i--) { if (bit[i] != -1) { if (bit[i] > X[i]) { return res; } if (bit[i] < X[i]) { res += pw2[rem]; return res; } continue; } int k = id[i]; --rem; if (X[i] == 0) { for (int v : group[k]) bit[v] = (0 ^ color[i] ^ color[v]); } else { res += pw2[rem]; for (int v : group[k]) bit[v] = (1 ^ color[i] ^ color[v]); } } res += 1; return res; } int main() { #ifdef DEBUG ifstream ifs("in.txt"); cin.rdbuf(ifs.rdbuf()); #endif int N; string S; while (cin >> N >> S) { pw2[0] = 1; for (int i = 1; i <= N; i++) pw2[i] = pw2[i - 1] * 2; reverse(S.begin(), S.end()); vector<int> X(N); for (int i = 0; i < N; i++) X[i] = S[i] - '0'; auto divs = divisors(2 * N); vector<mint> ways(divs.size()); for (int i = 0; i < divs.size(); i++) ways[i] = solve(N, X, divs[i]); for (int i = 0; i < divs.size(); i++) { for (int j = 0; j < i; j++) { if (divs[i] % divs[j] == 0) ways[i] -= ways[j]; } } mint res = 0; for (int i = 0; i < divs.size(); i++) { res += ways[i] * divs[i]; } cout << res.value() << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdint> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; using ll = long long; #define fst first #define snd second /* clang-format off */ template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; }; template <class T> struct _vec<T, 0> { using type = T; }; template <class T, size_t D> using vec = typename _vec<T, D>::type; template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); } template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); } template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; } template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; } /* clang-format on */ template <std::uint_fast64_t Modulus> class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(const modint rhs) noexcept { return *this *= ~rhs; } constexpr modint power(u64 exp) const noexcept { modint v = 1, x = *this; while (exp) { if (exp & 1) { v *= x; } x *= x; exp >>= 1; } return v; } constexpr modint operator~() const noexcept { return power(Modulus - 2); } }; vector<int> divisors(int N) { vector<int> res; for (int i = 1; i * i <= N; i++) { if (N % i == 0) { res.push_back(i); if (N / i != i) res.push_back(N / i); } } sort(res.begin(), res.end()); return res; } using mint = modint<998244353>; struct edge_t { int to; int w; }; mint pw2[200000 + 10]; mint solve(int N, const vector<int> &X, int L) { if (N % L == 0) return 0; vec<edge_t, 2> G(N); for (int i = 0; i < N; i++) { int j = (i + L) % N; int w = ((i + L) / N) % 2; G[i].push_back({j, w}); } vector<int> id(N, -1), color(N, -1); vec<int, 2> group; for (int i = 0; i < N; i++) { if (id[i] != -1) continue; int v = i, c = 0, k = group.size(); group.push_back({}); while (id[v] == -1) { id[v] = k; color[v] = c; group[k].push_back(v); assert(G[v].size() == 1); int nv = G[v][0].to; int w = G[v][0].w; v = nv; c ^= w; } assert(v == i); if (c) return 0; } mint res = 0; int rem = group.size(); vector<int> bit(N, -1); for (int i = N - 1; i >= 0; i--) { if (bit[i] != -1) { if (bit[i] > X[i]) { return res; } if (bit[i] < X[i]) { res += pw2[rem]; return res; } continue; } int k = id[i]; --rem; if (X[i] == 0) { for (int v : group[k]) bit[v] = (0 ^ color[i] ^ color[v]); } else { res += pw2[rem]; for (int v : group[k]) bit[v] = (1 ^ color[i] ^ color[v]); } } res += 1; return res; } int main() { #ifdef DEBUG ifstream ifs("in.txt"); cin.rdbuf(ifs.rdbuf()); #endif int N; string S; while (cin >> N >> S) { pw2[0] = 1; for (int i = 1; i <= N; i++) pw2[i] = pw2[i - 1] * 2; reverse(S.begin(), S.end()); vector<int> X(N); for (int i = 0; i < N; i++) X[i] = S[i] - '0'; auto divs = divisors(2 * N); vector<mint> ways(divs.size()); for (int i = 0; i < divs.size(); i++) ways[i] = solve(N, X, divs[i]); for (int i = 0; i < divs.size(); i++) { for (int j = 0; j < i; j++) { if (divs[i] % divs[j] == 0) ways[i] -= ways[j]; } } mint res = 0; for (int i = 0; i < divs.size(); i++) { res += ways[i] * divs[i]; } cout << res.value() << endl; } return 0; }
insert
112
112
112
114
TLE
p02893
C++
Runtime Error
#include <bits/stdc++.h> #define ri register int #define pb push_back #define fi first #define se second using namespace std; const int rlen = 1 << 18 | 1; char buf[rlen], *ib = buf, *ob = buf; #define gc() \ (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)), ib == ob) \ ? -1 \ : *ib++) typedef long long ll; typedef pair<int, int> pii; inline int read() { int ans = 0; char ch = gc(); while (!isdigit(ch)) ch = gc(); while (isdigit(ch)) ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc(); return ans; } inline int Read(char *s) { int top = 0; char ch = gc(); while (!isdigit(ch)) ch = gc(); while (isdigit(ch)) s[++top] = ch, ch = gc(); return top; } const int mod = 998244353; inline int add(int a, int b) { return (a += b) < mod ? a : a - mod; } inline void Add(int &a, int b) { a = add(a, b); } inline int dec(int a, int b) { return (a -= b) < 0 ? a + mod : a; } inline void Dec(int &a, int b) { a = dec(a, b); } inline int mul(int a, int b) { return (ll)a * b % mod; } inline void Mul(int &a, int b) { a = (ll)a * b % mod; } inline int ksm(int a, int p) { int ret = 1; for (; p; p >>= 1, Mul(a, a)) (p & 1) && (Mul(ret, a), 1); return ret; } const int N = 2e5 + 5; char s[N], t[N]; int n; int pre[N], f[N]; inline int solve(int len) { int res = pre[len]; for (ri i = 1; i <= len; ++i) t[i] = s[i]; for (ri l = len + 1, r = len << 1; l <= n; l += len, r += len) { for (ri i = l; i <= r; ++i) { t[i] = t[i - len] ^ 1; if (s[i] > t[i]) return res + 1; if (s[i] < t[i]) return res; } } return res + 1; } int pri[N], tot = 0; bool vs[N]; inline void init() { for (ri i = 2; i <= n; ++i) { if (!vs[i]) pri[++tot] = i; for (ri j = 1, up = n / i; j <= tot && pri[j] <= up; ++j) { vs[i * pri[j]] = 1; if (i == i / pri[j] * pri[j]) break; } } } int a[N], b[N], top; vector<int> divv; void dfs(int ps, int mt) { divv.pb(mt); for (ri mlt = a[ps], i = ps; i <= top; ++i, mlt = a[i]) for (ri j = 1; j <= b[ps]; ++j, mlt *= a[i]) dfs(i + 1, mt * mlt); } inline int calc(int lm) { int x = lm; top = 0; for (ri i = 1, up = sqrt(lm); i <= tot && pri[i] <= up && lm != 1; ++i) { if (lm != lm / pri[i] * pri[i]) continue; ++top, a[top] = pri[i], b[top] = 0; while (lm == lm / pri[i] * pri[i]) lm /= pri[i], ++b[top]; } if (lm ^ 1) a[++top] = lm, b[top] = 1; divv.clear(); dfs(1, 1); int res = 0; for (ri i = divv.size() - 1; ~i; --i) if (divv[i] != 1) if ((n / (x / divv[i])) & 1) Add(res, f[x / divv[i]]); return res; } int main() { #ifdef ldxcaicai freopen("lx.in", "r", stdin); #endif n = read(), Read(s); init(); int res = 0; for (ri i = 1; i <= n; ++i) pre[i] = add(add(pre[i - 1], pre[i - 1]), s[i] - '0'); res = add(pre[n], 1); Mul(res, n * 2); for (ri tp, i = 1; i * 3 <= n; ++i) { if (n % i) continue; if (!((n / i) & 1)) continue; f[i] = dec(solve(i), calc(i)); Dec(res, mul(2 * (n - i), f[i])); } cout << res; return 0; }
#include <bits/stdc++.h> #define ri register int #define pb push_back #define fi first #define se second using namespace std; const int rlen = 1 << 18 | 1; char buf[rlen], *ib = buf, *ob = buf; #define gc() \ (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)), ib == ob) \ ? -1 \ : *ib++) typedef long long ll; typedef pair<int, int> pii; inline int read() { int ans = 0; char ch = gc(); while (!isdigit(ch)) ch = gc(); while (isdigit(ch)) ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc(); return ans; } inline int Read(char *s) { int top = 0; char ch = gc(); while (!isdigit(ch)) ch = gc(); while (isdigit(ch)) s[++top] = ch, ch = gc(); return top; } const int mod = 998244353; inline int add(int a, int b) { return (a += b) < mod ? a : a - mod; } inline void Add(int &a, int b) { a = add(a, b); } inline int dec(int a, int b) { return (a -= b) < 0 ? a + mod : a; } inline void Dec(int &a, int b) { a = dec(a, b); } inline int mul(int a, int b) { return (ll)a * b % mod; } inline void Mul(int &a, int b) { a = (ll)a * b % mod; } inline int ksm(int a, int p) { int ret = 1; for (; p; p >>= 1, Mul(a, a)) (p & 1) && (Mul(ret, a), 1); return ret; } const int N = 2e5 + 5; char s[N], t[N]; int n; int pre[N], f[N]; inline int solve(int len) { int res = pre[len]; for (ri i = 1; i <= len; ++i) t[i] = s[i]; for (ri l = len + 1, r = len << 1; l <= n; l += len, r += len) { for (ri i = l; i <= r; ++i) { t[i] = t[i - len] ^ 1; if (s[i] > t[i]) return res + 1; if (s[i] < t[i]) return res; } } return res + 1; } int pri[N], tot = 0; bool vs[N]; inline void init() { for (ri i = 2; i <= n; ++i) { if (!vs[i]) pri[++tot] = i; for (ri j = 1, up = n / i; j <= tot && pri[j] <= up; ++j) { vs[i * pri[j]] = 1; if (i == i / pri[j] * pri[j]) break; } } } int a[N], b[N], top; vector<int> divv; void dfs(int ps, int mt) { divv.pb(mt); for (ri mlt = a[ps], i = ps; i <= top; ++i, mlt = a[i]) for (ri j = 1; j <= b[i]; ++j, mlt *= a[i]) dfs(i + 1, mt * mlt); } inline int calc(int lm) { int x = lm; top = 0; for (ri i = 1, up = sqrt(lm); i <= tot && pri[i] <= up && lm != 1; ++i) { if (lm != lm / pri[i] * pri[i]) continue; ++top, a[top] = pri[i], b[top] = 0; while (lm == lm / pri[i] * pri[i]) lm /= pri[i], ++b[top]; } if (lm ^ 1) a[++top] = lm, b[top] = 1; divv.clear(); dfs(1, 1); int res = 0; for (ri i = divv.size() - 1; ~i; --i) if (divv[i] != 1) if ((n / (x / divv[i])) & 1) Add(res, f[x / divv[i]]); return res; } int main() { #ifdef ldxcaicai freopen("lx.in", "r", stdin); #endif n = read(), Read(s); init(); int res = 0; for (ri i = 1; i <= n; ++i) pre[i] = add(add(pre[i - 1], pre[i - 1]), s[i] - '0'); res = add(pre[n], 1); Mul(res, n * 2); for (ri tp, i = 1; i * 3 <= n; ++i) { if (n % i) continue; if (!((n / i) & 1)) continue; f[i] = dec(solve(i), calc(i)); Dec(res, mul(2 * (n - i), f[i])); } cout << res; return 0; }
replace
82
83
82
83
0
p02893
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef long double ld; #define pb emplace_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define ALL(x) x.begin(), x.end() #define SZ(x) (ll) x.size() #define f first #define s second #define MAXN 201000 vi bucket_sizes; int N; string S; ll ans; ll MOD = 998244353; ll mem[1010]; ll mod2[MAXN]; string rep, con, opp; int main() { cin >> N >> S; for (int i = 3; i <= N; i += 2) if (!(N % i)) { bucket_sizes.pb(N / i); } // for (auto i : bucket_sizes)cout<<i<<' ';cout<<'\n'; reverse(ALL(bucket_sizes)); ll m = 1; ans = 1; for (int i = N - 1; i >= 0; --i) { if (S[i] == '1') { ans += m; ans %= MOD; } mod2[N - 1 - i] = m; m = (m * 2) % MOD; } ans = ans * (2 * N) % MOD; for (auto bsiz : bucket_sizes) { ll offset = 0; for (auto j : bucket_sizes) if (bsiz % j == 0) { offset += mem[j]; offset %= MOD; } // cout<<bsiz<<' '<<offset<<'\n'; ll s = 0; rep = con = opp = ""; for (int j = 0; j < bsiz; ++j) { if (S[bsiz - 1 - j] == '1') { s += mod2[j]; s %= MOD; } rep += S[j]; opp += ('0' + '1' - S[j]); } // cout<<s<<'\n'; // Number of things that work from 0 to id-1 // Must construct if its equal int in = 0; while (SZ(con) < N) { if (in) for (auto i : opp) con += (i); else for (auto i : rep) con += (i); in = 1 - in; } if (con <= S) ++s; s = (s + MOD - offset) % MOD; // cout<<"Discount "<<bsiz<<' '<<s<<'\n'; ans = (ans + MOD - (s * (2 * N - 2 * bsiz)) % MOD) % MOD; mem[bsiz] = s; } cout << ans; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef long double ld; #define pb emplace_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define ALL(x) x.begin(), x.end() #define SZ(x) (ll) x.size() #define f first #define s second #define MAXN 201000 vi bucket_sizes; int N; string S; ll ans; ll MOD = 998244353; ll mem[MAXN]; ll mod2[MAXN]; string rep, con, opp; int main() { cin >> N >> S; for (int i = 3; i <= N; i += 2) if (!(N % i)) { bucket_sizes.pb(N / i); } // for (auto i : bucket_sizes)cout<<i<<' ';cout<<'\n'; reverse(ALL(bucket_sizes)); ll m = 1; ans = 1; for (int i = N - 1; i >= 0; --i) { if (S[i] == '1') { ans += m; ans %= MOD; } mod2[N - 1 - i] = m; m = (m * 2) % MOD; } ans = ans * (2 * N) % MOD; for (auto bsiz : bucket_sizes) { ll offset = 0; for (auto j : bucket_sizes) if (bsiz % j == 0) { offset += mem[j]; offset %= MOD; } // cout<<bsiz<<' '<<offset<<'\n'; ll s = 0; rep = con = opp = ""; for (int j = 0; j < bsiz; ++j) { if (S[bsiz - 1 - j] == '1') { s += mod2[j]; s %= MOD; } rep += S[j]; opp += ('0' + '1' - S[j]); } // cout<<s<<'\n'; // Number of things that work from 0 to id-1 // Must construct if its equal int in = 0; while (SZ(con) < N) { if (in) for (auto i : opp) con += (i); else for (auto i : rep) con += (i); in = 1 - in; } if (con <= S) ++s; s = (s + MOD - offset) % MOD; // cout<<"Discount "<<bsiz<<' '<<s<<'\n'; ans = (ans + MOD - (s * (2 * N - 2 * bsiz)) % MOD) % MOD; mem[bsiz] = s; } cout << ans; }
replace
23
24
23
24
0