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
p02820
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <string> using namespace std; typedef long long ll; int N, K; ll R, S, P; string T; ll dp[100000 + 100][3]; // j -> 0->r 1->s 2->p int main() { cin >> N >> K >> R >> S >> P >> T; ll ans = 0; for (int i = 0; i < K; i++) { if (T[i] == 'r') { dp[i][2] = P; } else if (T[i] == 'p') { dp[i][1] = S; } else { dp[i][0] = R; } ll tmp = 0; for (int j = i + K; j < N; j++) { dp[j][0] = max(dp[j - K][1], dp[j - K][2]); dp[j][1] = max(dp[j - K][0], dp[j - K][2]); dp[j][2] = max(dp[j - K][0], dp[j - K][1]); if (T[j] == 'r') { dp[j][2] = P + max(dp[j - K][0], dp[j - K][1]); } else if (T[j] == 'p') { dp[j][1] = S + max(dp[j - K][0], dp[j - K][2]); } else { dp[j][0] = R + max(dp[j - K][1], dp[j - K][2]); } tmp = max(tmp, max(dp[j][0], max(dp[j][1], dp[j][2]))); } // ans += tmp; } for (int i = N - 1; i > N - 1 - K; i--) { ans += max(dp[i][0], max(dp[i][1], dp[i][2])); } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; typedef long long ll; int N, K; ll R, S, P; string T; ll dp[100000 + 100][3]; // j -> 0->r 1->s 2->p int main() { cin >> N >> K >> R >> S >> P >> T; ll ans = 0; for (int i = 0; i < K; i++) { if (T[i] == 'r') { dp[i][2] = P; } else if (T[i] == 'p') { dp[i][1] = S; } else { dp[i][0] = R; } ll tmp = 0; for (int j = i + K; j < N; j += K) { dp[j][0] = max(dp[j - K][1], dp[j - K][2]); dp[j][1] = max(dp[j - K][0], dp[j - K][2]); dp[j][2] = max(dp[j - K][0], dp[j - K][1]); if (T[j] == 'r') { dp[j][2] = P + max(dp[j - K][0], dp[j - K][1]); } else if (T[j] == 'p') { dp[j][1] = S + max(dp[j - K][0], dp[j - K][2]); } else { dp[j][0] = R + max(dp[j - K][1], dp[j - K][2]); } tmp = max(tmp, max(dp[j][0], max(dp[j][1], dp[j][2]))); } // ans += tmp; } for (int i = N - 1; i > N - 1 - K; i--) { ans += max(dp[i][0], max(dp[i][1], dp[i][2])); } cout << ans << endl; return 0; }
replace
26
27
26
27
TLE
p02820
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define fi first #define se second #define all(vr) vr.begin(), vr.end() typedef long long ll; ll x, n; ll r, s, p, k, ans, dem; string t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); freopen("input.txt", "r", stdin); cin >> n >> k; cin >> r >> s >> p; cin >> t; t = '@' + t; for (int i = 1; i <= k; i++) t = t + '@'; dem = 1; for (int i = 1; i <= min(n, k); i++) for (int j = 1; j <= n && j * k + i <= n + k; j++) { if (j * k + i > n + k) break; if (t[j * k + i] != t[(j - 1) * k + i]) { if (dem != 0) { dem += (dem % 2 == 1); if (t[(j - 1) * k + i] == 'r') ans += p * (dem / 2); if (t[(j - 1) * k + i] == 's') ans += r * (dem / 2); if (t[(j - 1) * k + i] == 'p') ans += s * (dem / 2); dem = 1; continue; } if (t[(j - 1) * k + i] == 'r') ans += p; if (t[(j - 1) * k + i] == 's') ans += r; if (t[(j - 1) * k + i] == 'p') ans += s; } else dem++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define fi first #define se second #define all(vr) vr.begin(), vr.end() typedef long long ll; ll x, n; ll r, s, p, k, ans, dem; string t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt","r",stdin); cin >> n >> k; cin >> r >> s >> p; cin >> t; t = '@' + t; for (int i = 1; i <= k; i++) t = t + '@'; dem = 1; for (int i = 1; i <= min(n, k); i++) for (int j = 1; j <= n && j * k + i <= n + k; j++) { if (j * k + i > n + k) break; if (t[j * k + i] != t[(j - 1) * k + i]) { if (dem != 0) { dem += (dem % 2 == 1); if (t[(j - 1) * k + i] == 'r') ans += p * (dem / 2); if (t[(j - 1) * k + i] == 's') ans += r * (dem / 2); if (t[(j - 1) * k + i] == 'p') ans += s * (dem / 2); dem = 1; continue; } if (t[(j - 1) * k + i] == 'r') ans += p; if (t[(j - 1) * k + i] == 's') ans += r; if (t[(j - 1) * k + i] == 'p') ans += s; } else dem++; } cout << ans << endl; return 0; }
replace
18
19
18
19
0
p02820
C++
Runtime Error
#define LOCAL #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define ALL(x) ((x).begin(), (x).end()) #define SIZE(x) ((int)(x).size()) #define BITS(x) (1LL << (x)) constexpr int INF = 1001001001; constexpr ll LINF = 1001001001001001001LL; constexpr int MOD = 1000000007; constexpr double EPS = 1e-10; constexpr double PI = 3.141592653589793; constexpr int dx4[4] = {1, 0, -1, 0}; constexpr int dy4[4] = {0, 1, 0, -1}; constexpr int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; constexpr int dy8[8] = {1, 0, -1, 1, -1, 1, 0, -1}; random_device seed_gen; mt19937 mrand(seed_gen()); mt19937_64 mrand64(seed_gen()); int rand32(int x) { return mrand() % x; } ll rand64(long x) { return mrand64() % x; } template <typename A, typename B> ostream &operator<<(ostream &out, const pair<A, B> &a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream &operator<<(ostream &out, const array<T, N> &a) { out << "["; bool first = true; for (auto &v : a) { out << (first ? "" : ","); out << v; first = 0; } out << "]"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) { out << "["; bool first = true; for (auto &v : a) { out << (first ? "" : ","); out << v; first = 0; } out << "]"; return out; } template <typename T, class Cmp> ostream &operator<<(ostream &out, const set<T, Cmp> &a) { out << "{"; bool first = true; for (auto &v : a) { out << (first ? "" : ","); out << v; first = 0; } out << "}"; return out; } template <typename U, typename T, class Cmp> ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) { out << "{"; bool first = true; for (auto &p : a) { out << (first ? "" : ","); out << p.first << ": " << p.second; first = 0; } out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif 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...); } struct fast_ios { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(IOS_PREC); if (AUTOFLUSH) cout << unitbuf; } } fast_ios_; int dp[110000][4]; int main() { int n, k, r, s, p; string t; cin >> n >> k >> r >> s >> p >> t; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { if (j != 0) dp[i + k][j] = max(dp[i + k][j], dp[i][0]); if (j != 1) dp[i + k][j] = max(dp[i + k][j], dp[i][1]); if (j != 2) dp[i + k][j] = max(dp[i + k][j], dp[i][2]); } if (t[i] == 'r') dp[i + k][2] += p; if (t[i] == 's') dp[i + k][0] += r; if (t[i] == 'p') dp[i + k][1] += s; } int ans = 0; for (int i = n; i < n + k; i++) { ans += max({dp[i][0], dp[i][1], dp[i][2]}); } cout << ans << endl; return 0; }
#define LOCAL #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define ALL(x) ((x).begin(), (x).end()) #define SIZE(x) ((int)(x).size()) #define BITS(x) (1LL << (x)) constexpr int INF = 1001001001; constexpr ll LINF = 1001001001001001001LL; constexpr int MOD = 1000000007; constexpr double EPS = 1e-10; constexpr double PI = 3.141592653589793; constexpr int dx4[4] = {1, 0, -1, 0}; constexpr int dy4[4] = {0, 1, 0, -1}; constexpr int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; constexpr int dy8[8] = {1, 0, -1, 1, -1, 1, 0, -1}; random_device seed_gen; mt19937 mrand(seed_gen()); mt19937_64 mrand64(seed_gen()); int rand32(int x) { return mrand() % x; } ll rand64(long x) { return mrand64() % x; } template <typename A, typename B> ostream &operator<<(ostream &out, const pair<A, B> &a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream &operator<<(ostream &out, const array<T, N> &a) { out << "["; bool first = true; for (auto &v : a) { out << (first ? "" : ","); out << v; first = 0; } out << "]"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) { out << "["; bool first = true; for (auto &v : a) { out << (first ? "" : ","); out << v; first = 0; } out << "]"; return out; } template <typename T, class Cmp> ostream &operator<<(ostream &out, const set<T, Cmp> &a) { out << "{"; bool first = true; for (auto &v : a) { out << (first ? "" : ","); out << v; first = 0; } out << "}"; return out; } template <typename U, typename T, class Cmp> ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) { out << "{"; bool first = true; for (auto &p : a) { out << (first ? "" : ","); out << p.first << ": " << p.second; first = 0; } out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif 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...); } struct fast_ios { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(IOS_PREC); if (AUTOFLUSH) cout << unitbuf; } } fast_ios_; int dp[220000][4]; int main() { int n, k, r, s, p; string t; cin >> n >> k >> r >> s >> p >> t; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { if (j != 0) dp[i + k][j] = max(dp[i + k][j], dp[i][0]); if (j != 1) dp[i + k][j] = max(dp[i + k][j], dp[i][1]); if (j != 2) dp[i + k][j] = max(dp[i + k][j], dp[i][2]); } if (t[i] == 'r') dp[i + k][2] += p; if (t[i] == 's') dp[i + k][0] += r; if (t[i] == 'p') dp[i + k][1] += s; } int ans = 0; for (int i = n; i < n + k; i++) { ans += max({dp[i][0], dp[i][1], dp[i][2]}); } cout << ans << endl; return 0; }
replace
134
135
134
135
0
p02821
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define mod (int)(1e9 + 7) #define inf (int)(3e18) #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i < n; i++) #define P std::pair<int, int> #define PiP std::pair<int, std::pair<int, int>> #define all(v) v.begin(), v.end() #define mkp std::make_pair #define prique(T) std::priority_queue<T, vector<T>, greater<T>> using namespace std; template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); } template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); } bool prime(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return x != 1; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int mod_pow(int x, int y, int m = mod) { int res = 1; while (y > 0) { if (y & 1) { res = res * x % m; } x = x * x % m; y >>= 1; } return res; } int comb(int x, int y) { if (y > x) return 0; return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod; } int get_rand(int MIN, int MAX) { std::random_device rnd; std::mt19937 mt32(rnd()); std::uniform_int_distribution<int> engine(MIN, MAX); return engine(mt32); } /*--------Library Zone!--------*/ int n, m, a[100005]; int sum[100005]; signed main() { cin >> n >> m; REP(i, n + 1) { cin >> a[i]; } sort(a + 1, a + n + 1); REP(i, n + 1) { sum[i] += a[i]; sum[i + 1] += sum[i]; } int ok = inf, ng = 0; while (ok - ng > 1) { int mid = (ok + ng) / 2; int sum = 0; REP(i, n + 1) { int ko = lower_bound(a + 1, a + n + 1, mid - a[i]) - a; sum += n - ko + 1; } if (sum <= m) ok = mid; else ng = mid; } vector<int> v; int all = 0; int ans = 0; REP(i, n + 1) { int ko = lower_bound(a + 1, a + n + 1, ok - a[i]) - a; all += n - ko + 1; ans += sum[n] - sum[ko - 1]; ans += a[i] * (n - ko + 1); if (ko != 1) { v.push_back(a[i] + a[ko - 1]); } } sort(all(v), greater<>()); rep(i, m - all) ans += v[i]; cout << ans << endl; }
#include <bits/stdc++.h> #define int long long #define mod (int)(1e9 + 7) #define inf (int)(3e18) #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i < n; i++) #define P std::pair<int, int> #define PiP std::pair<int, std::pair<int, int>> #define all(v) v.begin(), v.end() #define mkp std::make_pair #define prique(T) std::priority_queue<T, vector<T>, greater<T>> using namespace std; template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); } template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); } bool prime(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return x != 1; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int mod_pow(int x, int y, int m = mod) { int res = 1; while (y > 0) { if (y & 1) { res = res * x % m; } x = x * x % m; y >>= 1; } return res; } int comb(int x, int y) { if (y > x) return 0; return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod; } int get_rand(int MIN, int MAX) { std::random_device rnd; std::mt19937 mt32(rnd()); std::uniform_int_distribution<int> engine(MIN, MAX); return engine(mt32); } /*--------Library Zone!--------*/ int n, m, a[100005]; int sum[100005]; signed main() { cin >> n >> m; REP(i, n + 1) { cin >> a[i]; } sort(a + 1, a + n + 1); REP(i, n + 1) { sum[i] += a[i]; sum[i + 1] += sum[i]; } int ok = inf, ng = 0; while (ok - ng > 1) { int mid = (ok + ng) / 2; int sum = 0; REP(i, n + 1) { int ko = lower_bound(a + 1, a + n + 1, mid - a[i]) - a; sum += n - ko + 1; } if (sum <= m) ok = mid; else ng = mid; } vector<int> v; int all = 0; int ans = 0; REP(i, n + 1) { int ko = lower_bound(a + 1, a + n + 1, ok - a[i]) - a; all += n - ko + 1; ans += sum[n] - sum[ko - 1]; ans += a[i] * (n - ko + 1); if (ko != 1) { v.push_back(a[i] + a[ko - 1]); } } sort(all(v), greater<>()); ans += (m - all) * (ok - 1); cout << ans << endl; }
replace
96
97
96
97
0
p02821
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int64; const int MAX = 1e5 + 5; int n, a[MAX]; int64 m, t[MAX], sum[MAX]; pair<int64, int64> f(int x) { int64 cnt = 0, tot_sum = 0; for (int i = 1; i <= n; i++) { int64 c = t[max(1, x - a[i])]; int64 s = sum[max(1, x - a[i])]; cnt += c; tot_sum += s; tot_sum += c * a[i]; } return {cnt, tot_sum}; } int main() { scanf("%d%lld", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); t[a[i]]++; sum[a[i]] += a[i]; } for (int i = 1e5; i > 0; i--) { t[i] += t[i + 1]; sum[i] += sum[i + 1]; } int st = 1, nd = 2e5, x = 0; while (st <= nd) { int mid = (st + nd) >> 1; if (f(mid).first >= m) { x = max(x, mid); st = mid + 1; } else nd = mid - 1; } auto o = f(x + 1); int64 ans = 1LL * (m - o.first) * x + o.second; printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int64; const int MAX = 2e5 + 5; int n, a[MAX]; int64 m, t[MAX], sum[MAX]; pair<int64, int64> f(int x) { int64 cnt = 0, tot_sum = 0; for (int i = 1; i <= n; i++) { int64 c = t[max(1, x - a[i])]; int64 s = sum[max(1, x - a[i])]; cnt += c; tot_sum += s; tot_sum += c * a[i]; } return {cnt, tot_sum}; } int main() { scanf("%d%lld", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); t[a[i]]++; sum[a[i]] += a[i]; } for (int i = 1e5; i > 0; i--) { t[i] += t[i + 1]; sum[i] += sum[i + 1]; } int st = 1, nd = 2e5, x = 0; while (st <= nd) { int mid = (st + nd) >> 1; if (f(mid).first >= m) { x = max(x, mid); st = mid + 1; } else nd = mid - 1; } auto o = f(x + 1); int64 ans = 1LL * (m - o.first) * x + o.second; printf("%lld\n", ans); return 0; }
replace
4
5
4
5
0
p02821
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, m) for (long long i = 0; i < m; i++) #define per(i, m) for (long long i = m - 1; i >= 0; i--) #define FOR(i, n, m) for (long long i = n; i < m; i++) #define ROF(i, n, m) for (long long i = m - 1; i >= n; i--) #define SORT(v, n) \ do { \ sort(v, v + n); \ reverse(v, v + n); \ } while (0) #define all(x) (x).begin(), (x).end() #define F first #define S second #define MP make_pair #define MT make_tuple #define EPS (1e-7) #define INF (1e18) #define PI (acos(-1)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using namespace std; typedef long long ll; const ll MOD = 1000000007; typedef pair<int, int> P; typedef pair<ll, ll> LP; ll POW(ll x, ll n) { x %= MOD; if (n == 0) return 1; if (n % 2 == 0) return POW(x * x, n / 2) % MOD; return x % MOD * POW(x, n - 1) % MOD; } ll POW2(ll x, ll n) { if (n == 0) return 1; if (n % 2 == 0) return POW2(x * x, n / 2); return x * POW2(x, n - 1); } ll POW3(ll x, ll n, ll m) { x %= m; if (n == 0) return 1; if (n % 2 == 0) return POW3(x * x, n / 2, m) % m; return x * POW3(x, n - 1, m) % m; } ll gcd(ll u, ll v) { ll r; while (0 != v) { r = u % v; u = v; v = r; } return u; } ll lcm(ll u, ll v) { return u / gcd(u, v) * v; } ll KAI(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI(m - 1) % MOD; } ll KAI2(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI2(m - 1); } ll extGCD(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } inline ll mod(ll a, ll m) { return (a % m + m) % m; } ll modinv(ll a) { ll x, y; extGCD(a, MOD, x, y); return mod(x, MOD); } ll COM(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI(m) % MOD * modinv(KAI(n) % MOD * KAI(m - n) % MOD) % MOD; } ll COM2(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI2(m) / KAI2(n) / KAI2(m - n); } ll DEC(ll x, ll m, ll n) // xのm進数でのx^nの位の値 { return x % POW2(m, n + 1) / POW2(m, n); } ll keta(ll x, ll n) // xのn進数での桁数 { if (x == 0) return 0; return keta(x / n, n) + 1; } ll DIV(ll x, ll n) // x!のnで割り切れる回数 { if (x == 0) return 0; return x / n + DIV(x / n, n); } ll ORD(ll x, ll n) // xのnで割り切れる回数 { if (x == 0) return INF; if (x % n != 0) return 0; return 1 + ORD(x / n, n); } ll SUP(ll x, ll n) // xのnで割れなくなるまで割ったときの余り { if (x == 0) return 0; if (x % n != 0) return x; return SUP(x / n, n); } ll DigSum(ll n) // 10進数での桁和 { if (n == 0) return 0; return n % 10 + DigSum(n / 10); } ll SGS(ll x, ll y, ll m) // 1+x+…+x^(y-1)をmで割った余り { if (y == 0) return 0; if (y % 2 == 0) { return (1 + POW3(x, y / 2, m)) * SGS(x, y / 2, m) % m; } return (1 + x * SGS(x, y - 1, m)) % m; } ll SSGS(ll x, ll y, ll m) // Σ[k=1→y](1+x+…+x^(k-1))をmで割った余り { if (y == 0) return 0; if (y == 1) return 1; if (y % 2 == 0) { return (SSGS(x, y / 2, m) * (POW3(x, y / 2, m) + 1) % m + SGS(x, y / 2, m) * y / 2 % m) % m; } return (SSGS(x, y - 1, m) * x % m + y) % m; } void shuffle(ll array[], ll size) { for (ll i = 0; i < size; i++) { ll j = rand() % size; ll t = array[i]; array[i] = array[j]; array[j] = t; } } ll SQRT(ll n) { ll ok, ng, mid; ng = n + 1; if (303700500 < ng) ng = 303700500; ok = 0; while (abs(ok - ng) > 1) { mid = (ok + ng) / 2; if (mid * mid <= n) { ok = mid; } else { ng = mid; } } return ok; } struct UnionFind { vector<int> par; vector<int> sizes; UnionFind(int n) : par(n), sizes(n, 1) { rep(i, n) par[i] = i; } int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sizes[x] < sizes[y]) swap(x, y); par[y] = x; sizes[x] += sizes[y]; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sizes[find(x)]; } }; map<int64_t, int> prime_factor(int64_t n) { map<int64_t, int> ret; for (int64_t i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } bool is_prime(int64_t x) { if (x == 1) return false; for (int64_t i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } struct edge { ll to, cost; }; struct graph { ll V; vector<vector<edge>> G; vector<ll> d; graph(ll n) { init(n); } void init(ll n) { V = n; G.resize(V); d.resize(V); rep(i, V) { d[i] = INF; } } void add_edge(ll s, ll t, ll cost) { edge e; e.to = t, e.cost = cost; G[s].push_back(e); } void dijkstra(ll s) { rep(i, V) { d[i] = INF; } d[s] = 0; priority_queue<LP, vector<LP>, greater<LP>> que; que.push(LP(0, s)); while (!que.empty()) { LP p = que.top(); que.pop(); ll v = p.second; if (d[v] < p.first) continue; for (auto e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(LP(d[e.to], e.to)); } } } } }; ll d[3100][3100]; void warshall_floyd(ll n) { rep(i, n) rep(j, n) rep(k, n) d[j][k] = min(d[j][k], d[j][i] + d[i][k]); } struct bit { ll m; vector<ll> b; bit(ll i) { m = i; b.resize(m + 1); } ll num(ll i) { return b[i]; } ll sum(ll i) { ll s = 0; while (i > 0) { s += b[i]; i -= i & -i; } return s; } void add(ll i, ll x) { while (i <= m) { b[i] += x; i += i & -i; } } }; int main() { ll n, m, a[110000], c[110000] = {}, k, u[110000] = {}, l; cin >> n >> m; rep(i, n) cin >> a[i]; sort(a, a + n); rep(i, n) c[a[i]]++; per(i, 105000) c[i] += c[i + 1]; per(i, 105000) { if (c[i + 1] < c[i]) { rep(j, c[i] - c[i + 1]) { u[c[i + 1] + j + 1] = u[c[i + 1] + j] + i; } } } ll ok, ng, mid; ng = 210000; ok = 0; while (abs(ok - ng) > 1) { mid = (ok + ng) / 2; k = 0; rep(i, n) { k += c[max(mid - a[i], 0ll)]; } if (k >= m) { ok = mid; l = k; } else { ng = mid; } } k = 0; rep(i, n) { k += c[max(ok - a[i], 0ll)] * a[i]; k += u[c[max(ok - a[i], 0ll)]]; } printf("%lld", k - (l - m) * ok); }
#include <bits/stdc++.h> #define rep(i, m) for (long long i = 0; i < m; i++) #define per(i, m) for (long long i = m - 1; i >= 0; i--) #define FOR(i, n, m) for (long long i = n; i < m; i++) #define ROF(i, n, m) for (long long i = m - 1; i >= n; i--) #define SORT(v, n) \ do { \ sort(v, v + n); \ reverse(v, v + n); \ } while (0) #define all(x) (x).begin(), (x).end() #define F first #define S second #define MP make_pair #define MT make_tuple #define EPS (1e-7) #define INF (1e18) #define PI (acos(-1)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using namespace std; typedef long long ll; const ll MOD = 1000000007; typedef pair<int, int> P; typedef pair<ll, ll> LP; ll POW(ll x, ll n) { x %= MOD; if (n == 0) return 1; if (n % 2 == 0) return POW(x * x, n / 2) % MOD; return x % MOD * POW(x, n - 1) % MOD; } ll POW2(ll x, ll n) { if (n == 0) return 1; if (n % 2 == 0) return POW2(x * x, n / 2); return x * POW2(x, n - 1); } ll POW3(ll x, ll n, ll m) { x %= m; if (n == 0) return 1; if (n % 2 == 0) return POW3(x * x, n / 2, m) % m; return x * POW3(x, n - 1, m) % m; } ll gcd(ll u, ll v) { ll r; while (0 != v) { r = u % v; u = v; v = r; } return u; } ll lcm(ll u, ll v) { return u / gcd(u, v) * v; } ll KAI(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI(m - 1) % MOD; } ll KAI2(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI2(m - 1); } ll extGCD(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } inline ll mod(ll a, ll m) { return (a % m + m) % m; } ll modinv(ll a) { ll x, y; extGCD(a, MOD, x, y); return mod(x, MOD); } ll COM(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI(m) % MOD * modinv(KAI(n) % MOD * KAI(m - n) % MOD) % MOD; } ll COM2(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI2(m) / KAI2(n) / KAI2(m - n); } ll DEC(ll x, ll m, ll n) // xのm進数でのx^nの位の値 { return x % POW2(m, n + 1) / POW2(m, n); } ll keta(ll x, ll n) // xのn進数での桁数 { if (x == 0) return 0; return keta(x / n, n) + 1; } ll DIV(ll x, ll n) // x!のnで割り切れる回数 { if (x == 0) return 0; return x / n + DIV(x / n, n); } ll ORD(ll x, ll n) // xのnで割り切れる回数 { if (x == 0) return INF; if (x % n != 0) return 0; return 1 + ORD(x / n, n); } ll SUP(ll x, ll n) // xのnで割れなくなるまで割ったときの余り { if (x == 0) return 0; if (x % n != 0) return x; return SUP(x / n, n); } ll DigSum(ll n) // 10進数での桁和 { if (n == 0) return 0; return n % 10 + DigSum(n / 10); } ll SGS(ll x, ll y, ll m) // 1+x+…+x^(y-1)をmで割った余り { if (y == 0) return 0; if (y % 2 == 0) { return (1 + POW3(x, y / 2, m)) * SGS(x, y / 2, m) % m; } return (1 + x * SGS(x, y - 1, m)) % m; } ll SSGS(ll x, ll y, ll m) // Σ[k=1→y](1+x+…+x^(k-1))をmで割った余り { if (y == 0) return 0; if (y == 1) return 1; if (y % 2 == 0) { return (SSGS(x, y / 2, m) * (POW3(x, y / 2, m) + 1) % m + SGS(x, y / 2, m) * y / 2 % m) % m; } return (SSGS(x, y - 1, m) * x % m + y) % m; } void shuffle(ll array[], ll size) { for (ll i = 0; i < size; i++) { ll j = rand() % size; ll t = array[i]; array[i] = array[j]; array[j] = t; } } ll SQRT(ll n) { ll ok, ng, mid; ng = n + 1; if (303700500 < ng) ng = 303700500; ok = 0; while (abs(ok - ng) > 1) { mid = (ok + ng) / 2; if (mid * mid <= n) { ok = mid; } else { ng = mid; } } return ok; } struct UnionFind { vector<int> par; vector<int> sizes; UnionFind(int n) : par(n), sizes(n, 1) { rep(i, n) par[i] = i; } int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sizes[x] < sizes[y]) swap(x, y); par[y] = x; sizes[x] += sizes[y]; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sizes[find(x)]; } }; map<int64_t, int> prime_factor(int64_t n) { map<int64_t, int> ret; for (int64_t i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } bool is_prime(int64_t x) { if (x == 1) return false; for (int64_t i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } struct edge { ll to, cost; }; struct graph { ll V; vector<vector<edge>> G; vector<ll> d; graph(ll n) { init(n); } void init(ll n) { V = n; G.resize(V); d.resize(V); rep(i, V) { d[i] = INF; } } void add_edge(ll s, ll t, ll cost) { edge e; e.to = t, e.cost = cost; G[s].push_back(e); } void dijkstra(ll s) { rep(i, V) { d[i] = INF; } d[s] = 0; priority_queue<LP, vector<LP>, greater<LP>> que; que.push(LP(0, s)); while (!que.empty()) { LP p = que.top(); que.pop(); ll v = p.second; if (d[v] < p.first) continue; for (auto e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(LP(d[e.to], e.to)); } } } } }; ll d[3100][3100]; void warshall_floyd(ll n) { rep(i, n) rep(j, n) rep(k, n) d[j][k] = min(d[j][k], d[j][i] + d[i][k]); } struct bit { ll m; vector<ll> b; bit(ll i) { m = i; b.resize(m + 1); } ll num(ll i) { return b[i]; } ll sum(ll i) { ll s = 0; while (i > 0) { s += b[i]; i -= i & -i; } return s; } void add(ll i, ll x) { while (i <= m) { b[i] += x; i += i & -i; } } }; int main() { ll n, m, a[310000], c[310000] = {}, k, u[310000] = {}, l; cin >> n >> m; rep(i, n) cin >> a[i]; sort(a, a + n); rep(i, n) c[a[i]]++; per(i, 105000) c[i] += c[i + 1]; per(i, 105000) { if (c[i + 1] < c[i]) { rep(j, c[i] - c[i + 1]) { u[c[i + 1] + j + 1] = u[c[i + 1] + j] + i; } } } ll ok, ng, mid; ng = 210000; ok = 0; while (abs(ok - ng) > 1) { mid = (ok + ng) / 2; k = 0; rep(i, n) { k += c[max(mid - a[i], 0ll)]; } if (k >= m) { ok = mid; l = k; } else { ng = mid; } } k = 0; rep(i, n) { k += c[max(ok - a[i], 0ll)] * a[i]; k += u[c[max(ok - a[i], 0ll)]]; } printf("%lld", k - (l - m) * ok); }
replace
340
341
340
341
0
p02821
C++
Runtime Error
// Problem : E - Handshake // Contest : AtCoder Beginner Contest 149 // URL : https://atcoder.jp/contests/abc149/tasks/abc149_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; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; #define all(x) (x).begin(), (x).end() #define io() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) vector<int> a; int n; ll m; bool check(int mid) { int j = n; ll ret = 0; for (int i = 1; i <= n; i++) { while (j >= 1 && a[i] + a[j] >= mid) j--; ret += n - j; } return ret >= m; } int main() { scanf("%d %lld", &n, &m); a.resize(n + 1); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a.begin() + 1, a.end()); int low = a[0] << 1, high = a[n] << 1 | 1, mid, ans = -1; while (low <= high) { mid = (low + high) >> 1; if (check(mid)) low = mid + 1, ans = mid; else high = mid - 1; } assert(ans != -1); vector<ll> psa(n + 1); for (int i = 1; i <= n; i++) psa[i] = psa[i - 1] + a[i]; ll ret = 0; int j = n; ll cnt = 0; int worst = INT_MAX; for (int i = 1; i <= n; i++) { while (j >= 1 && a[i] + a[j] >= ans) j--; if (j < n) worst = min(worst, a[i] + a[j + 1]); ret += psa[n] - psa[j] + a[i] * ll(n - j); cnt += n - j; } assert(cnt - m <= 1); printf("%lld\n", cnt > m ? ret - worst : ret); return 0; }
// Problem : E - Handshake // Contest : AtCoder Beginner Contest 149 // URL : https://atcoder.jp/contests/abc149/tasks/abc149_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; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; #define all(x) (x).begin(), (x).end() #define io() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) vector<int> a; int n; ll m; bool check(int mid) { int j = n; ll ret = 0; for (int i = 1; i <= n; i++) { while (j >= 1 && a[i] + a[j] >= mid) j--; ret += n - j; } return ret >= m; } int main() { scanf("%d %lld", &n, &m); a.resize(n + 1); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a.begin() + 1, a.end()); int low = a[0] << 1, high = a[n] << 1 | 1, mid, ans = -1; while (low <= high) { mid = (low + high) >> 1; if (check(mid)) low = mid + 1, ans = mid; else high = mid - 1; } assert(ans != -1); vector<ll> psa(n + 1); for (int i = 1; i <= n; i++) psa[i] = psa[i - 1] + a[i]; ll ret = 0; int j = n; ll cnt = 0; int worst = INT_MAX; for (int i = 1; i <= n; i++) { while (j >= 1 && a[i] + a[j] >= ans) j--; if (j < n) worst = min(worst, a[i] + a[j + 1]); ret += psa[n] - psa[j] + a[i] * ll(n - j); cnt += n - j; } assert(worst == ans); printf("%lld\n", ret - ans * (cnt - m)); return 0; }
replace
62
64
62
64
0
p02821
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #define ll long long using namespace std; ll n, suff[100100], cnt[100100], suffval[100100], a[100100]; int main() { ll m; cin >> n >> m; for (ll i = 0; i < n; i++) cin >> a[i], cnt[a[i]]++; sort(a, a + n); for (ll i = 1e5; i >= 0; i--) suff[i] = cnt[i] + suff[i + 1]; for (ll i = 1e5; i >= 0; i--) suffval[i] = suffval[i + 1] + (cnt[i] * i); ll Start = 0, End = 2e5, canSum = 0; while (Start <= End) { ll mid = (Start + End) / 2; ll needSum = mid + 1; ll Res = 0; for (ll i = 0; i < n; i++) { Res += (suff[max((ll)0, needSum - a[i])] - (needSum - a[i] <= a[i] ? cnt[a[i]] : 0)); if (needSum - a[i] <= a[i]) Res += cnt[a[i]]; } if (Res <= m) canSum = mid, End = mid - 1; else Start = mid + 1; } ll Ans = 0; canSum++; for (ll i = 0; i < n; i++) { Ans += suffval[max((ll)0, canSum - a[i])]; ll Ways = 0; Ways += (suff[max((ll)0, canSum - a[i])] - (canSum - a[i] <= a[i] ? cnt[a[i]] : 0)); if (canSum - a[i] <= a[i]) Ways += cnt[a[i]]; m -= Ways; Ans += a[i] * Ways; } cout << Ans + m * (canSum - 1); return 0; }
#include <bits/stdc++.h> #include <iostream> #define ll long long using namespace std; ll n, suff[200100], cnt[200100], suffval[200100], a[200100]; int main() { ll m; cin >> n >> m; for (ll i = 0; i < n; i++) cin >> a[i], cnt[a[i]]++; sort(a, a + n); for (ll i = 1e5; i >= 0; i--) suff[i] = cnt[i] + suff[i + 1]; for (ll i = 1e5; i >= 0; i--) suffval[i] = suffval[i + 1] + (cnt[i] * i); ll Start = 0, End = 2e5, canSum = 0; while (Start <= End) { ll mid = (Start + End) / 2; ll needSum = mid + 1; ll Res = 0; for (ll i = 0; i < n; i++) { Res += (suff[max((ll)0, needSum - a[i])] - (needSum - a[i] <= a[i] ? cnt[a[i]] : 0)); if (needSum - a[i] <= a[i]) Res += cnt[a[i]]; } if (Res <= m) canSum = mid, End = mid - 1; else Start = mid + 1; } ll Ans = 0; canSum++; for (ll i = 0; i < n; i++) { Ans += suffval[max((ll)0, canSum - a[i])]; ll Ways = 0; Ways += (suff[max((ll)0, canSum - a[i])] - (canSum - a[i] <= a[i] ? cnt[a[i]] : 0)); if (canSum - a[i] <= a[i]) Ways += cnt[a[i]]; m -= Ways; Ans += a[i] * Ways; } cout << Ans + m * (canSum - 1); return 0; }
replace
5
6
5
6
0
p02821
Python
Runtime Error
n, m = map(int, input().split()) a = sorted(list(map(int, input().split())), reverse=True) s = a[0] t = 0 j = 1 ret = 0 for i in range(n): s -= a[i] ret += a[i] * 2 m -= 1 while j < n and (i >= n - 1 or a[i + 1] + a[i + 1] <= a[i] + a[j]): if m < i * 2: for k in range(i): if m > 0: ret += a[j] + a[k] m -= 1 if m > 0: ret += a[j] + a[k] m -= 1 else: ret += a[j] * i * 2 + t * 2 m -= i * 2 s += a[j] j += 1 num = (j - i - 1) * 2 if m <= num: for k in range(i + 1, j): if m > 0: ret += a[i] + a[k] m -= 1 if m > 0: ret += a[i] + a[k] m -= 1 break m -= num ret += s * 2 + a[i] * num t += a[i] print(a, m, num, s, i, j, ret) print(ret)
n, m = map(int, input().split()) a = sorted(list(map(int, input().split())), reverse=True) s = [0] for ai in a: s.append(ai + s[-1]) def count(x, accum=False): ret = 0 for ai in a: lo, hi = -1, n while hi - lo > 1: mid = (lo + hi) // 2 if ai + a[mid] >= x: lo = mid else: hi = mid ret += ai * hi + s[hi] if accum else hi return ret lo, hi = 0, 1000000000 while hi - lo > 1: mid = (lo + hi) // 2 if count(mid) >= m: lo = mid else: hi = mid print(count(lo, accum=True) - (count(lo) - m) * lo)
replace
2
39
2
30
0
p02821
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma comment(linker, "/stack:200000000") #include <bits/stdc++.h> #define SIZE 2010 #define rep(i, a, b) for (int i = a; i <= b; ++i) #define ll long long #define mp make_pair #define pi acos(-1.0) #define eps 1e-6 using namespace std; void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } ll n, k, ans, cnt, res; ll a[SIZE], pre[SIZE]; bool check(ll x) { res = cnt = 0; rep(i, 1, n) { ll pos = lower_bound(a + 1, a + n + 1, x - a[i]) - a; cnt += n - pos + 1ll; res += pre[n] - pre[pos - 1] + a[i] * (n - pos + 1); } return cnt >= k; } int main() { io(); cin >> n >> k; rep(i, 1, n) cin >> a[i]; sort(a + 1, a + 1 + n); rep(i, 1, n) pre[i] = pre[i - 1] + a[i]; ll L = 1, R = 2e5 + 10, md; while (L <= R) { md = (L + R) >> 1; if (check(md)) ans = md, L = md + 1; else R = md - 1; } cout << res - (cnt - k) * ans; }
#define _CRT_SECURE_NO_WARNINGS #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma comment(linker, "/stack:200000000") #include <bits/stdc++.h> #define SIZE 200010 #define rep(i, a, b) for (int i = a; i <= b; ++i) #define ll long long #define mp make_pair #define pi acos(-1.0) #define eps 1e-6 using namespace std; void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } ll n, k, ans, cnt, res; ll a[SIZE], pre[SIZE]; bool check(ll x) { res = cnt = 0; rep(i, 1, n) { ll pos = lower_bound(a + 1, a + n + 1, x - a[i]) - a; cnt += n - pos + 1ll; res += pre[n] - pre[pos - 1] + a[i] * (n - pos + 1); } return cnt >= k; } int main() { io(); cin >> n >> k; rep(i, 1, n) cin >> a[i]; sort(a + 1, a + 1 + n); rep(i, 1, n) pre[i] = pre[i - 1] + a[i]; ll L = 1, R = 2e5 + 10, md; while (L <= R) { md = (L + R) >> 1; if (check(md)) ans = md, L = md + 1; else R = md - 1; } cout << res - (cnt - k) * ans; }
replace
6
7
6
7
0
p02821
C++
Runtime Error
//{{{ #include <bits/stdc++.h> using namespace std; #define repX(a, b, c, x, ...) x #define repN(a) repX a #define rep(...) repN((__VA_ARGS__, rep3, rep2, loop))(__VA_ARGS__) #define rrep(...) repN((__VA_ARGS__, rrep3, rrep2))(__VA_ARGS__) #define loop(n) rep2(i_, n) #define rep2(i, n) rep3(i, 0, n) #define rep3(i, begin, end) \ for (int i = (int)(begin), i##_end = (int)(end); i < i##_end; ++i) #define rrep2(i, n) rrep3(i, 0, n) #define rrep3(i, begin, end) \ for (int i = (int)(end - 1), i##_end = (int)(begin); i >= i##_end; --i) #define foreach(x, a) for (auto &x : a) #define each(x, a) for (auto &x : a) #define sz(x) ((int)(x).size()) struct IoSetup { IoSetup() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); }; } ioSetup; using ll = long long; template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &j) { o << "{" << j.first << ", " << j.second << "}"; return o; } template <class T, class U> ostream &operator<<(ostream &o, const map<T, U> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const set<T> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const multiset<T> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &j) { o << "{"; for (int i = 0; i < (int)j.size(); ++i) o << (i > 0 ? ", " : "") << j[i]; o << "}"; return o; } #ifdef LOCAL inline void dump(void) { cerr << endl; } template <class Head, class... Tail> inline void dump(Head &&head, Tail &&...tail) { cerr << head << " "; dump(tail...); } #define debug(...) \ do { \ cerr << "[L." << __LINE__ << "]\t" << #__VA_ARGS__ << "="; \ dump(__VA_ARGS__); \ } while (0) #else #define dump(...) #define debug(...) #endif template <class T> inline void sort(T &a) { sort(a.begin(), a.end()); } template <class T, class Compare> inline void sort(T &a, Compare comp) { sort(a.begin(), a.end(), comp); } template <class T> inline void rsort(T &a) { sort(a.rbegin(), a.rend()); } template <class T> inline void reverse(T &a) { reverse(a.begin(), a.end()); } template <class T> inline T Sum(vector<T> &a) { return accumulate(a.begin(), a.end(), (T)0); } template <class T> inline T max(vector<T> &a) { return *max_element(a.begin(), a.end()); } template <class T> inline T min(vector<T> &a) { return *min_element(a.begin(), a.end()); } template <class T, class U> inline bool chmax(T &a, const U &b) { return (b > a) ? (a = b, true) : false; } template <class T, class U> inline bool chmin(T &a, const U &b) { return (b < a) ? (a = b, true) : false; } ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; } ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; } class in { int n; public: in() { n = -1; } in(int n_) : n(n_){}; template <class T> operator T() { T ret; cin >> ret; return ret; } template <class T> operator vector<T>() { assert(n >= 0); vector<T> ret(n); for (int i = 0; i < n; ++i) cin >> ret[i]; return ret; } }; template <class T> void print(const T &a) { cout << a; } int out() { cout << '\n'; return 0; } template <class T> int out(const T &t) { print(t); cout << '\n'; return 0; } template <class Head, class... Tail> int out(const Head &head, const Tail &...tail) { print(head); cout << " "; out(tail...); return 0; } template <std::uint_fast64_t Mod> class Modular { using u64 = std::uint_fast64_t; public: u64 a; constexpr Modular(const u64 x = 0) noexcept : a(x % Mod) {} constexpr u64 val() const noexcept { return a; } constexpr Modular operator+(const Modular rhs) const noexcept { return Modular(*this) += rhs; } constexpr Modular operator-(const Modular rhs) const noexcept { return Modular(*this) -= rhs; } constexpr Modular operator*(const Modular rhs) const noexcept { return Modular(*this) *= rhs; } constexpr Modular operator/(const Modular rhs) const noexcept { return Modular(*this) /= rhs; } constexpr bool operator==(const Modular rhs) const noexcept { return Modular(*this).val() == rhs.val(); } Modular &operator+=(const Modular rhs) noexcept { a += rhs.a; if (a >= Mod) a -= Mod; return *this; } Modular &operator-=(const Modular rhs) noexcept { if (a < rhs.a) a += Mod; a -= rhs.a; return *this; } Modular &operator++() noexcept { return *this += 1; } Modular &operator--() noexcept { return *this -= 1; } Modular &operator*=(const Modular rhs) noexcept { a = a * rhs.a % Mod; return *this; } Modular &operator/=(Modular rhs) noexcept { u64 exp = Mod - 2; while (exp) { if (exp % 2) *this *= rhs; rhs *= rhs; exp /= 2; } return *this; } }; template <std::uint_fast64_t Mod> ostream &operator<<(ostream &os, const Modular<Mod> &m) { return os << m.a; } const double pi = acos(-1); const double eps = 1e-9; const ll inf = 1001001001; const ll mod = (ll)1e9 + 7; using mint = Modular<mod>; //}}} int main() { ll N = in(); ll M = in(); vector<ll> A = in(N); sort(A); vector<ll> B(A); reverse(B); // 上位 i 番目までの人の値の和 vector<ll> cum(N); cum[0] = 0; rep(i, 1, N + 1) { cum[i] = cum[i - 1] + B[i - 1]; } // 和がx以上の組がM組以上ならtrue auto f = [&](ll x) { ll cnt = 0; rep(i, N) { ll num = A.end() - lower_bound(A.begin(), A.end(), x - A[i]); cnt += num; } return cnt >= M; }; // x以上の要素をM組まで足した値を返す。 // ただし、上位M組目の和がちょうどxであることがわかっているものとする。 auto g = [&](ll x) { ll res = 0; ll cnt = 0; rep(i, N) { ll num = A.end() - lower_bound(A.begin(), A.end(), x - A[i]); cnt += num; res += num * A[i] + cum[num]; } res -= (cnt - M) * x; return res; }; ll ok = 0; ll ng = A[N - 1] * 2 + 1; while (abs(ok - ng) > 1) { ll md = (ok + ng) / 2; (f(md) ? ok : ng) = md; } ll ans = g(ok); out(ans); }
//{{{ #include <bits/stdc++.h> using namespace std; #define repX(a, b, c, x, ...) x #define repN(a) repX a #define rep(...) repN((__VA_ARGS__, rep3, rep2, loop))(__VA_ARGS__) #define rrep(...) repN((__VA_ARGS__, rrep3, rrep2))(__VA_ARGS__) #define loop(n) rep2(i_, n) #define rep2(i, n) rep3(i, 0, n) #define rep3(i, begin, end) \ for (int i = (int)(begin), i##_end = (int)(end); i < i##_end; ++i) #define rrep2(i, n) rrep3(i, 0, n) #define rrep3(i, begin, end) \ for (int i = (int)(end - 1), i##_end = (int)(begin); i >= i##_end; --i) #define foreach(x, a) for (auto &x : a) #define each(x, a) for (auto &x : a) #define sz(x) ((int)(x).size()) struct IoSetup { IoSetup() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); }; } ioSetup; using ll = long long; template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &j) { o << "{" << j.first << ", " << j.second << "}"; return o; } template <class T, class U> ostream &operator<<(ostream &o, const map<T, U> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const set<T> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const multiset<T> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &j) { o << "{"; for (int i = 0; i < (int)j.size(); ++i) o << (i > 0 ? ", " : "") << j[i]; o << "}"; return o; } #ifdef LOCAL inline void dump(void) { cerr << endl; } template <class Head, class... Tail> inline void dump(Head &&head, Tail &&...tail) { cerr << head << " "; dump(tail...); } #define debug(...) \ do { \ cerr << "[L." << __LINE__ << "]\t" << #__VA_ARGS__ << "="; \ dump(__VA_ARGS__); \ } while (0) #else #define dump(...) #define debug(...) #endif template <class T> inline void sort(T &a) { sort(a.begin(), a.end()); } template <class T, class Compare> inline void sort(T &a, Compare comp) { sort(a.begin(), a.end(), comp); } template <class T> inline void rsort(T &a) { sort(a.rbegin(), a.rend()); } template <class T> inline void reverse(T &a) { reverse(a.begin(), a.end()); } template <class T> inline T Sum(vector<T> &a) { return accumulate(a.begin(), a.end(), (T)0); } template <class T> inline T max(vector<T> &a) { return *max_element(a.begin(), a.end()); } template <class T> inline T min(vector<T> &a) { return *min_element(a.begin(), a.end()); } template <class T, class U> inline bool chmax(T &a, const U &b) { return (b > a) ? (a = b, true) : false; } template <class T, class U> inline bool chmin(T &a, const U &b) { return (b < a) ? (a = b, true) : false; } ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; } ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; } class in { int n; public: in() { n = -1; } in(int n_) : n(n_){}; template <class T> operator T() { T ret; cin >> ret; return ret; } template <class T> operator vector<T>() { assert(n >= 0); vector<T> ret(n); for (int i = 0; i < n; ++i) cin >> ret[i]; return ret; } }; template <class T> void print(const T &a) { cout << a; } int out() { cout << '\n'; return 0; } template <class T> int out(const T &t) { print(t); cout << '\n'; return 0; } template <class Head, class... Tail> int out(const Head &head, const Tail &...tail) { print(head); cout << " "; out(tail...); return 0; } template <std::uint_fast64_t Mod> class Modular { using u64 = std::uint_fast64_t; public: u64 a; constexpr Modular(const u64 x = 0) noexcept : a(x % Mod) {} constexpr u64 val() const noexcept { return a; } constexpr Modular operator+(const Modular rhs) const noexcept { return Modular(*this) += rhs; } constexpr Modular operator-(const Modular rhs) const noexcept { return Modular(*this) -= rhs; } constexpr Modular operator*(const Modular rhs) const noexcept { return Modular(*this) *= rhs; } constexpr Modular operator/(const Modular rhs) const noexcept { return Modular(*this) /= rhs; } constexpr bool operator==(const Modular rhs) const noexcept { return Modular(*this).val() == rhs.val(); } Modular &operator+=(const Modular rhs) noexcept { a += rhs.a; if (a >= Mod) a -= Mod; return *this; } Modular &operator-=(const Modular rhs) noexcept { if (a < rhs.a) a += Mod; a -= rhs.a; return *this; } Modular &operator++() noexcept { return *this += 1; } Modular &operator--() noexcept { return *this -= 1; } Modular &operator*=(const Modular rhs) noexcept { a = a * rhs.a % Mod; return *this; } Modular &operator/=(Modular rhs) noexcept { u64 exp = Mod - 2; while (exp) { if (exp % 2) *this *= rhs; rhs *= rhs; exp /= 2; } return *this; } }; template <std::uint_fast64_t Mod> ostream &operator<<(ostream &os, const Modular<Mod> &m) { return os << m.a; } const double pi = acos(-1); const double eps = 1e-9; const ll inf = 1001001001; const ll mod = (ll)1e9 + 7; using mint = Modular<mod>; //}}} int main() { ll N = in(); ll M = in(); vector<ll> A = in(N); sort(A); vector<ll> B(A); reverse(B); // 上位 i 番目までの人の値の和 vector<ll> cum(N + 1); cum[0] = 0; rep(i, 1, N + 1) { cum[i] = cum[i - 1] + B[i - 1]; } // 和がx以上の組がM組以上ならtrue auto f = [&](ll x) { ll cnt = 0; rep(i, N) { ll num = A.end() - lower_bound(A.begin(), A.end(), x - A[i]); cnt += num; } return cnt >= M; }; // x以上の要素をM組まで足した値を返す。 // ただし、上位M組目の和がちょうどxであることがわかっているものとする。 auto g = [&](ll x) { ll res = 0; ll cnt = 0; rep(i, N) { ll num = A.end() - lower_bound(A.begin(), A.end(), x - A[i]); cnt += num; res += num * A[i] + cum[num]; } res -= (cnt - M) * x; return res; }; ll ok = 0; ll ng = A[N - 1] * 2 + 1; while (abs(ok - ng) > 1) { ll md = (ok + ng) / 2; (f(md) ? ok : ng) = md; } ll ans = g(ok); out(ans); }
replace
218
219
218
219
0
p02821
C++
Time Limit Exceeded
#pragma target("avx") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> P; typedef vector<ll> V; typedef unordered_map<ll, ll> U_MAP; typedef priority_queue<ll> pq; typedef priority_queue<ll, vector<ll>, greater<ll>> rpq; const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10; const ll LINF = 1e18; #define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++) #define rrep(i, a, b) for (ll i = (a); i < (b); i++) #define rrrep(i, a, b) for (ll i = (a); i >= (b); i--) #define all(v) (v).begin(), (v).end() #define Size(n) (n).size() #define Cout(x) cout << (x) << endl #define doublecout(a) cout << fixed << setprecision(15) << a << endl; #define fi first #define se second #define m_p make_pair #define p_b push_back string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) //------ Believe yourself as a genius!!!!!! ------ ////////////////////////// vector<complex<ld>> fft(vector<complex<ld>> a, bool inverse = false) { ll n = a.size(); ll h = 0; for (ll i = 0; 1 << i < n; i++) h++; rep(i, n) { ll j = 0; rep(k, h) j |= (i >> k & 1) << (h - 1 - k); if (i < j) swap(a[i], a[j]); } for (ll b = 1; b < n; b *= 2) { rep(j, b) { complex<ld> w = polar(1.0, (2 * M_PI) / (2 * b) * j * (inverse ? 1 : -1)); for (ll k = 0; k < n; k += b * 2) { complex<ld> s = a[j + k]; complex<ld> t = a[j + k + b] * w; a[j + k] = s + t; a[j + k + b] = s - t; } } } if (inverse) rep(i, n) a[i] /= n; return a; } vector<complex<ld>> fft(vector<ld> a, bool inverse = false) { vector<complex<ld>> a_complex(a.size()); rep(i, Size(a)) a_complex[i] = complex<ld>(a[i], 0); return fft(a_complex, inverse); } vector<ld> convolve(vector<ld> a, vector<ld> b) { ll s = a.size() + b.size() - 1; ll t = 1; while (t < s) t *= 2; a.resize(t); b.resize(t); vector<complex<ld>> A = fft(a); vector<complex<ld>> B = fft(b); rep(i, t) { A[i] *= B[i]; } A = fft(A, true); a.resize(s); rep(i, s) a[i] = A[i].real(); return a; } //////////////////////// int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; // int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1}; string alph("abcdefghijklmnopqrstuvwxyz"), s; ll n, cnt, c, d, tmp, m, h, w, x, y, sum, k, q, now, tans; int main(void) { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cin >> n >> m; vector<ld> a(n + 1), dat(5 * 100000); dat[0] = 0; rep(i, n) cin >> a[i], dat[a[i]]++; vector<ld> ans = convolve(dat, dat); rrrep(i, Size(ans) - 1, 0) { tmp = (ll)(ans[i] + 0.5); if (now + tmp <= m) { now += tmp; tans += i * tmp; } else { now = (m - now); tans += i * now; break; } } Cout(tans); return 0; }
#pragma target("avx") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> P; typedef vector<ll> V; typedef unordered_map<ll, ll> U_MAP; typedef priority_queue<ll> pq; typedef priority_queue<ll, vector<ll>, greater<ll>> rpq; const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10; const ll LINF = 1e18; #define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++) #define rrep(i, a, b) for (ll i = (a); i < (b); i++) #define rrrep(i, a, b) for (ll i = (a); i >= (b); i--) #define all(v) (v).begin(), (v).end() #define Size(n) (n).size() #define Cout(x) cout << (x) << endl #define doublecout(a) cout << fixed << setprecision(15) << a << endl; #define fi first #define se second #define m_p make_pair #define p_b push_back string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) //------ Believe yourself as a genius!!!!!! ------ ////////////////////////// vector<complex<ld>> fft(vector<complex<ld>> a, bool inverse = false) { ll n = a.size(); ll h = 0; for (ll i = 0; 1 << i < n; i++) h++; rep(i, n) { ll j = 0; rep(k, h) j |= (i >> k & 1) << (h - 1 - k); if (i < j) swap(a[i], a[j]); } for (ll b = 1; b < n; b *= 2) { rep(j, b) { complex<ld> w = polar(1.0, (2 * M_PI) / (2 * b) * j * (inverse ? 1 : -1)); for (ll k = 0; k < n; k += b * 2) { complex<ld> s = a[j + k]; complex<ld> t = a[j + k + b] * w; a[j + k] = s + t; a[j + k + b] = s - t; } } } if (inverse) rep(i, n) a[i] /= n; return a; } vector<complex<ld>> fft(vector<ld> a, bool inverse = false) { vector<complex<ld>> a_complex(a.size()); rep(i, Size(a)) a_complex[i] = complex<ld>(a[i], 0); return fft(a_complex, inverse); } vector<ld> convolve(vector<ld> a, vector<ld> b) { ll s = a.size() + b.size() - 1; ll t = 1; while (t < s) t *= 2; a.resize(t); b.resize(t); vector<complex<ld>> A = fft(a); vector<complex<ld>> B = fft(b); rep(i, t) { A[i] *= B[i]; } A = fft(A, true); a.resize(s); rep(i, s) a[i] = A[i].real(); return a; } //////////////////////// int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; // int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1}; string alph("abcdefghijklmnopqrstuvwxyz"), s; ll n, cnt, c, d, tmp, m, h, w, x, y, sum, k, q, now, tans; int main(void) { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cin >> n >> m; vector<ld> a(n + 1), dat(2 * 100000 + 1); dat[0] = 0; rep(i, n) cin >> a[i], dat[a[i]]++; vector<ld> ans = convolve(dat, dat); rrrep(i, Size(ans) - 1, 0) { tmp = (ll)(ans[i] + 0.5); if (now + tmp <= m) { now += tmp; tans += i * tmp; } else { now = (m - now); tans += i * now; break; } } Cout(tans); return 0; }
replace
119
120
119
120
TLE
p02821
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; int n = 0, m = 0; int arr[100000]; int suf[100010]; int get(int l, int r) { int res = suf[l]; res -= suf[r + 1]; return res; } bool check(int mm) { int res = 0; for (int i = 0; i < n; i++) { auto it = lower_bound(arr, arr + n, mm - arr[i]); res += (arr + n) - it; } return (res >= m); } void print(int mm) { int res = 0, Min = INT_MAX, sum = 0; for (int i = 0; i < n; i++) { auto it = lower_bound(arr, arr + n, mm - arr[i]); if (it != arr + n) Min = min(Min, (*it) + arr[i]); res += (arr + n) - it; sum += suf[it - arr]; sum += ((arr + n) - it) * arr[i]; } assert((res == m) || (res == m + 1) || (res == m + 2)); if (res > m) { sum -= Min; } cout << sum << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); int sm = 0; for (int i = n - 1; i >= 0; i--) { sm += arr[i]; suf[i] = sm; } int ll = arr[0] * 2, rr = arr[n - 1] * 2; while (ll < rr - 1) { int mm = (ll + rr) / 2; if (check(mm)) { ll = mm; } else rr = mm; } if (check(rr)) { print(rr); } else print(ll); return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; int n = 0, m = 0; int arr[100000]; int suf[100010]; int get(int l, int r) { int res = suf[l]; res -= suf[r + 1]; return res; } bool check(int mm) { int res = 0; for (int i = 0; i < n; i++) { auto it = lower_bound(arr, arr + n, mm - arr[i]); res += (arr + n) - it; } return (res >= m); } void print(int mm) { int res = 0, Min = INT_MAX, sum = 0; for (int i = 0; i < n; i++) { auto it = lower_bound(arr, arr + n, mm - arr[i]); if (it != arr + n) Min = min(Min, (*it) + arr[i]); res += (arr + n) - it; sum += suf[it - arr]; sum += ((arr + n) - it) * arr[i]; } sum -= (res - m) * Min; cout << sum << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); int sm = 0; for (int i = n - 1; i >= 0; i--) { sm += arr[i]; suf[i] = sm; } int ll = arr[0] * 2, rr = arr[n - 1] * 2; while (ll < rr - 1) { int mm = (ll + rr) / 2; if (check(mm)) { ll = mm; } else rr = mm; } if (check(rr)) { print(rr); } else print(ll); return 0; }
replace
34
38
34
35
0
p02821
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<int, P> P1; typedef pair<P, P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define mod 1000000007 #define fi first #define sc second #define rep(i, x) for (long long i = 0; i < x; i++) #define repn(i, x) for (long long i = 1; i <= x; i++) #define SORT(x) sort(x.begin(), x.end()) #define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end()) #define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin()) #define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin()) vector<pair<string, P>> vec; // vector<vector<int>> data(3, vector<int>(4)); int main() { long long N; long long M; scanf("%lld", &N); vector<long long> A(N - 1 + 1); scanf("%lld", &M); for (int i = 0; i <= N - 1; i++) { scanf("%lld", &A[i]); } SORT(A); vector<ll> sum(N); repn(i, N) { sum[i] = sum[i - 1] + A[i - 1]; } auto calc = [&](ll x) { ll res = 0, total = 0; rep(i, N) { res += N - POSL(A, x - A[i]); // if (x == 67) // cout << i << "; " << POSL(A, x - A[i]) << res << " " // << (sum[N] - sum[POSL(A, x - A[i])]) << " " << A[i] * // res // << endl; total += (sum[N] - sum[POSL(A, x - A[i])]) + A[i] * (N - POSL(A, x - A[i])); } return P(res, total); }; long long lb = -1, ub = 10000000000001; while (ub - lb > 1) { long long mid = (lb + ub) / 2; if (calc(mid).first >= M) lb = mid; else ub = mid; } ll c1 = calc(lb).first, c2 = calc(lb).second; // cout << c1 << " " << c2 << " " << lb << endl; cout << c2 - (c1 - M) * lb << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<int, P> P1; typedef pair<P, P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define mod 1000000007 #define fi first #define sc second #define rep(i, x) for (long long i = 0; i < x; i++) #define repn(i, x) for (long long i = 1; i <= x; i++) #define SORT(x) sort(x.begin(), x.end()) #define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end()) #define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin()) #define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin()) vector<pair<string, P>> vec; // vector<vector<int>> data(3, vector<int>(4)); int main() { long long N; long long M; scanf("%lld", &N); vector<long long> A(N - 1 + 1); scanf("%lld", &M); for (int i = 0; i <= N - 1; i++) { scanf("%lld", &A[i]); } SORT(A); vector<ll> sum(N + 1); repn(i, N) { sum[i] = sum[i - 1] + A[i - 1]; } auto calc = [&](ll x) { ll res = 0, total = 0; rep(i, N) { res += N - POSL(A, x - A[i]); // if (x == 67) // cout << i << "; " << POSL(A, x - A[i]) << res << " " // << (sum[N] - sum[POSL(A, x - A[i])]) << " " << A[i] * // res // << endl; total += (sum[N] - sum[POSL(A, x - A[i])]) + A[i] * (N - POSL(A, x - A[i])); } return P(res, total); }; long long lb = -1, ub = 10000000000001; while (ub - lb > 1) { long long mid = (lb + ub) / 2; if (calc(mid).first >= M) lb = mid; else ub = mid; } ll c1 = calc(lb).first, c2 = calc(lb).second; // cout << c1 << " " << c2 << " " << lb << endl; cout << c2 - (c1 - M) * lb << endl; return 0; }
replace
34
35
34
35
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p02821
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; /// source: https://atcoder.jp/contests/abc149/submissions/9237911 long long n, m; vector<long long> a, sum; long long solve(); long long check(long long x); int main() { cin >> n >> m; a.resize(n); for (int i = 0; i < n; ++i) cin >> a[i]; sort(a.begin(), a.end(), greater<long long>()); cout << solve() << endl; return 0; } long long solve() { long long res = 0, l = 0, r = 2 * a[0] + 1; while (r > l) { long long now = (l + r) / 2; if (check(now) >= m) l = now; else r = now; } sum = a; for (int i = 1; i < n; ++i) sum[i] += sum[i - 1]; for (int i = 0; i < n; ++i) { if (a[0] + a[i] < l) continue; long long now = upper_bound(a.begin(), a.end(), l - a[i], greater<long long>()) - a.begin(); res += now * a[i] + sum[now - 1]; } res -= l * (check(l) - m); return res; } long long check(long long x) { long long cnt = 0; for (int i = 0; i < n; ++i) { if (a[0] + a[i] < x) continue; cnt += upper_bound(a.begin(), a.end(), x - a[i], greater<long long>()) - a.begin(); } return cnt; }
#include <bits/stdc++.h> using namespace std; /// source: https://atcoder.jp/contests/abc149/submissions/9237911 long long n, m; vector<long long> a, sum; long long solve(); long long check(long long x); int main() { cin >> n >> m; a.resize(n); for (int i = 0; i < n; ++i) cin >> a[i]; sort(a.begin(), a.end(), greater<long long>()); cout << solve() << endl; return 0; } long long solve() { long long res = 0, l = 0, r = 2 * a[0] + 1; /// while(r > l) { DILE TLE ASHE while (r > l + 1) { long long now = (l + r) / 2; if (check(now) >= m) l = now; else r = now; } sum = a; for (int i = 1; i < n; ++i) sum[i] += sum[i - 1]; for (int i = 0; i < n; ++i) { if (a[0] + a[i] < l) continue; long long now = upper_bound(a.begin(), a.end(), l - a[i], greater<long long>()) - a.begin(); res += now * a[i] + sum[now - 1]; } res -= l * (check(l) - m); return res; } long long check(long long x) { long long cnt = 0; for (int i = 0; i < n; ++i) { if (a[0] + a[i] < x) continue; cnt += upper_bound(a.begin(), a.end(), x - a[i], greater<long long>()) - a.begin(); } return cnt; }
replace
21
22
21
24
TLE
p02821
C++
Runtime Error
/* -*- coding: utf-8 -*- * * e.cc: E - Handshake */ #include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_A = 100000; /* typedef */ typedef long long ll; typedef pair<ll, ll> pll; /* global variables */ int as[MAX_N]; ll ass[MAX_N + 1]; /* subroutines */ ll check(int n, int b) { ll cnt = 0; for (int i = 0; i < n; i++) { int k = lower_bound(as, as + n, b - as[i]) - as; cnt += (n - k); } return cnt; } pll cntsum(int n, int b) { ll cnt = 0, sum = 0; for (int i = 0; i < n; i++) { int k = lower_bound(as, as + n, b - as[i]) - as; cnt += n - k; sum += ass[n] - ass[k] + (ll)(n - k) * as[i]; } return pll(cnt, sum); } /* main */ int main() { int n; ll m; scanf("%d%lld", &n, &m); for (int i = 0; i < n; i++) scanf("%d", as + i); sort(as, as + n); // for (int i = 0; i < n; i++) printf("%d ", as[i]); putchar('\n'); for (int i = 0; i < n; i++) ass[i + 1] = ass[i] + as[i]; int b0 = 0, b1 = MAX_A * 2; while (b0 + 1 < b1) { int b = (b0 + b1) / 2; if (check(n, b) >= m) b0 = b; else b1 = b; } pll p0 = cntsum(n, b0); pll p1 = cntsum(n, b1); ll c0 = p0.first, s0 = p0.second; ll c1 = p1.first, s1 = p1.second; // printf("b0=%d, cnt=%lld, sum=%lld\n", b0, c0, s0); // printf("b1=%d, cnt=%lld, sum=%lld\n", b1, c1, s1); ll s = (s0 - s1) / (c0 - c1) * (m - c1) + s1; printf("%lld\n", s); return 0; }
/* -*- coding: utf-8 -*- * * e.cc: E - Handshake */ #include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_A = 100000; /* typedef */ typedef long long ll; typedef pair<ll, ll> pll; /* global variables */ int as[MAX_N]; ll ass[MAX_N + 1]; /* subroutines */ ll check(int n, int b) { ll cnt = 0; for (int i = 0; i < n; i++) { int k = lower_bound(as, as + n, b - as[i]) - as; cnt += (n - k); } return cnt; } pll cntsum(int n, int b) { ll cnt = 0, sum = 0; for (int i = 0; i < n; i++) { int k = lower_bound(as, as + n, b - as[i]) - as; cnt += n - k; sum += ass[n] - ass[k] + (ll)(n - k) * as[i]; } return pll(cnt, sum); } /* main */ int main() { int n; ll m; scanf("%d%lld", &n, &m); for (int i = 0; i < n; i++) scanf("%d", as + i); sort(as, as + n); // for (int i = 0; i < n; i++) printf("%d ", as[i]); putchar('\n'); for (int i = 0; i < n; i++) ass[i + 1] = ass[i] + as[i]; int b0 = 0, b1 = MAX_A * 2 + 1; while (b0 + 1 < b1) { int b = (b0 + b1) / 2; if (check(n, b) >= m) b0 = b; else b1 = b; } pll p0 = cntsum(n, b0); pll p1 = cntsum(n, b1); ll c0 = p0.first, s0 = p0.second; ll c1 = p1.first, s1 = p1.second; // printf("b0=%d, cnt=%lld, sum=%lld\n", b0, c0, s0); // printf("b1=%d, cnt=%lld, sum=%lld\n", b1, c1, s1); ll s = (s0 - s1) / (c0 - c1) * (m - c1) + s1; printf("%lld\n", s); return 0; }
replace
77
78
77
78
0
p02821
C++
Runtime Error
// clang-format off #include <bits/stdc++.h> #define mp make_pair #define fst first #define snd second #define forn(i,n) for (size_t i = 0; i < size_t(n); i++) #define forn1(i,n) for (size_t i = 1; i <= size_t(n); i++) #define popcnt __builtin_popcount #define ffs __builtin_ffs #define ctz __builtin_ctz #define clz __builtin_clz #define all(a) (a).begin(), (a).end() using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pli = pair<ll,int>; using pil = pair<int,ll>; using pll = pair<ll,ll>; template <typename T> using vec = vector<T>; using vi = vec<int>; using vl = vec<ll>; template <typename T> using que = queue<T>; template <typename T> using deq = deque<T>; template <typename T> T id(T b) {return b;}; template <typename T> void chmax(T &x, T y) {if (x < y) x = y;} template <typename T> void chmin(T &x, T y) {if (x > y) x = y;} template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); } void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); } // clang-format on const int MAX_A = 100000; const int MAX_N = 100000; ll n, m; int a[MAX_N]; ll cnt[MAX_A + 1]; ll sum[MAX_A + 1]; bool check(int x) { ll c = 0; forn(i, n) { c += cnt[max(0, x - a[i])]; } return c >= m; } int main() { fastio(); cin >> n >> m; forn(i, n) { cin >> a[i]; cnt[a[i]]++; } sum[MAX_A] = cnt[MAX_A] * MAX_A; for (int i = MAX_A - 1; i >= 0; i--) { sum[i] += sum[i + 1] + cnt[i] * i; cnt[i] += cnt[i + 1]; } int l = 1, r = 200001; while (r - l > 1) { int x = (l + r) / 2; if (check(x)) l = x; else r = x; } // cout << l << endl; ll c = 0; ll ans = 0; forn(i, n) { int j = max(0, l + 1 - a[i]); if (j > MAX_A) continue; // cout << j << " " << cnt[j] << " " << sum[j] << endl; c += cnt[j]; ans += sum[j] + 1LL * cnt[j] * a[i]; } ans += (m - c) * l; cout << ans << endl; return 0; }
// clang-format off #include <bits/stdc++.h> #define mp make_pair #define fst first #define snd second #define forn(i,n) for (size_t i = 0; i < size_t(n); i++) #define forn1(i,n) for (size_t i = 1; i <= size_t(n); i++) #define popcnt __builtin_popcount #define ffs __builtin_ffs #define ctz __builtin_ctz #define clz __builtin_clz #define all(a) (a).begin(), (a).end() using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pli = pair<ll,int>; using pil = pair<int,ll>; using pll = pair<ll,ll>; template <typename T> using vec = vector<T>; using vi = vec<int>; using vl = vec<ll>; template <typename T> using que = queue<T>; template <typename T> using deq = deque<T>; template <typename T> T id(T b) {return b;}; template <typename T> void chmax(T &x, T y) {if (x < y) x = y;} template <typename T> void chmin(T &x, T y) {if (x > y) x = y;} template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); } void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); } // clang-format on const int MAX_A = 100000; const int MAX_N = 100000; ll n, m; int a[MAX_N]; ll cnt[MAX_A + 1]; ll sum[MAX_A + 1]; bool check(int x) { ll c = 0; forn(i, n) { int j = max(0, x - a[i]); if (j > MAX_A) continue; c += cnt[j]; } return c >= m; } int main() { fastio(); cin >> n >> m; forn(i, n) { cin >> a[i]; cnt[a[i]]++; } sum[MAX_A] = cnt[MAX_A] * MAX_A; for (int i = MAX_A - 1; i >= 0; i--) { sum[i] += sum[i + 1] + cnt[i] * i; cnt[i] += cnt[i + 1]; } int l = 1, r = 200001; while (r - l > 1) { int x = (l + r) / 2; if (check(x)) l = x; else r = x; } // cout << l << endl; ll c = 0; ll ans = 0; forn(i, n) { int j = max(0, l + 1 - a[i]); if (j > MAX_A) continue; // cout << j << " " << cnt[j] << " " << sum[j] << endl; c += cnt[j]; ans += sum[j] + 1LL * cnt[j] * a[i]; } ans += (m - c) * l; cout << ans << endl; return 0; }
replace
46
47
46
52
0
p02821
C++
Runtime Error
#pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx,avx2") #include <bits/stdc++.h> namespace { template <int n, typename T> void mult(const T *__restrict a, const T *__restrict b, T *__restrict res) { if (n <= 64) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { res[i + j] += a[i] * b[j]; } } } else { const int mid = n / 2; alignas(64) T btmp[n], E[n] = {}; auto atmp = btmp + mid; for (int i = 0; i < mid; i++) { atmp[i] = a[i] + a[i + mid]; btmp[i] = b[i] + b[i + mid]; } mult<mid>(atmp, btmp, E); mult<mid>(a + 0, b + 0, res); mult<mid>(a + mid, b + mid, res + n); for (int i = 0; i < mid; i++) { const auto tmp = res[i + mid]; res[i + mid] += E[i] - res[i] - res[i + 2 * mid]; res[i + 2 * mid] += E[i + mid] - tmp - res[i + 3 * mid]; } } } } // namespace using ll = long long; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); const int NMAX = (1 << 11) * 49; alignas(64) static ll a[NMAX], b[NMAX * 2]; int n; ll k; std::cin >> n >> k; for (int i = 0, val; i < n; i++) { std::cin >> val; a[val]++; } mult<NMAX>(a, a, b); ll answ(0); for (int i = 2 * NMAX - 1; k > 0 && i >= 0; i--) { answ += std::min(k, b[i]) * i; k -= b[i]; } std::cout << answ << std::endl; return 0; }
#pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx") #include <bits/stdc++.h> namespace { template <int n, typename T> void mult(const T *__restrict a, const T *__restrict b, T *__restrict res) { if (n <= 64) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { res[i + j] += a[i] * b[j]; } } } else { const int mid = n / 2; alignas(64) T btmp[n], E[n] = {}; auto atmp = btmp + mid; for (int i = 0; i < mid; i++) { atmp[i] = a[i] + a[i + mid]; btmp[i] = b[i] + b[i + mid]; } mult<mid>(atmp, btmp, E); mult<mid>(a + 0, b + 0, res); mult<mid>(a + mid, b + mid, res + n); for (int i = 0; i < mid; i++) { const auto tmp = res[i + mid]; res[i + mid] += E[i] - res[i] - res[i + 2 * mid]; res[i + 2 * mid] += E[i + mid] - tmp - res[i + 3 * mid]; } } } } // namespace using ll = long long; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); const int NMAX = (1 << 11) * 49; alignas(64) static ll a[NMAX], b[NMAX * 2]; int n; ll k; std::cin >> n >> k; for (int i = 0, val; i < n; i++) { std::cin >> val; a[val]++; } mult<NMAX>(a, a, b); ll answ(0); for (int i = 2 * NMAX - 1; k > 0 && i >= 0; i--) { answ += std::min(k, b[i]) * i; k -= b[i]; } std::cout << answ << std::endl; return 0; }
replace
1
2
1
2
0
p02821
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using i32 = std::int_fast32_t; using i64 = std::int_fast64_t; template <typename T> constexpr T inf = numeric_limits<T>::has_infinity ? numeric_limits<T>::infinity() : numeric_limits<T>::max() / 4; #define REP(i, stop) FOR(i, 0, stop) #define FOR(i, start, stop) \ for (int i = start, i##_len = stop; i < i##_len; ++i) #define RREP(i, n) for (int i = n; i-- > 0;) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define COMP(key) [](const auto &a, const auto &b) { return a.key < b.key; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &a) { return is >> a.first >> a.second; } template <typename T> istream &operator>>(istream &is, vector<T> &a) { for (T &x : a) is >> x; return is; } void LOG(const int &x) { cerr << x << "\n"; } template <typename T> void LOG(const vector<T> &a) { for (const T &x : a) cerr << x << " "; cerr << "\n"; } template <typename T, std::size_t S> void LOG(const T (&a)[S]) { REP(i, S) cerr << a[i] << " "; cerr << "\n"; } template <typename T, std::size_t S, std::size_t R> void LOG(const T (&a)[S][R]) { REP(i, S) { REP(j, R) cerr << a[i][j] << " "; cerr << "\n"; } } i64 ceil(i64 a, i64 b) { return (a - 1) / b + 1; } i64 gcd(i64 a, i64 b) { while (b != 0) { i64 t = b; b = a % b; a = t; } return a; } i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; } i64 gcd(const vector<i64> &v) { return accumulate(ALL(v), 1LL, static_cast<i64 (*)(i64, i64)>(gcd)); } i64 lcm(const vector<i64> &v) { return v.empty() ? 0LL : accumulate(v.begin() + 1, v.end(), v[0], static_cast<i64 (*)(i64, i64)>(lcm)); } i64 pow(i64 x, i64 y, i64 z) { i64 a = 1; while (y > 0) { if (y & 1) a = a * x % z; x = x * x % z; y /= 2; } return a; } i64 inv(i64 x, i64 m) { return pow(x, m - 2, m); } struct InitCpp { InitCpp() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } initCpp; const i64 INF = inf<i64>; using P = pair<i64, i64>; using T = pair<i64, P>; const i64 M = 1000000007; signed main() { i64 N, M; cin >> N >> M; vector<i64> A(N); cin >> A; sort(ALL(A)); vector<i64> S(N + 1); REP(i, N) S[i + 1] = S[i] + A[i]; i64 h_ng = 0; i64 h_ok = A[N - 1] * 2; i64 h = h_ok; i64 s = 0; i64 q = 0; i64 r = 0; i64 ans = 0; while (h_ok > h_ng + 1) { h = (h_ok + h_ng) / 2; s = 0; q = 0; r = 0; REP(i, N) { i64 ge = lower_bound(ALL(A), h - A[i]) - A.begin(); i64 gt = upper_bound(ALL(A), h - A[i]) - A.begin(); q += N - gt; r += gt - ge; s += S[N] - S[gt] + A[i] * (N - gt); } if (q <= M) { h_ok = h; if (M <= q + r) { cout << (s + (M - q) * h) << endl; exit(0); } } else { h_ng = h; } } exit(1); }
#include <bits/stdc++.h> using namespace std; using i32 = std::int_fast32_t; using i64 = std::int_fast64_t; template <typename T> constexpr T inf = numeric_limits<T>::has_infinity ? numeric_limits<T>::infinity() : numeric_limits<T>::max() / 4; #define REP(i, stop) FOR(i, 0, stop) #define FOR(i, start, stop) \ for (int i = start, i##_len = stop; i < i##_len; ++i) #define RREP(i, n) for (int i = n; i-- > 0;) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define COMP(key) [](const auto &a, const auto &b) { return a.key < b.key; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &a) { return is >> a.first >> a.second; } template <typename T> istream &operator>>(istream &is, vector<T> &a) { for (T &x : a) is >> x; return is; } void LOG(const int &x) { cerr << x << "\n"; } template <typename T> void LOG(const vector<T> &a) { for (const T &x : a) cerr << x << " "; cerr << "\n"; } template <typename T, std::size_t S> void LOG(const T (&a)[S]) { REP(i, S) cerr << a[i] << " "; cerr << "\n"; } template <typename T, std::size_t S, std::size_t R> void LOG(const T (&a)[S][R]) { REP(i, S) { REP(j, R) cerr << a[i][j] << " "; cerr << "\n"; } } i64 ceil(i64 a, i64 b) { return (a - 1) / b + 1; } i64 gcd(i64 a, i64 b) { while (b != 0) { i64 t = b; b = a % b; a = t; } return a; } i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; } i64 gcd(const vector<i64> &v) { return accumulate(ALL(v), 1LL, static_cast<i64 (*)(i64, i64)>(gcd)); } i64 lcm(const vector<i64> &v) { return v.empty() ? 0LL : accumulate(v.begin() + 1, v.end(), v[0], static_cast<i64 (*)(i64, i64)>(lcm)); } i64 pow(i64 x, i64 y, i64 z) { i64 a = 1; while (y > 0) { if (y & 1) a = a * x % z; x = x * x % z; y /= 2; } return a; } i64 inv(i64 x, i64 m) { return pow(x, m - 2, m); } struct InitCpp { InitCpp() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } initCpp; const i64 INF = inf<i64>; using P = pair<i64, i64>; using T = pair<i64, P>; const i64 M = 1000000007; signed main() { i64 N, M; cin >> N >> M; vector<i64> A(N); cin >> A; sort(ALL(A)); vector<i64> S(N + 1); REP(i, N) S[i + 1] = S[i] + A[i]; i64 h_ng = 0; i64 h_ok = A[N - 1] * 2 + 2; i64 h = h_ok; i64 s = 0; i64 q = 0; i64 r = 0; i64 ans = 0; while (h_ok > h_ng + 1) { h = (h_ok + h_ng) / 2; s = 0; q = 0; r = 0; REP(i, N) { i64 ge = lower_bound(ALL(A), h - A[i]) - A.begin(); i64 gt = upper_bound(ALL(A), h - A[i]) - A.begin(); q += N - gt; r += gt - ge; s += S[N] - S[gt] + A[i] * (N - gt); } if (q <= M) { h_ok = h; if (M <= q + r) { cout << (s + (M - q) * h) << endl; exit(0); } } else { h_ng = h; } } exit(1); }
replace
97
98
97
98
0
p02821
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pa pair<int, int> #define pal pair<long long, long long> #define pali pair<long long, int> #define pad pair<double, double> #define pb push_back #define mp make_pair #define COUT(v) \ for (int64_t i = 0; i < (v).size(); ++i) { \ cout << v.at(i) << endl; \ } #define REP(i, n) for (int64_t i = 0; i < n; ++i) #define FOR(i, r, n) for (int64_t i = (r); i < n; ++i) #define VIN(v) \ for (int64_t i = 0; i < (v).size(); ++i) { \ cin >> (v).at(i); \ } typedef vector<bool> bvec; typedef vector<int> ivec; typedef vector<long long> lvec; typedef vector<double> dvec; typedef vector<pa> pavec; typedef vector<pali> palivec; typedef vector<pal> palvec; typedef vector<vector<bool>> bmat; typedef vector<vector<int>> imat; typedef vector<vector<long long>> lmat; typedef vector<string> svec; typedef vector<vector<string>> smat; const ll infll = (1LL << 60) - 1; const int inf = (1 << 30) - 1; const ll MOD = 1000000007; ll gcd(ll x, ll y) { ll r = x % y; if (r == 0) return y; else return gcd(y, r); } ll lcm(ll x, ll y) { return x * y / gcd(x, y); } lvec mfactor(ll n) { bvec ip(n, true); lvec mf(n, -1); ip[0] = false; ip[1] = false; mf[0] = 0; mf[1] = 1; REP(i, n) { if (ip[i]) { mf[i] = i; for (ll j = i * i; j < n; j += i) { ip[j] = false; if (mf[j] == -1) mf[j] = i; } } } return mf; } palivec get_prime(ll n, const lvec &mf) { palivec plist; while (n != 1) { int cnt = 0; ll m = mf[n]; while (mf[n] == m) { cnt++; n /= m; } plist.pb(pali(m, cnt)); } return plist; } void COMinit(int m, lvec &fac, lvec &finv) { lvec inv(m); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < m; 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 pow_mod(ll a, ll n) { ll x = 1; while (n > 0) { if (n & 1) { x = x * a % MOD; } a = a * a % MOD; n >>= 1; } return x; } ll COM(int n, int k, const lvec &fac, const lvec &finv) { 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() { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; lvec a(n); lvec s(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.rbegin(), a.rend()); ll ok = 2 * a[0] + 1, ng = 1; while (ok - ng > 1) { ll t = (ng + ok) / 2; ll cnt = 0; ll i = 0, j = n - 1; while (i < n && j >= 0) { if (a[i] + a[j] > t) cnt += j + 1; else { if (j > 0) { j--; continue; } else j = n - 1; } i++; } if (cnt < m) ok = t; else ng = t; } s[0] = a[0]; for (int i = 1; i < n; i++) { s[i] = a[i] + s[i - 1]; } ll ans = 0; ll cnt = 0; for (int i = 0; i < n; i++) { ll ok2 = 0, ng2 = n; while (ng2 - ok2 > 1) { ll t = (ng2 + ok2) / 2; if (a[i] + a[t] >= ok) ok2 = t; else ng2 = t; } if (ok2 > 0) { ans += a[i] * (ok2 + 1) + s[ok2]; cnt += ok2 + 1; } else if (a[i] + a[ok2] >= ok) { ans += (a[i] + a[ok2]); cnt++; } } ans -= ok * (cnt - m); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pa pair<int, int> #define pal pair<long long, long long> #define pali pair<long long, int> #define pad pair<double, double> #define pb push_back #define mp make_pair #define COUT(v) \ for (int64_t i = 0; i < (v).size(); ++i) { \ cout << v.at(i) << endl; \ } #define REP(i, n) for (int64_t i = 0; i < n; ++i) #define FOR(i, r, n) for (int64_t i = (r); i < n; ++i) #define VIN(v) \ for (int64_t i = 0; i < (v).size(); ++i) { \ cin >> (v).at(i); \ } typedef vector<bool> bvec; typedef vector<int> ivec; typedef vector<long long> lvec; typedef vector<double> dvec; typedef vector<pa> pavec; typedef vector<pali> palivec; typedef vector<pal> palvec; typedef vector<vector<bool>> bmat; typedef vector<vector<int>> imat; typedef vector<vector<long long>> lmat; typedef vector<string> svec; typedef vector<vector<string>> smat; const ll infll = (1LL << 60) - 1; const int inf = (1 << 30) - 1; const ll MOD = 1000000007; ll gcd(ll x, ll y) { ll r = x % y; if (r == 0) return y; else return gcd(y, r); } ll lcm(ll x, ll y) { return x * y / gcd(x, y); } lvec mfactor(ll n) { bvec ip(n, true); lvec mf(n, -1); ip[0] = false; ip[1] = false; mf[0] = 0; mf[1] = 1; REP(i, n) { if (ip[i]) { mf[i] = i; for (ll j = i * i; j < n; j += i) { ip[j] = false; if (mf[j] == -1) mf[j] = i; } } } return mf; } palivec get_prime(ll n, const lvec &mf) { palivec plist; while (n != 1) { int cnt = 0; ll m = mf[n]; while (mf[n] == m) { cnt++; n /= m; } plist.pb(pali(m, cnt)); } return plist; } void COMinit(int m, lvec &fac, lvec &finv) { lvec inv(m); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < m; 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 pow_mod(ll a, ll n) { ll x = 1; while (n > 0) { if (n & 1) { x = x * a % MOD; } a = a * a % MOD; n >>= 1; } return x; } ll COM(int n, int k, const lvec &fac, const lvec &finv) { 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() { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; lvec a(n); lvec s(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.rbegin(), a.rend()); ll ok = 2 * a[0] + 1, ng = 1; while (ok - ng > 1) { ll t = (ng + ok) / 2; ll cnt = 0; ll i = 0, j = n - 1; while (i < n && j >= 0) { if (a[i] + a[j] > t) cnt += j + 1; else { if (j > 0) { j--; continue; } } i++; } if (cnt < m) ok = t; else ng = t; } s[0] = a[0]; for (int i = 1; i < n; i++) { s[i] = a[i] + s[i - 1]; } ll ans = 0; ll cnt = 0; for (int i = 0; i < n; i++) { ll ok2 = 0, ng2 = n; while (ng2 - ok2 > 1) { ll t = (ng2 + ok2) / 2; if (a[i] + a[t] >= ok) ok2 = t; else ng2 = t; } if (ok2 > 0) { ans += a[i] * (ok2 + 1) + s[ok2]; cnt += ok2 + 1; } else if (a[i] + a[ok2] >= ok) { ans += (a[i] + a[ok2]); cnt++; } } ans -= ok * (cnt - m); cout << ans << endl; }
replace
154
156
154
155
TLE
p02822
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define REP(i, N) for (int i = 0; i < (int)N; i++) #define FOR(i, a, b) for (int i = a; i < (int)b; i++) #define ALL(x) (x).begin(), (x).end() #define INF (1 << 30) #define LLINF (1LL << 62) #define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__) constexpr int MOD = 1000000007; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; inline int popcount(ll x) { return __builtin_popcountll(x); } inline int div2num(ll x) { return __builtin_ctzll(x); } inline bool bit(ll x, int b) { return (x >> b) & 1; } template <class T> string to_string(T s); template <class S, class T> string to_string(pair<S, T> p); string to_string(string s) { return s; } string to_string(const char s[]) { return to_string(string(s)); } template <class T> string to_string(T v) { if (v.empty()) return "{}"; string ret = "{"; for (auto x : v) ret += to_string(x) + ","; ret.back() = '}'; return ret; } template <class S, class T> string to_string(pair<S, T> p) { return "{" + to_string(p.first) + ":" + to_string(p.second) + "}"; } void debug() { cerr << endl; } template <class Head, class... Tail> void debug(Head head, Tail... tail) { cerr << to_string(head) << " "; debug(tail...); } struct IO { #ifdef _WIN32 inline char getchar_unlocked() { return getchar(); } inline void putchar_unlocked(char c) { putchar(c); } #endif std::string separator = " "; template <class T> inline void read(T &x) { char c; do { c = getchar_unlocked(); } while (c != '-' && (c < '0' || '9' < c)); bool minus = 0; if (c == '-') { minus = 1; c = getchar_unlocked(); } x = 0; while ('0' <= c && c <= '9') { x *= 10; x += c - '0'; c = getchar_unlocked(); } if (minus) x = -x; } inline void read(std::string &x) { char c; do { c = getchar_unlocked(); } while (c == ' ' || c == '\n'); x.clear(); do { x += c; c = getchar_unlocked(); } while (c != ' ' && c != '\n' && c != EOF); } template <class T> inline void read(std::vector<T> &v) { for (auto &x : v) read(x); } template <class S, class T> inline void read(std::pair<S, T> &p) { read(p.first); read(p.second); } template <class Head, class... Tail> inline void read(Head &head, Tail &...tail) { read(head); read(tail...); } template <class T> inline void write(T x) { char buf[32]; int p = 0; if (x < 0) { x = -x; putchar_unlocked('-'); } if (x == 0) putchar_unlocked('0'); while (x > 0) { buf[p++] = (x % 10) + '0'; x /= 10; } while (p) { putchar_unlocked(buf[--p]); } } inline void write(std::string x) { for (char c : x) putchar_unlocked(c); } inline void write(const char s[]) { for (int i = 0; s[i] != 0; ++i) putchar_unlocked(s[i]); } template <class T> inline void write(std::vector<T> v) { if (v.empty()) return; for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) { write(*itr); write(separator); } write(v.back()); } template <class Head, class... Tail> inline void write(Head head, Tail... tail) { write(head); write(separator); write(tail...); } template <class Head, class... Tail> inline void writeln(Head head, Tail... tail) { write(head, tail...); write("\n"); } void set_separator(std::string s) { separator = s; } } io; 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; } }; using modint = ModInt<MOD>; template <class T> struct Rerooting { function<T(T, T)> merge; function<T(T)> prop; T ident; vector<vector<int>> G; vector<vector<T>> dat, cuml, cumr; vector<T> val; vector<int> parent_id; Rerooting(vector<vector<int>> _G, function<T(T, T)> _merge, function<T(T)> _prop, T _ident) : G(_G), merge(_merge), prop(_prop), ident(_ident), dat(_G.size()), cuml(_G.size()), cumr(_G.size()), parent_id(_G.size()), val(_G.size()) { } T dfs1(int u, int par) { dat[u].resize(G[u].size()); cuml[u].resize(G[u].size() + 1); cumr[u].resize(G[u].size() + 1); cuml[u].front() = cumr[u].back() = ident; for (int i = 0; i < G[u].size(); ++i) { if (G[u][i] == par) { parent_id[u] = i; continue; } dat[u][i] = dfs1(G[u][i], u); cuml[u][i + 1] = merge(cuml[u][i], dat[u][i]); } for (int i = (int)G[u].size() - 1; i > parent_id[u]; --i) { dat[u][i] = dfs1(G[u][i], u); cumr[u][i] = merge(cumr[u][i + 1], dat[u][i]); } T ret = par == -1 ? ident : merge(cuml[u][parent_id[u]], cumr[u][parent_id[u] + 1]); return prop(ret); } void dfs2(int u, int par) { val[u] = cuml[u].back(); for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (v != par) { dat[v][parent_id[v]] = prop(merge(cuml[u][i], cumr[u][i + 1])); for (int j = parent_id[v]; j < G[v].size(); ++j) { cuml[v][j + 1] = merge(cuml[v][j], dat[v][j]); } for (int j = parent_id[v]; j >= 0; --j) { cumr[v][j] = merge(cumr[v][j + 1], dat[v][j]); } dfs2(v, u); } } } void solve() { dfs1(0, -1); dfs2(0, -1); } }; int main() { ll N; io.read(N); vector<vector<int>> G(N); REP(i, N - 1) { int A, B; io.read(A, B); G[A - 1].push_back(B - 1); G[B - 1].push_back(A - 1); } Rerooting<ll> rr( G, [](ll l, ll r) { return l + r; }, [](ll a) { return a + 1; }, 0); rr.solve(); modint ans = 0; vector<modint> pow2(N + 1); pow2[0] = 1; FOR(i, 1, N + 1) pow2[i] = pow2[i - 1] * 2; REP(i, N) { modint t = pow2[N - 1] - 1; REP(j, G[i].size()) t -= pow2[rr.dat[i][j]] - 1; ans += t; } ans /= pow2[N]; io.writeln(ans.x); return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define REP(i, N) for (int i = 0; i < (int)N; i++) #define FOR(i, a, b) for (int i = a; i < (int)b; i++) #define ALL(x) (x).begin(), (x).end() #define INF (1 << 30) #define LLINF (1LL << 62) #define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__) constexpr int MOD = 1000000007; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; inline int popcount(ll x) { return __builtin_popcountll(x); } inline int div2num(ll x) { return __builtin_ctzll(x); } inline bool bit(ll x, int b) { return (x >> b) & 1; } template <class T> string to_string(T s); template <class S, class T> string to_string(pair<S, T> p); string to_string(string s) { return s; } string to_string(const char s[]) { return to_string(string(s)); } template <class T> string to_string(T v) { if (v.empty()) return "{}"; string ret = "{"; for (auto x : v) ret += to_string(x) + ","; ret.back() = '}'; return ret; } template <class S, class T> string to_string(pair<S, T> p) { return "{" + to_string(p.first) + ":" + to_string(p.second) + "}"; } void debug() { cerr << endl; } template <class Head, class... Tail> void debug(Head head, Tail... tail) { cerr << to_string(head) << " "; debug(tail...); } struct IO { #ifdef _WIN32 inline char getchar_unlocked() { return getchar(); } inline void putchar_unlocked(char c) { putchar(c); } #endif std::string separator = " "; template <class T> inline void read(T &x) { char c; do { c = getchar_unlocked(); } while (c != '-' && (c < '0' || '9' < c)); bool minus = 0; if (c == '-') { minus = 1; c = getchar_unlocked(); } x = 0; while ('0' <= c && c <= '9') { x *= 10; x += c - '0'; c = getchar_unlocked(); } if (minus) x = -x; } inline void read(std::string &x) { char c; do { c = getchar_unlocked(); } while (c == ' ' || c == '\n'); x.clear(); do { x += c; c = getchar_unlocked(); } while (c != ' ' && c != '\n' && c != EOF); } template <class T> inline void read(std::vector<T> &v) { for (auto &x : v) read(x); } template <class S, class T> inline void read(std::pair<S, T> &p) { read(p.first); read(p.second); } template <class Head, class... Tail> inline void read(Head &head, Tail &...tail) { read(head); read(tail...); } template <class T> inline void write(T x) { char buf[32]; int p = 0; if (x < 0) { x = -x; putchar_unlocked('-'); } if (x == 0) putchar_unlocked('0'); while (x > 0) { buf[p++] = (x % 10) + '0'; x /= 10; } while (p) { putchar_unlocked(buf[--p]); } } inline void write(std::string x) { for (char c : x) putchar_unlocked(c); } inline void write(const char s[]) { for (int i = 0; s[i] != 0; ++i) putchar_unlocked(s[i]); } template <class T> inline void write(std::vector<T> v) { if (v.empty()) return; for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) { write(*itr); write(separator); } write(v.back()); } template <class Head, class... Tail> inline void write(Head head, Tail... tail) { write(head); write(separator); write(tail...); } template <class Head, class... Tail> inline void writeln(Head head, Tail... tail) { write(head, tail...); write("\n"); } void set_separator(std::string s) { separator = s; } } io; 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; } }; using modint = ModInt<MOD>; template <class T> struct Rerooting { function<T(T, T)> merge; function<T(T)> prop; T ident; vector<vector<int>> G; vector<vector<T>> dat, cuml, cumr; vector<T> val; vector<int> parent_id; Rerooting(vector<vector<int>> _G, function<T(T, T)> _merge, function<T(T)> _prop, T _ident) : G(_G), merge(_merge), prop(_prop), ident(_ident), dat(_G.size()), cuml(_G.size()), cumr(_G.size()), parent_id(_G.size()), val(_G.size()) { } T dfs1(int u, int par) { dat[u].resize(G[u].size()); cuml[u].resize(G[u].size() + 1); cumr[u].resize(G[u].size() + 1); cuml[u].front() = cumr[u].back() = ident; for (int i = 0; i < G[u].size(); ++i) { if (G[u][i] == par) { parent_id[u] = i; break; } dat[u][i] = dfs1(G[u][i], u); cuml[u][i + 1] = merge(cuml[u][i], dat[u][i]); } for (int i = (int)G[u].size() - 1; i > parent_id[u]; --i) { dat[u][i] = dfs1(G[u][i], u); cumr[u][i] = merge(cumr[u][i + 1], dat[u][i]); } T ret = par == -1 ? ident : merge(cuml[u][parent_id[u]], cumr[u][parent_id[u] + 1]); return prop(ret); } void dfs2(int u, int par) { val[u] = cuml[u].back(); for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (v != par) { dat[v][parent_id[v]] = prop(merge(cuml[u][i], cumr[u][i + 1])); for (int j = parent_id[v]; j < G[v].size(); ++j) { cuml[v][j + 1] = merge(cuml[v][j], dat[v][j]); } for (int j = parent_id[v]; j >= 0; --j) { cumr[v][j] = merge(cumr[v][j + 1], dat[v][j]); } dfs2(v, u); } } } void solve() { dfs1(0, -1); dfs2(0, -1); } }; int main() { ll N; io.read(N); vector<vector<int>> G(N); REP(i, N - 1) { int A, B; io.read(A, B); G[A - 1].push_back(B - 1); G[B - 1].push_back(A - 1); } Rerooting<ll> rr( G, [](ll l, ll r) { return l + r; }, [](ll a) { return a + 1; }, 0); rr.solve(); modint ans = 0; vector<modint> pow2(N + 1); pow2[0] = 1; FOR(i, 1, N + 1) pow2[i] = pow2[i - 1] * 2; REP(i, N) { modint t = pow2[N - 1] - 1; REP(j, G[i].size()) t -= pow2[rr.dat[i][j]] - 1; ans += t; } ans /= pow2[N]; io.writeln(ans.x); return 0; }
replace
257
258
257
258
TLE
p02822
C++
Runtime Error
// warm heart, wagging tail,and a smile just for you! // ███████████ // ███╬╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬████╬╬╬╬╬╬███ // ███████████ // ██╬╬╬╬╬████╬╬████╬╬╬╬╬██ // █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██ // ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██ // ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██ // ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████ // █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██ // ██████████████ // ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████ // ███████ █████ // ███████████████████ // #include "bits/stdc++.h" using namespace std; #define MOD 1000000007 // #define MOD 998244353 #define INF (1LL << 60) #define fs first #define sc second #define int long long #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i) #define REP(i, n) FOR(i, 0, (n)) #define RREP(i, n) RFOR(i, 0, (n)) #define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr) #define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr) #define range(i, a, b) ((a) <= (i) && (i) < (b)) #define debug(x) cout << #x << " = " << (x) << endl; #define SP << " " << #define MSB(x) (63 - __builtin_clzll(x)) #define pcnt(x) (__builtin_popcountll(x)) #define parity(i, j) (i & (1LL << j)) typedef pair<int, int> P; #include <cstdint> template <std::uint_fast64_t Modulus> class modint { using u64 = std::uint_fast64_t; u64 a; public: constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 val() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr bool operator==(const modint rhs) const noexcept { return modint(*this).val() == rhs.val(); } modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; using mint = modint<MOD>; typedef vector<mint> vec; typedef vector<vector<mint>> mat; const int N = 1e5 + 10; vector<int> edge[N]; vec beki(N, 1); mint ans = 1; int n; int dfs(int no, int par) { int res = 1; for (int to : edge[no]) { if (to == par) continue; int now = dfs(to, no); res += now; ans += ((mint)1 - beki[now]) * ((mint)1 - beki[n - now]); } return res; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; REP(_, n - 1) { int x, y; cin >> x >> y; x--; y--; edge[x].emplace_back(y); edge[y].emplace_back(x); } REP(i, n) beki[i + 1] = beki[i] / 2; dfs(0, -1); ans -= (mint)n / 2; ans -= beki[n]; cout << ans.val() << endl; return 0; }
// warm heart, wagging tail,and a smile just for you! // ███████████ // ███╬╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬████╬╬╬╬╬╬███ // ███████████ // ██╬╬╬╬╬████╬╬████╬╬╬╬╬██ // █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██ // ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██ // ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██ // ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████ // █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██ // ██████████████ // ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████ // ███████ █████ // ███████████████████ // #include "bits/stdc++.h" using namespace std; #define MOD 1000000007 // #define MOD 998244353 #define INF (1LL << 60) #define fs first #define sc second #define int long long #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i) #define REP(i, n) FOR(i, 0, (n)) #define RREP(i, n) RFOR(i, 0, (n)) #define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr) #define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr) #define range(i, a, b) ((a) <= (i) && (i) < (b)) #define debug(x) cout << #x << " = " << (x) << endl; #define SP << " " << #define MSB(x) (63 - __builtin_clzll(x)) #define pcnt(x) (__builtin_popcountll(x)) #define parity(i, j) (i & (1LL << j)) typedef pair<int, int> P; #include <cstdint> template <std::uint_fast64_t Modulus> class modint { using u64 = std::uint_fast64_t; u64 a; public: constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 val() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr bool operator==(const modint rhs) const noexcept { return modint(*this).val() == rhs.val(); } modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; using mint = modint<MOD>; typedef vector<mint> vec; typedef vector<vector<mint>> mat; const int N = 2e5 + 10; vector<int> edge[N]; vec beki(N, 1); mint ans = 1; int n; int dfs(int no, int par) { int res = 1; for (int to : edge[no]) { if (to == par) continue; int now = dfs(to, no); res += now; ans += ((mint)1 - beki[now]) * ((mint)1 - beki[n - now]); } return res; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; REP(_, n - 1) { int x, y; cin >> x >> y; x--; y--; edge[x].emplace_back(y); edge[y].emplace_back(x); } REP(i, n) beki[i + 1] = beki[i] / 2; dfs(0, -1); ans -= (mint)n / 2; ans -= beki[n]; cout << ans.val() << endl; return 0; }
replace
107
108
107
108
0
p02822
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; template <typename t> void myMemset(t *arr, t val, unsigned int size) { size /= sizeof(t); t *ptr = arr; while (size--) { *ptr = val; ptr++; } } const int mod = 1e9 + 7; int add(int a, int b) { return (a + b) % mod; } int mul(int a, int b) { return (1ll * a * b) % mod; } int fastpow(int b, int p) { if (!p) return 1; int ret = fastpow(b, p / 2); ret = mul(ret, ret); if (p & 1) ret = mul(ret, b); return ret; } const int N = 2e5 + 5; vector<int> g[N]; vector<int> szs[N]; int n, sz[N]; void dfs(int node, int parent = -1) { sz[node] = 1; for (auto &neg : g[node]) if (neg != parent) dfs(neg, node), sz[node] += sz[neg], szs[node].push_back(sz[neg]); if (n - sz[node] >= 1) szs[node].push_back(n - sz[node]); } int dp[N]; int dp2[N][3]; int val[N]; int mark[N][3], half; int solve2(int sz) { if (sz == 0) return 0; int &ans = dp[sz]; if (ans != -1) return ans; return ans = add(half, mul(half, solve2(sz - 1))); } int solve(int node, int idx, int cnt) { if (idx == szs[node].size()) return cnt == 2; int &ans = dp2[idx][cnt]; if (mark[idx][cnt] == node) return ans; mark[idx][cnt] = 1; return ans = add(mul(solve(node, idx + 1, cnt), val[szs[node][idx]]), mul(solve(node, idx + 1, min(cnt + 1, 2)), solve2(szs[node][idx]))); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); half = fastpow(2, mod - 2); val[0] = 1; for (int i = 1; i < N; i++) val[i] = mul(val[i - 1], half); cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1); int ans = 0; memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) ans = add(ans, mul(half, solve(i, 0, 0))); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; template <typename t> void myMemset(t *arr, t val, unsigned int size) { size /= sizeof(t); t *ptr = arr; while (size--) { *ptr = val; ptr++; } } const int mod = 1e9 + 7; int add(int a, int b) { return (a + b) % mod; } int mul(int a, int b) { return (1ll * a * b) % mod; } int fastpow(int b, int p) { if (!p) return 1; int ret = fastpow(b, p / 2); ret = mul(ret, ret); if (p & 1) ret = mul(ret, b); return ret; } const int N = 2e5 + 5; vector<int> g[N]; vector<int> szs[N]; int n, sz[N]; void dfs(int node, int parent = -1) { sz[node] = 1; for (auto &neg : g[node]) if (neg != parent) dfs(neg, node), sz[node] += sz[neg], szs[node].push_back(sz[neg]); if (n - sz[node] >= 1) szs[node].push_back(n - sz[node]); } int dp[N]; int dp2[N][3]; int val[N]; int mark[N][3], half; int solve2(int sz) { if (sz == 0) return 0; int &ans = dp[sz]; if (ans != -1) return ans; return ans = add(half, mul(half, solve2(sz - 1))); } int solve(int node, int idx, int cnt) { if (idx == szs[node].size()) return cnt == 2; int &ans = dp2[idx][cnt]; if (mark[idx][cnt] == node) return ans; mark[idx][cnt] = node; return ans = add(mul(solve(node, idx + 1, cnt), val[szs[node][idx]]), mul(solve(node, idx + 1, min(cnt + 1, 2)), solve2(szs[node][idx]))); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); half = fastpow(2, mod - 2); val[0] = 1; for (int i = 1; i < N; i++) val[i] = mul(val[i - 1], half); cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1); int ans = 0; memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) ans = add(ans, mul(half, solve(i, 0, 0))); cout << ans; return 0; }
replace
66
67
66
67
TLE
p02822
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int inv2 = 500000004; const int MAXN = 200005; int n, ans, pw2[MAXN]; int fir[MAXN], to[MAXN << 1], nxt[MAXN], cnt; inline void link(int u, int v) { to[++cnt] = v; nxt[cnt] = fir[u]; fir[u] = cnt; to[++cnt] = u; nxt[cnt] = fir[v]; fir[v] = cnt; } int sz[MAXN]; void dfs(int u, int ff) { sz[u] = 1; int p0 = 1, p1 = 0, q0, q1, p; for (int i = fir[u], v; i; i = nxt[i]) if ((v = to[i]) != ff) { dfs(v, u); sz[u] += sz[v]; p = pw2[sz[v]]; q0 = 1ll * p0 * p % mod; q1 = (1ll * p1 * p + 1ll * p0 * (mod + 1 - p)) % mod; p0 = q0, p1 = q1; } p = pw2[n - sz[u]]; q0 = 1ll * p0 * p % mod; q1 = (1ll * p1 * p + 1ll * p0 * (mod + 1 - p)) % mod; p0 = q0, p1 = q1; ans = (ans + (1ll + mod + mod - p0 - p1) % mod * inv2 % mod) % mod; } int main() { scanf("%d", &n); pw2[0] = 1; for (int i = 1, x, y; i < n; ++i) scanf("%d%d", &x, &y), link(x, y), pw2[i] = 1ll * pw2[i - 1] * inv2 % mod; pw2[n] = 1ll * pw2[n - 1] * inv2 % mod; dfs(1, 0); printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int inv2 = 500000004; const int MAXN = 200005; int n, ans, pw2[MAXN]; int fir[MAXN], to[MAXN << 1], nxt[MAXN << 1], cnt; inline void link(int u, int v) { to[++cnt] = v; nxt[cnt] = fir[u]; fir[u] = cnt; to[++cnt] = u; nxt[cnt] = fir[v]; fir[v] = cnt; } int sz[MAXN]; void dfs(int u, int ff) { sz[u] = 1; int p0 = 1, p1 = 0, q0, q1, p; for (int i = fir[u], v; i; i = nxt[i]) if ((v = to[i]) != ff) { dfs(v, u); sz[u] += sz[v]; p = pw2[sz[v]]; q0 = 1ll * p0 * p % mod; q1 = (1ll * p1 * p + 1ll * p0 * (mod + 1 - p)) % mod; p0 = q0, p1 = q1; } p = pw2[n - sz[u]]; q0 = 1ll * p0 * p % mod; q1 = (1ll * p1 * p + 1ll * p0 * (mod + 1 - p)) % mod; p0 = q0, p1 = q1; ans = (ans + (1ll + mod + mod - p0 - p1) % mod * inv2 % mod) % mod; } int main() { scanf("%d", &n); pw2[0] = 1; for (int i = 1, x, y; i < n; ++i) scanf("%d%d", &x, &y), link(x, y), pw2[i] = 1ll * pw2[i - 1] * inv2 % mod; pw2[n] = 1ll * pw2[n - 1] * inv2 % mod; dfs(1, 0); printf("%d\n", ans); }
replace
6
7
6
7
0
p02822
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define HALF ((MOD + 1) / 2) int n; vector<int> v[100005]; int dp[100005]; long long ans; long long po(long long b, int p) { long long re = 1; while (p) { if (p % 2) re = re * b % MOD; b = b * b % MOD; p /= 2; } return re; } void dfs(int a, int b) { dp[a] = 1; vector<int> tv; for (int i : v[a]) if (i != b) { dfs(i, a); dp[a] += dp[i]; tv.push_back(dp[i]); } tv.push_back(n - dp[a]); long long tmp = 1; tmp = (tmp + MOD - po(HALF, n - 1)) % MOD; for (int i : tv) tmp = (tmp + MOD - po(HALF, n - 1 - i) * (1 + MOD - po(HALF, i)) % MOD) % MOD; ans = (ans + HALF * tmp) % MOD; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int ta, tb; scanf("%d%d", &ta, &tb); v[ta].push_back(tb); v[tb].push_back(ta); } dfs(1, 0); printf("%lld\n", ans); }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define HALF ((MOD + 1) / 2) int n; vector<int> v[200005]; int dp[200005]; long long ans; long long po(long long b, int p) { long long re = 1; while (p) { if (p % 2) re = re * b % MOD; b = b * b % MOD; p /= 2; } return re; } void dfs(int a, int b) { dp[a] = 1; vector<int> tv; for (int i : v[a]) if (i != b) { dfs(i, a); dp[a] += dp[i]; tv.push_back(dp[i]); } tv.push_back(n - dp[a]); long long tmp = 1; tmp = (tmp + MOD - po(HALF, n - 1)) % MOD; for (int i : tv) tmp = (tmp + MOD - po(HALF, n - 1 - i) * (1 + MOD - po(HALF, i)) % MOD) % MOD; ans = (ans + HALF * tmp) % MOD; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int ta, tb; scanf("%d%d", &ta, &tb); v[ta].push_back(tb); v[tb].push_back(ta); } dfs(1, 0); printf("%lld\n", ans); }
replace
8
10
8
10
0
p02822
C++
Runtime Error
// // Created by 陈基彤 on 2020-01-09. // #include <bits/stdc++.h> using namespace std; #define LL long long const int maxn = 1e5 + 5; const LL mod = 1e9 + 7; vector<int> edge[maxn]; int n; LL sum[maxn], num[maxn]; LL tt; void dfs(int x, int fa) { LL nows = 0, nown = 0, tot = 0; for (auto i : edge[x]) if (i != fa) { dfs(i, x); tot = (tot + nown * num[i] % mod + nown * sum[i] % mod + num[i] * nows % mod) % mod; nows = (nown * sum[i] % mod + num[i] * nows % mod + sum[i] + nows) % mod; nown = (nown * num[i] % mod + num[i] + nown) % mod; } num[x] = (nown * 2 + 1) % mod; sum[x] = (nows * 2 + nown) % mod; tt = (tt + tot + nows) % mod; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; LL inv2 = (mod + 1) / 2; // printf("inv2 is %lld\n",inv2); for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; edge[a].push_back(b); edge[b].push_back(a); } dfs(1, 0); // for(int i=1;i<=n;i++) // printf("%lld %lld %lld\n",i,num[i],sum[i]); tt = (tt % mod + mod) % mod; // printf("%lld\n",tt); LL dv = 1; for (int i = 1; i <= n; i++) dv = (dv * inv2) % mod; cout << tt * dv % mod << endl; return 0; } /* 5 1 2 1 5 2 3 2 4 */
// // Created by 陈基彤 on 2020-01-09. // #include <bits/stdc++.h> using namespace std; #define LL long long const int maxn = 2e5 + 5; const LL mod = 1e9 + 7; vector<int> edge[maxn]; int n; LL sum[maxn], num[maxn]; LL tt; void dfs(int x, int fa) { LL nows = 0, nown = 0, tot = 0; for (auto i : edge[x]) if (i != fa) { dfs(i, x); tot = (tot + nown * num[i] % mod + nown * sum[i] % mod + num[i] * nows % mod) % mod; nows = (nown * sum[i] % mod + num[i] * nows % mod + sum[i] + nows) % mod; nown = (nown * num[i] % mod + num[i] + nown) % mod; } num[x] = (nown * 2 + 1) % mod; sum[x] = (nows * 2 + nown) % mod; tt = (tt + tot + nows) % mod; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; LL inv2 = (mod + 1) / 2; // printf("inv2 is %lld\n",inv2); for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; edge[a].push_back(b); edge[b].push_back(a); } dfs(1, 0); // for(int i=1;i<=n;i++) // printf("%lld %lld %lld\n",i,num[i],sum[i]); tt = (tt % mod + mod) % mod; // printf("%lld\n",tt); LL dv = 1; for (int i = 1; i <= n; i++) dv = (dv * inv2) % mod; cout << tt * dv % mod << endl; return 0; } /* 5 1 2 1 5 2 3 2 4 */
replace
6
7
6
7
0
p02822
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define f first #define s second #define pb push_back #define reset(a, b) memset(a, b, sizeof a); #define ALL(x) x.begin(), x.end() template <typename T> void cetak(T t) { cout << t << ')' << endl; } template <typename T, typename... V> void cetak(T t, V... v) { cout << t; if (sizeof...(v)) cout << ", "; cetak(v...); } #define debug(x...) \ cout << '(' << #x << ") = ("; \ cetak(x) #define sz(x) (int)(x.size()) #define lt(x) (int)(x.length()) const int MOD = 1e9 + 7; int add(int x, int y) { int ret = x + y; if (ret >= MOD) ret -= MOD; return ret; } int sub(int x, int y) { int ret = x - y; if (ret < 0) ret += MOD; return ret; } int mul(int x, int y) { return (long long)x * y % MOD; } const int mx = 1e5 + 10; int n, sz[mx]; vector<int> g[mx]; int POW(int a, int b) { int ret = 1; while (b > 0) { if (b & 1) ret = mul(ret, a); b >>= 1; a = mul(a, a); } return ret; } int ans; void dfs(int now, int par) { sz[now] = 1; for (int i : g[now]) { if (i != par) { dfs(i, now); sz[now] += sz[i]; } } vector<int> subtree; int gak_masuk = 1; // semua nya putih if (n - sz[now] > 0) subtree.pb(n - sz[now]); for (int i : g[now]) { if (i != par) subtree.pb(sz[i]); } for (int i : subtree) { int hitam = sub(POW(2, i), 1); gak_masuk = add(gak_masuk, hitam); } int total = POW(2, n - 1); total = sub(total, gak_masuk); ans = add(ans, total); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } dfs(1, 0); ans = mul(ans, POW(POW(2, n), MOD - 2)); cout << ans << endl; }
#include "bits/stdc++.h" using namespace std; #define f first #define s second #define pb push_back #define reset(a, b) memset(a, b, sizeof a); #define ALL(x) x.begin(), x.end() template <typename T> void cetak(T t) { cout << t << ')' << endl; } template <typename T, typename... V> void cetak(T t, V... v) { cout << t; if (sizeof...(v)) cout << ", "; cetak(v...); } #define debug(x...) \ cout << '(' << #x << ") = ("; \ cetak(x) #define sz(x) (int)(x.size()) #define lt(x) (int)(x.length()) const int MOD = 1e9 + 7; int add(int x, int y) { int ret = x + y; if (ret >= MOD) ret -= MOD; return ret; } int sub(int x, int y) { int ret = x - y; if (ret < 0) ret += MOD; return ret; } int mul(int x, int y) { return (long long)x * y % MOD; } const int mx = 2e5 + 10; int n, sz[mx]; vector<int> g[mx]; int POW(int a, int b) { int ret = 1; while (b > 0) { if (b & 1) ret = mul(ret, a); b >>= 1; a = mul(a, a); } return ret; } int ans; void dfs(int now, int par) { sz[now] = 1; for (int i : g[now]) { if (i != par) { dfs(i, now); sz[now] += sz[i]; } } vector<int> subtree; int gak_masuk = 1; // semua nya putih if (n - sz[now] > 0) subtree.pb(n - sz[now]); for (int i : g[now]) { if (i != par) subtree.pb(sz[i]); } for (int i : subtree) { int hitam = sub(POW(2, i), 1); gak_masuk = add(gak_masuk, hitam); } int total = POW(2, n - 1); total = sub(total, gak_masuk); ans = add(ans, total); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } dfs(1, 0); ans = mul(ans, POW(POW(2, n), MOD - 2)); cout << ans << endl; }
replace
38
39
38
39
0
p02822
C++
Runtime Error
//----------------------------templates #pragma GCC optimize("Ofast") #pragma GCC target("tune=native") #pragma GCC target("avx") //---------------------------- #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int ll #define FOR(i, j, n) for (int i = (int)(j); i < (n); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPS(i, n) for (int i = 1; i <= (int)(n); i++) #define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REPNS(i, n) for (int i = (int)(n); i > 0; i--) #define I(n) scanf("%lld", &(n)) #define LL(n) scanf("%lld", &(n)) #define pb(n) push_back((n)) #define mp(i, j) make_pair((i), (j)) #define eb(i, j) emplace_back((i), (j)) #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; typedef vector<vvi> vvvi; const int mod = 1000000007; //-------------------------------------------- ll powll(ll base, ll power) { ll ans = 1; while (power) { if (power & 1) ans = (base * ans) % mod; base = (base * base) % mod; power >>= 1; } return ans; } int n, a, b, ret; vvi children; bool visited[200010]; int f(int x) { visited[x] = true; if (sz(children[x]) == 1) return 1; vi nodes; for (auto v : children[x]) { if (visited[v]) continue; nodes.pb(f(v)); } int subtree = accumulate(all(nodes), 0LL) + 1; if (n - subtree) nodes.pb(n - subtree); // ^ -x = ^ mod-1-x int zerotree = powll(2, mod - n); int onetree = 0; for (auto nd : nodes) { int lit = 1 - powll(2, mod - 1 - nd) + mod; int dim = powll(2, mod - (n - nd)); onetree = (onetree + lit * dim) % mod; } ret = (ret + (1 - zerotree - onetree + 2 * mod) * powll(2, mod - 2)) % mod; return subtree; } signed main() { I(n); children.resize(n); REP(i, n - 1) { I(a); I(b); a--; b--; children[a].pb(b); children[b].pb(a); } int st = 0; while (sz(children[st]) == 1) st++; cerr << st << endl; f(st); cout << ret << endl; }
//----------------------------templates #pragma GCC optimize("Ofast") #pragma GCC target("tune=native") #pragma GCC target("avx") //---------------------------- #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int ll #define FOR(i, j, n) for (int i = (int)(j); i < (n); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPS(i, n) for (int i = 1; i <= (int)(n); i++) #define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REPNS(i, n) for (int i = (int)(n); i > 0; i--) #define I(n) scanf("%lld", &(n)) #define LL(n) scanf("%lld", &(n)) #define pb(n) push_back((n)) #define mp(i, j) make_pair((i), (j)) #define eb(i, j) emplace_back((i), (j)) #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; typedef vector<vvi> vvvi; const int mod = 1000000007; //-------------------------------------------- ll powll(ll base, ll power) { ll ans = 1; while (power) { if (power & 1) ans = (base * ans) % mod; base = (base * base) % mod; power >>= 1; } return ans; } int n, a, b, ret; vvi children; bool visited[200010]; int f(int x) { visited[x] = true; if (sz(children[x]) == 1) return 1; vi nodes; for (auto v : children[x]) { if (visited[v]) continue; nodes.pb(f(v)); } int subtree = accumulate(all(nodes), 0LL) + 1; if (n - subtree) nodes.pb(n - subtree); // ^ -x = ^ mod-1-x int zerotree = powll(2, mod - n); int onetree = 0; for (auto nd : nodes) { int lit = 1 - powll(2, mod - 1 - nd) + mod; int dim = powll(2, mod - (n - nd)); onetree = (onetree + lit * dim) % mod; } ret = (ret + (1 - zerotree - onetree + 2 * mod) * powll(2, mod - 2)) % mod; return subtree; } signed main() { I(n); if (n == 2) { cout << 0 << endl; return 0; } children.resize(n); REP(i, n - 1) { I(a); I(b); a--; b--; children[a].pb(b); children[b].pb(a); } int st = 0; while (sz(children[st]) == 1) st++; cerr << st << endl; f(st); cout << ret << endl; }
insert
87
87
87
91
0
1.0
p02822
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <memory> #include <numeric> #include <queue> #include <random> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> constexpr long long int MOD = 1000000007LL; long long int power(const long long int base, const int exp) { switch (exp) { case 0: return 1LL; case 1: return base % MOD; default: return power(base * base % MOD, exp / 2) * power(base, exp % 2) % MOD; } } long long int inverse(const long long int base) { return power(base, MOD - 2); } long long int power_by_size(const int size) { static std::vector<long long int> memo(200001, -1); if (memo[size] >= 0) return memo[size]; return memo[size] = power(2, size); } long long int inverse_by_size(const int size) { static std::vector<long long int> memo(200001, -1); if (memo[size] >= 0) return memo[size]; return memo[size] = inverse(power(2, size)); } void move_root(int from, int to, std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { size[from] = 1; for (const auto c : nodes[from]) if (c != to) { size[from] += size[c]; } size[to] = 1; for (const auto c : nodes[to]) { size[to] += size[c]; } } long long int prob(int root, const std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { long long int not_hole = inverse_by_size(size.size() - 1); for (const auto child : nodes[root]) { not_hole += inverse_by_size(size.size() - 1 - size[child]) * (1 - inverse_by_size(size[child]) + MOD) % MOD; not_hole %= MOD; } return (1 - not_hole + MOD) * inverse_by_size(1) % MOD; } long long int cal_estimate(int root, int prev, std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { long long int result = prob(root, size, nodes); for (const auto next : nodes[root]) if (next != prev) { move_root(root, next, size, nodes); result += cal_estimate(next, root, size, nodes); result %= MOD; move_root(next, root, size, nodes); } return result; } int main() { int n; std::cin >> n; std::vector<std::vector<int>> nodes(n); std::vector<int> size(n, 0), prev(n, -1); for (auto i = 1; i < n; ++i) { int a, b; std::cin >> a >> b; --a; --b; nodes[a].push_back(b); nodes[b].push_back(a); } std::stack<int> stack; stack.push(0); while (!stack.empty()) { auto top = stack.top(); stack.pop(); if (top >= 0) { stack.push(-top - 1); size[top] = 1; for (const auto next : nodes[top]) if (size[next] == 0) { prev[next] = top; stack.push(next); } } else { if (prev[-top - 1] >= 0) { size[prev[-top - 1]] += size[-top - 1]; } } } std::cout << cal_estimate(0, -1, size, nodes) << std::endl; }
#include <algorithm> #include <array> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <memory> #include <numeric> #include <queue> #include <random> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> constexpr long long int MOD = 1000000007LL; long long int power(const long long int base, const int exp) { switch (exp) { case 0: return 1LL; case 1: return base % MOD; default: return power(base * base % MOD, exp / 2) * power(base, exp % 2) % MOD; } } long long int inverse(const long long int base) { return power(base, MOD - 2); } long long int power_by_size(const int size) { static std::vector<long long int> memo(200001, -1); if (memo[size] >= 0) return memo[size]; return memo[size] = power(2, size); } long long int inverse_by_size(const int size) { static std::vector<long long int> memo(200001, -1); if (memo[size] >= 0) return memo[size]; return memo[size] = inverse(power(2, size)); } void move_root(int from, int to, std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { int root_size = size[from]; int child_size = size[to]; size[from] -= child_size; size[to] = root_size; } long long int prob(int root, const std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { long long int not_hole = inverse_by_size(size.size() - 1); for (const auto child : nodes[root]) { not_hole += inverse_by_size(size.size() - 1 - size[child]) * (1 - inverse_by_size(size[child]) + MOD) % MOD; not_hole %= MOD; } return (1 - not_hole + MOD) * inverse_by_size(1) % MOD; } long long int cal_estimate(int root, int prev, std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { long long int result = prob(root, size, nodes); for (const auto next : nodes[root]) if (next != prev) { move_root(root, next, size, nodes); result += cal_estimate(next, root, size, nodes); result %= MOD; move_root(next, root, size, nodes); } return result; } int main() { int n; std::cin >> n; std::vector<std::vector<int>> nodes(n); std::vector<int> size(n, 0), prev(n, -1); for (auto i = 1; i < n; ++i) { int a, b; std::cin >> a >> b; --a; --b; nodes[a].push_back(b); nodes[b].push_back(a); } std::stack<int> stack; stack.push(0); while (!stack.empty()) { auto top = stack.top(); stack.pop(); if (top >= 0) { stack.push(-top - 1); size[top] = 1; for (const auto next : nodes[top]) if (size[next] == 0) { prev[next] = top; stack.push(next); } } else { if (prev[-top - 1] >= 0) { size[prev[-top - 1]] += size[-top - 1]; } } } std::cout << cal_estimate(0, -1, size, nodes) << std::endl; }
replace
48
57
48
52
TLE
p02822
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <memory> #include <numeric> #include <queue> #include <random> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> constexpr long long int MOD = 1000000007LL; long long int power(const long long int base, const int exp) { switch (exp) { case 0: return 1LL; case 1: return base % MOD; default: return power(base * base % MOD, exp / 2) * power(base, exp % 2) % MOD; } } long long int inverse(const long long int base) { return power(base, MOD - 2); } void move_root(int from, int to, std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { size[from] = 1; for (const auto c : nodes[from]) if (c != to) { size[from] += size[c]; } size[to] = 1; for (const auto c : nodes[to]) { size[to] += size[c]; } } long long int prob(int root, const std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { long long int not_hole = inverse(power(2, size.size() - 1)); for (const auto child : nodes[root]) { not_hole += inverse(power(2, size.size() - 1 - size[child])) * (1 - inverse(power(2, size[child])) + MOD) % MOD; not_hole %= MOD; } return (1 - not_hole + MOD) * inverse(2) % MOD; } long long int cal_estimate(int root, int prev, std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { long long int result = prob(root, size, nodes); for (const auto next : nodes[root]) if (next != prev) { move_root(root, next, size, nodes); result += cal_estimate(next, root, size, nodes); result %= MOD; move_root(next, root, size, nodes); } return result; } int main() { int n; std::cin >> n; std::vector<std::vector<int>> nodes(n); std::vector<int> size(n, 0), prev(n, -1); for (auto i = 1; i < n; ++i) { int a, b; std::cin >> a >> b; --a; --b; nodes[a].push_back(b); nodes[b].push_back(a); } std::stack<int> stack; stack.push(0); while (!stack.empty()) { auto top = stack.top(); stack.pop(); if (top >= 0) { stack.push(-top - 1); size[top] = 1; for (const auto next : nodes[top]) if (size[next] == 0) { prev[next] = top; stack.push(next); } } else { if (prev[-top - 1] >= 0) { size[prev[-top - 1]] += size[-top - 1]; } } } std::cout << cal_estimate(0, -1, size, nodes) << std::endl; }
#include <algorithm> #include <array> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <memory> #include <numeric> #include <queue> #include <random> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> constexpr long long int MOD = 1000000007LL; long long int power(const long long int base, const int exp) { switch (exp) { case 0: return 1LL; case 1: return base % MOD; default: return power(base * base % MOD, exp / 2) * power(base, exp % 2) % MOD; } } long long int inverse(const long long int base) { return power(base, MOD - 2); } void move_root(int from, int to, std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { int root_size = size[from]; int child_size = size[to]; size[from] -= child_size; size[to] = root_size; } long long int prob(int root, const std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { long long int not_hole = inverse(power(2, size.size() - 1)); for (const auto child : nodes[root]) { not_hole += inverse(power(2, size.size() - 1 - size[child])) * (1 - inverse(power(2, size[child])) + MOD) % MOD; not_hole %= MOD; } return (1 - not_hole + MOD) * inverse(2) % MOD; } long long int cal_estimate(int root, int prev, std::vector<int> &size, const std::vector<std::vector<int>> &nodes) { long long int result = prob(root, size, nodes); for (const auto next : nodes[root]) if (next != prev) { move_root(root, next, size, nodes); result += cal_estimate(next, root, size, nodes); result %= MOD; move_root(next, root, size, nodes); } return result; } int main() { int n; std::cin >> n; std::vector<std::vector<int>> nodes(n); std::vector<int> size(n, 0), prev(n, -1); for (auto i = 1; i < n; ++i) { int a, b; std::cin >> a >> b; --a; --b; nodes[a].push_back(b); nodes[b].push_back(a); } std::stack<int> stack; stack.push(0); while (!stack.empty()) { auto top = stack.top(); stack.pop(); if (top >= 0) { stack.push(-top - 1); size[top] = 1; for (const auto next : nodes[top]) if (size[next] == 0) { prev[next] = top; stack.push(next); } } else { if (prev[-top - 1] >= 0) { size[prev[-top - 1]] += size[-top - 1]; } } } std::cout << cal_estimate(0, -1, size, nodes) << std::endl; }
replace
35
44
35
39
TLE
p02822
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i)) using ll = long long; using P = pair<int, int>; using namespace std; const ll mod = 1e9 + 7; ll mod_pow(ll x, ll n) { ll ret = 1; while (n) { if (n & 1) { ret = ret * x % mod; x = x * x % mod; n >>= 1; } else { x = x * x % mod; n >>= 1; } } return ret; } int n; vector<int> g[101010]; ll ans = 0; ll dfs(int v, int p) { vector<int> child; int sz = 0; for (auto &u : g[v]) { if (u == p) continue; child.push_back(dfs(u, v)); sz += child[child.size() - 1]; } child.push_back(n - 1 - sz); ll prob = mod_pow(mod_pow(2, n), mod - 2); for (auto &c : child) { prob += mod_pow(mod_pow(2, n - c), mod - 2) * (1 - mod_pow(mod_pow(2, c), mod - 2)) % mod; } ans += (mod_pow(2, mod - 2) - prob) % mod; ans %= mod; return sz + 1; } int main() { cin >> n; rep(i, n - 1) { int x, y; cin >> x >> y; x--; y--; g[x].push_back(y); g[y].push_back(x); } dfs(0, -1); 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 P = pair<int, int>; using namespace std; const ll mod = 1e9 + 7; ll mod_pow(ll x, ll n) { ll ret = 1; while (n) { if (n & 1) { ret = ret * x % mod; x = x * x % mod; n >>= 1; } else { x = x * x % mod; n >>= 1; } } return ret; } int n; vector<int> g[201010]; ll ans = 0; ll dfs(int v, int p) { vector<int> child; int sz = 0; for (auto &u : g[v]) { if (u == p) continue; child.push_back(dfs(u, v)); sz += child[child.size() - 1]; } child.push_back(n - 1 - sz); ll prob = mod_pow(mod_pow(2, n), mod - 2); for (auto &c : child) { prob += mod_pow(mod_pow(2, n - c), mod - 2) * (1 - mod_pow(mod_pow(2, c), mod - 2)) % mod; } ans += (mod_pow(2, mod - 2) - prob) % mod; ans %= mod; return sz + 1; } int main() { cin >> n; rep(i, n - 1) { int x, y; cin >> x >> y; x--; y--; g[x].push_back(y); g[y].push_back(x); } dfs(0, -1); cout << ans << endl; }
replace
25
26
25
26
0
p02822
C++
Time Limit Exceeded
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; const ll MOD = 1e9 + 7; const int MAX_N = 200000; map<pair<int, int>, int> mp; // mp[make_pair(i, j)] iから見てjの先にあるnodeの数 vector<int> edge[MAX_N]; int N; ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } int nodes(int i, int j) { pair<int, int> k = make_pair(i, j); if (mp.find(k) != mp.end()) return mp[k]; if (edge[j].size() == 1) { mp[k] = 1; mp[make_pair(j, i)] = N - 1; return 1; } int ret = 1; rep(l, edge[j].size()) { if (edge[j][l] == i) continue; ret += nodes(j, edge[j][l]); } mp[k] = ret; return ret; } int main() { int A, B; cin >> N; rep(i, N - 1) { cin >> A >> B; A--; B--; edge[A].push_back(B); edge[B].push_back(A); } /* rep(i, N) { rep(j, edge[i].size()) { cout << "nodes[" << i << "," << edge[i][j] << "]=" << nodes(i, edge[i][j]) << endl; } } */ ll ans = 0; rep(i, N) { ans = (ans + mod_pow(2, N - 1, MOD) - 1) % MOD; rep(j, edge[i].size()) { ans = (ans - mod_pow(2, nodes(i, edge[i][j]), MOD) + 1 + MOD) % MOD; } } cout << (ans * mod_pow(mod_pow(2, N, MOD), MOD - 2, MOD)) % MOD << "\n"; }
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; const ll MOD = 1e9 + 7; const int MAX_N = 200000; map<pair<int, int>, int> mp; // mp[make_pair(i, j)] iから見てjの先にあるnodeの数 vector<int> edge[MAX_N]; int N; ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } int nodes(int i, int j) { pair<int, int> k = make_pair(i, j); if (mp.find(k) != mp.end()) return mp[k]; if (edge[j].size() == 1) { mp[k] = 1; mp[make_pair(j, i)] = N - 1; return 1; } int ret = 1; rep(l, edge[j].size()) { if (edge[j][l] == i) continue; ret += nodes(j, edge[j][l]); } mp[k] = ret; mp[make_pair(j, i)] = N - ret; return ret; } int main() { int A, B; cin >> N; rep(i, N - 1) { cin >> A >> B; A--; B--; edge[A].push_back(B); edge[B].push_back(A); } /* rep(i, N) { rep(j, edge[i].size()) { cout << "nodes[" << i << "," << edge[i][j] << "]=" << nodes(i, edge[i][j]) << endl; } } */ ll ans = 0; rep(i, N) { ans = (ans + mod_pow(2, N - 1, MOD) - 1) % MOD; rep(j, edge[i].size()) { ans = (ans - mod_pow(2, nodes(i, edge[i][j]), MOD) + 1 + MOD) % MOD; } } cout << (ans * mod_pow(mod_pow(2, N, MOD), MOD - 2, MOD)) % MOD << "\n"; }
insert
40
40
40
41
TLE
p02822
C++
Time Limit Exceeded
// https://atcoder.jp/contests/abc149/tasks/abc149_f #include "algorithm" #include "bitset" #include "cmath" #include "functional" #include "iomanip" #include "iostream" #include "map" #include "numeric" #include "queue" #include "set" #include "string" #include "vector" #define rep(i, to) for (ll i = 0; i < (to); ++i) #define rep1(i, to) for (ll i = 1; i <= (to); ++i) #define repf(i, from, to) for (ll i = from; i < (to); ++i) #define repr(i, from) for (ll i = from - 1; i >= 0; --i) #define all(vec) vec.begin(), vec.end() #define unless(cond) if (!(cond)) #define fi first #define se second using namespace std; typedef long long ll; template <typename T> using V = vector<T>; using VL = V<ll>; using VVL = V<VL>; template <typename T, typename U> using P = pair<T, U>; using PL = P<ll, ll>; using VPL = V<PL>; template <typename T> inline bool chmax(T &a, T b); template <typename T> inline bool chmin(T &a, T b); void print_ints(vector<ll> v); template <typename T> void drop(T a); const ll INF = 1e18; template <int MOD> class Fp { public: long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += 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; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += 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; 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 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; } }; // const ll MOD = 998244353; const ll MOD = 1000000007; using mint = Fp<MOD>; ll N; VVL edge_list; V<map<ll, ll>> ct_memo; // u 以降の点の数, p を含まない ll get_node_ct(ll u, ll p) { if (ct_memo[p].count(u) > 0) return ct_memo[p][u]; ll res = 1; for (auto v : edge_list[u]) { if (v == p) continue; res += get_node_ct(v, u); } ct_memo[p][u] = res; return res; } void solve() { cin >> N; edge_list = VVL(N, VL(0)); ct_memo = V<map<ll, ll>>(N, map<ll, ll>()); rep(i, N - 1) { ll a, b; cin >> a >> b; a--, b--; edge_list[a].push_back(b); edge_list[b].push_back(a); } // 全 S の頂点数の期待値 - 全木の黒頂点の数の期待値 // 全 S の頂点数の期待値 = 全 S の辺の数の期待値 + 1 // 各辺が S の頂点数に寄与する期待値 = S の頂点に入る確率 を合計すれば良い mint edge_ct_e = 0; rep(u, N) { for (ll v : edge_list[u]) { unless(u < v) continue; ll ct_u = get_node_ct(u, v); ll ct_v = N - ct_u; // この辺が S に含まれる確率 = u 側, v 側に一つでも黒がある確率 mint r_for_edge = (mint(1) - mint(1) / modpow(mint(2), ct_u)) * (mint(1) - mint(1) / modpow(mint(2), ct_v)); edge_ct_e += r_for_edge; } } mint empty_prob = mint(1) / modpow(mint(2), N); mint node_ct_e = edge_ct_e + 1; node_ct_e -= empty_prob; // 空グラフの場合の 1 が加算されている mint res = node_ct_e; // 全木の黒頂点の数の期待値 res -= mint(N) / 2; cout << res << endl; } struct exit_exception : public std::exception { const char *what() const throw() { return "Exited"; } }; #ifndef TEST int main() { cin.tie(0); ios::sync_with_stdio(false); try { solve(); } catch (exit_exception &e) { } return 0; } #endif template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void print_ints(vector<ll> v) { rep(i, v.size()) { if (i > 0) { cout << " "; } cout << v[i]; } cout << endl; } template <typename T> void drop(T res) { cout << res << endl; throw exit_exception(); }
// https://atcoder.jp/contests/abc149/tasks/abc149_f #include "algorithm" #include "bitset" #include "cmath" #include "functional" #include "iomanip" #include "iostream" #include "map" #include "numeric" #include "queue" #include "set" #include "string" #include "vector" #define rep(i, to) for (ll i = 0; i < (to); ++i) #define rep1(i, to) for (ll i = 1; i <= (to); ++i) #define repf(i, from, to) for (ll i = from; i < (to); ++i) #define repr(i, from) for (ll i = from - 1; i >= 0; --i) #define all(vec) vec.begin(), vec.end() #define unless(cond) if (!(cond)) #define fi first #define se second using namespace std; typedef long long ll; template <typename T> using V = vector<T>; using VL = V<ll>; using VVL = V<VL>; template <typename T, typename U> using P = pair<T, U>; using PL = P<ll, ll>; using VPL = V<PL>; template <typename T> inline bool chmax(T &a, T b); template <typename T> inline bool chmin(T &a, T b); void print_ints(vector<ll> v); template <typename T> void drop(T a); const ll INF = 1e18; template <int MOD> class Fp { public: long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += 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; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += 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; 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 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; } }; // const ll MOD = 998244353; const ll MOD = 1000000007; using mint = Fp<MOD>; ll N; VVL edge_list; V<map<ll, ll>> ct_memo; // u 以降の点の数, p を含まない ll get_node_ct(ll u, ll p) { if (ct_memo[p].count(u) > 0) return ct_memo[p][u]; if (ct_memo[u].count(p) > 0) return N - ct_memo[u][p]; ll res = 1; for (auto v : edge_list[u]) { if (v == p) continue; res += get_node_ct(v, u); } ct_memo[p][u] = res; return res; } void solve() { cin >> N; edge_list = VVL(N, VL(0)); ct_memo = V<map<ll, ll>>(N, map<ll, ll>()); rep(i, N - 1) { ll a, b; cin >> a >> b; a--, b--; edge_list[a].push_back(b); edge_list[b].push_back(a); } // 全 S の頂点数の期待値 - 全木の黒頂点の数の期待値 // 全 S の頂点数の期待値 = 全 S の辺の数の期待値 + 1 // 各辺が S の頂点数に寄与する期待値 = S の頂点に入る確率 を合計すれば良い mint edge_ct_e = 0; rep(u, N) { for (ll v : edge_list[u]) { unless(u < v) continue; ll ct_u = get_node_ct(u, v); ll ct_v = N - ct_u; // この辺が S に含まれる確率 = u 側, v 側に一つでも黒がある確率 mint r_for_edge = (mint(1) - mint(1) / modpow(mint(2), ct_u)) * (mint(1) - mint(1) / modpow(mint(2), ct_v)); edge_ct_e += r_for_edge; } } mint empty_prob = mint(1) / modpow(mint(2), N); mint node_ct_e = edge_ct_e + 1; node_ct_e -= empty_prob; // 空グラフの場合の 1 が加算されている mint res = node_ct_e; // 全木の黒頂点の数の期待値 res -= mint(N) / 2; cout << res << endl; } struct exit_exception : public std::exception { const char *what() const throw() { return "Exited"; } }; #ifndef TEST int main() { cin.tie(0); ios::sync_with_stdio(false); try { solve(); } catch (exit_exception &e) { } return 0; } #endif template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void print_ints(vector<ll> v) { rep(i, v.size()) { if (i > 0) { cout << " "; } cout << v[i]; } cout << endl; } template <typename T> void drop(T res) { cout << res << endl; throw exit_exception(); }
insert
112
112
112
114
TLE
p02822
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cassert> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef long double ld; using edge = struct { ll to; ll cost; }; struct point { ll x; ll y; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } }; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::pair<ll, pl> pl3; typedef std::map<string, ll> msl; typedef std::map<char, ll> mcl; typedef std::map<ll, ll> mll; typedef std::vector<ll> vl; typedef std::vector<pl> vpl; typedef std::vector<point> points; typedef std::vector<pl3> vpl3; typedef std::priority_queue<ll> pq; typedef std::priority_queue<ll, vl, greater<ll>> pql; // priority queue taking from the lower value. typedef std::vector<edge> Graph; const ll N_DIGITS = 60; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-9; const long double PI = 3.14159265358979323846; vpl dirs = { {-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0} // self }; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define pr(a) cout << (a) #define prl(a) cout << (a) << endl #define prl2(a, b) cout << (a) << " " << (b) << endl #define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) cout << (a) << " " #define prs2(a, b) cout << (a) << " " << (b) << " " #define prs3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define e1 first #define e2 second #define ctol(c) ll((c)) - ll('0') #define ltos(n) to_string((n)) #define items(kv, v) for (auto &(kv) : (v)) #define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)])) #define rsort(a, n) sort(a, a + n, greater<>()) #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ cout << fixed << setprecision(digits) << (num) << endl; #define XOR(a, b) (a) ^ (b) #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #define DEBUG(x) cerr << #x << ": " << (x) << endl #define DEBUG_VEC(v) \ cerr << #v << ": "; \ rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \ cerr << endl #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; class UnionFind { vl parents, set_size; public: UnionFind() {} UnionFind(ll n) { parents = set_size = vl(n); rep(i, n) { parents[i] = i; set_size[i] = 1LL; } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } }; struct Doubling { // ABC167D ll n; ll sz; vector<vl> next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly input a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; /* class LCA{ public: ll N, logN; vl depth, len; Graph tree[200005]; // global declaration later. vector<vl> parents; LCA(ll n){ N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vector<vl>(logN, vl(N)); init(0, -1, 0, 0); build(); } void init(ll source, ll parent, ll d, ll l){ depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()){ ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build(){ rep(k, logN - 1) rep(n, N){ // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v){ if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN){ if (parents[k][u] != parents[k][v]){ u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v){ ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; */ struct BIT { ll n; vl dat; BIT(ll n, ll ini = 0) : dat(n + 1, ini), n(n){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, dat); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { return query(r) - query(l); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vector<bool> visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; struct StronglyConnectedComponents { ll n; vector<vl> graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return num; } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_mod(ll a, ll power, ll mod) { ll value = 1; while (power != 0) { if (power & 1) value = (value * a) % mod; a = (a * a) % mod; power = power >> 1; } return value % mod; } ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } ll comb_memo[55][55]; ll pascal_triangle(ll n) { comb_memo[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb_memo[i][j] += comb_memo[i - 1][j]; if (j > 0) comb_memo[i][j] += comb_memo[i - 1][j - 1]; } } ll combination(ll n, ll r, ll mod) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { ll num = (n - i) % mod, den = (i + 1) % mod; (numerator *= num) %= mod; (denomenator *= modinv(den, mod)) %= mod; } return (numerator * denomenator) % mod; } ll combination_no_mod(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { numerator *= n - i; denomenator *= i + 1; ll g = gcd(numerator, denomenator); numerator /= g; denomenator /= g; } return numerator; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } ll bin_search(ll key, ll A[], ll left, ll right) { // return the index idx where A[idx] = key. // A[left] is start and A[right] is end.. // In other words, A[right], not A[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (A[mid] == key) return mid; else if (A[mid] > key) right = mid - 1; else if (A[mid] < key) left = mid + 1; } return -1; } /* ll bin_search_temp(ll left, ll right, callable judge){ while(right > left){ // when seeking lower bound ll mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; // when seeking upper bound ll mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } return right; } trinary_search // Care the value EPS!!! Compare to the condition ld left = 0; ld right = p; while(abs(right - left) > EPS){ ld left2 = (2 * left + right) / 3.0; ld right2 = (left + 2 * right) / 3.0; ld f1 = tak_func(left2); ld f2 = tak_func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } */ str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } ll lower_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] >= key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] < key && key <= A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] < key && key <= A[mid]) return mid; else if (A[mid - 1] >= key) right = mid - 1; else if (A[mid] < key) left = mid; } return -1; // all the elements < key } ll inf_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] > key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] <= key && key < A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] <= key && key < A[mid]) return mid; else if (A[mid - 1] > key) right = mid - 1; else if (A[mid] <= key) left = mid; } return -1; // all the elements <= key } ll upper_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] <= key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] <= key && key < A[right]) return right - 1; else return -1; } if (A[mid] <= key && key < A[mid + 1]) return mid; else if (A[mid] > key) right = mid; else if (A[mid + 1] <= key) left = mid + 1; } return -1; // all the elements > key } ll sup_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] < key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] < key && key <= A[right]) return right - 1; else return -1; } if (A[mid] < key && key <= A[mid + 1]) return mid; else if (A[mid] >= key) right = mid; else if (A[mid + 1] < key) left = mid + 1; } return -1; // all the elements >= key } ll bin_search_vector(ll key, vl v, ll left, ll right) { // return the index idx where v[idx] = key. // v[left] is start and v[right] is end.. // In other words, v[right], not v[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (v[mid] == key) return mid; else if (v[mid] > key) right = mid - 1; else if (v[mid] < key) left = mid + 1; } return -1; } ll lower_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] < key <= v[N] // N == -1 if all the elements < key return lower_bound(all(v), key) - v.begin(); } ll inf_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] <= key < v[N] <= key + 1 // N == -1 if all the elements <= key return lower_bound(all(v), key + 1) - v.begin(); } ll upper_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key < v[N + 1] // N == -1 if all the elements > key return upper_bound(all(v), key) - v.begin(); // (- 1) } ll sup_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key - 1 < key <= v[N + 1] // N == -1 if all the elements >= key return upper_bound(all(v), key - 1) - v.begin() - 1; } ll fact(ll n) { if (n == 0) return 1; return n * fact(n - 1); } ll fact_mod(ll n, ll mod) { if (n == 0) return 1; return n * fact_mod(n - 1, mod); } /* ll facts[6000], facts_inv[6000], inv[6000]; void fact_memo(ll n, ll mod){ facts[0] = facts[1] = 1; facts_inv[0] = facts_inv[1] = 1; inv[1] = 1; For(i, 2, n + 1){ facts[i] = (i * facts[i - 1]) % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * inv[i] % mod; } } ll nCk(ll n, ll r){ if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % MOD; val *= facts_inv[r]; return val % MOD; } */ bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } ll bool_sum(ll a1, ll a2) { if (a1 == 1 || a2 == 1) return 1; return 0; } mll prime_factorization(ll n) { ll i = 2; mll table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } ll char_to_idx(char c) { ll idx = 0; Forchar(cc, 'a', 'z') { if (c == cc) return idx; else idx++; } } ll next_combination(ll sub) { /* ### Attention ### if k is 0 or 1 and n is 0, it does not work well. ll n, k; ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F vl z_algorithm(str s) { /* Paramters --------- str: the string of interest Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ ll n = s.length(); vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll big_number_mod(str s, ll mod) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth) % mod; val %= mod; tenth *= 10; tenth %= mod; idx++; } return val; } ll big_number_compare(str s1, str s2) { if (s1.length() > s2.length()) return 1; else if (s1.length() < s2.length()) return -1; else if (s1 == s2) return 0; return 2 * (s1 > s2) - 1; } ll string_to_ll(str s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth); tenth *= 10; idx++; } return val; } str reflected_string(str s) { str t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(point l_begin, point l_end, point p) { ll xl1 = l_begin.x, yl1 = l_begin.y; ll xl2 = l_end.x, yl2 = l_end.y; ll xp = p.x, yp = p.y; ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) { ll x1 = l1_begin.x, y1 = l1_begin.y; ll x2 = l1_end.x, y2 = l1_end.y; ll x3 = l2_begin.x, y3 = l2_begin.y; ll x4 = l2_end.x, y4 = l2_end.y; ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } ld space_of_triangle(point p1, point p2, point p3) { ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } pair<point, ll> center_2det_of_3points(point p1, point p2, point p3) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; // center of 2 points ll c2x_2 = x2 + x1, c2y_2 = y2 + y1; ll c3x_2 = x3 + x1, c3y_2 = y3 + y1; // vertical vector of 2 lines ll b2y = x2 - x1, b2x = -y2 + y1; ll b3y = x3 - x1, b3x = -y3 + y1; ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x; ll det = -b3y * b2x + b3x * b2y; if (det == 0) return {{INF, INF}, det}; ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2; return {{cx_2det, cy_2det}, det}; } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BIT bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = rank + 1; j < n; j++) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v, ll n) { vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return find(all(dp), INF) - dp.begin(); } /* diameter of tree Graph tree[MAX_N]; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree const ll N_VERTEX = 310; ll a, b, t; vector<vl> dist; void warshall_floyd(ll n){ // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } int main(void){ in2(n, m); rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(i, m){ in3(a, b, t); a--; b--; dist[a][b] = t; dist[b][a] = t; } warshall_floyd(n); } vl dist, vertex_pre; void dijkstra(ll start, ll n) { priority_queue<pl, vector<pl>, greater<pl>> edge_costs; dist = vl(n, INF); // vertex_pre = vl(n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { pl edge_cost = edge_costs.top(); edge_costs.pop(); ll idx = edge_cost.second; ll cost = edge_cost.first; if (dist[idx] < cost) continue; rep(i, graph[idx].size()){ edge e = graph[idx][i]; if (dist[e.to] > dist[idx] + e.cost){ dist[e.to] = dist[idx] + e.cost; // vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g){ vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } int main(void){ in2(n, m); rep(i, m){ in3(a, b, t); a--; b--; G[a].pb({b, t}); G[b].pb({a, t}); } dijkstra(0, n); } # ABC061D bool find_negative_cycle(ll goal){ rep(i, n) rep(v, n) rep(k, graph[v].size()){ edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost){ dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll n, ll goal){ // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != -1) fill(dist, dist + n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()){ edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(goal)) return false; return true; } */ /* # 1. The usage of map pair map<pl, ll> cnt; cnt[{i, j}] = 0; items(kv, cnt){ prl2(kv.first, kv.second); } # 2. The usage of next_permutation and combination (factorial search) ll a[8]; rep(i, 8) a[i] = i; sort(a, a + 8); do{ }while(next_permutation(a, a+n)); // here, combination ll n, r; ll i1, i2, ..., ir; For(i1, n - r, n) For(i2, n - r - 1, i1) ... For(ir, n - 2 * r + 1, irr){ process;} # 3. bit search ll n; in1(n); const ll base = 3; ll upper = power_normal(base, n); rep(i, upper){ ll tmp = i; rep(j, n){ rep(k, base) if (tmp % base == k) prl(k); tmp /= base; } } # 4. imos method // used when we would like to count the number which // shows how many times the numbers between l and r belongs to smt. // This method is composed of three process. ll n, m, s[MAX_M], l, r; in2(n, m); rep(i, m) s[i] = 0; // 1st step rep(i, n){ in3(l, r, c); l--; r--; // if l starts from 1. s[l] += c; s[r + 1] -= c; } // 2nd step rep(i, m - 1) s[i + 1] += s[i]; // 3rd step: judgement... #5. shakutori method (syakutori, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ r = max(r, l); if (l == r) r++; while(r < n && cond) r++; answer += r - l; l++; } prl(answer); #6. priority queue pq q; ll answer = 0; ll v; rep(i, n) q.push(a[i]); rep(i, m){ v = q.top(); q.pop(); // get the top value and dump the value from queue v /= 2; q.push(v); // add the new value } while(!q.empty()){ answer += q.top(); q.pop(); } #7. The shortest path (distance) between the k-th edge and another edge (Tree) Graph tree[MAX_N]; ll depth[MAX_N]; void dfs(ll source, ll parent, ll all_cost){ depth[source] = all_cost; items(e, tree[source]){ if (e.to == parent) continue; dfs(e.to, source, all_cost + e.cost); } } ll n, k, a, b, c; in2(n, k); rep(i, n - 1){ in3(a, b, c); a--; b--; tree[a].pb({b, c}); tree[b].pb({a, c}); } k--; dfs(k, -1, 0); #10. Visiting Subtree using recurrent function (ABC138D) Graph tree[MAX_N]; ll c[MAX_N]; bool visited[MAX_N]; void dfs(ll source, ll parent, ll val){ visited[source] = true; c[source] += val; rep(i, tree[source].size()){ rep(i, m){ll res = n % match[i].e1;} ll vertex = tree[source][i].to; if (vertex == parent) continue; dfs(vertex, source, c[source]); } } #11. bfs ABC146D, ABC007C 1. first create a tree. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = graph[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = graph[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } #12. grid to distance matrix (dx, dy) ll w, h; ll pos_to_idx(ll x, ll y){ return y * w + x; } pl idx_to_pos(ll idx){ return mp(idx % w, idx / w); } rep(y, h){ in1(s); rep(x, w){ if (s[x] == '#') wall[x][y] = true; else wall[x][y] = false; } } rep(i1, h * w)rep(i2, h * w) dist[i1][i2] = INF * (i1 != i2); rep(x, w)rep(y, h){ ll idx1 = pos_to_idx(x, y); ll idx2; if (wall[x][y]) continue; if (x != 0 && !wall[x - 1][y]){ idx2 = pos_to_idx(x - 1, y); // if warshall floyd dist[idx1][idx2] = 1; // if dijkstra // graph[idx1].pb({idx2, 1}); } if (x != w - 1 && !wall[x + 1][y]){ idx2 = pos_to_idx(x + 1, y); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != 0 && !wall[x][y - 1]){ idx2 = pos_to_idx(x, y - 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != h - 1 && !wall[x][y + 1]){ idx2 = pos_to_idx(x, y + 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } } # Cumulative sum (2 dimension) ll func(ll x, ll y, ll dx, ll dy){ if (x + dx > w || y + dy > h) return - INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h + 1) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vs[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; # DEBUG_MATRIX rep(i, n){ rep(j, n) prs(a[i][j]); prl(""); } */ /* # the operators regarding bit & (AND), | (OR), ^ (XOR) - (REVERSE), >> (SMALLER SHIFT) << (BIGGER SHIFT) x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & - x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 x << y is x * 2 ** y x >> y is rep(i, y) x = x // 2 ####### Attention ####### 1 << i and 1LL << i is different. If programs show WA, try switch to this one. Let S be a bit sequence and i be a non-negative integer S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & -(1 << i) -> S - {i} __builtin_popcount(S) -> the number of elements in S S = 0 -> S is an empty set __builtin_popcountl(i) -> the number of 1 in binary S = (1 << n) - 1 -> S includes all the elements up to the n-th #Conditional Operator condition ? true : false; #iterator type declaration: auto value reference: *itr increment: itr++ decrement: itr-- substitution of value: *itr = smt # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; bool okay = false; ll answer = 0; ll n, m, k; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; ll memo[MAX_N]; void solve() { RerootingTreeDP<Merger> dp(n); rep(i, n - 1) { ll a, b; in2(a, b); a--, b--; dp.add_edge(a, b, 1); } memo[0] = 1; rep(i, n + 3) memo[i + 1] = (memo[i] * 2) % MOD; dp.dp(); rep(i, n) { // dp.tree[i] := the number of children (answer += memo[n - 1] - 1) %= MOD; // all the cases except all of them are white for (auto &&e : dp.tree[i]) // the case where the i-th vertex is not a hole (answer += MOD - (memo[e.value] - 1)) %= MOD; } ll x_inv = modinv(power_mod(2, n, MOD), MOD); ll y = answer; prl((y * x_inv) % MOD); } int main(void) { in1(n); assert(n <= 9); solve(); return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cassert> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef long double ld; using edge = struct { ll to; ll cost; }; struct point { ll x; ll y; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } }; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::pair<ll, pl> pl3; typedef std::map<string, ll> msl; typedef std::map<char, ll> mcl; typedef std::map<ll, ll> mll; typedef std::vector<ll> vl; typedef std::vector<pl> vpl; typedef std::vector<point> points; typedef std::vector<pl3> vpl3; typedef std::priority_queue<ll> pq; typedef std::priority_queue<ll, vl, greater<ll>> pql; // priority queue taking from the lower value. typedef std::vector<edge> Graph; const ll N_DIGITS = 60; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-9; const long double PI = 3.14159265358979323846; vpl dirs = { {-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0} // self }; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define pr(a) cout << (a) #define prl(a) cout << (a) << endl #define prl2(a, b) cout << (a) << " " << (b) << endl #define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) cout << (a) << " " #define prs2(a, b) cout << (a) << " " << (b) << " " #define prs3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define e1 first #define e2 second #define ctol(c) ll((c)) - ll('0') #define ltos(n) to_string((n)) #define items(kv, v) for (auto &(kv) : (v)) #define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)])) #define rsort(a, n) sort(a, a + n, greater<>()) #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ cout << fixed << setprecision(digits) << (num) << endl; #define XOR(a, b) (a) ^ (b) #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #define DEBUG(x) cerr << #x << ": " << (x) << endl #define DEBUG_VEC(v) \ cerr << #v << ": "; \ rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \ cerr << endl #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; class UnionFind { vl parents, set_size; public: UnionFind() {} UnionFind(ll n) { parents = set_size = vl(n); rep(i, n) { parents[i] = i; set_size[i] = 1LL; } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } }; struct Doubling { // ABC167D ll n; ll sz; vector<vl> next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly input a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; /* class LCA{ public: ll N, logN; vl depth, len; Graph tree[200005]; // global declaration later. vector<vl> parents; LCA(ll n){ N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vector<vl>(logN, vl(N)); init(0, -1, 0, 0); build(); } void init(ll source, ll parent, ll d, ll l){ depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()){ ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build(){ rep(k, logN - 1) rep(n, N){ // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v){ if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN){ if (parents[k][u] != parents[k][v]){ u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v){ ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; */ struct BIT { ll n; vl dat; BIT(ll n, ll ini = 0) : dat(n + 1, ini), n(n){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, dat); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { return query(r) - query(l); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vector<bool> visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; struct StronglyConnectedComponents { ll n; vector<vl> graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return num; } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_mod(ll a, ll power, ll mod) { ll value = 1; while (power != 0) { if (power & 1) value = (value * a) % mod; a = (a * a) % mod; power = power >> 1; } return value % mod; } ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } ll comb_memo[55][55]; ll pascal_triangle(ll n) { comb_memo[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb_memo[i][j] += comb_memo[i - 1][j]; if (j > 0) comb_memo[i][j] += comb_memo[i - 1][j - 1]; } } ll combination(ll n, ll r, ll mod) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { ll num = (n - i) % mod, den = (i + 1) % mod; (numerator *= num) %= mod; (denomenator *= modinv(den, mod)) %= mod; } return (numerator * denomenator) % mod; } ll combination_no_mod(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { numerator *= n - i; denomenator *= i + 1; ll g = gcd(numerator, denomenator); numerator /= g; denomenator /= g; } return numerator; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } ll bin_search(ll key, ll A[], ll left, ll right) { // return the index idx where A[idx] = key. // A[left] is start and A[right] is end.. // In other words, A[right], not A[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (A[mid] == key) return mid; else if (A[mid] > key) right = mid - 1; else if (A[mid] < key) left = mid + 1; } return -1; } /* ll bin_search_temp(ll left, ll right, callable judge){ while(right > left){ // when seeking lower bound ll mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; // when seeking upper bound ll mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } return right; } trinary_search // Care the value EPS!!! Compare to the condition ld left = 0; ld right = p; while(abs(right - left) > EPS){ ld left2 = (2 * left + right) / 3.0; ld right2 = (left + 2 * right) / 3.0; ld f1 = tak_func(left2); ld f2 = tak_func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } */ str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } ll lower_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] >= key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] < key && key <= A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] < key && key <= A[mid]) return mid; else if (A[mid - 1] >= key) right = mid - 1; else if (A[mid] < key) left = mid; } return -1; // all the elements < key } ll inf_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] > key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] <= key && key < A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] <= key && key < A[mid]) return mid; else if (A[mid - 1] > key) right = mid - 1; else if (A[mid] <= key) left = mid; } return -1; // all the elements <= key } ll upper_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] <= key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] <= key && key < A[right]) return right - 1; else return -1; } if (A[mid] <= key && key < A[mid + 1]) return mid; else if (A[mid] > key) right = mid; else if (A[mid + 1] <= key) left = mid + 1; } return -1; // all the elements > key } ll sup_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] < key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] < key && key <= A[right]) return right - 1; else return -1; } if (A[mid] < key && key <= A[mid + 1]) return mid; else if (A[mid] >= key) right = mid; else if (A[mid + 1] < key) left = mid + 1; } return -1; // all the elements >= key } ll bin_search_vector(ll key, vl v, ll left, ll right) { // return the index idx where v[idx] = key. // v[left] is start and v[right] is end.. // In other words, v[right], not v[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (v[mid] == key) return mid; else if (v[mid] > key) right = mid - 1; else if (v[mid] < key) left = mid + 1; } return -1; } ll lower_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] < key <= v[N] // N == -1 if all the elements < key return lower_bound(all(v), key) - v.begin(); } ll inf_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] <= key < v[N] <= key + 1 // N == -1 if all the elements <= key return lower_bound(all(v), key + 1) - v.begin(); } ll upper_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key < v[N + 1] // N == -1 if all the elements > key return upper_bound(all(v), key) - v.begin(); // (- 1) } ll sup_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key - 1 < key <= v[N + 1] // N == -1 if all the elements >= key return upper_bound(all(v), key - 1) - v.begin() - 1; } ll fact(ll n) { if (n == 0) return 1; return n * fact(n - 1); } ll fact_mod(ll n, ll mod) { if (n == 0) return 1; return n * fact_mod(n - 1, mod); } /* ll facts[6000], facts_inv[6000], inv[6000]; void fact_memo(ll n, ll mod){ facts[0] = facts[1] = 1; facts_inv[0] = facts_inv[1] = 1; inv[1] = 1; For(i, 2, n + 1){ facts[i] = (i * facts[i - 1]) % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * inv[i] % mod; } } ll nCk(ll n, ll r){ if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % MOD; val *= facts_inv[r]; return val % MOD; } */ bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } ll bool_sum(ll a1, ll a2) { if (a1 == 1 || a2 == 1) return 1; return 0; } mll prime_factorization(ll n) { ll i = 2; mll table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } ll char_to_idx(char c) { ll idx = 0; Forchar(cc, 'a', 'z') { if (c == cc) return idx; else idx++; } } ll next_combination(ll sub) { /* ### Attention ### if k is 0 or 1 and n is 0, it does not work well. ll n, k; ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F vl z_algorithm(str s) { /* Paramters --------- str: the string of interest Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ ll n = s.length(); vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll big_number_mod(str s, ll mod) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth) % mod; val %= mod; tenth *= 10; tenth %= mod; idx++; } return val; } ll big_number_compare(str s1, str s2) { if (s1.length() > s2.length()) return 1; else if (s1.length() < s2.length()) return -1; else if (s1 == s2) return 0; return 2 * (s1 > s2) - 1; } ll string_to_ll(str s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth); tenth *= 10; idx++; } return val; } str reflected_string(str s) { str t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(point l_begin, point l_end, point p) { ll xl1 = l_begin.x, yl1 = l_begin.y; ll xl2 = l_end.x, yl2 = l_end.y; ll xp = p.x, yp = p.y; ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) { ll x1 = l1_begin.x, y1 = l1_begin.y; ll x2 = l1_end.x, y2 = l1_end.y; ll x3 = l2_begin.x, y3 = l2_begin.y; ll x4 = l2_end.x, y4 = l2_end.y; ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } ld space_of_triangle(point p1, point p2, point p3) { ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } pair<point, ll> center_2det_of_3points(point p1, point p2, point p3) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; // center of 2 points ll c2x_2 = x2 + x1, c2y_2 = y2 + y1; ll c3x_2 = x3 + x1, c3y_2 = y3 + y1; // vertical vector of 2 lines ll b2y = x2 - x1, b2x = -y2 + y1; ll b3y = x3 - x1, b3x = -y3 + y1; ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x; ll det = -b3y * b2x + b3x * b2y; if (det == 0) return {{INF, INF}, det}; ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2; return {{cx_2det, cy_2det}, det}; } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BIT bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = rank + 1; j < n; j++) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v, ll n) { vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return find(all(dp), INF) - dp.begin(); } /* diameter of tree Graph tree[MAX_N]; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree const ll N_VERTEX = 310; ll a, b, t; vector<vl> dist; void warshall_floyd(ll n){ // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } int main(void){ in2(n, m); rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(i, m){ in3(a, b, t); a--; b--; dist[a][b] = t; dist[b][a] = t; } warshall_floyd(n); } vl dist, vertex_pre; void dijkstra(ll start, ll n) { priority_queue<pl, vector<pl>, greater<pl>> edge_costs; dist = vl(n, INF); // vertex_pre = vl(n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { pl edge_cost = edge_costs.top(); edge_costs.pop(); ll idx = edge_cost.second; ll cost = edge_cost.first; if (dist[idx] < cost) continue; rep(i, graph[idx].size()){ edge e = graph[idx][i]; if (dist[e.to] > dist[idx] + e.cost){ dist[e.to] = dist[idx] + e.cost; // vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g){ vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } int main(void){ in2(n, m); rep(i, m){ in3(a, b, t); a--; b--; G[a].pb({b, t}); G[b].pb({a, t}); } dijkstra(0, n); } # ABC061D bool find_negative_cycle(ll goal){ rep(i, n) rep(v, n) rep(k, graph[v].size()){ edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost){ dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll n, ll goal){ // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != -1) fill(dist, dist + n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()){ edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(goal)) return false; return true; } */ /* # 1. The usage of map pair map<pl, ll> cnt; cnt[{i, j}] = 0; items(kv, cnt){ prl2(kv.first, kv.second); } # 2. The usage of next_permutation and combination (factorial search) ll a[8]; rep(i, 8) a[i] = i; sort(a, a + 8); do{ }while(next_permutation(a, a+n)); // here, combination ll n, r; ll i1, i2, ..., ir; For(i1, n - r, n) For(i2, n - r - 1, i1) ... For(ir, n - 2 * r + 1, irr){ process;} # 3. bit search ll n; in1(n); const ll base = 3; ll upper = power_normal(base, n); rep(i, upper){ ll tmp = i; rep(j, n){ rep(k, base) if (tmp % base == k) prl(k); tmp /= base; } } # 4. imos method // used when we would like to count the number which // shows how many times the numbers between l and r belongs to smt. // This method is composed of three process. ll n, m, s[MAX_M], l, r; in2(n, m); rep(i, m) s[i] = 0; // 1st step rep(i, n){ in3(l, r, c); l--; r--; // if l starts from 1. s[l] += c; s[r + 1] -= c; } // 2nd step rep(i, m - 1) s[i + 1] += s[i]; // 3rd step: judgement... #5. shakutori method (syakutori, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ r = max(r, l); if (l == r) r++; while(r < n && cond) r++; answer += r - l; l++; } prl(answer); #6. priority queue pq q; ll answer = 0; ll v; rep(i, n) q.push(a[i]); rep(i, m){ v = q.top(); q.pop(); // get the top value and dump the value from queue v /= 2; q.push(v); // add the new value } while(!q.empty()){ answer += q.top(); q.pop(); } #7. The shortest path (distance) between the k-th edge and another edge (Tree) Graph tree[MAX_N]; ll depth[MAX_N]; void dfs(ll source, ll parent, ll all_cost){ depth[source] = all_cost; items(e, tree[source]){ if (e.to == parent) continue; dfs(e.to, source, all_cost + e.cost); } } ll n, k, a, b, c; in2(n, k); rep(i, n - 1){ in3(a, b, c); a--; b--; tree[a].pb({b, c}); tree[b].pb({a, c}); } k--; dfs(k, -1, 0); #10. Visiting Subtree using recurrent function (ABC138D) Graph tree[MAX_N]; ll c[MAX_N]; bool visited[MAX_N]; void dfs(ll source, ll parent, ll val){ visited[source] = true; c[source] += val; rep(i, tree[source].size()){ rep(i, m){ll res = n % match[i].e1;} ll vertex = tree[source][i].to; if (vertex == parent) continue; dfs(vertex, source, c[source]); } } #11. bfs ABC146D, ABC007C 1. first create a tree. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = graph[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = graph[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } #12. grid to distance matrix (dx, dy) ll w, h; ll pos_to_idx(ll x, ll y){ return y * w + x; } pl idx_to_pos(ll idx){ return mp(idx % w, idx / w); } rep(y, h){ in1(s); rep(x, w){ if (s[x] == '#') wall[x][y] = true; else wall[x][y] = false; } } rep(i1, h * w)rep(i2, h * w) dist[i1][i2] = INF * (i1 != i2); rep(x, w)rep(y, h){ ll idx1 = pos_to_idx(x, y); ll idx2; if (wall[x][y]) continue; if (x != 0 && !wall[x - 1][y]){ idx2 = pos_to_idx(x - 1, y); // if warshall floyd dist[idx1][idx2] = 1; // if dijkstra // graph[idx1].pb({idx2, 1}); } if (x != w - 1 && !wall[x + 1][y]){ idx2 = pos_to_idx(x + 1, y); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != 0 && !wall[x][y - 1]){ idx2 = pos_to_idx(x, y - 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != h - 1 && !wall[x][y + 1]){ idx2 = pos_to_idx(x, y + 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } } # Cumulative sum (2 dimension) ll func(ll x, ll y, ll dx, ll dy){ if (x + dx > w || y + dy > h) return - INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h + 1) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vs[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; # DEBUG_MATRIX rep(i, n){ rep(j, n) prs(a[i][j]); prl(""); } */ /* # the operators regarding bit & (AND), | (OR), ^ (XOR) - (REVERSE), >> (SMALLER SHIFT) << (BIGGER SHIFT) x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & - x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 x << y is x * 2 ** y x >> y is rep(i, y) x = x // 2 ####### Attention ####### 1 << i and 1LL << i is different. If programs show WA, try switch to this one. Let S be a bit sequence and i be a non-negative integer S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & -(1 << i) -> S - {i} __builtin_popcount(S) -> the number of elements in S S = 0 -> S is an empty set __builtin_popcountl(i) -> the number of 1 in binary S = (1 << n) - 1 -> S includes all the elements up to the n-th #Conditional Operator condition ? true : false; #iterator type declaration: auto value reference: *itr increment: itr++ decrement: itr-- substitution of value: *itr = smt # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; bool okay = false; ll answer = 0; ll n, m, k; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; ll memo[MAX_N]; void solve() { RerootingTreeDP<Merger> dp(n); rep(i, n - 1) { ll a, b; in2(a, b); a--, b--; dp.add_edge(a, b, 1); } memo[0] = 1; rep(i, n + 3) memo[i + 1] = (memo[i] * 2) % MOD; dp.dp(); rep(i, n) { // dp.tree[i] := the number of children (answer += memo[n - 1] - 1) %= MOD; // all the cases except all of them are white for (auto &&e : dp.tree[i]) // the case where the i-th vertex is not a hole (answer += MOD - (memo[e.value] - 1)) %= MOD; } ll x_inv = modinv(power_mod(2, n, MOD), MOD); ll y = answer; prl((y * x_inv) % MOD); } int main(void) { in1(n); solve(); return 0; }
delete
1,861
1,862
1,861
1,861
0
p02822
C++
Runtime Error
// Words are flowing out like endless rain into a paper cup // They slither while they pass they slip away across the universe // Pools of sorrow, waves of joy are drifting through my open mind // Possessing and caressing me #include <bits/stdc++.h> using namespace std; using LL = long long; namespace _buff { const size_t BUFF = 1 << 19; char ibuf[BUFF], *ib = ibuf, *ie = ibuf; char getc() { if (ib == ie) { ib = ibuf; ie = ibuf + fread(ibuf, 1, BUFF, stdin); } return ib == ie ? -1 : *ib++; } } // namespace _buff LL read() { using namespace _buff; LL ret = 0; bool pos = true; char c = getc(); for (; (c < '0' || c > '9') && c != '-'; c = getc()) { assert(~c); } if (c == '-') { pos = false; c = getc(); } for (; c >= '0' && c <= '9'; c = getc()) { ret = (ret << 3) + (ret << 1) + (c ^ 48); } return pos ? ret : -ret; } const size_t N = 1e5 + 5; const int MOD = 1e9 + 7; int qpow(int base, int e) { int ret = 1; for (; e; e >>= 1) { if (e & 1) { ret = (LL)ret * base % MOD; } base = (LL)base * base % MOD; } return ret; } void iadd(int &dp, const int &val) { dp += val; if (dp >= MOD) { dp -= MOD; } } void ladd(int &dp, const LL &val) { dp = (dp + val) % MOD; } int n, ans; vector<int> g[N]; int siz[N]; int dfs(int u = 1, int p = 0) { vector<int> vc; siz[u] = 1; for (int v : g[u]) { if (v != p) { vc.emplace_back(dfs(v, u)); siz[u] += siz[v]; } } vc.emplace_back(n - siz[u]); int y = 0, z = n; for (int x : vc) { z -= x; ladd(ans, (LL)(MOD + 1 - qpow(2, MOD - 1 - x)) * (MOD + 1 - qpow(2, MOD - 1 - y)) % MOD * qpow(2, MOD - 1 - z)); y += x; } return siz[u]; } int main() { n = read(); for (int i = 1; i < n; ++i) { int a = read(), b = read(); g[a].emplace_back(b); g[b].emplace_back(a); } dfs(); cout << ans; return 0; }
// Words are flowing out like endless rain into a paper cup // They slither while they pass they slip away across the universe // Pools of sorrow, waves of joy are drifting through my open mind // Possessing and caressing me #include <bits/stdc++.h> using namespace std; using LL = long long; namespace _buff { const size_t BUFF = 1 << 19; char ibuf[BUFF], *ib = ibuf, *ie = ibuf; char getc() { if (ib == ie) { ib = ibuf; ie = ibuf + fread(ibuf, 1, BUFF, stdin); } return ib == ie ? -1 : *ib++; } } // namespace _buff LL read() { using namespace _buff; LL ret = 0; bool pos = true; char c = getc(); for (; (c < '0' || c > '9') && c != '-'; c = getc()) { assert(~c); } if (c == '-') { pos = false; c = getc(); } for (; c >= '0' && c <= '9'; c = getc()) { ret = (ret << 3) + (ret << 1) + (c ^ 48); } return pos ? ret : -ret; } const size_t N = 2e5 + 5; const int MOD = 1e9 + 7; int qpow(int base, int e) { int ret = 1; for (; e; e >>= 1) { if (e & 1) { ret = (LL)ret * base % MOD; } base = (LL)base * base % MOD; } return ret; } void iadd(int &dp, const int &val) { dp += val; if (dp >= MOD) { dp -= MOD; } } void ladd(int &dp, const LL &val) { dp = (dp + val) % MOD; } int n, ans; vector<int> g[N]; int siz[N]; int dfs(int u = 1, int p = 0) { vector<int> vc; siz[u] = 1; for (int v : g[u]) { if (v != p) { vc.emplace_back(dfs(v, u)); siz[u] += siz[v]; } } vc.emplace_back(n - siz[u]); int y = 0, z = n; for (int x : vc) { z -= x; ladd(ans, (LL)(MOD + 1 - qpow(2, MOD - 1 - x)) * (MOD + 1 - qpow(2, MOD - 1 - y)) % MOD * qpow(2, MOD - 1 - z)); y += x; } return siz[u]; } int main() { n = read(); for (int i = 1; i < n; ++i) { int a = read(), b = read(); g[a].emplace_back(b); g[b].emplace_back(a); } dfs(); cout << ans; return 0; }
replace
42
43
42
43
0
p02822
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>; // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } // combination mod prime // https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619 vector<int> to[100005]; mint ans; int N; int dfs(int v, int p = -1) { int res = 1; vector<int> ts; for (int e : to[v]) { if (p == e) { continue; } int t = dfs(e, v); res += t; ts.push_back(t); } if (-1 != p) { ts.push_back(N - res); } mint now = mint(2).pow(N - 1) - 1; for (int t : ts) { now -= mint(2).pow(t) - 1; } ans += now; return res; } int main() { cin >> N; for (int n = 0; n < N - 1; ++n) { int a, b; cin >> a >> b; a--; b--; to[a].push_back(b); to[b].push_back(a); } dfs(0); ans /= mint(2).pow(N); cout << ans.x << endl; return 0; }
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } // combination mod prime // https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619 vector<int> to[200005]; mint ans; int N; int dfs(int v, int p = -1) { int res = 1; vector<int> ts; for (int e : to[v]) { if (p == e) { continue; } int t = dfs(e, v); res += t; ts.push_back(t); } if (-1 != p) { ts.push_back(N - res); } mint now = mint(2).pow(N - 1) - 1; for (int t : ts) { now -= mint(2).pow(t) - 1; } ans += now; return res; } int main() { cin >> N; for (int n = 0; n < N - 1; ++n) { int a, b; cin >> a >> b; a--; b--; to[a].push_back(b); to[b].push_back(a); } dfs(0); ans /= mint(2).pow(N); cout << ans.x << endl; return 0; }
replace
52
53
52
53
0
p02822
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define int long long int n; vector<int> graph[100010]; int dp1[200010]; int dp2[200010]; int zi[200010]; int pow_mod(int i, int j) { int ans = 1; while (j > 0) { if (j & 1) ans = (ans * i) % MOD; i = (i * i) % MOD; j >>= 1; } return ans; } void dp(int idx, int par) { int ans = 1; int ans2 = zi[n - 1]; for (int i = 0; i < graph[idx].size(); i++) { int child = graph[idx][i]; if (child == par) continue; dp(child, idx); ans += dp1[child]; ans2 -= (MOD + zi[dp1[child]] - 1) % MOD; if (ans2 < 0) ans2 = MOD + ans2; } dp1[idx] = ans; ans2 -= zi[n - ans]; if (ans2 < 0) ans2 = MOD + ans2; dp2[idx] = ans2; return; } signed main() { cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; graph[a].push_back(b); graph[b].push_back(a); } zi[0] = 1; for (int i = 1; i < 200010; i++) { zi[i] = zi[i - 1] * 2 % MOD; } dp(0, -1); int ans = 0; for (int i = 0; i < n; i++) { ans += dp2[i]; ans %= MOD; } cout << ans * pow_mod(zi[n], MOD - 2) % MOD << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define int long long int n; vector<int> graph[200010]; int dp1[200010]; int dp2[200010]; int zi[200010]; int pow_mod(int i, int j) { int ans = 1; while (j > 0) { if (j & 1) ans = (ans * i) % MOD; i = (i * i) % MOD; j >>= 1; } return ans; } void dp(int idx, int par) { int ans = 1; int ans2 = zi[n - 1]; for (int i = 0; i < graph[idx].size(); i++) { int child = graph[idx][i]; if (child == par) continue; dp(child, idx); ans += dp1[child]; ans2 -= (MOD + zi[dp1[child]] - 1) % MOD; if (ans2 < 0) ans2 = MOD + ans2; } dp1[idx] = ans; ans2 -= zi[n - ans]; if (ans2 < 0) ans2 = MOD + ans2; dp2[idx] = ans2; return; } signed main() { cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; graph[a].push_back(b); graph[b].push_back(a); } zi[0] = 1; for (int i = 1; i < 200010; i++) { zi[i] = zi[i - 1] * 2 % MOD; } dp(0, -1); int ans = 0; for (int i = 0; i < n; i++) { ans += dp2[i]; ans %= MOD; } cout << ans * pow_mod(zi[n], MOD - 2) % MOD << endl; return 0; }
replace
8
9
8
9
0
p02822
C++
Runtime Error
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define printa(x, n) \ for (ll i = 0; i < n; i++) { \ cout << (x[i]) << " \n"[i == n - 1]; \ }; void print() {} template <class H, class... T> void print(H &&h, T &&...t) { cout << h << " \n"[sizeof...(t) == 0]; print(forward<T>(t)...); } ll dist[100010] = {}; vector<ll> tree[200010]; void dfs(ll cur, ll par) { dist[cur] = 1; for (auto &e : tree[cur]) { if (e == par) continue; dfs(e, cur); dist[cur] += dist[e]; } } ll ans = 0; ll N; ll power(ll x, ll n) { if (n == 0) return 1LL; ll res = power(x * x % MOD, n / 2); if (n % 2 == 1) res = res * x % MOD; return res; } void dfs2(ll cur, ll par) { if (tree[cur].size() > 1) { ll res = power(2, N - 1); for (auto &e : tree[cur]) { res -= power(2, dist[e]) - 1; res = (res + MOD) % MOD; } res = (res - 1 + MOD) % MOD; ans += res; ans %= MOD; } for (auto &e : tree[cur]) { if (e == par) continue; ll tmp = dist[e]; dist[cur] = dist[cur] - dist[e]; dist[e] = N; dfs2(e, cur); dist[e] = tmp; dist[cur] = N; } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; ll A[200010], B[200010]; rep(i, 0, N - 1) { cin >> A[i] >> B[i]; A[i]--; B[i]--; tree[A[i]].push_back(B[i]); tree[B[i]].push_back(A[i]); } dfs(0, -1); dfs2(0, -1); ll v = power(2, N); ans *= power(v, MOD - 2); ans %= MOD; print(ans); }
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define printa(x, n) \ for (ll i = 0; i < n; i++) { \ cout << (x[i]) << " \n"[i == n - 1]; \ }; void print() {} template <class H, class... T> void print(H &&h, T &&...t) { cout << h << " \n"[sizeof...(t) == 0]; print(forward<T>(t)...); } ll dist[200010] = {}; vector<ll> tree[200010]; void dfs(ll cur, ll par) { dist[cur] = 1; for (auto &e : tree[cur]) { if (e == par) continue; dfs(e, cur); dist[cur] += dist[e]; } } ll ans = 0; ll N; ll power(ll x, ll n) { if (n == 0) return 1LL; ll res = power(x * x % MOD, n / 2); if (n % 2 == 1) res = res * x % MOD; return res; } void dfs2(ll cur, ll par) { if (tree[cur].size() > 1) { ll res = power(2, N - 1); for (auto &e : tree[cur]) { res -= power(2, dist[e]) - 1; res = (res + MOD) % MOD; } res = (res - 1 + MOD) % MOD; ans += res; ans %= MOD; } for (auto &e : tree[cur]) { if (e == par) continue; ll tmp = dist[e]; dist[cur] = dist[cur] - dist[e]; dist[e] = N; dfs2(e, cur); dist[e] = tmp; dist[cur] = N; } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; ll A[200010], B[200010]; rep(i, 0, N - 1) { cin >> A[i] >> B[i]; A[i]--; B[i]--; tree[A[i]].push_back(B[i]); tree[B[i]].push_back(A[i]); } dfs(0, -1); dfs2(0, -1); ll v = power(2, N); ans *= power(v, MOD - 2); ans %= MOD; print(ans); }
replace
18
19
18
19
0
p02822
Python
Time Limit Exceeded
import sys from collections import deque read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10**9) INF = 1 << 60 def main(): MOD = 1000000007 N, *AB = map(int, read().split()) G = [[] for _ in range(N)] for a, b in zip(AB[::2], AB[1::2]): G[a - 1].append(b - 1) G[b - 1].append(a - 1) stack = deque([0]) order = [] prev = [0] * N prev[0] = -1 while stack: v = stack.pop() order.append(v) for nv in G[v]: if nv != prev[v]: stack.append(nv) prev[nv] = v pow2 = [0] * (N + 1) pow2[0] = 1 for i in range(N): pow2[i + 1] = pow2[i] * 2 nodes = [1] * N numer = 0 for v in reversed(order): if prev[v] != -1: nodes[prev[v]] += nodes[v] numer = (numer + pow2[N - 1] - 1 - (pow2[N - nodes[v]] - 1)) % MOD for nv in G[v]: if nv != prev[v]: numer = (numer - (pow2[nodes[nv]] - 1)) % MOD denom = pow2[N] ans = (numer * pow(denom, MOD - 2, MOD)) % MOD print(ans) return if __name__ == "__main__": main()
import sys from collections import deque read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10**9) INF = 1 << 60 def main(): MOD = 1000000007 N, *AB = map(int, read().split()) G = [[] for _ in range(N)] for a, b in zip(AB[::2], AB[1::2]): G[a - 1].append(b - 1) G[b - 1].append(a - 1) stack = deque([0]) order = [] prev = [0] * N prev[0] = -1 while stack: v = stack.pop() order.append(v) for nv in G[v]: if nv != prev[v]: stack.append(nv) prev[nv] = v pow2 = [0] * (N + 1) pow2[0] = 1 for i in range(N): pow2[i + 1] = pow2[i] * 2 % MOD nodes = [1] * N numer = 0 for v in reversed(order): if prev[v] != -1: nodes[prev[v]] += nodes[v] numer = (numer + pow2[N - 1] - 1 - (pow2[N - nodes[v]] - 1)) % MOD for nv in G[v]: if nv != prev[v]: numer = (numer - (pow2[nodes[nv]] - 1)) % MOD denom = pow2[N] ans = (numer * pow(denom, MOD - 2, MOD)) % MOD print(ans) return if __name__ == "__main__": main()
replace
33
34
33
34
TLE
p02822
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define pb push_back #define mp make_pair #define F first #define S second #define pii pair<int, int> #define pll pair<ll, ll> #define pcc pair<char, char> #define vi vector<int> #define vl vector<ll> #define sd(x) scanf("%d", &x) #define slld(x) scanf("%lld", &x) #define pd(x) printf("%d", x) #define plld(x) printf("%lld", x) #define pds(x) printf("%d ", x) #define pllds(x) printf("%lld ", x) #define pdn(x) printf("%d\n", x) #define plldn(x) printf("%lld\n", x) #define INF 2e9 #define INFLL 4e18 using namespace std; ll powmod(ll base, ll exponent, ll mod) { // with mod < 1e9 ll ans = 1; while (exponent) { if (exponent & 1) ans = (ans * base) % mod; base = (base * base) % mod; exponent /= 2; } return ans; } ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } const int upperlimit = 1e5 + 1; const int mod = 1e9 + 7; int subtr[upperlimit]; bool visited[upperlimit]; vi adj[upperlimit]; void dfs(int node) { visited[node] = true; subtr[node] = 1; for (int i = 0; i < adj[node].size(); i++) { if (!visited[adj[node][i]]) { dfs(adj[node][i]); subtr[node] += subtr[adj[node][i]]; } } } int main() { int n, a, b; sd(n); ll ans = 0; for (int i = 1; i < n; i++) { sd(a); sd(b); adj[a].pb(b); adj[b].pb(a); } dfs(1); for (int i = 1; i <= n; i++) { ll calc = powmod(2, n - 1, mod) - 1; for (int j = 0; j < adj[i].size(); j++) { if (subtr[adj[i][j]] < subtr[i]) calc -= powmod(2, subtr[adj[i][j]], mod) - 1; else calc -= powmod(2, n - subtr[i], mod) - 1; if (calc < 0) calc += mod; } ans += calc; ans %= mod; } ans *= powmod(2, mod - 1 - n, mod); ans %= mod; plld(ans); return 0; }
#include <bits/stdc++.h> #define ll long long #define pb push_back #define mp make_pair #define F first #define S second #define pii pair<int, int> #define pll pair<ll, ll> #define pcc pair<char, char> #define vi vector<int> #define vl vector<ll> #define sd(x) scanf("%d", &x) #define slld(x) scanf("%lld", &x) #define pd(x) printf("%d", x) #define plld(x) printf("%lld", x) #define pds(x) printf("%d ", x) #define pllds(x) printf("%lld ", x) #define pdn(x) printf("%d\n", x) #define plldn(x) printf("%lld\n", x) #define INF 2e9 #define INFLL 4e18 using namespace std; ll powmod(ll base, ll exponent, ll mod) { // with mod < 1e9 ll ans = 1; while (exponent) { if (exponent & 1) ans = (ans * base) % mod; base = (base * base) % mod; exponent /= 2; } return ans; } ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } const int upperlimit = 2e5 + 1; const int mod = 1e9 + 7; int subtr[upperlimit]; bool visited[upperlimit]; vi adj[upperlimit]; void dfs(int node) { visited[node] = true; subtr[node] = 1; for (int i = 0; i < adj[node].size(); i++) { if (!visited[adj[node][i]]) { dfs(adj[node][i]); subtr[node] += subtr[adj[node][i]]; } } } int main() { int n, a, b; sd(n); ll ans = 0; for (int i = 1; i < n; i++) { sd(a); sd(b); adj[a].pb(b); adj[b].pb(a); } dfs(1); for (int i = 1; i <= n; i++) { ll calc = powmod(2, n - 1, mod) - 1; for (int j = 0; j < adj[i].size(); j++) { if (subtr[adj[i][j]] < subtr[i]) calc -= powmod(2, subtr[adj[i][j]], mod) - 1; else calc -= powmod(2, n - subtr[i], mod) - 1; if (calc < 0) calc += mod; } ans += calc; ans %= mod; } ans *= powmod(2, mod - 1 - n, mod); ans %= mod; plld(ans); return 0; }
replace
38
39
38
39
0
p02822
C++
Runtime Error
#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 using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, int> pli; 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]; } int n; vector<int> G[MAX]; int sz[MAX]; vector<int> sz_child[MAX]; mint ans; void dfs(int v, int pv) { sz[v] = 1; mint t = (mint(2) ^ (n - 1)) - 1; for (int nv : G[v]) if (nv != pv) { dfs(nv, v); sz[v] += sz[nv]; t -= (mint(2) ^ sz[nv]) - 1; } t -= (mint(2) ^ (n - sz[v])) - 1; ans += t; } int main() { scanf("%d", &n); rep(i, n - 1) { int a, b; scanf("%d%d", &a, &b); --a; --b; G[a].push_back(b); G[b].push_back(a); } ans = 0; dfs(0, -1); mint div = mint(2) ^ n; printf("%lld\n", (ans / div).a); }
#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 using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, int> pli; 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 = 200010; 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]; } int n; vector<int> G[MAX]; int sz[MAX]; vector<int> sz_child[MAX]; mint ans; void dfs(int v, int pv) { sz[v] = 1; mint t = (mint(2) ^ (n - 1)) - 1; for (int nv : G[v]) if (nv != pv) { dfs(nv, v); sz[v] += sz[nv]; t -= (mint(2) ^ sz[nv]) - 1; } t -= (mint(2) ^ (n - sz[v])) - 1; ans += t; } int main() { scanf("%d", &n); rep(i, n - 1) { int a, b; scanf("%d%d", &a, &b); --a; --b; G[a].push_back(b); G[b].push_back(a); } ans = 0; dfs(0, -1); mint div = mint(2) ^ n; printf("%lld\n", (ans / div).a); }
replace
32
33
32
33
0
p02822
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (lli i = 0; i < (n); i++) #define rrep(i, n) for (lli i = (n)-1; i >= 0; i--) using namespace std; using lli = long long int; template <signed M, unsigned T> struct mod_int { constexpr static signed MODULO = M; constexpr static unsigned TABLE_SIZE = T; signed x; mod_int() : x(0) {} mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {} mod_int(int y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {} mod_int &operator+=(const mod_int &rhs) { if ((x += rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int &operator-=(const mod_int &rhs) { if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int &operator*=(const mod_int &rhs) { x = static_cast<signed>(1LL * x * rhs.x % MODULO); return *this; } mod_int &operator/=(const mod_int &rhs) { x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO); return *this; } mod_int operator-() const { return mod_int(-x); } mod_int operator+(const mod_int &rhs) const { return mod_int(*this) += rhs; } mod_int operator-(const mod_int &rhs) const { return mod_int(*this) -= rhs; } mod_int operator*(const mod_int &rhs) const { return mod_int(*this) *= rhs; } mod_int operator/(const mod_int &rhs) const { return mod_int(*this) /= rhs; } bool operator<(const mod_int &rhs) const { return x < rhs.x; } mod_int inv() const { assert(x != 0); if (x <= static_cast<signed>(TABLE_SIZE)) { if (_inv[1].x == 0) prepare(); return _inv[x]; } else { signed a = x, b = MODULO, u = 1, v = 0, t; while (b) { t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return mod_int(u); } } mod_int pow(long long t) const { assert(!(x == 0 && t == 0)); mod_int e = *this, res = mod_int(1); for (; t; e *= e, t >>= 1) if (t & 1) res *= e; return res; } mod_int fact() { if (_fact[0].x == 0) prepare(); return _fact[x]; } mod_int inv_fact() { if (_fact[0].x == 0) prepare(); return _inv_fact[x]; } mod_int choose(mod_int y) { assert(y.x <= x); return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact(); } static mod_int _inv[TABLE_SIZE + 1]; static mod_int _fact[TABLE_SIZE + 1]; static mod_int _inv_fact[TABLE_SIZE + 1]; static void prepare() { _inv[1] = 1; for (int i = 2; i <= (int)TABLE_SIZE; ++i) { _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO; } _fact[0] = 1; for (unsigned i = 1; i <= TABLE_SIZE; ++i) { _fact[i] = _fact[i - 1] * int(i); } _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv(); for (int i = (int)TABLE_SIZE - 1; i >= 0; --i) { _inv_fact[i] = _inv_fact[i + 1] * (i + 1); } } }; template <int M, unsigned F> std::ostream &operator<<(std::ostream &os, const mod_int<M, F> &rhs) { return os << rhs.x; } template <int M, unsigned F> std::istream &operator>>(std::istream &is, mod_int<M, F> &rhs) { long long s; is >> s; rhs = mod_int<M, F>(s); return is; } template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1]; template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1]; template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1]; template <int M, unsigned F> bool operator==(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) { return lhs.x == rhs.x; } template <int M, unsigned F> bool operator!=(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) { return !(lhs == rhs); } const int MF = 100005; // 割り算しないなら const int MOD = 1000000007; using mint = mod_int<MOD, MF>; mint binom(int n, int r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); } mint fact(int n) { return mint(n).fact(); } mint inv_fact(int n) { return mint(n).inv_fact(); } lli sz[200005] = {}; mint sp[200005] = {}; mint t = 0; lli N; vector<lli> e[100005]; pair<lli, mint> dfs(int u, lli p) { sz[u] = 1; // cout << u << " " << p << endl; mint pp = 0; vector<lli> y; for (auto to : e[u]) { if (to == p) { continue; } auto p = dfs(to, u); sp[u] += p.second; sz[u] += p.first; y.push_back(p.first); } if (p != -1) y.push_back(N - sz[u]); if (y.size() > 1) { // cout << u << " "; mint allshiro = 1; for (auto s : y) { allshiro *= mint(2).pow(s).inv(); // cout << s << " " ; } for (auto s : y) { pp += (mint(1) - mint(2).pow(s).inv()) * allshiro * mint(2).pow(s); } // cout << endl; t += (mint(1) - pp - allshiro) * mint(2).inv(); } return make_pair(sz[u], sp[u]); } int main() { lli n; cin >> n; N = n; vector<lli> a(n), b(n); rep(i, n - 1) cin >> a[i] >> b[i], a[i]--, b[i]--; rep(i, n - 1) e[a[i]].push_back(b[i]), e[b[i]].push_back(a[i]); // cout << "HOGE"<<endl; dfs(0, -1); cout << t << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (lli i = 0; i < (n); i++) #define rrep(i, n) for (lli i = (n)-1; i >= 0; i--) using namespace std; using lli = long long int; template <signed M, unsigned T> struct mod_int { constexpr static signed MODULO = M; constexpr static unsigned TABLE_SIZE = T; signed x; mod_int() : x(0) {} mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {} mod_int(int y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {} mod_int &operator+=(const mod_int &rhs) { if ((x += rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int &operator-=(const mod_int &rhs) { if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int &operator*=(const mod_int &rhs) { x = static_cast<signed>(1LL * x * rhs.x % MODULO); return *this; } mod_int &operator/=(const mod_int &rhs) { x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO); return *this; } mod_int operator-() const { return mod_int(-x); } mod_int operator+(const mod_int &rhs) const { return mod_int(*this) += rhs; } mod_int operator-(const mod_int &rhs) const { return mod_int(*this) -= rhs; } mod_int operator*(const mod_int &rhs) const { return mod_int(*this) *= rhs; } mod_int operator/(const mod_int &rhs) const { return mod_int(*this) /= rhs; } bool operator<(const mod_int &rhs) const { return x < rhs.x; } mod_int inv() const { assert(x != 0); if (x <= static_cast<signed>(TABLE_SIZE)) { if (_inv[1].x == 0) prepare(); return _inv[x]; } else { signed a = x, b = MODULO, u = 1, v = 0, t; while (b) { t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return mod_int(u); } } mod_int pow(long long t) const { assert(!(x == 0 && t == 0)); mod_int e = *this, res = mod_int(1); for (; t; e *= e, t >>= 1) if (t & 1) res *= e; return res; } mod_int fact() { if (_fact[0].x == 0) prepare(); return _fact[x]; } mod_int inv_fact() { if (_fact[0].x == 0) prepare(); return _inv_fact[x]; } mod_int choose(mod_int y) { assert(y.x <= x); return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact(); } static mod_int _inv[TABLE_SIZE + 1]; static mod_int _fact[TABLE_SIZE + 1]; static mod_int _inv_fact[TABLE_SIZE + 1]; static void prepare() { _inv[1] = 1; for (int i = 2; i <= (int)TABLE_SIZE; ++i) { _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO; } _fact[0] = 1; for (unsigned i = 1; i <= TABLE_SIZE; ++i) { _fact[i] = _fact[i - 1] * int(i); } _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv(); for (int i = (int)TABLE_SIZE - 1; i >= 0; --i) { _inv_fact[i] = _inv_fact[i + 1] * (i + 1); } } }; template <int M, unsigned F> std::ostream &operator<<(std::ostream &os, const mod_int<M, F> &rhs) { return os << rhs.x; } template <int M, unsigned F> std::istream &operator>>(std::istream &is, mod_int<M, F> &rhs) { long long s; is >> s; rhs = mod_int<M, F>(s); return is; } template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1]; template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1]; template <int M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1]; template <int M, unsigned F> bool operator==(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) { return lhs.x == rhs.x; } template <int M, unsigned F> bool operator!=(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) { return !(lhs == rhs); } const int MF = 100005; // 割り算しないなら const int MOD = 1000000007; using mint = mod_int<MOD, MF>; mint binom(int n, int r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); } mint fact(int n) { return mint(n).fact(); } mint inv_fact(int n) { return mint(n).inv_fact(); } lli sz[200005] = {}; mint sp[200005] = {}; mint t = 0; lli N; vector<lli> e[200005]; pair<lli, mint> dfs(int u, lli p) { sz[u] = 1; // cout << u << " " << p << endl; mint pp = 0; vector<lli> y; for (auto to : e[u]) { if (to == p) { continue; } auto p = dfs(to, u); sp[u] += p.second; sz[u] += p.first; y.push_back(p.first); } if (p != -1) y.push_back(N - sz[u]); if (y.size() > 1) { // cout << u << " "; mint allshiro = 1; for (auto s : y) { allshiro *= mint(2).pow(s).inv(); // cout << s << " " ; } for (auto s : y) { pp += (mint(1) - mint(2).pow(s).inv()) * allshiro * mint(2).pow(s); } // cout << endl; t += (mint(1) - pp - allshiro) * mint(2).inv(); } return make_pair(sz[u], sp[u]); } int main() { lli n; cin >> n; N = n; vector<lli> a(n), b(n); rep(i, n - 1) cin >> a[i] >> b[i], a[i]--, b[i]--; rep(i, n - 1) e[a[i]].push_back(b[i]), e[b[i]].push_back(a[i]); // cout << "HOGE"<<endl; dfs(0, -1); cout << t << endl; }
replace
166
167
166
167
0
p02822
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define fi first #define se second typedef pair<ll, ll> P; using VP = vector<P>; using VVP = vector<VP>; using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>; const int inf = 1e9 + 7; const ll INF = 1LL << 61; const ll mod = 1e9 + 7; ll ans = 0; int n; vector<int> E[101010]; VI v(101010); VI w(101010); VI tw(202020); void dfs(int cu, int pa = -1) { v[cu] = 1; w[cu] = tw[n - 1] - 1; for (int to : E[cu]) { if (to != pa) { // dfs(to, cu); // v[cu] += v[to]; w[cu] += w[to]; w[cu] += (mod - tw[v[to]] + 1) % mod; w[cu] %= mod; } } w[cu] += (mod - (tw[n - v[cu]]) + 1) % mod; w[cu] %= mod; } ll modinv(ll a) { ll m = mod; 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() { int i, j; cin >> n; for (i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; E[a].pb(b); E[b].pb(a); } tw[0] = 1; for (i = 1; i < 202020; i++) { tw[i] = tw[i - 1] * 2 % mod; } dfs(0); for (i = 0; i < n; i++) { // cout<<i+1<<" "<<w[i]<<" "<<v[i]<<endl; } ll ans = w[0] * modinv(tw[n]) % mod; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define fi first #define se second typedef pair<ll, ll> P; using VP = vector<P>; using VVP = vector<VP>; using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>; const int inf = 1e9 + 7; const ll INF = 1LL << 61; const ll mod = 1e9 + 7; ll ans = 0; int n; vector<int> E[201010]; VI v(201010); VI w(201010); VI tw(202020); void dfs(int cu, int pa = -1) { v[cu] = 1; w[cu] = tw[n - 1] - 1; for (int to : E[cu]) { if (to != pa) { // dfs(to, cu); // v[cu] += v[to]; w[cu] += w[to]; w[cu] += (mod - tw[v[to]] + 1) % mod; w[cu] %= mod; } } w[cu] += (mod - (tw[n - v[cu]]) + 1) % mod; w[cu] %= mod; } ll modinv(ll a) { ll m = mod; 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() { int i, j; cin >> n; for (i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; E[a].pb(b); E[b].pb(a); } tw[0] = 1; for (i = 1; i < 202020; i++) { tw[i] = tw[i - 1] * 2 % mod; } dfs(0); for (i = 0; i < n; i++) { // cout<<i+1<<" "<<w[i]<<" "<<v[i]<<endl; } ll ans = w[0] * modinv(tw[n]) % mod; cout << ans << endl; return 0; }
replace
18
21
18
21
0
p02822
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <queue> #include <stack> #include <stdlib.h> #include <string.h> #include <string> #include <typeinfo> #include <utility> #include <vector> #define mod 1000000007 using namespace std; typedef long long ll; typedef pair<int, int> pi; typedef pair<double, double> pd; typedef pair<ll, ll> pl; vector<int> adj[200001]; int childs[200001]; ll res; int n; int modpow(int a, int b) { ll powres = 1; for (int i = 0; i < b; i++) { powres *= a; powres %= mod; } return powres; } int inv(int a) { return modpow(a, mod - 2); } int dfs(int cur, int pre) { vector<int> vcounts; childs[cur] = 1; for (int i = 0; i < adj[cur].size(); i++) if (adj[cur][i] != pre) { vcounts.push_back(dfs(adj[cur][i], cur)); childs[cur] += vcounts.back(); } if (childs[cur] < n) vcounts.push_back(n - childs[cur]); ll p[3] = {(mod + 1) / 2, 0, 0}; for (int i = 0; i < vcounts.size(); i++) { ll q[3] = {}; int w = modpow((mod + 1) / 2, vcounts[i]); int b = (mod + 1 - w) % mod; q[0] = (p[0] * w) % mod; q[1] = ((p[0] * b) % mod + (p[1] * w) % mod) % mod; q[2] = (p[2] + p[1] * b) % mod; swap(p, q); /*if(cur == 2) printf("%llda\n", w);*/ } // printf("%lld\n", p[2]); res += p[2]; res %= mod; return childs[cur]; } int main() { scanf("%d", &n); for (int i = 0; i < n - 1; i++) { int a, b; scanf("%d %d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } dfs(1, -1); // printf("%lld\n", (((ll)mod+1)/2 * ((mod+1)/2))%mod); printf("%lld\n", res); return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <queue> #include <stack> #include <stdlib.h> #include <string.h> #include <string> #include <typeinfo> #include <utility> #include <vector> #define mod 1000000007 using namespace std; typedef long long ll; typedef pair<int, int> pi; typedef pair<double, double> pd; typedef pair<ll, ll> pl; vector<int> adj[200001]; int childs[200001]; ll res; int n; ll modpow(int a, int b) { if (b == 1) return a; else if (b % 2 == 0) { ll temp = modpow(a, b / 2); return temp * temp % mod; } else return modpow(a, b - 1) * a % mod; } int inv(int a) { return modpow(a, mod - 2); } int dfs(int cur, int pre) { vector<int> vcounts; childs[cur] = 1; for (int i = 0; i < adj[cur].size(); i++) if (adj[cur][i] != pre) { vcounts.push_back(dfs(adj[cur][i], cur)); childs[cur] += vcounts.back(); } if (childs[cur] < n) vcounts.push_back(n - childs[cur]); ll p[3] = {(mod + 1) / 2, 0, 0}; for (int i = 0; i < vcounts.size(); i++) { ll q[3] = {}; int w = modpow((mod + 1) / 2, vcounts[i]); int b = (mod + 1 - w) % mod; q[0] = (p[0] * w) % mod; q[1] = ((p[0] * b) % mod + (p[1] * w) % mod) % mod; q[2] = (p[2] + p[1] * b) % mod; swap(p, q); /*if(cur == 2) printf("%llda\n", w);*/ } // printf("%lld\n", p[2]); res += p[2]; res %= mod; return childs[cur]; } int main() { scanf("%d", &n); for (int i = 0; i < n - 1; i++) { int a, b; scanf("%d %d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } dfs(1, -1); // printf("%lld\n", (((ll)mod+1)/2 * ((mod+1)/2))%mod); printf("%lld\n", res); return 0; }
replace
28
37
28
36
TLE
p02822
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; vector<int> Childnum(100000, -1); vector<vector<int>> Child(100000, vector<int>(0)); // https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a // a^n mod を計算する long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a^{-1} mod を計算する long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int dp(int i) { if (Childnum[i] != -1) { return Childnum[i]; } if (Child[i].size() == 0) { Childnum[i] = 0; return 0; } int res = 0; for (auto &&j : Child[i]) { res += dp(j) + 1; } Childnum[i] = res; return res; } int main(void) { long long int mod = 1000000007; int N; cin >> N; vector<vector<int>> G(N, vector<int>(0)); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; G[a - 1].push_back(b - 1); G[b - 1].push_back(a - 1); } queue<int> q; q.push(0); vector<int> reached(N, 0); while (!q.empty()) { int r = q.front(); q.pop(); reached[r] = 1; for (auto &&p : G[r]) { if (reached[p] == 1) { continue; } Child[r].push_back(p); q.push(p); } } dp(0); long long int ans = (N * (1 - modinv(modpow(2, N - 1, mod), mod))) % mod; for (int r = 0; r < N; r++) { long long int tmp = 0; for (auto &&p : Child[r]) { long long int ai = Childnum[p] + 1; tmp += ai; long long int k1 = (1 - modinv(modpow(2, ai, mod), mod)) % mod; long long int k2 = modinv(modpow(2, N - 1 - ai, mod), mod); ans -= (k1 * k2) % mod; ans %= mod; } long long int ai = N - 1 - tmp; long long int k1 = (1 - modinv(modpow(2, ai, mod), mod)) % mod; long long int k2 = modinv(modpow(2, N - 1 - ai, mod), mod); ans -= (k1 * k2) % mod; ans %= mod; } cout << ((ans * modinv(2, mod)) % mod) << endl; }
#include <bits/stdc++.h> using namespace std; vector<int> Childnum(200005, -1); vector<vector<int>> Child(200005, vector<int>(0)); // https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a // a^n mod を計算する long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a^{-1} mod を計算する long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int dp(int i) { if (Childnum[i] != -1) { return Childnum[i]; } if (Child[i].size() == 0) { Childnum[i] = 0; return 0; } int res = 0; for (auto &&j : Child[i]) { res += dp(j) + 1; } Childnum[i] = res; return res; } int main(void) { long long int mod = 1000000007; int N; cin >> N; vector<vector<int>> G(N, vector<int>(0)); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; G[a - 1].push_back(b - 1); G[b - 1].push_back(a - 1); } queue<int> q; q.push(0); vector<int> reached(N, 0); while (!q.empty()) { int r = q.front(); q.pop(); reached[r] = 1; for (auto &&p : G[r]) { if (reached[p] == 1) { continue; } Child[r].push_back(p); q.push(p); } } dp(0); long long int ans = (N * (1 - modinv(modpow(2, N - 1, mod), mod))) % mod; for (int r = 0; r < N; r++) { long long int tmp = 0; for (auto &&p : Child[r]) { long long int ai = Childnum[p] + 1; tmp += ai; long long int k1 = (1 - modinv(modpow(2, ai, mod), mod)) % mod; long long int k2 = modinv(modpow(2, N - 1 - ai, mod), mod); ans -= (k1 * k2) % mod; ans %= mod; } long long int ai = N - 1 - tmp; long long int k1 = (1 - modinv(modpow(2, ai, mod), mod)) % mod; long long int k2 = modinv(modpow(2, N - 1 - ai, mod), mod); ans -= (k1 * k2) % mod; ans %= mod; } cout << ((ans * modinv(2, mod)) % mod) << endl; }
replace
2
4
2
4
0
p02822
C++
Runtime Error
#include <bits/stdc++.h> #define SORT(x) sort((x).begin(), (x).end()) #define ALL(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < n; i++) #define reps(i, m, n) for (ll i = m; i < n; i++) #define repr(i, m, n) for (ll i = m; i >= n; i--) #define de(x) cout << #x << "=" << x << endl; template <class T> bool maxi(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool mini(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define dame \ { \ cout << "-1" \ << "\n"; \ return; \ } #define INF2 1000000000000000037 #define INF 1000000007 #define MOD 1000000007 #define PI (acos(-1)) using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; //--GLOBAL--------------------------------- ll modpow(ll a, ll n = MOD - 2, ll mod = MOD) { // a^n % MOD の計算 ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } vector<ll> bubunode[100010]; struct edge { ll to; }; struct graph { ll N; vector<vector<edge>> G; vector<bool> used; // vector<vector<ll>> bubunode; graph(ll n) : N(n), G(N), used(N, false) {} void add_edge(ll s, ll t) { G[s].emplace_back((edge){t}); G[t].emplace_back((edge){s}); // 無向ならON } ll dfs(ll v = 0) { used[v] = true; ll ct = 1; for (auto E : G[v]) { if (!used[E.to]) { ll x = dfs(E.to); bubunode[v].emplace_back(x); ct += x; } } ll y = N - ct; if (y > 0) bubunode[v].emplace_back(y); return ct; } }; //--MAIN----------------------------------- void Main() { ll N; cin >> N; graph g(N); rep(i, N - 1) { ll a, b; cin >> a >> b; g.add_edge(--a, --b); } g.dfs(); ll ans = 0; ll ans3 = modpow(2, N - 1) - 1; rep(i, N) { auto &ve = bubunode[i]; if (ve.size() <= 1) continue; ll ans2 = ans3; for (auto &x : ve) { ans2 -= modpow(2, x) - 1; ans2 += MOD; ans2 %= MOD; } ans += ans2; ans %= MOD; } ll gya = modpow(2, N); gya = modpow(gya); ans *= gya; ans %= MOD; cout << ans << "\n"; } //--START---------------------------------- int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); } //-----------------------------------------
#include <bits/stdc++.h> #define SORT(x) sort((x).begin(), (x).end()) #define ALL(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < n; i++) #define reps(i, m, n) for (ll i = m; i < n; i++) #define repr(i, m, n) for (ll i = m; i >= n; i--) #define de(x) cout << #x << "=" << x << endl; template <class T> bool maxi(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool mini(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define dame \ { \ cout << "-1" \ << "\n"; \ return; \ } #define INF2 1000000000000000037 #define INF 1000000007 #define MOD 1000000007 #define PI (acos(-1)) using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; //--GLOBAL--------------------------------- ll modpow(ll a, ll n = MOD - 2, ll mod = MOD) { // a^n % MOD の計算 ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } vector<ll> bubunode[200010]; struct edge { ll to; }; struct graph { ll N; vector<vector<edge>> G; vector<bool> used; // vector<vector<ll>> bubunode; graph(ll n) : N(n), G(N), used(N, false) {} void add_edge(ll s, ll t) { G[s].emplace_back((edge){t}); G[t].emplace_back((edge){s}); // 無向ならON } ll dfs(ll v = 0) { used[v] = true; ll ct = 1; for (auto E : G[v]) { if (!used[E.to]) { ll x = dfs(E.to); bubunode[v].emplace_back(x); ct += x; } } ll y = N - ct; if (y > 0) bubunode[v].emplace_back(y); return ct; } }; //--MAIN----------------------------------- void Main() { ll N; cin >> N; graph g(N); rep(i, N - 1) { ll a, b; cin >> a >> b; g.add_edge(--a, --b); } g.dfs(); ll ans = 0; ll ans3 = modpow(2, N - 1) - 1; rep(i, N) { auto &ve = bubunode[i]; if (ve.size() <= 1) continue; ll ans2 = ans3; for (auto &x : ve) { ans2 -= modpow(2, x) - 1; ans2 += MOD; ans2 %= MOD; } ans += ans2; ans %= MOD; } ll gya = modpow(2, N); gya = modpow(gya); ans *= gya; ans %= MOD; cout << ans << "\n"; } //--START---------------------------------- int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); } //-----------------------------------------
replace
46
47
46
47
0
p02822
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1e5 + 10, inf = 1e9, MOD = 1e9 + 7; int a[N], sz[N], n, ans, X; vector<int> g[N]; int Pow(int a, int m) { if (m == 0) return 1; int x = Pow(a, m / 2); return (((x * x) % MOD) * (m % 2 ? a : 1)) % MOD; } int dfs(int v, int p = 0) { sz[v] = 1; vector<int> vec; for (int u : g[v]) if (u != p) { dfs(u, v); sz[v] += sz[u]; vec.push_back(sz[u]); } vec.push_back(n - sz[v]); int val = 0; for (int i : vec) { val = (val + Pow(X, n - i - 1) - Pow(X, n - 1) + MOD) % MOD; } val = (val + Pow(X, n - 1)) % MOD; ans = (ans + X * (1 - val + MOD)) % MOD; } int32_t main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); X = Pow(2, MOD - 2); cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1); cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 2e5 + 10, inf = 1e9, MOD = 1e9 + 7; int a[N], sz[N], n, ans, X; vector<int> g[N]; int Pow(int a, int m) { if (m == 0) return 1; int x = Pow(a, m / 2); return (((x * x) % MOD) * (m % 2 ? a : 1)) % MOD; } int dfs(int v, int p = 0) { sz[v] = 1; vector<int> vec; for (int u : g[v]) if (u != p) { dfs(u, v); sz[v] += sz[u]; vec.push_back(sz[u]); } vec.push_back(n - sz[v]); int val = 0; for (int i : vec) { val = (val + Pow(X, n - i - 1) - Pow(X, n - 1) + MOD) % MOD; } val = (val + Pow(X, n - 1)) % MOD; ans = (ans + X * (1 - val + MOD)) % MOD; } int32_t main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); X = Pow(2, MOD - 2); cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1); cout << ans << "\n"; }
replace
4
5
4
5
0
p02822
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define mp make_pair #define pb push_back #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) const ll mod = 1e9 + 7; ll mu(ll a, ll b) { return (ll)a * b % mod; } ll ad(ll a, ll b) { if ((a += b) >= mod) a -= mod; return a; } ll pw(ll a, ll b, ll c = 1LL) { for (; b; b >>= 1, a = mu(a, a)) if (b & 1) c = mu(c, a); return c; } ll dv(ll a, ll b) { return mu(a, pw(b, mod - 2)); } ll ans = 0; const int n_max = 1e5; vector<int> g[n_max]; ll pwall; ll n; int dfs(int v, int p) { vector<int> kv; int sum = 1; for (auto i : g[v]) { if (i == p) continue; int k = dfs(i, v); sum += k; kv.pb(k); } kv.pb(n - sum); ll out = 1; rep(j, kv.size()) { // cout << kv[j] << endl; out = ad(out, ad(pw(2, kv[j]) - 1 + mod, 0)); } ll ok = ad(pwall - out + mod, 0); if (sum != 1) { ans = ad(ans, ok); } return sum; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; pwall = pw(2, n - 1); rep(i, n - 1) { int a, b; cin >> a >> b; --a; --b; g[a].pb(b); g[b].pb(a); } dfs(0, -1); // cout << "場合の数 " << ans << endl; cout << dv(ans, pw(2, n)) << endl; // cout << endl << endl; // cout << static_cast<double>(clock()) / CLOCKS_PER_SEC << "s"<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define mp make_pair #define pb push_back #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) const ll mod = 1e9 + 7; ll mu(ll a, ll b) { return (ll)a * b % mod; } ll ad(ll a, ll b) { if ((a += b) >= mod) a -= mod; return a; } ll pw(ll a, ll b, ll c = 1LL) { for (; b; b >>= 1, a = mu(a, a)) if (b & 1) c = mu(c, a); return c; } ll dv(ll a, ll b) { return mu(a, pw(b, mod - 2)); } ll ans = 0; const int n_max = 2e5 + 10; vector<int> g[n_max]; ll pwall; ll n; int dfs(int v, int p) { vector<int> kv; int sum = 1; for (auto i : g[v]) { if (i == p) continue; int k = dfs(i, v); sum += k; kv.pb(k); } kv.pb(n - sum); ll out = 1; rep(j, kv.size()) { // cout << kv[j] << endl; out = ad(out, ad(pw(2, kv[j]) - 1 + mod, 0)); } ll ok = ad(pwall - out + mod, 0); if (sum != 1) { ans = ad(ans, ok); } return sum; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; pwall = pw(2, n - 1); rep(i, n - 1) { int a, b; cin >> a >> b; --a; --b; g[a].pb(b); g[b].pb(a); } dfs(0, -1); // cout << "場合の数 " << ans << endl; cout << dv(ans, pw(2, n)) << endl; // cout << endl << endl; // cout << static_cast<double>(clock()) / CLOCKS_PER_SEC << "s"<<endl; return 0; }
replace
24
25
24
25
0
p02822
C++
Runtime Error
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; #define MOD 1000000007 #define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++) #define REP(i, n) rep(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; const long long INF = 1LL << 60; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string &s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge( from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph &G, ll s, ll t) // O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = INF; } } REP(i, G.size()) { REP(j, G[i].size()) { d[i][G[i][j].to] = G[i][j].cap; } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, vector<int> &order) { // トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; void visit(const Graph &g, int v, Matrix &scc, stack<ll> &S, Array &inS, Array &low, Array &num, int &time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; FOR(e, g[v]) { int w = e->to; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(Array()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const Graph &g, Matrix &scc) { // 強連結成分分解 O(E+V) const int n = g.size(); Array num(n), low(n); stack<ll> S; Array inS(n); int time = 0; REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { // xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { // xの集合のサイズを返す return -data[root(x)]; } int numSet() { // 集合の数を返す return num; } }; class SumSegTree { private: ll _sum(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return 0; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return s1 + s2; } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) SumSegTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, 0); } // 場所i(0-indexed)にxを足す void add(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] += x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] += x; } } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: ll _find(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return INF; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return min(s1, s2); } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) RmqTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, INF); } // 場所i(0-indexed)をxにする void update(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] = x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } // 区間[a,b)の最小値。ノードk=[l,r)に着目している。 ll find(ll a, ll b) { return _find(a, b, 0, 0, n); } }; // 約数求める //約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void divisor_prime(ll n, vector<P> &ret) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({i, 0}); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1) ret.push_back({n, 1}); } vector<ll> lis_fast(const vector<ll> &a) { // 最長部分増加列 const ll n = a.size(); vector<ll> A(n, INT_MAX); vector<ll> id(n); for (int i = 0; i < n; ++i) { id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i])); A[id[i]] = a[i]; } ll m = *max_element(id.begin(), id.end()); vector<ll> b(m + 1); for (int i = n - 1; i >= 0; --i) if (id[i] == m) b[m--] = a[i]; return b; } bool z_algorithm(string &str, vector<int> &z, ll s) { // s&tを渡してtにsが含まれるかを返す const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } if (z[i] == s) return true; } return false; } bool z_algorithm(string &str, vector<int> &z) { // z[i]==|s|のときstr[i]からsが含まれる const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } } return true; } // ローリングハッシュ // 二分探索で LCP を求める機能つき struct RollingHash { static const int base1 = 1007, base2 = 2009; static const int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; // construct RollingHash(const string &S) { int n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } // get hash of S[left:right] inline pair<long long, long long> get(int l, int r) const { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } // get lcp of S[a:] and T[b:] inline int getLCP(int a, int b) const { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } }; ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } // nCrとか class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll nCr(ll n, ll r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll n, ll _mod) { mod = _mod; fact.resize(n + 1); fact[0] = 1; REP(i, n) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n + 1); inv[n] = mod_inv(fact[n]); for (int i = n; i > 0; i--) { inv[i - 1] = inv[i] * i % mod; } } }; ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } // gcd ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } Matrix mIdentity(ll n) { Matrix A(n, Array(n)); for (int i = 0; i < n; ++i) A[i][i] = 1; return A; } Matrix mMul(const Matrix &A, const Matrix &B) { Matrix C(A.size(), Array(B[0].size())); for (int i = 0; i < C.size(); ++i) for (int j = 0; j < C[i].size(); ++j) for (int k = 0; k < A[i].size(); ++k) (C[i][j] += (A[i][k] % MOD) * (B[k][j] % MOD)) %= MOD; return C; } // O( n^3 log e ) Matrix mPow(const Matrix &A, ll e) { return e == 0 ? mIdentity(A.size()) : e % 2 == 0 ? mPow(mMul(A, A), e / 2) : mMul(A, mPow(A, e - 1)); } template <class T> class RectangleSum { public: vector<vector<T>> sum; T GetSum(int left, int right, int top, int bottom) { //[left, right], [top, bottom] T res = sum[bottom][right]; if (left > 0) res -= sum[bottom][left - 1]; if (top > 0) res -= sum[top - 1][right]; if (left > 0 && top > 0) res += sum[top - 1][left - 1]; return res; } RectangleSum(const vector<vector<T>> &s, int h, int w) { sum.resize(h); for (int i = 0; i < h; i++) sum[i].resize(w, 0); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { sum[y][x] = s[y][x]; if (y > 0) sum[y][x] += sum[y - 1][x]; if (x > 0) sum[y][x] += sum[y][x - 1]; if (y > 0 && x > 0) sum[y][x] -= sum[y - 1][x - 1]; } } } }; // NTT ll _garner(Array &xs, Array &mods) { int M = xs.size(); Array coeffs(M, 1), constants(M, 0); for (int i = 0; i < M - 1; ++i) { ll mod_i = mods[i]; // coffs[i] * v + constants[i] == mr[i].val (mod mr[i].first) を解く ll v = (xs[i] - constants[i] + mod_i) % mod_i; v = (v * mod_pow(coeffs[i], mod_i - 2, mod_i)) % mod_i; for (int j = i + 1; j < M; j++) { ll mod_j = mods[j]; constants[j] = (constants[j] + coeffs[j] * v) % mod_j; coeffs[j] = (coeffs[j] * mod_i) % mod_j; } } return constants.back(); } template <typename T> inline void bit_reverse(vector<T> &a) { int n = a.size(); int i = 0; for (int j = 1; j < n - 1; ++j) { for (int k = n >> 1; k > (i ^= k); k >>= 1) ; if (j < i) swap(a[i], a[j]); } } template <long long mod, long long primitive_root> class NTT { public: long long get_mod() { return mod; } void _ntt(vector<long long> &a, int sign) { const int n = a.size(); assert((n ^ (n & -n)) == 0); // n = 2^k const long long g = primitive_root; // g is primitive root of mod long long tmp = (mod - 1) * mod_pow((ll)n, mod - 2, mod) % mod; // -1/n long long h = mod_pow(g, tmp, mod); // ^n√g if (sign == -1) h = mod_pow(h, mod - 2, mod); bit_reverse(a); for (int m = 1; m < n; m <<= 1) { const int m2 = 2 * m; long long _base = mod_pow((ll)h, (ll)(n / m2), mod); long long _w = 1; for (int x = 0; x < m; ++x) { for (int s = x; s < n; s += m2) { long long u = a[s]; long long d = (a[s + m] * _w) % mod; a[s] = (u + d) % mod; a[s + m] = (u - d + mod) % mod; } _w = (_w * _base) % mod; } } } void ntt(vector<long long> &input) { _ntt(input, 1); } void intt(vector<long long> &input) { _ntt(input, -1); const long long n_inv = mod_pow((ll)input.size(), mod - 2, mod); for (auto &x : input) x = (x * n_inv) % mod; } // 畳み込み演算を行う vector<long long> convolution(const vector<long long> &a, const vector<long long> &b) { int result_size = a.size() + b.size() - 1; int n = 1; while (n < result_size) n <<= 1; vector<long long> _a = a, _b = b; _a.resize(n, 0); _b.resize(n, 0); ntt(_a); ntt(_b); for (int i = 0; i < n; ++i) _a[i] = (_a[i] * _b[i]) % mod; intt(_a); _a.resize(result_size); return _a; } }; vector<long long> convolution_ntt(vector<long long> &a, vector<long long> &b, long long mod = 1224736769LL) { for (auto &x : a) x %= mod; for (auto &x : b) x %= mod; ll maxval = max(a.size(), b.size()) * *max_element(a.begin(), a.end()) * *max_element(b.begin(), b.end()); if (maxval < 1224736769) { NTT<1224736769, 3> ntt3; return ntt3.convolution(a, b); } NTT<167772161, 3> ntt1; NTT<469762049, 3> ntt2; NTT<1224736769, 3> ntt3; vector<long long> x1 = ntt1.convolution(a, b); vector<long long> x2 = ntt2.convolution(a, b); vector<long long> x3 = ntt3.convolution(a, b); vector<long long> ret(x1.size()); vector<long long> mods{167772161, 469762049, 1224736769, mod}; for (int i = 0; i < x1.size(); ++i) { vector<long long> xs{x1[i], x2[i], x3[i], 0}; ret[i] = _garner(xs, mods); } return ret; } int popcount3(int x) { x = (x & 0x55555555) + (x >> 1 & 0x55555555); x = (x & 0x33333333) + (x >> 2 & 0x33333333); x = (x & 0x0F0F0F0F) + (x >> 4 & 0x0F0F0F0F); x = (x & 0x00FF00FF) + (x >> 8 & 0x00FF00FF); x = (x & 0x0000FFFF) + (x >> 16 & 0x0000FFFF); return x; } template <typename T> void rowReduction(vector<T> mat, vector<T> &basis) { // 掃き出し法 for (auto e : mat) { for (auto b : basis) chmin(e, e ^ b); if (e) basis.push_back(e); } sort(all(basis), greater<T>()); } /* const ll mod = 998244353; class SegmentTree { private: int n; Array node[2]; public: SegmentTree(Array &v) { int sz = v.size(); n = 1; while (n < sz)n *= 2; node[0].resize(2*n-1,1); node[1].resize(2*n-1,1); REP(i, sz) { node[0][i + n - 1] = mod_inv(v[i],mod) * 100 % mod; node[1][i + n - 1] = node[0][i + n - 1]; } for (int i = n - 2; i >= 0; i--) { node[0][i] = node[0][i * 2 + 1] * node[0][i * 2 + 2] % mod; node[1][i] = (node[1][i * 2 + 1] * node[0][i * 2 + 2] + node[1][i * 2 + 2]) % mod; } } P getSum(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0)r = n; if (r <= a || b <= l)return { 1,0 }; if (a <= l && r <= b)return { node[0][k],node[1][k] }; P vl = getSum(a, b, 2 * k + 1, l, (l + r) / 2); P vr = getSum(a, b, 2 * k + 2, (l + r) / 2, r); P ret = { vl.first * vr.first % mod ,(vl.second * vr.first + vr.second) % mod }; return ret; } }; */ /* string s, t; cin >> s >> t; REP(i, 210)REP(j, 210)REP(k, 210)dp[i][j][k] = INF; s.push_back('_'); t.push_back('_'); dp[0][0][0] = 0; REP(i, s.size()+1) { REP(j, t.size() + 1) { REP(k, 201) { if (dp[i][j][k] == INF)continue; ll nextS = i; ll nextT = j; if (s[i] == '(')nextS++; if (t[j] == '(')nextT++; chmin(dp[nextS][nextT][k + 1], dp[i][j][k] + 1); if (k != 0) { } } } } */ ll two_inv[101010]; P dfs(Graph &graph, ll v, ll par) { P ret = {0, 1}; Array kid; REP(i, graph[v].size()) { ll to = graph[v][i].to; if (to != par) { P temp = dfs(graph, to, v); ret.second += temp.second; (ret.first += temp.first) %= MOD; kid.push_back(temp.second); } } if (ret.second != graph.size()) kid.push_back(graph.size() - ret.second); if (kid.size() < 2) return ret; ll temp = (1 + MOD - two_inv[graph.size() - 1]) % MOD; REP(i, kid.size()) { ll a = two_inv[graph.size() - 1]; (a *= mod_inv(two_inv[kid[i]], MOD)) %= MOD; a *= (1 + MOD - two_inv[kid[i]]) % MOD; a %= MOD; (temp += MOD - a) %= MOD; } (temp *= mod_inv(2, MOD)) %= MOD; (ret.first += temp) %= MOD; return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; Graph graph(n); REP(i, n - 1) { ll a, b; cin >> a >> b; a--; b--; add_edge(graph, a, b, 1, true, 1); } two_inv[0] = 1; REP(i, n + 1) { two_inv[i + 1] = two_inv[i] * mod_inv(2, MOD) % MOD; } REP(i, n) { if (graph[i].size() == 1) { cout << dfs(graph, i, -1).first << endl; return 0; } } return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; #define MOD 1000000007 #define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++) #define REP(i, n) rep(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; const long long INF = 1LL << 60; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string &s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge( from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph &G, ll s, ll t) // O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = INF; } } REP(i, G.size()) { REP(j, G[i].size()) { d[i][G[i][j].to] = G[i][j].cap; } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, vector<int> &order) { // トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; void visit(const Graph &g, int v, Matrix &scc, stack<ll> &S, Array &inS, Array &low, Array &num, int &time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; FOR(e, g[v]) { int w = e->to; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(Array()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const Graph &g, Matrix &scc) { // 強連結成分分解 O(E+V) const int n = g.size(); Array num(n), low(n); stack<ll> S; Array inS(n); int time = 0; REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { // xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { // xの集合のサイズを返す return -data[root(x)]; } int numSet() { // 集合の数を返す return num; } }; class SumSegTree { private: ll _sum(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return 0; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return s1 + s2; } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) SumSegTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, 0); } // 場所i(0-indexed)にxを足す void add(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] += x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] += x; } } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: ll _find(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return INF; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return min(s1, s2); } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) RmqTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, INF); } // 場所i(0-indexed)をxにする void update(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] = x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } // 区間[a,b)の最小値。ノードk=[l,r)に着目している。 ll find(ll a, ll b) { return _find(a, b, 0, 0, n); } }; // 約数求める //約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void divisor_prime(ll n, vector<P> &ret) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({i, 0}); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1) ret.push_back({n, 1}); } vector<ll> lis_fast(const vector<ll> &a) { // 最長部分増加列 const ll n = a.size(); vector<ll> A(n, INT_MAX); vector<ll> id(n); for (int i = 0; i < n; ++i) { id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i])); A[id[i]] = a[i]; } ll m = *max_element(id.begin(), id.end()); vector<ll> b(m + 1); for (int i = n - 1; i >= 0; --i) if (id[i] == m) b[m--] = a[i]; return b; } bool z_algorithm(string &str, vector<int> &z, ll s) { // s&tを渡してtにsが含まれるかを返す const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } if (z[i] == s) return true; } return false; } bool z_algorithm(string &str, vector<int> &z) { // z[i]==|s|のときstr[i]からsが含まれる const int L = str.size(); z.resize(str.size()); for (int i = 1, left = 0, right = 0; i < L; i++) { if (i > right) { left = right = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } else { int k = i - left; if (z[k] < right - i + 1) { z[i] = z[k]; } else { left = i; for (; right < L && str[right - left] == str[right]; right++) ; z[i] = right - left; right--; } } } return true; } // ローリングハッシュ // 二分探索で LCP を求める機能つき struct RollingHash { static const int base1 = 1007, base2 = 2009; static const int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; // construct RollingHash(const string &S) { int n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } // get hash of S[left:right] inline pair<long long, long long> get(int l, int r) const { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } // get lcp of S[a:] and T[b:] inline int getLCP(int a, int b) const { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } }; ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } // nCrとか class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll nCr(ll n, ll r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll n, ll _mod) { mod = _mod; fact.resize(n + 1); fact[0] = 1; REP(i, n) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n + 1); inv[n] = mod_inv(fact[n]); for (int i = n; i > 0; i--) { inv[i - 1] = inv[i] * i % mod; } } }; ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } // gcd ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } Matrix mIdentity(ll n) { Matrix A(n, Array(n)); for (int i = 0; i < n; ++i) A[i][i] = 1; return A; } Matrix mMul(const Matrix &A, const Matrix &B) { Matrix C(A.size(), Array(B[0].size())); for (int i = 0; i < C.size(); ++i) for (int j = 0; j < C[i].size(); ++j) for (int k = 0; k < A[i].size(); ++k) (C[i][j] += (A[i][k] % MOD) * (B[k][j] % MOD)) %= MOD; return C; } // O( n^3 log e ) Matrix mPow(const Matrix &A, ll e) { return e == 0 ? mIdentity(A.size()) : e % 2 == 0 ? mPow(mMul(A, A), e / 2) : mMul(A, mPow(A, e - 1)); } template <class T> class RectangleSum { public: vector<vector<T>> sum; T GetSum(int left, int right, int top, int bottom) { //[left, right], [top, bottom] T res = sum[bottom][right]; if (left > 0) res -= sum[bottom][left - 1]; if (top > 0) res -= sum[top - 1][right]; if (left > 0 && top > 0) res += sum[top - 1][left - 1]; return res; } RectangleSum(const vector<vector<T>> &s, int h, int w) { sum.resize(h); for (int i = 0; i < h; i++) sum[i].resize(w, 0); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { sum[y][x] = s[y][x]; if (y > 0) sum[y][x] += sum[y - 1][x]; if (x > 0) sum[y][x] += sum[y][x - 1]; if (y > 0 && x > 0) sum[y][x] -= sum[y - 1][x - 1]; } } } }; // NTT ll _garner(Array &xs, Array &mods) { int M = xs.size(); Array coeffs(M, 1), constants(M, 0); for (int i = 0; i < M - 1; ++i) { ll mod_i = mods[i]; // coffs[i] * v + constants[i] == mr[i].val (mod mr[i].first) を解く ll v = (xs[i] - constants[i] + mod_i) % mod_i; v = (v * mod_pow(coeffs[i], mod_i - 2, mod_i)) % mod_i; for (int j = i + 1; j < M; j++) { ll mod_j = mods[j]; constants[j] = (constants[j] + coeffs[j] * v) % mod_j; coeffs[j] = (coeffs[j] * mod_i) % mod_j; } } return constants.back(); } template <typename T> inline void bit_reverse(vector<T> &a) { int n = a.size(); int i = 0; for (int j = 1; j < n - 1; ++j) { for (int k = n >> 1; k > (i ^= k); k >>= 1) ; if (j < i) swap(a[i], a[j]); } } template <long long mod, long long primitive_root> class NTT { public: long long get_mod() { return mod; } void _ntt(vector<long long> &a, int sign) { const int n = a.size(); assert((n ^ (n & -n)) == 0); // n = 2^k const long long g = primitive_root; // g is primitive root of mod long long tmp = (mod - 1) * mod_pow((ll)n, mod - 2, mod) % mod; // -1/n long long h = mod_pow(g, tmp, mod); // ^n√g if (sign == -1) h = mod_pow(h, mod - 2, mod); bit_reverse(a); for (int m = 1; m < n; m <<= 1) { const int m2 = 2 * m; long long _base = mod_pow((ll)h, (ll)(n / m2), mod); long long _w = 1; for (int x = 0; x < m; ++x) { for (int s = x; s < n; s += m2) { long long u = a[s]; long long d = (a[s + m] * _w) % mod; a[s] = (u + d) % mod; a[s + m] = (u - d + mod) % mod; } _w = (_w * _base) % mod; } } } void ntt(vector<long long> &input) { _ntt(input, 1); } void intt(vector<long long> &input) { _ntt(input, -1); const long long n_inv = mod_pow((ll)input.size(), mod - 2, mod); for (auto &x : input) x = (x * n_inv) % mod; } // 畳み込み演算を行う vector<long long> convolution(const vector<long long> &a, const vector<long long> &b) { int result_size = a.size() + b.size() - 1; int n = 1; while (n < result_size) n <<= 1; vector<long long> _a = a, _b = b; _a.resize(n, 0); _b.resize(n, 0); ntt(_a); ntt(_b); for (int i = 0; i < n; ++i) _a[i] = (_a[i] * _b[i]) % mod; intt(_a); _a.resize(result_size); return _a; } }; vector<long long> convolution_ntt(vector<long long> &a, vector<long long> &b, long long mod = 1224736769LL) { for (auto &x : a) x %= mod; for (auto &x : b) x %= mod; ll maxval = max(a.size(), b.size()) * *max_element(a.begin(), a.end()) * *max_element(b.begin(), b.end()); if (maxval < 1224736769) { NTT<1224736769, 3> ntt3; return ntt3.convolution(a, b); } NTT<167772161, 3> ntt1; NTT<469762049, 3> ntt2; NTT<1224736769, 3> ntt3; vector<long long> x1 = ntt1.convolution(a, b); vector<long long> x2 = ntt2.convolution(a, b); vector<long long> x3 = ntt3.convolution(a, b); vector<long long> ret(x1.size()); vector<long long> mods{167772161, 469762049, 1224736769, mod}; for (int i = 0; i < x1.size(); ++i) { vector<long long> xs{x1[i], x2[i], x3[i], 0}; ret[i] = _garner(xs, mods); } return ret; } int popcount3(int x) { x = (x & 0x55555555) + (x >> 1 & 0x55555555); x = (x & 0x33333333) + (x >> 2 & 0x33333333); x = (x & 0x0F0F0F0F) + (x >> 4 & 0x0F0F0F0F); x = (x & 0x00FF00FF) + (x >> 8 & 0x00FF00FF); x = (x & 0x0000FFFF) + (x >> 16 & 0x0000FFFF); return x; } template <typename T> void rowReduction(vector<T> mat, vector<T> &basis) { // 掃き出し法 for (auto e : mat) { for (auto b : basis) chmin(e, e ^ b); if (e) basis.push_back(e); } sort(all(basis), greater<T>()); } /* const ll mod = 998244353; class SegmentTree { private: int n; Array node[2]; public: SegmentTree(Array &v) { int sz = v.size(); n = 1; while (n < sz)n *= 2; node[0].resize(2*n-1,1); node[1].resize(2*n-1,1); REP(i, sz) { node[0][i + n - 1] = mod_inv(v[i],mod) * 100 % mod; node[1][i + n - 1] = node[0][i + n - 1]; } for (int i = n - 2; i >= 0; i--) { node[0][i] = node[0][i * 2 + 1] * node[0][i * 2 + 2] % mod; node[1][i] = (node[1][i * 2 + 1] * node[0][i * 2 + 2] + node[1][i * 2 + 2]) % mod; } } P getSum(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0)r = n; if (r <= a || b <= l)return { 1,0 }; if (a <= l && r <= b)return { node[0][k],node[1][k] }; P vl = getSum(a, b, 2 * k + 1, l, (l + r) / 2); P vr = getSum(a, b, 2 * k + 2, (l + r) / 2, r); P ret = { vl.first * vr.first % mod ,(vl.second * vr.first + vr.second) % mod }; return ret; } }; */ /* string s, t; cin >> s >> t; REP(i, 210)REP(j, 210)REP(k, 210)dp[i][j][k] = INF; s.push_back('_'); t.push_back('_'); dp[0][0][0] = 0; REP(i, s.size()+1) { REP(j, t.size() + 1) { REP(k, 201) { if (dp[i][j][k] == INF)continue; ll nextS = i; ll nextT = j; if (s[i] == '(')nextS++; if (t[j] == '(')nextT++; chmin(dp[nextS][nextT][k + 1], dp[i][j][k] + 1); if (k != 0) { } } } } */ ll two_inv[201010]; P dfs(Graph &graph, ll v, ll par) { P ret = {0, 1}; Array kid; REP(i, graph[v].size()) { ll to = graph[v][i].to; if (to != par) { P temp = dfs(graph, to, v); ret.second += temp.second; (ret.first += temp.first) %= MOD; kid.push_back(temp.second); } } if (ret.second != graph.size()) kid.push_back(graph.size() - ret.second); if (kid.size() < 2) return ret; ll temp = (1 + MOD - two_inv[graph.size() - 1]) % MOD; REP(i, kid.size()) { ll a = two_inv[graph.size() - 1]; (a *= mod_inv(two_inv[kid[i]], MOD)) %= MOD; a *= (1 + MOD - two_inv[kid[i]]) % MOD; a %= MOD; (temp += MOD - a) %= MOD; } (temp *= mod_inv(2, MOD)) %= MOD; (ret.first += temp) %= MOD; return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; Graph graph(n); REP(i, n - 1) { ll a, b; cin >> a >> b; a--; b--; add_edge(graph, a, b, 1, true, 1); } two_inv[0] = 1; REP(i, n + 1) { two_inv[i + 1] = two_inv[i] * mod_inv(2, MOD) % MOD; } REP(i, n) { if (graph[i].size() == 1) { cout << dfs(graph, i, -1).first << endl; return 0; } } return 0; }
replace
853
854
853
854
0
p02822
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; class ModInt { public: static int _BM; long long int m_value; ModInt() { m_value = 0; } ModInt(int v) { m_value = (long long int)v; } ModInt(const ModInt &t) { m_value = t.getint(); } int operator+(const ModInt &t) { return ModInt::modadd(getint(), t.getint()); } int operator+(int t) { return ModInt::modadd(getint(), t); } int operator-(const ModInt &t) { return ModInt::modminus(getint(), t.getint()); } int operator-(int t) { return ModInt::modminus(getint(), t); } int operator*(const ModInt &t) { return ModInt::modmul(getint(), t.getint()); } int operator*(int t) { return ModInt::modmul(getint(), t); } ModInt &operator+=(const ModInt &t) { (*this) += t.getint(); return (*this); } ModInt &operator*=(const ModInt &t) { (*this) *= t.getint(); return (*this); } ModInt &operator-=(const ModInt &t) { (*this) -= t.getint(); return (*this); } ModInt &operator+=(int v) { m_value += v; if (m_value >= _BM) m_value -= _BM; return *this; } ModInt &operator-=(int v) { m_value -= v; if (m_value < 0) m_value += _BM; return *this; } ModInt &operator*=(int v) { m_value *= v; m_value %= _BM; return *this; } static int modadd(int a, int b) { ModInt t(a); t += b; return t.getint(); } static int modminus(int a, int b) { ModInt t(a); t -= b; return t.getint(); } static int modmul(int a, int b) { ModInt t(a); t *= b; return t.getint(); } // a^b; static int modpow(int a, int b) { ModInt ans(1); int heap[32]; heap[0] = a; for (int bit = 0; b >= (1 << bit); bit++) { if (bit > 0) heap[bit] = modmul(heap[bit - 1], heap[bit - 1]); if (b & (1 << bit)) { ans *= heap[bit]; } } return ans.getint(); } static int reciprocal(int a) { return modpow(a, _BM - 2); } static int combination(int n, int r) { int *fac = (int *)malloc(sizeof(int) * (n + 1)); int *facinv = (int *)malloc(sizeof(int) * (n + 1)); fac[0] = 1; for (int k = 1; k <= n; k++) fac[k] = modmul(k, fac[k - 1]); facinv[n] = reciprocal(fac[n]); for (int k = n; k > 0; k--) facinv[k - 1] = modmul(k, facinv[k]); ModInt ans(fac[n]); ans *= facinv[r]; ans *= facinv[n - r]; free(fac); free(facinv); return ans.getint(); } int getint() const { return (int)m_value; } private: void _mod() { if (m_value < 0) m_value %= _BM; } }; int ModInt::_BM = 1000000007; typedef struct { int parent; vector<int> children; int childtotal; } NODE; NODE nodes[20000]; vector<int> connection[20000]; int countnode(int index, int parent) { int count = 0; nodes[index].parent = parent; for (int j = 0; j < (int)connection[index].size(); j++) { if (connection[index][j] != parent) { int cc = countnode(connection[index][j], index); nodes[index].children.push_back(connection[index][j]); count += cc; } } nodes[index].childtotal = count + 1; return count + 1; } int checktree(int index, int total) { ModInt ans = 0; for (int i = 0; i < (int)nodes[index].children.size(); i++) { ans += checktree(nodes[index].children[i], total); } ModInt p0 = ModInt::reciprocal(ModInt::modpow(2, total - 1)); ModInt ng = p0; if ((int)nodes[index].children.size() == 0 || ((int)nodes[index].children.size() == 1 && nodes[index].parent < 0)) return ans.getint(); for (int i = 0; i < (int)nodes[index].children.size(); i++) { ModInt p1 = 1; p1 -= ModInt::reciprocal( ModInt::modpow(2, nodes[nodes[index].children[i]].childtotal)); p1 *= ModInt::reciprocal(ModInt::modpow( 2, total - 1 - nodes[nodes[index].children[i]].childtotal)); ng += p1; } if (nodes[index].parent >= 0) { ModInt p1 = ModInt::reciprocal(ModInt::modpow(2, nodes[index].childtotal - 1)); p1 -= p0; ng += p1; } ModInt ok = 1; ok -= ng; ok *= ModInt::reciprocal(2); ans += ok; return ans.getint(); } int main() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a; cin >> b; a--; b--; connection[a].push_back(b); connection[b].push_back(a); } countnode(0, -1); cout << checktree(0, n); }
#include <bits/stdc++.h> #include <iostream> using namespace std; class ModInt { public: static int _BM; long long int m_value; ModInt() { m_value = 0; } ModInt(int v) { m_value = (long long int)v; } ModInt(const ModInt &t) { m_value = t.getint(); } int operator+(const ModInt &t) { return ModInt::modadd(getint(), t.getint()); } int operator+(int t) { return ModInt::modadd(getint(), t); } int operator-(const ModInt &t) { return ModInt::modminus(getint(), t.getint()); } int operator-(int t) { return ModInt::modminus(getint(), t); } int operator*(const ModInt &t) { return ModInt::modmul(getint(), t.getint()); } int operator*(int t) { return ModInt::modmul(getint(), t); } ModInt &operator+=(const ModInt &t) { (*this) += t.getint(); return (*this); } ModInt &operator*=(const ModInt &t) { (*this) *= t.getint(); return (*this); } ModInt &operator-=(const ModInt &t) { (*this) -= t.getint(); return (*this); } ModInt &operator+=(int v) { m_value += v; if (m_value >= _BM) m_value -= _BM; return *this; } ModInt &operator-=(int v) { m_value -= v; if (m_value < 0) m_value += _BM; return *this; } ModInt &operator*=(int v) { m_value *= v; m_value %= _BM; return *this; } static int modadd(int a, int b) { ModInt t(a); t += b; return t.getint(); } static int modminus(int a, int b) { ModInt t(a); t -= b; return t.getint(); } static int modmul(int a, int b) { ModInt t(a); t *= b; return t.getint(); } // a^b; static int modpow(int a, int b) { ModInt ans(1); int heap[32]; heap[0] = a; for (int bit = 0; b >= (1 << bit); bit++) { if (bit > 0) heap[bit] = modmul(heap[bit - 1], heap[bit - 1]); if (b & (1 << bit)) { ans *= heap[bit]; } } return ans.getint(); } static int reciprocal(int a) { return modpow(a, _BM - 2); } static int combination(int n, int r) { int *fac = (int *)malloc(sizeof(int) * (n + 1)); int *facinv = (int *)malloc(sizeof(int) * (n + 1)); fac[0] = 1; for (int k = 1; k <= n; k++) fac[k] = modmul(k, fac[k - 1]); facinv[n] = reciprocal(fac[n]); for (int k = n; k > 0; k--) facinv[k - 1] = modmul(k, facinv[k]); ModInt ans(fac[n]); ans *= facinv[r]; ans *= facinv[n - r]; free(fac); free(facinv); return ans.getint(); } int getint() const { return (int)m_value; } private: void _mod() { if (m_value < 0) m_value %= _BM; } }; int ModInt::_BM = 1000000007; typedef struct { int parent; vector<int> children; int childtotal; } NODE; NODE nodes[200004]; vector<int> connection[200004]; int countnode(int index, int parent) { int count = 0; nodes[index].parent = parent; for (int j = 0; j < (int)connection[index].size(); j++) { if (connection[index][j] != parent) { int cc = countnode(connection[index][j], index); nodes[index].children.push_back(connection[index][j]); count += cc; } } nodes[index].childtotal = count + 1; return count + 1; } int checktree(int index, int total) { ModInt ans = 0; for (int i = 0; i < (int)nodes[index].children.size(); i++) { ans += checktree(nodes[index].children[i], total); } ModInt p0 = ModInt::reciprocal(ModInt::modpow(2, total - 1)); ModInt ng = p0; if ((int)nodes[index].children.size() == 0 || ((int)nodes[index].children.size() == 1 && nodes[index].parent < 0)) return ans.getint(); for (int i = 0; i < (int)nodes[index].children.size(); i++) { ModInt p1 = 1; p1 -= ModInt::reciprocal( ModInt::modpow(2, nodes[nodes[index].children[i]].childtotal)); p1 *= ModInt::reciprocal(ModInt::modpow( 2, total - 1 - nodes[nodes[index].children[i]].childtotal)); ng += p1; } if (nodes[index].parent >= 0) { ModInt p1 = ModInt::reciprocal(ModInt::modpow(2, nodes[index].childtotal - 1)); p1 -= p0; ng += p1; } ModInt ok = 1; ok -= ng; ok *= ModInt::reciprocal(2); ans += ok; return ans.getint(); } int main() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a; cin >> b; a--; b--; connection[a].push_back(b); connection[b].push_back(a); } countnode(0, -1); cout << checktree(0, n); }
replace
136
138
136
138
0
p02822
C++
Runtime Error
#include <algorithm> #include <iostream> #include <stdio.h> #include <vector> using namespace std; typedef long long LL; const int M = 1e5 + 5; const int MO = 1e9 + 7; int add(int x, int y) { x += y; if (x >= MO) x -= MO; return x; } int sub(int x, int y) { x -= y; if (x < 0) x += MO; return x; } int mul(int x, int y) { return 1LL * x * y % MO; } int qp(int a, int n) { int ans = 1; while (n) { if (n & 1) ans = mul(ans, a); a = mul(a, a); n >>= 1; } return ans; } vector<int> g[M]; int sz[M]; int dfs1(int u, int fa) { sz[u] = 1; for (int v : g[u]) { if (v == fa) continue; sz[u] += dfs1(v, u); } return sz[u]; } int ans = 0; int n; void dfs2(int u, int fa) { ans = add(ans, sub(qp(2, n - 1), 1)); if (fa) ans = sub(ans, sub(qp(2, n - sz[u]), 1)); for (int v : g[u]) { if (v == fa) continue; ans = sub(ans, sub(qp(2, sz[v]), 1)); dfs2(v, u); } } int main() { scanf("%d", &n); int u, v; for (int i = 0; i < n - 1; i++) { scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs1(1, 0); dfs2(1, 0); printf("%d\n", mul(ans, qp(qp(2, n), MO - 2))); return 0; }
#include <algorithm> #include <iostream> #include <stdio.h> #include <vector> using namespace std; typedef long long LL; const int M = 2e5 + 5; const int MO = 1e9 + 7; int add(int x, int y) { x += y; if (x >= MO) x -= MO; return x; } int sub(int x, int y) { x -= y; if (x < 0) x += MO; return x; } int mul(int x, int y) { return 1LL * x * y % MO; } int qp(int a, int n) { int ans = 1; while (n) { if (n & 1) ans = mul(ans, a); a = mul(a, a); n >>= 1; } return ans; } vector<int> g[M]; int sz[M]; int dfs1(int u, int fa) { sz[u] = 1; for (int v : g[u]) { if (v == fa) continue; sz[u] += dfs1(v, u); } return sz[u]; } int ans = 0; int n; void dfs2(int u, int fa) { ans = add(ans, sub(qp(2, n - 1), 1)); if (fa) ans = sub(ans, sub(qp(2, n - sz[u]), 1)); for (int v : g[u]) { if (v == fa) continue; ans = sub(ans, sub(qp(2, sz[v]), 1)); dfs2(v, u); } } int main() { scanf("%d", &n); int u, v; for (int i = 0; i < n - 1; i++) { scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs1(1, 0); dfs2(1, 0); printf("%d\n", mul(ans, qp(qp(2, n), MO - 2))); return 0; }
replace
7
8
7
8
0
p02823
C++
Runtime Error
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define REP(i, a, b) for (int i = a; i < (b); ++i) #define all(x) (x).begin(), (x).end() const int INF = 1000000007; typedef long long ll; using namespace std; 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; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } ll fac(ll a) { return a > 1 ? fac(a - 1) * a : 1; } int main() { // ios::sync_with_stdio(false);cin.tie(nullptr); ll n, a, b; cin >> n >> a >> b; ll A, B, C, D; if (abs(b - a) % 2 == 0) { A = (b - a) / 2; cout << A << endl; } else { return -1; } return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define REP(i, a, b) for (int i = a; i < (b); ++i) #define all(x) (x).begin(), (x).end() const int INF = 1000000007; typedef long long ll; using namespace std; 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; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } ll fac(ll a) { return a > 1 ? fac(a - 1) * a : 1; } int main() { // ios::sync_with_stdio(false);cin.tie(nullptr); ll n, a, b; cin >> n >> a >> b; ll A, B, C, D; if (abs(b - a) % 2 == 0) { A = (b - a) / 2; cout << A << endl; } else { A = max(abs(n - b), abs(n - a)); B = max(abs(1 - b), abs(1 - a)); ll aa = a; ll bb = b; C += abs(1 - a); bb -= C; C++; bb--; if (bb != 1) C += abs(1 - bb) / 2; D += abs(n - b); aa += D; D++; a++; if (aa != n) D += abs(n - aa) / 2; cout << min({A, B, C, D}) << endl; } return 0; }
replace
34
35
34
52
0
p02823
C++
Time Limit Exceeded
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; typedef long long ll; int main() { ll n, a, b; cin >> n >> a >> b; ll ans = 0; if ((b - a) % 2 == 0) { ans = (b - a) / 2; } else { while ((b - a) % 2 != 0) { ans++; b--; a--; a = a > 0 ? a : 1; if ((b - a) % 2 == 0) ans += (b - a) / 2; } } cout << ans << endl; }
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; typedef long long ll; int main() { ll n, a, b; cin >> n >> a >> b; ll ans = 0; if ((b - a) % 2 == 0) { ans = (b - a) / 2; } else { ans = min(n - b, a - 1) + 1 + (b - a - 1) / 2; } cout << ans << endl; }
replace
20
28
20
21
TLE
p02823
C++
Time Limit Exceeded
/******************************************************************************** Code by a weak man who named CYJian, and he hope the code can get more points. Algorithm: ********************************************************************************/ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int __SIZE = 1 << 18; char ibuf[__SIZE], *iS, *iT; #define ge \ (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), \ (iS == iT ? EOF : *iS++)) \ : *iS++) #define ri read_int() #define rl read_ll() #define FILE(s) freopen(s "in", "r", stdin), freopen(s "out", "w", stdout) template <typename T> inline void read(T &x) { char ch, t = 0; x = 0; while (!isdigit(ch = ge)) t |= ch == '-'; while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = ge; x = t ? -x : x; } inline int read_int() { int x; return read(x), x; } inline ll read_ll() { ll x; return read(x), x; } template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; } int main() { #ifndef ONLINE_JUDGE FILE(""); #endif ll n = rl, A = rl, B = rl; if ((A ^ B) & 1) { if (A > B) swap(A, B); cout << min(A, n - B + 1) + (B - A) / 2 << endl; } else cout << abs(A - B) / 2 << endl; return 0; }
/******************************************************************************** Code by a weak man who named CYJian, and he hope the code can get more points. Algorithm: ********************************************************************************/ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int __SIZE = 1 << 18; char ibuf[__SIZE], *iS, *iT; #define ge \ (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), \ (iS == iT ? EOF : *iS++)) \ : *iS++) #define ri read_int() #define rl read_ll() #define FILE(s) freopen(s "in", "r", stdin), freopen(s "out", "w", stdout) template <typename T> inline void read(T &x) { char ch, t = 0; x = 0; while (!isdigit(ch = ge)) t |= ch == '-'; while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = ge; x = t ? -x : x; } inline int read_int() { int x; return read(x), x; } inline ll read_ll() { ll x; return read(x), x; } template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; } int main() { ll n = rl, A = rl, B = rl; if ((A ^ B) & 1) { if (A > B) swap(A, B); cout << min(A, n - B + 1) + (B - A) / 2 << endl; } else cout << abs(A - B) / 2 << endl; return 0; }
delete
47
50
47
47
TLE
p02823
Python
Runtime Error
def main(): N, A, B = [int(i) for i in input().split()] diff = B - A if diff % 2 == 0: ans = diff // 2 else: ans = min( B - 1, N - A, (A - 1) + 1 + (diff - 1) // 2, (N - B) + 1(diff - 1) // 2 ) print(ans) if __name__ == "__main__": main()
def main(): N, A, B = [int(i) for i in input().split()] diff = B - A if diff % 2 == 0: ans = diff // 2 else: ans = min( B - 1, N - A, (A - 1) + 1 + (diff - 1) // 2, (N - B) + 1 + (diff - 1) // 2 ) print(ans) if __name__ == "__main__": main()
replace
7
8
7
8
0
/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02823/Python/s158682603.py:7: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? ans = min(B - 1, N - A, (A - 1) + 1 + (diff - 1) // 2, (N - B) + 1 (diff - 1) // 2)
p02823
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; typedef long long int64; int64 solve(int64 N, int64 A, int64 B) { if ((B - A) % 2 == 0) return (B - A) / 2; int64 ret = min(B - 1, N - A); ret = min(ret, solve(N, 1, N - A) + A); ret = min(ret, solve(N, A + (N - B) + 1, N) + (N - B) + 1); return ret; } int main() { int64 N, A, B; scanf("%lld%lld%lld", &N, &A, &B); int64 ans = solve(N, A, B); printf("%lld\n", ans); }
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; typedef long long int64; int64 solve(int64 N, int64 A, int64 B) { if ((B - A) % 2 == 0) return (B - A) / 2; int64 ret = min(B - 1, N - A); ret = min(ret, solve(N, 1, B - A) + A); ret = min(ret, solve(N, A + (N - B) + 1, N) + (N - B) + 1); return ret; } int main() { int64 N, A, B; scanf("%lld%lld%lld", &N, &A, &B); int64 ans = solve(N, A, B); printf("%lld\n", ans); }
replace
9
10
9
10
0
p02823
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; const ll N = 5e5 + 5; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n, a, b; cin >> n >> a >> b; ll ans = min(b - 1, n - a); if (a % 2 == b % 2) ans = min(ans, (b - a) / 2); if (a % 2 != b % 2) while (1) { ; } cout << ans; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; const ll N = 5e5 + 5; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n, a, b; cin >> n >> a >> b; ll ans = min(b - 1, n - a); if (a % 2 == b % 2) ans = min(ans, (b - a) / 2); else { ll val = min(a - 1, n - b); val = val + (b - a + 1) / 2; ans = min(ans, val); } cout << ans; }
replace
14
19
14
19
TLE
p02823
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define int ll template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } #define REP(i, n) for (int i = 0; i < n; ++i) #define SORT(name) sort(name.begin(), name.end()) #define ZERO(p) memset(p, 0, sizeof(p)) #define MINUS(p) memset(p, -1, sizeof(p)) #if 1 #define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__) #else #define DBG(fmt, ...) #endif const ll LLINF = (1LL << 60); const int INF = (1LL << 30); const double DINF = std::numeric_limits<double>::infinity(); const int MOD = 1000000007; const int MAX_N = 100010; signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); ll N, A, B; cin >> N >> A >> B; ll ab_diff = abs(A - B); ll ans = LLINF; if (ab_diff % 2 == 0) { ans = ab_diff / 2; } else { assert(false); ans = min(max(A - 1, B - 1), max(N - A, N - B)); } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define int ll template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } #define REP(i, n) for (int i = 0; i < n; ++i) #define SORT(name) sort(name.begin(), name.end()) #define ZERO(p) memset(p, 0, sizeof(p)) #define MINUS(p) memset(p, -1, sizeof(p)) #if 1 #define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__) #else #define DBG(fmt, ...) #endif const ll LLINF = (1LL << 60); const int INF = (1LL << 30); const double DINF = std::numeric_limits<double>::infinity(); const int MOD = 1000000007; const int MAX_N = 100010; signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); ll N, A, B; cin >> N >> A >> B; ll ab_diff = abs(A - B); ll ans = LLINF; if (ab_diff % 2 == 0) { ans = ab_diff / 2; } else { // 左端に移動する場合 ll cand1 = A - 1; // 左に行き着くのにかかるターン数 ll tmp_b = B - cand1; if (tmp_b % 2 != 0) { tmp_b--; cand1++; } cand1 += tmp_b / 2; // 右端に移動する場合 ll cand2 = N - B; tmp_b = (N - A) - cand2; if (tmp_b % 2 != 0) { tmp_b--; cand2++; } cand2 += tmp_b / 2; ans = min(cand1, cand2); } printf("%lld\n", ans); return 0; }
replace
44
46
44
62
0
p02823
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int N, A, B; cin >> N >> A >> B; assert(A != B); if (A > B) swap(A, B); assert(A < B); if (A % 2 == B % 2) { cout << (B - A) / 2 << '\n'; } else { cout << min((N - B) + 1 + (N - A), (A - 1) + 1 + (B - 1)) / 2 << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long N, A, B; cin >> N >> A >> B; assert(A != B); if (A > B) swap(A, B); assert(A < B); if (A % 2 == B % 2) { cout << (B - A) / 2 << '\n'; } else { cout << min((N - B) + 1 + (N - A), (A - 1) + 1 + (B - 1)) / 2 << '\n'; } return 0; }
replace
5
6
5
6
0
p02823
C++
Time Limit Exceeded
// include //------------------------------------------ #include <algorithm> #include <bits/stdc++.h> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // math //------------------------------------------- template <class T> inline T sqr(T x) { return x * x; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define fore(i, a) for (auto &i : a) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); // clear memory #define CLR(a) memset((a), 0, sizeof(a)) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; LL gcd(LL a, LL b) { if (b == 0) return a; return gcd(b, a % b); } LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g * b; } int main() { long long N, A, B; cin >> N >> A >> B; long long kyori = B - A; long long ans = 0; if (kyori % 2 == 0) { ans = kyori / 2; } else { if (A - 1 <= N - B) { while ((B - A) % 2 != 0) { if (A != 1) { A--; B--; ++ans; } else { A = 1; B--; ++ans; } } ans += (B - A) / 2; } else { while ((B - A) % 2 != 0) { if (B != N) { A++; B++; ++ans; } else { B = N; A++; ++ans; } } ans += (B - A) / 2; } } cout << ans; }
// include //------------------------------------------ #include <algorithm> #include <bits/stdc++.h> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // math //------------------------------------------- template <class T> inline T sqr(T x) { return x * x; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define fore(i, a) for (auto &i : a) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); // clear memory #define CLR(a) memset((a), 0, sizeof(a)) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; LL gcd(LL a, LL b) { if (b == 0) return a; return gcd(b, a % b); } LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g * b; } int main() { long long N, A, B; cin >> N >> A >> B; long long kyori = B - A; long long ans = 0; if (kyori % 2 == 0) { ans = kyori / 2; } else { ans = min(A - 1, N - B) + 1 + (B - A - 1) / 2; } cout << ans; }
replace
110
137
110
111
TLE
p02823
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define rgi register int #define rgl register ll #define int ll #define maxn 1000005 #define mod 10007LL #define inf 1000000007LL namespace lib { using namespace std; class fastio { private: #define _NUMLEN 111 char _num[_NUMLEN], _ch; int _f, _head; bool _useSTD; public: inline void ioSTD() { _useSTD = 1; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); } inline void gc(char &_c) { if (_useSTD) cin >> _c; else _c = getchar(); } inline void pc(char _c) { if (_useSTD) cout << _c; else putchar(_c); } template <typename _T> inline fastio &operator-(_T &_x) { if (_useSTD) cin >> _x; else { _x = 0; while (!isdigit(_ch)) _f |= _ch == '-', gc(_ch); while (isdigit(_ch)) _x = (_x << 1) + (_x << 3) + (_ch ^ 48), gc(_ch); _f && (_x = -_x, _f = 0); } return *this; } template <typename _T> inline fastio &operator+(_T _x) { if (_useSTD) cin >> _x; else { _T _k; if (_x == 0) { pc('0'), pc(' '); return *this; } if (_x < 0) pc('-'), _x = -_x; while (_x > 0) _k = _x / 10, ++_head, _num[_head] = (_x - (_k << 1) - (_k << 3)) ^ 48, _x = _k; while (_head > 0) pc(_num[_head]), --_head; pc(' '); } return *this; } template <typename _T> inline fastio &operator>>(_T &_x) { cin >> _x; return *this; } template <typename _T> inline fastio &operator<<(_T _x) { cout << _x; return *this; } #define endln pc('\n') fastio() { _f = 0, _head = 0, _useSTD = 0, _ch = ' '; } } io; inline void fileio(string _r, string _w) { if (_r[0] != '-') freopen(_r.c_str(), "r", stdin); if (_w[0] != '-') freopen(_w.c_str(), "w", stdout); } #define mem(_x, _set) memset(_x, _set, sizeof(_x)) } // namespace lib using namespace lib; signed main() { // fileio("tt.in","tt.out"); // io.ioSTD(); ll n, a, b; io - n - a - b; if ((b - a) % 2 == 0) io + (b - a) / 2; else io + min((a + b - 1) / 2, (n - a + n - b + 1) / 2); return 1; }
#include <bits/stdc++.h> #define ll long long #define rgi register int #define rgl register ll #define int ll #define maxn 1000005 #define mod 10007LL #define inf 1000000007LL namespace lib { using namespace std; class fastio { private: #define _NUMLEN 111 char _num[_NUMLEN], _ch; int _f, _head; bool _useSTD; public: inline void ioSTD() { _useSTD = 1; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); } inline void gc(char &_c) { if (_useSTD) cin >> _c; else _c = getchar(); } inline void pc(char _c) { if (_useSTD) cout << _c; else putchar(_c); } template <typename _T> inline fastio &operator-(_T &_x) { if (_useSTD) cin >> _x; else { _x = 0; while (!isdigit(_ch)) _f |= _ch == '-', gc(_ch); while (isdigit(_ch)) _x = (_x << 1) + (_x << 3) + (_ch ^ 48), gc(_ch); _f && (_x = -_x, _f = 0); } return *this; } template <typename _T> inline fastio &operator+(_T _x) { if (_useSTD) cin >> _x; else { _T _k; if (_x == 0) { pc('0'), pc(' '); return *this; } if (_x < 0) pc('-'), _x = -_x; while (_x > 0) _k = _x / 10, ++_head, _num[_head] = (_x - (_k << 1) - (_k << 3)) ^ 48, _x = _k; while (_head > 0) pc(_num[_head]), --_head; pc(' '); } return *this; } template <typename _T> inline fastio &operator>>(_T &_x) { cin >> _x; return *this; } template <typename _T> inline fastio &operator<<(_T _x) { cout << _x; return *this; } #define endln pc('\n') fastio() { _f = 0, _head = 0, _useSTD = 0, _ch = ' '; } } io; inline void fileio(string _r, string _w) { if (_r[0] != '-') freopen(_r.c_str(), "r", stdin); if (_w[0] != '-') freopen(_w.c_str(), "w", stdout); } #define mem(_x, _set) memset(_x, _set, sizeof(_x)) } // namespace lib using namespace lib; signed main() { // fileio("tt.in","tt.out"); // io.ioSTD(); ll n, a, b; io - n - a - b; if ((b - a) % 2 == 0) io + (b - a) / 2; else io + min((a + b - 1) / 2, (n - a + n - b + 1) / 2); return 0; }
replace
98
99
98
99
1
p02823
C++
Runtime Error
// BUGSBUNNY // IIT ROORKEE // _,add8ba, // ,d888888888b, // d8888888888888b _,ad8ba,_ // d888888888888888) ,d888888888b, // I8888888888888888 _________ ,8888888888888b // __________`Y88888888888888P"""""""""""baaa,__ // ,888888888888888, // ,adP"""""""""""9888888888P""^ ^""Y8888888888888888I // ,a8"^ ,d888P"888P^ ^"Y8888888888P' // ,a8^ ,d8888' ^Y8888888P' // a88' ,d8888P' I88P"^ // ,d88' d88888P' "b, // ,d88' d888888' `b, // ,d88' d888888I `b, // d88I ,8888888' ___ `b, // ,888' d8888888 ,d88888b, ____ `b, // d888 ,8888888I d88888888b, ,d8888b, `b // ,8888 I8888888I d8888888888I ,88888888b 8, I8888 // 88888888b d88888888888' 8888888888b 8I d8886 // 888888888 Y888888888P' Y8888888888, ,8b 88888b // I88888888b `Y8888888^ `Y888888888I d88, Y88888b // `888888888b, `""""^ `Y8888888P' d888I `888888b // 88888888888b, `Y8888P^ d88888 // Y888888b ,8888888888888ba,_ _______ `""^ ,d888888 // I8888888b, ,888888888888888888ba,_ d88888888b ,ad8888888I // `888888888b, I8888888888888888888888b, ^"Y888P"^ ____.,ad88888888888I // 88888888888b,`888888888888888888888888b, "" ad888888888888888888888' // 8888888888888698888888888888888888888888b_,ad88ba,_,d88888888888888888888888 // 88888888888888888888888888888888888888888b,`"""^ // d8888888888888888888888888I // 8888888888888888888888888888888888888888888baaad888888888888888888888888888' // Y8888888888888888888888888888888888888888888888888888888888888888888888888P // I888888888888888888888888888888888888888888888P^ ^Y8888888888888888888888' // `Y88888888888888888P88888888888888888888888888' ^88888888888888888888I // `Y8888888888888888 `8888888888888888888888888 8888888888888888888P' // `Y888888888888888 `888888888888888888888888, ,888888888888888888P' // `Y88888888888888b `88888888888888888888888I I888888888888888888' // "Y8888888888888b `8888888888888888888888I I88888888888888888' // "Y88888888888P `888888888888888888888b d8888888888888888' // ^""""""""^ `Y88888888888888888888, 888888888888888P' // "8888888888888888888b, Y888888888888P^ // `Y888888888888888888b `Y8888888P"^ // "Y8888888888888888P `""""^ // `"YY88888888888P' // ^""""""""' #include <bits/stdc++.h> using namespace std; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define int long long #define ll long long #define ld long double #define endl '\n' #define vi vector<int> #define vvi vector<vi> #define vs vector<string> #define vl vector<long long> #define vpii vector<pii> #define vpipii vector<pipii> #define vb vector<bool> #define pb push_back #define pob pop_back #define gcd __gcd #define mp make_pair #define pii pair<int, int> #define pipii pair<int, pii> #define pll pair<long long, long long> #define pld pair<long double, long double> #define mod 1000000007 #define mod2 998244353 #define rep(i, n) for (ll i = 0; i < n; i++) #define repp(i, a, b) for (ll i = a; i < b; i++) #define reppr(i, a, b) for (ll i = a - 1; i >= b; i--) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define ff first #define ss second #define pc putchar_unlocked #define gc getchar_unlocked #define inf 1000000000000 #define infn -9223372036854775807 #define pi 3.14159265358979323846 #define eps 0.0000000001 #define sp << " " << #define len(s) s.size() #define setprecision0 cout << fixed << setprecision(0); #define setprecision10 cout << fixed << setprecision(10); #define all(n) n.begin(), n.end() #define SORT(v) sort(all(v)) void solve(); signed main() { fast; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; // cin >> t; while (t--) solve(); } void solve() { int n, a, b; cin >> n >> a >> b; if ((b - a) % 2 == 0) cout << (b - a) / 2 << endl; else { int o1 = 0, o2 = 0; int ta = a, tb = b; o1 += (n - b) + 1, tb = n, ta += (n - b + 1); o1 += (tb - ta) / 2; tb = b, ta = a; o2 += a, tb -= a, ta = 1; o2 += (tb - ta) / 2; cout << min(o1, o2) << endl; } }
// BUGSBUNNY // IIT ROORKEE // _,add8ba, // ,d888888888b, // d8888888888888b _,ad8ba,_ // d888888888888888) ,d888888888b, // I8888888888888888 _________ ,8888888888888b // __________`Y88888888888888P"""""""""""baaa,__ // ,888888888888888, // ,adP"""""""""""9888888888P""^ ^""Y8888888888888888I // ,a8"^ ,d888P"888P^ ^"Y8888888888P' // ,a8^ ,d8888' ^Y8888888P' // a88' ,d8888P' I88P"^ // ,d88' d88888P' "b, // ,d88' d888888' `b, // ,d88' d888888I `b, // d88I ,8888888' ___ `b, // ,888' d8888888 ,d88888b, ____ `b, // d888 ,8888888I d88888888b, ,d8888b, `b // ,8888 I8888888I d8888888888I ,88888888b 8, I8888 // 88888888b d88888888888' 8888888888b 8I d8886 // 888888888 Y888888888P' Y8888888888, ,8b 88888b // I88888888b `Y8888888^ `Y888888888I d88, Y88888b // `888888888b, `""""^ `Y8888888P' d888I `888888b // 88888888888b, `Y8888P^ d88888 // Y888888b ,8888888888888ba,_ _______ `""^ ,d888888 // I8888888b, ,888888888888888888ba,_ d88888888b ,ad8888888I // `888888888b, I8888888888888888888888b, ^"Y888P"^ ____.,ad88888888888I // 88888888888b,`888888888888888888888888b, "" ad888888888888888888888' // 8888888888888698888888888888888888888888b_,ad88ba,_,d88888888888888888888888 // 88888888888888888888888888888888888888888b,`"""^ // d8888888888888888888888888I // 8888888888888888888888888888888888888888888baaad888888888888888888888888888' // Y8888888888888888888888888888888888888888888888888888888888888888888888888P // I888888888888888888888888888888888888888888888P^ ^Y8888888888888888888888' // `Y88888888888888888P88888888888888888888888888' ^88888888888888888888I // `Y8888888888888888 `8888888888888888888888888 8888888888888888888P' // `Y888888888888888 `888888888888888888888888, ,888888888888888888P' // `Y88888888888888b `88888888888888888888888I I888888888888888888' // "Y8888888888888b `8888888888888888888888I I88888888888888888' // "Y88888888888P `888888888888888888888b d8888888888888888' // ^""""""""^ `Y88888888888888888888, 888888888888888P' // "8888888888888888888b, Y888888888888P^ // `Y888888888888888888b `Y8888888P"^ // "Y8888888888888888P `""""^ // `"YY88888888888P' // ^""""""""' #include <bits/stdc++.h> using namespace std; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define int long long #define ll long long #define ld long double #define endl '\n' #define vi vector<int> #define vvi vector<vi> #define vs vector<string> #define vl vector<long long> #define vpii vector<pii> #define vpipii vector<pipii> #define vb vector<bool> #define pb push_back #define pob pop_back #define gcd __gcd #define mp make_pair #define pii pair<int, int> #define pipii pair<int, pii> #define pll pair<long long, long long> #define pld pair<long double, long double> #define mod 1000000007 #define mod2 998244353 #define rep(i, n) for (ll i = 0; i < n; i++) #define repp(i, a, b) for (ll i = a; i < b; i++) #define reppr(i, a, b) for (ll i = a - 1; i >= b; i--) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define ff first #define ss second #define pc putchar_unlocked #define gc getchar_unlocked #define inf 1000000000000 #define infn -9223372036854775807 #define pi 3.14159265358979323846 #define eps 0.0000000001 #define sp << " " << #define len(s) s.size() #define setprecision0 cout << fixed << setprecision(0); #define setprecision10 cout << fixed << setprecision(10); #define all(n) n.begin(), n.end() #define SORT(v) sort(all(v)) void solve(); signed main() { fast; // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int t = 1; // cin >> t; while (t--) solve(); } void solve() { int n, a, b; cin >> n >> a >> b; if ((b - a) % 2 == 0) cout << (b - a) / 2 << endl; else { int o1 = 0, o2 = 0; int ta = a, tb = b; o1 += (n - b) + 1, tb = n, ta += (n - b + 1); o1 += (tb - ta) / 2; tb = b, ta = a; o2 += a, tb -= a, ta = 1; o2 += (tb - ta) / 2; cout << min(o1, o2) << endl; } }
replace
97
101
97
101
0
p02823
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdint> #include <cstdio> #include <iostream> #include <map> #include <stdlib.h> #include <string> #include <utility> #include <vector> using namespace std; using ll = long long; int main() { ll N, A, B; cin >> N >> A >> B; ll ans; if ((B - A) % 2 == 0) ans = (B - A) / 2; else { while (1) { } ans = min(B - 1, N - A); } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdint> #include <cstdio> #include <iostream> #include <map> #include <stdlib.h> #include <string> #include <utility> #include <vector> using namespace std; using ll = long long; int main() { ll N, A, B; cin >> N >> A >> B; ll ans; if ((B - A) % 2 == 0) ans = (B - A) / 2; else { ans = min(A - 1, N - B) + 1 + (B - A - 1) / 2; } cout << ans << endl; return 0; }
replace
21
24
21
22
TLE
p02824
C++
Runtime Error
#define STOPIT #include <bits/stdc++.h> #define loop(n) \ for (lint ngtkana_is_a_genius = 0; ngtkana_is_a_genius < lint(n); \ ngtkana_is_a_genius++) #define rep(i, begin, end) for (lint i = lint(begin); (i) < lint(end); i++) #define all(v) v.begin(), v.end() #define rand(l, r) std::uniform_int_distribution<>(l, r)(mt) using lint = long long; auto mt = std::mt19937_64(std::random_device{}()); auto cmn = [](auto &&a, auto b) { if (a > b) { a = b; return true; } return false; }; auto cmx = [](auto &&a, auto b) { if (a < b) { a = b; return true; } return false; }; void debug_impl() { std::cerr << std::endl; } template <typename Head, typename... Tail> void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); } #ifndef STOPIT #define debug(...) \ do { \ std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:"; \ debug_impl(__VA_ARGS__); \ std::cerr << std::noboolalpha; \ } while (false) #else #define debug(...) \ {} #endif template <typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same<Container, std::string>::value, std::nullptr_t> = nullptr> std::istream &operator>>(std::istream &is, Container &v) { for (auto &x : v) { is >> x; } return is; } template <typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same<Container, std::string>::value, std::nullptr_t> = nullptr> std::ostream &operator<<(std::ostream &os, Container const &v) { os << "{"; for (auto it = v.begin(); it != v.end(); it++) { os << (it != v.begin() ? "," : "") << *it; } return os << "}"; } /* 二分探索です。i 番目が行ける条件を考えます。 まず、i < p なら明らかに OK です。 そうでないとき、まず、全振りして a_{p-1} に勝てなければダメです。 それが大丈夫だとして、残りが始めの p-1 個と右のものだけに振れるなら OK です。 それがダメなら、[p-1,i[ に、忖度しながら振っていきます。 */ int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); lint n, m, v, p; std::cin >> n >> m >> v >> p; std::vector<lint> a(n); std::cin >> a; std::sort(all(a), std::greater<>{}); debug(m, v, p, a); std::vector<lint> b = {0}; std::partial_sum(all(a), std::back_inserter(b)); debug(b); lint rival = a.at(p - 1); debug(rival); auto check = [&](lint i) -> bool { if (i < p) return true; debug("foo"); lint me = a.at(i); if (rival > me + m) return false; lint u = n + p - i - 2; debug(u, "goo"); if (v - 1 <= u) return true; debug("hoo"); lint sum = b.at(i) - b.at(p - 1); lint k = v - 1 - u; debug(sum, k); debug(i - p + 1, me + m); return sum + k * m <= (i - p + 1) * (me + m); }; // debug // rep(i,0,n) { // bool b = check(i); // debug(i,b); // std::cerr<<std::endl; // } lint ok = 0, ng = n + 1; while (1 < ng - ok) { lint mid = (ng + ok) / 2; (check(mid) ? ok : ng) = mid; } lint ans = ok + 1; std::cout << ans << std::endl; return 0; }
#define STOPIT #include <bits/stdc++.h> #define loop(n) \ for (lint ngtkana_is_a_genius = 0; ngtkana_is_a_genius < lint(n); \ ngtkana_is_a_genius++) #define rep(i, begin, end) for (lint i = lint(begin); (i) < lint(end); i++) #define all(v) v.begin(), v.end() #define rand(l, r) std::uniform_int_distribution<>(l, r)(mt) using lint = long long; auto mt = std::mt19937_64(std::random_device{}()); auto cmn = [](auto &&a, auto b) { if (a > b) { a = b; return true; } return false; }; auto cmx = [](auto &&a, auto b) { if (a < b) { a = b; return true; } return false; }; void debug_impl() { std::cerr << std::endl; } template <typename Head, typename... Tail> void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); } #ifndef STOPIT #define debug(...) \ do { \ std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:"; \ debug_impl(__VA_ARGS__); \ std::cerr << std::noboolalpha; \ } while (false) #else #define debug(...) \ {} #endif template <typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same<Container, std::string>::value, std::nullptr_t> = nullptr> std::istream &operator>>(std::istream &is, Container &v) { for (auto &x : v) { is >> x; } return is; } template <typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same<Container, std::string>::value, std::nullptr_t> = nullptr> std::ostream &operator<<(std::ostream &os, Container const &v) { os << "{"; for (auto it = v.begin(); it != v.end(); it++) { os << (it != v.begin() ? "," : "") << *it; } return os << "}"; } /* 二分探索です。i 番目が行ける条件を考えます。 まず、i < p なら明らかに OK です。 そうでないとき、まず、全振りして a_{p-1} に勝てなければダメです。 それが大丈夫だとして、残りが始めの p-1 個と右のものだけに振れるなら OK です。 それがダメなら、[p-1,i[ に、忖度しながら振っていきます。 */ int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); lint n, m, v, p; std::cin >> n >> m >> v >> p; std::vector<lint> a(n); std::cin >> a; std::sort(all(a), std::greater<>{}); debug(m, v, p, a); std::vector<lint> b = {0}; std::partial_sum(all(a), std::back_inserter(b)); debug(b); lint rival = a.at(p - 1); debug(rival); auto check = [&](lint i) -> bool { if (i < p) return true; debug("foo"); lint me = a.at(i); if (rival > me + m) return false; lint u = n + p - i - 2; debug(u, "goo"); if (v - 1 <= u) return true; debug("hoo"); lint sum = b.at(i) - b.at(p - 1); lint k = v - 1 - u; debug(sum, k); debug(i - p + 1, me + m); return sum + k * m <= (i - p + 1) * (me + m); }; // debug // rep(i,0,n) { // bool b = check(i); // debug(i,b); // std::cerr<<std::endl; // } lint ok = 0, ng = n; while (1 < ng - ok) { lint mid = (ng + ok) / 2; (check(mid) ? ok : ng) = mid; } lint ans = ok + 1; std::cout << ans << std::endl; return 0; }
replace
112
113
112
113
0
p02824
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <time.h> using namespace std; typedef long long ll; int n, m, v, p, a[100005], b[100005], ans, pth_min; bool judge(int pth) { ll res = 0; int i; for (i = 1; i <= n; i++) b[i] = min(m, pth - a[i]); sort(b + 1, b + n + 1); if (b[p] < 0) return 0; for (i = 1; i <= n; i++) res += i <= p - 1 ? m : b[i]; return res >= (ll)m * v; } int solve() { int l = 0, r = 2e9, mid, res = -1; while (l <= r) { mid = (ll)(l + r) >> 1; if (judge(mid)) r = mid - 1, res = mid; else l = mid + 1; } return res; } int main() { scanf("%d%d%d%d", &n, &m, &v, &p); int i; for (i = 1; i <= n; i++) scanf("%d", &a[i]); pth_min = solve(); for (i = 1; i <= n; i++) ans += a[i] + m >= pth_min; printf("%d\n", ans); return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <time.h> using namespace std; typedef long long ll; int n, m, v, p, a[100005], b[100005], ans, pth_min; bool judge(int pth) { ll res = 0; int i; for (i = 1; i <= n; i++) b[i] = min(m, pth - a[i]); sort(b + 1, b + n + 1); if (b[p] < 0) return 0; for (i = 1; i <= n; i++) res += i <= p - 1 ? m : b[i]; return res >= (ll)m * v; } int solve() { int l = 0, r = 2e9, mid, res = -1; while (l <= r) { mid = ((ll)l + r) >> 1; if (judge(mid)) r = mid - 1, res = mid; else l = mid + 1; } return res; } int main() { scanf("%d%d%d%d", &n, &m, &v, &p); int i; for (i = 1; i <= n; i++) scanf("%d", &a[i]); pth_min = solve(); for (i = 1; i <= n; i++) ans += a[i] + m >= pth_min; printf("%d\n", ans); return 0; }
replace
23
24
23
24
TLE
p02824
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>; int n, m, v, p, a[100010]; bool calc(int mid, ll cnt = 0) { int x = a[mid] + m; if (a[p - 1] > x) return false; cnt += (ll)(p - 1) * m; cnt += (ll)(n - mid) * m; for (int i = p - 1; i <= x - 1; ++i) { cnt += min(m, max(0, x - a[i])); } return cnt >= (ll)m * v; } int main() { cin >> n >> m >> v >> p; rep(i, n) cin >> a[i]; sort(a, a + n); reverse(a, a + n); int l = 0, r = n; while (r - l > 1) { int mid = (l + r) / 2; if (calc(mid)) l = mid; else r = mid; } // 1-indexedに修正 cout << l + 1 << 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>; int n, m, v, p, a[100010]; bool calc(int mid, ll cnt = 0) { int x = a[mid] + m; if (a[p - 1] > x) return false; cnt += (ll)(p - 1) * m; cnt += (ll)(n - mid) * m; for (int i = p - 1; i <= mid - 1; ++i) { cnt += min(m, max(0, x - a[i])); } return cnt >= (ll)m * v; } int main() { cin >> n >> m >> v >> p; rep(i, n) cin >> a[i]; sort(a, a + n); reverse(a, a + n); int l = 0, r = n; while (r - l > 1) { int mid = (l + r) / 2; if (calc(mid)) l = mid; else r = mid; } // 1-indexedに修正 cout << l + 1 << endl; }
replace
14
15
14
15
0
p02824
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> p_ll; template <class T> void debug(T itr1, T itr2) { auto now = itr1; while (now < itr2) { cout << *now << " "; now++; } cout << endl; } #define repr(i, from, to) for (int i = (int)from; i < (int)to; i++) #define all(vec) vec.begin(), vec.end() #define rep(i, N) repr(i, 0, N) #define per(i, N) for (int i = (int)N - 1; i >= 0; i--) const ll MOD = pow(10, 9) + 7; const ll LLINF = pow(2, 61) - 1; const int INF = pow(2, 30) - 1; int main() { ll N, M, V, P; cin >> N >> M >> V >> P; ll A[N]; rep(i, N) cin >> A[i]; sort(A, A + N); ll l = 0; while (A[N - P] - A[l] > M) l++; ll r = (N - 1) - P; while (l != r) { ll mid = (l + r) / 2; ll num = 0; rep(i, N - (P - 1)) { num += M - max((ll)0, A[i] - A[mid]); } // cout << mid << " " << num << endl; if (num >= (V - (P - 1)) * M) r = mid; else l = mid + 1; } ll result = N - l; cout << result << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> p_ll; template <class T> void debug(T itr1, T itr2) { auto now = itr1; while (now < itr2) { cout << *now << " "; now++; } cout << endl; } #define repr(i, from, to) for (int i = (int)from; i < (int)to; i++) #define all(vec) vec.begin(), vec.end() #define rep(i, N) repr(i, 0, N) #define per(i, N) for (int i = (int)N - 1; i >= 0; i--) const ll MOD = pow(10, 9) + 7; const ll LLINF = pow(2, 61) - 1; const int INF = pow(2, 30) - 1; int main() { ll N, M, V, P; cin >> N >> M >> V >> P; ll A[N]; rep(i, N) cin >> A[i]; sort(A, A + N); ll l = 0; while (A[N - P] - A[l] > M) l++; ll r = (N - 1) - P; r = max(l, r); while (l != r) { ll mid = (l + r) / 2; ll num = 0; rep(i, N - (P - 1)) { num += M - max((ll)0, A[i] - A[mid]); } // cout << mid << " " << num << endl; if (num >= (V - (P - 1)) * M) r = mid; else l = mid + 1; } ll result = N - l; cout << result << endl; return 0; }
insert
33
33
33
34
TLE
p02824
C++
Runtime Error
#include <bits/stdc++.h> typedef long long int ll; typedef long double ld; #define pb push_back #define pii pair<int, int> #define F first #define S second #define int long long int #define sync \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") using namespace std; /// age ye mosh dp ro baze dashti dp tedad baz shodeye baste nashode yadet nare /// !! const int N = 1e5 + 100; ll a[N]; ll n, m, v, p; ll vis[N]; ll fen[N]; void add(ll idx, ll val) { for (; idx < N; idx += idx & (-idx)) { fen[idx] += val; } } ll get(ll idx) { ll s = 0; for (; idx; idx -= idx & (-idx)) { s += fen[idx]; } return s; } ll bs(ll x) { ll val = a[x]; val += m; memset(vis, 0, sizeof vis); memset(fen, 0, sizeof fen); vector<int> c; for (int i = x + 1; i <= n; i++) { if (a[i] > val) { vis[i] = 1; c.pb(m); } } ll p1 = c.size(); if (p1 > p - 1) return 0; for (int i = n; i > 0; i--) { if (vis[i] || i == x) continue; if (p1 < p - 1) { c.pb(m); // if (x==3) // cout << c.back() << " " << i << endl; p1++; continue; } else { c.pb(min(m, val - a[i])); // if (x==3) // cout << c.back() << " " << i << endl; } } ll ans = 0; for (int i = 0; i < c.size(); i++) { ll u = c[i]; ans += u; } ans /= (v - 1); if (ans >= m) return 1; return 0; } int32_t main() { cin >> n >> m >> v >> p; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); ll l = 0, r = n + 1; while (r - l > 1) { ll mid = (r + l) / 2; ll z = bs(mid); if (z == 1) r = mid; else l = mid; } cout << n + 1 - r; }
#include <bits/stdc++.h> typedef long long int ll; typedef long double ld; #define pb push_back #define pii pair<int, int> #define F first #define S second #define int long long int #define sync \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") using namespace std; /// age ye mosh dp ro baze dashti dp tedad baz shodeye baste nashode yadet nare /// !! const int N = 1e5 + 100; ll a[N]; ll n, m, v, p; ll vis[N]; ll fen[N]; void add(ll idx, ll val) { for (; idx < N; idx += idx & (-idx)) { fen[idx] += val; } } ll get(ll idx) { ll s = 0; for (; idx; idx -= idx & (-idx)) { s += fen[idx]; } return s; } ll bs(ll x) { ll val = a[x]; val += m; memset(vis, 0, sizeof vis); memset(fen, 0, sizeof fen); vector<int> c; for (int i = x + 1; i <= n; i++) { if (a[i] > val) { vis[i] = 1; c.pb(m); } } ll p1 = c.size(); if (p1 > p - 1) return 0; for (int i = n; i > 0; i--) { if (vis[i] || i == x) continue; if (p1 < p - 1) { c.pb(m); // if (x==3) // cout << c.back() << " " << i << endl; p1++; continue; } else { c.pb(min(m, val - a[i])); // if (x==3) // cout << c.back() << " " << i << endl; } } ll ans = 0; for (int i = 0; i < c.size(); i++) { ll u = c[i]; ans += u; } if (v == 1) { return 1; } ans /= (v - 1); if (ans >= m) return 1; return 0; } int32_t main() { cin >> n >> m >> v >> p; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); ll l = 0, r = n + 1; while (r - l > 1) { ll mid = (r + l) / 2; ll z = bs(mid); if (z == 1) r = mid; else l = mid; } cout << n + 1 - r; }
insert
70
70
70
73
0
p02824
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define all(x) (x).begin(), (x).end() #define endl '\n' typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, int> pdi; const ll INF = 2e18; const int MOD = 1e9 + 7; const int MAXN = 2e5 + 5; int N, M, V, P, A[MAXN]; bool chk(int idx) { int x = A[idx], a = idx - 1; if (V - P - a <= 0) return (x + M >= A[N - P + 1]); if (A[N - P + 1] > x + M) return 0; ll sum = 0; for (int i = idx + 1; i <= N - P + 1; i++) sum += x + M - A[i]; return (sum >= 1LL * (V - P - a) * M); } int main() { ios::sync_with_stdio(0); cin.tie(0); freopen("in.txt", "r", stdin); cin >> N >> M >> V >> P; for (int i = 1; i <= N; i++) cin >> A[i]; sort(A + 1, A + 1 + N); int l = 1, r = N - P, ans = P; while (l <= r) { int mid = l + r >> 1; if (chk(mid)) { r = mid - 1; ans = N - mid + 1; } else l = mid + 1; } cout << ans; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define all(x) (x).begin(), (x).end() #define endl '\n' typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, int> pdi; const ll INF = 2e18; const int MOD = 1e9 + 7; const int MAXN = 2e5 + 5; int N, M, V, P, A[MAXN]; bool chk(int idx) { int x = A[idx], a = idx - 1; if (V - P - a <= 0) return (x + M >= A[N - P + 1]); if (A[N - P + 1] > x + M) return 0; ll sum = 0; for (int i = idx + 1; i <= N - P + 1; i++) sum += x + M - A[i]; return (sum >= 1LL * (V - P - a) * M); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> M >> V >> P; for (int i = 1; i <= N; i++) cin >> A[i]; sort(A + 1, A + 1 + N); int l = 1, r = N - P, ans = P; while (l <= r) { int mid = l + r >> 1; if (chk(mid)) { r = mid - 1; ans = N - mid + 1; } else l = mid + 1; } cout << ans; }
delete
35
37
35
35
0
p02824
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const int maxn = 10005; inline int read() { int ret, f = 1; char c; while ((c = getchar()) && (c < '0' || c > '9')) if (c == '-') f = -1; ret = c - '0'; while ((c = getchar()) && (c >= '0' && c <= '9')) ret = (ret << 3) + (ret << 1) + c - '0'; return ret * f; } int n, m, v, p, a[maxn]; bool check(int x) { int val = a[x] + m, cnt = 0; ll rst = 1LL * (v - 1) * m; for (int i = n; i > x; --i) { if (a[i] > val) { ++cnt; rst -= m; } else { if (cnt < p - 1) { ++cnt; rst -= m; } else { rst -= (val - a[i]); } } } if (cnt >= p) return 0; return rst <= 1LL * m * (x - 1); } int main() { n = read(); m = read(); v = read(); p = read(); for (int i = 1; i <= n; ++i) a[i] = read(); sort(a + 1, a + n + 1); int l = 1, r = n, mid, ans; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) ans = mid, r = mid - 1; else l = mid + 1; } printf("%d", n - ans + 1); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const int maxn = 100005; inline int read() { int ret, f = 1; char c; while ((c = getchar()) && (c < '0' || c > '9')) if (c == '-') f = -1; ret = c - '0'; while ((c = getchar()) && (c >= '0' && c <= '9')) ret = (ret << 3) + (ret << 1) + c - '0'; return ret * f; } int n, m, v, p, a[maxn]; bool check(int x) { int val = a[x] + m, cnt = 0; ll rst = 1LL * (v - 1) * m; for (int i = n; i > x; --i) { if (a[i] > val) { ++cnt; rst -= m; } else { if (cnt < p - 1) { ++cnt; rst -= m; } else { rst -= (val - a[i]); } } } if (cnt >= p) return 0; return rst <= 1LL * m * (x - 1); } int main() { n = read(); m = read(); v = read(); p = read(); for (int i = 1; i <= n; ++i) a[i] = read(); sort(a + 1, a + n + 1); int l = 1, r = n, mid, ans; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) ans = mid, r = mid - 1; else l = mid + 1; } printf("%d", n - ans + 1); return 0; }
replace
6
7
6
7
0
p02824
C++
Runtime Error
#include <bits/stdc++.h> #include <math.h> #define _GLIBCXX_DEBUG #define _LIBCPP_DEBUG 0 using namespace std; #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define MOD (1000000007) #define vi vector<int> #define vl vector<ll> #define vb vector<bool> #define vvi vector<vi> #define vvl vector<vl> #define pii pair<int, int> #define pli pair<ll, int> a #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } ll keta(ll n) { string s = to_string(n); ll num = s.size(); return num; } const ll INF = 1LL << 60; const int dh[4] = {1, 0, -1, 0}; const int dw[4] = {0, 1, 0, -1}; struct Edge { int to; int weight; Edge(int t, int w) : to(t), weight(w) {} }; using Graph = vector<vector<Edge>>; using P = pair<ll, int>; class UnionFind { public: vi Parent; UnionFind(int n) { Parent = vi(n, -1); } int root(int a) { if (Parent[a] < 0) return a; else return Parent[a] = root(Parent[a]); } int size(int a) { return -Parent[root(a)]; } bool merge(int a, int b) { a = root(a); b = root(b); if (a == b) return false; if (size(a) < size(b)) swap(a, b); Parent[a] += Parent[b]; Parent[b] = a; return true; } }; class Factrial { public: vl Fac; Factrial(int MAX) { Fac = vl(MAX + 1); rep(i, MAX) { if (i == 0) Fac[i + 1] = 1; else Fac[i + 1] = ((i + 1) * Fac[i]) % MOD; } } }; ll n, m, v, p; vl a; vl s; bool check(int X) { if (X <= p - 1) return true; if (a[p - 1] - a[X] > m) return false; // 票の受け皿の数 ll num = n * m + (X - p + 1) * a[X] - (s[X] - s[p - 1]); return num >= m * v; } int main() { cin >> n >> m >> v >> p; a.resize(n); rep(i, n) cin >> a[i]; sort(all(a), greater<ll>()); s.resize(n + 1); rep(i, n) s[i + 1] = s[i] + a[i]; rrep(i, n) { if (check(i)) return i + 1; } }
#include <bits/stdc++.h> #include <math.h> #define _GLIBCXX_DEBUG #define _LIBCPP_DEBUG 0 using namespace std; #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define MOD (1000000007) #define vi vector<int> #define vl vector<ll> #define vb vector<bool> #define vvi vector<vi> #define vvl vector<vl> #define pii pair<int, int> #define pli pair<ll, int> a #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } ll keta(ll n) { string s = to_string(n); ll num = s.size(); return num; } const ll INF = 1LL << 60; const int dh[4] = {1, 0, -1, 0}; const int dw[4] = {0, 1, 0, -1}; struct Edge { int to; int weight; Edge(int t, int w) : to(t), weight(w) {} }; using Graph = vector<vector<Edge>>; using P = pair<ll, int>; class UnionFind { public: vi Parent; UnionFind(int n) { Parent = vi(n, -1); } int root(int a) { if (Parent[a] < 0) return a; else return Parent[a] = root(Parent[a]); } int size(int a) { return -Parent[root(a)]; } bool merge(int a, int b) { a = root(a); b = root(b); if (a == b) return false; if (size(a) < size(b)) swap(a, b); Parent[a] += Parent[b]; Parent[b] = a; return true; } }; class Factrial { public: vl Fac; Factrial(int MAX) { Fac = vl(MAX + 1); rep(i, MAX) { if (i == 0) Fac[i + 1] = 1; else Fac[i + 1] = ((i + 1) * Fac[i]) % MOD; } } }; ll n, m, v, p; vl a; vl s; bool check(int X) { if (X <= p - 1) return true; if (a[p - 1] - a[X] > m) return false; // 票の受け皿の数 ll num = n * m + (X - p + 1) * a[X] - (s[X] - s[p - 1]); return num >= m * v; } int main() { cin >> n >> m >> v >> p; a.resize(n); rep(i, n) cin >> a[i]; sort(all(a), greater<ll>()); s.resize(n + 1); rep(i, n) s[i + 1] = s[i] + a[i]; rrep(i, n) { if (check(i)) { cout << i + 1 << endl; return 0; } } }
replace
133
135
133
137
5
p02824
C++
Runtime Error
#include <algorithm> #include <cstdio> #define mxn 10010 #define pii pair<int, int> #define fr first #define sc second #define mp make_pair #define LL long long using namespace std; const int mod = 1e9 + 7; int n, m, K, N, sl, fh, ans, a[mxn]; LL s[mxn]; int rd() { sl = 0; fh = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') fh = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar(); return sl * fh; } int main() { n = rd(); m = rd(); K = rd(); ans = N = rd(); K = max(1, K - N + 1); for (int i = 1; i <= n; ++i) a[i] = rd(); sort(a + 1, a + n + 1, [&](const int &x, const int &y) { return x > y; }); for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i]; for (int i = N + 1; i <= n; ++i) if (i + K - 1 <= n) ans += a[i] + m >= a[N]; else if (a[i] + m >= a[N]) ans += 1ll * (i - N) * (a[i] + m) - s[i - 1] + s[N - 1] >= 1ll * m * (K - (n - i + 1)); printf("%d\n", ans); return 0; }
#include <algorithm> #include <cstdio> #define mxn 1000010 #define pii pair<int, int> #define fr first #define sc second #define mp make_pair #define LL long long using namespace std; const int mod = 1e9 + 7; int n, m, K, N, sl, fh, ans, a[mxn]; LL s[mxn]; int rd() { sl = 0; fh = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') fh = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar(); return sl * fh; } int main() { n = rd(); m = rd(); K = rd(); ans = N = rd(); K = max(1, K - N + 1); for (int i = 1; i <= n; ++i) a[i] = rd(); sort(a + 1, a + n + 1, [&](const int &x, const int &y) { return x > y; }); for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i]; for (int i = N + 1; i <= n; ++i) if (i + K - 1 <= n) ans += a[i] + m >= a[N]; else if (a[i] + m >= a[N]) ans += 1ll * (i - N) * (a[i] + m) - s[i - 1] + s[N - 1] >= 1ll * m * (K - (n - i + 1)); printf("%d\n", ans); return 0; }
replace
2
3
2
3
0
p02824
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn = 100005; int n, m, v, p, a[maxn], ans; ll b[maxn]; inline int cmp1(int x, int y) { return x > y; } inline int read() { int res = 0, f_f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f_f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') res = (res << 3) + (res << 1) - '0' + ch, ch = getchar(); return res * f_f; } int main() { freopen("D.in", "r", stdin); freopen("D.out", "w", stdout); n = read(), m = read(), v = read(), p = read(), ans = p; for (int i = 1; i <= n; i++) { a[i] = read(); } sort(a + 1, a + n + 1, cmp1); for (int i = 1; i <= n; i++) { b[i] = b[i - 1] + a[i]; } for (int i = p + 1; i <= n; i++) { if (a[i] + m < a[p]) continue; ll delta = 1ll * max(0, v - (p - 1) - (n - i + 1)) * m; if (delta == 0) { ans++; continue; } ll w = 1ll * m * (i - p) - (b[i - 1] - b[p - 1] - 1ll * a[i] * (i - p)); if (w >= delta) ans++; } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn = 100005; int n, m, v, p, a[maxn], ans; ll b[maxn]; inline int cmp1(int x, int y) { return x > y; } inline int read() { int res = 0, f_f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f_f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') res = (res << 3) + (res << 1) - '0' + ch, ch = getchar(); return res * f_f; } int main() { n = read(), m = read(), v = read(), p = read(), ans = p; for (int i = 1; i <= n; i++) { a[i] = read(); } sort(a + 1, a + n + 1, cmp1); for (int i = 1; i <= n; i++) { b[i] = b[i - 1] + a[i]; } for (int i = p + 1; i <= n; i++) { if (a[i] + m < a[p]) continue; ll delta = 1ll * max(0, v - (p - 1) - (n - i + 1)) * m; if (delta == 0) { ans++; continue; } ll w = 1ll * m * (i - p) - (b[i - 1] - b[p - 1] - 1ll * a[i] * (i - p)); if (w >= delta) ans++; } printf("%d\n", ans); return 0; }
delete
20
22
20
20
TLE
p02824
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1000000007; const double PI = 3.14159265358979; const ll INF = pow(10, 18); typedef pair<ll, ll> P; typedef vector<ll> vl; typedef vector<vl> vvl; #define FOR(i, a, b) for (ll i = a; i < b; i++) #define rep(i, n) FOR(i, 0, n) string abc = "abcdefghijklmnopqrstuvwxyz"; string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int main() { ll n, m, v, p; cin >> n >> m >> v >> p; vl a(n); rep(i, n) { cin >> a[i]; } sort(a.begin(), a.end()); ll A = -1, B = n; ll M; while (A != B) { M = (A + B) / 2; bool ok = false; bool sub = true; ll s = 0; // a[M]で条件満たすの? FOR(i, M + 1, n - p + 1) { if (a[M] + m - a[i] >= 0) { s += (a[M] + m - a[i]); } else sub = false; } if ((M + 1) * m + s >= m * (v - p + 1)) { ok = true; } // にぶたん if (ok && sub) { A = min(A, B); B = M; } else { B = max(A, B); A = M + 1; } } cout << n - A << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1000000007; const double PI = 3.14159265358979; const ll INF = pow(10, 18); typedef pair<ll, ll> P; typedef vector<ll> vl; typedef vector<vl> vvl; #define FOR(i, a, b) for (ll i = a; i < b; i++) #define rep(i, n) FOR(i, 0, n) string abc = "abcdefghijklmnopqrstuvwxyz"; string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int main() { ll n, m, v, p; cin >> n >> m >> v >> p; vl a(n); rep(i, n) { cin >> a[i]; } sort(a.begin(), a.end()); ll A = 0, B = n - 1; ll M; while (A != B) { M = (A + B) / 2; bool ok = false; bool sub = true; ll s = 0; // a[M]で条件満たすの? FOR(i, M + 1, n - p + 1) { if (a[M] + m - a[i] >= 0) { s += (a[M] + m - a[i]); } else sub = false; } if ((M + 1) * m + s >= m * (v - p + 1)) { ok = true; } // にぶたん if (ok && sub) { A = min(A, B); B = M; } else { B = max(A, B); A = M + 1; } } cout << n - A << endl; }
replace
20
21
20
21
TLE
p02824
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n, m, v, p; cin >> n >> m >> v >> p; vector<ll> a(n); vector<ll> ruiseki(n); ll tmp; // cin >> tmp; // a.at(0) = tmp; // ruiseki.at(0) = tmp; for (ll i = 0; i < n; i++) { cin >> tmp; a.at(i) = tmp; // ruiseki.at(i) = ruiseki.at(i-1) + tmp; } sort(a.begin(), a.end(), greater<ll>()); ruiseki.at(0) = a.at(0); for (ll i = 1; i < n; i++) { ruiseki.at(i) = a.at(i) + ruiseki.at(i - 1); } ll vn = (n - v) * m; // ll cum = 0; ll ans = 0; ll ruiseki_p = ruiseki.at(p - 2); for (int j = p; j < n; j++) { if ((ruiseki.at(j - 1) - ruiseki_p) <= vn) ans++; } cout << p + ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n, m, v, p; cin >> n >> m >> v >> p; vector<ll> a(n); vector<ll> ruiseki(n); ll tmp; // cin >> tmp; // a.at(0) = tmp; // ruiseki.at(0) = tmp; for (ll i = 0; i < n; i++) { cin >> tmp; a.at(i) = tmp; // ruiseki.at(i) = ruiseki.at(i-1) + tmp; } sort(a.begin(), a.end(), greater<ll>()); ruiseki.at(0) = a.at(0); for (ll i = 1; i < n; i++) { ruiseki.at(i) = a.at(i) + ruiseki.at(i - 1); } ll vn = (n - v) * m; // ll cum = 0; ll ans = 0; ll ruiseki_p; if (1 < p) { ruiseki_p = ruiseki.at(p - 2); } else { ruiseki_p = 0; } for (int j = 1; j <= n - p; j++) { if (a.at(p + j - 1) + m < a.at(p - 1)) { } else { ll R = (ruiseki.at(p + j - 2) - ruiseki_p) - a.at(p + j - 1) * j; if (R <= vn) ans++; } } cout << p + ans << endl; }
replace
25
29
25
38
0
p02824
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define lsb(x) (x & (-x)) #define ll long long #define ull unsigned long long #define uint unsigned int #if 0 const int MOD = (int) ; inline int lgput(int a, int b) { int ans = 1; while(b > 0) { if(b & 1) ans = (1LL * ans * a) % MOD; b >>= 1; a = (1LL * a * a) % MOD; } return ans; } inline void mod(int &x) { if(x >= MOD) x -= MOD; } inline void add(int &x, int y) { x += y; mod(x); } inline void sub(int &x, int y) { x += MOD - y; mod(x); } inline void mul(int &x, int y) { x = (1LL * x * y) % MOD; } inline int inv(int x) { return lgput(x, MOD - 2); } #endif #if 0 int fact[], invfact[]; inline void prec(int n) { fact[0] = 1; for(int i = 1; i <= n; i++) { fact[i] = (1LL * fact[i - 1] * i) % MOD; } invfact[n] = lgput(fact[n], MOD - 2); for(int i = n - 1; i >= 0; i--) { invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD; } } inline int comb(int n, int k) { if(n < k) return 0; return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD; } #endif using namespace std; int main() { #ifdef HOME ifstream cin("A.in"); ofstream cout("A.out"); #endif int i, n, m, v, p; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m >> v >> p; vector<int> a(n + 1); for (i = 1; i <= n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); vector<ll> x(n + 1); vector<bool> vis(n + 1); auto check = [&](int pos) -> bool { int cur = a[pos] + m; int big = 0; for (i = 1; i <= n; i++) { if (a[i] > cur) { big++; } } if (big >= p) { return 0; } fill(vis.begin(), vis.end(), 0); int sz = n; for (i = n; i >= 1; i--) { if (i == pos) continue; if (a[i] > cur) { x[sz--] = 1e18; vis[i] = 1; } else { if (big < p - 1) { x[sz--] = 1e18; big++; vis[i] = 1; } } } for (i = 1; i <= n; i++) { if (i == pos || vis[i]) continue; x[sz--] = cur - a[i]; } x[1] = 0; pos = 0; ll sum = 0; for (int k = 1; k <= m; k++) { while (pos <= n && x[pos] <= k) { sum += x[pos]; pos++; } if (sum + 1LL * k * (n - pos + 1) < 1LL * k * (v - 1)) { return 0; } } return 1; }; int res = 0; for (int step = 1 << 18; step; step >>= 1) { if (res + step <= n && check(res + step) == 0) { res += step; } } cout << n - res; return 0; }
#include <bits/stdc++.h> #define lsb(x) (x & (-x)) #define ll long long #define ull unsigned long long #define uint unsigned int #if 0 const int MOD = (int) ; inline int lgput(int a, int b) { int ans = 1; while(b > 0) { if(b & 1) ans = (1LL * ans * a) % MOD; b >>= 1; a = (1LL * a * a) % MOD; } return ans; } inline void mod(int &x) { if(x >= MOD) x -= MOD; } inline void add(int &x, int y) { x += y; mod(x); } inline void sub(int &x, int y) { x += MOD - y; mod(x); } inline void mul(int &x, int y) { x = (1LL * x * y) % MOD; } inline int inv(int x) { return lgput(x, MOD - 2); } #endif #if 0 int fact[], invfact[]; inline void prec(int n) { fact[0] = 1; for(int i = 1; i <= n; i++) { fact[i] = (1LL * fact[i - 1] * i) % MOD; } invfact[n] = lgput(fact[n], MOD - 2); for(int i = n - 1; i >= 0; i--) { invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD; } } inline int comb(int n, int k) { if(n < k) return 0; return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD; } #endif using namespace std; int main() { #ifdef HOME ifstream cin("A.in"); ofstream cout("A.out"); #endif int i, n, m, v, p; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m >> v >> p; vector<int> a(n + 1); for (i = 1; i <= n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); vector<ll> x(n + 1); vector<bool> vis(n + 1); auto check = [&](int pos) -> bool { int cur = a[pos] + m; int big = 0; for (i = 1; i <= n; i++) { if (a[i] > cur) { big++; } } if (big >= p) { return 0; } fill(vis.begin(), vis.end(), 0); int sz = n; for (i = n; i >= 1; i--) { if (i == pos) continue; if (a[i] > cur) { x[sz--] = 1e18; vis[i] = 1; } else { if (big < p - 1) { x[sz--] = 1e18; big++; vis[i] = 1; } } } for (i = 1; i <= n; i++) { if (i == pos || vis[i]) continue; x[sz--] = cur - a[i]; } x[1] = 0; pos = 0; ll sum = 0; for (int j = 1; j <= n; j++) { if (x[j] == 0) continue; ll k = min(x[j] - 1, 1LL * m); while (pos <= n && x[pos] <= k) { sum += x[pos]; pos++; } if (sum + 1LL * k * (n - pos + 1) < 1LL * k * (v - 1)) { return 0; } } return 1; }; int res = 0; for (int step = 1 << 18; step; step >>= 1) { if (res + step <= n && check(res + step) == 0) { res += step; } } cout << n - res; return 0; }
replace
131
132
131
136
TLE
p02824
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; using WGraph = vector<vector<pair<int, ll>>>; int main() { ll n, m, v, p; cin >> n >> m >> v >> p; vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } sort(a.begin(), a.end(), greater<ll>()); vector<ll> plusM(n); for (int i = 0; i < n; i++) { plusM.at(i) = a.at(i) + m; } ll cnt = p; ll larger_cnt = 0; ll larger_sum = 0; for (ll i = p; i < n; i++) { ll cur = plusM.at(i); if (a.at(p - 1) > cur) { break; } ll point = m * (v - (p - 1) - (n - i)); ll cur_larger = plusM.rend() - upper_bound(plusM.rbegin() + n - i, plusM.rend() - p + 1, cur) - (p - 1); ll cur_smaller = i - p + 1 - cur_larger; for (int j = p - 1 + larger_cnt; j < p + cur_larger - 1; j++) { larger_sum += cur - a.at(j); } larger_cnt = cur_larger; if (point <= cur_smaller * m + larger_sum) { cnt++; larger_sum -= larger_cnt * (cur - plusM.at(i + 1)); } else { break; } } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; using WGraph = vector<vector<pair<int, ll>>>; int main() { ll n, m, v, p; cin >> n >> m >> v >> p; vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } sort(a.begin(), a.end(), greater<ll>()); vector<ll> plusM(n); for (int i = 0; i < n; i++) { plusM.at(i) = a.at(i) + m; } ll cnt = p; ll larger_cnt = 0; ll larger_sum = 0; for (ll i = p; i < n; i++) { ll cur = plusM.at(i); if (a.at(p - 1) > cur) { break; } ll point = m * (v - (p - 1) - (n - i)); ll cur_larger = plusM.rend() - upper_bound(plusM.rbegin() + n - i, plusM.rend() - p + 1, cur) - (p - 1); ll cur_smaller = i - p + 1 - cur_larger; for (int j = p - 1 + larger_cnt; j < p + cur_larger - 1; j++) { larger_sum += cur - a.at(j); } larger_cnt = cur_larger; if (point <= cur_smaller * m + larger_sum) { cnt++; if (i != n - 1) { larger_sum -= larger_cnt * (cur - plusM.at(i + 1)); } } else { break; } } cout << cnt << endl; return 0; }
replace
39
40
39
42
0
p02824
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; long long Sum[100005], A[100005]; long long extra(int poz, int len, int init) { return (Sum[poz] - Sum[poz - len]) - 1LL * len * init; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, v, p; cin >> n >> m >> v >> p; for (int i = 1; i <= n; i++) cin >> A[i]; sort(A + 1, A + n + 1); for (int i = 1; i <= n; i++) Sum[i] = Sum[i - 1] + A[i]; int rez = 0; int ind = 1; for (int i = 1; i <= n; i++) { while (A[ind + 1] <= A[i] + m) ind++; if (n - ind + 1 > p) continue; long long cr = 1LL * (i - 1) * m + 1LL * (n - ind + 1) * m + 1LL * (ind - i) * (A[i] + m) - (Sum[ind] - Sum[i]); long long total = 1LL * m * v; if (cr >= total) { rez++; continue; } int add = 0; for (int j = 16; j >= 0; j--) if (ind - (add + (1 << j)) + 1 > i && cr + extra(ind, add + (1 << j), A[i]) < total) add += (1 << j); add++; if (n - ind + 1 + add <= p) rez++; } cout << rez << "\n"; return 0; }
#include <algorithm> #include <iostream> using namespace std; long long Sum[100005], A[100005]; long long extra(int poz, int len, int init) { return (Sum[poz] - Sum[poz - len]) - 1LL * len * init; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, v, p; cin >> n >> m >> v >> p; for (int i = 1; i <= n; i++) cin >> A[i]; sort(A + 1, A + n + 1); for (int i = 1; i <= n; i++) Sum[i] = Sum[i - 1] + A[i]; int rez = 0; int ind = 1; for (int i = 1; i <= n; i++) { while (A[ind + 1] <= A[i] + m && ind + 1 <= n) ind++; if (n - ind + 1 > p) continue; long long cr = 1LL * (i - 1) * m + 1LL * (n - ind + 1) * m + 1LL * (ind - i) * (A[i] + m) - (Sum[ind] - Sum[i]); long long total = 1LL * m * v; if (cr >= total) { rez++; continue; } int add = 0; for (int j = 16; j >= 0; j--) if (ind - (add + (1 << j)) + 1 > i && cr + extra(ind, add + (1 << j), A[i]) < total) add += (1 << j); add++; if (n - ind + 1 + add <= p) rez++; } cout << rez << "\n"; return 0; }
replace
29
30
29
30
-11
p02825
C++
Runtime Error
#include <bits/stdc++.h> #define fst first #define sec second #define MAX_N (1 << 20) #define rep(i, a, b) for (int i = a; i < b; i++) #define rer(i, a, b) for (int i = a - 1; i >= b; i--) #define sz(a) int(a.size()) using namespace std; typedef vector<int> vi; typedef long long int ll; const ll linf = (1ll << 60); int N; char ans[1010][1010]; string three[3] = {"aac", "b.c", "bdd"}; string four[4] = {"aacd", "bbcd", "efgg", "efhh"}; string five[5] = {"abbcc", "add.e", "i..je", "i..jf", "hhggf"}; string seven[7] = { "aabbcc.", "ggh...d", "j.h...d", "jii...e", "...kkle", "...n.lf", "...nmmf", }; int main() { cin >> N; rep(i, 0, N) { rep(j, 0, N) { ans[i][j] = '.'; } } if (N == 2) cout << -1 << "\n"; else { if (N == 7 || N == 11) { ans[0][0] = 'z'; ans[0][1] = 'z'; ans[0][2] = 'y'; ans[0][3] = 'y'; ans[0][4] = 'x'; ans[0][5] = 'x'; ans[1][6] = 'z'; ans[2][6] = 'z'; ans[3][6] = 'y'; ans[4][6] = 'y'; ans[5][6] = 'x'; ans[6][6] = 'x'; rep(i, 0, 3) { rep(j, 0, 3) { ans[i + 1][j] = three[i][j]; } } rep(i, 0, 3) { rep(j, 0, 3) { ans[i + 4][j + 3] = three[i][j]; } } if (N == 11) { rep(i, 0, 4) { rep(j, 0, 4) { ans[i + 7][j + 7] = four[i][j]; } } } } else if (N % 3 == 0) { rep(k, 0, N / 3) { rep(i, 0, 3) { rep(j, 0, 3) { ans[k * 3 + i][k * 3 + j] = three[i][j]; } } } } else { int a = -1, b = -1; for (int i = 0; i <= N; i++) { if (N - 4 * i >= 0 && (N - 4 * i) % 5 == 0) { a = i; b = (N - 3 * i) / 5; } } if (a != -1) { int at = 0; while (a--) { rep(i, 0, 4) { rep(j, 0, 4) { ans[at + i][at + j] = four[i][j]; } } at += 4; } while (b--) { rep(i, 0, 5) { rep(j, 0, 5) { ans[at + i][at + j] = five[i][j]; } } at += 5; } } } rep(i, 0, N) { rep(j, 0, N) { cout << ans[i][j]; } cout << "\n"; } } }
#include <bits/stdc++.h> #define fst first #define sec second #define MAX_N (1 << 20) #define rep(i, a, b) for (int i = a; i < b; i++) #define rer(i, a, b) for (int i = a - 1; i >= b; i--) #define sz(a) int(a.size()) using namespace std; typedef vector<int> vi; typedef long long int ll; const ll linf = (1ll << 60); int N; char ans[1010][1010]; string three[3] = {"aac", "b.c", "bdd"}; string four[4] = {"aacd", "bbcd", "efgg", "efhh"}; string five[5] = {"abbcc", "add.e", "i..je", "i..jf", "hhggf"}; string seven[7] = { "aabbcc.", "ggh...d", "j.h...d", "jii...e", "...kkle", "...n.lf", "...nmmf", }; int main() { cin >> N; rep(i, 0, N) { rep(j, 0, N) { ans[i][j] = '.'; } } if (N == 2) cout << -1 << "\n"; else { if (N == 7 || N == 11) { ans[0][0] = 'z'; ans[0][1] = 'z'; ans[0][2] = 'y'; ans[0][3] = 'y'; ans[0][4] = 'x'; ans[0][5] = 'x'; ans[1][6] = 'z'; ans[2][6] = 'z'; ans[3][6] = 'y'; ans[4][6] = 'y'; ans[5][6] = 'x'; ans[6][6] = 'x'; rep(i, 0, 3) { rep(j, 0, 3) { ans[i + 1][j] = three[i][j]; } } rep(i, 0, 3) { rep(j, 0, 3) { ans[i + 4][j + 3] = three[i][j]; } } if (N == 11) { rep(i, 0, 4) { rep(j, 0, 4) { ans[i + 7][j + 7] = four[i][j]; } } } } else if (N % 3 == 0) { rep(k, 0, N / 3) { rep(i, 0, 3) { rep(j, 0, 3) { ans[k * 3 + i][k * 3 + j] = three[i][j]; } } } } else { int a = -1, b = -1; for (int i = 0; i <= N; i++) { if (N - 4 * i >= 0 && (N - 4 * i) % 5 == 0) { a = i; b = (N - 4 * i) / 5; } } if (a != -1) { int at = 0; while (a--) { rep(i, 0, 4) { rep(j, 0, 4) { ans[at + i][at + j] = four[i][j]; } } at += 4; } while (b--) { rep(i, 0, 5) { rep(j, 0, 5) { ans[at + i][at + j] = five[i][j]; } } at += 5; } } } rep(i, 0, N) { rep(j, 0, N) { cout << ans[i][j]; } cout << "\n"; } } }
replace
66
67
66
67
0
p02825
C++
Runtime Error
#include <algorithm> #include <chrono> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <ratio> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> using namespace std; int n; using TField = vector<string>; TField GetField3() { return {"aad", "b.d", "bcc"}; } TField GetField4() { return {"abee", "abff", "eeab", "ffab"}; } TField GetField5() { return {"aadaa", "b.dbb", "bccdd", "abd..", "abd.."}; } TField GetField7() { return {"aad...x", "b.d...x", "bcc...y", "...aady", "...b.dz", "...bccz", "xxyyzz."}; } int InferN(const TField &a) { int n = a.size(); for (const auto &s : a) { assert(s.size() == n); } return n; } TField CombineOnDiagonal(const TField &lhs, const TField &rhs) { int n1 = InferN(lhs); int n2 = InferN(rhs); int n = n1 + n2; TField result(n); for (int i = 0; i < n; ++i) { result[i].resize(n); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i < n1 && j < n1) { result[i][j] = lhs[i][j]; } else if (i >= n1 && j >= n1) { result[i][j] = rhs[i][j]; } else { result[i][j] = '.'; } } } return result; } void Show(const TField &a) { for (const auto &s : a) { cout << s << '\n'; } cout.flush(); } void Solve() { switch (n) { case 2: { cout << -1 << endl; break; } case 3: { Show(GetField3()); break; } case 4: { Show(GetField4()); break; } case 5: { Show(GetField5()); break; } case 6: { Show(CombineOnDiagonal(GetField3(), GetField3())); break; } case 7: { Show(GetField7()); break; } case 8: { Show(CombineOnDiagonal(GetField4(), GetField4())); break; } case 9: { Show(CombineOnDiagonal(CombineOnDiagonal(GetField3(), GetField3()), GetField3())); break; } case 10: { Show(CombineOnDiagonal(GetField5(), GetField5())); break; } case 11: { Show(CombineOnDiagonal(GetField4(), GetField7())); break; } case 12: { Show(CombineOnDiagonal(CombineOnDiagonal(GetField4(), GetField4()), GetField4())); break; } case 13: { Show(CombineOnDiagonal(CombineOnDiagonal(GetField4(), GetField4()), GetField5())); break; } case 14: { Show(CombineOnDiagonal(CombineOnDiagonal(GetField5(), GetField5()), GetField4())); break; } default: { assert(n >= 15); bool found = false; for (int k = 0; k <= 3; ++k) { int r = n - k * 5; assert(r >= 0); if (r % 4 == 0) { found = true; TField result; for (int i = 0; i < k; ++i) { result = CombineOnDiagonal(result, GetField5()); } for (int i = 0; i < r / 4; ++i) { result = CombineOnDiagonal(result, GetField4()); } Show(result); } } assert(found); break; } } } bool Read() { if (!(cin >> n)) { return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); while (Read()) { Solve(); } return 0; }
#include <algorithm> #include <chrono> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <ratio> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> using namespace std; int n; using TField = vector<string>; TField GetField3() { return {"aad", "b.d", "bcc"}; } TField GetField4() { return {"abee", "abff", "eeab", "ffab"}; } TField GetField5() { return {"aadaa", "b.dbb", "bccdd", "abd..", "abd.."}; } TField GetField7() { return {"aad...x", "b.d...x", "bcc...y", "...aady", "...b.dz", "...bccz", "xxyyzz."}; } int InferN(const TField &a) { int n = a.size(); for (const auto &s : a) { assert(s.size() == n); } return n; } TField CombineOnDiagonal(const TField &lhs, const TField &rhs) { int n1 = InferN(lhs); int n2 = InferN(rhs); int n = n1 + n2; TField result(n); for (int i = 0; i < n; ++i) { result[i].resize(n); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i < n1 && j < n1) { result[i][j] = lhs[i][j]; } else if (i >= n1 && j >= n1) { result[i][j] = rhs[i - n1][j - n1]; } else { result[i][j] = '.'; } } } return result; } void Show(const TField &a) { for (const auto &s : a) { cout << s << '\n'; } cout.flush(); } void Solve() { switch (n) { case 2: { cout << -1 << endl; break; } case 3: { Show(GetField3()); break; } case 4: { Show(GetField4()); break; } case 5: { Show(GetField5()); break; } case 6: { Show(CombineOnDiagonal(GetField3(), GetField3())); break; } case 7: { Show(GetField7()); break; } case 8: { Show(CombineOnDiagonal(GetField4(), GetField4())); break; } case 9: { Show(CombineOnDiagonal(CombineOnDiagonal(GetField3(), GetField3()), GetField3())); break; } case 10: { Show(CombineOnDiagonal(GetField5(), GetField5())); break; } case 11: { Show(CombineOnDiagonal(GetField4(), GetField7())); break; } case 12: { Show(CombineOnDiagonal(CombineOnDiagonal(GetField4(), GetField4()), GetField4())); break; } case 13: { Show(CombineOnDiagonal(CombineOnDiagonal(GetField4(), GetField4()), GetField5())); break; } case 14: { Show(CombineOnDiagonal(CombineOnDiagonal(GetField5(), GetField5()), GetField4())); break; } default: { assert(n >= 15); bool found = false; for (int k = 0; k <= 3; ++k) { int r = n - k * 5; assert(r >= 0); if (r % 4 == 0) { found = true; TField result; for (int i = 0; i < k; ++i) { result = CombineOnDiagonal(result, GetField5()); } for (int i = 0; i < r / 4; ++i) { result = CombineOnDiagonal(result, GetField4()); } Show(result); } } assert(found); break; } } } bool Read() { if (!(cin >> n)) { return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); while (Read()) { Solve(); } return 0; }
replace
53
54
53
54
-11
p02825
C++
Runtime Error
#include <iostream> #include <string> int n, k; std::string s[4][7] = { {"aacd", "bbcd", "cdaa", "cdbb"}, {"aa.ab", ".bbab", "a.cca", "a.a.a", "bbabb"}, {"aacd..", "bbcd..", "cd..aa", "cd..bb", "..aacd", "..bbcd"}, {"aacd...", "bbcd...", "cd.aa..", "cd...bb", "..a.baa", "..a.b.b", "..bbaab"}}; int main() { std::cin >> n, k = n & 3; if (n < 3) std::cout << "-1\n"; else if (n == 3) std::cout << "aab\nb.b\nbaa\n"; else for (int i = 1; i <= n; ++i) { std::string t(n, '.'); i < n - k - 4 ? t.replace(i / 4 * 4, 4, s[0][i % 4]) : t.replace(n - k - 4, k + 4, s[k][i - n + k + 4]); std::cout << t << '\n'; } }
#include <iostream> #include <string> int n, k; std::string s[4][7] = { {"aacd", "bbcd", "cdaa", "cdbb"}, {"aa.ab", ".bbab", "a.cca", "a.a.a", "bbabb"}, {"aacd..", "bbcd..", "cd..aa", "cd..bb", "..aacd", "..bbcd"}, {"aacd...", "bbcd...", "cd.aa..", "cd...bb", "..a.baa", "..a.b.b", "..bbaab"}}; int main() { std::cin >> n, k = n & 3; if (n < 3) std::cout << "-1\n"; else if (n == 3) std::cout << "aab\nb.b\nbaa\n"; else for (int i = 0; i < n; ++i) { std::string t(n, '.'); i < n - k - 4 ? t.replace(i / 4 * 4, 4, s[0][i % 4]) : t.replace(n - k - 4, k + 4, s[k][i - n + k + 4]); std::cout << t << '\n'; } }
replace
16
17
16
17
0
p02825
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; #define rep(i, n) for (int i = 0; i < int(n); i++) #define rep1(i, n) for (int i = 1; i <= int(n); i++) #define per(i, n) for (int i = int(n) - 1; i >= 0; i--) #define per1(i, n) for (int i = int(n); i > 0; i--) #define all(c) c.begin(), c.end() #define si(x) int(x.size()) #define pb emplace_back #define fs first #define sc second template <class T> using V = vector<T>; template <class T> using VV = vector<vector<T>>; template <class T, class U> void chmax(T &x, U y) { if (x < y) x = y; } template <class T, class U> void chmin(T &x, U y) { if (y < x) x = y; } template <class T> void mkuni(V<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <class S, class T> ostream &operator<<(ostream &o, const pair<S, T> &p) { return o << "(" << p.fs << "," << p.sc << ")"; } template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) { o << "{"; for (const T &v : vc) o << v << ","; o << "}"; return o; } constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } #ifdef LOCAL #define show(x) \ cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl void dmpr(ostream &os) { os << endl; } template <class T, class... Args> void dmpr(ostream &os, const T &t, const Args &...args) { os << t << " ~ "; dmpr(os, args...); } #define shows(...) \ cerr << "LINE" << __LINE__ << " : "; \ dmpr(cerr, ##__VA_ARGS__) #define dump(x) \ cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \ for (auto v : x) \ cerr << v << ","; \ cerr << "}" << endl; #else #define show(x) void(0) #define dump(x) void(0) #define shows(...) void(0) #endif int main() { cin.tie(0); ios::sync_with_stdio(false); // DON'T USE scanf/printf/puts !! cout << fixed << setprecision(20); int N; cin >> N; assert(N == 5); cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; #define rep(i, n) for (int i = 0; i < int(n); i++) #define rep1(i, n) for (int i = 1; i <= int(n); i++) #define per(i, n) for (int i = int(n) - 1; i >= 0; i--) #define per1(i, n) for (int i = int(n); i > 0; i--) #define all(c) c.begin(), c.end() #define si(x) int(x.size()) #define pb emplace_back #define fs first #define sc second template <class T> using V = vector<T>; template <class T> using VV = vector<vector<T>>; template <class T, class U> void chmax(T &x, U y) { if (x < y) x = y; } template <class T, class U> void chmin(T &x, U y) { if (y < x) x = y; } template <class T> void mkuni(V<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <class S, class T> ostream &operator<<(ostream &o, const pair<S, T> &p) { return o << "(" << p.fs << "," << p.sc << ")"; } template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) { o << "{"; for (const T &v : vc) o << v << ","; o << "}"; return o; } constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } #ifdef LOCAL #define show(x) \ cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl void dmpr(ostream &os) { os << endl; } template <class T, class... Args> void dmpr(ostream &os, const T &t, const Args &...args) { os << t << " ~ "; dmpr(os, args...); } #define shows(...) \ cerr << "LINE" << __LINE__ << " : "; \ dmpr(cerr, ##__VA_ARGS__) #define dump(x) \ cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \ for (auto v : x) \ cerr << v << ","; \ cerr << "}" << endl; #else #define show(x) void(0) #define dump(x) void(0) #define shows(...) void(0) #endif int main() { cin.tie(0); ios::sync_with_stdio(false); // DON'T USE scanf/printf/puts !! cout << fixed << setprecision(20); int N; cin >> N; if (N == 2) { cout << -1 << endl; return 0; } if (N == 3) { cout << "aab" << endl; cout << "d.b" << endl; cout << "dcc" << endl; return 0; } if (N == 7) { cout << "aabbcc." << endl; cout << "dde...a" << endl; cout << "g.e...a" << endl; cout << "gff...b" << endl; cout << "...ddeb" << endl; cout << "...g.ec" << endl; cout << "...gffc" << endl; return 0; } V<string> s[10]; s[4] = {"abcc", "abdd", "eegh", "ffgh"}; s[5] = {"aabbc", "hii.c", "h..jd", "g..jd", "gffee"}; s[6] = {"aabbcc", "xxyyzz", "a.a.a.", "a.a.a.", ".a.a.a", ".a.a.a"}; rep(n5, 4) rep(n6, 4) { int n4 = N - n5 * 5 - n6 * 6; if (n4 < 0 || n4 % 4) continue; n4 /= 4; V<string> a(N, string(N, '.')); int o = 0; rep(i, n4) { rep(x, 4) rep(y, 4) a[o + x][o + y] = s[4][x][y]; o += 4; } rep(i, n5) { rep(x, 5) rep(y, 5) a[o + x][o + y] = s[5][x][y]; o += 5; } rep(i, n6) { rep(x, 6) rep(y, 6) a[o + x][o + y] = s[6][x][y]; o += 6; } rep(i, N) cout << a[i] << endl; return 0; } }
replace
70
72
70
116
-6
0005cd30-2319-43d2-84bf-696125f62b3b.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02825/C++/s218399334.cpp:53: int main(): Assertion `N == 5' failed.
p02825
C++
Runtime Error
#include <algorithm> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define Inf 1000000000 #define nmax_def 110000 #define mod 1000000007 #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; int N; vector<string> ans; int main() { cin >> N; ans.resize(N); rep(i, N) { string anstmp; anstmp = ""; rep(j, N) { anstmp.push_back('.'); } ans[i] = anstmp; } if (N == 2) { cout << "-1" << endl; } else if (N % 2 == 0) { int N2 = N / 2; rep(i, N2) { int j = i; ans[i * 2][j * 2] = 'a'; ans[i * 2][j * 2 + 1] = 'a'; ans[i * 2 + 1][j * 2] = 'b'; ans[i * 2 + 1][j * 2 + 1] = 'b'; j++; j %= N2; ans[i * 2][j * 2] = 'c'; ans[i * 2][j * 2 + 1] = 'd'; ans[i * 2 + 1][j * 2] = 'c'; ans[i * 2 + 1][j * 2 + 1] = 'd'; } rep(i, N) { cout << ans[i] << endl; } } else { int N2 = (N - 1) / 2; if (N >= 5) { int x, y; x = 0; y = 0; rep(i, N2 / 2 - 1) { ans[x][y] = 'a'; ans[x + 1][y] = 'e'; ans[x][y + 1] = 'a'; ans[x + 1][y + 1] = 'e'; x += 2; y += 2; } x = N - 1; y = 0; rep(i, N2 / 2 - 1) { ans[x][y] = 'b'; ans[x - 1][y] = 'b'; ans[x][y + 1] = 'f'; ans[x - 1][y + 1] = 'f'; x -= 2; y += 2; } x = N - 1; y = N - 1; rep(i, N2 / 2 - 1) { ans[x][y] = 'c'; ans[x - 1][y] = 'g'; ans[x][y - 1] = 'c'; ans[x - 1][y - 1] = 'g'; x -= 2; y -= 2; } x = 0; y = N - 1; rep(i, N2 / 2 - 1) { ans[x][y] = 'd'; ans[x + 1][y] = 'd'; ans[x][y - 1] = 'h'; ans[x + 1][y - 1] = 'h'; x += 2; y -= 2; } } if (N2 % 2 == 1) { int x = 0; int y = 0; int count = 0; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; y++; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; y++; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; y++; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; y++; x++; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; y++; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; y++; x = 0; y = 6; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; x++; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; x++; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; x++; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; x++; y--; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; x++; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; x++; x = 6; y = 6; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; y--; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; y--; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; y--; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; y--; x--; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; y--; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; y--; x = 6; y = 0; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; x--; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; x--; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; x--; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; x--; y++; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; x--; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; x--; ans[N2 - 1][N2 - 1] = 'i'; ans[N2 - 1][N2] = 'i'; ans[N2][N2 + 1] = 'j'; ans[N2 + 1][N2 + 1] = 'j'; } else { int x = 0; int y = 0; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; y++; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; y++; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; y++; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; y++; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; x++; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; x++; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; x++; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; x++; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; y--; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; y--; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; y--; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; y--; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; x--; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; x--; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; x--; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; x--; ans[N2 - 1][N2 - 1] = 'i'; ans[N2 - 1][N2] = 'i'; ans[N2][N2 + 1] = 'j'; ans[N2 + 1][N2 + 1] = 'j'; } rep(i, N) { cout << ans[i] << endl; } } return 0; }
#include <algorithm> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define Inf 1000000000 #define nmax_def 110000 #define mod 1000000007 #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; int N; vector<string> ans; int main() { cin >> N; ans.resize(N); rep(i, N) { string anstmp; anstmp = ""; rep(j, N) { anstmp.push_back('.'); } ans[i] = anstmp; } if (N == 2) { cout << "-1" << endl; } else if (N == 3) { ans[0][0] = 'a'; ans[0][1] = 'a'; ans[0][2] = 'b'; ans[1][2] = 'b'; ans[2][2] = 'c'; ans[2][1] = 'c'; ans[2][0] = 'd'; ans[1][0] = 'd'; rep(i, N) { cout << ans[i] << endl; } } else if (N % 2 == 0) { int N2 = N / 2; rep(i, N2) { int j = i; ans[i * 2][j * 2] = 'a'; ans[i * 2][j * 2 + 1] = 'a'; ans[i * 2 + 1][j * 2] = 'b'; ans[i * 2 + 1][j * 2 + 1] = 'b'; j++; j %= N2; ans[i * 2][j * 2] = 'c'; ans[i * 2][j * 2 + 1] = 'd'; ans[i * 2 + 1][j * 2] = 'c'; ans[i * 2 + 1][j * 2 + 1] = 'd'; } rep(i, N) { cout << ans[i] << endl; } } else { int N2 = (N - 1) / 2; if (N >= 5) { int x, y; x = 0; y = 0; rep(i, N2 / 2 - 1) { ans[x][y] = 'a'; ans[x + 1][y] = 'e'; ans[x][y + 1] = 'a'; ans[x + 1][y + 1] = 'e'; x += 2; y += 2; } x = N - 1; y = 0; rep(i, N2 / 2 - 1) { ans[x][y] = 'b'; ans[x - 1][y] = 'b'; ans[x][y + 1] = 'f'; ans[x - 1][y + 1] = 'f'; x -= 2; y += 2; } x = N - 1; y = N - 1; rep(i, N2 / 2 - 1) { ans[x][y] = 'c'; ans[x - 1][y] = 'g'; ans[x][y - 1] = 'c'; ans[x - 1][y - 1] = 'g'; x -= 2; y -= 2; } x = 0; y = N - 1; rep(i, N2 / 2 - 1) { ans[x][y] = 'd'; ans[x + 1][y] = 'd'; ans[x][y - 1] = 'h'; ans[x + 1][y - 1] = 'h'; x += 2; y -= 2; } } if (N2 % 2 == 1) { int x = 0; int y = 0; int count = 0; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; y++; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; y++; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; y++; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; y++; x++; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; y++; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; y++; x = 0; y = 6; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; x++; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; x++; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; x++; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; x++; y--; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; x++; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; x++; x = 6; y = 6; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; y--; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; y--; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; y--; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; y--; x--; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; y--; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; y--; x = 6; y = 0; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; x--; ans[N2 - 3 + x][N2 - 3 + y] = 'a'; x--; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; x--; ans[N2 - 3 + x][N2 - 3 + y] = 'b'; x--; y++; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; x--; ans[N2 - 3 + x][N2 - 3 + y] = 'c'; x--; ans[N2 - 1][N2 - 1] = 'i'; ans[N2 - 1][N2] = 'i'; ans[N2][N2 + 1] = 'j'; ans[N2 + 1][N2 + 1] = 'j'; } else { int x = 0; int y = 0; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; y++; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; y++; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; y++; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; y++; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; x++; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; x++; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; x++; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; x++; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; y--; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; y--; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; y--; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; y--; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; x--; ans[N2 - 2 + x][N2 - 2 + y] = 'a'; x--; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; x--; ans[N2 - 2 + x][N2 - 2 + y] = 'b'; x--; ans[N2 - 1][N2 - 1] = 'i'; ans[N2 - 1][N2] = 'i'; ans[N2][N2 + 1] = 'j'; ans[N2 + 1][N2 + 1] = 'j'; } rep(i, N) { cout << ans[i] << endl; } } return 0; }
insert
36
36
36
46
0
p02825
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; using WGraph = vector<vector<pair<int, ll>>>; vector<string> three = {"a..", "a..", ".bb"}; vector<string> four = {"ahff", "ahgg", "bbde", "ccde"}; vector<string> five = {"ahhgg", "ai..f", "bi..f", "b.jje", "ccdde"}; vector<string> six = {"aa..hi", "bb..hi", "cc..jk", "..ddjk", "..eelm", "..fflm"}; vector<string> seven = {"ahhii..", "aj..mm.", "bjk....", "b.k..oo", "c..l..p", "c..l..p", ".ddeeff"}; int main() { int n = 0; cin >> n; if (n == 2) { cout << -1 << endl; } else if (n == 3) { for (auto s : three) { cout << s << endl; } } else { vector<string> ans(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ans.at(i).push_back('.'); } } int mod = n % 4; if (mod == 0) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { ans.at(i).at(j) = four.at(i).at(j); } } } else if (mod == 1) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { ans.at(i).at(j) = five.at(i).at(j); } } } else if (mod == 2) { for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { ans.at(i).at(j) = six.at(i).at(j); } } } else if (mod == 3) { for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { ans.at(i).at(j) = seven.at(i).at(j); } } } int cur = 4 + mod; while (cur + 4 <= n) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { ans.at(cur + i).at(cur + j) = four.at(i).at(j); } } } for (auto s : ans) { cout << s << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; using WGraph = vector<vector<pair<int, ll>>>; vector<string> three = {"a..", "a..", ".bb"}; vector<string> four = {"ahff", "ahgg", "bbde", "ccde"}; vector<string> five = {"ahhgg", "ai..f", "bi..f", "b.jje", "ccdde"}; vector<string> six = {"aa..hi", "bb..hi", "cc..jk", "..ddjk", "..eelm", "..fflm"}; vector<string> seven = {"ahhii..", "aj..mm.", "bjk....", "b.k..oo", "c..l..p", "c..l..p", ".ddeeff"}; int main() { int n = 0; cin >> n; if (n == 2) { cout << -1 << endl; } else if (n == 3) { for (auto s : three) { cout << s << endl; } } else { vector<string> ans(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ans.at(i).push_back('.'); } } int mod = n % 4; if (mod == 0) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { ans.at(i).at(j) = four.at(i).at(j); } } } else if (mod == 1) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { ans.at(i).at(j) = five.at(i).at(j); } } } else if (mod == 2) { for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { ans.at(i).at(j) = six.at(i).at(j); } } } else if (mod == 3) { for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { ans.at(i).at(j) = seven.at(i).at(j); } } } int cur = 4 + mod; while (cur + 4 <= n) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { ans.at(cur + i).at(cur + j) = four.at(i).at(j); } } cur += 4; } for (auto s : ans) { cout << s << endl; } } return 0; }
insert
64
64
64
65
TLE
p02825
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, a) for (int i = 0; i < (a); i++) #define ALL(a) (a).begin(), (a).end() typedef long long ll; typedef pair<int, int> P; const int INF = 1e9; const long long LINF = 1e18; const long long MOD = 1e9 + 7; signed main() { int n; cin >> n; if (n < 3) { cout << -1 << endl; return 0; } if (n % 3 == 0) { vector<vector<char>> ans(n, vector<char>(n, '.')); REP(i, n) { REP(j, n) { if (i == j && i % 3 == 0 && j % 3 == 0) { ans[i][j] = 'a'; ans[i][j + 1] = 'a'; ans[i][j + 2] = 'b'; ans[i + 1][j] = 'b'; ans[i + 1][j + 2] = 'b'; ans[i + 2][j] = 'b'; ans[i + 2][j + 1] = 'a'; ans[i + 2][j + 2] = 'a'; } } } } vector<vector<char>> ans(n, vector<char>(n, '.')); if (n % 4 == 1) { char d5[5][5] = { 'a', 'a', 'b', 'b', 'a', 'b', 'c', 'c', '.', 'a', 'b', 'a', 'a', 'b', 'b', 'a', '.', '.', 'a', 'c', 'a', '.', '.', 'a', 'c', }; REP(i, 5) { REP(j, 5) { ans[i][j] = d5[i][j]; } } } else if (n % 4 == 2) { char d6[6][6] = { 'a', 'a', 'b', 'b', 'a', 'a', 'b', 'c', 'a', '.', '.', '.', 'b', 'c', 'a', '.', '.', '.', '.', '.', '.', 'a', 'b', 'a', '.', '.', '.', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'c', 'c', }; REP(i, 6) { REP(j, 6) { ans[i][j] = d6[i][j]; } } } else if (n % 4 == 3) { char d7[7][7] = { 'a', 'a', 'b', 'b', 'a', 'a', '.', 'b', 'c', '.', '.', '.', '.', 'a', 'b', 'c', '.', '.', '.', '.', 'a', 'a', 'b', '.', '.', '.', '.', 'b', 'a', 'b', '.', '.', '.', '.', 'b', '.', '.', 'a', 'a', 'b', 'b', 'c', '.', '.', 'b', 'b', 'a', 'a', 'c', }; REP(i, 7) { REP(j, 7) { ans[i][j] = d7[i][j]; } } } char d4[4][4] = { 'a', 'a', 'c', 'a', 'b', 'b', 'c', 'a', 'a', 'c', 'd', 'd', 'a', 'c', 'a', 'a', }; int t = 4; t += n % 4; if (n % 4 == 0) t = 0; for (int i = t; i < n; i++) { for (int j = t; j < n; j++) { if (i - t == j - t && (i - t) % 4 == 0 && (j - t) % 4 == 0) { REP(k, 4) { REP(l, 4) { ans[i + k][j + l] = d4[k][l]; } } } } } REP(i, n) { REP(j, n) { cout << ans[i][j]; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, a) for (int i = 0; i < (a); i++) #define ALL(a) (a).begin(), (a).end() typedef long long ll; typedef pair<int, int> P; const int INF = 1e9; const long long LINF = 1e18; const long long MOD = 1e9 + 7; signed main() { int n; cin >> n; if (n < 3) { cout << -1 << endl; return 0; } if (n == 3) { cout << "aab" << endl; cout << "b.b" << endl; cout << "baa" << endl; return 0; } vector<vector<char>> ans(n, vector<char>(n, '.')); if (n % 4 == 1) { char d5[5][5] = { 'a', 'a', 'b', 'b', 'a', 'b', 'c', 'c', '.', 'a', 'b', 'a', 'a', 'b', 'b', 'a', '.', '.', 'a', 'c', 'a', '.', '.', 'a', 'c', }; REP(i, 5) { REP(j, 5) { ans[i][j] = d5[i][j]; } } } else if (n % 4 == 2) { char d6[6][6] = { 'a', 'a', 'b', 'b', 'a', 'a', 'b', 'c', 'a', '.', '.', '.', 'b', 'c', 'a', '.', '.', '.', '.', '.', '.', 'a', 'b', 'a', '.', '.', '.', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'c', 'c', }; REP(i, 6) { REP(j, 6) { ans[i][j] = d6[i][j]; } } } else if (n % 4 == 3) { char d7[7][7] = { 'a', 'a', 'b', 'b', 'a', 'a', '.', 'b', 'c', '.', '.', '.', '.', 'a', 'b', 'c', '.', '.', '.', '.', 'a', 'a', 'b', '.', '.', '.', '.', 'b', 'a', 'b', '.', '.', '.', '.', 'b', '.', '.', 'a', 'a', 'b', 'b', 'c', '.', '.', 'b', 'b', 'a', 'a', 'c', }; REP(i, 7) { REP(j, 7) { ans[i][j] = d7[i][j]; } } } char d4[4][4] = { 'a', 'a', 'c', 'a', 'b', 'b', 'c', 'a', 'a', 'c', 'd', 'd', 'a', 'c', 'a', 'a', }; int t = 4; t += n % 4; if (n % 4 == 0) t = 0; for (int i = t; i < n; i++) { for (int j = t; j < n; j++) { if (i - t == j - t && (i - t) % 4 == 0 && (j - t) % 4 == 0) { REP(k, 4) { REP(l, 4) { ans[i + k][j + l] = d4[k][l]; } } } } } REP(i, n) { REP(j, n) { cout << ans[i][j]; } cout << endl; } return 0; }
replace
17
33
17
22
0
p02825
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); if (n == 2) { puts("-1"); return 0; } if (n == 3) { puts("aa."); puts("..b"); puts("..b"); return 0; } vector<vector<int>> p(n, vector<int>(n, -1)); vector<int> col(n), row(n); function<void(int, int, int)> Dfs = [&](int r, int c, int dep) { // printf("r = %d c = %d dep = %d\n", r, c, dep); if (c == n) return Dfs(r + 1, 0, dep); if (r > 1 && row[r - 1] != row[0]) return; if (r == n) { bool ok = true; for (int i = 0; i < n; ++i) ok &= row[i] == row[0]; for (int i = 0; i < n; ++i) ok &= col[i] == row[0]; if (ok) { printf("value = %d\n", row[0]); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (p[i][j] == -1) printf("."); else printf("%c", p[i][j] + 'a'); } puts(""); } puts(""); } return; } if (c + 1 < n && p[r][c] == -1 && p[r][c + 1] == -1) { p[r][c] = p[r][c + 1] = dep; col[c]++, col[c + 1]++, row[r]++; Dfs(r, c + 1, dep + 1); col[c]--, col[c + 1]--, row[r]--; p[r][c] = p[r][c + 1] = -1; } if (r + 1 < n && p[r][c] == -1 && p[r + 1][c] == -1) { p[r][c] = p[r + 1][c] = dep; col[c]++, row[r + 1]++, row[r]++; Dfs(r, c + 1, dep + 1); col[c]--, row[r + 1]--, row[r]--; p[r][c] = p[r + 1][c] = -1; } Dfs(r, c + 1, dep); }; // Dfs(0, 0, 0); const vector<vector<string>> kPattern = { {"aabc", "ddbc", "efgg", "efhh"}, {"..abc", "..abc", "ddeff", "gge.h", "iijjh"}, {"aabb.c", "..deec", "ffd..g", "hh..ig", "..j.ik", "..jllk"}, {"aabbcc.", "dde...f", "..e..gf", "....hgi", "....hji", "...kkjl", "mmnn..l"}}; if (n == 7) { for (int i = 0; i < 7; ++i) printf("%s\n", kPattern[3][i].c_str()); return 0; } for (int i = 0; i * 4 <= n; ++i) { for (int j = 0; i * 5 + j * 5 <= n; ++j) { if ((n - i * 4 - j * 5) % 6 == 0) { int g = 0; vector<string> ans(n, string(n, '.')); for (int k = 0; k < i; ++k) { for (int a = 0; a < 4; ++a) { for (int b = 0; b < 4; ++b) ans[g + a][g + b] = kPattern[0][a][b]; } g += 4; } for (int k = 0; k < j; ++k) { for (int a = 0; a < 5; ++a) { for (int b = 0; b < 5; ++b) ans[g + a][g + b] = kPattern[1][a][b]; } g += 5; } for (int k = 0; g < n; ++k) { for (int a = 0; a < 6; ++a) { for (int b = 0; b < 6; ++b) ans[g + a][g + b] = kPattern[2][a][b]; } g += 6; } for (int k = 0; k < n; ++k) printf("%s\n", ans[k].c_str()); return 0; } } } assert(false); }
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); if (n == 2) { puts("-1"); return 0; } if (n == 3) { puts("aa."); puts("..b"); puts("..b"); return 0; } vector<vector<int>> p(n, vector<int>(n, -1)); vector<int> col(n), row(n); function<void(int, int, int)> Dfs = [&](int r, int c, int dep) { // printf("r = %d c = %d dep = %d\n", r, c, dep); if (c == n) return Dfs(r + 1, 0, dep); if (r > 1 && row[r - 1] != row[0]) return; if (r == n) { bool ok = true; for (int i = 0; i < n; ++i) ok &= row[i] == row[0]; for (int i = 0; i < n; ++i) ok &= col[i] == row[0]; if (ok) { printf("value = %d\n", row[0]); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (p[i][j] == -1) printf("."); else printf("%c", p[i][j] + 'a'); } puts(""); } puts(""); } return; } if (c + 1 < n && p[r][c] == -1 && p[r][c + 1] == -1) { p[r][c] = p[r][c + 1] = dep; col[c]++, col[c + 1]++, row[r]++; Dfs(r, c + 1, dep + 1); col[c]--, col[c + 1]--, row[r]--; p[r][c] = p[r][c + 1] = -1; } if (r + 1 < n && p[r][c] == -1 && p[r + 1][c] == -1) { p[r][c] = p[r + 1][c] = dep; col[c]++, row[r + 1]++, row[r]++; Dfs(r, c + 1, dep + 1); col[c]--, row[r + 1]--, row[r]--; p[r][c] = p[r + 1][c] = -1; } Dfs(r, c + 1, dep); }; // Dfs(0, 0, 0); const vector<vector<string>> kPattern = { {"aabc", "ddbc", "efgg", "efhh"}, {"..abc", "..abc", "ddeff", "gge.h", "iijjh"}, {"aabb.c", "..deec", "ffd..g", "hh..ig", "..j.ik", "..jllk"}, {"aabbcc.", "dde...f", "..e..gf", "....hgi", "....hji", "...kkjl", "mmnn..l"}}; if (n == 7) { for (int i = 0; i < 7; ++i) printf("%s\n", kPattern[3][i].c_str()); return 0; } for (int i = 0; i * 4 <= n; ++i) { for (int j = 0; i * 4 + j * 5 <= n; ++j) { if ((n - i * 4 - j * 5) % 6 == 0) { int g = 0; vector<string> ans(n, string(n, '.')); for (int k = 0; k < i; ++k) { for (int a = 0; a < 4; ++a) { for (int b = 0; b < 4; ++b) ans[g + a][g + b] = kPattern[0][a][b]; } g += 4; } for (int k = 0; k < j; ++k) { for (int a = 0; a < 5; ++a) { for (int b = 0; b < 5; ++b) ans[g + a][g + b] = kPattern[1][a][b]; } g += 5; } for (int k = 0; g < n; ++k) { for (int a = 0; a < 6; ++a) { for (int b = 0; b < 6; ++b) ans[g + a][g + b] = kPattern[2][a][b]; } g += 6; } for (int k = 0; k < n; ++k) printf("%s\n", ans[k].c_str()); return 0; } } } assert(false); }
replace
78
79
78
79
0
p02825
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; int main() { int N; cin >> N; if (N == 2) { cout << -1 << endl; return 0; } if (N == 3) { cout << "aa.\n..b\n..b\n"; return 0; } if (N == 6) { cout << "aa....\n..b...\n..b...\n...aa.\n.....b\n.....b\n"; return 0; } if (N == 7) { cout << "aabbcc.\nddee..h\nffgg..h\n....mki\n....mki\n....nlj\n....nlj\n"; return 0; } if (N == 11) { cout << "aacd.......\nbbcd.......\nefgg.......\nefhh.......\n....aabbcc.\n." "...ddee..h\n....ffgg..h\n........mki\n........mki\n........nlj\n.." "......nlj\n"; } vector<vector<char>> V4 = {{'a', 'a', 'c', 'd'}, {'b', 'b', 'c', 'd'}, {'e', 'f', 'g', 'g'}, {'e', 'f', 'h', 'h'}}; vector<vector<char>> V5 = {{'a', 'a', 'b', 'b', 'c'}, {'h', 'i', 'i', '.', 'c'}, {'h', '.', '.', 'j', 'd'}, {'g', '.', '.', 'j', 'd'}, {'g', 'f', 'f', 'e', 'e'}}; vector<vector<char>> ANS(N, vector<char>(N, '.')); int C4, C5; for (int i = 0;; i++) { if ((N - i * 4) % 5 == 0) { C4 = i; C5 = (N - i * 4) / 5; break; } } int NOW = 0; for (int i = 0; i < C4; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { ANS[NOW + j][NOW + k] = V4[j][k]; } } NOW += 4; } for (int i = 0; i < C5; i++) { for (int j = 0; j < 5; j++) { for (int k = 0; k < 5; k++) { ANS[NOW + j][NOW + k] = V5[j][k]; } } NOW += 5; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << ANS[i][j]; } cout << endl; } }
#include "bits/stdc++.h" using namespace std; int main() { int N; cin >> N; if (N == 2) { cout << -1 << endl; return 0; } if (N == 3) { cout << "aa.\n..b\n..b\n"; return 0; } if (N == 6) { cout << "aa....\n..b...\n..b...\n...aa.\n.....b\n.....b\n"; return 0; } if (N == 7) { cout << "aabbcc.\nddee..h\nffgg..h\n....mki\n....mki\n....nlj\n....nlj\n"; return 0; } if (N == 11) { cout << "aacd.......\nbbcd.......\nefgg.......\nefhh.......\n....aabbcc.\n." "...ddee..h\n....ffgg..h\n........mki\n........mki\n........nlj\n.." "......nlj\n"; return 0; } vector<vector<char>> V4 = {{'a', 'a', 'c', 'd'}, {'b', 'b', 'c', 'd'}, {'e', 'f', 'g', 'g'}, {'e', 'f', 'h', 'h'}}; vector<vector<char>> V5 = {{'a', 'a', 'b', 'b', 'c'}, {'h', 'i', 'i', '.', 'c'}, {'h', '.', '.', 'j', 'd'}, {'g', '.', '.', 'j', 'd'}, {'g', 'f', 'f', 'e', 'e'}}; vector<vector<char>> ANS(N, vector<char>(N, '.')); int C4, C5; for (int i = 0;; i++) { if ((N - i * 4) % 5 == 0) { C4 = i; C5 = (N - i * 4) / 5; break; } } int NOW = 0; for (int i = 0; i < C4; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { ANS[NOW + j][NOW + k] = V4[j][k]; } } NOW += 4; } for (int i = 0; i < C5; i++) { for (int j = 0; j < 5; j++) { for (int k = 0; k < 5; k++) { ANS[NOW + j][NOW + k] = V5[j][k]; } } NOW += 5; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << ANS[i][j]; } cout << endl; } }
insert
26
26
26
27
0
p02825
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { string sep = " "; if (v.size()) os << v[0]; for (int i = 1; i < v.size(); i++) os << sep << v[i]; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (int i = 0; i < v.size(); i++) is >> v[i]; return is; } #ifdef DBG void debug_() { cout << endl; } template <typename T, typename... Args> void debug_(T &&x, Args &&...xs) { cout << x << " "; debug_(forward<Args>(xs)...); } #define dbg(...) debug_(__VA_ARGS__) #else #define dbg(...) #endif void rec(vector<vector<int>> &b, int k, vector<vector<vector<int>>> &res) { int n = b.size(); int mx = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) mx = max(mx, b[i][j]); } int si = 0, sj = 0; if (mx > 0) { bool done = false; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (b[i][j] == mx) { si = i; sj = j; done = true; break; } } if (done) break; } } int x = -1; bool ok = true; for (int i = 0; i < n; i++) { set<int> a; for (int j = 0; j < n; j++) { if (b[i][j] > 0) a.insert(b[i][j]); } if (x < 0) x = a.size(); if (a.size() > k) return; if (x != a.size()) { if (i < si) return; ok = false; } } for (int i = 0; i < n; i++) { set<int> a; for (int j = 0; j < n; j++) { if (b[j][i] > 0) a.insert(b[j][i]); } if (a.size() > k) return; if (x != a.size()) ok = false; } if (ok && x == k) { res.push_back(b); return; } for (int i = si; i < n; i++) { for (int j = (i == si ? sj : 0); j < n; j++) { if (i + 1 < n && b[i][j] == 0 && b[i + 1][j] == 0) { b[i][j] = b[i + 1][j] = mx + 1; rec(b, k, res); if (res.size()) return; b[i][j] = b[i + 1][j] = 0; } if (j + 1 < n && b[i][j] == 0 && b[i][j + 1] == 0) { b[i][j] = b[i][j + 1] = mx + 1; rec(b, k, res); if (res.size()) return; b[i][j] = b[i][j + 1] = 0; } } } } vector<vector<vector<int>>> naive(int n, int k) { vector<vector<vector<int>>> ret; vector<vector<int>> b(n, vector<int>(n)); rec(b, k, ret); return ret; } string x3[3] = { "aab", "c.b", "cdd", }; string x4[4] = { "aabc", "ddbc", "efgg", "efhh", }; string x5[5] = { "..abc", "..abc", "ddeef", "ggh.f", "iihjj", }; string x6[6] = { "abc...", "abc...", "d.eeff", "dghh..", ".g.ijj", "kk.ill", }; string x7[7] = { "abc....", "abc....", "def....", "def....", "g..hhii", "g..jjkk", ".llmmnn", }; int main() { ios_base::sync_with_stdio(false); cout << setprecision(20) << fixed; int n; cin >> n; if (n <= 2) { cout << -1 << endl; return 0; } if (n == 3) { for (int i = 0; i < 3; i++) cout << x3[i] << endl; } int k = n % 4 + 4; int l = n / 4 - 1; for (int i = 0; i < l; i++) { for (int j = 0; j < 4; j++) { cout << string(i * 4, '.') << x4[j] << string(n - 4 * (i + 1), '.') << endl; } } if (k == 4) { for (int j = 0; j < 4; j++) { cout << string(n - 4, '.') << x4[j] << endl; } } else if (k == 5) { for (int j = 0; j < 5; j++) { cout << string(n - 5, '.') << x5[j] << endl; } } else if (k == 6) { for (int j = 0; j < 6; j++) { cout << string(n - 6, '.') << x6[j] << endl; } } else if (k == 7) { for (int j = 0; j < 7; j++) { cout << string(n - 7, '.') << x7[j] << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { string sep = " "; if (v.size()) os << v[0]; for (int i = 1; i < v.size(); i++) os << sep << v[i]; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (int i = 0; i < v.size(); i++) is >> v[i]; return is; } #ifdef DBG void debug_() { cout << endl; } template <typename T, typename... Args> void debug_(T &&x, Args &&...xs) { cout << x << " "; debug_(forward<Args>(xs)...); } #define dbg(...) debug_(__VA_ARGS__) #else #define dbg(...) #endif void rec(vector<vector<int>> &b, int k, vector<vector<vector<int>>> &res) { int n = b.size(); int mx = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) mx = max(mx, b[i][j]); } int si = 0, sj = 0; if (mx > 0) { bool done = false; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (b[i][j] == mx) { si = i; sj = j; done = true; break; } } if (done) break; } } int x = -1; bool ok = true; for (int i = 0; i < n; i++) { set<int> a; for (int j = 0; j < n; j++) { if (b[i][j] > 0) a.insert(b[i][j]); } if (x < 0) x = a.size(); if (a.size() > k) return; if (x != a.size()) { if (i < si) return; ok = false; } } for (int i = 0; i < n; i++) { set<int> a; for (int j = 0; j < n; j++) { if (b[j][i] > 0) a.insert(b[j][i]); } if (a.size() > k) return; if (x != a.size()) ok = false; } if (ok && x == k) { res.push_back(b); return; } for (int i = si; i < n; i++) { for (int j = (i == si ? sj : 0); j < n; j++) { if (i + 1 < n && b[i][j] == 0 && b[i + 1][j] == 0) { b[i][j] = b[i + 1][j] = mx + 1; rec(b, k, res); if (res.size()) return; b[i][j] = b[i + 1][j] = 0; } if (j + 1 < n && b[i][j] == 0 && b[i][j + 1] == 0) { b[i][j] = b[i][j + 1] = mx + 1; rec(b, k, res); if (res.size()) return; b[i][j] = b[i][j + 1] = 0; } } } } vector<vector<vector<int>>> naive(int n, int k) { vector<vector<vector<int>>> ret; vector<vector<int>> b(n, vector<int>(n)); rec(b, k, ret); return ret; } string x3[3] = { "aab", "c.b", "cdd", }; string x4[4] = { "aabc", "ddbc", "efgg", "efhh", }; string x5[5] = { "..abc", "..abc", "ddeef", "ggh.f", "iihjj", }; string x6[6] = { "abc...", "abc...", "d.eeff", "dghh..", ".g.ijj", "kk.ill", }; string x7[7] = { "abc....", "abc....", "def....", "def....", "g..hhii", "g..jjkk", ".llmmnn", }; int main() { ios_base::sync_with_stdio(false); cout << setprecision(20) << fixed; int n; cin >> n; if (n <= 2) { cout << -1 << endl; return 0; } if (n == 3) { for (int i = 0; i < 3; i++) cout << x3[i] << endl; return 0; } int k = n % 4 + 4; int l = n / 4 - 1; for (int i = 0; i < l; i++) { for (int j = 0; j < 4; j++) { cout << string(i * 4, '.') << x4[j] << string(n - 4 * (i + 1), '.') << endl; } } if (k == 4) { for (int j = 0; j < 4; j++) { cout << string(n - 4, '.') << x4[j] << endl; } } else if (k == 5) { for (int j = 0; j < 5; j++) { cout << string(n - 5, '.') << x5[j] << endl; } } else if (k == 6) { for (int j = 0; j < 6; j++) { cout << string(n - 6, '.') << x6[j] << endl; } } else if (k == 7) { for (int j = 0; j < 7; j++) { cout << string(n - 7, '.') << x7[j] << endl; } } return 0; }
insert
155
155
155
156
0
p02825
C++
Runtime Error
#include <stdio.h> char board[30][30]; int main() { bool f; int n; scanf("%d", &n); if (n < 3) printf("-1\n"); else if (n == 4) { printf("aabd\nccbd\nbdaa\nbdcc\n"); } else if (n % 3 == 0) { for (int i = 1; i <= n; i += 3) { for (int j = 1; j <= n; j += 3) printf("a.."); printf("\n"); for (int j = 1; j <= n; j += 3) printf("a.."); printf("\n"); for (int j = 1; j <= n; j += 3) printf(".bb"); printf("\n"); } } else if (n % 6 == 5) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) board[i][j] = '.'; int tot = (n - 3) >> 1, x, y, pos; x = tot / 2; y = tot % 2; for (int i = 0; i < x; i++) { for (int j = 2; j < n; j += 3) { pos = (i * 3 + j - 2) % (n - 2) + 2; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos][j + 2] = 'd'; board[pos + 1][j] = 'f'; board[pos + 1][j + 2] = 'd'; board[pos + 2][j] = 'f'; board[pos + 2][j + 1] = 'e'; board[pos + 2][j + 2] = 'e'; } } if (y) { for (int j = 2; j < n; j += 3) { pos = (x * 3 + j - 2) % (n - 2) + 2; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos + 1][j + 2] = 'e'; board[pos + 2][j + 2] = 'e'; } } for (int j = 2; j <= n; j += 2, f = !f) { board[1][j] = board[1][j - 1] = f ? 'a' : 'b'; board[n][j] = board[n][j + 1] = ((n / 6) & 1) ^ f ? 'b' : 'a'; } for (int j = 2; j <= n; j += 2, f = !f) { board[j][n] = board[j - 1][n] = f ? 'a' : 'b'; board[j][1] = board[j + 1][1] = ((n / 6) & 1) ^ f ? 'b' : 'a'; } for (int i = 1; i <= n; i++, printf("\n")) for (int j = 1; j <= n; j++) printf("%c", board[i][j]); } else if (n % 6 == 4) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) board[i][j] = '.'; int tot = (n - 5) >> 1, x, y, pos; x = tot / 2; y = tot % 2; for (int i = 0; i < x; i++) { for (int j = 3; j < n; j += 3) { pos = (i * 3 + j - 3) % (n - 4) + 3; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos][j + 2] = 'd'; board[pos + 1][j] = 'f'; board[pos + 1][j + 2] = 'd'; board[pos + 2][j] = 'f'; board[pos + 2][j + 1] = 'e'; board[pos + 2][j + 2] = 'e'; } } if (y) { for (int j = 3; j < n; j += 3) { pos = (x * 3 + j - 3) % (n - 4) + 3; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos + 1][j + 2] = 'e'; board[pos + 2][j + 2] = 'e'; } } for (int j = 2; j < n; j += 2, f = !f) { board[1][j] = board[1][j - 1] = f ? 'b' : 'a'; board[2][j] = board[2][j - 1] = f ? 'a' : 'b'; board[n - 1][j + 1] = board[n - 1][j + 2] = ((n / 6) & 1) ^ f ? 'a' : 'b'; board[n][j + 1] = board[n][j + 2] = ((n / 6) & 1) ^ f ? 'b' : 'a'; } for (int j = 2; j < n; j += 2, f = !f) { board[j][n] = board[j - 1][n] = f ? 'z' : 'y'; board[j][n - 1] = board[j - 1][n - 1] = f ? 'y' : 'z'; board[j + 2][2] = board[j + 1][2] = ((n / 6) & 1) ^ f ? 'z' : 'y'; board[j + 2][1] = board[j + 1][1] = ((n / 6) & 1) ^ f ? 'y' : 'z'; } for (int i = 1; i <= n; i++, printf("\n")) for (int j = 1; j <= n; j++) printf("%c", board[i][j]); } else if (n % 6 == 1) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) board[i][j] = '.'; int tot = (n - 3) >> 1, x, y, pos; x = tot / 2; y = tot % 2; for (int i = 0; i < x; i++) { for (int j = 2; j < n; j += 3) { pos = (i * 3 + j - 2) % (n - 1) + 2; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos][j + 2] = 'd'; board[pos + 1][j] = 'f'; board[pos + 1][j + 2] = 'd'; board[pos + 2][j] = 'f'; board[pos + 2][j + 1] = 'e'; board[pos + 2][j + 2] = 'e'; } } if (y) { for (int j = 2; j < n; j += 3) { pos = (x * 3 + j - 2) % (n - 1) + 2; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos + 1][j + 2] = 'e'; board[pos + 2][j + 2] = 'e'; } } for (int j = 2; j < n; j += 2, f = !f) { board[1][j] = board[1][j + 1] = f ? 'b' : 'a'; } for (int j = 2; j < n; j += 2, f = !f) { board[j][1] = board[j + 1][1] = f ? 'z' : 'y'; } for (int i = 1; i <= n; i++, printf("\n")) for (int j = 1; j <= n; j++) printf("%c", board[i][j]); } else if (n % 6 == 2) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) board[i][j] = '.'; int tot = (n - 6) >> 1, x, y, pos; x = tot / 2; y = tot % 2; for (int i = 0; i < x; i++) { for (int j = 3; j < n; j += 3) { pos = (i * 3 + j - 3) % (n - 2) + 3; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos][j + 2] = 'd'; board[pos + 1][j] = 'f'; board[pos + 1][j + 2] = 'd'; board[pos + 2][j] = 'f'; board[pos + 2][j + 1] = 'e'; board[pos + 2][j + 2] = 'e'; } } if (y) { for (int j = 3; j < n; j += 3) { pos = (x * 3 + j - 3) % (n - 2) + 3; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos + 1][j + 2] = 'e'; board[pos + 2][j + 2] = 'e'; } } for (int j = 3; j < n; j += 2, f = !f) { board[1][j] = board[1][j + 1] = f ? 'b' : 'a'; board[2][j] = board[2][j + 1] = f ? 'a' : 'b'; } for (int j = 3; j < n; j += 2, f = !f) { board[j][1] = board[j + 1][1] = f ? 'z' : 'y'; board[j][2] = board[j + 1][2] = f ? 'y' : 'z'; } for (int i = 1; i <= n; i++, printf("\n")) for (int j = 1; j <= n; j++) printf("%c", board[i][j]); } }
#include <stdio.h> char board[1010][1010]; int main() { bool f; int n; scanf("%d", &n); if (n < 3) printf("-1\n"); else if (n == 4) { printf("aabd\nccbd\nbdaa\nbdcc\n"); } else if (n % 3 == 0) { for (int i = 1; i <= n; i += 3) { for (int j = 1; j <= n; j += 3) printf("a.."); printf("\n"); for (int j = 1; j <= n; j += 3) printf("a.."); printf("\n"); for (int j = 1; j <= n; j += 3) printf(".bb"); printf("\n"); } } else if (n % 6 == 5) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) board[i][j] = '.'; int tot = (n - 3) >> 1, x, y, pos; x = tot / 2; y = tot % 2; for (int i = 0; i < x; i++) { for (int j = 2; j < n; j += 3) { pos = (i * 3 + j - 2) % (n - 2) + 2; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos][j + 2] = 'd'; board[pos + 1][j] = 'f'; board[pos + 1][j + 2] = 'd'; board[pos + 2][j] = 'f'; board[pos + 2][j + 1] = 'e'; board[pos + 2][j + 2] = 'e'; } } if (y) { for (int j = 2; j < n; j += 3) { pos = (x * 3 + j - 2) % (n - 2) + 2; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos + 1][j + 2] = 'e'; board[pos + 2][j + 2] = 'e'; } } for (int j = 2; j <= n; j += 2, f = !f) { board[1][j] = board[1][j - 1] = f ? 'a' : 'b'; board[n][j] = board[n][j + 1] = ((n / 6) & 1) ^ f ? 'b' : 'a'; } for (int j = 2; j <= n; j += 2, f = !f) { board[j][n] = board[j - 1][n] = f ? 'a' : 'b'; board[j][1] = board[j + 1][1] = ((n / 6) & 1) ^ f ? 'b' : 'a'; } for (int i = 1; i <= n; i++, printf("\n")) for (int j = 1; j <= n; j++) printf("%c", board[i][j]); } else if (n % 6 == 4) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) board[i][j] = '.'; int tot = (n - 5) >> 1, x, y, pos; x = tot / 2; y = tot % 2; for (int i = 0; i < x; i++) { for (int j = 3; j < n; j += 3) { pos = (i * 3 + j - 3) % (n - 4) + 3; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos][j + 2] = 'd'; board[pos + 1][j] = 'f'; board[pos + 1][j + 2] = 'd'; board[pos + 2][j] = 'f'; board[pos + 2][j + 1] = 'e'; board[pos + 2][j + 2] = 'e'; } } if (y) { for (int j = 3; j < n; j += 3) { pos = (x * 3 + j - 3) % (n - 4) + 3; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos + 1][j + 2] = 'e'; board[pos + 2][j + 2] = 'e'; } } for (int j = 2; j < n; j += 2, f = !f) { board[1][j] = board[1][j - 1] = f ? 'b' : 'a'; board[2][j] = board[2][j - 1] = f ? 'a' : 'b'; board[n - 1][j + 1] = board[n - 1][j + 2] = ((n / 6) & 1) ^ f ? 'a' : 'b'; board[n][j + 1] = board[n][j + 2] = ((n / 6) & 1) ^ f ? 'b' : 'a'; } for (int j = 2; j < n; j += 2, f = !f) { board[j][n] = board[j - 1][n] = f ? 'z' : 'y'; board[j][n - 1] = board[j - 1][n - 1] = f ? 'y' : 'z'; board[j + 2][2] = board[j + 1][2] = ((n / 6) & 1) ^ f ? 'z' : 'y'; board[j + 2][1] = board[j + 1][1] = ((n / 6) & 1) ^ f ? 'y' : 'z'; } for (int i = 1; i <= n; i++, printf("\n")) for (int j = 1; j <= n; j++) printf("%c", board[i][j]); } else if (n % 6 == 1) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) board[i][j] = '.'; int tot = (n - 3) >> 1, x, y, pos; x = tot / 2; y = tot % 2; for (int i = 0; i < x; i++) { for (int j = 2; j < n; j += 3) { pos = (i * 3 + j - 2) % (n - 1) + 2; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos][j + 2] = 'd'; board[pos + 1][j] = 'f'; board[pos + 1][j + 2] = 'd'; board[pos + 2][j] = 'f'; board[pos + 2][j + 1] = 'e'; board[pos + 2][j + 2] = 'e'; } } if (y) { for (int j = 2; j < n; j += 3) { pos = (x * 3 + j - 2) % (n - 1) + 2; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos + 1][j + 2] = 'e'; board[pos + 2][j + 2] = 'e'; } } for (int j = 2; j < n; j += 2, f = !f) { board[1][j] = board[1][j + 1] = f ? 'b' : 'a'; } for (int j = 2; j < n; j += 2, f = !f) { board[j][1] = board[j + 1][1] = f ? 'z' : 'y'; } for (int i = 1; i <= n; i++, printf("\n")) for (int j = 1; j <= n; j++) printf("%c", board[i][j]); } else if (n % 6 == 2) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) board[i][j] = '.'; int tot = (n - 6) >> 1, x, y, pos; x = tot / 2; y = tot % 2; for (int i = 0; i < x; i++) { for (int j = 3; j < n; j += 3) { pos = (i * 3 + j - 3) % (n - 2) + 3; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos][j + 2] = 'd'; board[pos + 1][j] = 'f'; board[pos + 1][j + 2] = 'd'; board[pos + 2][j] = 'f'; board[pos + 2][j + 1] = 'e'; board[pos + 2][j + 2] = 'e'; } } if (y) { for (int j = 3; j < n; j += 3) { pos = (x * 3 + j - 3) % (n - 2) + 3; board[pos][j] = 'c'; board[pos][j + 1] = 'c'; board[pos + 1][j + 2] = 'e'; board[pos + 2][j + 2] = 'e'; } } for (int j = 3; j < n; j += 2, f = !f) { board[1][j] = board[1][j + 1] = f ? 'b' : 'a'; board[2][j] = board[2][j + 1] = f ? 'a' : 'b'; } for (int j = 3; j < n; j += 2, f = !f) { board[j][1] = board[j + 1][1] = f ? 'z' : 'y'; board[j][2] = board[j + 1][2] = f ? 'y' : 'z'; } for (int i = 1; i <= n; i++, printf("\n")) for (int j = 1; j <= n; j++) printf("%c", board[i][j]); } }
replace
2
3
2
3
0
p02825
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define x first #define y second #define all(x) begin(x), end(x) typedef long long ll; const int MAX_N = 1024; char ans[MAX_N][MAX_N]; char for4[][5] = {"aacd", "bbcd", "efgg", "efhh"}; char for5[][6] = {"aabbc", "hii.c", "h..jd", "g..jd", "gffee"}; signed main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; if (n == 2) { cout << "-1\n"; } else if (n == 3) { cout << "aa.\n..b\n..b\n"; } else if (n == 6) { cout << "aabb..\nb..zz.\nba....\n.a..aa\n..a..b\n..a..b\n"; } else if (n == 7 || n == 11) { assert(0); } else { int offset = 0; int oldn = n; while (n % 4 != 0) { for (int i = 0; i < 5; ++i) for (int j = 0; j < 5; ++j) ans[i + offset][j + offset] = for5[i][j]; offset += 5; n -= 5; } while (n) { for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) ans[i + offset][j + offset] = for4[i][j]; offset += 4; n -= 4; } n = oldn; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (ans[i][j]) cout << ans[i][j]; else cout << "."; } cout << "\n"; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define x first #define y second #define all(x) begin(x), end(x) typedef long long ll; const int MAX_N = 1024; char ans[MAX_N][MAX_N]; char for4[][5] = {"aacd", "bbcd", "efgg", "efhh"}; char for5[][6] = {"aabbc", "hii.c", "h..jd", "g..jd", "gffee"}; signed main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; if (n == 2) { cout << "-1\n"; } else if (n == 3) { cout << "aa.\n..b\n..b\n"; } else if (n == 6) { cout << "aabb..\nb..zz.\nba....\n.a..aa\n..a..b\n..a..b\n"; } else if (n == 7) { cout << "\ .aabaa.\n\ a..b..a\n\ a.cc..a\n\ bb..dbb\n\ a...d.a\n\ a..b..a\n\ .aabaa.\n"; } else if (n == 11) { cout << "\ .aabaa.....\n\ a..b..a....\n\ a.cc..a....\n\ bb..dbb....\n\ a...d.a....\n\ a..b..a....\n\ .aabaa.....\n\ .......aacd\n\ .......bbcd\n\ .......efaa\n\ .......efbb\n\ "; } else { int offset = 0; int oldn = n; while (n % 4 != 0) { for (int i = 0; i < 5; ++i) for (int j = 0; j < 5; ++j) ans[i + offset][j + offset] = for5[i][j]; offset += 5; n -= 5; } while (n) { for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) ans[i + offset][j + offset] = for4[i][j]; offset += 4; n -= 4; } n = oldn; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (ans[i][j]) cout << ans[i][j]; else cout << "."; } cout << "\n"; } } return 0; }
replace
26
28
26
49
0
p02825
C++
Runtime Error
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; typedef long long ll; char ans[1000][1000]; vector<vector<char>> b3 = {{'.', 'a', 'a'}, {'b', '.', '.'}, {'b', '.', '.'}}; vector<vector<char>> b4 = {{'a', 'a', 'c', 'd'}, {'b', 'b', 'c', 'd'}, {'c', 'd', 'b', 'b'}, {'c', 'd', 'a', 'a'}}; vector<vector<char>> b5 = { {'a', 'b', 'b', 'c', 'c'}, {'a', 'c', 'c', '.', 'b'}, {'b', '.', '.', 'c', 'b'}, {'b', '.', '.', 'c', 'a'}, {'a', 'a', 'b', 'b', 'a'}, }; vector<vector<char>> b7 = { {'a', '.', 'b', '.', '.', 'c', 'c'}, {'a', '.', 'b', 'c', 'c', '.', '.'}, {'.', 'b', 'a', 'a', '.', 'c', 'c'}, {'.', 'b', '.', '.', 'a', 'b', '.'}, {'a', 'a', '.', '.', 'a', 'b', '.'}, {'.', '.', 'a', 'a', 'c', '.', 'c'}, {'a', 'a', '.', '.', 'c', '.', 'c'}}; vector<int> find(int n) { vector<int> ans; if (n % 3 == 0) { for (int i = 0; i < n / 3; i++) ans.push_back(3); } else if (n % 4 == 0) { for (int i = 0; i < n / 4; i++) ans.push_back(4); } else { for (int i = 0; i < n / 4; i++) { int rem = n - i * 4; if (rem % 5 == 0) { for (int j = 0; j < i; j++) ans.push_back(4); for (int j = 0; j < rem / 5; j++) ans.push_back(5); break; } if (rem % 7 == 0) { for (int j = 0; j < i; j++) ans.push_back(4); for (int j = 0; j < rem / 7; j++) ans.push_back(7); break; } } if (ans.size() == 0) { for (int i = 0; i < n / 5; i++) { int rem = n - i * 5; if (rem % 7 == 0) { for (int j = 0; j < i; j++) ans.push_back(5); for (int j = 0; j < rem / 7; j++) ans.push_back(7); break; } } } } return ans; } vector<vector<char>> buf[10]; void create(vector<int> v) { int cur = 0; for (int i = 0; i < v.size(); i++) { for (int j = 0; j < buf[v[i]].size(); j++) { for (int k = 0; k < buf[v[i]].size(); k++) { ans[cur + j][cur + k] = buf[v[i]][j][k]; } } cur += buf[v[i]].size(); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; buf[3] = b3; buf[4] = b4; buf[5] = b5; buf[7] = b7; int N; cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { ans[i][j] = '.'; } } if (N == 2) { cout << -1 << endl; return -1; } vector<int> v = find(N); create(v); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << ans[i][j]; } cout << endl; } }
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; typedef long long ll; char ans[1000][1000]; vector<vector<char>> b3 = {{'.', 'a', 'a'}, {'b', '.', '.'}, {'b', '.', '.'}}; vector<vector<char>> b4 = {{'a', 'a', 'c', 'd'}, {'b', 'b', 'c', 'd'}, {'c', 'd', 'b', 'b'}, {'c', 'd', 'a', 'a'}}; vector<vector<char>> b5 = { {'a', 'b', 'b', 'c', 'c'}, {'a', 'c', 'c', '.', 'b'}, {'b', '.', '.', 'c', 'b'}, {'b', '.', '.', 'c', 'a'}, {'a', 'a', 'b', 'b', 'a'}, }; vector<vector<char>> b7 = { {'a', '.', 'b', '.', '.', 'c', 'c'}, {'a', '.', 'b', 'c', 'c', '.', '.'}, {'.', 'b', 'a', 'a', '.', 'c', 'c'}, {'.', 'b', '.', '.', 'a', 'b', '.'}, {'a', 'a', '.', '.', 'a', 'b', '.'}, {'.', '.', 'a', 'a', 'c', '.', 'c'}, {'a', 'a', '.', '.', 'c', '.', 'c'}}; vector<int> find(int n) { vector<int> ans; if (n % 3 == 0) { for (int i = 0; i < n / 3; i++) ans.push_back(3); } else if (n % 4 == 0) { for (int i = 0; i < n / 4; i++) ans.push_back(4); } else { for (int i = 0; i < n / 4; i++) { int rem = n - i * 4; if (rem % 5 == 0) { for (int j = 0; j < i; j++) ans.push_back(4); for (int j = 0; j < rem / 5; j++) ans.push_back(5); break; } if (rem % 7 == 0) { for (int j = 0; j < i; j++) ans.push_back(4); for (int j = 0; j < rem / 7; j++) ans.push_back(7); break; } } if (ans.size() == 0) { for (int i = 0; i < n / 5; i++) { int rem = n - i * 5; if (rem % 7 == 0) { for (int j = 0; j < i; j++) ans.push_back(5); for (int j = 0; j < rem / 7; j++) ans.push_back(7); break; } } } } return ans; } vector<vector<char>> buf[10]; void create(vector<int> v) { int cur = 0; for (int i = 0; i < v.size(); i++) { for (int j = 0; j < buf[v[i]].size(); j++) { for (int k = 0; k < buf[v[i]].size(); k++) { ans[cur + j][cur + k] = buf[v[i]][j][k]; } } cur += buf[v[i]].size(); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; buf[3] = b3; buf[4] = b4; buf[5] = b5; buf[7] = b7; int N; cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { ans[i][j] = '.'; } } if (N == 2) { cout << -1 << endl; return 0; } vector<int> v = find(N); create(v); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << ans[i][j]; } cout << endl; } }
replace
105
106
105
106
0
p02825
C++
Runtime Error
#include <bits/stdc++.h> #define For(i, x, y) for (register int i = (x); i <= (y); i++) #define FOR(i, x, y) for (register int i = (x); i < (y); i++) #define Dow(i, x, y) for (register int i = (x); i >= (y); i--) #define Debug(v) \ for (auto i : v) \ printf("%lld ", i); \ puts("") #define mp make_pair #define fi first #define se second #define pb push_back #define ep emplace_back #define siz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define fil(a, b) memset((a), (b), sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pa; typedef pair<ll, ll> PA; typedef vector<int> poly; inline ll read() { ll x = 0, f = 1; char c = getchar(); while ((c < '0' || c > '9') && (c != '-')) c = getchar(); if (c == '-') f = -1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } const int N = 1010; const string ans3[3] = { "..a", "..a", "bb.", }; const string ans5[5] = {"aabbc", "bbd.c", "ccdaa", "..abc", "..abc"}; const string ans6[6] = { "aaba..", "..babb", "bacc..", "ba..aa", "..aabc", "aa..bc", }; const string ans7[7] = {"..a..ab", "..a..ab", "aa.aa.c", "..a..ac", "..a..ab", "aa.aa.b", "bbccbb."}; const string ans8[8] = {"aa..bc..", "..aabc..", "..bcaa..", "..bc..aa", "aa....bc", "..aa..bc", "bc..aa..", "bc....aa"}; char ans[N][N]; inline void solve(int x1, int y1, int n) { if (n == 5) { FOR(i, 0, n) FOR(j, 0, n) ans[x1 + i][y1 + j] = ans5[i][j]; return; } if (n == 6) { FOR(i, 0, n) FOR(j, 0, n) ans[x1 + i][y1 + j] = ans6[i][j]; return; } if (n == 7) { FOR(i, 0, n) FOR(j, 0, n) ans[x1 + i][y1 + j] = ans7[i][j]; return; } if (n == 8) { FOR(i, 0, n) FOR(j, 0, n) ans[x1 + i][y1 + j] = ans8[i][j]; return; } if (n == 9) { FOR(i, 0, n) FOR(j, 0, n) ans[x1 + i][y1 + j] = ans3[i % 3][j % 3]; return; } int m = n >> 1; solve(x1, y1, m), solve(x1 + m, y1 + m, n - m); } int main() { int n = read(); if (n == 2) return puts("-1"); if (n == 3) { puts("aa."); puts("..a"); puts("..a"); return 0; } if (n == 4) { puts("aacd"); puts("bbcd"); puts("dcaa"); puts("dcbb"); return 0; } For(i, 1, n) For(j, 1, n) ans[i][j] = '.'; solve(1, 1, n); FOR(i, 1, n + 1) { FOR(j, 1, n + 1) printf("%c", ans[i][j]); puts(""); } }
#include <bits/stdc++.h> #define For(i, x, y) for (register int i = (x); i <= (y); i++) #define FOR(i, x, y) for (register int i = (x); i < (y); i++) #define Dow(i, x, y) for (register int i = (x); i >= (y); i--) #define Debug(v) \ for (auto i : v) \ printf("%lld ", i); \ puts("") #define mp make_pair #define fi first #define se second #define pb push_back #define ep emplace_back #define siz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define fil(a, b) memset((a), (b), sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pa; typedef pair<ll, ll> PA; typedef vector<int> poly; inline ll read() { ll x = 0, f = 1; char c = getchar(); while ((c < '0' || c > '9') && (c != '-')) c = getchar(); if (c == '-') f = -1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } const int N = 1010; const string ans3[3] = { "..a", "..a", "bb.", }; const string ans5[5] = {"aabbc", "bbd.c", "ccdaa", "..abc", "..abc"}; const string ans6[6] = { "aaba..", "..babb", "bacc..", "ba..aa", "..aabc", "aa..bc", }; const string ans7[7] = {"..a..ab", "..a..ab", "aa.aa.c", "..a..ac", "..a..ab", "aa.aa.b", "bbccbb."}; const string ans8[8] = {"aa..bc..", "..aabc..", "..bcaa..", "..bc..aa", "aa....bc", "..aa..bc", "bc..aa..", "bc....aa"}; char ans[N][N]; inline void solve(int x1, int y1, int n) { if (n == 5) { FOR(i, 0, n) FOR(j, 0, n) ans[x1 + i][y1 + j] = ans5[i][j]; return; } if (n == 6) { FOR(i, 0, n) FOR(j, 0, n) ans[x1 + i][y1 + j] = ans6[i][j]; return; } if (n == 7) { FOR(i, 0, n) FOR(j, 0, n) ans[x1 + i][y1 + j] = ans7[i][j]; return; } if (n == 8) { FOR(i, 0, n) FOR(j, 0, n) ans[x1 + i][y1 + j] = ans8[i][j]; return; } if (n == 9) { FOR(i, 0, n) FOR(j, 0, n) ans[x1 + i][y1 + j] = ans3[i % 3][j % 3]; return; } int m = n >> 1; solve(x1, y1, m), solve(x1 + m, y1 + m, n - m); } int main() { int n = read(); if (n == 2) return puts("-1"), 0; if (n == 3) { puts("aa."); puts("..a"); puts("..a"); return 0; } if (n == 4) { puts("aacd"); puts("bbcd"); puts("dcaa"); puts("dcbb"); return 0; } For(i, 1, n) For(j, 1, n) ans[i][j] = '.'; solve(1, 1, n); FOR(i, 1, n + 1) { FOR(j, 1, n + 1) printf("%c", ans[i][j]); puts(""); } }
replace
77
78
77
78
0
p02825
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; void solve(long long N) { if (N == 7 || N == 11) { abort(); } } int main() { long long N; scanf("%lld", &N); solve(N); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; void solve(long long N) { if (N == 7) { cout << "a..aabb" << endl; cout << "abb..d." << endl; cout << "c...cd." << endl; cout << "c...c.a" << endl; cout << ".abb..a" << endl; cout << ".a..eeb" << endl; cout << "eeff..b" << endl; } else if (N == 11) { cout << "a..aabb...." << endl; cout << "abb..d....." << endl; cout << "c...cd....." << endl; cout << "c...c.a...." << endl; cout << ".abb..a...." << endl; cout << ".a..eeb...." << endl; cout << "eeff..b...." << endl; cout << ".......aacd" << endl; cout << ".......bbcd" << endl; cout << ".......cdaa" << endl; cout << ".......cdbb" << endl; } else if (N == 6) { cout << "aa...." << endl; cout << "..b..." << endl; cout << "..b..." << endl; cout << "...cc." << endl; cout << ".....d" << endl; cout << ".....d" << endl; } else if (N == 3) { cout << "aa." << endl; cout << "..b" << endl; cout << "..b" << endl; } else { for (int i = 0; i * 4 <= N; i++) { if ((N - i * 4) % 5 == 0) { int cnt = 0; for (int j = 0; j < i; j++) { for (int k = 0; k < cnt; k++) { cout << '.'; } cout << "aacd"; for (int k = cnt + 4; k < N; k++) { cout << '.'; } cout << endl; for (int k = 0; k < cnt; k++) { cout << '.'; } cout << "bbcd"; for (int k = cnt + 4; k < N; k++) { cout << '.'; } cout << endl; for (int k = 0; k < cnt; k++) { cout << '.'; } cout << "cdaa"; for (int k = cnt + 4; k < N; k++) { cout << '.'; } cout << endl; for (int k = 0; k < cnt; k++) { cout << '.'; } cout << "cdbb"; for (int k = cnt + 4; k < N; k++) { cout << '.'; } cout << endl; cnt += 4; } for (int j = 0; j < (N - i * 4) / 5; j++) { for (int k = 0; k < cnt; k++) { cout << '.'; } cout << "aabbc"; for (int k = cnt + 5; k < N; k++) { cout << '.'; } cout << endl; for (int k = 0; k < cnt; k++) { cout << '.'; } cout << "d.eec"; for (int k = cnt + 5; k < N; k++) { cout << '.'; } cout << endl; for (int k = 0; k < cnt; k++) { cout << '.'; } cout << "df..b"; for (int k = cnt + 5; k < N; k++) { cout << '.'; } cout << endl; for (int k = 0; k < cnt; k++) { cout << '.'; } cout << "ef..b"; for (int k = cnt + 5; k < N; k++) { cout << '.'; } cout << endl; for (int k = 0; k < cnt; k++) { cout << '.'; } cout << "eggqq"; for (int k = cnt + 5; k < N; k++) { cout << '.'; } cout << endl; cnt += 5; } return; } } cout << -1 << endl; } } int main() { long long N; scanf("%lld", &N); solve(N); return 0; }
replace
5
7
5
123
0
p02826
Python
Time Limit Exceeded
import numpy as np n, m = map(int, input().split()) coef = np.minimum(np.arange(n, 1, -1), np.arange(1, n)) dp = np.zeros(n, dtype=np.int64) dp[0] = 1 for c in coef: w = np.zeros(n, dtype=np.int64) w[::c] = 1 dp = np.convolve(dp, w, mode="full")[:n] % m print((dp * np.arange(n, 0, -1) % m).sum() % m)
import numpy as np n, m = map(int, input().split()) coef = np.minimum(np.arange(n, 1, -1), np.arange(1, n)) dp = np.zeros(n, dtype=np.int64) dp[0] = 1 for c in coef: ndp = np.zeros(n, dtype=np.int64) for i in range(0, n, c): ndp[i:] += dp[: n - i] dp = ndp % m print((dp * np.arange(n, 0, -1) % m).sum() % m)
replace
8
11
8
12
TLE
p02826
C++
Runtime Error
#include <bits/stdc++.h> #define ADD(a, b) a = (a + ll(b)) % mod #define MUL(a, b) a = (a * ll(b)) % mod #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define rep(i, a, b) for (int i = int(a); i < int(b); i++) #define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--) #define all(a) (a).begin(), (a).end() #define sz(v) (int)(v).size() #define pb push_back #define sec second #define fst first #define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<int, pi> ppi; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vl> mat; typedef complex<double> comp; void Debug() { cerr << '\n'; } template <class FIRST, class... REST> void Debug(FIRST arg, REST... rest) { cerr << arg << " "; Debug(rest...); } template <class T> ostream &operator<<(ostream &out, const vector<T> &v) { out << "["; if (!v.empty()) { rep(i, 0, sz(v) - 1) out << v[i] << ", "; out << v.back(); } out << "]"; return out; } template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &v) { out << "(" << v.first << ", " << v.second << ")"; return out; } const int MAX_N = 500010; const int MAX_V = 100010; const double eps = 1e-6; const int inf = (1 << 30) - 1; const ll linf = 1LL << 60; const double PI = 3.14159265358979323846; mt19937 rng; // use it by rng() % mod, shuffle(all(vec), rng) /////////////////////////////////////////////////////////////////////////////////////////////////// int N; ll mod; // ll dp[2][5010][5010]; ll C[1010][1010], D[1010][1010]; // x^K/(1-x)(1-x^2)...(1-x^L)?? void solve() { cin >> N >> mod; D[0][0] = 1; rep(i, 0, N / 2) { rep(j, 0, N + 1) { D[i + 1][j] = D[i][j]; } rep(j, 0, N + 1) { if (j + i + 1 <= N) ADD(D[i + 1][j + i + 1], D[i + 1][j]); } } rep(i, 0, N / 2 + 1) { rep(j, 0, N + 1) { ADD(D[i][j + 1], D[i][j]); } } rep(n, 0, N) { rep(k, 1, N + 1) { if (n == 0 || k == 1) C[k][n] = 1; else { C[k][n] = (C[k - 1][n] + (k > n ? 0 : C[k][n - k])) % mod; } } } rep(k, 1, N + 1) { rep(n, 0, N) { ADD(C[k][n + 1], C[k][n]); } } auto get_val = [&](int L, int K, int S) { if (L >= S) { return (D[K][S] - (S > 0 ? D[K][S - 1] : 0) + mod) % mod; } else { if (L == 1) { if (S <= K) return 1ll; else return 0ll; } else return (2 * mod + (C[L][S] - C[L][S - 1]) - (S - K - 1 < 0 ? 0 : C[L - 1][S - K - 1])) % mod; } }; // int now = 0, nex = 1; // // dp[now][0][0] = 1; // // rep(i, 0, N / 2) { // rep(j, 0, N) { // if((N / 2 - 1 - i - 1) * j <= N) memset(dp[nex][j], 0, // sizeof(dp[nex][j])); // } // rep(j, 0, N) { // rep(k, 0, N) { // if((N / 2 - 1 - i) * j + k > N || dp[now][j][k] == 0) // continue; if(k + j <= N) ADD(dp[nex][j][k + j], dp[now][j][k]); // ADD(dp[now][j + 1][k], dp[now][j][k]); // debug(i, j, k, dp[now][j][k]); // } // } // swap(nex, now); // } // rep(j, 0, N) { // rep(k, 0, N) { // if(dp[now][j][k] == 0) continue; // ADD(dp[now][j + 1][k], dp[now][j][k]); // debug(N / 2, j, k, dp[now][j][k]); // } // } // rep(i, 0, 2) { // rep(j, 0, N) { // rep(k, 0, N) { // ADD(dp[i][j][k + 1], dp[i][j][k]); // } // } // } ll res = 0; rep(a, 0, N) { rep(b, 0, a + 1) { if (N % 2 == 1) { // debug(N - 1 - a, N / 2, a - b, dp[now][a][b], D[N / 2][b], (dp[now][N // - 1 - a][a - b] - (a - b != 0 ? dp[now][N - 1 - a][a - b - 1] : 0) + // mod) % mod, get_val(N - 1 - a, N / 2, a - b)); // ADD(res, (dp[now][a][b] - (b != 0 ? dp[now][a][b - 1] : 0) + mod) % // mod * dp[now][N - 1 - a][a - b] % mod); ADD(res, get_val(N - 1 - a, N / 2, b) * D[N / 2][a - b] % mod); } else { // ADD(res, (dp[now][a][b] * (dp[nex][N - 1 - a][a - b] - (a - b != 0 ? // dp[nex][N - 1 - a][a - b - 1] : 0) + mod) % mod)); debug(N - 1 - a, N // / 2 - 1, a - b, dp[now][a][b], D[N / 2][b], (dp[nex][N - 1 - a][a - // b] - (a - b != 0 ? dp[nex][N - 1 - a][a - b - 1] : 0) + mod) % mod, // get_val(N - 1 - a, N / 2 - 1, a - b)); ADD(res, get_val(N - 1 - a, N / 2 - 1, a - b) * D[N / 2][b] % mod); } } } cout << res << "\n"; } uint32_t rd() { uint32_t res; #ifdef __MINGW32__ asm volatile("rdrand %0" : "=a"(res)::"cc"); #else res = std::random_device()(); #endif return res; } int main() { #ifndef LOCAL ios::sync_with_stdio(false); cin.tie(0); #endif cout << fixed; cout.precision(20); cerr << fixed; cerr.precision(6); rng.seed(rd()); #ifdef LOCAL // freopen("in.txt", "wt", stdout); //for tester if (!freopen("in.txt", "rt", stdin)) return 1; #endif solve(); cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0; }
#include <bits/stdc++.h> #define ADD(a, b) a = (a + ll(b)) % mod #define MUL(a, b) a = (a * ll(b)) % mod #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define rep(i, a, b) for (int i = int(a); i < int(b); i++) #define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--) #define all(a) (a).begin(), (a).end() #define sz(v) (int)(v).size() #define pb push_back #define sec second #define fst first #define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<int, pi> ppi; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vl> mat; typedef complex<double> comp; void Debug() { cerr << '\n'; } template <class FIRST, class... REST> void Debug(FIRST arg, REST... rest) { cerr << arg << " "; Debug(rest...); } template <class T> ostream &operator<<(ostream &out, const vector<T> &v) { out << "["; if (!v.empty()) { rep(i, 0, sz(v) - 1) out << v[i] << ", "; out << v.back(); } out << "]"; return out; } template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &v) { out << "(" << v.first << ", " << v.second << ")"; return out; } const int MAX_N = 500010; const int MAX_V = 100010; const double eps = 1e-6; const int inf = (1 << 30) - 1; const ll linf = 1LL << 60; const double PI = 3.14159265358979323846; mt19937 rng; // use it by rng() % mod, shuffle(all(vec), rng) /////////////////////////////////////////////////////////////////////////////////////////////////// int N; ll mod; // ll dp[2][5010][5010]; ll C[5010][5010], D[5010][5010]; // x^K/(1-x)(1-x^2)...(1-x^L)?? void solve() { cin >> N >> mod; D[0][0] = 1; rep(i, 0, N / 2) { rep(j, 0, N + 1) { D[i + 1][j] = D[i][j]; } rep(j, 0, N + 1) { if (j + i + 1 <= N) ADD(D[i + 1][j + i + 1], D[i + 1][j]); } } rep(i, 0, N / 2 + 1) { rep(j, 0, N + 1) { ADD(D[i][j + 1], D[i][j]); } } rep(n, 0, N) { rep(k, 1, N + 1) { if (n == 0 || k == 1) C[k][n] = 1; else { C[k][n] = (C[k - 1][n] + (k > n ? 0 : C[k][n - k])) % mod; } } } rep(k, 1, N + 1) { rep(n, 0, N) { ADD(C[k][n + 1], C[k][n]); } } auto get_val = [&](int L, int K, int S) { if (L >= S) { return (D[K][S] - (S > 0 ? D[K][S - 1] : 0) + mod) % mod; } else { if (L == 1) { if (S <= K) return 1ll; else return 0ll; } else return (2 * mod + (C[L][S] - C[L][S - 1]) - (S - K - 1 < 0 ? 0 : C[L - 1][S - K - 1])) % mod; } }; // int now = 0, nex = 1; // // dp[now][0][0] = 1; // // rep(i, 0, N / 2) { // rep(j, 0, N) { // if((N / 2 - 1 - i - 1) * j <= N) memset(dp[nex][j], 0, // sizeof(dp[nex][j])); // } // rep(j, 0, N) { // rep(k, 0, N) { // if((N / 2 - 1 - i) * j + k > N || dp[now][j][k] == 0) // continue; if(k + j <= N) ADD(dp[nex][j][k + j], dp[now][j][k]); // ADD(dp[now][j + 1][k], dp[now][j][k]); // debug(i, j, k, dp[now][j][k]); // } // } // swap(nex, now); // } // rep(j, 0, N) { // rep(k, 0, N) { // if(dp[now][j][k] == 0) continue; // ADD(dp[now][j + 1][k], dp[now][j][k]); // debug(N / 2, j, k, dp[now][j][k]); // } // } // rep(i, 0, 2) { // rep(j, 0, N) { // rep(k, 0, N) { // ADD(dp[i][j][k + 1], dp[i][j][k]); // } // } // } ll res = 0; rep(a, 0, N) { rep(b, 0, a + 1) { if (N % 2 == 1) { // debug(N - 1 - a, N / 2, a - b, dp[now][a][b], D[N / 2][b], (dp[now][N // - 1 - a][a - b] - (a - b != 0 ? dp[now][N - 1 - a][a - b - 1] : 0) + // mod) % mod, get_val(N - 1 - a, N / 2, a - b)); // ADD(res, (dp[now][a][b] - (b != 0 ? dp[now][a][b - 1] : 0) + mod) % // mod * dp[now][N - 1 - a][a - b] % mod); ADD(res, get_val(N - 1 - a, N / 2, b) * D[N / 2][a - b] % mod); } else { // ADD(res, (dp[now][a][b] * (dp[nex][N - 1 - a][a - b] - (a - b != 0 ? // dp[nex][N - 1 - a][a - b - 1] : 0) + mod) % mod)); debug(N - 1 - a, N // / 2 - 1, a - b, dp[now][a][b], D[N / 2][b], (dp[nex][N - 1 - a][a - // b] - (a - b != 0 ? dp[nex][N - 1 - a][a - b - 1] : 0) + mod) % mod, // get_val(N - 1 - a, N / 2 - 1, a - b)); ADD(res, get_val(N - 1 - a, N / 2 - 1, a - b) * D[N / 2][b] % mod); } } } cout << res << "\n"; } uint32_t rd() { uint32_t res; #ifdef __MINGW32__ asm volatile("rdrand %0" : "=a"(res)::"cc"); #else res = std::random_device()(); #endif return res; } int main() { #ifndef LOCAL ios::sync_with_stdio(false); cin.tie(0); #endif cout << fixed; cout.precision(20); cerr << fixed; cerr.precision(6); rng.seed(rd()); #ifdef LOCAL // freopen("in.txt", "wt", stdout); //for tester if (!freopen("in.txt", "rt", stdin)) return 1; #endif solve(); cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0; }
replace
54
55
54
55
0
Time: 0.033507 s.
p02826
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 1000000007 #define INF (1 << 29) #define LINF (1LL << 60) #define EPS (1e-10) typedef long long Int; typedef pair<Int, Int> P; Int ans = 0, n, m; Int dp[5500]; int main() { dp[0] = 1; cin >> n >> m; for (int i = 2; i <= n; i++) { Int need = 0; if (i >= n - n / 2 + 1) need = n - i + 2; else need = n / 2 + 1; if (i <= n / 2 + 1) need -= n / 2 + 1 - i + 1; // cout << need << endl; for (int j = n; j >= 0; j--) { for (int k = need; k <= j; k += need) { dp[j] += dp[j - k]; dp[j] %= m; } } } for (int i = 0; i < n; i++) { // cout << i << " " << dp[i] << endl; ans += dp[i] * (n - i) % m; ans %= m; } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 1000000007 #define INF (1 << 29) #define LINF (1LL << 60) #define EPS (1e-10) typedef long long Int; typedef pair<Int, Int> P; Int ans = 0, n, m; Int dp[5500]; int main() { dp[0] = 1; cin >> n >> m; for (int i = 2; i <= n; i++) { Int need = 0; if (i >= n - n / 2 + 1) need = n - i + 2; else need = n / 2 + 1; if (i <= n / 2 + 1) need -= n / 2 + 1 - i + 1; // cout << need << endl; for (int j = need; j <= n; j++) { dp[j] += dp[j - need]; dp[j] %= m; } } for (int i = 0; i < n; i++) { // cout << i << " " << dp[i] << endl; ans += dp[i] * (n - i) % m; ans %= m; } cout << ans << endl; return 0; }
replace
32
37
32
35
TLE
p02826
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mod 998244353 #define MOD 1000000007 #define inf 0x3f3f3f3f #define linf 0x3f3f3f3f3f3f3f3fll typedef long long ll; typedef pair<int, int> pii; typedef unsigned long long ull; int n; int M; int dp[2][5005][5005]; inline void add(int &a, int b) { a += b; if (a >= M) a -= M; } int main() { scanf("%d%d", &n, &M); int p = 0, q = 1; for (int i = 1; i <= n; i++) dp[p][n - i + 1][i] = 1; int k = (n + 3) / 2; for (int i = 2; i < k; i++) { // cout<<i<<endl; int lim = min(n, n / (i - 1) + 1); for (int j = 1; j <= lim * 2; j++) for (int k = 1; k <= n; k++) dp[q][j][k] = 0; for (int k = 1; k <= n; k++) // sum { int ans = 0, presum = 0; for (int j = lim; j >= 1; j--) // val=j-1 { add(presum, dp[p][j][k]); add(ans, presum); if (k - j + 1 > 0) { add(dp[q][j][k - j + 1], ans); } } } swap(p, q); } int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { add(ans, dp[p][i][j] * 1ll * (n & 1 ? 1 : i) % M); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define mod 998244353 #define MOD 1000000007 #define inf 0x3f3f3f3f #define linf 0x3f3f3f3f3f3f3f3fll typedef long long ll; typedef pair<int, int> pii; typedef unsigned long long ull; int n; int M; int dp[2][5005][5005]; inline void add(int &a, int b) { a += b; if (a >= M) a -= M; } int main() { scanf("%d%d", &n, &M); int p = 0, q = 1; for (int i = 1; i <= n; i++) dp[p][n - i + 1][i] = 1; int k = (n + 3) / 2; for (int i = 2; i < k; i++) { // cout<<i<<endl; int lim = min(n, n / (i - 1) + 1); for (int j = 1; j <= lim * 2 && j <= n; j++) for (int k = 1; k <= n; k++) dp[q][j][k] = 0; for (int k = 1; k <= n; k++) // sum { int ans = 0, presum = 0; for (int j = lim; j >= 1; j--) // val=j-1 { add(presum, dp[p][j][k]); add(ans, presum); if (k - j + 1 > 0) { add(dp[q][j][k - j + 1], ans); } } } swap(p, q); } int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { add(ans, dp[p][i][j] * 1ll * (n & 1 ? 1 : i) % M); } printf("%d\n", ans); return 0; }
replace
26
27
26
27
-11
p02827
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) // const ll mod = 1000000007; int N, M, T; int x[100000], y[100000]; string ans; bool Reach[100000]; void solve1() { vector<bitset<50000>> bits; bits.resize(N); for (int i = 0; i < N; i++) { bits[i][i] = true; } for (int i = 0; i < M; i++) { bits[x[i]] |= bits[y[i]]; bits[y[i]] |= bits[x[i]]; } int ansidx = -1; for (int i = 0; i < N; i++) { if (bits[i].count() == N) ansidx = i; } if (ansidx == -1) { cout << -1 << endl; return; } Reach[ansidx] = true; for (int e = M - 1; e >= 0; e--) { if (Reach[x[e]]) ans[e] = '^'; else ans[e] = 'v'; Reach[x[e]] |= Reach[y[e]]; Reach[y[e]] |= Reach[x[e]]; } cout << ans << endl; } vector<int> Connect[50000]; int Index[50000]; void solve2() { if (N == 2) { cout << -1 << endl; return; } for (int i = 0; i < M; i++) { Connect[x[i]].push_back(y[i]); Connect[y[i]].push_back(x[i]); } for (int i = 0; i < N; i++) { Reach[i] = true; } int num = N; for (int e = 0; e < M; e++) { assert(Connect[x[e]][Index[x[e]]] == y[e]); assert(Connect[y[e]][Index[y[e]]] == x[e]); if (num > 3) { ans[e] = '^'; num -= Reach[x[e]]; num -= Reach[y[e]]; Reach[x[e]] |= Reach[y[e]]; Reach[y[e]] = false; num += Reach[x[e]]; num += Reach[y[e]]; } else { num -= Reach[x[e]]; num -= Reach[y[e]]; if (Index[x[e]] + 1 < Connect[x[e]].size() and Reach[Connect[x[e]][Index[x[e]] + 1]]) { int to = Connect[x[e]][Index[x[e] + 1]]; // cerr << x[e] << " " << y[e] << " " << to << endl; /* if(Connect[to].size() <= Index[to]) { for(int i = 0; i <= 1e9; i++) cout << "A" << endl; } */ int newidx = Index[to]; if (to == y[e]) newidx++; if (Connect[to][newidx] == x[e]) { ans[e] = 'v'; Reach[y[e]] |= Reach[x[e]]; Reach[x[e]] = false; } else { ans[e] = '^'; Reach[x[e]] |= Reach[y[e]]; Reach[y[e]] = false; } } else { ans[e] = '^'; Reach[x[e]] |= Reach[y[e]]; Reach[y[e]] = false; } num += Reach[x[e]]; num += Reach[y[e]]; } Index[x[e]]++; Index[y[e]]++; /* for(int i = 0; i < N; i++) cerr << Index[i] << " "; cerr << endl; */ } cout << ans << endl; } int main() { // cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); cin >> N >> M >> T; for (int i = 0; i < M; i++) { cin >> x[i] >> y[i]; x[i]--; y[i]--; } ans = string(M, 'a'); if (T == 1) solve1(); else solve2(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) // const ll mod = 1000000007; int N, M, T; int x[100000], y[100000]; string ans; bool Reach[100000]; void solve1() { vector<bitset<50000>> bits; bits.resize(N); for (int i = 0; i < N; i++) { bits[i][i] = true; } for (int i = 0; i < M; i++) { bits[x[i]] |= bits[y[i]]; bits[y[i]] |= bits[x[i]]; } int ansidx = -1; for (int i = 0; i < N; i++) { if (bits[i].count() == N) ansidx = i; } if (ansidx == -1) { cout << -1 << endl; return; } Reach[ansidx] = true; for (int e = M - 1; e >= 0; e--) { if (Reach[x[e]]) ans[e] = '^'; else ans[e] = 'v'; Reach[x[e]] |= Reach[y[e]]; Reach[y[e]] |= Reach[x[e]]; } cout << ans << endl; } vector<int> Connect[50000]; int Index[50000]; void solve2() { if (N == 2) { cout << -1 << endl; return; } for (int i = 0; i < M; i++) { Connect[x[i]].push_back(y[i]); Connect[y[i]].push_back(x[i]); } for (int i = 0; i < N; i++) { Reach[i] = true; } int num = N; for (int e = 0; e < M; e++) { assert(Connect[x[e]][Index[x[e]]] == y[e]); assert(Connect[y[e]][Index[y[e]]] == x[e]); if (num > 3) { ans[e] = '^'; num -= Reach[x[e]]; num -= Reach[y[e]]; Reach[x[e]] |= Reach[y[e]]; Reach[y[e]] = false; num += Reach[x[e]]; num += Reach[y[e]]; } else { num -= Reach[x[e]]; num -= Reach[y[e]]; if (Index[x[e]] + 1 < Connect[x[e]].size() and Reach[Connect[x[e]][Index[x[e]] + 1]]) { int to = Connect[x[e]][Index[x[e]] + 1]; // cerr << x[e] << " " << y[e] << " " << to << endl; /* if(Connect[to].size() <= Index[to]) { for(int i = 0; i <= 1e9; i++) cout << "A" << endl; } */ int newidx = Index[to]; if (to == y[e]) newidx++; if (Connect[to][newidx] == x[e]) { ans[e] = 'v'; Reach[y[e]] |= Reach[x[e]]; Reach[x[e]] = false; } else { ans[e] = '^'; Reach[x[e]] |= Reach[y[e]]; Reach[y[e]] = false; } } else { ans[e] = '^'; Reach[x[e]] |= Reach[y[e]]; Reach[y[e]] = false; } num += Reach[x[e]]; num += Reach[y[e]]; } Index[x[e]]++; Index[y[e]]++; /* for(int i = 0; i < N; i++) cerr << Index[i] << " "; cerr << endl; */ } cout << ans << endl; } int main() { // cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); cin >> N >> M >> T; for (int i = 0; i < M; i++) { cin >> x[i] >> y[i]; x[i]--; y[i]--; } ans = string(M, 'a'); if (T == 1) solve1(); else solve2(); return 0; }
replace
93
94
93
94
0
p02827
C++
Runtime Error
// #include"stdafx.h" #include <algorithm> #include <bitset> #include <cassert> #include <fstream> #include <iostream> #include <map> #include <random> #include <set> #include <string> #include <vector> using namespace std; // int mod = 998244353; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<vector<int>> vvi; typedef vector<vector<char>> vvc; typedef vector<vector<ll>> vvl; typedef vector<pair<ll, ll>> vpll; typedef vector<pair<ld, ld>> vpld; typedef vector<pair<int, int>> vpi; typedef pair<ld, ld> pld; #define mp make_pair #define pb push_back int mod = 998244353; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { ll c = a * 1LL * b; return c % mod; } int ibit(int n, int i) { return ((n >> i) & 1); } void outp(vvi &ou) { for (int i = 0; i < ou.size(); i++) { for (int j = 0; j < ou[i].size(); j++) { cout << ou[i][j] << ' '; } cout << '\n'; } } void un(int n, int m, vpi bals) { if (n < 3) { cout << "-1" << endl; } else { vi vals(n), ovals(n), ans(m); for (int i = 0; i < n; i++) { vals[i] = i; ovals[i] = 1; } for (int i = m - 1; i >= 0; i--) { int x1 = bals[i].first, y1 = bals[i].second; int v1 = vals[x1], v2 = vals[y1]; if (ovals[v1] > 1) { // cerr << i << " 1" << endl; ans[i] = -1; ovals[v1]--; ovals[v2]++; vals[x1] = v2; } else { // cerr << i << " 2" << endl; ans[i] = 1; ovals[v2]--; ovals[v1]++; vals[y1] = v1; } } for (int i = 0; i < m; i++) { if (ans[i] == -1) cout << 'v'; else cout << '^'; } cout << endl; } } void non_un(int n, int m, vpi bals) { int bits = 50001 / 60 + 1; vvl dost(n, vl(bits)); for (int i = 0; i < n; i++) { ll x = i % 60; dost[i][i / 60] += (1LL << x); } for (int i = m - 1; i >= 0; i--) { // cerr << i << ' ' << dost[3][0] << endl; int x1 = bals[i].first, y1 = bals[i].second; for (int j = 0; j < bits; j++) { dost[x1][j] |= dost[y1][j]; dost[y1][j] |= dost[x1][j]; } } vl cur = dost[0]; for (int i = 1; i < n; i++) for (int j = 0; j < bits; j++) cur[j] &= dost[i][j]; int fr = -1; for (int i = 0; i < bits; i++) { // cerr << cur[i] << endl; if (cur[i] != 0) { int val = cur[i], pos = -1; for (int j = 0; j < 61; j++) { if ((val >> j) & 1) { pos = j; break; } } assert(pos >= 0); fr = i * 60 + pos; // cerr << i << ' ' << fr << endl; break; } } if (fr == -1) { cout << "-1" << endl; } else { vi used(n), ans(m); used[fr] = 1; for (int i = m - 1; i >= 0; i--) { int x1 = bals[i].first, y1 = bals[i].second; if (used[x1]) ans[i] = -1; used[x1] |= used[y1]; used[y1] |= used[x1]; } for (int i = 0; i < m; i++) { if (ans[i] != -1) cout << 'v'; else cout << '^'; } cout << endl; } } void solve(istream &cin = std::cin, ostream &cout = std::cout) { int n, m, t; cin >> n >> m >> t; vpi bals; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; bals.pb(mp(x - 1, y - 1)); } if (t == 1) { non_un(n, m, bals); } else { un(n, m, bals); } } int main() { solve(); int n; cin >> n; }
// #include"stdafx.h" #include <algorithm> #include <bitset> #include <cassert> #include <fstream> #include <iostream> #include <map> #include <random> #include <set> #include <string> #include <vector> using namespace std; // int mod = 998244353; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<vector<int>> vvi; typedef vector<vector<char>> vvc; typedef vector<vector<ll>> vvl; typedef vector<pair<ll, ll>> vpll; typedef vector<pair<ld, ld>> vpld; typedef vector<pair<int, int>> vpi; typedef pair<ld, ld> pld; #define mp make_pair #define pb push_back int mod = 998244353; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { ll c = a * 1LL * b; return c % mod; } int ibit(int n, int i) { return ((n >> i) & 1); } void outp(vvi &ou) { for (int i = 0; i < ou.size(); i++) { for (int j = 0; j < ou[i].size(); j++) { cout << ou[i][j] << ' '; } cout << '\n'; } } void un(int n, int m, vpi bals) { if (n < 3) { cout << "-1" << endl; } else { vi vals(n), ovals(n), ans(m); for (int i = 0; i < n; i++) { vals[i] = i; ovals[i] = 1; } for (int i = m - 1; i >= 0; i--) { int x1 = bals[i].first, y1 = bals[i].second; int v1 = vals[x1], v2 = vals[y1]; if (ovals[v1] > 1) { // cerr << i << " 1" << endl; ans[i] = -1; ovals[v1]--; ovals[v2]++; vals[x1] = v2; } else { // cerr << i << " 2" << endl; ans[i] = 1; ovals[v2]--; ovals[v1]++; vals[y1] = v1; } } for (int i = 0; i < m; i++) { if (ans[i] == -1) cout << 'v'; else cout << '^'; } cout << endl; } } void non_un(int n, int m, vpi bals) { int bits = 50001 / 60 + 1; vvl dost(n, vl(bits)); for (int i = 0; i < n; i++) { ll x = i % 60; dost[i][i / 60] += (1LL << x); } for (int i = m - 1; i >= 0; i--) { // cerr << i << ' ' << dost[3][0] << endl; int x1 = bals[i].first, y1 = bals[i].second; for (int j = 0; j < bits; j++) { dost[x1][j] |= dost[y1][j]; dost[y1][j] |= dost[x1][j]; } } vl cur = dost[0]; for (int i = 1; i < n; i++) for (int j = 0; j < bits; j++) cur[j] &= dost[i][j]; int fr = -1; for (int i = 0; i < bits; i++) { // cerr << cur[i] << endl; if (cur[i] != 0) { ll val = cur[i], pos = -1; for (int j = 0; j < 61; j++) { if ((val >> j) & 1) { pos = j; break; } } assert(pos >= 0); fr = i * 60 + pos; // cerr << i << ' ' << fr << endl; break; } } if (fr == -1) { cout << "-1" << endl; } else { vi used(n), ans(m); used[fr] = 1; for (int i = m - 1; i >= 0; i--) { int x1 = bals[i].first, y1 = bals[i].second; if (used[x1]) ans[i] = -1; used[x1] |= used[y1]; used[y1] |= used[x1]; } for (int i = 0; i < m; i++) { if (ans[i] != -1) cout << 'v'; else cout << '^'; } cout << endl; } } void solve(istream &cin = std::cin, ostream &cout = std::cout) { int n, m, t; cin >> n >> m >> t; vpi bals; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; bals.pb(mp(x - 1, y - 1)); } if (t == 1) { non_un(n, m, bals); } else { un(n, m, bals); } } int main() { solve(); int n; cin >> n; }
replace
122
123
122
123
0
p02827
C++
Runtime Error
#include <bits/stdc++.h> #define Tp template <typename Ty> #define Ts template <typename Ty, typename... Ar> #define Reg register #define RI Reg int #define Con const #define CI Con int & #define I inline #define W while #define N 50000 #define M 100000 using namespace std; int n, m, op, a[M + 5], b[M + 5]; char ans[N + 5]; class UniformingSolver { private: int p[N + 5]; bitset<N + 5> S[N + 5]; public: I void Solve() { RI i, j; for (i = 1; i <= n; ++i) S[i].set(i); for (i = 1; i <= m; ++i) S[a[i]] = S[b[i]] = S[a[i]] | S[b[i]]; for (i = 1; i <= n; ++i) if (S[i].count() == n) { for (p[i] = 1, j = m; j; --j) p[a[j]] ? (p[b[j]] = 1, ans[j] = '^') : (p[a[j]] = p[b[j]], ans[j] = 'v'); puts(ans + 1); return; } puts("-1"); } } S1; class NonUniformingSolver { private: int t[N + 5], k[N + 5]; public: I void Solve() { RI i; if (n <= 2) return (void)(puts("-1")); for (i = 1; i <= n; ++i) k[t[i] = i] = 1; #define U(x, y) (--k[t[x]], ++k[t[x] = t[y]]) for (i = m; i; --i) k[t[a[i]]] <= k[t[b[i]]] ? (ans[i] = '^', U(b[i], a[i])) : (ans[i] = 'v', U(a[i], b[i])); puts(ans + 1); } } S2; int main() { RI i; for (scanf("%d%d%d", &n, &m, &op), i = 1; i <= m; ++i) scanf("%d%d", a + i, b + i); return op == 1 ? S1.Solve() : S2.Solve(), 0; }
#include <bits/stdc++.h> #define Tp template <typename Ty> #define Ts template <typename Ty, typename... Ar> #define Reg register #define RI Reg int #define Con const #define CI Con int & #define I inline #define W while #define N 50000 #define M 100000 using namespace std; int n, m, op, a[M + 5], b[M + 5]; char ans[M + 5]; class UniformingSolver { private: int p[N + 5]; bitset<N + 5> S[N + 5]; public: I void Solve() { RI i, j; for (i = 1; i <= n; ++i) S[i].set(i); for (i = 1; i <= m; ++i) S[a[i]] = S[b[i]] = S[a[i]] | S[b[i]]; for (i = 1; i <= n; ++i) if (S[i].count() == n) { for (p[i] = 1, j = m; j; --j) p[a[j]] ? (p[b[j]] = 1, ans[j] = '^') : (p[a[j]] = p[b[j]], ans[j] = 'v'); puts(ans + 1); return; } puts("-1"); } } S1; class NonUniformingSolver { private: int t[N + 5], k[N + 5]; public: I void Solve() { RI i; if (n <= 2) return (void)(puts("-1")); for (i = 1; i <= n; ++i) k[t[i] = i] = 1; #define U(x, y) (--k[t[x]], ++k[t[x] = t[y]]) for (i = m; i; --i) k[t[a[i]]] <= k[t[b[i]]] ? (ans[i] = '^', U(b[i], a[i])) : (ans[i] = 'v', U(a[i], b[i])); puts(ans + 1); } } S2; int main() { RI i; for (scanf("%d%d%d", &n, &m, &op), i = 1; i <= m; ++i) scanf("%d%d", a + i, b + i); return op == 1 ? S1.Solve() : S2.Solve(), 0; }
replace
13
14
13
14
-11