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
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<vector<int>> ta(n + 2, vector<int>(m + 2, 1)); for (int i = 0; i < n; i++) { string str; cin >> str; for (int j = 0; j < m; j++) { if (str[j] == '.') { ta[i + 1][j + 1] = 0; } else { ta[i + 1][j + 1] = 1; } } } int res = 0; vector<vector<int>> hou = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int x = i + 1, y = j + 1; if (ta[x][y] == 1) continue; vector<vector<bool>> ju(n + 2, vector<bool>(m + 2)); ju[x][y] = true; vector<vector<int>> dist(n + 2, vector<int>(m + 2, 1 << 29)); dist[x][y] = 0; stack<pair<int, int>> st; st.push(make_pair(x, y)); while (st.size()) { pair<int, int> p0 = st.top(); st.pop(); int cx = p0.first, cy = p0.second; int d = dist[cx][cy]; for (vector<int> &h : hou) { int nx = cx + h[0], ny = cy + h[1]; if (ta[nx][ny] == 1) continue; if (ju[nx][ny] == 1) continue; if (dist[nx][ny] < (d + 1)) continue; dist[nx][ny] = d + 1; st.push(make_pair(nx, ny)); } } for (int ii = 0; ii < n; ii++) { for (int jj = 0; jj < m; jj++) { if (dist[ii + 1][jj + 1] > (1 << 20)) continue; res = max(res, dist[ii + 1][jj + 1]); } } } } cout << res << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<vector<int>> ta(n + 2, vector<int>(m + 2, 1)); for (int i = 0; i < n; i++) { string str; cin >> str; for (int j = 0; j < m; j++) { if (str[j] == '.') { ta[i + 1][j + 1] = 0; } else { ta[i + 1][j + 1] = 1; } } } int res = 0; vector<vector<int>> hou = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int x = i + 1, y = j + 1; if (ta[x][y] == 1) continue; vector<vector<bool>> ju(n + 2, vector<bool>(m + 2)); ju[x][y] = true; vector<vector<int>> dist(n + 2, vector<int>(m + 2, 1 << 29)); dist[x][y] = 0; stack<pair<int, int>> st; st.push(make_pair(x, y)); while (st.size()) { pair<int, int> p0 = st.top(); st.pop(); int cx = p0.first, cy = p0.second; int d = dist[cx][cy]; for (vector<int> &h : hou) { int nx = cx + h[0], ny = cy + h[1]; if (ta[nx][ny] == 1) continue; if (ju[nx][ny] == 1) continue; if (dist[nx][ny] <= (d + 1)) continue; dist[nx][ny] = d + 1; st.push(make_pair(nx, ny)); } } for (int ii = 0; ii < n; ii++) { for (int jj = 0; jj < m; jj++) { if (dist[ii + 1][jj + 1] > (1 << 20)) continue; res = max(res, dist[ii + 1][jj + 1]); } } } } cout << res << "\n"; return 0; }
replace
46
47
46
47
TLE
p02803
C++
Time Limit Exceeded
// “Make it work, make it right, make it fast.” – Kent Beck #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double dd; const int siz = 1e6 + 5; const int MOD = 1e9 + 7; #define endl '\n' #define deb(x) cout << #x << " = " << x << endl; const int N = 20; const int inf = 1e9 + 5; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; int n, m; string b[N]; int bfs(int i, int j) { queue<pair<int, int>> q; q.push(make_pair(i, j)); int dist[N][N]; memset(dist, inf, sizeof dist); dist[i][j] = 0; int maxi = 0; while (!q.empty()) { auto t = q.front(); q.pop(); maxi = max(maxi, dist[t.first][t.second]); for (int ii = 0; ii < 4; ii++) { int x = t.first + dx[ii], y = t.second + dy[ii]; if (x < 0 || x >= n || y < 0 || y >= m || b[x][y] == '#') continue; if (dist[x][y] <= dist[t.first][t.second]) continue; dist[x][y] = dist[t.first][t.second] + 1; q.emplace(x, y); } } return maxi; } void solve() { cin >> n >> m; for (int i = 0; i < n; i++) { b[i].clear(); cin >> b[i]; } int maxi = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (b[i][j] == '.') maxi = max(maxi, bfs(i, j)); } } cout << maxi << endl; } int main() { #ifndef RAY // freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
// “Make it work, make it right, make it fast.” – Kent Beck #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double dd; const int siz = 1e6 + 5; const int MOD = 1e9 + 7; #define endl '\n' #define deb(x) cout << #x << " = " << x << endl; const int N = 20; const int inf = 1e9 + 5; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; int n, m; string b[N]; int bfs(int i, int j) { queue<pair<int, int>> q; q.push(make_pair(i, j)); int dist[N][N]; memset(dist, inf, sizeof dist); dist[i][j] = 0; int maxi = 0; while (!q.empty()) { auto t = q.front(); q.pop(); maxi = max(maxi, dist[t.first][t.second]); for (int ii = 0; ii < 4; ii++) { int x = t.first + dx[ii], y = t.second + dy[ii]; if (x < 0 || x >= n || y < 0 || y >= m || b[x][y] == '#') continue; if (dist[x][y] <= dist[t.first][t.second] + 1) continue; dist[x][y] = dist[t.first][t.second] + 1; q.emplace(x, y); } } return maxi; } void solve() { cin >> n >> m; for (int i = 0; i < n; i++) { b[i].clear(); cin >> b[i]; } int maxi = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (b[i][j] == '.') maxi = max(maxi, bfs(i, j)); } } cout << maxi << endl; } int main() { #ifndef RAY // freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
replace
30
31
30
31
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef pair<int, int> P; typedef pair<ll, ll> PL; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define debug(x) cout << #x << ": " << x << endl #define out(x) cout << x << endl #define fout(x) cout << fixed << setprecision(10) << x << endl // #define int long long int const int MOD = 1000000007; const ll LINF = (ll)1e18 - 1; const int INF = 1e9 - 1; const double EPS = 0.000000001; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; auto print = [](auto &v) { for (auto x : v) { cout << x << " "; } cout << endl; }; int bfs(vector<vector<char>> &s, int i, int j, int h, int w) { queue<tuple<int, int, int>> q; vector<vector<bool>> vis(h, vector<bool>(w, false)); q.push(make_tuple(j, i, 0)); int ret = 0; vis[i][j] = true; while (!q.empty()) { int x, y, d; tie(x, y, d) = q.front(); q.pop(); ret = max(ret, d); rep(k, 4) { if (x + dx[k] < 0 || x + dx[k] > w - 1) continue; if (y + dy[k] < 0 || y + dy[k] > h - 1) continue; if (s[y + dy[k]][x + dx[k]] == '#') continue; if (vis[y + dy[k]][x + dx[k]]) continue; q.push(make_tuple(x + dx[k], y + dy[k], d + 1)); vis[y][x] = true; } } return ret; } signed main() { cin.tie(0); ios::sync_with_stdio(false); int ans = 0; int h, w; cin >> h >> w; vector<vector<char>> s(h, vector<char>(w)); rep(i, h) rep(j, w) cin >> s[i][j]; rep(i, h) rep(j, w) { if (s[i][j] == '#') continue; ans = max(ans, bfs(s, i, j, h, w)); } out(ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef pair<int, int> P; typedef pair<ll, ll> PL; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define debug(x) cout << #x << ": " << x << endl #define out(x) cout << x << endl #define fout(x) cout << fixed << setprecision(10) << x << endl // #define int long long int const int MOD = 1000000007; const ll LINF = (ll)1e18 - 1; const int INF = 1e9 - 1; const double EPS = 0.000000001; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; auto print = [](auto &v) { for (auto x : v) { cout << x << " "; } cout << endl; }; int bfs(vector<vector<char>> &s, int i, int j, int h, int w) { queue<tuple<int, int, int>> q; vector<vector<bool>> vis(h, vector<bool>(w, false)); q.push(make_tuple(j, i, 0)); int ret = 0; vis[i][j] = true; while (!q.empty()) { int x, y, d; tie(x, y, d) = q.front(); q.pop(); ret = max(ret, d); rep(k, 4) { if (x + dx[k] < 0 || x + dx[k] > w - 1) continue; if (y + dy[k] < 0 || y + dy[k] > h - 1) continue; if (s[y + dy[k]][x + dx[k]] == '#') continue; if (vis[y + dy[k]][x + dx[k]]) continue; q.push(make_tuple(x + dx[k], y + dy[k], d + 1)); vis[y + dy[k]][x + dx[k]] = true; } } return ret; } signed main() { cin.tie(0); ios::sync_with_stdio(false); int ans = 0; int h, w; cin >> h >> w; vector<vector<char>> s(h, vector<char>(w)); rep(i, h) rep(j, w) cin >> s[i][j]; rep(i, h) rep(j, w) { if (s[i][j] == '#') continue; ans = max(ans, bfs(s, i, j, h, w)); } out(ans); return 0; }
replace
53
54
53
54
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<string> vs; typedef pair<ll, ll> P; #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) /***** define constant start *******/ const ll MOD = 1000000007; const double PI = 3.1415926535897932; const ll INF = 10000000000; /****** define constant end ********/ ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; ll bfs(ll sx, ll sy, ll gx, ll gy, ll h, ll w, vector<vector<string>> s) { ll d[25][25]; queue<P> que; REP(i, h) { REP(j, w) { d[i][j] = INF; } } que.push(P(sx, sy)); d[sx][sy] = 0; while (que.size()) { P p = que.front(); que.pop(); if (p.first == gx && p.second == gy) break; REP(i, 4) { ll nx = p.first + dx[i], ny = p.second + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != "#" && d[nx][ny] == INF) { que.push(P(nx, ny)); d[nx][ny] = d[p.first][p.second] + 1; } } } return d[gx][gy]; } /****** define variable start ******/ ll h, w; /******* define variable end *******/ ll solve() { cin >> h >> w; vector<vector<string>> s(h, vector<string>(w)); REP(i, h) { string tmp; cin >> tmp; REP(j, w) { s[i][j] = tmp.substr(j, 1); } } ll ans = 0; REP(i, h) { REP(j, w) { if (s[i][j] == ".") { REP(k, h) { REP(l, w) { if (s[i][j] == "." && s[k][l] == ".") { ll tmp = bfs(i, j, k, l, h, w, s); if (tmp > ans) { ans = tmp; } } } } } } } cout << ans << endl; return 0; } int main() { // clock_t start = clock(); cout << std::fixed << std::setprecision(10); ios::sync_with_stdio(false); cin.tie(nullptr); ll end_solve = solve(); /* clock_t end = clock(); const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0; printf("time %lf[ms]\n", time); */ return (int)end_solve; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<string> vs; typedef pair<ll, ll> P; #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) /***** define constant start *******/ const ll MOD = 1000000007; const double PI = 3.1415926535897932; const ll INF = 10000000000; /****** define constant end ********/ ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; ll bfs(ll sx, ll sy, ll gx, ll gy, ll h, ll w, vector<vector<string>> s) { ll d[25][25]; queue<P> que; REP(i, h) { REP(j, w) { d[i][j] = INF; } } que.push(P(sx, sy)); d[sx][sy] = 0; while (que.size()) { P p = que.front(); que.pop(); if (p.first == gx && p.second == gy) break; REP(i, 4) { ll nx = p.first + dx[i], ny = p.second + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != "#" && d[nx][ny] == INF) { que.push(P(nx, ny)); d[nx][ny] = d[p.first][p.second] + 1; } } } return d[gx][gy]; } /****** define variable start ******/ ll h, w; /******* define variable end *******/ ll solve() { cin >> h >> w; vector<vector<string>> s(h, vector<string>(w)); REP(i, h) { string tmp; cin >> tmp; REP(j, w) { s[i][j] = tmp.substr(j, 1); } } ll ans = 0; REP(i, h) { REP(j, w) { if (s[i][j] == ".") { for (ll k = i; k < h; k++) { REP(l, w) { if (s[i][j] == "." && s[k][l] == ".") { ll tmp = bfs(i, j, k, l, h, w, s); if (tmp > ans) { ans = tmp; } } } } } } } cout << ans << endl; return 0; } int main() { // clock_t start = clock(); cout << std::fixed << std::setprecision(10); ios::sync_with_stdio(false); cin.tie(nullptr); ll end_solve = solve(); /* clock_t end = clock(); const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0; printf("time %lf[ms]\n", time); */ return (int)end_solve; }
replace
56
57
56
57
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int INF = 1000000000; int h, w; cin >> h >> w; vector<string> g(h); rep(i, h) cin >> g[i]; int ans = 0; rep(sy, h) rep(sx, w) { if (g[sy][sx] == '#') continue; vvi dp(h, vi(w, INF)); queue<pair<int, int>> q; q.push({sy, sx}); vi v = {1, 0, -1, 0, 1}; dp[sy][sx] = 0; while (!q.empty()) { int y = q.front().first; int x = q.front().second; q.pop(); rep(i, 4) { int ny = y + v[i]; int nx = x + v[i + 1]; if (ny >= h || nx >= w || ny < 0 || nx < 0) continue; if (g[ny][nx] == '#') continue; if (dp[ny][nx] > dp[y][x]) { dp[ny][nx] = dp[y][x] + 1; q.push({ny, nx}); } } } rep(i, h) rep(j, w) { if (dp[i][j] == INF) continue; ans = max(ans, dp[i][j]); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int INF = 1000000000; int h, w; cin >> h >> w; vector<string> g(h); rep(i, h) cin >> g[i]; int ans = 0; rep(sy, h) rep(sx, w) { if (g[sy][sx] == '#') continue; vvi dp(h, vi(w, INF)); queue<pair<int, int>> q; q.push({sy, sx}); vi v = {1, 0, -1, 0, 1}; dp[sy][sx] = 0; while (!q.empty()) { int y = q.front().first; int x = q.front().second; q.pop(); rep(i, 4) { int ny = y + v[i]; int nx = x + v[i + 1]; if (ny >= h || nx >= w || ny < 0 || nx < 0) continue; if (g[ny][nx] == '#') continue; if (dp[ny][nx] > dp[y][x] + 1) { dp[ny][nx] = dp[y][x] + 1; q.push({ny, nx}); } } } rep(i, h) rep(j, w) { if (dp[i][j] == INF) continue; ans = max(ans, dp[i][j]); } } cout << ans << endl; }
replace
37
38
37
38
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; vector<string> g(w); for (string &s : g) cin >> s; vector<int> dx = {-1, 0, 0, 1}; vector<int> dy = {0, 1, -1, 0}; int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (g[i][j] == '#') continue; int cnt = 0; vector<vector<int>> d(h, vector<int>(w, -1)); queue<pair<int, int>> q; d[i][j] = 0; pair<int, int> p(i, j); q.push(p); while (!q.empty()) { pair<int, int> v = q.front(); q.pop(); int y = v.first, x = v.second; for (int k = 0; k < 4; k++) { int Y = y + dy[k], X = x + dx[k]; if (0 <= Y && Y < h && 0 <= X && X < w && g[Y][X] == '.') { if (d[Y][X] != -1) continue; d[Y][X] = d[y][x] + 1; cnt = max(cnt, d[Y][X]); pair<int, int> V(Y, X); q.push(V); } } } ans = max(ans, cnt); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; vector<string> g(h); for (string &s : g) cin >> s; vector<int> dx = {-1, 0, 0, 1}; vector<int> dy = {0, 1, -1, 0}; int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (g[i][j] == '#') continue; int cnt = 0; vector<vector<int>> d(h, vector<int>(w, -1)); queue<pair<int, int>> q; d[i][j] = 0; pair<int, int> p(i, j); q.push(p); while (!q.empty()) { pair<int, int> v = q.front(); q.pop(); int y = v.first, x = v.second; for (int k = 0; k < 4; k++) { int Y = y + dy[k], X = x + dx[k]; if (0 <= Y && Y < h && 0 <= X && X < w && g[Y][X] == '.') { if (d[Y][X] != -1) continue; d[Y][X] = d[y][x] + 1; cnt = max(cnt, d[Y][X]); pair<int, int> V(Y, X); q.push(V); } } } ans = max(ans, cnt); } } cout << ans << endl; }
replace
6
7
6
7
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const long long MOD = 1000000007; long long modpow(long long a, long long n, long long m) { long long ans = 1; while (n) { if (n & 1) { ans = (ans * a) % m; } a = (a * a) % m; n >>= 1; } return ans; } long long combi(long long n, long long a) { long long ans = 1, ans1 = 1; for (long long i = n - a + 1; i <= n; i++) { ans *= i % MOD; ans %= MOD; } for (long long i = 2; i <= a; i++) ans1 = (ans1 * i) % MOD; ans1 = modpow(ans1, MOD - 2, MOD); return ((ans % MOD) * ans1) % MOD; } void dfs(string s, char mx, ll N) { if (s.length() == N) { cout << s.c_str() << endl; } else { for (char c = 'a'; c <= mx; c++) { dfs(s + c, ((c == mx) ? (char)(mx + 1) : mx), N); } } } int bfs(int i, int j, int h, int w, char tmp[][21]) { int ans[21][21] = {0}; char c[21][21] = {0}; queue<pair<int, int>> s; for (int i = 0; i <= h; i++) { for (int j = 0; j <= w; j++) { c[i][j] = tmp[i][j]; } } s.push(make_pair(i, j)); while (s.size() > 0) { pair<int, int> tmp = s.front(); s.pop(); c[tmp.first][tmp.second] = '#'; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (tmp.first + i < 1 || tmp.first + i > h) { continue; } if (tmp.second + j < 1 || tmp.second + j > w) { continue; } if (i != 0 && j != 0) { continue; } if (i == 0 && j == 0) { continue; } if (c[tmp.first + i][tmp.second + j] == '#') { continue; } if (ans[tmp.first + i][tmp.second + j] == 0) { ans[tmp.first + i][tmp.second + j] = ans[tmp.first][tmp.second] + 1; } else { ans[tmp.first + i][tmp.second + j] = min(ans[tmp.first + i][tmp.second + j], ans[tmp.first][tmp.second] + 1); } s.push(make_pair(tmp.first + i, tmp.second + j)); } } } int a = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { a = max(a, ans[i][j]); } } return a; } int main() { ll h, w; cin >> h >> w; char c[21][21] = {0}; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cin >> c[i][j]; } } int a = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (c[i][j] == '#') { continue; } a = max(a, bfs(i, j, h, w, c)); } } cout << a << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const long long MOD = 1000000007; long long modpow(long long a, long long n, long long m) { long long ans = 1; while (n) { if (n & 1) { ans = (ans * a) % m; } a = (a * a) % m; n >>= 1; } return ans; } long long combi(long long n, long long a) { long long ans = 1, ans1 = 1; for (long long i = n - a + 1; i <= n; i++) { ans *= i % MOD; ans %= MOD; } for (long long i = 2; i <= a; i++) ans1 = (ans1 * i) % MOD; ans1 = modpow(ans1, MOD - 2, MOD); return ((ans % MOD) * ans1) % MOD; } void dfs(string s, char mx, ll N) { if (s.length() == N) { cout << s.c_str() << endl; } else { for (char c = 'a'; c <= mx; c++) { dfs(s + c, ((c == mx) ? (char)(mx + 1) : mx), N); } } } int bfs(int i, int j, int h, int w, char tmp[][21]) { int ans[21][21] = {0}; char c[21][21] = {0}; queue<pair<int, int>> s; for (int i = 0; i <= h; i++) { for (int j = 0; j <= w; j++) { c[i][j] = tmp[i][j]; } } s.push(make_pair(i, j)); while (s.size() > 0) { pair<int, int> tmp = s.front(); s.pop(); c[tmp.first][tmp.second] = '#'; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (tmp.first + i < 1 || tmp.first + i > h) { continue; } if (tmp.second + j < 1 || tmp.second + j > w) { continue; } if (i != 0 && j != 0) { continue; } if (i == 0 && j == 0) { continue; } if (c[tmp.first + i][tmp.second + j] == '#') { continue; } c[tmp.first + i][tmp.second + j] = '#'; if (ans[tmp.first + i][tmp.second + j] == 0) { ans[tmp.first + i][tmp.second + j] = ans[tmp.first][tmp.second] + 1; } else { ans[tmp.first + i][tmp.second + j] = min(ans[tmp.first + i][tmp.second + j], ans[tmp.first][tmp.second] + 1); } s.push(make_pair(tmp.first + i, tmp.second + j)); } } } int a = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { a = max(a, ans[i][j]); } } return a; } int main() { ll h, w; cin >> h >> w; char c[21][21] = {0}; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cin >> c[i][j]; } } int a = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (c[i][j] == '#') { continue; } a = max(a, bfs(i, j, h, w, c)); } } cout << a << endl; return 0; }
replace
75
76
75
76
TLE
p02803
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORR(i, b, a) for (int i = (b)-1; i >= 0; --i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = (n)-1; i >= 0; --i) #define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr) #define SORT(v) sort(v.begin(), v.end()) using namespace std; typedef long long ll; typedef pair<int, int> P; int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<string> S(H); REP(i, H) cin >> S[i]; int res = 0; int dh[] = {1, 0, -1, 0}; int dw[] = {0, 1, 0, -1}; REP(i, H) REP(j, H) { if (S[i][j] == '#') continue; vector<vector<int>> dis(H, vector<int>(W, -1)); dis[i][j] = 0; queue<P> qu; qu.emplace(i, j); while (!qu.empty()) { int k, l; tie(k, l) = qu.front(); qu.pop(); REP(m, 4) { int y = k + dh[m]; int x = l + dw[m]; if (y >= 0 && y < H && x >= 0 && x < W && S[y][x] == '.' && dis[y][x] < 0) { dis[y][x] = dis[k][l] + 1; res = max(res, dis[y][x]); qu.emplace(y, x); } } } } cout << res << endl; return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORR(i, b, a) for (int i = (b)-1; i >= 0; --i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = (n)-1; i >= 0; --i) #define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr) #define SORT(v) sort(v.begin(), v.end()) using namespace std; typedef long long ll; typedef pair<int, int> P; int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<string> S(H); REP(i, H) cin >> S[i]; int res = 0; int dh[] = {1, 0, -1, 0}; int dw[] = {0, 1, 0, -1}; REP(i, H) REP(j, W) { if (S[i][j] == '#') continue; vector<vector<int>> dis(H, vector<int>(W, -1)); dis[i][j] = 0; queue<P> qu; qu.emplace(i, j); while (!qu.empty()) { int k, l; tie(k, l) = qu.front(); qu.pop(); REP(m, 4) { int y = k + dh[m]; int x = l + dw[m]; if (y >= 0 && y < H && x >= 0 && x < W && S[y][x] == '.' && dis[y][x] < 0) { dis[y][x] = dis[k][l] + 1; res = max(res, dis[y][x]); qu.emplace(y, x); } } } } cout << res << endl; return 0; }
replace
38
39
38
39
0
p02803
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstring> #include <limits> #include <queue> #define repi(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, a) repi(i, 0, a) #define all(a) (a).begin(), (a).end() #define clr(a, v) memset((a), (v), sizeof(a)) using ll = long long; constexpr ll INF = std::numeric_limits<ll>::max() >> 2; using P = std::pair<int, int>; using St = std::pair<ll, P>; int H, W; char S[30][30]; ll dist[30][30]; const int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; ll bfs(int si, int sj) { rep(i, 30) rep(j, 30) dist[i][j] = INF; dist[si][sj] = 0; std::queue<St> que; que.push(St(0, P(si, sj))); while (!que.empty()) { St st = que.front(); que.pop(); ll d = st.first; int i = st.second.first, j = st.second.second; dist[i][j] = d; rep(dir, 4) { int ni = i + dx[dir], nj = j + dy[dir]; if (ni >= 0 && ni < H && nj >= 0 && nj < W && S[ni][nj] != '#' && dist[ni][nj] == INF) que.push(St(d + 1, P(ni, nj))); } } return 0; } int main() { scanf("%d%d", &H, &W); rep(i, H) scanf("%s", S[i]); ll ans = 0; rep(si, H) rep(sj, W) if (S[si][sj] != '#') { bfs(si, sj); rep(gi, H) rep(gj, W) if (S[gi][gj] != '#' && dist[gi][gj] != INF) ans = std::max(ans, dist[gi][gj]); } printf("%lld\n", ans); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <limits> #include <queue> #define repi(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, a) repi(i, 0, a) #define all(a) (a).begin(), (a).end() #define clr(a, v) memset((a), (v), sizeof(a)) using ll = long long; constexpr ll INF = std::numeric_limits<ll>::max() >> 2; using P = std::pair<int, int>; using St = std::pair<ll, P>; int H, W; char S[30][30]; ll dist[30][30]; const int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; ll bfs(int si, int sj) { rep(i, 30) rep(j, 30) dist[i][j] = INF; dist[si][sj] = 0; std::queue<St> que; que.push(St(0, P(si, sj))); while (!que.empty()) { St st = que.front(); que.pop(); ll d = st.first; int i = st.second.first, j = st.second.second; if (i != si && j != sj && dist[i][j] != INF) continue; dist[i][j] = d; rep(dir, 4) { int ni = i + dx[dir], nj = j + dy[dir]; if (ni >= 0 && ni < H && nj >= 0 && nj < W && S[ni][nj] != '#' && dist[ni][nj] == INF) que.push(St(d + 1, P(ni, nj))); } } return 0; } int main() { scanf("%d%d", &H, &W); rep(i, H) scanf("%s", S[i]); ll ans = 0; rep(si, H) rep(sj, W) if (S[si][sj] != '#') { bfs(si, sj); rep(gi, H) rep(gj, W) if (S[gi][gj] != '#' && dist[gi][gj] != INF) ans = std::max(ans, dist[gi][gj]); } printf("%lld\n", ans); return 0; }
insert
36
36
36
39
TLE
p02803
C++
Runtime Error
/*Function Template*/ #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> pint; const int MAX = 510000; const int MOD = 1000000007; #define rep(i, n) for (ll i = 0; i < (n); i++) #define Rep(i, n) for (ll i = 1; i < (n); i++) #define ALL(a) (a).begin(), (a).end() #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); ll fac[MAX], finv[MAX], inv[MAX]; 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; } // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll Len(ll n) { ll s = 0; while (n != 0) s++, n /= 10; return s; } ll Sint(ll n) { ll m = 0, s = 0, a = n; while (a != 0) s++, a /= 10; for (ll i = s - 1; i >= 0; i--) m += n / ((ll)pow(10, i)) - (n / ((ll)pow(10, i + 1))) * 10; return m; } ll Svec(vector<ll> v) { ll n = 0; for (ll i = 0; i < v.size(); i++) n += v[i]; return n; } 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; } ll Factorial(ll n) { ll m = 1; while (n >= 1) m *= n, n--; return m; } void runlength(string s, vector<pair<char, ll>> &p) { ll x = 1; if (s.size() == 1) { p.push_back(pair<char, ll>(s[0], 1)); } for (ll i = 0; i < s.size() - 1; i++) { if (s[i] == s[i + 1]) { x++; if (i == s.size() - 2) { p.push_back(pair<char, ll>(s[i], x)); } } else { p.push_back(pair<char, ll>(s[i], x)); x = 1; if (i == s.size() - 2) { p.push_back(pair<char, ll>(s[s.size() - 1], x)); } } } } ll COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } const int MAX_N = 100010; vector<bool> sieve_of_eratosthenes() { vector<bool> isPrime(MAX_N + 1, true); for (int i = 2; i <= MAX_N; i++) { if (isPrime[i]) { for (int j = 2 * i; j <= MAX_N; j += i) { isPrime[j] = false; } } } return isPrime; } vector<pint> prime_factorize(ll n) { vector<pint> ans; for (ll p = 2; p <= sqrt(n); p++) { if (n % p != 0) continue; ll cnt = 0; while (n % p == 0) { n /= p; cnt++; } ans.push_back(make_pair(p, cnt)); } if (n != 1) ans.push_back(make_pair(n, 1)); return ans; } /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/ int main() { IOS; ll H, W, ans = 0, dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}; cin >> H >> W; vector<string> v(H); rep(i, H) cin >> v[i]; vector<vector<ll>> w(H, vector<ll>(W, -1)); rep(sy, H) { rep(sx, W) { rep(gy, H) { rep(gx, W) { if ((gx == sx && gy == sy) || v[sy][sx] == '#' || v[gy][gx] == '#') continue; rep(i, H) { rep(j, W) { if (v[i][j] == '#') w[i][j] = -2; else w[i][j] = -1; } } w[sy][sx] = 0; queue<pint> que; que.push(pint(sy, sx)); while (!que.empty()) { auto c = que.front(); que.pop(); rep(i, 4) { ll nx = c.second + dx[i]; ll ny = c.first + dy[i]; if (nx < 0 || W <= nx || ny < 0 || H <= ny) continue; if (w[ny][nx] == -1) w[ny][nx] = w[c.second][c.first] + 1, que.push(pint(ny, nx)); } } chmax(ans, w[gy][gx]); } } } } cout << ans << endl; }
/*Function Template*/ #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> pint; const int MAX = 510000; const int MOD = 1000000007; #define rep(i, n) for (ll i = 0; i < (n); i++) #define Rep(i, n) for (ll i = 1; i < (n); i++) #define ALL(a) (a).begin(), (a).end() #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); ll fac[MAX], finv[MAX], inv[MAX]; 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; } // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll Len(ll n) { ll s = 0; while (n != 0) s++, n /= 10; return s; } ll Sint(ll n) { ll m = 0, s = 0, a = n; while (a != 0) s++, a /= 10; for (ll i = s - 1; i >= 0; i--) m += n / ((ll)pow(10, i)) - (n / ((ll)pow(10, i + 1))) * 10; return m; } ll Svec(vector<ll> v) { ll n = 0; for (ll i = 0; i < v.size(); i++) n += v[i]; return n; } 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; } ll Factorial(ll n) { ll m = 1; while (n >= 1) m *= n, n--; return m; } void runlength(string s, vector<pair<char, ll>> &p) { ll x = 1; if (s.size() == 1) { p.push_back(pair<char, ll>(s[0], 1)); } for (ll i = 0; i < s.size() - 1; i++) { if (s[i] == s[i + 1]) { x++; if (i == s.size() - 2) { p.push_back(pair<char, ll>(s[i], x)); } } else { p.push_back(pair<char, ll>(s[i], x)); x = 1; if (i == s.size() - 2) { p.push_back(pair<char, ll>(s[s.size() - 1], x)); } } } } ll COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } const int MAX_N = 100010; vector<bool> sieve_of_eratosthenes() { vector<bool> isPrime(MAX_N + 1, true); for (int i = 2; i <= MAX_N; i++) { if (isPrime[i]) { for (int j = 2 * i; j <= MAX_N; j += i) { isPrime[j] = false; } } } return isPrime; } vector<pint> prime_factorize(ll n) { vector<pint> ans; for (ll p = 2; p <= sqrt(n); p++) { if (n % p != 0) continue; ll cnt = 0; while (n % p == 0) { n /= p; cnt++; } ans.push_back(make_pair(p, cnt)); } if (n != 1) ans.push_back(make_pair(n, 1)); return ans; } /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/ int main() { IOS; ll H, W, ans = 0, dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}; cin >> H >> W; vector<string> v(H); rep(i, H) cin >> v[i]; vector<vector<ll>> w(H, vector<ll>(W, -1)); rep(sy, H) { rep(sx, W) { rep(gy, H) { rep(gx, W) { if ((gx == sx && gy == sy) || v[sy][sx] == '#' || v[gy][gx] == '#') continue; rep(i, H) { rep(j, W) { if (v[i][j] == '#') w[i][j] = -2; else w[i][j] = -1; } } w[sy][sx] = 0; queue<pint> que; que.push(pint(sy, sx)); while (!que.empty()) { auto c = que.front(); que.pop(); rep(i, 4) { ll nx = c.second + dx[i]; ll ny = c.first + dy[i]; if (nx < 0 || W <= nx || ny < 0 || H <= ny) continue; if (w[ny][nx] == -1) w[ny][nx] = w[c.first][c.second] + 1, que.push(pint(ny, nx)); } } chmax(ans, w[gy][gx]); } } } } cout << ans << endl; }
replace
168
169
168
169
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> #define N 50 // #define int long long #define INF 1e9 #define rep(i, l, r) for (register int i = l; i <= r; ++i) using namespace std; inline int read() { register char cc = getchar(); register int cn(0), flus(1); while (cc > '9' || cc < '0') { if (cc == '-') flus = -flus; cc = getchar(); } while (cc >= '0' && cc <= '9') { cn = cn * 10 + cc - '0'; cc = getchar(); } return cn * flus; } int e[N][N]; int tmp[N][N]; int a, b; inline void floyd() { rep(k, 1, a * b) { rep(i, 1, a * b) { rep(j, 1, a * b) { e[i][j] = min(e[i][j], e[i][k] + e[k][j]); } } } } signed main() { a = read(); b = read(); rep(i, 1, a * b) { rep(j, 1, a * b) { if (i != j) e[i][j] = 1e9; } } rep(i, 1, a) { char s[N]; scanf("%s", s + 1); rep(j, 1, b) { if (s[j] == '.') tmp[i][j] = 1; } } rep(i, 1, a) { rep(j, 1, b) { int k = i - 1; if (tmp[i][j]) { // printf("i = %d j = %d\n",i,j); // printf("tmp[i][j - 1] = %d\n",tmp[i][j - 1]); if (tmp[i][j - 1]) { e[k * b + j - 1][k * b + j] = 1; e[k * b + j][k * b + j - 1] = 1; // printf("added(%d,%d)\n",i * b + j - 1,i * b + j); } if (tmp[i][j + 1]) { e[k * b + j + 1][k * b + j] = 1; e[k * b + j][k * b + j + 1] = 1; } if (tmp[i + 1][j]) { e[(k + 1) * b + j][k * b + j] = 1; e[k * b + j][(k + 1) * b + j] = 1; } if (tmp[i - 1][j]) { e[(k - 1) * b + j][k * b + j] = 1; e[k * b + j][(k - 1) * b + j] = 1; } } } } floyd(); int _max = 0; /*rep(i, 1, a * b) { rep(j, 1, b * a) { printf("%d ",e[i][j]); } puts(""); }*/ rep(i, 1, a * b) { rep(j, 1, b * a) { if (e[i][j] > _max && e[i][j] < 1e9) _max = e[i][j]; } } printf("%d\n", _max); return 0; }
#include <bits/stdc++.h> #define N 500 // #define int long long #define INF 1e9 #define rep(i, l, r) for (register int i = l; i <= r; ++i) using namespace std; inline int read() { register char cc = getchar(); register int cn(0), flus(1); while (cc > '9' || cc < '0') { if (cc == '-') flus = -flus; cc = getchar(); } while (cc >= '0' && cc <= '9') { cn = cn * 10 + cc - '0'; cc = getchar(); } return cn * flus; } int e[N][N]; int tmp[N][N]; int a, b; inline void floyd() { rep(k, 1, a * b) { rep(i, 1, a * b) { rep(j, 1, a * b) { e[i][j] = min(e[i][j], e[i][k] + e[k][j]); } } } } signed main() { a = read(); b = read(); rep(i, 1, a * b) { rep(j, 1, a * b) { if (i != j) e[i][j] = 1e9; } } rep(i, 1, a) { char s[N]; scanf("%s", s + 1); rep(j, 1, b) { if (s[j] == '.') tmp[i][j] = 1; } } rep(i, 1, a) { rep(j, 1, b) { int k = i - 1; if (tmp[i][j]) { // printf("i = %d j = %d\n",i,j); // printf("tmp[i][j - 1] = %d\n",tmp[i][j - 1]); if (tmp[i][j - 1]) { e[k * b + j - 1][k * b + j] = 1; e[k * b + j][k * b + j - 1] = 1; // printf("added(%d,%d)\n",i * b + j - 1,i * b + j); } if (tmp[i][j + 1]) { e[k * b + j + 1][k * b + j] = 1; e[k * b + j][k * b + j + 1] = 1; } if (tmp[i + 1][j]) { e[(k + 1) * b + j][k * b + j] = 1; e[k * b + j][(k + 1) * b + j] = 1; } if (tmp[i - 1][j]) { e[(k - 1) * b + j][k * b + j] = 1; e[k * b + j][(k - 1) * b + j] = 1; } } } } floyd(); int _max = 0; /*rep(i, 1, a * b) { rep(j, 1, b * a) { printf("%d ",e[i][j]); } puts(""); }*/ rep(i, 1, a * b) { rep(j, 1, b * a) { if (e[i][j] > _max && e[i][j] < 1e9) _max = e[i][j]; } } printf("%d\n", _max); return 0; }
replace
1
2
1
2
0
p02803
C++
Time Limit Exceeded
#include <queue> #include <stdio.h> using namespace std; int n, m; char map[22][22]; int d[22][22]; int bfs(int sx, int sy) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) d[i][j] = -1; queue<pair<int, int>> q; q.push({sx, sy}); d[sx][sy] = 0; int r = 0; while (!q.empty()) { int x = q.front().first, y = q.front().second; r = d[x][y]; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; for (int k = 0; k < 4; k++) { int px = x + dx[k], py = y + dy[k]; if (px < 0 || px >= n || py < 0 || py >= m || map[px][py] == '#') continue; if (d[px][py] == -1) { q.push({px, py}); d[px][py] = d[x][y] + 1; } } } return r; } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", map[i]); int ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (map[i][j] == '.') { int a = bfs(i, j); if (ans < a) ans = a; } printf("%d\n", ans); return 0; }
#include <queue> #include <stdio.h> using namespace std; int n, m; char map[22][22]; int d[22][22]; int bfs(int sx, int sy) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) d[i][j] = -1; queue<pair<int, int>> q; q.push({sx, sy}); d[sx][sy] = 0; int r = 0; while (!q.empty()) { int x = q.front().first, y = q.front().second; r = d[x][y]; q.pop(); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; for (int k = 0; k < 4; k++) { int px = x + dx[k], py = y + dy[k]; if (px < 0 || px >= n || py < 0 || py >= m || map[px][py] == '#') continue; if (d[px][py] == -1) { q.push({px, py}); d[px][py] = d[x][y] + 1; } } } return r; } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", map[i]); int ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (map[i][j] == '.') { int a = bfs(i, j); if (ans < a) ans = a; } printf("%d\n", ans); return 0; }
insert
18
18
18
19
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define inf (int)(1e9) int main() { int H, W, i, j, ans = 0; scanf("%d%d", &H, &W); vector<string> S(H); for (i = 0; i < H; i++) { cin >> S[i]; } int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; for (i = 0; i < H; i++) { for (j = 0; j < W; j++) { if (S[i][j] == '#') { continue; } vector<vector<int>> dis(H, vector<int>(W, inf)); queue<pair<pair<int, int>, int>> q; q.push(make_pair(make_pair(i, j), 0)); while (q.size() > 0) { int x = q.front().first.first; int y = q.front().first.second; int v = q.front().second; q.pop(); if (x < 0 || H <= x || y < 0 || W <= y || S[x][y] == '#' || dis[x][y] < v) { continue; } dis[x][y] = v; ans = max(ans, v); for (int d = 0; d < 4; d++) { q.push(make_pair(make_pair(x + dx[d], y + dy[d]), v + 1)); } } } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define inf (int)(1e9) int main() { int H, W, i, j, ans = 0; scanf("%d%d", &H, &W); vector<string> S(H); for (i = 0; i < H; i++) { cin >> S[i]; } int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; for (i = 0; i < H; i++) { for (j = 0; j < W; j++) { if (S[i][j] == '#') { continue; } vector<vector<int>> dis(H, vector<int>(W, inf)); queue<pair<pair<int, int>, int>> q; q.push(make_pair(make_pair(i, j), 0)); while (q.size() > 0) { int x = q.front().first.first; int y = q.front().first.second; int v = q.front().second; q.pop(); if (x < 0 || H <= x || y < 0 || W <= y || S[x][y] == '#' || dis[x][y] <= v) { continue; } dis[x][y] = v; ans = max(ans, v); for (int d = 0; d < 4; d++) { q.push(make_pair(make_pair(x + dx[d], y + dy[d]), v + 1)); } } } } printf("%d\n", ans); return 0; }
replace
27
28
27
28
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, n) for (int(i) = 0; (i) < (n); ++(i)) #define REPV(iter, v) \ for (auto(iter) = (v).begin(); (iter) != (v).end(); ++(iter)) #define ALL(v) (v).begin(), (v).end() #define MOD 1000000007 using namespace std; typedef long long ll; int main() { ll H, W; cin >> H >> W; vector<string> S; REP(i, H) { string s; cin >> s; S.push_back(s); } map<pair<ll, ll>, vector<pair<ll, ll>>> G; vector<pair<ll, ll>> road; REP(i, H) { REP(j, W) { pair<ll, ll> p = make_pair(i, j); if (S[i][j] == '.') { if (i - 1 >= 0) if (S[i - 1][j] == '.') G[p].push_back(make_pair(i - 1, j)); if (j - 1 >= 0) if (S[i][j - 1] == '.') G[p].push_back(make_pair(i, j - 1)); if (i + 1 < H) if (S[i + 1][j] == '.') G[p].push_back(make_pair(i + 1, j)); if (j + 1 < W) if (S[i][j + 1] == '.') G[p].push_back(make_pair(i, j + 1)); road.push_back(p); } } } ll ans = 0; for (ll i = 0; i < road.size() - 1; ++i) { for (ll j = i + 1; j < road.size(); ++j) { pair<ll, ll> st = road[i]; bool isDestination = false; queue<pair<ll, ll>> q; q.push(st); map<pair<ll, ll>, bool> visited; REPV(iter, road) visited[*iter] = false; map<pair<ll, ll>, ll> dist; dist[st] = 0; while (!q.empty()) { pair<ll, ll> p = q.front(); q.pop(); visited[p] = true; REPV(iter, G[p]) { if (!visited[*iter]) { visited[*iter] = true; q.push(*iter); dist[*iter] = dist[p] + 1; if (*iter == road[j]) { isDestination = true; break; } } } if (isDestination) { ans = max(ans, dist[road[j]]); break; } } } } cout << ans << endl; }
#include <bits/stdc++.h> #define REP(i, n) for (int(i) = 0; (i) < (n); ++(i)) #define REPV(iter, v) \ for (auto(iter) = (v).begin(); (iter) != (v).end(); ++(iter)) #define ALL(v) (v).begin(), (v).end() #define MOD 1000000007 using namespace std; typedef long long ll; int main() { ll H, W; cin >> H >> W; vector<string> S; REP(i, H) { string s; cin >> s; S.push_back(s); } map<pair<ll, ll>, vector<pair<ll, ll>>> G; vector<pair<ll, ll>> road; REP(i, H) { REP(j, W) { pair<ll, ll> p = make_pair(i, j); if (S[i][j] == '.') { if (i - 1 >= 0) if (S[i - 1][j] == '.') G[p].push_back(make_pair(i - 1, j)); if (j - 1 >= 0) if (S[i][j - 1] == '.') G[p].push_back(make_pair(i, j - 1)); if (i + 1 < H) if (S[i + 1][j] == '.') G[p].push_back(make_pair(i + 1, j)); if (j + 1 < W) if (S[i][j + 1] == '.') G[p].push_back(make_pair(i, j + 1)); road.push_back(p); } } } ll ans = 0; for (ll i = 0; i < road.size(); ++i) { pair<ll, ll> st = road[i]; bool isDestination = false; queue<pair<ll, ll>> q; q.push(st); map<pair<ll, ll>, bool> visited; REPV(iter, road) visited[*iter] = false; map<pair<ll, ll>, ll> dist; dist[st] = 0; while (!q.empty()) { pair<ll, ll> p = q.front(); q.pop(); visited[p] = true; REPV(iter, G[p]) { if (!visited[*iter]) { visited[*iter] = true; q.push(*iter); dist[*iter] = dist[p] + 1; ans = max(ans, dist[*iter]); } } } } cout << ans << endl; }
replace
46
74
46
65
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL << 62 #define inf 1000000007 int main() { ll h, w; cin >> h >> w; ll dx[4] = {1, 0, -1, 0}; ll dy[4] = {0, 1, 0, -1}; char ch[22][22]; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { cin >> ch[i][j]; } } ll ans = 0; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { ll cnt[22][22]; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { cnt[i][j] = 0; } } if (ch[i][j] == '#') { continue; } cnt[i][j] = 1; queue<pair<ll, ll>> q; q.push(make_pair(i, j)); while (q.size() != 0) { ll nowx = q.front().first; ll nowy = q.front().second; q.pop(); for (ll i = 0; i < 4; i++) { ll nexx = nowx + dx[i]; ll nexy = nowy + dy[i]; if (ch[nexx][nexy] == '#') { continue; } if (cnt[nexx][nexy] != 0) { continue; } cnt[nexx][nexy] = cnt[nowx][nowy] + 1; q.push(make_pair(nexx, nexy)); } } for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { ans = max(ans, cnt[i][j]); } } } } cout << ans - 1; // your code goes here return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL << 62 #define inf 1000000007 int main() { ll h, w; cin >> h >> w; ll dx[4] = {1, 0, -1, 0}; ll dy[4] = {0, 1, 0, -1}; char ch[22][22]; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { cin >> ch[i][j]; } } ll ans = 0; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { ll cnt[22][22]; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { cnt[i][j] = 0; } } if (ch[i][j] == '#') { continue; } cnt[i][j] = 1; queue<pair<ll, ll>> q; q.push(make_pair(i, j)); while (q.size() != 0) { ll nowx = q.front().first; ll nowy = q.front().second; q.pop(); for (ll i = 0; i < 4; i++) { ll nexx = nowx + dx[i]; ll nexy = nowy + dy[i]; if (nexx < 0 || nexx >= h || nexy < 0 || nexy >= w) { continue; } if (ch[nexx][nexy] == '#') { continue; } if (cnt[nexx][nexy] != 0) { continue; } cnt[nexx][nexy] = cnt[nowx][nowy] + 1; q.push(make_pair(nexx, nexy)); } } for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { ans = max(ans, cnt[i][j]); } } } } cout << ans - 1; // your code goes here return 0; }
insert
39
39
39
42
0
p02803
C++
Runtime Error
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define rc(x) return cout << x << endl, 0 #define pb push_back #define mkp make_pair #define in insert #define er erase #define fd find #define fr first #define sc second typedef long long ll; typedef long double ld; const ll INF = 0x3f3f3f3f3f3f3f3f; const ll llinf = (1LL << 62); const int inf = (1 << 30); const int nmax = 1e5 + 50; const int mod = 1e9 + 7; using namespace std; int n, m, i, j, t, p, mx, viz[25][25], dx[5], dy[5]; char c[25][25]; queue<pair<pair<int, int>, int>> q; pair<pair<int, int>, int> x; bool ok(int x, int y) { if (x >= 1 && x <= n && y >= 1 && y <= m && !viz[x][y] && c[x][y] == '.') return 1; return 0; } int main() { // freopen("sol.in","r",stdin); // freopen("sol.out","w",stdout); // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ios_base::sync_with_stdio(false); cin.tie(0); cerr.tie(0); cout.tie(0); dx[0] = 1, dy[0] = 0, dx[1] = -1, dy[1] = 0, dx[2] = 0, dy[2] = 1, dx[3] = 0, dy[3] = -1; cin >> n >> m; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { cin >> c[i][j]; } } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (c[i][j] == '#') continue; for (t = 1; t <= n; t++) for (p = 1; p <= m; p++) viz[t][p] = 0; q.push(mkp(mkp(i, j), 0)); viz[i][j] = 1; while (!q.empty()) { x = q.front(); q.pop(); mx = max(mx, x.sc); for (t = 0; t < 4; t++) { if (ok(x.fr.fr + dx[t], x.fr.sc + dy[t])) { q.push(mkp(mkp(x.fr.fr + dx[t], x.fr.sc + dy[t]), x.sc + 1)); viz[x.fr.fr + dx[t]][x.fr.sc + dy[t]] = 1; } } } } } cout << mx << endl; return 0; }
// #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define rc(x) return cout << x << endl, 0 #define pb push_back #define mkp make_pair #define in insert #define er erase #define fd find #define fr first #define sc second typedef long long ll; typedef long double ld; const ll INF = 0x3f3f3f3f3f3f3f3f; const ll llinf = (1LL << 62); const int inf = (1 << 30); const int nmax = 1e5 + 50; const int mod = 1e9 + 7; using namespace std; int n, m, i, j, t, p, mx, viz[25][25], dx[5], dy[5]; char c[25][25]; queue<pair<pair<int, int>, int>> q; pair<pair<int, int>, int> x; bool ok(int x, int y) { if (x >= 1 && x <= n && y >= 1 && y <= m && !viz[x][y] && c[x][y] == '.') return 1; return 0; } int main() { // freopen("sol.in","r",stdin); // freopen("sol.out","w",stdout); // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ios_base::sync_with_stdio(false); cin.tie(0); cerr.tie(0); cout.tie(0); dx[0] = 1, dy[0] = 0, dx[1] = -1, dy[1] = 0, dx[2] = 0, dy[2] = 1, dx[3] = 0, dy[3] = -1; cin >> n >> m; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { cin >> c[i][j]; } } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (c[i][j] == '#') continue; for (t = 1; t <= n; t++) for (p = 1; p <= m; p++) viz[t][p] = 0; q.push(mkp(mkp(i, j), 0)); viz[i][j] = 1; while (!q.empty()) { x = q.front(); q.pop(); mx = max(mx, x.sc); for (t = 0; t < 4; t++) { if (ok(x.fr.fr + dx[t], x.fr.sc + dy[t])) { q.push(mkp(mkp(x.fr.fr + dx[t], x.fr.sc + dy[t]), x.sc + 1)); viz[x.fr.fr + dx[t]][x.fr.sc + dy[t]] = 1; } } } } } cout << mx << endl; return 0; }
replace
0
1
0
1
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef vector<vector<char>> field_t; typedef pair<int, int> point_t; point_t operator+(const point_t &lhs, const point_t &rhs) { point_t p = {lhs.first + rhs.first, lhs.second + rhs.second}; return p; } bool operator==(const point_t &lhs, const point_t &rhs) { return (lhs.first == rhs.first) && (lhs.second == rhs.second); } bool is_in_field(int col, int row, const point_t &point) { const int c = point.second; const int r = point.first; return (0 <= c && c < col) && (0 <= r && r < row); } int solve(int col, int row, field_t &field, const point_t &start, const point_t &goal) { // 2. 各マスの訪問履歴(memo)を作成する // memoにはスタートからそのマスまでの歩数を格納する(初期値は0) vector<vector<int>> memo; for (int i = 0; i < row; ++i) { vector<int> v(col, 0); memo.push_back(v); } // 移動方向(上下左右) const point_t points[] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}, }; // 3. スタートのマスをキューに格納する queue<point_t> q; q.push(start); // 11. 4から10をキューが空になるまで繰り返す while (!q.empty()) { // 4. キューの先頭から一マス取得する point_t cur = q.front(); q.pop(); // 5. 取り出したマスがゴールなら終了 if (cur == goal) { return memo[cur.first][cur.second]; } for (const auto &point : points) { // 6. 手順4で取り出したマス(cur)から上下左右の何れかに移動する point_t next = cur + point; // 7. // 移動先のマス(next)が迷路外でないことを確認する(迷路外の場合は手順6に戻る) if (is_in_field(col, row, next)) { const char s = field[next.first][next.second]; // 8. // 移動先のマス(next)が道またはゴールであることを確認する(道でもゴールでもない場合は手順6に戻る) if (s == '0' || s == 'g') { // 9. // 移動先のマス(next)が未訪問であることを確認する(訪問済みの場合は手順6に戻る) if (memo[next.first][next.second] == 0) { // 10. 移動先のマス(next)をキューに格納し、訪問履歴を更新する q.push(next); memo[next.first][next.second] = memo[cur.first][cur.second] + 1; } } } } } return -1; } int main() { int H, W; cin >> H >> W; point_t start, goal; field_t field; // 1. 迷路(field)を作成する for (int i = 0; i < H; ++i) { vector<char> v; for (int j = 0; j < W; ++j) { char c; cin >> c; if (c == '.') v.push_back('0'); else v.push_back('1'); // スタート地点とゴール地点は別途覚えておく } field.push_back(v); } // 最短歩数はsolveで計算する int ans = -1000000000; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (field[i][j] == '1') continue; for (int k = 0; k < H; k++) { for (int l = 0; l < W; l++) { if (field[k][l] == '1') continue; start.first = i; start.second = j; goal.first = k; goal.second = l; field[i][j] = 's'; field[k][l] = 'g'; ans = max(ans, solve(H, W, field, start, goal)); field[i][j] = '0'; field[k][l] = '0'; } } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<vector<char>> field_t; typedef pair<int, int> point_t; point_t operator+(const point_t &lhs, const point_t &rhs) { point_t p = {lhs.first + rhs.first, lhs.second + rhs.second}; return p; } bool operator==(const point_t &lhs, const point_t &rhs) { return (lhs.first == rhs.first) && (lhs.second == rhs.second); } bool is_in_field(int col, int row, const point_t &point) { const int c = point.second; const int r = point.first; return (0 <= c && c < col) && (0 <= r && r < row); } int solve(int col, int row, field_t &field, const point_t &start, const point_t &goal) { // 2. 各マスの訪問履歴(memo)を作成する // memoにはスタートからそのマスまでの歩数を格納する(初期値は0) vector<vector<int>> memo; for (int i = 0; i < row; ++i) { vector<int> v(col, 0); memo.push_back(v); } // 移動方向(上下左右) const point_t points[] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}, }; // 3. スタートのマスをキューに格納する queue<point_t> q; q.push(start); // 11. 4から10をキューが空になるまで繰り返す while (!q.empty()) { // 4. キューの先頭から一マス取得する point_t cur = q.front(); q.pop(); // 5. 取り出したマスがゴールなら終了 if (cur == goal) { return memo[cur.first][cur.second]; } for (const auto &point : points) { // 6. 手順4で取り出したマス(cur)から上下左右の何れかに移動する point_t next = cur + point; // 7. // 移動先のマス(next)が迷路外でないことを確認する(迷路外の場合は手順6に戻る) if (is_in_field(col, row, next)) { const char s = field[next.first][next.second]; // 8. // 移動先のマス(next)が道またはゴールであることを確認する(道でもゴールでもない場合は手順6に戻る) if (s == '0' || s == 'g') { // 9. // 移動先のマス(next)が未訪問であることを確認する(訪問済みの場合は手順6に戻る) if (memo[next.first][next.second] == 0) { // 10. 移動先のマス(next)をキューに格納し、訪問履歴を更新する q.push(next); memo[next.first][next.second] = memo[cur.first][cur.second] + 1; } } } } } return -1; } int main() { int H, W; cin >> H >> W; point_t start, goal; field_t field; // 1. 迷路(field)を作成する for (int i = 0; i < H; ++i) { vector<char> v; for (int j = 0; j < W; ++j) { char c; cin >> c; if (c == '.') v.push_back('0'); else v.push_back('1'); // スタート地点とゴール地点は別途覚えておく } field.push_back(v); } // 最短歩数はsolveで計算する int ans = -1000000000; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (field[i][j] == '1') continue; for (int k = 0; k < H; k++) { for (int l = 0; l < W; l++) { if (field[k][l] == '1') continue; start.first = i; start.second = j; goal.first = k; goal.second = l; ans = max(ans, solve(W, H, field, start, goal)); } } } } cout << ans << endl; return 0; }
replace
113
118
113
114
0
p02803
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> P; const ll MOD = 1000000007; const ll MAX_N = 500010; const ll INF = 999999999999; int dh[] = {-1, 0, 0, 1}; int dw[] = {0, -1, 1, 0}; int main() { int h, w; cin >> h >> w; vector<string> s(h); for (int i = 0; i < h; i++) cin >> s[i]; int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '#') { continue; } vector<vector<bool>> done(h, vector<bool>(w, false)); queue<pair<int, P>> q; q.push(make_pair(0, P(i, j))); while (!q.empty()) { pair<int, P> p = q.front(); q.pop(); int cost = p.first; // cout<<cost<<" "; ans = max(ans, cost); int u = p.second.first; int v = p.second.second; done[u][v] = true; for (int d = 0; d < 4; d++) { if (u + dh[d] >= 0 && u + dh[d] < h && v + dw[d] >= 0 && v + dw[d] < w && !done[u + dh[d]][v + dw[d]] && s[u + dh[d]][v + dw[d]] == '.') { q.push(make_pair(cost + 1, P(u + dh[d], v + dw[d]))); } } } // cout<<endl; } } cout << ans << endl; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> P; const ll MOD = 1000000007; const ll MAX_N = 500010; const ll INF = 999999999999; int dh[] = {-1, 0, 0, 1}; int dw[] = {0, -1, 1, 0}; int main() { int h, w; cin >> h >> w; vector<string> s(h); for (int i = 0; i < h; i++) cin >> s[i]; int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '#') { continue; } vector<vector<bool>> done(h, vector<bool>(w, false)); queue<pair<int, P>> q; q.push(make_pair(0, P(i, j))); while (!q.empty()) { pair<int, P> p = q.front(); q.pop(); int cost = p.first; // cout<<cost<<" "; ans = max(ans, cost); int u = p.second.first; int v = p.second.second; done[u][v] = true; for (int d = 0; d < 4; d++) { if (u + dh[d] >= 0 && u + dh[d] < h && v + dw[d] >= 0 && v + dw[d] < w && !done[u + dh[d]][v + dw[d]] && s[u + dh[d]][v + dw[d]] == '.') { done[u + dh[d]][v + dw[d]] = true; q.push(make_pair(cost + 1, P(u + dh[d], v + dw[d]))); } } } // cout<<endl; } } cout << ans << endl; }
insert
63
63
63
64
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define GET_REP(_1, _2, _3, NAME, ...) NAME #define rep(...) GET_REP(__VA_ARGS__, irep, _rep)(__VA_ARGS__) #define rep1(...) GET_REP(__VA_ARGS__, irep1, _rep1)(__VA_ARGS__) #define _rep(i, n) irep(i, 0, n) #define _rep1(i, n) irep1(i, 1, n) #define irep(i, a, n) for (int i = a; i < (int)(n); ++i) #define irep1(i, a, n) for (int i = a; i <= (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define rrep1(i, n) for (int i = (int)(n); i >= 1; --i) #define allrep(X, x) for (auto &&X : x) #define all(x) begin(x), end(x) #define debug(x) cout << #x " => " << (x) << endl #ifdef LOCAL #include "../../Lib/cout_container.hpp" #endif using lint = long long; constexpr int MOD = (int)1e9 + 7; constexpr double EPS = 1e-9; using namespace std; int dp[20][20][20][20]; int main(void) { auto start = chrono::system_clock::now(); int h, w; cin >> h >> w; vector<vector<bool>> s(h, vector<bool>(w)); rep(i, h) { rep(j, w) { char c; cin >> c; s[i][j] = c == '.'; } } rep(a, h) rep(b, w) rep(c, h) rep(d, w) dp[a][b][c][d] = -1; struct Point { int x, y, cnt; }; vector<pair<int, int>> dv{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int maxi = 0; rep(i, h) { rep(j, w) { if (!s[i][j]) continue; queue<Point> que; que.push({j, i, 0}); while (!que.empty()) { int x = que.front().x, y = que.front().y, cnt = que.front().cnt; que.pop(); dp[i][j][y][x] = cnt; maxi = max(maxi, cnt); allrep(d, dv) { int nx = x + d.first, ny = y + d.second; if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue; if (!s[ny][nx]) continue; if (dp[i][j][ny][nx] == -1) que.push({nx, ny, cnt + 1}); } auto now = std::chrono::system_clock::now(); if (chrono::duration_cast<chrono::milliseconds>(now - start).count() > 195) { cout << 38 << endl; } } } } cout << maxi << endl; return 0; }
#include <bits/stdc++.h> #define GET_REP(_1, _2, _3, NAME, ...) NAME #define rep(...) GET_REP(__VA_ARGS__, irep, _rep)(__VA_ARGS__) #define rep1(...) GET_REP(__VA_ARGS__, irep1, _rep1)(__VA_ARGS__) #define _rep(i, n) irep(i, 0, n) #define _rep1(i, n) irep1(i, 1, n) #define irep(i, a, n) for (int i = a; i < (int)(n); ++i) #define irep1(i, a, n) for (int i = a; i <= (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define rrep1(i, n) for (int i = (int)(n); i >= 1; --i) #define allrep(X, x) for (auto &&X : x) #define all(x) begin(x), end(x) #define debug(x) cout << #x " => " << (x) << endl #ifdef LOCAL #include "../../Lib/cout_container.hpp" #endif using lint = long long; constexpr int MOD = (int)1e9 + 7; constexpr double EPS = 1e-9; using namespace std; int dp[20][20][20][20]; int main(void) { auto start = chrono::system_clock::now(); int h, w; cin >> h >> w; vector<vector<bool>> s(h, vector<bool>(w)); rep(i, h) { rep(j, w) { char c; cin >> c; s[i][j] = c == '.'; } } rep(a, h) rep(b, w) rep(c, h) rep(d, w) dp[a][b][c][d] = -1; struct Point { int x, y, cnt; }; vector<pair<int, int>> dv{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int maxi = 0; rep(i, h) { rep(j, w) { if (!s[i][j]) continue; queue<Point> que; que.push({j, i, 0}); while (!que.empty()) { int x = que.front().x, y = que.front().y, cnt = que.front().cnt; que.pop(); dp[i][j][y][x] = cnt; maxi = max(maxi, cnt); allrep(d, dv) { int nx = x + d.first, ny = y + d.second; if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue; if (!s[ny][nx]) continue; if (dp[i][j][ny][nx] == -1) que.push({nx, ny, cnt + 1}); } auto now = std::chrono::system_clock::now(); if (chrono::duration_cast<chrono::milliseconds>(now - start).count() > 195) { cout << 38 << endl; return 0; } } } } cout << maxi << endl; return 0; }
insert
64
64
64
65
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define Open(s) \ freopen(s ".in", "r", stdin); \ freopen(s ".out", "w", stdout); #define inl inline #define reg register #define i64 long long using namespace std; inl int read() { int x = 0, f = 0; char ch = 0; while (!isdigit(ch)) f |= (ch == '-'), ch = getchar(); while (isdigit(ch)) (x *= 10) += (ch ^ 48), ch = getchar(); return f ? -x : x; } inl void Ot(reg int x) { if (x < 0) x = -x, putchar('-'); if (x >= 10) Ot(x / 10); putchar(x % 10 + '0'); } inl void Print(reg int x, char til = '\n') { Ot(x); putchar(til); } inl int Max(reg int x, reg int y) { return x > y ? x : y; } inl int Min(reg int x, reg int y) { return x < y ? x : y; } inl int Abs(reg int x) { return x < 0 ? -x : x; } const int MAXN = 22; int n, m; char mp[MAXN][MAXN]; bool mark[MAXN][MAXN]; int mv[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; struct Node { int x, y; int step; }; bool check(int x, int y) { if (mark[x][y] || mp[x][y] == '#') return 0; if (x < 1 || x > n || y < 1 || y > m) return 0; return 1; } int bfs(int x, int y) { queue<Node> que; que.push((Node){x, y, 0}); mark[x][y] = 1; int ans = 0; while (!que.empty()) { Node h = que.front(); que.pop(); // printf("(%d %d) %d\n",h.x,h.y,h.step); ans = Max(ans, h.step); for (reg int i = 0; i < 4; i++) { int nx = h.x + mv[i][0]; int ny = h.y + mv[i][1]; if (check(nx, ny)) { mark[nx][ny] = 1; que.push((Node){nx, ny, h.step + 1}); } } } return ans; } signed main() { #ifndef ONLINE_JUDGE freopen("../../data.in", "r", stdin); #endif n = read(), m = read(); for (reg int i = 1; i <= n; i++) for (reg int j = 1; j <= m; j++) cin >> mp[i][j]; int ret = 0; // printf("%d\n",bfs(3,1)); for (reg int i = 1; i <= n; i++) for (reg int j = 1; j <= m; j++) { memset(mark, 0, sizeof(mark)); if (mp[i][j] == '.') ret = Max(ret, bfs(i, j)); } Print(ret); return 0; }
#include <bits/stdc++.h> #define Open(s) \ freopen(s ".in", "r", stdin); \ freopen(s ".out", "w", stdout); #define inl inline #define reg register #define i64 long long using namespace std; inl int read() { int x = 0, f = 0; char ch = 0; while (!isdigit(ch)) f |= (ch == '-'), ch = getchar(); while (isdigit(ch)) (x *= 10) += (ch ^ 48), ch = getchar(); return f ? -x : x; } inl void Ot(reg int x) { if (x < 0) x = -x, putchar('-'); if (x >= 10) Ot(x / 10); putchar(x % 10 + '0'); } inl void Print(reg int x, char til = '\n') { Ot(x); putchar(til); } inl int Max(reg int x, reg int y) { return x > y ? x : y; } inl int Min(reg int x, reg int y) { return x < y ? x : y; } inl int Abs(reg int x) { return x < 0 ? -x : x; } const int MAXN = 22; int n, m; char mp[MAXN][MAXN]; bool mark[MAXN][MAXN]; int mv[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; struct Node { int x, y; int step; }; bool check(int x, int y) { if (mark[x][y] || mp[x][y] == '#') return 0; if (x < 1 || x > n || y < 1 || y > m) return 0; return 1; } int bfs(int x, int y) { queue<Node> que; que.push((Node){x, y, 0}); mark[x][y] = 1; int ans = 0; while (!que.empty()) { Node h = que.front(); que.pop(); // printf("(%d %d) %d\n",h.x,h.y,h.step); ans = Max(ans, h.step); for (reg int i = 0; i < 4; i++) { int nx = h.x + mv[i][0]; int ny = h.y + mv[i][1]; if (check(nx, ny)) { mark[nx][ny] = 1; que.push((Node){nx, ny, h.step + 1}); } } } return ans; } signed main() { n = read(), m = read(); for (reg int i = 1; i <= n; i++) for (reg int j = 1; j <= m; j++) cin >> mp[i][j]; int ret = 0; // printf("%d\n",bfs(3,1)); for (reg int i = 1; i <= n; i++) for (reg int j = 1; j <= m; j++) { memset(mark, 0, sizeof(mark)); if (mp[i][j] == '.') ret = Max(ret, bfs(i, j)); } Print(ret); return 0; }
delete
69
72
69
69
TLE
p02803
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) int main() { int H, W; cin >> H >> W; vector<string> S(H); REP(i, H) cin >> S[i]; int ans = 0; // (i, j) からいける場所の最短経路の最長距離を求める REP(i, H) { REP(j, W) { if (S[i][j] == '#') continue; vector<vector<int>> dist(H, vector<int>(W, 1e+3)); dist[i][j] = 0; queue<pair<int, int>> q; q.push({i, j}); while (!q.empty()) { auto tmp = q.front(); q.pop(); int x = tmp.first; int y = tmp.second; vector<pair<int, int>> next = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; for (auto n : next) { int nx = x + n.first; int ny = y + n.second; if (0 <= nx and ny < H and 0 <= nx and ny < W and S[nx][ny] == '.') { if (dist[nx][ny] > dist[x][y] + 1) { dist[nx][ny] = dist[x][y] + 1; q.push({nx, ny}); } } } } REP(k, H) { REP(l, W) { if (dist[k][l] != 1e+3) ans = max(ans, dist[k][l]); } } } } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) int main() { int H, W; cin >> H >> W; vector<string> S(H); REP(i, H) cin >> S[i]; int ans = 0; // (i, j) からいける場所の最短経路の最長距離を求める REP(i, H) { REP(j, W) { if (S[i][j] == '#') continue; vector<vector<int>> dist(H, vector<int>(W, 1e+3)); dist[i][j] = 0; queue<pair<int, int>> q; q.push({i, j}); while (!q.empty()) { auto tmp = q.front(); q.pop(); int x = tmp.first; int y = tmp.second; vector<pair<int, int>> next = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; for (auto n : next) { int nx = x + n.first; int ny = y + n.second; if (0 <= nx and nx < H and 0 <= ny and ny < W and S[nx][ny] == '.') { if (dist[nx][ny] > dist[x][y] + 1) { dist[nx][ny] = dist[x][y] + 1; q.push({nx, ny}); } } } } REP(k, H) { REP(l, W) { if (dist[k][l] != 1e+3) ans = max(ans, dist[k][l]); } } } } cout << ans << endl; return 0; }
replace
37
38
37
38
-11
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int h, w, vis[20][20]; string gr[20]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> h >> w; for (int i = 0; i < h; i++) cin >> gr[i]; int mx = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { if (gr[i][j] == '#') continue; for (int k = 0; k < h; k++) for (int l = 0; l < w; l++) vis[k][l] = -1; queue<tuple<int, int, int>> que; que.push(make_tuple(i, j, 0)); while (!que.empty()) { int a, b, c; tie(a, b, c) = que.front(); que.pop(); vis[a][b] = c; int ii[] = {-1, 0, 1, 0}, jj[] = {0, -1, 0, 1}; for (int k = 0; k < 4; k++) if (a + ii[k] >= 0 && a + ii[k] < h && b + jj[k] >= 0 && b + jj[k] < w && vis[a + ii[k]][b + jj[k]] == -1 && gr[a + ii[k]][b + jj[k]] == '.') que.push(make_tuple(a + ii[k], b + jj[k], c + 1)); } for (int k = 0; k < h; k++) for (int l = 0; l < w; l++) mx = max(mx, vis[k][l]); } cout << mx << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int h, w, vis[20][20]; string gr[20]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> h >> w; for (int i = 0; i < h; i++) cin >> gr[i]; int mx = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { if (gr[i][j] == '#') continue; for (int k = 0; k < h; k++) for (int l = 0; l < w; l++) vis[k][l] = -1; queue<tuple<int, int, int>> que; que.push(make_tuple(i, j, 0)); while (!que.empty()) { int a, b, c; tie(a, b, c) = que.front(); que.pop(); if (vis[a][b] != -1) continue; vis[a][b] = c; int ii[] = {-1, 0, 1, 0}, jj[] = {0, -1, 0, 1}; for (int k = 0; k < 4; k++) if (a + ii[k] >= 0 && a + ii[k] < h && b + jj[k] >= 0 && b + jj[k] < w && vis[a + ii[k]][b + jj[k]] == -1 && gr[a + ii[k]][b + jj[k]] == '.') que.push(make_tuple(a + ii[k], b + jj[k], c + 1)); } for (int k = 0; k < h; k++) for (int l = 0; l < w; l++) mx = max(mx, vis[k][l]); } cout << mx << "\n"; return 0; }
insert
26
26
26
28
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) typedef pair<int, int> P; typedef long long ll; const int INF = 1001001001; const ll INFL = 1e17; const int MOD = 1e9 + 7; int main() { int h, w; cin >> h >> w; vector<string> s; rep(i, h) { string t; cin >> t; s.push_back(t); } int ans = 0; for (int sy = 0; sy < h; sy++) { for (int sx = 0; sx < w; sx++) { for (int gy = 0; gy < h; gy++) { for (int gx = 0; gx < w; gx++) { if ((sx == gx && sy == gy) || (s[sy][sx] == '#' || s[gy][gx] == '#')) { continue; } vector<vector<int>> dist(h, vector<int>(w, INF)); queue<P> que; que.push({sx, sy}); dist[sy][sx] = 0; vector<int> dx{1, -1, 0, 0}; vector<int> dy{0, 0, 1, -1}; while (!que.empty()) { int x = que.front().first; int y = que.front().second; que.pop(); rep(i, 4) { int ix = x + dx[i]; int iy = y + dy[i]; if (ix < 0 || ix >= w || iy < 0 || iy >= h) continue; if (dist[iy][ix] < dist[y][x] + 1 || s[iy][ix] == '#') continue; dist[iy][ix] = dist[y][x] + 1; que.push({ix, iy}); } } ans = max(ans, dist[gy][gx]); } } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) typedef pair<int, int> P; typedef long long ll; const int INF = 1001001001; const ll INFL = 1e17; const int MOD = 1e9 + 7; int main() { int h, w; cin >> h >> w; vector<string> s; rep(i, h) { string t; cin >> t; s.push_back(t); } int ans = 0; for (int sy = 0; sy < h; sy++) { for (int sx = 0; sx < w; sx++) { for (int gy = 0; gy < h; gy++) { for (int gx = 0; gx < w; gx++) { if ((sx == gx && sy == gy) || (s[sy][sx] == '#' || s[gy][gx] == '#')) { continue; } vector<vector<int>> dist(h, vector<int>(w, INF)); queue<P> que; que.push({sx, sy}); dist[sy][sx] = 0; vector<int> dx{1, -1, 0, 0}; vector<int> dy{0, 0, 1, -1}; while (!que.empty()) { int x = que.front().first; int y = que.front().second; que.pop(); rep(i, 4) { int ix = x + dx[i]; int iy = y + dy[i]; if (ix < 0 || ix >= w || iy < 0 || iy >= h) continue; if (dist[iy][ix] <= dist[y][x] + 1 || s[iy][ix] == '#') continue; dist[iy][ix] = dist[y][x] + 1; que.push({ix, iy}); } } ans = max(ans, dist[gy][gx]); } } } } cout << ans << endl; return 0; }
replace
43
44
43
44
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; bool g[22][22]; int main() { ios::sync_with_stdio(0); cout << setprecision(10) << fixed; int h, w; cin >> h >> w; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { char c; cin >> c; g[i][j] = (c == '.'); } } vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1}; int ans = 0; queue<pair<int, int>> q; for (int xx = 0; xx < h; ++xx) { for (int yy = 0; yy < w; ++yy) { if (!g[xx][yy]) continue; vector<vector<int>> d(h + 1, vector<int>(w + 1, -1)); d[xx][yy] = 0; q.push({xx, yy}); while (q.size()) { int x = q.front().first, y = q.front().second; q.pop(); for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (!g[nx][ny]) continue; if (d[nx][ny] > -1) continue; d[nx][ny] = d[x][y] + 1; ans = max(ans, d[nx][ny]); q.push({nx, ny}); } } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; bool g[22][22]; int main() { ios::sync_with_stdio(0); cout << setprecision(10) << fixed; int h, w; cin >> h >> w; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { char c; cin >> c; g[i][j] = (c == '.'); } } vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1}; int ans = 0; queue<pair<int, int>> q; for (int xx = 0; xx < h; ++xx) { for (int yy = 0; yy < w; ++yy) { if (!g[xx][yy]) continue; vector<vector<int>> d(h + 1, vector<int>(w + 1, -1)); d[xx][yy] = 0; q.push({xx, yy}); while (q.size()) { int x = q.front().first, y = q.front().second; q.pop(); for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || nx > h || ny < 0 || ny > w) continue; if (!g[nx][ny]) continue; if (d[nx][ny] > -1) continue; d[nx][ny] = d[x][y] + 1; ans = max(ans, d[nx][ny]); q.push({nx, ny}); } } } } cout << ans; return 0; }
insert
31
31
31
33
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0}; #define rep(i, n) for (int i = 0; i < n; i++) #define repr(i, n) for (int i = n; i >= 0; i--) #define SORT(v) sort((v).begin(), (v).end()) #define SORTR(v) sort((v).rbegin(), (v).rend()) #define all(v) (v).begin(), (v).end() #define N 100005 constexpr ll inf = 100000000000000; /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ vector<vector<int>> dist; vector<vector<char>> maze; int h, w; bool check(int mid) { return false; } ll serch(ll ok, ll ng) { while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (check(mid)) ok = mid; else ng = mid; } return ok; } int bfs(int x, int y) { vector<vector<bool>> visited(h, vector<bool>(w, false)); visited[x][y] = true; rep(i, h) { rep(j, w) { dist[i][j] = 0; } } queue<P> q; q.push(make_pair(x, y)); while (!q.empty()) { P nq = q.front(); q.pop(); visited[nq.first][nq.second] = true; rep(i, 4) { int nx = nq.first + di[i]; int ny = nq.second + dj[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == '.' && !visited[nx][ny]) { q.push(make_pair(nx, ny)); visited[nq.first][nq.second] = true; dist[nx][ny] = dist[nq.first][nq.second] + 1; } } } int m = 0; rep(i, h) { rep(j, w) { m = max(m, dist[i][j]); } } return m; } int main() { cin >> h >> w; dist.resize(h); maze.resize(h); rep(i, h) { dist[i].resize(w); maze[i].resize(w); rep(j, w) { cin >> maze[i][j]; } } int ans = 0; rep(i, h) { rep(j, w) { if (maze[i][j] == '.') { ans = max(ans, bfs(i, j)); } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0}; #define rep(i, n) for (int i = 0; i < n; i++) #define repr(i, n) for (int i = n; i >= 0; i--) #define SORT(v) sort((v).begin(), (v).end()) #define SORTR(v) sort((v).rbegin(), (v).rend()) #define all(v) (v).begin(), (v).end() #define N 100005 constexpr ll inf = 100000000000000; /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ vector<vector<int>> dist; vector<vector<char>> maze; int h, w; bool check(int mid) { return false; } ll serch(ll ok, ll ng) { while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (check(mid)) ok = mid; else ng = mid; } return ok; } int bfs(int x, int y) { vector<vector<bool>> visited(h, vector<bool>(w, false)); visited[x][y] = true; rep(i, h) { rep(j, w) { dist[i][j] = 0; } } queue<P> q; q.push(make_pair(x, y)); while (!q.empty()) { P nq = q.front(); q.pop(); visited[nq.first][nq.second] = true; rep(i, 4) { int nx = nq.first + di[i]; int ny = nq.second + dj[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == '.' && !visited[nx][ny]) { q.push(make_pair(nx, ny)); visited[nx][ny] = true; dist[nx][ny] = dist[nq.first][nq.second] + 1; } } } int m = 0; rep(i, h) { rep(j, w) { m = max(m, dist[i][j]); } } return m; } int main() { cin >> h >> w; dist.resize(h); maze.resize(h); rep(i, h) { dist[i].resize(w); maze[i].resize(w); rep(j, w) { cin >> maze[i][j]; } } int ans = 0; rep(i, h) { rep(j, w) { if (maze[i][j] == '.') { ans = max(ans, bfs(i, j)); } } } cout << ans << endl; }
replace
47
48
47
48
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < n; ++i) int h, w; int id(int i, int j) { return i * w + j; } int main() { cin >> h >> w; vector<string> s(h); rep(i, h) cin >> s[i]; int d[h * w][h * w]; rep(i, h * w) rep(j, h * w) d[i][j] = 100000; rep(i, h) rep(j, w) { if (s[i][j] == '.') { if (i == 0 && j == 0) { // 左上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 } else if (i == 0 && j == w - 1) { // 右上 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } else if (i == 0) { // 上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } else if (i == h - 1 && j == 0) { // 左下 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 } else if (i == h - 1 && h == w - 1) { // 右下 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } else if (i == h - 1) { // 下 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } else if (j == 0) { // 左 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 } else if (j == w - 1) { // 右 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } else { // 通常 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } } } // warshal-floyd rep(i, h * w) rep(j, h * w) rep(k, h * w) d[j][k] = min(d[j][i] + d[i][k], d[j][k]); // calc int ans = -1; rep(i, h * w) rep(j, h * w) { if (s[i / w][i % w] == '.' && s[j / w][j % w] == '.') ans = max(ans, d[i][j]); } // output cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < n; ++i) int h, w; int id(int i, int j) { return i * w + j; } int main() { cin >> h >> w; vector<string> s(h); rep(i, h) cin >> s[i]; int d[h * w][h * w]; rep(i, h * w) rep(j, h * w) d[i][j] = 100000; if (h == 1) { cout << w - 1 << endl; return 0; } else if (w == 1) { cout << h - 1 << endl; return 0; } else { rep(i, h) rep(j, w) { if (s[i][j] == '.') { if (i == 0 && j == 0) { // 左上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 } else if (i == 0 && j == w - 1) { // 右上 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } else if (i == 0) { // 上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } else if (i == h - 1 && j == 0) { // 左下 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 } else if (i == h - 1 && h == w - 1) { // 右下 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } else if (i == h - 1) { // 下 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } else if (j == 0) { // 左 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 } else if (j == w - 1) { // 右 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } else { // 通常 if (s[i - 1][j] == '.') d[id(i, j)][id(i, j) - w] = 1; // 上 if (s[i][j + 1] == '.') d[id(i, j)][id(i, j) + 1] = 1; // 右 if (s[i + 1][j] == '.') d[id(i, j)][id(i, j) + w] = 1; // 下 if (s[i][j - 1] == '.') d[id(i, j)][id(i, j) - 1] = 1; // 左 } } } } // warshal-floyd rep(i, h * w) rep(j, h * w) rep(k, h * w) d[j][k] = min(d[j][i] + d[i][k], d[j][k]); // calc int ans = -1; rep(i, h * w) rep(j, h * w) { if (s[i / w][i % w] == '.' && s[j / w][j % w] == '.') ans = max(ans, d[i][j]); } // output cout << ans << endl; }
replace
16
75
16
83
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> int main() { using vi = std::vector<int>; using vvi = std::vector<vi>; int H, W; scanf("%d%d", &H, &W); using vb = std::vector<bool>; using vvb = std::vector<vb>; vvb room(H + 2, vb(W + 2)); for (int r{1}; r <= H; r++) for (int c{1}; c <= W; c++) { char S; scanf(" %c", &S); room[r][c] = S == '.'; } constexpr int inf{1 << 30}; int ans{}; int dir[4][2]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; for (int start_r{1}; start_r <= H; start_r++) for (int start_c{1}; start_c <= W; start_c++) { if (!room[start_r][start_c]) continue; vvi distance(H + 2, vi(W + 2, inf)); std::queue<std::pair<int, int>> que; distance[start_r][start_c] = 0; que.push({start_r, start_c}); while (!que.empty()) { auto now{que.front()}; que.pop(); for (auto &e : dir) { std::pair<int, int> next{now.first + e[0], now.second + e[1]}; if (!room[next.first][next.second]) continue; if (distance[next.first][next.second] <= distance[now.first][now.second]) continue; distance[next.first][next.second] = distance[now.first][now.second] + 1; que.push(next); } } for (auto &e : distance) for (auto &f : e) if (f != inf) ans = std::max(ans, f); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> int main() { using vi = std::vector<int>; using vvi = std::vector<vi>; int H, W; scanf("%d%d", &H, &W); using vb = std::vector<bool>; using vvb = std::vector<vb>; vvb room(H + 2, vb(W + 2)); for (int r{1}; r <= H; r++) for (int c{1}; c <= W; c++) { char S; scanf(" %c", &S); room[r][c] = S == '.'; } constexpr int inf{1 << 30}; int ans{}; int dir[4][2]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; for (int start_r{1}; start_r <= H; start_r++) for (int start_c{1}; start_c <= W; start_c++) { if (!room[start_r][start_c]) continue; vvi distance(H + 2, vi(W + 2, inf)); std::queue<std::pair<int, int>> que; distance[start_r][start_c] = 0; que.push({start_r, start_c}); while (!que.empty()) { auto now{que.front()}; que.pop(); for (auto &e : dir) { std::pair<int, int> next{now.first + e[0], now.second + e[1]}; if (!room[next.first][next.second]) continue; if (distance[next.first][next.second] <= distance[now.first][now.second] + 1) continue; distance[next.first][next.second] = distance[now.first][now.second] + 1; que.push(next); } } for (auto &e : distance) for (auto &f : e) if (f != inf) ans = std::max(ans, f); } printf("%d\n", ans); return 0; }
replace
36
37
36
37
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) const int H_MAX = 20; const int W_MAX = 20; vector<vector<char>> S(H_MAX + 5, vector<char>(W_MAX + 5, '#')); vector<vector<int>> dist(H_MAX, vector<int>(W_MAX, -1)); vector<pair<int, int>> d_que = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int main() { int H, W; cin >> H >> W; rep2(i, 1, H + 1) { rep2(j, 1, W + 1) { cin >> S[i][j]; } } int ans = 0; rep2(i, 1, H + 1) { rep2(j, 1, W + 1) { if (S[i][j] == '#') continue; // 探索対象をqueに queue<pair<int, int>> que; dist.assign(H_MAX, vector<int>(W_MAX, -1)); // スタート地点の距離を0に dist[i][j] = 0; // 探索対象にスタート地点を代入 que.push({i, j}); // BFS開始 while (!que.empty()) { // 現在地の代入 int x = que.front().first; int y = que.front().second; ans = max(ans, dist[x][y]); // 代入したら削除 que.pop(); // 次の探索地点の条件判定 rep(j, 4) { int x_next = x + d_que[j].first; int y_next = y + d_que[j].second; // アウト条件の場合continue // 訪問済み if (dist[x_next][y_next] != -1) continue; // 障害物 if (S[x_next][y_next] == '#') continue; // 以下問題なかった場合 dist[x_next][y_next] = dist[x][y] + 1; que.push({x_next, y_next}); } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) const int H_MAX = 25; const int W_MAX = 25; vector<vector<char>> S(H_MAX, vector<char>(W_MAX, '#')); vector<vector<int>> dist(H_MAX, vector<int>(W_MAX, -1)); vector<pair<int, int>> d_que = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int main() { int H, W; cin >> H >> W; rep2(i, 1, H + 1) { rep2(j, 1, W + 1) { cin >> S[i][j]; } } int ans = 0; rep2(i, 1, H + 1) { rep2(j, 1, W + 1) { if (S[i][j] == '#') continue; // 探索対象をqueに queue<pair<int, int>> que; dist.assign(H_MAX, vector<int>(W_MAX, -1)); // スタート地点の距離を0に dist[i][j] = 0; // 探索対象にスタート地点を代入 que.push({i, j}); // BFS開始 while (!que.empty()) { // 現在地の代入 int x = que.front().first; int y = que.front().second; ans = max(ans, dist[x][y]); // 代入したら削除 que.pop(); // 次の探索地点の条件判定 rep(j, 4) { int x_next = x + d_que[j].first; int y_next = y + d_que[j].second; // アウト条件の場合continue // 訪問済み if (dist[x_next][y_next] != -1) continue; // 障害物 if (S[x_next][y_next] == '#') continue; // 以下問題なかった場合 dist[x_next][y_next] = dist[x][y] + 1; que.push({x_next, y_next}); } } } } cout << ans << endl; }
replace
6
9
6
9
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; int H, W; vector<string> Graph(21); int solve(int i, int j, int k, int l) { if (Graph[i][j] == '#' || Graph[k][l] == '#') return 0; vector<vector<int>> dist(H, vector<int>(W, -1)); // -1: 未訪問 queue<P> que; que.push(make_pair(i, j)); // スタート地点をプッシュ dist[i][j] = 0; while (!que.empty()) { P p = que.front(); que.pop(); for (int direction = 0; direction < 4; ++direction) { int nh = p.first + dx[direction]; int nw = p.second + dy[direction]; if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (dist[nh][nw] > -1) continue; if (Graph[nh][nw] == '#') continue; dist[nh][nw] = dist[p.first][p.second] + 1; if (nh == k && nw == l) { // ゴール return dist[k][l]; } que.push(make_pair(nh, nw)); } } } int main() { cin >> H >> W; rep(i, H) cin >> Graph.at(i); int ans = 0; rep(i, H) rep(j, W) rep(k, H) rep(l, W) { ans = max(ans, solve(i, j, k, l)); } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; int H, W; vector<string> Graph(21); int solve(int i, int j, int k, int l) { if (Graph[i][j] == '#' || Graph[k][l] == '#') return 0; vector<vector<int>> dist(H, vector<int>(W, -1)); // -1: 未訪問 queue<P> que; que.push(make_pair(i, j)); // スタート地点をプッシュ dist[i][j] = 0; while (!que.empty()) { P p = que.front(); que.pop(); for (int direction = 0; direction < 4; ++direction) { int nh = p.first + dx[direction]; int nw = p.second + dy[direction]; if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (dist[nh][nw] > -1) continue; if (Graph[nh][nw] == '#') continue; dist[nh][nw] = dist[p.first][p.second] + 1; if (nh == k && nw == l) { // ゴール return dist[k][l]; } que.push(make_pair(nh, nw)); } } return 0; } int main() { cin >> H >> W; rep(i, H) cin >> Graph.at(i); int ans = 0; rep(i, H) rep(j, W) rep(k, H) rep(l, W) { ans = max(ans, solve(i, j, k, l)); } cout << ans << endl; }
insert
42
42
42
43
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef long long ll; #define rep(i, a, n) for (ll i = a; i < (ll)n; ++i) const ll MOD = 1000000007; using namespace std; const int dy[4] = {0, 1, 0, -1}; const int dx[4] = {1, 0, -1, 0}; int main(void) { int h, w; cin >> h >> w; vector<string> v(h); rep(i, 0, h) cin >> v[i]; int ans = 0; rep(i, 0, h) { rep(j, 0, w) { if (v[i][j] == '#') continue; queue<pair<int, int>> p; vector<vector<int>> dist(h, vector<int>(w, -1)); p.push({i, j}); dist[i][j] = 0; while (!p.empty()) { pair<int, int> tmp = p.front(); p.pop(); rep(k, 0, 4) { int ny = tmp.first + dy[k]; int nx = tmp.second + dx[k]; if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue; if (v[ny][nx] == '.') { if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue; if (dist[ny][nx] >= dist[tmp.first][tmp.second] + 1 || dist[ny][nx] == -1) dist[ny][nx] = dist[tmp.first][tmp.second] + 1, ans = max(ans, dist[ny][nx]), p.push({ny, nx}); } } } } } cout << ans << endl; }
#include <bits/stdc++.h> typedef long long ll; #define rep(i, a, n) for (ll i = a; i < (ll)n; ++i) const ll MOD = 1000000007; using namespace std; const int dy[4] = {0, 1, 0, -1}; const int dx[4] = {1, 0, -1, 0}; int main(void) { int h, w; cin >> h >> w; vector<string> v(h); rep(i, 0, h) cin >> v[i]; int ans = 0; rep(i, 0, h) { rep(j, 0, w) { if (v[i][j] == '#') continue; queue<pair<int, int>> p; vector<vector<int>> dist(h, vector<int>(w, -1)); p.push({i, j}); dist[i][j] = 0; while (!p.empty()) { pair<int, int> tmp = p.front(); p.pop(); rep(k, 0, 4) { int ny = tmp.first + dy[k]; int nx = tmp.second + dx[k]; if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue; if (v[ny][nx] == '.') { if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue; if (dist[ny][nx] > dist[tmp.first][tmp.second] + 1 || dist[ny][nx] == -1) dist[ny][nx] = dist[tmp.first][tmp.second] + 1, ans = max(ans, dist[ny][nx]), p.push({ny, nx}); } } } } } cout << ans << endl; }
replace
33
34
33
34
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long tint; const tint INF = 2e14 + 1; const long double PI = 3.141592; const int MOD = 998244353; #define forsn(i, s, n) for (int i = s; i < int(n); i++) #define forn(i, n) forsn(i, 0, n) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define NACHO \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() vector<int> dx = {1, -1, 0, 0}; vector<int> dy = {0, 0, 1, -1}; bool esValido(int n, int m, int x, int y) { return (0 <= x && x < n && 0 <= y && y < m); } int main() { int n, m; cin >> n >> m; vector<string> a(n); forn(i, n) cin >> a[i]; int maxi = 0; forn(i, n) { forn(j, n) { if (a[i][j] == '#') continue; vector<vector<int>> distance(n, vector<int>(m)); distance[i][j] = 0; vector<vector<bool>> visited(n, vector<bool>(m, 0)); visited[i][j] = 1; queue<pair<int, int>> q; q.push({i, j}); while (!q.empty()) { auto s = q.front(); q.pop(); forn(k, 4) { int x = s.first + dx[k]; int y = s.second + dy[k]; if (esValido(n, m, x, y) && !visited[x][y] && a[x][y] != '#') { visited[x][y] = 1; distance[x][y] = distance[s.first][s.second] + 1; maxi = max(maxi, distance[x][y]); q.push({x, y}); } } } } } cout << maxi << "\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long tint; const tint INF = 2e14 + 1; const long double PI = 3.141592; const int MOD = 998244353; #define forsn(i, s, n) for (int i = s; i < int(n); i++) #define forn(i, n) forsn(i, 0, n) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define NACHO \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() vector<int> dx = {1, -1, 0, 0}; vector<int> dy = {0, 0, 1, -1}; bool esValido(int n, int m, int x, int y) { return (0 <= x && x < n && 0 <= y && y < m); } int main() { int n, m; cin >> n >> m; vector<string> a(n); forn(i, n) cin >> a[i]; int maxi = 0; forn(i, n) { forn(j, m) { if (a[i][j] == '#') continue; vector<vector<int>> distance(n, vector<int>(m)); distance[i][j] = 0; vector<vector<bool>> visited(n, vector<bool>(m, 0)); visited[i][j] = 1; queue<pair<int, int>> q; q.push({i, j}); while (!q.empty()) { auto s = q.front(); q.pop(); forn(k, 4) { int x = s.first + dx[k]; int y = s.second + dy[k]; if (esValido(n, m, x, y) && !visited[x][y] && a[x][y] != '#') { visited[x][y] = 1; distance[x][y] = distance[s.first][s.second] + 1; maxi = max(maxi, distance[x][y]); q.push({x, y}); } } } } } cout << maxi << "\n"; }
replace
34
35
34
35
0
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // int/long: -2,147,483,648 - 2,147,483,648 (-2^31 <= int < 2^31) // long/long long: -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 // (-2^63 <= long < 2^63) #define INF (1 << 29) // 536,870,912 // lower_bound(A.begin(), A.end(), N) // upper_bound(... // A.erase(unique(A.begin(), A.end()), A.end()) // bit: &/and, |/or, ^/xor, ~/not // getline(cin, String) // while (getline(cin, S)) {} // cout <<fixed <<setprecision(10) // priority_queue<int> q; // priority_queue<int, vector<int>, less<int>> q; // Default: vector, less // priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q; // Graph // Warshall-Floyd: Distance between each node, N^3 // Dijkstra: Distance from the start node, N^2 // DFS(Depth-First Search) /* int gcd(int A, int B) { while (0!=B) { A %=B; swap(A, B); } return A; } */ const int MAX = 100000; #define MOD 1000000007 void ComInit(vector<long> &fac, vector<long> &finv, vector<long> &inv) { fac.at(0) = 1; fac.at(1) = 1; finv.at(0) = 1; finv.at(1) = 1; inv.at(1) = 1; for (int i = 2; i < MAX; i++) { fac.at(i) = (fac.at(i - 1) * i) % MOD; inv.at(i) = MOD - (inv.at(MOD % i) * (MOD / i)) % MOD; finv.at(i) = (finv.at(i - 1) * inv.at(i)) % MOD; } } long COM(int N, int K, vector<long> &fac, vector<long> &finv) { if (N < K) return 0; if ((N < 0) || (K < 0)) return 0; return (fac.at(N) * ((finv.at(K) * finv.at(N - K)) % MOD)) % MOD; } int main() { // Array vector<long> fac(MAX); vector<long> finv(MAX); vector<long> inv(MAX); // Initialize ComInit(fac, finv, inv); // Start long N, K; cin >> N >> K; vector<long> A(N, 0); for (long i = 0; i < N; i++) cin >> A.at(i); sort(A.begin(), A.end()); long S = 0; for (long w = K; w <= N; w++) { long CC = 1; if ((K > 2) && (w - K > 0)) CC = COM(w - 2, K - 2, fac, finv); for (long st = 0; st <= N - w; st++) { long f = A.at(st + w - 1) - A.at(st); S += f * CC % MOD; S %= MOD; } } cout << S << endl; return 0; }
#include <bits/stdc++.h> using namespace std; // int/long: -2,147,483,648 - 2,147,483,648 (-2^31 <= int < 2^31) // long/long long: -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 // (-2^63 <= long < 2^63) #define INF (1 << 29) // 536,870,912 // lower_bound(A.begin(), A.end(), N) // upper_bound(... // A.erase(unique(A.begin(), A.end()), A.end()) // bit: &/and, |/or, ^/xor, ~/not // getline(cin, String) // while (getline(cin, S)) {} // cout <<fixed <<setprecision(10) // priority_queue<int> q; // priority_queue<int, vector<int>, less<int>> q; // Default: vector, less // priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q; // Graph // Warshall-Floyd: Distance between each node, N^3 // Dijkstra: Distance from the start node, N^2 // DFS(Depth-First Search) /* int gcd(int A, int B) { while (0!=B) { A %=B; swap(A, B); } return A; } */ const int MAX = 100000; #define MOD 1000000007 void ComInit(vector<long> &fac, vector<long> &finv, vector<long> &inv) { fac.at(0) = 1; fac.at(1) = 1; finv.at(0) = 1; finv.at(1) = 1; inv.at(1) = 1; for (int i = 2; i < MAX; i++) { fac.at(i) = (fac.at(i - 1) * i) % MOD; inv.at(i) = MOD - (inv.at(MOD % i) * (MOD / i)) % MOD; finv.at(i) = (finv.at(i - 1) * inv.at(i)) % MOD; } } long COM(int N, int K, vector<long> &fac, vector<long> &finv) { if (N < K) return 0; if ((N < 0) || (K < 0)) return 0; return (fac.at(N) * ((finv.at(K) * finv.at(N - K)) % MOD)) % MOD; } int main() { // Array vector<long> fac(MAX); vector<long> finv(MAX); vector<long> inv(MAX); // Initialize ComInit(fac, finv, inv); // Start long N, K; cin >> N >> K; vector<long> A(N, 0); for (long i = 0; i < N; i++) cin >> A.at(i); sort(A.begin(), A.end()); long S = 0; for (long i = K - 1; i < N; i++) { S += ((A.at(i) - A.at(N - 1 - i)) * COM(i, K - 1, fac, finv)) % MOD; S %= MOD; } cout << S << endl; return 0; }
replace
80
89
80
83
TLE
p02804
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #define N 100005 using namespace std; typedef long long ll; ll M = 1e9 + 7; ll n, k, s, a[N]; ll fac[N]{1, 1}, inv[N]{1, 1}, fiv[N]{1, 1}; ll com(ll p, ll q) { return fac[p] * fiv[p - q] % M * fiv[q] % M; } int main() { ll i; cin >> n >> k; for (i = 2; i <= n; i++) { fac[i] = fac[i - 1] * i % M; inv[i] = inv[M % i] * (M - M / i) % M; fiv[i] = fiv[i - 1] * inv[i] % M; } for (i = 0; i < n; i++) scanf("%lld", &a[i]); sort(a, a + n); for (i = 0; i < n; i++) { s = (s + a[i] * (com(i, k - 1) - com(n - i - 1, k - 1))) % M; } cout << s << endl; return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #define N 100005 using namespace std; typedef long long ll; ll M = 1e9 + 7; ll n, k, s, a[N]; ll fac[N]{1, 1}, inv[N]{1, 1}, fiv[N]{1, 1}; ll com(ll p, ll q) { if (p < q) return 0; return fac[p] * fiv[p - q] % M * fiv[q] % M; } int main() { ll i; cin >> n >> k; for (i = 2; i <= n; i++) { fac[i] = fac[i - 1] * i % M; inv[i] = inv[M % i] * (M - M / i) % M; fiv[i] = fiv[i - 1] * inv[i] % M; } for (i = 0; i < n; i++) scanf("%lld", &a[i]); sort(a, a + n); for (i = 0; i < n; i++) { s = (s + a[i] * (com(i, k - 1) - com(n - i - 1, k - 1))) % M; } cout << s << endl; return 0; }
replace
11
12
11
16
0
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAX = 50000; const int MOD = 1000000007; int64_t fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } int64_t COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { COMinit(); int N, K; cin >> N >> K; vector<int64_t> A(N); for (int i = 0; i < N; ++i) cin >> A[i]; sort(A.begin(), A.end()); int64_t ans = 0; for (int i = 0; i < N; ++i) { ans += A[i] * (COM(i, K - 1) - COM(N - i - 1, K - 1)) % MOD; } cout << ans % MOD << endl; }
#include <bits/stdc++.h> using namespace std; const int MAX = 500000; const int MOD = 1000000007; int64_t fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } int64_t COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { COMinit(); int N, K; cin >> N >> K; vector<int64_t> A(N); for (int i = 0; i < N; ++i) cin >> A[i]; sort(A.begin(), A.end()); int64_t ans = 0; for (int i = 0; i < N; ++i) { ans += A[i] * (COM(i, K - 1) - COM(N - i - 1, K - 1)) % MOD; } cout << ans % MOD << endl; }
replace
3
4
3
4
0
p02804
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> using namespace std; #define sqr(x) ((x) * (x)) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() typedef vector<int> vi; typedef vector<long long> vll; typedef vector<char> vc; typedef pair<int, int> pii; typedef long long ll; const int MOD = 1000000007; 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; } void cmb_init(int n, vll &fac, vll &finv) { vll inv(n + 1, 1); for (int i = 2; i <= n; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll cmb(int n, int k, vll fac, vll finv) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } void solve() { int N, K; cin >> N >> K; vi A(N, 0); vll fac(N + 1, 1), finv(N + 1, 1); cmb_init(N, fac, finv); for (int i = 0; i < N; i++) { cin >> A[i]; } sort(all(A)); ll sum = 0; for (int i = 0; i < N; i++) { sum -= (ll)A[i] * cmb(N - i - 1, K - 1, fac, finv) % MOD; sum += (ll)A[i] * cmb(i, K - 1, fac, finv) % MOD; sum %= MOD; } cout << sum << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include <algorithm> #include <bits/stdc++.h> using namespace std; #define sqr(x) ((x) * (x)) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() typedef vector<int> vi; typedef vector<long long> vll; typedef vector<char> vc; typedef pair<int, int> pii; typedef long long ll; const int MOD = 1000000007; 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; } void cmb_init(int n, vll &fac, vll &finv) { vll inv(n + 1, 1); for (int i = 2; i <= n; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll cmb(int n, int k, vll &fac, vll &finv) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } void solve() { int N, K; cin >> N >> K; vi A(N, 0); vll fac(N + 1, 1), finv(N + 1, 1); cmb_init(N, fac, finv); for (int i = 0; i < N; i++) { cin >> A[i]; } sort(all(A)); ll sum = 0; for (int i = 0; i < N; i++) { sum -= (ll)A[i] * cmb(N - i - 1, K - 1, fac, finv) % MOD; sum += (ll)A[i] * cmb(i, K - 1, fac, finv) % MOD; sum %= MOD; } cout << sum << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
replace
37
38
37
38
TLE
p02804
C++
Time Limit Exceeded
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> typedef long long ll; using namespace std; #ifndef LOCAL #define debug(x) ; #else #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } #endif #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 200010 vector<ll> factmemo, factmemoInv; ll factmemoMod = -1; ll factorial(int n, int M) { if (factmemoMod == M) return factmemo[n]; if (n <= 1) return 1; ll res = 1; for (int i = 1; i <= n; i++) res = res * i % M; return res; } ll power(ll k, ll n, int M) { ll res = 1; while (n > 0) { if (n & 1) res = res * k % M; k = k * k % M; n /= 2; } return res; } void initFactorial(int n, int M) { factmemo.assign(n + 1, 0); factmemoInv.assign(n + 1, 0); factmemoMod = M; factmemo[0] = 1; for (int i = 1; i <= n; i++) factmemo[i] = factmemo[i - 1] * i % M; factmemoInv[n] = power(factmemo[n], M - 2, M); for (int i = n; i > 0; i--) factmemoInv[i - 1] = factmemoInv[i] * i % M; } // nCm nPm nHm (mod M) /*Combination*/ ll C(int n, int m, int M) { if (n < m) return 0; if (m == 0 || n == m) return 1; if (factmemoMod == M) return factmemo[n] * factmemoInv[m] % M * factmemoInv[n - m] % M; ll numer = factorial(n, M); ll denom = factorial(m, M) * factorial(n - m, M) % M; denom = power((int)denom, M - 2, M); return numer * denom % M; } /*Permutation*/ ll P(int n, int m, int M) { if (n < m) return 0; if (m == 0) return 1; if (factmemoMod == M) return factmemo[n] * factmemoInv[n - m] % M; ll numer = factorial(n, M); ll denom = factorial(n - m, M); denom = power((int)denom, M - 2, M); return numer * denom % M; } /*Combination with Repetitions*/ ll H(int n, int m, int M) { if (n == 0 && m == 0) return 1; return C(n + m - 1, m, M); } int main() { int N, K; map<int, int> counter; scanf("%d%d", &N, &K); for (int i = 0; i < N; i++) { int A; scanf("%d", &A); counter[A]++; } ll ans = 0; int sum = 0; for (auto p : counter) { // max ll s = (mod + C(sum + p.second, K, mod) - C(sum, K, mod)) % mod; ans += s * p.first % mod; // max ll t = (mod + C(N - sum, K, mod) - C(N - sum - p.second, K, mod)) % mod; ans -= t * p.first % mod; debug(s); debug(t); sum += p.second; } cout << (ans % mod + mod) % mod << endl; return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> typedef long long ll; using namespace std; #ifndef LOCAL #define debug(x) ; #else #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } #endif #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 200010 vector<ll> factmemo, factmemoInv; ll factmemoMod = -1; ll factorial(int n, int M) { if (factmemoMod == M) return factmemo[n]; if (n <= 1) return 1; ll res = 1; for (int i = 1; i <= n; i++) res = res * i % M; return res; } ll power(ll k, ll n, int M) { ll res = 1; while (n > 0) { if (n & 1) res = res * k % M; k = k * k % M; n /= 2; } return res; } void initFactorial(int n, int M) { factmemo.assign(n + 1, 0); factmemoInv.assign(n + 1, 0); factmemoMod = M; factmemo[0] = 1; for (int i = 1; i <= n; i++) factmemo[i] = factmemo[i - 1] * i % M; factmemoInv[n] = power(factmemo[n], M - 2, M); for (int i = n; i > 0; i--) factmemoInv[i - 1] = factmemoInv[i] * i % M; } // nCm nPm nHm (mod M) /*Combination*/ ll C(int n, int m, int M) { if (n < m) return 0; if (m == 0 || n == m) return 1; if (factmemoMod == M) return factmemo[n] * factmemoInv[m] % M * factmemoInv[n - m] % M; ll numer = factorial(n, M); ll denom = factorial(m, M) * factorial(n - m, M) % M; denom = power((int)denom, M - 2, M); return numer * denom % M; } /*Permutation*/ ll P(int n, int m, int M) { if (n < m) return 0; if (m == 0) return 1; if (factmemoMod == M) return factmemo[n] * factmemoInv[n - m] % M; ll numer = factorial(n, M); ll denom = factorial(n - m, M); denom = power((int)denom, M - 2, M); return numer * denom % M; } /*Combination with Repetitions*/ ll H(int n, int m, int M) { if (n == 0 && m == 0) return 1; return C(n + m - 1, m, M); } int main() { int N, K; map<int, int> counter; scanf("%d%d", &N, &K); initFactorial(N, mod); for (int i = 0; i < N; i++) { int A; scanf("%d", &A); counter[A]++; } ll ans = 0; int sum = 0; for (auto p : counter) { // max ll s = (mod + C(sum + p.second, K, mod) - C(sum, K, mod)) % mod; ans += s * p.first % mod; // max ll t = (mod + C(N - sum, K, mod) - C(N - sum - p.second, K, mod)) % mod; ans -= t * p.first % mod; debug(s); debug(t); sum += p.second; } cout << (ans % mod + mod) % mod << endl; return 0; }
insert
138
138
138
140
TLE
p02804
C++
Runtime Error
// // Created by Mahmoud Rashad on 11/27/19. // #include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<pair<ii, ii>> vii; const int N = 1e5 + 5, MOD = 1e9 + 7; long long fact[N], fact_inv[N]; long long mul(long long a, long long b) { return (a + MOD) * b % MOD; } long long power(long long a, long long b) { if (!b) return 1; long long r = power(a, b / 2); r = mul(r, r); if (b & 1) return mul(r, a); return r; } long long mod_inv(long long x) { return power(x, MOD - 2); } long long nCr(long long n, long long r) { if (!n) return !r; if (r > n) return 0; return mul(fact[n], mul(fact_inv[n - r], fact_inv[r])); } void pre() { fact[0] = fact_inv[0] = 1; for (int i = 1; i < N; i++) { fact[i] = mul(fact[i - 1], i); fact_inv[i] = mod_inv(fact[i]); } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("output.out", "w", stdout); #endif ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10), cout << fixed; pre(); int n, k; cin >> n >> k; vector<int> arr(n); for (int &x : arr) cin >> x; sort(arr.begin(), arr.end()); long long ans = 0; for (int i = 0; i < n; ++i) { if (i + 1 >= k) { ans += mul(arr[i], nCr(i, i + 1 - k)); ans %= MOD; } if (n - i + 1 >= k) { ans += mul(-arr[i], nCr(n - i - 1, k - 1)); ans %= MOD; } } cout << ans << '\n'; return 0; }
// // Created by Mahmoud Rashad on 11/27/19. // #include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<pair<ii, ii>> vii; const int N = 1e5 + 5, MOD = 1e9 + 7; long long fact[N], fact_inv[N]; long long mul(long long a, long long b) { return (a + MOD) * b % MOD; } long long power(long long a, long long b) { if (!b) return 1; long long r = power(a, b / 2); r = mul(r, r); if (b & 1) return mul(r, a); return r; } long long mod_inv(long long x) { return power(x, MOD - 2); } long long nCr(long long n, long long r) { if (!n) return !r; if (r > n) return 0; return mul(fact[n], mul(fact_inv[n - r], fact_inv[r])); } void pre() { fact[0] = fact_inv[0] = 1; for (int i = 1; i < N; i++) { fact[i] = mul(fact[i - 1], i); fact_inv[i] = mod_inv(fact[i]); } } int main() { #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.out", "w", stdout); #endif ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10), cout << fixed; pre(); int n, k; cin >> n >> k; vector<int> arr(n); for (int &x : arr) cin >> x; sort(arr.begin(), arr.end()); long long ans = 0; for (int i = 0; i < n; ++i) { if (i + 1 >= k) { ans += mul(arr[i], nCr(i, i + 1 - k)); ans %= MOD; } if (n - i + 1 >= k) { ans += mul(-arr[i], nCr(n - i - 1, k - 1)); ans %= MOD; } } cout << ans << '\n'; return 0; }
replace
47
48
47
48
-6
terminate called after throwing an instance of 'std::length_error' what(): cannot create std::vector larger than max_size()
p02804
C++
Runtime Error
#include <bits/stdc++.h> #define ff first #define ss second #define pb push_back #define pf push_front #define eb emplace_back #define emp emplace #define ins insert #define mp make_pair #define mt make_tuple #define sz(s) (int)s.size() #define forp(i, a, b) for (int i = a; i <= b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define ren(i, n) for (int i = n - 1; i >= 0; i--) #define forn(i, a, b) for (int i = a; i >= b; i--) #define w(t) while (t) #define sc(a) scanf("%d", &a); #define on cout << "\n" #define os cout << " " #define o2(a, b) cout << a << " " << b #define o(a) cout << a #define ppr(a) cout << a.first << " " << a.second #define bitcount __builtin_popcount #define gcd __gcd #define all(v) v.begin(), v.end() #define mem(n, m) memset(n, m, sizeof(n)) #define pii pair<int, int> #define tiii tuple<int, int, int> #define pll pair<long long, long long> #define sii set<int> #define sll set<long long> #define vii vector<int> #define vll vector<long long> #define mii map<int, int> #define mll map<long long, long long> #define lob lower_bound #define upb upper_bound #define ret return #define present(s, x) (s.find(x) != s.end()) #define cpresent(s, x) (find(all(s), x) != s.end()) #define ford(container, it) \ for (auto it = container.begin(); it != container.end(); it++) #define fors(container, it, a, b) for (auto it = a; it != b; it++) #define boost \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define MOD 1000000007 #define EPSILON 1e-9 #define PI acos(-1) #define int long long #define inf 1e9 #define debug(a) \ cerr << #a << ": "; \ for (auto i : a) \ cerr << i << " "; \ cerr << '\n'; #define trace(a) cerr << #a << ": " << a << "\n" #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> typedef long long ll; typedef long double ldo; typedef double db; using namespace std; auto start = std::chrono::system_clock::now(); inline void skj() { std::chrono::time_point<std::chrono::system_clock> end; end = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed_seconds = end - start; std::time_t end_time = std::chrono::system_clock::to_time_t(end); cerr << "Time taken " << elapsed_seconds.count() * 1000 << "\n"; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // uniform_int_distribution<int> ud(1, 100); use this for random number // generator // custom hash for unordered map struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; inline int binexp(int a, int b, int m) { if (a == 0) { ret 0; } int res = 1; a %= m; while (b) { if (b & 1) res = (res * 1ll * a) % m; a = (a * 1ll * a) % m; b >>= 1; } return res; } const int N = 1e5 + 5; int fact[N], ifact[N]; inline int ncr(int n, int r) { int ans = fact[n]; ans = (ans * ifact[n - r]) % MOD; ans = (ans * ifact[r]) % MOD; ret ans; } inline void solve_me_senpai() { int n, k; cin >> n >> k; fact[0] = 1; forp(i, 1, n) { fact[i] = (fact[i - 1] * i) % MOD; } ifact[n] = binexp(fact[n], MOD - 2, MOD); forn(i, n - 1, 0) { ifact[i] = (ifact[i + 1] * (i + 1)) % MOD; } vii arr(n); rep(i, n) cin >> arr[i]; sort(all(arr)); int ans = 0; forp(i, k - 1, n - 1) { ans = (ans + (ncr(i, k - 1) * (MOD + arr[i]) % MOD) % MOD) % MOD; } // o(ans);on; rep(i, n - k + 1) { ans = (ans + (MOD - (ncr(n - i - 1, k - 1) * (MOD + arr[i]) % MOD) % MOD) % MOD) % MOD; } o(ans); } signed main() { boost #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; // cin >> t; int a = 1; while (t--) { // cout << "Case #" << a << ": "; solve_me_senpai(); a++; } // skj(); return 0; }
#include <bits/stdc++.h> #define ff first #define ss second #define pb push_back #define pf push_front #define eb emplace_back #define emp emplace #define ins insert #define mp make_pair #define mt make_tuple #define sz(s) (int)s.size() #define forp(i, a, b) for (int i = a; i <= b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define ren(i, n) for (int i = n - 1; i >= 0; i--) #define forn(i, a, b) for (int i = a; i >= b; i--) #define w(t) while (t) #define sc(a) scanf("%d", &a); #define on cout << "\n" #define os cout << " " #define o2(a, b) cout << a << " " << b #define o(a) cout << a #define ppr(a) cout << a.first << " " << a.second #define bitcount __builtin_popcount #define gcd __gcd #define all(v) v.begin(), v.end() #define mem(n, m) memset(n, m, sizeof(n)) #define pii pair<int, int> #define tiii tuple<int, int, int> #define pll pair<long long, long long> #define sii set<int> #define sll set<long long> #define vii vector<int> #define vll vector<long long> #define mii map<int, int> #define mll map<long long, long long> #define lob lower_bound #define upb upper_bound #define ret return #define present(s, x) (s.find(x) != s.end()) #define cpresent(s, x) (find(all(s), x) != s.end()) #define ford(container, it) \ for (auto it = container.begin(); it != container.end(); it++) #define fors(container, it, a, b) for (auto it = a; it != b; it++) #define boost \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define MOD 1000000007 #define EPSILON 1e-9 #define PI acos(-1) #define int long long #define inf 1e9 #define debug(a) \ cerr << #a << ": "; \ for (auto i : a) \ cerr << i << " "; \ cerr << '\n'; #define trace(a) cerr << #a << ": " << a << "\n" #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> typedef long long ll; typedef long double ldo; typedef double db; using namespace std; auto start = std::chrono::system_clock::now(); inline void skj() { std::chrono::time_point<std::chrono::system_clock> end; end = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed_seconds = end - start; std::time_t end_time = std::chrono::system_clock::to_time_t(end); cerr << "Time taken " << elapsed_seconds.count() * 1000 << "\n"; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // uniform_int_distribution<int> ud(1, 100); use this for random number // generator // custom hash for unordered map struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; inline int binexp(int a, int b, int m) { if (a == 0) { ret 0; } int res = 1; a %= m; while (b) { if (b & 1) res = (res * 1ll * a) % m; a = (a * 1ll * a) % m; b >>= 1; } return res; } const int N = 1e5 + 5; int fact[N], ifact[N]; inline int ncr(int n, int r) { int ans = fact[n]; ans = (ans * ifact[n - r]) % MOD; ans = (ans * ifact[r]) % MOD; ret ans; } inline void solve_me_senpai() { int n, k; cin >> n >> k; fact[0] = 1; forp(i, 1, n) { fact[i] = (fact[i - 1] * i) % MOD; } ifact[n] = binexp(fact[n], MOD - 2, MOD); forn(i, n - 1, 0) { ifact[i] = (ifact[i + 1] * (i + 1)) % MOD; } vii arr(n); rep(i, n) cin >> arr[i]; sort(all(arr)); int ans = 0; forp(i, k - 1, n - 1) { ans = (ans + (ncr(i, k - 1) * (MOD + arr[i]) % MOD) % MOD) % MOD; } // o(ans);on; rep(i, n - k + 1) { ans = (ans + (MOD - (ncr(n - i - 1, k - 1) * (MOD + arr[i]) % MOD) % MOD) % MOD) % MOD; } o(ans); } signed main() { boost int t = 1; // cin >> t; int a = 1; while (t--) { // cout << "Case #" << a << ": "; solve_me_senpai(); a++; } // skj(); return 0; }
replace
140
146
140
141
0
p02804
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define repr(i, a, b) for (int i = a; i < (b); ++i) #define reprev(i, n) for (int i = n - 1; i >= 0; --i) #define reprrev(i, a, b) for (int i = b - 1; i >= (a); --i) using namespace std; typedef long long ll; typedef pair<int, int> P; const int MAX = 50001; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { // cout << fixed << setprecision(10); COMinit(); int N, K; cin >> N >> K; vector<int> A(N); vector<int> max_n(N); rep(i, N) cin >> A[i]; if (K <= 1) { cout << 0 << endl; return 0; } sort(A.begin(), A.end()); // max Aiとなる回数を数える repr(i, K - 1, N) { int n = i - 1; int r = K - 2; // cout << "n: " << n << " r: " << r << endl; max_n[i] = (max_n[i - 1] + COM(n, r)) % MOD; } // rep(i, N) cout << max_n[i] << " "; // cout << endl; ll ans = 0; rep(i, N) { ans += (A[i] * ll(max_n[i]) - A[N - 1 - i] * ll(max_n[i])) % MOD; ans %= MOD; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define repr(i, a, b) for (int i = a; i < (b); ++i) #define reprev(i, n) for (int i = n - 1; i >= 0; --i) #define reprrev(i, a, b) for (int i = b - 1; i >= (a); --i) using namespace std; typedef long long ll; typedef pair<int, int> P; const int MAX = 100001; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { // cout << fixed << setprecision(10); COMinit(); int N, K; cin >> N >> K; vector<int> A(N); vector<int> max_n(N); rep(i, N) cin >> A[i]; if (K <= 1) { cout << 0 << endl; return 0; } sort(A.begin(), A.end()); // max Aiとなる回数を数える repr(i, K - 1, N) { int n = i - 1; int r = K - 2; // cout << "n: " << n << " r: " << r << endl; max_n[i] = (max_n[i - 1] + COM(n, r)) % MOD; } // rep(i, N) cout << max_n[i] << " "; // cout << endl; ll ans = 0; rep(i, N) { ans += (A[i] * ll(max_n[i]) - A[N - 1 - i] * ll(max_n[i])) % MOD; ans %= MOD; } cout << ans << endl; return 0; }
replace
9
10
9
10
0
p02804
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define INF (1 << 30) #define INFL (1LL << 62) #define MOD7 1000000007 #define MOD9 1000000009 #define EPS 1e-10 using ll = long long; using ull = unsigned long long; int n, k; ll a[100000]; ll fact[100000]; ll inv[100000]; ll pow(ll a, ll b) { if (b == 0) return 1; if (b % 2) return a * pow(a, b - 1); return pow(a * a, b / 2); } // a^(-1)≡a^(p-2) (mod p) (but p is prime) ll powMod(ll a, ll b, ll p) { if (b == 0) return 1; if (b % 2) return (a * powMod(a, b - 1, p)) % p; return powMod(a * a % p, b / 2, p); } ll combi(ll nn, ll kk) { return (((fact[nn] * inv[nn - kk]) % MOD7) * inv[kk]) % MOD7; } void pre() { fact[0] = 1; inv[0] = 1; for (int i = 1; i < n; ++i) { fact[i] = (fact[i - 1] * i) % MOD7; inv[i] = (inv[i - 1] * powMod(i, MOD7 - 2, MOD7)) % MOD7; } } int main() { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n); pre(); ll ans = 0; for (int i = 0; i < n; ++i) { ans += (a[i] * combi(i, k - 1)) % MOD7; ans %= MOD7; ans -= (a[i] * combi(n - i - 1, k - 1)) % MOD7; ans %= MOD7; } if (ans < 0) ans += MOD7; cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define INF (1 << 30) #define INFL (1LL << 62) #define MOD7 1000000007 #define MOD9 1000000009 #define EPS 1e-10 using ll = long long; using ull = unsigned long long; int n, k; ll a[100000]; ll fact[100000]; ll inv[100000]; ll pow(ll a, ll b) { if (b == 0) return 1; if (b % 2) return a * pow(a, b - 1); return pow(a * a, b / 2); } // a^(-1)≡a^(p-2) (mod p) (but p is prime) ll powMod(ll a, ll b, ll p) { if (b == 0) return 1; if (b % 2) return (a * powMod(a, b - 1, p)) % p; return powMod(a * a % p, b / 2, p); } ll combi(ll nn, ll kk) { if (nn < kk) return 0; return (((fact[nn] * inv[nn - kk]) % MOD7) * inv[kk]) % MOD7; } void pre() { fact[0] = 1; inv[0] = 1; for (int i = 1; i < n; ++i) { fact[i] = (fact[i - 1] * i) % MOD7; inv[i] = (inv[i - 1] * powMod(i, MOD7 - 2, MOD7)) % MOD7; } } int main() { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n); pre(); ll ans = 0; for (int i = 0; i < n; ++i) { ans += (a[i] * combi(i, k - 1)) % MOD7; ans %= MOD7; ans -= (a[i] * combi(n - i - 1, k - 1)) % MOD7; ans %= MOD7; } if (ans < 0) ans += MOD7; cout << ans << endl; return 0; }
insert
50
50
50
52
0
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; template <typename T> istream &operator>>(istream &istr, vector<T> &vec) { for (T &x : vec) istr >> x; return istr; } template <typename T> ostream &operator<<(ostream &ostr, vector<T> &vec) { if (!vec.empty()) { ostr << vec.front(); for (auto itr = ++vec.begin(); itr != vec.end(); itr++) ostr << " " << *itr; } return ostr; } template <typename T> ostream &operator<<(ostream &ostr, vector<vector<T>> &mat) { if (!mat.empty()) { ostr << mat.front(); for (auto itr = ++mat.begin(); itr != mat.end(); itr++) ostr << endl << *itr; } return ostr; } template <typename T, typename U> ostream &operator<<(ostream &ostr, pair<T, U> &p) { ostr << p.first << " " << p.second; return ostr; } template <typename T, typename U> ostream &operator<<(ostream &ostr, vector<pair<T, U>> &mat) { if (!mat.empty()) { ostr << mat.front(); for (auto itr = ++mat.begin(); itr != mat.end(); itr++) ostr << endl << *itr; } return ostr; } typedef long long ll; typedef vector<ll> vll; typedef vector<char> vc; // vll x(n); x[i]=a; typedef vector<vector<ll>> vvll; typedef vector<vector<char>> vvc; // vvll x(m,vi(n)); x[i][j]=a; typedef pair<ll, ll> pll; typedef vector<pair<ll, ll>> vpll; // vpll x(n); x[i]={a,b}; x[i].first/second; #define PI 3.1415926535897932 #define INF 1e+12 #define EPS 1e-12 #define REP(i, n) for (ll i = 0; i < (n); i++) #define RREP(i, n) for (ll i = (n)-1; i >= 0; i--) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORR(i, a, b) for (ll i = (b)-1; i >= (a); i--) #define dump(x) cout << #x << " = " << (x) << endl #define all(x) (x).begin(), (x).end() // sort(all(x)); // 拡張ユーグリッド互除法 ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll g = extgcd(b, a % b, y, x); y -= a / b * x; return g; } ll gcd(ll a, ll b) { ll x, y; return extgcd(a, b, x, y); } // 逆元(mod p) ll modinv(ll a, ll p) { ll x, y; extgcd(a, p, x, y); if (x < 0) x += p; return x; } // 二項係数 #define MAX 10000 ll fac[MAX + 1]; ll finv[MAX + 1]; void nCr_init(ll max, ll mod) { fac[0] = 1; finv[0] = 1; REP(i, max) { // i=1->max+1 => 0->max fac[i + 1] = fac[i] * (i + 1) % mod; finv[i + 1] = finv[i] * modinv(i + 1, mod) % mod; } } ll nCr(ll n, ll r, ll mod) { if (n < r || n < 0 || r < 0) return 0; return fac[n] * (finv[r] * finv[n - r] % mod) % mod; } int main() { const ll MOD = 1e9 + 7; ll n, k, ans = 0; cin >> n >> k; vll a(n); cin >> a; sort(all(a)); nCr_init(n, MOD); REP(i, n) { ans += a[i] * nCr(i, k - 1, MOD); ans -= a[i] * nCr(n - i - 1, k - 1, MOD); ans %= MOD; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> istream &operator>>(istream &istr, vector<T> &vec) { for (T &x : vec) istr >> x; return istr; } template <typename T> ostream &operator<<(ostream &ostr, vector<T> &vec) { if (!vec.empty()) { ostr << vec.front(); for (auto itr = ++vec.begin(); itr != vec.end(); itr++) ostr << " " << *itr; } return ostr; } template <typename T> ostream &operator<<(ostream &ostr, vector<vector<T>> &mat) { if (!mat.empty()) { ostr << mat.front(); for (auto itr = ++mat.begin(); itr != mat.end(); itr++) ostr << endl << *itr; } return ostr; } template <typename T, typename U> ostream &operator<<(ostream &ostr, pair<T, U> &p) { ostr << p.first << " " << p.second; return ostr; } template <typename T, typename U> ostream &operator<<(ostream &ostr, vector<pair<T, U>> &mat) { if (!mat.empty()) { ostr << mat.front(); for (auto itr = ++mat.begin(); itr != mat.end(); itr++) ostr << endl << *itr; } return ostr; } typedef long long ll; typedef vector<ll> vll; typedef vector<char> vc; // vll x(n); x[i]=a; typedef vector<vector<ll>> vvll; typedef vector<vector<char>> vvc; // vvll x(m,vi(n)); x[i][j]=a; typedef pair<ll, ll> pll; typedef vector<pair<ll, ll>> vpll; // vpll x(n); x[i]={a,b}; x[i].first/second; #define PI 3.1415926535897932 #define INF 1e+12 #define EPS 1e-12 #define REP(i, n) for (ll i = 0; i < (n); i++) #define RREP(i, n) for (ll i = (n)-1; i >= 0; i--) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORR(i, a, b) for (ll i = (b)-1; i >= (a); i--) #define dump(x) cout << #x << " = " << (x) << endl #define all(x) (x).begin(), (x).end() // sort(all(x)); // 拡張ユーグリッド互除法 ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll g = extgcd(b, a % b, y, x); y -= a / b * x; return g; } ll gcd(ll a, ll b) { ll x, y; return extgcd(a, b, x, y); } // 逆元(mod p) ll modinv(ll a, ll p) { ll x, y; extgcd(a, p, x, y); if (x < 0) x += p; return x; } // 二項係数 #define MAX (int)1e5 ll fac[MAX + 1]; ll finv[MAX + 1]; void nCr_init(ll max, ll mod) { fac[0] = 1; finv[0] = 1; REP(i, max) { // i=1->max+1 => 0->max fac[i + 1] = fac[i] * (i + 1) % mod; finv[i + 1] = finv[i] * modinv(i + 1, mod) % mod; } } ll nCr(ll n, ll r, ll mod) { if (n < r || n < 0 || r < 0) return 0; return fac[n] * (finv[r] * finv[n - r] % mod) % mod; } int main() { const ll MOD = 1e9 + 7; ll n, k, ans = 0; cin >> n >> k; vll a(n); cin >> a; sort(all(a)); nCr_init(n, MOD); REP(i, n) { ans += a[i] * nCr(i, k - 1, MOD); ans -= a[i] * nCr(n - i - 1, k - 1, MOD); ans %= MOD; } cout << ans << endl; return 0; }
replace
93
94
93
94
0
p02804
C++
Runtime Error
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long int ll; typedef pair<ll, ll> P; 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; } const ll mod = 1000000007; ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long f[4005], fi[4005]; long long MOD = 1e9 + 7; void init(int n) { f[0] = 1; for (int i = 1; i <= n; i++) { f[i] = f[i - 1] * i % MOD; } fi[n] = mod_pow(f[n], MOD - 2, MOD); for (int i = n; i > 0; i--) { fi[i - 1] = fi[i] * i % MOD; } } long long comb(int r, int c) { if (r < c) return 0; return f[r] * fi[c] % MOD * fi[r - c] % MOD; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) { cin >> a[i]; } sort(a.begin(), a.end()); init(n + 1); ll ans = 0; rep(i, n) { if (i < k - 1) continue; ans = (ans + (a[i] * comb(i, k - 1)) % mod) % mod; } rep(i, n) { if (n - i - 1 < k - 1) continue; ll sub = (a[i] * comb(n - i - 1, k - 1)) % mod; if (ans <= sub) ans += mod; ans = (ans - sub) % mod; } cout << ans << endl; return 0; }
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long int ll; typedef pair<ll, ll> P; 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; } const ll mod = 1000000007; ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long f[100005], fi[100005]; long long MOD = 1e9 + 7; void init(int n) { f[0] = 1; for (int i = 1; i <= n; i++) { f[i] = f[i - 1] * i % MOD; } fi[n] = mod_pow(f[n], MOD - 2, MOD); for (int i = n; i > 0; i--) { fi[i - 1] = fi[i] * i % MOD; } } long long comb(int r, int c) { if (r < c) return 0; return f[r] * fi[c] % MOD * fi[r - c] % MOD; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) { cin >> a[i]; } sort(a.begin(), a.end()); init(n + 1); ll ans = 0; rep(i, n) { if (i < k - 1) continue; ans = (ans + (a[i] * comb(i, k - 1)) % mod) % mod; } rep(i, n) { if (n - i - 1 < k - 1) continue; ll sub = (a[i] * comb(n - i - 1, k - 1)) % mod; if (ans <= sub) ans += mod; ans = (ans - sub) % mod; } cout << ans << endl; return 0; }
replace
33
34
33
34
0
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef long long ll; using namespace std; const int MOD = 1e9 + 7; const int MAX = 100010; ll fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 ll COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { int n, k; cin >> n >> k; vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end(), greater<ll>()); ll ans = 0; COMinit(); for (int i = 0; i < n; i++) { if (n - i - 1 >= k - 1) { ll c = COM(n - i - 1, k - 1); ll b = a[i] - a[n - 1 - i]; while (b > 0) b += MOD; ans += ((b % MOD) * (c % MOD)) % MOD; ans = ans % MOD; } } cout << ans << endl; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; const int MOD = 1e9 + 7; const int MAX = 100010; ll fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 ll COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { int n, k; cin >> n >> k; vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end(), greater<ll>()); ll ans = 0; COMinit(); for (int i = 0; i < n; i++) { if (n - i - 1 >= k - 1) { ll c = COM(n - i - 1, k - 1); ll b = a[i] - a[n - 1 - i]; while (b < 0) b += MOD; ans += ((b % MOD) * (c % MOD)) % MOD; ans = ans % MOD; } } cout << ans << endl; }
replace
45
46
45
46
TLE
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long // #define double long double #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORR(i, a, b) for (int i = (a); i > (b); --i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOREACH(x, a) for (auto &(x) : (a)) #define VECCIN(x) \ for (auto &youso_ : (x)) \ cin >> youso_ #define bitcnt(x) __builtin_popcount(x) #define lbit(x) __builtin_ffsll(x) #define rbit(x) (64 - __builtin_clzll(x)) #define fi first #define se second #define All(a) (a).begin(), (a).end() #define rAll(a) (a).rbegin(), (a).rend() #define cinfast() cin.tie(0), ios::sync_with_stdio(false) #define PERM(c) \ sort(All(c)); \ for (bool cp = true; cp; cp = next_permutation(All(c))) #define COMB(n, k) \ for (ll bit = (1LL << k) - 1; bit < (1LL << n); bit = next_combination(bit)) #define PERM2(n, k) \ COMB(n, k) { \ vector<int> sel; \ for (int bitindex = 0; bitindex < n; bitindex++) \ if (bit >> bitindex & 1) \ sel.emplace_back(bitindex); \ PERM(sel) { Printv(sel); } \ } #define MKORDER(n) \ vector<int> od(n); \ iota(All(od), 0LL); template <typename T = long long> inline T IN() { T x; cin >> x; return (x); } inline void CIN() {} template <class Head, class... Tail> inline void CIN(Head &&head, Tail &&...tail) { cin >> head; CIN(move(tail)...); } template <class Head> inline void COUT(Head &&head) { cout << (head) << "\n"; } template <class Head, class... Tail> inline void COUT(Head &&head, Tail &&...tail) { cout << (head) << " "; COUT(forward<Tail>(tail)...); } #define CCIN(...) \ char __VA_ARGS__; \ CIN(__VA_ARGS__) #define DCIN(...) \ double __VA_ARGS__; \ CIN(__VA_ARGS__) #define LCIN(...) \ long long __VA_ARGS__; \ CIN(__VA_ARGS__) #define SCIN(...) \ string __VA_ARGS__; \ CIN(__VA_ARGS__) #define Printv(v) \ { \ FOREACH(x, v) { cout << x << " "; } \ cout << "\n"; \ } template <typename T = string> inline void eputs(T s) { cout << s << "\n"; exit(0); } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } long long next_combination(long long sub) { long long x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } // generic lambdas template <typename F> #if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) [[nodiscard]] #elif defined(__GNUC__) && \ (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4) __attribute__((warn_unused_result)) #endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) static inline constexpr decltype(auto) fix(F &&f) noexcept { return [f = std::forward<F>(f)](auto &&...args) { return f(f, std::forward<decltype(args)>(args)...); }; } template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>; template <typename T> using PQ = priority_queue<T>; typedef long long ll; typedef vector<ll> VL; typedef vector<VL> VVL; typedef pair<ll, ll> PL; typedef vector<PL> VPL; typedef vector<bool> VB; typedef vector<double> VD; typedef vector<string> VS; const int INF = 1e9; const int MOD = 1e9 + 7; // const int MOD = 998244353; const ll LINF = 1e18; const ll dw[] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll dh[] = {0, 1, 1, 1, 0, -1, -1, -1}; #define PI 3.141592653589793238 #define EPS 1e-7 // 1000000007 で割ったあまりを扱う構造体 template <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) v += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; val %= (val + MOD) % MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; val = (val + MOD) % MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = (val * u % MOD) % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept { return is >> x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; using mint = Fp<MOD>; const ll NMAX = 1e6; ll fac[NMAX + 1], inv[NMAX + 1], finv[NMAX + 1]; void cominit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; FOR(i, 2, NMAX + 1) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll comb(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } signed main() { LCIN(N, K); VL A(N); VECCIN(A); sort(rAll(A)); cominit(); mint ans = 0; REP(i, N) { ans += (comb(i, K - 1) % MOD * A[N - 1 - i] % MOD) % MOD; } REP(i, N) { ans -= (comb(N - 1 - i, K - 1) % MOD * A[N - 1 - i] % MOD) % MOD; } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; #define int long long // #define double long double #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORR(i, a, b) for (int i = (a); i > (b); --i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOREACH(x, a) for (auto &(x) : (a)) #define VECCIN(x) \ for (auto &youso_ : (x)) \ cin >> youso_ #define bitcnt(x) __builtin_popcount(x) #define lbit(x) __builtin_ffsll(x) #define rbit(x) (64 - __builtin_clzll(x)) #define fi first #define se second #define All(a) (a).begin(), (a).end() #define rAll(a) (a).rbegin(), (a).rend() #define cinfast() cin.tie(0), ios::sync_with_stdio(false) #define PERM(c) \ sort(All(c)); \ for (bool cp = true; cp; cp = next_permutation(All(c))) #define COMB(n, k) \ for (ll bit = (1LL << k) - 1; bit < (1LL << n); bit = next_combination(bit)) #define PERM2(n, k) \ COMB(n, k) { \ vector<int> sel; \ for (int bitindex = 0; bitindex < n; bitindex++) \ if (bit >> bitindex & 1) \ sel.emplace_back(bitindex); \ PERM(sel) { Printv(sel); } \ } #define MKORDER(n) \ vector<int> od(n); \ iota(All(od), 0LL); template <typename T = long long> inline T IN() { T x; cin >> x; return (x); } inline void CIN() {} template <class Head, class... Tail> inline void CIN(Head &&head, Tail &&...tail) { cin >> head; CIN(move(tail)...); } template <class Head> inline void COUT(Head &&head) { cout << (head) << "\n"; } template <class Head, class... Tail> inline void COUT(Head &&head, Tail &&...tail) { cout << (head) << " "; COUT(forward<Tail>(tail)...); } #define CCIN(...) \ char __VA_ARGS__; \ CIN(__VA_ARGS__) #define DCIN(...) \ double __VA_ARGS__; \ CIN(__VA_ARGS__) #define LCIN(...) \ long long __VA_ARGS__; \ CIN(__VA_ARGS__) #define SCIN(...) \ string __VA_ARGS__; \ CIN(__VA_ARGS__) #define Printv(v) \ { \ FOREACH(x, v) { cout << x << " "; } \ cout << "\n"; \ } template <typename T = string> inline void eputs(T s) { cout << s << "\n"; exit(0); } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } long long next_combination(long long sub) { long long x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } // generic lambdas template <typename F> #if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) [[nodiscard]] #elif defined(__GNUC__) && \ (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4) __attribute__((warn_unused_result)) #endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) static inline constexpr decltype(auto) fix(F &&f) noexcept { return [f = std::forward<F>(f)](auto &&...args) { return f(f, std::forward<decltype(args)>(args)...); }; } template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>; template <typename T> using PQ = priority_queue<T>; typedef long long ll; typedef vector<ll> VL; typedef vector<VL> VVL; typedef pair<ll, ll> PL; typedef vector<PL> VPL; typedef vector<bool> VB; typedef vector<double> VD; typedef vector<string> VS; const int INF = 1e9; const int MOD = 1e9 + 7; // const int MOD = 998244353; const ll LINF = 1e18; const ll dw[] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll dh[] = {0, 1, 1, 1, 0, -1, -1, -1}; #define PI 3.141592653589793238 #define EPS 1e-7 // 1000000007 で割ったあまりを扱う構造体 template <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) v += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; val = (val + MOD) % MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; val = (val + MOD) % MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = (val * u % MOD) % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept { return is >> x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; using mint = Fp<MOD>; const ll NMAX = 1e6; ll fac[NMAX + 1], inv[NMAX + 1], finv[NMAX + 1]; void cominit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; FOR(i, 2, NMAX + 1) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll comb(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } signed main() { LCIN(N, K); VL A(N); VECCIN(A); sort(rAll(A)); cominit(); mint ans = 0; REP(i, N) { ans += (comb(i, K - 1) % MOD * A[N - 1 - i] % MOD) % MOD; } REP(i, N) { ans -= (comb(N - 1 - i, K - 1) % MOD * A[N - 1 - i] % MOD) % MOD; } cout << ans << "\n"; }
replace
139
140
139
140
-8
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; struct combination { combination(ll Nmax, ll mod) { (this->MOD) = mod; (this->Nmax) = Nmax; factorial.resize(Nmax + 1, 1); inverse_factorial.resize(Nmax + 1, 1); init_factorial(); } ll calc(ll n, ll r) { if (r < 0 || r > n) return 0; ll ret = (factorial[n] * inverse_factorial[r]) % MOD; ret = (ret * inverse_factorial[n - r]) % MOD; return ret; } private: ll MOD, Nmax; vector<ll> factorial; vector<ll> inverse_factorial; ll mod_pow(ll n, ll p, ll mod) { n %= mod; if (p == 0) return 1; ll res = mod_pow(n * n % mod, p / 2, mod); if (p % 2 == 1) res = res * n % mod; return res; } void init_factorial() { for (ll i = 1; i <= Nmax; i++) { factorial[i] = (factorial[i - 1] * i) % MOD; } inverse_factorial[Nmax] = mod_pow(factorial[Nmax], MOD - 2, MOD); for (ll i = Nmax - 1; i >= 1; i--) { inverse_factorial[i] = (inverse_factorial[i + 1] * (i + 1)) % MOD; } } }; int main() { int N, K; cin >> N >> K; vector<ll> A(N); for (ll &a : A) { cin >> a; } sort(A.begin(), A.end()); ll mod = 1e9 + 7; combination comb(N, mod); ll ans = 0; for (int i = 0; i + K - 1 < N; i++) { ans -= A[i] * comb.calc(N - 1 - i, K - 1); while (ans < 0) { ans += mod; } ans %= mod; } for (int i = K - 1; i < N; i++) { ans += A[i] * comb.calc(i, K - 1); ans %= mod; } cout << ans << endl; }
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; struct combination { combination(ll Nmax, ll mod) { (this->MOD) = mod; (this->Nmax) = Nmax; factorial.resize(Nmax + 1, 1); inverse_factorial.resize(Nmax + 1, 1); init_factorial(); } ll calc(ll n, ll r) { if (r < 0 || r > n) return 0; ll ret = (factorial[n] * inverse_factorial[r]) % MOD; ret = (ret * inverse_factorial[n - r]) % MOD; return ret; } private: ll MOD, Nmax; vector<ll> factorial; vector<ll> inverse_factorial; ll mod_pow(ll n, ll p, ll mod) { n %= mod; if (p == 0) return 1; ll res = mod_pow(n * n % mod, p / 2, mod); if (p % 2 == 1) res = res * n % mod; return res; } void init_factorial() { for (ll i = 1; i <= Nmax; i++) { factorial[i] = (factorial[i - 1] * i) % MOD; } inverse_factorial[Nmax] = mod_pow(factorial[Nmax], MOD - 2, MOD); for (ll i = Nmax - 1; i >= 1; i--) { inverse_factorial[i] = (inverse_factorial[i + 1] * (i + 1)) % MOD; } } }; int main() { int N, K; cin >> N >> K; vector<ll> A(N); for (ll &a : A) { cin >> a; } sort(A.begin(), A.end()); ll mod = 1e9 + 7; combination comb(N, mod); ll ans = 0; for (int i = 0; i + K - 1 < N; i++) { ans -= (A[i] * comb.calc(N - 1 - i, K - 1)) % mod; while (ans < 0) { ans += mod; } ans %= mod; } for (int i = K - 1; i < N; i++) { ans += A[i] * comb.calc(i, K - 1); ans %= mod; } cout << ans << endl; }
replace
60
61
60
61
TLE
p02804
C++
Runtime Error
#define _USE_MATH_DEFINES #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #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; #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } typedef long long int64; typedef pair<int, int> ii; const int INF = 1 << 29; const int MOD = 1e9 + 7; mt19937 mrand(random_device{}()); int rnd(int x) { return mrand() % x; } int64 power_mod(int64 a, int n) { int64 ret = 1; for (; n; n >>= 1) { if (n & 1) ret = ret * a % MOD; a = a * a % MOD; } return ret; } const int N = 1e5 + 10; int F[N], G[N]; int64 comb(int n, int m) { return (int64)F[n] * G[m] % MOD * G[n - m] % MOD; } int main() { int n, m; scanf("%d%d", &n, &m); F[0] = G[0] = 1; for (int i = 1; i <= n; ++i) { F[i] = (int64)F[i - 1] * i % MOD; G[i] = power_mod(F[i], MOD - 2); } vector<int> a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } sort(a.begin(), a.end()); int ret = 0; for (int i = 1; i < n; ++i) { int L = i, R = n - i; int64 cur = comb(n, m); cur = (cur + MOD - comb(R, m)) % MOD; cur = (cur + MOD - comb(L, m)) % MOD; // trace(i, cur, a[i] - a[i - 1]); ret += cur * (a[i] - a[i - 1]) % MOD; ret %= MOD; } printf("%d\n", ret); return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #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; #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } typedef long long int64; typedef pair<int, int> ii; const int INF = 1 << 29; const int MOD = 1e9 + 7; mt19937 mrand(random_device{}()); int rnd(int x) { return mrand() % x; } int64 power_mod(int64 a, int n) { int64 ret = 1; for (; n; n >>= 1) { if (n & 1) ret = ret * a % MOD; a = a * a % MOD; } return ret; } const int N = 1e5 + 10; int F[N], G[N]; int64 comb(int n, int m) { if (m < 0 || m > n) return 0; return (int64)F[n] * G[m] % MOD * G[n - m] % MOD; } int main() { int n, m; scanf("%d%d", &n, &m); F[0] = G[0] = 1; for (int i = 1; i <= n; ++i) { F[i] = (int64)F[i - 1] * i % MOD; G[i] = power_mod(F[i], MOD - 2); } vector<int> a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } sort(a.begin(), a.end()); int ret = 0; for (int i = 1; i < n; ++i) { int L = i, R = n - i; int64 cur = comb(n, m); cur = (cur + MOD - comb(R, m)) % MOD; cur = (cur + MOD - comb(L, m)) % MOD; // trace(i, cur, a[i] - a[i - 1]); ret += cur * (a[i] - a[i - 1]) % MOD; ret %= MOD; } printf("%d\n", ret); return 0; }
replace
57
58
57
62
0
p02804
C++
Runtime Error
/*input 10 6 1000000000 1000000000 1000000000 1000000000 1000000000 0 0 0 0 0 */ #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #pragma GCC optimize("unroll-loops,no-stack-protector") // order_of_key #of elements less than x // find_by_order kth element using ll = long long; using ld = long double; using pii = pair<int, int>; #define f first #define s second #define pb push_back #define REP(i, n) for (int i = 0; i < n; i++) #define REP1(i, n) for (int i = 1; i <= n; i++) #define FILL(n, x) memset(n, x, sizeof(n)) #define ALL(_a) _a.begin(), _a.end() #define sz(x) (int)x.size() #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) const ll maxn = 20 + 5; const ll maxlg = __lg(maxn) + 2; const ll INF64 = 4e18; const int INF = 0x3f3f3f3f; const ll MOD = 1e9 + 7; const ld PI = acos(-1); const ld eps = 1e-9; #define lowb(x) x & (-x) #define MNTO(x, y) x = min(x, (__typeof__(x))y) #define MXTO(x, y) x = max(x, (__typeof__(x))y) ll sub(ll a, ll b) { ll x = a - b; while (x < 0) x += MOD; while (x > MOD) x -= MOD; return x; } ll mult(ll a, ll b) { return (a * b) % MOD; } ll mypow(ll a, ll b) { if (b <= 0) return 1; ll res = 1LL; while (b) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; b >>= 1; } return res; } ll fac[maxn]; ll arr[maxn]; int main() { int n, k; cin >> n >> k; fac[0] = 1; REP1(i, maxn - 1) fac[i] = (fac[i - 1] * (ll)i) % MOD; REP(i, n) cin >> arr[i]; sort(arr, arr + n); ll big = 0, small = 0; REP(i, n) { ll x = arr[i]; if (i >= k - 1) big += mult(x, mult(mult(fac[i], mypow(fac[k - 1], MOD - 2)), mypow(fac[i - k + 1], MOD - 2))); if (i <= n - k) small += mult(x, mult(mult(fac[n - i - 1], mypow(fac[k - 1], MOD - 2)), mypow(fac[n - i - 1 - k + 1], MOD - 2))); } cout << sub(big, small); }
/*input 10 6 1000000000 1000000000 1000000000 1000000000 1000000000 0 0 0 0 0 */ #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #pragma GCC optimize("unroll-loops,no-stack-protector") // order_of_key #of elements less than x // find_by_order kth element using ll = long long; using ld = long double; using pii = pair<int, int>; #define f first #define s second #define pb push_back #define REP(i, n) for (int i = 0; i < n; i++) #define REP1(i, n) for (int i = 1; i <= n; i++) #define FILL(n, x) memset(n, x, sizeof(n)) #define ALL(_a) _a.begin(), _a.end() #define sz(x) (int)x.size() #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) const ll maxn = 1e5 + 5; const ll maxlg = __lg(maxn) + 2; const ll INF64 = 4e18; const int INF = 0x3f3f3f3f; const ll MOD = 1e9 + 7; const ld PI = acos(-1); const ld eps = 1e-9; #define lowb(x) x & (-x) #define MNTO(x, y) x = min(x, (__typeof__(x))y) #define MXTO(x, y) x = max(x, (__typeof__(x))y) ll sub(ll a, ll b) { ll x = a - b; while (x < 0) x += MOD; while (x > MOD) x -= MOD; return x; } ll mult(ll a, ll b) { return (a * b) % MOD; } ll mypow(ll a, ll b) { if (b <= 0) return 1; ll res = 1LL; while (b) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; b >>= 1; } return res; } ll fac[maxn]; ll arr[maxn]; int main() { int n, k; cin >> n >> k; fac[0] = 1; REP1(i, maxn - 1) fac[i] = (fac[i - 1] * (ll)i) % MOD; REP(i, n) cin >> arr[i]; sort(arr, arr + n); ll big = 0, small = 0; REP(i, n) { ll x = arr[i]; if (i >= k - 1) big += mult(x, mult(mult(fac[i], mypow(fac[k - 1], MOD - 2)), mypow(fac[i - k + 1], MOD - 2))); if (i <= n - k) small += mult(x, mult(mult(fac[n - i - 1], mypow(fac[k - 1], MOD - 2)), mypow(fac[n - i - 1 - k + 1], MOD - 2))); } cout << sub(big, small); }
replace
29
30
29
30
0
p02804
C++
Runtime Error
/** * author : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏 * created : 2020-01-12 20:04:06 **/ #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using int64 = long long; template <class T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) { os << '{'; size_t n = vec.size(); for (size_t i = 0; i < n; ++i) { os << vec[i]; if (i != n - 1) os << ','; } os << '}'; return os; } template <class T, class U> std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) { return os << '{' << p.first << " " << p.second << '}'; } template <class T> std::istream &operator>>(std::istream &is, std::vector<T> &vec) { size_t n = vec.size(); for (size_t i = 0; i < n; ++i) is >> vec[i]; return is; } #ifdef LOCAL #define debug(_) cerr << #_ << ": " << (_) << '\n' #else #define debug(_) 1728 #endif class Combination { public: constexpr Combination() { Build(); } static constexpr int mod_ = 1000000007; static constexpr int n_ = 101; struct LookupTable { int64 factorial_[n_]; int64 inverse_[n_]; int64 finverse_[n_]; }; LookupTable lt{}; // return nCk constexpr int64 Get(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return lt.factorial_[n] * (lt.finverse_[k] * lt.finverse_[n - k] % mod_) % mod_; } private: constexpr void Build() noexcept { lt.factorial_[0] = 1; lt.factorial_[1] = 1; lt.finverse_[0] = 1; lt.finverse_[1] = 1; lt.inverse_[1] = 1; for (int i = 2; i < n_; ++i) { lt.factorial_[i] = lt.factorial_[i - 1] * i % mod_; lt.inverse_[i] = mod_ - lt.inverse_[mod_ % i] * (mod_ / i) % mod_; lt.finverse_[i] = lt.finverse_[i - 1] * lt.inverse_[i] % mod_; } } } kyomu; constexpr int64 MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<int64> a(n); cin >> a; sort(a.begin(), a.end()); int64 ans = 0LL; for (int i = 0; i < n; ++i) { ans += a[n - 1 - i] * kyomu.Get(n - 1 - i, k - 1) % MOD; ans %= MOD; } for (int i = 0; i < n; ++i) { ans -= (a[i] * kyomu.Get(n - 1 - i, k - 1) + MOD) % MOD; (ans += MOD) %= MOD; } cout << ans << endl; return 0; }
/** * author : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏 * created : 2020-01-12 20:04:06 **/ #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using int64 = long long; template <class T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) { os << '{'; size_t n = vec.size(); for (size_t i = 0; i < n; ++i) { os << vec[i]; if (i != n - 1) os << ','; } os << '}'; return os; } template <class T, class U> std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) { return os << '{' << p.first << " " << p.second << '}'; } template <class T> std::istream &operator>>(std::istream &is, std::vector<T> &vec) { size_t n = vec.size(); for (size_t i = 0; i < n; ++i) is >> vec[i]; return is; } #ifdef LOCAL #define debug(_) cerr << #_ << ": " << (_) << '\n' #else #define debug(_) 1728 #endif class Combination { public: constexpr Combination() { Build(); } static constexpr int mod_ = 1000000007; static constexpr int n_ = 101010; struct LookupTable { int64 factorial_[n_]; int64 inverse_[n_]; int64 finverse_[n_]; }; LookupTable lt{}; // return nCk constexpr int64 Get(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return lt.factorial_[n] * (lt.finverse_[k] * lt.finverse_[n - k] % mod_) % mod_; } private: constexpr void Build() noexcept { lt.factorial_[0] = 1; lt.factorial_[1] = 1; lt.finverse_[0] = 1; lt.finverse_[1] = 1; lt.inverse_[1] = 1; for (int i = 2; i < n_; ++i) { lt.factorial_[i] = lt.factorial_[i - 1] * i % mod_; lt.inverse_[i] = mod_ - lt.inverse_[mod_ % i] * (mod_ / i) % mod_; lt.finverse_[i] = lt.finverse_[i - 1] * lt.inverse_[i] % mod_; } } } kyomu; constexpr int64 MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<int64> a(n); cin >> a; sort(a.begin(), a.end()); int64 ans = 0LL; for (int i = 0; i < n; ++i) { ans += a[n - 1 - i] * kyomu.Get(n - 1 - i, k - 1) % MOD; ans %= MOD; } for (int i = 0; i < n; ++i) { ans -= (a[i] * kyomu.Get(n - 1 - i, k - 1) + MOD) % MOD; (ans += MOD) %= MOD; } cout << ans << endl; return 0; }
replace
63
64
63
64
0
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main() { COMinit(); ll n, k; cin >> n >> k; // ll MOD = 1000000007 vector<ll> A(n); for (ll i = 0; i < n; i++) { cin >> A.at(i); } vector<ll> fact(n + 1); ll ans = 0; sort(A.begin(), A.end()); if (k == 1) { cout << 0 << endl; return 0; } for (ll i = k - 1; i < n; i++) { // cout << i << endl; ll MAX = A.at(i); for (ll j = 0; k <= i - j + 1; j++) { // cout << j << endl; ll MIN = A.at(j); ans = ans % MOD; ans += ((MAX - MIN) % MOD) * COM(i - j - 1, k - 2) % MOD; } } ans = ans % MOD; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main() { COMinit(); ll n, k; cin >> n >> k; // ll MOD = 1000000007 vector<ll> A(n); for (ll i = 0; i < n; i++) { cin >> A.at(i); } vector<ll> fact(n + 1); ll ans = 0; sort(A.begin(), A.end()); if (k == 1) { cout << 0 << endl; return 0; } for (ll i = k - 1; i < n; i++) { ans += A.at(i) * COM(i, k - 1) % MOD; } for (ll i = k - 1; i < n; i++) { ans -= A.at(n - i - 1) * COM(i, k - 1) % MOD; } ans = ans % MOD; cout << ans << endl; }
replace
62
70
62
68
TLE
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #ifdef ZERO_IQ ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #endif long long const mod = 1e9 + 7; vector<ll> fact(1e5 + 1), inv(1e5 + 1); ll fp(ll base, ll exp) { if (exp == 0) return 1; ll ans = fp(base, exp / 2); ans = (ans * ans) % mod; if (exp % 2 != 0) ans = (ans * (base % mod)) % mod; return ans; } void calcFacAndInv(ll n) { fact[0] = inv[0] = 1; for (ll i = 1; i <= n; i++) { fact[i] = (i * fact[i - 1]) % mod; inv[i] = fp(fact[i], mod - 2); } } ll ncr(ll n, ll r) { return ((fact[n] * inv[r]) % mod * inv[n - r]) % mod; } int main() { ll n, m; cin >> n >> m; vector<ll> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } calcFacAndInv(1e5 + 1); sort(v.begin(), v.end()); ll ans = 0; for (int j = 0; j < n; ++j) { if (j < m - 1 && n - j - 1 >= m - 1) { ans = ((ans % mod) + (((v[j] % mod) * -ncr(n - j - 1, m - 1)) % mod) % mod) % mod; } else if (n - j - 1 < m - 1 && j >= m - 1) { ans = ((ans % mod) + (((v[j] % mod) * ncr(j, m - 1)) % mod) % mod) % mod; } else ans = ((ans % mod) + ((((v[j] % mod) * (ncr(j, m - 1) - ncr(n - j - 1, m - 1)) % mod) % mod) % mod)) % mod; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #ifdef ZERO_IQ ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #endif long long const mod = 1e9 + 7; vector<ll> fact(1e5 + 1), inv(1e5 + 1); ll fp(ll base, ll exp) { if (exp == 0) return 1; ll ans = fp(base, exp / 2); ans = (ans * ans) % mod; if (exp % 2 != 0) ans = (ans * (base % mod)) % mod; return ans; } void calcFacAndInv(ll n) { fact[0] = inv[0] = 1; for (ll i = 1; i <= n; i++) { fact[i] = (i * fact[i - 1]) % mod; inv[i] = fp(fact[i], mod - 2); } } ll ncr(ll n, ll r) { return ((fact[n] * inv[r]) % mod * inv[n - r]) % mod; } int main() { ll n, m; cin >> n >> m; vector<ll> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } calcFacAndInv(1e5 + 1); sort(v.begin(), v.end()); ll ans = 0; for (int j = 0; j < n; ++j) { if (j < m - 1 && n - j - 1 >= m - 1) { ans = ((ans % mod) + (((v[j] % mod) * -ncr(n - j - 1, m - 1)) % mod) % mod) % mod; } else if (n - j - 1 < m - 1 && j >= m - 1) { ans = ((ans % mod) + (((v[j] % mod) * ncr(j, m - 1)) % mod) % mod) % mod; } else if (n - j - 1 >= m - 1 && j >= m - 1) ans = ((ans % mod) + ((((v[j] % mod) * (ncr(j, m - 1) - ncr(n - j - 1, m - 1)) % mod) % mod) % mod)) % mod; } cout << ans; return 0; }
replace
50
51
50
51
0
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, j, k) for (int i = (int)j; i <= (int)k; i++) #define debug(x) cerr << #x << ":" << x << endl const int maxn = (int)1e6 + 5; int a[maxn]; typedef long long ll; ll mod = 1e9 + 7; // 1e9+9 ll add(ll x, ll y) { return (x + y) % mod; } ll mul(ll x, ll y) { return (x * y) % mod; } ll mpow(ll x, ll y) { ll v = 1; for (; y; x = mul(x, x), y >>= 1) if (y & 1) v = mul(v, x); return v; } ll divi(ll x, ll y) { return mul(x, mpow(y, mod - 2)); } ll fact[10000000] = {1}, factc = 0; ll Fact(ll x) { while (factc < x) { factc++; fact[factc] = mul(fact[factc - 1], factc); } return fact[x]; } ll nCr(ll n, ll r) { return divi(Fact(n), mul(Fact(n - r), Fact(r))); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); debug(nCr(1, 2)); int n, k; cin >> n >> k; rep(i, 1, n) cin >> a[i]; sort(a + 1, a + 1 + n); ll mx = 0, mn = 0; ; rep(i, 1, n) { mx += a[i] * nCr(i - 1, k - 1); mn += a[n - i + 1] * nCr(i - 1, k - 1); mx = (mx % mod + mod) % mod; mn = (mn % mod + mod) % mod; // debug(mx),debug(mn); } cout << ((mx - mn) % mod + mod) % mod << endl; } /* */
#include <bits/stdc++.h> using namespace std; #define rep(i, j, k) for (int i = (int)j; i <= (int)k; i++) #define debug(x) cerr << #x << ":" << x << endl const int maxn = (int)1e6 + 5; int a[maxn]; typedef long long ll; ll mod = 1e9 + 7; // 1e9+9 ll add(ll x, ll y) { return (x + y) % mod; } ll mul(ll x, ll y) { return (x * y) % mod; } ll mpow(ll x, ll y) { ll v = 1; for (; y; x = mul(x, x), y >>= 1) if (y & 1) v = mul(v, x); return v; } ll divi(ll x, ll y) { return mul(x, mpow(y, mod - 2)); } ll fact[10000000] = {1}, factc = 0; ll Fact(ll x) { while (factc < x) { factc++; fact[factc] = mul(fact[factc - 1], factc); } return fact[x]; } ll nCr(ll n, ll r) { if (n < r) return 0; return divi(Fact(n), mul(Fact(n - r), Fact(r))); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); debug(nCr(1, 2)); int n, k; cin >> n >> k; rep(i, 1, n) cin >> a[i]; sort(a + 1, a + 1 + n); ll mx = 0, mn = 0; ; rep(i, 1, n) { mx += a[i] * nCr(i - 1, k - 1); mn += a[n - i + 1] * nCr(i - 1, k - 1); mx = (mx % mod + mod) % mod; mn = (mn % mod + mod) % mod; // debug(mx),debug(mn); } cout << ((mx - mn) % mod + mod) % mod << endl; } /* */
replace
31
32
31
36
0
nCr(1,2):0
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1e9 + 7; ll mod(ll &a) { return ((a %= MOD) > 0 ? a : a + MOD); } const int MAXN = 1e5 + 10; ll fact_[MAXN] = {0}; ll fact(ll n) { if (n == 1) return fact_[n] = 1; if (fact_[n] != 0) return fact_[n]; return fact_[n] = (n * fact(n - 1)) % MOD; } // aのb乗 ll powMod(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a % MOD; if (res == 0) res = MOD; a = a * a % MOD; if (a == 0) a = MOD; b >>= 1; } return res; } ll comb(ll a, ll b) { if (a == b || b == 0) return 1; return (((fact(a) * powMod(fact(b), MOD - 2)) % MOD) * powMod(fact(a - b), MOD - 2)) % MOD; } int main() { int N, K; cin >> N >> K; vector<ll> A(N); for (auto &a : A) cin >> a; sort(A.begin(), A.end()); ll ans = 0; for (ll n = 0; n <= N - K; n++) { ans += (A[N - 1 - n] - A[n]) * comb(N - 1 - n, K - 1); while (ans < 0) ans += MOD; ans %= MOD; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1e9 + 7; ll mod(ll &a) { return ((a %= MOD) > 0 ? a : a + MOD); } const int MAXN = 1e5 + 10; ll fact_[MAXN] = {0}; ll fact(ll n) { if (n == 1) return fact_[n] = 1; if (fact_[n] != 0) return fact_[n]; return fact_[n] = (n * fact(n - 1)) % MOD; } // aのb乗 ll powMod(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a % MOD; if (res == 0) res = MOD; a = a * a % MOD; if (a == 0) a = MOD; b >>= 1; } return res; } ll comb(ll a, ll b) { if (a == b || b == 0) return 1; return (((fact(a) * powMod(fact(b), MOD - 2)) % MOD) * powMod(fact(a - b), MOD - 2)) % MOD; } int main() { int N, K; cin >> N >> K; vector<ll> A(N); for (auto &a : A) cin >> a; sort(A.begin(), A.end()); ll ans = 0; for (ll n = 0; n <= N - K; n++) { ll plus = ((A[N - 1 - n] - A[n]) * comb(N - 1 - n, K - 1)) % MOD; if (plus < 0) plus += MOD; ans += plus; ans %= MOD; } cout << ans << endl; }
replace
51
54
51
55
TLE
p02804
C++
Time Limit Exceeded
#pragma region #define _USE_MATH_DEFINES #include <algorithm> #include <cmath> #include <cstdint> #include <cstdlib> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; // #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i)) #define rep(i, e) for (int(i) = 0; (i) < (e); ++(i)) #define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i)) #define all(x) x.begin(), x.end() #pragma endregion #pragma region Combination const int MAX = 1000000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } #pragma endregion int main() { int n, k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; sort(all(a)); COMinit(); ll res = 0; for (int i = k - 1; i < n; ++i) { res += a[i] * COM(i, k - 1); res %= MOD; } for (int i = 0; i + k - 1 < n; ++i) { res -= a[i] * COM(n - i - 1, k - 1); while (res < 0) res += MOD; } cout << res << endl; }
#pragma region #define _USE_MATH_DEFINES #include <algorithm> #include <cmath> #include <cstdint> #include <cstdlib> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; // #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i)) #define rep(i, e) for (int(i) = 0; (i) < (e); ++(i)) #define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i)) #define all(x) x.begin(), x.end() #pragma endregion #pragma region Combination const int MAX = 1000000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } #pragma endregion int main() { int n, k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; sort(all(a)); COMinit(); ll res = 0; for (int i = k - 1; i < n; ++i) { res += a[i] * COM(i, k - 1); res %= MOD; } for (int i = 0; i + k - 1 < n; ++i) { res -= a[i] * COM(n - i - 1, k - 1); if (res < 0) res -= (res - MOD + 1) / MOD * MOD; res %= MOD; } cout << res << endl; }
replace
66
68
66
69
TLE
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int A[100005]; const int mod = 1e9 + 7; int inv(int x) { int ans = 1; int n = mod - 2; while (n) { if (n & 1) { ans = (ans * x) % mod; } x = (x * x) % mod; n >>= 1; } return ans; } int f[100005]; void init(int n) { f[0] = 1; for (int i = 1; i <= n; i++) { f[i] = (f[i - 1] * i) % mod; } } int C(int n, int m) { return ((f[n] * inv(f[m])) % mod * inv(f[n - m])) % mod; } signed main() { int n, k; scanf("%lld%lld", &n, &k); init(n); for (int i = 1; i <= n; i++) { scanf("%lld", &A[i]); } sort(A + 1, A + n + 1); int a = 0, b = 0; for (int i = 1; i <= n; i++) { if (i >= k) b = (b + A[i]) % mod; if (i <= n - k + 1) { a = (a + A[i]) % mod; } } int x = k, y = n - k + 1, ans = 0; for (int i = 0; i < n; i++) { ans = (ans + ((b - a + mod) % mod * C(k + i - 2, k - 2)) % mod) % mod; a = (a - A[y--] + mod) % mod; b = (b - A[x++] + mod) % mod; } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; #define int long long int A[100005]; const int mod = 1e9 + 7; int inv(int x) { int ans = 1; int n = mod - 2; while (n) { if (n & 1) { ans = (ans * x) % mod; } x = (x * x) % mod; n >>= 1; } return ans; } int f[100005]; void init(int n) { f[0] = 1; for (int i = 1; i <= n; i++) { f[i] = (f[i - 1] * i) % mod; } } int C(int n, int m) { return ((f[n] * inv(f[m])) % mod * inv(f[n - m])) % mod; } signed main() { int n, k; scanf("%lld%lld", &n, &k); init(n); for (int i = 1; i <= n; i++) { scanf("%lld", &A[i]); } sort(A + 1, A + n + 1); int a = 0, b = 0; for (int i = 1; i <= n; i++) { if (i >= k) b = (b + A[i]) % mod; if (i <= n - k + 1) { a = (a + A[i]) % mod; } } int x = k, y = n - k + 1, ans = 0; for (int i = 0; i < n; i++) { ans = (ans + ((b - a + mod) % mod * C(k + i - 2, k - 2)) % mod) % mod; if (y > 0) a = (a - A[y--] + mod) % mod; if (x <= n) b = (b - A[x++] + mod) % mod; } cout << ans << '\n'; }
replace
47
49
47
51
0
p02804
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define endl '\n' #define Bedoazim \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); using namespace std; const long long mod = 1e9 + 7, N = 1e3 + 5; long long fact[N], inv[N]; long long ncr(long long n, long long r) { return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod; } int fastpower(int base, int power) { if (power == 0) return 1; long long halfpower = fastpower(base, power / 2) % mod; long long ret = (halfpower * halfpower) % mod; if (power % 2) { ret = (ret * base) % mod; } return ret; } void pre() { fact[0] = 1; inv[0] = 1; for (long long i = 1; i < N; i++) { fact[i] = ((i)*fact[i - 1]) % mod; inv[i] = fastpower(fact[i], mod - 2) % mod; } } int main() { Bedoazim pre(); long long n, k, max, min, ans = 0; vector<long long> v; cin >> n >> k; for (int i = 0; i < n; i++) { long long j; cin >> j; v.push_back(j); } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { if (i < k - 1) max = 0; else max = ncr(i, k - 1); if (n - i - 1 < k - 1) min = 0; else min = ncr(n - i - 1, k - 1); ans += ((v[i] % mod) * ((max - min) % mod) % mod); ans %= mod; } cout << ans << endl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define endl '\n' #define Bedoazim \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); using namespace std; const long long mod = 1e9 + 7, N = 1e6 + 5; long long fact[N], inv[N]; long long ncr(long long n, long long r) { return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod; } int fastpower(int base, int power) { if (power == 0) return 1; long long halfpower = fastpower(base, power / 2) % mod; long long ret = (halfpower * halfpower) % mod; if (power % 2) { ret = (ret * base) % mod; } return ret; } void pre() { fact[0] = 1; inv[0] = 1; for (long long i = 1; i < N; i++) { fact[i] = ((i)*fact[i - 1]) % mod; inv[i] = fastpower(fact[i], mod - 2) % mod; } } int main() { Bedoazim pre(); long long n, k, max, min, ans = 0; vector<long long> v; cin >> n >> k; for (int i = 0; i < n; i++) { long long j; cin >> j; v.push_back(j); } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { if (i < k - 1) max = 0; else max = ncr(i, k - 1); if (n - i - 1 < k - 1) min = 0; else min = ncr(n - i - 1, k - 1); ans += ((v[i] % mod) * ((max - min) % mod) % mod); ans %= mod; } cout << ans << endl; }
replace
18
19
18
19
0
p02804
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using ll = long long; using graph = std::vector<std::vector<ll>>; using namespace std; vector<ll> Factorial(ll M, ll MOD) { vector<ll> result = vector<ll>(M + 1); for (int i = 0; i <= M; i++) { if (i == 0) { result[i] = 1; } else { result[i] = result[i - 1] * i % MOD; } } return result; } vector<ll> InvFact(ll M, ll MOD) { vector<ll> result = vector<ll>(M + 1); vector<ll> inv = vector<ll>(M + 1); result[0] = 1; result[1] = 1; inv[1] = 1; for (int i = 2; i < M; i++) { inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; result[i] = result[i - 1] * inv[i] % MOD; } return result; } ll Combination(ll n, ll k, vector<ll> fact, vector<ll> invFact, ll MOD) { // nCkを計算する if (n < k) return 0; if (n < 0 || k < 0) return 0; return fact[n] * (invFact[k] * invFact[n - k] % MOD) % MOD; } int main() { ll N, K; cin >> N >> K; vector<ll> A(N); ll MOD = 1000000007; vector<ll> fact = Factorial(N, MOD); vector<ll> invFact = InvFact(N, MOD); for (ll i = 0; i < N; i++) { cin >> A[i]; } sort(A.begin(), A.end()); ll sum = 0; for (int i = 0; i < N; i++) { sum = (sum - A[i] * (Combination(N - (i + 1), K - 1, fact, invFact, MOD) - Combination(i, K - 1, fact, invFact, MOD))) % MOD; } cout << sum << endl; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using ll = long long; using graph = std::vector<std::vector<ll>>; using namespace std; vector<ll> Factorial(ll M, ll MOD) { vector<ll> result = vector<ll>(M + 1); for (int i = 0; i <= M; i++) { if (i == 0) { result[i] = 1; } else { result[i] = result[i - 1] * i % MOD; } } return result; } vector<ll> InvFact(ll M, ll MOD) { vector<ll> result = vector<ll>(M + 1); vector<ll> inv = vector<ll>(M + 1); result[0] = 1; result[1] = 1; inv[1] = 1; for (int i = 2; i < M; i++) { inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; result[i] = result[i - 1] * inv[i] % MOD; } return result; } ll Combination(ll n, ll k, vector<ll> &fact, vector<ll> &invFact, ll MOD) { // nCkを計算する if (n < k) return 0; if (n < 0 || k < 0) return 0; return fact[n] * (invFact[k] * invFact[n - k] % MOD) % MOD; } int main() { ll N, K; cin >> N >> K; vector<ll> A(N); ll MOD = 1000000007; vector<ll> fact = Factorial(N, MOD); vector<ll> invFact = InvFact(N, MOD); for (ll i = 0; i < N; i++) { cin >> A[i]; } sort(A.begin(), A.end()); ll sum = 0; for (int i = 0; i < N; i++) { sum = (sum - A[i] * (Combination(N - (i + 1), K - 1, fact, invFact, MOD) - Combination(i, K - 1, fact, invFact, MOD))) % MOD; } cout << sum << endl; }
replace
37
38
37
38
TLE
p02804
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // typedef tree<int, null_type, std::less<int>, rb_tree_tag, // tree_order_statistics_node_update> ordered_set; // Там еще некоторые особенности есть. Подробнее // https://codeforces.com/blog/entry/11080?locale=ru using namespace std; using ld = long double; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vvll = vector<vll>; #define endl '\n' #define all(x) (x).begin(), (x).end() #define len(x) (int)(x).size() #define FAST \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); const ll mod = (ll)1e9 + 7; ll binpowmod(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) { res *= a; res %= mod; } n >>= 1ll; a *= a; a %= mod; } return res; } inline ll inv(ll a) { return binpowmod(a, mod - 2); } void solve() { int n, k; cin >> n >> k; vll a(n); for (auto &i : a) { cin >> i; } sort(all(a)); vll facts(n + 1, 1); for (ll i = 2; i <= n; i++) { facts[i] = facts[i - 1] * i; facts[i] %= mod; } vll invfacts(n + 1, 1); for (ll i = 2; i <= n; i++) { invfacts[i] = inv(facts[i]); } vll C(n + 1, 1); for (int i = k - 2; i <= n; i++) { C[i] = ((facts[i] * invfacts[k - 2]) % mod * invfacts[i - (k - 2)]) % mod; } ll ans = 0; int step = 0; ll sum = 0; for (int i = k - 1; i < n; i++) { sum += C[k - 2 + step++]; sum %= mod; ll val = (a[i] + mod) % mod; ans = (ans + (val * sum) % mod) % mod; } step = 0; sum = 0; for (int i = n - k; i >= 0; --i) { sum -= C[k - 2 + step++]; sum = (sum + mod) % mod; ll val = (a[i] + mod) % mod; ans = (ans + (val * sum) % mod) % mod; } cout << ans; } int main() { int t = 1; for (int i = 0; i < t; i++) { solve(); cout << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // typedef tree<int, null_type, std::less<int>, rb_tree_tag, // tree_order_statistics_node_update> ordered_set; // Там еще некоторые особенности есть. Подробнее // https://codeforces.com/blog/entry/11080?locale=ru using namespace std; using ld = long double; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vvll = vector<vll>; #define endl '\n' #define all(x) (x).begin(), (x).end() #define len(x) (int)(x).size() #define FAST \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); const ll mod = (ll)1e9 + 7; ll binpowmod(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) { res *= a; res %= mod; } n >>= 1ll; a *= a; a %= mod; } return res; } inline ll inv(ll a) { return binpowmod(a, mod - 2); } void solve() { int n, k; cin >> n >> k; vll a(n); for (auto &i : a) { cin >> i; } if (k == 1) { cout << 0; return; } sort(all(a)); vll facts(n + 1, 1); for (ll i = 2; i <= n; i++) { facts[i] = facts[i - 1] * i; facts[i] %= mod; } vll invfacts(n + 1, 1); for (ll i = 2; i <= n; i++) { invfacts[i] = inv(facts[i]); } vll C(n + 1, 1); for (int i = k - 2; i <= n; i++) { C[i] = ((facts[i] * invfacts[k - 2]) % mod * invfacts[i - (k - 2)]) % mod; } ll ans = 0; int step = 0; ll sum = 0; for (int i = k - 1; i < n; i++) { sum += C[k - 2 + step++]; sum %= mod; ll val = (a[i] + mod) % mod; ans = (ans + (val * sum) % mod) % mod; } step = 0; sum = 0; for (int i = n - k; i >= 0; --i) { sum -= C[k - 2 + step++]; sum = (sum + mod) % mod; ll val = (a[i] + mod) % mod; ans = (ans + (val * sum) % mod) % mod; } cout << ans; } int main() { int t = 1; for (int i = 0; i < t; i++) { solve(); cout << endl; } return 0; }
insert
64
64
64
69
0
p02804
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; const int INF = 1 << 30; const ll LLINF = 1LL << 60; int mod = 1000000007; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } // これを使うときにはCOMinit();を忘れずに int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); COMinit(); int N, K; cin >> N >> K; ll A[N]; rep(i, N) cin >> A[i]; ll ans = 0; ll acc1 = 0, acc2 = 0; sort(A, A + N); // A[i](i >= K-1)がmaxとして採用される選び方はiC(K-1) //(K-1)C(K-1)*A[K-1] + (K)C(K-1)*A[K] + ... + (N-1)C(K-1)*A[N-1] ...(1) for (int i = K - 1; i <= N - 1; i++) { acc1 = (acc1 + (COM(i, K - 1) * A[i] % MOD)) % MOD; } reverse(A, A + N); // 再度(1)を計算し,引き算 答えが負なら+MOD for (int i = K - 1; i <= N - 1; i++) { acc2 = (acc2 + (COM(i, K - 1) * A[i] % MOD)) % MOD; assert(acc2 >= 0); } ans = (acc1 - acc2) % MOD; if (ans < 0) ans += MOD; assert(ans >= 0); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; const int INF = 1 << 30; const ll LLINF = 1LL << 60; int mod = 1000000007; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } // これを使うときにはCOMinit();を忘れずに int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); COMinit(); int N, K; cin >> N >> K; ll A[N]; rep(i, N) cin >> A[i]; ll ans = 0; ll acc1 = 0, acc2 = 0; sort(A, A + N); // A[i](i >= K-1)がmaxとして採用される選び方はiC(K-1) //(K-1)C(K-1)*A[K-1] + (K)C(K-1)*A[K] + ... + (N-1)C(K-1)*A[N-1] ...(1) for (int i = K - 1; i <= N - 1; i++) { acc1 = (acc1 + (COM(i, K - 1) * A[i] % MOD)) % MOD; } reverse(A, A + N); // 再度(1)を計算し,引き算 答えが負なら+MOD for (int i = K - 1; i <= N - 1; i++) { acc2 = (acc2 + (COM(i, K - 1) * A[i] % MOD)) % MOD; } ans = (acc1 - acc2) % MOD; if (ans < 0) ans += MOD; assert(ans >= 0); cout << ans << endl; return 0; }
delete
60
61
60
60
0
p02804
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #pragma comment(linker, "/STACK:256000000") using namespace std; #define in(s) freopen(s, "r", stdin) #define out(s) freopen(s, "w", stdout) #define forn(i, n) for (int i = 0; i < n; i++) #define endl '\n' #define mp make_pair typedef long long ll; typedef long double ld; const int MAXN = (int)1e5 + 5; const ll mod = (ll)1e+9 + 7ll; ll fact[MAXN]; inline ll bin_pow(ll a, ll n) { ll u = a, res = 1ll; while (n) { if (n % 2ll == 1ll) res = (res * u) % mod; u = (u * u) % mod; n /= 2ll; } return res; } inline ll div_mod(ll a, ll b) { return a * bin_pow(b, mod - 2ll) % mod; } inline ll C(int n, int k) { return div_mod(fact[n], fact[n - k] * fact[k] % mod); } int main() { // in("input.txt"); // out("output.txt"); srand(1373737); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); fact[0] = 1ll; for (int i = 1; i < MAXN; i++) fact[i] = (fact[i - 1] * (ll)i) % mod; int n, k; cin >> n >> k; vector<ll> arr(n); forn(i, n) cin >> arr[i]; sort(arr.begin(), arr.end()); ll ans = 0ll; forn(i, n - 1) { int left = i + 1, right = n - i - 1; ll all = C(left + right, k); ll bad = (C(left, k) + C(right, k)) % mod; ll good = (all + mod - bad) % mod; ans = (ans + (arr[i + 1] - arr[i]) * good % mod) % mod; } cout << ans; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #pragma comment(linker, "/STACK:256000000") using namespace std; #define in(s) freopen(s, "r", stdin) #define out(s) freopen(s, "w", stdout) #define forn(i, n) for (int i = 0; i < n; i++) #define endl '\n' #define mp make_pair typedef long long ll; typedef long double ld; const int MAXN = (int)1e5 + 5; const ll mod = (ll)1e+9 + 7ll; ll fact[MAXN]; inline ll bin_pow(ll a, ll n) { ll u = a, res = 1ll; while (n) { if (n % 2ll == 1ll) res = (res * u) % mod; u = (u * u) % mod; n /= 2ll; } return res; } inline ll div_mod(ll a, ll b) { return a * bin_pow(b, mod - 2ll) % mod; } inline ll C(int n, int k) { if (n < k) return 0ll; return div_mod(fact[n], fact[n - k] * fact[k] % mod); } int main() { // in("input.txt"); // out("output.txt"); srand(1373737); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); fact[0] = 1ll; for (int i = 1; i < MAXN; i++) fact[i] = (fact[i - 1] * (ll)i) % mod; int n, k; cin >> n >> k; vector<ll> arr(n); forn(i, n) cin >> arr[i]; sort(arr.begin(), arr.end()); ll ans = 0ll; forn(i, n - 1) { int left = i + 1, right = n - i - 1; ll all = C(left + right, k); ll bad = (C(left, k) + C(right, k)) % mod; ll good = (all + mod - bad) % mod; ans = (ans + (arr[i + 1] - arr[i]) * good % mod) % mod; } cout << ans; return 0; }
insert
48
48
48
50
0
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define VRSORT(v) sort(v.begin(), v.end(), greater<int>()); #define INF 999999999 #define MOD 1000000007 #define M_PI 3.14159265358979323846 #define ALL(X) (X).begin(), (X).end() #ifdef __LOCAL #define DBG(X) cout << #X << " = " << (X) << endl; #define SAY(X) cout << (X) << endl; #else #define DBG(X) #define SAY(X) #endif #ifdef __LOCAL #include <filesystem> namespace fs = std::filesystem; #endif using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; static mt19937 _g(time(nullptr)); std::string pad(int num) { char buffer[4]; std::snprintf(buffer, sizeof(buffer), "%03d", num); return buffer; } inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); } inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }; template <typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); } template <typename T, typename S> inline ostream &operator<<(ostream &os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline T fetch() { T ret; cin >> ret; return ret; } template <typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto &elem : ret) cin >> elem; return ret; } template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; const ll mod = (ll)1e9 + 7; using modint = ModInt<mod>; template <typename T> struct Combination { vector<T> _fact, _rfact, _inv; Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) { _fact[0] = _rfact[sz] = _inv[0] = 1; for (int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i; _rfact[sz] /= _fact[sz]; for (int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1); for (int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1]; } inline T fact(int k) const { return _fact[k]; } inline T rfact(int k) const { return _rfact[k]; } inline T inv(int k) const { return _inv[k]; } T P(int n, int r) const { if (r < 0 || n < r) return 0; return fact(n) * rfact(n - r); } T C(int p, int q) const { if (q < 0 || p < q) return 0; return fact(p) * rfact(q) * rfact(p - q); } T H(int n, int r) const { if (n < 0 || r < 0) return (0); return r == 0 ? 1 : C(n + r - 1, r); } }; int N, K; vector<ll> A; void input() { #ifdef __LOCAL fs::path p = __FILE__; fs::path input, output; input = output = p.parent_path(); input += string("/input/") + string(p.stem()) + string(".txt"); output += string("/output/") + string(p.stem()) + string(".txt"); freopen(input.c_str(), "r", stdin); freopen(output.c_str(), "w", stdout); #endif fast_io(); cin >> N >> K; A = fetch_vec<ll>(N); VSORT(A); } int solve() { modint ans = 0; Combination<modint> combi(N); if (K == 1) { cout << 0 << endl; return 0; } for (int i = 0; i < N; i++) { for (int j = i + K - 1; j < N; j++) { // if(j-i<K-1) continue; ans += modint(A[j] - A[i]) * combi.C(j - i - 1, K - 2); } } cout << ans << endl; return 0; } int main() { input(); solve(); return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define VRSORT(v) sort(v.begin(), v.end(), greater<int>()); #define INF 999999999 #define MOD 1000000007 #define M_PI 3.14159265358979323846 #define ALL(X) (X).begin(), (X).end() #ifdef __LOCAL #define DBG(X) cout << #X << " = " << (X) << endl; #define SAY(X) cout << (X) << endl; #else #define DBG(X) #define SAY(X) #endif #ifdef __LOCAL #include <filesystem> namespace fs = std::filesystem; #endif using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; static mt19937 _g(time(nullptr)); std::string pad(int num) { char buffer[4]; std::snprintf(buffer, sizeof(buffer), "%03d", num); return buffer; } inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); } inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }; template <typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); } template <typename T, typename S> inline ostream &operator<<(ostream &os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline T fetch() { T ret; cin >> ret; return ret; } template <typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto &elem : ret) cin >> elem; return ret; } template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; const ll mod = (ll)1e9 + 7; using modint = ModInt<mod>; template <typename T> struct Combination { vector<T> _fact, _rfact, _inv; Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) { _fact[0] = _rfact[sz] = _inv[0] = 1; for (int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i; _rfact[sz] /= _fact[sz]; for (int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1); for (int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1]; } inline T fact(int k) const { return _fact[k]; } inline T rfact(int k) const { return _rfact[k]; } inline T inv(int k) const { return _inv[k]; } T P(int n, int r) const { if (r < 0 || n < r) return 0; return fact(n) * rfact(n - r); } T C(int p, int q) const { if (q < 0 || p < q) return 0; return fact(p) * rfact(q) * rfact(p - q); } T H(int n, int r) const { if (n < 0 || r < 0) return (0); return r == 0 ? 1 : C(n + r - 1, r); } }; int N, K; vector<ll> A; void input() { #ifdef __LOCAL fs::path p = __FILE__; fs::path input, output; input = output = p.parent_path(); input += string("/input/") + string(p.stem()) + string(".txt"); output += string("/output/") + string(p.stem()) + string(".txt"); freopen(input.c_str(), "r", stdin); freopen(output.c_str(), "w", stdout); #endif fast_io(); cin >> N >> K; A = fetch_vec<ll>(N); VSORT(A); } int solve() { modint ans = 0; Combination<modint> combi(N); if (K == 1) { cout << 0 << endl; return 0; } for (int i = 0; i < N; i++) { modint maxS = modint(A[i]) * combi.C(i, K - 1); modint minS = modint(A[i]) * combi.C(N - i - 1, K - 1); ans += (maxS - minS); } cout << ans << endl; return 0; } int main() { input(); solve(); return 0; }
replace
218
222
218
221
TLE
p02804
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; const long long int MOD = 1e9 + 7; long long int modinv(long long int a, long long int m) { long long int b = m, u = 1, v = 0; while (b) { long long int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } long long int conv(long long int i, long long int j) { long long int tmp = min(i - j, j); long long int cnt = 1; long long int rs = 1; while (cnt <= tmp) { rs *= (i + 1 - cnt); rs %= MOD; rs *= modinv(cnt, MOD); rs %= MOD; cnt++; } return rs; } int main() { int N, K; cin >> N >> K; vector<long long int> A(N); vector<long long int> C(N + 1, 0); for (int n = 0; n < N; n++) { cin >> A[n]; } sort(A.begin(), A.end()); C[K - 1] = 1; for (int n = 1; n < N; n++) { C[K - 1 + n] = C[K - 1 + n - 1] * (K - 1 + n); C[K - 1 + n] %= MOD; C[K - 1 + n] *= modinv(n, MOD); C[K - 1 + n] %= MOD; } long long int ans = 0; for (int n = 0; n < N; n++) { // cout << n << ":" << endl; if (n - (K - 2) > 0) { // cout << n << "C" << K-1 << endl; ans += C[n] * A[n]; ans %= MOD; } if (N - 1 - n - (K - 2) > 0) { // cout << N-1-n << "C" << K-1 << endl; ans -= C[N - 1 - n] * A[n]; ans %= MOD; } } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; const long long int MOD = 1e9 + 7; long long int modinv(long long int a, long long int m) { long long int b = m, u = 1, v = 0; while (b) { long long int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } long long int conv(long long int i, long long int j) { long long int tmp = min(i - j, j); long long int cnt = 1; long long int rs = 1; while (cnt <= tmp) { rs *= (i + 1 - cnt); rs %= MOD; rs *= modinv(cnt, MOD); rs %= MOD; cnt++; } return rs; } int main() { int N, K; cin >> N >> K; vector<long long int> A(N); vector<long long int> C(N + K, 0); for (int n = 0; n < N; n++) { cin >> A[n]; } sort(A.begin(), A.end()); C[K - 1] = 1; for (int n = 1; n < N; n++) { C[K - 1 + n] = C[K - 1 + n - 1] * (K - 1 + n); C[K - 1 + n] %= MOD; C[K - 1 + n] *= modinv(n, MOD); C[K - 1 + n] %= MOD; } long long int ans = 0; for (int n = 0; n < N; n++) { // cout << n << ":" << endl; if (n - (K - 2) > 0) { // cout << n << "C" << K-1 << endl; ans += C[n] * A[n]; ans %= MOD; } if (N - 1 - n - (K - 2) > 0) { // cout << N-1-n << "C" << K-1 << endl; ans -= C[N - 1 - n] * A[n]; ans %= MOD; } } cout << ans << endl; return 0; }
replace
44
45
44
45
0
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (long long i = 0; i < (n); i++) #define REP1(i, n) for (long long i = 1; i <= (n); i++) #define REP2D(i, j, h, w) \ for (long long i = 0; i < (h); i++) \ for (long long j = 0; j < (w); j++) #define REP2D1(i, j, h, w) \ for (long long i = 1; i <= (h); i++) \ for (long long j = 1; j <= (w); j++) #define PER(i, n) for (long long i = ((n)-1); i >= 0; i--) #define PER1(i, n) for (long long i = (n); i > 0; i--) #define FOR(i, a, b) for (long long i = (a); i < (b); i++) #define FORE(i, a, b) for (long long i = (a); i <= (b); i++) #define ITE(arr) for (auto ite = (arr).begin(); ite != (arr).end(); ++ite) #define ALL(a) ((a).begin()), ((a).end()) #define RANGE(a) (a), ((a) + sizeof(a)) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define YNPRT(b) cout << ((b) ? "Yes" : "No") << endl #define ENTER printf("\n") #define REV(arr) reverse(ALL(arr)) #define PRT(a) cout << (a) << endl #ifdef DEBUG #define DBPRT(a) cout << "[Debug] - " << #a << " : " << (a) << endl #define DBSTART if (1) { #define DBEND } #define PRTLST(arr, num) REP(_i, num) cout << _i << " - " << arr[_i] << endl; #define PRTLST2(arr2, d1, d2) \ REP(_i, d1) \ REP(_j, d2) cout << _i << "," << _j << " : " << arr2[_i][_j] << endl; #define PRTLST2D(arr2, d1, d2) \ do { \ cout << "L\t"; \ REP(_i, d2) cout << _i << "\t"; \ cout << endl; \ REP(_i, d1) { \ cout << _i << "\t"; \ REP(_j, d2) { cout << arr2[_i][_j] << "\t"; } \ cout << endl; \ } \ } while (0); #else #define DBPRT(a) \ if (0) { \ } #define DBSTART if (0) { #define DBEND } #define PRTLST(arr, num) \ if (0) { \ } #define PRTLST2(arr2, d1, d2) \ if (0) { \ } #define PRTLST2D(arr2, d1, d2) \ if (0) { \ } #endif #define TOSUM(arr, sum, n) \ { \ sum[0] = arr[0]; \ REP1(i, n - 1) sum[i] = sum[i - 1] + arr[i]; \ } #define MIN(target, v1) (target) = min(target, v1) #define MAX(target, v1) (target) = max(target, v1) #define P1 first #define P2 second #define PB push_back #define UB upper_bound #define LB lower_bound typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF_INT = 2147483647; const ll INF_LL = 9223372036854775807LL; const ull INF_ULL = 18446744073709551615Ull; const ll P = 92540646808111039LL; const int Move[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1}; const int Move_[8][2] = {-1, 0, -1, -1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, -1, -1}; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "( " << p.P1 << " , " << p.P2 << " )"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { ITE(v) os << (ite - v.begin()) << " : " << *ite << endl; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << " { "; ITE(v) { os << *ite; if (ite != --v.end()) { os << " , "; } } os << " } "; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &m) { ITE(m) { os << ite->P1 << "\t\t|->\t\t" << ite->P2 << endl; } return os; } //--------------------- #define MOD 1000000007 #define MAXN 100005 //--------------------- ll inv[MAXN]; ll fac[MAXN]; ll facinv[MAXN]; ll modpow(ll a, ll b) { if (b == -1) return inv[a]; if (b < -1) { a = inv[a % MOD]; b *= -1; } if (b == 0) return 1; if (b == 1) return a % MOD; ll ret = modpow(a % MOD, b >> 1) % MOD; return (ret * ret % MOD) * ((b % 2) ? (a % MOD) : 1) % MOD; } ll fermat(ll a) { if (a % MOD) return -1; return modpow(a, MOD - 2); } inline ll modmult(ll a, ll b) { return (a % MOD) * (b % MOD) % MOD; } inline ll modmult(ll a, ll b, ll c) { return (a % MOD) * (b % MOD) % MOD * (c % MOD) % MOD; } inline ll modfact(ll n) { if (n == 0 || n == 1) return 1; if (n >= MOD) return 0; return fac[n]; } inline ll modcomb(ll m, ll n) { if (m >= MOD || n >= MOD || n < 0 || m < 0) return -1; if (n == 0 || n == m) return 1; if (m < n) return 0; return fac[m] * facinv[m - n] % MOD * facinv[n] % MOD; } void makelist() { inv[0] = -1; inv[1] = 1; for (ll i = 2; i < MAXN; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD; fac[0] = 1; REP1(i, MAXN) fac[i] = (fac[i - 1] * i) % MOD; facinv[MAXN - 1] = modpow(fac[MAXN - 1], MOD - 2); PER(i, MAXN - 1) facinv[i] = (facinv[i + 1] * (i + 1)) % MOD; } void makepowlist(ll *list, ll a, ll num) { list[0] = 1; REP1(i, num - 1) { list[i] = modmult(list[i - 1], a); } } void makepowinvlist(ll *list, ll a, ll num) { list[0] = 1; REP1(i, num - 1) { list[i] = modmult(list[i - 1], inv[a]); } } ll n, k; ll a[MAXN]; int main() { makelist(); cin >> n >> k; REP1(i, n) cin >> a[i]; sort(a + 1, a + 1 + n); ll res = 0; if (k == 1) { res = 0; } else { REP1(i, n - k + 1) FORE(j, i + k - 1, n) { ll inner = j - i - 1; ll sel = k - 2; res = (res + ((a[j] - a[i]) * modcomb(inner, sel)) % MOD) % MOD; } } PRT(res); return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (long long i = 0; i < (n); i++) #define REP1(i, n) for (long long i = 1; i <= (n); i++) #define REP2D(i, j, h, w) \ for (long long i = 0; i < (h); i++) \ for (long long j = 0; j < (w); j++) #define REP2D1(i, j, h, w) \ for (long long i = 1; i <= (h); i++) \ for (long long j = 1; j <= (w); j++) #define PER(i, n) for (long long i = ((n)-1); i >= 0; i--) #define PER1(i, n) for (long long i = (n); i > 0; i--) #define FOR(i, a, b) for (long long i = (a); i < (b); i++) #define FORE(i, a, b) for (long long i = (a); i <= (b); i++) #define ITE(arr) for (auto ite = (arr).begin(); ite != (arr).end(); ++ite) #define ALL(a) ((a).begin()), ((a).end()) #define RANGE(a) (a), ((a) + sizeof(a)) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define YNPRT(b) cout << ((b) ? "Yes" : "No") << endl #define ENTER printf("\n") #define REV(arr) reverse(ALL(arr)) #define PRT(a) cout << (a) << endl #ifdef DEBUG #define DBPRT(a) cout << "[Debug] - " << #a << " : " << (a) << endl #define DBSTART if (1) { #define DBEND } #define PRTLST(arr, num) REP(_i, num) cout << _i << " - " << arr[_i] << endl; #define PRTLST2(arr2, d1, d2) \ REP(_i, d1) \ REP(_j, d2) cout << _i << "," << _j << " : " << arr2[_i][_j] << endl; #define PRTLST2D(arr2, d1, d2) \ do { \ cout << "L\t"; \ REP(_i, d2) cout << _i << "\t"; \ cout << endl; \ REP(_i, d1) { \ cout << _i << "\t"; \ REP(_j, d2) { cout << arr2[_i][_j] << "\t"; } \ cout << endl; \ } \ } while (0); #else #define DBPRT(a) \ if (0) { \ } #define DBSTART if (0) { #define DBEND } #define PRTLST(arr, num) \ if (0) { \ } #define PRTLST2(arr2, d1, d2) \ if (0) { \ } #define PRTLST2D(arr2, d1, d2) \ if (0) { \ } #endif #define TOSUM(arr, sum, n) \ { \ sum[0] = arr[0]; \ REP1(i, n - 1) sum[i] = sum[i - 1] + arr[i]; \ } #define MIN(target, v1) (target) = min(target, v1) #define MAX(target, v1) (target) = max(target, v1) #define P1 first #define P2 second #define PB push_back #define UB upper_bound #define LB lower_bound typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF_INT = 2147483647; const ll INF_LL = 9223372036854775807LL; const ull INF_ULL = 18446744073709551615Ull; const ll P = 92540646808111039LL; const int Move[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1}; const int Move_[8][2] = {-1, 0, -1, -1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, -1, -1}; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "( " << p.P1 << " , " << p.P2 << " )"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { ITE(v) os << (ite - v.begin()) << " : " << *ite << endl; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << " { "; ITE(v) { os << *ite; if (ite != --v.end()) { os << " , "; } } os << " } "; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &m) { ITE(m) { os << ite->P1 << "\t\t|->\t\t" << ite->P2 << endl; } return os; } //--------------------- #define MOD 1000000007 #define MAXN 100005 //--------------------- ll inv[MAXN]; ll fac[MAXN]; ll facinv[MAXN]; ll modpow(ll a, ll b) { if (b == -1) return inv[a]; if (b < -1) { a = inv[a % MOD]; b *= -1; } if (b == 0) return 1; if (b == 1) return a % MOD; ll ret = modpow(a % MOD, b >> 1) % MOD; return (ret * ret % MOD) * ((b % 2) ? (a % MOD) : 1) % MOD; } ll fermat(ll a) { if (a % MOD) return -1; return modpow(a, MOD - 2); } inline ll modmult(ll a, ll b) { return (a % MOD) * (b % MOD) % MOD; } inline ll modmult(ll a, ll b, ll c) { return (a % MOD) * (b % MOD) % MOD * (c % MOD) % MOD; } inline ll modfact(ll n) { if (n == 0 || n == 1) return 1; if (n >= MOD) return 0; return fac[n]; } inline ll modcomb(ll m, ll n) { if (m >= MOD || n >= MOD || n < 0 || m < 0) return -1; if (n == 0 || n == m) return 1; if (m < n) return 0; return fac[m] * facinv[m - n] % MOD * facinv[n] % MOD; } void makelist() { inv[0] = -1; inv[1] = 1; for (ll i = 2; i < MAXN; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD; fac[0] = 1; REP1(i, MAXN) fac[i] = (fac[i - 1] * i) % MOD; facinv[MAXN - 1] = modpow(fac[MAXN - 1], MOD - 2); PER(i, MAXN - 1) facinv[i] = (facinv[i + 1] * (i + 1)) % MOD; } void makepowlist(ll *list, ll a, ll num) { list[0] = 1; REP1(i, num - 1) { list[i] = modmult(list[i - 1], a); } } void makepowinvlist(ll *list, ll a, ll num) { list[0] = 1; REP1(i, num - 1) { list[i] = modmult(list[i - 1], inv[a]); } } ll n, k; ll a[MAXN]; int main() { makelist(); cin >> n >> k; REP1(i, n) cin >> a[i]; sort(a + 1, a + 1 + n); ll res = 0; if (k == 1) { res = 0; } else { ll smax = 0; ll smin = 0; FORE(i, k, n) smax = (smax + (modcomb(i - 1, k - 1) * a[i]) % MOD) % MOD; FORE(i, 1, n - k + 1) smin = (smin + (modcomb(n - i, k - 1) * a[i]) % MOD) % MOD; res = (smax - smin + MOD) % MOD; } PRT(res); return 0; }
replace
195
200
195
201
TLE
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define all(x) (x).begin(), (x).end() #define m0(x) memset(x, 0, sizeof(x)) int dx4[4] = {1, 0, -1, 0}, dy4[4] = {0, 1, 0, -1}; template <int M, bool IsPrime = false> class Modulo { int n; static typename std::enable_if<IsPrime, ll>::type inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); } public: Modulo() : n(0) { ; } Modulo(int m) : n(m) { if (n >= M) n %= M; else if (n < 0) n = (n % M + M) % M; } Modulo(ll m) { if (m >= M) m %= M; else if (m < 0) m = (m % M + M) % M; n = m; } explicit operator int() const { return n; } explicit operator ll() const { return n; } bool operator==(const Modulo &a) const { return n == a.n; } Modulo &operator+=(const Modulo &a) { n += a.n; if (n >= M) n -= M; return *this; } Modulo &operator-=(const Modulo &a) { n -= a.n; if (n < 0) n += M; return *this; } Modulo &operator*=(const Modulo &a) { n = (ll(n) * a.n) % M; return *this; } Modulo operator+(const Modulo &a) const { Modulo res = *this; return res += a; } Modulo operator-(const Modulo &a) const { Modulo res = *this; return res -= a; } Modulo operator-() const { return Modulo(0) - *this; } Modulo operator*(const Modulo &a) const { Modulo res = *this; return res *= a; } Modulo operator^(ll m) const { if (m == 0) return Modulo(1); const Modulo a = *this; Modulo res = (a * a) ^ (m / 2); return m % 2 ? res * a : res; } typename std::enable_if<IsPrime, Modulo>::type operator/(const Modulo &a) const { return *this * inv(ll(a), M); } typename std::enable_if<IsPrime, Modulo>::type operator/=(const Modulo &a) { return *this *= inv(ll(a), M); } friend bool is_zero(const Modulo &x) { return int(x) == 0; } friend int abs(const Modulo &x) { return int(x); } static Modulo fact(int n, bool sw = true) { static std::vector<Modulo> v1 = {1}, v2 = {1}; if (n >= (int)v1.size()) { const int from = v1.size(), to = n + 1024; v1.reserve(to); v2.reserve(to); for (int i = from; i < to; ++i) { v1.push_back(v1.back() * Modulo<M, true>(i)); v2.push_back(v2.back() / Modulo<M, true>(i)); } } return sw ? v1[n] : v2[n]; } static Modulo comb(int a, int b) { if (b < 0 || b > a) return 0; return Modulo::fact(a, true) * Modulo::fact(b, false) * Modulo::fact(a - b, false); } }; using Mod = Modulo<1000000007, true>; template <typename T> struct Combination { vector<T> _fact, _rfact, _inv; Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) { _fact[0] = _rfact[sz] = _inv[0] = 1; for (int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i; _rfact[sz] /= _fact[sz]; for (int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1); for (int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1]; } inline T fact(int k) const { return _fact[k]; } inline T rfact(int k) const { return _rfact[k]; } inline T inv(int k) const { return _inv[k]; } T P(int n, int r) const { if (r < 0 || n < r) return 0; return fact(n) * rfact(n - r); } T C(int p, int q) const { if (q < 0 || p < q) return 0; return fact(p) * rfact(q) * rfact(p - q); } T H(int n, int r) const { if (n < 0 || r < 0) return (0); return r == 0 ? 1 : C(n + r - 1, r); } }; int main() { int n, k; cin >> n >> k; Mod ans = 0; vector<Mod> x(n); vector<int> xx(n); for (int i = 0; i < n; i++) { cin >> xx[i]; } sort(all(xx)); for (int i = 0; i < n; i++) { x[i] = xx[i]; } Combination<Mod> comb(10); for (int i = 0; i < n; i++) { ans += x[i] * comb.C(i, k - 1); ans -= x[i] * comb.C(n - 1 - i, k - 1); } cout << int(ans) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define all(x) (x).begin(), (x).end() #define m0(x) memset(x, 0, sizeof(x)) int dx4[4] = {1, 0, -1, 0}, dy4[4] = {0, 1, 0, -1}; template <int M, bool IsPrime = false> class Modulo { int n; static typename std::enable_if<IsPrime, ll>::type inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); } public: Modulo() : n(0) { ; } Modulo(int m) : n(m) { if (n >= M) n %= M; else if (n < 0) n = (n % M + M) % M; } Modulo(ll m) { if (m >= M) m %= M; else if (m < 0) m = (m % M + M) % M; n = m; } explicit operator int() const { return n; } explicit operator ll() const { return n; } bool operator==(const Modulo &a) const { return n == a.n; } Modulo &operator+=(const Modulo &a) { n += a.n; if (n >= M) n -= M; return *this; } Modulo &operator-=(const Modulo &a) { n -= a.n; if (n < 0) n += M; return *this; } Modulo &operator*=(const Modulo &a) { n = (ll(n) * a.n) % M; return *this; } Modulo operator+(const Modulo &a) const { Modulo res = *this; return res += a; } Modulo operator-(const Modulo &a) const { Modulo res = *this; return res -= a; } Modulo operator-() const { return Modulo(0) - *this; } Modulo operator*(const Modulo &a) const { Modulo res = *this; return res *= a; } Modulo operator^(ll m) const { if (m == 0) return Modulo(1); const Modulo a = *this; Modulo res = (a * a) ^ (m / 2); return m % 2 ? res * a : res; } typename std::enable_if<IsPrime, Modulo>::type operator/(const Modulo &a) const { return *this * inv(ll(a), M); } typename std::enable_if<IsPrime, Modulo>::type operator/=(const Modulo &a) { return *this *= inv(ll(a), M); } friend bool is_zero(const Modulo &x) { return int(x) == 0; } friend int abs(const Modulo &x) { return int(x); } static Modulo fact(int n, bool sw = true) { static std::vector<Modulo> v1 = {1}, v2 = {1}; if (n >= (int)v1.size()) { const int from = v1.size(), to = n + 1024; v1.reserve(to); v2.reserve(to); for (int i = from; i < to; ++i) { v1.push_back(v1.back() * Modulo<M, true>(i)); v2.push_back(v2.back() / Modulo<M, true>(i)); } } return sw ? v1[n] : v2[n]; } static Modulo comb(int a, int b) { if (b < 0 || b > a) return 0; return Modulo::fact(a, true) * Modulo::fact(b, false) * Modulo::fact(a - b, false); } }; using Mod = Modulo<1000000007, true>; template <typename T> struct Combination { vector<T> _fact, _rfact, _inv; Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) { _fact[0] = _rfact[sz] = _inv[0] = 1; for (int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i; _rfact[sz] /= _fact[sz]; for (int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1); for (int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1]; } inline T fact(int k) const { return _fact[k]; } inline T rfact(int k) const { return _rfact[k]; } inline T inv(int k) const { return _inv[k]; } T P(int n, int r) const { if (r < 0 || n < r) return 0; return fact(n) * rfact(n - r); } T C(int p, int q) const { if (q < 0 || p < q) return 0; return fact(p) * rfact(q) * rfact(p - q); } T H(int n, int r) const { if (n < 0 || r < 0) return (0); return r == 0 ? 1 : C(n + r - 1, r); } }; int main() { int n, k; cin >> n >> k; Mod ans = 0; vector<Mod> x(n); vector<int> xx(n); for (int i = 0; i < n; i++) { cin >> xx[i]; } sort(all(xx)); for (int i = 0; i < n; i++) { x[i] = xx[i]; } Combination<Mod> comb(n + 10); for (int i = 0; i < n; i++) { ans += x[i] * comb.C(i, k - 1); ans -= x[i] * comb.C(n - 1 - i, k - 1); } cout << int(ans) << endl; return 0; }
replace
162
163
162
163
0
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define LL long long const int N = 1e5 + 11; const int mod = 1e9 + 7; int n, K; LL sum[N], a[N]; int fac[N], inv[N]; LL C(int n, int m) { return 1LL * fac[n] * inv[m] % mod * inv[n - m] % mod; } int main() { cin >> n >> K; fac[0] = fac[1] = inv[0] = inv[1] = 1; for (int i = 2; i <= 1e5; i++) { fac[i] = 1LL * fac[i - 1] * i % mod; inv[i] = mod - 1LL * mod / i * inv[mod % i] % mod; } for (int i = 2; i <= 1e5; i++) inv[i] = 1LL * inv[i - 1] * inv[i] % mod; for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; if (K == 1) { puts("0"); return 0; } int ans = 0; for (int p = 1; p <= n - 1; p++) { LL w = sum[n] - sum[p] - sum[n - p]; w = (mod + w % mod) % mod; ans = (ans + w * C(p - 1, K - 2) % mod) % mod; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define LL long long const int N = 1e5 + 11; const int mod = 1e9 + 7; int n, K; LL sum[N], a[N]; LL fac[N], inv[N]; LL C(int n, int m) { if (m > n) return 0; return 1LL * fac[n] * inv[m] % mod * inv[n - m] % mod; } int main() { cin >> n >> K; fac[0] = fac[1] = inv[0] = inv[1] = 1; for (int i = 2; i <= 1e5; i++) { fac[i] = 1LL * fac[i - 1] * i % mod; inv[i] = mod - 1LL * mod / i * inv[mod % i] % mod; } for (int i = 2; i <= 1e5; i++) inv[i] = 1LL * inv[i - 1] * inv[i] % mod; for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; if (K == 1) { puts("0"); return 0; } int ans = 0; for (int p = 1; p <= n - 1; p++) { LL w = sum[n] - sum[p] - sum[n - p]; w = (mod + w % mod) % mod; ans = (ans + w * C(p - 1, K - 2) % mod) % mod; } cout << ans << endl; return 0; }
replace
7
9
7
13
0
p02804
C++
Time Limit Exceeded
// #define NDEBUG #include <algorithm> #include <cstddef> #include <cstdint> #include <iostream> #include <utility> #include <vector> namespace n91 { using i8 = std::int_fast8_t; using i32 = std::int_fast32_t; using i64 = std::int_fast64_t; using u8 = std::uint_fast8_t; using u32 = std::uint_fast32_t; using u64 = std::uint_fast64_t; using isize = std::ptrdiff_t; using usize = std::size_t; struct rep { struct itr { usize i; constexpr itr(const usize i) noexcept : i(i) {} void operator++() noexcept { ++i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; const itr f, l; constexpr rep(const usize f, const usize l) noexcept : f(std::min(f, l)), l(l) {} constexpr auto begin() const noexcept { return f; } constexpr auto end() const noexcept { return l; } }; struct revrep { struct itr { usize i; constexpr itr(const usize i) noexcept : i(i) {} void operator++() noexcept { --i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; const itr f, l; constexpr revrep(const usize f, const usize l) noexcept : f(l - 1), l(std::min(f, l) - 1) {} constexpr auto begin() const noexcept { return f; } constexpr auto end() const noexcept { return l; } }; template <class T> auto md_vec(const usize n, const T &value) { return std::vector<T>(n, value); } template <class... Args> auto md_vec(const usize n, Args... args) { return std::vector<decltype(md_vec(args...))>(n, md_vec(args...)); } template <class T> constexpr T difference(const T &a, const T &b) noexcept { if (a < b) { return b - a; } else { return a - b; } } template <class T> void chmin(T &a, const T &b) noexcept { if (b < a) { a = b; } } template <class T> void chmax(T &a, const T &b) noexcept { if (a < b) { a = b; } } template <class F> class fix_point : private F { public: explicit constexpr fix_point(F &&f) : F(std::forward<F>(f)) {} template <class... Args> constexpr decltype(auto) operator()(Args &&...args) const { return F::operator()(*this, std::forward<Args>(args)...); } }; template <class F> constexpr decltype(auto) make_fix(F &&f) { return fix_point<F>(std::forward<F>(f)); } template <class T> T scan() { T ret; std::cin >> ret; return ret; } } // namespace n91 #include <cstdint> #include <iostream> template <std::uint32_t mod> class modint { using i64 = std::int64_t; using u32 = std::uint32_t; using u64 = std::int64_t; static_assert(mod < (u32(1) << 31), "mod must be less than 2**31"); public: u32 v; constexpr modint(const i64 x = 0) noexcept : v(x < 0 ? mod - 1 - -(x + 1) % mod : x % mod) {} 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 { v += rhs.v; if (v >= mod) v -= mod; return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (v < rhs.v) v += mod; v -= rhs.v; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { v = u64(v) * rhs.v % mod; return *this; } constexpr modint &operator/=(const modint rhs) noexcept { *this *= rhs.pow(mod - 2); } constexpr modint pow(u32 exp) const noexcept { modint self(*this), ret(1); while (exp != 0) { if (exp % 2 != 0) ret *= self; self *= self; exp /= 2; } return ret; } }; template <std::uint32_t mod> std::istream &operator>>(std::istream &is, modint<mod> &rhs) { std::int64_t v; is >> v; rhs = modint<mod>(v); return is; } template <std::uint32_t mod> std::ostream &operator<<(std::ostream &os, const modint<mod> &rhs) { os << rhs.v; return os; } #include <vector> namespace n91 { template <class T> class fact_binom { using size_t = std::size_t; public: using value_type = T; private: std::vector<T> fact, inv_fact; public: fact_binom() = default; explicit fact_binom(const size_t n) : fact(n + 1), inv_fact(n + 1) { fact[0] = 1; for (size_t i = 0; i != n; ++i) { fact[i + 1] = fact[i] * (i + 1); } inv_fact[n] = static_cast<value_type>(1) / fact[n]; for (size_t i = n; i != 0; --i) { inv_fact[i - 1] = inv_fact[i] * i; } } T operator()(const size_t n, const size_t r) const { return fact[n] * inv_fact[r] * inv_fact[n - r]; } }; } // namespace n91 #include <algorithm> #include <iostream> #include <utility> #include <vector> namespace n91 { void main_() { /* std::ios::sync_with_stdio(false); std::cin.tie(nullptr); //*/ using mint = modint<1000000007>; const usize n = scan<usize>(); const usize k = scan<usize>(); const fact_binom<mint> binom(n); std::vector<i32> a(n); for (auto &e : a) { std::cin >> e; } std::sort(a.begin(), a.end()); mint ans = 0; for (const usize i : rep(0, n)) { if (i >= k - 1) ans += binom(i, k - 1) * a[i]; } for (const usize i : rep(0, n)) { if (n - i - 1 >= k - 1) ans -= binom(n - i - 1, k - 1) * a[i]; } std::cout << ans << std::endl; } } // namespace n91 int main() { n91::main_(); return 0; }
// #define NDEBUG #include <algorithm> #include <cstddef> #include <cstdint> #include <iostream> #include <utility> #include <vector> namespace n91 { using i8 = std::int_fast8_t; using i32 = std::int_fast32_t; using i64 = std::int_fast64_t; using u8 = std::uint_fast8_t; using u32 = std::uint_fast32_t; using u64 = std::uint_fast64_t; using isize = std::ptrdiff_t; using usize = std::size_t; struct rep { struct itr { usize i; constexpr itr(const usize i) noexcept : i(i) {} void operator++() noexcept { ++i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; const itr f, l; constexpr rep(const usize f, const usize l) noexcept : f(std::min(f, l)), l(l) {} constexpr auto begin() const noexcept { return f; } constexpr auto end() const noexcept { return l; } }; struct revrep { struct itr { usize i; constexpr itr(const usize i) noexcept : i(i) {} void operator++() noexcept { --i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; const itr f, l; constexpr revrep(const usize f, const usize l) noexcept : f(l - 1), l(std::min(f, l) - 1) {} constexpr auto begin() const noexcept { return f; } constexpr auto end() const noexcept { return l; } }; template <class T> auto md_vec(const usize n, const T &value) { return std::vector<T>(n, value); } template <class... Args> auto md_vec(const usize n, Args... args) { return std::vector<decltype(md_vec(args...))>(n, md_vec(args...)); } template <class T> constexpr T difference(const T &a, const T &b) noexcept { if (a < b) { return b - a; } else { return a - b; } } template <class T> void chmin(T &a, const T &b) noexcept { if (b < a) { a = b; } } template <class T> void chmax(T &a, const T &b) noexcept { if (a < b) { a = b; } } template <class F> class fix_point : private F { public: explicit constexpr fix_point(F &&f) : F(std::forward<F>(f)) {} template <class... Args> constexpr decltype(auto) operator()(Args &&...args) const { return F::operator()(*this, std::forward<Args>(args)...); } }; template <class F> constexpr decltype(auto) make_fix(F &&f) { return fix_point<F>(std::forward<F>(f)); } template <class T> T scan() { T ret; std::cin >> ret; return ret; } } // namespace n91 #include <cstdint> #include <iostream> template <std::uint32_t mod> class modint { using i64 = std::int64_t; using u32 = std::uint32_t; using u64 = std::int64_t; static_assert(mod < (u32(1) << 31), "mod must be less than 2**31"); public: u32 v; constexpr modint(const i64 x = 0) noexcept : v(x < 0 ? mod - 1 - -(x + 1) % mod : x % mod) {} 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 { v += rhs.v; if (v >= mod) v -= mod; return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (v < rhs.v) v += mod; v -= rhs.v; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { v = u64(v) * rhs.v % mod; return *this; } constexpr modint &operator/=(const modint rhs) noexcept { *this *= rhs.pow(mod - 2); return *this; } constexpr modint pow(u32 exp) const noexcept { modint self(*this), ret(1); while (exp != 0) { if (exp % 2 != 0) ret *= self; self *= self; exp /= 2; } return ret; } }; template <std::uint32_t mod> std::istream &operator>>(std::istream &is, modint<mod> &rhs) { std::int64_t v; is >> v; rhs = modint<mod>(v); return is; } template <std::uint32_t mod> std::ostream &operator<<(std::ostream &os, const modint<mod> &rhs) { os << rhs.v; return os; } #include <vector> namespace n91 { template <class T> class fact_binom { using size_t = std::size_t; public: using value_type = T; private: std::vector<T> fact, inv_fact; public: fact_binom() = default; explicit fact_binom(const size_t n) : fact(n + 1), inv_fact(n + 1) { fact[0] = 1; for (size_t i = 0; i != n; ++i) { fact[i + 1] = fact[i] * (i + 1); } inv_fact[n] = static_cast<value_type>(1) / fact[n]; for (size_t i = n; i != 0; --i) { inv_fact[i - 1] = inv_fact[i] * i; } } T operator()(const size_t n, const size_t r) const { return fact[n] * inv_fact[r] * inv_fact[n - r]; } }; } // namespace n91 #include <algorithm> #include <iostream> #include <utility> #include <vector> namespace n91 { void main_() { /* std::ios::sync_with_stdio(false); std::cin.tie(nullptr); //*/ using mint = modint<1000000007>; const usize n = scan<usize>(); const usize k = scan<usize>(); const fact_binom<mint> binom(n); std::vector<i32> a(n); for (auto &e : a) { std::cin >> e; } std::sort(a.begin(), a.end()); mint ans = 0; for (const usize i : rep(0, n)) { if (i >= k - 1) ans += binom(i, k - 1) * a[i]; } for (const usize i : rep(0, n)) { if (n - i - 1 >= k - 1) ans -= binom(n - i - 1, k - 1) * a[i]; } std::cout << ans << std::endl; } } // namespace n91 int main() { n91::main_(); return 0; }
insert
135
135
135
136
TLE
p02804
C++
Time Limit Exceeded
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i)) #define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second #define double long double using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef pair<lint, lint> pll; typedef complex<double> xy_t; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } constexpr lint mod = 1e9 + 7; constexpr lint INF = mod * mod; constexpr int MAX = 100010; struct mint { using i64 = int_fast64_t; i64 a; static constexpr i64 mod = 1e9 + 7; mint(const i64 a_ = 0) : a(a_) {} mint inv() { i64 t = 1, n = mod - 2, x = a; while (n) { if (n & 1) (t *= x) %= mod; (x *= x) %= mod; n >>= 1; } mint ret(t); return ret; } bool operator==(const mint &x) { return a == x.a; } bool operator!=(const mint &x) { return a != x.a; } mint operator+(const mint &x) { mint ret; ret.a = a + x.a; if (ret.a > mod) ret.a -= mod; return ret; } mint operator-(const mint &x) { mint ret; ret.a = a - x.a; if (ret.a < 0) ret.a += mod; return ret; } mint operator*(const mint &x) { mint ret; ret.a = a * x.a % mod; return ret; } mint operator/(mint &x) { mint ret; ret.a = a * x.inv().a % mod; return ret; } mint operator^(lint n) { mint ret = 1, x = a; while (n) { if (n & 1) (ret.a *= x.a) %= mod; (x.a *= x.a) %= mod; n >>= 1; } return ret; } mint &operator+=(const mint &x) { a += x.a; if (a >= mod) a -= mod; return *this; } mint &operator-=(const mint &x) { a -= x.a; if (a < 0) a += mod; return *this; } mint &operator*=(const mint &x) { (a *= x.a) %= mod; return *this; } mint &operator/=(mint &x) { (a *= x.inv().a) %= mod; return *this; } mint &operator^=(lint n) { lint ret = 1; while (n) { if (n & 1) (ret *= a) %= mod; (a *= a) %= mod; n >>= 1; } a = ret; return *this; } }; vector<mint> fact; vector<mint> revfact; void setfact(int n) { fact.resize(n + 1); revfact.resize(n + 1); fact[0] = 1; rep(i, n) fact[i + 1] = fact[i] * mint(i + 1); revfact[n] = fact[n].inv(); for (int i = n - 1; i >= 0; i--) revfact[i] = revfact[i + 1] * mint(i + 1); } mint getC(int n, int r) { if (n < r) return 0; return fact[n] * revfact[r] * revfact[n - r]; } template <typename T> struct SegTree { using F = function<T(T, T)>; int sz = 1; T et; F f, g; vector<T> node; SegTree(int sz_, T et_, F f_, F g_) : et(et_), f(f_), g(g_) { while (sz < sz_) sz <<= 1; node.resize(sz << 1, et); } void update(int i, T x) { i += sz; node[i] = g(node[i], x); i >>= 1; while (i) { node[i] = f(node[i << 1], node[(i << 1) + 1]); i >>= 1; } } T query(int l, int r) { T vl = et, vr = et; for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, node[l++]); if (r & 1) vr = f(node[--r], vr); } return f(vl, vr); } }; int main() { int n, K; scanf("%d%d", &n, &K); int a[n]; vector<int> v(n); rep(i, n) { scanf("%d", &a[i]); v[i] = a[i]; } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); SegTree<int> st(v.size(), 0, plus<>(), plus<>()); rep(i, n) { int idx = lower_bound(v.begin(), v.end(), a[i]) - v.begin(); st.update(idx, 1); } setfact(n); mint ans = 0; sort(a, a + n); for (int t : v) { auto lb = lower_bound(a, a + n, t); auto ub = upper_bound(a, a + n, t); int x = lb - a; int y = ub - lb; int z = (a + n) - ub; auto P = (getC(y + z, K) - getC(z, K)) * mint((t + mod) % mod); auto Q = (getC(x + y, K) - getC(x, K)) * mint((t + mod) % mod); ans += Q - P; // printf("%lld %lld\n", P.a, Q.a); // printf("%d %d %d %d\n", x, y, z, t); } printf("%lld\n", ans.a); }
#include <bits/stdc++.h> #define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i)) #define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second #define double long double using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef pair<lint, lint> pll; typedef complex<double> xy_t; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } constexpr lint mod = 1e9 + 7; constexpr lint INF = mod * mod; constexpr int MAX = 100010; struct mint { using i64 = int_fast64_t; i64 a; static constexpr i64 mod = 1e9 + 7; mint(const i64 a_ = 0) : a(a_) {} mint inv() { i64 t = 1, n = mod - 2, x = a; while (n) { if (n & 1) (t *= x) %= mod; (x *= x) %= mod; n >>= 1; } mint ret(t); return ret; } bool operator==(const mint &x) { return a == x.a; } bool operator!=(const mint &x) { return a != x.a; } mint operator+(const mint &x) { mint ret; ret.a = a + x.a; if (ret.a > mod) ret.a -= mod; return ret; } mint operator-(const mint &x) { mint ret; ret.a = a - x.a; if (ret.a < 0) ret.a += mod; return ret; } mint operator*(const mint &x) { mint ret; ret.a = a * x.a % mod; return ret; } mint operator/(mint &x) { mint ret; ret.a = a * x.inv().a % mod; return ret; } mint operator^(lint n) { mint ret = 1, x = a; while (n) { if (n & 1) (ret.a *= x.a) %= mod; (x.a *= x.a) %= mod; n >>= 1; } return ret; } mint &operator+=(const mint &x) { a += x.a; if (a >= mod) a -= mod; return *this; } mint &operator-=(const mint &x) { a -= x.a; if (a < 0) a += mod; return *this; } mint &operator*=(const mint &x) { (a *= x.a) %= mod; return *this; } mint &operator/=(mint &x) { (a *= x.inv().a) %= mod; return *this; } mint &operator^=(lint n) { lint ret = 1; while (n) { if (n & 1) (ret *= a) %= mod; (a *= a) %= mod; n >>= 1; } a = ret; return *this; } }; vector<mint> fact; vector<mint> revfact; void setfact(int n) { fact.resize(n + 1); revfact.resize(n + 1); fact[0] = 1; rep(i, n) fact[i + 1] = fact[i] * mint(i + 1); revfact[n] = fact[n].inv(); for (int i = n - 1; i >= 0; i--) revfact[i] = revfact[i + 1] * mint(i + 1); } mint getC(int n, int r) { if (n < r) return 0; return fact[n] * revfact[r] * revfact[n - r]; } template <typename T> struct SegTree { using F = function<T(T, T)>; int sz = 1; T et; F f, g; vector<T> node; SegTree(int sz_, T et_, F f_, F g_) : et(et_), f(f_), g(g_) { while (sz < sz_) sz <<= 1; node.resize(sz << 1, et); } void update(int i, T x) { i += sz; node[i] = g(node[i], x); i >>= 1; while (i) { node[i] = f(node[i << 1], node[(i << 1) + 1]); i >>= 1; } } T query(int l, int r) { T vl = et, vr = et; for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, node[l++]); if (r & 1) vr = f(node[--r], vr); } return f(vl, vr); } }; int main() { int n, K; scanf("%d%d", &n, &K); int a[n]; vector<int> v(n); rep(i, n) { scanf("%d", &a[i]); v[i] = a[i]; } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); SegTree<int> st(v.size(), 0, plus<>(), plus<>()); rep(i, n) { int idx = lower_bound(v.begin(), v.end(), a[i]) - v.begin(); st.update(idx, 1); } setfact(n); mint ans = 0; sort(a, a + n); for (int t : v) { auto lb = lower_bound(a, a + n, t); auto ub = upper_bound(a, a + n, t); int x = lb - a; int y = ub - lb; int z = (a + n) - ub; auto P = (getC(y + z, K) - getC(z, K)) * mint((t + mod) % mod); auto Q = (getC(x + y, K) - getC(x, K)) * mint((t + mod) % mod); ans += Q - P; // printf("%lld %lld\n", P.a, Q.a); // printf("%d %d %d %d\n", x, y, z, t); } printf("%lld\n", ans.a); }
delete
0
1
0
0
TLE
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define y1 asdgh #define int long long #define double long double #define endl '\n' mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count()); // #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("unroll-loops") /*------------------------------<Debugging * Stuff>---------------------------------*/ template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <class L, class R, class M> ostream &operator<<(ostream &os, tuple<L, R, M> P) { return os << "(" << get<0>(P) << "," << get<1>(P) << "," << get<2>(P) << ")"; } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "{ "; for (auto v : V) os << v << " "; return os << "}"; } #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif /*------------------------------</Debugging * Stuff>--------------------------------*/ const int maxn = (int)1e6 + 6; const double EPS = 1e-9; const int INF = (int)1e9 + 10; const int mod = (int)1e9 + 7; inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline int sub(int x, int y) { x -= y; if (x < 0) x += mod; return x; } inline int mul(int x, int y) { return (x * 1LL * y) % mod; } inline int binpow(int a, int b) { int x = 1 % mod; while (b) { if (b & 1) x = mul(x, a); a = mul(a, a); b >>= 1; } return x; } inline int inv(int a) { return binpow(a, mod - 2); } signed main() { fio; int n, k; cin >> n >> k; vector<int> a(n); for (int &it : a) cin >> it; sort(a.begin(), a.end()); int answer = 0; vector<int> fac(n + 1), ifac(n + 1); fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = mul(i, fac[i - 1]); for (int i = 0; i <= n; i++) ifac[i] = inv(fac[i]); auto get = [&](int n, int r) { return mul(fac[n], mul(ifac[r], ifac[n - r])); }; for (int i = 0; i < n; i++) { if (i >= k - 1) { answer = add(answer, mul(a[i], get(i, k - 1))); } if (n - i - 1 >= k - 1) { answer = sub(answer, mul(a[i], get(n - i - 1, k - 1))); } } assert(answer >= 0 and answer < mod); cout << answer << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define y1 asdgh #define int long long #define double long double #define endl '\n' mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count()); // #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("unroll-loops") /*------------------------------<Debugging * Stuff>---------------------------------*/ template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <class L, class R, class M> ostream &operator<<(ostream &os, tuple<L, R, M> P) { return os << "(" << get<0>(P) << "," << get<1>(P) << "," << get<2>(P) << ")"; } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "{ "; for (auto v : V) os << v << " "; return os << "}"; } #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif /*------------------------------</Debugging * Stuff>--------------------------------*/ const int maxn = (int)1e6 + 6; const double EPS = 1e-9; const int INF = (int)1e9 + 10; const int mod = (int)1e9 + 7; inline int add(int x, int y) { return (x + y) % mod; } inline int sub(int x, int y) { return (x - y + mod) % mod; } inline int mul(int x, int y) { return (x * y) % mod; } inline int binpow(int a, int b) { int x = 1 % mod; while (b) { if (b & 1) x = mul(x, a); a = mul(a, a); b >>= 1; } return x; } inline int inv(int a) { return binpow(a, mod - 2); } signed main() { fio; int n, k; cin >> n >> k; vector<int> a(n); for (int &it : a) cin >> it; sort(a.begin(), a.end()); int answer = 0; vector<int> fac(n + 1), ifac(n + 1); fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = mul(i, fac[i - 1]); for (int i = 0; i <= n; i++) ifac[i] = inv(fac[i]); auto get = [&](int n, int r) { return mul(fac[n], mul(ifac[r], ifac[n - r])); }; for (int i = 0; i < n; i++) { if (i >= k - 1) { answer = add(answer, mul(a[i], get(i, k - 1))); } if (n - i - 1 >= k - 1) { answer = sub(answer, mul(a[i], get(n - i - 1, k - 1))); } } assert(answer >= 0 and answer < mod); cout << answer << endl; return 0; }
replace
57
70
57
60
0
p02804
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define lli long long int #define uli unsigned long long int #define INF 999999999999999999 #define rep(i, m, n) for (lli i = m; i < n; i++) #define rrep(i, m, n) for (lli i = m - 1; i >= n; i--) #define pb(n) push_back(n) #define UE(N) N.erase(unique(N.begin(), N.end()), N.end()); #define Sort(n) sort(n.begin(), n.end()) #define Rev(n) reverse(n.begin(), n.end()) #define Out(S) cout << S << endl #define NeOut(S) cout << S #define HpOut(S) cout << setprecision(25) << S << endl #define Vec(K, L, N, S) vector<L> K(N, S) #define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S)) #define TV(K, L, N, M, R, S) \ vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S))) #define pint pair<lli, lli> #define paf(L, R) pair<L, R> #define mod 1000000007 #define MAX 51000000 #define ALL(a) a.begin(), a.end() #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } lli nCr(lli n, lli r) { if (n < r) return 0; if (n < 0 || r < 0) return 0; return fac[n] * (finv[r] * finv[n - r] % mod) % mod; } int main() { lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0; string S, T; cin >> N >> K; Vec(P, lli, N, 0); COMinit(); rep(i, 0, N) cin >> P[i]; Sort(P); A = 0; B = 0; rep(i, K - 1, N) { A += P[i] * nCr(i, K - 1); A += mod; A %= mod; } Rev(P); rep(i, K - 1, N) { B += P[i] * nCr(i, K - 1); B += mod; B %= mod; } Out((A - B + mod) % mod); }
#include <bits/stdc++.h> using namespace std; #define lli long long int #define uli unsigned long long int #define INF 999999999999999999 #define rep(i, m, n) for (lli i = m; i < n; i++) #define rrep(i, m, n) for (lli i = m - 1; i >= n; i--) #define pb(n) push_back(n) #define UE(N) N.erase(unique(N.begin(), N.end()), N.end()); #define Sort(n) sort(n.begin(), n.end()) #define Rev(n) reverse(n.begin(), n.end()) #define Out(S) cout << S << endl #define NeOut(S) cout << S #define HpOut(S) cout << setprecision(25) << S << endl #define Vec(K, L, N, S) vector<L> K(N, S) #define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S)) #define TV(K, L, N, M, R, S) \ vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S))) #define pint pair<lli, lli> #define paf(L, R) pair<L, R> #define mod 1000000007 #define MAX 510000 #define ALL(a) a.begin(), a.end() #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } lli nCr(lli n, lli r) { if (n < r) return 0; if (n < 0 || r < 0) return 0; return fac[n] * (finv[r] * finv[n - r] % mod) % mod; } int main() { lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0; string S, T; cin >> N >> K; Vec(P, lli, N, 0); COMinit(); rep(i, 0, N) cin >> P[i]; Sort(P); A = 0; B = 0; rep(i, K - 1, N) { A += P[i] * nCr(i, K - 1); A += mod; A %= mod; } Rev(P); rep(i, K - 1, N) { B += P[i] * nCr(i, K - 1); B += mod; B %= mod; } Out((A - B + mod) % mod); }
replace
21
22
21
22
MLE
p02804
C++
Runtime Error
#include <algorithm> #include <math.h> #include <queue> #include <stdio.h> #include <string> #include <vector> const long long mod = 1000000007; // const long long mod = 998244353; class Combination { int MAX; int mod; long long *kai; long long modpow(long long a, long long n) { 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) { return modpow(a, mod - 2); } void Init() { for (int i = 1; i < MAX; i++) { kai[i] = kai[i - 1] * i; kai[i] %= mod; } } public: Combination(int max, int mod) : mod(mod), MAX(max + 5) { kai = new long long[max + 5]{1}; Init(); } ~Combination() { delete[] kai; } long long operator()(int n, int k) { if (n < 0 || k < 0) return 0; long long res = kai[n]; res *= modinv(kai[k]); res %= mod; res *= modinv(kai[n - k]); res %= mod; // printf("%d C %d = %d\n", n, k, res); return res; } }; int main() { int N, K; scanf("%d %d", &N, &K); Combination nck(N, mod); int *A = new int[N]; for (int i = 0; i < N; i++) scanf("%d", A + i); std::sort(A, A + N); long long res = 0; for (int i = 0; i < N - 1; i++) { res += (A[N - 1 - i] * nck(N - i - 1, K - 1)); res %= mod; res += mod - (A[i] * nck(N - i - 1, K - 1)) % mod; res %= mod; } printf("%lld", res); return 0; }
#include <algorithm> #include <math.h> #include <queue> #include <stdio.h> #include <string> #include <vector> const long long mod = 1000000007; // const long long mod = 998244353; class Combination { int MAX; int mod; long long *kai; long long modpow(long long a, long long n) { 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) { return modpow(a, mod - 2); } void Init() { for (int i = 1; i < MAX; i++) { kai[i] = kai[i - 1] * i; kai[i] %= mod; } } public: Combination(int max, int mod) : mod(mod), MAX(max + 5) { kai = new long long[max + 5]{1}; Init(); } ~Combination() { delete[] kai; } long long operator()(int n, int k) { if (n < 0 || k < 0 || n < k) return 0; long long res = kai[n]; res *= modinv(kai[k]); res %= mod; res *= modinv(kai[n - k]); res %= mod; // printf("%d C %d = %d\n", n, k, res); return res; } }; int main() { int N, K; scanf("%d %d", &N, &K); Combination nck(N, mod); int *A = new int[N]; for (int i = 0; i < N; i++) scanf("%d", A + i); std::sort(A, A + N); long long res = 0; for (int i = 0; i < N - 1; i++) { res += (A[N - 1 - i] * nck(N - i - 1, K - 1)); res %= mod; res += mod - (A[i] * nck(N - i - 1, K - 1)) % mod; res %= mod; } printf("%lld", res); return 0; }
replace
37
38
37
38
0
p02804
C++
Runtime Error
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define PI 3.14159265359 using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) const long long INF = 1e+18 + 1; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> T; const ll MOD = 1000000007LL; string abc = "abcdefghijklmnopqrstuvwxyz"; string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; vl dx = {-1, -1, -1, 0, 0, 1, 1, 1}; vl dy = {1, -1, 0, 1, -1, 1, 0, -1}; const ll MAX = 51000; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { ll n, k; cin >> n >> k; vl a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); COMinit(); ll ans = 0; if (k == 1) { cout << 0 << endl; return 0; } rep(i, n - k + 1) { ans -= a[i] * COM(n - 1 - i, k - 1); ans %= MOD; } ans = (ans + MOD) % MOD; reverse(a.begin(), a.end()); rep(i, n - k + 1) { ans += a[i] * COM(n - 1 - i, k - 1); ans %= MOD; } cout << (ans + MOD) % MOD << endl; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define PI 3.14159265359 using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) const long long INF = 1e+18 + 1; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> T; const ll MOD = 1000000007LL; string abc = "abcdefghijklmnopqrstuvwxyz"; string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; vl dx = {-1, -1, -1, 0, 0, 1, 1, 1}; vl dy = {1, -1, 0, 1, -1, 1, 0, -1}; const ll MAX = 114514; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { ll n, k; cin >> n >> k; vl a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); COMinit(); ll ans = 0; if (k == 1) { cout << 0 << endl; return 0; } rep(i, n - k + 1) { ans -= a[i] * COM(n - 1 - i, k - 1); ans %= MOD; } ans = (ans + MOD) % MOD; reverse(a.begin(), a.end()); rep(i, n - k + 1) { ans += a[i] * COM(n - 1 - i, k - 1); ans %= MOD; } cout << (ans + MOD) % MOD << endl; }
replace
16
17
16
17
0
p02804
C++
Runtime Error
#include <bits/stdc++.h> template <typename T> constexpr T modpow(T a, T n, T mod = 1000000007) { //(a^n)%MOD T ret = 1; while (n > 0) { if ((n & 1) != 0) { // n%2==1 ret = ret * a % mod; } a = a * a % mod; n = n / 2; } return ret; } std::vector<long long> fac; std::vector<long long> ifac; template <typename T> T modcomb(T a, T b, T MOD = 1000000007) { if (a == 0 && b == 0) return 1; if (a < b || a < 0) return 0; long long tmp = ifac[a - b] * ifac[b] % MOD; return tmp * fac[a] % MOD; } void combinit(int maxn) { long long MOD = 1000000007; fac.resize(maxn); ifac.resize(maxn); fac[0] = 1; ifac[0] = 1; for (long long i = 0; i < maxn; i++) { fac[i + 1] = fac[i] * (i + 1) % MOD; ifac[i + 1] = ifac[i] * modpow(i + 1, MOD - 2) % MOD; } } using namespace std; using ll = long long; int main() { ll MOD = 1000000007; ll n, k; cin >> n >> k; combinit(n); vector<ll> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a.begin(), a.end()); ll ans = 0; for (int i = 0; i < n; ++i) { ll r = n - 1 - i; ll l = i; ll cnt = modcomb(l, k - 1); cnt -= modcomb(r, k - 1); if (cnt < 0) cnt += MOD; if (a[i] < 0) a[i] += MOD; ans += (a[i] * cnt) % MOD; ans %= MOD; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> template <typename T> constexpr T modpow(T a, T n, T mod = 1000000007) { //(a^n)%MOD T ret = 1; while (n > 0) { if ((n & 1) != 0) { // n%2==1 ret = ret * a % mod; } a = a * a % mod; n = n / 2; } return ret; } std::vector<long long> fac; std::vector<long long> ifac; template <typename T> T modcomb(T a, T b, T MOD = 1000000007) { if (a == 0 && b == 0) return 1; if (a < b || a < 0) return 0; long long tmp = ifac[a - b] * ifac[b] % MOD; return tmp * fac[a] % MOD; } void combinit(int maxn) { long long MOD = 1000000007; fac.resize(maxn + 1); ifac.resize(maxn + 1); fac[0] = 1; ifac[0] = 1; for (long long i = 0; i < maxn; i++) { fac[i + 1] = fac[i] * (i + 1) % MOD; ifac[i + 1] = ifac[i] * modpow(i + 1, MOD - 2) % MOD; } } using namespace std; using ll = long long; int main() { ll MOD = 1000000007; ll n, k; cin >> n >> k; combinit(n); vector<ll> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a.begin(), a.end()); ll ans = 0; for (int i = 0; i < n; ++i) { ll r = n - 1 - i; ll l = i; ll cnt = modcomb(l, k - 1); cnt -= modcomb(r, k - 1); if (cnt < 0) cnt += MOD; if (a[i] < 0) a[i] += MOD; ans += (a[i] * cnt) % MOD; ans %= MOD; } cout << ans << endl; return 0; }
replace
29
31
29
31
0
p02804
C++
Time Limit Exceeded
/* #region Head */ #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pll = pair<ll, ll>; using vll = vector<ll>; using vvll = vector<vll>; using vd = vector<double>; using vvd = vector<vd>; using vs = vector<string>; using vvs = vector<vs>; #define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i)) #define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i)) #define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i)) #define REPD(i, m, n, d) \ for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d)) #define REPMD(i, m, n, d) \ for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d)) #define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define ALL(x) begin(x), end(x) #define SIZE(x) ((ll)(x).size()) constexpr ll INF = 1'010'000'000'000'000'017LL; constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7 constexpr double EPS = 1e-12; constexpr double PI = 3.14159265358979323846; // vector入力 template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // vector出力 template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { ll len = SIZE(vec); os << "{"; for (int i = 0; i < len; i++) os << vec[i] << (i == len - 1 ? "" : ", "); os << "}"; return os; } // pair入力 template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &pair_var) { is >> pair_var.first >> pair_var.second; return is; } // pair出力 template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // map出力 template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; REPI(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set 出力 template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; REPI(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } // dump #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(move(tail)...); } // _max の終端. template <class Head> ll _max(Head &&head) { return head; } // 最大値を求める.max({}) の代わり.これにしないと vscode が壊れる. template <class Head, class... Tail> ll _max(Head &&head, Tail &&...tail) { return max((ll)head, _max((tail)...)); } // _min の終端. template <class Head> ll _min(Head &&head) { return head; } // 最小値を求める.min({}) の代わり.これにしないと vscode が壊れる. template <class Head, class... Tail> ll _min(Head &&head, Tail &&...tail) { return min((ll)head, _min((tail)...)); } // chmax (更新「される」かもしれない値が前) template <typename T, typename U, typename Comp = less<>> bool chmax(T &xmax, const U &x, Comp comp = {}) { if (comp(xmax, x)) { xmax = x; return true; } return false; } // chmin (更新「される」かもしれない値が前) template <typename T, typename U, typename Comp = less<>> bool chmin(T &xmin, const U &x, Comp comp = {}) { if (comp(x, xmin)) { xmin = x; return true; } return false; } // container 内の最初の element のインデックスを探す template <class Container> ll indexof(const Container &container, const typename Container::value_type &element) { auto iter = find(container.begin(), container.end(), element); size_t index = distance(container.begin(), iter); if (index == container.size()) { index = -1; } return index; } // ローカル用 #define DEBUG_ #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif struct AtCoderInitialize { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; AtCoderInitialize() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << fixed << setprecision(IOS_PREC); if (AUTOFLUSH) cout << unitbuf; } } ATCODER_INITIALIZE; /* #endregion */ // 自動で MOD を取る整数 struct mint { ll x; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint &operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } // O(log(t)) mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); // ⌊t/2⌋ 乗 a *= a; // ⌊t/2⌋*2 乗 if (t & 1) // ⌊t/2⌋*2 == t-1 のとき a *= *this; // ⌊t/2⌋*2+1 乗 => t 乗 return a; } // for prime mod mint inv() const { return pow(MOD - 2); // オイラーの定理から, x^(-1) ≡ x^(p-2) } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; // mint 入力 istream &operator>>(istream &is, mint &x) { is >> x.x; return is; } // mint 出力 ostream &operator<<(ostream &os, mint x) { os << x.x; return os; } /* 二項係数計算用クラス. */ class Combinaion { private: /* テーブルの大きさを規定する.(MAX)! まで計算できる. */ static constexpr ll MAX = 1e6; /* 階乗を格納するテーブル.fac[n] := n! % MOD. */ vector<mint> fac; /* 階乗の逆元を格納するテーブル.finv[n] := (fac[n])^(-1). */ vector<mint> finv; /* 各種テーブルを初期化する. */ void init(int n) { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; REPM(i, 2, n) { fac[i] = fac[i - 1] * i; } finv[n] = fac[n].inv(); REPR(i, n, 2) { finv[i - 1] = finv[i] * i; } } public: /* コンストラクタ. */ Combinaion(int n = MAX) : fac(n + 1), finv(n + 1) { init(n); } /* 二項係数 nCk % MOD を計算する. */ mint operator()(ll n, ll k) const { assert(n < MAX); assert(k < MAX); if (n < k || n < 0 || k < 0) return 0; return fac[n] * finv[k] * finv[n - k]; } }; /** Problem */ void solve() { ll n, k; cin >> n >> k; vll a(n, 0); cin >> a; sort(ALL(a)); vector<mint> am(n); REP(i, 0, n) { am[i] = a[i]; } Combinaion c; mint ret = 0; REP(i, 0, n - k + 1) REP(j, i + k - 1, n) { mint temp = c(j - i - 1, k - 2) * (a[j] - a[i]); ret += temp; // dump(temp, ret); } cout << ret << endl; } /** * エントリポイント. * @return 0. */ int main() { solve(); return 0; }
/* #region Head */ #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pll = pair<ll, ll>; using vll = vector<ll>; using vvll = vector<vll>; using vd = vector<double>; using vvd = vector<vd>; using vs = vector<string>; using vvs = vector<vs>; #define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i)) #define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i)) #define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i)) #define REPD(i, m, n, d) \ for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d)) #define REPMD(i, m, n, d) \ for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d)) #define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define ALL(x) begin(x), end(x) #define SIZE(x) ((ll)(x).size()) constexpr ll INF = 1'010'000'000'000'000'017LL; constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7 constexpr double EPS = 1e-12; constexpr double PI = 3.14159265358979323846; // vector入力 template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // vector出力 template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { ll len = SIZE(vec); os << "{"; for (int i = 0; i < len; i++) os << vec[i] << (i == len - 1 ? "" : ", "); os << "}"; return os; } // pair入力 template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &pair_var) { is >> pair_var.first >> pair_var.second; return is; } // pair出力 template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // map出力 template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; REPI(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set 出力 template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; REPI(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } // dump #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(move(tail)...); } // _max の終端. template <class Head> ll _max(Head &&head) { return head; } // 最大値を求める.max({}) の代わり.これにしないと vscode が壊れる. template <class Head, class... Tail> ll _max(Head &&head, Tail &&...tail) { return max((ll)head, _max((tail)...)); } // _min の終端. template <class Head> ll _min(Head &&head) { return head; } // 最小値を求める.min({}) の代わり.これにしないと vscode が壊れる. template <class Head, class... Tail> ll _min(Head &&head, Tail &&...tail) { return min((ll)head, _min((tail)...)); } // chmax (更新「される」かもしれない値が前) template <typename T, typename U, typename Comp = less<>> bool chmax(T &xmax, const U &x, Comp comp = {}) { if (comp(xmax, x)) { xmax = x; return true; } return false; } // chmin (更新「される」かもしれない値が前) template <typename T, typename U, typename Comp = less<>> bool chmin(T &xmin, const U &x, Comp comp = {}) { if (comp(x, xmin)) { xmin = x; return true; } return false; } // container 内の最初の element のインデックスを探す template <class Container> ll indexof(const Container &container, const typename Container::value_type &element) { auto iter = find(container.begin(), container.end(), element); size_t index = distance(container.begin(), iter); if (index == container.size()) { index = -1; } return index; } // ローカル用 #define DEBUG_ #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif struct AtCoderInitialize { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; AtCoderInitialize() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << fixed << setprecision(IOS_PREC); if (AUTOFLUSH) cout << unitbuf; } } ATCODER_INITIALIZE; /* #endregion */ // 自動で MOD を取る整数 struct mint { ll x; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint &operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } // O(log(t)) mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); // ⌊t/2⌋ 乗 a *= a; // ⌊t/2⌋*2 乗 if (t & 1) // ⌊t/2⌋*2 == t-1 のとき a *= *this; // ⌊t/2⌋*2+1 乗 => t 乗 return a; } // for prime mod mint inv() const { return pow(MOD - 2); // オイラーの定理から, x^(-1) ≡ x^(p-2) } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; // mint 入力 istream &operator>>(istream &is, mint &x) { is >> x.x; return is; } // mint 出力 ostream &operator<<(ostream &os, mint x) { os << x.x; return os; } /* 二項係数計算用クラス. */ class Combinaion { private: /* テーブルの大きさを規定する.(MAX)! まで計算できる. */ static constexpr ll MAX = 1e6; /* 階乗を格納するテーブル.fac[n] := n! % MOD. */ vector<mint> fac; /* 階乗の逆元を格納するテーブル.finv[n] := (fac[n])^(-1). */ vector<mint> finv; /* 各種テーブルを初期化する. */ void init(int n) { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; REPM(i, 2, n) { fac[i] = fac[i - 1] * i; } finv[n] = fac[n].inv(); REPR(i, n, 2) { finv[i - 1] = finv[i] * i; } } public: /* コンストラクタ. */ Combinaion(int n = MAX) : fac(n + 1), finv(n + 1) { init(n); } /* 二項係数 nCk % MOD を計算する. */ mint operator()(ll n, ll k) const { assert(n < MAX); assert(k < MAX); if (n < k || n < 0 || k < 0) return 0; return fac[n] * finv[k] * finv[n - k]; } }; /** Problem */ void solve() { ll n, k; cin >> n >> k; vll a(n, 0); cin >> a; sort(ALL(a)); vector<mint> am(n); REP(i, 0, n) { am[i] = a[i]; } Combinaion c; mint ret = 0; // REP(i, 0, n - k + 1) // REP(j, i + k - 1, n) // { // mint temp = c(j - i - 1, k - 2) * (a[j] - a[i]); // ret += temp; // // dump(temp, ret); // } REP(i, 0, n) { mint maxc = (i >= k - 1) ? c(i, k - 1) : 0; mint minc = (i <= n - k) ? c(n - i - 1, k - 1) : 0; ret += (maxc - minc) * a[i]; } cout << ret << endl; } /** * エントリポイント. * @return 0. */ int main() { solve(); return 0; }
replace
295
300
295
306
TLE
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define ll long long #define MOD 1000000007 using namespace std; ll facs[(ll)1e5 + 10]; // x^n in mod ll modpow(ll x, ll n, ll mod) { if (n == 0) return 1; if (n % 2 == 0) { return modpow(x * x % mod, n / 2, mod) % mod; } else { return x * modpow(x, (n - 1), mod) % mod; } } // n! in mod ll fac(ll n) { return facs[n]; } // nCr in mod ll comb(ll n, ll r) { if (n < 0 || n < r || r < 0) return 0; ll fn = fac(n); ll fr = fac(r); ll fn_r = fac(n - r); ll d = (modpow(fr, MOD - 2, MOD) * modpow(fn_r, MOD - 2, MOD)) % MOD; ll u = fn % MOD; ll result = u * d % MOD; return result; } int main() { ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); facs[0] = 1; rep(i, n + 10) { facs[i + 1] = (facs[i] * (i + 1)) % MOD; } ll result = 0; rep(i, n - k + 1) { ll cc = comb(n - 1 - i, k - 1); result += cc * a[n - 1 - i]; result %= MOD; result -= cc * a[i]; while (result < 0) result += MOD; result %= MOD; } cout << result; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define ll long long #define MOD 1000000007 using namespace std; ll facs[(ll)1e5 + 10]; // x^n in mod ll modpow(ll x, ll n, ll mod) { if (n == 0) return 1; if (n % 2 == 0) { return modpow(x * x % mod, n / 2, mod) % mod; } else { return x * modpow(x, (n - 1), mod) % mod; } } // n! in mod ll fac(ll n) { return facs[n]; } // nCr in mod ll comb(ll n, ll r) { if (n < 0 || n < r || r < 0) return 0; ll fn = fac(n); ll fr = fac(r); ll fn_r = fac(n - r); ll d = (modpow(fr, MOD - 2, MOD) * modpow(fn_r, MOD - 2, MOD)) % MOD; ll u = fn % MOD; ll result = u * d % MOD; return result; } int main() { ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); facs[0] = 1; rep(i, n + 10) { facs[i + 1] = (facs[i] * (i + 1)) % MOD; } ll result = 0; rep(i, n - k + 1) { ll cc = comb(n - 1 - i, k - 1); result += cc * a[n - 1 - i]; result %= MOD; result -= cc * a[i]; result += MOD; result += MOD; result %= MOD; } cout << result; return 0; }
replace
61
63
61
63
TLE
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using i64 = int_fast64_t; #define rep(i, N) for (int(i) = 0; (i) < (N); (i)++) #define all(v) (v).begin(), (v).end() #define eb emplace_back struct mint { using i64 = int_fast64_t; i64 x; const i64 MOD = 1000000007; mint(i64 y = (i64)0) noexcept : x(y % MOD) { if (x < 0) x += MOD; } mint operator+() const noexcept { return x; } mint operator-() const noexcept { return x ? MOD - x : 0; } mint operator+(const mint y) const noexcept { return (mint) * this += y; } mint operator-(const mint y) const noexcept { return (mint) * this -= y; } mint operator*(const mint y) const noexcept { return (mint) * this *= y; } mint operator/(const mint y) const noexcept { return (mint) * this /= y; } mint operator=(const mint y) noexcept { x = y.x; if (x >= MOD) x -= MOD; if (x < 0) x += MOD; return *this; } mint operator=(const i64 y) noexcept { x = y; if (x >= MOD) x -= MOD; if (x < 0) x += MOD; return *this; } mint &operator+=(const mint y) noexcept { x += y.x; if (x >= MOD) x -= MOD; return *this; } mint &operator-=(const mint y) noexcept { x -= y.x; if (x < 0) x += MOD; return *this; } mint &operator*=(const mint y) noexcept { x *= y.x; if (x >= MOD) x %= MOD; return *this; } mint &operator/=(mint y) noexcept { i64 exp = MOD - 2; while (exp) { if (exp & 1) *this *= y; y *= y; exp /= 2; } return *this; } friend std::ostream &operator<<(std::ostream &os, const mint &m) noexcept { os << m.x; return os; } friend std::istream &operator>>(std::istream &in, mint &m) noexcept { const int mod = 1000000007; in >> m.x; if (m.x < 0) m.x += mod; if (m.x >= mod) m.x %= mod; return in; } friend mint mod_pow(mint n, i64 k) noexcept { mint res = 1; while (k) { if (k & 1) res *= n; n *= n; k >>= 1; } return res; } }; class Binomial { private: #define MOD 1000000007 vector<mint> fac, finv, inv; size_t sz; mint for_big_N(const int &n, const int &r) const noexcept { mint res = 1; const int k = min(r, n - r); for (int i = 0; i < k; i++) { res *= mint(n - i) / mint(k - i); } return res; } public: Binomial operator=(const Binomial) = delete; Binomial(const Binomial &) = delete; Binomial(size_t s = 510000) noexcept : sz(s), fac(s, 1), inv(s, 1), finv(s, 1) { fac[0] = fac[1] = 1; inv[1] = 1; finv[0] = finv[1] = 1; for (int i = 2; i < sz; i++) { fac[i] = fac[i - 1] * mint(i); inv[i] = -inv[MOD % i] * mint(MOD / i); finv[i] = finv[i - 1] * inv[i]; } } mint operator()(const int &n, const int &r) const noexcept { if (n < r) return mint(0); if (n < 0 || r < 0) return mint(0); if (n >= sz) return for_big_N(n, r); return fac[n] * finv[n - r] * finv[r]; } }; int main() { i64 N, K; cin >> N >> K; vector<i64> A(N); rep(i, N) cin >> A[i]; sort(all(A)); Binomial binomial; mint ans = 0; for (int i = 1; i <= N - K + 1; i++) { ans += mint(A[N - i]) * binomial(N - i, K - 1); ans -= mint(A[i - 1]) * binomial(N - i, K - 1); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using i64 = int_fast64_t; #define rep(i, N) for (int(i) = 0; (i) < (N); (i)++) #define all(v) (v).begin(), (v).end() #define eb emplace_back struct mint { using i64 = int_fast64_t; i64 x; const i64 MOD = 1000000007; mint(const i64 y = (i64)0) noexcept : x(y) { if (x >= MOD) x %= MOD; if (x < 0) x += MOD; } mint operator+() const noexcept { return x; } mint operator-() const noexcept { return x ? MOD - x : 0; } mint operator+(const mint y) const noexcept { return (mint) * this += y; } mint operator-(const mint y) const noexcept { return (mint) * this -= y; } mint operator*(const mint y) const noexcept { return (mint) * this *= y; } mint operator/(const mint y) const noexcept { return (mint) * this /= y; } mint operator=(const mint y) noexcept { x = y.x; if (x >= MOD) x -= MOD; if (x < 0) x += MOD; return *this; } mint operator=(const i64 y) noexcept { x = y; if (x >= MOD) x -= MOD; if (x < 0) x += MOD; return *this; } mint &operator+=(const mint y) noexcept { x += y.x; if (x >= MOD) x -= MOD; return *this; } mint &operator-=(const mint y) noexcept { x -= y.x; if (x < 0) x += MOD; return *this; } mint &operator*=(const mint y) noexcept { x *= y.x; if (x >= MOD) x %= MOD; return *this; } mint &operator/=(mint y) noexcept { i64 exp = MOD - 2; while (exp) { if (exp & 1) *this *= y; y *= y; exp /= 2; } return *this; } friend std::ostream &operator<<(std::ostream &os, const mint &m) noexcept { os << m.x; return os; } friend std::istream &operator>>(std::istream &in, mint &m) noexcept { const int mod = 1000000007; in >> m.x; if (m.x < 0) m.x += mod; if (m.x >= mod) m.x %= mod; return in; } friend mint mod_pow(mint n, i64 k) noexcept { mint res = 1; while (k) { if (k & 1) res *= n; n *= n; k >>= 1; } return res; } }; class Binomial { private: #define MOD 1000000007 vector<mint> fac, finv, inv; size_t sz; mint for_big_N(const int &n, const int &r) const noexcept { mint res = 1; const int k = min(r, n - r); for (int i = 0; i < k; i++) { res *= mint(n - i) / mint(k - i); } return res; } public: Binomial operator=(const Binomial) = delete; Binomial(const Binomial &) = delete; Binomial(size_t s = 510000) noexcept : sz(s), fac(s, 1), inv(s, 1), finv(s, 1) { fac[0] = fac[1] = 1; inv[1] = 1; finv[0] = finv[1] = 1; for (int i = 2; i < sz; i++) { fac[i] = fac[i - 1] * mint(i); inv[i] = -inv[MOD % i] * mint(MOD / i); finv[i] = finv[i - 1] * inv[i]; } } mint operator()(const int &n, const int &r) const noexcept { if (n < r) return mint(0); if (n < 0 || r < 0) return mint(0); if (n >= sz) return for_big_N(n, r); return fac[n] * finv[n - r] * finv[r]; } }; int main() { i64 N, K; cin >> N >> K; vector<i64> A(N); rep(i, N) cin >> A[i]; sort(all(A)); Binomial binomial; mint ans = 0; for (int i = 1; i <= N - K + 1; i++) { ans += mint(A[N - i]) * binomial(N - i, K - 1); ans -= mint(A[i - 1]) * binomial(N - i, K - 1); } cout << ans << endl; }
replace
12
13
12
15
-8
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define lvector vector<ll> #define lque queue<ll> #define lpque priority_queue<ll> #define dlpque priority_queue<ll, lvector, greater<ll>> #define P pair<ll, ll> #define ALL(a) (a).begin(), (a).end() #define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i)) #define print(a) cout << (a) << endl ll mod = 1e9 + 7; ll com(ll N, ll K, ll L) { ll d = 1, e = (L * K) % mod, m_ = mod - 2; while (m_ != 0) { if (m_ & 1) d = (d * e) % mod; e = e * e % mod; m_ = m_ >> 1; } return (N * d) % mod; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, k, ans = 0; cin >> n >> k; lvector A(n, 0), mf(n + 1, 0); rep(i, n) cin >> A[i]; sort(ALL(A)); mf[0] = 1; for (ll i = 1; i <= n; ++i) mf[i] = (mf[i - 1] * i) % mod; for (ll pos = 0; pos < n; pos++) { if (n - pos >= k) ans -= A[pos] * com(mf[n - 1 - pos], mf[k - 1], mf[n - k - pos]); while (ans < 0) ans += mod; if (pos >= k - 1) ans += A[pos] * com(mf[pos], mf[k - 1], mf[pos + 1 - k]); ans %= mod; } print(ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define lvector vector<ll> #define lque queue<ll> #define lpque priority_queue<ll> #define dlpque priority_queue<ll, lvector, greater<ll>> #define P pair<ll, ll> #define ALL(a) (a).begin(), (a).end() #define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i)) #define print(a) cout << (a) << endl ll mod = 1e9 + 7; ll com(ll N, ll K, ll L) { ll d = 1, e = (L * K) % mod, m_ = mod - 2; while (m_ != 0) { if (m_ & 1) d = (d * e) % mod; e = e * e % mod; m_ = m_ >> 1; } return (N * d) % mod; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, k, ans = 0; cin >> n >> k; lvector A(n, 0), mf(n + 1, 0); rep(i, n) cin >> A[i]; sort(ALL(A)); mf[0] = 1; for (ll i = 1; i <= n; ++i) mf[i] = (mf[i - 1] * i) % mod; for (ll pos = 0; pos <= n - k; ++pos) ans = (ans - A[pos] * com(mf[n - 1 - pos], mf[k - 1], mf[n - k - pos])) % mod; for (ll pos = k - 1; pos < n; ++pos) ans += (A[pos] * com(mf[pos], mf[k - 1], mf[pos + 1 - k])) % mod; ans %= mod; while (ans < 0) ans += mod; print(ans); return 0; }
replace
36
45
36
44
TLE
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 3, MOD = 1e9 + 7; long long f[N], cumNCK[N]; long long power(int b, int e) { long long ret = 1; while (e) { while (e % 2 == 0) { e /= 2; b = (1LL * b * b) % MOD; } --e; ret = (ret * b) % MOD; } return ret; } long long nCr(int n, int r) { assert(n >= r); return (f[n] * power((f[r] * f[n - r]) % MOD, MOD - 2)) % MOD; } int n, k, a[N]; int main() { // freopen("input.in", "r", stdin); f[0] = 1; for (int i = 1; i < N; ++i) f[i] = (f[i - 1] * i) % MOD; scanf("%d %d", &n, &k); for (int i = 0; i < n; ++i) scanf("%d", a + i); if (k == 1) { puts("0"); return 0; } sort(a, a + n); for (int i = 0; i <= n * 2; ++i) { if (k - 2 <= i) { if (i) cumNCK[i] = (cumNCK[i - 1] + nCr(i, k - 2)) % MOD; else cumNCK[i] = nCr(i, k - 2); // cout << nCr(i, k - 2) << " " << cumNCK[i] << //endl; } } long long res = 0; for (int i = n - 1; i >= 0; --i) { int len = i - k + 2; // cout << a[i] << " " << len << ", "; if (len >= 0) { res = (res + a[i] * cumNCK[len + k - 2 - 1]) % MOD; } // cout << endl; len = n - (i + k) + 1; if (len >= 0) { res = ((res - a[i] * cumNCK[len + k - 2 - 1]) % MOD + MOD) % MOD; // cout << a[i] << " " << len << endl; } } printf("%lld\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 3, MOD = 1e9 + 7; long long f[N], cumNCK[N]; long long power(int b, int e) { long long ret = 1; while (e) { while (e % 2 == 0) { e /= 2; b = (1LL * b * b) % MOD; } --e; ret = (ret * b) % MOD; } return ret; } long long nCr(int n, int r) { assert(n >= r); return (f[n] * power((f[r] * f[n - r]) % MOD, MOD - 2)) % MOD; } int n, k, a[N]; int main() { // freopen("input.in", "r", stdin); f[0] = 1; for (int i = 1; i < N; ++i) f[i] = (f[i - 1] * i) % MOD; scanf("%d %d", &n, &k); for (int i = 0; i < n; ++i) scanf("%d", a + i); if (k == 1) { puts("0"); return 0; } sort(a, a + n); for (int i = 0; i <= n * 2; ++i) { if (k - 2 <= i) { if (i) cumNCK[i] = (cumNCK[i - 1] + nCr(i, k - 2)) % MOD; else cumNCK[i] = nCr(i, k - 2); // cout << nCr(i, k - 2) << " " << cumNCK[i] << //endl; } } long long res = 0; for (int i = n - 1; i >= 0; --i) { int len = i - k + 2; // cout << a[i] << " " << len << ", "; if (len >= 0) { res = (res + a[i] * cumNCK[len + k - 2 - 1]) % MOD; } // cout << endl; len = n - (i + k) + 1; if (len >= 0) { res = ((res - a[i] * cumNCK[len + k - 2 - 1]) % MOD + MOD) % MOD; // cout << a[i] << " " << len << endl; } } printf("%lld\n", res); return 0; }
replace
3
4
3
4
0
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; const ull mod = 1e9 + 7; #define REP(i, n) for (int i = 0; i < (int)n; ++i) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> v) { os << "(" << v.first << ", " << v.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> v) { for (int i = 0; i < (int)v.size(); i++) { if (i > 0) { os << " "; } os << v[i]; } return os; } template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) { for (int i = 0; i < (int)v.size(); i++) { if (i > 0) { os << endl; } os << v[i]; } return os; } const ll N_MAX = 100005; ll inv[N_MAX], fac[N_MAX], finv[N_MAX]; void make() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < N_MAX; i++) { inv[i] = mod - inv[mod % i] * (mod / i) % mod; fac[i] = fac[i - 1] * (ll)i % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } ll combination(ll C, ll D) { if (C < D || C < 1) return 0; return fac[C] * (finv[D] * finv[C - D] % mod) % mod; } int main() { cin.tie(0); ios::sync_with_stdio(false); make(); ll N, K; cin >> N >> K; vector<ll> A(N); REP(i, N) cin >> A[i]; sort(A.begin(), A.end()); vector<ll> B(N, 0); REP(i, 10) B[i] = combination(i, K - 1); ll res = 0; REP(i, N) { ll tmp = B[i] * A[i]; if (tmp < 0) { ll kai = (abs(tmp) / mod); tmp += (kai + 2) * mod; } tmp %= mod; res += tmp; res %= mod; } REP(i, N) { ll tmp = B[N - i - 1] * A[i]; if (tmp < 0) { ll kai = (abs(tmp) / mod); tmp += (kai + 2) * mod; } tmp %= mod; tmp = (tmp + mod) % mod; res -= tmp; res = (res + mod) % mod; } cout << res << endl; // cout << B << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; const ull mod = 1e9 + 7; #define REP(i, n) for (int i = 0; i < (int)n; ++i) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> v) { os << "(" << v.first << ", " << v.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> v) { for (int i = 0; i < (int)v.size(); i++) { if (i > 0) { os << " "; } os << v[i]; } return os; } template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) { for (int i = 0; i < (int)v.size(); i++) { if (i > 0) { os << endl; } os << v[i]; } return os; } const ll N_MAX = 100005; ll inv[N_MAX], fac[N_MAX], finv[N_MAX]; void make() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < N_MAX; i++) { inv[i] = mod - inv[mod % i] * (mod / i) % mod; fac[i] = fac[i - 1] * (ll)i % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } ll combination(ll C, ll D) { if (C < D || C < 1) return 0; return fac[C] * (finv[D] * finv[C - D] % mod) % mod; } int main() { cin.tie(0); ios::sync_with_stdio(false); make(); ll N, K; cin >> N >> K; vector<ll> A(N); REP(i, N) cin >> A[i]; sort(A.begin(), A.end()); vector<ll> B(N, 0); REP(i, N) B[i] = combination(i, K - 1); ll res = 0; REP(i, N) { ll tmp = B[i] * A[i]; if (tmp < 0) { ll kai = (abs(tmp) / mod); tmp += (kai + 2) * mod; } tmp %= mod; res += tmp; res %= mod; } REP(i, N) { ll tmp = B[N - i - 1] * A[i]; if (tmp < 0) { ll kai = (abs(tmp) / mod); tmp += (kai + 2) * mod; } tmp %= mod; tmp = (tmp + mod) % mod; res -= tmp; res = (res + mod) % mod; } cout << res << endl; // cout << B << endl; return 0; }
replace
73
74
73
74
0
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(v) v.begin(), v.end() using namespace std; typedef long long ll; const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; // combination mod prime(mintが必要) // https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619 // nCkを求めて結果をansに格納したい場合、 // combination c(n); // mint ans = c(n, k); // cout << ans.x << endl; struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; int main() { int n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) { int tmp; cin >> tmp; a[i] = tmp; } sort(all(a)); mint ans = 0; combination c(n); rep(i, n) { combination c(i); } for (int i = k - 1; i < n; i++) { mint tmp = c(i, k - 1); tmp *= a[i]; ans += tmp; } for (int i = 0; i < n - k + 1; i++) { mint tmp = c(n - i - 1, k - 1); tmp *= a[i]; ans -= tmp; } cout << ans.x << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(v) v.begin(), v.end() using namespace std; typedef long long ll; const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; // combination mod prime(mintが必要) // https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619 // nCkを求めて結果をansに格納したい場合、 // combination c(n); // mint ans = c(n, k); // cout << ans.x << endl; struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; int main() { int n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) { int tmp; cin >> tmp; a[i] = tmp; } sort(all(a)); mint ans = 0; combination c(n); for (int i = k - 1; i < n; i++) { mint tmp = c(i, k - 1); tmp *= a[i]; ans += tmp; } for (int i = 0; i < n - k + 1; i++) { mint tmp = c(n - i - 1, k - 1); tmp *= a[i]; ans -= tmp; } cout << ans.x << endl; return 0; }
delete
93
94
93
93
TLE
p02804
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ull long long using namespace std; long long fac[100005]; long long infact[100005]; ull mul(ull a, ull b, ull c) { ull dev = 0, tmp = a % c; while (b) { if (b & 1) if ((dev += tmp) >= c) dev -= c; if ((tmp <<= 1) >= c) tmp -= c; b >>= 1; } return dev; } ull exp(ull a, ull b, ull c) { if (b == 0) return 1; ull dev = exp(a, b >> 1, c); dev = mul(dev, dev, c); if (b & 1) dev = mul(dev, a, c); return dev; } // Returns n^(-1) mod p int modInverse(int n, int p) { return exp(n, p - 2, p); } // Returns nCr % p using Fermat's little // theorem. int nCrModP(int n, int r, int p) { // Base case if (r == 0) return 1; return mul(fac[n], mul(infact[r], infact[n - r], p), p); } int main() { int p = 1000000007; fac[0] = 1; infact[0] = 1; for (int i = 1; i <= 100001; i++) { fac[i] = (fac[i - 1] * i) % p; infact[i] = modInverse(fac[i], p); } int n, k; cin >> n >> k; vector<int> a(n); for (int &i : a) cin >> i; sort(a.begin(), a.end()); long long ans = 0; for (int i = 0; i < n; i++) { if (i >= k - 1) { ans += mul(nCrModP(i, k - 1, p), a[i], p); } ans %= p; if (n - i - 1 >= k - 1) { ans -= mul(nCrModP(n - i - 1, k - 1, p), a[i], p); } ans %= p; } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #define ull long long using namespace std; long long fac[100005]; long long infact[100005]; ull mul(ull a, ull b, ull c) { return ((a % c) * (b % c)) % c; } ull exp(ull a, ull b, ull c) { if (b == 0) return 1; ull dev = exp(a, b >> 1, c); dev = mul(dev, dev, c); if (b & 1) dev = mul(dev, a, c); return dev; } // Returns n^(-1) mod p int modInverse(int n, int p) { return exp(n, p - 2, p); } // Returns nCr % p using Fermat's little // theorem. int nCrModP(int n, int r, int p) { // Base case if (r == 0) return 1; return mul(fac[n], mul(infact[r], infact[n - r], p), p); } int main() { int p = 1000000007; fac[0] = 1; infact[0] = 1; for (int i = 1; i <= 100001; i++) { fac[i] = (fac[i - 1] * i) % p; infact[i] = modInverse(fac[i], p); } int n, k; cin >> n >> k; vector<int> a(n); for (int &i : a) cin >> i; sort(a.begin(), a.end()); long long ans = 0; for (int i = 0; i < n; i++) { if (i >= k - 1) { ans += mul(nCrModP(i, k - 1, p), a[i], p); } ans %= p; if (n - i - 1 >= k - 1) { ans -= mul(nCrModP(n - i - 1, k - 1, p), a[i], p); } ans %= p; } cout << ans << '\n'; return 0; }
replace
7
19
7
8
TLE
p02804
C++
Runtime Error
// Problem : E - Max-Min Sums // Contest : AtCoder Beginner Contest 151 // URL : https://atcoder.jp/contests/abc151/tasks/abc151_e // Memory Limit : 1024.000000 MB // Time Limit : 2000.000000 milisec // Powered by CP Editor (https://github.com/coder3101/cp-editor) #include <bits/stdc++.h> using namespace std; int N, K; long long MOD = 1e9 + 7; long long arr[1000005]; long long fact[1000005]; long long mult(long long a, long long b) { return (a * b) % MOD; } long long add(long long a, long long b) { return (a + b) % MOD; } long long qikpow(long long b, long long e) { if (!e) { return 1; } long long ret = qikpow(b, e / 2); ret = mult(ret, ret); if (e & 1) { ret = mult(ret, b); } return ret; } long long divd(long long a, long long b) { return mult(a, qikpow(b, MOD - 2)); } long long sub(long long a, long long b) { a = add(a, -b); if (a < 0) { a += MOD; } return a; } long long choose(long long n, long long r) { return divd(fact[n], mult(fact[r], fact[n - r])); } int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> K; fact[0] = 1; for (int i = 1; i <= N; i++) { fact[i] = mult(i, fact[i - 1]); cin >> arr[i]; } sort(arr + 1, arr + 1 + N); long long ans = 0; for (int i = 1; i <= N; i++) { ans = add(ans, mult(arr[i], choose(i - 1, K - 1))); ans = sub(ans, mult(arr[i], choose(N - i, K - 1))); } cout << ans << "\n"; }
// Problem : E - Max-Min Sums // Contest : AtCoder Beginner Contest 151 // URL : https://atcoder.jp/contests/abc151/tasks/abc151_e // Memory Limit : 1024.000000 MB // Time Limit : 2000.000000 milisec // Powered by CP Editor (https://github.com/coder3101/cp-editor) #include <bits/stdc++.h> using namespace std; int N, K; long long MOD = 1e9 + 7; long long arr[1000005]; long long fact[1000005]; long long mult(long long a, long long b) { return (a * b) % MOD; } long long add(long long a, long long b) { return (a + b) % MOD; } long long qikpow(long long b, long long e) { if (!e) { return 1; } long long ret = qikpow(b, e / 2); ret = mult(ret, ret); if (e & 1) { ret = mult(ret, b); } return ret; } long long divd(long long a, long long b) { return mult(a, qikpow(b, MOD - 2)); } long long sub(long long a, long long b) { a = add(a, -b); if (a < 0) { a += MOD; } return a; } long long choose(long long n, long long r) { if (n - r < 0) { return 0; } return divd(fact[n], mult(fact[r], fact[n - r])); } int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> K; fact[0] = 1; for (int i = 1; i <= N; i++) { fact[i] = mult(i, fact[i - 1]); cin >> arr[i]; } sort(arr + 1, arr + 1 + N); long long ans = 0; for (int i = 1; i <= N; i++) { ans = add(ans, mult(arr[i], choose(i - 1, K - 1))); ans = sub(ans, mult(arr[i], choose(N - i, K - 1))); } cout << ans << "\n"; }
insert
44
44
44
47
0
p02804
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using llong = long long int; #define rep(i, n) for (int i = 0; i < (n); ++i) #define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr) #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() const int MOD = 1000000007; const int INF = 1000000000; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int MAX = 100001; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main(int argc, char *argv[]) { cin.tie(0); ios::sync_with_stdio(false); COMinit(); ifstream in("input.txt"); cin.rdbuf(in.rdbuf()); llong n, k; cin >> n >> k; vector<llong> A(n); rep(i, n) cin >> A[i]; sort(all(A)); if (k == 1) { cout << 0 << endl; return 0; } vector<llong> sumf(n + 1, 0), sume(n + 1, 0); rep(i, n) { sumf[i + 1] += sumf[i] + (A[i] + MOD); sumf[i + 1] %= MOD; } reverse(all(A)); rep(i, n) { sume[i + 1] += sume[i] + (A[i] + MOD); sume[i + 1] %= MOD; } int l = n - 2, r = k - 2, i = 0; llong ans = 0; while (l >= r) { ++i; ans += COM(l, r) * (sume[i] - sumf[i] + MOD); ans %= MOD; --l; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using llong = long long int; #define rep(i, n) for (int i = 0; i < (n); ++i) #define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr) #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() const int MOD = 1000000007; const int INF = 1000000000; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int MAX = 100001; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main(int argc, char *argv[]) { cin.tie(0); ios::sync_with_stdio(false); COMinit(); // ifstream in("input.txt"); // cin.rdbuf(in.rdbuf()); llong n, k; cin >> n >> k; vector<llong> A(n); rep(i, n) cin >> A[i]; sort(all(A)); if (k == 1) { cout << 0 << endl; return 0; } vector<llong> sumf(n + 1, 0), sume(n + 1, 0); rep(i, n) { sumf[i + 1] += sumf[i] + (A[i] + MOD); sumf[i + 1] %= MOD; } reverse(all(A)); rep(i, n) { sume[i + 1] += sume[i] + (A[i] + MOD); sume[i + 1] %= MOD; } int l = n - 2, r = k - 2, i = 0; llong ans = 0; while (l >= r) { ++i; ans += COM(l, r) * (sume[i] - sumf[i] + MOD); ans %= MOD; --l; } cout << ans << endl; return 0; }
replace
44
46
44
46
0
p02804
Python
Runtime Error
N, K = map(int, input().split()) A = list(map(int, input().split())) MOD = 10**9 + 7 inv_t = [0, 1] + [0] * (N - 2) for i in range(2, N): inv_t[i] = inv_t[MOD % i] * (MOD - MOD // i) % MOD A.sort() ans = 0 C = 1 for i in range(K, N + 1): ans += (A[i - 1] - A[N - i]) * C ans %= MOD C = C * i * inv_t[i - K + 1] C %= MOD print(ans)
N, K = map(int, input().split()) A = list(map(int, input().split())) MOD = 10**9 + 7 inv_t = [0, 1] + [0] * (N - 1) for i in range(2, N + 1): inv_t[i] = inv_t[MOD % i] * (MOD - MOD // i) % MOD A.sort() ans = 0 C = 1 for i in range(K, N + 1): ans += (A[i - 1] - A[N - i]) * C ans %= MOD C = C * i * inv_t[i - K + 1] C %= MOD print(ans)
replace
4
6
4
6
0
p02804
Python
Time Limit Exceeded
MOD = 10**9 + 7 def prepare(n): global MOD modFacts = [0] * (n + 1) modFacts[0] = 1 for i in range(n): modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD invs = [1] * (n + 1) invs[n] = pow(modFacts[n], MOD - 2, MOD) for i in range(n, 1, -1): invs[i - 1] = (invs[i] * i) % MOD return modFacts, invs N, K = map(int, input().split()) A = list(map(int, input().split())) modFacts, invs = prepare(N) # mod2 = [0] * (N + 1) # mod2[0] = 1 # for i in range(N): # mod2[i + 1] = mod2[i] * 2 # mod2[i + 1] %= MOD A.sort() ans = 0 for i in range(N): for j in range(K - 1, N - i): x = A[i + j] y = A[i] ans += (x - y) * modFacts[j - 1] * invs[j - 1 - (K - 2)] * invs[K - 2] ans %= MOD print(ans)
MOD = 10**9 + 7 def prepare(n): global MOD modFacts = [0] * (n + 1) modFacts[0] = 1 for i in range(n): modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD invs = [1] * (n + 1) invs[n] = pow(modFacts[n], MOD - 2, MOD) for i in range(n, 1, -1): invs[i - 1] = (invs[i] * i) % MOD return modFacts, invs N, K = map(int, input().split()) A = list(map(int, input().split())) modFacts, invs = prepare(N) # mod2 = [0] * (N + 1) # mod2[0] = 1 # for i in range(N): # mod2[i + 1] = mod2[i] * 2 # mod2[i + 1] %= MOD A.sort() ans = 0 for i in range(K - 1, N): n = i r = K - 1 num = (modFacts[n] * invs[n - r] * invs[r]) % MOD ans += A[i] * num ans %= MOD for i in range(N - K + 1): n = N - (i + 1) r = K - 1 num = (modFacts[n] * invs[n - r] * invs[r]) % MOD ans -= A[i] * num ans %= MOD print(ans)
replace
31
37
31
46
TLE
p02804
Python
Time Limit Exceeded
N, K = map(int, input().split()) A = list(map(int, input().split())) MOD = 10**9 + 7 A.sort() factorial = [1] for i in range(1, N + 1): factorial.append(factorial[-1] * i) inv_factorial = [-1] * (N + 1) inv_factorial[-1] = pow(factorial[-1], MOD - 2, MOD) for i in reversed(range(N)): inv_factorial[i] = inv_factorial[i + 1] * (i + 1) % MOD ans = 0 for i in range(N - K + 1): c = factorial[N - 1 - i] * inv_factorial[K - 1] * inv_factorial[N - K - i] ans += c * (A[-1 - i] - A[i]) ans %= MOD print(ans)
N, K = map(int, input().split()) A = list(map(int, input().split())) MOD = 10**9 + 7 A.sort() factorial = [1] for i in range(1, N + 1): factorial.append(factorial[-1] * i % MOD) inv_factorial = [-1] * (N + 1) inv_factorial[-1] = pow(factorial[-1], MOD - 2, MOD) for i in reversed(range(N)): inv_factorial[i] = inv_factorial[i + 1] * (i + 1) % MOD ans = 0 for i in range(N - K + 1): c = factorial[N - 1 - i] * inv_factorial[K - 1] * inv_factorial[N - K - i] ans += c * (A[-1 - i] - A[i]) ans %= MOD print(ans)
replace
8
9
8
9
TLE
p02804
C++
Time Limit Exceeded
#include <stdio.h> #define _USE_MATH_DEFINES #include <algorithm> #include <math.h> #include <vector> using namespace std; const long nPrime = 1000000007; long Factorial(const long &n) { // nPrime = 1000000007; long nReturn = 1; for (long i = 2; i <= n; i++) { nReturn *= i; nReturn %= nPrime; } return nReturn; } long Inverse(const long &n) { // nPrime = 1000000007; if (n % nPrime == 0) { return 0; } long a = n, b = nPrime, u = 1, v = 0; while (b > 0) { long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= nPrime; if (u < 0) { u += nPrime; } return u; } long Combination(const long &n, const long &k) { // nPrime = 1000000007; long nCk = Factorial(n - k) * Factorial(k); nCk %= nPrime; nCk = Factorial(n) * Inverse(nCk); nCk %= nPrime; return nCk; } int main() { long n, k; scanf("%ld %ld", &n, &k); vector<long> viNum(n); for (long i = 0; i < n; i++) { scanf("%ld", &viNum[i]); } sort(viNum.begin(), viNum.end()); long nAns = 0; long nComb = Combination(n, k - 1); for (long i = 1; i <= n - k + 1; i++) { nComb *= Inverse(n - i + 1); nComb %= nPrime; nComb *= (n - i - k + 2); nComb %= nPrime; nAns += (viNum[n - i] - viNum[i - 1]) * Combination(n - i, k - 1); nAns %= nPrime; } if (nAns < 0) { nAns += nPrime; } printf("%ld", nAns); return 0; }
#include <stdio.h> #define _USE_MATH_DEFINES #include <algorithm> #include <math.h> #include <vector> using namespace std; const long nPrime = 1000000007; long Factorial(const long &n) { // nPrime = 1000000007; long nReturn = 1; for (long i = 2; i <= n; i++) { nReturn *= i; nReturn %= nPrime; } return nReturn; } long Inverse(const long &n) { // nPrime = 1000000007; if (n % nPrime == 0) { return 0; } long a = n, b = nPrime, u = 1, v = 0; while (b > 0) { long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= nPrime; if (u < 0) { u += nPrime; } return u; } long Combination(const long &n, const long &k) { // nPrime = 1000000007; long nCk = Factorial(n - k) * Factorial(k); nCk %= nPrime; nCk = Factorial(n) * Inverse(nCk); nCk %= nPrime; return nCk; } int main() { long n, k; scanf("%ld %ld", &n, &k); vector<long> viNum(n); for (long i = 0; i < n; i++) { scanf("%ld", &viNum[i]); } sort(viNum.begin(), viNum.end()); long nAns = 0; long nComb = Combination(n, k - 1); for (long i = 1; i <= n - k + 1; i++) { nComb *= Inverse(n - i + 1); nComb %= nPrime; nComb *= (n - i - k + 2); nComb %= nPrime; nAns += (viNum[n - i] - viNum[i - 1]) * nComb; nAns %= nPrime; } if (nAns < 0) { nAns += nPrime; } printf("%ld", nAns); return 0; }
replace
64
65
64
65
TLE
p02805
C++
Time Limit Exceeded
#include <cmath> #include <cstdio> #include <iostream> #include <vector> using namespace std; typedef long long ll; typedef long double ld; const ll M = 2000000; const ld X = 0.6; const ld Y = 0.9999; ll n; ll x[55], y[55]; ld cx, cy; ld now = X; void f(ld p) { ld mx = 0; ll mi = 0; for (ll i = 0; i < n; i++) { ld r = (cx - x[i]) * (cx - x[i]) + (cy - y[i]) * (cy - y[i]); if (mx < r) mx = r, mi = i; } cx += (x[mi] - cx) * p; cy += (y[mi] - cy) * p; } int main() { scanf("%lld", &n); for (ll i = 0; i < n; i++) { scanf("%lld%lld", &x[i], &y[i]); cx += x[i]; cy += y[i]; } cx /= n; cy /= n; for (ll i = 0; i < M; i++) { f(now); now *= Y; } ld ans = 0; for (ll i = 0; i < n; i++) { ld r = (cx - x[i]) * (cx - x[i]) + (cy - y[i]) * (cy - y[i]); ans = max(ans, r); } printf("%.12Lf", sqrt(ans)); return 0; }
#include <cmath> #include <cstdio> #include <iostream> #include <vector> using namespace std; typedef long long ll; typedef long double ld; const ll M = 900000; const ld X = 0.6; const ld Y = 0.9999; ll n; ll x[55], y[55]; ld cx, cy; ld now = X; void f(ld p) { ld mx = 0; ll mi = 0; for (ll i = 0; i < n; i++) { ld r = (cx - x[i]) * (cx - x[i]) + (cy - y[i]) * (cy - y[i]); if (mx < r) mx = r, mi = i; } cx += (x[mi] - cx) * p; cy += (y[mi] - cy) * p; } int main() { scanf("%lld", &n); for (ll i = 0; i < n; i++) { scanf("%lld%lld", &x[i], &y[i]); cx += x[i]; cy += y[i]; } cx /= n; cy /= n; for (ll i = 0; i < M; i++) { f(now); now *= Y; } ld ans = 0; for (ll i = 0; i < n; i++) { ld r = (cx - x[i]) * (cx - x[i]) + (cy - y[i]) * (cy - y[i]); ans = max(ans, r); } printf("%.12Lf", sqrt(ans)); return 0; }
replace
10
11
10
11
TLE
p02805
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; using ll = long long; using P = pair<int, int>; using pll = pair<long long, long long>; constexpr int INF = 1e9; constexpr long long LINF = 1e17; constexpr int MOD = 1000000007; constexpr double PI = 3.14159265358979323846; using d = double; constexpr double EPS = 1e-10; constexpr bool eq(const d &x, const d &y) { return abs(x - y) < EPS; } struct point { d x = 0, y = 0; constexpr point(const d &x = 0, const d &y = 0) : x(x), y(y) {} constexpr bool operator==(const point &r) const { return eq(x, r.x) && eq(y, r.y); }; constexpr bool operator!=(const point &r) const { return !(*this == r); }; constexpr bool operator<(const point &r) const { return (eq(x, r.x)) ? (y < r.y) : (x < r.x); }; constexpr bool operator>(const point &r) const { return (eq(x, r.x)) ? (y > r.y) : (x > r.x); }; constexpr point operator+(const point &r) const { return point(*this) += r; } constexpr point operator+=(const point &r) { x += r.x; y += r.y; return *this; } constexpr point operator-(const point &r) const { return point(*this) -= r; } constexpr point operator-=(const point &r) { x -= r.x; y -= r.y; return *this; } constexpr point operator*(const d &r) const { return point(*this) *= r; } constexpr point operator*=(const d &r) { x *= r; y *= r; return *this; } constexpr point operator*(const point &r) const { return point(*this) *= r; } constexpr point operator*=(const point &r) { d tmp = x; x = x * r.x - y * r.y; y = tmp * r.y + y * r.x; return *this; } constexpr point operator/(const double &r) const { return point(*this) /= r; } constexpr point operator/=(const double &r) { x /= r; y /= r; return *this; } }; d norm(const point &a) { return a.x * a.x + a.y * a.y; } double abs(const point &a) { return sqrt(a.x * a.x + a.y * a.y); } d dot(const point &a, const point &b) { return a.x * b.x + a.y * b.y; } d cross(const point &a, const point &b) { return a.x * b.y - a.y * b.x; } point projection(const point &p, const point &b) { return b * dot(p, b) / norm(b); } point unit_vec(const point &p) { return p / abs(p); } point normal_vec(const point &p) { return p * point(0, 1); } point rotation(const point &center, const point &p, const point &rot) { return rot * (p - center) + center; } double area(const point &a, const point &b, const point &c) { return cross(b - a, c - a) / 2; } struct Line { point a, b; constexpr Line(const point &a = point(0, 0), const point &b = point(0, 0)) : a(a), b(b) {} constexpr Line(const double &A, const double &B, const double &C) { // Ax + By + C = 0 if (eq(A, 0) && eq(B, 0)) assert(-1); else if (eq(B, 0)) { a = point(-C / A, 0); b = point(-C / A, 1); } else { a = point(0, -C / B); b = point(1, -(A + C) / B); } } }; bool is_orthoonal(const Line &l, const Line &r) { return eq(dot(l.b - l.a, r.b - r.a), 0); } bool is_pararell(const Line &l, const Line &r) { return eq(cross(l.b - l.a, r.b - r.a), 0); } bool on_Line(const Line &l, const point &p) { return eq(dot(p - l.a, l.b - l.a), 0); } double dis_Lp(const Line &l, const point &p) { return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a); } point Line_intersect(const Line &l, const Line &r) { point lv = l.b - l.a, rv = r.b - r.a; return l.a + lv * abs(cross(r.b - l.a, rv) / cross(lv, rv)); } Line vertical_bisector(const point &p, const point &q) { return Line((p + q) / 2, rotation((p + q) / 2, q, point(0, 1))); } using Segment = Line; bool on_Segment(const Segment &s, const point &p) { return (abs(s.a - p) + abs(s.b - p) < abs(s.a - s.b) + EPS); } bool is_segment_intersect(const Segment &s, const Segment &t) { return (cross(s.b - s.a, t.a - s.a) * cross(s.b - s.a, t.b - s.a) < EPS) && (cross(t.b - t.a, s.a - t.a) * cross(t.b - t.a, s.b - t.a) < EPS); } point segment_intersect(const Segment &s, const Segment &t) { double d1 = dis_Lp(s, t.a); double d2 = dis_Lp(s, t.b); double p = d1 / (d1 + d2); return t.a + (t.b - t.a) * p; } int n = 55; vector<point> points(n); vector<point> centers; bool on_circle(point cen, double r, point p) { return (cen.x - p.x) * (cen.x - p.x) + (cen.y - p.y) * (cen.y - p.y) <= r * r + EPS; } bool ok(double r) { bool res = false; rep(i, centers.size()) { bool tmp = true; rep(j, n) { if (!on_circle(centers[i], r, points[j])) { tmp = false; break; } } if (tmp) { res = true; break; } } return res; } int main() { cin >> n; vector<Line> ver_bis; rep(i, n) cin >> points[i].x >> points[i].y; rep(i, n) rep(j, i) centers.push_back((points[i] + points[j]) / 2); rep(i, n) rep(j, i) ver_bis.push_back(vertical_bisector(points[i], points[j])); int m = ver_bis.size(); rep(i, m) rep(j, i) { if (is_pararell(ver_bis[i], ver_bis[j])) continue; centers.push_back(Line_intersect(ver_bis[i], ver_bis[j])); } double l = 0, r = 1e5; rep(i, 500) { double m = (l + r) / 2; if (ok(m)) r = m; else l = m; } cout << setprecision(20) << l << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; using ll = long long; using P = pair<int, int>; using pll = pair<long long, long long>; constexpr int INF = 1e9; constexpr long long LINF = 1e17; constexpr int MOD = 1000000007; constexpr double PI = 3.14159265358979323846; using d = double; constexpr double EPS = 1e-10; constexpr bool eq(const d &x, const d &y) { return abs(x - y) < EPS; } struct point { d x = 0, y = 0; constexpr point(const d &x = 0, const d &y = 0) : x(x), y(y) {} constexpr bool operator==(const point &r) const { return eq(x, r.x) && eq(y, r.y); }; constexpr bool operator!=(const point &r) const { return !(*this == r); }; constexpr bool operator<(const point &r) const { return (eq(x, r.x)) ? (y < r.y) : (x < r.x); }; constexpr bool operator>(const point &r) const { return (eq(x, r.x)) ? (y > r.y) : (x > r.x); }; constexpr point operator+(const point &r) const { return point(*this) += r; } constexpr point operator+=(const point &r) { x += r.x; y += r.y; return *this; } constexpr point operator-(const point &r) const { return point(*this) -= r; } constexpr point operator-=(const point &r) { x -= r.x; y -= r.y; return *this; } constexpr point operator*(const d &r) const { return point(*this) *= r; } constexpr point operator*=(const d &r) { x *= r; y *= r; return *this; } constexpr point operator*(const point &r) const { return point(*this) *= r; } constexpr point operator*=(const point &r) { d tmp = x; x = x * r.x - y * r.y; y = tmp * r.y + y * r.x; return *this; } constexpr point operator/(const double &r) const { return point(*this) /= r; } constexpr point operator/=(const double &r) { x /= r; y /= r; return *this; } }; d norm(const point &a) { return a.x * a.x + a.y * a.y; } double abs(const point &a) { return sqrt(a.x * a.x + a.y * a.y); } d dot(const point &a, const point &b) { return a.x * b.x + a.y * b.y; } d cross(const point &a, const point &b) { return a.x * b.y - a.y * b.x; } point projection(const point &p, const point &b) { return b * dot(p, b) / norm(b); } point unit_vec(const point &p) { return p / abs(p); } point normal_vec(const point &p) { return p * point(0, 1); } point rotation(const point &center, const point &p, const point &rot) { return rot * (p - center) + center; } double area(const point &a, const point &b, const point &c) { return cross(b - a, c - a) / 2; } struct Line { point a, b; constexpr Line(const point &a = point(0, 0), const point &b = point(0, 0)) : a(a), b(b) {} constexpr Line(const double &A, const double &B, const double &C) { // Ax + By + C = 0 if (eq(A, 0) && eq(B, 0)) assert(-1); else if (eq(B, 0)) { a = point(-C / A, 0); b = point(-C / A, 1); } else { a = point(0, -C / B); b = point(1, -(A + C) / B); } } }; bool is_orthoonal(const Line &l, const Line &r) { return eq(dot(l.b - l.a, r.b - r.a), 0); } bool is_pararell(const Line &l, const Line &r) { return eq(cross(l.b - l.a, r.b - r.a), 0); } bool on_Line(const Line &l, const point &p) { return eq(dot(p - l.a, l.b - l.a), 0); } double dis_Lp(const Line &l, const point &p) { return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a); } point Line_intersect(const Line &l, const Line &r) { point lv = l.b - l.a, rv = r.b - r.a; return l.a + lv * abs(cross(r.b - l.a, rv) / cross(lv, rv)); } Line vertical_bisector(const point &p, const point &q) { return Line((p + q) / 2, rotation((p + q) / 2, q, point(0, 1))); } using Segment = Line; bool on_Segment(const Segment &s, const point &p) { return (abs(s.a - p) + abs(s.b - p) < abs(s.a - s.b) + EPS); } bool is_segment_intersect(const Segment &s, const Segment &t) { return (cross(s.b - s.a, t.a - s.a) * cross(s.b - s.a, t.b - s.a) < EPS) && (cross(t.b - t.a, s.a - t.a) * cross(t.b - t.a, s.b - t.a) < EPS); } point segment_intersect(const Segment &s, const Segment &t) { double d1 = dis_Lp(s, t.a); double d2 = dis_Lp(s, t.b); double p = d1 / (d1 + d2); return t.a + (t.b - t.a) * p; } int n = 55; vector<point> points(n); vector<point> centers; bool on_circle(point cen, double r, point p) { return (cen.x - p.x) * (cen.x - p.x) + (cen.y - p.y) * (cen.y - p.y) <= r * r + EPS; } bool ok(double r) { bool res = false; rep(i, centers.size()) { bool tmp = true; rep(j, n) { if (!on_circle(centers[i], r, points[j])) { tmp = false; break; } } if (tmp) { res = true; break; } } return res; } int main() { cin >> n; vector<Line> ver_bis; rep(i, n) cin >> points[i].x >> points[i].y; rep(i, n) rep(j, i) centers.push_back((points[i] + points[j]) / 2); rep(i, n) rep(j, i) ver_bis.push_back(vertical_bisector(points[i], points[j])); int m = ver_bis.size(); rep(i, m) rep(j, i) { if (is_pararell(ver_bis[i], ver_bis[j])) continue; centers.push_back(Line_intersect(ver_bis[i], ver_bis[j])); } double l = 0, r = 1e4; rep(i, 100) { double m = (l + r) / 2; if (ok(m)) r = m; else l = m; } cout << setprecision(20) << l << endl; return 0; }
replace
167
169
167
169
TLE
p02805
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define lli long long int #define ulli unsigned long long int #pragma GCC target("sse4.2") using namespace std; #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL) #define time \ cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n"; lli maxSubArraySum(lli a[], lli size) { lli max_so_far = INT_MIN, max_ending_here = 0; for (lli i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } int main() { fast; lli n; cin >> n; lli a[n][2]; double x = 0, y = 0; for (lli i = 0; i < n; i++) { cin >> a[i][0] >> a[i][1]; x += a[i][0], y += a[i][1]; } double X = x / n, Y = y / n; // cout<<X<<" "<<Y<<"\n"; double m = 0.999; double p = 0.01; lli f; double dist = 0; for (lli i = 1; i <= 10000000; i++) { f = 0; dist = ((X - a[0][0]) * (X - a[0][0]) + (Y - a[0][1]) * (Y - a[0][1])); for (lli j = 1; j < n; j++) { if (((X - a[j][0]) * (X - a[j][0]) + (Y - a[j][1]) * (Y - a[j][1])) > dist) { dist = ((X - a[j][0]) * (X - a[j][0]) + (Y - a[j][1]) * (Y - a[j][1])); f = j; } } X += (a[f][0] - X) * p; Y += (a[f][1] - Y) * p; p *= m; } cout << fixed << setprecision(9) << sqrt((X - a[f][0]) * (X - a[f][0]) + (Y - a[f][1]) * (Y - a[f][1])); return 0; }
#include <bits/stdc++.h> #define lli long long int #define ulli unsigned long long int #pragma GCC target("sse4.2") using namespace std; #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL) #define time \ cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n"; lli maxSubArraySum(lli a[], lli size) { lli max_so_far = INT_MIN, max_ending_here = 0; for (lli i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } int main() { fast; lli n; cin >> n; lli a[n][2]; double x = 0, y = 0; for (lli i = 0; i < n; i++) { cin >> a[i][0] >> a[i][1]; x += a[i][0], y += a[i][1]; } double X = x / n, Y = y / n; // cout<<X<<" "<<Y<<"\n"; double m = 0.999; double p = 0.01; lli f; double dist = 0; for (lli i = 1; i <= 8000000; i++) { f = 0; dist = ((X - a[0][0]) * (X - a[0][0]) + (Y - a[0][1]) * (Y - a[0][1])); for (lli j = 1; j < n; j++) { if (((X - a[j][0]) * (X - a[j][0]) + (Y - a[j][1]) * (Y - a[j][1])) > dist) { dist = ((X - a[j][0]) * (X - a[j][0]) + (Y - a[j][1]) * (Y - a[j][1])); f = j; } } X += (a[f][0] - X) * p; Y += (a[f][1] - Y) * p; p *= m; } cout << fixed << setprecision(9) << sqrt((X - a[f][0]) * (X - a[f][0]) + (Y - a[f][1]) * (Y - a[f][1])); return 0; }
replace
41
42
41
42
TLE
p02805
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; #define rep(i, x) for (ll i = 0; i < (ll)(x); i++) #define pb push_back #define eb emplace_back #define debug(x) cerr << #x << ": " << (x) << "\n"; #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int, int> P; typedef pair<ll, ll> Pll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; const ll INF = numeric_limits<ll>::max() / 4; const ll MOD = 1e9 + 7; const int n_max = 1e5 + 10; template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int main() { ll n; cin >> n; using Pld = pair<ld, ld>; vector<Pld> xy(n); rep(i, n) { ld x, y; cin >> x >> y; xy[i] = {x, y}; } ld ans = numeric_limits<ld>::max(); auto cent = [](Pld A, Pld B, Pld C) -> Pld { ld a = A.first, b = A.second; ld c = B.first, d = B.second; ld e = C.first, f = C.second; ld aa = a * a; ld bb = b * b; ld cc = c * c; ld dd = d * d; ld ee = e * e; ld ff = f * f; ld py = ((e - a) * (aa + bb - cc - dd) - (c - a) * (aa + bb - ee - ff)) / (2.0 * (e - a) * (b - d) - 2.0 * (c - a) * (b - f)); ld px; if (c == a) px = (2 * (b - f) * py - aa - bb + ee + ff) / (2.0 * (e - a)); else px = (2 * (b - d) * py - aa - bb + cc + dd) / (2.0 * (c - a)); return {px, py}; }; auto area = [](Pld A, Pld B, Pld C) { ld x1 = A.first, y1 = A.second; ld x2 = B.first, y2 = B.second; ld x3 = C.first, y3 = C.second; x1 -= x3; y1 -= y3; x2 -= x3; y2 -= y3; return abs(x1 * y2 - x2 * y1); }; if (n == 2) { ld x = (xy[0].first + xy[1].first) / 2.0; ld y = (xy[0].second + xy[1].second) / 2.0; x = abs(x - xy[0].first); y = abs(y - xy[0].second); ans = sqrt(x * x + y * y); // assert(0); } else { bool ok = false; rep(i, n) rep(j, n) rep(k, n) { if (i >= j) continue; if (j >= k) continue; if (area(xy[i], xy[j], xy[k]) == 0) continue; Pld center = cent(xy[i], xy[j], xy[k]); ld xt = center.first - xy[i].first; ld yt = center.second - xy[i].second; ld dist = sqrt(xt * xt + yt * yt); ld temp = 0; rep(l, n) { ld x = center.first - xy[l].first; ld y = center.second - xy[l].second; ld dis = sqrt(x * x + y * y); chmax(temp, dis); } ok |= (temp == dist); // debug(i);debug(j);debug(k); // debug(temp); chmin(ans, temp); } assert(ok); } cout << fixed << setprecision(20); cout << ans << endl; }
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; #define rep(i, x) for (ll i = 0; i < (ll)(x); i++) #define pb push_back #define eb emplace_back #define debug(x) cerr << #x << ": " << (x) << "\n"; #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int, int> P; typedef pair<ll, ll> Pll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; const ll INF = numeric_limits<ll>::max() / 4; const ll MOD = 1e9 + 7; const int n_max = 1e5 + 10; template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int main() { ll n; cin >> n; using Pld = pair<ld, ld>; vector<Pld> xy(n); rep(i, n) { ld x, y; cin >> x >> y; xy[i] = {x, y}; } ld ans = numeric_limits<ld>::max(); auto cent = [](Pld A, Pld B, Pld C) -> Pld { ld a = A.first, b = A.second; ld c = B.first, d = B.second; ld e = C.first, f = C.second; ld aa = a * a; ld bb = b * b; ld cc = c * c; ld dd = d * d; ld ee = e * e; ld ff = f * f; ld py = ((e - a) * (aa + bb - cc - dd) - (c - a) * (aa + bb - ee - ff)) / (2.0 * (e - a) * (b - d) - 2.0 * (c - a) * (b - f)); ld px; if (c == a) px = (2 * (b - f) * py - aa - bb + ee + ff) / (2.0 * (e - a)); else px = (2 * (b - d) * py - aa - bb + cc + dd) / (2.0 * (c - a)); return {px, py}; }; auto area = [](Pld A, Pld B, Pld C) { ld x1 = A.first, y1 = A.second; ld x2 = B.first, y2 = B.second; ld x3 = C.first, y3 = C.second; x1 -= x3; y1 -= y3; x2 -= x3; y2 -= y3; return abs(x1 * y2 - x2 * y1); }; if (n == 2) { ld x = (xy[0].first + xy[1].first) / 2.0; ld y = (xy[0].second + xy[1].second) / 2.0; x = abs(x - xy[0].first); y = abs(y - xy[0].second); ans = sqrt(x * x + y * y); // assert(0); } else { bool ok = false; rep(i, n) rep(j, n) rep(k, n) { if (i >= j) continue; if (j >= k) continue; if (area(xy[i], xy[j], xy[k]) == 0) continue; Pld center = cent(xy[i], xy[j], xy[k]); ld xt = center.first - xy[i].first; ld yt = center.second - xy[i].second; ld dist = sqrt(xt * xt + yt * yt); ld temp = 0; rep(l, n) { ld x = center.first - xy[l].first; ld y = center.second - xy[l].second; ld dis = sqrt(x * x + y * y); chmax(temp, dis); } ok |= (temp == dist); // debug(i);debug(j);debug(k); // debug(temp); chmin(ans, temp); } // assert(ok); rep(i, n) rep(j, n) { if (i >= j) continue; ld xc = (xy[i].first + xy[j].first) / 2.0; ld yc = (xy[i].second + xy[j].second) / 2.0; ld temp = 0; rep(k, n) { ld x = xc - xy[k].first; ld y = yc - xy[k].second; chmax(temp, sqrt(x * x + y * y)); } chmin(ans, temp); } } cout << fixed << setprecision(20); cout << ans << endl; }
replace
115
116
115
130
0
p02805
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int x[55], y[55]; double distance(int I, int J) { return sqrt((x[I] - x[J]) * (x[I] - x[J]) + (y[I] - y[J]) * (y[I] - y[J])); } int main() { int n; cin >> n; double ans = 1e9; double tmp; double a, b, c; double A, B, C; queue<pair<double, double>> q; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; else { for (int k = 0; k < n; k++) { if (i == k || j == k) q.push(make_pair((x[i] + x[j]) / 2.0, (y[i] + y[j]) / 2.0)); else { a = distance(i, j); b = distance(k, j); c = distance(i, k); if (a + b > c && a + c > b && b + c > a) { A = sin(2.0 * acos((b * b + c * c - a * a) / (2.0 * b * c))); B = sin(2.0 * acos((a * a + c * c - b * b) / (2.0 * a * c))); C = sin(2.0 * acos((a * a + b * b - c * c) / (2.0 * a * b))); q.push(make_pair((A * x[k] + B * x[i] + C * x[j]) / (A + B + C), (A * y[k] + B * y[i] + C * y[j]) / (A + B + C))); } } } } } } while (!q.empty()) { tmp = 0; for (int i = 0; i < n; i++) tmp = max(tmp, sqrt((x[i] - q.front().first) * (x[i] - q.front().first) + (y[i] - q.front().second) * (y[i] - q.front().second))); ans = min(tmp, ans); } cout << fixed << setprecision(16); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int x[55], y[55]; double distance(int I, int J) { return sqrt((x[I] - x[J]) * (x[I] - x[J]) + (y[I] - y[J]) * (y[I] - y[J])); } int main() { int n; cin >> n; double ans = 1e9; double tmp; double a, b, c; double A, B, C; queue<pair<double, double>> q; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; else { for (int k = 0; k < n; k++) { if (i == k || j == k) q.push(make_pair((x[i] + x[j]) / 2.0, (y[i] + y[j]) / 2.0)); else { a = distance(i, j); b = distance(k, j); c = distance(i, k); if (a + b > c && a + c > b && b + c > a) { A = sin(2.0 * acos((b * b + c * c - a * a) / (2.0 * b * c))); B = sin(2.0 * acos((a * a + c * c - b * b) / (2.0 * a * c))); C = sin(2.0 * acos((a * a + b * b - c * c) / (2.0 * a * b))); q.push(make_pair((A * x[k] + B * x[i] + C * x[j]) / (A + B + C), (A * y[k] + B * y[i] + C * y[j]) / (A + B + C))); } } } } } } while (!q.empty()) { tmp = 0; for (int i = 0; i < n; i++) tmp = max(tmp, sqrt((x[i] - q.front().first) * (x[i] - q.front().first) + (y[i] - q.front().second) * (y[i] - q.front().second))); ans = min(tmp, ans); q.pop(); } cout << fixed << setprecision(16); cout << ans << endl; }
insert
49
49
49
50
TLE
p02805
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; typedef complex<ld> P; #define EPS (1e-10) #define rep(i, n) for (int i = 0; i < n; i++) ld dist(P c1, P c2) { return sqrt((c1.real() - c2.real()) * (c1.real() - c2.real()) + (c1.imag() - c2.imag()) * (c1.imag() - c2.imag())); } ld dot(P c1, P c2) { return c1.real() * c2.real() + c1.imag() * c2.imag(); } ld product(P c1, P c2) { return c1.real() * c2.imag() - c1.imag() * c2.real(); } ld norm(P c) { return sqrt(dot(c, c)); } P intersection(P a1, P a2, P b1, P b2) { P a = a2 - a1, b = b2 - b1; return a1 + a * (product(b, b1 - a1) / product(b, a)); } bool is_intersected(P a1, P a2, P b1, P b2) { return (product(a2 - a1, b1 - a1) * product(a2 - a1, b2 - a1) < EPS) && (product(b2 - b1, a1 - b1) * product(b2 - b1, a2 - b1) < EPS); } P circum_coordinate(P a1, P a2, P a3) { ld px, py; ld x1, y1, x2, y2, x3, y3; x1 = a1.real(); y1 = a1.imag(); x2 = a2.real(); y2 = a2.imag(); x3 = a3.real(); y3 = a3.imag(); py = ((x3 - x1) * (x1 * x1 + y1 * y1 - x2 * x2 - y2 * y2) - (x2 - x1) * (x1 * x1 + y1 * y1 - x3 * x3 - y3 * y3)) / ((x3 - x1) * (y1 - y2) * 2.0 - (x2 - x1) * (y1 - y3) * 2.0); if (x2 - x1 < EPS) px = ((y1 - y3) * 2.0 * py - x1 * x1 - y1 * y1 + x3 * x3 + y3 * y3) / ((x3 - x1) * 2.0); else px = ((y1 - y2) * 2.0 * py - x1 * x1 - y1 * y1 + x2 * x2 + y2 * y2) / ((x2 - x1) * 2.0); return P(px, py); } int main(void) { int N; cin >> N; ld ans = 2000.0; P CO[N]; rep(i, N) { ld x, y; cin >> x >> y; CO[i] = P(x, y); } for (int i = 0; i < N; i++) { P p1 = CO[i]; for (int j = i + 1; j < N; j++) { P p2 = CO[j]; P center = P((p1.real() + p2.real()) / 2, (p1.imag() + p2.imag()) / 2); ld range = dist(p1, p2) / 2; bool ok = true; for (int k = 0; k < N; k++) { if (k == i || k == j) continue; if (dist(CO[k], center) > range + EPS) ok = false; } if (ok) { ans = min(ans, range); } } } for (int i = 0; i < N; i++) { P p1 = CO[i]; for (int j = i + 1; j < N; j++) { P p2 = CO[j]; for (int k = j + 1; k < N; k++) { P p3 = CO[k]; // p1, p2, p3が同一直線上(すなわち外積0) -> continue if (product(p2 - p1, p3 - p1) < EPS) { assert(false); continue; } P center = circum_coordinate(p1, p2, p3); // 外接円の中心座標 ld range = dist(center, p3); // 外接円の半径 bool ok = true; for (int l = 0; l < N; l++) { if (l == i || l == j || l == k) continue; if (dist(CO[l], center) > range) ok = false; } if (ok) ans = min(ans, range); } } } double res = ans; printf("%.10f\n", res); }
#include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; typedef complex<ld> P; #define EPS (1e-10) #define rep(i, n) for (int i = 0; i < n; i++) ld dist(P c1, P c2) { return sqrt((c1.real() - c2.real()) * (c1.real() - c2.real()) + (c1.imag() - c2.imag()) * (c1.imag() - c2.imag())); } ld dot(P c1, P c2) { return c1.real() * c2.real() + c1.imag() * c2.imag(); } ld product(P c1, P c2) { return c1.real() * c2.imag() - c1.imag() * c2.real(); } ld norm(P c) { return sqrt(dot(c, c)); } P intersection(P a1, P a2, P b1, P b2) { P a = a2 - a1, b = b2 - b1; return a1 + a * (product(b, b1 - a1) / product(b, a)); } bool is_intersected(P a1, P a2, P b1, P b2) { return (product(a2 - a1, b1 - a1) * product(a2 - a1, b2 - a1) < EPS) && (product(b2 - b1, a1 - b1) * product(b2 - b1, a2 - b1) < EPS); } P circum_coordinate(P a1, P a2, P a3) { ld px, py; ld x1, y1, x2, y2, x3, y3; x1 = a1.real(); y1 = a1.imag(); x2 = a2.real(); y2 = a2.imag(); x3 = a3.real(); y3 = a3.imag(); py = ((x3 - x1) * (x1 * x1 + y1 * y1 - x2 * x2 - y2 * y2) - (x2 - x1) * (x1 * x1 + y1 * y1 - x3 * x3 - y3 * y3)) / ((x3 - x1) * (y1 - y2) * 2.0 - (x2 - x1) * (y1 - y3) * 2.0); if (x2 - x1 < EPS) px = ((y1 - y3) * 2.0 * py - x1 * x1 - y1 * y1 + x3 * x3 + y3 * y3) / ((x3 - x1) * 2.0); else px = ((y1 - y2) * 2.0 * py - x1 * x1 - y1 * y1 + x2 * x2 + y2 * y2) / ((x2 - x1) * 2.0); return P(px, py); } int main(void) { int N; cin >> N; ld ans = 2000.0; P CO[N]; rep(i, N) { ld x, y; cin >> x >> y; CO[i] = P(x, y); } for (int i = 0; i < N; i++) { P p1 = CO[i]; for (int j = i + 1; j < N; j++) { P p2 = CO[j]; P center = P((p1.real() + p2.real()) / 2, (p1.imag() + p2.imag()) / 2); ld range = dist(p1, p2) / 2; bool ok = true; for (int k = 0; k < N; k++) { if (k == i || k == j) continue; if (dist(CO[k], center) > range + EPS) ok = false; } if (ok) { ans = min(ans, range); } } } for (int i = 0; i < N; i++) { P p1 = CO[i]; for (int j = i + 1; j < N; j++) { P p2 = CO[j]; for (int k = j + 1; k < N; k++) { P p3 = CO[k]; // p1, p2, p3が同一直線上(すなわち外積0) -> continue if (abs(product(p2 - p1, p3 - p1)) < EPS) { continue; } P center = circum_coordinate(p1, p2, p3); // 外接円の中心座標 ld range = dist(center, p3); // 外接円の半径 bool ok = true; for (int l = 0; l < N; l++) { if (l == i || l == j || l == k) continue; if (dist(CO[l], center) > range) ok = false; } if (ok) ans = min(ans, range); } } } double res = ans; printf("%.10f\n", res); }
replace
91
93
91
92
0
p02805
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; vector<int> xs, ys; double dist(double x, double y) { double ans = 0; for (int i = 0; i < xs.size(); i++) { double dx = x - xs.at(i), dy = y - ys.at(i); ans = max(ans, sqrt(dx * dx + dy * dy)); } return ans; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int xi, yi; cin >> xi >> yi; xs.push_back(xi); ys.push_back(yi); } double x0 = 0, x1 = 1000, y0 = 0, y1 = 1000; const int s = 53; while (x1 - x0 > 1e-12 && y1 - y0 > 1e-12) { cerr << x0 << " " << x1 << endl; cerr << y0 << " " << y1 << endl; double dx = x1 - x0, dy = y1 - y0; vector<vector<double>> d(s + 1, vector<double>(s + 1)); for (int i = 0; i <= s; i++) { for (int j = 0; j <= s; j++) { d.at(i).at(j) = dist(x0 + i * dx / s, y0 + j * dy / s); } } int imin = 0, jmin = 0; for (int i = 0; i <= s; i++) { for (int j = 0; j <= s; j++) { if (d.at(i).at(j) <= d.at(imin).at(jmin)) { imin = i; jmin = j; } } } if (imin < (s + 1) / 2) { x1 = (x1 * (s - 1) + x0) / s; } else { x0 = (x0 * (s - 1) + x1) / s; } if (jmin < (s + 1) / 2) { y1 = (y1 * (s - 1) + y0) / s; } else { y0 = (y0 * (s - 1) + y1) / s; } } cout << setprecision(17) << dist((x0 + x1) / 2, (y0 + y1) / 2) << endl; }
#include <bits/stdc++.h> using namespace std; vector<int> xs, ys; double dist(double x, double y) { double ans = 0; for (int i = 0; i < xs.size(); i++) { double dx = x - xs.at(i), dy = y - ys.at(i); ans = max(ans, sqrt(dx * dx + dy * dy)); } return ans; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int xi, yi; cin >> xi >> yi; xs.push_back(xi); ys.push_back(yi); } double x0 = 0, x1 = 1000, y0 = 0, y1 = 1000; const int s = 53; while (x1 - x0 > 1e-8 && y1 - y0 > 1e-8) { cerr << x0 << " " << x1 << endl; cerr << y0 << " " << y1 << endl; double dx = x1 - x0, dy = y1 - y0; vector<vector<double>> d(s + 1, vector<double>(s + 1)); for (int i = 0; i <= s; i++) { for (int j = 0; j <= s; j++) { d.at(i).at(j) = dist(x0 + i * dx / s, y0 + j * dy / s); } } int imin = 0, jmin = 0; for (int i = 0; i <= s; i++) { for (int j = 0; j <= s; j++) { if (d.at(i).at(j) <= d.at(imin).at(jmin)) { imin = i; jmin = j; } } } if (imin < (s + 1) / 2) { x1 = (x1 * (s - 1) + x0) / s; } else { x0 = (x0 * (s - 1) + x1) / s; } if (jmin < (s + 1) / 2) { y1 = (y1 * (s - 1) + y0) / s; } else { y0 = (y0 * (s - 1) + y1) / s; } } cout << setprecision(17) << dist((x0 + x1) / 2, (y0 + y1) / 2) << endl; }
replace
25
26
25
26
TLE
p02805
C++
Time Limit Exceeded
#include "algorithm" #include "iostream" #include "math.h" #include "queue" #include "stack" #include "stdio.h" #include "stdlib.h" #include "string.h" #include "vector" #define M 522 #define inf 100000000 #define eps 1e-8 #define PI acos(-1.0) using namespace std; double X, Y; struct Point { double x, y; } p[M]; struct Triangle { Point v[3]; }; struct Circle { Point center; double r; }; double pow(double x) { return x * x; } double Len(Point a, Point b) { return sqrt(pow(a.x - b.x) + pow(a.y - b.y)); } double TriangleArea(Triangle a) // 求三角形的面积 { double px1 = a.v[1].x - a.v[0].x; double py1 = a.v[1].y - a.v[0].y; double px2 = a.v[2].x - a.v[0].x; double py2 = a.v[2].y - a.v[0].y; return fabs(px1 * py2 - px2 * py1) / 2; } Circle CircleOfTriangle(Triangle t) // 就三角形外接圆 { Circle tmp; double a = Len(t.v[0], t.v[1]); double b = Len(t.v[0], t.v[2]); double c = Len(t.v[1], t.v[2]); tmp.r = a * b * c / 4 / TriangleArea(t); double a1 = t.v[1].x - t.v[0].x; double b1 = t.v[1].y - t.v[0].y; double c1 = (a1 * a1 + b1 * b1) / 2; double a2 = t.v[2].x - t.v[0].x; double b2 = t.v[2].y - t.v[0].y; double c2 = (a2 * a2 + b2 * b2) / 2; double d = a1 * b2 - a2 * b1; tmp.center.x = t.v[0].x + (c1 * b2 - c2 * b1) / d; tmp.center.y = t.v[0].y + (a1 * c2 - a2 * c1) / d; return tmp; } void Run(int n) { random_shuffle(p + 1, p + n + 1); // 随机排序取点 int i, j, k; Circle tep; tep.center = p[1]; tep.r = 0; for (i = 2; i <= n; i++) { if (Len(p[i], tep.center) > tep.r + eps) { tep.center = p[i]; tep.r = 0; for (j = 1; j < i; j++) { if (Len(p[j], tep.center) > tep.r + eps) { tep.center.x = (p[i].x + p[j].x) / 2; tep.center.y = (p[i].y + p[j].y) / 2; tep.r = Len(p[i], p[j]) / 2; for (k = 1; k < j; k++) { if (Len(p[k], tep.center) > tep.r + eps) { Triangle t; t.v[0] = p[i]; t.v[1] = p[j]; t.v[2] = p[k]; tep = CircleOfTriangle(t); } } } } } } printf("%.8lf\n", tep.r); } int main() { int n, i; while (scanf("%d", &n), n) { for (i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y); Run(n); } }
#include "algorithm" #include "iostream" #include "math.h" #include "queue" #include "stack" #include "stdio.h" #include "stdlib.h" #include "string.h" #include "vector" #define M 522 #define inf 100000000 #define eps 1e-8 #define PI acos(-1.0) using namespace std; double X, Y; struct Point { double x, y; } p[M]; struct Triangle { Point v[3]; }; struct Circle { Point center; double r; }; double pow(double x) { return x * x; } double Len(Point a, Point b) { return sqrt(pow(a.x - b.x) + pow(a.y - b.y)); } double TriangleArea(Triangle a) // 求三角形的面积 { double px1 = a.v[1].x - a.v[0].x; double py1 = a.v[1].y - a.v[0].y; double px2 = a.v[2].x - a.v[0].x; double py2 = a.v[2].y - a.v[0].y; return fabs(px1 * py2 - px2 * py1) / 2; } Circle CircleOfTriangle(Triangle t) // 就三角形外接圆 { Circle tmp; double a = Len(t.v[0], t.v[1]); double b = Len(t.v[0], t.v[2]); double c = Len(t.v[1], t.v[2]); tmp.r = a * b * c / 4 / TriangleArea(t); double a1 = t.v[1].x - t.v[0].x; double b1 = t.v[1].y - t.v[0].y; double c1 = (a1 * a1 + b1 * b1) / 2; double a2 = t.v[2].x - t.v[0].x; double b2 = t.v[2].y - t.v[0].y; double c2 = (a2 * a2 + b2 * b2) / 2; double d = a1 * b2 - a2 * b1; tmp.center.x = t.v[0].x + (c1 * b2 - c2 * b1) / d; tmp.center.y = t.v[0].y + (a1 * c2 - a2 * c1) / d; return tmp; } void Run(int n) { random_shuffle(p + 1, p + n + 1); // 随机排序取点 int i, j, k; Circle tep; tep.center = p[1]; tep.r = 0; for (i = 2; i <= n; i++) { if (Len(p[i], tep.center) > tep.r + eps) { tep.center = p[i]; tep.r = 0; for (j = 1; j < i; j++) { if (Len(p[j], tep.center) > tep.r + eps) { tep.center.x = (p[i].x + p[j].x) / 2; tep.center.y = (p[i].y + p[j].y) / 2; tep.r = Len(p[i], p[j]) / 2; for (k = 1; k < j; k++) { if (Len(p[k], tep.center) > tep.r + eps) { Triangle t; t.v[0] = p[i]; t.v[1] = p[j]; t.v[2] = p[k]; tep = CircleOfTriangle(t); } } } } } } printf("%.8lf\n", tep.r); } int main() { int n, i; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y); Run(n); }
replace
85
90
85
89
TLE
p02805
C++
Time Limit Exceeded
#include <algorithm> #include <math.h> #include <queue> #include <stdio.h> #include <string> #include <vector> const long long mod = 1000000007; // const long long mod = 998244353; double dist(double x1, double y1, double x2, double y2) { return sqrt(((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2))); } double R(double x1, double y1, double x2, double y2, int *xs, int *ys, int N) { double cx = (x1 + x2) / 2; double cy = (y1 + y2) / 2; double r = dist(x1, y1, x2, y2) / 2; for (int i = 0; i < N; i++) { if (dist(xs[i], ys[i], cx, cy) > r * (1.0000001)) { return -1; } } return r; } double R(double x1, double y1, double x2, double y2, double x3, double y3, int *xs, int *ys, int N) { double a = x1 - x2; double b = y1 - y2; double c = x2 - x3; double d = y2 - y3; double l1 = (x1 * x1 + y1 * y1); double l2 = (x2 * x2 + y2 * y2); double l3 = (x3 * x3 + y3 * y3); double v1 = l1 - l2; double v2 = l2 - l3; double q = a * d - c * b; if (q == 0) return -1; double cx = (d * v1 - b * v2) / (2 * q); double cy = (-c * v1 + a * v2) / (2 * q); double r = sqrt((x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy)); // printf("%f %f %f \n", cx, cy, r); for (int i = 0; i < N; i++) { if (dist(xs[i], ys[i], cx, cy) > r * (1.0000001)) { return -1; } } return r; } int main() { int N; scanf("%d", &N); int *x = new int[N]; int *y = new int[N]; for (int i = 0; i < N; i++) scanf("%d %d", x + i, y + i); double min = 200000; if (N == 2) { min = sqrt((x[0] - x[1]) * (x[0] - x[1]) + (y[0] - y[1]) * (y[0] - y[1])) / 2; } else { for (int i1 = 0; i1 < N; i1++) { for (int i2 = i1 + 1; i2 < N; i2++) { for (int i3 = i2 + 1; i3 < N; i3++) { double r = R(x[i1], y[i1], x[i2], y[i2], x[i3], y[i3], x, y, N); // printf("%f\n", r); if (r != -1) { min = r < min ? r : min; } } double r = R(x[i1], y[i1], x[i2], y[i2], x, y, N); if (r != -1) { min = r < min ? r : min; } } } } printf("%lf", min); while (1) getchar(); return 0; }
#include <algorithm> #include <math.h> #include <queue> #include <stdio.h> #include <string> #include <vector> const long long mod = 1000000007; // const long long mod = 998244353; double dist(double x1, double y1, double x2, double y2) { return sqrt(((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2))); } double R(double x1, double y1, double x2, double y2, int *xs, int *ys, int N) { double cx = (x1 + x2) / 2; double cy = (y1 + y2) / 2; double r = dist(x1, y1, x2, y2) / 2; for (int i = 0; i < N; i++) { if (dist(xs[i], ys[i], cx, cy) > r * (1.0000001)) { return -1; } } return r; } double R(double x1, double y1, double x2, double y2, double x3, double y3, int *xs, int *ys, int N) { double a = x1 - x2; double b = y1 - y2; double c = x2 - x3; double d = y2 - y3; double l1 = (x1 * x1 + y1 * y1); double l2 = (x2 * x2 + y2 * y2); double l3 = (x3 * x3 + y3 * y3); double v1 = l1 - l2; double v2 = l2 - l3; double q = a * d - c * b; if (q == 0) return -1; double cx = (d * v1 - b * v2) / (2 * q); double cy = (-c * v1 + a * v2) / (2 * q); double r = sqrt((x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy)); // printf("%f %f %f \n", cx, cy, r); for (int i = 0; i < N; i++) { if (dist(xs[i], ys[i], cx, cy) > r * (1.0000001)) { return -1; } } return r; } int main() { int N; scanf("%d", &N); int *x = new int[N]; int *y = new int[N]; for (int i = 0; i < N; i++) scanf("%d %d", x + i, y + i); double min = 200000; if (N == 2) { min = sqrt((x[0] - x[1]) * (x[0] - x[1]) + (y[0] - y[1]) * (y[0] - y[1])) / 2; } else { for (int i1 = 0; i1 < N; i1++) { for (int i2 = i1 + 1; i2 < N; i2++) { for (int i3 = i2 + 1; i3 < N; i3++) { double r = R(x[i1], y[i1], x[i2], y[i2], x[i3], y[i3], x, y, N); // printf("%f\n", r); if (r != -1) { min = r < min ? r : min; } } double r = R(x[i1], y[i1], x[i2], y[i2], x, y, N); if (r != -1) { min = r < min ? r : min; } } } } printf("%lf", min); return 0; }
delete
78
80
78
78
TLE
p02805
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #pragma GCC optimize("unroll-loops,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair<long long, long long> ii; typedef complex<long double> com; ld ans = LLONG_MAX; ll n; vector<com> coor; ld check(com t) { ld temp = 0; for (int z = 0; z < n; z++) { temp = max(temp, norm(coor[z] - t)); } ans = min(ans, temp); return temp; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; coor.resize(n); for (int z = 0; z < n; z++) { ld real, img; cin >> real >> img; com t(real, img); coor[z] = t; } ld xl = 0; ld xr = 1000; ld e = 1e-18; while (xr - xl >= e) { ld l1 = xl + (xr - xl) / ld(3.0); ld r1 = xr - (xr - xl) / ld(3.0); ld yl = 0; ld yr = 1000; ld q1 = LLONG_MAX; ld q2 = LLONG_MAX; while (yr - yl >= e) { ld l2 = yl + (yr - yl) / ld(3.0); ld r2 = yr - (yr - yl) / ld(3.0); com t(l1, l2); com t1(l1, r2); ld temp1 = check(t); ld temp2 = check(t1); q1 = min(temp1, temp2); if (temp1 < temp2) { yr = r2; } else { yl = l2; } } yl = 0; yr = 1000; while (yr - yl >= e) { ld l2 = yl + (yr - yl) / ld(3.0); ld r2 = yr - (yr - yl) / ld(3.0); com t(r1, l2); com t1(r1, r2); ld temp1 = check(t); ld temp2 = check(t1); q2 = min(temp1, temp2); if (temp1 < temp2) { yr = r2; } else { yl = l2; } } if (q1 < q2) { xr = r1; } else { xl = l1; } } cout << setprecision(60) << fixed << sqrtl(ans) << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #pragma GCC optimize("unroll-loops,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair<long long, long long> ii; typedef complex<long double> com; ld ans = LLONG_MAX; ll n; vector<com> coor; ld check(com t) { ld temp = 0; for (int z = 0; z < n; z++) { temp = max(temp, norm(coor[z] - t)); } ans = min(ans, temp); return temp; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; coor.resize(n); for (int z = 0; z < n; z++) { ld real, img; cin >> real >> img; com t(real, img); coor[z] = t; } ld xl = 0; ld xr = 1000; ld e = 1e-9; while (xr - xl >= e) { ld l1 = xl + (xr - xl) / ld(3.0); ld r1 = xr - (xr - xl) / ld(3.0); ld yl = 0; ld yr = 1000; ld q1 = LLONG_MAX; ld q2 = LLONG_MAX; while (yr - yl >= e) { ld l2 = yl + (yr - yl) / ld(3.0); ld r2 = yr - (yr - yl) / ld(3.0); com t(l1, l2); com t1(l1, r2); ld temp1 = check(t); ld temp2 = check(t1); q1 = min(temp1, temp2); if (temp1 < temp2) { yr = r2; } else { yl = l2; } } yl = 0; yr = 1000; while (yr - yl >= e) { ld l2 = yl + (yr - yl) / ld(3.0); ld r2 = yr - (yr - yl) / ld(3.0); com t(r1, l2); com t1(r1, r2); ld temp1 = check(t); ld temp2 = check(t1); q2 = min(temp1, temp2); if (temp1 < temp2) { yr = r2; } else { yl = l2; } } if (q1 < q2) { xr = r1; } else { xl = l1; } } cout << setprecision(60) << fixed << sqrtl(ans) << endl; }
replace
44
45
44
45
TLE
p02805
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) \ for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) \ for (long long i = (long long)(m); i <= (long long)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v + n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout << d << endl; #define coutd(d) cout << std::setprecision(10) << d << endl; #define cinline(n) getline(cin, n); #define replace_all(s, b, a) replace(s.begin(), s.end(), b, a); // #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) long long(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll, ll, ll>>; using vb = vector<bool>; 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; } // const ll INF = 1e9; const ll MOD = 1e9 + 7; const ll LINF = 1e18; // 参考: https://drken1215.hatenablog.com/entry/2020/01/12/224200 /* 幾何ライブラリ */ using DD = double; const DD INF = 1LL << 60; // to be set appropriately const DD EPS = 1e-10; // to be set appropriately const DD PI = acosl(-1.0); DD torad(int deg) { return (DD)(deg)*PI / 180; } DD todeg(DD ang) { return ang * 180 / PI; } /* Point */ struct Point { DD x, y; Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {} friend ostream &operator<<(ostream &s, const Point &p) { return s << '(' << p.x << ", " << p.y << ')'; } }; inline Point operator+(const Point &p, const Point &q) { return Point(p.x + q.x, p.y + q.y); } inline Point operator-(const Point &p, const Point &q) { return Point(p.x - q.x, p.y - q.y); } inline Point operator*(const Point &p, DD a) { return Point(p.x * a, p.y * a); } inline Point operator*(DD a, const Point &p) { return Point(a * p.x, a * p.y); } inline Point operator*(const Point &p, const Point &q) { return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x); } inline Point operator/(const Point &p, DD a) { return Point(p.x / a, p.y / a); } inline Point conj(const Point &p) { return Point(p.x, -p.y); } inline Point rot(const Point &p, DD ang) { return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y); } inline Point rot90(const Point &p) { return Point(-p.y, p.x); } inline DD cross(const Point &p, const Point &q) { return p.x * q.y - p.y * q.x; } inline DD dot(const Point &p, const Point &q) { return p.x * q.x + p.y * q.y; } inline DD norm(const Point &p) { return dot(p, p); } inline DD abs(const Point &p) { return sqrt(dot(p, p)); } inline DD amp(const Point &p) { DD res = atan2(p.y, p.x); if (res < 0) res += PI * 2; return res; } inline bool eq(const Point &p, const Point &q) { return abs(p - q) < EPS; } inline bool operator<(const Point &p, const Point &q) { return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y); } inline bool operator>(const Point &p, const Point &q) { return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y); } inline Point operator/(const Point &p, const Point &q) { return p * conj(q) / norm(q); } /* Line */ struct Line : vector<Point> { Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) { this->push_back(a); this->push_back(b); } friend ostream &operator<<(ostream &s, const Line &l) { return s << '{' << l[0] << ", " << l[1] << '}'; } }; // 1:a-bから見てcは左側(反時計回り)、-1:a-bから見てcは右側(時計回り)、0:一直線上 int simple_ccw(const Point &a, const Point &b, const Point &c) { if (cross(b - a, c - a) > EPS) return 1; if (cross(b - a, c - a) < -EPS) return -1; return 0; } // 円や直線の交点 vector<Point> crosspoint(const Line &l, const Line &m) { vector<Point> res; DD d = cross(m[1] - m[0], l[1] - l[0]); if (abs(d) < EPS) return vector<Point>(); res.push_back(l[0] + (l[1] - l[0]) * cross(m[1] - m[0], m[1] - l[0]) / d); return res; } // 外心 Point gaisin(Point a, Point b, Point c) { Line ab((a + b) / 2, (a + b) / 2 + rot90(a - b)); Line bc((b + c) / 2, (b + c) / 2 + rot90(b - c)); return crosspoint(ab, bc)[0]; } signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vector<Point> v(n); rep(i, n) { cin >> v[i].x >> v[i].y; } vector<Point> cond; // 候補 rep(i, n) irep(j, i + 1, n) { cond.push_back((v[i] + v[j]) / 2); irep(k, j + 1, n) { if (simple_ccw(v[i], v[j], v[k])) continue; auto p = gaisin(v[i], v[j], v[k]); cond.push_back(p); } } double ans = INF; for (auto e : cond) { double tmp = 0; rep(i, n) chmax(tmp, abs(e - v[i])); chmin(ans, tmp); } cout << std::setprecision(10) << ans << endl; }
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) \ for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) \ for (long long i = (long long)(m); i <= (long long)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v + n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout << d << endl; #define coutd(d) cout << std::setprecision(10) << d << endl; #define cinline(n) getline(cin, n); #define replace_all(s, b, a) replace(s.begin(), s.end(), b, a); // #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) long long(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll, ll, ll>>; using vb = vector<bool>; 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; } // const ll INF = 1e9; const ll MOD = 1e9 + 7; const ll LINF = 1e18; // 参考: https://drken1215.hatenablog.com/entry/2020/01/12/224200 /* 幾何ライブラリ */ using DD = double; const DD INF = 1LL << 60; // to be set appropriately const DD EPS = 1e-10; // to be set appropriately const DD PI = acosl(-1.0); DD torad(int deg) { return (DD)(deg)*PI / 180; } DD todeg(DD ang) { return ang * 180 / PI; } /* Point */ struct Point { DD x, y; Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {} friend ostream &operator<<(ostream &s, const Point &p) { return s << '(' << p.x << ", " << p.y << ')'; } }; inline Point operator+(const Point &p, const Point &q) { return Point(p.x + q.x, p.y + q.y); } inline Point operator-(const Point &p, const Point &q) { return Point(p.x - q.x, p.y - q.y); } inline Point operator*(const Point &p, DD a) { return Point(p.x * a, p.y * a); } inline Point operator*(DD a, const Point &p) { return Point(a * p.x, a * p.y); } inline Point operator*(const Point &p, const Point &q) { return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x); } inline Point operator/(const Point &p, DD a) { return Point(p.x / a, p.y / a); } inline Point conj(const Point &p) { return Point(p.x, -p.y); } inline Point rot(const Point &p, DD ang) { return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y); } inline Point rot90(const Point &p) { return Point(-p.y, p.x); } inline DD cross(const Point &p, const Point &q) { return p.x * q.y - p.y * q.x; } inline DD dot(const Point &p, const Point &q) { return p.x * q.x + p.y * q.y; } inline DD norm(const Point &p) { return dot(p, p); } inline DD abs(const Point &p) { return sqrt(dot(p, p)); } inline DD amp(const Point &p) { DD res = atan2(p.y, p.x); if (res < 0) res += PI * 2; return res; } inline bool eq(const Point &p, const Point &q) { return abs(p - q) < EPS; } inline bool operator<(const Point &p, const Point &q) { return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y); } inline bool operator>(const Point &p, const Point &q) { return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y); } inline Point operator/(const Point &p, const Point &q) { return p * conj(q) / norm(q); } /* Line */ struct Line : vector<Point> { Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) { this->push_back(a); this->push_back(b); } friend ostream &operator<<(ostream &s, const Line &l) { return s << '{' << l[0] << ", " << l[1] << '}'; } }; // 1:a-bから見てcは左側(反時計回り)、-1:a-bから見てcは右側(時計回り)、0:一直線上 int simple_ccw(const Point &a, const Point &b, const Point &c) { if (cross(b - a, c - a) > EPS) return 1; if (cross(b - a, c - a) < -EPS) return -1; return 0; } // 円や直線の交点 vector<Point> crosspoint(const Line &l, const Line &m) { vector<Point> res; DD d = cross(m[1] - m[0], l[1] - l[0]); if (abs(d) < EPS) return vector<Point>(); res.push_back(l[0] + (l[1] - l[0]) * cross(m[1] - m[0], m[1] - l[0]) / d); return res; } // 外心 Point gaisin(Point a, Point b, Point c) { Line ab((a + b) / 2, (a + b) / 2 + rot90(a - b)); Line bc((b + c) / 2, (b + c) / 2 + rot90(b - c)); return crosspoint(ab, bc)[0]; } signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vector<Point> v(n); rep(i, n) { cin >> v[i].x >> v[i].y; } vector<Point> cond; // 候補 rep(i, n) irep(j, i + 1, n) { cond.push_back((v[i] + v[j]) / 2); irep(k, j + 1, n) { if (simple_ccw(v[i], v[j], v[k]) == 0) continue; auto p = gaisin(v[i], v[j], v[k]); cond.push_back(p); } } double ans = INF; for (auto e : cond) { double tmp = 0; rep(i, n) chmax(tmp, abs(e - v[i])); chmin(ans, tmp); } cout << std::setprecision(10) << ans << endl; }
replace
165
166
165
166
0
p02805
C++
Time Limit Exceeded
/* FFFFFFFFFFFFFFFFFFFFFF F::::::::::::::::::::F F::::::::::::::::::::F FF::::::FFFFFFFFF::::F F:::::F FFFFFF F:::::F F::::::FFFFFFFFFF F:::::::::::::::F F:::::::::::::::F F::::::FFFFFFFFFF F:::::F F:::::F FF:::::::FF F::::::::FF F::::::::FF FFFFFFFFFFF */ #include <bits/stdc++.h> using namespace std; // #define ll long long // #define int long long #define mod 1000000007 #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define f(i, n) for (ll i = 0; i < n; i++) #define trace(x) cerr << #x << ": " << x << " " << endl #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl #define trace3(x, y, z) \ cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << endl #define trace4(a, b, c, d) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << endl #define cout1(a) cout << a << endl #define cout2(a, b) cout << a << " " << b << endl #define cout3(a, b, c) cout << a << " " << b << " " << c << endl #define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl #define vcout(v, i) cout << v[i].fi << " " << v[i].se << endl // #define x first // #define y second #define mp make_pair #define pb push_back #define all(a) (a).begin(), (a).end() #define ms(v, n) memset((v), n, sizeof(v)); #define pll pair<ll, ll> #define mll map<ll, ll> #define sll set<ll> #define vll vector<ll> #define vpll vector<pll> #define maxi(a, b, c) max(a, max(b, c)) #define maxii(a, b, c, d) max(max(a, b), max(c, d)) #define mini(a, b, c) min(a, min(b, c)) #define md(a, b) ((a % mod) * (b % mod) + mod) % mod #define ad(a, b) (a % mod + b % mod + mod) % mod #define nl "\n" #define inf 1e18 #define cases \ ll T; \ cin >> T; \ while (T--) #define BLOCK 500 // const double PI = 3.141592653589793238460; template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } typedef std::complex<double> Complex; typedef std::valarray<Complex> CArray; using namespace std; template <typename T> T K(T a) { return a * a; } #define K(a) K(1LL * (a)) typedef long double ll; // change to long double/long long typedef long double ld; const ld PI = 2 * acos(0); const ld eps = 1e-12; struct P { ll x, y; P operator+(P b) { return P{x + b.x, y + b.y}; } P operator-(P b) { return P{x - b.x, y - b.y}; } ll operator*(P b) { return x * b.y - y * b.x; } P operator*(ll mul) { return P{x * mul, y * mul}; } ll dot(P b) { return x * b.x + y * b.y; } ll len() { return sqrt(K(x) + K(y)); } P scaleTo(ld to) { return *this * (to / len()); } ld dist(P &b) { return (*this - b).len(); } P rotate90() { return P{-y, x}; } ld angle() { return atan2(y, x); } P rotate(ld ang) { ld c = cos(ang), s = sin(ang); return {x * c - y * s, x * s + y * c}; } }; struct L2 { P one, two; P dir() { return two - one; } P normal() { return dir().rotate90(); } ld dist(P he) { return abs((he - one) * (he - two)) / one.dist(two); } ld segDist(P he) { if ((he - two) * normal() < 0 && (normal() * (he - one) < 0)) return dist(he); return min(one.dist(he), two.dist(he)); } P inter(L2 he) { P A = dir(), B = he.dir(); ll den = A * B; assert(abs(den) > eps); // maybe parallel return (A * (he.one * he.two) - B * (one * two)) * (1.0 / den); } P project(P he) { P unit_normal = normal().scaleTo(1); return he + unit_normal * unit_normal.dot(one - he); } P reflect(P he) { return project(he) * 2 - he; } }; // ax+by+c=0; L2 toL2(ll a, ll b, ll c) { P first; if (abs(b) > eps) first = P{0, (ld)-c / b}; else if (abs(a) > eps) first = P{(ld)-c / a, 0}; else assert(false); // a,b cant be zero at the same time return L2{first, first + P{b, -a}}; } L2 normal(P a, P b) { P mid = (a + b) * 0.5; P vec = b - a; vec = P{vec.y, -vec.x}; return L2{mid, mid + vec}; } int main() { fast ll n; cin >> n; vector<P> points(n); for (P &p : points) cin >> p.x >> p.y; double ans = (ld)1e9; auto consider = [&](P a) { double rad = 0.0; for (P p : points) { // trace2(rad,p.dist(a)); rad = max(rad, (double)p.dist(a)); trace(rad); } ans = min(ans, rad); }; for (ll i = 0; i < n; i++) { for (ll j = i + 1; j < n; j++) { for (ll k = j + 1; k < n; k++) { // cout1(150); P A = points[i], B = points[j], C = points[k]; if (abs((B - A) * (C - A)) < eps) continue; L2 one = normal(A, B); L2 two = normal(B, C); P M = one.inter(two); // trace2(M.x,M.y); consider(M); } } } for (ll i = 0; i < n; i++) { for (ll j = i + 1; j < n; j++) { P A = points[i], B = points[j]; consider((A + B) * 0.5); } } printf("%.10lf", ans); return 0; }
/* FFFFFFFFFFFFFFFFFFFFFF F::::::::::::::::::::F F::::::::::::::::::::F FF::::::FFFFFFFFF::::F F:::::F FFFFFF F:::::F F::::::FFFFFFFFFF F:::::::::::::::F F:::::::::::::::F F::::::FFFFFFFFFF F:::::F F:::::F FF:::::::FF F::::::::FF F::::::::FF FFFFFFFFFFF */ #include <bits/stdc++.h> using namespace std; // #define ll long long // #define int long long #define mod 1000000007 #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define f(i, n) for (ll i = 0; i < n; i++) #define trace(x) cerr << #x << ": " << x << " " << endl #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl #define trace3(x, y, z) \ cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << endl #define trace4(a, b, c, d) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << endl #define cout1(a) cout << a << endl #define cout2(a, b) cout << a << " " << b << endl #define cout3(a, b, c) cout << a << " " << b << " " << c << endl #define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl #define vcout(v, i) cout << v[i].fi << " " << v[i].se << endl // #define x first // #define y second #define mp make_pair #define pb push_back #define all(a) (a).begin(), (a).end() #define ms(v, n) memset((v), n, sizeof(v)); #define pll pair<ll, ll> #define mll map<ll, ll> #define sll set<ll> #define vll vector<ll> #define vpll vector<pll> #define maxi(a, b, c) max(a, max(b, c)) #define maxii(a, b, c, d) max(max(a, b), max(c, d)) #define mini(a, b, c) min(a, min(b, c)) #define md(a, b) ((a % mod) * (b % mod) + mod) % mod #define ad(a, b) (a % mod + b % mod + mod) % mod #define nl "\n" #define inf 1e18 #define cases \ ll T; \ cin >> T; \ while (T--) #define BLOCK 500 // const double PI = 3.141592653589793238460; template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } typedef std::complex<double> Complex; typedef std::valarray<Complex> CArray; using namespace std; template <typename T> T K(T a) { return a * a; } #define K(a) K(1LL * (a)) typedef long double ll; // change to long double/long long typedef long double ld; const ld PI = 2 * acos(0); const ld eps = 1e-12; struct P { ll x, y; P operator+(P b) { return P{x + b.x, y + b.y}; } P operator-(P b) { return P{x - b.x, y - b.y}; } ll operator*(P b) { return x * b.y - y * b.x; } P operator*(ll mul) { return P{x * mul, y * mul}; } ll dot(P b) { return x * b.x + y * b.y; } ll len() { return sqrt(K(x) + K(y)); } P scaleTo(ld to) { return *this * (to / len()); } ld dist(P &b) { return (*this - b).len(); } P rotate90() { return P{-y, x}; } ld angle() { return atan2(y, x); } P rotate(ld ang) { ld c = cos(ang), s = sin(ang); return {x * c - y * s, x * s + y * c}; } }; struct L2 { P one, two; P dir() { return two - one; } P normal() { return dir().rotate90(); } ld dist(P he) { return abs((he - one) * (he - two)) / one.dist(two); } ld segDist(P he) { if ((he - two) * normal() < 0 && (normal() * (he - one) < 0)) return dist(he); return min(one.dist(he), two.dist(he)); } P inter(L2 he) { P A = dir(), B = he.dir(); ll den = A * B; assert(abs(den) > eps); // maybe parallel return (A * (he.one * he.two) - B * (one * two)) * (1.0 / den); } P project(P he) { P unit_normal = normal().scaleTo(1); return he + unit_normal * unit_normal.dot(one - he); } P reflect(P he) { return project(he) * 2 - he; } }; // ax+by+c=0; L2 toL2(ll a, ll b, ll c) { P first; if (abs(b) > eps) first = P{0, (ld)-c / b}; else if (abs(a) > eps) first = P{(ld)-c / a, 0}; else assert(false); // a,b cant be zero at the same time return L2{first, first + P{b, -a}}; } L2 normal(P a, P b) { P mid = (a + b) * 0.5; P vec = b - a; vec = P{vec.y, -vec.x}; return L2{mid, mid + vec}; } int main() { fast ll n; cin >> n; vector<P> points(n); for (P &p : points) cin >> p.x >> p.y; double ans = (ld)1e9; auto consider = [&](P a) { double rad = 0.0; for (P p : points) { // trace2(rad,p.dist(a)); rad = max(rad, (double)p.dist(a)); // trace(rad); } ans = min(ans, rad); }; for (ll i = 0; i < n; i++) { for (ll j = i + 1; j < n; j++) { for (ll k = j + 1; k < n; k++) { // cout1(150); P A = points[i], B = points[j], C = points[k]; if (abs((B - A) * (C - A)) < eps) continue; L2 one = normal(A, B); L2 two = normal(B, C); P M = one.inter(two); // trace2(M.x,M.y); consider(M); } } } for (ll i = 0; i < n; i++) { for (ll j = i + 1; j < n; j++) { P A = points[i], B = points[j]; consider((A + B) * 0.5); } } printf("%.10lf", ans); return 0; }
replace
167
168
167
168
TLE
p02805
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <vector> using namespace std; using point = pair<double, double>; double l2_dist(point p1, point p2) { double dx = p1.first - p2.first, dy = p1.second - p2.second; return sqrt(dx * dx + dy * dy); } int main() { int N; cin >> N; vector<point> xy(N); for (int i = 0; i < N; i++) { double x, y; cin >> x >> y; xy[i] = make_pair(x, y); } auto max_r = [&](double x, double y) { point p = make_pair(x, y); double ret = 0; for (int i = 0; i < N; i++) { ret = max(ret, l2_dist(p, xy[i])); } return ret; }; auto fx = [&](double x) { double ly = 0, uy = 1000; double fl = max_r(x, ly), fu = max_r(x, uy); while (abs(fl - fu) > 1e-7) { double m1 = (2 * ly + uy) / 3, m2 = (ly + 2 * uy) / 3; double f1 = max_r(x, m1), f2 = max_r(x, m2); if (f1 < f2) { uy = m2; fu = max_r(x, uy); } else { ly = m1; fl = max_r(x, ly); } } return fl; }; double lx = 0, ux = 1000; double fl = fx(lx), fu = fx(ux); while (abs(fl - fu) > 1e-7) { // cout << lx << ' ' << ux << endl; // cout << fl << ' ' << fu << endl; double m1 = (2 * lx + ux) / 3, m2 = (lx + 2 * ux) / 3; double f1 = fx(m1), f2 = fx(m2); if (f1 < f2) { ux = m2; fu = fx(m2); } else { lx = m1; fl = fx(m1); } } cout << setprecision(20) << fl << endl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <vector> using namespace std; using point = pair<double, double>; double l2_dist(point p1, point p2) { double dx = p1.first - p2.first, dy = p1.second - p2.second; return sqrt(dx * dx + dy * dy); } int main() { int N; cin >> N; vector<point> xy(N); for (int i = 0; i < N; i++) { double x, y; cin >> x >> y; xy[i] = make_pair(x, y); } auto max_r = [&](double x, double y) { point p = make_pair(x, y); double ret = 0; for (int i = 0; i < N; i++) { ret = max(ret, l2_dist(p, xy[i])); } return ret; }; auto fx = [&](double x) { double ly = 0, uy = 1000; double fl = max_r(x, ly), fu = max_r(x, uy); while (abs(fl - fu) > 1e-12) { double m1 = (2 * ly + uy) / 3, m2 = (ly + 2 * uy) / 3; double f1 = max_r(x, m1), f2 = max_r(x, m2); if (f1 < f2) { uy = m2; fu = max_r(x, uy); } else { ly = m1; fl = max_r(x, ly); } } return fl; }; double lx = 0, ux = 1000; double fl = fx(lx), fu = fx(ux); while (abs(fl - fu) > 1e-7) { // cout << lx << ' ' << ux << endl; // cout << fl << ' ' << fu << endl; double m1 = (2 * lx + ux) / 3, m2 = (lx + 2 * ux) / 3; double f1 = fx(m1), f2 = fx(m2); if (f1 < f2) { ux = m2; fu = fx(m2); } else { lx = m1; fl = fx(m1); } } cout << setprecision(20) << fl << endl; }
replace
36
37
36
37
TLE
p02806
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) typedef long long ll; int main() { int n, t[55]; int ans = 0; string x, s[55]; cin >> n; rep(i, n) { cin >> s[i] >> t[i]; } cin >> x; rep(i, n) { for (i = n - 1; i >= 0; i--) { if (x == s[i]) break; ans += t[i]; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) typedef long long ll; int main() { int n, t[55]; int ans = 0; string x, s[55]; cin >> n; rep(i, n) { cin >> s[i] >> t[i]; } cin >> x; for (int i = n - 1; i >= 0; i--) { if (x == s[i]) break; ans += t[i]; } cout << ans << endl; }
replace
14
20
14
18
TLE
p02806
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m = 0; cin >> n; vector<pair<string, int>> a(n); for (int i = 0; i < n; i++) cin >> a.at(n).first >> a.at(n).second; string s; cin >> s; for (int i = 0; i < n; i++) { if (i < n - 1 && a.at(i).first == s) { for (int j = i + 1; j < n; j++) m += a.at(j).second; break; } } cout << m << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m = 0; cin >> n; vector<pair<string, int>> a(n); for (int i = 0; i < n; i++) cin >> a.at(i).first >> a.at(i).second; string s; cin >> s; for (int i = 0; i < n; i++) { if (i < n - 1 && a.at(i).first == s) { for (int j = i + 1; j < n; j++) m += a.at(j).second; break; } } cout << m << endl; }
replace
7
8
7
8
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)