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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02881 | C++ | Time Limit Exceeded | #define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(a) (a).begin(), (a).end()
#define EPS (1e-5)
#define bit(n, k) ((n >> k) & 1)
const ll Mod = 1000000007;
const ll mod = 998244353;
struct H {
ll x, y;
bool operator<(const H &b) const {
if (x != b.x)
return x < b.x;
return y < b.y;
}
bool operator>(const H &b) const {
if (x != b.x)
return x > b.x;
return y > b.y;
}
bool operator==(const H &b) const { return x == b.x && y == b.y; }
bool operator!=(const H &b) const { return (*this) != b; }
};
struct P {
ll pos, cost;
bool operator<(const P &b) const { return cost < b.cost; }
bool operator>(const P &b) const { return cost > b.cost; }
};
struct B {
ll to, cost;
};
struct E {
ll from, to, cost;
bool operator<(const E &b) const { return cost < b.cost; }
bool operator>(const E &b) const { return cost > b.cost; }
};
template <typename T, typename U> void chmin(T &a, U b) {
if (a > b)
a = b;
}
template <typename T, typename U> void chmax(T &a, U b) {
if (a < b)
a = b;
}
template <typename T> T max_0(T a) {
if (a < 0)
return 0;
return a;
}
template <typename T> T min_0(T a) {
if (a > 0)
return 0;
return a;
}
ll read() {
ll u;
ll k = scanf("%lld", &u);
return u;
}
ll gcd(ll i, ll j) {
if (i > j)
swap(i, j);
if (i == 0)
return j;
return gcd(j % i, i);
}
ll mod_pow(ll x, ll n, ll p) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * x % p;
x = x * x % p;
n >>= 1;
}
return res;
} // x^n%p
ll bitcount(ll x) {
int sum = 0;
for (int i = 0; i < 20; i++)
if ((1ll << i) & x)
sum++;
return sum;
}
/*ll fact_[200000];
ll comb(ll x, ll y, ll mod = Mod) {
if (!fact_[0]) {
fact_[0] = 1;
for (int i = 1; i < 200000; i++) {
fact_[i] = fact_[i - 1] * i % mod;
}
}
if (x < y) return 0;
return mod_pow(fact_[x], mod, mod) *
mod_pow(fact_[x - y], mod - 2, mod) % mod *
mod_pow(fact_[y], mod - 2, mod) % mod;
}//xCy*/
const ll Inf = 3023372036854775807;
const int inf = 1500000000;
#define int long long
//----------------------------------------------------
signed main() {
int n;
cin >> n;
int ans = Inf;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
ans = min(ans, i - 1 + (n / i - 1));
}
}
cout << ans << endl;
} | #define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(a) (a).begin(), (a).end()
#define EPS (1e-5)
#define bit(n, k) ((n >> k) & 1)
const ll Mod = 1000000007;
const ll mod = 998244353;
struct H {
ll x, y;
bool operator<(const H &b) const {
if (x != b.x)
return x < b.x;
return y < b.y;
}
bool operator>(const H &b) const {
if (x != b.x)
return x > b.x;
return y > b.y;
}
bool operator==(const H &b) const { return x == b.x && y == b.y; }
bool operator!=(const H &b) const { return (*this) != b; }
};
struct P {
ll pos, cost;
bool operator<(const P &b) const { return cost < b.cost; }
bool operator>(const P &b) const { return cost > b.cost; }
};
struct B {
ll to, cost;
};
struct E {
ll from, to, cost;
bool operator<(const E &b) const { return cost < b.cost; }
bool operator>(const E &b) const { return cost > b.cost; }
};
template <typename T, typename U> void chmin(T &a, U b) {
if (a > b)
a = b;
}
template <typename T, typename U> void chmax(T &a, U b) {
if (a < b)
a = b;
}
template <typename T> T max_0(T a) {
if (a < 0)
return 0;
return a;
}
template <typename T> T min_0(T a) {
if (a > 0)
return 0;
return a;
}
ll read() {
ll u;
ll k = scanf("%lld", &u);
return u;
}
ll gcd(ll i, ll j) {
if (i > j)
swap(i, j);
if (i == 0)
return j;
return gcd(j % i, i);
}
ll mod_pow(ll x, ll n, ll p) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * x % p;
x = x * x % p;
n >>= 1;
}
return res;
} // x^n%p
ll bitcount(ll x) {
int sum = 0;
for (int i = 0; i < 20; i++)
if ((1ll << i) & x)
sum++;
return sum;
}
/*ll fact_[200000];
ll comb(ll x, ll y, ll mod = Mod) {
if (!fact_[0]) {
fact_[0] = 1;
for (int i = 1; i < 200000; i++) {
fact_[i] = fact_[i - 1] * i % mod;
}
}
if (x < y) return 0;
return mod_pow(fact_[x], mod, mod) *
mod_pow(fact_[x - y], mod - 2, mod) % mod *
mod_pow(fact_[y], mod - 2, mod) % mod;
}//xCy*/
const ll Inf = 3023372036854775807;
const int inf = 1500000000;
#define int long long
//----------------------------------------------------
signed main() {
int n;
cin >> n;
int ans = Inf;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, i - 1 + (n / i - 1));
}
}
cout << ans << endl;
}
| replace | 128 | 129 | 128 | 129 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll N;
cin >> N;
ll res = N;
for (int a = 1; a * a <= N; a++) {
if (N % a == 0)
res = min(res, a + N / a - 2);
}
cout << res;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll N;
cin >> N;
ll res = N;
for (ll a = 1; a * a <= N; a++) {
if (N % a == 0)
res = min(res, a + N / a - 2);
}
cout << res;
return 0;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long n, a, b, res;
cin >> n;
res = 1000000000000;
for (long i = 2; i < n / 2; ++i) {
if (n % i == 0) {
a = n / i;
if (res > a + i - 2) {
res = a + i - 2;
} else if (res == a + i - 2) {
break;
}
}
}
if (a < 1)
res = n - 1;
cout << res << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long n, a, b, res;
cin >> n;
res = 1000000000000;
for (long i = 2; i <= sqrt(n); ++i) {
if (n % i == 0) {
a = n / i;
if (res > a + i - 2) {
res = a + i - 2;
} else if (res == a + i - 2) {
break;
}
}
}
if (a < 1)
res = n - 1;
cout << res << endl;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
int main() {
ll N;
cin >> N;
ll ans = N + 1 - 2;
for (int i = 2; i * i <= N; i++) {
if (N % i == 0) {
// cout << N/i << " " << i << endl;
ans = min(ans, N / i + i - 2);
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
int main() {
ll N;
cin >> N;
ll ans = N + 1 - 2;
for (ll i = 2; i * i <= N; i++) {
if (N % i == 0) {
// cout << N/i << " " << i << endl;
ans = min(ans, N / i + i - 2);
}
}
cout << ans << endl;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll ans = 1e9;
for (int i = 1; i * i <= n; i++)
if (n % i == 0)
ans = min(ans, i + n / i - 2);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll ans = 1e18;
for (ll i = 1; i * i <= n; i++)
if (n % i == 0)
ans = min(ans, i + n / i - 2);
cout << ans << endl;
return 0;
} | replace | 7 | 9 | 7 | 9 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
long int a, b;
cin >> a;
long int min = 0, t;
for (int i = 1; i < a / 2 + 1; ++i) {
if (a % i == 0) {
t = i - 1 + a / i - 1;
if (min == 0 || min > t) {
min = t;
}
}
}
cout << min << endl;
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
long int a, b;
cin >> a;
long int min = 0, t;
for (int i = 1; i <= sqrt(a); ++i) {
if (a % i == 0) {
t = i - 1 + a / i - 1;
if (min == 0 || min > t) {
min = t;
}
}
}
cout << min << endl;
return 0;
} | replace | 15 | 16 | 15 | 17 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
while (cin >> n) {
ll maxn = n - 1;
for (ll i = n / 2; i > 1; i--) {
for (ll j = 2; j <= n / i; j++) {
if (i * j == n) {
maxn = min(i + j - 2, maxn);
}
}
}
cout << maxn << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
while (cin >> n) {
ll maxn = n - 1;
for (ll i = sqrt(n); i > 1; i--) {
if (i * (n / i) == n) {
maxn = min(i + (n / i) - 2, maxn);
break;
}
}
cout << maxn << endl;
}
} | replace | 7 | 12 | 7 | 11 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#include <math.h>
int main(void) {
queue<long> que;
long N;
cin >> N;
long min = N;
for (long i = 1; i < sqrt(N); i++) {
for (long j = i; j <= N; j++) {
if (N == i * j) {
que.push(i + j - 2);
}
}
}
while (!que.empty()) {
if (min > que.front()) {
min = que.front();
}
que.pop();
}
cout << min << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#include <math.h>
int main(void) {
queue<long> que;
long N;
cin >> N;
long min = N;
for (long i = 1; i <= sqrt(N); i++) {
if (!(N % i)) {
que.push(i + N / i - 2);
}
}
while (!que.empty()) {
if (min > que.front()) {
min = que.front();
}
que.pop();
}
cout << min << endl;
}
| replace | 9 | 14 | 9 | 12 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long n;
cin >> n;
long ans = -1;
long i, j;
for (i = 1; i <= n; i++) {
if (n % i == 0) {
j = n / i;
if (ans != -1) {
if (ans > i - 1 + j - 1) {
ans = i - 1 + j - 1;
}
} else {
ans = i - 1 + j - 1;
}
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long n;
cin >> n;
long ans = -1;
long i, j;
for (i = 1; i * i <= n; i++) {
if (n % i == 0) {
j = n / i;
if (ans != -1) {
if (ans > i - 1 + j - 1) {
ans = i - 1 + j - 1;
}
} else {
ans = i - 1 + j - 1;
}
}
}
cout << ans << endl;
return 0;
} | replace | 12 | 13 | 12 | 13 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define pu push
#define pp pop_back
#define in insert
#define ld long double
#define debug cout << "Hold right there sparky.....\n";
#define forn(low, high, i) for (i = low; i < high; i++)
#define forrev(high, low, i) for (i = high; i >= low; i--)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[ ";
for (auto v : V)
os << v << " ";
return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ll mod = 1e9 + 7;
const ll maxn = 5 * 100001;
ll power(ll x, ll n) {
ll res = 1;
for (; n > 0; n >>= 1) {
if (n & 1)
res = (res * x) % mod;
x = (x * x) % mod;
}
return res;
}
int32_t main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
IOS ll n;
cin >> n;
ll ans = n - 1;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, i - 1 + (abs((n / i) - 1)));
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define pu push
#define pp pop_back
#define in insert
#define ld long double
#define debug cout << "Hold right there sparky.....\n";
#define forn(low, high, i) for (i = low; i < high; i++)
#define forrev(high, low, i) for (i = high; i >= low; i--)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[ ";
for (auto v : V)
os << v << " ";
return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ll mod = 1e9 + 7;
const ll maxn = 5 * 100001;
ll power(ll x, ll n) {
ll res = 1;
for (; n > 0; n >>= 1) {
if (n & 1)
res = (res * x) % mod;
x = (x * x) % mod;
}
return res;
}
int32_t main() {
IOS ll n;
cin >> n;
ll ans = n - 1;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, i - 1 + (abs((n / i) - 1)));
}
}
cout << ans << endl;
}
| delete | 69 | 76 | 69 | 69 | 0 | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long ans = n - 1;
for (long long i = 1; i <= n / 2; i++)
if (n % i == 0)
ans = min(ans, i + (n / i) - 2);
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long ans = n - 1;
for (long long i = 1; i <= sqrt(n); i++)
if (n % i == 0)
ans = min(ans, i + (n / i) - 2);
cout << ans << endl;
return 0;
}
| replace | 7 | 8 | 7 | 8 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long i = 0; i < n; i++)
using ll = long long;
int main() {
ll n, ans;
cin >> n;
ans = n;
rep(i, sqrt(n)) {
if (n % i == 0) {
ans = min(i + n / i - 2, ans);
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long i = 1; i <= n; i++)
using ll = long long;
int main() {
ll n, ans;
cin >> n;
ans = n;
rep(i, sqrt(n)) {
if (n % i == 0) {
ans = min(i + n / i - 2, ans);
}
}
cout << ans << endl;
}
| replace | 2 | 3 | 2 | 3 | -8 | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef vector<vector<int>> vvi;
#define rep(i, a, n) for (int i = a; 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 PER(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define PERS(i, n) for (int i = (int)(n); i > 0; i--)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c) \
for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).end(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) (container).size()
#define FILL0(n) setfill('0') << right << setw(n)
#define mp(a, b) make_pair(a, b)
#define toLower(c) c + 0x20
#define toUpper(c) c - 0x20
#define pb push_back
#define eb emplace_back
const int INF = 1e9;
const long long LLINF = 1e18;
const int MOD = 1000000007;
int main() {
ll n;
cin >> n;
ll ans = n - 1;
for (ll i = 1; i * i <= n; n++) {
if (n % i == 0) {
ans = min(ans, i + n / i - 2);
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef vector<vector<int>> vvi;
#define rep(i, a, n) for (int i = a; 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 PER(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define PERS(i, n) for (int i = (int)(n); i > 0; i--)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c) \
for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).end(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) (container).size()
#define FILL0(n) setfill('0') << right << setw(n)
#define mp(a, b) make_pair(a, b)
#define toLower(c) c + 0x20
#define toUpper(c) c - 0x20
#define pb push_back
#define eb emplace_back
const int INF = 1e9;
const long long LLINF = 1e18;
const int MOD = 1000000007;
int main() {
ll n;
cin >> n;
ll ans = n - 1;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, i + n / i - 2);
}
}
cout << ans << endl;
return 0;
}
| replace | 54 | 55 | 54 | 55 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
// ifstream in("data1.txt");
// cin.rdbuf(in.rdbuf());
ll N;
ll ans = 1e12;
cin >> N;
for (ll i = 1; i * 1 <= N; i++) {
if (N % i == 0) {
ans = min(ans, i + N / i - 2);
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
// ifstream in("data1.txt");
// cin.rdbuf(in.rdbuf());
ll N;
ll ans = 1e12;
cin >> N;
for (ll i = 1; i * i <= N; i++) {
if (N % i == 0) {
ans = min(ans, i + N / i - 2);
}
}
cout << ans << endl;
}
| replace | 10 | 11 | 10 | 11 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
int main() {
ll n;
cin >> n;
bool in = false;
for (int i = sqrt(n); i <= n; i++) {
if (n % i == 0) {
cout << n / i + i - 2;
break;
}
}
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
int main() {
ll n;
cin >> n;
bool in = false;
for (int i = sqrt(n); i >= 1; i--) {
if (n % i == 0) {
cout << n / i + i - 2;
break;
}
}
}
| replace | 12 | 13 | 12 | 13 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
long long N, min;
cin >> N;
min = N - 1;
for (int i = 1; i * i <= N; i++) {
if (N % i == 0) {
if (N / i + i - 2 < min)
min = N / i + i - 2;
}
}
cout << min;
return 0;
} | #include <iostream>
using namespace std;
int main() {
long long N, min;
cin >> N;
min = N - 1;
for (long long i = 1; i * i <= N; i++) {
if (N % i == 0) {
if (N / i + i - 2 < min)
min = N / i + i - 2;
}
}
cout << min;
return 0;
} | replace | 6 | 7 | 6 | 7 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
bool is_Prime(long long N) {
if (N == 1)
return false;
for (long long i = 2; i * i <= N; i++) {
if (N % i == 0)
return false;
}
return true;
}
int main() {
long long N;
cin >> N;
if (is_Prime(N)) {
cout << N - 1 << endl;
return 0;
}
long long ans = 0;
for (int i = 1; i * i <= N; i++) {
if (N % i == 0) { // 割り切れるとき
long long a = i;
long long b = N / a;
ans = a + b -
2; // 1,1スタートなので(下に1,右に1移動してからスタート)2を引く
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
bool is_Prime(long long N) {
if (N == 1)
return false;
for (long long i = 2; i * i <= N; i++) {
if (N % i == 0)
return false;
}
return true;
}
int main() {
long long N;
cin >> N;
if (is_Prime(N)) {
cout << N - 1 << endl;
return 0;
}
long long ans = 0;
for (long long i = 1; i * i <= N; i++) {
if (N % i == 0) { // 割り切れるとき
long long a = i;
long long b = N / a;
ans = a + b -
2; // 1,1スタートなので(下に1,右に1移動してからスタート)2を引く
}
}
cout << ans << endl;
} | replace | 22 | 23 | 22 | 23 | TLE | |
p02881 | Python | Time Limit Exceeded | n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-(n**0.5) // 1)) + 1):
while tmp % i == 0:
factor.append(i)
tmp //= i
if tmp != 1:
factor.append(tmp)
w = len(factor)
if w == 0:
factor.append(n)
w = 1
if w == 1:
print(n - 1)
else:
ans = 1000000000000
for i in range(1, 2**w // 2):
f2 = [1, 1]
for j in range(w):
f2[i & (2**j) == 0] *= factor[j]
ans = min(ans, sum(f2) - 2)
if ans < n**0.5 * 2:
break
print(ans)
| n = int(input())
ans = 1000000000000
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
a = i
b = n // i
ans = min(ans, a + b - 2)
print(ans)
| replace | 1 | 25 | 1 | 8 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
signed main() {
unsigned long long int N;
cin >> N;
unsigned long long int count = 0;
for (int i = 0; i < sqrt(N) + 1; i++) {
if (N % i == 0) {
if (count == 0) {
count = i + N / i - 2;
} else {
count = min(count, i + N / i - 2);
}
}
}
cout << count << endl;
}
| #include <bits/stdc++.h>
using namespace std;
signed main() {
unsigned long long int N;
cin >> N;
unsigned long long int count = 0;
for (int i = 1; i < sqrt(N) + 1; i++) {
if (N % i == 0) {
if (count == 0) {
count = i + N / i - 2;
} else {
count = min(count, i + N / i - 2);
}
}
}
cout << count << endl;
}
| replace | 7 | 8 | 7 | 8 | -8 | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep_i(i, n) for (int i = 0; i < (n); ++i)
#define rep_ll(i, n) for (long long i = 0; i < (n); ++i)
#define r_rep_i(i, start, end) for (int i = (start); i < (end); ++i)
#define r_rep_ll(i, start, end) for (long long i = (start); i < (end); ++i)
using namespace std;
int main() {
long long n;
cin >> n;
long long a, b;
a = 1;
b = 1;
long long shortest = 100000000000000;
r_rep_ll(i, 1, (n / 2 + 1)) {
if (n % i == 0) {
b = n / i;
shortest = min(shortest, i + b - 2);
}
}
// while (a*b <= 1000000000000){
// if(a*b == n){
// shortest = a + b - 2;
// break;
// }else if(a*b > n){
// break;
// }
// b += 1;
// }
cout << shortest << endl;
} | #include <bits/stdc++.h>
#define rep_i(i, n) for (int i = 0; i < (n); ++i)
#define rep_ll(i, n) for (long long i = 0; i < (n); ++i)
#define r_rep_i(i, start, end) for (int i = (start); i < (end); ++i)
#define r_rep_ll(i, start, end) for (long long i = (start); i < (end); ++i)
using namespace std;
int main() {
long long n;
cin >> n;
long long a, b;
a = 1;
b = 1;
long long shortest = 100000000000000;
r_rep_ll(i, 1, pow(n, 0.5) + 1) {
if (n % i == 0) {
b = n / i;
shortest = min(shortest, i + b - 2);
}
}
// while (a*b <= 1000000000000){
// if(a*b == n){
// shortest = a + b - 2;
// break;
// }else if(a*b > n){
// break;
// }
// b += 1;
// }
cout << shortest << endl;
} | replace | 14 | 15 | 14 | 15 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
using P = pair<int, int>;
int main() {
ll n;
cin >> n;
ll ans = 1e18;
for (ll i = 0; i * i <= n; i++) {
if (n % i != 0)
continue;
ll j = n / i;
ans = min(ans, i + j - 2);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
using P = pair<int, int>;
int main() {
ll n;
cin >> n;
ll ans = 1e18;
for (ll i = 1; i * i <= n; i++) {
if (n % i != 0)
continue;
ll j = n / i;
ans = min(ans, i + j - 2);
}
cout << ans << endl;
return 0;
} | replace | 10 | 11 | 10 | 11 | -8 | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
long long n;
cin >> n;
long long ans = n - 1;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, (n / i) + i - 2);
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
long long n;
cin >> n;
long long ans = n - 1;
for (long long i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
ans = min(ans, (n / i) + i - 2);
}
}
cout << ans << endl;
return 0;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define INF 10010010010
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using namespace std;
int main() {
ll n;
cin >> n;
ll ans = 1000000000005;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
ll a = i;
ll b = n / i;
ans = min(ans, a + b - 2);
}
}
cout << ans << endl;
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define INF 10010010010
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using namespace std;
int main() {
ll n;
cin >> n;
ll ans = 1000000000005;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ll a = i;
ll b = n / i;
ans = min(ans, a + b - 2);
}
}
cout << ans << endl;
}
| replace | 25 | 26 | 25 | 26 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define DEBUG
int main() {
ll n;
cin >> n;
ll min = (ll)1e18;
for (int i = 1; i < n; i++) {
if ((n % i) == 0 && ((n / i) + i - 2) < min) {
// cout << i << "\t" << n/i << endl;
min = (i + (n / i)) - 2;
}
}
cout << min << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define DEBUG
int main() {
ll n;
cin >> n;
ll min = (ll)1e18;
for (ll i = 1; i <= sqrt(n); i++) {
if ((n % i) == 0 && ((n / i) + i - 2) < min) {
// cout << i << "\t" << n/i << endl;
min = (i + (n / i)) - 2;
}
}
cout << min << endl;
return 0;
}
| replace | 16 | 17 | 16 | 17 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define lint long long int
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define arep(i, a, n) for (int i = a; i < n; i++)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define fill(a, x) fill(a.begin(), a.end(), x)
#define eb(data) emplace_back(data)
#define pb(data) emplace_back(data)
#define mp make_pair
#define ALNUM 26
#define vint vector<int>
#define vlint vector<lint>
#define F first
#define S second
#define ALL(data) data.begin(), data.end()
#define GEts(s) getline(cin, s);
#define UNIQUE(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
template <typename Rast> inline void out(Rast rast) {
cout << rast << "\n";
return;
}
template <typename Rast> inline void in(Rast &rast) {
cin >> rast;
return;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
cin >> first;
in(rest...);
return;
}
template <typename First, typename... Rest>
void out(First first, Rest... rest) {
cout << first << " ";
out(rest...);
return;
}
template <typename T> T gcd(T a, T b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
} else {
return false;
}
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
} else {
return false;
}
}
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }
static const double pi = 3.141592653589793;
lint modpow(lint a, lint n, lint p) {
if (n == 0)
return 1 % p;
if (n == 1)
return a % p;
if (n % 2 == 1)
return (a * modpow(a, n - 1, p)) % p;
lint t = modpow(a, n / 2, p);
return (t * t) % p;
} // a^n%p
lint MOD = pow(10, 9) + 7;
// lint MOD=998244353;
lint inf = pow(2, 50);
int intinf = pow(2, 30);
/**int dirx[]={1,0};int diry[]={0,1};//*/ // 右、下
/**int dirx[]={0,1,0,-1};int diry[]={-1,0,1,0};//*/ // 四方位
/**int dirx[]={-1,0,1,1,1,0,-1,-1};int diry[]={-1,-1,-1,0,1,1,1,0};//*/ // 八方位
class unionfind {
public:
vector<int> table;
void init(int size) {
table.resize(size);
rep(i, size) table[i] = i;
};
int root(int index) {
if (table[index] == index)
return index;
else {
int hoge = root(table[index]);
table[index] = hoge;
return hoge;
}
};
bool same(int x, int y) { return (root(x) == root(y)); };
int marge(int x, int y) {
int yroot = root(y);
int xroot = root(x);
if (xroot == yroot)
return 0;
table[yroot] = xroot;
return 0;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::fixed << std::setprecision(19);
lint n;
in(n);
lint ans = n - 1;
for (int i = 1; i * i <= n; i++)
if (n % i == 0)
chmin(ans, i - 2 + (n / i));
out(ans);
return 0;
}
| #include <bits/stdc++.h>
#define lint long long int
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define arep(i, a, n) for (int i = a; i < n; i++)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define fill(a, x) fill(a.begin(), a.end(), x)
#define eb(data) emplace_back(data)
#define pb(data) emplace_back(data)
#define mp make_pair
#define ALNUM 26
#define vint vector<int>
#define vlint vector<lint>
#define F first
#define S second
#define ALL(data) data.begin(), data.end()
#define GEts(s) getline(cin, s);
#define UNIQUE(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
template <typename Rast> inline void out(Rast rast) {
cout << rast << "\n";
return;
}
template <typename Rast> inline void in(Rast &rast) {
cin >> rast;
return;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
cin >> first;
in(rest...);
return;
}
template <typename First, typename... Rest>
void out(First first, Rest... rest) {
cout << first << " ";
out(rest...);
return;
}
template <typename T> T gcd(T a, T b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
} else {
return false;
}
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
} else {
return false;
}
}
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }
static const double pi = 3.141592653589793;
lint modpow(lint a, lint n, lint p) {
if (n == 0)
return 1 % p;
if (n == 1)
return a % p;
if (n % 2 == 1)
return (a * modpow(a, n - 1, p)) % p;
lint t = modpow(a, n / 2, p);
return (t * t) % p;
} // a^n%p
lint MOD = pow(10, 9) + 7;
// lint MOD=998244353;
lint inf = pow(2, 50);
int intinf = pow(2, 30);
/**int dirx[]={1,0};int diry[]={0,1};//*/ // 右、下
/**int dirx[]={0,1,0,-1};int diry[]={-1,0,1,0};//*/ // 四方位
/**int dirx[]={-1,0,1,1,1,0,-1,-1};int diry[]={-1,-1,-1,0,1,1,1,0};//*/ // 八方位
class unionfind {
public:
vector<int> table;
void init(int size) {
table.resize(size);
rep(i, size) table[i] = i;
};
int root(int index) {
if (table[index] == index)
return index;
else {
int hoge = root(table[index]);
table[index] = hoge;
return hoge;
}
};
bool same(int x, int y) { return (root(x) == root(y)); };
int marge(int x, int y) {
int yroot = root(y);
int xroot = root(x);
if (xroot == yroot)
return 0;
table[yroot] = xroot;
return 0;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::fixed << std::setprecision(19);
lint n;
in(n);
lint ans = n - 1;
for (lint i = 1; i * i <= n; i++)
if (n % i == 0)
chmin(ans, i - 2 + (n / i));
out(ans);
return 0;
}
| replace | 121 | 122 | 121 | 122 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long int int64;
int main() {
int64 n;
cin >> n;
int64 ans = 1e18;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, (i - 1) + ((n / i) - 1));
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int int64;
int main() {
int64 n;
cin >> n;
int64 ans = 1e18;
for (int64 i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, (i - 1) + ((n / i) - 1));
}
}
cout << ans << endl;
return 0;
} | replace | 10 | 11 | 10 | 11 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
signed main() {
int N;
cin >> N;
int ans = N;
for (int i = 1; i * i <= N; i++)
if (N % i == 0)
ans = i + N / i - 2;
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int N;
cin >> N;
int ans = N;
for (int i = 1; i * i <= N; i++)
if (N % i == 0)
ans = i + N / i - 2;
cout << ans << endl;
}
| insert | 2 | 2 | 2 | 3 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ll N;
cin >> N;
ll ans = 1e18;
for (int i = 1; i * i <= N; i++) {
if (N % i != 0) {
continue;
}
ll j = N / i;
ans = min(ans, i + j - 2);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ll N;
cin >> N;
ll ans = 1e18;
for (ll i = 1; i * i <= N; i++) {
if (N % i != 0) {
continue;
}
ll j = N / i;
ans = min(ans, i + j - 2);
}
cout << ans << endl;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
ll n, ans = 1e12 + 7;
cin >> n;
for (ll i = 1; i * 1 <= n; i++) {
if (n % i == 0)
ans = min(ans, i + n / i - 2);
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
ll n, ans = 1e12 + 7;
cin >> n;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0)
ans = min(ans, i + n / i - 2);
}
cout << ans;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>
int main() {
uint64_t target;
std::cin >> target;
uint64_t sq_target = std::sqrt(target);
uint64_t ans = std::numeric_limits<uint64_t>::max();
for (uint64_t i = 1; i <= sq_target; ++i) {
for (uint64_t j = i;; ++j) {
if (i * j >= target) {
if (i * j == target)
ans = std::min(ans, (i + j - 2));
break;
}
}
}
std::cout << ans << std::endl;
} | #include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>
int main() {
uint64_t target;
std::cin >> target;
uint64_t sq_target = std::sqrt(target);
uint64_t ans = std::numeric_limits<uint64_t>::max();
for (uint64_t i = 1; i <= sq_target; ++i) {
if (target % i == 0) {
ans = std::min(ans, i + (target / i) - 2);
}
}
std::cout << ans << std::endl;
} | replace | 13 | 19 | 13 | 15 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i < n; ++i)
#define EPS (1e-7)
#define gearup \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
ll MOD = 1000000007;
const long long INF = 1LL << 60;
// cout<<fixed<<setprecision(10);
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> void debug(T v) {
rep(i, v.size()) cout << v[i] << " ";
cout << endl;
}
signed main() {
// gearup;
ll n;
cin >> n;
// 素数判定
bool is_prime = true;
ll res = 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
is_prime = false;
res = i;
}
}
if (!is_prime)
cout << res - 1 + n / res - 1 << endl;
else
cout << n - 1 << endl;
}
| #include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i < n; ++i)
#define EPS (1e-7)
#define gearup \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
ll MOD = 1000000007;
const long long INF = 1LL << 60;
// cout<<fixed<<setprecision(10);
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> void debug(T v) {
rep(i, v.size()) cout << v[i] << " ";
cout << endl;
}
signed main() {
// gearup;
ll n;
cin >> n;
// 素数判定
bool is_prime = true;
ll res = 0;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
is_prime = false;
res = i;
}
}
if (!is_prime)
cout << res - 1 + n / res - 1 << endl;
else
cout << n - 1 << endl;
}
| replace | 47 | 48 | 47 | 48 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
long long n;
cin >> n;
long long mi = n - 1;
for (int i = 1; i < n + 1; i++) {
if (n % i == 0) {
mi = min(mi, (i - 1) + (n / i - 1));
}
}
cout << mi;
} | #include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
long long n;
cin >> n;
long long mi = n - 1;
for (long long i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
mi = min(mi, (i - 1) + (n / i - 1));
}
}
cout << mi;
} | replace | 19 | 20 | 19 | 20 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
std::ios::sync_with_stdio(0);
cin.tie(0);
ll ans = 0;
ll anss = 0;
ll count = 0;
ll i, j;
ll K;
vector<ll> a;
ll Q, N;
string S;
cin >> N;
ll flag = 0;
ll t1 = 0;
ll t2 = 0;
for (i = (int)(sqrt(N) + 0.9); i < N + 1; i++) {
if (N % i == 0) {
cout << i + N / i - 2 << endl;
return 0;
}
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
std::ios::sync_with_stdio(0);
cin.tie(0);
ll ans = 0;
ll anss = 0;
ll count = 0;
ll i, j;
ll K;
vector<ll> a;
ll Q, N;
string S;
cin >> N;
ll flag = 0;
ll t1 = 0;
ll t2 = 0;
for (i = (int)(sqrt(N)); i > 0; i--) {
if (N % i == 0) {
cout << i + N / i - 2 << endl;
return 0;
}
}
return 0;
} | replace | 39 | 40 | 39 | 40 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main() {
int n;
cin >> n;
int ans = n - 1;
for (int i = 1; i < n; ++i) {
if (n % i == 0)
ans = min(ans, i + n / i - 2);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main() {
int n;
cin >> n;
int ans = n - 1;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0)
ans = min(ans, i + n / i - 2);
}
cout << ans << endl;
return 0;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
using namespace std;
long long chmin(long long a, long long b) {
if (a > b) {
return b;
}
return a;
}
int main() {
long long n;
cin >> n;
long long min = n - 1;
for (long long i = 1; i <= (int)sqrt(n); i++) {
for (long long j = i; j <= n; j++) {
if (i * j > n)
break;
if (i * j == n) {
min = chmin(min, i - 1 + j - 1);
}
}
}
cout << min << endl;
return 0;
} | #include <cmath>
#include <iostream>
using namespace std;
long long chmin(long long a, long long b) {
if (a > b) {
return b;
}
return a;
}
int main() {
long long n;
cin >> n;
long long min = n - 1;
for (long long i = 1; i <= (int)sqrt(n); i++) {
long long j = n / i;
if (n % i == 0) {
min = chmin(min, i - 1 + j - 1);
}
}
cout << min << endl;
return 0;
} | replace | 15 | 21 | 15 | 18 | TLE | |
p02881 | C++ | Runtime Error | #include <cmath>
#include <iostream>
using namespace std;
int main() {
long n;
cin >> n;
long dmax = sqrt(n);
while (dmax--)
if (n % dmax == 0)
break;
cout << dmax + n / dmax - 2 << endl;
}
| #include <cmath>
#include <iostream>
using namespace std;
int main() {
long n;
cin >> n;
long dmax = sqrt(n) + 1;
while (dmax--)
if (n % dmax == 0)
break;
cout << dmax + n / dmax - 2 << endl;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll ans = n - 1;
for (ll i = 1; i <= n; i++) {
ll j = (ll)(n / i);
if (i * j == n) {
if (i + j - 2 < ans) {
ans = i + j - 2;
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll ans = n - 1;
for (ll i = 1; i <= sqrt(n); i++) {
ll j = (ll)(n / i);
if (i * j == n) {
if (i + j - 2 < ans) {
ans = i + j - 2;
}
}
}
cout << ans << endl;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
int main() {
ll n;
cin >> n;
ll ans = 10000000000000;
for (ll i = 1; i <= n; i++) {
if (n % i == 0) {
ll x = i;
ll y = n / i;
ans = min(ans, x + y - 2);
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
int main() {
ll n;
cin >> n;
ll ans = 10000000000000;
for (ll i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
ll x = i;
ll y = n / i;
ans = min(ans, x + y - 2);
}
}
cout << ans << endl;
} | replace | 11 | 12 | 11 | 12 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long int i, j, N, min, flag;
flag = 0;
cin >> N;
if (N > 1000000) {
for (i = 1; i <= N / 1000; i++) {
if (0 == N % i) {
j = N / i;
// 初回
if (flag == 0) {
min = (j + i) - 2;
flag = flag + 1;
}
// 2回目以降
if (min > (j + i) - 2) {
min = (j + i) - 2;
}
}
}
} else {
for (i = 1; i <= N; i++) {
if (0 == N % i) {
j = N / i;
// 初回
if (flag == 0) {
min = (j + i) - 2;
flag = flag + 1;
}
// 2回目以降
if (min > (j + i) - 2) {
min = (j + i) - 2;
}
}
}
}
cout << min << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long int i, j, N, min, flag;
flag = 0;
cin >> N;
if (N > 1000000) {
for (i = 1; i <= N / 10000; i++) {
if (0 == N % i) {
j = N / i;
// 初回
if (flag == 0) {
min = (j + i) - 2;
flag = flag + 1;
}
// 2回目以降
if (min > (j + i) - 2) {
min = (j + i) - 2;
}
}
}
} else {
for (i = 1; i <= N; i++) {
if (0 == N % i) {
j = N / i;
// 初回
if (flag == 0) {
min = (j + i) - 2;
flag = flag + 1;
}
// 2回目以降
if (min > (j + i) - 2) {
min = (j + i) - 2;
}
}
}
}
cout << min << endl;
}
| replace | 8 | 9 | 8 | 9 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
long long int n, a, b, res;
cin >> n;
res = n + 1;
for (int i = 2; i < n / 2; i++) {
if (n % i == 0) {
b = n / i;
a = i;
res = min(res, a + b);
}
}
cout << res - 2;
return 0;
}
| #include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
long long int n, a, b, res;
cin >> n;
res = n + 1;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
b = n / i;
a = i;
res = min(res, a + b);
}
}
cout << res - 2;
return 0;
}
| replace | 8 | 9 | 8 | 9 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#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 rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
// const int64 INF = 1LL << 58;
#define dame \
{ \
puts("-1"); \
return 0; \
}
#define YES \
{ \
cout << "YES" << endl; \
return 0; \
}
#define NO \
{ \
cout << "NO" << endl; \
return 0; \
}
#define Yes \
{ \
cout << "Yes" << endl; \
return 0; \
}
#define No \
{ \
cout << "No" << endl; \
return 0; \
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
ll a, b;
for (int i = 1; i * i <= N; i++) {
if (N % i == 0) {
a = i;
b = N / i;
}
}
cout << (a - 1) + (b - 1) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#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 rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
// const int64 INF = 1LL << 58;
#define dame \
{ \
puts("-1"); \
return 0; \
}
#define YES \
{ \
cout << "YES" << endl; \
return 0; \
}
#define NO \
{ \
cout << "NO" << endl; \
return 0; \
}
#define Yes \
{ \
cout << "Yes" << endl; \
return 0; \
}
#define No \
{ \
cout << "No" << endl; \
return 0; \
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
ll a, b;
for (ll i = 1; i * i <= N; i++) {
if (N % i == 0) {
a = i;
b = N / i;
}
}
cout << (a - 1) + (b - 1) << endl;
}
| replace | 84 | 85 | 84 | 85 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <math.h>
#include <stdio.h>
int main() {
long N, ans = 0;
scanf("%ld", &N);
for (long i = sqrt(N); i < N; ++i) {
if (N % i == 0) {
ans = N / i + i - 2;
break;
}
}
printf("%ld", ans);
} | #include <math.h>
#include <stdio.h>
int main() {
long N, ans = 0;
scanf("%ld", &N);
for (long i = sqrt(N); i > 0; --i) {
if (N % i == 0) {
ans = N / i + i - 2;
break;
}
}
printf("%ld", ans);
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ld long double
#define ll long long int
#define f(i, g, n) for (int i = g; i <= n; i++)
#define rf(i, g, n) for (int i = n; i >= g; i--)
const int N = 1e3 + 1;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n, m, k;
cin >> n;
m = sqrt(n);
for (int i = m; i >= 1; i--) {
if (n % i == 0) {
m = i;
k = n / i;
break;
}
}
cout << m + k - 2;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ld long double
#define ll long long int
#define f(i, g, n) for (int i = g; i <= n; i++)
#define rf(i, g, n) for (int i = n; i >= g; i--)
const int N = 1e3 + 1;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
ll n, m, k;
cin >> n;
m = sqrt(n);
for (int i = m; i >= 1; i--) {
if (n % i == 0) {
m = i;
k = n / i;
break;
}
}
cout << m + k - 2;
return 0;
} | delete | 16 | 21 | 16 | 16 | 0 | |
p02881 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
long long N;
cin >> N;
long long a = 1, b = N;
long long M = a + b;
for (long long i = N / 2; i > 1; i--) {
if ((N % i == 0) && ((i + N / i) < M)) {
a = i;
b = N / i;
M = a + b;
}
}
if (a == 1 && b == 1)
cout << "0" << endl;
else
cout << a + b - 2 << endl;
} | #include <iostream>
using namespace std;
int main() {
long long N;
cin >> N;
long long a = 1, b = N;
long long M = a + b;
for (long long i = 2; i * i <= N; i++) {
if ((N % i == 0) && ((i + N / i) < M)) {
a = i;
b = N / i;
M = a + b;
}
}
if (a == 1 && b == 1)
cout << "0" << endl;
else
cout << a + b - 2 << endl;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
typedef long long ll;
using namespace std;
int main() {
ll N;
cin >> N;
ll a = 1000000000000;
for (ll i = 1; i < (N / 2 + 1); i++) {
if (N % i == 0) {
if (a > (i + (N / i) - 2)) {
a = i;
a += (N / i) - 2;
}
}
}
cout << a;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
typedef long long ll;
using namespace std;
int main() {
ll N;
cin >> N;
ll a = 10000000000000;
for (ll i = 1; i < 5000001; i++) {
if (N % i == 0) {
if (a > (i + (N / i) - 2)) {
a = i;
a += (N / i) - 2;
}
}
}
cout << a;
return 0;
}
| replace | 12 | 14 | 12 | 14 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pp = pair<ll, ll>;
const ll amari = 1e9 + 7;
const char BC = 'A' - 'a';
#define ben(a) a.begin(), a.end() // 昇順
#define enb(a) a.rbegin(), a.rend() // 降順
#define pb(a, b) a.push_back(b)
// fixed << setprecision(20)
int main() {
ll n;
cin >> n;
ll a = sqrt(n);
ll maxi = 0;
for (ll i = 1; i < a; i++) {
if (n % i == 0) {
maxi = max(i, maxi);
}
}
cout << maxi + (n / maxi) - 2 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pp = pair<ll, ll>;
const ll amari = 1e9 + 7;
const char BC = 'A' - 'a';
#define ben(a) a.begin(), a.end() // 昇順
#define enb(a) a.rbegin(), a.rend() // 降順
#define pb(a, b) a.push_back(b)
// fixed << setprecision(20)
int main() {
ll n;
cin >> n;
ll a = sqrt(n);
ll maxi = 0;
for (ll i = 1; i <= a; i++) {
if (n % i == 0) {
maxi = max(i, maxi);
}
}
cout << maxi + (n / maxi) - 2 << endl;
return 0;
}
| replace | 17 | 18 | 17 | 18 | 0 | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long N;
cin >> N;
long a = N, b = 0;
for (long i = 1; i <= N / 2; i++) {
if (N % i == 0) {
b = (i - 1) + (N / i - 1);
a = min(a, b);
}
}
cout << a << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long N;
cin >> N;
long a = N, b = 0;
for (long i = 1; i <= sqrt(N); i++) {
if (N % i == 0) {
b = (i - 1) + (N / i - 1);
a = min(a, b);
}
}
cout << a << endl;
} | replace | 7 | 8 | 7 | 8 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int num, n;
int digits;
vector<long long int> vec;
cin >> n;
digits = int(floor(log10(n) + 1));
digits = (digits / 2);
for (int i = 1; i <= (int(pow(10, digits)) * int(pow(10, 2))); i++) {
if (n % i == 0 && vec.size() == 0) {
vec.push_back(i);
vec.push_back((n / i));
} else if (n % i == 0 && vec.size() != 0) {
vec.push_back(i);
vec.push_back((n / i));
}
}
sort(vec.begin(), vec.end());
num = (vec.size() / 2);
cout << (vec[num - 1] - 1) + (vec[num] - 1) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long int num, n;
int digits;
vector<long long int> vec;
cin >> n;
digits = int(floor(log10(n) + 1));
digits = (digits / 2);
for (int i = 1; i <= int(pow(10, (digits + 1))); i++) {
if (n % i == 0 && vec.size() == 0) {
vec.push_back(i);
vec.push_back((n / i));
} else if (n % i == 0 && vec.size() != 0) {
vec.push_back(i);
vec.push_back((n / i));
}
}
sort(vec.begin(), vec.end());
num = (vec.size() / 2);
cout << (vec[num - 1] - 1) + (vec[num] - 1) << endl;
return 0;
}
| replace | 11 | 12 | 11 | 12 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define Cout(s) cout << s << endl
using namespace std;
typedef long long ll;
string Yes(bool b) {
if (b) {
return "Yes";
} else {
return "No";
}
}
string YES(bool b) {
if (b) {
return "YES";
} else {
return "NO";
}
}
//================================================
bool primejudge(const ll x) {
if (x == 2)
return true;
if (x < 2 || x % 2 == 0)
return false;
for (int i = 3; i * i <= x; i += 2)
if (x % i == 0)
return false;
return true;
}
int a = 0, b = 0, c, d, n, k = 0, ans = 0;
string s, t;
int main() {
ll n;
cin >> n;
if (primejudge(n)) {
cout << n - 1 << endl;
return 0;
}
int flag = 1;
for (int i = sqrt(n); i > 0; i--) {
if (n % i == 0) {
flag = i;
break;
}
}
cout << flag + n / flag - 2 << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define Cout(s) cout << s << endl
using namespace std;
typedef long long ll;
string Yes(bool b) {
if (b) {
return "Yes";
} else {
return "No";
}
}
string YES(bool b) {
if (b) {
return "YES";
} else {
return "NO";
}
}
//================================================
bool primejudge(const ll x) {
if (x == 2)
return true;
if (x < 2 || x % 2 == 0)
return false;
for (int i = 3; i * i <= x; i += 2)
if (x % i == 0)
return false;
return true;
}
int a = 0, b = 0, c, d, n, k = 0, ans = 0;
string s, t;
int main() {
ll n;
cin >> n;
int flag = 1;
for (int i = sqrt(n); i > 0; i--) {
if (n % i == 0) {
flag = i;
break;
}
}
cout << flag + n / flag - 2 << endl;
return 0;
}
| delete | 50 | 54 | 50 | 50 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"
using namespace std;
constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;
// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
long long int ans = MOD * MOD;
for (int i = 1; i * i <= N; i++) {
if (N % i)
continue;
ans = min(ans, i + N / i);
}
cout << ans - 2 << endl;
}
| #include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"
using namespace std;
constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;
// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
long long int ans = MOD * MOD;
for (long long int i = 1; i * i <= N; i++) {
if (N % i)
continue;
ans = min(ans, i + N / i);
}
cout << ans - 2 << endl;
}
| replace | 37 | 38 | 37 | 38 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
long long MOD = 1e9 + 7;
using namespace std;
int main() {
ll N;
cin >> N;
vector<ll> ya;
ll ku = 0;
if (N % 2 == 0) {
ku = N / 2;
} else {
ku = (N + 1) / 2;
}
for (ll i = 1; i < ku + 1; i++) {
if (N % i == 0) {
ya.push_back(i);
}
}
ll res = 1e12 + 1;
for (const ll &e : ya) {
res = min(res, e + (N / e) - 2);
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
long long MOD = 1e9 + 7;
using namespace std;
int main() {
ll N;
cin >> N;
vector<ll> ya;
for (ll i = 1; i * i < N + 1; i++) {
if (N % i == 0) {
ya.push_back(i);
}
}
ll res = 1e12 + 1;
for (const ll &e : ya) {
res = min(res, e + (N / e) - 2);
}
cout << res << endl;
return 0;
} | replace | 12 | 19 | 12 | 13 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
// f
int main(void) {
long long n;
cin >> n;
long long ans = n;
for (long long i = 1; i * i <= n; i++) {
for (long long j = i; i * j <= n; j++) {
if (i * j == n) {
ans = min(ans, i - 1 + j - 1);
}
}
}
cout << ans << endl;
return 0;
} | #include <iostream>
using namespace std;
// f
int main(void) {
long long n;
cin >> n;
long long ans = n;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, i - 1 + n / i - 1);
}
}
cout << ans << endl;
return 0;
}
| replace | 8 | 12 | 8 | 10 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
/**** Type Define ****/
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> Q;
/**** Const List ****/
const ll INF = 1LL << 60;
const ll mod = 1000000007;
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, -1, 0, 1};
/**** General Functions ****/
ll ketawa(ll n) {
ll a = 0;
while (n != 0) {
a += n % 10;
n /= 10;
}
return a;
}
ll RepeatSquaring(ll N, ll P, ll M) {
if (P == 0)
return 1;
if (P % 2 == 0) {
ll t = RepeatSquaring(N, P / 2, M);
return t * t % M;
}
return N * RepeatSquaring(N, P - 1, M);
}
bool IsPrime(ll a) { // order root a
if (a == 1)
return false;
for (int i = 2; i * i <= a; i++) {
if (a % i == 0 && a != i) {
return false;
}
}
return true;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
ll q = a / b, g = extgcd(b, a - q * b, x, y);
ll z = x - q * y;
x = y;
y = z;
return g;
}
ll invmod(ll a, ll m) { // a^-1 mod m
ll x, y;
extgcd(a, m, x, y);
x %= m;
if (x < 0)
x += m;
return x;
}
ll nCk(ll n, ll k, ll mod) {
ll ans = 1;
for (ll i = n, j = 1; j <= k; i--, j++)
ans = (((ans * i) % mod) * invmod(j, mod)) % mod;
return ans;
}
ll lmin(ll a, ll b) { return a > b ? b : a; };
ll lmax(ll a, ll b) { return a > b ? a : b; };
ll lsum(ll a, ll b) { return a + b; };
/**** Zip ****/
template <typename T> class Zip {
vector<T> d;
bool flag;
public:
Zip() { flag = false; }
void add(T x) {
d.push_back(x);
flag = true;
}
ll getNum(T x) { // T need to have operator < !!
if (flag) {
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
flag = false;
}
return lower_bound(d.begin(), d.end(), x) - d.begin();
}
ll size() {
if (flag) {
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
flag = false;
}
return (ll)d.size();
}
};
/**** Union Find ****/
class UnionFind {
vector<ll> par, rank; // par > 0: number, par < 0: -par
public:
void init(ll n) {
par.resize(n, 1);
rank.resize(n, 0);
}
ll getSize(ll x) { return par[find(x)]; }
ll find(ll x) {
if (par[x] > 0)
return x;
return -(par[x] = -find(-par[x]));
}
void merge(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
par[y] += par[x];
par[x] = -y;
} else {
par[x] += par[y];
par[y] = -x;
if (rank[x] == rank[y])
rank[x]++;
}
}
bool isSame(ll x, ll y) { return find(x) == find(y); }
};
/**** Segment Tree ****/
template <typename T> class SegmentTree {
ll n;
vector<T> node;
function<T(T, T)> fun, fun2;
bool customChange;
T outValue, initValue;
public:
void init(ll num, function<T(T, T)> resultFunction, T init, T out,
function<T(T, T)> changeFunction = NULL) {
// changeFunction: (input, beforevalue) => newvalue
fun = resultFunction;
fun2 = changeFunction;
customChange = changeFunction != NULL;
n = 1;
while (n < num)
n *= 2;
node.resize(2 * n - 1, init);
outValue = out;
initValue = init;
}
void valueChange(ll num, T value) {
num += n - 1;
if (customChange)
node[num] = fun2(value, node[num]);
else
node[num] = value;
while (num > 0)
num = (num - 1) / 2,
node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);
}
T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b)
if (r == -1)
r = n;
if (a <= l && r <= b)
return node[k];
if (b <= l || r <= a)
return outValue;
ll mid = (l + r) / 2;
return fun(rangeQuery(a, b, l, mid, 2 * k + 1),
rangeQuery(a, b, mid, r, 2 * k + 2));
}
};
/**** LIS ****/
ll lis(ll *a, ll n, ll *dp) {
fill(dp, dp + n, INF); // INFを代入
for (ll i = 0; i < n; i++)
*lower_bound(dp, dp + n, a[i]) = a[i];
return (ll)(lower_bound(dp, dp + n, INF) - dp);
}
/**** main function ****/
ll n;
ll i = 2, aa = 1, bb = 1, ans = 9999999999999;
int main() {
cin >> n;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = lmin(i + n / i - 2, ans);
}
}
cout << ans << endl;
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
/**** Type Define ****/
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> Q;
/**** Const List ****/
const ll INF = 1LL << 60;
const ll mod = 1000000007;
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, -1, 0, 1};
/**** General Functions ****/
ll ketawa(ll n) {
ll a = 0;
while (n != 0) {
a += n % 10;
n /= 10;
}
return a;
}
ll RepeatSquaring(ll N, ll P, ll M) {
if (P == 0)
return 1;
if (P % 2 == 0) {
ll t = RepeatSquaring(N, P / 2, M);
return t * t % M;
}
return N * RepeatSquaring(N, P - 1, M);
}
bool IsPrime(ll a) { // order root a
if (a == 1)
return false;
for (int i = 2; i * i <= a; i++) {
if (a % i == 0 && a != i) {
return false;
}
}
return true;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
ll q = a / b, g = extgcd(b, a - q * b, x, y);
ll z = x - q * y;
x = y;
y = z;
return g;
}
ll invmod(ll a, ll m) { // a^-1 mod m
ll x, y;
extgcd(a, m, x, y);
x %= m;
if (x < 0)
x += m;
return x;
}
ll nCk(ll n, ll k, ll mod) {
ll ans = 1;
for (ll i = n, j = 1; j <= k; i--, j++)
ans = (((ans * i) % mod) * invmod(j, mod)) % mod;
return ans;
}
ll lmin(ll a, ll b) { return a > b ? b : a; };
ll lmax(ll a, ll b) { return a > b ? a : b; };
ll lsum(ll a, ll b) { return a + b; };
/**** Zip ****/
template <typename T> class Zip {
vector<T> d;
bool flag;
public:
Zip() { flag = false; }
void add(T x) {
d.push_back(x);
flag = true;
}
ll getNum(T x) { // T need to have operator < !!
if (flag) {
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
flag = false;
}
return lower_bound(d.begin(), d.end(), x) - d.begin();
}
ll size() {
if (flag) {
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
flag = false;
}
return (ll)d.size();
}
};
/**** Union Find ****/
class UnionFind {
vector<ll> par, rank; // par > 0: number, par < 0: -par
public:
void init(ll n) {
par.resize(n, 1);
rank.resize(n, 0);
}
ll getSize(ll x) { return par[find(x)]; }
ll find(ll x) {
if (par[x] > 0)
return x;
return -(par[x] = -find(-par[x]));
}
void merge(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
par[y] += par[x];
par[x] = -y;
} else {
par[x] += par[y];
par[y] = -x;
if (rank[x] == rank[y])
rank[x]++;
}
}
bool isSame(ll x, ll y) { return find(x) == find(y); }
};
/**** Segment Tree ****/
template <typename T> class SegmentTree {
ll n;
vector<T> node;
function<T(T, T)> fun, fun2;
bool customChange;
T outValue, initValue;
public:
void init(ll num, function<T(T, T)> resultFunction, T init, T out,
function<T(T, T)> changeFunction = NULL) {
// changeFunction: (input, beforevalue) => newvalue
fun = resultFunction;
fun2 = changeFunction;
customChange = changeFunction != NULL;
n = 1;
while (n < num)
n *= 2;
node.resize(2 * n - 1, init);
outValue = out;
initValue = init;
}
void valueChange(ll num, T value) {
num += n - 1;
if (customChange)
node[num] = fun2(value, node[num]);
else
node[num] = value;
while (num > 0)
num = (num - 1) / 2,
node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);
}
T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b)
if (r == -1)
r = n;
if (a <= l && r <= b)
return node[k];
if (b <= l || r <= a)
return outValue;
ll mid = (l + r) / 2;
return fun(rangeQuery(a, b, l, mid, 2 * k + 1),
rangeQuery(a, b, mid, r, 2 * k + 2));
}
};
/**** LIS ****/
ll lis(ll *a, ll n, ll *dp) {
fill(dp, dp + n, INF); // INFを代入
for (ll i = 0; i < n; i++)
*lower_bound(dp, dp + n, a[i]) = a[i];
return (ll)(lower_bound(dp, dp + n, INF) - dp);
}
/**** main function ****/
ll n;
ll i = 2, aa = 1, bb = 1, ans = 9999999999999;
int main() {
cin >> n;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = lmin(i + n / i - 2, ans);
}
}
cout << ans << endl;
}
| replace | 219 | 220 | 219 | 220 | TLE | |
p02881 | C++ | Time Limit Exceeded | #ifndef LOCAL
#pragma GCC optimize("O2")
#endif
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int main() {
long long int N;
cin >> N;
long long int score;
score = 1e9;
for (int i = (int)sqrt(N); i <= N; i++) {
if (N % i == 0) {
// cout << score << "\n";
score = min(i + N / i - 2, score);
}
}
cout << score;
} | #ifndef LOCAL
#pragma GCC optimize("O2")
#endif
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int main() {
long long int N;
cin >> N;
long long int score;
score = __LONG_LONG_MAX__;
for (int i = 1; i <= (int)sqrt(N) + 1; i++) {
if (N % i == 0) {
// cout << score << "\n";
score = min(i + N / i - 2, score);
}
}
cout << score;
} | replace | 13 | 15 | 13 | 15 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int NMAX = 2e5 + 10;
const int MOD = 1e9 + 7;
int main(int argc, char const *argv[]) {
ll n;
ll ans;
scanf("%lld", &n);
ans = n;
for (register int i = 1; i * i <= n; i++)
if (n % i == 0)
ans = min(ans, i + n / i - 2);
printf("%lld\n", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int NMAX = 2e5 + 10;
const int MOD = 1e9 + 7;
int main(int argc, char const *argv[]) {
ll n;
ll ans;
scanf("%lld", &n);
ans = n;
for (register ll i = 1; i * i <= n; i++)
if (n % i == 0)
ans = min(ans, i + n / i - 2);
printf("%lld\n", ans);
return 0;
} | replace | 11 | 12 | 11 | 12 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define pp pair<int, int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define mk(arr, n, type) type *arr = new type[n];
#define sl s1.length();
using namespace std;
void fastio() {
#ifndef ONLINE_JUDGE
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
#endif
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int p(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
signed main() {
fastio();
int n, c = 0, a;
cin >> n;
int x = sqrt(n);
if (p(n) == 1)
cout << n - 1;
else {
for (int i = x; i <= x + 100000; i++) {
if (n % i == 0) {
a = i;
break;
}
}
int zz = n / a;
cout << (a + zz) - 2;
}
} | #include <bits/stdc++.h>
#define int long long
#define pp pair<int, int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define mk(arr, n, type) type *arr = new type[n];
#define sl s1.length();
using namespace std;
void fastio() {
#ifndef ONLINE_JUDGE
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
#endif
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int p(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
signed main() {
fastio();
int n, c = 0, a;
cin >> n;
int x = sqrt(n);
if (p(n) == 1)
cout << n - 1;
else {
for (int i = x; i >= 1; i--) {
if (n % i == 0) {
a = i;
break;
}
}
int zz = n / a;
cout << (a + zz) - 2;
}
} | replace | 34 | 35 | 34 | 35 | 0 | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
int main() {
long long int n;
cin >> n;
long long int a;
long long int c;
for (long long int i = 0; i < 1000000; i++) {
if (n <= i * i) {
a = i;
break;
}
}
long long int b;
for (long long int i = a; i > 0; i--) {
if (n % i == 0) {
b = i;
break;
}
}
c = n / b;
cout << c + b - 2;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
int main() {
long long int n;
cin >> n;
long long int a;
long long int c;
for (long long int i = 0; i < 1000001; i++) {
if (n <= i * i) {
a = i;
break;
}
}
long long int b;
for (long long int i = a; i > 0; i--) {
if (n % i == 0) {
b = i;
break;
}
}
c = n / b;
cout << c + b - 2;
return 0;
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define inf INT_MAX
#define sc(a) scanf("%d", &a)
#define scc(a, b) scanf("%d %d", &a, &b)
#define sccc(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pf(a) printf("%d", a)
#define pff(a, b) printf("%d %d", a, b)
#define pfff(a, b, c) printf("%d %d %d", a, b, c)
#define pb(a) push_back(a)
#define em empty()
#define fr front()
#define cl clear()
#define sz size()
#define mp(a, b) make_pair(a, b)
#define fri(b) freopen(b, "r", stdin)
#define fro(b) freopen(b, "w", stdout)
/*
ID: uslusam1
TASK: test
LANG: C++
*/
using namespace std;
int main() {
// fri("a.gir");
// fro("a.cik");
long long n;
cin >> n;
long long x = (int)sqrt(n);
while (n % x != 0) {
x++;
}
long long y = n / x;
cout << x + y - 2;
return 0;
} | #include <bits/stdc++.h>
#define inf INT_MAX
#define sc(a) scanf("%d", &a)
#define scc(a, b) scanf("%d %d", &a, &b)
#define sccc(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pf(a) printf("%d", a)
#define pff(a, b) printf("%d %d", a, b)
#define pfff(a, b, c) printf("%d %d %d", a, b, c)
#define pb(a) push_back(a)
#define em empty()
#define fr front()
#define cl clear()
#define sz size()
#define mp(a, b) make_pair(a, b)
#define fri(b) freopen(b, "r", stdin)
#define fro(b) freopen(b, "w", stdout)
/*
ID: uslusam1
TASK: test
LANG: C++
*/
using namespace std;
int main() {
// fri("a.gir");
// fro("a.cik");
long long n;
cin >> n;
long long x = (int)sqrt(n);
while (n % x != 0) {
x--;
}
long long y = n / x;
cout << x + y - 2;
return 0;
} | replace | 30 | 31 | 30 | 31 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
long long n, a, b, c = 1;
int main() {
cin >> n;
for (a = 2; a * a <= n && c; a++)
c = (n % a != 0);
if (!c) {
c = n;
for (a = 2; a * 2 <= n; a++) {
if (n % a == 0) {
b = n / a;
if (a + b - 2 < c)
c = a + b - 2;
}
}
cout << c << '\n';
} else
cout << n - 1 << '\n';
} | #include <iostream>
using namespace std;
long long n, a, b, c = 1;
int main() {
cin >> n;
for (a = 2; a * a <= n && c; a++)
c = (n % a != 0);
if (!c) {
c = n;
for (a = 2; a * a <= n; a++) {
if (n % a == 0) {
b = n / a;
if (a + b - 2 < c)
c = a + b - 2;
}
}
cout << c << '\n';
} else
cout << n - 1 << '\n';
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
#define int long long
int n;
vector<int> v;
void check(int a, int b) {
if (a * b == n)
v.push_back(a + b - 2);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
check(i, n / i);
}
cout << *min_element(v.begin(), v.end());
} | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
#define int long long
int n;
vector<int> v;
void check(int a, int b) {
if (a * b == n)
v.push_back(a + b - 2);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= sqrt(n); ++i) {
check(i, n / i);
}
cout << *min_element(v.begin(), v.end());
} | replace | 18 | 19 | 18 | 19 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b[1000], c = 0, d;
cin >> a;
for (int i = 1; i <= 1000000; i++) {
if (a % i == 0) {
d = a / i;
b[c] = d + i - 2;
c++;
}
}
sort(b, b + c);
cout << b[0] << endl;
return (0);
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b[10000000], c = 0, d;
cin >> a;
for (int i = 1; i <= 1000000; i++) {
if (a % i == 0) {
d = a / i;
b[c] = d + i - 2;
c++;
}
}
sort(b, b + c);
cout << b[0] << endl;
return (0);
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
const long long INF = 1000000007;
using namespace std;
typedef long long llong;
// int isalpha(char ch): ch がアルファベットなら true を返す
// int isdigit(char ch): ch が数字なら true を返す
// int islower(char ch): ch が小文字なら true を返す
// int isupper(char ch): ch が大文字なら true を返す
// int tolower(char ch): ch の小文字を返す
// int toupper(char ch): ch の大文字を返す
// string型
// size() 文字数を返す
// Insert() (指定した場所に)文字・文字列を挿入する
// erase() (指定した場所の)文字・文字列を削除する
// clear() すべての文字を削除する
// substr() 文字列の(指定した)部分文字列を返す
// replace() (指定した)部分文字列を新しい文字列に置換する
// c_str()変換
// 文字列の比較は、<=や==などを使え
// replace関数を使い、簡単に文字列を置換
// リバース関数:reverse(str.begin(), str.end());
// map<type, type> dict;で宣言
// グラフ理論用変数
// vector<vector<llong> > graph(N);
// ソート
// 降順sort(v.begin(), v.end(), std::greater<Type>());
// 大文字から小文字へんかん
// w[i] = w[i]-'A'+'a';
// vector
// assignメソッド 引数:サイズ、値
// 与えられたサイズと値でvectorを初期化する
// queueクラス
// find()次に取り出す値の表示をする。
// pop()値を取り出す。戻り値はなし
// push()キューに値をプッシュする
// priority_queueクラス
// 切り上げ
// ceil
// floor
vector<pair<llong, llong>> Enumdivisor(llong N) {
vector<pair<llong, llong>> A;
pair<llong, llong> X;
for (int i = 1; i * i <= N; i++) {
if (N % i == 0) {
X.first = i;
X.second = N / i;
A.push_back(X);
}
}
return A;
}
int main() {
llong N;
cin >> N;
llong ans = 10000000000000;
vector<pair<llong, llong>> divisor_list;
divisor_list = Enumdivisor(N);
for (auto x : divisor_list) {
ans = min(ans, x.first - 2 + x.second);
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
const long long INF = 1000000007;
using namespace std;
typedef long long llong;
// int isalpha(char ch): ch がアルファベットなら true を返す
// int isdigit(char ch): ch が数字なら true を返す
// int islower(char ch): ch が小文字なら true を返す
// int isupper(char ch): ch が大文字なら true を返す
// int tolower(char ch): ch の小文字を返す
// int toupper(char ch): ch の大文字を返す
// string型
// size() 文字数を返す
// Insert() (指定した場所に)文字・文字列を挿入する
// erase() (指定した場所の)文字・文字列を削除する
// clear() すべての文字を削除する
// substr() 文字列の(指定した)部分文字列を返す
// replace() (指定した)部分文字列を新しい文字列に置換する
// c_str()変換
// 文字列の比較は、<=や==などを使え
// replace関数を使い、簡単に文字列を置換
// リバース関数:reverse(str.begin(), str.end());
// map<type, type> dict;で宣言
// グラフ理論用変数
// vector<vector<llong> > graph(N);
// ソート
// 降順sort(v.begin(), v.end(), std::greater<Type>());
// 大文字から小文字へんかん
// w[i] = w[i]-'A'+'a';
// vector
// assignメソッド 引数:サイズ、値
// 与えられたサイズと値でvectorを初期化する
// queueクラス
// find()次に取り出す値の表示をする。
// pop()値を取り出す。戻り値はなし
// push()キューに値をプッシュする
// priority_queueクラス
// 切り上げ
// ceil
// floor
vector<pair<llong, llong>> Enumdivisor(llong N) {
vector<pair<llong, llong>> A;
pair<llong, llong> X;
for (int i = 1; i < sqrt(N) + 1; i++) {
if (N % i == 0) {
X.first = i;
X.second = N / i;
A.push_back(X);
}
}
return A;
}
int main() {
llong N;
cin >> N;
llong ans = 10000000000000;
vector<pair<llong, llong>> divisor_list;
divisor_list = Enumdivisor(N);
for (auto x : divisor_list) {
ans = min(ans, x.first - 2 + x.second);
}
cout << ans << endl;
return 0;
}
| replace | 63 | 64 | 63 | 64 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
vector<long long> division(long long n) {
vector<long long> v;
for (long long d = 1; d <= n / 2; d += 1)
if (n % d == 0)
v.push_back(d);
v.push_back(n);
return v;
}
int main() {
long long n;
cin >> n;
vector<long long> v = division(n);
vector<long long> m;
if (v.size() == 2) {
cout << n - 1;
return 0;
}
for (long long i = 0; i < v.size(); i++)
m.push_back(n / v[i] + v[i] - 2);
cout << *min_element(m.begin(), m.end());
return 0;
} | #include <bits/stdc++.h>
using namespace std;
vector<long long> division(long long n) {
vector<long long> v;
for (long long d = 1; d * d <= n; d += 1)
if (n % d == 0)
v.push_back(d);
v.push_back(n);
return v;
}
int main() {
long long n;
cin >> n;
vector<long long> v = division(n);
vector<long long> m;
if (v.size() == 2) {
cout << n - 1;
return 0;
}
for (long long i = 0; i < v.size(); i++)
m.push_back(n / v[i] + v[i] - 2);
cout << *min_element(m.begin(), m.end());
return 0;
} | replace | 6 | 7 | 6 | 7 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
using namespace std;
int main() {
long long n;
cin >> n;
long long m = sqrt(n);
long long i;
for (i = m; i <= n; i++) {
if (n % i == 0)
break;
}
cout << i + n / i - 2;
} | #include <cmath>
#include <iostream>
using namespace std;
int main() {
long long n;
cin >> n;
long long m = sqrt(n);
long long i;
for (i = m; i >= 1; i--) {
if (n % i == 0)
break;
}
cout << i + n / i - 2;
} | replace | 10 | 11 | 10 | 11 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long int n;
cin >> n;
long int ans = n;
for (int i = 1; i * i <= n; i++) {
if (n % i != 0)
continue;
long int a = (i - 1) + (n / i - 1);
ans = min(ans, a);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long int n;
cin >> n;
long int ans = n;
for (long int i = 1; i * i <= n; i++) {
if (n % i != 0)
continue;
long int a = (i - 1) + (n / i - 1);
ans = min(ans, a);
}
cout << ans << endl;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n;
cin >> n;
int64_t j = 1;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
j = i;
}
}
cout << j + (n / j) - 2 << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n;
cin >> n;
int64_t j = 1;
int z = sqrt(n);
for (int i = 1; i < z + 1; i++) {
if (n % i == 0) {
j = i;
}
}
cout << j + (n / j) - 2 << endl;
} | replace | 7 | 8 | 7 | 9 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <iostream>
#include <stdint.h>
#include <utility>
#include <vector>
using namespace std;
int main() {
int64_t A;
cin >> A;
vector<pair<int64_t, int64_t>> v;
for (int64_t i = 1; i <= A / 2; i++) {
if (A % i == 0) {
v.push_back(pair<int64_t, int64_t>(i, A / i));
}
}
int64_t min = 1e12;
int64_t tmp = 0;
for (int64_t j = 0; j < v.size(); j++) {
tmp = v[j].first + v[j].second - 2;
if (tmp < min)
min = tmp;
}
cout << min << endl;
return 0;
} | #include <iostream>
#include <stdint.h>
#include <utility>
#include <vector>
using namespace std;
int main() {
int64_t A;
cin >> A;
vector<pair<int64_t, int64_t>> v;
for (int64_t i = 1; i * i <= A; i++) {
if (A % i == 0) {
v.push_back(pair<int64_t, int64_t>(i, A / i));
}
}
int64_t min = 1e12;
int64_t tmp = 0;
for (int64_t j = 0; j < v.size(); j++) {
tmp = v[j].first + v[j].second - 2;
if (tmp < min)
min = tmp;
}
cout << min << endl;
return 0;
} | replace | 10 | 11 | 10 | 11 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()
const long long INF = 1e15;
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll ans = INF;
for (int i; i * i <= n; i++) {
if (n % i == 0) {
ll j = n / i;
ans = min(ans, i + j - 2);
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()
const long long INF = 1e15;
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll ans = INF;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ll j = n / i;
ans = min(ans, i + j - 2);
}
}
cout << ans << endl;
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
long long n, ans;
int main() {
cin >> n;
for (int i = 1; i * i <= n; i++)
if (n % i == 0)
ans = i + n / i;
cout << ans - 2;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
long long n, ans;
int main() {
cin >> n;
for (int i = 1; 1ll * i * i <= n; i++)
if (n % i == 0)
ans = i + n / i;
cout << ans - 2;
return 0;
} | replace | 5 | 6 | 5 | 6 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define prtd(var, i) cout << fixed << setprecision(i) << var << endl;
#define ll long long
#define P pair<int, int>
using namespace std;
int main() {
ll n;
cin >> n;
ll ans = 1e15;
for (int i = 1; i * i <= n; i++) {
long long int d = n / i;
if (d * i == n) {
ans = min(ans, (i - 1) + (ll)(d - 1));
// cout << ans << " " << i << " " << d << endl;
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define prtd(var, i) cout << fixed << setprecision(i) << var << endl;
#define ll long long
#define P pair<int, int>
using namespace std;
int main() {
ll n;
cin >> n;
ll ans = 1e15;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, (i - 1) + (ll)(n / i - 1));
// cout << ans << " " << i << " " << d << endl;
}
}
cout << ans << endl;
}
| replace | 11 | 15 | 11 | 14 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
int main() {
vector<ll> mass_i;
vector<ll> mass_j;
ll N, best_score;
cin >> N;
for (ll i = 1; i <= N; i++) {
if (N % i == 0) {
mass_i.push_back(i);
mass_j.push_back(N / i);
}
}
for (ll i = 0; i < mass_i.size(); i++) {
if (best_score > mass_i[i] + mass_j[i] - 2 || best_score == 0) {
best_score = mass_i[i] + mass_j[i] - 2;
}
}
cout << best_score;
} | #include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
int main() {
vector<ll> mass_i;
vector<ll> mass_j;
ll N, best_score;
cin >> N;
for (ll i = 1; i <= N; i++) {
if (i > N / i)
break;
if (N % i == 0) {
mass_i.push_back(i);
mass_j.push_back(N / i);
}
}
for (ll i = 0; i < mass_i.size(); i++) {
if (best_score > mass_i[i] + mass_j[i] - 2 || best_score == 0) {
best_score = mass_i[i] + mass_j[i] - 2;
}
}
cout << best_score;
} | insert | 9 | 9 | 9 | 11 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(a, b) for (int a = 0; a < (b); ++a)
#define REP1(i, n) for (int i = 1; i <= (n); ++i)
#define debug(x) cerr << #x << ": " << x << '\n'
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define INF (1 << 29)
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
template <class T = int> T in() {
T x;
cin >> x;
return (x);
}
template <class T> void print(T &x) { cout << x << '\n'; }
const int MOD = (int)1e9 + 7;
const int MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];
void COMint() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
if (a > b) {
swap(a, b);
}
return gcd(a, b % a);
}
ll lcm(ll a, ll b) {
ll g;
g = gcd(a, b);
return a * b / g;
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
if (a.second != b.second) {
return a.second < b.second;
} else {
return a.first < b.first;
}
}
bool compare_by_a(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) {
return a.first < b.first;
} else {
return a.second < b.second;
}
}
vector<pair<int, ll>> vec[100000];
ll cost[100000];
void dfs(int now, ll COST) {
cost[now] = COST;
int next;
REP(i, vec[now].size()) {
next = vec[now][i].first;
if (cost[next] < 0) {
dfs(next, COST + vec[now][i].second);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll N = in<ll>();
ll n;
n = 0;
n = sqrt(N);
for (ll i = n; i--; i > 0) {
if (N % i == 0) {
ll ans;
ans = (i - 1) + (N / i - 1);
cout << ans << endl;
return 0;
}
}
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(a, b) for (int a = 0; a < (b); ++a)
#define REP1(i, n) for (int i = 1; i <= (n); ++i)
#define debug(x) cerr << #x << ": " << x << '\n'
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define INF (1 << 29)
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
template <class T = int> T in() {
T x;
cin >> x;
return (x);
}
template <class T> void print(T &x) { cout << x << '\n'; }
const int MOD = (int)1e9 + 7;
const int MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];
void COMint() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
if (a > b) {
swap(a, b);
}
return gcd(a, b % a);
}
ll lcm(ll a, ll b) {
ll g;
g = gcd(a, b);
return a * b / g;
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
if (a.second != b.second) {
return a.second < b.second;
} else {
return a.first < b.first;
}
}
bool compare_by_a(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) {
return a.first < b.first;
} else {
return a.second < b.second;
}
}
vector<pair<int, ll>> vec[100000];
ll cost[100000];
void dfs(int now, ll COST) {
cost[now] = COST;
int next;
REP(i, vec[now].size()) {
next = vec[now][i].first;
if (cost[next] < 0) {
dfs(next, COST + vec[now][i].second);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll N = in<ll>();
ll n;
n = 0;
n = sqrt(N);
for (ll i = n + 1; i--; i > 0) {
if (N % i == 0) {
ll ans;
ans = (i - 1) + (N / i - 1);
cout << ans << endl;
return 0;
}
}
return 0;
}
| replace | 97 | 98 | 97 | 98 | 0 | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t N;
cin >> N;
int j = 1;
for (int i = 1; i * i <= N; i++)
if (N % i == 0)
j = i;
cout << j + N / j - 2 << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t N;
cin >> N;
int j = 1;
for (int i = 1; i <= N / i; i++)
if (N % i == 0)
j = i;
cout << j + N / j - 2 << endl;
} | replace | 7 | 8 | 7 | 8 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pair;
const int inf = 1000000000; // 10^9
const ll inff = 1000000000000000000; // 10^18
int main() {
ll n;
cin >> n;
ll ans = 1e18;
for (ll i = 0; i * i <= n; i++) {
if (n % i != 0)
continue;
ll j = n / i;
ans = min(ans, i + j - 2);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pair;
const int inf = 1000000000; // 10^9
const ll inff = 1000000000000000000; // 10^18
int main() {
ll n;
cin >> n;
ll ans = 1e18;
for (ll i = 1; i * i <= n; i++) {
if (n % i != 0)
continue;
ll j = n / i;
ans = min(ans, i + j - 2);
}
cout << ans << endl;
return 0;
} | replace | 14 | 15 | 14 | 15 | -8 | |
p02881 | C++ | Time Limit Exceeded |
#include <bits/stdc++.h>
#include <math.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int mINF = -1001001001;
// << fixed << setprecision(10) <<
int main() {
ll n;
cin >> n;
ll mi = INF;
for (ll i = 1; i <= n; i++) {
if (n % i == 0) {
mi = min(mi, (n / i) + i);
}
}
cout << mi - 2 << endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <math.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int mINF = -1001001001;
// << fixed << setprecision(10) <<
int main() {
ll n;
cin >> n;
ll mi = 9223372036854775807;
for (ll i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
mi = min(mi, (n / i) + i);
}
}
cout << mi - 2 << endl;
return 0;
}
| replace | 15 | 17 | 15 | 17 | TLE | |
p02881 | C++ | Time Limit Exceeded |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ll n;
cin >> n;
ll ans = 1ll * 10000 * 10000 * 10000;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, n / i + i - 2);
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ll n;
cin >> n;
ll ans = 1ll * 10000 * 10000 * 10000;
for (int i = 1; 1ll * i * i <= n; i++) {
if (n % i == 0) {
ans = min(ans, n / i + i - 2);
}
}
cout << ans << endl;
return 0;
}
| replace | 8 | 9 | 8 | 9 | TLE | |
p02881 | C++ | Time Limit Exceeded | /*
Zeolim - An AC a day keeps the bug away
*/
// #pragma GCC optimize(2)
// #pragma GCC ("-W1,--stack=128000000")
#include <bits/stdc++.h>
using namespace std;
#define mp(x, y) make_pair(x, y)
#define fr(x, y, z) for (int x = y; x < z; ++x)
#define pb(x) push_back(x)
#define mem(x, y) memset(x, y, sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
void re(ll &x) {
x = 0;
char s = getchar();
while (s < '0' || s > '9')
s = getchar();
while (s >= '0' && s <= '9') {
x = x * 10 + s - '0';
s = getchar();
}
}
void wr(ll x) {
if (x > 9)
wr(x / 10);
putchar(x % 10 + '0');
}
const ld PI = acos(-1.0);
const ld E = exp(1.0);
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 386910137;
const ull P = 13331;
const int MAXN = 1e6 + 100;
int main() {
// ios::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
// freopen("d:\out.txt","w",stdout);
// freopen("d:\in.txt","r",stdin);
ll n, m;
cin >> n;
ll ans = INF;
for (int i = 1; i * i <= n; ++i) {
ll ry = n / i;
if (ry * i == n) {
ans = min((i - 1 + ry - 1), ans);
}
}
cout << ans << '\n';
return 0;
} | /*
Zeolim - An AC a day keeps the bug away
*/
// #pragma GCC optimize(2)
// #pragma GCC ("-W1,--stack=128000000")
#include <bits/stdc++.h>
using namespace std;
#define mp(x, y) make_pair(x, y)
#define fr(x, y, z) for (int x = y; x < z; ++x)
#define pb(x) push_back(x)
#define mem(x, y) memset(x, y, sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
void re(ll &x) {
x = 0;
char s = getchar();
while (s < '0' || s > '9')
s = getchar();
while (s >= '0' && s <= '9') {
x = x * 10 + s - '0';
s = getchar();
}
}
void wr(ll x) {
if (x > 9)
wr(x / 10);
putchar(x % 10 + '0');
}
const ld PI = acos(-1.0);
const ld E = exp(1.0);
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 386910137;
const ull P = 13331;
const int MAXN = 1e6 + 100;
int main() {
// ios::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
// freopen("d:\out.txt","w",stdout);
// freopen("d:\in.txt","r",stdin);
ll n, m;
cin >> n;
ll ans = INF;
for (ll i = 1; i * i <= n; ++i) {
ll ry = n / i;
if (ry * i == n) {
ans = min((i - 1 + ry - 1), ans);
}
}
cout << ans << '\n';
return 0;
} | replace | 51 | 52 | 51 | 52 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main() {
ll n;
cin >> n;
ll ans = 1e18;
for (int i = 1; i * i <= n; i++) {
if (n % i != 0)
continue;
ll j = n / i;
ans = min(ans, i + j - 2);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main() {
ll n;
cin >> n;
ll ans = 1e18;
for (ll i = 1; i * i <= n; i++) {
if (n % i != 0)
continue;
ll j = n / i;
ans = min(ans, i + j - 2);
}
cout << ans << endl;
return 0;
} | replace | 10 | 11 | 10 | 11 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define f(i, x, n) for (ll int i = x; i < n; i++)
#define re(j, x, n) for (ll int j = x; j >= n; j--)
#define f1(k, x, n) for (ll int k = x; k <= n; k++)
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define MAX 1000001
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll int n;
cin >> n;
ll int mn = n - 1, f = 0;
f1(i, 2, sqrt(n)) {
if (n % i == 0) {
f = 1;
break;
}
}
if (f == 0) {
cout << n - 1;
return 0;
}
f1(i, 2, n / 2) {
if (n % i == 0) {
if (mn < (i - 1) + ((n / i) - 1))
break;
mn = min(mn, (i - 1) + ((n / i) - 1));
}
}
cout << mn;
} | #include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define f(i, x, n) for (ll int i = x; i < n; i++)
#define re(j, x, n) for (ll int j = x; j >= n; j--)
#define f1(k, x, n) for (ll int k = x; k <= n; k++)
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define MAX 1000001
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll int n;
cin >> n;
ll int mn = n - 1, f = 0;
f1(i, 2, sqrt(n)) {
if (n % i == 0) {
f = 1;
break;
}
}
if (f == 0) {
cout << n - 1;
return 0;
}
f1(i, 2, sqrt(n)) {
if (n % i == 0) {
if (mn < (i - 1) + ((n / i) - 1))
break;
mn = min(mn, (i - 1) + ((n / i) - 1));
}
}
cout << mn;
} | replace | 27 | 28 | 27 | 28 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1LL << 60;
int main() {
ll N;
cin >> N;
ll x = 1, y;
for (int i = 1; i * i <= N; i++) {
if (N % i == 0) {
x = i;
y = N / i;
}
}
cout << (x - 1) + (y - 1) << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1LL << 60;
int main() {
ll N;
cin >> N;
ll x = 1, y;
for (ll i = 1; i * i <= N; i++) {
if (N % i == 0) {
x = i;
y = N / i;
}
}
cout << (x - 1) + (y - 1) << endl;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, i, min = 10000000000000000;
scanf("%lld", &n);
for (i = 1; i <= n / 2; ++i) {
if (n % i == 0) {
if (min > (i - 1 + n / i - 1))
min = (i - 1 + n / i - 1);
}
}
printf("%ld", min);
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, i, min = 10000000000000000;
scanf("%lld", &n);
for (i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
if (min > (i - 1 + n / i - 1))
min = (i - 1 + n / i - 1);
}
}
printf("%ld", min);
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t N, a = 10000000000000;
cin >> N;
for (int i = 1; i * i < N; i++) {
if (N % i == 0) {
a = min(a, N / i + i);
}
}
cout << a - 2 << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t N, a = 10000000000000;
cin >> N;
for (int64_t i = 1; i * i <= N; i++) {
if (N % i == 0) {
a = min(a, N / i + i);
}
}
cout << a - 2 << endl;
}
| replace | 6 | 7 | 6 | 7 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long N;
cin >> N;
long long minimum = N - 1;
for (long long i = 1; i < N / 2; i++) {
if (N % i == 0) {
minimum = min(minimum, N / i + i - 2);
}
}
cout << minimum << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long N;
cin >> N;
long long minimum = N - 1;
for (long long i = 1; i <= sqrt(N); i++) {
if (N % i == 0) {
minimum = min(minimum, N / i + i - 2);
}
}
cout << minimum << endl;
}
| replace | 6 | 7 | 6 | 7 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
using namespace std;
int main() {
long long N;
cin >> N;
vector<long long> factor(1000000, 1000000000003);
for (long long i = 1ll; i * i <= N; ++i) {
if (N % i == 0) {
factor.at(i) = i + N / i;
}
}
sort(factor.begin(), factor.end());
cout << factor.at(0) - 2 << endl;
}
| #include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
using namespace std;
int main() {
long long N;
cin >> N;
vector<long long> factor(1000001, 1000000000003);
for (long long i = 1ll; i * i <= N; ++i) {
if (N % i == 0) {
factor.at(i) = i + N / i;
}
}
sort(factor.begin(), factor.end());
cout << factor.at(0) - 2 << endl;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p02881 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
unsigned long long n, minimum;
cin >> n;
minimum = n - 1;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
// if (((n / i) - 1) + (i - 1) < minimum)
// minimum = ((n / i) - 1) + (i - 1);
minimum = min(((n / i) - 1) + (i - 1), minimum);
}
}
cout << minimum;
return 0;
} | #include <iostream>
using namespace std;
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
unsigned long long n, minimum;
cin >> n;
minimum = n - 1;
for (unsigned long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
// if (((n / i) - 1) + (i - 1) < minimum)
// minimum = ((n / i) - 1) + (i - 1);
minimum = min(((n / i) - 1) + (i - 1), minimum);
}
}
cout << minimum;
return 0;
} | replace | 11 | 12 | 11 | 12 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n + 1); ++i)
using namespace std;
using ll = long long;
const int INF = -100100100;
typedef pair<int, int> P;
int main() {
ll n;
cin >> n;
double ans = -1 * INF;
for (double i = 1; i <= n / 2; ++i) {
double j = n / i;
if (j != int(n / i))
continue;
ans = min(ans, i + j - 2);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n + 1); ++i)
using namespace std;
using ll = long long;
const int INF = -100100100;
typedef pair<int, int> P;
int main() {
ll n;
cin >> n;
ll ans = 1e18;
for (ll i = 1; i <= pow(n, 0.5); ++i) {
ll j = n / i;
if (n % i != 0)
continue;
ans = min(ans, i + j - 2);
}
cout << ans << endl;
return 0;
} | replace | 11 | 15 | 11 | 15 | TLE | |
p02881 | C++ | Time Limit Exceeded | #pragma GCC optimize( \
"O3,Ofast,inline,fast-math,unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
#define int int64_t
#define double long double
#define INIT \
std::ios_base::sync_with_stdio(0); \
std::cin.tie(0); \
std::cout << std::fixed << std::setprecision(15);
#define WRITE_FUNCTION_UNDER_HERE DEFALLTYPE(VDEF) DEFALLTYPE(PDEF3)
const int MOD = 1e9 + 7;
// using namespace std;const int MAX=std::numeric_limits<int>::max();const int
// MIN=std::numeric_limits<int>::min();const double EPS=1e-7;
#define fou(i, a, n) for (int i = a; i < n; i++)
#define fod(i, a, n) for (int i = n - 1; i >= a; i--)
#define tra(e, v) for (auto &e : v)
#define elif(c) else if (c)
#define fi first
#define se second
#define fir fi
#define sec se.fi
#define thi se.se
#define firs fi
#define seco sec
#define thir thi.fi
#define four thi.se
#define DEFALLTYPE(XDEF) \
XDEF(int, I) \
XDEF(double, D) XDEF(char, C) XDEF(String, S) XDEF(bool, B) XDEF(Modulo, M)
#define VDEF(t, T) \
typedef Vector<t> V##T; \
typedef Vector<V##T> VV##T; \
typedef Vector<VV##T> VVV##T;
#define PDEF(x, X, y, Y, z, Z, w, W) \
typedef std::pair<x, y> P##X##Y; \
VDEF(P##X##Y, P##X##Y) typedef std::pair<x, std::pair<y, z>> P##X##Y##Z; \
VDEF(P##X##Y##Z, P##X##Y##Z) \
typedef std::pair<x, std::pair<y, std::pair<z, w>>> P##X##Y##Z##W; \
VDEF(P##X##Y##Z##W, P##X##Y##Z##W)
#define PDEF1(y, Y, z, Z, w, W) \
PDEF(int, I, y, Y, z, Z, w, W) \
PDEF(double, D, y, Y, z, Z, w, W) PDEF(char, C, y, Y, z, Z, w, W) \
PDEF(String, S, y, Y, z, Z, w, W) PDEF(bool, B, y, Y, z, Z, w, W) \
PDEF(Modulo, M, y, Y, z, Z, w, W)
#define PDEF2(z, Z, w, W) \
PDEF1(int, I, z, Z, w, W) \
PDEF1(double, D, z, Z, w, W) PDEF1(char, C, z, Z, w, W) \
PDEF1(String, S, z, Z, w, W) PDEF1(bool, B, z, Z, w, W) \
PDEF1(Modulo, M, z, Z, w, W)
#define PDEF3(w, W) \
PDEF2(int, I, w, W) \
PDEF2(double, D, w, W) PDEF2(char, C, w, W) PDEF2(String, S, w, W) \
PDEF2(bool, B, w, W) PDEF2(Modulo, M, w, W)
// in, out
#define in(T, ...) \
T __VA_ARGS__; \
_in(__VA_ARGS__)
void _in() {}
template <typename Hd, typename... Tl> void _in(Hd &hd, Tl &&...tl) {
std::cin >> hd;
_in(std::forward<Tl>(tl)...);
}
void out() { std::cout << '\n'; }
template <typename T> void out(const T &a) { std::cout << a << '\n'; }
template <typename Hd, typename... Tl> void out(const Hd &hd, const Tl &...tl) {
std::cout << hd << ' ';
out(tl...);
}
// std::pair io,calc
#define PairCalc(OP) \
template <typename T, typename U, typename V, typename W> \
std::pair<T, U> operator OP(const std::pair<T, U> &l, \
const std::pair<V, W> &r) { \
return std::make_pair(l.first OP r.first, l.second OP r.second); \
} \
template <typename T, typename U, typename V, typename W> \
std::pair<T, U> operator OP##=(std::pair<T, U> &l, \
const std::pair<V, W> &r) { \
return l = l OP r; \
}
template <typename T, typename U>
std::istream &operator>>(std::istream &i, std::pair<T, U> &p) {
i >> p.first >> p.second;
return i;
}
template <typename T, typename U>
std::ostream &operator<<(std::ostream &o, const std::pair<T, U> &p) {
return o << p.first << ' ' << p.second;
}
PairCalc(+) PairCalc(-) PairCalc(*) PairCalc(/) PairCalc(%) PairCalc(<<)
PairCalc(>>) PairCalc(&) PairCalc(|)
PairCalc(^) template <typename T, typename U>
std::pair<T, U> mp(const T &t, const U &u) {
return std::make_pair(t, u);
}
template <typename T, typename U, typename V>
std::pair<T, std::pair<U, V>> mp(const T &t, const U &u, const V &v) {
return std::make_pair(t, std::make_pair(u, v));
}
template <typename T, typename U, typename V, typename W>
std::pair<T, std::pair<U, std::pair<V, W>>> mp(const T &t, const U &u,
const V &v, const W &w) {
return std::make_pair(t, std::make_pair(u, std::make_pair(v, w)));
}
// Vector class
#define VecCalc(OP) \
VT operator OP(const VT &r) const { \
VT v(std::min(size(), r.size())); \
for (int i = 0; i < v.size(); i++) \
v[i] = (*this)[i] OP r[i]; \
return v; \
} \
VT operator OP(const T &r) const { \
VT v(size()); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
} \
VT operator OP##=(const VT &r) { return *this = *this OP r; } \
VT operator OP##=(const T &r) { return *this = *this OP r; }
#define VecComp(OP) \
Vector<int> operator OP(const VT &r) const { \
Vector<int> v(std::min(size(), r.size())); \
for (int i = 0; i < v.size(); i++) \
v[i] = (*this)[i] OP r[i]; \
return v; \
} \
Vector<int> operator OP(const T &r) const { \
Vector<int> v(size()); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
}
#define VVCalc(OP) \
template <typename T, typename U> \
Vector<Vector<T>> operator OP(Vector<Vector<T>> l, const U &r) { \
for (Vector<T> & e : l) \
e OP## = r; \
return l; \
} \
template <typename T, typename U> \
Vector<Vector<T>> operator OP##=(Vector<Vector<T>> &l, const U &r) { \
return l = l OP r; \
}
#define VVComp(OP) \
template <typename T, typename U> \
Vector<Vector<int>> operator OP(const Vector<Vector<T>> &l, const U &r) { \
Vector<Vector<int>> v(l.size()); \
for (int i = 0; i < l.size(); i++) \
v[i] = l[i] OP r; \
return v; \
}
template <typename T> class Vector : public std::vector<T> {
public:
typedef std::vector<T> SVT;
typedef Vector<T> VT;
using SVT::operator[];
using SVT::operator=;
using SVT::assign;
using SVT::begin;
using SVT::end;
using SVT::pop_back;
using SVT::push_back;
using SVT::rbegin;
using SVT::rend;
using SVT::resize;
Vector(const SVT &v) : SVT(v) {}
Vector(int s = 0, T v = T()) {
if (s >= 0)
assign(s, v);
else {
assign(-s, v);
for (T &e : *this)
std::cin >> e;
}
}
Vector(const std::initializer_list<T> &l) : SVT(l) {}
template <typename U> Vector(const std::vector<U> &v) {
for (const T &e : v)
pub(e);
}
template <typename U>
Vector(const __gnu_pbds::tree<
U, __gnu_pbds::null_type, std::less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &t) {
for (const T &e : t)
pub(e);
}
template <typename U> Vector(const __gnu_cxx::rope<U> &r) {
for (const T &e : r)
pub(e);
}
Vector(const std::string &s) {
for (const T &e : s)
pub(e);
}
int size() const { return SVT::size(); }
VT operator[](const std::initializer_list<int> &l) const {
if (l.size() == 2 || l.size() == 3) {
int first = *l.begin(), last = *(l.begin() + 1),
step = (l.size() == 2 ? 1 : *(l.begin() + 2));
last = (last < 0 ? last + size() + 1 : last);
VT v;
for (int i = first; i < last; i += step)
v.pub((*this)[i]);
return v;
}
return *this;
}
VT operator+() const { return *this; }
VT operator-() const {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = -(*this)[i];
return v;
}
VT operator++() {
for (T &e : *this)
e++;
return *this;
}
VT operator--() {
for (T &e : *this)
e--;
return *this;
}
VT operator++(int32_t) {
VT v = *this;
for (T &e : *this)
e++;
return v;
}
VT operator--(int32_t) {
VT v = *this;
for (T &e : *this)
e--;
return v;
}
void out() {
for (const T &e : *this)
std::cout << e << '\n';
}
void outstr() {
for (int i = 0; i < size() - 1; i++)
std::cout << (*this)[i];
std::cout << last() << '\n';
}
void iota() {
for (int i = 0; i < size(); i++)
(*this)[i] = i;
}
T last() const { return (*this)[size() - 1]; }
T &last() { return (*this)[size() - 1]; }
T sum(int l = 0, int r = -1) const {
T v = T();
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v += (*this)[i];
return v;
}
T prod(int l = 0, int r = -1) const {
T v = T() + 1;
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v *= (*this)[i];
return v;
}
T xorsum(int l = 0, int r = -1) const {
T v = T();
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v ^= (*this)[i];
return v;
}
int argmin() const { return std::min_element(begin(), end()) - begin(); }
int argmax() const { return std::max_element(begin(), end()) - begin(); }
T min() const { return (*this)[argmin()]; }
T max() const { return (*this)[argmax()]; }
T dot(const VT &v) const {
T r = T();
for (int i = 0; i < std::min(size(), v.size()); i++)
r += (*this)[i] * v[i];
return r;
}
void pub(const T &v) { SVT::push_back(v); }
void pub(const VT &v) { SVT::insert(end(), v.begin(), v.end()); }
void insert(int i, const T &v) { SVT::insert(begin() + i, v); }
void insert(int i, const VT &v) {
SVT::insert(begin() + i, v.begin(), v.end());
}
void pob() { pop_back(); }
void erase(int i) { SVT::erase(begin() + i); }
void erase(int i, int j) { SVT::erase(begin() + i, begin() + j); }
void erase_unique() { SVT::erase(std::unique(begin(), end()), end()); }
void reverse() { std::reverse(begin(), end()); }
void rotatedown(int i) {
if (i > 0)
std::rotate(begin(), begin() + i, end());
}
void rotateup(int i) {
if (i > 0)
std::rotate(rbegin(), rbegin() + i, rend());
}
void sortup() { std::sort(begin(), end()); }
void sortdown() { std::sort(rbegin(), rend()); }
void cumsum() {
for (int i = 0; i < size() - 1; i++)
(*this)[i + 1] += (*this)[i];
}
void decumsum() {
for (int i = size() - 2; i >= 0; i--)
(*this)[i + 1] -= (*this)[i];
}
T cumsumpart(int l, int r) const {
return (*this)[r - 1] - (l = 0 ? 0 : (*this)[l - 1]);
}
void cumxor() {
for (int i = 0; i < size() - 1; i++)
(*this)[i + 1] ^= (*this)[i];
}
void decumxor() {
for (int i = size() - 2; i >= 0; i--)
(*this)[i + 1] ^= (*this)[i];
}
T cumxorpart(int l, int r) const {
return (*this)[r - 1] ^ (l = 0 ? 0 : (*this)[l - 1]);
}
void bucketsortup() {
int m = min();
Vector<int> b(max() - m + 1);
for (int e : (*this))
b[e - m]++;
b.cumsum();
for (int i = 0; i < b.size(); i++)
for (int j = (i == 0 ? 0 : b[i - 1]); j < b[i]; j++)
(*this)[j] = m + i;
}
void bucketsortdown() {
bucketsortup();
reverse();
}
void compress() {
VT u(*this);
u.sortup();
u.erase_unique();
std::unordered_map<T, int> m;
for (int i = 0; i < u.size(); i++)
m[u[i]] = i;
for (int i = 0; i < size(); i++)
(*this)[i] = m[(*this)[i]];
}
int findupgeq(const T &v) {
return std::lower_bound(begin(), end(), v) - begin();
}
int findupleq(const T &v) {
return std::upper_bound(begin(), end(), v) - begin() - 1;
}
int upcount(const T &v) { return findupleq(v) - findupgeq(v) + 1; }
int finddowngeq(const T &v) {
return size() - (std::lower_bound(rbegin(), rend(), v) - rbegin()) - 1;
}
int finddownleq(const T &v) {
return size() - (std::upper_bound(rbegin(), rend(), v) - rbegin());
}
int downcount(const T &v) { return finddowngeq(v) - finddownleq(v) + 1; }
VecCalc(+) VecCalc(-) VecCalc(*) VecCalc(/) VecCalc(%) VecCalc(<<) VecCalc(>>)
VecCalc(&) VecCalc(|) VecCalc(^) VecComp(==) VecComp(!=) VecComp(>=)
VecComp(<=) VecComp(>) VecComp(<) VT iotaed() const {
VT r(*this);
r.iota();
return r;
}
VT pubed(const T &v) const {
VT r(*this);
r.pub(v);
return r;
}
VT pubed(const VT &v) const {
VT r(*this);
r.pub(v);
return r;
}
VT inserted(int i, const T &v) const {
VT r(*this);
r.insert(i, v);
return r;
}
VT inserted(int i, const VT &v) const {
VT r(*this);
r.insert(i, v);
return r;
}
VT pobed() const {
VT r(*this);
r.pob();
return r;
}
VT erased(int i, int j = -1) const {
VT r(*this);
r.erase(i, j);
return r;
}
VT erase_uniqued() const {
VT r(*this);
r.erase_unique();
return r;
}
VT reversed() const {
VT r(*this);
r.reverse();
return r;
}
VT rotatedowned(int i) const {
VT r(*this);
r.rotatedown(i);
return r;
}
VT rotateuped(int i) const {
VT r(*this);
r.rotateup(i);
return r;
}
VT sortuped() const {
VT r(*this);
r.sortup();
return r;
}
VT sortdowned() const {
VT r(*this);
r.sortdown();
return r;
}
VT cumsumed() const {
VT r(*this);
r.cumsum();
return r;
}
VT decumsumed() const {
VT r(*this);
r.decumsum();
return r;
}
VT cumxored() const {
VT r(*this);
r.cumxor();
return r;
}
VT decumxored() const {
VT r(*this);
r.decumxor();
return r;
}
VT bucketsortuped() const {
VT r(*this);
r.bucketsortup();
return r;
}
VT bucketsortdowned() const {
VT r(*this);
r.bucketsortdown();
return r;
}
VT compressed() const {
VT r(*this);
r.compress();
return r;
}
};
VVCalc(+) VVCalc(-) VVCalc(*) VVCalc(/) VVCalc(%) VVCalc(<<) VVCalc(>>)
VVCalc(&) VVCalc(|) VVCalc(^) VVComp(==) VVComp(!=) VVComp(>=) VVComp(<=)
VVComp(>) VVComp(<) template <typename T, typename U>
Vector<std::pair<T, U>> unite(const Vector<T> &v, const Vector<U> &u) {
int s = std::min(v.size(), u.size());
Vector<std::pair<T, U>> r(s);
for (int i = 0; i < s; i++)
r[i] = std::make_pair(v[i], u[i]);
return r;
}
template <typename T, typename U>
std::pair<Vector<T>, Vector<U>> separate(const Vector<std::pair<T, U>> &p) {
Vector<T> v(p.size());
Vector<U> u(p.size());
for (int i = 0; i < p.size(); i++) {
v[i] = p[i].first;
u[i] = p[i].second;
}
return std::make_pair(v, u);
}
template <typename T>
std::istream &operator>>(std::istream &i, std::vector<T> &v) {
for (T &e : v)
i >> e;
return i;
}
template <typename T>
std::ostream &operator<<(std::ostream &o, const std::vector<T> &v) {
if (v.size() == 0)
return o;
for (int i = 0; i < v.size() - 1; i++)
o << v[i] << ' ';
return o << v[v.size() - 1];
}
Vector<int> baseVector(int n, int b) {
Vector<int> v(64);
for (int &e : v) {
e = n % b;
n /= b;
}
return v;
}
int baseVectorValue(Vector<int> v, int b) {
int n = 0, m = 1;
for (int &e : v) {
n += e * m;
m *= b;
}
return n;
}
// Set class
template <typename T>
class Set
: public __gnu_pbds::tree<T, __gnu_pbds::null_type, std::less<T>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> {
public:
typedef __gnu_pbds::tree<T, __gnu_pbds::null_type, std::less<T>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>
tree;
using tree::operator=;
using tree::begin;
using tree::end;
using tree::erase;
using tree::insert;
using tree::split;
Set(const tree &t) : tree(t) {}
Set(int n = 0) {
for (int i = 0; i < -n; i++) {
T v;
std::cin >> v;
insert(v);
}
}
Set(const std::initializer_list<T> &l) {
for (const T &e : l)
insert(e);
}
int size() const { return tree::size(); }
template <typename U> Set(const std::vector<U> &v) {
for (const T &e : v)
insert(e);
}
template <typename U>
Set(const __gnu_pbds::tree<
U, __gnu_pbds::null_type, std::less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &t) {
for (const T &e : t)
insert(e);
}
template <typename U> Set(const __gnu_cxx::rope<U> &r) {
for (const T &e : r)
insert(e);
}
Set(const std::string &s) {
for (const T &e : s)
insert(e);
}
bool contains(const T &v) const { return tree::find(v) != end(); }
void merge(const tree &s) {
tree t = s;
tree::join(t);
}
int order(const T &v) { return tree::order_of_key(v); }
int rorder(const T &v) { return size() - tree::order_of_key(v) - 1; }
T operator[](int i) const { return *(tree::find_by_order(i)); }
std::pair<Set<T>, Set<T>> splitted(const T &v) const {
Set<T> s = *this, t;
s.split(v, t);
return std::make_pair(s, t);
}
Set<T> &operator<<(const T &v) {
insert(v);
return *this;
}
Set<T> &operator<<(const tree &s) {
merge(s);
return *this;
}
Set<T> &operator>>(const T &v) {
erase(v);
return *this;
}
bool operator>(const T &v) { return contains(v); }
};
template <typename T>
std::ostream &operator<<(std::ostream &o, const Set<T> &s) {
if (s.size() == 0)
return o;
for (int i = 0; i < s.size() - 1; i++)
o << s[i] << ' ';
return o << s[s.size() - 1];
}
// Map class
template <typename T, typename U>
class Map
: public __gnu_pbds::tree<T, U, std::less<T>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> {
public:
typedef __gnu_pbds::tree<T, U, std::less<T>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>
tree;
using tree::operator=;
using tree::operator[];
using tree::begin;
using tree::end;
using tree::erase;
using tree::insert;
using tree::split;
Map(const tree &t) : tree(t) {}
Map(int n = 0) {
for (int i = 0; i < -n; i++) {
T k;
U v;
std::cin >> k >> v;
insert(k, v);
}
}
Map(std::initializer_list<std::pair<T, U>> l) {
for (const std::pair<T, U> &e : l)
insert(e);
}
int size() const { return tree::size(); }
std::pair<T, U> key_value(int i) const { return *tree::find_by_order(i); }
void insert(const T &k, const U &v) { tree::insert(std::make_pair(k, v)); }
bool contains(const T &k) const { return tree::find(k) != end(); }
void merge(const tree &t) {
Map<T, U> n = t;
tree::join(n);
}
int order(const T &k) { return tree::order_of_key(k); }
int rorder(const T &v) { return size() - tree::order_of_key(v) - 1; }
std::pair<Map<T, U>, Map<T, U>> splitted(const T &v) const {
Map<T, U> s = *this, t;
s.split(v, t);
return std::make_pair(s, t);
}
Map<T, U> &operator<<(const std::pair<T, U> &v) {
insert(v);
return *this;
}
Map<T, U> &operator<<(const tree &m) {
merge(m);
return *this;
}
Map<T, U> &operator>>(const T &v) {
erase(v);
return *this;
}
bool operator>(const T &v) { return contains(v); }
};
template <typename T, typename U>
std::ostream &operator<<(std::ostream &o, const Map<T, U> &m) {
if (m.size() == 0)
return o;
for (int i = 0; i < m.size() - 1; i++)
o << m.key_value(i).first << ' ' << m.key_value(i).second << '\n';
return o << m.key_value(m.size() - 1).first << ' '
<< m.key_value(m.size() - 1).second;
}
// Rope class
template <typename T> class Rope : public __gnu_cxx::rope<T> {
typedef __gnu_cxx::rope<T> rope;
public:
using rope::insert;
using rope::replace;
using rope::operator[];
using rope::operator=;
using rope::begin;
using rope::end;
using rope::rbegin;
using rope::rend;
Rope(const rope &r) : rope(r) {}
Rope(int n = 0) {
for (int i = 0; i < -n; i++) {
T v;
std::cin >> v;
pub(v);
}
}
Rope(const std::initializer_list<T> &l) {
for (const T &e : l)
pub(e);
}
template <typename U> Rope(const std::vector<U> &v) {
for (const T &e : v)
pub(e);
}
template <typename U>
Rope(const __gnu_pbds::tree<
U, __gnu_pbds::null_type, std::less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &t) {
for (const T &e : t)
pub(e);
}
template <typename U> Rope(const __gnu_cxx::rope<U> &r) {
for (const T &e : r)
pub(e);
}
Rope(const std::string &s) {
for (const T &e : s)
pub(e);
}
int size() const { return rope::size(); }
void pub(const T &v) { insert(size(), v); }
void erase(int i) { rope::erase(i, 1); }
void erase(int i, int j) { rope::erase(i, j - i); }
void pob() { erase(size() - 1); }
void resize(int s, const T &v = T()) {
while (s < size())
pob();
while (s > size())
pub(v);
}
Rope<T> operator[](const std::initializer_list<int> &l) const {
if (l.size() == 2)
return rope::substr(*l.begin(), *(l.begin() + 1) - *l.begin());
if (l.size() == 3) {
int first = *l.begin(), last = *(l.begin() + 1), step = *(l.begin() + 2);
last = (last < 0 ? last + size() + 1 : last);
Rope<T> v;
for (int i = first; i < last; i += step)
v.pub((*this)[i]);
return v;
}
return *this;
}
Rope<T> operator+() const { return *this; }
Rope<T> operator-() const {
Rope<T> v;
for (const T &e : *this)
v.pub(-e);
return v;
}
Rope<T> operator++() {
for (int i = 0; i < size(); i++)
replace(i, (*this)[i] + 1);
return *this;
}
Rope<T> operator--() {
for (int i = 0; i < size(); i++)
replace(i, (*this)[i] - 1);
return *this;
}
Rope<T> operator++(int32_t) {
Rope<T> v = *this;
for (int i = 0; i < size(); i++)
replace(i, (*this)[i] + 1);
return v;
}
Rope<T> operator--(int32_t) {
Rope<T> v = *this;
for (int i = 0; i < size(); i++)
replace(i, (*this)[i] - 1);
return v;
}
void out() {
for (const T &e : *this)
std::cout << e << '\n';
}
void outvec() {
for (int i = 0; i < size() - 1; i++)
std::cout << (*this)[i] << ' ';
std::cout << last() << '\n';
}
T last() const { return (*this)[size() - 1]; }
T sum(int l = 0, int r = -1) const {
T v = T();
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v += (*this)[i];
return v;
}
T prod(int l = 0, int r = -1) const {
T v = T() + 1;
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v *= (*this)[i];
return v;
}
T xorsum(int l = 0, int r = -1) const {
T v = T();
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v ^= (*this)[i];
return v;
}
T cumsumpart(int l, int r) const {
return (*this)[r - 1] - (l = 0 ? 0 : (*this)[l - 1]);
}
T cumxorpart(int l, int r) const {
return (*this)[r - 1] ^ (l = 0 ? 0 : (*this)[l - 1]);
}
int findupgeq(const T &v) {
return std::lower_bound(begin(), end(), v) - begin();
}
int findupleq(const T &v) {
return std::upper_bound(begin(), end(), v) - begin() - 1;
}
int upcount(const T &v) { return findupleq(v) - findupgeq(v) + 1; }
int finddowngeq(const T &v) {
return size() - (std::lower_bound(rbegin(), rend(), v) - rbegin()) - 1;
}
int finddownleq(const T &v) {
return size() - (std::upper_bound(rbegin(), rend(), v) - rbegin());
}
int downcount(const T &v) { return finddowngeq(v) - finddownleq(v) + 1; }
};
// String class
class String : public std::string {
typedef std::string SS;
public:
using SS::operator[];
using SS::operator=;
using SS::assign;
using SS::begin;
using SS::end;
using SS::pop_back;
using SS::push_back;
using SS::rbegin;
using SS::rend;
using SS::resize;
using SS::operator+=;
String() {}
String(const char *s) : SS(s) {}
String(char c) : SS(1, c) {}
String(int i, char c) : SS(i, c) {}
String(int32_t i) : SS(std::to_string(i)) {}
String(int64_t i) : SS(std::to_string(i)) {}
String(const std::vector<char> &v) {
for (char e : v)
pub(e);
}
String(const __gnu_pbds::tree<
char, __gnu_pbds::null_type, std::less<char>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &t) {
for (char e : t)
pub(e);
}
String(const __gnu_cxx::rope<char> &r) {
for (char e : r)
pub(e);
}
void pub(char c) { push_back(c); }
void pub(String s) {
for (char c : s)
push_back(c);
}
void pob() { SS::pop_back(); }
String operator[](const std::initializer_list<int> &l) const {
if (l.size() == 2 || l.size() == 3) {
int first = *l.begin(), last = *(l.begin() + 1),
step = (l.size() == 2 ? 1 : *(l.begin() + 2));
last = (last < 0 ? last + size() + 1 : last);
String s;
for (int i = first; i < last; i += step)
s.pub((*this)[i]);
return s;
}
return *this;
}
String operator+(const String &s) const {
String r(*this);
r.pub(s);
return r;
}
char last() const { return (*this)[size() - 1]; }
String &operator<<(const String &s) {
pub(s);
return *this;
}
char &last() { return (*this)[size() - 1]; }
int size() const { return SS::size(); }
int32_t toInt() const { return std::stoi(*this); }
int64_t toLong() const { return std::stol(*this); }
std::vector<int> find(const String &s) const {
Vector<int> v;
int i = SS::find(s);
while (i != SS::npos) {
v.pub(i);
i = SS::find(s, i + s.size());
}
return v;
}
std::vector<int> find_dup(const String &s) const {
Vector<int> v;
int i = SS::find(s);
while (i != SS::npos) {
v.pub(i);
i = SS::find(s, i + 1);
}
return v;
}
void reverse() { std::reverse(begin(), end()); }
void insert(int i, const String &s) { SS::insert(i, s); }
void erase(int i) { SS::erase(i, 1); }
void erase(int i, int j) { SS::erase(i, j - i); }
void erase_unique() { SS::erase(std::unique(begin(), end()), end()); }
void rotatedown(int i) { std::rotate(begin(), begin() + i, end()); }
void rotateup(int i) { std::rotate(rbegin(), rbegin() + i, rend()); }
void sortup() {
int m = *std::min_element(begin(), end());
int M = *std::max_element(begin(), end());
std::vector<char> b(M - m + 1);
for (char e : *this)
b[e - m]++;
for (int i = 0; i < b.size() - 1; i++)
b[i + 1] += b[i];
for (int i = 0; i < b.size(); i++)
for (int j = (i == 0 ? 0 : b[i - 1]); j < b[i]; j++)
(*this)[j] = m + i;
}
void sortdown() {
sortup();
reverse();
}
String reversed() const {
String r(*this);
r.reverse();
return r;
}
String inserted(int i, const String &s) const {
String r(*this);
r.insert(i, s);
return r;
}
String erased(int i) const {
String r(*this);
r.erase(i);
return r;
}
String erased(int i, int j) const {
String r(*this);
r.erase(i, j);
return r;
}
String erase_uniqued() const {
String r(*this);
r.erase_unique();
return r;
}
String rotatedowned(int i) const {
String r(*this);
r.rotatedown(i);
return r;
}
String rotateuped(int i) const {
String r(*this);
r.rotateup(i);
return r;
}
String sortuped() const {
String r(*this);
r.sortup();
return r;
}
String sortdowned() const {
String r(*this);
r.sortdown();
return r;
}
};
// Trie class
class Trie
: public __gnu_pbds::trie<std::string, __gnu_pbds::null_type,
__gnu_pbds::trie_string_access_traits<>,
__gnu_pbds::pat_trie_tag,
__gnu_pbds::trie_prefix_search_node_update> {
public:
typedef __gnu_pbds::trie<std::string, __gnu_pbds::null_type,
__gnu_pbds::trie_string_access_traits<>,
__gnu_pbds::pat_trie_tag,
__gnu_pbds::trie_prefix_search_node_update>
tree;
using tree::operator=;
using tree::begin;
using tree::end;
using tree::erase;
using tree::insert;
using tree::split;
Trie(const tree &t) : tree(t) {}
Trie(int n = 0) {
for (int i = 0; i < -n; i++) {
std::string v;
std::cin >> v;
insert(v);
}
}
Trie(const std::initializer_list<std::string> &l) {
for (const std::string &e : l)
insert(e);
}
int size() const { return tree::size(); }
bool contains(const std::string &s) const { return tree::find(s) != end(); }
void merge(const tree &s) {
tree t = s;
tree::join(t);
}
std::pair<Trie, Trie> splitted(const std::string &s) const {
Trie r = *this, t;
r.split(s, t);
return std::make_pair(r, t);
}
void eraseall(const std::string &s) {
std::vector<std::string> match = (*this)[s];
for (const std::string &e : match)
erase(e);
}
void exploit(const std::string &s) {
std::vector<std::string> match;
std::pair<tree::const_iterator, tree::const_iterator> range =
prefix_range(s);
for (tree::const_iterator it = begin(); it != range.first; ++it)
match.push_back(*it);
for (tree::const_iterator it = range.second; it != end(); ++it)
match.push_back(*it);
for (const std::string &e : match)
erase(e);
}
Trie &operator<<(const std::string &s) {
insert(s);
return *this;
}
Trie &operator<<(const tree &t) {
merge(t);
return *this;
}
Trie &operator>>(const std::string &s) {
erase(s);
return *this;
}
bool operator>(const std::string &s) { return contains(s); }
std::vector<std::string> operator[](const std::string &s) const {
std::vector<std::string> match;
std::pair<tree::const_iterator, tree::const_iterator> range =
prefix_range(s);
for (tree::const_iterator it = range.first; it != range.second; ++it)
match.push_back(*it);
return match;
}
};
std::ostream &operator<<(std::ostream &o, const Trie &t) {
auto it = t.begin();
for (int i = 0; i < t.size() - 1; i++, it++)
o << *it << '\n';
return o << *it;
}
// Modulo class
#define ModCalc(OP) \
Modulo operator OP(const Modulo &l, const Modulo &r) { \
return Modulo(l.val() OP r.val()); \
} \
Modulo operator OP(const Modulo &l, const int64_t &r) { \
return Modulo(l.val() OP Modulo(r)); \
} \
Modulo operator OP(const int64_t &l, const Modulo &r) { \
return Modulo(l) OP r.val(); \
} \
Modulo operator OP##=(Modulo &l, const Modulo &r) { return l = l OP r; } \
Modulo operator OP##=(Modulo &l, const int64_t &r) { return l = l OP r; } \
int64_t operator OP##=(int64_t &l, const Modulo &r) { \
return l = l OP r.val(); \
}
#define ModComp(OP) \
bool operator OP(const Modulo &l, const Modulo &r) { \
return l.val() OP r.val(); \
} \
bool operator OP(const Modulo &l, const int64_t &r) { return l.val() OP r; } \
bool operator OP(const int64_t &l, const Modulo &r) { return l OP r.val(); }
class Modulo {
int64_t value;
public:
Modulo() : value(0) {}
Modulo(const int64_t &v) {
value = v;
normalize();
}
void normalize() { value = (value % mod() + mod()) % mod(); }
Modulo(const Modulo &m) : value(m.val()) {}
Modulo inv() const {
int64_t a = value;
int64_t b = mod(), u = 0, v = 1;
while (a > 1) {
u -= b / a * v;
b %= a;
std::swap(a, b);
std::swap(u, v);
}
return Modulo(v);
}
int64_t val() const { return value; }
int64_t &val() { return value; }
Modulo &operator=(const Modulo &m) {
value = m.val();
return *this;
}
int64_t mod() const { return MOD; }
Modulo &operator=(const int64_t &v) {
value = v;
return *this;
}
Modulo operator++() { return Modulo(++value); }
Modulo operator--() { return Modulo(--value); }
Modulo operator++(int32_t) { return Modulo(value++); }
Modulo operator--(int32_t) { return Modulo(value--); }
Modulo operator+() const { return *this; }
Modulo operator-() const { return Modulo(-value); }
Modulo power(int64_t i) const {
int64_t x = value;
int64_t r = 1;
while (i > 0) {
if (i & 1)
r = r * x % mod();
x = x * x % mod();
i /= 2;
}
return Modulo(r);
}
};
std::ostream &operator<<(std::ostream &o, const Modulo &m) {
return o << m.val();
}
std::istream &operator>>(std::istream &i, Modulo &m) {
i >> m.val();
m.normalize();
return i;
}
ModCalc(+) ModCalc(-) ModCalc(*) ModCalc(<<) ModCalc(>>) ModCalc(&) ModCalc(|)
ModCalc(^) ModComp(==) ModComp(!=) ModComp(>=) ModComp(<=) ModComp(>)
ModComp(<) Modulo
operator/(const Modulo &l, const Modulo &r) {
return Modulo(l.val() * r.inv());
}
Modulo operator/(const Modulo &l, const int64_t &r) {
return Modulo(l.val() * Modulo(r).inv());
}
Modulo operator/(const int64_t &l, const Modulo &r) {
return Modulo(Modulo(l) * r.inv());
}
// Matrix class need Vector class
#define MatCalc(OP) \
Matrix<T> operator OP(const VVT &r) const { \
Matrix<T> v(std::min(size(), r.size())); \
for (int i = 0; i < v.size(); i++) \
v[i] = (*this)[i] OP r[i]; \
v.rect(); \
return v; \
} \
Matrix<T> operator OP(const VT &r) const { \
Matrix<T> v(std::min(size(), r.size())); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
} \
Matrix<T> operator OP(const T &r) const { \
Matrix<T> v(size()); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
} \
Matrix<T> operator OP##=(const VVT &r) { return *this = *this OP r; } \
Matrix<T> operator OP##=(const VT &r) { return *this = *this OP r; } \
Matrix<T> operator OP##=(const T &r) { return *this = *this OP r; }
#define MatComp(OP) \
Matrix<int> operator OP(const VVT &r) const { \
Matrix<int> v(std::min(size(), r.size())); \
for (int i = 0; i < v.size(); i++) \
v[i] = (*this)[i] OP r[i]; \
r.rect(); \
return v; \
} \
Matrix<int> operator OP(const VT &r) const { \
Matrix<int> v(size()); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
} \
Matrix<int> operator OP(const T &r) const { \
Matrix<int> v(size()); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
}
template <typename T> class Matrix : private Vector<Vector<T>> {
typedef Vector<T> VT;
typedef Vector<VT> VVT;
using VVT::erase;
using VVT::insert;
using VVT::pob;
using VVT::pub;
public:
using VVT::operator+;
using VVT::operator-;
using VVT::operator++;
using VVT::operator--;
using VVT::operator[];
using VVT::assign;
using VVT::begin;
using VVT::end;
using VVT::last;
using VVT::out;
using VVT::rbegin;
using VVT::rend;
using VVT::resize;
using VVT::size;
Matrix() {}
Matrix(const VVT &m) : VVT(m) { rect(); }
Matrix(const VT &v) : VVT(1, v) {}
Matrix(int c, int r = 0, T v = T()) {
if (c >= 0 && r >= 0)
assign(c, r, v);
else {
assign(-c, -r, v);
for (VT &e : *this)
std::cin >> e;
}
}
Matrix(std::pair<int, int> p, T v = T()) { assign(p.first, p.second, v); }
Matrix(const std::initializer_list<std::initializer_list<T>> &l) {
resize(l.size());
for (int i = 0; i < size(); i++)
(*this)[i] = *(l.begin() + i);
rect();
}
template <typename U> Matrix(const Matrix<U> &v) {
resize(v.size());
for (int i = 0; i < size(); i++)
(*this)[i] = v[i];
}
void rect() {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = (*this)[i].size();
resize(size(), v.max());
}
std::pair<int, int> shape() const {
return std::make_pair(size(), size() == 0 ? 0 : (*this)[0].size());
}
bool isEmpty() { return shape().first == 0 && shape().second == 0; }
void resize(int c, int r, T v = T()) {
resize(c);
for (VT &e : *this)
e.resize(r, v);
}
void resize(std::pair<int, int> p, T v = T()) {
resize(p.first, p.second, v);
}
void assign(int c, int r, T v) { assign(c, VT(r, v)); }
void assign(std::pair<int, int> p, const T &v) {
assign(p.first, p.second, v);
}
T operator[](std::pair<int, int> p) const {
return (*this)[p.first][p.second];
}
T &operator[](std::pair<int, int> p) { return (*this)[p.first][p.second]; }
Matrix<T> operator[](const std::pair<std::initializer_list<T>,
std::initializer_list<T>> &p) const {
int columnFirst = *p.first.begin(), columnLast = *(p.first.begin() + 1),
columnStep = (p.first.size() == 2 ? 1 : *(p.first.begin() + 2));
columnLast = (columnLast < 0 ? columnLast + size() + 1 : columnLast);
int rowFirst = *p.second.begin(), rowLast = *(p.second.begin() + 1),
rowStep = (p.second.size() == 2 ? 1 : *(p.second.begin() + 2));
rowLast = (rowLast < 0 ? rowLast + size() + 1 : rowLast);
Matrix<T> m;
for (int i = columnFirst; i < columnLast; i += columnStep) {
VT v;
for (int j = rowFirst; j < rowLast; j += rowStep)
v.pub((*this)[i][j]);
m.vstack(v);
}
return m;
}
VT vector() const {
VT v;
for (const VT &e : *this)
v.pub(e);
return v;
}
T sum(int cl = 0, int rl = 0, int cr = -1, int rr = -1) const {
T v = T();
cr = (cr < 0 ? cr + size() + 1 : cr);
for (int i = cl; i < cr; i++)
v += (*this)[i].sum(rl, rr);
return v;
}
T prod(int cl = 0, int rl = 0, int cr = -1, int rr = -1) const {
T v = T() + 1;
cr = (cr < 0 ? cr + size() + 1 : cr);
for (int i = cl; i < cr; i++)
v *= (*this)[i].prod(rl, rr);
return v;
}
T xorsum(int cl = 0, int rl = 0, int cr = -1, int rr = -1) const {
T v = T();
cr = (cr < 0 ? cr + size() + 1 : cr);
for (int i = cl; i < cr; i++)
v ^= (*this)[i].xorsum(rl, rr);
return v;
}
Matrix<T> &operator=(const VVT &v) {
resize(v.size());
for (int i = 0; i < size(); i++)
(*this)[i] = v[i];
rect();
}
Matrix<T> &operator=(const VT &v) {
assign(1, v);
return *this;
}
std::pair<int, int> argmin() const {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = (*this)[i].min();
int m = v.argmin();
return std::make_pair(m, (*this)[m].argmin());
}
std::pair<int, int> argmax() const {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = (*this)[i].max();
int m = v.argmax();
return std::make_pair(m, (*this)[m].argmax());
}
T min() const {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = (*this)[i].min();
return v.min();
}
T max() const {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = (*this)[i].max();
return v.max();
}
void dot(Matrix<T> v) {
Matrix<T> m(*this);
assign(shape().first, v.shape().second, 0);
for (int i = 0; i < shape().first; i++)
for (int j = 0; j < shape().second; j++)
for (int k = 0; k < std::min(m.shape().second, v.shape().first); k++)
(*this)[i][j] += m[i][k] * v[k][j];
}
void vstack(const VT &v) {
if (shape().second < v.size() && size() > 0) {
pub(v[{0, shape().second}]);
} else {
pub(v);
last().resize(shape().second);
}
}
void vstack(const VVT &v) {
for (const VT &e : v)
vstack(e);
}
void hstack(const VT &v) {
if (size() == 0)
resize(v.size());
for (int i = 0; i < std::min(size(), v.size()); i++)
(*this)[i].pub(v[i]);
resize(shape());
}
void hstack(const VVT &v) {
if (size() == 0)
resize(v.size());
for (int i = 0; i < std::min(size(), v.size()); i++)
(*this)[i].pub(v[i]);
rect();
}
void vpob() { pob(); }
void hpob() {
for (VT &e : *this)
e.pob();
}
void verase(int i) { erase(i); }
void verase(int i, int j) { erase(i, j); }
void herase(int i) {
for (VT &e : *this)
e.erase(i);
}
void herase(int i, int j) {
for (VT &e : *this)
e.erase(i, j);
}
void vinsert(int i, const VT &v) {
if (isEmpty())
(*this) = v;
else {
insert(i, v[{0, std::min(shape().second, v.size()), 1}]);
if (i == 0)
rect();
else
(*this)[i].resize(shape().second);
}
}
void vinsert(int i, const Matrix<T> &v) {
if (isEmpty())
(*this) = v;
else {
insert(
i,
v[{{0, -1, 1}, {0, std::min(shape().second, v.shape().second), 1}}]);
if (i == 0)
rect();
else
(*this)[i].resize(shape().second);
}
}
void hinsert(int i, const VT &v) {
if (isEmpty())
(*this) = v;
else
for (int j = 0; j < std::min(size(), v.size()); j++)
(*this)[j].insert(i, v[j]);
for (int j = std::min(size(), v.size()); j < size(); j++)
(*this)[j].insert(i, 0);
}
void hinsert(int i, const Matrix<T> &v) {
if (isEmpty())
(*this) = v;
else
for (int j = 0; j < std::min(size(), v.size()); j++)
(*this)[j].insert(i, v[j]);
for (int j = std::min(size(), v.size()); j < size(); j++)
(*this)[j].resize(v.shape().second);
}
void transpose() {
std::pair<int, int> s = shape();
if (s.first < s.second)
for (int i = 0; i < s.second - s.first; i++)
pub(VT(s.first));
else
for (int i = 0; i < s.second; i++)
(*this)[i].resize(s.first);
for (int i = 0; i < std::min(s.first, s.second); i++)
for (int j = i + 1; j < size(); j++)
std::swap((*this)[i][j], (*this)[j][i]);
resize(s.second, s.first);
}
void power(int n) {
Matrix<T> m(*this);
assign(size(), size(), 0);
for (int i = 0; i < size(); i++)
(*this)[i][i] = 1;
while (n > 0) {
if (n & 1)
dot(m);
m.dot(m);
n /= 2;
}
}
void cumsum() {
VVT::cumsum();
for (VT &e : *this)
e.cumsum();
}
void decumsum() {
VVT::decumsum();
for (VT &e : *this)
e.decumsum();
}
T cumsumpart(int cl, int rl, int cr, int rr) const {
return (*this)[cr - 1][rr - 1] - (cl == 0 ? 0 : (*this)[cl - 1][rr - 1]) -
(rl == 0 ? 0 : (*this)[cr - 1][rl - 1]) +
((cl == 0 || rl == 0) ? 0 : (*this)[cl - 1][rl - 1]);
}
void cumxor() {
VVT::cumxor();
for (VT &e : *this)
e.cumxor();
}
void decumxor() {
VVT::decumxor();
for (VT &e : *this)
e.decumxor();
}
T cumxorpart(int cl, int rl, int cr, int rr) const {
return (*this)[cr - 1][rr - 1] ^ (cl == 0 ? 0 : (*this)[cl - 1][rr - 1]) ^
(rl == 0 ? 0 : (*this)[cr - 1][rl - 1]) ^
((cl == 0 || rl == 0) ? 0 : (*this)[cl - 1][rl - 1]);
}
void compress() {
VT u = vector();
u.sortup();
u.erase_unique();
std::unordered_map<T, int> m;
for (int i = 0; i < u.size(); i++)
m[u[i]] = i;
for (int i = 0; i < shape().first; i++)
for (int j = 0; j < shape().second; j++)
(*this)[i][j] = m[(*this)[i][j]];
}
MatCalc(+) MatCalc(-) MatCalc(*) MatCalc(/) MatCalc(%) MatCalc(<<) MatCalc(>>)
MatCalc(&) MatCalc(|) MatCalc(^) MatComp(==) MatComp(!=) MatComp(>=)
MatComp(<=) MatComp(>)
MatComp(<) Matrix<T> vstacked(const VT &v) const {
Matrix<T> r(*this);
r.vstack(v);
return r;
}
Matrix<T> vstacked(const VVT &v) const {
Matrix<T> r(*this);
r.vstack(v);
return r;
}
Matrix<T> hstacked(const VT &v) const {
Matrix<T> r(*this);
r.hstack(v);
return r;
}
Matrix<T> hstacked(const VVT &v) const {
Matrix<T> r(*this);
r.hstack(v);
return r;
}
Matrix<T> vpobed() const {
Matrix<T> r(*this);
r.vpob();
return r;
}
Matrix<T> hpobed() const {
Matrix<T> r(*this);
r.hpob();
return r;
}
Matrix<T> verased(int i) const {
Matrix<T> r(*this);
r.verase(i);
return r;
}
Matrix<T> verased(int i, int j) const {
Matrix<T> r(*this);
r.verase(i, j);
return r;
}
Matrix<T> herased(int i) const {
Matrix<T> r(*this);
r.herase(i);
return r;
}
Matrix<T> herased(int i, int j) const {
Matrix<T> r(*this);
r.herase(i, j);
return r;
}
Matrix<T> vinserted(int i, const VT &v) const {
Matrix<T> r(*this);
r.vinsert(i, v);
return r;
}
Matrix<T> vinserted(int i, const Matrix<T> &v) const {
Matrix<T> r(*this);
r.vinsert(i, v);
return r;
}
Matrix<T> hinserted(int i, const VT &v) const {
Matrix<T> r(*this);
r.hinsert(i, v);
return r;
}
Matrix<T> hinserted(int i, const Matrix<T> &v) const {
Matrix<T> r(*this);
r.hinsert(i, v);
return r;
}
Matrix<T> transposed() const {
Matrix<T> r(*this);
r.transpose();
return r;
}
Matrix<T> powered(int n) const {
Matrix<T> r(*this);
r.power(n);
return r;
}
Matrix<T> cumsumed() const {
Matrix<T> r(*this);
r.cumsum();
return r;
}
Matrix<T> decumsumed() const {
Matrix<T> r(*this);
r.decumsum();
return r;
}
Matrix<T> cumxored() const {
Matrix<T> r(*this);
r.cumxor();
return r;
}
Matrix<T> decumxored() const {
Matrix<T> r(*this);
r.decumxor();
return r;
}
Matrix<T> compressed() const {
Matrix<T> r(*this);
r.compress();
return r;
}
};
// FFT, NNT need Vector Modulo class
std::unordered_map<int, int> MOD_ROOT = {
{1224736769, 3}, {1053818881, 7}, {1051721729, 6}, {1045430273, 3},
{1012924417, 5}, {1007681537, 3}, {1004535809, 3}, {998244353, 3},
{985661441, 3}, {976224257, 3}, {975175681, 17}, {962592769, 7},
{950009857, 7}, {943718401, 7}, {935329793, 3}, {924844033, 5},
{469762049, 3}, {167772161, 3}};
Vector<std::complex<double>> fft(Vector<std::complex<double>> a,
bool inverse = false) {
int n = a.size(), h = std::log2(n);
for (int i = 0; i < n; i++) {
int j = 0;
for (int k = 0; k < h; k++)
j |= (i >> k & 1) << (h - 1 - k);
if (i < j)
std::swap(a[i], a[j]);
}
for (int b = 1; b < n; b *= 2) {
for (int j = 0; j < b; j++) {
std::complex<double> w =
std::polar(1.0, (2 * M_PI) / (2 * b) * j * (inverse ? 1 : -1));
for (int k = 0; k < n; k += b * 2) {
std::complex<double> s = a[j + k], t = a[j + k + b] * w;
a[j + k] = s + t;
a[j + k + b] = s - t;
}
}
}
a /= std::complex<double>(inverse ? n : 1);
return a;
}
Vector<Modulo> ntt(Vector<Modulo> a, bool inverse = false) {
int n = a.size(), h = std::log2(n);
Modulo root = MOD_ROOT[MOD];
for (int i = 0; i < n; i++) {
int j = 0;
for (int k = 0; k < h; k++)
j |= (i >> k & 1) << (h - 1 - k);
if (i < j)
std::swap(a[i], a[j]);
}
for (int b = 1; b < n; b *= 2) {
Modulo w = root.power((a[0].mod() - 1) / (b * 2));
if (inverse)
w = w.inv();
for (int j = 0; j < n; j += b * 2) {
Modulo wn = 1;
for (int k = 0; k < b; k++) {
Modulo s = a[j + k + 0], t = a[j + k + b] * wn;
a[j + k] = s + t;
a[j + k + b] = s - t;
wn = wn * w;
}
}
}
if (inverse) {
Modulo v = Modulo(n).inv();
a *= v;
}
return a;
}
Vector<double> convolve(Vector<double> a, Vector<double> b) {
if (a.size() == 0 || b.size() == 0)
return Vector<double>();
int s = a.size() + b.size() - 1, t = std::pow(2, std::ceil(std::log2(s)));
a.resize(t);
b.resize(t);
Vector<std::complex<double>> A = fft(a), B = fft(b);
A *= B;
A = fft(A, true);
a.resize(s);
for (int i = 0; i < s; i++)
a[i] = A[i].real();
return a;
}
Vector<int> convolve(Vector<int> a, Vector<int> b) {
return convolve(Vector<double>(a), Vector<double>(b)) + 0.5;
}
Vector<Modulo> convolve(Vector<Modulo> a, Vector<Modulo> b) {
if (a.size() == 0 || b.size() == 0)
return Vector<Modulo>();
int s = a.size() + b.size() - 1, t = std::pow(2, std::ceil(std::log2(s)));
a.resize(t);
b.resize(t);
Vector<Modulo> A = ntt(a), B = ntt(b);
A *= B;
a = ntt(A, true);
a.resize(s);
return a;
}
// MultipleVector class need Vector, Modulo class
class MultipleVector {
Vector<Modulo> vfact, ifact, pows;
Modulo p;
public:
MultipleVector(int n = 0, int _p = 1)
: vfact(Vector<Modulo>(1, 1)), ifact(Vector<Modulo>(1, 1)),
pows(Vector<Modulo>(1, 1)), p(_p) {
resize(n);
}
void resize(int n) {
if (vfact.size() >= n)
return;
int s = vfact.size();
vfact.resize(n + 1);
for (int i = s; i <= n; i++)
vfact[i] = i * vfact[i - 1];
ifact.resize(n + 1);
ifact[n] = vfact[n].inv();
for (int i = n; i > s; i--)
ifact[i - 1] = i * ifact[i];
pows.resize(n + 1);
for (int i = s; i <= n; i++)
pows[i] = p * pows[i - 1];
}
Modulo fact(Modulo n) {
resize(n.val());
return vfact[n.val()];
}
Vector<Modulo> facts(Modulo n) {
resize(n.val());
return vfact[{0, n.val(), 1}];
}
Modulo factinv(Modulo n) {
resize(n.val());
return ifact[n.val()];
}
Vector<Modulo> factinvs(Modulo n) {
resize(n.val());
return ifact[{0, n.val(), 1}];
}
Modulo power(Modulo n) {
resize(n.val());
return pows[n.val()];
}
Vector<Modulo> powers(Modulo n) {
resize(n.val());
return pows[{0, n.val(), 1}];
}
Modulo parm(Modulo n, Modulo r) {
if (n < r)
return Modulo();
resize(n.val());
return vfact[n.val()] * ifact[(n - r).val()];
}
Modulo comb(Modulo n, Modulo r) {
if (n < r)
return Modulo();
resize(n.val());
return vfact[n.val()] * ifact[(n - r).val()] * ifact[r.val()];
}
Modulo homo(Modulo n, Modulo r) { return comb(n + r - 1, r); }
};
// Eratos class need Vector class
class Eratos {
Vector<int> divs, prms, ords;
int order_max;
public:
Eratos(int n = 0) : order_max(0) { resize(n); }
int size() const { return divs.size(); }
void resize(int n) {
if (size() >= n)
return;
int m = size();
divs.resize(n + 1);
for (int i = m; i < 2; i++)
if (i < size())
divs[i] = -1;
int r = std::sqrt(n - 1) + 1;
for (int i = 2; i < r; i++)
if (divs[i] == 0)
for (int j = std::max((m - 1) / i + 1, (int)2) * i; j <= n; j += i)
divs[j] = (divs[j] == 0 ? i : divs[j]);
for (int i = m; i <= n; i++)
if (divs[i] == 0)
prms.pub(i);
ords.resize(n + 1);
for (int i = m; i <= n; i++)
ords[i] = (divs[i] == 0 ? ++order_max : 0);
}
Vector<int> divisors() const { return divs; }
Vector<int> primes() const { return prms; }
Vector<int> orders() const { return ords; }
};
// Unionfind class need Vector class
template <typename T> class Unionfind {
Vector<int> p;
Vector<T> w;
int sign(int x) { return (x > 0) - (x < 0); }
int get(int x) {
if (p[x] == (x + 1))
return x + 1;
else {
int parent = sign(p[x]) * get(abs(p[x]) - 1);
w[x] += w[abs(p[x]) - 1];
return p[x] = parent;
}
}
void get(int &x, int &y) {
x = get(x);
y = get(y);
}
int weight(int x) {
get(x);
return w[x];
}
public:
Unionfind(int n = 0) { resize(n); }
void resize(const int &n) {
p.resize(n);
for (int i = w.size(); i < p.size(); i++)
p[i] = i + 1;
w.resize(n);
}
int size() const { return p.size(); }
bool unite(int x, int y, T vv = T()) {
if (vv < 0) {
std::swap(x, y);
vv *= -1;
}
vv += weight(x) - weight(y);
get(x, y);
if (abs(x) == abs(y))
return vv == weight(x) - weight(y);
p[abs(y) - 1] = x * sign(y);
w[abs(y) - 1] = vv;
return true;
}
bool separate(int x, int y, T vv = T()) {
if (vv < 0) {
std::swap(x, y);
vv *= -1;
}
vv += weight(x) - weight(y);
get(x, y);
if (abs(x) == abs(y))
return vv == weight(x) - weight(y);
p[abs(y) - 1] = -x * sign(y);
w[abs(y) - 1] = vv;
return true;
}
int same(int x, int y) {
get(x, y);
return (x == y) - (x == -y);
}
Vector<int> same(int x) {
Vector<int> v(size());
for (int i = 0; i < size(); i++)
v[i] = same(x, i);
return v;
}
T diff(int x, int y) { return sign(same(x, y)) * (weight(y) - weight(x)); }
Vector<T> diff(int x) {
Vector<int> v(size());
for (int i = 0; i < size(); i++)
v[i] = diff(x, i);
return v;
}
};
// Graph class need Vector class
template <typename T> struct Edge {
int from, to;
T cost;
Edge(int f = 0, int t = 0, T c = T()) : from(f), to(t), cost(c) {}
};
template <typename T> std::istream &operator>>(std::istream &i, Edge<T> &e) {
i >> e.from >> e.to >> e.cost;
return i;
}
template <typename T>
std::ostream &operator<<(std::ostream &o, const Edge<T> &e) {
return o << e.from << ' ' << e.to << ' ' << e.cost;
}
template <typename T> class Graph {
public:
Vector<Edge<T>> edge;
Vector<Vector<int>> node;
Graph(int n = 0) { resize(n); }
void resize(int n) { node.resize(n); }
void clear() {
edge = Vector<Edge<T>>();
node = Vector<Vector<int>>(nodes());
}
int edges() const { return edge.size(); }
int nodes() const { return node.size(); }
virtual int add(int from, int to, T cost) = 0;
Vector<T> dijkstra(int start) {
Vector<T> d(nodes(), std::numeric_limits<T>::max());
std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>,
std::greater<std::pair<T, int>>>
s;
d[start] = 0;
s.emplace(d[start], start);
while (!s.empty()) {
T dist = s.top().first;
int from = s.top().second;
s.pop();
if (d[from] < dist)
continue;
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
if (d[from] + edge[id].cost < d[to]) {
d[to] = d[from] + edge[id].cost;
s.emplace(d[to], to);
}
}
}
return d;
}
};
// Undigraph class need Vector, Graph class
template <typename T> class Undigraph : public Graph<T> {
Vector<int> brgs, crus, lows, ords;
bool lowlinked;
int lowlink_dfs(int from, int k, int parent) {
ords[from] = lows[from] = k++;
bool is_crunode = false;
int cnt = 0;
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
if (ords[to] == -1) {
cnt++;
k = lowlink_dfs(to, k, from);
lows[from] = std::min(lows[from], lows[to]);
is_crunode |= parent != -1 && lows[to] >= ords[from];
if (ords[from] < lows[to])
brgs.pub(id);
} else if (to != parent)
lows[from] = std::min(lows[from], lows[to]);
}
is_crunode |= parent == -1 && cnt > 1;
if (is_crunode)
crus.pub(from);
return k;
}
void lowlink() {
brgs = Vector<int>();
crus = Vector<int>();
ords.assign(this->nodes(), -1), lows.assign(this->nodes(), -1);
int k = 0;
for (int i = 0; i < this->nodes(); i++)
if (ords[i] == -1)
k = lowlink_dfs(i, k, -1);
lowlinked = true;
}
public:
using Graph<T>::edge;
using Graph<T>::node;
Undigraph(int n = 0) : Graph<T>(n) { lowlinked = false; }
int add(int from, int to, T cost = 1) {
int id = this->edges();
node[from].pub(id);
node[to].pub(id);
edge.pub(Edge<T>(from, to, cost));
return id;
}
void reset_lowlink() { lowlink(); }
Vector<int> bridges() {
if (!lowlinked)
lowlink();
return brgs;
}
Vector<int> crunodes() {
if (!lowlinked)
lowlink();
return crus;
}
T prim(int start = 0) {
Vector<std::pair<T, std::pair<int, int>>> cft;
T total = 0;
Vector<int> used(this->nodes(), 0);
std::priority_queue<std::pair<T, std::pair<int, int>>,
std::vector<std::pair<T, std::pair<int, int>>>,
std::greater<std::pair<T, std::pair<int, int>>>>
s;
s.emplace(std::make_pair(0, std::make_pair(-1, start)));
while (!s.empty()) {
T cost = s.top().first;
int fromfrom = s.top().second.first, from = s.top().second.second;
s.pop();
if (used[from])
continue;
used[from] = true;
total += cost;
if (fromfrom > -1)
cft.pub(std::make_pair(cost, std::make_pair(fromfrom, from)));
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
s.emplace(std::make_pair(edge[id].cost, std::make_pair(from, to)));
}
}
this->clear();
for (std::pair<T, std::pair<int, int>> e : cft)
add(e.second.first, e.second.second, e.first);
return total;
}
};
// Forest class need Vector, Graph, Undigraph class
template <typename T> class Forest : public Undigraph<T> {
bool table_created;
Vector<Vector<int>> lca_table;
void root_dfs(int from) {
for (int &id : node[from]) {
if (from == edge[id].from)
std::swap(edge[id].from, edge[id].to);
int to = edge[id].from;
if (root[to] != -1)
continue;
root[to] = root[from];
parent[to] = from;
depth[to] = depth[from] + 1;
dist[to] = dist[from] + edge[id].cost;
children[from].pub(to);
root_dfs(to);
treesize[from] += treesize[to];
}
}
void make_lca_table() {
Vector<int> next_parent = parent;
while ((next_parent == Vector<int>(this->nodes(), -1)).sum() <
this->nodes()) {
lca_table.pub(next_parent);
for (int i = 0; i < this->nodes(); i++) {
next_parent[i] =
(next_parent[i] == -1 ? -1 : lca_table.last()[next_parent[i]]);
}
}
table_created = true;
}
public:
Vector<int> root, parent, treesize, depth;
Vector<T> dist;
Vector<Vector<int>> children;
using Undigraph<T>::edge;
using Undigraph<T>::node;
using Undigraph<T>::resize;
using Undigraph<T>::edges;
using Undigraph<T>::nodes;
Forest(int n = 0) : Undigraph<T>(n) {
initialize();
table_created = false;
}
void initialize() {
root.assign(nodes(), -1);
parent.assign(nodes(), -1);
treesize.assign(nodes(), 1);
depth.assign(nodes(), 0);
dist.assign(nodes(), 0);
children.assign(nodes(), Vector<int>());
}
void setTreeRoot(int n) {
initialize();
root[n] = n;
root_dfs(n);
}
void setForestRoot() {
initialize();
for (int n = 0; n < nodes(); n++)
if (root[n] == -1) {
root[n] = n;
root_dfs(n);
};
}
T diameter(int n = 0) {
setTreeRoot(n);
int terminal = dist.argmax();
setTreeRoot(terminal);
return dist.max();
}
int ancestor(int n, int i) {
if (!table_created)
make_lca_table();
if (i < 0)
return -1;
i = depth[n] - i;
for (int j = 0; i > 0; j++) {
if (i & 1)
n = lca_table[j][n];
i /= 2;
}
return n;
}
int lca(int u, int v) {
if (depth[u] > depth[v])
std::swap(u, v);
int i = 0, j = depth[u];
while (j - i > 1) {
int m = (i + j) / 2;
if (ancestor(u, m) == ancestor(v, m))
i = m;
else
j = m;
}
return ancestor(u, i);
}
void reset_lca() {
lca_table = Vector<Vector<int>>();
make_lca_table();
}
};
// Digraph class need Vector, Graph class
template <typename T> class Digraph : public Graph<T> {
Vector<Vector<int>> rev_node;
Vector<Edge<T>> rev_edge;
Vector<int> strong_edgeid, weak_edgeid, strong_id, ords;
bool scced;
void scc_dfs(int from) {
if (strong_id[from] == -1)
return;
strong_id[from] = -1;
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
scc_dfs(to);
}
ords.pub(from);
}
void scc_rdfs(int from, int cnt) {
if (strong_id[from] != -1)
return;
strong_id[from] = cnt;
for (int id : rev_node[from]) {
int to = rev_edge[id].from ^ rev_edge[id].to ^ from;
if (strong_id[to] == -1) {
strong_edgeid.pub(id);
scc_rdfs(to, cnt);
} else if (strong_id[to] == cnt)
strong_edgeid.pub(id);
else if (strong_id[to] < cnt)
weak_edgeid.pub(id);
}
}
public:
using Graph<T>::edge;
using Graph<T>::node;
Digraph(int n = 0) : Graph<T>(n), scced(false) {}
int add(int from, int to, T cost = 1) {
int id = this->edges();
node[from].pub(id);
edge.pub(Edge<T>(from, to, cost));
return id;
}
Digraph<T> reversed() const {
Digraph<T> r(this->edges());
for (Edge<T> e : edge)
r.add(e.to, e.from, e.cost);
return r;
}
void scc() {
strong_edgeid = Vector<int>();
weak_edgeid = Vector<int>();
strong_id.assign(this->nodes(), 0);
ords = Vector<int>(this->nodes());
rev_node = Vector<Vector<int>>(this->nodes());
rev_edge = Vector<Edge<T>>(this->edges());
for (int id = 0; id < this->edges(); id++) {
rev_node[edge[id].to].pub(id);
rev_edge[id] = Edge<T>(edge[id].to, edge[id].from, edge[id].cost);
}
for (int from = 0; from < this->nodes(); from++)
scc_dfs(from);
ords.reverse();
int cnt = 0;
for (int from : ords)
if (strong_id[from] == -1) {
scc_rdfs(from, cnt);
cnt++;
}
Vector<Edge<T>> strong_edge;
for (int id : strong_edgeid)
strong_edge.pub(edge[id]);
this->clear();
for (Edge<T> e : strong_edge)
add(e.from, e.to, e.cost);
scced = true;
}
Vector<int> weakid() {
if (!scced)
scc();
return weak_edgeid;
};
Vector<int> stronggroup() {
if (!scced)
scc();
return strong_id;
}
Vector<T> bellman(int start) {
Vector<T> d(this->nodes(), std::numeric_limits<T>::max());
d[start] = 0;
bool negloop = false;
Vector<int> updating(1, start);
for (int i = 0; i < this->nodes() && updating.size() > 0; i++) {
Vector<int> toUpdate;
for (int from : updating) {
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
if (d[to] > d[from] + edge[id].cost) {
d[to] = d[from] + edge[id].cost;
toUpdate.pub(to);
if (i == this->nodes() - 1)
d[to] = std::numeric_limits<T>::min();
negloop = true;
}
}
}
updating = toUpdate;
}
if (negloop) {
for (int i = 0; i < this->nodes() && updating.size() > 0; i++) {
Vector<int> toUpdate;
for (int from : updating) {
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
if (d[to] != std::numeric_limits<T>::min()) {
d[to] = std::numeric_limits<T>::min();
toUpdate.pub(to);
}
}
}
updating = toUpdate;
}
}
return d;
}
Vector<int> topsort() {
Vector<int> to_node(this->nodes(), 0);
for (Edge<T> e : edge)
to_node[e.to]++;
Vector<int> sorted;
for (int i = 0; i < this->nodes(); i++)
if (to_node[i] == 0)
sorted.pub(i);
for (int i = 0; i < sorted.size(); i++)
for (int id : node[sorted[i]]) {
if (--to_node[edge[id].to] == 0)
sorted.pub(edge[id].to);
}
if (sorted.size() != this->nodes())
return Vector<int>();
return sorted;
}
};
// math function for Vector, Matrix, Modulo class
template <typename T> T sign(const T &x) { return (x > 0) - (x < 0); }
template <typename T> T abs(const T &x) { return x * sign(x); }
template <typename T> T power(T x, int y) {
T r = x * 0 + 1;
while (y > 0) {
if (y & 1)
r *= x;
x *= x;
y /= 2;
}
return r;
}
bool is_power(int n, int m) {
return n == power(llround(std::pow(n, (double)+1 / m)), m);
}
int _gcd(const int &a, const int &b) { return b ? _gcd(b, a % b) : a; }
int gcd(const int &a, const int &b) {
return _gcd(std::max(a, b), std::min(a, b));
}
int lcm(const int &a, const int &b) { return a / gcd(a, b) * b; }
bool is_prime(int n) {
int m = std::sqrt(n - 1) + 1;
for (int i = 2; i < m + 1; i++)
if (n % i == 0)
return false;
return n > 1;
}
int fact(int n) {
int v = 1;
for (int i = 1; i < n + 1; i++)
v *= i;
return v;
}
Modulo fact(Modulo n) {
Modulo v = 1;
for (int i = 1; i < n + 1; i++)
v *= i;
return v;
}
int parm(int n, int r) {
int v = 1;
for (int i = n - r + 1; i < n + 1; i++)
v *= i;
return v;
}
Modulo parm(Modulo n, Modulo r) {
Modulo v = 1;
for (int i = n.val() - r.val() + 1; i < n.val() + 1; i++)
v *= i;
return v;
}
int comb(int n, int r) {
int v = 1;
r = std::min(r, n - r);
for (int i = 0; i < r; i++) {
v *= n - i;
v /= i + 1;
}
return v;
}
Modulo comb(Modulo n, Modulo r) {
Modulo p = 1, q = 1;
r = std::min(r, n - r);
for (int i = 0; i < r; i++) {
p *= n - i;
q *= i + 1;
}
return p / q;
}
int homo(int m, int n) { return comb(m + n - 1, n); }
Modulo homo(Modulo m, Modulo n) { return comb(m + n - 1, n); }
Map<int, int> factor(int n) {
Map<int, int> f;
for (int i = 2; i * i <= n; i++)
while (n % i == 0) {
f[i]++;
n /= i;
}
if (n != 1)
f[n]++;
return f;
}
// ex. function_find(fname,0,1024,'u',100,output)
#define function_find(FUNCTION_INT_TO_VALUE, INT_RANGE_MIN, INT_RANGE_MAX, \
CHAR_KIND, VALUE_TARGET, INT_AND_OUTPUT) \
{ \
int left = INT_RANGE_MIN - 1; \
int right = INT_RANGE_MAX + 1; \
bool outleft = (CHAR_KIND == 'd' || CHAR_KIND == 'U'); \
int getmin = (CHAR_KIND == 'u' || CHAR_KIND == 'd') ? 1 : -1; \
while (right - left > 1) { \
int mid = left + (right - left) / 2; \
bool OK = FUNCTION_INT_TO_VALUE(mid) * getmin >= VALUE_TARGET * getmin; \
if (outleft == OK) \
left = mid; \
else \
right = mid; \
} \
INT_AND_OUTPUT = outleft ? left : right; \
}
// BinaryIndexedTree class need Vector class
#define BITCalc(OP) \
BinaryIndexedTree<T> operator OP##=(const T &v) { \
if (range.first + 1 == range.second) \
for (int i = range.first; i < size(); i |= (i + 1)) \
t1[i] OP## = v; \
else \
for (int i = range.first; i < size(); i |= (i + 1)) { \
t1[i] OP## = -v * (range.first - 1); \
t2[i] OP## = v; \
} \
for (int i = range.second; i < size(); i |= (i + 1)) { \
t1[i] OP## = v * (range.second - 1); \
t2[i] OP## = -v; \
} \
return *this; \
}
template <typename T> class BinaryIndexedTree {
Vector<T> t1, t2;
std::pair<int, int> range;
public:
T sum(int x) {
T v = T();
for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
v += t1[i] + t2[i] * x;
}
return v;
}
T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
T get(int x) { return sum(x, x + 1); }
void resize(int n) {
t1.resize(n);
t2.resize(n);
}
int size() const { return t1.size(); }
Vector<T> firstVector() const { return t1; }
Vector<T> secondVector() const { return t2; }
BinaryIndexedTree(int n = 0) : range(std::make_pair(0, 0)) { resize(n); }
BinaryIndexedTree(const BinaryIndexedTree &bit)
: t1(bit.firstVector()), t2(bit.secondVector()),
range(std::make_pair(0, -1)) {
resize(bit.size());
}
BinaryIndexedTree<T> &operator=(const BinaryIndexedTree<T> &bit) {
resize(bit.size());
t1 = bit.firstVector();
t2 = bit.secondVector();
return *this;
}
T operator[](int i) const { return get(i); }
BinaryIndexedTree<T> &operator[](std::initializer_list<int> l) {
if (l.size() == 1) {
range.first = *l.begin();
range.second = (*l.begin()) + 1;
}
if (l.size() == 2) {
range.first = *l.begin();
range.second = (*(l.begin() + 1) < 0 ? *(l.begin() + 1) + size() + 1
: *(l.begin() + 1));
}
return *this;
}
BITCalc(+) BITCalc(-) T sum() { return sum(range.first, range.second); }
BinaryIndexedTree<T> operator=(const T &v) {
for (int i = range.first; i < range.second; i++) {
operator[]({i});
operator+=(-get(i) + v);
}
return *this;
}
Vector<T> toVector() {
Vector<T> v(size());
for (int i = 0; i < size(); i++)
v[i] = get(i);
return v;
}
};
// BIT^2
// 10^5 -> NlogN, 3000 -> N^2, 200 -> N^3, 50 -> N^4, 20 -> 2^N
WRITE_FUNCTION_UNDER_HERE
int32_t main() {
INIT;
in(int, n);
fou(i, sqrt(n), n + 1) {
if (n % i == 0) {
out(i + n / i - 2);
return 0;
}
}
} | #pragma GCC optimize( \
"O3,Ofast,inline,fast-math,unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
#define int int64_t
#define double long double
#define INIT \
std::ios_base::sync_with_stdio(0); \
std::cin.tie(0); \
std::cout << std::fixed << std::setprecision(15);
#define WRITE_FUNCTION_UNDER_HERE DEFALLTYPE(VDEF) DEFALLTYPE(PDEF3)
const int MOD = 1e9 + 7;
// using namespace std;const int MAX=std::numeric_limits<int>::max();const int
// MIN=std::numeric_limits<int>::min();const double EPS=1e-7;
#define fou(i, a, n) for (int i = a; i < n; i++)
#define fod(i, a, n) for (int i = n - 1; i >= a; i--)
#define tra(e, v) for (auto &e : v)
#define elif(c) else if (c)
#define fi first
#define se second
#define fir fi
#define sec se.fi
#define thi se.se
#define firs fi
#define seco sec
#define thir thi.fi
#define four thi.se
#define DEFALLTYPE(XDEF) \
XDEF(int, I) \
XDEF(double, D) XDEF(char, C) XDEF(String, S) XDEF(bool, B) XDEF(Modulo, M)
#define VDEF(t, T) \
typedef Vector<t> V##T; \
typedef Vector<V##T> VV##T; \
typedef Vector<VV##T> VVV##T;
#define PDEF(x, X, y, Y, z, Z, w, W) \
typedef std::pair<x, y> P##X##Y; \
VDEF(P##X##Y, P##X##Y) typedef std::pair<x, std::pair<y, z>> P##X##Y##Z; \
VDEF(P##X##Y##Z, P##X##Y##Z) \
typedef std::pair<x, std::pair<y, std::pair<z, w>>> P##X##Y##Z##W; \
VDEF(P##X##Y##Z##W, P##X##Y##Z##W)
#define PDEF1(y, Y, z, Z, w, W) \
PDEF(int, I, y, Y, z, Z, w, W) \
PDEF(double, D, y, Y, z, Z, w, W) PDEF(char, C, y, Y, z, Z, w, W) \
PDEF(String, S, y, Y, z, Z, w, W) PDEF(bool, B, y, Y, z, Z, w, W) \
PDEF(Modulo, M, y, Y, z, Z, w, W)
#define PDEF2(z, Z, w, W) \
PDEF1(int, I, z, Z, w, W) \
PDEF1(double, D, z, Z, w, W) PDEF1(char, C, z, Z, w, W) \
PDEF1(String, S, z, Z, w, W) PDEF1(bool, B, z, Z, w, W) \
PDEF1(Modulo, M, z, Z, w, W)
#define PDEF3(w, W) \
PDEF2(int, I, w, W) \
PDEF2(double, D, w, W) PDEF2(char, C, w, W) PDEF2(String, S, w, W) \
PDEF2(bool, B, w, W) PDEF2(Modulo, M, w, W)
// in, out
#define in(T, ...) \
T __VA_ARGS__; \
_in(__VA_ARGS__)
void _in() {}
template <typename Hd, typename... Tl> void _in(Hd &hd, Tl &&...tl) {
std::cin >> hd;
_in(std::forward<Tl>(tl)...);
}
void out() { std::cout << '\n'; }
template <typename T> void out(const T &a) { std::cout << a << '\n'; }
template <typename Hd, typename... Tl> void out(const Hd &hd, const Tl &...tl) {
std::cout << hd << ' ';
out(tl...);
}
// std::pair io,calc
#define PairCalc(OP) \
template <typename T, typename U, typename V, typename W> \
std::pair<T, U> operator OP(const std::pair<T, U> &l, \
const std::pair<V, W> &r) { \
return std::make_pair(l.first OP r.first, l.second OP r.second); \
} \
template <typename T, typename U, typename V, typename W> \
std::pair<T, U> operator OP##=(std::pair<T, U> &l, \
const std::pair<V, W> &r) { \
return l = l OP r; \
}
template <typename T, typename U>
std::istream &operator>>(std::istream &i, std::pair<T, U> &p) {
i >> p.first >> p.second;
return i;
}
template <typename T, typename U>
std::ostream &operator<<(std::ostream &o, const std::pair<T, U> &p) {
return o << p.first << ' ' << p.second;
}
PairCalc(+) PairCalc(-) PairCalc(*) PairCalc(/) PairCalc(%) PairCalc(<<)
PairCalc(>>) PairCalc(&) PairCalc(|)
PairCalc(^) template <typename T, typename U>
std::pair<T, U> mp(const T &t, const U &u) {
return std::make_pair(t, u);
}
template <typename T, typename U, typename V>
std::pair<T, std::pair<U, V>> mp(const T &t, const U &u, const V &v) {
return std::make_pair(t, std::make_pair(u, v));
}
template <typename T, typename U, typename V, typename W>
std::pair<T, std::pair<U, std::pair<V, W>>> mp(const T &t, const U &u,
const V &v, const W &w) {
return std::make_pair(t, std::make_pair(u, std::make_pair(v, w)));
}
// Vector class
#define VecCalc(OP) \
VT operator OP(const VT &r) const { \
VT v(std::min(size(), r.size())); \
for (int i = 0; i < v.size(); i++) \
v[i] = (*this)[i] OP r[i]; \
return v; \
} \
VT operator OP(const T &r) const { \
VT v(size()); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
} \
VT operator OP##=(const VT &r) { return *this = *this OP r; } \
VT operator OP##=(const T &r) { return *this = *this OP r; }
#define VecComp(OP) \
Vector<int> operator OP(const VT &r) const { \
Vector<int> v(std::min(size(), r.size())); \
for (int i = 0; i < v.size(); i++) \
v[i] = (*this)[i] OP r[i]; \
return v; \
} \
Vector<int> operator OP(const T &r) const { \
Vector<int> v(size()); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
}
#define VVCalc(OP) \
template <typename T, typename U> \
Vector<Vector<T>> operator OP(Vector<Vector<T>> l, const U &r) { \
for (Vector<T> & e : l) \
e OP## = r; \
return l; \
} \
template <typename T, typename U> \
Vector<Vector<T>> operator OP##=(Vector<Vector<T>> &l, const U &r) { \
return l = l OP r; \
}
#define VVComp(OP) \
template <typename T, typename U> \
Vector<Vector<int>> operator OP(const Vector<Vector<T>> &l, const U &r) { \
Vector<Vector<int>> v(l.size()); \
for (int i = 0; i < l.size(); i++) \
v[i] = l[i] OP r; \
return v; \
}
template <typename T> class Vector : public std::vector<T> {
public:
typedef std::vector<T> SVT;
typedef Vector<T> VT;
using SVT::operator[];
using SVT::operator=;
using SVT::assign;
using SVT::begin;
using SVT::end;
using SVT::pop_back;
using SVT::push_back;
using SVT::rbegin;
using SVT::rend;
using SVT::resize;
Vector(const SVT &v) : SVT(v) {}
Vector(int s = 0, T v = T()) {
if (s >= 0)
assign(s, v);
else {
assign(-s, v);
for (T &e : *this)
std::cin >> e;
}
}
Vector(const std::initializer_list<T> &l) : SVT(l) {}
template <typename U> Vector(const std::vector<U> &v) {
for (const T &e : v)
pub(e);
}
template <typename U>
Vector(const __gnu_pbds::tree<
U, __gnu_pbds::null_type, std::less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &t) {
for (const T &e : t)
pub(e);
}
template <typename U> Vector(const __gnu_cxx::rope<U> &r) {
for (const T &e : r)
pub(e);
}
Vector(const std::string &s) {
for (const T &e : s)
pub(e);
}
int size() const { return SVT::size(); }
VT operator[](const std::initializer_list<int> &l) const {
if (l.size() == 2 || l.size() == 3) {
int first = *l.begin(), last = *(l.begin() + 1),
step = (l.size() == 2 ? 1 : *(l.begin() + 2));
last = (last < 0 ? last + size() + 1 : last);
VT v;
for (int i = first; i < last; i += step)
v.pub((*this)[i]);
return v;
}
return *this;
}
VT operator+() const { return *this; }
VT operator-() const {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = -(*this)[i];
return v;
}
VT operator++() {
for (T &e : *this)
e++;
return *this;
}
VT operator--() {
for (T &e : *this)
e--;
return *this;
}
VT operator++(int32_t) {
VT v = *this;
for (T &e : *this)
e++;
return v;
}
VT operator--(int32_t) {
VT v = *this;
for (T &e : *this)
e--;
return v;
}
void out() {
for (const T &e : *this)
std::cout << e << '\n';
}
void outstr() {
for (int i = 0; i < size() - 1; i++)
std::cout << (*this)[i];
std::cout << last() << '\n';
}
void iota() {
for (int i = 0; i < size(); i++)
(*this)[i] = i;
}
T last() const { return (*this)[size() - 1]; }
T &last() { return (*this)[size() - 1]; }
T sum(int l = 0, int r = -1) const {
T v = T();
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v += (*this)[i];
return v;
}
T prod(int l = 0, int r = -1) const {
T v = T() + 1;
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v *= (*this)[i];
return v;
}
T xorsum(int l = 0, int r = -1) const {
T v = T();
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v ^= (*this)[i];
return v;
}
int argmin() const { return std::min_element(begin(), end()) - begin(); }
int argmax() const { return std::max_element(begin(), end()) - begin(); }
T min() const { return (*this)[argmin()]; }
T max() const { return (*this)[argmax()]; }
T dot(const VT &v) const {
T r = T();
for (int i = 0; i < std::min(size(), v.size()); i++)
r += (*this)[i] * v[i];
return r;
}
void pub(const T &v) { SVT::push_back(v); }
void pub(const VT &v) { SVT::insert(end(), v.begin(), v.end()); }
void insert(int i, const T &v) { SVT::insert(begin() + i, v); }
void insert(int i, const VT &v) {
SVT::insert(begin() + i, v.begin(), v.end());
}
void pob() { pop_back(); }
void erase(int i) { SVT::erase(begin() + i); }
void erase(int i, int j) { SVT::erase(begin() + i, begin() + j); }
void erase_unique() { SVT::erase(std::unique(begin(), end()), end()); }
void reverse() { std::reverse(begin(), end()); }
void rotatedown(int i) {
if (i > 0)
std::rotate(begin(), begin() + i, end());
}
void rotateup(int i) {
if (i > 0)
std::rotate(rbegin(), rbegin() + i, rend());
}
void sortup() { std::sort(begin(), end()); }
void sortdown() { std::sort(rbegin(), rend()); }
void cumsum() {
for (int i = 0; i < size() - 1; i++)
(*this)[i + 1] += (*this)[i];
}
void decumsum() {
for (int i = size() - 2; i >= 0; i--)
(*this)[i + 1] -= (*this)[i];
}
T cumsumpart(int l, int r) const {
return (*this)[r - 1] - (l = 0 ? 0 : (*this)[l - 1]);
}
void cumxor() {
for (int i = 0; i < size() - 1; i++)
(*this)[i + 1] ^= (*this)[i];
}
void decumxor() {
for (int i = size() - 2; i >= 0; i--)
(*this)[i + 1] ^= (*this)[i];
}
T cumxorpart(int l, int r) const {
return (*this)[r - 1] ^ (l = 0 ? 0 : (*this)[l - 1]);
}
void bucketsortup() {
int m = min();
Vector<int> b(max() - m + 1);
for (int e : (*this))
b[e - m]++;
b.cumsum();
for (int i = 0; i < b.size(); i++)
for (int j = (i == 0 ? 0 : b[i - 1]); j < b[i]; j++)
(*this)[j] = m + i;
}
void bucketsortdown() {
bucketsortup();
reverse();
}
void compress() {
VT u(*this);
u.sortup();
u.erase_unique();
std::unordered_map<T, int> m;
for (int i = 0; i < u.size(); i++)
m[u[i]] = i;
for (int i = 0; i < size(); i++)
(*this)[i] = m[(*this)[i]];
}
int findupgeq(const T &v) {
return std::lower_bound(begin(), end(), v) - begin();
}
int findupleq(const T &v) {
return std::upper_bound(begin(), end(), v) - begin() - 1;
}
int upcount(const T &v) { return findupleq(v) - findupgeq(v) + 1; }
int finddowngeq(const T &v) {
return size() - (std::lower_bound(rbegin(), rend(), v) - rbegin()) - 1;
}
int finddownleq(const T &v) {
return size() - (std::upper_bound(rbegin(), rend(), v) - rbegin());
}
int downcount(const T &v) { return finddowngeq(v) - finddownleq(v) + 1; }
VecCalc(+) VecCalc(-) VecCalc(*) VecCalc(/) VecCalc(%) VecCalc(<<) VecCalc(>>)
VecCalc(&) VecCalc(|) VecCalc(^) VecComp(==) VecComp(!=) VecComp(>=)
VecComp(<=) VecComp(>) VecComp(<) VT iotaed() const {
VT r(*this);
r.iota();
return r;
}
VT pubed(const T &v) const {
VT r(*this);
r.pub(v);
return r;
}
VT pubed(const VT &v) const {
VT r(*this);
r.pub(v);
return r;
}
VT inserted(int i, const T &v) const {
VT r(*this);
r.insert(i, v);
return r;
}
VT inserted(int i, const VT &v) const {
VT r(*this);
r.insert(i, v);
return r;
}
VT pobed() const {
VT r(*this);
r.pob();
return r;
}
VT erased(int i, int j = -1) const {
VT r(*this);
r.erase(i, j);
return r;
}
VT erase_uniqued() const {
VT r(*this);
r.erase_unique();
return r;
}
VT reversed() const {
VT r(*this);
r.reverse();
return r;
}
VT rotatedowned(int i) const {
VT r(*this);
r.rotatedown(i);
return r;
}
VT rotateuped(int i) const {
VT r(*this);
r.rotateup(i);
return r;
}
VT sortuped() const {
VT r(*this);
r.sortup();
return r;
}
VT sortdowned() const {
VT r(*this);
r.sortdown();
return r;
}
VT cumsumed() const {
VT r(*this);
r.cumsum();
return r;
}
VT decumsumed() const {
VT r(*this);
r.decumsum();
return r;
}
VT cumxored() const {
VT r(*this);
r.cumxor();
return r;
}
VT decumxored() const {
VT r(*this);
r.decumxor();
return r;
}
VT bucketsortuped() const {
VT r(*this);
r.bucketsortup();
return r;
}
VT bucketsortdowned() const {
VT r(*this);
r.bucketsortdown();
return r;
}
VT compressed() const {
VT r(*this);
r.compress();
return r;
}
};
VVCalc(+) VVCalc(-) VVCalc(*) VVCalc(/) VVCalc(%) VVCalc(<<) VVCalc(>>)
VVCalc(&) VVCalc(|) VVCalc(^) VVComp(==) VVComp(!=) VVComp(>=) VVComp(<=)
VVComp(>) VVComp(<) template <typename T, typename U>
Vector<std::pair<T, U>> unite(const Vector<T> &v, const Vector<U> &u) {
int s = std::min(v.size(), u.size());
Vector<std::pair<T, U>> r(s);
for (int i = 0; i < s; i++)
r[i] = std::make_pair(v[i], u[i]);
return r;
}
template <typename T, typename U>
std::pair<Vector<T>, Vector<U>> separate(const Vector<std::pair<T, U>> &p) {
Vector<T> v(p.size());
Vector<U> u(p.size());
for (int i = 0; i < p.size(); i++) {
v[i] = p[i].first;
u[i] = p[i].second;
}
return std::make_pair(v, u);
}
template <typename T>
std::istream &operator>>(std::istream &i, std::vector<T> &v) {
for (T &e : v)
i >> e;
return i;
}
template <typename T>
std::ostream &operator<<(std::ostream &o, const std::vector<T> &v) {
if (v.size() == 0)
return o;
for (int i = 0; i < v.size() - 1; i++)
o << v[i] << ' ';
return o << v[v.size() - 1];
}
Vector<int> baseVector(int n, int b) {
Vector<int> v(64);
for (int &e : v) {
e = n % b;
n /= b;
}
return v;
}
int baseVectorValue(Vector<int> v, int b) {
int n = 0, m = 1;
for (int &e : v) {
n += e * m;
m *= b;
}
return n;
}
// Set class
template <typename T>
class Set
: public __gnu_pbds::tree<T, __gnu_pbds::null_type, std::less<T>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> {
public:
typedef __gnu_pbds::tree<T, __gnu_pbds::null_type, std::less<T>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>
tree;
using tree::operator=;
using tree::begin;
using tree::end;
using tree::erase;
using tree::insert;
using tree::split;
Set(const tree &t) : tree(t) {}
Set(int n = 0) {
for (int i = 0; i < -n; i++) {
T v;
std::cin >> v;
insert(v);
}
}
Set(const std::initializer_list<T> &l) {
for (const T &e : l)
insert(e);
}
int size() const { return tree::size(); }
template <typename U> Set(const std::vector<U> &v) {
for (const T &e : v)
insert(e);
}
template <typename U>
Set(const __gnu_pbds::tree<
U, __gnu_pbds::null_type, std::less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &t) {
for (const T &e : t)
insert(e);
}
template <typename U> Set(const __gnu_cxx::rope<U> &r) {
for (const T &e : r)
insert(e);
}
Set(const std::string &s) {
for (const T &e : s)
insert(e);
}
bool contains(const T &v) const { return tree::find(v) != end(); }
void merge(const tree &s) {
tree t = s;
tree::join(t);
}
int order(const T &v) { return tree::order_of_key(v); }
int rorder(const T &v) { return size() - tree::order_of_key(v) - 1; }
T operator[](int i) const { return *(tree::find_by_order(i)); }
std::pair<Set<T>, Set<T>> splitted(const T &v) const {
Set<T> s = *this, t;
s.split(v, t);
return std::make_pair(s, t);
}
Set<T> &operator<<(const T &v) {
insert(v);
return *this;
}
Set<T> &operator<<(const tree &s) {
merge(s);
return *this;
}
Set<T> &operator>>(const T &v) {
erase(v);
return *this;
}
bool operator>(const T &v) { return contains(v); }
};
template <typename T>
std::ostream &operator<<(std::ostream &o, const Set<T> &s) {
if (s.size() == 0)
return o;
for (int i = 0; i < s.size() - 1; i++)
o << s[i] << ' ';
return o << s[s.size() - 1];
}
// Map class
template <typename T, typename U>
class Map
: public __gnu_pbds::tree<T, U, std::less<T>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> {
public:
typedef __gnu_pbds::tree<T, U, std::less<T>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>
tree;
using tree::operator=;
using tree::operator[];
using tree::begin;
using tree::end;
using tree::erase;
using tree::insert;
using tree::split;
Map(const tree &t) : tree(t) {}
Map(int n = 0) {
for (int i = 0; i < -n; i++) {
T k;
U v;
std::cin >> k >> v;
insert(k, v);
}
}
Map(std::initializer_list<std::pair<T, U>> l) {
for (const std::pair<T, U> &e : l)
insert(e);
}
int size() const { return tree::size(); }
std::pair<T, U> key_value(int i) const { return *tree::find_by_order(i); }
void insert(const T &k, const U &v) { tree::insert(std::make_pair(k, v)); }
bool contains(const T &k) const { return tree::find(k) != end(); }
void merge(const tree &t) {
Map<T, U> n = t;
tree::join(n);
}
int order(const T &k) { return tree::order_of_key(k); }
int rorder(const T &v) { return size() - tree::order_of_key(v) - 1; }
std::pair<Map<T, U>, Map<T, U>> splitted(const T &v) const {
Map<T, U> s = *this, t;
s.split(v, t);
return std::make_pair(s, t);
}
Map<T, U> &operator<<(const std::pair<T, U> &v) {
insert(v);
return *this;
}
Map<T, U> &operator<<(const tree &m) {
merge(m);
return *this;
}
Map<T, U> &operator>>(const T &v) {
erase(v);
return *this;
}
bool operator>(const T &v) { return contains(v); }
};
template <typename T, typename U>
std::ostream &operator<<(std::ostream &o, const Map<T, U> &m) {
if (m.size() == 0)
return o;
for (int i = 0; i < m.size() - 1; i++)
o << m.key_value(i).first << ' ' << m.key_value(i).second << '\n';
return o << m.key_value(m.size() - 1).first << ' '
<< m.key_value(m.size() - 1).second;
}
// Rope class
template <typename T> class Rope : public __gnu_cxx::rope<T> {
typedef __gnu_cxx::rope<T> rope;
public:
using rope::insert;
using rope::replace;
using rope::operator[];
using rope::operator=;
using rope::begin;
using rope::end;
using rope::rbegin;
using rope::rend;
Rope(const rope &r) : rope(r) {}
Rope(int n = 0) {
for (int i = 0; i < -n; i++) {
T v;
std::cin >> v;
pub(v);
}
}
Rope(const std::initializer_list<T> &l) {
for (const T &e : l)
pub(e);
}
template <typename U> Rope(const std::vector<U> &v) {
for (const T &e : v)
pub(e);
}
template <typename U>
Rope(const __gnu_pbds::tree<
U, __gnu_pbds::null_type, std::less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &t) {
for (const T &e : t)
pub(e);
}
template <typename U> Rope(const __gnu_cxx::rope<U> &r) {
for (const T &e : r)
pub(e);
}
Rope(const std::string &s) {
for (const T &e : s)
pub(e);
}
int size() const { return rope::size(); }
void pub(const T &v) { insert(size(), v); }
void erase(int i) { rope::erase(i, 1); }
void erase(int i, int j) { rope::erase(i, j - i); }
void pob() { erase(size() - 1); }
void resize(int s, const T &v = T()) {
while (s < size())
pob();
while (s > size())
pub(v);
}
Rope<T> operator[](const std::initializer_list<int> &l) const {
if (l.size() == 2)
return rope::substr(*l.begin(), *(l.begin() + 1) - *l.begin());
if (l.size() == 3) {
int first = *l.begin(), last = *(l.begin() + 1), step = *(l.begin() + 2);
last = (last < 0 ? last + size() + 1 : last);
Rope<T> v;
for (int i = first; i < last; i += step)
v.pub((*this)[i]);
return v;
}
return *this;
}
Rope<T> operator+() const { return *this; }
Rope<T> operator-() const {
Rope<T> v;
for (const T &e : *this)
v.pub(-e);
return v;
}
Rope<T> operator++() {
for (int i = 0; i < size(); i++)
replace(i, (*this)[i] + 1);
return *this;
}
Rope<T> operator--() {
for (int i = 0; i < size(); i++)
replace(i, (*this)[i] - 1);
return *this;
}
Rope<T> operator++(int32_t) {
Rope<T> v = *this;
for (int i = 0; i < size(); i++)
replace(i, (*this)[i] + 1);
return v;
}
Rope<T> operator--(int32_t) {
Rope<T> v = *this;
for (int i = 0; i < size(); i++)
replace(i, (*this)[i] - 1);
return v;
}
void out() {
for (const T &e : *this)
std::cout << e << '\n';
}
void outvec() {
for (int i = 0; i < size() - 1; i++)
std::cout << (*this)[i] << ' ';
std::cout << last() << '\n';
}
T last() const { return (*this)[size() - 1]; }
T sum(int l = 0, int r = -1) const {
T v = T();
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v += (*this)[i];
return v;
}
T prod(int l = 0, int r = -1) const {
T v = T() + 1;
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v *= (*this)[i];
return v;
}
T xorsum(int l = 0, int r = -1) const {
T v = T();
r = (r < 0 ? r + size() + 1 : r);
for (int i = l; i < r; i++)
v ^= (*this)[i];
return v;
}
T cumsumpart(int l, int r) const {
return (*this)[r - 1] - (l = 0 ? 0 : (*this)[l - 1]);
}
T cumxorpart(int l, int r) const {
return (*this)[r - 1] ^ (l = 0 ? 0 : (*this)[l - 1]);
}
int findupgeq(const T &v) {
return std::lower_bound(begin(), end(), v) - begin();
}
int findupleq(const T &v) {
return std::upper_bound(begin(), end(), v) - begin() - 1;
}
int upcount(const T &v) { return findupleq(v) - findupgeq(v) + 1; }
int finddowngeq(const T &v) {
return size() - (std::lower_bound(rbegin(), rend(), v) - rbegin()) - 1;
}
int finddownleq(const T &v) {
return size() - (std::upper_bound(rbegin(), rend(), v) - rbegin());
}
int downcount(const T &v) { return finddowngeq(v) - finddownleq(v) + 1; }
};
// String class
class String : public std::string {
typedef std::string SS;
public:
using SS::operator[];
using SS::operator=;
using SS::assign;
using SS::begin;
using SS::end;
using SS::pop_back;
using SS::push_back;
using SS::rbegin;
using SS::rend;
using SS::resize;
using SS::operator+=;
String() {}
String(const char *s) : SS(s) {}
String(char c) : SS(1, c) {}
String(int i, char c) : SS(i, c) {}
String(int32_t i) : SS(std::to_string(i)) {}
String(int64_t i) : SS(std::to_string(i)) {}
String(const std::vector<char> &v) {
for (char e : v)
pub(e);
}
String(const __gnu_pbds::tree<
char, __gnu_pbds::null_type, std::less<char>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &t) {
for (char e : t)
pub(e);
}
String(const __gnu_cxx::rope<char> &r) {
for (char e : r)
pub(e);
}
void pub(char c) { push_back(c); }
void pub(String s) {
for (char c : s)
push_back(c);
}
void pob() { SS::pop_back(); }
String operator[](const std::initializer_list<int> &l) const {
if (l.size() == 2 || l.size() == 3) {
int first = *l.begin(), last = *(l.begin() + 1),
step = (l.size() == 2 ? 1 : *(l.begin() + 2));
last = (last < 0 ? last + size() + 1 : last);
String s;
for (int i = first; i < last; i += step)
s.pub((*this)[i]);
return s;
}
return *this;
}
String operator+(const String &s) const {
String r(*this);
r.pub(s);
return r;
}
char last() const { return (*this)[size() - 1]; }
String &operator<<(const String &s) {
pub(s);
return *this;
}
char &last() { return (*this)[size() - 1]; }
int size() const { return SS::size(); }
int32_t toInt() const { return std::stoi(*this); }
int64_t toLong() const { return std::stol(*this); }
std::vector<int> find(const String &s) const {
Vector<int> v;
int i = SS::find(s);
while (i != SS::npos) {
v.pub(i);
i = SS::find(s, i + s.size());
}
return v;
}
std::vector<int> find_dup(const String &s) const {
Vector<int> v;
int i = SS::find(s);
while (i != SS::npos) {
v.pub(i);
i = SS::find(s, i + 1);
}
return v;
}
void reverse() { std::reverse(begin(), end()); }
void insert(int i, const String &s) { SS::insert(i, s); }
void erase(int i) { SS::erase(i, 1); }
void erase(int i, int j) { SS::erase(i, j - i); }
void erase_unique() { SS::erase(std::unique(begin(), end()), end()); }
void rotatedown(int i) { std::rotate(begin(), begin() + i, end()); }
void rotateup(int i) { std::rotate(rbegin(), rbegin() + i, rend()); }
void sortup() {
int m = *std::min_element(begin(), end());
int M = *std::max_element(begin(), end());
std::vector<char> b(M - m + 1);
for (char e : *this)
b[e - m]++;
for (int i = 0; i < b.size() - 1; i++)
b[i + 1] += b[i];
for (int i = 0; i < b.size(); i++)
for (int j = (i == 0 ? 0 : b[i - 1]); j < b[i]; j++)
(*this)[j] = m + i;
}
void sortdown() {
sortup();
reverse();
}
String reversed() const {
String r(*this);
r.reverse();
return r;
}
String inserted(int i, const String &s) const {
String r(*this);
r.insert(i, s);
return r;
}
String erased(int i) const {
String r(*this);
r.erase(i);
return r;
}
String erased(int i, int j) const {
String r(*this);
r.erase(i, j);
return r;
}
String erase_uniqued() const {
String r(*this);
r.erase_unique();
return r;
}
String rotatedowned(int i) const {
String r(*this);
r.rotatedown(i);
return r;
}
String rotateuped(int i) const {
String r(*this);
r.rotateup(i);
return r;
}
String sortuped() const {
String r(*this);
r.sortup();
return r;
}
String sortdowned() const {
String r(*this);
r.sortdown();
return r;
}
};
// Trie class
class Trie
: public __gnu_pbds::trie<std::string, __gnu_pbds::null_type,
__gnu_pbds::trie_string_access_traits<>,
__gnu_pbds::pat_trie_tag,
__gnu_pbds::trie_prefix_search_node_update> {
public:
typedef __gnu_pbds::trie<std::string, __gnu_pbds::null_type,
__gnu_pbds::trie_string_access_traits<>,
__gnu_pbds::pat_trie_tag,
__gnu_pbds::trie_prefix_search_node_update>
tree;
using tree::operator=;
using tree::begin;
using tree::end;
using tree::erase;
using tree::insert;
using tree::split;
Trie(const tree &t) : tree(t) {}
Trie(int n = 0) {
for (int i = 0; i < -n; i++) {
std::string v;
std::cin >> v;
insert(v);
}
}
Trie(const std::initializer_list<std::string> &l) {
for (const std::string &e : l)
insert(e);
}
int size() const { return tree::size(); }
bool contains(const std::string &s) const { return tree::find(s) != end(); }
void merge(const tree &s) {
tree t = s;
tree::join(t);
}
std::pair<Trie, Trie> splitted(const std::string &s) const {
Trie r = *this, t;
r.split(s, t);
return std::make_pair(r, t);
}
void eraseall(const std::string &s) {
std::vector<std::string> match = (*this)[s];
for (const std::string &e : match)
erase(e);
}
void exploit(const std::string &s) {
std::vector<std::string> match;
std::pair<tree::const_iterator, tree::const_iterator> range =
prefix_range(s);
for (tree::const_iterator it = begin(); it != range.first; ++it)
match.push_back(*it);
for (tree::const_iterator it = range.second; it != end(); ++it)
match.push_back(*it);
for (const std::string &e : match)
erase(e);
}
Trie &operator<<(const std::string &s) {
insert(s);
return *this;
}
Trie &operator<<(const tree &t) {
merge(t);
return *this;
}
Trie &operator>>(const std::string &s) {
erase(s);
return *this;
}
bool operator>(const std::string &s) { return contains(s); }
std::vector<std::string> operator[](const std::string &s) const {
std::vector<std::string> match;
std::pair<tree::const_iterator, tree::const_iterator> range =
prefix_range(s);
for (tree::const_iterator it = range.first; it != range.second; ++it)
match.push_back(*it);
return match;
}
};
std::ostream &operator<<(std::ostream &o, const Trie &t) {
auto it = t.begin();
for (int i = 0; i < t.size() - 1; i++, it++)
o << *it << '\n';
return o << *it;
}
// Modulo class
#define ModCalc(OP) \
Modulo operator OP(const Modulo &l, const Modulo &r) { \
return Modulo(l.val() OP r.val()); \
} \
Modulo operator OP(const Modulo &l, const int64_t &r) { \
return Modulo(l.val() OP Modulo(r)); \
} \
Modulo operator OP(const int64_t &l, const Modulo &r) { \
return Modulo(l) OP r.val(); \
} \
Modulo operator OP##=(Modulo &l, const Modulo &r) { return l = l OP r; } \
Modulo operator OP##=(Modulo &l, const int64_t &r) { return l = l OP r; } \
int64_t operator OP##=(int64_t &l, const Modulo &r) { \
return l = l OP r.val(); \
}
#define ModComp(OP) \
bool operator OP(const Modulo &l, const Modulo &r) { \
return l.val() OP r.val(); \
} \
bool operator OP(const Modulo &l, const int64_t &r) { return l.val() OP r; } \
bool operator OP(const int64_t &l, const Modulo &r) { return l OP r.val(); }
class Modulo {
int64_t value;
public:
Modulo() : value(0) {}
Modulo(const int64_t &v) {
value = v;
normalize();
}
void normalize() { value = (value % mod() + mod()) % mod(); }
Modulo(const Modulo &m) : value(m.val()) {}
Modulo inv() const {
int64_t a = value;
int64_t b = mod(), u = 0, v = 1;
while (a > 1) {
u -= b / a * v;
b %= a;
std::swap(a, b);
std::swap(u, v);
}
return Modulo(v);
}
int64_t val() const { return value; }
int64_t &val() { return value; }
Modulo &operator=(const Modulo &m) {
value = m.val();
return *this;
}
int64_t mod() const { return MOD; }
Modulo &operator=(const int64_t &v) {
value = v;
return *this;
}
Modulo operator++() { return Modulo(++value); }
Modulo operator--() { return Modulo(--value); }
Modulo operator++(int32_t) { return Modulo(value++); }
Modulo operator--(int32_t) { return Modulo(value--); }
Modulo operator+() const { return *this; }
Modulo operator-() const { return Modulo(-value); }
Modulo power(int64_t i) const {
int64_t x = value;
int64_t r = 1;
while (i > 0) {
if (i & 1)
r = r * x % mod();
x = x * x % mod();
i /= 2;
}
return Modulo(r);
}
};
std::ostream &operator<<(std::ostream &o, const Modulo &m) {
return o << m.val();
}
std::istream &operator>>(std::istream &i, Modulo &m) {
i >> m.val();
m.normalize();
return i;
}
ModCalc(+) ModCalc(-) ModCalc(*) ModCalc(<<) ModCalc(>>) ModCalc(&) ModCalc(|)
ModCalc(^) ModComp(==) ModComp(!=) ModComp(>=) ModComp(<=) ModComp(>)
ModComp(<) Modulo
operator/(const Modulo &l, const Modulo &r) {
return Modulo(l.val() * r.inv());
}
Modulo operator/(const Modulo &l, const int64_t &r) {
return Modulo(l.val() * Modulo(r).inv());
}
Modulo operator/(const int64_t &l, const Modulo &r) {
return Modulo(Modulo(l) * r.inv());
}
// Matrix class need Vector class
#define MatCalc(OP) \
Matrix<T> operator OP(const VVT &r) const { \
Matrix<T> v(std::min(size(), r.size())); \
for (int i = 0; i < v.size(); i++) \
v[i] = (*this)[i] OP r[i]; \
v.rect(); \
return v; \
} \
Matrix<T> operator OP(const VT &r) const { \
Matrix<T> v(std::min(size(), r.size())); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
} \
Matrix<T> operator OP(const T &r) const { \
Matrix<T> v(size()); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
} \
Matrix<T> operator OP##=(const VVT &r) { return *this = *this OP r; } \
Matrix<T> operator OP##=(const VT &r) { return *this = *this OP r; } \
Matrix<T> operator OP##=(const T &r) { return *this = *this OP r; }
#define MatComp(OP) \
Matrix<int> operator OP(const VVT &r) const { \
Matrix<int> v(std::min(size(), r.size())); \
for (int i = 0; i < v.size(); i++) \
v[i] = (*this)[i] OP r[i]; \
r.rect(); \
return v; \
} \
Matrix<int> operator OP(const VT &r) const { \
Matrix<int> v(size()); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
} \
Matrix<int> operator OP(const T &r) const { \
Matrix<int> v(size()); \
for (int i = 0; i < size(); i++) \
v[i] = (*this)[i] OP r; \
return v; \
}
template <typename T> class Matrix : private Vector<Vector<T>> {
typedef Vector<T> VT;
typedef Vector<VT> VVT;
using VVT::erase;
using VVT::insert;
using VVT::pob;
using VVT::pub;
public:
using VVT::operator+;
using VVT::operator-;
using VVT::operator++;
using VVT::operator--;
using VVT::operator[];
using VVT::assign;
using VVT::begin;
using VVT::end;
using VVT::last;
using VVT::out;
using VVT::rbegin;
using VVT::rend;
using VVT::resize;
using VVT::size;
Matrix() {}
Matrix(const VVT &m) : VVT(m) { rect(); }
Matrix(const VT &v) : VVT(1, v) {}
Matrix(int c, int r = 0, T v = T()) {
if (c >= 0 && r >= 0)
assign(c, r, v);
else {
assign(-c, -r, v);
for (VT &e : *this)
std::cin >> e;
}
}
Matrix(std::pair<int, int> p, T v = T()) { assign(p.first, p.second, v); }
Matrix(const std::initializer_list<std::initializer_list<T>> &l) {
resize(l.size());
for (int i = 0; i < size(); i++)
(*this)[i] = *(l.begin() + i);
rect();
}
template <typename U> Matrix(const Matrix<U> &v) {
resize(v.size());
for (int i = 0; i < size(); i++)
(*this)[i] = v[i];
}
void rect() {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = (*this)[i].size();
resize(size(), v.max());
}
std::pair<int, int> shape() const {
return std::make_pair(size(), size() == 0 ? 0 : (*this)[0].size());
}
bool isEmpty() { return shape().first == 0 && shape().second == 0; }
void resize(int c, int r, T v = T()) {
resize(c);
for (VT &e : *this)
e.resize(r, v);
}
void resize(std::pair<int, int> p, T v = T()) {
resize(p.first, p.second, v);
}
void assign(int c, int r, T v) { assign(c, VT(r, v)); }
void assign(std::pair<int, int> p, const T &v) {
assign(p.first, p.second, v);
}
T operator[](std::pair<int, int> p) const {
return (*this)[p.first][p.second];
}
T &operator[](std::pair<int, int> p) { return (*this)[p.first][p.second]; }
Matrix<T> operator[](const std::pair<std::initializer_list<T>,
std::initializer_list<T>> &p) const {
int columnFirst = *p.first.begin(), columnLast = *(p.first.begin() + 1),
columnStep = (p.first.size() == 2 ? 1 : *(p.first.begin() + 2));
columnLast = (columnLast < 0 ? columnLast + size() + 1 : columnLast);
int rowFirst = *p.second.begin(), rowLast = *(p.second.begin() + 1),
rowStep = (p.second.size() == 2 ? 1 : *(p.second.begin() + 2));
rowLast = (rowLast < 0 ? rowLast + size() + 1 : rowLast);
Matrix<T> m;
for (int i = columnFirst; i < columnLast; i += columnStep) {
VT v;
for (int j = rowFirst; j < rowLast; j += rowStep)
v.pub((*this)[i][j]);
m.vstack(v);
}
return m;
}
VT vector() const {
VT v;
for (const VT &e : *this)
v.pub(e);
return v;
}
T sum(int cl = 0, int rl = 0, int cr = -1, int rr = -1) const {
T v = T();
cr = (cr < 0 ? cr + size() + 1 : cr);
for (int i = cl; i < cr; i++)
v += (*this)[i].sum(rl, rr);
return v;
}
T prod(int cl = 0, int rl = 0, int cr = -1, int rr = -1) const {
T v = T() + 1;
cr = (cr < 0 ? cr + size() + 1 : cr);
for (int i = cl; i < cr; i++)
v *= (*this)[i].prod(rl, rr);
return v;
}
T xorsum(int cl = 0, int rl = 0, int cr = -1, int rr = -1) const {
T v = T();
cr = (cr < 0 ? cr + size() + 1 : cr);
for (int i = cl; i < cr; i++)
v ^= (*this)[i].xorsum(rl, rr);
return v;
}
Matrix<T> &operator=(const VVT &v) {
resize(v.size());
for (int i = 0; i < size(); i++)
(*this)[i] = v[i];
rect();
}
Matrix<T> &operator=(const VT &v) {
assign(1, v);
return *this;
}
std::pair<int, int> argmin() const {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = (*this)[i].min();
int m = v.argmin();
return std::make_pair(m, (*this)[m].argmin());
}
std::pair<int, int> argmax() const {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = (*this)[i].max();
int m = v.argmax();
return std::make_pair(m, (*this)[m].argmax());
}
T min() const {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = (*this)[i].min();
return v.min();
}
T max() const {
VT v(size());
for (int i = 0; i < size(); i++)
v[i] = (*this)[i].max();
return v.max();
}
void dot(Matrix<T> v) {
Matrix<T> m(*this);
assign(shape().first, v.shape().second, 0);
for (int i = 0; i < shape().first; i++)
for (int j = 0; j < shape().second; j++)
for (int k = 0; k < std::min(m.shape().second, v.shape().first); k++)
(*this)[i][j] += m[i][k] * v[k][j];
}
void vstack(const VT &v) {
if (shape().second < v.size() && size() > 0) {
pub(v[{0, shape().second}]);
} else {
pub(v);
last().resize(shape().second);
}
}
void vstack(const VVT &v) {
for (const VT &e : v)
vstack(e);
}
void hstack(const VT &v) {
if (size() == 0)
resize(v.size());
for (int i = 0; i < std::min(size(), v.size()); i++)
(*this)[i].pub(v[i]);
resize(shape());
}
void hstack(const VVT &v) {
if (size() == 0)
resize(v.size());
for (int i = 0; i < std::min(size(), v.size()); i++)
(*this)[i].pub(v[i]);
rect();
}
void vpob() { pob(); }
void hpob() {
for (VT &e : *this)
e.pob();
}
void verase(int i) { erase(i); }
void verase(int i, int j) { erase(i, j); }
void herase(int i) {
for (VT &e : *this)
e.erase(i);
}
void herase(int i, int j) {
for (VT &e : *this)
e.erase(i, j);
}
void vinsert(int i, const VT &v) {
if (isEmpty())
(*this) = v;
else {
insert(i, v[{0, std::min(shape().second, v.size()), 1}]);
if (i == 0)
rect();
else
(*this)[i].resize(shape().second);
}
}
void vinsert(int i, const Matrix<T> &v) {
if (isEmpty())
(*this) = v;
else {
insert(
i,
v[{{0, -1, 1}, {0, std::min(shape().second, v.shape().second), 1}}]);
if (i == 0)
rect();
else
(*this)[i].resize(shape().second);
}
}
void hinsert(int i, const VT &v) {
if (isEmpty())
(*this) = v;
else
for (int j = 0; j < std::min(size(), v.size()); j++)
(*this)[j].insert(i, v[j]);
for (int j = std::min(size(), v.size()); j < size(); j++)
(*this)[j].insert(i, 0);
}
void hinsert(int i, const Matrix<T> &v) {
if (isEmpty())
(*this) = v;
else
for (int j = 0; j < std::min(size(), v.size()); j++)
(*this)[j].insert(i, v[j]);
for (int j = std::min(size(), v.size()); j < size(); j++)
(*this)[j].resize(v.shape().second);
}
void transpose() {
std::pair<int, int> s = shape();
if (s.first < s.second)
for (int i = 0; i < s.second - s.first; i++)
pub(VT(s.first));
else
for (int i = 0; i < s.second; i++)
(*this)[i].resize(s.first);
for (int i = 0; i < std::min(s.first, s.second); i++)
for (int j = i + 1; j < size(); j++)
std::swap((*this)[i][j], (*this)[j][i]);
resize(s.second, s.first);
}
void power(int n) {
Matrix<T> m(*this);
assign(size(), size(), 0);
for (int i = 0; i < size(); i++)
(*this)[i][i] = 1;
while (n > 0) {
if (n & 1)
dot(m);
m.dot(m);
n /= 2;
}
}
void cumsum() {
VVT::cumsum();
for (VT &e : *this)
e.cumsum();
}
void decumsum() {
VVT::decumsum();
for (VT &e : *this)
e.decumsum();
}
T cumsumpart(int cl, int rl, int cr, int rr) const {
return (*this)[cr - 1][rr - 1] - (cl == 0 ? 0 : (*this)[cl - 1][rr - 1]) -
(rl == 0 ? 0 : (*this)[cr - 1][rl - 1]) +
((cl == 0 || rl == 0) ? 0 : (*this)[cl - 1][rl - 1]);
}
void cumxor() {
VVT::cumxor();
for (VT &e : *this)
e.cumxor();
}
void decumxor() {
VVT::decumxor();
for (VT &e : *this)
e.decumxor();
}
T cumxorpart(int cl, int rl, int cr, int rr) const {
return (*this)[cr - 1][rr - 1] ^ (cl == 0 ? 0 : (*this)[cl - 1][rr - 1]) ^
(rl == 0 ? 0 : (*this)[cr - 1][rl - 1]) ^
((cl == 0 || rl == 0) ? 0 : (*this)[cl - 1][rl - 1]);
}
void compress() {
VT u = vector();
u.sortup();
u.erase_unique();
std::unordered_map<T, int> m;
for (int i = 0; i < u.size(); i++)
m[u[i]] = i;
for (int i = 0; i < shape().first; i++)
for (int j = 0; j < shape().second; j++)
(*this)[i][j] = m[(*this)[i][j]];
}
MatCalc(+) MatCalc(-) MatCalc(*) MatCalc(/) MatCalc(%) MatCalc(<<) MatCalc(>>)
MatCalc(&) MatCalc(|) MatCalc(^) MatComp(==) MatComp(!=) MatComp(>=)
MatComp(<=) MatComp(>)
MatComp(<) Matrix<T> vstacked(const VT &v) const {
Matrix<T> r(*this);
r.vstack(v);
return r;
}
Matrix<T> vstacked(const VVT &v) const {
Matrix<T> r(*this);
r.vstack(v);
return r;
}
Matrix<T> hstacked(const VT &v) const {
Matrix<T> r(*this);
r.hstack(v);
return r;
}
Matrix<T> hstacked(const VVT &v) const {
Matrix<T> r(*this);
r.hstack(v);
return r;
}
Matrix<T> vpobed() const {
Matrix<T> r(*this);
r.vpob();
return r;
}
Matrix<T> hpobed() const {
Matrix<T> r(*this);
r.hpob();
return r;
}
Matrix<T> verased(int i) const {
Matrix<T> r(*this);
r.verase(i);
return r;
}
Matrix<T> verased(int i, int j) const {
Matrix<T> r(*this);
r.verase(i, j);
return r;
}
Matrix<T> herased(int i) const {
Matrix<T> r(*this);
r.herase(i);
return r;
}
Matrix<T> herased(int i, int j) const {
Matrix<T> r(*this);
r.herase(i, j);
return r;
}
Matrix<T> vinserted(int i, const VT &v) const {
Matrix<T> r(*this);
r.vinsert(i, v);
return r;
}
Matrix<T> vinserted(int i, const Matrix<T> &v) const {
Matrix<T> r(*this);
r.vinsert(i, v);
return r;
}
Matrix<T> hinserted(int i, const VT &v) const {
Matrix<T> r(*this);
r.hinsert(i, v);
return r;
}
Matrix<T> hinserted(int i, const Matrix<T> &v) const {
Matrix<T> r(*this);
r.hinsert(i, v);
return r;
}
Matrix<T> transposed() const {
Matrix<T> r(*this);
r.transpose();
return r;
}
Matrix<T> powered(int n) const {
Matrix<T> r(*this);
r.power(n);
return r;
}
Matrix<T> cumsumed() const {
Matrix<T> r(*this);
r.cumsum();
return r;
}
Matrix<T> decumsumed() const {
Matrix<T> r(*this);
r.decumsum();
return r;
}
Matrix<T> cumxored() const {
Matrix<T> r(*this);
r.cumxor();
return r;
}
Matrix<T> decumxored() const {
Matrix<T> r(*this);
r.decumxor();
return r;
}
Matrix<T> compressed() const {
Matrix<T> r(*this);
r.compress();
return r;
}
};
// FFT, NNT need Vector Modulo class
std::unordered_map<int, int> MOD_ROOT = {
{1224736769, 3}, {1053818881, 7}, {1051721729, 6}, {1045430273, 3},
{1012924417, 5}, {1007681537, 3}, {1004535809, 3}, {998244353, 3},
{985661441, 3}, {976224257, 3}, {975175681, 17}, {962592769, 7},
{950009857, 7}, {943718401, 7}, {935329793, 3}, {924844033, 5},
{469762049, 3}, {167772161, 3}};
Vector<std::complex<double>> fft(Vector<std::complex<double>> a,
bool inverse = false) {
int n = a.size(), h = std::log2(n);
for (int i = 0; i < n; i++) {
int j = 0;
for (int k = 0; k < h; k++)
j |= (i >> k & 1) << (h - 1 - k);
if (i < j)
std::swap(a[i], a[j]);
}
for (int b = 1; b < n; b *= 2) {
for (int j = 0; j < b; j++) {
std::complex<double> w =
std::polar(1.0, (2 * M_PI) / (2 * b) * j * (inverse ? 1 : -1));
for (int k = 0; k < n; k += b * 2) {
std::complex<double> s = a[j + k], t = a[j + k + b] * w;
a[j + k] = s + t;
a[j + k + b] = s - t;
}
}
}
a /= std::complex<double>(inverse ? n : 1);
return a;
}
Vector<Modulo> ntt(Vector<Modulo> a, bool inverse = false) {
int n = a.size(), h = std::log2(n);
Modulo root = MOD_ROOT[MOD];
for (int i = 0; i < n; i++) {
int j = 0;
for (int k = 0; k < h; k++)
j |= (i >> k & 1) << (h - 1 - k);
if (i < j)
std::swap(a[i], a[j]);
}
for (int b = 1; b < n; b *= 2) {
Modulo w = root.power((a[0].mod() - 1) / (b * 2));
if (inverse)
w = w.inv();
for (int j = 0; j < n; j += b * 2) {
Modulo wn = 1;
for (int k = 0; k < b; k++) {
Modulo s = a[j + k + 0], t = a[j + k + b] * wn;
a[j + k] = s + t;
a[j + k + b] = s - t;
wn = wn * w;
}
}
}
if (inverse) {
Modulo v = Modulo(n).inv();
a *= v;
}
return a;
}
Vector<double> convolve(Vector<double> a, Vector<double> b) {
if (a.size() == 0 || b.size() == 0)
return Vector<double>();
int s = a.size() + b.size() - 1, t = std::pow(2, std::ceil(std::log2(s)));
a.resize(t);
b.resize(t);
Vector<std::complex<double>> A = fft(a), B = fft(b);
A *= B;
A = fft(A, true);
a.resize(s);
for (int i = 0; i < s; i++)
a[i] = A[i].real();
return a;
}
Vector<int> convolve(Vector<int> a, Vector<int> b) {
return convolve(Vector<double>(a), Vector<double>(b)) + 0.5;
}
Vector<Modulo> convolve(Vector<Modulo> a, Vector<Modulo> b) {
if (a.size() == 0 || b.size() == 0)
return Vector<Modulo>();
int s = a.size() + b.size() - 1, t = std::pow(2, std::ceil(std::log2(s)));
a.resize(t);
b.resize(t);
Vector<Modulo> A = ntt(a), B = ntt(b);
A *= B;
a = ntt(A, true);
a.resize(s);
return a;
}
// MultipleVector class need Vector, Modulo class
class MultipleVector {
Vector<Modulo> vfact, ifact, pows;
Modulo p;
public:
MultipleVector(int n = 0, int _p = 1)
: vfact(Vector<Modulo>(1, 1)), ifact(Vector<Modulo>(1, 1)),
pows(Vector<Modulo>(1, 1)), p(_p) {
resize(n);
}
void resize(int n) {
if (vfact.size() >= n)
return;
int s = vfact.size();
vfact.resize(n + 1);
for (int i = s; i <= n; i++)
vfact[i] = i * vfact[i - 1];
ifact.resize(n + 1);
ifact[n] = vfact[n].inv();
for (int i = n; i > s; i--)
ifact[i - 1] = i * ifact[i];
pows.resize(n + 1);
for (int i = s; i <= n; i++)
pows[i] = p * pows[i - 1];
}
Modulo fact(Modulo n) {
resize(n.val());
return vfact[n.val()];
}
Vector<Modulo> facts(Modulo n) {
resize(n.val());
return vfact[{0, n.val(), 1}];
}
Modulo factinv(Modulo n) {
resize(n.val());
return ifact[n.val()];
}
Vector<Modulo> factinvs(Modulo n) {
resize(n.val());
return ifact[{0, n.val(), 1}];
}
Modulo power(Modulo n) {
resize(n.val());
return pows[n.val()];
}
Vector<Modulo> powers(Modulo n) {
resize(n.val());
return pows[{0, n.val(), 1}];
}
Modulo parm(Modulo n, Modulo r) {
if (n < r)
return Modulo();
resize(n.val());
return vfact[n.val()] * ifact[(n - r).val()];
}
Modulo comb(Modulo n, Modulo r) {
if (n < r)
return Modulo();
resize(n.val());
return vfact[n.val()] * ifact[(n - r).val()] * ifact[r.val()];
}
Modulo homo(Modulo n, Modulo r) { return comb(n + r - 1, r); }
};
// Eratos class need Vector class
class Eratos {
Vector<int> divs, prms, ords;
int order_max;
public:
Eratos(int n = 0) : order_max(0) { resize(n); }
int size() const { return divs.size(); }
void resize(int n) {
if (size() >= n)
return;
int m = size();
divs.resize(n + 1);
for (int i = m; i < 2; i++)
if (i < size())
divs[i] = -1;
int r = std::sqrt(n - 1) + 1;
for (int i = 2; i < r; i++)
if (divs[i] == 0)
for (int j = std::max((m - 1) / i + 1, (int)2) * i; j <= n; j += i)
divs[j] = (divs[j] == 0 ? i : divs[j]);
for (int i = m; i <= n; i++)
if (divs[i] == 0)
prms.pub(i);
ords.resize(n + 1);
for (int i = m; i <= n; i++)
ords[i] = (divs[i] == 0 ? ++order_max : 0);
}
Vector<int> divisors() const { return divs; }
Vector<int> primes() const { return prms; }
Vector<int> orders() const { return ords; }
};
// Unionfind class need Vector class
template <typename T> class Unionfind {
Vector<int> p;
Vector<T> w;
int sign(int x) { return (x > 0) - (x < 0); }
int get(int x) {
if (p[x] == (x + 1))
return x + 1;
else {
int parent = sign(p[x]) * get(abs(p[x]) - 1);
w[x] += w[abs(p[x]) - 1];
return p[x] = parent;
}
}
void get(int &x, int &y) {
x = get(x);
y = get(y);
}
int weight(int x) {
get(x);
return w[x];
}
public:
Unionfind(int n = 0) { resize(n); }
void resize(const int &n) {
p.resize(n);
for (int i = w.size(); i < p.size(); i++)
p[i] = i + 1;
w.resize(n);
}
int size() const { return p.size(); }
bool unite(int x, int y, T vv = T()) {
if (vv < 0) {
std::swap(x, y);
vv *= -1;
}
vv += weight(x) - weight(y);
get(x, y);
if (abs(x) == abs(y))
return vv == weight(x) - weight(y);
p[abs(y) - 1] = x * sign(y);
w[abs(y) - 1] = vv;
return true;
}
bool separate(int x, int y, T vv = T()) {
if (vv < 0) {
std::swap(x, y);
vv *= -1;
}
vv += weight(x) - weight(y);
get(x, y);
if (abs(x) == abs(y))
return vv == weight(x) - weight(y);
p[abs(y) - 1] = -x * sign(y);
w[abs(y) - 1] = vv;
return true;
}
int same(int x, int y) {
get(x, y);
return (x == y) - (x == -y);
}
Vector<int> same(int x) {
Vector<int> v(size());
for (int i = 0; i < size(); i++)
v[i] = same(x, i);
return v;
}
T diff(int x, int y) { return sign(same(x, y)) * (weight(y) - weight(x)); }
Vector<T> diff(int x) {
Vector<int> v(size());
for (int i = 0; i < size(); i++)
v[i] = diff(x, i);
return v;
}
};
// Graph class need Vector class
template <typename T> struct Edge {
int from, to;
T cost;
Edge(int f = 0, int t = 0, T c = T()) : from(f), to(t), cost(c) {}
};
template <typename T> std::istream &operator>>(std::istream &i, Edge<T> &e) {
i >> e.from >> e.to >> e.cost;
return i;
}
template <typename T>
std::ostream &operator<<(std::ostream &o, const Edge<T> &e) {
return o << e.from << ' ' << e.to << ' ' << e.cost;
}
template <typename T> class Graph {
public:
Vector<Edge<T>> edge;
Vector<Vector<int>> node;
Graph(int n = 0) { resize(n); }
void resize(int n) { node.resize(n); }
void clear() {
edge = Vector<Edge<T>>();
node = Vector<Vector<int>>(nodes());
}
int edges() const { return edge.size(); }
int nodes() const { return node.size(); }
virtual int add(int from, int to, T cost) = 0;
Vector<T> dijkstra(int start) {
Vector<T> d(nodes(), std::numeric_limits<T>::max());
std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>,
std::greater<std::pair<T, int>>>
s;
d[start] = 0;
s.emplace(d[start], start);
while (!s.empty()) {
T dist = s.top().first;
int from = s.top().second;
s.pop();
if (d[from] < dist)
continue;
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
if (d[from] + edge[id].cost < d[to]) {
d[to] = d[from] + edge[id].cost;
s.emplace(d[to], to);
}
}
}
return d;
}
};
// Undigraph class need Vector, Graph class
template <typename T> class Undigraph : public Graph<T> {
Vector<int> brgs, crus, lows, ords;
bool lowlinked;
int lowlink_dfs(int from, int k, int parent) {
ords[from] = lows[from] = k++;
bool is_crunode = false;
int cnt = 0;
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
if (ords[to] == -1) {
cnt++;
k = lowlink_dfs(to, k, from);
lows[from] = std::min(lows[from], lows[to]);
is_crunode |= parent != -1 && lows[to] >= ords[from];
if (ords[from] < lows[to])
brgs.pub(id);
} else if (to != parent)
lows[from] = std::min(lows[from], lows[to]);
}
is_crunode |= parent == -1 && cnt > 1;
if (is_crunode)
crus.pub(from);
return k;
}
void lowlink() {
brgs = Vector<int>();
crus = Vector<int>();
ords.assign(this->nodes(), -1), lows.assign(this->nodes(), -1);
int k = 0;
for (int i = 0; i < this->nodes(); i++)
if (ords[i] == -1)
k = lowlink_dfs(i, k, -1);
lowlinked = true;
}
public:
using Graph<T>::edge;
using Graph<T>::node;
Undigraph(int n = 0) : Graph<T>(n) { lowlinked = false; }
int add(int from, int to, T cost = 1) {
int id = this->edges();
node[from].pub(id);
node[to].pub(id);
edge.pub(Edge<T>(from, to, cost));
return id;
}
void reset_lowlink() { lowlink(); }
Vector<int> bridges() {
if (!lowlinked)
lowlink();
return brgs;
}
Vector<int> crunodes() {
if (!lowlinked)
lowlink();
return crus;
}
T prim(int start = 0) {
Vector<std::pair<T, std::pair<int, int>>> cft;
T total = 0;
Vector<int> used(this->nodes(), 0);
std::priority_queue<std::pair<T, std::pair<int, int>>,
std::vector<std::pair<T, std::pair<int, int>>>,
std::greater<std::pair<T, std::pair<int, int>>>>
s;
s.emplace(std::make_pair(0, std::make_pair(-1, start)));
while (!s.empty()) {
T cost = s.top().first;
int fromfrom = s.top().second.first, from = s.top().second.second;
s.pop();
if (used[from])
continue;
used[from] = true;
total += cost;
if (fromfrom > -1)
cft.pub(std::make_pair(cost, std::make_pair(fromfrom, from)));
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
s.emplace(std::make_pair(edge[id].cost, std::make_pair(from, to)));
}
}
this->clear();
for (std::pair<T, std::pair<int, int>> e : cft)
add(e.second.first, e.second.second, e.first);
return total;
}
};
// Forest class need Vector, Graph, Undigraph class
template <typename T> class Forest : public Undigraph<T> {
bool table_created;
Vector<Vector<int>> lca_table;
void root_dfs(int from) {
for (int &id : node[from]) {
if (from == edge[id].from)
std::swap(edge[id].from, edge[id].to);
int to = edge[id].from;
if (root[to] != -1)
continue;
root[to] = root[from];
parent[to] = from;
depth[to] = depth[from] + 1;
dist[to] = dist[from] + edge[id].cost;
children[from].pub(to);
root_dfs(to);
treesize[from] += treesize[to];
}
}
void make_lca_table() {
Vector<int> next_parent = parent;
while ((next_parent == Vector<int>(this->nodes(), -1)).sum() <
this->nodes()) {
lca_table.pub(next_parent);
for (int i = 0; i < this->nodes(); i++) {
next_parent[i] =
(next_parent[i] == -1 ? -1 : lca_table.last()[next_parent[i]]);
}
}
table_created = true;
}
public:
Vector<int> root, parent, treesize, depth;
Vector<T> dist;
Vector<Vector<int>> children;
using Undigraph<T>::edge;
using Undigraph<T>::node;
using Undigraph<T>::resize;
using Undigraph<T>::edges;
using Undigraph<T>::nodes;
Forest(int n = 0) : Undigraph<T>(n) {
initialize();
table_created = false;
}
void initialize() {
root.assign(nodes(), -1);
parent.assign(nodes(), -1);
treesize.assign(nodes(), 1);
depth.assign(nodes(), 0);
dist.assign(nodes(), 0);
children.assign(nodes(), Vector<int>());
}
void setTreeRoot(int n) {
initialize();
root[n] = n;
root_dfs(n);
}
void setForestRoot() {
initialize();
for (int n = 0; n < nodes(); n++)
if (root[n] == -1) {
root[n] = n;
root_dfs(n);
};
}
T diameter(int n = 0) {
setTreeRoot(n);
int terminal = dist.argmax();
setTreeRoot(terminal);
return dist.max();
}
int ancestor(int n, int i) {
if (!table_created)
make_lca_table();
if (i < 0)
return -1;
i = depth[n] - i;
for (int j = 0; i > 0; j++) {
if (i & 1)
n = lca_table[j][n];
i /= 2;
}
return n;
}
int lca(int u, int v) {
if (depth[u] > depth[v])
std::swap(u, v);
int i = 0, j = depth[u];
while (j - i > 1) {
int m = (i + j) / 2;
if (ancestor(u, m) == ancestor(v, m))
i = m;
else
j = m;
}
return ancestor(u, i);
}
void reset_lca() {
lca_table = Vector<Vector<int>>();
make_lca_table();
}
};
// Digraph class need Vector, Graph class
template <typename T> class Digraph : public Graph<T> {
Vector<Vector<int>> rev_node;
Vector<Edge<T>> rev_edge;
Vector<int> strong_edgeid, weak_edgeid, strong_id, ords;
bool scced;
void scc_dfs(int from) {
if (strong_id[from] == -1)
return;
strong_id[from] = -1;
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
scc_dfs(to);
}
ords.pub(from);
}
void scc_rdfs(int from, int cnt) {
if (strong_id[from] != -1)
return;
strong_id[from] = cnt;
for (int id : rev_node[from]) {
int to = rev_edge[id].from ^ rev_edge[id].to ^ from;
if (strong_id[to] == -1) {
strong_edgeid.pub(id);
scc_rdfs(to, cnt);
} else if (strong_id[to] == cnt)
strong_edgeid.pub(id);
else if (strong_id[to] < cnt)
weak_edgeid.pub(id);
}
}
public:
using Graph<T>::edge;
using Graph<T>::node;
Digraph(int n = 0) : Graph<T>(n), scced(false) {}
int add(int from, int to, T cost = 1) {
int id = this->edges();
node[from].pub(id);
edge.pub(Edge<T>(from, to, cost));
return id;
}
Digraph<T> reversed() const {
Digraph<T> r(this->edges());
for (Edge<T> e : edge)
r.add(e.to, e.from, e.cost);
return r;
}
void scc() {
strong_edgeid = Vector<int>();
weak_edgeid = Vector<int>();
strong_id.assign(this->nodes(), 0);
ords = Vector<int>(this->nodes());
rev_node = Vector<Vector<int>>(this->nodes());
rev_edge = Vector<Edge<T>>(this->edges());
for (int id = 0; id < this->edges(); id++) {
rev_node[edge[id].to].pub(id);
rev_edge[id] = Edge<T>(edge[id].to, edge[id].from, edge[id].cost);
}
for (int from = 0; from < this->nodes(); from++)
scc_dfs(from);
ords.reverse();
int cnt = 0;
for (int from : ords)
if (strong_id[from] == -1) {
scc_rdfs(from, cnt);
cnt++;
}
Vector<Edge<T>> strong_edge;
for (int id : strong_edgeid)
strong_edge.pub(edge[id]);
this->clear();
for (Edge<T> e : strong_edge)
add(e.from, e.to, e.cost);
scced = true;
}
Vector<int> weakid() {
if (!scced)
scc();
return weak_edgeid;
};
Vector<int> stronggroup() {
if (!scced)
scc();
return strong_id;
}
Vector<T> bellman(int start) {
Vector<T> d(this->nodes(), std::numeric_limits<T>::max());
d[start] = 0;
bool negloop = false;
Vector<int> updating(1, start);
for (int i = 0; i < this->nodes() && updating.size() > 0; i++) {
Vector<int> toUpdate;
for (int from : updating) {
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
if (d[to] > d[from] + edge[id].cost) {
d[to] = d[from] + edge[id].cost;
toUpdate.pub(to);
if (i == this->nodes() - 1)
d[to] = std::numeric_limits<T>::min();
negloop = true;
}
}
}
updating = toUpdate;
}
if (negloop) {
for (int i = 0; i < this->nodes() && updating.size() > 0; i++) {
Vector<int> toUpdate;
for (int from : updating) {
for (int id : node[from]) {
int to = edge[id].from ^ edge[id].to ^ from;
if (d[to] != std::numeric_limits<T>::min()) {
d[to] = std::numeric_limits<T>::min();
toUpdate.pub(to);
}
}
}
updating = toUpdate;
}
}
return d;
}
Vector<int> topsort() {
Vector<int> to_node(this->nodes(), 0);
for (Edge<T> e : edge)
to_node[e.to]++;
Vector<int> sorted;
for (int i = 0; i < this->nodes(); i++)
if (to_node[i] == 0)
sorted.pub(i);
for (int i = 0; i < sorted.size(); i++)
for (int id : node[sorted[i]]) {
if (--to_node[edge[id].to] == 0)
sorted.pub(edge[id].to);
}
if (sorted.size() != this->nodes())
return Vector<int>();
return sorted;
}
};
// math function for Vector, Matrix, Modulo class
template <typename T> T sign(const T &x) { return (x > 0) - (x < 0); }
template <typename T> T abs(const T &x) { return x * sign(x); }
template <typename T> T power(T x, int y) {
T r = x * 0 + 1;
while (y > 0) {
if (y & 1)
r *= x;
x *= x;
y /= 2;
}
return r;
}
bool is_power(int n, int m) {
return n == power(llround(std::pow(n, (double)+1 / m)), m);
}
int _gcd(const int &a, const int &b) { return b ? _gcd(b, a % b) : a; }
int gcd(const int &a, const int &b) {
return _gcd(std::max(a, b), std::min(a, b));
}
int lcm(const int &a, const int &b) { return a / gcd(a, b) * b; }
bool is_prime(int n) {
int m = std::sqrt(n - 1) + 1;
for (int i = 2; i < m + 1; i++)
if (n % i == 0)
return false;
return n > 1;
}
int fact(int n) {
int v = 1;
for (int i = 1; i < n + 1; i++)
v *= i;
return v;
}
Modulo fact(Modulo n) {
Modulo v = 1;
for (int i = 1; i < n + 1; i++)
v *= i;
return v;
}
int parm(int n, int r) {
int v = 1;
for (int i = n - r + 1; i < n + 1; i++)
v *= i;
return v;
}
Modulo parm(Modulo n, Modulo r) {
Modulo v = 1;
for (int i = n.val() - r.val() + 1; i < n.val() + 1; i++)
v *= i;
return v;
}
int comb(int n, int r) {
int v = 1;
r = std::min(r, n - r);
for (int i = 0; i < r; i++) {
v *= n - i;
v /= i + 1;
}
return v;
}
Modulo comb(Modulo n, Modulo r) {
Modulo p = 1, q = 1;
r = std::min(r, n - r);
for (int i = 0; i < r; i++) {
p *= n - i;
q *= i + 1;
}
return p / q;
}
int homo(int m, int n) { return comb(m + n - 1, n); }
Modulo homo(Modulo m, Modulo n) { return comb(m + n - 1, n); }
Map<int, int> factor(int n) {
Map<int, int> f;
for (int i = 2; i * i <= n; i++)
while (n % i == 0) {
f[i]++;
n /= i;
}
if (n != 1)
f[n]++;
return f;
}
// ex. function_find(fname,0,1024,'u',100,output)
#define function_find(FUNCTION_INT_TO_VALUE, INT_RANGE_MIN, INT_RANGE_MAX, \
CHAR_KIND, VALUE_TARGET, INT_AND_OUTPUT) \
{ \
int left = INT_RANGE_MIN - 1; \
int right = INT_RANGE_MAX + 1; \
bool outleft = (CHAR_KIND == 'd' || CHAR_KIND == 'U'); \
int getmin = (CHAR_KIND == 'u' || CHAR_KIND == 'd') ? 1 : -1; \
while (right - left > 1) { \
int mid = left + (right - left) / 2; \
bool OK = FUNCTION_INT_TO_VALUE(mid) * getmin >= VALUE_TARGET * getmin; \
if (outleft == OK) \
left = mid; \
else \
right = mid; \
} \
INT_AND_OUTPUT = outleft ? left : right; \
}
// BinaryIndexedTree class need Vector class
#define BITCalc(OP) \
BinaryIndexedTree<T> operator OP##=(const T &v) { \
if (range.first + 1 == range.second) \
for (int i = range.first; i < size(); i |= (i + 1)) \
t1[i] OP## = v; \
else \
for (int i = range.first; i < size(); i |= (i + 1)) { \
t1[i] OP## = -v * (range.first - 1); \
t2[i] OP## = v; \
} \
for (int i = range.second; i < size(); i |= (i + 1)) { \
t1[i] OP## = v * (range.second - 1); \
t2[i] OP## = -v; \
} \
return *this; \
}
template <typename T> class BinaryIndexedTree {
Vector<T> t1, t2;
std::pair<int, int> range;
public:
T sum(int x) {
T v = T();
for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
v += t1[i] + t2[i] * x;
}
return v;
}
T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
T get(int x) { return sum(x, x + 1); }
void resize(int n) {
t1.resize(n);
t2.resize(n);
}
int size() const { return t1.size(); }
Vector<T> firstVector() const { return t1; }
Vector<T> secondVector() const { return t2; }
BinaryIndexedTree(int n = 0) : range(std::make_pair(0, 0)) { resize(n); }
BinaryIndexedTree(const BinaryIndexedTree &bit)
: t1(bit.firstVector()), t2(bit.secondVector()),
range(std::make_pair(0, -1)) {
resize(bit.size());
}
BinaryIndexedTree<T> &operator=(const BinaryIndexedTree<T> &bit) {
resize(bit.size());
t1 = bit.firstVector();
t2 = bit.secondVector();
return *this;
}
T operator[](int i) const { return get(i); }
BinaryIndexedTree<T> &operator[](std::initializer_list<int> l) {
if (l.size() == 1) {
range.first = *l.begin();
range.second = (*l.begin()) + 1;
}
if (l.size() == 2) {
range.first = *l.begin();
range.second = (*(l.begin() + 1) < 0 ? *(l.begin() + 1) + size() + 1
: *(l.begin() + 1));
}
return *this;
}
BITCalc(+) BITCalc(-) T sum() { return sum(range.first, range.second); }
BinaryIndexedTree<T> operator=(const T &v) {
for (int i = range.first; i < range.second; i++) {
operator[]({i});
operator+=(-get(i) + v);
}
return *this;
}
Vector<T> toVector() {
Vector<T> v(size());
for (int i = 0; i < size(); i++)
v[i] = get(i);
return v;
}
};
// BIT^2
// 10^5 -> NlogN, 3000 -> N^2, 200 -> N^3, 50 -> N^4, 20 -> 2^N
WRITE_FUNCTION_UNDER_HERE
int32_t main() {
INIT;
in(int, n);
int r = sqrt(n) + 1;
fod(i, 1, r) {
if (n % i == 0) {
out(i + n / i - 2);
return 0;
}
}
} | replace | 2,471 | 2,472 | 2,471 | 2,473 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll a = 1;
ll b = 1;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
a = i;
}
}
b = n / a;
ll ans;
ans = a + b - 2;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll a = 1;
ll b = 1;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
a = i;
}
}
b = n / a;
ll ans;
ans = a + b - 2;
cout << ans << endl;
return 0;
}
| replace | 11 | 12 | 11 | 12 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (long i = 1; i <= n; i++)
using namespace std;
int main(void) {
long n;
double tmp;
long ans = 1000000000005;
cin >> n;
rep(i, n / 2 + 1) {
tmp = (double)n / i;
if (floor(tmp) != (tmp))
continue;
if (ans > i + tmp - 2)
ans = i + tmp - 2;
}
cout << ans;
} | #include <bits/stdc++.h>
#define rep(i, n) for (long i = 1; i <= n; i++)
using namespace std;
int main(void) {
long n;
double tmp;
long ans = 1000000000005;
cin >> n;
rep(i, sqrt(n) + 1) {
tmp = (double)n / i;
if (floor(tmp) != (tmp))
continue;
if (ans > i + tmp - 2)
ans = i + tmp - 2;
}
cout << ans;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N;
cin >> N;
for (ll i = sqrt(N); i >= 1; i++) {
if (N % i == 0) {
cout << i + (N / i) - 2 << endl;
break;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N;
cin >> N;
for (ll i = sqrt(N); i >= 1; i--) {
if (N % i == 0) {
cout << i + (N / i) - 2 << endl;
break;
}
}
return 0;
} | replace | 6 | 7 | 6 | 7 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll n1, n2 = 0;
ll ans = 1e+13;
for (ll i = 1; i < floor(n / 2) + 1; i++) {
// cout << "i = " << i << " n/i = " << n/i << " ans = "<<ans<<endl;
if ((n / i) * i == n) {
n1 = i;
n2 = (n / i);
ll a = n1 + n2 - 2;
// cout << a << endl;
if (a < ans)
ans = a;
}
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll n1, n2 = 0;
ll ans = 1e+13;
for (ll i = 1; i < sqrt(n) + 1; i++) {
if (ans > i + (n / i) - 2 && i * (n / i) == n) {
ans = i + (n / i) - 2;
}
}
cout << ans;
} | replace | 10 | 19 | 10 | 13 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define mod 1000000000
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vs vector<string>
#define psi pair<string, ll>
#define pii pair<ll, ll>
#define vsi vector<psi>
#define vii vector<pii>
#define mp make_pair
#define pb push_back
#define rep(n) for (long i = 0; i < n; i++)
#define repr(n) for (long i = (n - 1); i >= 0; i--)
#define fori(i, a, b) for (auto i = a; i <= b; i++)
#define ford(i, b, a) for (auto i = b; i >= a; i--)
#define mset(a, i) memset(a, i, sizeof(a))
#define minv(a) min_element(a.begin(), a.end())
#define maxv(a) max_element(a.begin(), a.end())
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define sotit(a) sort(a.begin(), a.end());
#define sot(a) sort(a, a + n);
#define ff first
#define maxheap(v) priority_queue<v>
#define minheap(v) priority_queue<v, vector<v>, greater<v>>
#define ss second
#define endl "\n"
#define NL cout << endl;
#define printmat(a, m, n) \
for (int i = 0; i < m; i++) { \
for (int j = 0; j < n; j++) { \
cout << a[i][j] << " "; \
} \
cout << endl; \
}
#define dbgn(x) cout << " $ " << x << " $ " << endl;
#define dbg(x) cout << " $ " << x << " $ ";
#define all(v) v.begin(), v.end()
#define show(a, n) \
for (int i = 0; i < n; i++) { \
cout << a[i] << " "; \
} \
NL
#define fast_io() \
cin.sync_with_stdio(false); \
cout.sync_with_stdio(false); \
cin.tie(NULL);
// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val
int main() {
fast_io()
ll n;
cin >> n;
ll m = ceil(sqrt(n));
for (int i = m; i >= 1; i++) {
if (n % i == 0) {
cout << (n / i) - 2 + i;
return 0;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define mod 1000000000
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vs vector<string>
#define psi pair<string, ll>
#define pii pair<ll, ll>
#define vsi vector<psi>
#define vii vector<pii>
#define mp make_pair
#define pb push_back
#define rep(n) for (long i = 0; i < n; i++)
#define repr(n) for (long i = (n - 1); i >= 0; i--)
#define fori(i, a, b) for (auto i = a; i <= b; i++)
#define ford(i, b, a) for (auto i = b; i >= a; i--)
#define mset(a, i) memset(a, i, sizeof(a))
#define minv(a) min_element(a.begin(), a.end())
#define maxv(a) max_element(a.begin(), a.end())
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define sotit(a) sort(a.begin(), a.end());
#define sot(a) sort(a, a + n);
#define ff first
#define maxheap(v) priority_queue<v>
#define minheap(v) priority_queue<v, vector<v>, greater<v>>
#define ss second
#define endl "\n"
#define NL cout << endl;
#define printmat(a, m, n) \
for (int i = 0; i < m; i++) { \
for (int j = 0; j < n; j++) { \
cout << a[i][j] << " "; \
} \
cout << endl; \
}
#define dbgn(x) cout << " $ " << x << " $ " << endl;
#define dbg(x) cout << " $ " << x << " $ ";
#define all(v) v.begin(), v.end()
#define show(a, n) \
for (int i = 0; i < n; i++) { \
cout << a[i] << " "; \
} \
NL
#define fast_io() \
cin.sync_with_stdio(false); \
cout.sync_with_stdio(false); \
cin.tie(NULL);
// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val
int main() {
fast_io()
ll n;
cin >> n;
ll m = ceil(sqrt(n));
for (int i = m; i >= 1; i--) {
if (n % i == 0) {
cout << (n / i) - 2 + i;
return 0;
}
}
return 0;
} | replace | 59 | 60 | 59 | 60 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
long long int n, i;
cin >> n;
i = sqrt(n);
while (1) {
if (n % i == 0) {
cout << n / i + i - 2;
break;
}
i++;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
long long int n, i;
cin >> n;
i = sqrt(n);
while (1) {
if (n % i == 0) {
cout << n / i + i - 2;
break;
}
i--;
}
return 0;
} | replace | 13 | 14 | 13 | 14 | TLE | |
p02881 | C++ | Time Limit Exceeded |
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() { cerr << endl; }
template <typename H, typename... T> void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T> void print(T x) { cout << x << endl; }
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;
ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;
void divisor(ll N, vll &ret) {}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll N;
cin >> N;
ll ans = INF;
for (int i = 1; i * i <= N; ++i) {
if (N % i == 0) {
ans = min(ans, i + N / i - 2);
}
}
print(ans);
}
|
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() { cerr << endl; }
template <typename H, typename... T> void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T> void print(T x) { cout << x << endl; }
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;
ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;
void divisor(ll N, vll &ret) {}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll N;
cin >> N;
ll ans = INF;
for (ll i = 1; i * i <= N; ++i) {
if (N % i == 0) {
ans = min(ans, i + N / i - 2);
}
}
print(ans);
}
| replace | 54 | 55 | 54 | 55 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define m7 1000000007
#define m9 1000000009
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int main() {
ll a, n;
cin >> n;
a = n - 1;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
a = min(a, n / i + i - 2);
}
}
cout << a;
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define m7 1000000007
#define m9 1000000009
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int main() {
ll a, n;
cin >> n;
a = n - 1;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
a = min(a, n / i + i - 2);
}
}
cout << a;
return 0;
} | replace | 29 | 30 | 29 | 30 | TLE | |
p02881 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n;
cin >> n;
int64_t start = int64_t(sqrt(n));
if (start * start == n) {
cout << 2 * (start - 1) << endl;
return 1;
}
while (n % start != 0)
start--;
int64_t ans = (start - 1) + (n / start - 1);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n;
cin >> n;
int64_t start = int64_t(sqrt(n));
// if(start*start == n) {
// cout<<2*(start-1)<<endl;
// return 1;
// }
while (n % start != 0)
start--;
int64_t ans = (start - 1) + (n / start - 1);
cout << ans << endl;
return 0;
} | replace | 8 | 12 | 8 | 12 | 0 | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(n) n.begin(), n.end()
#define yn(x) cout << (x ? "Yes" : "No") << endl;
int main() {
ll n;
cin >> n;
ll ans = n;
rep(i, n + 1) {
rep(j, n + 1) {
if (n == i * j)
ans = min(ans, i + j - 2);
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(n) n.begin(), n.end()
#define yn(x) cout << (x ? "Yes" : "No") << endl;
int main() {
ll n;
cin >> n;
ll ans;
for (ll i = sqrt(n); i > 0; i--) {
if (n % i == 0) {
ans = i + n / i - 2;
break;
}
}
cout << ans << endl;
}
| replace | 10 | 15 | 10 | 15 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
using namespace std;
long long int a, b;
int main() {
cin >> a;
b = 0;
for (int i = 1; i * i <= a; i++) {
if (a % i == 0) {
b = i;
}
}
cout << a / b + b - 2 << endl;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
using namespace std;
long long int a, b;
int main() {
cin >> a;
b = 0;
for (long long int i = 1; i * i <= a; i++) {
if (a % i == 0) {
b = i;
}
}
cout << a / b + b - 2 << endl;
}
| replace | 11 | 12 | 11 | 12 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define swap(a, b) \
a ^= b; \
b ^= a; \
a ^= b;
#define chmax(a, b) \
{ \
if (a < b) \
a = b; \
}
#define chmin(a, b) \
{ \
if (a > b) \
a = b; \
}
using namespace std;
typedef long long ll;
ll gcd(ll, ll);
vector<bool> to_binary(int);
int sum(vector<int>);
int min(vector<int>);
int max(vector<int>);
ll nCr(ll n, ll r);
const ll INF = 1LL << 60;
typedef tuple<ll, ll> mtupl;
bool mycomp(mtupl x, mtupl y) { return get<1>(x) < get<1>(y); }
vector<vector<ll>> memo;
ll get(int x, int y) {
if (memo[x][y] != -1)
return memo[x][y];
if (x == 0)
return 1;
if (y < x)
return 0;
memo[x][y] = get(x, y - 1) + get(x - 1, y - 1);
return memo[x][y];
}
ll mpow(ll a, ll b) {
ll ans = 1;
for (ll i = 0; i < b; i++) {
ans *= a;
ans %= 1000000007;
}
return ans;
}
ll nCr(ll n, ll r) {
ll res = 1;
for (int i = 0; i < r; i++) {
res *= (n - i);
}
for (int i = 1; i <= r; i++) {
res /= i;
}
return res;
}
int vsum(vector<int> v) {
int res = 0;
for (auto it = v.begin(); it != v.end(); it++)
res += *it;
return res;
}
int vmin(vector<int> v) {
int min = INT32_MAX;
for (auto it = v.begin(); it != v.end(); it++)
min = std::min(min, *it);
return min;
}
int vmax(vector<int> v) {
int max = INT32_MIN;
for (auto it = v.begin(); it != v.end(); it++)
max = std::max(max, *it);
return max;
}
ll gcd(ll A, ll B) {
if (A == 0 || B == 0)
return max(A, B);
int r = A % B;
while (r != 0) {
A = B;
B = r;
r = A % B;
}
return B;
}
ll _gcd(ll A, ll B) { return (B == 0) ? A : gcd(A % B, B); }
vector<bool> to_binary(int x) {
vector<bool> res;
while (x != 0) {
res.push_back(x % 2);
x /= 2;
}
return res;
}
template <int index>
bool comp(tuple<ll, ll, ll, ll> a, tuple<ll, ll, ll, ll> b) {
return get<index>(a) < get<index>(b);
}
int vector_finder(std::vector<ll> vec, ll number) {
auto itr = std::find(vec.begin(), vec.end(), number);
size_t index = std::distance(vec.begin(), itr);
if (index != vec.size()) { // 発見できたとき
return index;
} else { // 発見できなかったとき
return 0;
}
}
int len(int x) { return (int)log10(x) + 1; }
int same_vec(vector<int> a, vector<int> b) {
int res = 1, num = a.size();
for (int i = 0; i < num; i++) {
if (a[i] != b[i]) {
res = 0;
break;
}
}
return res;
}
ll fibs[100] = {0};
ll fib(int n) {
if (fibs[n] == 0) {
return fibs[n] = fib(n - 1) + fib(n - 2);
}
return fibs[n];
}
vector<ll> prime_factor(int64_t n) {
// map< ll, ll > ret;
vector<ll> ret;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
// ret[i]++;
ret.push_back(i);
n /= i;
}
}
if (n != 1)
ret.push_back(n); // ret[n] = 1;
return ret;
}
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
// Program Start
ll N;
cin >> N;
ll ans = INF;
for (int i = 1; i * i <= N; i++) {
if (N % i == 0) {
ans = min(ans, N / i + i - 2);
}
}
cout << ans << endl;
ProgramEndPointLabel:;
// Program End
#ifndef DEBUG
system("pause");
#endif // !DEBUG
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define swap(a, b) \
a ^= b; \
b ^= a; \
a ^= b;
#define chmax(a, b) \
{ \
if (a < b) \
a = b; \
}
#define chmin(a, b) \
{ \
if (a > b) \
a = b; \
}
using namespace std;
typedef long long ll;
ll gcd(ll, ll);
vector<bool> to_binary(int);
int sum(vector<int>);
int min(vector<int>);
int max(vector<int>);
ll nCr(ll n, ll r);
const ll INF = 1LL << 60;
typedef tuple<ll, ll> mtupl;
bool mycomp(mtupl x, mtupl y) { return get<1>(x) < get<1>(y); }
vector<vector<ll>> memo;
ll get(int x, int y) {
if (memo[x][y] != -1)
return memo[x][y];
if (x == 0)
return 1;
if (y < x)
return 0;
memo[x][y] = get(x, y - 1) + get(x - 1, y - 1);
return memo[x][y];
}
ll mpow(ll a, ll b) {
ll ans = 1;
for (ll i = 0; i < b; i++) {
ans *= a;
ans %= 1000000007;
}
return ans;
}
ll nCr(ll n, ll r) {
ll res = 1;
for (int i = 0; i < r; i++) {
res *= (n - i);
}
for (int i = 1; i <= r; i++) {
res /= i;
}
return res;
}
int vsum(vector<int> v) {
int res = 0;
for (auto it = v.begin(); it != v.end(); it++)
res += *it;
return res;
}
int vmin(vector<int> v) {
int min = INT32_MAX;
for (auto it = v.begin(); it != v.end(); it++)
min = std::min(min, *it);
return min;
}
int vmax(vector<int> v) {
int max = INT32_MIN;
for (auto it = v.begin(); it != v.end(); it++)
max = std::max(max, *it);
return max;
}
ll gcd(ll A, ll B) {
if (A == 0 || B == 0)
return max(A, B);
int r = A % B;
while (r != 0) {
A = B;
B = r;
r = A % B;
}
return B;
}
ll _gcd(ll A, ll B) { return (B == 0) ? A : gcd(A % B, B); }
vector<bool> to_binary(int x) {
vector<bool> res;
while (x != 0) {
res.push_back(x % 2);
x /= 2;
}
return res;
}
template <int index>
bool comp(tuple<ll, ll, ll, ll> a, tuple<ll, ll, ll, ll> b) {
return get<index>(a) < get<index>(b);
}
int vector_finder(std::vector<ll> vec, ll number) {
auto itr = std::find(vec.begin(), vec.end(), number);
size_t index = std::distance(vec.begin(), itr);
if (index != vec.size()) { // 発見できたとき
return index;
} else { // 発見できなかったとき
return 0;
}
}
int len(int x) { return (int)log10(x) + 1; }
int same_vec(vector<int> a, vector<int> b) {
int res = 1, num = a.size();
for (int i = 0; i < num; i++) {
if (a[i] != b[i]) {
res = 0;
break;
}
}
return res;
}
ll fibs[100] = {0};
ll fib(int n) {
if (fibs[n] == 0) {
return fibs[n] = fib(n - 1) + fib(n - 2);
}
return fibs[n];
}
vector<ll> prime_factor(int64_t n) {
// map< ll, ll > ret;
vector<ll> ret;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
// ret[i]++;
ret.push_back(i);
n /= i;
}
}
if (n != 1)
ret.push_back(n); // ret[n] = 1;
return ret;
}
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
// Program Start
ll N;
cin >> N;
ll ans = INF;
for (ll i = 1; i * i <= N; i++) {
if (N % i == 0) {
ans = min(ans, N / i + i - 2);
}
}
cout << ans << endl;
ProgramEndPointLabel:;
// Program End
#ifndef DEBUG
system("pause");
#endif // !DEBUG
return 0;
}
| replace | 182 | 183 | 182 | 183 | TLE | |
p02881 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
int main() {
ll N;
cin >> N;
ll ans = N - 1;
for (ll i = 1; i * i <= N; i++) {
for (ll j = i; i * j <= N; j++) {
if (i * j == N) {
if (ans > i + j - 2)
ans = i + j - 2;
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
int main() {
ll N;
cin >> N;
ll ans = N - 1;
for (ll i = 1; i * i <= N; i++) {
if (N % i == 0) {
ll j = N / i;
ans = min(ans, j + i - 2);
}
}
cout << ans << endl;
} | replace | 13 | 18 | 13 | 16 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.