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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<int, null_type, lessl<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define ll long long
#define int long long
#define double long double
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define endl "\n"
#define f(i, l, r) for (int i = l; i <= r; i++)
#define rf(i, r, l) for (int i = r; i >= l; i--)
#define bp __builtin_popcountll
#define inf 1e18
const int N = 3e2 + 5;
const int M = 1e9 + 7;
int dp[N][N];
int ans[N][N][4];
void solve() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
dp[i][j] = 0;
for (int p = 0; p < 4; p++)
ans[i][j][p] = 0;
}
}
while (k--) {
int r1, c1, v1;
cin >> r1 >> c1 >> v1;
dp[r1][c1] = v1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
ans[i][j][0] = max(ans[i - 1][j][3], ans[i][j - 1][0]);
ans[i][j][1] = max(dp[i][j] + ans[i - 1][j][3],
max(ans[i][j - 1][1], ans[i][j - 1][0] + dp[i][j]));
ans[i][j][2] = max(dp[i][j] + ans[i - 1][j][3],
max(ans[i][j - 1][2], ans[i][j - 1][1] + dp[i][j]));
ans[i][j][3] = max(dp[i][j] + ans[i - 1][j][3],
max(ans[i][j - 1][3], ans[i][j - 1][2] + dp[i][j]));
ans[i][j][1] = max(ans[i][j][1], ans[i][j][0]);
ans[i][j][2] = max(ans[i][j][2], ans[i][j][1]);
ans[i][j][3] = max(ans[i][j][3], ans[i][j][2]);
// cout<<ans[i][j][3]<<" ";
}
// cout<<endl;
}
int res = ans[n][m][3];
cout << res << endl;
}
signed main() {
FAST int t = 1;
// cin>>t;
for (int tc = 1; tc <= t; tc++) {
// cout<<"Case #"<<tc<<": ";
solve();
}
}
| #include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<int, null_type, lessl<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define ll long long
#define int long long
#define double long double
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define endl "\n"
#define f(i, l, r) for (int i = l; i <= r; i++)
#define rf(i, r, l) for (int i = r; i >= l; i--)
#define bp __builtin_popcountll
#define inf 1e18
const int N = 3e3 + 5;
const int M = 1e9 + 7;
int dp[N][N];
int ans[N][N][4];
void solve() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
dp[i][j] = 0;
for (int p = 0; p < 4; p++)
ans[i][j][p] = 0;
}
}
while (k--) {
int r1, c1, v1;
cin >> r1 >> c1 >> v1;
dp[r1][c1] = v1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
ans[i][j][0] = max(ans[i - 1][j][3], ans[i][j - 1][0]);
ans[i][j][1] = max(dp[i][j] + ans[i - 1][j][3],
max(ans[i][j - 1][1], ans[i][j - 1][0] + dp[i][j]));
ans[i][j][2] = max(dp[i][j] + ans[i - 1][j][3],
max(ans[i][j - 1][2], ans[i][j - 1][1] + dp[i][j]));
ans[i][j][3] = max(dp[i][j] + ans[i - 1][j][3],
max(ans[i][j - 1][3], ans[i][j - 1][2] + dp[i][j]));
ans[i][j][1] = max(ans[i][j][1], ans[i][j][0]);
ans[i][j][2] = max(ans[i][j][2], ans[i][j][1]);
ans[i][j][3] = max(ans[i][j][3], ans[i][j][2]);
// cout<<ans[i][j][3]<<" ";
}
// cout<<endl;
}
int res = ans[n][m][3];
cout << res << endl;
}
signed main() {
FAST int t = 1;
// cin>>t;
for (int tc = 1; tc <= t; tc++) {
// cout<<"Case #"<<tc<<": ";
solve();
}
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;
int main() {
int r, c, k;
cin >> r >> c >> k;
long v[r][c];
rep(i, r) rep(j, c) v[i][j] = 0;
long dp[4][r][c];
rep(i, 4) rep(j, r) rep(l, c) dp[i][j][l] = 0;
rep(i, k) {
long ri, ci, vi;
cin >> ri >> ci >> vi;
ri--;
ci--;
v[ri][ci] = vi;
}
rep(i, r - 1) {
rep(j, 4) {
rep(l, c - 1) {
if (j != 3)
dp[j + 1][i][l + 1] = max(dp[j + 1][i][l + 1], dp[j][i][l] + v[i][l]);
dp[j][i][l + 1] = max(dp[j][i][l + 1], dp[j][i][l]);
if (j != 3)
dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l] + v[i][l]);
dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l]);
}
if (j != 3)
dp[0][i + 1][c - 1] =
max(dp[0][i + 1][c - 1], dp[j][i][c - 1] + v[i][c - 1]);
dp[0][i + 1][c - 1] = max(dp[0][i + 1][c - 1], dp[j][i][c - 1]);
}
}
rep(j, 4) {
rep(l, c - 1) {
if (j != 3)
dp[j + 1][r - 1][l + 1] =
max(dp[j + 1][r - 1][l + 1], dp[j][r - 1][l] + v[r - 1][l]);
dp[j][r - 1][l + 1] = max(dp[j][r - 1][l + 1], dp[j][r - 1][l]);
}
}
long ans = max(dp[0][r - 1][c - 1] + v[r - 1][c - 1],
dp[1][r - 1][c - 1] + v[r - 1][c - 1]);
ans = max(ans, dp[2][r - 1][c - 1] + v[r - 1][c - 1]);
ans = max(ans, dp[3][r - 1][c - 1]);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;
int main() {
int r, c, k;
cin >> r >> c >> k;
long v[r][c];
rep(i, r) rep(j, c) v[i][j] = 0;
// long dp[4][r][c];
vector<vector<vector<long>>> dp(4, vector<vector<long>>(r, vector<long>(c)));
rep(i, 4) rep(j, r) rep(l, c) dp[i][j][l] = 0;
rep(i, k) {
long ri, ci, vi;
cin >> ri >> ci >> vi;
ri--;
ci--;
v[ri][ci] = vi;
}
rep(i, r - 1) {
rep(j, 4) {
rep(l, c - 1) {
if (j != 3)
dp[j + 1][i][l + 1] = max(dp[j + 1][i][l + 1], dp[j][i][l] + v[i][l]);
dp[j][i][l + 1] = max(dp[j][i][l + 1], dp[j][i][l]);
if (j != 3)
dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l] + v[i][l]);
dp[0][i + 1][l] = max(dp[0][i + 1][l], dp[j][i][l]);
}
if (j != 3)
dp[0][i + 1][c - 1] =
max(dp[0][i + 1][c - 1], dp[j][i][c - 1] + v[i][c - 1]);
dp[0][i + 1][c - 1] = max(dp[0][i + 1][c - 1], dp[j][i][c - 1]);
}
}
rep(j, 4) {
rep(l, c - 1) {
if (j != 3)
dp[j + 1][r - 1][l + 1] =
max(dp[j + 1][r - 1][l + 1], dp[j][r - 1][l] + v[r - 1][l]);
dp[j][r - 1][l + 1] = max(dp[j][r - 1][l + 1], dp[j][r - 1][l]);
}
}
long ans = max(dp[0][r - 1][c - 1] + v[r - 1][c - 1],
dp[1][r - 1][c - 1] + v[r - 1][c - 1]);
ans = max(ans, dp[2][r - 1][c - 1] + v[r - 1][c - 1]);
ans = max(ans, dp[3][r - 1][c - 1]);
cout << ans << endl;
return 0;
} | replace | 10 | 11 | 10 | 12 | 0 | |
p02586 | C++ | Runtime Error | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#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 rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const ll inf = 1ll << 60;
ll dp[10][10][5];
ll g[10][10];
int main() {
int r, c, k;
cin >> r >> c >> k;
rep(i, k) {
int a, b, d;
cin >> a >> b >> d;
a--;
b--;
g[a][b] = d;
}
if (g[0][0]) {
dp[0][0][1] = g[0][0];
dp[0][0][0] = -inf;
}
rep(i, r) rep(j, c) rep(k, 4) {
if (i - 1 >= 0) {
if (g[i][j] != 0)
chmax(dp[i][j][1], dp[i - 1][j][k] + g[i][j]);
chmax(dp[i][j][0], dp[i - 1][j][k]);
}
if (j - 1 >= 0) {
if (g[i][j] != 0)
chmax(dp[i][j][k + 1], dp[i][j - 1][k] + g[i][j]);
chmax(dp[i][j][k], dp[i][j - 1][k]);
}
}
ll ans = -inf;
rep(k, 4) { chmax(ans, dp[r - 1][c - 1][k]); }
cout << ans << endl;
} | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#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 rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const ll inf = 1ll << 60;
ll dp[3010][3010][5];
ll g[3010][3010];
int main() {
int r, c, k;
cin >> r >> c >> k;
rep(i, k) {
int a, b, d;
cin >> a >> b >> d;
a--;
b--;
g[a][b] = d;
}
if (g[0][0]) {
dp[0][0][1] = g[0][0];
dp[0][0][0] = -inf;
}
rep(i, r) rep(j, c) rep(k, 4) {
if (i - 1 >= 0) {
if (g[i][j] != 0)
chmax(dp[i][j][1], dp[i - 1][j][k] + g[i][j]);
chmax(dp[i][j][0], dp[i - 1][j][k]);
}
if (j - 1 >= 0) {
if (g[i][j] != 0)
chmax(dp[i][j][k + 1], dp[i][j - 1][k] + g[i][j]);
chmax(dp[i][j][k], dp[i][j - 1][k]);
}
}
ll ans = -inf;
rep(k, 4) { chmax(ans, dp[r - 1][c - 1][k]); }
cout << ans << endl;
} | replace | 30 | 32 | 30 | 32 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef pair<ll, ll> prl;
typedef vector<ll> vcl;
typedef map<ll, ll> mapl;
typedef unordered_map<ll, ll> umap;
#define pb push_back
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repi(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define reps(i, v) for (ll i = 0; i < v.size(); i++)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
const ll mod = 1e9 + 7;
int main() {
// your code goes here
ll R, C, K, r, c, v;
cin >> R >> C >> K;
ll vs[3005][3005] = {0};
ll dp0[3005][3005] = {0};
ll dp1[3005][3005] = {0};
ll dp2[3005][3005] = {0};
ll dp3[3005][3005] = {0};
ll ans = 0;
rep(i, 1, K) {
cin >> r >> c >> v;
vs[r][c] = v;
}
rep(i, 1, R) {
rep(j, 1, C) {
rep(k, 0, 3) {
if (k == 0) {
chmax(dp0[i][j], dp0[i][j - 1]);
chmax(dp0[i][j], dp0[i - 1][j]);
chmax(dp0[i][j], dp1[i - 1][j]);
chmax(dp0[i][j], dp2[i - 1][j]);
chmax(dp0[i][j], dp3[i - 1][j]);
} else if (vs[i][j] == 0) {
chmax(dp0[i][j], dp0[i][j - 1]);
chmax(dp1[i][j], dp1[i][j - 1]);
chmax(dp2[i][j], dp2[i][j - 1]);
chmax(dp3[i][j], dp3[i][j - 1]);
} else if (k != 1) {
chmax(dp2[i][j], dp2[i][j - 1]);
chmax(dp3[i][j], dp3[i][j - 1]);
chmax(dp2[i][j], dp1[i][j - 1] + vs[i][j]);
chmax(dp3[i][j], dp2[i][j - 1] + vs[i][j]);
} else {
chmax(dp1[i][j], dp1[i][j - 1]);
chmax(dp1[i][j], dp0[i][j - 1] + vs[i][j]);
chmax(dp1[i][j], dp0[i - 1][j] + vs[i][j]);
chmax(dp1[i][j], dp1[i - 1][j] + vs[i][j]);
chmax(dp1[i][j], dp2[i - 1][j] + vs[i][j]);
chmax(dp1[i][j], dp3[i - 1][j] + vs[i][j]);
}
}
}
}
chmax(ans, dp0[R][C]);
chmax(ans, dp1[R][C]);
chmax(ans, dp2[R][C]);
chmax(ans, dp3[R][C]);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef pair<ll, ll> prl;
typedef vector<ll> vcl;
typedef map<ll, ll> mapl;
typedef unordered_map<ll, ll> umap;
#define pb push_back
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repi(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define reps(i, v) for (ll i = 0; i < v.size(); i++)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
const ll mod = 1e9 + 7;
int main() {
// your code goes here
ll R, C, K, r, c, v;
cin >> R >> C >> K;
vector<vcl> vs(3005, vcl(3005));
vector<vcl> dp0(3005, vcl(3005));
vector<vcl> dp1(3005, vcl(3005));
vector<vcl> dp2(3005, vcl(3005));
vector<vcl> dp3(3005, vcl(3005));
ll ans = 0;
rep(i, 1, K) {
cin >> r >> c >> v;
vs[r][c] = v;
}
rep(i, 1, R) {
rep(j, 1, C) {
rep(k, 0, 3) {
if (k == 0) {
chmax(dp0[i][j], dp0[i][j - 1]);
chmax(dp0[i][j], dp0[i - 1][j]);
chmax(dp0[i][j], dp1[i - 1][j]);
chmax(dp0[i][j], dp2[i - 1][j]);
chmax(dp0[i][j], dp3[i - 1][j]);
} else if (vs[i][j] == 0) {
chmax(dp0[i][j], dp0[i][j - 1]);
chmax(dp1[i][j], dp1[i][j - 1]);
chmax(dp2[i][j], dp2[i][j - 1]);
chmax(dp3[i][j], dp3[i][j - 1]);
} else if (k != 1) {
chmax(dp2[i][j], dp2[i][j - 1]);
chmax(dp3[i][j], dp3[i][j - 1]);
chmax(dp2[i][j], dp1[i][j - 1] + vs[i][j]);
chmax(dp3[i][j], dp2[i][j - 1] + vs[i][j]);
} else {
chmax(dp1[i][j], dp1[i][j - 1]);
chmax(dp1[i][j], dp0[i][j - 1] + vs[i][j]);
chmax(dp1[i][j], dp0[i - 1][j] + vs[i][j]);
chmax(dp1[i][j], dp1[i - 1][j] + vs[i][j]);
chmax(dp1[i][j], dp2[i - 1][j] + vs[i][j]);
chmax(dp1[i][j], dp3[i - 1][j] + vs[i][j]);
}
}
}
}
chmax(ans, dp0[R][C]);
chmax(ans, dp1[R][C]);
chmax(ans, dp2[R][C]);
chmax(ans, dp3[R][C]);
cout << ans << endl;
return 0;
} | replace | 24 | 29 | 24 | 29 | -11 | |
p02586 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
int main() {
int r, c, k;
cin >> r >> c >> k;
map<P, ll> mp;
rep(i, k) {
int r, c, v;
cin >> r >> c >> v;
r--;
c--;
mp[make_pair(r, c)] = v;
}
vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4, 0)));
dp[0][0][1] = mp[make_pair(0, 0)];
rep(i, r) rep(j, c) {
P p = make_pair(i, j);
if (i == 0 && j == 0)
continue;
else if (i == 0) {
rep(n, 3) {
dp[i][j][n + 1] = max(dp[i][j - 1][n + 1], dp[i][j - 1][n] + mp[p]);
}
} else if (j == 0) {
dp[i][j][1] = dp[i - 1][j][1] + mp[p];
dp[i][j][0] = dp[i - 1][j][1];
} else {
rep(n, 4) dp[i][j][n] = dp[i][j - 1][n];
rep(n, 4) {
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][n]);
dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][n] + mp[p]);
}
rep(n, 3) {
dp[i][j][n + 1] = max({dp[i][j][n + 1], dp[i][j - 1][n] + mp[p]});
}
}
}
ll ans = 0;
rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
int main() {
int r, c, k;
cin >> r >> c >> k;
map<P, ll> mp;
rep(i, k) {
int r, c, v;
cin >> r >> c >> v;
r--;
c--;
mp[make_pair(r, c)] = v;
}
vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4, 0)));
dp[0][0][1] = mp[make_pair(0, 0)];
rep(i, r) rep(j, c) {
P p = make_pair(i, j);
if (i == 0 && j == 0)
continue;
else if (i == 0) {
rep(n, 3) {
dp[i][j][n + 1] = max(dp[i][j - 1][n + 1], dp[i][j - 1][n] + mp[p]);
}
} else if (j == 0) {
dp[i][j][1] = dp[i - 1][j][1] + mp[p];
dp[i][j][0] = dp[i - 1][j][1];
} else {
rep(n, 4) dp[i][j][n] = dp[i][j - 1][n];
rep(n, 4) { dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][n]); }
if (mp.find(p) != mp.end()) {
rep(n, 4) { dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][n] + mp[p]); }
rep(n, 3) {
dp[i][j][n + 1] = max({dp[i][j][n + 1], dp[i][j - 1][n] + mp[p]});
}
}
}
}
ll ans = 0;
rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
cout << ans << endl;
} | replace | 36 | 42 | 36 | 42 | TLE | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename key>
using ordered_set = tree<key, null_type, less<key>, rb_tree_tag,
tree_order_statistics_node_update>;
#define F first
#define S second
#define T second.first
#define D second.second
#define FIO \
ios_base::sync_with_stdio(0); \
cin.tie(0);
#define all(v) v.begin(), v.end()
#define OO 0x3f3f3f3fLL
#define INF 0x3f3f3f3f3f3f3f3fLL
#define sz(s) (int)(s.size())
#define RT(s) return cout << s, 0
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, pair<ll, ll>> piii;
const int N = 3e2 + 5, M = 20, mod = 1000000000;
const double EPS = 1e-10;
ll n, m, k;
ll A[N][N], mem[N][N][4];
ll solve(int x, int y, int rem) {
if (x == n - 1 && y == m - 1) {
if (rem)
return A[x][y];
return 0;
}
if (x == n || y == m) {
return -INF;
}
ll &ret = mem[x][y][rem];
if (~ret)
return ret;
if (rem) {
ret = max(solve(x, y + 1, rem - 1), solve(x + 1, y, 3)) + A[x][y];
}
ret = max(ret, solve(x, y + 1, rem));
ret = max(ret, solve(x + 1, y, 3));
return ret;
}
int main() {
FIO
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >>
n >> m >> k;
for (int i = 0; i < k; i++) {
ll a, b, c;
cin >> a >> b >> c;
A[a - 1][b - 1] = c;
}
memset(mem, -1, sizeof mem);
cout << solve(0, 0, 3);
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename key>
using ordered_set = tree<key, null_type, less<key>, rb_tree_tag,
tree_order_statistics_node_update>;
#define F first
#define S second
#define T second.first
#define D second.second
#define FIO \
ios_base::sync_with_stdio(0); \
cin.tie(0);
#define all(v) v.begin(), v.end()
#define OO 0x3f3f3f3fLL
#define INF 0x3f3f3f3f3f3f3f3fLL
#define sz(s) (int)(s.size())
#define RT(s) return cout << s, 0
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, pair<ll, ll>> piii;
const int N = 3e3 + 5, M = 20, mod = 1000000000;
const double EPS = 1e-10;
ll n, m, k;
ll A[N][N], mem[N][N][4];
ll solve(int x, int y, int rem) {
if (x == n - 1 && y == m - 1) {
if (rem)
return A[x][y];
return 0;
}
if (x == n || y == m) {
return -INF;
}
ll &ret = mem[x][y][rem];
if (~ret)
return ret;
if (rem) {
ret = max(solve(x, y + 1, rem - 1), solve(x + 1, y, 3)) + A[x][y];
}
ret = max(ret, solve(x, y + 1, rem));
ret = max(ret, solve(x + 1, y, 3));
return ret;
}
int main() {
FIO
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >>
n >> m >> k;
for (int i = 0; i < k; i++) {
ll a, b, c;
cin >> a >> b >> c;
A[a - 1][b - 1] = c;
}
memset(mem, -1, sizeof mem);
cout << solve(0, 0, 3);
}
| replace | 24 | 25 | 24 | 25 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;
#define ll long long int
#define pb push_back
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define pre(i, j, n) for (ll i = j; i >= n; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<bool> vb;
#define br "\n"
#define MAX 1000001
#define ff first
#define ss second
vector<vll> v;
ll dp[3001][3001][4];
void solve() {
ll n, m, cunt;
cin >> n >> m >> cunt;
v.resize(n + 1, vll(m + 1, 0));
ll dp[n + 1][m + 1][4];
rep(i, 0, n + 1) {
rep(j, 0, m + 1) { rep(k, 0, 4) dp[i][j][k] = 0; }
}
rep(i, 0, cunt) {
ll a, b, val;
cin >> a >> b >> val;
v[a][b] = val;
}
// for(auto x:v){
// for(auto y:x) cout << y << " ";
// cout << br;
// }
rep(i, 1, n + 1) {
rep(j, 1, m + 1) {
ll mx = 0;
rep(k, 1, 4) mx = max(dp[i - 1][j][k], mx);
rep(k, 1, 4) dp[i][j][k] = max(
mx + v[i][j], max(dp[i][j - 1][k], dp[i][j - 1][k - 1] + v[i][j]));
// cout << dp[i][j][1] << " ";
}
// cout << br;
}
// for(auto x:sum){
// for(auto y:x) cout << y << " ";
// cout << br;
// }
ll ans = 0;
rep(i, 0, 4) ans = max(ans, dp[n][m][i]);
cout << ans << br;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t = 1;
// cin >> t;
rep(i, 0, t) { solve(); }
} | #include <bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;
#define ll long long int
#define pb push_back
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define pre(i, j, n) for (ll i = j; i >= n; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<bool> vb;
#define br "\n"
#define MAX 1000001
#define ff first
#define ss second
vector<vll> v;
ll dp[3001][3001][4];
void solve() {
ll n, m, cunt;
cin >> n >> m >> cunt;
v.resize(n + 1, vll(m + 1, 0));
rep(i, 0, n + 1) {
rep(j, 0, m + 1) { rep(k, 0, 4) dp[i][j][k] = 0; }
}
rep(i, 0, cunt) {
ll a, b, val;
cin >> a >> b >> val;
v[a][b] = val;
}
// for(auto x:v){
// for(auto y:x) cout << y << " ";
// cout << br;
// }
rep(i, 1, n + 1) {
rep(j, 1, m + 1) {
ll mx = 0;
rep(k, 1, 4) mx = max(dp[i - 1][j][k], mx);
rep(k, 1, 4) dp[i][j][k] = max(
mx + v[i][j], max(dp[i][j - 1][k], dp[i][j - 1][k - 1] + v[i][j]));
// cout << dp[i][j][1] << " ";
}
// cout << br;
}
// for(auto x:sum){
// for(auto y:x) cout << y << " ";
// cout << br;
// }
ll ans = 0;
rep(i, 0, 4) ans = max(ans, dp[n][m][i]);
cout << ans << br;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t = 1;
// cin >> t;
rep(i, 0, t) { solve(); }
} | delete | 27 | 28 | 27 | 27 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1e3 + 5;
ll r, c, k, x, y, z, g[N][N], dp[N][N][4];
int main() {
cin >> r >> c >> k;
while (k--) {
cin >> x >> y >> z;
g[x][y] = z;
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
for (int k = 1; k <= 3; k++) {
dp[i][j][k] = max(dp[i - 1][j][3] + g[i][j],
max(dp[i][j - 1][k - 1] + g[i][j], dp[i][j - 1][k]));
}
}
}
cout << dp[r][c][3];
}
| #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 3e3 + 5;
ll r, c, k, x, y, z, g[N][N], dp[N][N][4];
int main() {
cin >> r >> c >> k;
while (k--) {
cin >> x >> y >> z;
g[x][y] = z;
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
for (int k = 1; k <= 3; k++) {
dp[i][j][k] = max(dp[i - 1][j][3] + g[i][j],
max(dp[i][j - 1][k - 1] + g[i][j], dp[i][j - 1][k]));
}
}
}
cout << dp[r][c][3];
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
using pii = std::pair<int, int>;
using namespace std;
const int maxn = 3005;
int R, C, k, r, c, v, dp[4][maxn][maxn], val[maxn][maxn];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> R >> C >> k;
for (int i = 0; i < k; i++) {
cin >> r >> c >> v;
r--;
c--;
val[r][c] = v;
}
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
for (int k = 0; k < 4; k++) {
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
if (k < 3) {
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
dp[i][j + 1][k + 1] =
max(dp[i][j + 1][k + 1], dp[i][j][k] + val[i][j]);
}
}
int ans = 0;
for (int k = 0; k < 4; k++)
ans = max(ans, max(dp[R][C - 1][k], dp[R - 1][C][k]));
cout << ans << "\n";
return 0;
} | #include <bits/stdc++.h>
#define int long long
using pii = std::pair<int, int>;
using namespace std;
const int maxn = 3005;
int R, C, k, r, c, v, dp[maxn][maxn][4], val[maxn][maxn];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> R >> C >> k;
for (int i = 0; i < k; i++) {
cin >> r >> c >> v;
r--;
c--;
val[r][c] = v;
}
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
for (int k = 0; k < 4; k++) {
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
if (k < 3) {
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
dp[i][j + 1][k + 1] =
max(dp[i][j + 1][k + 1], dp[i][j][k] + val[i][j]);
}
}
int ans = 0;
for (int k = 0; k < 4; k++)
ans = max(ans, max(dp[R][C - 1][k], dp[R - 1][C][k]));
cout << ans << "\n";
return 0;
} | replace | 7 | 8 | 7 | 8 | -11 | |
p02586 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 3333;
int a[N][N];
ll f[N][N][3];
int n, m, k;
int main() {
int x, y, v;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; i++) {
scanf("%d%d%d", &x, &y, &v);
a[x][y] = v;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
for (int k = 0; k <= 3; k++) {
if (k)
f[i][j][k] = max(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
f[i][j][k] = max(f[i][j][k], f[i][j - 1][k]);
}
for (int k = 0; k <= 3; k++) {
f[i][j][0] = max(f[i][j][0], f[i - 1][j][k]);
f[i][j][1] = max(f[i][j][1], f[i - 1][j][k] + a[i][j]);
}
}
ll ans = 0;
for (int i = 0; i <= 3; i++)
ans = max(ans, f[n][m][i]);
printf("%lld\n", ans);
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 3333;
int a[N][N];
ll f[N][N][4];
int n, m, k;
int main() {
int x, y, v;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; i++) {
scanf("%d%d%d", &x, &y, &v);
a[x][y] = v;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
for (int k = 0; k <= 3; k++) {
if (k)
f[i][j][k] = max(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
f[i][j][k] = max(f[i][j][k], f[i][j - 1][k]);
}
for (int k = 0; k <= 3; k++) {
f[i][j][0] = max(f[i][j][0], f[i - 1][j][k]);
f[i][j][1] = max(f[i][j][1], f[i - 1][j][k] + a[i][j]);
}
}
ll ans = 0;
for (int i = 0; i <= 3; i++)
ans = max(ans, f[n][m][i]);
printf("%lld\n", ans);
return 0;
} | replace | 9 | 10 | 9 | 10 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;
ll mod(ll a, ll b) {
ll ret = a % b;
if (ret < 0)
ret += b;
return ret;
}
ll modpow(ll a, ll b, ll c) {
ll res = 1;
while (b > 0) {
if (b & 1)
res = mod(res * a, c);
a = mod(a * a, c);
b >>= 1;
}
return res;
}
int main() {
int r, c, k;
cin >> r >> c >> k;
int memo[r + 1][c + 1];
for (int i = 0; i < r + 1; i++) {
for (int j = 0; j < c + 1; j++) {
memo[i][j] = 0;
}
}
for (int i = 0; i < k; i++) {
int R, C, v;
cin >> R >> C >> v;
memo[R][C] = v;
}
ll dp[4][r + 1][c + 1];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < r + 1; j++) {
for (int l = 0; l < c + 1; l++) {
dp[i][j][l] = 0;
}
}
}
for (int i = 1; i < r + 1; i++) {
for (int j = 1; j < c + 1; j++) {
for (int l = 0; l < 4; l++) {
dp[0][i][j] = max(dp[0][i][j], dp[l][i - 1][j]);
}
dp[0][i][j] = max(dp[0][i][j], dp[0][i][j - 1]);
dp[1][i][j] = dp[1][i][j - 1];
dp[2][i][j] = dp[2][i][j - 1];
dp[3][i][j] = dp[3][i][j - 1];
if (memo[i][j] != 0) {
dp[1][i][j] = max(dp[1][i][j - 1], dp[0][i][j] + memo[i][j]);
dp[2][i][j] = max(dp[2][i][j - 1], dp[1][i][j - 1] + memo[i][j]);
dp[3][i][j] = max(dp[3][i][j - 1], dp[2][i][j - 1] + memo[i][j]);
}
}
}
ll ans = 0;
for (int i = 0; i < 4; i++) {
ans = max(ans, dp[i][r][c]);
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;
ll mod(ll a, ll b) {
ll ret = a % b;
if (ret < 0)
ret += b;
return ret;
}
ll modpow(ll a, ll b, ll c) {
ll res = 1;
while (b > 0) {
if (b & 1)
res = mod(res * a, c);
a = mod(a * a, c);
b >>= 1;
}
return res;
}
int main() {
int r, c, k;
cin >> r >> c >> k;
int memo[r + 1][c + 1];
for (int i = 0; i < r + 1; i++) {
for (int j = 0; j < c + 1; j++) {
memo[i][j] = 0;
}
}
for (int i = 0; i < k; i++) {
int R, C, v;
cin >> R >> C >> v;
memo[R][C] = v;
}
vector<vector<vector<ll>>> dp(
4, vector<vector<ll>>(r + 1, vector<ll>(c + 1, 0)));
for (int i = 1; i < r + 1; i++) {
for (int j = 1; j < c + 1; j++) {
for (int l = 0; l < 4; l++) {
dp[0][i][j] = max(dp[0][i][j], dp[l][i - 1][j]);
}
dp[0][i][j] = max(dp[0][i][j], dp[0][i][j - 1]);
dp[1][i][j] = dp[1][i][j - 1];
dp[2][i][j] = dp[2][i][j - 1];
dp[3][i][j] = dp[3][i][j - 1];
if (memo[i][j] != 0) {
dp[1][i][j] = max(dp[1][i][j - 1], dp[0][i][j] + memo[i][j]);
dp[2][i][j] = max(dp[2][i][j - 1], dp[1][i][j - 1] + memo[i][j]);
dp[3][i][j] = max(dp[3][i][j - 1], dp[2][i][j - 1] + memo[i][j]);
}
}
}
ll ans = 0;
for (int i = 0; i < 4; i++) {
ans = max(ans, dp[i][r][c]);
}
cout << ans << endl;
return 0;
}
| replace | 41 | 49 | 41 | 43 | 0 | |
p02586 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int N = 2e6 + 10;
const ll mod = 1e9 + 7;
int n, m, k;
ll a[3010][3010];
ll dp[3010][3010][4];
int main() {
scanf("%d%d%d", &n, &m, &k);
// for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)a[i][j] = -inf;
for (int i = 1; i <= k; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (a[x][y] != -inf)
while (1)
;
scanf("%lld", &a[x][y]);
}
// for (int i = 0; i <= n; i++)for (int j = 0; j <= m; j++)for (int x = 0; x
// <= 3; x++)dp[i][j][k] = -inf;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
dp[i][j][0] = 0;
dp[1][1][0] = 0;
if (a[1][1] >= 0)
dp[1][1][1] = a[1][1];
ll ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 1; k <= 3; k++) {
dp[i][j][k] = max(dp[i - 1][j][3] + a[i][j],
max(dp[i][j - 1][k - 1] + a[i][j], dp[i][j - 1][k]));
ans = max(ans, dp[i][j][k]);
}
}
}
printf("%lld\n", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int N = 2e6 + 10;
const ll mod = 1e9 + 7;
int n, m, k;
ll a[3010][3010];
ll dp[3010][3010][4];
int main() {
scanf("%d%d%d", &n, &m, &k);
// for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)a[i][j] = -inf;
for (int i = 1; i <= k; i++) {
int x, y;
scanf("%d%d", &x, &y);
scanf("%lld", &a[x][y]);
}
// for (int i = 0; i <= n; i++)for (int j = 0; j <= m; j++)for (int x = 0; x
// <= 3; x++)dp[i][j][k] = -inf;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
dp[i][j][0] = 0;
dp[1][1][0] = 0;
if (a[1][1] >= 0)
dp[1][1][1] = a[1][1];
ll ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 1; k <= 3; k++) {
dp[i][j][k] = max(dp[i - 1][j][3] + a[i][j],
max(dp[i][j - 1][k - 1] + a[i][j], dp[i][j - 1][k]));
ans = max(ans, dp[i][j][k]);
}
}
}
printf("%lld\n", ans);
return 0;
} | delete | 16 | 19 | 16 | 16 | TLE | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;
ll mod(ll a, ll b) {
ll ret = a % b;
if (ret < 0)
ret += b;
return ret;
}
ll modpow(ll a, ll b, ll c) {
ll res = 1;
while (b > 0) {
if (b & 1)
res = mod(res * a, c);
a = mod(a * a, c);
b >>= 1;
}
return res;
}
int main() {
int r, c, k;
cin >> r >> c >> k;
ll memo[r + 1][c + 1];
for (int i = 0; i < r + 1; i++) {
for (int j = 0; j < c + 1; j++) {
memo[i][j] = 0;
}
}
for (int i = 0; i < k; i++) {
int R, C;
ll v;
cin >> R >> C >> v;
memo[R][C] = v;
}
ll dp[r + 1][c + 1][4];
for (int i = 0; i < r + 1; i++) {
for (int j = 0; j < c + 1; j++) {
for (int l = 0; l < 4; l++) {
dp[i][j][l] = 0;
}
}
}
for (int i = 1; i < r + 1; i++) {
for (int j = 1; j < c + 1; j++) {
for (int l = 0; l < 4; l++) {
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
}
dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j][0] + memo[i][j]);
dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + memo[i][j]);
dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + memo[i][j]);
}
}
ll ans = 0;
for (int i = 0; i < 4; i++) {
ans = max(ans, dp[r][c][i]);
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;
ll mod(ll a, ll b) {
ll ret = a % b;
if (ret < 0)
ret += b;
return ret;
}
ll modpow(ll a, ll b, ll c) {
ll res = 1;
while (b > 0) {
if (b & 1)
res = mod(res * a, c);
a = mod(a * a, c);
b >>= 1;
}
return res;
}
int main() {
int r, c, k;
cin >> r >> c >> k;
ll memo[r + 1][c + 1];
for (int i = 0; i < r + 1; i++) {
for (int j = 0; j < c + 1; j++) {
memo[i][j] = 0;
}
}
for (int i = 0; i < k; i++) {
int R, C;
ll v;
cin >> R >> C >> v;
memo[R][C] = v;
}
vector<vector<vector<ll>>> dp(r + 1,
vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
for (int i = 0; i < r + 1; i++) {
for (int j = 0; j < c + 1; j++) {
for (int l = 0; l < 4; l++) {
dp[i][j][l] = 0;
}
}
}
for (int i = 1; i < r + 1; i++) {
for (int j = 1; j < c + 1; j++) {
for (int l = 0; l < 4; l++) {
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
}
dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j][0] + memo[i][j]);
dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + memo[i][j]);
dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + memo[i][j]);
}
}
ll ans = 0;
for (int i = 0; i < 4; i++) {
ans = max(ans, dp[r][c][i]);
}
cout << ans << endl;
return 0;
}
| replace | 42 | 43 | 42 | 44 | 0 | |
p02586 | C++ | Runtime Error | // Hail god Yato
#include <bits/stdc++.h>
using namespace std;
#define hs \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 3005;
//
//
int r, c;
ll g[MAX][MAX];
ll dp[MAX][MAX][4]; // 0 1 2 3
ll check(int x, int y, int type) {
if (x > r || y > c || x <= 0 || y <= 0 || type < 0)
return LONG_LONG_MIN;
ll &ans = dp[x][y][type];
if (ans != -1)
return ans;
if (x == 1 && y == 1) {
if (type != 0)
return g[1][1];
// else if(type == 0)
return 0;
}
ans = max({max({check(x - 1, y, 3), check(x - 1, y, 2), check(x - 1, y, 1),
check(x - 1, y, 0)}) +
((type > 0) ? g[x][y] : 0),
check(x, y - 1, type), check(x, y - 1, type - 1) + g[x][y]});
return ans;
}
void solve() {
cin >> r >> c;
int k;
cin >> k;
memset(g, 0, sizeof(g));
memset(dp, -1, sizeof(dp));
for (int i = 0; i < k; i++) {
int x, y, val;
cin >> x >> y >> val;
g[x][y] = val;
}
cout << max({check(r, c, 0), check(r, c, 1), check(r, c, 2), check(r, c, 3)})
<< "\n";
}
int main() {
hs;
ll t;
t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
} | // Hail god Yato
#include <bits/stdc++.h>
using namespace std;
#define hs \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 3005;
//
//
int r, c;
ll g[MAX][MAX];
ll dp[MAX][MAX][4]; // 0 1 2 3
ll check(int x, int y, int type) {
if (x > r || y > c || x <= 0 || y <= 0 || type < 0)
return LONG_LONG_MIN;
ll &ans = dp[x][y][type];
if (ans != -1)
return ans;
if (x == 1 && y == 1) {
if (type != 0)
return g[1][1];
// else if(type == 0)
return 0;
}
ans = max({max({check(x - 1, y, 3), check(x - 1, y, 2), check(x - 1, y, 1),
check(x - 1, y, 0)}) +
((type > 0) ? g[x][y] : 0),
check(x, y - 1, type), check(x, y - 1, type - 1) + g[x][y]});
return ans;
}
void solve() {
cin >> r >> c;
int k;
cin >> k;
memset(g, 0, sizeof(g));
memset(dp, -1, sizeof(dp));
for (int i = 0; i < k; i++) {
int x, y, val;
cin >> x >> y >> val;
g[x][y] = val;
}
cout << max({check(r, c, 0), check(r, c, 1), check(r, c, 2), check(r, c, 3)})
<< "\n";
}
int main() {
hs;
ll t;
t = 1;
// cin>>t;
for (int i = 1; i <= t; i++) {
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
} | replace | 58 | 59 | 58 | 59 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
long long dp[6][6][4];
int main() {
int R, C, K;
cin >> R >> C >> K;
vector<vector<long long>> table(R, vector<long long>(C));
for (int i = 0; i < K; ++i) {
int r, c, v;
cin >> r >> c >> v;
r--;
c--;
table[r][c] = v;
}
// init
for (int i = 0; i < R; ++i) {
for (int j = 0; j < C; ++j) {
for (int k = 0; k < 3; ++k) {
// 右に行く場合
// 使う場合
dp[i][j + 1][k + 1] =
max(dp[i][j + 1][k + 1], dp[i][j][k] + table[i][j]);
// 使わない場合
dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k + 1]);
// 下に行く場合
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j + 1][k + 1]);
}
}
}
cout << dp[R][C - 1][0] << endl;
} | #include <bits/stdc++.h>
using namespace std;
long long dp[3005][3005][4];
int main() {
int R, C, K;
cin >> R >> C >> K;
vector<vector<long long>> table(R, vector<long long>(C));
for (int i = 0; i < K; ++i) {
int r, c, v;
cin >> r >> c >> v;
r--;
c--;
table[r][c] = v;
}
// init
for (int i = 0; i < R; ++i) {
for (int j = 0; j < C; ++j) {
for (int k = 0; k < 3; ++k) {
// 右に行く場合
// 使う場合
dp[i][j + 1][k + 1] =
max(dp[i][j + 1][k + 1], dp[i][j][k] + table[i][j]);
// 使わない場合
dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k + 1]);
// 下に行く場合
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j + 1][k + 1]);
}
}
}
cout << dp[R][C - 1][0] << endl;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02586 | C++ | Runtime Error | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
if (v.empty()) {
os << "{ }";
return os;
}
os << "{" << v.front();
for (auto itr = ++v.begin(); itr != v.end(); itr++) {
os << ", " << *itr;
}
os << "}";
return os;
}
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
int main() {
ll R, C, k;
cin >> R >> C >> k;
vector<vector<ll>> m(R, vector<ll>(C, -1));
rep(i, k) {
ll r, c, v;
cin >> r >> c >> v;
r--;
c--;
m[r][c] = v;
}
ll dp[4][R + 1][C];
rep(i, 4) rep(j, R + 1) rep(K, C) dp[i][j][K] = -1;
dp[0][0][0] = 0;
rep(i, R) rep(j, C) for (int K = 3; K >= 0; K--) {
if (dp[K][i][j] == -1)
continue;
if (j != C - 1)
dp[K][i][j + 1] = max(dp[K][i][j + 1], dp[K][i][j]);
dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[K][i][j]);
if (m[i][j] != -1) {
if (K < 3 && dp[K + 1][i][j] < dp[K][i][j] + m[i][j]) {
dp[K + 1][i][j] = dp[K][i][j] + m[i][j];
if (j != C - 1)
dp[K + 1][i][j + 1] = max(dp[K + 1][i][j + 1], dp[K + 1][i][j]);
dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[K + 1][i][j]);
}
}
}
ll ans = 0;
rep(i, 4) rep(j, C) ans = max(ans, dp[i][R][j]);
cout << ans << endl;
} | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
if (v.empty()) {
os << "{ }";
return os;
}
os << "{" << v.front();
for (auto itr = ++v.begin(); itr != v.end(); itr++) {
os << ", " << *itr;
}
os << "}";
return os;
}
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
int main() {
ll R, C, k;
cin >> R >> C >> k;
vector<vector<ll>> m(R, vector<ll>(C, -1));
rep(i, k) {
ll r, c, v;
cin >> r >> c >> v;
r--;
c--;
m[r][c] = v;
}
vector<vector<vector<ll>>> dp(4,
vector<vector<ll>>(R + 1, vector<ll>(C, -1)));
dp[0][0][0] = 0;
rep(i, R) rep(j, C) for (int K = 3; K >= 0; K--) {
if (dp[K][i][j] == -1)
continue;
if (j != C - 1)
dp[K][i][j + 1] = max(dp[K][i][j + 1], dp[K][i][j]);
dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[K][i][j]);
if (m[i][j] != -1) {
if (K < 3 && dp[K + 1][i][j] < dp[K][i][j] + m[i][j]) {
dp[K + 1][i][j] = dp[K][i][j] + m[i][j];
if (j != C - 1)
dp[K + 1][i][j + 1] = max(dp[K + 1][i][j + 1], dp[K + 1][i][j]);
dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[K + 1][i][j]);
}
}
}
ll ans = 0;
rep(i, 4) rep(j, C) ans = max(ans, dp[i][R][j]);
cout << ans << endl;
} | replace | 42 | 44 | 42 | 44 | 0 | |
p02586 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define int long long
#define INF 1e18
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define endl "\n"
#define GCD(x, y) (__gcd((x), (y)))
#define LCM(x, y) (((x) / __gcd((x), (y))) * (y))
#define debug(x) cout << #x << " is " << (x) << endl
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define sp(ans, p) fixed << setprecision(p) << ans;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
constexpr int N = 3005;
int r, c, k;
int arr[N][N];
int dp[N][N][4];
int recur(int r, int c, int pick) {
if (r < 0 || c < 0 || pick <= 0) {
return 0;
}
if (dp[r][c][pick] != -1)
return dp[r][c][pick];
int a = recur(r - 1, c, 3) + arr[r][c];
int b = recur(r, c - 1, pick - 1) + arr[r][c];
int cc = recur(r, c - 1, pick);
int z = max(a, max(b, cc));
return z;
}
void solve() {
mem(dp, -1);
cin >> r >> c >> k;
rep(i, 0, k) {
int a, b, v;
cin >> a >> b >> v;
a--, b--;
arr[a][b] = v;
}
cout << recur(r - 1, c - 1, 3) << endl;
return;
}
signed main() {
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// sieve();
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
} | #include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define int long long
#define INF 1e18
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define endl "\n"
#define GCD(x, y) (__gcd((x), (y)))
#define LCM(x, y) (((x) / __gcd((x), (y))) * (y))
#define debug(x) cout << #x << " is " << (x) << endl
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define sp(ans, p) fixed << setprecision(p) << ans;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
constexpr int N = 3005;
int r, c, k;
int arr[N][N];
int dp[N][N][4];
int recur(int r, int c, int pick) {
if (r < 0 || c < 0 || pick <= 0) {
return 0;
}
if (dp[r][c][pick] != -1)
return dp[r][c][pick];
int a = recur(r - 1, c, 3) + arr[r][c];
int b = recur(r, c - 1, pick - 1) + arr[r][c];
int cc = recur(r, c - 1, pick);
int z = max(a, max(b, cc));
return dp[r][c][pick] = z;
}
void solve() {
mem(dp, -1);
cin >> r >> c >> k;
rep(i, 0, k) {
int a, b, v;
cin >> a >> b >> v;
a--, b--;
arr[a][b] = v;
}
cout << recur(r - 1, c - 1, 3) << endl;
return;
}
signed main() {
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// sieve();
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
| replace | 48 | 49 | 48 | 49 | TLE | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <float.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const ll INF = 1LL << 62;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
const int MOD = 1000000007;
const int max_v = 3001;
ll dp[max_v][max_v][4];
int main() {
int R, C, K;
cin >> R >> C >> K;
vector<vector<ll>> rcv(R, vector<ll>(C, 0));
rep(i, K) {
ll tr, tc, tv;
cin >> tr >> tc >> tv;
tr--;
tc--;
rcv[tr][tc] = tv;
}
rep(i, max_v) {
rep(j, max_v) {
rep(k, 4) { dp[i][j][k] = -INF; }
}
}
dp[0][0][0] = 0;
dp[0][0][1] = rcv[0][0];
rep(i, R) {
rep(j, C) {
rep(k, 4) {
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + rcv[i + 1][j]);
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
if (k + 1 < 4) {
dp[i][j + 1][k + 1] =
max(dp[i][j + 1][k + 1], dp[i][j][k] + rcv[i][j + 1]);
}
}
}
}
ll ans = -1;
rep(i, 4) { ans = max(ans, dp[R - 1][C - 1][i]); }
cout << ans << endl;
} | #include <bits/stdc++.h>
#include <float.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const ll INF = 1LL << 62;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
const int MOD = 1000000007;
const int max_v = 3001;
ll dp[max_v][max_v][4];
int main() {
int R, C, K;
cin >> R >> C >> K;
vector<vector<ll>> rcv(R + 1, vector<ll>(C + 1, 0));
rep(i, K) {
ll tr, tc, tv;
cin >> tr >> tc >> tv;
tr--;
tc--;
rcv[tr][tc] = tv;
}
rep(i, max_v) {
rep(j, max_v) {
rep(k, 4) { dp[i][j][k] = -INF; }
}
}
dp[0][0][0] = 0;
dp[0][0][1] = rcv[0][0];
rep(i, R) {
rep(j, C) {
rep(k, 4) {
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + rcv[i + 1][j]);
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
if (k + 1 < 4) {
dp[i][j + 1][k + 1] =
max(dp[i][j + 1][k + 1], dp[i][j][k] + rcv[i][j + 1]);
}
}
}
}
ll ans = -1;
rep(i, 4) { ans = max(ans, dp[R - 1][C - 1][i]); }
cout << ans << endl;
} | replace | 38 | 39 | 38 | 39 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
typedef pair<ll, ll> Pair;
template <class T> T input() {
T t;
cin >> t;
return t;
}
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define FIND(x, key) x.find(key) != x.end()
#define IN_RANGE_V2(v, k, x, y) \
(0 <= x + dx[k] && x + dx[k] < v[y].size() && 0 <= y + dy[k] && \
y + dy[k] < v.size())
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define PRINT_ARRAY_H(a) \
REPALL(__i, a) { cout << a[__i] << (__i != a.size() - 1 ? " " : ""); } \
newline;
#define PRINT_ARRAY_V(a) \
REPALL(__i, a) { cout << a[__i] << endl; }
#define e1 first
#define e2 second
#define newline putchar('\n')
#define cspace ' '
#define sspace " "
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};
// const VI dx = {1, 1, 0, -1, -1, -1, 0, 1};
// const VI dy = {0, 1, 1, 1, 0, -1, -1, -1};
VVLL values(3333, VLL(3333));
VVLL cval(3333, VLL(4));
void solve() {
ll R, C, K;
cin >> R >> C >> K;
REP(i, K) {
ll r, c, v;
cin >> r >> c >> v;
r--;
c--;
values[r][c] = v;
}
cval[0][1] = values[0][0];
REP(i, R + C - 1) {
VVLL tval(3333, VLL(4));
REP(j, 3333) {
ll k = i - j;
if (k < 0)
break;
else if (k >= C)
continue;
REP(t, 4) {
CHMAX(tval[j][t], cval[j][t]);
if (t < 3)
CHMAX(tval[j][t + 1], cval[j][t] + values[j][k + 1]);
CHMAX(tval[j + 1][0], cval[j][t]);
CHMAX(tval[j + 1][1], cval[j][t] + values[j + 1][k]);
}
}
cval = tval;
// cout<<i<<sspace<<*MAX_ELEMENT(cval[i])<<endl;
}
cout << *MAX_ELEMENT(cval[R]) << endl;
}
int main() {
cout << fixed << setprecision(20);
int __t = 1;
// cin>>__t;
REP(i, __t) { solve(); }
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
typedef pair<ll, ll> Pair;
template <class T> T input() {
T t;
cin >> t;
return t;
}
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define FIND(x, key) x.find(key) != x.end()
#define IN_RANGE_V2(v, k, x, y) \
(0 <= x + dx[k] && x + dx[k] < v[y].size() && 0 <= y + dy[k] && \
y + dy[k] < v.size())
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define PRINT_ARRAY_H(a) \
REPALL(__i, a) { cout << a[__i] << (__i != a.size() - 1 ? " " : ""); } \
newline;
#define PRINT_ARRAY_V(a) \
REPALL(__i, a) { cout << a[__i] << endl; }
#define e1 first
#define e2 second
#define newline putchar('\n')
#define cspace ' '
#define sspace " "
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};
// const VI dx = {1, 1, 0, -1, -1, -1, 0, 1};
// const VI dy = {0, 1, 1, 1, 0, -1, -1, -1};
VVLL values(3333, VLL(3333));
VVLL cval(3333, VLL(4));
void solve() {
ll R, C, K;
cin >> R >> C >> K;
REP(i, K) {
ll r, c, v;
cin >> r >> c >> v;
r--;
c--;
values[r][c] = v;
}
cval[0][1] = values[0][0];
REP(i, R + C - 1) {
VVLL tval(3333, VLL(4));
REP(j, R) {
ll k = i - j;
if (k < 0)
break;
else if (k >= C)
continue;
REP(t, 4) {
CHMAX(tval[j][t], cval[j][t]);
if (t < 3)
CHMAX(tval[j][t + 1], cval[j][t] + values[j][k + 1]);
CHMAX(tval[j + 1][0], cval[j][t]);
CHMAX(tval[j + 1][1], cval[j][t] + values[j + 1][k]);
}
}
cval = tval;
// cout<<i<<sspace<<*MAX_ELEMENT(cval[i])<<endl;
}
cout << *MAX_ELEMENT(cval[R]) << endl;
}
int main() {
cout << fixed << setprecision(20);
int __t = 1;
// cin>>__t;
REP(i, __t) { solve(); }
return 0;
} | replace | 72 | 73 | 72 | 73 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
typedef pair<ll, ll> Pair;
template <class T> T input() {
T t;
cin >> t;
return t;
}
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define FIND(x, key) x.find(key) != x.end()
#define IN_RANGE_V2(v, k, x, y) \
(0 <= x + dx[k] && x + dx[k] < v[y].size() && 0 <= y + dy[k] && \
y + dy[k] < v.size())
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define PRINT_ARRAY_H(a) \
REPALL(__i, a) { cout << a[__i] << (__i != a.size() - 1 ? " " : ""); } \
newline;
#define PRINT_ARRAY_V(a) \
REPALL(__i, a) { cout << a[__i] << endl; }
#define e1 first
#define e2 second
#define newline putchar('\n')
#define cspace ' '
#define sspace " "
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};
// const VI dx = {1, 1, 0, -1, -1, -1, 0, 1};
// const VI dy = {0, 1, 1, 1, 0, -1, -1, -1};
VVLL values(3333, VLL(333));
vector<vector<vector<ll>>> dp(3333, VVLL(3333, VLL(5, 0)));
void solve() {
ll R, C, K;
cin >> R >> C >> K;
REP(i, K) {
ll r, c, v;
cin >> r >> c >> v;
r--;
c--;
values[r][c] = v;
}
dp[0][0][1] = values[0][0];
REP(i, R + C - 1) {
REP(j, R) {
ll k = i - j;
if (k < 0)
break;
else if (k >= C)
continue;
REP(t, 4) {
CHMAX(dp[j][k + 1][t], dp[j][k][t]);
CHMAX(dp[j][k + 1][t + 1], dp[j][k][t] + values[j][k + 1]);
CHMAX(dp[j + 1][k][0], dp[j][k][t]);
CHMAX(dp[j + 1][k][1], dp[j][k][t] + values[j + 1][k]);
}
}
}
cout << *MAX_ELEMENT(dp[R - 1][C - 1]) << endl;
}
int main() {
cout << fixed << setprecision(20);
int __t = 1;
// cin>>__t;
REP(i, __t) { solve(); }
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
typedef pair<ll, ll> Pair;
template <class T> T input() {
T t;
cin >> t;
return t;
}
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define FIND(x, key) x.find(key) != x.end()
#define IN_RANGE_V2(v, k, x, y) \
(0 <= x + dx[k] && x + dx[k] < v[y].size() && 0 <= y + dy[k] && \
y + dy[k] < v.size())
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define PRINT_ARRAY_H(a) \
REPALL(__i, a) { cout << a[__i] << (__i != a.size() - 1 ? " " : ""); } \
newline;
#define PRINT_ARRAY_V(a) \
REPALL(__i, a) { cout << a[__i] << endl; }
#define e1 first
#define e2 second
#define newline putchar('\n')
#define cspace ' '
#define sspace " "
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};
// const VI dx = {1, 1, 0, -1, -1, -1, 0, 1};
// const VI dy = {0, 1, 1, 1, 0, -1, -1, -1};
VVLL values(3333, VLL(3333));
vector<vector<vector<ll>>> dp(3333, VVLL(3333, VLL(4, 0)));
void solve() {
ll R, C, K;
cin >> R >> C >> K;
REP(i, K) {
ll r, c, v;
cin >> r >> c >> v;
r--;
c--;
values[r][c] = v;
}
dp[0][0][1] = values[0][0];
REP(i, R + C - 1) {
REP(j, R) {
ll k = i - j;
if (k < 0)
break;
else if (k >= C)
continue;
REP(t, 4) {
CHMAX(dp[j][k + 1][t], dp[j][k][t]);
CHMAX(dp[j][k + 1][t + 1], dp[j][k][t] + values[j][k + 1]);
CHMAX(dp[j + 1][k][0], dp[j][k][t]);
CHMAX(dp[j + 1][k][1], dp[j][k][t] + values[j + 1][k]);
}
}
}
cout << *MAX_ELEMENT(dp[R - 1][C - 1]) << endl;
}
int main() {
cout << fixed << setprecision(20);
int __t = 1;
// cin>>__t;
REP(i, __t) { solve(); }
return 0;
}
| replace | 55 | 57 | 55 | 57 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02586 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const double pi = acos(-1.0);
const ll MOD = 1000000007;
const ll INF = 1e18;
const ll MAX_N = 201010;
// 組み合わせの余りを求める
ll fac[MAX_N], finv[MAX_N], inv[MAX_N];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX_N; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) {
if (x == 0 || y == 0)
return 0;
return (x / gcd(x, y) * y);
}
ll dx[4] = {0, 0, -1, 1};
ll dy[4] = {-1, 1, 0, 0};
char notes[110][10];
ll tre[3030][3030];
ll dp[3030][3030][4];
// long longしか使わない
// 素数は1より大きい
// lower_boundは指定したkey以上の要素の一番左のイテレータをかえす
// upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす
int main() {
ll r, c, n;
cin >> r >> c >> n;
rep(i, n) {
ll x, y, v;
x--;
y--;
tre[x][y] = v;
}
rep(i, r) {
rep(j, c) {
for (ll k = 2; k >= 0; --k) {
chmax(dp[i][j][k + 1], dp[i][j][k] + tre[i][j]);
}
rep(k, 4) {
chmax(dp[i + 1][j][0], dp[i][j][k]);
chmax(dp[i][j + 1][k], dp[i][j][k]);
}
}
}
ll ans = 0;
rep(i, 4) { chmax(ans, dp[r - 1][c - 1][i]); }
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const double pi = acos(-1.0);
const ll MOD = 1000000007;
const ll INF = 1e18;
const ll MAX_N = 201010;
// 組み合わせの余りを求める
ll fac[MAX_N], finv[MAX_N], inv[MAX_N];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX_N; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) {
if (x == 0 || y == 0)
return 0;
return (x / gcd(x, y) * y);
}
ll dx[4] = {0, 0, -1, 1};
ll dy[4] = {-1, 1, 0, 0};
char notes[110][10];
ll tre[3030][3030];
ll dp[3030][3030][4];
// long longしか使わない
// 素数は1より大きい
// lower_boundは指定したkey以上の要素の一番左のイテレータをかえす
// upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす
int main() {
ll r, c, n;
cin >> r >> c >> n;
rep(i, n) {
ll x, y, v;
cin >> x >> y >> v;
x--;
y--;
tre[x][y] = v;
}
rep(i, r) {
rep(j, c) {
for (ll k = 2; k >= 0; --k) {
chmax(dp[i][j][k + 1], dp[i][j][k] + tre[i][j]);
}
rep(k, 4) {
chmax(dp[i + 1][j][0], dp[i][j][k]);
chmax(dp[i][j + 1][k], dp[i][j][k]);
}
}
}
ll ans = 0;
rep(i, 4) { chmax(ans, dp[r - 1][c - 1][i]); }
cout << ans << endl;
return 0;
}
| insert | 82 | 82 | 82 | 83 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;
#define vec vector<int>
#define vecll vector<ll>
#define vecd vector<double>
#define vecst vector<string>
#define vecb vector<bool>
#define v2(v, n, m, init) vector<vector<int>> v(n, vector<int>(m, init))
#define vb2(v, n, m, init) vector<vector<bool>> v(n, vector<bool>(m, init))
#define vll2(v, n, m, init) vector<vector<ll>> v(n, vector<ll>(m, init))
#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define arr(var, n) \
vec var(n); \
rep(i, n) { cin >> var[i]; }
#define arrll(var, n) \
vecll var(n); \
rep(i, n) { cin >> var[i]; }
#define arrst(var, n) \
vecst var(n); \
rep(i, n) { cin >> var[i]; }
#define all(var) (var).begin(), (var).end()
#define sortall(var) sort(all(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define pb(var) push_back(var)
#define prt(var) cout << (var) << "\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtall(v) \
rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\n"); }
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
//----------------------------------------------------------------
int main(void) {
ll dp[3010][3010][8];
int R, C, K;
cin >> R >> C >> K;
vll2(item, R + 1, C + 1, -1);
rep(i, K) {
int a, b;
ll v;
cin >> a >> b >> v;
item[a][b] = v;
}
rep(i, 3010) {
rep(j, 3010) {
rep(k, 8) { dp[i][j][k] = 0; }
}
}
REP(i, 1, R + 1) {
REP(j, 1, C + 1) {
ll tmp = 0;
rep(k, 4) { chmax(tmp, dp[i - 1][j][k]); }
chmax(dp[i][j][0], tmp);
if (item[i][j] > 0)
chmax(dp[i][j][1], tmp + item[i][j]);
rep(k, 4) {
chmax(dp[i][j][k], dp[i][j - 1][k]);
if (k != 0 && item[i][j] > 0) {
chmax(dp[i][j][k], dp[i][j - 1][k - 1] + item[i][j]);
}
}
}
}
ll ans = 0;
rep(i, 4) { chmax(ans, dp[R][C][i]); }
prt(ans);
}
| #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;
#define vec vector<int>
#define vecll vector<ll>
#define vecd vector<double>
#define vecst vector<string>
#define vecb vector<bool>
#define v2(v, n, m, init) vector<vector<int>> v(n, vector<int>(m, init))
#define vb2(v, n, m, init) vector<vector<bool>> v(n, vector<bool>(m, init))
#define vll2(v, n, m, init) vector<vector<ll>> v(n, vector<ll>(m, init))
#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define arr(var, n) \
vec var(n); \
rep(i, n) { cin >> var[i]; }
#define arrll(var, n) \
vecll var(n); \
rep(i, n) { cin >> var[i]; }
#define arrst(var, n) \
vecst var(n); \
rep(i, n) { cin >> var[i]; }
#define all(var) (var).begin(), (var).end()
#define sortall(var) sort(all(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define pb(var) push_back(var)
#define prt(var) cout << (var) << "\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtall(v) \
rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\n"); }
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
//----------------------------------------------------------------
int main(void) {
ll ***dp = new ll **[3010];
for (int i = 0; i < 3010; i++) {
dp[i] = new ll *[3010];
for (int j = 0; j < 3010; j++) {
dp[i][j] = new ll[8];
}
}
int R, C, K;
cin >> R >> C >> K;
vll2(item, R + 1, C + 1, -1);
rep(i, K) {
int a, b;
ll v;
cin >> a >> b >> v;
item[a][b] = v;
}
rep(i, 3010) {
rep(j, 3010) {
rep(k, 8) { dp[i][j][k] = 0; }
}
}
REP(i, 1, R + 1) {
REP(j, 1, C + 1) {
ll tmp = 0;
rep(k, 4) { chmax(tmp, dp[i - 1][j][k]); }
chmax(dp[i][j][0], tmp);
if (item[i][j] > 0)
chmax(dp[i][j][1], tmp + item[i][j]);
rep(k, 4) {
chmax(dp[i][j][k], dp[i][j - 1][k]);
if (k != 0 && item[i][j] > 0) {
chmax(dp[i][j][k], dp[i][j - 1][k - 1] + item[i][j]);
}
}
}
}
ll ans = 0;
rep(i, 4) { chmax(ans, dp[R][C][i]); }
prt(ans);
}
| replace | 53 | 54 | 53 | 61 | -11 | |
p02586 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(0)
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = 3456;
int val[MAXN][MAXN];
LL dp[MAXN][MAXN][3];
int main() {
int R, C, K;
scanf("%d %d %d", &R, &C, &K);
for (int i = 0; i < K; i++) {
int r, c, v;
scanf("%d %d %d", &r, &c, &v);
val[r][c] = v;
}
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
for (int k = 0; k <= 3; k++) {
if (k < 3)
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
// not take
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
// not take
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
if (k) // take
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k - 1] + val[i][j]);
// printf("i = %d j = %d k = %d dp = %lld\n", i, j, k, dp[i][j][k]);
}
}
}
LL ans = 0;
for (int k = 0; k <= 3; k++) {
if (k != 3)
dp[R][C][k] += val[R][C];
ans = max(ans, dp[R][C][k]);
}
printf("%lld\n", ans);
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
by Benq;
*/
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(0)
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = 3456;
int val[MAXN][MAXN];
LL dp[MAXN][MAXN][4];
int main() {
int R, C, K;
scanf("%d %d %d", &R, &C, &K);
for (int i = 0; i < K; i++) {
int r, c, v;
scanf("%d %d %d", &r, &c, &v);
val[r][c] = v;
}
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
for (int k = 0; k <= 3; k++) {
if (k < 3)
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
// not take
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
// not take
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
if (k) // take
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k - 1] + val[i][j]);
// printf("i = %d j = %d k = %d dp = %lld\n", i, j, k, dp[i][j][k]);
}
}
}
LL ans = 0;
for (int k = 0; k <= 3; k++) {
if (k != 3)
dp[R][C][k] += val[R][C];
ans = max(ans, dp[R][C][k]);
}
printf("%lld\n", ans);
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
by Benq;
*/
| replace | 25 | 26 | 25 | 26 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
ll gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } // 最小公倍数
using Graph = vector<vector<int>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
int main() {
int r, c, k;
cin >> r >> c >> k;
ll o[r][c];
rep(i, r) rep(j, c) o[i][j] = 0;
rep(i, k) {
ll a, b, c;
cin >> a >> b >> c;
a--;
b--;
o[a][b] = c;
}
ll dp[r][c][4];
rep(i, r) rep(j, c) rep(l, 4) dp[i][j][l] = 0;
dp[0][0][1] = o[0][0];
rep(i, r) {
rep(j, c) {
if (i + j == 0)
continue;
if (o[i][j] == 0) {
rep(l, 4) {
if (j != 0) {
dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
}
if (i != 0) {
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
}
}
} else {
if (j != 0) {
rep(l, 4) {
dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
if (l != 3)
dp[i][j][l + 1] = max(dp[i][j][l + 1], o[i][j] + dp[i][j - 1][l]);
}
}
if (i != 0) {
rep(l, 4) {
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
dp[i][j][1] = max(dp[i][j][1], o[i][j] + dp[i - 1][j][l]);
}
}
}
}
}
ll ans = 0;
rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
ll gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } // 最小公倍数
using Graph = vector<vector<int>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
int main() {
int r, c, k;
cin >> r >> c >> k;
ll o[r][c];
rep(i, r) rep(j, c) o[i][j] = 0;
rep(i, k) {
ll a, b, c;
cin >> a >> b >> c;
a--;
b--;
o[a][b] = c;
}
vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4)));
rep(i, r) rep(j, c) rep(l, 4) dp[i][j][l] = 0;
dp[0][0][1] = o[0][0];
rep(i, r) {
rep(j, c) {
if (i + j == 0)
continue;
if (o[i][j] == 0) {
rep(l, 4) {
if (j != 0) {
dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
}
if (i != 0) {
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
}
}
} else {
if (j != 0) {
rep(l, 4) {
dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
if (l != 3)
dp[i][j][l + 1] = max(dp[i][j][l + 1], o[i][j] + dp[i][j - 1][l]);
}
}
if (i != 0) {
rep(l, 4) {
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
dp[i][j][1] = max(dp[i][j][1], o[i][j] + dp[i - 1][j][l]);
}
}
}
}
}
ll ans = 0;
rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
cout << ans << endl;
}
| replace | 21 | 22 | 21 | 22 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define l long
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define pii pair<int, int>
using namespace std;
ll dp[3000][3000][4];
ll mat[3000][3000];
int main() {
fast;
ll n, m, k, j, i;
cin >> n >> m >> k;
while (k--) {
ll r, c, v;
cin >> r >> c >> v;
mat[r][c] = v;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
ll ha = max(
{dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2], dp[i - 1][j][3]});
dp[i][j][0] = max({ha, dp[i][j - 1][0]});
dp[i][j][1] =
max({ha + mat[i][j], dp[i][j - 1][0] + mat[i][j], dp[i][j - 1][1]});
dp[i][j][2] = max(dp[i][j - 1][1] + mat[i][j], dp[i][j - 1][2]);
dp[i][j][3] = max(dp[i][j - 1][2] + mat[i][j], dp[i][j - 1][3]);
}
}
cout << max({dp[n][m][0], dp[n][m][1], dp[n][m][2], dp[n][m][3]});
} | #include <bits/stdc++.h>
#define ll long long
#define l long
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define pii pair<int, int>
using namespace std;
ll dp[3003][3003][5];
ll mat[3003][3003];
int main() {
fast;
ll n, m, k, j, i;
cin >> n >> m >> k;
while (k--) {
ll r, c, v;
cin >> r >> c >> v;
mat[r][c] = v;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
ll ha = max(
{dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2], dp[i - 1][j][3]});
dp[i][j][0] = max({ha, dp[i][j - 1][0]});
dp[i][j][1] =
max({ha + mat[i][j], dp[i][j - 1][0] + mat[i][j], dp[i][j - 1][1]});
dp[i][j][2] = max(dp[i][j - 1][1] + mat[i][j], dp[i][j - 1][2]);
dp[i][j][3] = max(dp[i][j - 1][2] + mat[i][j], dp[i][j - 1][3]);
}
}
cout << max({dp[n][m][0], dp[n][m][1], dp[n][m][2], dp[n][m][3]});
} | replace | 15 | 17 | 15 | 17 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll g[3001][3001]{};
ll a[3001][3001][4]{};
int main() {
int R, C, K;
cin >> R >> C >> K;
int r, c, v;
for (int i = 0; i < K; i++) {
cin >> r >> c >> v;
g[r][c] = v;
}
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
for (int k = 0; k <= 3; k++) {
a[i + 1][j][0] = max(a[i + 1][j][0], a[i][j][k]);
a[i][j + 1][k] = max(a[i][j + 1][k], a[i][j][k]);
if (k == 3) {
a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
} else {
a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
a[i][j + 1][k + 1] =
max(a[i][j + 1][k + 1], a[i][j][k] + g[i][j + 1]);
}
}
}
}
ll best = 0;
for (int i = 0; i <= 3; i++) {
best = max(best, a[R][C][i]);
}
cout << best << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll g[3002][3002]{};
ll a[3002][3002][4]{};
int main() {
int R, C, K;
cin >> R >> C >> K;
int r, c, v;
for (int i = 0; i < K; i++) {
cin >> r >> c >> v;
g[r][c] = v;
}
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
for (int k = 0; k <= 3; k++) {
a[i + 1][j][0] = max(a[i + 1][j][0], a[i][j][k]);
a[i][j + 1][k] = max(a[i][j + 1][k], a[i][j][k]);
if (k == 3) {
a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
} else {
a[i + 1][j][1] = max(a[i + 1][j][1], a[i][j][k] + g[i + 1][j]);
a[i][j + 1][k + 1] =
max(a[i][j + 1][k + 1], a[i][j][k] + g[i][j + 1]);
}
}
}
}
ll best = 0;
for (int i = 0; i <= 3; i++) {
best = max(best, a[R][C][i]);
}
cout << best << "\n";
return 0;
} | replace | 4 | 6 | 4 | 6 | -11 | |
p02586 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
const long long MOD = static_cast<long long>(1e9 + 7);
const long long INFINITE = static_cast<long long>(1e16);
const bool IS_DEBUG = true;
#undef int
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int R, C, K;
cin >> R >> C >> K;
vector<vector<long long>> items(R, vector<long long>(C, 0));
for (int c = 0; c < K; c++) {
long long int x, y, v;
cin >> x >> y >> v;
items[x - 1][y - 1] = v;
}
vector<vector<vector<long long>>> dp(
R, vector<vector<long long>>(C, vector<long long>(10, 0)));
long long res = 0;
for (int x = 0; x < R; x++) {
for (int y = 0; y < C; y++) {
for (int c2 = 0; c2 < 4; c2++) {
if (c2 > 0)
dp[x][y][c2] = max(dp[x][y][c2 - 1], dp[x][y][c2]);
if (x > 0) {
dp[x][y][c2] = max(dp[x][y][c2], dp[x - 1][y][3]);
if (c2 != 0) {
dp[x][y][c2] = max(dp[x][y][c2], dp[x - 1][y][3] + items[x][y]);
}
}
if (y > 0) {
dp[x][y][c2] = max(dp[x][y][c2], dp[x][y - 1][c2]);
if (c2 > 0) {
dp[x][y][c2] =
max(dp[x][y][c2], dp[x][y - 1][c2 - 1] + items[x][y]);
}
}
if (x == 0 && y == 0 && c2 > 0) {
dp[x][y][c2] = max(dp[x][y][c2], items[x][y]);
}
res = max(res, dp[x][y][c2]);
// cout<<x<<" "<<y<<" "<<c2<<" "<<dp[x][y][c2]<<endl;
}
}
}
cout << res;
}
| #include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
const long long MOD = static_cast<long long>(1e9 + 7);
const long long INFINITE = static_cast<long long>(1e16);
const bool IS_DEBUG = true;
#undef int
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int R, C, K;
cin >> R >> C >> K;
vector<vector<long long>> items(R, vector<long long>(C, 0));
for (int c = 0; c < K; c++) {
long long int x, y, v;
cin >> x >> y >> v;
items[x - 1][y - 1] = v;
}
vector<vector<vector<long long>>> dp(
R, vector<vector<long long>>(C, vector<long long>(4, 0)));
long long res = 0;
for (int x = 0; x < R; x++) {
for (int y = 0; y < C; y++) {
for (int c2 = 0; c2 < 4; c2++) {
if (c2 > 0)
dp[x][y][c2] = max(dp[x][y][c2 - 1], dp[x][y][c2]);
if (x > 0) {
dp[x][y][c2] = max(dp[x][y][c2], dp[x - 1][y][3]);
if (c2 != 0) {
dp[x][y][c2] = max(dp[x][y][c2], dp[x - 1][y][3] + items[x][y]);
}
}
if (y > 0) {
dp[x][y][c2] = max(dp[x][y][c2], dp[x][y - 1][c2]);
if (c2 > 0) {
dp[x][y][c2] =
max(dp[x][y][c2], dp[x][y - 1][c2 - 1] + items[x][y]);
}
}
if (x == 0 && y == 0 && c2 > 0) {
dp[x][y][c2] = max(dp[x][y][c2], items[x][y]);
}
res = max(res, dp[x][y][c2]);
// cout<<x<<" "<<y<<" "<<c2<<" "<<dp[x][y][c2]<<endl;
}
}
}
cout << res;
}
| replace | 28 | 29 | 28 | 29 | MLE | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define sp(n) cout << fixed << setprecision(n)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
using namespace std;
int main(void) {
int R, C, k;
cin >> R >> C >> k;
vector<vector<ll>> t(R, vector<ll>(C, 0));
rep(i, k) {
int r, c;
ll v;
cin >> r >> c >> v;
r--;
c--;
t[r][c] = v;
}
ll dp[R][C][4];
rep(i, R) rep(j, C) rep(l, 4) dp[i][j][l] = 0;
rep(i, R) rep(j, C) {
if (i != R - 1) {
rep(l, 4) chmax(dp[i + 1][j][0], dp[i][j][l]);
rep(l, 3) chmax(dp[i + 1][j][0], dp[i][j][l] + t[i][j]);
}
if (j != C - 1) {
rep(l, 4) chmax(dp[i][j + 1][l], dp[i][j][l]);
rep(l, 3) chmax(dp[i][j + 1][l + 1], dp[i][j][l] + t[i][j]);
}
}
rep(i, 3)
chmax(dp[R - 1][C - 1][3 - i], dp[R - 1][C - 1][2 - i] + t[R - 1][C - 1]);
ll res = 0;
rep(i, 4) chmax(res, dp[R - 1][C - 1][i]);
cout << res << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define sp(n) cout << fixed << setprecision(n)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
using namespace std;
int main(void) {
int R, C, k;
cin >> R >> C >> k;
vector<vector<ll>> t(R, vector<ll>(C, 0));
rep(i, k) {
int r, c;
ll v;
cin >> r >> c >> v;
r--;
c--;
t[r][c] = v;
}
vector<vector<vector<ll>>> dp(R, vector<vector<ll>>(C, vector<ll>(4, 0)));
rep(i, R) rep(j, C) {
if (i != R - 1) {
rep(l, 4) chmax(dp[i + 1][j][0], dp[i][j][l]);
rep(l, 3) chmax(dp[i + 1][j][0], dp[i][j][l] + t[i][j]);
}
if (j != C - 1) {
rep(l, 4) chmax(dp[i][j + 1][l], dp[i][j][l]);
rep(l, 3) chmax(dp[i][j + 1][l + 1], dp[i][j][l] + t[i][j]);
}
}
rep(i, 3)
chmax(dp[R - 1][C - 1][3 - i], dp[R - 1][C - 1][2 - i] + t[R - 1][C - 1]);
ll res = 0;
rep(i, 4) chmax(res, dp[R - 1][C - 1][i]);
cout << res << endl;
} | replace | 32 | 34 | 32 | 33 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long int ll;
typedef long double ld;
const ll INF = (1LL << 62);
const ld pi = acosl((ld)-1);
const ll mod = 1000000007;
// const ll mod = 1234567;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
#define endn "\n"
#define TO_STRING(VariableName) #VariableName
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
rep(i, (int)v.size() - 1) cout << v[i] << " ";
cout << v[(int)v.size() - 1];
return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const map<T1, T2> &p) {
out << "(" << p.first << ", " << p.second << ")";
return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << "(" << p.first << ", " << p.second << ")";
return out;
}
template <class T> void debag(const T &obj) { cout << obj << endl; }
bool solve() {
ll r, c, k;
cin >> r >> c >> k;
swap(r, c);
vector<vector<ll>> items(r, vector<ll>(c, 0));
rep(i, k) {
ll a, b, v;
cin >> a >> b >> v;
swap(a, b);
a--;
b--;
items[a][b] = v;
}
ll dp[r + 1][c + 1][5];
rep(i, r) rep(j, c) rep(k, 5) dp[i][j][k] = 0;
rep(j, c) rep(i, r) rep(k, 4) {
// use
if (i + 1 < r and k < 3)
chmax(dp[i + 1][j][k + 1], dp[i][j][k] + items[i][j]);
if (j + 1 < c and k < 3)
chmax(dp[i][j + 1][0], dp[i][j][k] + items[i][j]);
// notuse
if (i + 1 < r)
chmax(dp[i + 1][j][k], dp[i][j][k]);
if (j + 1 < c)
chmax(dp[i][j + 1][0], dp[i][j][k]);
}
// rep(i,r)rep(j,c)cout<<i<<" "<<j<<" "<<dp[i][j][0]<<endl;
ll ans = 0;
rep(k, 3) chmax(ans, dp[r - 1][c - 1][k] + items[r - 1][c - 1]);
chmax(ans, dp[r - 1][c - 1][3]);
cout << ans << endl;
return false;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(30);
solve();
}
| #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long int ll;
typedef long double ld;
const ll INF = (1LL << 62);
const ld pi = acosl((ld)-1);
const ll mod = 1000000007;
// const ll mod = 1234567;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
#define endn "\n"
#define TO_STRING(VariableName) #VariableName
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
rep(i, (int)v.size() - 1) cout << v[i] << " ";
cout << v[(int)v.size() - 1];
return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const map<T1, T2> &p) {
out << "(" << p.first << ", " << p.second << ")";
return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << "(" << p.first << ", " << p.second << ")";
return out;
}
template <class T> void debag(const T &obj) { cout << obj << endl; }
bool solve() {
ll r, c, k;
cin >> r >> c >> k;
swap(r, c);
vector<vector<ll>> items(r, vector<ll>(c, 0));
rep(i, k) {
ll a, b, v;
cin >> a >> b >> v;
swap(a, b);
a--;
b--;
items[a][b] = v;
}
vector<vector<vector<ll>>> dp(r + 1,
vector<vector<ll>>(c + 1, vector<ll>(5, 0)));
rep(i, r) rep(j, c) rep(k, 5) dp[i][j][k] = 0;
rep(j, c) rep(i, r) rep(k, 4) {
// use
if (i + 1 < r and k < 3)
chmax(dp[i + 1][j][k + 1], dp[i][j][k] + items[i][j]);
if (j + 1 < c and k < 3)
chmax(dp[i][j + 1][0], dp[i][j][k] + items[i][j]);
// notuse
if (i + 1 < r)
chmax(dp[i + 1][j][k], dp[i][j][k]);
if (j + 1 < c)
chmax(dp[i][j + 1][0], dp[i][j][k]);
}
// rep(i,r)rep(j,c)cout<<i<<" "<<j<<" "<<dp[i][j][0]<<endl;
ll ans = 0;
rep(k, 3) chmax(ans, dp[r - 1][c - 1][k] + items[r - 1][c - 1]);
chmax(ans, dp[r - 1][c - 1][3]);
cout << ans << endl;
return false;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(30);
solve();
}
| replace | 63 | 64 | 63 | 65 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
/*
* JUST KEEP GOING
*/
#define endl '\n'
#define ll long long
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define For(i, s) for (auto i : s)
#define Forr(i, s) for (auto &i : s)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int, int>
#define vii vector<ii>
#define vll vector<pair<ll, ll>>
#define PI double PPPPPP = 3.14159265358979323846;
#define bit(mask, i) ((mask >> i) & 1)
void run() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 3000 + 5, mod = 1e9 + 7;
int r, c, k;
ll dp[N][N][5], arr[N][N];
ll solve(int i, int j, int cnt) {
if (i >= r || j >= c) {
return -1e17;
}
if (i == r - 1 && j == c - 1) {
return (cnt < 3 ? arr[i][j] : 0);
}
ll &ret = dp[i][j][cnt];
if (~ret)
return ret;
ret = 0;
if (cnt < 3) {
ret = max(ret, solve(i, j + 1, cnt + 1) + arr[i][j]);
ret = max(ret, solve(i + 1, j, 0) + arr[i][j]);
}
ret = max(ret, solve(i + 1, j, 0));
ret = max(ret, solve(i, j + 1, cnt));
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
run();
cin >> r >> c >> k;
for (int i = 0; i < k; ++i) {
int a, b, v;
cin >> a >> b >> v;
arr[a - 1][b - 1] = v;
}
clr(dp, -1);
cout << solve(0, 0, 0) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
/*
* JUST KEEP GOING
*/
#define endl '\n'
#define ll long long
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define For(i, s) for (auto i : s)
#define Forr(i, s) for (auto &i : s)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int, int>
#define vii vector<ii>
#define vll vector<pair<ll, ll>>
#define PI double PPPPPP = 3.14159265358979323846;
#define bit(mask, i) ((mask >> i) & 1)
void run() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 3000 + 5, mod = 1e9 + 7;
int r, c, k;
ll dp[N][N][5], arr[N][N];
ll solve(int i, int j, int cnt) {
if (i >= r || j >= c) {
return -1e17;
}
if (i == r - 1 && j == c - 1) {
return (cnt < 3 ? arr[i][j] : 0);
}
ll &ret = dp[i][j][cnt];
if (~ret)
return ret;
ret = 0;
if (cnt < 3) {
ret = max(ret, solve(i, j + 1, cnt + 1) + arr[i][j]);
ret = max(ret, solve(i + 1, j, 0) + arr[i][j]);
}
ret = max(ret, solve(i + 1, j, 0));
ret = max(ret, solve(i, j + 1, cnt));
return ret;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
run();
cin >> r >> c >> k;
for (int i = 0; i < k; ++i) {
int a, b, v;
cin >> a >> b >> v;
arr[a - 1][b - 1] = v;
}
clr(dp, -1);
cout << solve(0, 0, 0) << endl;
}
| insert | 50 | 50 | 50 | 51 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
// cout << fixed << setprecision(10) << flush;
int R, C, K;
cin >> R >> C >> K;
vector<vector<int>> dist(R + 1, vector<int>(C + 1, 0));
for (int i = 0; i < K; i++) {
int r, c, v;
cin >> r >> c >> v;
dist[r][c] = v;
}
// dp[i][j][k][l] は i で k 個、j で l 個 アイテムを拾った時のmax
int dp[R + 1][C + 1][4];
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
for (int k = 0; k < 4; k++) {
dp[i][j][k] = 0;
}
}
}
// 範囲チェック
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
for (int k = 0; k < 4; k++) {
dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + dist[i][j]);
if (k != 0 && dist[i][j] != 0) {
dp[i][j][k] = max(dp[i][j - 1][k - 1] + dist[i][j], dp[i][j][k]);
}
}
}
}
int ans = 0;
for (int k = 0; k < 4; k++) {
ans = max(ans, dp[R][C][k]);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
// cout << fixed << setprecision(10) << flush;
int R, C, K;
cin >> R >> C >> K;
vector<vector<int>> dist(R + 1, vector<int>(C + 1, 0));
for (int i = 0; i < K; i++) {
int r, c, v;
cin >> r >> c >> v;
dist[r][c] = v;
}
// dp[i][j][k] は i で k 個アイテムを拾った時のmax
vector<vector<vector<int>>> dp(R + 1,
vector<vector<int>>(C + 1, vector<int>(4, 0)));
// for(int i=0; i<=R; i++){
// for(int j=0; j<=C; j++){
// for(int k=0; k<4; k++){
// dp[i][j][k] = 0;
// }
// }
// }
// 範囲チェック
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
for (int k = 0; k < 4; k++) {
dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + dist[i][j]);
if (k != 0 && dist[i][j] != 0) {
dp[i][j][k] = max(dp[i][j - 1][k - 1] + dist[i][j], dp[i][j][k]);
}
}
}
}
int ans = 0;
for (int k = 0; k < 4; k++) {
ans = max(ans, dp[R][C][k]);
}
cout << ans << endl;
return 0;
} | replace | 16 | 25 | 16 | 26 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define EACH(i, a, b) for (int i = (a); i <= (b); i++)
#define REACH(i, a, b) for (int i = (b); i >= (a); i--)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(a, x, b) ((a) <= (x) && (x) < (b))
const int MOD = 1e9 + 7;
const int INF = 1 << 29;
const double EPS = 1e-10;
using ll = long long;
using P = pair<int, int>;
template <class T = int> using V = vector<T>;
template <class T> bool inline chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool inline chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T = int> T inline input() {
T x;
cin >> x;
return (x);
}
template <class T> void inline print(T &x) { cout << x << '\n'; }
#define debug(x) cerr << #x << ": " << x << endl;
const int dxs[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dys[] = {0, -1, 0, 1, 1, -1, -1, 1};
ll dp[3001][3001][4];
int RC[3001][3001];
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int R, C, K;
cin >> R >> C >> K;
REP(i, K) {
int r, c, v;
cin >> r >> c >> v;
RC[r][c] = v;
}
dp[1][1][1] = dp[1][1][2] = dp[1][1][3] = RC[1][1];
EACH(i, 1, R) {
EACH(j, 1, C) {
REP(k, 4) {
chmax<ll>(dp[i + 1][j][0], dp[i][j][k]);
chmax<ll>(dp[i + 1][j][1], dp[i][j][k] + RC[i + 1][j]);
}
chmax<ll>(dp[i][j + 1][0], dp[i][j][0]);
chmax<ll>(dp[i][j + 1][1], max(dp[i][j][1], dp[i][j][0] + RC[i][j + 1]));
chmax<ll>(dp[i][j + 1][2], max(dp[i][j][2], dp[i][j][1] + RC[i][j + 1]));
chmax<ll>(dp[i][j + 1][3], max(dp[i][j][3], dp[i][j][2] + RC[i][j + 1]));
}
}
ll ans = 0;
REP(k, 4) chmax(ans, dp[R][C][k]);
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define EACH(i, a, b) for (int i = (a); i <= (b); i++)
#define REACH(i, a, b) for (int i = (b); i >= (a); i--)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(a, x, b) ((a) <= (x) && (x) < (b))
const int MOD = 1e9 + 7;
const int INF = 1 << 29;
const double EPS = 1e-10;
using ll = long long;
using P = pair<int, int>;
template <class T = int> using V = vector<T>;
template <class T> bool inline chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool inline chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T = int> T inline input() {
T x;
cin >> x;
return (x);
}
template <class T> void inline print(T &x) { cout << x << '\n'; }
#define debug(x) cerr << #x << ": " << x << endl;
const int dxs[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dys[] = {0, -1, 0, 1, 1, -1, -1, 1};
ll dp[3010][3010][4];
int RC[3001][3001];
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int R, C, K;
cin >> R >> C >> K;
REP(i, K) {
int r, c, v;
cin >> r >> c >> v;
RC[r][c] = v;
}
dp[1][1][1] = dp[1][1][2] = dp[1][1][3] = RC[1][1];
EACH(i, 1, R) {
EACH(j, 1, C) {
REP(k, 4) {
chmax<ll>(dp[i + 1][j][0], dp[i][j][k]);
chmax<ll>(dp[i + 1][j][1], dp[i][j][k] + RC[i + 1][j]);
}
chmax<ll>(dp[i][j + 1][0], dp[i][j][0]);
chmax<ll>(dp[i][j + 1][1], max(dp[i][j][1], dp[i][j][0] + RC[i][j + 1]));
chmax<ll>(dp[i][j + 1][2], max(dp[i][j][2], dp[i][j][1] + RC[i][j + 1]));
chmax<ll>(dp[i][j + 1][3], max(dp[i][j][3], dp[i][j][2] + RC[i][j + 1]));
}
}
ll ans = 0;
REP(k, 4) chmax(ans, dp[R][C][k]);
cout << ans << endl;
return 0;
}
| replace | 50 | 51 | 50 | 51 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h> // ver2.7.1
#define int long long
#define endl "\n" // fflush(stdout);
#define ALL(v) (v).begin(), (v).end()
#define Vi vector<int>
#define VVi vector<Vi>
#define VVVi vector<VVi>
#define Vm vector<mint>
#define Vs vector<string>
#define Vd vector<double>
#define Vc vector<char>
#define Pii pair<int, int>
#define Pdd pair<double, double>
#define VPii vector<Pii>
#define Tiii tuple<int, int, int>
#define VTiii vector<Tiii>
#define PQi priority_queue<int>
#define PQir priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define itos to_string
#define stoi stoll
#define FI first
#define SE second
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define cyes cout << "yes" << endl
#define cno cout << "no" << endl
#define sortr(v) sort(v, greater<>())
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define repreq(i, a, b) for (int i = a; i >= b; i--)
#define dem(a, b) ((a + b - 1) / (b))
#define INF 3000000000000000000 // 3.0*10^18
#define MAX LLONG_MAX
#define PI acos(-1.0L)
// int MOD = 998244353; // case
const int MOD = 1000000007; // 10^9 + 7
const double EPS = 1e-10;
using namespace std;
struct mint {
int x;
mint(int x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(int t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
mint inv() const { return pow(MOD - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
/* debug */
template <typename T> // vector
ostream &operator<<(ostream &os, const vector<T> &V) {
int N = V.size();
if (N == 0) {
os << '.';
return os;
}
rep(i, 0, N - 1) { os << V[i] << ' '; }
os << V[N - 1];
return os;
}
template <typename T> // vector vector
ostream &operator<<(ostream &os, const vector<vector<T>> &V) {
int N = V.size();
rep(i, 0, N - 1) cout << V[i] << endl;
cout << V[N - 1];
return os;
}
template <typename T, typename S> // pair
ostream &operator<<(ostream &os, pair<T, S> const &P) {
os << P.FI << ' ' << P.SE;
return os;
}
template <typename T> // set
ostream &operator<<(ostream &os, set<T> &S) {
auto it = S.begin();
while (it != S.end()) {
os << *it;
if (next(it, 1) != S.end())
os << ' ';
it++;
};
return os;
}
template <typename T> // deque
ostream &operator<<(ostream &os, deque<T> &q) {
for (auto it = q.begin(); it < q.end(); it++) {
os << *it;
if (it + 1 != q.end())
os << " ";
}
return os;
}
/* useful */
template <typename T> void Vin(vector<T> &v) {
int n = v.size();
rep(i, 0, n) cin >> v[i];
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int mypow(int x, int n, int m) {
if (n == 0)
return 1;
if (n % 2 == 0)
return mypow(x * x % m, n / 2, m);
else
return x * mypow(x, n - 1, m) % m;
}
int scomb(int n, int r) {
if (r < 0 || r > n)
return 0;
if ((n - r) < r)
r = n - r; // nCr
int a = 1;
for (int i = n; i > n - r; --i) {
a = a * i;
}
for (int i = 1; i < r + 1; ++i) {
a = a / i;
}
return a;
}
Vi vis(Vi &v) {
Vi S(v.size() + 1);
rep(i, 1, S.size()) S[i] += v[i - 1] + S[i - 1];
return S;
}
int digitsum(int n) {
int ret = 0;
while (n > 0) {
ret += n % 10;
n /= 10;
}
return ret;
}
int digit(int k, int i) {
string s = itos(k);
return s[s.size() - i] - '0';
} // i桁目の数字
template <typename T> void press(T &v) {
v.erase(unique(ALL(v)), v.end());
} // 圧縮
Vi zip(Vi b) {
int Z = b.size(); // 座標圧縮
Pii p[Z + 10];
int a = b.size();
Vi l(a);
for (int i = 0; i < a; i++)
p[i] = mp(b[i], i);
sort(p, p + a);
int w = 0;
for (int i = 0; i < a; i++) {
if (i && p[i].first != p[i - 1].first)
w++;
l[p[i].second] = w;
}
return l;
}
Vi nibe2V() {
Vi a(60);
int q = 1;
rep(i, 0, 60) {
a[i] = q;
q *= 2;
}
return a;
}
// Vi nibe = nibe2V(); // 2^n
int modiv(int a, int b) {
return a * mypow(b, MOD - 2, MOD) % MOD;
} // a÷b(%MOD)
int SMALLER(Vi &a, int x) {
return lower_bound(a.begin(), a.end(), x) - a.begin();
}
int orSMALLER(Vi &a, int x) {
return upper_bound(a.begin(), a.end(), x) - a.begin();
}
int BIGGER(Vi &a, int x) { return a.size() - orSMALLER(a, x); }
int orBIGGER(Vi &a, int x) { return a.size() - SMALLER(a, x); }
int COUNT(Vi &a, int x) {
return upper_bound(ALL(a), x) - lower_bound(ALL(a), x);
}
int maxind(Vi &a) { return max_element(ALL(a)) - a.begin(); }
int minind(Vi &a) { return min_element(ALL(a)) - a.begin(); }
Vi stpowV() {
Vi a(1000005);
a[0] = 1;
repeq(i, 1, 1000004) a[i] = a[i - 1] * i % MOD;
return a;
}
// Vi stpow = stpowV(); // 階乗配列(%MOD)
/****************************** START ******************************/
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
int h, w, K;
cin >> h >> w >> K;
VVi mapp(h + 1, Vi(w + 1));
rep(i, 0, K) {
int qw, er, ty;
cin >> qw >> er >> ty;
qw--, er--;
mapp[qw][er] = ty;
}
int dp[3006][3006][4];
rep(i, 0, h) {
rep(j, 0, w) {
repreq(k, 3, 1) {
dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + mapp[i][j]);
}
int maxx = 0;
rep(k, 0, 4) {
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
maxx = max(dp[i][j][k], maxx);
}
dp[i + 1][j][0] = maxx;
}
}
int ans = 0;
rep(i, 0, 3005) rep(j, 0, 3005) rep(k, 0, 4) { ans = max(ans, dp[i][j][k]); }
cout << ans << endl;
// cout << nibe << endl;
return 0;
} | #include <bits/stdc++.h> // ver2.7.1
#define int long long
#define endl "\n" // fflush(stdout);
#define ALL(v) (v).begin(), (v).end()
#define Vi vector<int>
#define VVi vector<Vi>
#define VVVi vector<VVi>
#define Vm vector<mint>
#define Vs vector<string>
#define Vd vector<double>
#define Vc vector<char>
#define Pii pair<int, int>
#define Pdd pair<double, double>
#define VPii vector<Pii>
#define Tiii tuple<int, int, int>
#define VTiii vector<Tiii>
#define PQi priority_queue<int>
#define PQir priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define itos to_string
#define stoi stoll
#define FI first
#define SE second
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define cyes cout << "yes" << endl
#define cno cout << "no" << endl
#define sortr(v) sort(v, greater<>())
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define repreq(i, a, b) for (int i = a; i >= b; i--)
#define dem(a, b) ((a + b - 1) / (b))
#define INF 3000000000000000000 // 3.0*10^18
#define MAX LLONG_MAX
#define PI acos(-1.0L)
// int MOD = 998244353; // case
const int MOD = 1000000007; // 10^9 + 7
const double EPS = 1e-10;
using namespace std;
struct mint {
int x;
mint(int x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(int t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
mint inv() const { return pow(MOD - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
/* debug */
template <typename T> // vector
ostream &operator<<(ostream &os, const vector<T> &V) {
int N = V.size();
if (N == 0) {
os << '.';
return os;
}
rep(i, 0, N - 1) { os << V[i] << ' '; }
os << V[N - 1];
return os;
}
template <typename T> // vector vector
ostream &operator<<(ostream &os, const vector<vector<T>> &V) {
int N = V.size();
rep(i, 0, N - 1) cout << V[i] << endl;
cout << V[N - 1];
return os;
}
template <typename T, typename S> // pair
ostream &operator<<(ostream &os, pair<T, S> const &P) {
os << P.FI << ' ' << P.SE;
return os;
}
template <typename T> // set
ostream &operator<<(ostream &os, set<T> &S) {
auto it = S.begin();
while (it != S.end()) {
os << *it;
if (next(it, 1) != S.end())
os << ' ';
it++;
};
return os;
}
template <typename T> // deque
ostream &operator<<(ostream &os, deque<T> &q) {
for (auto it = q.begin(); it < q.end(); it++) {
os << *it;
if (it + 1 != q.end())
os << " ";
}
return os;
}
/* useful */
template <typename T> void Vin(vector<T> &v) {
int n = v.size();
rep(i, 0, n) cin >> v[i];
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int mypow(int x, int n, int m) {
if (n == 0)
return 1;
if (n % 2 == 0)
return mypow(x * x % m, n / 2, m);
else
return x * mypow(x, n - 1, m) % m;
}
int scomb(int n, int r) {
if (r < 0 || r > n)
return 0;
if ((n - r) < r)
r = n - r; // nCr
int a = 1;
for (int i = n; i > n - r; --i) {
a = a * i;
}
for (int i = 1; i < r + 1; ++i) {
a = a / i;
}
return a;
}
Vi vis(Vi &v) {
Vi S(v.size() + 1);
rep(i, 1, S.size()) S[i] += v[i - 1] + S[i - 1];
return S;
}
int digitsum(int n) {
int ret = 0;
while (n > 0) {
ret += n % 10;
n /= 10;
}
return ret;
}
int digit(int k, int i) {
string s = itos(k);
return s[s.size() - i] - '0';
} // i桁目の数字
template <typename T> void press(T &v) {
v.erase(unique(ALL(v)), v.end());
} // 圧縮
Vi zip(Vi b) {
int Z = b.size(); // 座標圧縮
Pii p[Z + 10];
int a = b.size();
Vi l(a);
for (int i = 0; i < a; i++)
p[i] = mp(b[i], i);
sort(p, p + a);
int w = 0;
for (int i = 0; i < a; i++) {
if (i && p[i].first != p[i - 1].first)
w++;
l[p[i].second] = w;
}
return l;
}
Vi nibe2V() {
Vi a(60);
int q = 1;
rep(i, 0, 60) {
a[i] = q;
q *= 2;
}
return a;
}
// Vi nibe = nibe2V(); // 2^n
int modiv(int a, int b) {
return a * mypow(b, MOD - 2, MOD) % MOD;
} // a÷b(%MOD)
int SMALLER(Vi &a, int x) {
return lower_bound(a.begin(), a.end(), x) - a.begin();
}
int orSMALLER(Vi &a, int x) {
return upper_bound(a.begin(), a.end(), x) - a.begin();
}
int BIGGER(Vi &a, int x) { return a.size() - orSMALLER(a, x); }
int orBIGGER(Vi &a, int x) { return a.size() - SMALLER(a, x); }
int COUNT(Vi &a, int x) {
return upper_bound(ALL(a), x) - lower_bound(ALL(a), x);
}
int maxind(Vi &a) { return max_element(ALL(a)) - a.begin(); }
int minind(Vi &a) { return min_element(ALL(a)) - a.begin(); }
Vi stpowV() {
Vi a(1000005);
a[0] = 1;
repeq(i, 1, 1000004) a[i] = a[i - 1] * i % MOD;
return a;
}
// Vi stpow = stpowV(); // 階乗配列(%MOD)
/****************************** START ******************************/
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
int h, w, K;
cin >> h >> w >> K;
VVi mapp(h + 1, Vi(w + 1));
rep(i, 0, K) {
int qw, er, ty;
cin >> qw >> er >> ty;
qw--, er--;
mapp[qw][er] = ty;
}
// int dp[3006][3006][4];
VVVi dp(3010, VVi(3010, Vi(4)));
rep(i, 0, h) {
rep(j, 0, w) {
repreq(k, 3, 1) {
dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + mapp[i][j]);
}
int maxx = 0;
rep(k, 0, 4) {
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
maxx = max(dp[i][j][k], maxx);
}
dp[i + 1][j][0] = maxx;
}
}
int ans = 0;
rep(i, 0, 3005) rep(j, 0, 3005) rep(k, 0, 4) { ans = max(ans, dp[i][j][k]); }
cout << ans << endl;
// cout << nibe << endl;
return 0;
} | replace | 243 | 244 | 243 | 245 | -11 | |
p02587 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
typedef pair<ll, pair<string, int>> S;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n;
cin >> n;
vector<string> s(n);
vector<ll> c(n);
vector<string> revs(n);
rep(i, n) cin >> s[i] >> c[i];
map<string, ll> mp;
priority_queue<S, vector<S>, greater<S>> que;
que.push({0, {"", 0}});
que.push({0, {"", 1}});
int f = 0;
while (!que.empty()) {
S p = que.top();
que.pop();
ll cost = p.first;
string now = p.second.first;
int mode = p.second.second;
int len = now.size();
if (cost != 0) {
bool ok = true;
rep(i, len) if (now[i] != now[len - 1 - i]) ok = false;
if (ok) {
cout << cost << '\n';
return 0;
}
}
f++;
if (f > 1000000)
break;
if (cost > mp[now])
continue;
rep(i, n) {
int sz = s[i].size();
string tmp = s[i];
if (mode == 0)
reverse(all(tmp));
if (now.size() >= sz && now.substr(0, sz) == tmp) {
string rem = now.substr(sz);
mp[rem] = cost + c[i];
que.push({cost + c[i], {rem, mode}});
}
if (tmp.substr(0, len) == now) {
string rem = tmp.substr(len);
mp[rem] = cost + c[i];
que.push({cost + c[i], {rem, mode ^ 1}});
}
reverse(all(tmp));
if (now.size() + s[i].size() <= 20) {
string m = now + tmp;
mp[m] = cost + c[i];
que.push({cost + c[i], {m, mode}});
}
}
}
cout << "-1\n";
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
typedef pair<ll, pair<string, int>> S;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n;
cin >> n;
vector<string> s(n);
vector<ll> c(n);
vector<string> revs(n);
rep(i, n) cin >> s[i] >> c[i];
map<string, ll> mp;
priority_queue<S, vector<S>, greater<S>> que;
que.push({0, {"", 0}});
que.push({0, {"", 1}});
int f = 0;
while (!que.empty()) {
S p = que.top();
que.pop();
ll cost = p.first;
string now = p.second.first;
int mode = p.second.second;
int len = now.size();
if (cost != 0) {
bool ok = true;
rep(i, len) if (now[i] != now[len - 1 - i]) ok = false;
if (ok) {
cout << cost << '\n';
return 0;
}
}
f++;
if (f > 1000000)
break;
if (cost > mp[now])
continue;
rep(i, n) {
int sz = s[i].size();
string tmp = s[i];
if (mode == 0)
reverse(all(tmp));
if (now.size() >= sz && now.substr(0, sz) == tmp) {
string rem = now.substr(sz);
mp[rem] = cost + c[i];
que.push({cost + c[i], {rem, mode}});
}
if (tmp.substr(0, len) == now) {
string rem = tmp.substr(len);
mp[rem] = cost + c[i];
que.push({cost + c[i], {rem, mode ^ 1}});
}
}
}
cout << "-1\n";
} | delete | 106 | 112 | 106 | 106 | TLE | |
p02587 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[51][2][21];
ll ndp[51][2][21];
string s[51];
ll c[51];
bool good(int pos, int flag, int len) {
int sz = s[pos].size();
if (flag == 0) {
int l = sz - len, r = sz - 1;
for (; l <= r; l++, r--) {
if (s[pos][l] != s[pos][r])
return false;
}
return true;
} else {
int l = 0, r = len - 1;
for (; l <= r; l++, r--) {
if (s[pos][l] != s[pos][r])
return false;
}
return true;
}
}
void solve() {
int n;
cin >> n;
memset(dp, -1, sizeof dp);
for (int i = 1; i <= n; i++) {
for (int flag = 0; flag <= 1; flag++) {
for (int len = 1; len <= 20; len++) {
dp[i][flag][len] = 1e18;
}
}
}
ll ans = 1e18;
for (int i = 1; i <= n; i++) {
cin >> s[i] >> c[i];
int sz = s[i].size();
dp[i][0][sz] = c[i]; // prefix
dp[i][1][sz] = c[i]; // suffix
if (good(i, 0, sz)) {
ans = min(ans, c[i]);
}
}
for (int iter = 0; iter < 50 * 20 * 2 + 200; iter++) {
for (int i = 1; i <= n; i++) {
int sz1 = s[i].size();
for (int flag = 0; flag <= 1; flag++) {
for (int len = 1; len <= sz1; len++) {
if (dp[i][flag][len] == 1e18)
continue;
for (int j = 1; j <= n; j++) {
int sz2 = s[j].size();
if (flag == 0) {
// should take in the suffix, have i-th in the end with last len
// symbols
int equals = 1, pos1 = sz1 - len, pos2 = sz2 - 1;
for (; pos1 < sz1 && pos2 >= 0; pos1++, pos2--) {
equals &= (s[i][pos1] == s[j][pos2]);
}
if (len > sz2 && equals) {
dp[i][0][len - sz2] =
min(dp[i][0][len - sz2], dp[i][0][len] + c[j]);
if (good(i, 0, len - sz2)) {
ans = min(ans, dp[i][0][len - sz2]);
}
}
if (len < sz2 && equals) {
// cout << "i=" << i << " len=" << len << " j=" << j << "
// sz2-len=" << sz2 - len << endl;
dp[j][1][sz2 - len] =
min(dp[j][1][sz2 - len], dp[i][0][len] + c[j]);
if (good(j, 1, sz2 - len)) {
ans = min(ans, dp[j][1][sz2 - len]);
}
}
if (len == sz2 && equals) {
// cout << "1 " << "i=" << i << " len=" << len << " j=" << j
// << endl;
ans = min(ans, dp[i][0][len] + c[j]);
}
} else {
// should take in the prefix, have first len symbols
int equals = 1, pos1 = len - 1, pos2 = 0;
for (; pos1 >= 0 && pos2 < sz2; pos1--, pos2++) {
equals &= (s[i][pos1] == s[j][pos2]);
}
if (len > sz2 && equals) {
dp[i][1][len - sz2] =
min(dp[i][1][len - sz2], dp[i][1][len] + c[j]);
if (good(i, 1, len - sz2)) {
ans = min(ans, dp[i][1][len - sz2]);
}
}
if (len < sz2 && equals) {
dp[j][0][sz2 - len] =
min(dp[j][0][sz2 - len], dp[i][1][len] + c[j]);
if (good(j, 0, sz2 - len)) {
ans = min(ans, dp[j][0][sz2 - len]);
// cout << "2 " << "i=" << i << " len=" << len << " j=" << j
// << endl;
}
}
if (len == sz2 && equals) {
// cout << "2 " << "i=" << i << " len=" << len << " j=" << j
// << endl;
ans = min(ans, dp[i][1][len] + c[j]);
}
}
}
}
}
}
}
if (ans == 1e18)
cout << -1;
else
cout << ans;
}
int main() {
ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
cout.setf(ios::fixed), cout.precision(20);
solve();
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[51][2][21];
ll ndp[51][2][21];
string s[51];
ll c[51];
bool good(int pos, int flag, int len) {
int sz = s[pos].size();
if (flag == 0) {
int l = sz - len, r = sz - 1;
for (; l <= r; l++, r--) {
if (s[pos][l] != s[pos][r])
return false;
}
return true;
} else {
int l = 0, r = len - 1;
for (; l <= r; l++, r--) {
if (s[pos][l] != s[pos][r])
return false;
}
return true;
}
}
void solve() {
int n;
cin >> n;
memset(dp, -1, sizeof dp);
for (int i = 1; i <= n; i++) {
for (int flag = 0; flag <= 1; flag++) {
for (int len = 1; len <= 20; len++) {
dp[i][flag][len] = 1e18;
}
}
}
ll ans = 1e18;
for (int i = 1; i <= n; i++) {
cin >> s[i] >> c[i];
int sz = s[i].size();
dp[i][0][sz] = c[i]; // prefix
dp[i][1][sz] = c[i]; // suffix
if (good(i, 0, sz)) {
ans = min(ans, c[i]);
}
}
for (int iter = 0; iter < 50 * 10; iter++) {
for (int i = 1; i <= n; i++) {
int sz1 = s[i].size();
for (int flag = 0; flag <= 1; flag++) {
for (int len = 1; len <= sz1; len++) {
if (dp[i][flag][len] == 1e18)
continue;
for (int j = 1; j <= n; j++) {
int sz2 = s[j].size();
if (flag == 0) {
// should take in the suffix, have i-th in the end with last len
// symbols
int equals = 1, pos1 = sz1 - len, pos2 = sz2 - 1;
for (; pos1 < sz1 && pos2 >= 0; pos1++, pos2--) {
equals &= (s[i][pos1] == s[j][pos2]);
}
if (len > sz2 && equals) {
dp[i][0][len - sz2] =
min(dp[i][0][len - sz2], dp[i][0][len] + c[j]);
if (good(i, 0, len - sz2)) {
ans = min(ans, dp[i][0][len - sz2]);
}
}
if (len < sz2 && equals) {
// cout << "i=" << i << " len=" << len << " j=" << j << "
// sz2-len=" << sz2 - len << endl;
dp[j][1][sz2 - len] =
min(dp[j][1][sz2 - len], dp[i][0][len] + c[j]);
if (good(j, 1, sz2 - len)) {
ans = min(ans, dp[j][1][sz2 - len]);
}
}
if (len == sz2 && equals) {
// cout << "1 " << "i=" << i << " len=" << len << " j=" << j
// << endl;
ans = min(ans, dp[i][0][len] + c[j]);
}
} else {
// should take in the prefix, have first len symbols
int equals = 1, pos1 = len - 1, pos2 = 0;
for (; pos1 >= 0 && pos2 < sz2; pos1--, pos2++) {
equals &= (s[i][pos1] == s[j][pos2]);
}
if (len > sz2 && equals) {
dp[i][1][len - sz2] =
min(dp[i][1][len - sz2], dp[i][1][len] + c[j]);
if (good(i, 1, len - sz2)) {
ans = min(ans, dp[i][1][len - sz2]);
}
}
if (len < sz2 && equals) {
dp[j][0][sz2 - len] =
min(dp[j][0][sz2 - len], dp[i][1][len] + c[j]);
if (good(j, 0, sz2 - len)) {
ans = min(ans, dp[j][0][sz2 - len]);
// cout << "2 " << "i=" << i << " len=" << len << " j=" << j
// << endl;
}
}
if (len == sz2 && equals) {
// cout << "2 " << "i=" << i << " len=" << len << " j=" << j
// << endl;
ans = min(ans, dp[i][1][len] + c[j]);
}
}
}
}
}
}
}
if (ans == 1e18)
cout << -1;
else
cout << ans;
}
int main() {
ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
cout.setf(ios::fixed), cout.precision(20);
solve();
}
| replace | 51 | 52 | 51 | 52 | TLE | |
p02587 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using Vbo = vector<bool>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using VVbo = vector<Vbo>;
using VVV = vector<VV>;
using VVVll = vector<VVll>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define all(a) (a).begin(), (a).end()
struct fast_ios {
fast_ios() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
template <typename T> inline int sz(T &x) { return x.size(); }
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << '(' << p.first << ", " << p.second << ')';
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v)
is >> e;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto &e : v)
os << e << ' ';
return os;
}
template <typename T> inline int count_between(vector<T> &a, T l, T r) {
return lower_bound(all(a), r) - lower_bound(all(a), l);
} // [l, r)
inline int fLog2(const int x) {
assert(x > 0);
return 31 - __builtin_clz(x);
} // floor(log2(x))
inline int fLog2(const ll x) {
assert(x > 0);
return 63 - __builtin_clzll(x);
}
inline int cLog2(const int x) {
assert(x > 0);
return (x == 1) ? 0 : 32 - __builtin_clz(x - 1);
} // ceil(log2(x))
inline int cLog2(const ll x) {
assert(x > 0);
return (x == 1) ? 0 : 64 - __builtin_clzll(x - 1);
}
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
void fail() {
cout << -1 << '\n';
exit(0);
}
// const int INF = 1<<30;
const ll INFll = 1ll << 62;
// const ld EPS = 1e-10;
// const ld PI = acos(-1.0);
// const int MOD = int(1e9)+7;
// const int MOD = 998244353;
template <typename T> struct edge {
int v;
T w;
edge(int v, T w) : v(v), w(w) {}
};
template <typename T> using Graph = vector<vector<edge<T>>>;
string rev(string s) {
reverse(all(s));
return s;
}
map<string, int> idS[2];
vector<string> s[2];
int main() {
int n;
cin >> n;
V c(n);
rep(i, n) {
string x;
cin >> x >> c[i];
s[0].push_back(x);
s[1].push_back(rev(x));
}
const int M = 6;
auto nid = [&](int i, int j, int r) { return M * i + j + n * M * r; };
int nv = n * M * 2;
Graph<ll> G(nv);
rep(r, 2) rep(i, n) rep2(j, 1, sz(s[r][i]) + 1) rep(k, n) {
auto t1 = rev(s[r][i].substr(0, j));
auto t2 = s[r][k];
int l = sz(t2);
int mn = min(j, l);
if (t1.substr(0, mn) != t2.substr(0, mn))
continue;
int r2 = r;
int i2 = i;
int j2 = abs(j - l);
if (j < l)
r2 ^= 1, i2 = k;
G[nid(i, j, r)].emplace_back(nid(i2, j2, r2), c[k]);
}
using T = ll;
const auto INF_T = numeric_limits<T>::max();
vector<T> dist(nv, INF_T);
using PTi = pair<T, int>;
priority_queue<PTi, vector<PTi>, greater<PTi>> pq;
rep(i, n) {
int x = nid(i, sz(s[0][i]), 0);
dist[x] = c[i];
pq.emplace(dist[x], x);
}
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (dist[u] < d)
continue;
for (auto &[v, w] : G[u]) {
if (chmin(dist[v], d + w)) {
pq.emplace(dist[v], v);
}
}
}
ll ans = INFll;
rep(r, 2) rep(i, n) rep(j, sz(s[r][i]) + 1) {
auto t = s[r][i].substr(0, j);
if (t == rev(t)) {
chmin(ans, dist[nid(i, j, r)]);
}
}
cout << (ans == INFll ? -1 : ans) << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using Vbo = vector<bool>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using VVbo = vector<Vbo>;
using VVV = vector<VV>;
using VVVll = vector<VVll>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define all(a) (a).begin(), (a).end()
struct fast_ios {
fast_ios() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
template <typename T> inline int sz(T &x) { return x.size(); }
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << '(' << p.first << ", " << p.second << ')';
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v)
is >> e;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto &e : v)
os << e << ' ';
return os;
}
template <typename T> inline int count_between(vector<T> &a, T l, T r) {
return lower_bound(all(a), r) - lower_bound(all(a), l);
} // [l, r)
inline int fLog2(const int x) {
assert(x > 0);
return 31 - __builtin_clz(x);
} // floor(log2(x))
inline int fLog2(const ll x) {
assert(x > 0);
return 63 - __builtin_clzll(x);
}
inline int cLog2(const int x) {
assert(x > 0);
return (x == 1) ? 0 : 32 - __builtin_clz(x - 1);
} // ceil(log2(x))
inline int cLog2(const ll x) {
assert(x > 0);
return (x == 1) ? 0 : 64 - __builtin_clzll(x - 1);
}
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
void fail() {
cout << -1 << '\n';
exit(0);
}
// const int INF = 1<<30;
const ll INFll = 1ll << 62;
// const ld EPS = 1e-10;
// const ld PI = acos(-1.0);
// const int MOD = int(1e9)+7;
// const int MOD = 998244353;
template <typename T> struct edge {
int v;
T w;
edge(int v, T w) : v(v), w(w) {}
};
template <typename T> using Graph = vector<vector<edge<T>>>;
string rev(string s) {
reverse(all(s));
return s;
}
map<string, int> idS[2];
vector<string> s[2];
int main() {
int n;
cin >> n;
V c(n);
rep(i, n) {
string x;
cin >> x >> c[i];
s[0].push_back(x);
s[1].push_back(rev(x));
}
const int M = 21;
auto nid = [&](int i, int j, int r) { return M * i + j + n * M * r; };
int nv = n * M * 2;
Graph<ll> G(nv);
rep(r, 2) rep(i, n) rep2(j, 1, sz(s[r][i]) + 1) rep(k, n) {
auto t1 = rev(s[r][i].substr(0, j));
auto t2 = s[r][k];
int l = sz(t2);
int mn = min(j, l);
if (t1.substr(0, mn) != t2.substr(0, mn))
continue;
int r2 = r;
int i2 = i;
int j2 = abs(j - l);
if (j < l)
r2 ^= 1, i2 = k;
G[nid(i, j, r)].emplace_back(nid(i2, j2, r2), c[k]);
}
using T = ll;
const auto INF_T = numeric_limits<T>::max();
vector<T> dist(nv, INF_T);
using PTi = pair<T, int>;
priority_queue<PTi, vector<PTi>, greater<PTi>> pq;
rep(i, n) {
int x = nid(i, sz(s[0][i]), 0);
dist[x] = c[i];
pq.emplace(dist[x], x);
}
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (dist[u] < d)
continue;
for (auto &[v, w] : G[u]) {
if (chmin(dist[v], d + w)) {
pq.emplace(dist[v], v);
}
}
}
ll ans = INFll;
rep(r, 2) rep(i, n) rep(j, sz(s[r][i]) + 1) {
auto t = s[r][i].substr(0, j);
if (t == rev(t)) {
chmin(ans, dist[nid(i, j, r)]);
}
}
cout << (ans == INFll ? -1 : ans) << '\n';
return 0;
}
| replace | 122 | 123 | 122 | 123 | 0 | |
p02587 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h(int tp, int id, int len) { return (tp + (id << 1) + (len << 7)); }
string rev(string s) {
reverse(s.begin(), s.end());
return s;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
const int N = (1 << 12);
const ll inf = (1ll << 60);
vector<array<ll, 2>> v[N];
int n;
cin >> n;
vector<string> s(n + 1);
vector<ll> c(n + 1, 0);
for (int i = 1; i <= n; i++)
cin >> s[i] >> c[i];
for (int i = 1; i <= n; i++) {
int one = s[i].size();
for (int len = 1; len <= one; len++) {
for (int j = 1; j <= n; j++) {
int sz = s[j].size();
// prefix-0
if (sz < len && s[i].substr(len - sz, sz) == rev(s[j]))
v[h(0, i, len)].push_back({h(0, i, len - sz), c[j]});
if (sz == len && s[i].substr(0, len) == rev(s[j]))
v[h(0, i, len)].push_back({0, c[j]});
if (sz > len && s[i].substr(0, len) == rev(s[j].substr(0, len)))
v[h(0, i, len)].push_back({h(1, j, sz - len), c[j]});
// suffix-1
if (sz < len && s[i].substr(one - len, sz) == rev(s[j]))
v[h(1, i, len)].push_back({h(1, i, len - sz), c[j]});
if (sz == len && s[i].substr(one - len, len) == rev(s[j]))
v[h(1, i, len)].push_back({0, c[j]});
if (sz > len &&
s[i].substr(one - len, len) == rev(s[j].substr(sz - len, len)))
v[h(1, i, len)].push_back({h(0, j, sz - len), c[j]});
}
}
}
priority_queue<array<ll, 2>, vector<array<ll, 2>>, greater<array<ll, 2>>> q;
vector<ll> d(N, inf);
auto mn = [](ll &a, ll b) { a = min(a, b); };
for (int i = 1; i <= n; i++) {
int sz = s[i].size();
if (s[i] == rev(s[i]))
mn(d[0], c[i]);
// one
for (int j = 0; j < sz - j - 1; j++)
if (s[i].substr(0, j) == rev(s[i].substr(j + 1, j)))
mn(d[h(1, i, sz - (2 * j + 1))], c[i]);
for (int j = sz - 1; j > sz - j - 1; j--)
if (s[i].substr(j + 1, sz - j - 1) ==
rev(s[i].substr(j - (sz - j - 1), sz - j - 1)))
mn(d[h(0, i, sz - (2 * (sz - j - 1) + 1))], c[i]);
// two
for (int j = 0; j < sz - j - 2; j++)
if (s[i].substr(0, j + 1) == rev(s[i].substr(j + 1, j + 1)))
mn(d[h(1, i, sz - (2 * j + 2))], c[i]);
for (int j = sz - 2; j > sz - j - 2; j--)
if (s[i].substr(j + 1, sz - j - 1) ==
rev(s[i].substr(j - (sz - j - 1), sz - j - 1)))
mn(d[h(0, i, sz - (2 * j + 2))], c[i]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int one = s[i].size();
int two = s[j].size();
if (one == two && s[i] + s[j] == rev(s[i] + s[j]))
mn(d[0], c[i] + c[j]);
if (one < two && s[i] == rev(s[j].substr(0, one)))
mn(d[h(1, j, two - one)], c[i] + c[j]);
if (one > two && s[i].substr(one - two, two) == rev(s[j]))
mn(d[h(0, i, one - two)], c[i] + c[j]);
}
}
for (int i = 0; i < N; i++)
if (d[i] < inf)
q.push({d[i], i});
while (!q.empty()) {
auto [da, a] = q.top();
q.pop();
if (d[a] < da)
continue;
for (auto [to, w] : v[a]) {
if (d[a] + w < d[to]) {
d[to] = d[a] + w;
q.push({d[to], to});
}
}
}
if (d[0] == inf)
d[0] = -1;
cout << d[0] << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h(int tp, int id, int len) { return (tp + (id << 1) + (len << 7)); }
string rev(string s) {
reverse(s.begin(), s.end());
return s;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
const int N = (1 << 12);
const ll inf = (1ll << 60);
vector<array<ll, 2>> v[N];
int n;
cin >> n;
vector<string> s(n + 1);
vector<ll> c(n + 1, 0);
for (int i = 1; i <= n; i++)
cin >> s[i] >> c[i];
for (int i = 1; i <= n; i++) {
int one = s[i].size();
for (int len = 1; len <= one; len++) {
for (int j = 1; j <= n; j++) {
int sz = s[j].size();
// prefix-0
if (sz < len && s[i].substr(len - sz, sz) == rev(s[j]))
v[h(0, i, len)].push_back({h(0, i, len - sz), c[j]});
if (sz == len && s[i].substr(0, len) == rev(s[j]))
v[h(0, i, len)].push_back({0, c[j]});
if (sz > len && s[i].substr(0, len) == rev(s[j].substr(0, len)))
v[h(0, i, len)].push_back({h(1, j, sz - len), c[j]});
// suffix-1
if (sz < len && s[i].substr(one - len, sz) == rev(s[j]))
v[h(1, i, len)].push_back({h(1, i, len - sz), c[j]});
if (sz == len && s[i].substr(one - len, len) == rev(s[j]))
v[h(1, i, len)].push_back({0, c[j]});
if (sz > len &&
s[i].substr(one - len, len) == rev(s[j].substr(sz - len, len)))
v[h(1, i, len)].push_back({h(0, j, sz - len), c[j]});
}
}
}
priority_queue<array<ll, 2>, vector<array<ll, 2>>, greater<array<ll, 2>>> q;
vector<ll> d(N, inf);
auto mn = [](ll &a, ll b) { a = min(a, b); };
for (int i = 1; i <= n; i++) {
int sz = s[i].size();
if (s[i] == rev(s[i]))
mn(d[0], c[i]);
// one
for (int j = 0; j < sz - j - 1; j++)
if (s[i].substr(0, j) == rev(s[i].substr(j + 1, j)))
mn(d[h(1, i, sz - (2 * j + 1))], c[i]);
for (int j = sz - 1; j > sz - j - 1; j--)
if (s[i].substr(j + 1, sz - j - 1) ==
rev(s[i].substr(j - (sz - j - 1), sz - j - 1)))
mn(d[h(0, i, sz - (2 * (sz - j - 1) + 1))], c[i]);
// two
for (int j = 0; j < sz - j - 2; j++)
if (s[i].substr(0, j + 1) == rev(s[i].substr(j + 1, j + 1)))
mn(d[h(1, i, sz - (2 * j + 2))], c[i]);
for (int j = sz - 2; j > sz - j - 2; j--)
if (s[i].substr(j + 1, sz - j - 1) ==
rev(s[i].substr(j - (sz - j - 1) + 1, sz - j - 1)))
mn(d[h(0, i, sz - (2 * (sz - j - 2) + 2))], c[i]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int one = s[i].size();
int two = s[j].size();
if (one == two && s[i] + s[j] == rev(s[i] + s[j]))
mn(d[0], c[i] + c[j]);
if (one < two && s[i] == rev(s[j].substr(0, one)))
mn(d[h(1, j, two - one)], c[i] + c[j]);
if (one > two && s[i].substr(one - two, two) == rev(s[j]))
mn(d[h(0, i, one - two)], c[i] + c[j]);
}
}
for (int i = 0; i < N; i++)
if (d[i] < inf)
q.push({d[i], i});
while (!q.empty()) {
auto [da, a] = q.top();
q.pop();
if (d[a] < da)
continue;
for (auto [to, w] : v[a]) {
if (d[a] + w < d[to]) {
d[to] = d[a] + w;
q.push({d[to], to});
}
}
}
if (d[0] == inf)
d[0] = -1;
cout << d[0] << "\n";
return 0;
}
| replace | 68 | 70 | 68 | 70 | 0 | |
p02587 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int, int>;
using VI = vector<long long>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using VVVVI = vector<VVVI>;
const ll INF = 1002003004005006007LL;
bool is_palin(string &s, int from, int to) {
for (; from < to; from++, to--) {
if (s[from] != s[to])
return false;
}
return true;
}
int main() {
int n;
cin >> n;
vector<string> s(n);
vector<ll> c(n);
rep(i, n) cin >> s[i] >> c[i];
VVVI dp(n, VVI(22, VI(2, INF)));
queue<tuple<int, int, bool, ll>> q;
rep(si, n) {
dp[si][0][0] = dp[si][s.size() - 1][1] = c[si];
q.emplace(si, 0, 0, c[si]);
}
ll ans = INF;
while (!q.empty()) {
int si, i;
bool t;
ll cost;
tie(si, i, t, cost) = q.front();
q.pop();
if (dp[si][i][t] != cost)
continue;
if (t == 0) {
if (is_palin(s[si], i, s[si].size() - 1)) {
chmin(ans, cost) continue;
}
rep(ri, n) {
int i1 = i, i2 = s[ri].size() - 1;
bool ok = true;
while (i1 < s[si].size() && i2 >= 0) {
if (s[si][i1] != s[ri][i2]) {
ok = false;
break;
}
i1++;
i2--;
}
if (!ok)
continue;
ll nc = cost + c[ri];
if (i1 < s[si].size()) {
if (nc < dp[si][i1][0]) {
dp[si][i1][0] = nc;
q.emplace(si, i1, 0, nc);
}
} else if (i2 >= 0) {
if (nc < dp[ri][i2][1]) {
dp[ri][i2][1] = nc;
q.emplace(ri, i2, 1, nc);
}
} else {
chmin(ans, nc);
}
}
} else {
if (is_palin(s[si], 0, i)) {
chmin(ans, cost) continue;
}
rep(ri, n) {
int i1 = i, i2 = 0;
bool ok = true;
while (i1 >= 0 && i2 < s[ri].size()) {
if (s[si][i1] != s[ri][i2]) {
ok = false;
break;
}
i1--;
i2++;
}
if (!ok)
continue;
ll nc = cost + c[ri];
if (i1 >= 0) {
if (nc < dp[si][i1][1]) {
dp[si][i1][1] = nc;
q.emplace(si, i1, 1, nc);
}
} else if (i2 < s[ri].size()) {
if (nc < dp[ri][i2][0]) {
dp[ri][i2][0] = nc;
q.emplace(ri, i2, 0, nc);
}
} else {
chmin(ans, nc);
}
}
}
}
if (ans == INF)
ans = -1;
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int, int>;
using VI = vector<long long>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using VVVVI = vector<VVVI>;
const ll INF = 1002003004005006007LL;
bool is_palin(string &s, int from, int to) {
for (; from < to; from++, to--) {
if (s[from] != s[to])
return false;
}
return true;
}
int main() {
int n;
cin >> n;
vector<string> s(n);
vector<ll> c(n);
rep(i, n) cin >> s[i] >> c[i];
VVVI dp(n, VVI(22, VI(2, INF)));
queue<tuple<int, int, bool, ll>> q;
rep(si, n) {
dp[si][0][0] = c[si];
q.emplace(si, 0, 0, c[si]);
}
ll ans = INF;
while (!q.empty()) {
int si, i;
bool t;
ll cost;
tie(si, i, t, cost) = q.front();
q.pop();
if (dp[si][i][t] != cost)
continue;
if (t == 0) {
if (is_palin(s[si], i, s[si].size() - 1)) {
chmin(ans, cost) continue;
}
rep(ri, n) {
int i1 = i, i2 = s[ri].size() - 1;
bool ok = true;
while (i1 < s[si].size() && i2 >= 0) {
if (s[si][i1] != s[ri][i2]) {
ok = false;
break;
}
i1++;
i2--;
}
if (!ok)
continue;
ll nc = cost + c[ri];
if (i1 < s[si].size()) {
if (nc < dp[si][i1][0]) {
dp[si][i1][0] = nc;
q.emplace(si, i1, 0, nc);
}
} else if (i2 >= 0) {
if (nc < dp[ri][i2][1]) {
dp[ri][i2][1] = nc;
q.emplace(ri, i2, 1, nc);
}
} else {
chmin(ans, nc);
}
}
} else {
if (is_palin(s[si], 0, i)) {
chmin(ans, cost) continue;
}
rep(ri, n) {
int i1 = i, i2 = 0;
bool ok = true;
while (i1 >= 0 && i2 < s[ri].size()) {
if (s[si][i1] != s[ri][i2]) {
ok = false;
break;
}
i1--;
i2++;
}
if (!ok)
continue;
ll nc = cost + c[ri];
if (i1 >= 0) {
if (nc < dp[si][i1][1]) {
dp[si][i1][1] = nc;
q.emplace(si, i1, 1, nc);
}
} else if (i2 < s[ri].size()) {
if (nc < dp[ri][i2][0]) {
dp[ri][i2][0] = nc;
q.emplace(ri, i2, 0, nc);
}
} else {
chmin(ans, nc);
}
}
}
}
if (ans == INF)
ans = -1;
cout << ans << endl;
}
| replace | 31 | 32 | 31 | 32 | 0 | |
p02587 | C++ | Time Limit Exceeded | // This Code was made by Chinese_zjc_.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// #include<windows.h>
#define int long long
#define PI 3.14159265358979323
#define INF 0x3fffffffffffffff
using namespace std;
int n, ans = INF;
struct L {
int v;
string s, e;
bool operator<(const L &TMP) const { return v < TMP.v; }
} a[55];
struct F {
int used;
string g;
bool operator<(const F &TMP) const { return used > TMP.used; }
};
priority_queue<F> dl;
string work(string now) {
for (int i = 0; i < (int)now.length() >> 1; ++i) {
swap(now[i], now[now.length() - i - 1]);
}
return now;
}
bool good(string now, string x) {
for (int i = 0; i < (int)x.length(); ++i) {
if (now[i] != x[i]) {
return false;
}
}
return true;
}
bool allhigh(string now) {
for (int i = 1; i <= n; ++i) {
if (now.length() < a[i].e.length()) {
return false;
}
}
return true;
}
bool can(F now) {
if (allhigh(now.g)) {
for (int i = 1; i <= n; ++i) {
if (now.used + a[i].v < ans &&
now.g.substr(0, a[i].e.length()) == a[i].e) {
return true;
}
}
return false;
}
for (int i = 1; i <= n; ++i) {
if (now.used + a[i].v >= ans) {
continue;
}
int len = min(a[i].e.length(), now.g.length());
bool flag = true;
for (int j = 0; j < len; ++j) {
if (now.g[j] != a[i].e[j]) {
flag = false;
break;
}
}
if (flag) {
return true;
}
}
return false;
}
bool all(string now) {
for (int i = 0; i < (int)now.length() >> 1; ++i) {
if (now[i] != now[now.length() - 1 - i]) {
return false;
}
}
return true;
}
signed main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i].s >> a[i].v;
a[i].e = work(a[i].s);
}
sort(a + 1, a + 1 + n);
dl.push((F){0, ""});
while (!dl.empty() && dl.top().used < ans) {
F now = dl.top();
dl.pop();
for (int i = 1; i <= n; ++i) {
if (all(now.g + a[i].s)) {
ans = min(now.used + a[i].v, ans);
}
}
for (int i = 1; i <= n; ++i) {
if (good(now.g, a[i].e) &&
can((F){now.used + a[i].v,
now.g.substr(a[i].e.length(),
now.g.length() - a[i].e.length())})) {
dl.push((F){
now.used + a[i].v,
now.g.substr(a[i].e.length(), now.g.length() - a[i].e.length())});
}
}
if (allhigh(now.g)) {
continue;
}
for (int i = 1; i <= n; ++i) {
if (can((F){now.used + a[i].v, now.g + a[i].s})) {
dl.push((F){now.used + a[i].v, now.g + a[i].s});
}
}
}
cout << (ans == INF ? -1 : ans) << endl;
return 0;
} | // This Code was made by Chinese_zjc_.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// #include<windows.h>
#define int long long
#define PI 3.14159265358979323
#define INF 0x3fffffffffffffff
using namespace std;
int n, ans = INF;
struct L {
int v;
string s, e;
bool operator<(const L &TMP) const { return v < TMP.v; }
} a[55];
struct F {
int used;
string g;
bool operator<(const F &TMP) const { return used > TMP.used; }
};
priority_queue<F> dl;
string work(string now) {
for (int i = 0; i < (int)now.length() >> 1; ++i) {
swap(now[i], now[now.length() - i - 1]);
}
return now;
}
bool good(string now, string x) {
for (int i = 0; i < (int)x.length(); ++i) {
if (now[i] != x[i]) {
return false;
}
}
return true;
}
bool allhigh(string now) {
for (int i = 1; i <= n; ++i) {
if (now.length() < a[i].e.length() &&
now == a[i].e.substr(0, now.length())) {
return false;
}
}
return true;
}
bool can(F now) {
if (allhigh(now.g)) {
for (int i = 1; i <= n; ++i) {
if (now.used + a[i].v < ans &&
now.g.substr(0, a[i].e.length()) == a[i].e) {
return true;
}
}
return false;
}
for (int i = 1; i <= n; ++i) {
if (now.used + a[i].v >= ans) {
continue;
}
int len = min(a[i].e.length(), now.g.length());
bool flag = true;
for (int j = 0; j < len; ++j) {
if (now.g[j] != a[i].e[j]) {
flag = false;
break;
}
}
if (flag) {
return true;
}
}
return false;
}
bool all(string now) {
for (int i = 0; i < (int)now.length() >> 1; ++i) {
if (now[i] != now[now.length() - 1 - i]) {
return false;
}
}
return true;
}
signed main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i].s >> a[i].v;
a[i].e = work(a[i].s);
}
sort(a + 1, a + 1 + n);
dl.push((F){0, ""});
while (!dl.empty() && dl.top().used < ans) {
F now = dl.top();
dl.pop();
for (int i = 1; i <= n; ++i) {
if (all(now.g + a[i].s)) {
ans = min(now.used + a[i].v, ans);
}
}
for (int i = 1; i <= n; ++i) {
if (good(now.g, a[i].e) &&
can((F){now.used + a[i].v,
now.g.substr(a[i].e.length(),
now.g.length() - a[i].e.length())})) {
dl.push((F){
now.used + a[i].v,
now.g.substr(a[i].e.length(), now.g.length() - a[i].e.length())});
}
}
if (allhigh(now.g)) {
continue;
}
for (int i = 1; i <= n; ++i) {
if (can((F){now.used + a[i].v, now.g + a[i].s})) {
dl.push((F){now.used + a[i].v, now.g + a[i].s});
}
}
}
cout << (ans == INF ? -1 : ans) << endl;
return 0;
} | replace | 49 | 50 | 49 | 51 | TLE | |
p02587 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 105;
typedef long long ll;
int n, m, a[maxn], b[maxn], len;
int c[maxn];
string s[maxn];
int tt;
vector<pair<int, int>> adj[maxn];
inline bool chk1(string t) {
if (t.size() == 0)
return true;
int l = 0, r = (int)t.size() - 1;
while (l <= r) {
if (t[l] != t[r])
return false;
l++, r--;
}
return true;
}
struct node {
int id;
ll val;
bool operator<(const node &n1) const { return val > n1.val; }
node(int id = 0, ll val = 0) : id(id), val(val) {}
};
priority_queue<node> q;
ll d[maxn * 25];
int vis[maxn * 25];
int id[maxn][25];
ll ans = 1e16;
inline void diji() {
while (!q.empty()) {
int u = q.top().id;
q.pop();
if (vis[u])
continue;
vis[u] = 1;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i].first;
int w = adj[u][i].second;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
q.push(node(v, d[v]));
}
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
string s1;
int x;
cin >> s1 >> x;
if (chk1(s1)) {
ans = min(ans, (ll)x);
continue;
}
len++;
b[len] = s1.size();
s[len] = " " + s1;
c[len] = x;
}
n = len;
for (int i = n + 1; i <= n * 2; i++) {
s[i] = s[i - n];
c[i] = c[i - n];
b[i] = b[i - n];
int l = 1, r = b[i];
while (l <= r) {
swap(s[i][l], s[i][r]);
l++, r--;
}
}
for (int i = 1; i <= n * 2; i++) {
for (int j = 0; j <= b[i]; j++) {
id[i][j] = ++tt;
}
}
for (int i = 1; i <= n * 2; i++) {
for (int j = 0; j <= b[i]; j++) {
if (j == b[i])
continue;
int l, r;
if (i <= n) {
l = n + 1, r = n * 2;
} else {
l = 1, r = n;
}
for (int k = l; k <= r; k++) {
int p1 = j, p2 = 0;
while (p1 + 1 <= b[i] && p2 + 1 <= b[k] &&
s[i][p1 + 1] == s[k][p2 + 1]) {
p1++;
p2++;
}
if (p1 == b[i] && p2 == b[k]) {
adj[id[i][j]].push_back(make_pair(id[i][b[i]], c[k]));
adj[id[i][j]].push_back(make_pair(id[k][b[k]], c[k]));
continue;
}
if (p1 == b[i]) {
adj[id[i][j]].push_back(make_pair(id[k][p2], c[k]));
continue;
}
if (p2 == b[k]) {
adj[id[i][j]].push_back(make_pair(id[i][p1], c[k]));
continue;
}
}
}
}
memset(d, 0x3f, sizeof(d));
for (int i = 1; i <= n * 2; i++) {
d[id[i][0]] = c[i];
q.push(node(id[i][0], d[id[i][0]]));
}
diji();
for (int i = 1; i <= n * 2; i++) {
for (int j = 1; j <= b[i]; j++) {
string t = "";
for (int k = j + 1; k <= b[i]; k++) {
t.push_back(s[i][k]);
}
if (chk1(t)) {
ans = min(ans, d[id[i][j]]);
}
}
}
if (ans >= 1e16) {
cout << "-1" << endl;
} else
cout << ans << endl;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 105;
typedef long long ll;
int n, m, a[maxn], b[maxn], len;
int c[maxn];
string s[maxn];
int tt;
vector<pair<int, int>> adj[maxn * 25];
inline bool chk1(string t) {
if (t.size() == 0)
return true;
int l = 0, r = (int)t.size() - 1;
while (l <= r) {
if (t[l] != t[r])
return false;
l++, r--;
}
return true;
}
struct node {
int id;
ll val;
bool operator<(const node &n1) const { return val > n1.val; }
node(int id = 0, ll val = 0) : id(id), val(val) {}
};
priority_queue<node> q;
ll d[maxn * 25];
int vis[maxn * 25];
int id[maxn][25];
ll ans = 1e16;
inline void diji() {
while (!q.empty()) {
int u = q.top().id;
q.pop();
if (vis[u])
continue;
vis[u] = 1;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i].first;
int w = adj[u][i].second;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
q.push(node(v, d[v]));
}
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
string s1;
int x;
cin >> s1 >> x;
if (chk1(s1)) {
ans = min(ans, (ll)x);
continue;
}
len++;
b[len] = s1.size();
s[len] = " " + s1;
c[len] = x;
}
n = len;
for (int i = n + 1; i <= n * 2; i++) {
s[i] = s[i - n];
c[i] = c[i - n];
b[i] = b[i - n];
int l = 1, r = b[i];
while (l <= r) {
swap(s[i][l], s[i][r]);
l++, r--;
}
}
for (int i = 1; i <= n * 2; i++) {
for (int j = 0; j <= b[i]; j++) {
id[i][j] = ++tt;
}
}
for (int i = 1; i <= n * 2; i++) {
for (int j = 0; j <= b[i]; j++) {
if (j == b[i])
continue;
int l, r;
if (i <= n) {
l = n + 1, r = n * 2;
} else {
l = 1, r = n;
}
for (int k = l; k <= r; k++) {
int p1 = j, p2 = 0;
while (p1 + 1 <= b[i] && p2 + 1 <= b[k] &&
s[i][p1 + 1] == s[k][p2 + 1]) {
p1++;
p2++;
}
if (p1 == b[i] && p2 == b[k]) {
adj[id[i][j]].push_back(make_pair(id[i][b[i]], c[k]));
adj[id[i][j]].push_back(make_pair(id[k][b[k]], c[k]));
continue;
}
if (p1 == b[i]) {
adj[id[i][j]].push_back(make_pair(id[k][p2], c[k]));
continue;
}
if (p2 == b[k]) {
adj[id[i][j]].push_back(make_pair(id[i][p1], c[k]));
continue;
}
}
}
}
memset(d, 0x3f, sizeof(d));
for (int i = 1; i <= n * 2; i++) {
d[id[i][0]] = c[i];
q.push(node(id[i][0], d[id[i][0]]));
}
diji();
for (int i = 1; i <= n * 2; i++) {
for (int j = 1; j <= b[i]; j++) {
string t = "";
for (int k = j + 1; k <= b[i]; k++) {
t.push_back(s[i][k]);
}
if (chk1(t)) {
ans = min(ans, d[id[i][j]]);
}
}
}
if (ans >= 1e16) {
cout << "-1" << endl;
} else
cout << ans << endl;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p02588 | C++ | Time Limit Exceeded | #define _USE_MATH_DEFINES
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC target("avx")
// #include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <emmintrin.h>
#include <mmintrin.h>
#include <nmmintrin.h>
using namespace std;
#define dprint(Exp, ...) \
if (Exp) { \
fprintf(stderr, __VA_ARGS__); \
}
#define printe(...) fprintf(stderr, __VA_ARGS__);
// #define printe(...) void();
#define printE(...) fprintf(stderr, __VA_ARGS__);
// #define printE(...) void();
#define PrtExp(_Exp) cerr << #_Exp << " = " << (_Exp)
#define PrtExpN(_Exp) cerr << #_Exp << " = " << (_Exp) << "\n"
#define SINT(n) scanf("%d", &n)
#define SINT2(n, m) scanf("%d %d", &n, &m)
#define SINT3(n, m, o) scanf("%d %d %d", &n, &m, &o)
#define SINT4(n, m, o, P) scanf("%d %d %d %d", &n, &m, &o, &P)
#define SINT5(n, m, o, P, q) scanf("%d %d %d %d %d", &n, &m, &o, &P, &q)
#define SLL(n) scanf("%lld", &n)
#define SLL2(n, m) scanf("%lld %lld", &n, &m)
#define SLL3(n, m, o) scanf("%lld %lld %lld", &n, &m, &o)
#define SST(s) scanf("%s", s)
#define SCH(c) scanf("%c", &c)
#define GC() getchar()
#define PINT(n) printf("%d", (int)(n))
#define PINT2(n, m) printf("%d %d", (int)(n), (int)(m))
#define PINT3(n, m, l) printf("%d %d %d", (int)(n), (int)(m), (int)(l))
#define PLL(n) printf("%lld", (long long)(n))
#define PST(s) printf("%s", (s))
#define PCH(s) printf("%c", (s))
#define PINTN(n) printf("%d\n", (int)(n))
#define PINT2N(n, m) printf("%d %d\n", (int)(n), (int)(m))
#define PINT3N(n, m, l) printf("%d %d %d\n", (int)(n), (int)(m), (int)(l))
#define PLLN(n) printf("%lld\n", (long long)(n))
#define PSTN(s) printf("%s\n", (s))
#define PCHN(s) printf("%c\n", (s))
#define PSP() printf(" ")
#define PN() printf("\n")
#define PC(c) putchar(c)
#define CSP (' ')
#define SN ("\n")
#define rep(i, a) for (int i = 0; i < a; i++)
#define reP(i, a) for (int i = 0; i <= a; i++)
#define Rep(i, a) for (int i = a - 1; i >= 0; i--)
#define ReP(i, a) for (int i = a; i >= 0; i--)
#define rEp(i, a) for (i = 0; i < a; i++)
#define rEP(i, a) for (i = 0; i <= a; i++)
#define REp(i, a) for (i = a - 1; i >= 0; i--)
#define REP(i, a) for (i = a; i >= 0; i--)
#define repft(i, a, b) for (int i = a; i < b; i++)
#define repfT(i, a, b) for (int i = a; i <= b; i++)
#define Repft(i, a, b) for (int i = a - 1; i >= b; i--)
#define RepfT(i, a, b) for (int i = a; i >= b; i--)
#define foreach(a, it) for (auto it = a.begin(); it != a.end(); ++it)
#define FILL(a, v) fill(begin(a), end(a), v)
#define FILL0(a) memset(a, 0, sizeof(a))
#define FILL1(a) memset(a, -1, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
const int INF = 1'010'000'000; // 0x3C33'6080
const ll INFLL = 0x1f1f1f1f1f1f1f1fLL; // 2,242,545,357,980,376,863
template <class A, class B>
inline ostream &operator<<(ostream &st, const pair<A, B> &P) {
return st << "(" << P.first << "," << P.second << ")";
};
template <class A, class B>
inline pair<A, B> operator+(const pair<A, B> &P, const pair<A, B> &Q) {
return pair<A, B>(P.first + Q.first, P.second + Q.second);
};
template <class A, class B>
inline pair<A, B> operator-(const pair<A, B> &P, const pair<A, B> &Q) {
return pair<A, B>(P.first - Q.first, P.second - Q.second);
};
#define fs first
#define sc second
int bitCount(unsigned long long bits) {
#if _MSC_VER >= 1920 || __INTEL_COMPILER
return _mm_popcnt_u64(bits);
#else
return __builtin_popcountll(bits);
#endif
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int cnt[20][20];
int main() {
int n;
cin >> n;
const ll E9 = 1000000000;
ll ret = 0;
rep(i, n) {
string s;
cin >> s;
ll a = 0;
int j = 0;
int k = 9;
for (; j < s.size(); ++j) {
if (s[j] == '.') {
break;
}
a = a * 10 + s[j] - '0';
}
if (j != s.size()) {
for (++j; j < s.size(); ++j) {
a = a * 10 + s[j] - '0';
--k;
}
}
rep(_, k) a *= 10;
int m2 = 0;
int m5 = 0;
while (a % 2 == 0) {
m2++;
a /= 2;
}
while (a % 5 == 0) {
m5++;
a /= 5;
}
// printe("[%d %d] %lld\n", m2, m5, a);
rep(a, 20) rep(b, 20) {
if (a + m2 < 18)
continue;
if (b + m5 < 18)
continue;
ret += cnt[a][b];
if (cnt[a][b] > 0)
printe("%d %d (%d)\n", a, b, cnt[a][b]);
}
cnt[min(m2, 18)][min(m5, 18)]++;
// printe("[%d,%d]\n", m2, m5);
// if (m2 * 2 >= 9 && m5 * 2 >= 9) ret--;
}
cout << ret << endl;
}
| #define _USE_MATH_DEFINES
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC target("avx")
// #include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <emmintrin.h>
#include <mmintrin.h>
#include <nmmintrin.h>
using namespace std;
#define dprint(Exp, ...) \
if (Exp) { \
fprintf(stderr, __VA_ARGS__); \
}
#define printe(...) fprintf(stderr, __VA_ARGS__);
// #define printe(...) void();
#define printE(...) fprintf(stderr, __VA_ARGS__);
// #define printE(...) void();
#define PrtExp(_Exp) cerr << #_Exp << " = " << (_Exp)
#define PrtExpN(_Exp) cerr << #_Exp << " = " << (_Exp) << "\n"
#define SINT(n) scanf("%d", &n)
#define SINT2(n, m) scanf("%d %d", &n, &m)
#define SINT3(n, m, o) scanf("%d %d %d", &n, &m, &o)
#define SINT4(n, m, o, P) scanf("%d %d %d %d", &n, &m, &o, &P)
#define SINT5(n, m, o, P, q) scanf("%d %d %d %d %d", &n, &m, &o, &P, &q)
#define SLL(n) scanf("%lld", &n)
#define SLL2(n, m) scanf("%lld %lld", &n, &m)
#define SLL3(n, m, o) scanf("%lld %lld %lld", &n, &m, &o)
#define SST(s) scanf("%s", s)
#define SCH(c) scanf("%c", &c)
#define GC() getchar()
#define PINT(n) printf("%d", (int)(n))
#define PINT2(n, m) printf("%d %d", (int)(n), (int)(m))
#define PINT3(n, m, l) printf("%d %d %d", (int)(n), (int)(m), (int)(l))
#define PLL(n) printf("%lld", (long long)(n))
#define PST(s) printf("%s", (s))
#define PCH(s) printf("%c", (s))
#define PINTN(n) printf("%d\n", (int)(n))
#define PINT2N(n, m) printf("%d %d\n", (int)(n), (int)(m))
#define PINT3N(n, m, l) printf("%d %d %d\n", (int)(n), (int)(m), (int)(l))
#define PLLN(n) printf("%lld\n", (long long)(n))
#define PSTN(s) printf("%s\n", (s))
#define PCHN(s) printf("%c\n", (s))
#define PSP() printf(" ")
#define PN() printf("\n")
#define PC(c) putchar(c)
#define CSP (' ')
#define SN ("\n")
#define rep(i, a) for (int i = 0; i < a; i++)
#define reP(i, a) for (int i = 0; i <= a; i++)
#define Rep(i, a) for (int i = a - 1; i >= 0; i--)
#define ReP(i, a) for (int i = a; i >= 0; i--)
#define rEp(i, a) for (i = 0; i < a; i++)
#define rEP(i, a) for (i = 0; i <= a; i++)
#define REp(i, a) for (i = a - 1; i >= 0; i--)
#define REP(i, a) for (i = a; i >= 0; i--)
#define repft(i, a, b) for (int i = a; i < b; i++)
#define repfT(i, a, b) for (int i = a; i <= b; i++)
#define Repft(i, a, b) for (int i = a - 1; i >= b; i--)
#define RepfT(i, a, b) for (int i = a; i >= b; i--)
#define foreach(a, it) for (auto it = a.begin(); it != a.end(); ++it)
#define FILL(a, v) fill(begin(a), end(a), v)
#define FILL0(a) memset(a, 0, sizeof(a))
#define FILL1(a) memset(a, -1, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
const int INF = 1'010'000'000; // 0x3C33'6080
const ll INFLL = 0x1f1f1f1f1f1f1f1fLL; // 2,242,545,357,980,376,863
template <class A, class B>
inline ostream &operator<<(ostream &st, const pair<A, B> &P) {
return st << "(" << P.first << "," << P.second << ")";
};
template <class A, class B>
inline pair<A, B> operator+(const pair<A, B> &P, const pair<A, B> &Q) {
return pair<A, B>(P.first + Q.first, P.second + Q.second);
};
template <class A, class B>
inline pair<A, B> operator-(const pair<A, B> &P, const pair<A, B> &Q) {
return pair<A, B>(P.first - Q.first, P.second - Q.second);
};
#define fs first
#define sc second
int bitCount(unsigned long long bits) {
#if _MSC_VER >= 1920 || __INTEL_COMPILER
return _mm_popcnt_u64(bits);
#else
return __builtin_popcountll(bits);
#endif
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int cnt[20][20];
int main() {
int n;
cin >> n;
const ll E9 = 1000000000;
ll ret = 0;
rep(i, n) {
string s;
cin >> s;
ll a = 0;
int j = 0;
int k = 9;
for (; j < s.size(); ++j) {
if (s[j] == '.') {
break;
}
a = a * 10 + s[j] - '0';
}
if (j != s.size()) {
for (++j; j < s.size(); ++j) {
a = a * 10 + s[j] - '0';
--k;
}
}
rep(_, k) a *= 10;
int m2 = 0;
int m5 = 0;
while (a % 2 == 0) {
m2++;
a /= 2;
}
while (a % 5 == 0) {
m5++;
a /= 5;
}
// printe("[%d %d] %lld\n", m2, m5, a);
rep(a, 20) rep(b, 20) {
if (a + m2 < 18)
continue;
if (b + m5 < 18)
continue;
ret += cnt[a][b];
// if (cnt[a][b] > 0) printe("%d %d (%d)\n", a, b, cnt[a][b]);
}
cnt[min(m2, 18)][min(m5, 18)]++;
// printe("[%d,%d]\n", m2, m5);
// if (m2 * 2 >= 9 && m5 * 2 >= 9) ret--;
}
cout << ret << endl;
}
| replace | 182 | 184 | 182 | 183 | TLE | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> cnt(20, vector<int>(20));
vector<pair<int, int>> a;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
long long x = 0;
bool found = false;
for (int i = 0; i < (int)s.size(); i++) {
if (s[i] != '.')
(x *= 10) += s[i] - '0';
else
found = true;
}
if (!found) {
x *= (long long)1e9;
} else {
int cur = 0;
while (s.back() != '.') {
++cur;
s.pop_back();
}
for (int it = 0; it < 9 - cur; it++)
x *= 10;
}
int cnt2 = 0;
long long y = x;
while (y % 2 == 0) {
++cnt2;
y /= 2;
}
int cnt5 = 0;
y = x;
while (y % 5 == 0) {
++cnt5;
y /= 5;
}
++cnt[min(18, cnt2)][min(18, cnt5)];
a.emplace_back(cnt2, cnt5);
}
for (int i = 18; i >= 0; i--) {
for (int j = 18; j >= 0; j--) {
cnt[i][j] += cnt[i + 1][j] + cnt[i][j + 1] - cnt[i + 1][j + 1];
}
}
long long res = 0;
for (auto x : a) {
res += cnt[18 - x.first][18 - x.second] - (x.first >= 9 && x.second >= 9);
}
res /= 2;
cout << res << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> cnt(20, vector<int>(20));
vector<pair<int, int>> a;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
long long x = 0;
bool found = false;
for (int i = 0; i < (int)s.size(); i++) {
if (s[i] != '.')
(x *= 10) += s[i] - '0';
else
found = true;
}
if (!found) {
x *= (long long)1e9;
} else {
int cur = 0;
while (s.back() != '.') {
++cur;
s.pop_back();
}
for (int it = 0; it < 9 - cur; it++)
x *= 10;
}
int cnt2 = 0;
long long y = x;
while (y % 2 == 0) {
++cnt2;
y /= 2;
}
int cnt5 = 0;
y = x;
while (y % 5 == 0) {
++cnt5;
y /= 5;
}
++cnt[min(18, cnt2)][min(18, cnt5)];
a.emplace_back(min(18, cnt2), min(18, cnt5));
}
for (int i = 18; i >= 0; i--) {
for (int j = 18; j >= 0; j--) {
cnt[i][j] += cnt[i + 1][j] + cnt[i][j + 1] - cnt[i + 1][j + 1];
}
}
long long res = 0;
for (auto x : a) {
res += cnt[18 - x.first][18 - x.second] - (x.first >= 9 && x.second >= 9);
}
res /= 2;
cout << res << '\n';
return 0;
}
| replace | 46 | 47 | 46 | 47 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rp(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
if (former < b) {
former = b;
return true;
}
return false;
}
template <class T> bool chmin(T &former, const T &b) {
if (b < former) {
former = b;
return true;
}
return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v) \
std::sort(v.begin(), v.end(), \
std::greater<decltype(v[0])>()) // 降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v>>
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\n"; };
template <class T> inline void Yes(T condition) {
if (condition)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
if (condition)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////
void solve() {
ll n;
cin >> n;
Vi a(n);
rp(i, n) {
string s;
cin >> s;
int si = s.size();
bool point = false;
int num = 0;
rp(j, si) {
if (s[j] == '.') {
point = true;
num = si - j - 1;
continue;
}
if (point) {
s[j - 1] = s[j];
}
}
if (point)
s[si - 1] = '0';
ll aa = stoll(s);
if (!point)
aa *= 1000000000;
else {
rp(j, 9 - num - 1) aa *= 10;
if (num == 9)
aa /= 10;
}
a[i] = aa;
}
matrix mat(19, Vi(19, 0)), summat(19, Vi(19, 0));
rp(i, n) {
ll t = 0, f = 0;
ll as = a[i];
while (as % 2 == 0) {
as /= 2;
t++;
}
while (as % 5 == 0) {
as /= 5;
f++;
}
mat[t][f]++;
}
for (ll i = 18; i >= 0; i--) {
for (ll j = 18; j >= 0; j--) {
if (i == 18 && j == 18) {
summat[i][j] = mat[i][j];
continue;
}
if (i == 18) {
summat[i][j] = summat[i][j + 1] + mat[i][j];
continue;
}
if (j == 18) {
summat[i][j] = summat[i + 1][j] + mat[i][j];
continue;
}
summat[i][j] = summat[i + 1][j] + summat[i][j + 1] -
summat[i + 1][j + 1] + mat[i][j];
}
}
ll ans = 0;
rp(i, 19) {
rp(j, 19) {
if (i >= 9 && j >= 9) {
ans += mat[i][j] * (summat[18 - i][18 - j] - 1);
continue;
}
ans += mat[i][j] * summat[18 - i][18 - j];
}
}
ans /= 2;
print(ans);
return;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(30);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rp(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
if (former < b) {
former = b;
return true;
}
return false;
}
template <class T> bool chmin(T &former, const T &b) {
if (b < former) {
former = b;
return true;
}
return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v) \
std::sort(v.begin(), v.end(), \
std::greater<decltype(v[0])>()) // 降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v>>
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\n"; };
template <class T> inline void Yes(T condition) {
if (condition)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
if (condition)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////
void solve() {
ll n;
cin >> n;
Vi a(n);
rp(i, n) {
string s;
cin >> s;
int si = s.size();
bool point = false;
int num = 0;
rp(j, si) {
if (s[j] == '.') {
point = true;
num = si - j - 1;
continue;
}
if (point) {
s[j - 1] = s[j];
}
}
if (point)
s[si - 1] = '0';
ll aa = stoll(s);
if (!point)
aa *= 1000000000;
else {
rp(j, 9 - num - 1) aa *= 10;
if (num == 9)
aa /= 10;
}
a[i] = aa;
}
matrix mat(19, Vi(19, 0)), summat(19, Vi(19, 0));
rp(i, n) {
ll t = 0, f = 0;
ll as = a[i];
while (as % 2 == 0) {
as /= 2;
t++;
}
while (as % 5 == 0) {
as /= 5;
f++;
}
if (t > 18)
t = 18;
if (f > 18)
f = 18;
mat[t][f]++;
}
for (ll i = 18; i >= 0; i--) {
for (ll j = 18; j >= 0; j--) {
if (i == 18 && j == 18) {
summat[i][j] = mat[i][j];
continue;
}
if (i == 18) {
summat[i][j] = summat[i][j + 1] + mat[i][j];
continue;
}
if (j == 18) {
summat[i][j] = summat[i + 1][j] + mat[i][j];
continue;
}
summat[i][j] = summat[i + 1][j] + summat[i][j + 1] -
summat[i + 1][j + 1] + mat[i][j];
}
}
ll ans = 0;
rp(i, 19) {
rp(j, 19) {
if (i >= 9 && j >= 9) {
ans += mat[i][j] * (summat[18 - i][18 - j] - 1);
continue;
}
ans += mat[i][j] * summat[18 - i][18 - j];
}
}
ans /= 2;
print(ans);
return;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(30);
solve();
return 0;
} | insert | 96 | 96 | 96 | 100 | 0 | |
p02588 | C++ | Runtime Error | #line 2 "/Users/kaage/Desktop/ProgrammingWorkspace/library/other/template.hpp"
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx2")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define all(V) V.begin(), V.end()
typedef long long lint;
typedef unsigned long long ulint;
typedef std::pair<int, int> P;
typedef std::pair<lint, lint> LP;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;
constexpr double eps = DBL_EPSILON;
constexpr double PI = 3.141592653589793238462643383279;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
inline lint gcd(lint a, lint b) {
while (b) {
lint c = a;
a = b;
b = c % b;
}
return a;
}
inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
bool isprime(lint n) {
if (n == 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
template <typename T> T mypow(T a, lint b) {
T res(1);
while (b) {
if (b & 1)
res *= a;
a *= a;
b >>= 1;
}
return res;
}
lint modpow(lint a, lint b, lint m) {
lint res(1);
while (b) {
if (b & 1) {
res *= a;
res /= m;
}
a *= a;
a /= m;
b >>= 1;
}
return res;
}
template <typename T> void printArray(std::vector<T> &vec) {
rep(i, vec.size()) {
std::cout << vec[i];
std::cout << (i == (int)vec.size() - 1 ? "\n" : " ");
}
}
template <typename T> void printArray(T l, T r) {
T rprev = std::prev(r);
for (T i = l; i != rprev; i++) {
std::cout << *i << " ";
}
std::cout << *rprev << std::endl;
}
#line 2 "main.cpp"
int n, c[100010], d[10][45][20], e[100010], f[100010];
std::string a[100010];
int main() {
std::cin >> n;
rep(i, n) {
std::cin >> a[i];
lint b = 0;
bool g = false;
rep(j, a[i].size()) {
if (a[i][j] == '.')
g = true;
else {
if (g)
c[i]++;
b *= 10;
b += a[i][j] - '0';
}
}
while (b % 2 == 0) {
e[i]++;
b /= 2;
}
while (b % 5 == 0) {
f[i]++;
b /= 5;
}
d[c[i]][e[i]][f[i]]++;
}
rep(i, 10) {
for (int j = 43; j >= 0; j--) {
for (int k = 19; k >= 0; k--)
d[i][j][k] += d[i][j + 1][k];
}
for (int j = 44; j >= 0; j--) {
for (int k = 18; k >= 0; k--)
d[i][j][k] += d[i][j][k + 1];
}
}
lint ans = 0;
rep(i, n) {
if (c[i] * 2 - e[i] <= e[i] && c[i] * 2 - f[i] <= f[i])
ans--;
rep(j, 10) {
ans += d[j][std::max(0, c[i] + j - e[i])][std::max(0, c[i] + j - f[i])];
}
}
std::cout << ans / 2 << std::endl;
return 0;
} | #line 2 "/Users/kaage/Desktop/ProgrammingWorkspace/library/other/template.hpp"
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx2")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define all(V) V.begin(), V.end()
typedef long long lint;
typedef unsigned long long ulint;
typedef std::pair<int, int> P;
typedef std::pair<lint, lint> LP;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;
constexpr double eps = DBL_EPSILON;
constexpr double PI = 3.141592653589793238462643383279;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
inline lint gcd(lint a, lint b) {
while (b) {
lint c = a;
a = b;
b = c % b;
}
return a;
}
inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
bool isprime(lint n) {
if (n == 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
template <typename T> T mypow(T a, lint b) {
T res(1);
while (b) {
if (b & 1)
res *= a;
a *= a;
b >>= 1;
}
return res;
}
lint modpow(lint a, lint b, lint m) {
lint res(1);
while (b) {
if (b & 1) {
res *= a;
res /= m;
}
a *= a;
a /= m;
b >>= 1;
}
return res;
}
template <typename T> void printArray(std::vector<T> &vec) {
rep(i, vec.size()) {
std::cout << vec[i];
std::cout << (i == (int)vec.size() - 1 ? "\n" : " ");
}
}
template <typename T> void printArray(T l, T r) {
T rprev = std::prev(r);
for (T i = l; i != rprev; i++) {
std::cout << *i << " ";
}
std::cout << *rprev << std::endl;
}
#line 2 "main.cpp"
int n, c[200010], d[10][45][20], e[200010], f[200010];
std::string a[200010];
int main() {
std::cin >> n;
rep(i, n) {
std::cin >> a[i];
lint b = 0;
bool g = false;
rep(j, a[i].size()) {
if (a[i][j] == '.')
g = true;
else {
if (g)
c[i]++;
b *= 10;
b += a[i][j] - '0';
}
}
while (b % 2 == 0) {
e[i]++;
b /= 2;
}
while (b % 5 == 0) {
f[i]++;
b /= 5;
}
d[c[i]][e[i]][f[i]]++;
}
rep(i, 10) {
for (int j = 43; j >= 0; j--) {
for (int k = 19; k >= 0; k--)
d[i][j][k] += d[i][j + 1][k];
}
for (int j = 44; j >= 0; j--) {
for (int k = 18; k >= 0; k--)
d[i][j][k] += d[i][j][k + 1];
}
}
lint ans = 0;
rep(i, n) {
if (c[i] * 2 - e[i] <= e[i] && c[i] * 2 - f[i] <= f[i])
ans--;
rep(j, 10) {
ans += d[j][std::max(0, c[i] + j - e[i])][std::max(0, c[i] + j - f[i])];
}
}
std::cout << ans / 2 << std::endl;
return 0;
} | replace | 115 | 117 | 115 | 117 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reverse_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar()) {
if (ch == '-')
f = -1;
}
for (; isdigit(ch); ch = getchar()) {
x = x * 10 + ch - 48;
}
return x * f;
}
const int mxN = 2e5;
const int mxW = 100;
double a[mxN + 3];
int a1[mxW + 3], a2[mxW + 3];
int s[mxW + 3][mxW + 3];
int n;
int main() {
n = read();
for (int i = 1; i <= n; i++) {
scanf("%lf", &a[i]);
a[i] *= 1000000000.0;
llong x = (llong)(a[i] + 0.5);
while (x % 2 == 0) {
a1[i]++;
x /= 2;
}
while (x % 5 == 0) {
a2[i]++;
x /= 5;
}
s[a1[i]][a2[i]]++;
}
llong ans = 0ll;
for (int i = mxW; i >= 0; i--)
for (int j = mxW; j >= 0; j--) {
s[i][j] += s[i + 1][j] + s[i][j + 1] - s[i + 1][j + 1];
}
for (int i = 1; i <= n; i++) {
ans += s[max(0, 18 - a1[i])][max(0, 18 - a2[i])];
}
ans -= s[9][9];
printf("%lld\n", ans / 2ll);
return 0;
} | #include <bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reverse_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar()) {
if (ch == '-')
f = -1;
}
for (; isdigit(ch); ch = getchar()) {
x = x * 10 + ch - 48;
}
return x * f;
}
const int mxN = 2e5;
const int mxW = 100;
double a[mxN + 3];
int a1[mxN + 3], a2[mxN + 3];
int s[mxW + 3][mxW + 3];
int n;
int main() {
n = read();
for (int i = 1; i <= n; i++) {
scanf("%lf", &a[i]);
a[i] *= 1000000000.0;
llong x = (llong)(a[i] + 0.5);
while (x % 2 == 0) {
a1[i]++;
x /= 2;
}
while (x % 5 == 0) {
a2[i]++;
x /= 5;
}
s[a1[i]][a2[i]]++;
}
llong ans = 0ll;
for (int i = mxW; i >= 0; i--)
for (int j = mxW; j >= 0; j--) {
s[i][j] += s[i + 1][j] + s[i][j + 1] - s[i + 1][j + 1];
}
for (int i = 1; i <= n; i++) {
ans += s[max(0, 18 - a1[i])][max(0, 18 - a2[i])];
}
ans -= s[9][9];
printf("%lld\n", ans / 2ll);
return 0;
} | replace | 27 | 28 | 27 | 28 | 0 | |
p02588 | C++ | Runtime Error | #include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
// マクロ&定数&関数 ================================================
typedef unsigned int uint;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;
typedef vector<string> vstring;
typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<double, double>> vpdouble;
typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vpint> vvpint;
typedef vector<vpll> vvpll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<string>> vvstring;
typedef vector<vector<bool>> vvbool;
typedef vector<vector<vector<ll>>> vvvll;
const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;4近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:8近傍 9:(0,0)を含む
const double PI = 3.14159265358979323846264338327950288;
// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
const ll MOD = 1000000007; // 10^9 + 7
// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
} else
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
} else
return false;
}
//---------------------------------------------------------------
// オーバーフローチェック
//---------------------------------------------------------------
void is_overflow(ll a, ll b) {
if ((a * b) / b != a)
cout << "OVERFLOW!!!!!" << endl;
}
//---------------------------------------------------------------
// 約数列挙
//---------------------------------------------------------------
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
//---------------------------------------------------------------
// N以下のすべての素数を列挙する(エラトステネスの篩)
//---------------------------------------------------------------
vbool searchSosuu(ll N) {
vbool sosuu;
for (ll i = 0; i < N; i++) {
sosuu.emplace_back(true);
}
sosuu[0] = false;
sosuu[1] = false;
for (ll i = 2; i < N; i++) {
if (sosuu[i]) {
for (ll j = 2; i * j < N; j++) {
sosuu[i * j] = false;
}
}
}
return sosuu;
}
//---------------------------------------------------------------
// 素因数分解 O(√N)
//---------------------------------------------------------------
vpll div_prime(ll n) {
vpll prime_factor;
for (ll i = 2; i * i <= n; i++) {
ll count = 0;
while (n % i == 0) {
count++;
n /= i;
}
if (count) {
pair<ll, ll> temp = {i, count};
prime_factor.emplace_back(temp);
}
}
if (n != 1) {
pair<ll, ll> temp = {n, 1};
prime_factor.emplace_back(temp);
}
return prime_factor;
}
//---------------------------------------------------------------
// 素数判定
//---------------------------------------------------------------
bool is_sosuu(ll N) {
if (N < 2)
return false;
else if (N == 2)
return true;
else if (N % 2 == 0)
return false;
for (ll i = 3; i <= sqrt(N); i += 2) {
if (N % i == 0)
return false;
}
return true;
}
//---------------------------------------------------------------
// 最大公約数(ユークリッドの互除法)
//---------------------------------------------------------------
ll gcd(ll a, ll b) {
if (a < b) {
ll tmp = a;
a = b;
b = tmp;
}
ll r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
//---------------------------------------------------------------
// 最小公倍数
//---------------------------------------------------------------
ll lcm(ll a, ll b) {
ll temp = gcd(a, b);
return temp * (a / temp) * (b / temp);
}
//---------------------------------------------------------------
// 階乗
//---------------------------------------------------------------
ll factorial(ll n) {
if (n <= 1) {
return 1;
}
return (n * (factorial(n - 1))) % MOD;
}
//---------------------------------------------------------------
// 高速コンビネーション計算(前処理:O(N) 計算:O(1))
//---------------------------------------------------------------
// テーブルを作る前処理
ll comb_const = 300005;
vll fac(comb_const), finv(comb_const), inv(comb_const);
bool COMineted = false;
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < comb_const; 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;
}
COMineted = true;
}
// 二項係数計算
ll COM(ll n, ll k) {
if (COMineted == false)
COMinit();
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return (fac[n] * (finv[k] * finv[n - k] % MOD)) % MOD;
}
//---------------------------------------------------------------
// 繰り返し2乗法 base^sisuu
//---------------------------------------------------------------
ll RepeatSquaring(ll base, ll sisuu, ll mod = MOD) {
if (sisuu < 0) {
cout << "RepeatSquaring: 指数が負です!" << endl;
return 0;
}
if (sisuu == 0)
return 1;
if (sisuu % 2 == 0) {
ll t = RepeatSquaring(base, sisuu / 2, mod) % mod;
return (t * t) % mod;
}
return (base * RepeatSquaring(base, sisuu - 1, mod)) % mod;
}
//---------------------------------------------------------------
// 高速単発コンビネーション計算(O(logN))
//---------------------------------------------------------------
ll fast_com(ll a, ll b) {
ll bunshi = 1;
ll bunbo = 1;
for (ll i = 1; i <= b; i++) {
bunbo *= i;
bunbo %= MOD;
bunshi *= (a - i + 1);
bunshi %= MOD;
}
ll ret = bunshi * RepeatSquaring(bunbo, MOD - 2, MOD);
ret %= MOD;
while (ret < 0) {
ret += MOD;
}
return ret;
}
//---------------------------------------------------------------
// 整数をビットのリストに変換する ll->vbool
//---------------------------------------------------------------
vbool to_bitlist(ll bit, ll fixed_size = 1) {
if (bit == 0)
return vbool(fixed_size, 0);
vbool list;
while (bit > 0) {
list.emplace_back(bit & 1);
bit /= 2;
}
while (list.size() < fixed_size) {
list.emplace_back(0);
}
return list;
}
//---------------------------------------------------------------
// 座標圧縮(O(NlogN)) 0スタートであることに注意!
//---------------------------------------------------------------
class PosPress {
/*
配列Pを座圧→instance = PosPress(P);
座圧済み配列→instance.arr
fooの圧縮後→instance.func[foo]
fooの圧縮前→instance.rev[foo]
*/
public:
vll arr;
map<ll, ll> func;
map<ll, ll> rev;
PosPress(vll P) {
arr = P;
sort(P.begin(), P.end());
func[P[0]] = 0;
rev[0] = P[0];
ll next = 1;
for (int i = 1; i < P.size(); i++) {
if (P[i] != P[i - 1]) {
func[P[i]] = next;
rev[next] = P[i];
next++;
}
}
for (int i = 0; i < P.size(); i++) {
arr[i] = func[arr[i]];
}
}
};
//---------------------------------------------------------------
// 行列累乗(O(N^3))
//---------------------------------------------------------------
vvll mat_cross(vvll A, vvll B) {
ll N = A.size();
ll K = B.size();
ll M = B[0].size();
vvll C(N, vll(M));
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
for (int k = 0; k < K; k++)
C[i][j] += A[i][k] * B[k][j];
return C;
}
//---------------------------------------------------------------
// 2直線の交差判定(直線(x1, y1)->(x2, y2) と 直線(X1, Y1)->(X2, Y2))
//---------------------------------------------------------------
bool is_cross(ll x1, ll y1, ll x2, ll y2, ll X1, ll Y1, ll X2, ll Y2) {
ll dx_ai = X1 - x1;
ll dy_ai = Y1 - y1;
ll dx_bi = X1 - x2;
ll dy_bi = Y1 - y2;
ll dx_ai2 = X2 - x1;
ll dy_ai2 = Y2 - y1;
ll dx_bi2 = X2 - x2;
ll dy_bi2 = Y2 - y2;
ll si = dx_ai * dy_bi - dy_ai * dx_bi;
ll si2 = dx_ai2 * dy_bi2 - dy_ai2 * dx_bi2;
ll si3 = dx_ai * dy_ai2 - dy_ai * dx_ai2;
ll si4 = dx_bi * dy_bi2 - dy_bi * dx_bi2;
return (si * si2 < 0 && si3 * si4 < 0);
}
//---------------------------------------------------------------
// 最長増加部分列の長さ(O(NlogN))
//---------------------------------------------------------------
ll LSI(vll vec) {
ll size = vec.size();
vll lsi(size + 1); // 長さjを作った時の右端の最小値
for (ll i = 0; i <= size; i++) {
lsi[i] = LLINF;
}
lsi[0] = 0;
lsi[1] = vec[0];
for (ll i = 1; i < size; i++) {
// 初めてvec[i]の方が小さくなるところを探す
auto Iter = lower_bound(lsi.begin(), lsi.end(), vec[i]);
ll idx = Iter - lsi.begin();
if (idx > 0 && lsi[idx - 1] < vec[i]) {
lsi[idx] = vec[i];
}
}
for (ll i = size; i >= 0; i--) {
if (lsi[i] < LLINF) {
return i;
}
}
}
//---------------------------------------------------------------
// LCS(最長共通部分列) O(|S||T|)
//---------------------------------------------------------------
string LCS(string S, string T) {
vvll dp(S.length() + 1);
for (int i = 0; i < dp.size(); i++) {
vll t(T.length() + 1, 0);
dp[i] = t;
}
for (int i = 0; i < S.length(); i++) {
for (int j = 0; j < T.length(); j++) {
dp[i + 1][j + 1] = max({dp[i][j] + ll(S[i] == T[j]), dp[i + 1][j],
dp[i][j + 1], dp[i + 1][j + 1]});
}
}
ll len = dp[S.length()][T.length()];
string ans = "";
ll i = dp.size() - 1;
ll j = dp[0].size() - 1;
while (len > 0) {
if (dp[i - 1][j] == len) {
i--;
} else if (dp[i][j - 1] == len) {
j--;
} else {
ans += S[i - 1];
i--;
j--;
len--;
}
}
reverse(ans.begin(), ans.end());
return ans;
}
//---------------------------------------------------------------
// 木の根からの深さ
//---------------------------------------------------------------
vll tree_depth(vvll edge, ll start_node) {
ll n_node = edge.size();
vll dist(n_node, LLINF);
dist[start_node] = 0;
stack<pll> S;
S.push({start_node, 0});
while (!S.empty()) {
ll node = S.top().first;
ll d = S.top().second;
dist[node] = d;
S.pop();
for (int i = 0; i < edge[node].size(); i++) {
if (dist[edge[node][i]] == LLINF) {
S.push({edge[node][i], d + 1});
}
}
}
return dist;
}
//---------------------------------------------------------------
// ワーシャルフロイド法(O(N^3)) d: 正方行列(N*N)
//---------------------------------------------------------------
vvll warshall_floyd(vvll d) {
ll n = d.size();
for (int k = 0; k < n; k++) { // 経由する頂点
for (int i = 0; i < n; i++) { // 始点
for (int j = 0; j < n; j++) { // 終点
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
return d;
}
//---------------------------------------------------------------
// Union Find
//---------------------------------------------------------------
class UnionFind {
public:
/*
UnionFind uf(要素の個数);
for(int i = 0;i < 関係の個数; i++)
{
uf.merge(A[i], B[i]);
}
nを含む集合の大きさ = uf.size(n)
nを含む集合の代表者 = uf.root(n)
集合の個数 = uf.n_group
*/
vector<ll> par; // 各元の親を表す配列
vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)
ll n_group; // 集合の数
// Constructor
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
n_group = sz_;
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
n_group--;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
};
//---------------------------------------------------------------
// ダイクストラ(O(ElogV)) edge: 隣接リスト from->pair{to, cost}
//---------------------------------------------------------------
class Dijkstra {
public:
vll dist; // iまでの最短距離
vll past; // (iまで最短で行くときの一つ前)
// edge[i] -> {to, cost}
Dijkstra(vvpll edge, ll start)
: dist(edge.size(), LLINF), past(edge.size(), -1) {
using Pi = pll;
priority_queue<Pi, vector<Pi>, greater<Pi>> que;
dist[start] = 0;
que.emplace(dist[start], start);
while (!que.empty()) {
ll cost;
int idx;
tie(cost, idx) = que.top();
que.pop();
if (dist[idx] < cost)
continue;
for (int i = 0; i < edge[idx].size(); i++) {
ll next_cost = cost + edge[idx][i].second;
if (dist[edge[idx][i].first] <= next_cost)
continue;
dist[edge[idx][i].first] = next_cost;
past[edge[idx][i].first] = idx;
que.emplace(dist[edge[idx][i].first], edge[idx][i].first);
}
}
}
// goalまでの最短経路
vll shortest(ll goal) {
vll ret;
while (goal != -1) {
ret.emplace_back(goal);
goal = past[goal];
}
reverse(ret.begin(), ret.end());
return ret;
}
};
//---------------------------------------------------------------
// LCA: 最近共通祖先 (O())
//---------------------------------------------------------------
class Lca {
public:
vll depth; // 根0からiまでの最短距離
vvll doubling;
ll n_bit = 30;
// edge[i] -> {to, cost}
Lca(vvll edge) : doubling(50, vll(edge.size(), -1)) {
// 深さ
depth = tree_depth(edge, 0);
// ダブリングで親を辿れるようにする
// jから2^i回親を辿ったノード
doubling[0][0] = -1;
for (int i = 1; i < edge.size(); i++)
for (int j : edge[i])
if (depth[i] > depth[j]) {
doubling[0][i] = j;
break;
}
for (int i = 1; i < n_bit; i++)
for (int j = 0; j < edge.size(); j++) {
if (doubling[i - 1][j] != -1)
doubling[i][j] = doubling[i - 1][doubling[i - 1][j]];
else
doubling[i][j] = -1;
}
}
// aとbの最近共通祖先
ll get_lca(ll a, ll b) {
// depth[a] >= depth[b]にする
if (depth[a] < depth[b])
swap(a, b);
ll oa = a;
ll ob = b;
ll d = depth[a] - depth[b];
// aをdだけさかのぼる。
vbool bit = to_bitlist(d, n_bit);
for (int i = 0; i < n_bit; i++)
if (bit[i])
a = doubling[i][a];
// depth[a] == depth[b]になっている。
for (int i = n_bit - 1; i >= 0; i--) {
if (doubling[i][a] == doubling[i][b])
continue;
a = doubling[i][a];
b = doubling[i][b];
}
return a == b ? a : doubling[0][a];
}
};
//---------------------------------------------------------------
// Z-algorithm(SとS[i:]の最長接頭辞長) O(|S|)
//---------------------------------------------------------------
vll Zalgorithm(string S) {
ll n = S.size();
vll Z(n, 0);
ll start = -1;
ll last = -1;
for (ll i = 1; i < n; i++) {
if (start >= 0) {
Z[i] = min(Z[i - start], last - i);
chmax(Z[i], 0LL);
}
while (i + Z[i] < S.size() && S[Z[i]] == S[i + Z[i]])
Z[i]++;
if (last < i + Z[i]) {
last = i + Z[i];
start = i;
}
}
Z[0] = n;
return Z;
}
//---------------------------------------------------------------
// セグメント木
//---------------------------------------------------------------
class SegmentTree {
public:
const ll SEG_LEN = 1 << 19;
vector<set<ll>> seg;
ll s;
SegmentTree(vll A) : seg(SEG_LEN * 2, set<ll>({})) {
s = A.size();
for (int i = 0; i < A.size(); i++) {
seg[i + SEG_LEN] = {A[i]};
}
for (int i = 0; i < A.size(); i++) {
ll pos = i + SEG_LEN;
while (true) {
pos /= 2;
if (pos <= 0)
break;
if (seg[pos * 2 + 1].size() < seg[pos * 2].size()) {
seg[pos] = seg[pos * 2];
for (auto itr = seg[pos * 2 + 1].begin();
itr != seg[pos * 2 + 1].end(); ++itr) {
seg[pos].insert(*itr);
}
} else {
seg[pos] = seg[pos * 2 + 1];
for (auto itr = seg[pos * 2].begin(); itr != seg[pos * 2].end();
++itr) {
seg[pos].insert(*itr);
}
}
}
}
}
ll seg_max(ll left, ll right) {
left += SEG_LEN;
right += SEG_LEN + 1;
set<ll> ret = {};
vbool used(s + 1, false);
while (left < right) {
while (left % 2 == 1) {
if (seg[left].size()) {
for (auto itr = seg[left].begin(); itr != seg[left].end(); ++itr) {
if (!used[*itr]) {
ret.insert(*itr);
used[*itr] = true;
}
}
}
left++;
}
while (right % 2 == 1) {
if (seg[right - 1].size()) {
for (auto itr = seg[right - 1].begin(); itr != seg[right - 1].end();
++itr) {
if (!used[*itr]) {
ret.insert(*itr);
used[*itr] = true;
}
}
}
right--;
}
left /= 2;
right /= 2;
}
return ret.size();
}
};
//========================================================================
//========================================================================
//========================================================================
//========================================================================
double H(double x, double y) { return -x * y * log2(y); }
int main() {
//////==================================
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(25);
//////==================================
/*
~思いついたことはとりあえず絶対メモする!!~
*/
ll N;
cin >> N;
vll count2(N, 0);
vll count5(N, 0);
for (int i = 0; i < N; i++) {
string S;
cin >> S;
ll integer;
ll small_len = -LLINF; // 小数部分の長さ
for (int j = 0; j < S.length(); j++) {
if (S[j] == '.')
small_len = -1;
small_len++;
}
ll beki = 9 - small_len;
if (beki <= 9) {
integer = stoi(S.substr(0, S.length() - small_len - 1)) * pow(10, 9);
integer += stoi(S.substr(S.length() - small_len, small_len)) *
pow(10, 9 - small_len);
} else {
integer = stoi(S) * pow(10, 9);
}
while (integer % 2 == 0) {
integer /= 2;
count2[i]++;
}
while (integer % 5 == 0) {
integer /= 5;
count5[i]++;
}
}
ll M = 70;
vvll accum(M, vll(M, 0)); // 2^i, 2^j
for (int i = 0; i < N; i++) {
accum[count2[i]][count5[i]]++;
}
ll ans = 0;
for (int i = 0; i < N; i++) {
ll need2 = 18 - count2[i];
ll need5 = 18 - count5[i];
for (int t = need2; t < M; t++) {
for (int f = need5; f < M; f++) {
ans += accum[t][f];
}
}
if (count2[i] >= need2 && count5[i] >= need5)
ans--;
}
cout << ans / 2 << endl;
} | #include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
// マクロ&定数&関数 ================================================
typedef unsigned int uint;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;
typedef vector<string> vstring;
typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<double, double>> vpdouble;
typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vpint> vvpint;
typedef vector<vpll> vvpll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<string>> vvstring;
typedef vector<vector<bool>> vvbool;
typedef vector<vector<vector<ll>>> vvvll;
const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;4近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:8近傍 9:(0,0)を含む
const double PI = 3.14159265358979323846264338327950288;
// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
const ll MOD = 1000000007; // 10^9 + 7
// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
} else
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
} else
return false;
}
//---------------------------------------------------------------
// オーバーフローチェック
//---------------------------------------------------------------
void is_overflow(ll a, ll b) {
if ((a * b) / b != a)
cout << "OVERFLOW!!!!!" << endl;
}
//---------------------------------------------------------------
// 約数列挙
//---------------------------------------------------------------
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
//---------------------------------------------------------------
// N以下のすべての素数を列挙する(エラトステネスの篩)
//---------------------------------------------------------------
vbool searchSosuu(ll N) {
vbool sosuu;
for (ll i = 0; i < N; i++) {
sosuu.emplace_back(true);
}
sosuu[0] = false;
sosuu[1] = false;
for (ll i = 2; i < N; i++) {
if (sosuu[i]) {
for (ll j = 2; i * j < N; j++) {
sosuu[i * j] = false;
}
}
}
return sosuu;
}
//---------------------------------------------------------------
// 素因数分解 O(√N)
//---------------------------------------------------------------
vpll div_prime(ll n) {
vpll prime_factor;
for (ll i = 2; i * i <= n; i++) {
ll count = 0;
while (n % i == 0) {
count++;
n /= i;
}
if (count) {
pair<ll, ll> temp = {i, count};
prime_factor.emplace_back(temp);
}
}
if (n != 1) {
pair<ll, ll> temp = {n, 1};
prime_factor.emplace_back(temp);
}
return prime_factor;
}
//---------------------------------------------------------------
// 素数判定
//---------------------------------------------------------------
bool is_sosuu(ll N) {
if (N < 2)
return false;
else if (N == 2)
return true;
else if (N % 2 == 0)
return false;
for (ll i = 3; i <= sqrt(N); i += 2) {
if (N % i == 0)
return false;
}
return true;
}
//---------------------------------------------------------------
// 最大公約数(ユークリッドの互除法)
//---------------------------------------------------------------
ll gcd(ll a, ll b) {
if (a < b) {
ll tmp = a;
a = b;
b = tmp;
}
ll r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
//---------------------------------------------------------------
// 最小公倍数
//---------------------------------------------------------------
ll lcm(ll a, ll b) {
ll temp = gcd(a, b);
return temp * (a / temp) * (b / temp);
}
//---------------------------------------------------------------
// 階乗
//---------------------------------------------------------------
ll factorial(ll n) {
if (n <= 1) {
return 1;
}
return (n * (factorial(n - 1))) % MOD;
}
//---------------------------------------------------------------
// 高速コンビネーション計算(前処理:O(N) 計算:O(1))
//---------------------------------------------------------------
// テーブルを作る前処理
ll comb_const = 300005;
vll fac(comb_const), finv(comb_const), inv(comb_const);
bool COMineted = false;
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < comb_const; 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;
}
COMineted = true;
}
// 二項係数計算
ll COM(ll n, ll k) {
if (COMineted == false)
COMinit();
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return (fac[n] * (finv[k] * finv[n - k] % MOD)) % MOD;
}
//---------------------------------------------------------------
// 繰り返し2乗法 base^sisuu
//---------------------------------------------------------------
ll RepeatSquaring(ll base, ll sisuu, ll mod = MOD) {
if (sisuu < 0) {
cout << "RepeatSquaring: 指数が負です!" << endl;
return 0;
}
if (sisuu == 0)
return 1;
if (sisuu % 2 == 0) {
ll t = RepeatSquaring(base, sisuu / 2, mod) % mod;
return (t * t) % mod;
}
return (base * RepeatSquaring(base, sisuu - 1, mod)) % mod;
}
//---------------------------------------------------------------
// 高速単発コンビネーション計算(O(logN))
//---------------------------------------------------------------
ll fast_com(ll a, ll b) {
ll bunshi = 1;
ll bunbo = 1;
for (ll i = 1; i <= b; i++) {
bunbo *= i;
bunbo %= MOD;
bunshi *= (a - i + 1);
bunshi %= MOD;
}
ll ret = bunshi * RepeatSquaring(bunbo, MOD - 2, MOD);
ret %= MOD;
while (ret < 0) {
ret += MOD;
}
return ret;
}
//---------------------------------------------------------------
// 整数をビットのリストに変換する ll->vbool
//---------------------------------------------------------------
vbool to_bitlist(ll bit, ll fixed_size = 1) {
if (bit == 0)
return vbool(fixed_size, 0);
vbool list;
while (bit > 0) {
list.emplace_back(bit & 1);
bit /= 2;
}
while (list.size() < fixed_size) {
list.emplace_back(0);
}
return list;
}
//---------------------------------------------------------------
// 座標圧縮(O(NlogN)) 0スタートであることに注意!
//---------------------------------------------------------------
class PosPress {
/*
配列Pを座圧→instance = PosPress(P);
座圧済み配列→instance.arr
fooの圧縮後→instance.func[foo]
fooの圧縮前→instance.rev[foo]
*/
public:
vll arr;
map<ll, ll> func;
map<ll, ll> rev;
PosPress(vll P) {
arr = P;
sort(P.begin(), P.end());
func[P[0]] = 0;
rev[0] = P[0];
ll next = 1;
for (int i = 1; i < P.size(); i++) {
if (P[i] != P[i - 1]) {
func[P[i]] = next;
rev[next] = P[i];
next++;
}
}
for (int i = 0; i < P.size(); i++) {
arr[i] = func[arr[i]];
}
}
};
//---------------------------------------------------------------
// 行列累乗(O(N^3))
//---------------------------------------------------------------
vvll mat_cross(vvll A, vvll B) {
ll N = A.size();
ll K = B.size();
ll M = B[0].size();
vvll C(N, vll(M));
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
for (int k = 0; k < K; k++)
C[i][j] += A[i][k] * B[k][j];
return C;
}
//---------------------------------------------------------------
// 2直線の交差判定(直線(x1, y1)->(x2, y2) と 直線(X1, Y1)->(X2, Y2))
//---------------------------------------------------------------
bool is_cross(ll x1, ll y1, ll x2, ll y2, ll X1, ll Y1, ll X2, ll Y2) {
ll dx_ai = X1 - x1;
ll dy_ai = Y1 - y1;
ll dx_bi = X1 - x2;
ll dy_bi = Y1 - y2;
ll dx_ai2 = X2 - x1;
ll dy_ai2 = Y2 - y1;
ll dx_bi2 = X2 - x2;
ll dy_bi2 = Y2 - y2;
ll si = dx_ai * dy_bi - dy_ai * dx_bi;
ll si2 = dx_ai2 * dy_bi2 - dy_ai2 * dx_bi2;
ll si3 = dx_ai * dy_ai2 - dy_ai * dx_ai2;
ll si4 = dx_bi * dy_bi2 - dy_bi * dx_bi2;
return (si * si2 < 0 && si3 * si4 < 0);
}
//---------------------------------------------------------------
// 最長増加部分列の長さ(O(NlogN))
//---------------------------------------------------------------
ll LSI(vll vec) {
ll size = vec.size();
vll lsi(size + 1); // 長さjを作った時の右端の最小値
for (ll i = 0; i <= size; i++) {
lsi[i] = LLINF;
}
lsi[0] = 0;
lsi[1] = vec[0];
for (ll i = 1; i < size; i++) {
// 初めてvec[i]の方が小さくなるところを探す
auto Iter = lower_bound(lsi.begin(), lsi.end(), vec[i]);
ll idx = Iter - lsi.begin();
if (idx > 0 && lsi[idx - 1] < vec[i]) {
lsi[idx] = vec[i];
}
}
for (ll i = size; i >= 0; i--) {
if (lsi[i] < LLINF) {
return i;
}
}
}
//---------------------------------------------------------------
// LCS(最長共通部分列) O(|S||T|)
//---------------------------------------------------------------
string LCS(string S, string T) {
vvll dp(S.length() + 1);
for (int i = 0; i < dp.size(); i++) {
vll t(T.length() + 1, 0);
dp[i] = t;
}
for (int i = 0; i < S.length(); i++) {
for (int j = 0; j < T.length(); j++) {
dp[i + 1][j + 1] = max({dp[i][j] + ll(S[i] == T[j]), dp[i + 1][j],
dp[i][j + 1], dp[i + 1][j + 1]});
}
}
ll len = dp[S.length()][T.length()];
string ans = "";
ll i = dp.size() - 1;
ll j = dp[0].size() - 1;
while (len > 0) {
if (dp[i - 1][j] == len) {
i--;
} else if (dp[i][j - 1] == len) {
j--;
} else {
ans += S[i - 1];
i--;
j--;
len--;
}
}
reverse(ans.begin(), ans.end());
return ans;
}
//---------------------------------------------------------------
// 木の根からの深さ
//---------------------------------------------------------------
vll tree_depth(vvll edge, ll start_node) {
ll n_node = edge.size();
vll dist(n_node, LLINF);
dist[start_node] = 0;
stack<pll> S;
S.push({start_node, 0});
while (!S.empty()) {
ll node = S.top().first;
ll d = S.top().second;
dist[node] = d;
S.pop();
for (int i = 0; i < edge[node].size(); i++) {
if (dist[edge[node][i]] == LLINF) {
S.push({edge[node][i], d + 1});
}
}
}
return dist;
}
//---------------------------------------------------------------
// ワーシャルフロイド法(O(N^3)) d: 正方行列(N*N)
//---------------------------------------------------------------
vvll warshall_floyd(vvll d) {
ll n = d.size();
for (int k = 0; k < n; k++) { // 経由する頂点
for (int i = 0; i < n; i++) { // 始点
for (int j = 0; j < n; j++) { // 終点
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
return d;
}
//---------------------------------------------------------------
// Union Find
//---------------------------------------------------------------
class UnionFind {
public:
/*
UnionFind uf(要素の個数);
for(int i = 0;i < 関係の個数; i++)
{
uf.merge(A[i], B[i]);
}
nを含む集合の大きさ = uf.size(n)
nを含む集合の代表者 = uf.root(n)
集合の個数 = uf.n_group
*/
vector<ll> par; // 各元の親を表す配列
vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)
ll n_group; // 集合の数
// Constructor
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
n_group = sz_;
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
n_group--;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
};
//---------------------------------------------------------------
// ダイクストラ(O(ElogV)) edge: 隣接リスト from->pair{to, cost}
//---------------------------------------------------------------
class Dijkstra {
public:
vll dist; // iまでの最短距離
vll past; // (iまで最短で行くときの一つ前)
// edge[i] -> {to, cost}
Dijkstra(vvpll edge, ll start)
: dist(edge.size(), LLINF), past(edge.size(), -1) {
using Pi = pll;
priority_queue<Pi, vector<Pi>, greater<Pi>> que;
dist[start] = 0;
que.emplace(dist[start], start);
while (!que.empty()) {
ll cost;
int idx;
tie(cost, idx) = que.top();
que.pop();
if (dist[idx] < cost)
continue;
for (int i = 0; i < edge[idx].size(); i++) {
ll next_cost = cost + edge[idx][i].second;
if (dist[edge[idx][i].first] <= next_cost)
continue;
dist[edge[idx][i].first] = next_cost;
past[edge[idx][i].first] = idx;
que.emplace(dist[edge[idx][i].first], edge[idx][i].first);
}
}
}
// goalまでの最短経路
vll shortest(ll goal) {
vll ret;
while (goal != -1) {
ret.emplace_back(goal);
goal = past[goal];
}
reverse(ret.begin(), ret.end());
return ret;
}
};
//---------------------------------------------------------------
// LCA: 最近共通祖先 (O())
//---------------------------------------------------------------
class Lca {
public:
vll depth; // 根0からiまでの最短距離
vvll doubling;
ll n_bit = 30;
// edge[i] -> {to, cost}
Lca(vvll edge) : doubling(50, vll(edge.size(), -1)) {
// 深さ
depth = tree_depth(edge, 0);
// ダブリングで親を辿れるようにする
// jから2^i回親を辿ったノード
doubling[0][0] = -1;
for (int i = 1; i < edge.size(); i++)
for (int j : edge[i])
if (depth[i] > depth[j]) {
doubling[0][i] = j;
break;
}
for (int i = 1; i < n_bit; i++)
for (int j = 0; j < edge.size(); j++) {
if (doubling[i - 1][j] != -1)
doubling[i][j] = doubling[i - 1][doubling[i - 1][j]];
else
doubling[i][j] = -1;
}
}
// aとbの最近共通祖先
ll get_lca(ll a, ll b) {
// depth[a] >= depth[b]にする
if (depth[a] < depth[b])
swap(a, b);
ll oa = a;
ll ob = b;
ll d = depth[a] - depth[b];
// aをdだけさかのぼる。
vbool bit = to_bitlist(d, n_bit);
for (int i = 0; i < n_bit; i++)
if (bit[i])
a = doubling[i][a];
// depth[a] == depth[b]になっている。
for (int i = n_bit - 1; i >= 0; i--) {
if (doubling[i][a] == doubling[i][b])
continue;
a = doubling[i][a];
b = doubling[i][b];
}
return a == b ? a : doubling[0][a];
}
};
//---------------------------------------------------------------
// Z-algorithm(SとS[i:]の最長接頭辞長) O(|S|)
//---------------------------------------------------------------
vll Zalgorithm(string S) {
ll n = S.size();
vll Z(n, 0);
ll start = -1;
ll last = -1;
for (ll i = 1; i < n; i++) {
if (start >= 0) {
Z[i] = min(Z[i - start], last - i);
chmax(Z[i], 0LL);
}
while (i + Z[i] < S.size() && S[Z[i]] == S[i + Z[i]])
Z[i]++;
if (last < i + Z[i]) {
last = i + Z[i];
start = i;
}
}
Z[0] = n;
return Z;
}
//---------------------------------------------------------------
// セグメント木
//---------------------------------------------------------------
class SegmentTree {
public:
const ll SEG_LEN = 1 << 19;
vector<set<ll>> seg;
ll s;
SegmentTree(vll A) : seg(SEG_LEN * 2, set<ll>({})) {
s = A.size();
for (int i = 0; i < A.size(); i++) {
seg[i + SEG_LEN] = {A[i]};
}
for (int i = 0; i < A.size(); i++) {
ll pos = i + SEG_LEN;
while (true) {
pos /= 2;
if (pos <= 0)
break;
if (seg[pos * 2 + 1].size() < seg[pos * 2].size()) {
seg[pos] = seg[pos * 2];
for (auto itr = seg[pos * 2 + 1].begin();
itr != seg[pos * 2 + 1].end(); ++itr) {
seg[pos].insert(*itr);
}
} else {
seg[pos] = seg[pos * 2 + 1];
for (auto itr = seg[pos * 2].begin(); itr != seg[pos * 2].end();
++itr) {
seg[pos].insert(*itr);
}
}
}
}
}
ll seg_max(ll left, ll right) {
left += SEG_LEN;
right += SEG_LEN + 1;
set<ll> ret = {};
vbool used(s + 1, false);
while (left < right) {
while (left % 2 == 1) {
if (seg[left].size()) {
for (auto itr = seg[left].begin(); itr != seg[left].end(); ++itr) {
if (!used[*itr]) {
ret.insert(*itr);
used[*itr] = true;
}
}
}
left++;
}
while (right % 2 == 1) {
if (seg[right - 1].size()) {
for (auto itr = seg[right - 1].begin(); itr != seg[right - 1].end();
++itr) {
if (!used[*itr]) {
ret.insert(*itr);
used[*itr] = true;
}
}
}
right--;
}
left /= 2;
right /= 2;
}
return ret.size();
}
};
//========================================================================
//========================================================================
//========================================================================
//========================================================================
double H(double x, double y) { return -x * y * log2(y); }
int main() {
//////==================================
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(25);
//////==================================
/*
~思いついたことはとりあえず絶対メモする!!~
*/
ll N;
cin >> N;
vll count2(N, 0);
vll count5(N, 0);
for (int i = 0; i < N; i++) {
string S;
cin >> S;
ll integer;
ll small_len = -LLINF; // 小数部分の長さ
for (int j = 0; j < S.length(); j++) {
if (S[j] == '.')
small_len = -1;
small_len++;
}
ll beki = 9 - small_len;
if (beki <= 9) {
integer = stoi(S.substr(0, S.length() - small_len - 1)) * pow(10, 9);
integer += stoi(S.substr(S.length() - small_len, small_len)) *
pow(10, 9 - small_len);
} else {
integer = stoi(S) * pow(10, 9);
}
while (integer % 2 == 0) {
integer /= 2;
count2[i]++;
}
while (integer % 5 == 0) {
integer /= 5;
count5[i]++;
}
}
ll M = 70;
vvll accum(M, vll(M, 0)); // 2^i, 2^j
for (int i = 0; i < N; i++) {
accum[count2[i]][count5[i]]++;
}
ll ans = 0;
for (int i = 0; i < N; i++) {
ll need2 = max(18 - count2[i], 0LL);
ll need5 = max(18 - count5[i], 0LL);
for (int t = need2; t < M; t++) {
for (int f = need5; f < M; f++) {
ans += accum[t][f];
}
}
if (count2[i] >= need2 && count5[i] >= need5)
ans--;
}
cout << ans / 2 << endl;
} | replace | 793 | 795 | 793 | 795 | 0 | |
p02588 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#pragma region
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n)-1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)
const double PI = acos(-1.0);
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++)
cin >> v.at(i);
return is;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
cout << '(' << p.first << ", " << p.second << ')';
return os;
}
template <class T>
void print(const vector<T> &v, const string &delimiter = " ") {
rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i);
cout << endl;
}
template <class T>
void print(const vector<vector<T>> &vv, const string &delimiter) {
for (const auto &v : vv)
print(v, delimiter);
}
#pragma endregion
int cnt_decimal_part(const string &s) {
size_t p = s.find('.');
if (p == string::npos)
return 0;
return s.size() - s.find('.') - 1;
}
int cnt_exp(ll x, int p) {
int res = 0;
while (x % p == 0) {
res++;
x /= p;
}
return res;
}
int main() {
int n;
cin >> n;
vector<ll> a(n);
rep(i, n) {
string s;
cin >> s;
int c = cnt_decimal_part(s);
if (0 < c)
s.erase(s.find('.'), 1);
rep(i, 9 - c) s.push_back('0');
a[i] = stoll(s);
}
ll ans = 0;
vector<vector<ll>> cnt(19, vector<ll>(19));
rep(i, n) {
int e2 = cnt_exp(a[i], 2), e5 = cnt_exp(a[i], 5);
repr(x, 18 - e2, 19) repr(y, 18 - e5, 19) { ans += cnt[x][y]; }
cnt[e2][e5]++;
}
cout << ans << endl;
} | #include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#pragma region
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n)-1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)
const double PI = acos(-1.0);
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++)
cin >> v.at(i);
return is;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
cout << '(' << p.first << ", " << p.second << ')';
return os;
}
template <class T>
void print(const vector<T> &v, const string &delimiter = " ") {
rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i);
cout << endl;
}
template <class T>
void print(const vector<vector<T>> &vv, const string &delimiter) {
for (const auto &v : vv)
print(v, delimiter);
}
#pragma endregion
int cnt_decimal_part(const string &s) {
size_t p = s.find('.');
if (p == string::npos)
return 0;
return s.size() - s.find('.') - 1;
}
int cnt_exp(ll x, int p) {
int res = 0;
while (x % p == 0) {
res++;
x /= p;
}
return res;
}
int main() {
int n;
cin >> n;
vector<ll> a(n);
rep(i, n) {
string s;
cin >> s;
int c = cnt_decimal_part(s);
if (0 < c)
s.erase(s.find('.'), 1);
rep(i, 9 - c) s.push_back('0');
a[i] = stoll(s);
}
ll ans = 0;
vector<vector<ll>> cnt(19, vector<ll>(19));
rep(i, n) {
int e2 = min(18, cnt_exp(a[i], 2)), e5 = min(18, cnt_exp(a[i], 5));
repr(x, 18 - e2, 19) repr(y, 18 - e5, 19) { ans += cnt[x][y]; }
cnt[e2][e5]++;
}
cout << ans << endl;
} | replace | 107 | 108 | 107 | 108 | 0 | |
p02588 | C++ | Runtime Error | #include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#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-12;
// int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
int num[19][19] = {};
long long int ans = 0;
for (int i = 0; i < N; i++) {
string s;
cin >> s;
if (s.find('.') == s.npos) {
s.push_back('.');
}
while (s.size() - s.find('.') < 10) {
s.push_back('0');
}
s.erase(s.begin() + s.find('.'));
// cout << s << endl;
M = stoll(s);
while (s.front() == '0')
s.erase(s.begin());
// cout << M << endl;
int a = 0, b = 0;
while (M % 2 == 0) {
M /= 2;
a++;
}
while (M % 5 == 0) {
M /= 5;
b++;
}
for (int j = 0; j <= 18; j++) {
for (int k = 0; k <= 18; k++) {
if (a + j >= 18 && b + k >= 18)
ans += num[j][k];
}
}
num[a][b]++;
}
cout << ans << endl;
} | #include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#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-12;
// int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
int num[19][19] = {};
long long int ans = 0;
for (int i = 0; i < N; i++) {
string s;
cin >> s;
if (s.find('.') == s.npos) {
s.push_back('.');
}
while (s.size() - s.find('.') < 10) {
s.push_back('0');
}
s.erase(s.begin() + s.find('.'));
// cout << s << endl;
M = stoll(s);
while (s.front() == '0')
s.erase(s.begin());
// cout << M << endl;
int a = 0, b = 0;
while (M % 2 == 0) {
M /= 2;
a++;
}
while (M % 5 == 0) {
M /= 5;
b++;
}
a = min(a, 18);
b = min(b, 18);
for (int j = 0; j <= 18; j++) {
for (int k = 0; k <= 18; k++) {
if (a + j >= 18 && b + k >= 18)
ans += num[j][k];
}
}
num[a][b]++;
}
cout << ans << endl;
} | insert | 63 | 63 | 63 | 65 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000; // 10^9
const ll MOD = 1000000007; // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }
int main() {
int N;
cin >> N;
vector<llll> a(N);
string s;
int c, L;
ll t;
for (ll i = 0; i < N; i++) {
cin >> s;
c = 0;
L = s.length();
for (ll j = L - 1; j >= 0; j--) {
if (s[j] == '.') {
c = L - 1 - j;
}
}
a[i].second = c;
a[i].first = 0;
t = 1;
for (ll j = L - 1; j >= 0; j--) {
if (s[j] == '.') {
continue;
} else {
a[i].first += t * (s[j] - '0');
t = t * 10;
}
}
}
vector<iint> b(N);
for (int i = 0; i < N; i++) {
b[i].first = -a[i].second;
b[i].second = -a[i].second;
while (a[i].first % 2 == 0) {
b[i].first++;
a[i].first = a[i].first / 2;
}
while (a[i].first % 5 == 0) {
b[i].second++;
a[i].first = a[i].first / 5;
}
}
vector<vector<int>> ruiseki(70, vector<int>(40, 0));
for (int i = 0; i < N; i++) {
ruiseki[b[i].first + 15][b[i].second + 15]++;
}
for (int i = 0; i < 70; i++) {
for (int j = 1; j < 40; j++) {
ruiseki[i][j] = ruiseki[i][j - 1] + ruiseki[i][j];
}
}
// for (int i = 0; i < N; i++) {
// printf("%d %d\n", b[i].first, b[i].second);
// }
ll ans = 0;
for (int i = 0; i < N; i++) {
for (int j = -b[i].first + 15; j < 70; j++) {
ans += ruiseki[j][39] - ruiseki[j][-b[i].second + 14];
}
}
for (int i = 0; i < N; i++) {
if (b[i].first >= 0 && b[i].second >= 0) {
ans--;
}
}
ans = ans / 2;
printf("%lld\n", ans);
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000; // 10^9
const ll MOD = 1000000007; // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }
int main() {
int N;
cin >> N;
vector<llll> a(N);
string s;
int c, L;
ll t;
for (ll i = 0; i < N; i++) {
cin >> s;
c = 0;
L = s.length();
for (ll j = L - 1; j >= 0; j--) {
if (s[j] == '.') {
c = L - 1 - j;
}
}
a[i].second = c;
a[i].first = 0;
t = 1;
for (ll j = L - 1; j >= 0; j--) {
if (s[j] == '.') {
continue;
} else {
a[i].first += t * (s[j] - '0');
t = t * 10;
}
}
}
vector<iint> b(N);
for (int i = 0; i < N; i++) {
b[i].first = -a[i].second;
b[i].second = -a[i].second;
while (a[i].first % 2 == 0) {
b[i].first++;
a[i].first = a[i].first / 2;
}
while (a[i].first % 5 == 0) {
b[i].second++;
a[i].first = a[i].first / 5;
}
}
vector<vector<int>> ruiseki(70, vector<int>(40, 0));
for (int i = 0; i < N; i++) {
ruiseki[b[i].first + 15][b[i].second + 15]++;
}
for (int i = 0; i < 70; i++) {
for (int j = 1; j < 40; j++) {
ruiseki[i][j] = ruiseki[i][j - 1] + ruiseki[i][j];
}
}
// for (int i = 0; i < N; i++) {
// printf("%d %d\n", b[i].first, b[i].second);
// }
ll ans = 0;
for (int i = 0; i < N; i++) {
for (int j = max(-b[i].first + 15, 0); j < 70; j++) {
if (-b[i].second + 14 < 0) {
ans += ruiseki[j][39];
continue;
}
ans += ruiseki[j][39] - ruiseki[j][-b[i].second + 14];
}
}
for (int i = 0; i < N; i++) {
if (b[i].first >= 0 && b[i].second >= 0) {
ans--;
}
}
ans = ans / 2;
printf("%lld\n", ans);
} | replace | 75 | 76 | 75 | 80 | 0 | |
p02588 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < (N); i++)
int main() {
int n;
scanf("%d", &n);
map<pair<int, int>, int> cnt;
rep(i, n) {
double x;
cin >> x;
long long xl = llround(x * 1e9);
int two = 0, five = 0;
while (xl % 2 == 0) {
two++;
xl /= 2;
}
while (i % 5 == 0) {
five++;
xl /= 5;
}
++cnt[{two, five}];
}
long long answer = 0;
for (auto p1 : cnt) {
for (auto p2 : cnt) {
if (p1.first.first + p2.first.first >= 18 &&
p1.first.second + p2.first.second >= 18) {
if (p1 < p2) {
answer += (long long)p1.second * p2.second;
} else if (p1 == p2) {
answer += (long long)p1.second * (p1.second - 1) / 2;
}
}
}
}
cout << answer << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < (N); i++)
int main() {
int n;
scanf("%d", &n);
map<pair<int, int>, int> cnt;
rep(i, n) {
double x;
cin >> x;
long long xl = llround(x * 1e9);
int two = 0, five = 0;
while (xl % 2 == 0) {
two++;
xl /= 2;
}
while (xl % 5 == 0) {
five++;
xl /= 5;
}
++cnt[{two, five}];
}
long long answer = 0;
for (auto p1 : cnt) {
for (auto p2 : cnt) {
if (p1.first.first + p2.first.first >= 18 &&
p1.first.second + p2.first.second >= 18) {
if (p1 < p2) {
answer += (long long)p1.second * p2.second;
} else if (p1 == p2) {
answer += (long long)p1.second * (p1.second - 1) / 2;
}
}
}
}
cout << answer << endl;
}
| replace | 17 | 18 | 17 | 18 | TLE | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;
namespace Problem {
using namespace std;
class Solver {
public:
int n;
vector<string> a;
vector<vector<int>> cnt, sum;
vector<int> c2, c5;
const int geta = 9;
Solver(LL n)
: n(n), a(n), cnt(100, vector<int>(100)), sum(100, vector<int>(100)),
c2(n), c5(n){};
void solve() {
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
int d = 0;
int tmp = a[i].find('.');
if (tmp != -1) {
d = -(a[i].length() - tmp - 1);
}
LL b = 0;
for (int j = 0; j < a[i].length(); ++j) {
if (a[i][j] != '.') {
b *= 10;
b += a[i][j] - '0';
}
}
int cnt2 = d, cnt5 = d;
while (b > 0 && b % 2 == 0) {
cnt2++;
b /= 2;
}
while (b > 0 && b % 5 == 0) {
cnt5++;
b /= 5;
}
DBG(a[i], cnt2, cnt5)
cnt[cnt2 + geta][cnt5 + geta]++;
c2[i] = cnt2;
c5[i] = cnt5;
}
sum = cnt;
for (int i = (int)sum.size() - 1; i >= 0; --i) {
for (int j = (int)sum[i].size() - 2; j >= 0; --j) {
sum[i][j] += sum[i][j + 1];
}
}
for (int i = (int)sum.size() - 2; i >= 0; --i) {
for (int j = (int)sum[i].size() - 1; j >= 0; --j) {
sum[i][j] += sum[i + 1][j];
}
}
LL ans = 0, same = 0;
for (int i = 0; i < n; ++i) {
DBG(sum[-c2[i] + geta][-c5[i] + geta])
if (c2[i] >= 0 && c5[i] >= 0)
same++;
ans += sum[-c2[i] + geta][-c5[i] + geta];
}
cout << (ans - same) / 2 << endl;
}
}; // namespace Problem
} // namespace Problem
int main() {
std::cin.tie(0);
std::ios_base::sync_with_stdio(false);
// std::cout << std::fixed << std::setprecision(12);
long long n = 0;
std::cin >> n;
Problem::Solver sol(n);
sol.solve();
return 0;
}
| #include <bits/stdc++.h>
#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;
namespace Problem {
using namespace std;
class Solver {
public:
int n;
vector<string> a;
vector<vector<int>> cnt, sum;
vector<int> c2, c5;
const int geta = 9;
Solver(LL n)
: n(n), a(n), cnt(100, vector<int>(100)), sum(100, vector<int>(100)),
c2(n), c5(n){};
void solve() {
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
int d = 0;
int tmp = a[i].find('.');
if (tmp != -1) {
d = -(a[i].length() - tmp - 1);
}
LL b = 0;
for (int j = 0; j < a[i].length(); ++j) {
if (a[i][j] != '.') {
b *= 10;
b += a[i][j] - '0';
}
}
int cnt2 = d, cnt5 = d;
while (b > 0 && b % 2 == 0) {
cnt2++;
b /= 2;
}
while (b > 0 && b % 5 == 0) {
cnt5++;
b /= 5;
}
DBG(a[i], cnt2, cnt5)
cnt[cnt2 + geta][cnt5 + geta]++;
c2[i] = cnt2;
c5[i] = cnt5;
}
sum = cnt;
for (int i = (int)sum.size() - 1; i >= 0; --i) {
for (int j = (int)sum[i].size() - 2; j >= 0; --j) {
sum[i][j] += sum[i][j + 1];
}
}
for (int i = (int)sum.size() - 2; i >= 0; --i) {
for (int j = (int)sum[i].size() - 1; j >= 0; --j) {
sum[i][j] += sum[i + 1][j];
}
}
LL ans = 0, same = 0;
for (int i = 0; i < n; ++i) {
DBG(sum[-c2[i] + geta][-c5[i] + geta])
if (c2[i] >= 0 && c5[i] >= 0)
same++;
ans += sum[max(0, -c2[i] + geta)][max(0, -c5[i] + geta)];
}
cout << (ans - same) / 2 << endl;
}
}; // namespace Problem
} // namespace Problem
int main() {
std::cin.tie(0);
std::ios_base::sync_with_stdio(false);
// std::cout << std::fixed << std::setprecision(12);
long long n = 0;
std::cin >> n;
Problem::Solver sol(n);
sol.solve();
return 0;
}
| replace | 81 | 82 | 81 | 82 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
#define P pair<int, ll>
const ll MOD = 998244353;
const int INF = 1e9;
const double PI = acos(-1);
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n);
rep(i, n) {
string s;
cin >> s;
int m = s.size();
rep(j, m) {
if (s[j] == '.') {
rep(k, 9) {
if (j + 1 + k < m) {
a[i] += ll(s[j + 1 + k] - '0') * pow(10, 8 - k);
}
}
rep(k, j) a[i] += ll(s[j - 1 - k] - '0') * pow(10, 9 + k);
break;
} else if (j == m - 1) {
rep(k, j + 1) a[i] += ll(s[j - k] - '0') * pow(10, 9 + k);
}
}
}
ll md = 1e9;
vector<pair<int, int>> p(n);
vector<vector<ll>> q(50, vector<ll>(50));
rep(i, n) {
ll b = a[i];
int x = 0, y = 0;
ll u = md;
while (b % 2 == 0) {
b /= 2;
++x;
}
while (b % 5 == 0) {
b /= 5;
++y;
}
p[i] = {x, y};
++q[x][y];
}
ll ans = 0;
rep(i, 50) { invrep(j, 49) q[i][j] += q[i][j + 1]; }
rep(j, 50) { invrep(i, 49) q[i][j] += q[i + 1][j]; }
rep(i, n) {
ans += q[18 - p[i].first][18 - p[i].second];
if (9 <= p[i].first && 9 <= p[i].second)
--ans;
}
ans /= 2;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
#define P pair<int, ll>
const ll MOD = 998244353;
const int INF = 1e9;
const double PI = acos(-1);
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n);
rep(i, n) {
string s;
cin >> s;
int m = s.size();
rep(j, m) {
if (s[j] == '.') {
rep(k, 9) {
if (j + 1 + k < m) {
a[i] += ll(s[j + 1 + k] - '0') * pow(10, 8 - k);
}
}
rep(k, j) a[i] += ll(s[j - 1 - k] - '0') * pow(10, 9 + k);
break;
} else if (j == m - 1) {
rep(k, j + 1) a[i] += ll(s[j - k] - '0') * pow(10, 9 + k);
}
}
}
ll md = 1e9;
vector<pair<int, int>> p(n);
vector<vector<ll>> q(50, vector<ll>(50));
rep(i, n) {
ll b = a[i];
int x = 0, y = 0;
ll u = md;
while (b % 2 == 0) {
b /= 2;
++x;
}
while (b % 5 == 0) {
b /= 5;
++y;
}
p[i] = {x, y};
++q[x][y];
}
ll ans = 0;
rep(i, 50) { invrep(j, 49) q[i][j] += q[i][j + 1]; }
rep(j, 50) { invrep(i, 49) q[i][j] += q[i + 1][j]; }
rep(i, n) {
int x = max(0, 18 - p[i].first);
int y = max(0, 18 - p[i].second);
ans += q[x][y];
if (9 <= p[i].first && 9 <= p[i].second)
--ans;
}
ans /= 2;
cout << ans << endl;
return 0;
}
| replace | 60 | 61 | 60 | 63 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#pragma region all
///////////// マクロ, エイリアス /////////////////
#pragma region macros_and_aliases
#define rep(i, n) for (long long i = 0; i < (n); i++)
#define rrep(i, n) for (long long i = (n)-1; i > -1; i--)
#define Rep(i, m, n) for (long long i = (m); i < (n); i++)
#define rRep(i, m, n) for (long long i = (n)-1; i >= (m); i--)
#define REP(i, m, n, p) for (long long i = m; i < n; i += p)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pq priority_queue
#define bcnt(n) __builtin_popcountll(n)
#define endk endl
#define ednl endl
#define enld endl
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqll = priority_queue<long long>;
using pqllg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
using vpll = vector<pair<long long, long long>>;
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T = ll> using VVV = V<V<V<T>>>;
// 昇順pq(小さい方から取り出す)
template <class T = ll> using pqup = priority_queue<T, vector<T>, greater<T>>;
// 降順pq(大きい方から取り出す)
template <class T = ll> using pqdn = priority_queue<T>;
#pragma endregion
///////////// 定数など /////////////////
#pragma region constants
long long const limLL = 9223372036854775807; // POW(2,63)-1 ~ 9.22e18
long long const dekai = 3e16;
const long double pi = acos(-1);
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int ddx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int mod = 1000000007;
// const int mod = 998244353;
#pragma endregion
/////////////////// 基本処理系 ////////////////////
#pragma region basic_procedure
template <class T> inline bool isin(T x, T lef, T rig) {
return ((lef <= x) && (x < rig));
}
template <class T> inline bool isin(T x, T y, T lef, T rig) {
return isin(x, lef, rig) && isin(y, lef, rig);
}
template <class T> inline bool isin(T X, T lefX, T rigX, T Y, T lefY, T rigY) {
return isin(X, lefX, rigX) && isin(Y, lefY, rigY);
}
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;
}
#define debug(var) \
do { \
std::cout << #var << " : "; \
view(var); \
} while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
void Yes() { cout << "Yes\n"; }
void No() { cout << "No\n"; }
void YES() { cout << "YES\n"; }
void NO() { cout << "NO\n"; }
void err() { cout << -1 << endl; }
#pragma endregion
//////////////// 数学処理系 ///////////////////////
#pragma region math
template <class T> T divup(T a, T b) {
// 端数繰りあがり割り算
assert(b != 0);
T x = abs(a);
T y = abs(b);
T z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
long long POW(long long a, long long n) {
// 整数のとき限定の普通のPOW関数
// 標準機能のpow(a,n)は整数だとバグるのでこちらを使う
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
template <class T> int sgn(T x) { // 符号関数
if (x < 0)
return -1;
if (x == 0)
return x;
return 1;
}
long long modpow(long long a, long long n, long long mod) { // a^n mod
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { // a^{-1} mod
// modとaが互いに素のときのみ有効(数学的に逆元が一意に定まるのがそのときのみ)
return modpow(a, mod - 2, mod);
}
vvll comb(100, vll(100, -1));
long long com(long long n, long long k) { // 普通の二項計数(overflowに注意)
assert(n < 100 && k < 100);
if (n < k || k < 0 || n < 0)
return 0;
if (comb[n][k] != -1)
return comb[n][k];
ll res;
if (n - k < k)
res = com(n, n - k);
else if (k == 0)
res = 1;
else
res = com(n - 1, k - 1) + com(n - 1, k);
comb[n][k] = res;
return res;
}
// nCk modを求める
const ll MAX = 510000;
// この値は求める二項計数の値に応じて変える
// MAX=3*10^7のとき1900msほど、ほぼ比例
// MAX=5*10^6程度ならそれほど気にしなくてよい(300ms程)
long long fac[MAX], finv[MAX], inv[MAX];
void cominit() {
// テーブルを作る前処理
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
long long commod(ll n, ll k) { // 二項係数計算
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
long long pmod(ll n, ll k) { // 順列計算
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * finv[n - k] % mod;
}
long long hmod(ll n, ll k) { // nHk計算
// n個の区別しないoを区別するk個の箱に入れる方法の総数
//(n+k-1)C(k-1)と等しい
return commod(n + k - 1, n);
}
#pragma endregion
//////////////// vector処理系 ///////////////////////
#pragma region vector
vector<long long>
vin(long long n) { // 整数n個の入力を受け取ってベクトルに突っ込んで返す
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
// ベクトルの出力(検証済)
// vectorの中身を出力する 答えの出力に利用可能
template <class T> void vout(vector<T> &v, bool tate = 0) {
if (v.size() > 0) {
for (auto it = v.begin(); it < v.end(); it++) {
cout << *it;
if (it != v.end() - 1) {
if (tate)
cout << "\n";
else
cout << " ";
}
}
}
cout << endl;
}
template <class T> void add(vector<T> &v, T val) { // ベクトルの各要素に加算
for (auto &a : v)
a += val;
return;
}
#pragma endregion
//////////////// 累積和処理系 ///////////////////////
#pragma region ruisekiwa
// 1次元累積和
template <class T> vector<T> barsuminit(vector<T> &v) {
ll h = v.size();
V<T> ret(h + 1);
ret[0] = 0;
rep(i, h) ret[i + 1] = v[i] + ret[i];
return ret;
}
template <class T> T barsum(vector<T> &v, ll x0, ll x1) {
if (x1 <= x0)
return 0;
return v[x1] - v[x0];
}
// 2次元累積和
template <class T> vector<vector<T>> sheetsuminit(vector<vector<T>> &v) {
ll h = v.size();
assert(h != 0);
ll w = v.at(0).size();
VV<T> ret(h + 1, V<T>(w + 1));
rep(i, h + 1) ret[i][0] = 0;
rep(i, w + 1) ret[0][i] = 0;
rep(i, h) rep(j, w) ret[i + 1][j + 1] = v[i][j];
rep(i, h) rep(j, w) ret[i + 1][j + 1] += ret[i + 1][j];
rep(i, h) rep(j, w) ret[i + 1][j + 1] += ret[i][j + 1];
return ret;
}
//[x0,y0],(x1,y1)で囲まれた領域内の和
// 端点は(x,y)のみ含む.面積は(x1-x0)*(y1-y0);
template <class T>
T sheetsum(vector<vector<T>> &v, ll x0, ll y0, ll x1, ll y1) {
if (x0 >= x1 || y0 >= y1)
return 0;
return v[x0][y0] + v[x1][y1] - v[x0][y1] - v[x1][y0];
}
// 3次元累積和
template <class T>
vector<vector<vector<T>>> cubiodsuminit(vector<vector<vector<T>>> &v) {
ll h = v.size();
assert(h != 0);
ll w = v[0].size();
assert(w != 0);
ll z = v[0][0].size();
VVV<T> ret(h + 1, VV<T>(w + 1, V<T>(z + 1)));
rep(i, h + 1) rep(j, w + 1) rep(k, z + 1) {
ret[i][j][k] = (i == 0 || j == 0 || k == 0 ? 0 : v[i - 1][j - 1][k - 1]);
}
rep(i, h) rep(j, w) rep(k, z) ret[i + 1][j + 1][k + 1] +=
ret[i + 1][j + 1][k];
rep(i, h) rep(j, w) rep(k, z) ret[i + 1][j + 1][k + 1] +=
ret[i + 1][j][k + 1];
rep(i, h) rep(j, w) rep(k, z) ret[i + 1][j + 1][k + 1] +=
ret[i][j + 1][k + 1];
return ret;
}
//[x0,y0],(x1,y1)で囲まれた領域内の和
// 端点は(x,y)のみ含む.面積は(x1-x0)*(y1-y0);
template <class T>
T cubiodsum(vector<vector<vector<T>>> &v, ll x0, ll y0, ll z0, ll x1, ll y1,
ll z1) {
if (x0 >= x1 || y0 >= y1 || z0 >= z1)
return 0;
return v[x1][y1][z1] - v[x1][y1][z0] - v[x1][y0][z1] - v[x0][y1][z1] +
v[x1][y0][z0] + v[x0][y1][z0] + v[x0][y0][z1] - v[x0][y0][z0];
}
#pragma endregion
////////////// 構造体 ///////////////////////////
#pragma region objects
template <class T = ll> struct BIT {
// もとの配列a[i]はBIT内部ではvec[i+1]に対応
vector<T> vec;
ll n;
ll pw2 = 1; // n以下の最大の2冪
BIT(long long sz) : vec(sz + 1, 0), n(sz) {
while (pw2 * 2 <= n) {
pw2 *= 2;
}
}
// 元の値に足す 更新は不可
void add(long long place, T val, bool index = 0) {
if (!index)
place++;
for (long long x = place; x <= n; x += x & -x)
vec[x] += val;
}
// 端からの合計値計算
T sum0(long long a, bool index = 0) {
if (index)
a--;
T ret = 0;
for (int x = a; x > 0; x -= x & -x)
ret += vec[x];
return ret;
}
// クエリ
T sum(long long a, long long b) { return sum0(b) - sum0(a); }
// 更新 クエリと併用で可能にした
void update(long long place, T val, bool index = 0) {
T old = sum(place, place + 1);
add(place, val - old);
return;
}
ll LowerBound(T val, long long start = 0) {
// startから足していって初めてval以上になる箇所を0-indexedで返す
// 要素が全て非負のときのみ
// [start, ret) >= val を満たすstart以上の最小のret
// どこまで足してもvalに達しないときはn+1を返す
if (val <= 0)
return start;
val += sum0(start);
long long x = 0;
ll p = 1;
for (long long k = pw2; k > 0; k /= 2) {
if (x + k <= n && vec[x + k] < val) {
val -= vec[x + k];
x += k;
}
}
return max(start, x + 1);
}
ll UpperBound(T val, long long start = 0) {
// startから足していって初めてvalより大になる箇所を0-indexedで返す
// 要素が全て非負のときのみ
// [start, ret) > val を満たすstart以上の最小のret
// どこまで足してもvalより大に達しないときはn+1を返す
if (val < 0 /*LBとの変更点*/)
return start;
val += sum0(start);
long long x = 0;
ll p = 1;
for (long long k = pw2; k > 0; k /= 2) {
if (x + k <= n && vec[x + k] <= /*LBとの変更点*/ val) {
val -= vec[x + k];
x += k;
}
}
return max(start, x + 1);
}
};
template <class T = ll> struct edge {
T len;
ll to;
};
template <class T = ll> struct graph { // 0-indexed
vector<vector<edge<T>>> edges;
bool directed, weight;
long long ver;
// constructor
graph(long long vertex, bool direction = 0, bool weigh = 0) : edges(vertex) {
ver = vertex;
directed = direction;
weight = weigh;
edges.resize(vertex);
}
// 辺の追加 (0-indexed)
void update(long long from, long long to, T len = 1, bool direction = 1) {
edge<T> e;
e.to = to;
e.len = len;
edges[from].push_back(e);
if (!direction) {
e.to = from;
edges[to].push_back(e);
}
}
// 入力受取 (1-indexed)
void input(long long edge_num, long long index = 1) {
rep(i, edge_num) {
ll a;
ll b;
cin >> a >> b;
a -= index;
b -= index;
T c;
if (weight)
cin >> c;
else
c = 1;
update(a, b, c, directed);
}
}
// 長さが負のpathがないときの単一始点最短経路<vll> O((ver)log(ver)+(edge))
vector<T> dijkstra(long long start) {
vector<T> ret(ver, (T)dekai);
pqup<pair<T, long long>> p; //{dist,place}
p.push({0, start});
ret[start] = 0;
while (!p.empty()) {
T dist = p.top().first;
ll place = p.top().second;
p.pop();
if (ret[place] < dist)
continue;
for (auto &next : edges[place]) {
ll nextplace = next.to;
T dis = next.len;
if (chmin(ret[nextplace], dist + dis)) {
p.push({ret[nextplace], nextplace});
}
}
}
return ret;
}
// 単一始点最短経路 O((ver)*(edge))
// ある頂点までのコストが無限に小さくなり得るとき→ ret[i] = -dekai;
vector<T> BellmanFord(long long start) {
vector<T> ret(ver, dekai);
ret[start] = 0;
rep(loop, ver - 1) {
rep(v, ver) {
for (auto &e : edges[v]) {
chmin(ret[e.to], ret[v] + e.len);
}
}
}
// 無限降下点の検索
queue<ll> q;
vb chk(ver, 0);
rep(v, ver) {
for (auto &e : edges[v]) {
if (chmin(ret[e.to], ret[v] + e.len)) {
if (!chk[e.to]) {
q.push(e.to);
chk[e.to] = 1;
}
}
}
}
while (!q.empty()) {
ll now = q.front();
q.pop();
for (auto &e : edges[now]) {
if (!chk[e.to]) {
chk[e.to] = 1;
q.push(e.to);
}
}
}
rep(i, ver) if (chk[i]) ret[i] = -dekai;
return ret;
}
// 閉路に含まれない頂点列挙
// 要素数がver未満なら閉路が存在、そうでなければ閉路は存在しない
vector<long long> topo_sort() {
assert(directed);
vector<long long> num_input(ver);
// 入次数
for (long long i = 0; i < ver; i++) {
for (auto e : edges[i]) {
num_input[e.to]++;
}
}
// 入次数が0のノードをqueueで管理する
queue<long long> que;
for (int i = 0; i < ver; i++) {
if (num_input[i] == 0) {
que.push(i);
}
}
vector<long long> ans;
while (!que.empty()) {
auto node = que.front();
que.pop();
ans.push_back(node);
// 頂点の削除
for (auto e : edges[node]) {
num_input[e.to]--;
// 行き先の入次数が0になったらqueueに追加
if (num_input[e.to] == 0) {
que.push(e.to);
}
}
}
return ans;
}
pair<pair<long long, long long>, T>
DiameterOfTree() { //{{端点、端点},直径の大きさ}
ll v1, v2; // 0ともっとも遠い点、v1と最も遠い点
V<T> dis(ver, -1);
dis[0] = 0;
queue<ll> q;
q.push(0);
while (!q.empty()) {
ll now = q.front();
q.pop();
for (auto &e : edges[now]) {
if (dis[e.to] != -1)
continue;
dis[e.to] = dis[now] + e.len;
q.push(e.to);
}
if (q.empty())
v1 = now;
}
rep(i, ver) dis[i] = -1;
dis[v1] = 0;
q.push(v1);
while (!q.empty()) {
ll now = q.front();
q.pop();
for (auto &e : edges[now]) {
if (dis[e.to] != -1)
continue;
dis[e.to] = dis[now] + e.len;
q.push(e.to);
}
if (q.empty())
v2 = now;
}
pair<pll, T> ans = {{v1, v2}, dis[v2]};
return ans;
}
// 無向木構造を根から葉に伸びる有向木構造に書き換える
graph<T> RootToLeaf(ll root) { // 0-indexed
graph<T> ret(ver, 1, weight);
vb chk(ver, 0);
chk[root] = 1;
function<void(ll)> dfs = [&](ll now) {
for (auto &e : edges[now]) {
if (chk[e.to] == 1)
continue;
chk[e.to] = 1;
ret.update(now, e.to, e.len, 1);
dfs(e.to);
}
};
dfs(root);
return ret;
}
// 無向木構造を葉から根に伸びる有向木構造に書き換える
graph<T> LeafToRoot(ll root) { // 0-indexed
graph<T> ret(ver, 1, weight);
vb chk(ver, 0);
chk[root] = 1;
function<void(ll)> dfs = [&](ll now) {
for (auto &e : edges[now]) {
if (chk[e.to] == 1)
continue;
chk[e.to] = 1;
ret.update(e.to, now, e.len, 1);
dfs(e.to);
}
};
dfs(root);
ret.update(root, root, 0);
return ret;
}
// LeafToRootのvector版.par[i]=iの親の頂点
vector<long long> par(ll root) { // 0-indexed
vll ret(ver, -1);
ret[root] = root; // rootの親はroot
function<void(ll)> dfs = [&](ll now) {
for (auto &e : edges[now]) {
if (ret[e.to] != -1)
continue;
ret[e.to] = now;
dfs(e.to);
}
};
dfs(root);
return ret;
}
vector<edge<T>> ParentAndDistance(ll root) { // 0-indexed
V<edge<T>> ret(ver);
rep(i, ver) ret[i].to = -1;
ret[root].to = root; // rootの親はroot
ret[root].len = 0; // rootの親との距離は0
function<void(ll)> dfs = [&](ll now) {
for (auto &e : edges[now]) {
if (ret[e.to].to != -1)
continue;
ret[e.to].to = now;
ret[e.to].len = e.len;
dfs(e.to);
}
};
dfs(root);
return ret;
}
// 隣接sheet.主にwarshall用
VV<T> GraphArray(void) {
VV<T> ret(ver, V<ll>(ver, dekai));
rep(from, ver) {
for (auto &e : edges[from]) {
ret[from][e.to] = e.len;
}
ret[from][from] = 0;
}
return ret;
}
};
struct mint {
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
#pragma endregion
////////////////// メモ //////////////////////
#pragma region memo
#pragma region programming_memo
// Ctrl+Shift+[:折りたたみ
// Ctrl+Shift+]:展開
// Ctrl+k Ctrl+l:折り畳み/展開のトグル
// Ctrl+k Ctrl+[:再帰的に折りたたみ
// Ctrl+k Ctrl+]:再帰的に展開
// Ctrl+k Ctrl+0:すべて折りたたみ
// Ctrl+k Ctrl+j:すべて展開
// Ctrl+k Ctrl+1:レベル1で折りたたみ
// Ctrl+k Ctrl+2:レベル2で折りたたみ
// Ctrl+k Ctrl+3:レベル3で折りたたみ
// Ctrl+k Ctrl+8:すべての領域(region)の折りたたみ
// Ctrl+k Ctrl+9:すべての領域(region)の展開
// Ctrl+k Ctrl+/:すべてのブロックコメントの折りたたみ
// long long intとして表示
// (印字対象データの最上位ビットが立っているので負数とみなされる) printf("case3:
// %lld\n", a);
// C++拡張機能の更新で急にエラーの下線が表示されなくなった
// 下の青の線が紫になったり
// →ディレクトリを直すと直った(良く分からん)
// c char 文字
// s char *文字列
// d int, short 10進の整数
// u unsigned int, unsigned short 10進の符号なし整数
// o int, short, unsigned int,unsigned short 8進の整数
// x int, short, unsigned int,unsigned short 16進の整数
// f float 浮動小数点数
// e float 浮動小数点数の指数表示
// g float %eもしくは %fのどちらか最適な形式の浮動小数点数
// ld long 10進の倍精度整数
// lu unsinged long 10進の符号なし倍精度整数
// lo long,unsinged long 8進の倍精度整数
// lf double 倍精度浮動小数点数
// a double 16進の倍精度浮動小数点数
// lld long long
#pragma endregion
#pragma endregion
#pragma endregion
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// cout << fixed << setprecision(15);
ll n;
cin >> n;
vll a2(n, 0), a5(n, 0); // 含まれる2,5の素因数の数
rep(i, n) {
string s;
cin >> s;
string t;
ll m = s.size();
ll pt = m - 1;
// debug(s);
rep(j, m) {
if (s[j] == '.') {
pt = j;
} else {
t += s[j];
}
}
ll k = m - pt - 1; // 10でなんか祝ってるか
ll num = stoll(t);
ll cnt2 = 0, cnt5 = 0;
while (num % 2 == 0) {
num /= 2;
cnt2++;
}
while (num % 5 == 0) {
num /= 5;
cnt5++;
}
a2[i] = cnt2 - k;
a5[i] = cnt5 - k;
}
// debug(a2);
// debug(a5);
ll h = 41, w = 20;
vvll v(h, vll(h, (0LL)));
rep(i, n) { v[a2[i] + w][a5[i] + w]++; }
auto she = sheetsuminit(v);
ll ans = 0;
ll res = 0;
rep(i, h) {
rep(j, h) {
ll x, y;
x = h - 1 - i;
y = h - 1 - j;
ll add = v[i][j] * sheetsum(she, x, y, h, h);
ans += add;
if (add) {
// debug(i - w);
// debug(j - w);
// debug(add);
// debug(ans);
}
if (x <= i && y <= j) {
res += v[i][j];
// debug(v[i][j]);
// debug(res);
}
}
}
// debug(ans);
// debug(res);
ans -= res;
ans /= 2;
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#pragma region all
///////////// マクロ, エイリアス /////////////////
#pragma region macros_and_aliases
#define rep(i, n) for (long long i = 0; i < (n); i++)
#define rrep(i, n) for (long long i = (n)-1; i > -1; i--)
#define Rep(i, m, n) for (long long i = (m); i < (n); i++)
#define rRep(i, m, n) for (long long i = (n)-1; i >= (m); i--)
#define REP(i, m, n, p) for (long long i = m; i < n; i += p)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pq priority_queue
#define bcnt(n) __builtin_popcountll(n)
#define endk endl
#define ednl endl
#define enld endl
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqll = priority_queue<long long>;
using pqllg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
using vpll = vector<pair<long long, long long>>;
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T = ll> using VVV = V<V<V<T>>>;
// 昇順pq(小さい方から取り出す)
template <class T = ll> using pqup = priority_queue<T, vector<T>, greater<T>>;
// 降順pq(大きい方から取り出す)
template <class T = ll> using pqdn = priority_queue<T>;
#pragma endregion
///////////// 定数など /////////////////
#pragma region constants
long long const limLL = 9223372036854775807; // POW(2,63)-1 ~ 9.22e18
long long const dekai = 3e16;
const long double pi = acos(-1);
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int ddx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int mod = 1000000007;
// const int mod = 998244353;
#pragma endregion
/////////////////// 基本処理系 ////////////////////
#pragma region basic_procedure
template <class T> inline bool isin(T x, T lef, T rig) {
return ((lef <= x) && (x < rig));
}
template <class T> inline bool isin(T x, T y, T lef, T rig) {
return isin(x, lef, rig) && isin(y, lef, rig);
}
template <class T> inline bool isin(T X, T lefX, T rigX, T Y, T lefY, T rigY) {
return isin(X, lefX, rigX) && isin(Y, lefY, rigY);
}
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;
}
#define debug(var) \
do { \
std::cout << #var << " : "; \
view(var); \
} while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
void Yes() { cout << "Yes\n"; }
void No() { cout << "No\n"; }
void YES() { cout << "YES\n"; }
void NO() { cout << "NO\n"; }
void err() { cout << -1 << endl; }
#pragma endregion
//////////////// 数学処理系 ///////////////////////
#pragma region math
template <class T> T divup(T a, T b) {
// 端数繰りあがり割り算
assert(b != 0);
T x = abs(a);
T y = abs(b);
T z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
long long POW(long long a, long long n) {
// 整数のとき限定の普通のPOW関数
// 標準機能のpow(a,n)は整数だとバグるのでこちらを使う
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
template <class T> int sgn(T x) { // 符号関数
if (x < 0)
return -1;
if (x == 0)
return x;
return 1;
}
long long modpow(long long a, long long n, long long mod) { // a^n mod
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { // a^{-1} mod
// modとaが互いに素のときのみ有効(数学的に逆元が一意に定まるのがそのときのみ)
return modpow(a, mod - 2, mod);
}
vvll comb(100, vll(100, -1));
long long com(long long n, long long k) { // 普通の二項計数(overflowに注意)
assert(n < 100 && k < 100);
if (n < k || k < 0 || n < 0)
return 0;
if (comb[n][k] != -1)
return comb[n][k];
ll res;
if (n - k < k)
res = com(n, n - k);
else if (k == 0)
res = 1;
else
res = com(n - 1, k - 1) + com(n - 1, k);
comb[n][k] = res;
return res;
}
// nCk modを求める
const ll MAX = 510000;
// この値は求める二項計数の値に応じて変える
// MAX=3*10^7のとき1900msほど、ほぼ比例
// MAX=5*10^6程度ならそれほど気にしなくてよい(300ms程)
long long fac[MAX], finv[MAX], inv[MAX];
void cominit() {
// テーブルを作る前処理
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
long long commod(ll n, ll k) { // 二項係数計算
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
long long pmod(ll n, ll k) { // 順列計算
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * finv[n - k] % mod;
}
long long hmod(ll n, ll k) { // nHk計算
// n個の区別しないoを区別するk個の箱に入れる方法の総数
//(n+k-1)C(k-1)と等しい
return commod(n + k - 1, n);
}
#pragma endregion
//////////////// vector処理系 ///////////////////////
#pragma region vector
vector<long long>
vin(long long n) { // 整数n個の入力を受け取ってベクトルに突っ込んで返す
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
// ベクトルの出力(検証済)
// vectorの中身を出力する 答えの出力に利用可能
template <class T> void vout(vector<T> &v, bool tate = 0) {
if (v.size() > 0) {
for (auto it = v.begin(); it < v.end(); it++) {
cout << *it;
if (it != v.end() - 1) {
if (tate)
cout << "\n";
else
cout << " ";
}
}
}
cout << endl;
}
template <class T> void add(vector<T> &v, T val) { // ベクトルの各要素に加算
for (auto &a : v)
a += val;
return;
}
#pragma endregion
//////////////// 累積和処理系 ///////////////////////
#pragma region ruisekiwa
// 1次元累積和
template <class T> vector<T> barsuminit(vector<T> &v) {
ll h = v.size();
V<T> ret(h + 1);
ret[0] = 0;
rep(i, h) ret[i + 1] = v[i] + ret[i];
return ret;
}
template <class T> T barsum(vector<T> &v, ll x0, ll x1) {
if (x1 <= x0)
return 0;
return v[x1] - v[x0];
}
// 2次元累積和
template <class T> vector<vector<T>> sheetsuminit(vector<vector<T>> &v) {
ll h = v.size();
assert(h != 0);
ll w = v.at(0).size();
VV<T> ret(h + 1, V<T>(w + 1));
rep(i, h + 1) ret[i][0] = 0;
rep(i, w + 1) ret[0][i] = 0;
rep(i, h) rep(j, w) ret[i + 1][j + 1] = v[i][j];
rep(i, h) rep(j, w) ret[i + 1][j + 1] += ret[i + 1][j];
rep(i, h) rep(j, w) ret[i + 1][j + 1] += ret[i][j + 1];
return ret;
}
//[x0,y0],(x1,y1)で囲まれた領域内の和
// 端点は(x,y)のみ含む.面積は(x1-x0)*(y1-y0);
template <class T>
T sheetsum(vector<vector<T>> &v, ll x0, ll y0, ll x1, ll y1) {
if (x0 >= x1 || y0 >= y1)
return 0;
return v[x0][y0] + v[x1][y1] - v[x0][y1] - v[x1][y0];
}
// 3次元累積和
template <class T>
vector<vector<vector<T>>> cubiodsuminit(vector<vector<vector<T>>> &v) {
ll h = v.size();
assert(h != 0);
ll w = v[0].size();
assert(w != 0);
ll z = v[0][0].size();
VVV<T> ret(h + 1, VV<T>(w + 1, V<T>(z + 1)));
rep(i, h + 1) rep(j, w + 1) rep(k, z + 1) {
ret[i][j][k] = (i == 0 || j == 0 || k == 0 ? 0 : v[i - 1][j - 1][k - 1]);
}
rep(i, h) rep(j, w) rep(k, z) ret[i + 1][j + 1][k + 1] +=
ret[i + 1][j + 1][k];
rep(i, h) rep(j, w) rep(k, z) ret[i + 1][j + 1][k + 1] +=
ret[i + 1][j][k + 1];
rep(i, h) rep(j, w) rep(k, z) ret[i + 1][j + 1][k + 1] +=
ret[i][j + 1][k + 1];
return ret;
}
//[x0,y0],(x1,y1)で囲まれた領域内の和
// 端点は(x,y)のみ含む.面積は(x1-x0)*(y1-y0);
template <class T>
T cubiodsum(vector<vector<vector<T>>> &v, ll x0, ll y0, ll z0, ll x1, ll y1,
ll z1) {
if (x0 >= x1 || y0 >= y1 || z0 >= z1)
return 0;
return v[x1][y1][z1] - v[x1][y1][z0] - v[x1][y0][z1] - v[x0][y1][z1] +
v[x1][y0][z0] + v[x0][y1][z0] + v[x0][y0][z1] - v[x0][y0][z0];
}
#pragma endregion
////////////// 構造体 ///////////////////////////
#pragma region objects
template <class T = ll> struct BIT {
// もとの配列a[i]はBIT内部ではvec[i+1]に対応
vector<T> vec;
ll n;
ll pw2 = 1; // n以下の最大の2冪
BIT(long long sz) : vec(sz + 1, 0), n(sz) {
while (pw2 * 2 <= n) {
pw2 *= 2;
}
}
// 元の値に足す 更新は不可
void add(long long place, T val, bool index = 0) {
if (!index)
place++;
for (long long x = place; x <= n; x += x & -x)
vec[x] += val;
}
// 端からの合計値計算
T sum0(long long a, bool index = 0) {
if (index)
a--;
T ret = 0;
for (int x = a; x > 0; x -= x & -x)
ret += vec[x];
return ret;
}
// クエリ
T sum(long long a, long long b) { return sum0(b) - sum0(a); }
// 更新 クエリと併用で可能にした
void update(long long place, T val, bool index = 0) {
T old = sum(place, place + 1);
add(place, val - old);
return;
}
ll LowerBound(T val, long long start = 0) {
// startから足していって初めてval以上になる箇所を0-indexedで返す
// 要素が全て非負のときのみ
// [start, ret) >= val を満たすstart以上の最小のret
// どこまで足してもvalに達しないときはn+1を返す
if (val <= 0)
return start;
val += sum0(start);
long long x = 0;
ll p = 1;
for (long long k = pw2; k > 0; k /= 2) {
if (x + k <= n && vec[x + k] < val) {
val -= vec[x + k];
x += k;
}
}
return max(start, x + 1);
}
ll UpperBound(T val, long long start = 0) {
// startから足していって初めてvalより大になる箇所を0-indexedで返す
// 要素が全て非負のときのみ
// [start, ret) > val を満たすstart以上の最小のret
// どこまで足してもvalより大に達しないときはn+1を返す
if (val < 0 /*LBとの変更点*/)
return start;
val += sum0(start);
long long x = 0;
ll p = 1;
for (long long k = pw2; k > 0; k /= 2) {
if (x + k <= n && vec[x + k] <= /*LBとの変更点*/ val) {
val -= vec[x + k];
x += k;
}
}
return max(start, x + 1);
}
};
template <class T = ll> struct edge {
T len;
ll to;
};
template <class T = ll> struct graph { // 0-indexed
vector<vector<edge<T>>> edges;
bool directed, weight;
long long ver;
// constructor
graph(long long vertex, bool direction = 0, bool weigh = 0) : edges(vertex) {
ver = vertex;
directed = direction;
weight = weigh;
edges.resize(vertex);
}
// 辺の追加 (0-indexed)
void update(long long from, long long to, T len = 1, bool direction = 1) {
edge<T> e;
e.to = to;
e.len = len;
edges[from].push_back(e);
if (!direction) {
e.to = from;
edges[to].push_back(e);
}
}
// 入力受取 (1-indexed)
void input(long long edge_num, long long index = 1) {
rep(i, edge_num) {
ll a;
ll b;
cin >> a >> b;
a -= index;
b -= index;
T c;
if (weight)
cin >> c;
else
c = 1;
update(a, b, c, directed);
}
}
// 長さが負のpathがないときの単一始点最短経路<vll> O((ver)log(ver)+(edge))
vector<T> dijkstra(long long start) {
vector<T> ret(ver, (T)dekai);
pqup<pair<T, long long>> p; //{dist,place}
p.push({0, start});
ret[start] = 0;
while (!p.empty()) {
T dist = p.top().first;
ll place = p.top().second;
p.pop();
if (ret[place] < dist)
continue;
for (auto &next : edges[place]) {
ll nextplace = next.to;
T dis = next.len;
if (chmin(ret[nextplace], dist + dis)) {
p.push({ret[nextplace], nextplace});
}
}
}
return ret;
}
// 単一始点最短経路 O((ver)*(edge))
// ある頂点までのコストが無限に小さくなり得るとき→ ret[i] = -dekai;
vector<T> BellmanFord(long long start) {
vector<T> ret(ver, dekai);
ret[start] = 0;
rep(loop, ver - 1) {
rep(v, ver) {
for (auto &e : edges[v]) {
chmin(ret[e.to], ret[v] + e.len);
}
}
}
// 無限降下点の検索
queue<ll> q;
vb chk(ver, 0);
rep(v, ver) {
for (auto &e : edges[v]) {
if (chmin(ret[e.to], ret[v] + e.len)) {
if (!chk[e.to]) {
q.push(e.to);
chk[e.to] = 1;
}
}
}
}
while (!q.empty()) {
ll now = q.front();
q.pop();
for (auto &e : edges[now]) {
if (!chk[e.to]) {
chk[e.to] = 1;
q.push(e.to);
}
}
}
rep(i, ver) if (chk[i]) ret[i] = -dekai;
return ret;
}
// 閉路に含まれない頂点列挙
// 要素数がver未満なら閉路が存在、そうでなければ閉路は存在しない
vector<long long> topo_sort() {
assert(directed);
vector<long long> num_input(ver);
// 入次数
for (long long i = 0; i < ver; i++) {
for (auto e : edges[i]) {
num_input[e.to]++;
}
}
// 入次数が0のノードをqueueで管理する
queue<long long> que;
for (int i = 0; i < ver; i++) {
if (num_input[i] == 0) {
que.push(i);
}
}
vector<long long> ans;
while (!que.empty()) {
auto node = que.front();
que.pop();
ans.push_back(node);
// 頂点の削除
for (auto e : edges[node]) {
num_input[e.to]--;
// 行き先の入次数が0になったらqueueに追加
if (num_input[e.to] == 0) {
que.push(e.to);
}
}
}
return ans;
}
pair<pair<long long, long long>, T>
DiameterOfTree() { //{{端点、端点},直径の大きさ}
ll v1, v2; // 0ともっとも遠い点、v1と最も遠い点
V<T> dis(ver, -1);
dis[0] = 0;
queue<ll> q;
q.push(0);
while (!q.empty()) {
ll now = q.front();
q.pop();
for (auto &e : edges[now]) {
if (dis[e.to] != -1)
continue;
dis[e.to] = dis[now] + e.len;
q.push(e.to);
}
if (q.empty())
v1 = now;
}
rep(i, ver) dis[i] = -1;
dis[v1] = 0;
q.push(v1);
while (!q.empty()) {
ll now = q.front();
q.pop();
for (auto &e : edges[now]) {
if (dis[e.to] != -1)
continue;
dis[e.to] = dis[now] + e.len;
q.push(e.to);
}
if (q.empty())
v2 = now;
}
pair<pll, T> ans = {{v1, v2}, dis[v2]};
return ans;
}
// 無向木構造を根から葉に伸びる有向木構造に書き換える
graph<T> RootToLeaf(ll root) { // 0-indexed
graph<T> ret(ver, 1, weight);
vb chk(ver, 0);
chk[root] = 1;
function<void(ll)> dfs = [&](ll now) {
for (auto &e : edges[now]) {
if (chk[e.to] == 1)
continue;
chk[e.to] = 1;
ret.update(now, e.to, e.len, 1);
dfs(e.to);
}
};
dfs(root);
return ret;
}
// 無向木構造を葉から根に伸びる有向木構造に書き換える
graph<T> LeafToRoot(ll root) { // 0-indexed
graph<T> ret(ver, 1, weight);
vb chk(ver, 0);
chk[root] = 1;
function<void(ll)> dfs = [&](ll now) {
for (auto &e : edges[now]) {
if (chk[e.to] == 1)
continue;
chk[e.to] = 1;
ret.update(e.to, now, e.len, 1);
dfs(e.to);
}
};
dfs(root);
ret.update(root, root, 0);
return ret;
}
// LeafToRootのvector版.par[i]=iの親の頂点
vector<long long> par(ll root) { // 0-indexed
vll ret(ver, -1);
ret[root] = root; // rootの親はroot
function<void(ll)> dfs = [&](ll now) {
for (auto &e : edges[now]) {
if (ret[e.to] != -1)
continue;
ret[e.to] = now;
dfs(e.to);
}
};
dfs(root);
return ret;
}
vector<edge<T>> ParentAndDistance(ll root) { // 0-indexed
V<edge<T>> ret(ver);
rep(i, ver) ret[i].to = -1;
ret[root].to = root; // rootの親はroot
ret[root].len = 0; // rootの親との距離は0
function<void(ll)> dfs = [&](ll now) {
for (auto &e : edges[now]) {
if (ret[e.to].to != -1)
continue;
ret[e.to].to = now;
ret[e.to].len = e.len;
dfs(e.to);
}
};
dfs(root);
return ret;
}
// 隣接sheet.主にwarshall用
VV<T> GraphArray(void) {
VV<T> ret(ver, V<ll>(ver, dekai));
rep(from, ver) {
for (auto &e : edges[from]) {
ret[from][e.to] = e.len;
}
ret[from][from] = 0;
}
return ret;
}
};
struct mint {
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
#pragma endregion
////////////////// メモ //////////////////////
#pragma region memo
#pragma region programming_memo
// Ctrl+Shift+[:折りたたみ
// Ctrl+Shift+]:展開
// Ctrl+k Ctrl+l:折り畳み/展開のトグル
// Ctrl+k Ctrl+[:再帰的に折りたたみ
// Ctrl+k Ctrl+]:再帰的に展開
// Ctrl+k Ctrl+0:すべて折りたたみ
// Ctrl+k Ctrl+j:すべて展開
// Ctrl+k Ctrl+1:レベル1で折りたたみ
// Ctrl+k Ctrl+2:レベル2で折りたたみ
// Ctrl+k Ctrl+3:レベル3で折りたたみ
// Ctrl+k Ctrl+8:すべての領域(region)の折りたたみ
// Ctrl+k Ctrl+9:すべての領域(region)の展開
// Ctrl+k Ctrl+/:すべてのブロックコメントの折りたたみ
// long long intとして表示
// (印字対象データの最上位ビットが立っているので負数とみなされる) printf("case3:
// %lld\n", a);
// C++拡張機能の更新で急にエラーの下線が表示されなくなった
// 下の青の線が紫になったり
// →ディレクトリを直すと直った(良く分からん)
// c char 文字
// s char *文字列
// d int, short 10進の整数
// u unsigned int, unsigned short 10進の符号なし整数
// o int, short, unsigned int,unsigned short 8進の整数
// x int, short, unsigned int,unsigned short 16進の整数
// f float 浮動小数点数
// e float 浮動小数点数の指数表示
// g float %eもしくは %fのどちらか最適な形式の浮動小数点数
// ld long 10進の倍精度整数
// lu unsinged long 10進の符号なし倍精度整数
// lo long,unsinged long 8進の倍精度整数
// lf double 倍精度浮動小数点数
// a double 16進の倍精度浮動小数点数
// lld long long
#pragma endregion
#pragma endregion
#pragma endregion
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// cout << fixed << setprecision(15);
ll n;
cin >> n;
vll a2(n, 0), a5(n, 0); // 含まれる2,5の素因数の数
rep(i, n) {
string s;
cin >> s;
string t;
ll m = s.size();
ll pt = m - 1;
// debug(s);
rep(j, m) {
if (s[j] == '.') {
pt = j;
} else {
t += s[j];
}
}
ll k = m - pt - 1; // 10でなんか祝ってるか
ll num = stoll(t);
ll cnt2 = 0, cnt5 = 0;
while (num % 2 == 0) {
num /= 2;
cnt2++;
}
while (num % 5 == 0) {
num /= 5;
cnt5++;
}
a2[i] = cnt2 - k;
a5[i] = cnt5 - k;
}
// debug(a2);
// debug(a5);
ll h = 201, w = 100;
vvll v(h, vll(h, (0LL)));
rep(i, n) { v[a2[i] + w][a5[i] + w]++; }
auto she = sheetsuminit(v);
ll ans = 0;
ll res = 0;
rep(i, h) {
rep(j, h) {
ll x, y;
x = h - 1 - i;
y = h - 1 - j;
ll add = v[i][j] * sheetsum(she, x, y, h, h);
ans += add;
if (add) {
// debug(i - w);
// debug(j - w);
// debug(add);
// debug(ans);
}
if (x <= i && y <= j) {
res += v[i][j];
// debug(v[i][j]);
// debug(res);
}
}
}
// debug(ans);
// debug(res);
ans -= res;
ans /= 2;
cout << ans << endl;
}
| replace | 803 | 804 | 803 | 804 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
mt19937 mt(736);
void solve(istream &cin = std::cin, ostream &cout = std::cout) {
int n;
cin >> n;
vector<ll> arr(n);
for (auto &it : arr) {
string str;
cin >> str;
if (str.find('.') == string::npos)
str.push_back('.');
str.resize(str.find('.') + 10, '0');
it = stoi(str.substr(0, str.find('.'))) * (ll)1e9 +
stoi(str.substr(str.find('.') + 1));
}
#ifdef LOCAL
for (auto it : arr)
cout << it << ' ';
cout << endl;
#endif
vector<vector<int>> cnt(19, vector<int>(19));
for (auto it : arr)
if (it != 0) {
int two = 0, five = 0;
while (it % 2 == 0) {
it /= 2;
two++;
}
while (it % 5 == 0) {
it /= 5;
five++;
}
cnt[two][five]++;
} else
cnt.back().back()++;
auto C2 = [](ll q) { return q * (q - 1ll) / 2; };
ll ans = 0;
for (int two = 0; two < cnt.size(); two++)
for (int five = 0; five < cnt.size(); five++)
for (int i = 18 - two; i < cnt.size(); i++)
for (int j = 18 - five; j < cnt.size(); j++) {
if (two == i && five == j)
ans += 2 * C2(cnt[i][j]);
else
ans += cnt[two][five] * (ll)cnt[i][j];
}
cout << ans / 2 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed;
#ifdef LOCAL
auto st = clock();
ifstream fin("../input.txt");
do {
solve(fin);
cout << "===" << endl;
string str;
while (getline(fin, str) && str != string(max(1, (int)str.size()), '='))
;
} while (fin);
cout << setprecision(6) << "clock: " << double(clock() - st) / CLOCKS_PER_SEC
<< endl;
#else
solve();
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
mt19937 mt(736);
void solve(istream &cin = std::cin, ostream &cout = std::cout) {
int n;
cin >> n;
vector<ll> arr(n);
for (auto &it : arr) {
string str;
cin >> str;
if (str.find('.') == string::npos)
str.push_back('.');
str.resize(str.find('.') + 10, '0');
it = stoi(str.substr(0, str.find('.'))) * (ll)1e9 +
stoi(str.substr(str.find('.') + 1));
}
#ifdef LOCAL
for (auto it : arr)
cout << it << ' ';
cout << endl;
#endif
vector<vector<int>> cnt(19, vector<int>(19));
for (auto it : arr)
if (it != 0) {
int two = 0, five = 0;
while (it % 2 == 0) {
it /= 2;
two++;
}
while (it % 5 == 0) {
it /= 5;
five++;
}
cnt[min(two, (int)cnt.size() - 1)][min(five, (int)cnt.size() - 1)]++;
} else
cnt.back().back()++;
auto C2 = [](ll q) { return q * (q - 1ll) / 2; };
ll ans = 0;
for (int two = 0; two < cnt.size(); two++)
for (int five = 0; five < cnt.size(); five++)
for (int i = 18 - two; i < cnt.size(); i++)
for (int j = 18 - five; j < cnt.size(); j++) {
if (two == i && five == j)
ans += 2 * C2(cnt[i][j]);
else
ans += cnt[two][five] * (ll)cnt[i][j];
}
cout << ans / 2 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed;
#ifdef LOCAL
auto st = clock();
ifstream fin("../input.txt");
do {
solve(fin);
cout << "===" << endl;
string str;
while (getline(fin, str) && str != string(max(1, (int)str.size()), '='))
;
} while (fin);
cout << setprecision(6) << "clock: " << double(clock() - st) / CLOCKS_PER_SEC
<< endl;
#else
solve();
#endif
return 0;
}
| replace | 52 | 53 | 52 | 53 | 0 | |
p02588 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
#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 REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
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> inline void prn(vector<T> &v) {
int n = sz(v);
REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
int n = sz(v);
REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
for (auto u : v)
printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
int n = sz(v);
REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
}
map<ll, ll> prime_factorization(ll n) {
map<ll, ll> m;
ll x = n;
ll d = 2;
while (x > 1 && d * d <= n) {
if (x % d == 0) {
m[d]++;
x /= d;
} else {
d++;
}
}
if (x != 1)
m[x]++;
return m;
}
void solve() {
int n;
cin >> n;
double d;
map<pair<int, int>, ll> m;
REP(i, n) {
cin >> d;
auto l = prime_factorization(llround(d * 1000000000));
m[make_pair(l[2], l[5])]++;
}
ll ans = 0;
for (auto e : m) {
for (auto f : m) {
if (e.first.first + f.first.first >= 18 and
e.first.second + f.first.second >= 18) {
if (e.first == f.first) {
ans += e.second * (e.second - 1);
} else {
ans += e.second * f.second;
}
}
}
}
cout << ans / 2 << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
#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 REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
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> inline void prn(vector<T> &v) {
int n = sz(v);
REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
int n = sz(v);
REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
for (auto u : v)
printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
int n = sz(v);
REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
}
map<ll, ll> prime_factorization(ll n) {
map<ll, ll> m;
ll x = n;
ll d = 2;
while (x > 1 && d * d <= n) {
if (x % d == 0) {
m[d]++;
x /= d;
} else {
d++;
}
}
if (x != 1)
m[x]++;
return m;
}
void solve() {
int n;
cin >> n;
double d;
map<pair<int, int>, ll> m;
REP(i, n) {
cin >> d;
// auto l = prime_factorization(llround(d * 1000000000));
auto l = llround(d * 1000000000);
int two = 0;
while (l % 2 == 0) {
two++;
l /= 2;
}
int five = 0;
while (l % 5 == 0) {
five++;
l /= 5;
}
m[make_pair(two, five)]++;
}
ll ans = 0;
for (auto e : m) {
for (auto f : m) {
if (e.first.first + f.first.first >= 18 and
e.first.second + f.first.second >= 18) {
if (e.first == f.first) {
ans += e.second * (e.second - 1);
} else {
ans += e.second * f.second;
}
}
}
}
cout << ans / 2 << endl;
}
| replace | 92 | 94 | 92 | 105 | TLE | |
p02588 | C++ | Runtime Error | // g++ -std=c++14 test.cpp -o test.out
// 問題URL
// https://atcoder.jp/contests/agc047/tasks/agc047_a
// 参考:https://maspypy.com/atcoder-%E5%8F%82%E5%8A%A0%E6%84%9F%E6%83%B3-2020-08-09agc047
// https://www.hamayanhamayan.com/entry/2020/08/10/054247
// 小数をdoubleで受け取った時点で誤差が発生する、ということが考えられる。
// →string型で受け取ると良い
// Double型の変数で、例えば整数部4桁、小数部9桁の数を10倍してlongに変換する時、
// そのままx = y*10000000000などとやると誤差のせいでうまくいかないこともある
// Double型変数を一度文字列に変換、それをlongに直してやる必要がある
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;
LL N;
LL A[200000];
LL ans = 0;
// xがdivisorを何個約数に持つかを返す
LL numof_divisor(LL x, LL divisor) {
if (x == 0)
return 0;
for (LL i = 0;; i++) {
if (x % divisor == 0) {
x /= divisor;
} else
return i;
}
}
void solve() {
// 集合Aのうち、約数として2をi個、5をj個持つような数の個数をdiv25[i][j]とする
vector<vector<LL>> div25(19, vector<LL>(19, 0));
for (int i = 0; i < N; i++) {
LL d2 = numof_divisor(A[i], 2);
LL d5 = numof_divisor(A[i], 5);
div25[d2][d5]++;
}
for (int i = 0; i < N; i++) {
LL d2 = min(18LL, numof_divisor(A[i], 2));
LL d5 = min(18LL, numof_divisor(A[i], 5));
for (int i = 18 - d2; i <= 18; i++) {
for (int j = 18 - d5; j <= 18; j++) {
ans += div25[i][j];
if (i == d2 && j == d5)
ans--;
}
}
}
ans /= 2;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
string s;
cin >> s;
A[i] = 0;
bool rest_flag = false;
int rest = 9;
for (int j = 0; j < s.size(); j++) {
if (s[j] == '.')
rest_flag = true;
else {
A[i] = A[i] * 10LL + (s[j] - '0');
if (rest_flag)
rest--;
}
}
for (int j = 0; j < rest; j++)
A[i] *= 10LL;
}
solve();
cout << ans << endl;
return 0;
} | // g++ -std=c++14 test.cpp -o test.out
// 問題URL
// https://atcoder.jp/contests/agc047/tasks/agc047_a
// 参考:https://maspypy.com/atcoder-%E5%8F%82%E5%8A%A0%E6%84%9F%E6%83%B3-2020-08-09agc047
// https://www.hamayanhamayan.com/entry/2020/08/10/054247
// 小数をdoubleで受け取った時点で誤差が発生する、ということが考えられる。
// →string型で受け取ると良い
// Double型の変数で、例えば整数部4桁、小数部9桁の数を10倍してlongに変換する時、
// そのままx = y*10000000000などとやると誤差のせいでうまくいかないこともある
// Double型変数を一度文字列に変換、それをlongに直してやる必要がある
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;
LL N;
LL A[200000];
LL ans = 0;
// xがdivisorを何個約数に持つかを返す
LL numof_divisor(LL x, LL divisor) {
if (x == 0)
return 0;
for (LL i = 0;; i++) {
if (x % divisor == 0) {
x /= divisor;
} else
return i;
}
}
void solve() {
// 集合Aのうち、約数として2をi個、5をj個持つような数の個数をdiv25[i][j]とする
vector<vector<LL>> div25(19, vector<LL>(19, 0));
for (int i = 0; i < N; i++) {
LL d2 = min(18LL, numof_divisor(A[i], 2));
LL d5 = min(18LL, numof_divisor(A[i], 5));
div25[d2][d5]++;
}
for (int i = 0; i < N; i++) {
LL d2 = min(18LL, numof_divisor(A[i], 2));
LL d5 = min(18LL, numof_divisor(A[i], 5));
for (int i = 18 - d2; i <= 18; i++) {
for (int j = 18 - d5; j <= 18; j++) {
ans += div25[i][j];
if (i == d2 && j == d5)
ans--;
}
}
}
ans /= 2;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
string s;
cin >> s;
A[i] = 0;
bool rest_flag = false;
int rest = 9;
for (int j = 0; j < s.size(); j++) {
if (s[j] == '.')
rest_flag = true;
else {
A[i] = A[i] * 10LL + (s[j] - '0');
if (rest_flag)
rest--;
}
}
for (int j = 0; j < rest; j++)
A[i] *= 10LL;
}
solve();
cout << ans << endl;
return 0;
} | replace | 58 | 60 | 58 | 60 | 0 | |
p02588 | C++ | Runtime Error | // warm heart, wagging tail,and a smile just for you!
// ███████████
// ███╬╬╬╬╬╬╬╬╬╬███
// ███╬╬╬╬╬████╬╬╬╬╬╬███
// ███████████
// ██╬╬╬╬╬████╬╬████╬╬╬╬╬██
// █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██
// ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██
// ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██
// ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██
// ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████
// █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████
// ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██
// ██████████████
// ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████
// ███████ █████
// ███████████████████
//
#include "bits/stdc++.h"
using namespace std;
#define INF (1 << 30)
#define LINF (1LL << 60)
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR2(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RFOR2(i, a, b) for (int i = (b); i >= (a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define REP2(i, n) FOR2(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define RREP2(i, n) RFOR2(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl
#define SP << " " <<
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
#define MSB(x) (63 - __builtin_clzll(x))
#define pcnt(x) (__builtin_popcountll(x))
#define parity(i, j) (i & (1LL << j))
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<vector<int>> mat;
void solve() {
int N;
cin >> N;
mat a(20, vec(20, 0));
REP(_, N) {
string S;
cin >> S;
bool f = false;
int d = 0;
int num = 0;
REP(i, S.size()) {
if (S[i] == '.')
f = true;
else {
num *= 10;
num += S[i] - '0';
if (f)
d++;
}
}
int x = 0, y = 0;
while (d < 10)
num *= 10, d++;
while (num % 2 == 0)
x++, num /= 2;
while (num % 5 == 0)
y++, num /= 5;
a[x][y]++;
// cout << x SP y << endl;
}
int ans = 0;
REP(i, 20) REP(j, 20) {
REP(k, 20) REP(l, 20) {
if (i + k >= 20 && j + l >= 20 && (i != k || j != l))
ans += a[i][j] * a[k][l];
}
}
ans /= 2;
REP(i, 20) REP(j, 20) {
if (i * 2 >= 20 && j * 2 >= 20)
ans += a[i][j] * (a[i][j] - 1) / 2;
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
} | // warm heart, wagging tail,and a smile just for you!
// ███████████
// ███╬╬╬╬╬╬╬╬╬╬███
// ███╬╬╬╬╬████╬╬╬╬╬╬███
// ███████████
// ██╬╬╬╬╬████╬╬████╬╬╬╬╬██
// █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██
// ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██
// ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██
// ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██
// ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████
// █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████
// ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██
// ██████████████
// ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████
// ███████ █████
// ███████████████████
//
#include "bits/stdc++.h"
using namespace std;
#define INF (1 << 30)
#define LINF (1LL << 60)
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR2(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RFOR2(i, a, b) for (int i = (b); i >= (a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define REP2(i, n) FOR2(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define RREP2(i, n) RFOR2(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl
#define SP << " " <<
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
#define MSB(x) (63 - __builtin_clzll(x))
#define pcnt(x) (__builtin_popcountll(x))
#define parity(i, j) (i & (1LL << j))
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<vector<int>> mat;
void solve() {
int N;
cin >> N;
mat a(20, vec(20, 0));
REP(_, N) {
string S;
cin >> S;
bool f = false;
int d = 0;
int num = 0;
REP(i, S.size()) {
if (S[i] == '.')
f = true;
else {
num *= 10;
num += S[i] - '0';
if (f)
d++;
}
}
int x = 0, y = 0;
while (d < 10)
num *= 10, d++;
while (num % 2 == 0)
x++, num /= 2;
while (num % 5 == 0)
y++, num /= 5;
chmin(x, 19);
chmin(y, 19);
a[x][y]++;
// cout << x SP y << endl;
}
int ans = 0;
REP(i, 20) REP(j, 20) {
REP(k, 20) REP(l, 20) {
if (i + k >= 20 && j + l >= 20 && (i != k || j != l))
ans += a[i][j] * a[k][l];
}
}
ans /= 2;
REP(i, 20) REP(j, 20) {
if (i * 2 >= 20 && j * 2 >= 20)
ans += a[i][j] * (a[i][j] - 1) / 2;
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
} | insert | 98 | 98 | 98 | 100 | 0 | |
p02588 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;
vector<pair<int, int>> vp;
int N;
int dp[10099][40];
pair<int, int> find_repr(string &s) {
int n = s.size();
ll num = 0;
int di = -1;
for (int i = 0; i < n; i++) {
if (s[i] == '.') {
di = i;
continue;
}
num = num * 10 + int(s[i] - '0');
}
int j = (di == -1 ? 0 : n - di - 1);
for (; j < 9; j++) {
num *= 10;
}
// debug(num);
int x = -9, y = -9;
while (num % 2 == 0) {
x++;
num /= 2;
}
while (num % 5 == 0) {
y++;
num /= 5;
}
return {x, y};
}
int main() {
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(10) << fixed;
string s;
cin >> N;
rep(i, N) {
cin >> s;
vp.pb(find_repr(s));
}
sort(vp.begin(), vp.end());
// for(auto x : vp) {
// cout<<x.F<<","<<x.S<<" ";
// }
for (int i = vp.size() - 1; i >= 0; i--) {
dp[i][vp[i].S + 10] = 1;
for (int j = 0; j <= 20; j++)
dp[i][j] += dp[i + 1][j];
}
ll ans = 0;
for (int i = 0; i < vp.size(); i++) {
int x = -vp[i].F, y = -vp[i].S;
int j = lower_bound(vp.begin(), vp.end(), mp(x, -999)) - vp.begin();
if (x <= 0)
j = i + 1;
for (int k = 10 + y; k <= 20; k++)
ans += dp[j][k];
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;
vector<pair<int, int>> vp;
int N;
int dp[200099][40];
pair<int, int> find_repr(string &s) {
int n = s.size();
ll num = 0;
int di = -1;
for (int i = 0; i < n; i++) {
if (s[i] == '.') {
di = i;
continue;
}
num = num * 10 + int(s[i] - '0');
}
int j = (di == -1 ? 0 : n - di - 1);
for (; j < 9; j++) {
num *= 10;
}
// debug(num);
int x = -9, y = -9;
while (num % 2 == 0) {
x++;
num /= 2;
}
while (num % 5 == 0) {
y++;
num /= 5;
}
return {x, y};
}
int main() {
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(10) << fixed;
string s;
cin >> N;
rep(i, N) {
cin >> s;
vp.pb(find_repr(s));
}
sort(vp.begin(), vp.end());
// for(auto x : vp) {
// cout<<x.F<<","<<x.S<<" ";
// }
for (int i = vp.size() - 1; i >= 0; i--) {
dp[i][vp[i].S + 10] = 1;
for (int j = 0; j <= 20; j++)
dp[i][j] += dp[i + 1][j];
}
ll ans = 0;
for (int i = 0; i < vp.size(); i++) {
int x = -vp[i].F, y = -vp[i].S;
int j = lower_bound(vp.begin(), vp.end(), mp(x, -999)) - vp.begin();
if (x <= 0)
j = i + 1;
for (int k = 10 + y; k <= 20; k++)
ans += dp[j][k];
}
cout << ans << endl;
return 0;
}
| replace | 43 | 44 | 43 | 44 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;
const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;
#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod
const ll BIG = 1e9;
struct F {
ll Two;
ll Five;
};
bool two_sort(F x, F y) {
return x.Two != y.Two ? x.Two < y.Two : x.Five < y.Five;
}
bool five_sort(F x, F y) {
return x.Five != y.Five ? x.Five < y.Five : x.Two < y.Two;
}
F Change(string S) {
ll Count = 9;
bool below = false;
ll ret = 0;
rep(i, S.size()) {
if (S[i] == '.')
below = true;
else {
ret *= 10;
ret += (S[i] - '0');
if (below)
Count--;
}
}
ll two = Count, five = Count;
while (!(ret % 2)) {
ret /= 2;
two++;
}
while (!(ret % 5)) {
ret /= 5;
five++;
}
if (two >= 18)
two = 18;
if (five >= 18)
five = 18;
F RET = {two, five};
return RET;
}
int main() {
ll N;
cin >> N;
vector<F> d;
ll Count = 0;
rep(i, N) {
string S;
cin >> S;
d.pb(Change(S));
if (d[i].Two >= 9 && d[i].Five >= 9)
Count++;
}
sort(ALL(d), two_sort);
vec x(19, 0);
ll memo = N - 1;
ll ans = 0;
rep(i, N) {
while (true) {
if (d[memo].Two + d[i].Two < 18)
break;
x[d[memo].Five]++;
memo--;
}
REP(j, 18 - d[i].Five, 19) ans += x[j];
}
ans -= Count;
ans /= 2;
cout << ans << endl;
} | #include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;
const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;
#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod
const ll BIG = 1e9;
struct F {
ll Two;
ll Five;
};
bool two_sort(F x, F y) {
return x.Two != y.Two ? x.Two < y.Two : x.Five < y.Five;
}
bool five_sort(F x, F y) {
return x.Five != y.Five ? x.Five < y.Five : x.Two < y.Two;
}
F Change(string S) {
ll Count = 9;
bool below = false;
ll ret = 0;
rep(i, S.size()) {
if (S[i] == '.')
below = true;
else {
ret *= 10;
ret += (S[i] - '0');
if (below)
Count--;
}
}
ll two = Count, five = Count;
while (!(ret % 2)) {
ret /= 2;
two++;
}
while (!(ret % 5)) {
ret /= 5;
five++;
}
if (two >= 18)
two = 18;
if (five >= 18)
five = 18;
F RET = {two, five};
return RET;
}
int main() {
ll N;
cin >> N;
vector<F> d;
ll Count = 0;
rep(i, N) {
string S;
cin >> S;
d.pb(Change(S));
if (d[i].Two >= 9 && d[i].Five >= 9)
Count++;
}
sort(ALL(d), two_sort);
vec x(19, 0);
ll memo = N - 1;
ll ans = 0;
rep(i, N) {
while (memo >= 0) {
if (d[memo].Two + d[i].Two < 18)
break;
x[d[memo].Five]++;
memo--;
}
REP(j, 18 - d[i].Five, 19) ans += x[j];
}
ans -= Count;
ans /= 2;
cout << ans << endl;
} | replace | 92 | 93 | 92 | 93 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
// #pragma GCC optimize("no-stack-protector,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugp(x) \
cerr << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define debug2(x, y) \
cerr << "{" << #x << ", " << #y << "} = {" << (x) << ", " << (y) << "}" \
<< endl;
#define debugv(v) \
{ \
cerr << #v << " : "; \
for (auto x : v) \
cerr << x << ' '; \
cerr << endl; \
}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout << x << '\n', 0;
const int inf = 1000000010;
const ll INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010, T = 55;
ll ans; // :))
int n, m, k, u, v, x, y, t, a, b;
pii A[MAXN];
int cnt[T][T];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++) {
ld shit;
cin >> shit;
ll x = round(shit * 1000000000);
while (x % 2 == 0)
A[i].first++, x /= 2;
while (x % 5 == 0)
A[i].second++, x /= 5;
cnt[A[i].first][A[i].second]++;
// debugp(A[i])
if (min(A[i].first, A[i].second) >= 9)
ans--;
}
for (int i = T - 2; ~i; i--)
for (int j = T - 2; ~j; j--)
cnt[i][j] += cnt[i + 1][j] + cnt[i][j + 1] - cnt[i + 1][j + 1];
for (int i = 1; i <= n; i++)
ans += cnt[max(0, 18 - A[i].first)][max(0, 18 - A[i].second)];
ans /= 2;
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
// #pragma GCC optimize("no-stack-protector,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugp(x) \
cerr << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define debug2(x, y) \
cerr << "{" << #x << ", " << #y << "} = {" << (x) << ", " << (y) << "}" \
<< endl;
#define debugv(v) \
{ \
cerr << #v << " : "; \
for (auto x : v) \
cerr << x << ' '; \
cerr << endl; \
}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout << x << '\n', 0;
const int inf = 1000000010;
const ll INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 200010, T = 55;
ll ans; // :))
int n, m, k, u, v, x, y, t, a, b;
pii A[MAXN];
int cnt[T][T];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++) {
ld shit;
cin >> shit;
ll x = round(shit * 1000000000);
while (x % 2 == 0)
A[i].first++, x /= 2;
while (x % 5 == 0)
A[i].second++, x /= 5;
cnt[A[i].first][A[i].second]++;
// debugp(A[i])
if (min(A[i].first, A[i].second) >= 9)
ans--;
}
for (int i = T - 2; ~i; i--)
for (int j = T - 2; ~j; j--)
cnt[i][j] += cnt[i + 1][j] + cnt[i][j + 1] - cnt[i + 1][j + 1];
for (int i = 1; i <= n; i++)
ans += cnt[max(0, 18 - A[i].first)][max(0, 18 - A[i].second)];
ans /= 2;
cout << ans << "\n";
return 0;
}
| replace | 31 | 32 | 31 | 32 | 0 | |
p02588 | C++ | Runtime Error | #include <iostream>
#define val 1000000000
#define for2(x) for (x = 0; x <= 9; x++)
using namespace std;
int n, i, b1, b2, b3, b4, c1, c2, c3, c4;
long long x, y, r, sol;
int a[12][12][12][12];
double curr;
long long cmmdc(long long a, long long b) {
long long r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
int main() {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> curr;
curr += 0.0000000001;
x = curr * val;
y = val;
r = cmmdc(x, y);
x /= r;
y /= r;
b1 = b2 = b3 = b4 = 0;
if (y == 1) {
sol--;
}
while (x % 2 == 0) {
b1++;
x /= 2;
}
while (x % 5 == 0) {
b2++;
x /= 5;
}
while (y % 2 == 0) {
b3++;
y /= 2;
}
while (y % 5 == 0) {
b4++;
y /= 5;
}
a[b1][b2][b3][b4]++;
}
for2(b1) {
for2(b2) {
for2(b3) {
for2(b4) {
if (a[b1][b2][b3][b4] == 0) {
continue;
}
for2(c1) {
for2(c2) {
for2(c3) {
for2(c4) {
if (b1 >= c3 && b2 >= c4 && c1 >= b3 && c2 >= b4) {
sol += a[b1][b2][b3][b4] * 1LL * a[c1][c2][c3][c4];
}
}
}
}
}
}
}
}
}
sol /= 2;
cout << sol;
}
| #include <iostream>
#define val 1000000000
#define for2(x) for (x = 0; x <= 9; x++)
using namespace std;
int n, i, b1, b2, b3, b4, c1, c2, c3, c4;
long long x, y, r, sol;
int a[12][12][12][12];
double curr;
long long cmmdc(long long a, long long b) {
long long r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
int main() {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> curr;
curr += 0.0000000001;
x = curr * val;
y = val;
r = cmmdc(x, y);
x /= r;
y /= r;
b1 = b2 = b3 = b4 = 0;
if (y == 1) {
sol--;
}
while (x % 2 == 0) {
b1++;
x /= 2;
}
while (x % 5 == 0) {
b2++;
x /= 5;
}
while (y % 2 == 0) {
b3++;
y /= 2;
}
while (y % 5 == 0) {
b4++;
y /= 5;
}
b1 = min(b1, 9);
b2 = min(b2, 9);
a[b1][b2][b3][b4]++;
}
for2(b1) {
for2(b2) {
for2(b3) {
for2(b4) {
if (a[b1][b2][b3][b4] == 0) {
continue;
}
for2(c1) {
for2(c2) {
for2(c3) {
for2(c4) {
if (b1 >= c3 && b2 >= c4 && c1 >= b3 && c2 >= b4) {
sol += a[b1][b2][b3][b4] * 1LL * a[c1][c2][c3][c4];
}
}
}
}
}
}
}
}
}
sol /= 2;
cout << sol;
}
| insert | 47 | 47 | 47 | 49 | 0 | |
p02588 | C++ | Runtime Error | #include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
#define MAXN 200005
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
inline pi read() {
ll x = 0;
pi ans;
int f = 0;
char c = getchar();
while (!isdigit(c) && c != '.')
c = getchar();
while (isdigit(c) || c == '.') {
if (c == '.') {
f = 1;
c = getchar();
continue;
}
if (f)
--ans.first, --ans.second;
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
}
while (x % 2 == 0)
x /= 2, ++ans.first;
while (x % 5 == 0)
x /= 5, ++ans.second;
return ans;
}
pi a[MAXN];
int s[205][205];
const int N = 10;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
a[i] = read();
for (int i = 1; i <= n; i++)
++s[a[i].first + N][a[i].second + N];
for (int i = 0; i <= 2 * N; i++)
for (int j = 0; j <= 2 * N; j++) {
if (i == 0 && j == 0)
continue;
if (i == 0)
s[i][j] += s[i][j - 1];
else if (j == 0)
s[i][j] += s[i - 1][j];
else
s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
int x = N - a[i].first, y = N - a[i].second;
ans +=
s[2 * N][2 * N] - s[2 * N][y - 1] - s[x - 1][2 * N] + s[x - 1][y - 1];
}
for (int i = 1; i <= n; i++)
if (a[i].first >= 0 && a[i].second >= 0)
--ans;
ans >>= 1;
cout << ans;
return 0;
} | #include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
#define MAXN 200005
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
inline pi read() {
ll x = 0;
pi ans;
int f = 0;
char c = getchar();
while (!isdigit(c) && c != '.')
c = getchar();
while (isdigit(c) || c == '.') {
if (c == '.') {
f = 1;
c = getchar();
continue;
}
if (f)
--ans.first, --ans.second;
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
}
while (x % 2 == 0)
x /= 2, ++ans.first;
while (x % 5 == 0)
x /= 5, ++ans.second;
return ans;
}
pi a[MAXN];
int s[205][205];
const int N = 100;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
a[i] = read();
for (int i = 1; i <= n; i++)
++s[a[i].first + N][a[i].second + N];
for (int i = 0; i <= 2 * N; i++)
for (int j = 0; j <= 2 * N; j++) {
if (i == 0 && j == 0)
continue;
if (i == 0)
s[i][j] += s[i][j - 1];
else if (j == 0)
s[i][j] += s[i - 1][j];
else
s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
int x = N - a[i].first, y = N - a[i].second;
ans +=
s[2 * N][2 * N] - s[2 * N][y - 1] - s[x - 1][2 * N] + s[x - 1][y - 1];
}
for (int i = 1; i <= n; i++)
if (a[i].first >= 0 && a[i].second >= 0)
--ans;
ans >>= 1;
cout << ans;
return 0;
} | replace | 36 | 37 | 36 | 37 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
pair<int, int> f(string &s) {
int d = 0;
ll keep = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '.') {
d = s.size() - i - 1;
} else {
keep = keep * 10 + (s[i] - '0');
}
}
int two = 0, five = 0;
while (keep % 2 == 0) {
two++;
keep /= 2;
}
while (keep % 5 == 0) {
five++;
keep /= 5;
}
return make_pair(two - d, five - d);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vector<ll>> x(21, vector<ll>(21));
ll ans = 0;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
pair<int, int> p = f(s);
// p.first += 10;
// p.second += 10;
for (int j = -p.first + 10; j < 21; j++) {
for (int k = -p.second + 10; k < 21; k++) {
ans += x[j][k];
}
}
x[p.first + 10][p.second + 10]++;
}
cout << ans << '\n';
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
pair<int, int> f(string &s) {
int d = 0;
ll keep = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '.') {
d = s.size() - i - 1;
} else {
keep = keep * 10 + (s[i] - '0');
}
}
int two = 0, five = 0;
while (keep % 2 == 0) {
two++;
keep /= 2;
}
while (keep % 5 == 0) {
five++;
keep /= 5;
}
two -= d;
five -= d;
if (two > 10)
two = 10;
if (five > 10)
five = 10;
return make_pair(two, five);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vector<ll>> x(21, vector<ll>(21));
ll ans = 0;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
pair<int, int> p = f(s);
// p.first += 10;
// p.second += 10;
for (int j = -p.first + 10; j < 21; j++) {
for (int k = -p.second + 10; k < 21; k++) {
ans += x[j][k];
}
}
x[p.first + 10][p.second + 10]++;
}
cout << ans << '\n';
} | replace | 26 | 27 | 26 | 34 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<ll>;
using vv = vector<vi>;
// const int MOD = 1000000007;
const int MOD = 998244353;
const int INF = 1001001001;
const ll LINF = 1001001001001;
int main() {
int n;
cin >> n;
vv faclist(19, vi(19, 0));
rep(i, n) {
string a;
cin >> a;
ll anum = 0;
int dot = 0;
rep(j, a.size()) {
if (a[j] == '.') {
dot = j + 1 - a.size();
} else {
anum *= 10;
anum += a[j] - '0';
}
}
int fac2 = dot, fac5 = dot;
rep(j, 19) {
if (anum % 2)
break;
else {
anum /= 2;
fac2++;
}
}
rep(j, 19) {
if (anum % 5)
break;
else {
anum /= 5;
fac5++;
}
}
faclist[fac2 + 9][fac5 + 9]++;
}
vv acclist = faclist;
rep(i, 19) {
for (int j = 17; j >= 0; j--) {
acclist[i][j] += acclist[i][j + 1];
}
}
rep(j, 19) {
for (int i = 17; i >= 0; i--) {
acclist[i][j] += acclist[i + 1][j];
}
}
ll ans = 0;
rep(i, 19) rep(j, 19) {
if (!faclist[i][j])
continue;
if (i > 8 && j > 8)
ans += faclist[i][j] * (acclist[18 - i][18 - j] - 1);
else
ans += faclist[i][j] * acclist[18 - i][18 - j];
}
cout << ans / 2 << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<ll>;
using vv = vector<vi>;
// const int MOD = 1000000007;
const int MOD = 998244353;
const int INF = 1001001001;
const ll LINF = 1001001001001;
int main() {
int n;
cin >> n;
vv faclist(19, vi(19, 0));
rep(i, n) {
string a;
cin >> a;
ll anum = 0;
int dot = 0;
rep(j, a.size()) {
if (a[j] == '.') {
dot = j + 1 - a.size();
} else {
anum *= 10;
anum += a[j] - '0';
}
}
int fac2 = dot, fac5 = dot;
rep(j, 19) {
if (anum % 2)
break;
else {
anum /= 2;
fac2++;
}
}
rep(j, 19) {
if (anum % 5)
break;
else {
anum /= 5;
fac5++;
}
}
faclist[min(18, fac2 + 9)][min(18, fac5 + 9)]++;
}
vv acclist = faclist;
rep(i, 19) {
for (int j = 17; j >= 0; j--) {
acclist[i][j] += acclist[i][j + 1];
}
}
rep(j, 19) {
for (int i = 17; i >= 0; i--) {
acclist[i][j] += acclist[i + 1][j];
}
}
ll ans = 0;
rep(i, 19) rep(j, 19) {
if (!faclist[i][j])
continue;
if (i > 8 && j > 8)
ans += faclist[i][j] * (acclist[18 - i][18 - j] - 1);
else
ans += faclist[i][j] * acclist[18 - i][18 - j];
}
cout << ans / 2 << endl;
return 0;
} | replace | 52 | 53 | 52 | 53 | 0 | |
p02588 | C++ | Runtime Error | // https://atcoder.jp/contests/agc047/tasks/agc047_a
#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "map"
#include "numeric"
#include "queue"
#include "set"
#include "string"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define rep1(i, to) for (ll i = 1; i <= (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
#define repr(i, from) for (ll i = from - 1; i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define unless(cond) if (!(cond))
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using V = vector<T>;
using VL = V<ll>;
template <typename T> using VV = V<V<T>>;
using VVL = VV<ll>;
template <typename T> using VVV = VV<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T> using asc_pq = priority_queue<T, V<T>, greater<T>>;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline ll len(V<T> arr) { return arr.size(); }
struct exit_exception : public std::exception {
const char *what() const throw() { return "Exited"; }
};
template <typename T> void drop(T res) {
cout << res << endl;
throw exit_exception();
}
const ll INF = 1e18;
void solve();
#ifndef TEST
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
try {
solve();
} catch (exit_exception &e) {
}
return 0;
}
#endif
void solve() {
ll N;
cin >> N;
VL as(N);
rep(i, N) {
ld d;
cin >> d;
d *= 1e9;
as[i] = d + 0.5;
}
VVL ct(19, VL(19)); // 0 ~ 18
for (ll a : as) {
ll ct2 = 0;
ll ct5 = 0;
while (a % 2 == 0) {
a /= 2;
ct2++;
}
while (a % 5 == 0) {
a /= 5;
ct5++;
}
ct[ct2][ct5]++;
}
ll res = 0;
rep(i2, 19) rep(i5, 19) rep(j2, 19) rep(j5, 19) {
unless(i2 + j2 >= 18) continue;
unless(i5 + j5 >= 18) continue;
if (i2 == j2 && i5 == j5) {
res += ct[i2][i5] * (ct[j2][j5] - 1);
} else {
res += ct[i2][i5] * ct[j2][j5];
}
}
res /= 2;
cout << res << "\n";
}
| // https://atcoder.jp/contests/agc047/tasks/agc047_a
#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "map"
#include "numeric"
#include "queue"
#include "set"
#include "string"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define rep1(i, to) for (ll i = 1; i <= (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
#define repr(i, from) for (ll i = from - 1; i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define unless(cond) if (!(cond))
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using V = vector<T>;
using VL = V<ll>;
template <typename T> using VV = V<V<T>>;
using VVL = VV<ll>;
template <typename T> using VVV = VV<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T> using asc_pq = priority_queue<T, V<T>, greater<T>>;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline ll len(V<T> arr) { return arr.size(); }
struct exit_exception : public std::exception {
const char *what() const throw() { return "Exited"; }
};
template <typename T> void drop(T res) {
cout << res << endl;
throw exit_exception();
}
const ll INF = 1e18;
void solve();
#ifndef TEST
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
try {
solve();
} catch (exit_exception &e) {
}
return 0;
}
#endif
void solve() {
ll N;
cin >> N;
VL as(N);
rep(i, N) {
ld d;
cin >> d;
d *= 1e9;
as[i] = d + 0.5;
}
VVL ct(19, VL(19)); // 0 ~ 18
for (ll a : as) {
ll ct2 = 0;
ll ct5 = 0;
while (a % 2 == 0) {
a /= 2;
ct2++;
}
while (a % 5 == 0) {
a /= 5;
ct5++;
}
ct2 = min(ct2, 18ll);
ct5 = min(ct5, 18ll);
ct[ct2][ct5]++;
}
ll res = 0;
rep(i2, 19) rep(i5, 19) rep(j2, 19) rep(j5, 19) {
unless(i2 + j2 >= 18) continue;
unless(i5 + j5 >= 18) continue;
if (i2 == j2 && i5 == j5) {
res += ct[i2][i5] * (ct[j2][j5] - 1);
} else {
res += ct[i2][i5] * ct[j2][j5];
}
}
res /= 2;
cout << res << "\n";
}
| insert | 109 | 109 | 109 | 112 | 0 | |
p02588 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 102;
int a[N][N];
int solv() {
int q;
scanf("%d", &q);
while (q--) {
char s[20];
scanf(" %s", s);
int n = strlen(s);
ll x = 0;
bool z = false;
for (int i = 0; i < n; ++i) {
if (s[i] == '.') {
int q = 9 - (n - i - 1);
for (i = i + 1; i < n; ++i) {
x = x * 10 + s[i] - '0';
}
while (q--) {
x = x * 10;
}
z = true;
break;
}
x = x * 10 + s[i] - '0';
}
if (!z) {
for (int i = 0; i < 9; ++i)
x = x * 10;
}
int q2 = 0;
int q5 = 0;
while (x % 2 == 0) {
++q2;
x /= 2;
}
while (x % 5 == 0) {
++q5;
x /= 5;
}
a[q2][q5]++;
}
ll ans = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (!a[i][j])
continue;
for (int i1 = 0; i1 < N; ++i1) {
for (int j1 = 0; j1 < N; ++j1) {
if (min(i + i1, j + j1) >= 18) {
if (!(i == i1 && j == j1))
ans += a[i][j] * 1LL * a[i1][j1];
else
ans += (a[i][j] * 1LL * a[i][j] - a[i][j]);
}
}
}
}
}
ans /= 2;
printf("%lld\n", ans);
}
int main() {
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
// ios_base::sync_with_stdio(false), cin.tie(0);
solv();
return 0;
}
// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}
| #include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 102;
int a[N][N];
void solv() {
int q;
scanf("%d", &q);
while (q--) {
char s[20];
scanf(" %s", s);
int n = strlen(s);
ll x = 0;
bool z = false;
for (int i = 0; i < n; ++i) {
if (s[i] == '.') {
int q = 9 - (n - i - 1);
for (i = i + 1; i < n; ++i) {
x = x * 10 + s[i] - '0';
}
while (q--) {
x = x * 10;
}
z = true;
break;
}
x = x * 10 + s[i] - '0';
}
if (!z) {
for (int i = 0; i < 9; ++i)
x = x * 10;
}
int q2 = 0;
int q5 = 0;
while (x % 2 == 0) {
++q2;
x /= 2;
}
while (x % 5 == 0) {
++q5;
x /= 5;
}
a[q2][q5]++;
}
ll ans = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (!a[i][j])
continue;
for (int i1 = 0; i1 < N; ++i1) {
for (int j1 = 0; j1 < N; ++j1) {
if (min(i + i1, j + j1) >= 18) {
if (!(i == i1 && j == j1))
ans += a[i][j] * 1LL * a[i1][j1];
else
ans += (a[i][j] * 1LL * a[i][j] - a[i][j]);
}
}
}
}
}
ans /= 2;
printf("%lld\n", ans);
}
int main() {
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
// ios_base::sync_with_stdio(false), cin.tie(0);
solv();
return 0;
}
// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}
| replace | 14 | 15 | 14 | 15 | TLE | |
p02588 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define dump(x) \
if (dbg) { \
cerr << #x << " = " << (x) << endl; \
}
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
} else
return 0;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
} else
return 0;
}
void Yes(bool flag = true) {
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void No(bool flag = true) { Yes(!flag); }
void YES(bool flag = true) {
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void NO(bool flag = true) { YES(!flag); }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
bool dbg = false;
} // namespace mydef
using namespace mydef;
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
// #define P pair<int, int>
// #define V vector<int>
// #define S set<int>
#define asn ans
int N;
vector<pi> A;
int intpow(int a, int b) {
int ans = 1;
while (b) {
if (b & 1)
ans *= a;
a *= a;
b /= 2;
}
return ans;
}
pi f(string s) {
int check = -1;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '.') {
check = i;
break;
}
}
if (check < 0) {
return pi(atoll(s.c_str()), 1);
} else {
int n = s.size() - check - 1;
int se = intpow(10, n);
s.erase(check, 1);
int fi = atoll(s.c_str());
while (fi % 2 == 0 && se % 2 == 0) {
fi /= 2;
se /= 2;
}
while (fi % 5 == 0 && se % 5 == 0) {
fi /= 5;
se /= 5;
}
return pi(fi, se);
}
}
pi g(int x) {
int t2 = 0, t5 = 0;
while (x % 2 == 0) {
x /= 2;
t2++;
}
while (x % 5 == 0) {
x /= 5;
t5++;
}
return pi(t2, t5);
}
int cnt_fi[100][100];
int cnt_se[100][100];
int cnt[10][100][100];
void solve() {
int ans = 0;
for (auto &p : A) {
int fi = p.first;
int se = p.second;
pi P_se, P_fi;
P_se = g(se);
cnt_se[P_se.first][P_se.second]++;
P_fi = g(fi);
cnt_fi[P_fi.first][P_fi.second]++;
for (int i = 0; i < 65; i++) {
for (int j = 0; j < 25; j++) {
if (i >= P_se.first && j >= P_se.second) {
ans += cnt[1][i][j];
}
if (i >= P_se.first && j <= P_fi.second)
ans += cnt[2][i][j];
if (i <= P_fi.first && j >= P_se.second)
ans += cnt[3][i][j];
if (i <= P_fi.first && j <= P_fi.second)
ans += cnt[4][i][j];
}
}
cerr << fi << " " << se << " : " << ans << endl;
if (P_se.first && P_se.second) {
cnt[4][P_se.first][P_se.second]++;
} else if (P_se.first && !P_se.second) {
cnt[3][P_se.first][P_fi.second]++;
} else if (!P_se.first && P_se.second) {
cnt[2][P_fi.first][P_se.second]++;
} else
cnt[1][P_fi.first][P_fi.second]++;
}
cout << ans << endl;
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N;
A.resize(N);
for (int i = 0; i < N; i++) {
string s;
cin >> s;
A[i] = f(s);
}
solve();
return 0;
}
| #include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define dump(x) \
if (dbg) { \
cerr << #x << " = " << (x) << endl; \
}
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
} else
return 0;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
} else
return 0;
}
void Yes(bool flag = true) {
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void No(bool flag = true) { Yes(!flag); }
void YES(bool flag = true) {
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void NO(bool flag = true) { YES(!flag); }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
bool dbg = false;
} // namespace mydef
using namespace mydef;
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
// #define P pair<int, int>
// #define V vector<int>
// #define S set<int>
#define asn ans
int N;
vector<pi> A;
int intpow(int a, int b) {
int ans = 1;
while (b) {
if (b & 1)
ans *= a;
a *= a;
b /= 2;
}
return ans;
}
pi f(string s) {
int check = -1;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '.') {
check = i;
break;
}
}
if (check < 0) {
return pi(atoll(s.c_str()), 1);
} else {
int n = s.size() - check - 1;
int se = intpow(10, n);
s.erase(check, 1);
int fi = atoll(s.c_str());
while (fi % 2 == 0 && se % 2 == 0) {
fi /= 2;
se /= 2;
}
while (fi % 5 == 0 && se % 5 == 0) {
fi /= 5;
se /= 5;
}
return pi(fi, se);
}
}
pi g(int x) {
int t2 = 0, t5 = 0;
while (x % 2 == 0) {
x /= 2;
t2++;
}
while (x % 5 == 0) {
x /= 5;
t5++;
}
return pi(t2, t5);
}
int cnt_fi[100][100];
int cnt_se[100][100];
int cnt[10][100][100];
void solve() {
int ans = 0;
for (auto &p : A) {
int fi = p.first;
int se = p.second;
pi P_se, P_fi;
P_se = g(se);
cnt_se[P_se.first][P_se.second]++;
P_fi = g(fi);
cnt_fi[P_fi.first][P_fi.second]++;
for (int i = 0; i < 65; i++) {
for (int j = 0; j < 25; j++) {
if (i >= P_se.first && j >= P_se.second) {
ans += cnt[1][i][j];
}
if (i >= P_se.first && j <= P_fi.second)
ans += cnt[2][i][j];
if (i <= P_fi.first && j >= P_se.second)
ans += cnt[3][i][j];
if (i <= P_fi.first && j <= P_fi.second)
ans += cnt[4][i][j];
}
}
if (P_se.first && P_se.second) {
cnt[4][P_se.first][P_se.second]++;
} else if (P_se.first && !P_se.second) {
cnt[3][P_se.first][P_fi.second]++;
} else if (!P_se.first && P_se.second) {
cnt[2][P_fi.first][P_se.second]++;
} else
cnt[1][P_fi.first][P_fi.second]++;
}
cout << ans << endl;
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N;
A.resize(N);
for (int i = 0; i < N; i++) {
string s;
cin >> s;
A[i] = f(s);
}
solve();
return 0;
}
| delete | 157 | 159 | 157 | 157 | TLE | |
p02588 | C++ | Time Limit Exceeded | // gzhffIThh
#include <bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define ul unsigned ll
#define ld long double
#define pi pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define vi vector<int>
#define pb push_back
#define pq priority_queue
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = b; i >= a; i--)
#define dbg(x) cerr << #x " = " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fl(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout);
using namespace std;
namespace io {
const int SI = 1 << 21 | 1;
char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];
int f, t;
#define gc() \
(IS == IT \
? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) \
: *IS++)
inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; }
inline void pc(char x) {
*OS++ = x;
if (OS == OT)
flush();
}
template <class I> inline void rd(I &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc())
if (c == '-')
f = -1;
for (x = 0; c >= '0' && c <= '9';
x = (x << 3) + (x << 1) + (c & 15), c = gc())
;
x *= f;
}
template <class I> inline void rd(I &x, I &y) { rd(x), rd(y); }
template <class I> inline void rd(I &x, I &y, I &z) { rd(x), rd(y), rd(z); }
template <class I> inline void rda(I *a, int n) {
for (int i = 1; i <= n; i++)
rd(a[i]);
}
inline void rdc(char &c) {
for (c = gc(); c < 33 || c > 126; c = gc())
;
}
inline void rds(char *s, int &n) {
for (c = gc(); c < 33 || c > 126; c = gc())
;
for (n = 0; c >= 33 && c <= 126; s[++n] = c, c = gc())
;
s[n + 1] = '\0';
}
inline void rds(string &s) {
for (c = gc(); c < 33 || c > 126; c = gc())
;
for (s.clear(); c >= 33 && c <= 126; s.pb(c), c = gc())
;
}
template <class I> inline void print(I x, char k = '\n') {
if (!x)
pc('0');
if (x < 0)
pc('-'), x = -x;
while (x)
ch[++t] = x % 10 + '0', x /= 10;
while (t)
pc(ch[t--]);
pc(k);
}
template <class I> inline void print(I x, I y) { print(x, ' '), print(y); }
template <class I> inline void print(I x, I y, I z) {
print(x, ' '), print(y, ' '), print(z);
}
template <class I> inline void printa(I *a, int n) {
for (int i = 1; i <= n; i++)
print(a[i], " \n"[i == n]);
}
inline void printc(char c) { pc(c); }
inline void prints(char *s, int n) {
for (int i = 1; i <= n; i++)
pc(s[i]);
pc('\n');
}
inline void prints(string s) {
int n = s.length();
while (t < n)
pc(s[t++]);
pc('\n'), t = 0;
}
struct Flush {
~Flush() { flush(); }
} flusher;
} // namespace io
using io::print;
using io::printa;
using io::printc;
using io::prints;
using io::rd;
using io::rda;
using io::rdc;
using io::rds;
const int P = 1000000007;
struct modint {
int x;
inline modint(int x = 0) : x(x) {}
inline modint &operator=(int o) { return x = o, *this; }
inline modint &operator+=(modint o) {
return x = x + o.x >= P ? x + o.x - P : x + o.x, *this;
}
inline modint &operator-=(modint o) {
return x = x - o.x < 0 ? x - o.x + P : x - o.x, *this;
}
inline modint &operator*=(modint o) { return x = 1ll * x * o.x % P, *this; }
inline modint &operator^=(ll b) {
modint a = *this, c;
if (!~b)
b = P - 2;
c.x = 1 % P;
while (b) {
if (b & 1)
c *= a;
a *= a, b >>= 1;
}
return x = c.x, *this;
}
inline modint &operator/=(modint o) { return *this *= o ^= -1; }
inline modint &operator+=(int o) {
return x = x + o >= P ? x + o - P : x + o, *this;
}
inline modint &operator-=(int o) {
return x = x - o < 0 ? x - o + P : x - o, *this;
}
inline modint &operator*=(int o) { return x = 1ll * x * o % P, *this; }
inline modint &operator/=(int o) {
modint y = modint(o);
return *this *= y ^= -1;
}
template <class I> inline friend modint operator+(modint a, I b) {
return a += b;
}
template <class I> inline friend modint operator-(modint a, I b) {
return a -= b;
}
template <class I> inline friend modint operator*(modint a, I b) {
return a *= b;
}
inline friend modint operator^(modint a, ll b) { return a ^= b; }
template <class I> inline friend modint operator/(modint a, I b) {
return a /= b;
}
inline friend bool operator==(modint a, int b) { return a.x == b; }
inline friend bool operator!=(modint a, int b) { return a.x != b; }
inline friend bool operator<(modint a, int b) { return a.x < b; }
inline friend bool operator<=(modint a, int b) { return a.x <= b; }
inline friend bool operator>(modint a, int b) { return a.x > b; }
inline friend bool operator>=(modint a, int b) { return a.x >= b; }
inline friend bool operator==(modint a, modint b) { return a.x == b.x; }
inline friend bool operator!=(modint a, modint b) { return a.x != b.x; }
inline friend bool operator<(modint a, modint b) { return a.x < b.x; }
inline friend bool operator<=(modint a, modint b) { return a.x <= b.x; }
inline friend bool operator>(modint a, modint b) { return a.x > b.x; }
inline friend bool operator>=(modint a, modint b) { return a.x >= b.x; }
inline bool operator!() { return !x; }
inline modint operator-() { return x ? P - x : 0; }
};
inline void rd(modint &x) { rd(x.x); }
inline void print(modint x, char k = '\n') { print(x.x, k); }
template <class T> inline void cmin(T &a, T b) { a = b < a ? b : a; }
template <class T> inline void cmax(T &a, T b) { a = b > a ? b : a; }
// head
const int N = 200010;
const int M = 57;
int n;
long double a[N];
ll g[N], tmp = 1000000000;
int c[M][M];
ll ans;
int main() {
fl("A");
rd(n);
for (int i = 1; i <= n; i++) {
string s;
rds(s);
int p = -1;
for (ui j = 0; j < s.size(); j++)
if (s[j] == '.')
p = j;
if (!~p)
p = s.size(), s += ".0";
while ((int)s.size() - p < 10)
s += "0";
ll a = 0;
for (ui j = 0; j < s.size(); j++)
if (s[j] != '.')
a = a * 10 + s[j] - '0';
int c2 = 0, c5 = 0;
while (a % 2 == 0)
++c2, a /= 2;
while (a % 5 == 0)
++c5, a /= 5;
c[c2][c5]++;
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
for (int k = max(0, 18 - i); k < M; k++) {
for (int t = max(0, 18 - j); t < M; t++) {
if (i == k && j == t)
continue;
ans += 1ll * c[i][j] * c[k][t];
}
}
}
}
for (int i = 9; i < M; i++) {
for (int j = 9; j < M; j++) {
if (c[i][j]) {
ans += 1ll * c[i][j] * (c[i][j] - 1);
}
}
}
print(ans / 2);
return 0;
}
| // gzhffIThh
#include <bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define ul unsigned ll
#define ld long double
#define pi pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define vi vector<int>
#define pb push_back
#define pq priority_queue
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = b; i >= a; i--)
#define dbg(x) cerr << #x " = " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fl(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout);
using namespace std;
namespace io {
const int SI = 1 << 21 | 1;
char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];
int f, t;
#define gc() \
(IS == IT \
? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) \
: *IS++)
inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; }
inline void pc(char x) {
*OS++ = x;
if (OS == OT)
flush();
}
template <class I> inline void rd(I &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc())
if (c == '-')
f = -1;
for (x = 0; c >= '0' && c <= '9';
x = (x << 3) + (x << 1) + (c & 15), c = gc())
;
x *= f;
}
template <class I> inline void rd(I &x, I &y) { rd(x), rd(y); }
template <class I> inline void rd(I &x, I &y, I &z) { rd(x), rd(y), rd(z); }
template <class I> inline void rda(I *a, int n) {
for (int i = 1; i <= n; i++)
rd(a[i]);
}
inline void rdc(char &c) {
for (c = gc(); c < 33 || c > 126; c = gc())
;
}
inline void rds(char *s, int &n) {
for (c = gc(); c < 33 || c > 126; c = gc())
;
for (n = 0; c >= 33 && c <= 126; s[++n] = c, c = gc())
;
s[n + 1] = '\0';
}
inline void rds(string &s) {
for (c = gc(); c < 33 || c > 126; c = gc())
;
for (s.clear(); c >= 33 && c <= 126; s.pb(c), c = gc())
;
}
template <class I> inline void print(I x, char k = '\n') {
if (!x)
pc('0');
if (x < 0)
pc('-'), x = -x;
while (x)
ch[++t] = x % 10 + '0', x /= 10;
while (t)
pc(ch[t--]);
pc(k);
}
template <class I> inline void print(I x, I y) { print(x, ' '), print(y); }
template <class I> inline void print(I x, I y, I z) {
print(x, ' '), print(y, ' '), print(z);
}
template <class I> inline void printa(I *a, int n) {
for (int i = 1; i <= n; i++)
print(a[i], " \n"[i == n]);
}
inline void printc(char c) { pc(c); }
inline void prints(char *s, int n) {
for (int i = 1; i <= n; i++)
pc(s[i]);
pc('\n');
}
inline void prints(string s) {
int n = s.length();
while (t < n)
pc(s[t++]);
pc('\n'), t = 0;
}
struct Flush {
~Flush() { flush(); }
} flusher;
} // namespace io
using io::print;
using io::printa;
using io::printc;
using io::prints;
using io::rd;
using io::rda;
using io::rdc;
using io::rds;
const int P = 1000000007;
struct modint {
int x;
inline modint(int x = 0) : x(x) {}
inline modint &operator=(int o) { return x = o, *this; }
inline modint &operator+=(modint o) {
return x = x + o.x >= P ? x + o.x - P : x + o.x, *this;
}
inline modint &operator-=(modint o) {
return x = x - o.x < 0 ? x - o.x + P : x - o.x, *this;
}
inline modint &operator*=(modint o) { return x = 1ll * x * o.x % P, *this; }
inline modint &operator^=(ll b) {
modint a = *this, c;
if (!~b)
b = P - 2;
c.x = 1 % P;
while (b) {
if (b & 1)
c *= a;
a *= a, b >>= 1;
}
return x = c.x, *this;
}
inline modint &operator/=(modint o) { return *this *= o ^= -1; }
inline modint &operator+=(int o) {
return x = x + o >= P ? x + o - P : x + o, *this;
}
inline modint &operator-=(int o) {
return x = x - o < 0 ? x - o + P : x - o, *this;
}
inline modint &operator*=(int o) { return x = 1ll * x * o % P, *this; }
inline modint &operator/=(int o) {
modint y = modint(o);
return *this *= y ^= -1;
}
template <class I> inline friend modint operator+(modint a, I b) {
return a += b;
}
template <class I> inline friend modint operator-(modint a, I b) {
return a -= b;
}
template <class I> inline friend modint operator*(modint a, I b) {
return a *= b;
}
inline friend modint operator^(modint a, ll b) { return a ^= b; }
template <class I> inline friend modint operator/(modint a, I b) {
return a /= b;
}
inline friend bool operator==(modint a, int b) { return a.x == b; }
inline friend bool operator!=(modint a, int b) { return a.x != b; }
inline friend bool operator<(modint a, int b) { return a.x < b; }
inline friend bool operator<=(modint a, int b) { return a.x <= b; }
inline friend bool operator>(modint a, int b) { return a.x > b; }
inline friend bool operator>=(modint a, int b) { return a.x >= b; }
inline friend bool operator==(modint a, modint b) { return a.x == b.x; }
inline friend bool operator!=(modint a, modint b) { return a.x != b.x; }
inline friend bool operator<(modint a, modint b) { return a.x < b.x; }
inline friend bool operator<=(modint a, modint b) { return a.x <= b.x; }
inline friend bool operator>(modint a, modint b) { return a.x > b.x; }
inline friend bool operator>=(modint a, modint b) { return a.x >= b.x; }
inline bool operator!() { return !x; }
inline modint operator-() { return x ? P - x : 0; }
};
inline void rd(modint &x) { rd(x.x); }
inline void print(modint x, char k = '\n') { print(x.x, k); }
template <class T> inline void cmin(T &a, T b) { a = b < a ? b : a; }
template <class T> inline void cmax(T &a, T b) { a = b > a ? b : a; }
// head
const int N = 200010;
const int M = 57;
int n;
long double a[N];
ll g[N], tmp = 1000000000;
int c[M][M];
ll ans;
int main() {
// fl("A");
rd(n);
for (int i = 1; i <= n; i++) {
string s;
rds(s);
int p = -1;
for (ui j = 0; j < s.size(); j++)
if (s[j] == '.')
p = j;
if (!~p)
p = s.size(), s += ".0";
while ((int)s.size() - p < 10)
s += "0";
ll a = 0;
for (ui j = 0; j < s.size(); j++)
if (s[j] != '.')
a = a * 10 + s[j] - '0';
int c2 = 0, c5 = 0;
while (a % 2 == 0)
++c2, a /= 2;
while (a % 5 == 0)
++c5, a /= 5;
c[c2][c5]++;
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
for (int k = max(0, 18 - i); k < M; k++) {
for (int t = max(0, 18 - j); t < M; t++) {
if (i == k && j == t)
continue;
ans += 1ll * c[i][j] * c[k][t];
}
}
}
}
for (int i = 9; i < M; i++) {
for (int j = 9; j < M; j++) {
if (c[i][j]) {
ans += 1ll * c[i][j] * (c[i][j] - 1);
}
}
}
print(ans / 2);
return 0;
}
| replace | 203 | 204 | 203 | 204 | TLE | |
p02588 | C++ | Runtime Error | #define MOD 1000000007
#if 1
//------------------------------------------------------------
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
template <class T> struct Entry {
static void Run() { T().Run(); }
};
struct MyMain;
#if defined(TEST)
#include "test.hpp"
#else
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(16);
Entry<MyMain>::Run();
return 0;
}
#endif
//------------------------------------------------------------
using ll = long long;
#define int ll
#define FOR(i, s, e) for (ll i = ll(s); i < ll(e); ++i)
#define RFOR(i, s, e) for (ll i = ll(e) - 1; i >= ll(s); --i)
#define REP(i, n) for (ll i = 0, i##_size = ll(n); i < i##_size; ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define INF INT64_MAX
//------------------------------------------------------------
template <class T> struct ContainerHolder {
bool flag = true;
T &var;
ContainerHolder(T &v) : var(v) {}
operator bool() const { return flag; }
T *operator->() { return &var; }
};
template <class T> ContainerHolder<T> ContainerHold(T &v) {
return ContainerHolder<T>(v);
}
#define CFOR(it, container) \
for (auto holder = ContainerHold(container); holder; holder.flag = false) \
for (auto it = holder->begin(), endIt = holder->end(); it != endIt; ++it)
//------------------------------------------------------------
template <class T> struct arr : public vector<T> {
arr() {}
arr(initializer_list<T> il) : vector<T>(il) {}
explicit arr(ll n, T v = T()) : vector<T>(n, v) {}
T &operator()(int i) { return (*this)[i]; }
T const &operator()(int i) const { return (*this)[i]; }
void init(ll n, T v = T()) {
this->clear();
this->resize(n, v);
}
ll sz() const { return (ll)this->size(); }
void pb(T v) { this->push_back(v); }
void sort() { std::sort(this->begin(), this->end()); }
void sort(function<bool(T, T)> p) {
std::sort(this->begin(), this->end(), p);
}
void rsort() { std::sort(this->begin(), this->end(), greater<T>()); }
void reverse() { std::reverse(this->begin(), this->end()); }
void unique_erase() {
this->erase(std::unique(this->begin(), this->end()), this->end());
}
bool next_permutation() {
return std::next_permutation(this->begin(), this->end());
}
// これ以下はソート済み前提
int lower_bound(T const &v, function<bool(T, T)> p) {
return std::lower_bound(this->begin(), this->end(), v, p) - this->begin();
}
int lower_bound(T const &v) {
return std::lower_bound(this->begin(), this->end(), v) - this->begin();
}
int upper_bound(T const &v, function<bool(T, T)> p) {
return std::upper_bound(this->begin(), this->end(), v, p) - this->begin();
}
int upper_bound(T const &v) {
return std::upper_bound(this->begin(), this->end(), v) - this->begin();
}
int find_nearest(T const &v) {
int i = this->lower_bound(v);
if (i >= sz()) {
--i;
} else if ((*this)[i] != v) {
int p = i - 1;
if (p >= 0) {
int id = abs((*this)[i] - v);
int pd = abs((*this)[p] - v);
if (pd < id) {
i = p;
}
}
}
return i;
}
// 見つからなければ-1
int find(T const &v) {
int i = this->lower_bound(v);
if (i >= sz()) {
return -1;
}
if ((*this)[i] != v) {
return -1;
}
return i;
}
};
using ints = arr<ll>;
template <class T> struct que : public queue<T> {
ll sz() const { return (ll)this->size(); }
T popfront() {
T v = this->front();
this->pop();
return v;
}
};
template <class A, class B> struct pr {
union {
A a;
A key;
A first;
A x;
};
union {
B b;
B value;
B second;
B y;
};
pr() : a(A()), b(B()){};
pr(A a_, B b_) : a(a_), b(b_) {}
pr(pr const &r) : a(r.a), b(r.b){};
pr(pair<A, B> const &r) : a(r.first), b(r.second){};
bool operator==(pr const &r) const { return a == r.a && b == r.b; }
bool operator!=(pr const &r) const { return !((*this) == r); }
bool operator<(pr const &r) const {
if (a == r.a) {
return b < r.b;
}
return a < r.a;
}
pr operator+(pr v) const { return pr(x, y) += v; }
pr operator-(pr v) const { return pr(x, y) -= v; }
pr &operator+=(pr v) {
x += v.x;
y += v.y;
return *this;
}
pr &operator-=(pr v) {
x -= v.x;
y -= v.y;
return *this;
}
void flip() { swap(x, y); }
friend istream &operator>>(istream &is, pr &p) {
is >> p.a >> p.b;
return is;
}
friend ostream &operator<<(ostream &os, pr const &p) {
os << p.a << " " << p.b;
return os;
}
};
using pint = pr<ll, ll>;
using pints = arr<pint>;
template <class K, class V> struct dic : public map<K, V> {
bool get(K const &k, V *v) {
auto it = this->find(k);
if (it != this->end()) {
*v = it->second;
return true;
}
return false;
}
};
template <class T> struct arr2 {
vector<vector<T>> m_vec;
int m_width;
int m_height;
arr2() : m_width(0), m_height(0) {}
arr2(int w, int h, T const &value = T()) : m_width(w), m_height(h) {
m_vec.resize(h, vector<T>(w, value));
}
arr2(arr2 const &r) {
m_vec = r.m_vec;
m_width = r.m_width;
m_height = r.m_height;
}
arr2(arr2 &&r) {
m_vec = move(r.m_vec);
m_width = r.m_width;
m_height = r.m_height;
}
arr2 &operator=(arr2 const &r) {
m_vec = r.m_vec;
m_width = r.m_width;
m_height = r.m_height;
return *this;
}
arr2 &operator=(arr2 &&r) {
m_vec = move(r.m_vec);
m_width = r.m_width;
m_height = r.m_height;
return *this;
}
bool operator==(arr2 const &r) const { return m_vec = r.m_vec; }
bool operator<(arr2 const &r) const {
if (m_width != r.m_width) {
return m_width < r.m_width;
}
if (m_height != r.m_height) {
return m_height < r.m_height;
}
REP(y, m_height) {
REP(x, m_width) {
if ((*this)(x, y) != r(x, y)) {
return (*this)(x, y) < r(x, y);
}
}
}
return false;
}
pint size() const { return pint(m_width, m_height); }
int width() const { return m_width; }
int height() const { return m_height; }
void init(int w, int h, T const &value = T()) {
m_vec.clear();
m_vec.resize(h, vector<T>(w, value));
m_width = w;
m_height = h;
}
void init(pint size, T const &value = T()) { init(size.x, size.y, value); }
T &operator()(int x, int y) { return m_vec[y][x]; }
T const &operator()(int x, int y) const { return m_vec[y][x]; }
T &operator()(pint p) { return m_vec[p.y][p.x]; }
T const &operator()(pint p) const { return m_vec[p.y][p.x]; }
T &operator[](pint p) { return m_vec[p.y][p.x]; }
T const &operator[](pint p) const { return m_vec[p.y][p.x]; }
bool isIn(int x, int y) const {
return x >= 0 && x < m_width && y >= 0 && y < m_height;
}
bool isIn(pint p) const { return isIn(p.x, p.y); }
bool isOut(int x, int y) const {
return x < 0 || x >= m_width || y < 0 || y >= m_height;
}
bool isOut(pint p) const { return isOut(p.x, p.y); }
struct iterator {
private:
arr2<T> *owner;
public:
pint pt;
iterator(arr2<T> *owner_, pint pt_) : owner(owner_), pt(pt_) {}
bool operator==(iterator const &r) const { return pt == r.pt; }
bool operator!=(iterator const &r) const { return !((*this) == r); }
void operator++() {
++pt.x;
if (pt.x >= owner->width()) {
++pt.y;
pt.x = 0;
}
}
T &operator*() { return (*owner)(pt); }
};
iterator begin() { return iterator(this, pint(0, 0)); }
iterator end() { return iterator(this, pint(0, height())); }
void disp(ostream &os) {
REP(y, m_height) {
REP(x, m_width) { os << setw(2) << (*this)(x, y) << " "; }
os << endl;
}
os << endl;
}
};
const pints around4 = {pint(-1, 0), pint(0, -1), pint(1, 0), pint(0, 1)};
//------------------------------------------------------------
template <class T> void chmin(T &a, T b) {
if (b < a) {
a = b;
}
}
template <class T> void chmax(T &a, T b) {
if (b > a) {
a = b;
}
}
constexpr int gcd(int a, int b) {
if (a < 0) {
a = -a;
}
if (b < 0) {
b = -b;
}
if (a == 0) {
return b;
}
if (b == 0) {
return a;
}
while (int c = a % b) {
a = b;
b = c;
}
return b;
}
constexpr int lcm(int a, int b) { return a * b / gcd(a, b); }
//------------------------------------------------------------
template <int M> struct modint {
int raw;
modint() { raw = 0; }
modint(int v) {
if (v < 0) {
raw = (v % M) + M;
} else if (v >= M) {
raw = v % M;
} else {
raw = v;
}
}
modint operator+(modint v) const { return modint(raw) += v; }
modint operator-(modint v) const { return modint(raw) -= v; }
modint operator*(modint v) const { return modint(raw) *= v; }
modint &operator+=(modint v) {
raw += v.raw;
if (raw >= M) {
raw -= M;
}
return *this;
}
modint &operator-=(modint v) {
raw -= v.raw;
if (raw < 0) {
raw += M;
}
return *this;
}
modint &operator*=(modint v) {
raw = (raw * v.raw) % M;
return *this;
}
modint pow(int n) const { return modint::pow(raw, n); }
static modint pow(int a, int n) {
if (n < 0) {
// not support
abort();
}
int r = 1;
while (n) {
if (n & 1) {
r = (r * a) % M;
}
a = (a * a) % M;
n >>= 1;
}
return modint(r);
}
modint inv() const {
int a = raw;
int b = M;
int u = 1;
int v = 0;
while (b) {
int t = a / b;
a -= t * b;
u -= t * v;
swap(a, b);
swap(u, v);
}
u %= M;
if (u < 0) {
u += M;
}
return u;
}
friend istream &operator>>(istream &is, modint &m) {
int v;
is >> v;
m = modint(v);
return is;
}
friend ostream &operator<<(ostream &os, modint const &m) {
return os << m.raw;
}
};
using mint = modint<MOD>;
using mints = arr<mint>;
//------------------------------------------------------------
#if defined(TEST)
extern istream &mis;
extern ostream &mos;
#else
istream &mis = cin;
ostream &mos = cout;
#endif
//------------------------------------------------------------
struct OutputStream {
template <class T>
friend OutputStream &operator<<(OutputStream &s, T const &v) {
mos << v << '\n';
return s;
}
} out;
struct PrintStream {
stringstream ss;
template <class T> friend PrintStream &operator<<(PrintStream &s, T &&v) {
s.ss << v << ' ';
return s;
}
PrintStream &operator<<(PrintStream &(*manip)(PrintStream &)) {
return (*manip)(*this);
}
} prn;
PrintStream &endl(PrintStream &s) {
string str = s.ss.str();
s.ss = stringstream();
if (str.empty() == false) {
str.pop_back();
}
mos << str << '\n';
return s;
}
//------------------------------------------------------------
struct flip_t {
} flip;
template <class T> struct in_base {
T value;
in_base() { mis >> value; }
operator T() { return value; }
};
struct in_int {
int value;
in_int() { mis >> value; }
in_int(int add) {
mis >> value;
value += add;
}
operator int() { return value; }
};
template <class A, class B> struct in_pr {
pr<A, B> value;
in_pr() { mis >> value.a >> value.b; }
in_pr(int addA, int addB) {
mis >> value.a >> value.b;
value.a += addA;
value.b += addB;
}
in_pr(flip_t) { mis >> value.b >> value.a; }
in_pr(int addA, int addB, flip_t) {
mis >> value.b >> value.a;
value.a += addB;
value.b += addA;
}
operator pr<A, B>() { return value; }
};
template <class T> struct in_arr {
arr<T> value;
in_arr(int N) {
value.init(N);
REP(i, N) { mis >> value[i]; }
}
in_arr(int N, int add) {
value.init(N);
REP(i, N) {
mis >> value[i];
value[i] += add;
}
}
operator arr<T>() { return value; }
};
template <> struct in_arr<pint> {
arr<pint> value;
in_arr(int N) {
value.init(N);
REP(i, N) { mis >> value[i]; }
}
in_arr(int N, int addA, int addB) {
value.init(N);
REP(i, N) {
mis >> value[i].a >> value[i].b;
value[i].a += addA;
value[i].b += addB;
}
}
in_arr(int N, flip_t) {
value.init(N);
REP(i, N) { mis >> value[i].b >> value[i].a; }
}
in_arr(int N, int addA, int addB, flip_t) {
value.init(N);
REP(i, N) {
mis >> value[i].b >> value[i].a;
value[i].a += addB;
value[i].b += addA;
}
}
operator arr<pint>() { return value; }
};
template <class A, class B> struct in_dic {
dic<A, B> value;
in_dic(int N) {
REP(i, N) {
pair<A, B> pr;
mis >> pr.first >> pr.second;
value.insert(pr);
}
}
in_dic(int N, int addA, int addB) {
REP(i, N) {
pair<A, B> pr;
mis >> pr.first >> pr.second;
pr.first += addA;
pr.second += addB;
value.insert(pr);
}
}
in_dic(int N, flip_t) {
REP(i, N) {
pair<A, B> pr;
mis >> pr.second >> pr.first;
value.insert(pr);
}
}
in_dic(int N, int addA, int addB, flip_t) {
REP(i, N) {
pair<A, B> pr;
mis >> pr.second >> pr.first;
pr.first += addB;
pr.second += addA;
value.insert(pr);
}
}
operator dic<A, B>() { return value; }
};
template <class T> struct in_arr2 {
arr2<T> value;
in_arr2(int H, int W) {
value.init(W, H);
REP(y, H) {
REP(x, W) { mis >> value(x, y); }
}
}
in_arr2(int H, int W, int add) {
value.init(W, H);
REP(y, H) {
REP(x, W) {
mis >> value(x, y);
value(x, y) += add;
}
}
}
operator arr2<T>() { return value; }
};
template <class T, class Tuple, size_t... Index>
T MakeFromTupleImpl(Tuple &&t, index_sequence<Index...>) {
return T(get<Index>(forward<Tuple>(t))...);
}
template <class T, class Tuple> T MakeFromTuple(Tuple &&t) {
return MakeFromTupleImpl<T>(
forward<Tuple>(t),
make_index_sequence<tuple_size<remove_reference_t<Tuple>>::value>{});
}
template <class... ARGS> struct inputWithParam {
tuple<ARGS...> param;
inputWithParam() {}
explicit inputWithParam(tuple<ARGS...> &¶m_) : param(param_) {}
operator int() { return MakeFromTuple<in_int>(param); }
operator double() { return MakeFromTuple<in_base<double>>(param); }
operator string() { return MakeFromTuple<in_base<string>>(param); }
operator char() { return MakeFromTuple<in_base<char>>(param); }
template <class A, class B> operator pr<A, B>() {
return MakeFromTuple<in_pr<A, B>>(param);
}
template <class T> operator arr<T>() {
return MakeFromTuple<in_arr<T>>(param);
}
template <class A, class B> operator dic<A, B>() {
return MakeFromTuple<in_dic<A, B>>(param);
}
template <class T> operator arr2<T>() {
return MakeFromTuple<in_arr2<T>>(param);
}
};
struct input {
operator int() { return inputWithParam<>(); }
operator double() { return inputWithParam<>(); }
operator string() { return inputWithParam<>(); }
operator char() { return inputWithParam<>(); }
template <class A, class B> operator pr<A, B>() { return inputWithParam<>(); }
template <class... ARGS> inputWithParam<ARGS...> operator()(ARGS &&...args) {
inputWithParam<ARGS...> withParam(forward_as_tuple(args...));
return withParam;
}
} in;
//------------------------------------------------------------
#endif
struct MyMain {
int N = in;
arr<double> S = in(N);
struct Rational {
double original;
int child;
int parent;
int c2;
int c5;
};
int CalcC2(int v) {
for (int i = 0;; ++i) {
if (v % 2 != 0) {
return i;
}
v /= 2;
}
}
int CalcC5(int v) {
for (int i = 0;; ++i) {
if (v % 5 != 0) {
return i;
}
v /= 5;
}
}
void Run() {
int offset = 15;
int extendSize = offset * 2 + 1;
arr<Rational> A;
A.reserve(N);
REP(i, N) {
double real = S[i];
int child = (int)floor(real * 1000000000 + 0.5);
int parent = 1000000000;
int g = gcd(child, parent);
child /= g;
parent /= g;
Rational r;
r.original = real;
r.child = child;
r.parent = parent;
r.c2 = CalcC2(child) - CalcC2(parent);
r.c5 = CalcC5(child) - CalcC5(parent);
A.push_back(r);
assert(r.c2 < offset);
assert(r.c5 < offset);
assert(r.c2 > -offset);
assert(r.c5 > -offset);
}
arr2<int> counts(extendSize, extendSize);
REP(i, N) { counts(A[i].c2 + offset, A[i].c5 + offset) += 1; }
int ans = 0;
REP(y, extendSize) {
int c5 = y - offset;
int ly = -c5 + offset;
REP(x, extendSize) {
int c2 = x - offset;
int lx = -c2 + offset;
int bc = counts(x, y);
if (bc == 0) {
continue;
}
FOR(v, ly, extendSize) {
FOR(h, lx, extendSize) {
if (h == x && v == y) {
continue;
}
int sc = counts(h, v);
if (sc == 0) {
continue;
}
ans += sc * bc;
}
}
}
}
ans /= 2;
REP(y, extendSize) {
int c5 = y - offset;
int ly = -c5 + offset;
REP(x, extendSize) {
int c2 = x - offset;
int lx = -c2 + offset;
int bc = counts(x, y);
if (bc == 0) {
continue;
}
if (c2 >= 0 && c5 >= 0) {
ans += bc * (bc - 1) / 2;
}
}
}
out << ans;
}
};
| #define MOD 1000000007
#if 1
//------------------------------------------------------------
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
template <class T> struct Entry {
static void Run() { T().Run(); }
};
struct MyMain;
#if defined(TEST)
#include "test.hpp"
#else
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(16);
Entry<MyMain>::Run();
return 0;
}
#endif
//------------------------------------------------------------
using ll = long long;
#define int ll
#define FOR(i, s, e) for (ll i = ll(s); i < ll(e); ++i)
#define RFOR(i, s, e) for (ll i = ll(e) - 1; i >= ll(s); --i)
#define REP(i, n) for (ll i = 0, i##_size = ll(n); i < i##_size; ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define INF INT64_MAX
//------------------------------------------------------------
template <class T> struct ContainerHolder {
bool flag = true;
T &var;
ContainerHolder(T &v) : var(v) {}
operator bool() const { return flag; }
T *operator->() { return &var; }
};
template <class T> ContainerHolder<T> ContainerHold(T &v) {
return ContainerHolder<T>(v);
}
#define CFOR(it, container) \
for (auto holder = ContainerHold(container); holder; holder.flag = false) \
for (auto it = holder->begin(), endIt = holder->end(); it != endIt; ++it)
//------------------------------------------------------------
template <class T> struct arr : public vector<T> {
arr() {}
arr(initializer_list<T> il) : vector<T>(il) {}
explicit arr(ll n, T v = T()) : vector<T>(n, v) {}
T &operator()(int i) { return (*this)[i]; }
T const &operator()(int i) const { return (*this)[i]; }
void init(ll n, T v = T()) {
this->clear();
this->resize(n, v);
}
ll sz() const { return (ll)this->size(); }
void pb(T v) { this->push_back(v); }
void sort() { std::sort(this->begin(), this->end()); }
void sort(function<bool(T, T)> p) {
std::sort(this->begin(), this->end(), p);
}
void rsort() { std::sort(this->begin(), this->end(), greater<T>()); }
void reverse() { std::reverse(this->begin(), this->end()); }
void unique_erase() {
this->erase(std::unique(this->begin(), this->end()), this->end());
}
bool next_permutation() {
return std::next_permutation(this->begin(), this->end());
}
// これ以下はソート済み前提
int lower_bound(T const &v, function<bool(T, T)> p) {
return std::lower_bound(this->begin(), this->end(), v, p) - this->begin();
}
int lower_bound(T const &v) {
return std::lower_bound(this->begin(), this->end(), v) - this->begin();
}
int upper_bound(T const &v, function<bool(T, T)> p) {
return std::upper_bound(this->begin(), this->end(), v, p) - this->begin();
}
int upper_bound(T const &v) {
return std::upper_bound(this->begin(), this->end(), v) - this->begin();
}
int find_nearest(T const &v) {
int i = this->lower_bound(v);
if (i >= sz()) {
--i;
} else if ((*this)[i] != v) {
int p = i - 1;
if (p >= 0) {
int id = abs((*this)[i] - v);
int pd = abs((*this)[p] - v);
if (pd < id) {
i = p;
}
}
}
return i;
}
// 見つからなければ-1
int find(T const &v) {
int i = this->lower_bound(v);
if (i >= sz()) {
return -1;
}
if ((*this)[i] != v) {
return -1;
}
return i;
}
};
using ints = arr<ll>;
template <class T> struct que : public queue<T> {
ll sz() const { return (ll)this->size(); }
T popfront() {
T v = this->front();
this->pop();
return v;
}
};
template <class A, class B> struct pr {
union {
A a;
A key;
A first;
A x;
};
union {
B b;
B value;
B second;
B y;
};
pr() : a(A()), b(B()){};
pr(A a_, B b_) : a(a_), b(b_) {}
pr(pr const &r) : a(r.a), b(r.b){};
pr(pair<A, B> const &r) : a(r.first), b(r.second){};
bool operator==(pr const &r) const { return a == r.a && b == r.b; }
bool operator!=(pr const &r) const { return !((*this) == r); }
bool operator<(pr const &r) const {
if (a == r.a) {
return b < r.b;
}
return a < r.a;
}
pr operator+(pr v) const { return pr(x, y) += v; }
pr operator-(pr v) const { return pr(x, y) -= v; }
pr &operator+=(pr v) {
x += v.x;
y += v.y;
return *this;
}
pr &operator-=(pr v) {
x -= v.x;
y -= v.y;
return *this;
}
void flip() { swap(x, y); }
friend istream &operator>>(istream &is, pr &p) {
is >> p.a >> p.b;
return is;
}
friend ostream &operator<<(ostream &os, pr const &p) {
os << p.a << " " << p.b;
return os;
}
};
using pint = pr<ll, ll>;
using pints = arr<pint>;
template <class K, class V> struct dic : public map<K, V> {
bool get(K const &k, V *v) {
auto it = this->find(k);
if (it != this->end()) {
*v = it->second;
return true;
}
return false;
}
};
template <class T> struct arr2 {
vector<vector<T>> m_vec;
int m_width;
int m_height;
arr2() : m_width(0), m_height(0) {}
arr2(int w, int h, T const &value = T()) : m_width(w), m_height(h) {
m_vec.resize(h, vector<T>(w, value));
}
arr2(arr2 const &r) {
m_vec = r.m_vec;
m_width = r.m_width;
m_height = r.m_height;
}
arr2(arr2 &&r) {
m_vec = move(r.m_vec);
m_width = r.m_width;
m_height = r.m_height;
}
arr2 &operator=(arr2 const &r) {
m_vec = r.m_vec;
m_width = r.m_width;
m_height = r.m_height;
return *this;
}
arr2 &operator=(arr2 &&r) {
m_vec = move(r.m_vec);
m_width = r.m_width;
m_height = r.m_height;
return *this;
}
bool operator==(arr2 const &r) const { return m_vec = r.m_vec; }
bool operator<(arr2 const &r) const {
if (m_width != r.m_width) {
return m_width < r.m_width;
}
if (m_height != r.m_height) {
return m_height < r.m_height;
}
REP(y, m_height) {
REP(x, m_width) {
if ((*this)(x, y) != r(x, y)) {
return (*this)(x, y) < r(x, y);
}
}
}
return false;
}
pint size() const { return pint(m_width, m_height); }
int width() const { return m_width; }
int height() const { return m_height; }
void init(int w, int h, T const &value = T()) {
m_vec.clear();
m_vec.resize(h, vector<T>(w, value));
m_width = w;
m_height = h;
}
void init(pint size, T const &value = T()) { init(size.x, size.y, value); }
T &operator()(int x, int y) { return m_vec[y][x]; }
T const &operator()(int x, int y) const { return m_vec[y][x]; }
T &operator()(pint p) { return m_vec[p.y][p.x]; }
T const &operator()(pint p) const { return m_vec[p.y][p.x]; }
T &operator[](pint p) { return m_vec[p.y][p.x]; }
T const &operator[](pint p) const { return m_vec[p.y][p.x]; }
bool isIn(int x, int y) const {
return x >= 0 && x < m_width && y >= 0 && y < m_height;
}
bool isIn(pint p) const { return isIn(p.x, p.y); }
bool isOut(int x, int y) const {
return x < 0 || x >= m_width || y < 0 || y >= m_height;
}
bool isOut(pint p) const { return isOut(p.x, p.y); }
struct iterator {
private:
arr2<T> *owner;
public:
pint pt;
iterator(arr2<T> *owner_, pint pt_) : owner(owner_), pt(pt_) {}
bool operator==(iterator const &r) const { return pt == r.pt; }
bool operator!=(iterator const &r) const { return !((*this) == r); }
void operator++() {
++pt.x;
if (pt.x >= owner->width()) {
++pt.y;
pt.x = 0;
}
}
T &operator*() { return (*owner)(pt); }
};
iterator begin() { return iterator(this, pint(0, 0)); }
iterator end() { return iterator(this, pint(0, height())); }
void disp(ostream &os) {
REP(y, m_height) {
REP(x, m_width) { os << setw(2) << (*this)(x, y) << " "; }
os << endl;
}
os << endl;
}
};
const pints around4 = {pint(-1, 0), pint(0, -1), pint(1, 0), pint(0, 1)};
//------------------------------------------------------------
template <class T> void chmin(T &a, T b) {
if (b < a) {
a = b;
}
}
template <class T> void chmax(T &a, T b) {
if (b > a) {
a = b;
}
}
constexpr int gcd(int a, int b) {
if (a < 0) {
a = -a;
}
if (b < 0) {
b = -b;
}
if (a == 0) {
return b;
}
if (b == 0) {
return a;
}
while (int c = a % b) {
a = b;
b = c;
}
return b;
}
constexpr int lcm(int a, int b) { return a * b / gcd(a, b); }
//------------------------------------------------------------
template <int M> struct modint {
int raw;
modint() { raw = 0; }
modint(int v) {
if (v < 0) {
raw = (v % M) + M;
} else if (v >= M) {
raw = v % M;
} else {
raw = v;
}
}
modint operator+(modint v) const { return modint(raw) += v; }
modint operator-(modint v) const { return modint(raw) -= v; }
modint operator*(modint v) const { return modint(raw) *= v; }
modint &operator+=(modint v) {
raw += v.raw;
if (raw >= M) {
raw -= M;
}
return *this;
}
modint &operator-=(modint v) {
raw -= v.raw;
if (raw < 0) {
raw += M;
}
return *this;
}
modint &operator*=(modint v) {
raw = (raw * v.raw) % M;
return *this;
}
modint pow(int n) const { return modint::pow(raw, n); }
static modint pow(int a, int n) {
if (n < 0) {
// not support
abort();
}
int r = 1;
while (n) {
if (n & 1) {
r = (r * a) % M;
}
a = (a * a) % M;
n >>= 1;
}
return modint(r);
}
modint inv() const {
int a = raw;
int b = M;
int u = 1;
int v = 0;
while (b) {
int t = a / b;
a -= t * b;
u -= t * v;
swap(a, b);
swap(u, v);
}
u %= M;
if (u < 0) {
u += M;
}
return u;
}
friend istream &operator>>(istream &is, modint &m) {
int v;
is >> v;
m = modint(v);
return is;
}
friend ostream &operator<<(ostream &os, modint const &m) {
return os << m.raw;
}
};
using mint = modint<MOD>;
using mints = arr<mint>;
//------------------------------------------------------------
#if defined(TEST)
extern istream &mis;
extern ostream &mos;
#else
istream &mis = cin;
ostream &mos = cout;
#endif
//------------------------------------------------------------
struct OutputStream {
template <class T>
friend OutputStream &operator<<(OutputStream &s, T const &v) {
mos << v << '\n';
return s;
}
} out;
struct PrintStream {
stringstream ss;
template <class T> friend PrintStream &operator<<(PrintStream &s, T &&v) {
s.ss << v << ' ';
return s;
}
PrintStream &operator<<(PrintStream &(*manip)(PrintStream &)) {
return (*manip)(*this);
}
} prn;
PrintStream &endl(PrintStream &s) {
string str = s.ss.str();
s.ss = stringstream();
if (str.empty() == false) {
str.pop_back();
}
mos << str << '\n';
return s;
}
//------------------------------------------------------------
struct flip_t {
} flip;
template <class T> struct in_base {
T value;
in_base() { mis >> value; }
operator T() { return value; }
};
struct in_int {
int value;
in_int() { mis >> value; }
in_int(int add) {
mis >> value;
value += add;
}
operator int() { return value; }
};
template <class A, class B> struct in_pr {
pr<A, B> value;
in_pr() { mis >> value.a >> value.b; }
in_pr(int addA, int addB) {
mis >> value.a >> value.b;
value.a += addA;
value.b += addB;
}
in_pr(flip_t) { mis >> value.b >> value.a; }
in_pr(int addA, int addB, flip_t) {
mis >> value.b >> value.a;
value.a += addB;
value.b += addA;
}
operator pr<A, B>() { return value; }
};
template <class T> struct in_arr {
arr<T> value;
in_arr(int N) {
value.init(N);
REP(i, N) { mis >> value[i]; }
}
in_arr(int N, int add) {
value.init(N);
REP(i, N) {
mis >> value[i];
value[i] += add;
}
}
operator arr<T>() { return value; }
};
template <> struct in_arr<pint> {
arr<pint> value;
in_arr(int N) {
value.init(N);
REP(i, N) { mis >> value[i]; }
}
in_arr(int N, int addA, int addB) {
value.init(N);
REP(i, N) {
mis >> value[i].a >> value[i].b;
value[i].a += addA;
value[i].b += addB;
}
}
in_arr(int N, flip_t) {
value.init(N);
REP(i, N) { mis >> value[i].b >> value[i].a; }
}
in_arr(int N, int addA, int addB, flip_t) {
value.init(N);
REP(i, N) {
mis >> value[i].b >> value[i].a;
value[i].a += addB;
value[i].b += addA;
}
}
operator arr<pint>() { return value; }
};
template <class A, class B> struct in_dic {
dic<A, B> value;
in_dic(int N) {
REP(i, N) {
pair<A, B> pr;
mis >> pr.first >> pr.second;
value.insert(pr);
}
}
in_dic(int N, int addA, int addB) {
REP(i, N) {
pair<A, B> pr;
mis >> pr.first >> pr.second;
pr.first += addA;
pr.second += addB;
value.insert(pr);
}
}
in_dic(int N, flip_t) {
REP(i, N) {
pair<A, B> pr;
mis >> pr.second >> pr.first;
value.insert(pr);
}
}
in_dic(int N, int addA, int addB, flip_t) {
REP(i, N) {
pair<A, B> pr;
mis >> pr.second >> pr.first;
pr.first += addB;
pr.second += addA;
value.insert(pr);
}
}
operator dic<A, B>() { return value; }
};
template <class T> struct in_arr2 {
arr2<T> value;
in_arr2(int H, int W) {
value.init(W, H);
REP(y, H) {
REP(x, W) { mis >> value(x, y); }
}
}
in_arr2(int H, int W, int add) {
value.init(W, H);
REP(y, H) {
REP(x, W) {
mis >> value(x, y);
value(x, y) += add;
}
}
}
operator arr2<T>() { return value; }
};
template <class T, class Tuple, size_t... Index>
T MakeFromTupleImpl(Tuple &&t, index_sequence<Index...>) {
return T(get<Index>(forward<Tuple>(t))...);
}
template <class T, class Tuple> T MakeFromTuple(Tuple &&t) {
return MakeFromTupleImpl<T>(
forward<Tuple>(t),
make_index_sequence<tuple_size<remove_reference_t<Tuple>>::value>{});
}
template <class... ARGS> struct inputWithParam {
tuple<ARGS...> param;
inputWithParam() {}
explicit inputWithParam(tuple<ARGS...> &¶m_) : param(param_) {}
operator int() { return MakeFromTuple<in_int>(param); }
operator double() { return MakeFromTuple<in_base<double>>(param); }
operator string() { return MakeFromTuple<in_base<string>>(param); }
operator char() { return MakeFromTuple<in_base<char>>(param); }
template <class A, class B> operator pr<A, B>() {
return MakeFromTuple<in_pr<A, B>>(param);
}
template <class T> operator arr<T>() {
return MakeFromTuple<in_arr<T>>(param);
}
template <class A, class B> operator dic<A, B>() {
return MakeFromTuple<in_dic<A, B>>(param);
}
template <class T> operator arr2<T>() {
return MakeFromTuple<in_arr2<T>>(param);
}
};
struct input {
operator int() { return inputWithParam<>(); }
operator double() { return inputWithParam<>(); }
operator string() { return inputWithParam<>(); }
operator char() { return inputWithParam<>(); }
template <class A, class B> operator pr<A, B>() { return inputWithParam<>(); }
template <class... ARGS> inputWithParam<ARGS...> operator()(ARGS &&...args) {
inputWithParam<ARGS...> withParam(forward_as_tuple(args...));
return withParam;
}
} in;
//------------------------------------------------------------
#endif
struct MyMain {
int N = in;
arr<double> S = in(N);
struct Rational {
double original;
int child;
int parent;
int c2;
int c5;
};
int CalcC2(int v) {
for (int i = 0;; ++i) {
if (v % 2 != 0) {
return i;
}
v /= 2;
}
}
int CalcC5(int v) {
for (int i = 0;; ++i) {
if (v % 5 != 0) {
return i;
}
v /= 5;
}
}
void Run() {
int offset = 100;
int extendSize = offset * 2 + 1;
arr<Rational> A;
A.reserve(N);
REP(i, N) {
double real = S[i];
int child = (int)floor(real * 1000000000 + 0.5);
int parent = 1000000000;
int g = gcd(child, parent);
child /= g;
parent /= g;
Rational r;
r.original = real;
r.child = child;
r.parent = parent;
r.c2 = CalcC2(child) - CalcC2(parent);
r.c5 = CalcC5(child) - CalcC5(parent);
A.push_back(r);
assert(r.c2 < offset);
assert(r.c5 < offset);
assert(r.c2 > -offset);
assert(r.c5 > -offset);
}
arr2<int> counts(extendSize, extendSize);
REP(i, N) { counts(A[i].c2 + offset, A[i].c5 + offset) += 1; }
int ans = 0;
REP(y, extendSize) {
int c5 = y - offset;
int ly = -c5 + offset;
REP(x, extendSize) {
int c2 = x - offset;
int lx = -c2 + offset;
int bc = counts(x, y);
if (bc == 0) {
continue;
}
FOR(v, ly, extendSize) {
FOR(h, lx, extendSize) {
if (h == x && v == y) {
continue;
}
int sc = counts(h, v);
if (sc == 0) {
continue;
}
ans += sc * bc;
}
}
}
}
ans /= 2;
REP(y, extendSize) {
int c5 = y - offset;
int ly = -c5 + offset;
REP(x, extendSize) {
int c2 = x - offset;
int lx = -c2 + offset;
int bc = counts(x, y);
if (bc == 0) {
continue;
}
if (c2 >= 0 && c5 >= 0) {
ans += bc * (bc - 1) / 2;
}
}
}
out << ans;
}
};
| replace | 711 | 712 | 711 | 712 | 0 | |
p02588 | C++ | Runtime Error | /*
`-:://:::-
`//:-------:/:`
.+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.o:--...```..---+/`
`/y+o/---....---:+o.
`...````-os+/:---:/+o/--.`
`-/+++++/:. `...` :h+d+oooo+/+-` ...
`/++//:::://++-`....` -.`//````````:` `..`
`o+/::------://o/` `-` -. -` `..`
`---.-o/:./o/::-..``..-ЗАПУСКАЕМ .. .. -` `... ``..``
`....o+:-++/:--.```..-://s. `-` .- -` `-o:
.-//::::/:-`
`:s+/:--....-::/+s-` .- `- -` -///:--------:/:`
./s+//:::::://oo-``..НЕЙРОННУЮ: СЕТЬ:::::::-`РАБОТЯГИ
`+:--........--:/`
.:ooo+++++osso-` `.:-...`/` ./::-------:/:` -`
:+--..``````.--:+:...-+:-`
`.-/+++++/+-.-` -. ``:so:/:--.......--:+`
`-```````o+/+--..`````..--:o/-..:s+:.
```````:``.. `-` -`
`+:--..`````..--/+-.../.`````..-o:--.......---/o. `
`: `:- -. .o:--..`` ``..--:o` `-`
`:o+:--------:+o-`
`-`-... .. .o/--...```..--:+/` `-`
`oy/so/////++o/.`
-/` `-` `- ``+s/o/:---...---:++. `-`
.-../d://///:-.`
`.---..``-..- .-/..`````-oo+/:::::/+o+- `-``-` `-.
````
`:++++/+++++- ..``.-/:` /y-:/++o++/:.`..` ./. `-
-++/::::::://+/..:-``:` .. `-.` ```.``` `..` `..`-` `-
`` -o//:--....-::/++` -.-` `-`.-` `..`..` `-.-
-----ss+:++/:--.```..-://s. /. `:: `-:. ./`
`````/:..+o/::-..``.--:/+s. ..-` `-``-` ..` `-` `-`-`
`-s+/::-----::/+oo---``-` .. .:- ``` .-` .-.- `-`
`:oo+//::://+os/..:`..-/:` :y.-:::::::.`.-` ./-` `-`
`./+oooooooo+/.`- .-:...`.. .//:-------://` `- `..` `:.
``.-::::-.``-/` `-` `- `oo:+:--.......--:/` `-
`.:--h.``..```
-.-`.- .- `+:--..`````..--//` `-
/s-//::::::::.
-` `/- .. .o:--..`` ``..--:o.```.-
`//:--------://`
-` .-`.-`
-.`-o/--...```..--:+/.``-:....``:-.+:--....`...--:+`
..`-. `-. ``:os:o/:---...---:++. `-
``///+:-..``````.--:+-````-.`
`.:///////.-` .:-..` -``-+o+/:::::/+o/. `-
`:+:-..`````..--:o/:--/ys+-
`-++///////+o/. ``....`-. :` `.:++++++/:.` .- -o/---......---/o.
`.`
`++//:-----::/+o:..` .-` : ``````` .- `+so+:--------:++-`
`````:-``:o/::-..`..--:/+o` -. `- .- `../../+o+////+o+:.`
-----syo/o+/:--.```..-://s. .-` `- .- `...
``-:////:-``
.` `/s//:--....-::/+s. -. `-` .- `..`
.+o+/:::--:://+s/-..` .::+y ``` .- `..`
./oo++////+oso-` `.... :y-+:::::::/` ...
`.:+oooooo/-` `....-. .//:-------:/:-.`
``...`` /+:+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.+/--...```..--:+/`
`-o/:---...---:++.
`-+o+/:---:/+o/.
`.:+oooo+/-.`
``````
*/
#ifdef aimbot
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <immintrin.h>
#endif
#define hur(f, g) \
template <class c> int f(c a) { \
if (sizeof(c) == 8) \
return g##ll(a); \
else \
return g(a); \
}
hur(popc, __builtin_popcount) hur(ctz, __builtin_ctz) hur(clz, __builtin_clz)
/*
- place bitset modifications here
*/
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <limits>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define random escape__from__random__aetuhoetnuhshe
#define mt make_tuple
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define le(v) ((int)v.size())
#define f(i, n) for (int i = 0; i < (n); i++)
#define rof(i, n) for (int i = ((n)-1); i >= 0; i--)
#define apply(v, act) \
for (auto &x : v) { \
act; \
}
#define log(args...) \
{ \
string s = #args; \
deque<string> deq; \
string buf = ""; \
int bal = 0; \
for (char c : s) { \
if (c == '(' || c == '[' || c == '{') { \
bal++; \
} else if (c == ')' || c == ']' || c == '}') { \
bal--; \
} else { \
if (bal == 0) { \
if (c == ',') { \
deq.pb(buf); \
buf = ""; \
} else { \
if (c != ' ') { \
buf += c; \
} \
} \
} \
} \
} \
if (!buf.empty()) { \
deq.pb(buf); \
} \
smart_io::precall_print(); \
smart_io::_print(deq, args); \
}
inline int min(const int &x, const int &y) {
return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x;
}
inline int max(const int &x, const int &y) {
return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y;
}
inline long long min(const long long &x, const long long &y) {
return (((y - x) >> (64 - 1)) & (x ^ y)) ^ x;
}
inline long long max(const long long &x, const long long &y) {
return (((y - x) >> (64 - 1)) & (x ^ y)) ^ y;
}
#define print \
smart_io::precall_print(); \
cout,
#define scan cin,
#ifdef fast_allocator
const int MAXMEM = 200 * 1000 * 1024;
char _memory[MAXMEM];
size_t _ptr = 0;
void *operator new(size_t _x) {
_ptr += _x;
assert(_ptr < MAXMEM);
return _memory + _ptr - _x;
}
void operator delete(void *) noexcept {}
#endif
using namespace std;
char string_in_buffer[(int)260];
void fast_scan(int &x) { scanf("%d", &x); }
void fast_scan(long long &x) { scanf("%lld", &x); }
void fast_scan(unsigned long long &x) { scanf("%llu", &x); }
void fast_scan(double &x) { scanf("%lf", &x); }
void fast_scan(long double &x) { scanf("%Lf", &x); }
void fast_scan(char &x) {
scanf("%c", &x);
if (x == '\n') {
fast_scan(x);
}
}
void fast_scan(string &x) {
scanf("%s", string_in_buffer);
x = string(string_in_buffer);
}
template <class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
fast_scan(p.first);
fast_scan(p.second);
}
template <class T> void fast_scan(vector<T> &v) {
for (auto &x : v)
fast_scan(x);
}
void fast_print(const int &x) { printf("%d", x); }
void fast_print(const unsigned int &x) { printf("%u", x); }
void fast_print(const long long &x) { printf("%lld", x); }
void fast_print(const unsigned long long &x) { printf("%llu", x); }
void fast_print(const char &x) { printf("%c", x); };
// void fast_print(__int128 x) {
// if (x == 0) { fast_print('0'); return; }
// if (x < 0) {
// fast_print('-');
// x = -x;
// }
// __int128 p = 1;
// while (x / (p * 10)) p *= 10;
// while (p) {
// __int128 symb = x / p;
// fast_print((int)symb);
// x -= p * symb;
// p /= 10;
// }
// };
void fast_print(const double &x) { printf("%.15lf", x); }
void fast_print(const long double &x) { printf("%.15Lf", x); }
void fast_print(const string &x) { printf("%s", x.c_str()); }
void fast_print(const char v[]) { fast_print((string)v); }
template <class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
fast_print(p.first);
fast_print(' ');
fast_print(p.second);
}
template <class T> void fast_print(const vector<T> &v) {
if (v.empty())
return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print(' ');
fast_print(v[i]);
}
}
template <class T> void fast_print(const vector<vector<T>> &v) {
if (v.empty())
return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print('\n');
fast_print(v[i]);
}
}
template <class T> void fast_print(const T &v) {
for (const auto &x : v) {
fast_print(x);
fast_print(' ');
}
}
using namespace std;
namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;
void precall_print() {
fast_print(print_start);
print_start = "\n";
first_print = true;
}
void _print(deque<string>) {}
template <class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
if (!first_print) {
fast_print("\n");
} else {
first_print = false;
}
fast_print(names.front());
fast_print(" = ");
fast_print(elem);
names.pop_front();
_print(names, args...);
}
} // namespace smart_io
template <class T> ostream &operator,(ostream &os, const T &object) {
if (!smart_io::first_print) {
fast_print(smart_io::sep);
} else {
smart_io::first_print = false;
}
fast_print(object);
return os;
}
template <class T> istream &operator,(istream &is, T &object) {
fast_scan(object);
return is;
}
namespace random {
using namespace std::chrono;
mt19937 rng(duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count());
uniform_real_distribution<> prob_dist(0.0, 1.0);
}; // namespace random
namespace typedefs {
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long double ld;
} // namespace typedefs
/*
check every int.
check every array bound.
*/
namespace numbers_operation {
template <class T> inline T floor_mod(T a, const T &b) {
a %= b;
if (a < 0)
a += b;
return a;
}
} // namespace numbers_operation
using namespace numbers_operation;
using namespace typedefs;
using namespace random;
int n;
vector<int> div1[20][20];
signed main(signed argc, char *argv[]) {
scan n;
vector<tuple<int, int, int>> v;
f(i, n) {
string s;
scan s;
int div = 0;
for (int j = le(s) - 1; j >= 0; j--) {
if (s[j] == '.') {
div = le(s) - j - 1;
s.erase(s.begin() + j);
}
}
ll x = stoll(s);
int two = 0;
int five = 0;
while (x % 2 == 0) {
x /= 2;
two++;
}
while (x % 5 == 0) {
x /= 5;
five++;
}
// {div, two, five}
v.emplace_back(div, two, five);
}
sort(v.begin(), v.end());
ll rez = 0;
f(i, n) {
int div, two, five;
tie(div, two, five) = v[i];
for (int two1 = 0; two1 < 20; two1++) {
for (int five1 = 0; five1 < 20; five1++) {
int X = min(two + two1, five + five1);
rez += upper_bound(div1[two1][five1].begin(), div1[two1][five1].end(),
X - div) -
div1[two1][five1].begin();
}
}
div1[two][five].pb(div);
}
print rez;
} | /*
`-:://:::-
`//:-------:/:`
.+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.o:--...```..---+/`
`/y+o/---....---:+o.
`...````-os+/:---:/+o/--.`
`-/+++++/:. `...` :h+d+oooo+/+-` ...
`/++//:::://++-`....` -.`//````````:` `..`
`o+/::------://o/` `-` -. -` `..`
`---.-o/:./o/::-..``..-ЗАПУСКАЕМ .. .. -` `... ``..``
`....o+:-++/:--.```..-://s. `-` .- -` `-o:
.-//::::/:-`
`:s+/:--....-::/+s-` .- `- -` -///:--------:/:`
./s+//:::::://oo-``..НЕЙРОННУЮ: СЕТЬ:::::::-`РАБОТЯГИ
`+:--........--:/`
.:ooo+++++osso-` `.:-...`/` ./::-------:/:` -`
:+--..``````.--:+:...-+:-`
`.-/+++++/+-.-` -. ``:so:/:--.......--:+`
`-```````o+/+--..`````..--:o/-..:s+:.
```````:``.. `-` -`
`+:--..`````..--/+-.../.`````..-o:--.......---/o. `
`: `:- -. .o:--..`` ``..--:o` `-`
`:o+:--------:+o-`
`-`-... .. .o/--...```..--:+/` `-`
`oy/so/////++o/.`
-/` `-` `- ``+s/o/:---...---:++. `-`
.-../d://///:-.`
`.---..``-..- .-/..`````-oo+/:::::/+o+- `-``-` `-.
````
`:++++/+++++- ..``.-/:` /y-:/++o++/:.`..` ./. `-
-++/::::::://+/..:-``:` .. `-.` ```.``` `..` `..`-` `-
`` -o//:--....-::/++` -.-` `-`.-` `..`..` `-.-
-----ss+:++/:--.```..-://s. /. `:: `-:. ./`
`````/:..+o/::-..``.--:/+s. ..-` `-``-` ..` `-` `-`-`
`-s+/::-----::/+oo---``-` .. .:- ``` .-` .-.- `-`
`:oo+//::://+os/..:`..-/:` :y.-:::::::.`.-` ./-` `-`
`./+oooooooo+/.`- .-:...`.. .//:-------://` `- `..` `:.
``.-::::-.``-/` `-` `- `oo:+:--.......--:/` `-
`.:--h.``..```
-.-`.- .- `+:--..`````..--//` `-
/s-//::::::::.
-` `/- .. .o:--..`` ``..--:o.```.-
`//:--------://`
-` .-`.-`
-.`-o/--...```..--:+/.``-:....``:-.+:--....`...--:+`
..`-. `-. ``:os:o/:---...---:++. `-
``///+:-..``````.--:+-````-.`
`.:///////.-` .:-..` -``-+o+/:::::/+o/. `-
`:+:-..`````..--:o/:--/ys+-
`-++///////+o/. ``....`-. :` `.:++++++/:.` .- -o/---......---/o.
`.`
`++//:-----::/+o:..` .-` : ``````` .- `+so+:--------:++-`
`````:-``:o/::-..`..--:/+o` -. `- .- `../../+o+////+o+:.`
-----syo/o+/:--.```..-://s. .-` `- .- `...
``-:////:-``
.` `/s//:--....-::/+s. -. `-` .- `..`
.+o+/:::--:://+s/-..` .::+y ``` .- `..`
./oo++////+oso-` `.... :y-+:::::::/` ...
`.:+oooooo/-` `....-. .//:-------:/:-.`
``...`` /+:+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.+/--...```..--:+/`
`-o/:---...---:++.
`-+o+/:---:/+o/.
`.:+oooo+/-.`
``````
*/
#ifdef aimbot
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <immintrin.h>
#endif
#define hur(f, g) \
template <class c> int f(c a) { \
if (sizeof(c) == 8) \
return g##ll(a); \
else \
return g(a); \
}
hur(popc, __builtin_popcount) hur(ctz, __builtin_ctz) hur(clz, __builtin_clz)
/*
- place bitset modifications here
*/
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <limits>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define random escape__from__random__aetuhoetnuhshe
#define mt make_tuple
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define le(v) ((int)v.size())
#define f(i, n) for (int i = 0; i < (n); i++)
#define rof(i, n) for (int i = ((n)-1); i >= 0; i--)
#define apply(v, act) \
for (auto &x : v) { \
act; \
}
#define log(args...) \
{ \
string s = #args; \
deque<string> deq; \
string buf = ""; \
int bal = 0; \
for (char c : s) { \
if (c == '(' || c == '[' || c == '{') { \
bal++; \
} else if (c == ')' || c == ']' || c == '}') { \
bal--; \
} else { \
if (bal == 0) { \
if (c == ',') { \
deq.pb(buf); \
buf = ""; \
} else { \
if (c != ' ') { \
buf += c; \
} \
} \
} \
} \
} \
if (!buf.empty()) { \
deq.pb(buf); \
} \
smart_io::precall_print(); \
smart_io::_print(deq, args); \
}
inline int min(const int &x, const int &y) {
return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x;
}
inline int max(const int &x, const int &y) {
return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y;
}
inline long long min(const long long &x, const long long &y) {
return (((y - x) >> (64 - 1)) & (x ^ y)) ^ x;
}
inline long long max(const long long &x, const long long &y) {
return (((y - x) >> (64 - 1)) & (x ^ y)) ^ y;
}
#define print \
smart_io::precall_print(); \
cout,
#define scan cin,
#ifdef fast_allocator
const int MAXMEM = 200 * 1000 * 1024;
char _memory[MAXMEM];
size_t _ptr = 0;
void *operator new(size_t _x) {
_ptr += _x;
assert(_ptr < MAXMEM);
return _memory + _ptr - _x;
}
void operator delete(void *) noexcept {}
#endif
using namespace std;
char string_in_buffer[(int)260];
void fast_scan(int &x) { scanf("%d", &x); }
void fast_scan(long long &x) { scanf("%lld", &x); }
void fast_scan(unsigned long long &x) { scanf("%llu", &x); }
void fast_scan(double &x) { scanf("%lf", &x); }
void fast_scan(long double &x) { scanf("%Lf", &x); }
void fast_scan(char &x) {
scanf("%c", &x);
if (x == '\n') {
fast_scan(x);
}
}
void fast_scan(string &x) {
scanf("%s", string_in_buffer);
x = string(string_in_buffer);
}
template <class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
fast_scan(p.first);
fast_scan(p.second);
}
template <class T> void fast_scan(vector<T> &v) {
for (auto &x : v)
fast_scan(x);
}
void fast_print(const int &x) { printf("%d", x); }
void fast_print(const unsigned int &x) { printf("%u", x); }
void fast_print(const long long &x) { printf("%lld", x); }
void fast_print(const unsigned long long &x) { printf("%llu", x); }
void fast_print(const char &x) { printf("%c", x); };
// void fast_print(__int128 x) {
// if (x == 0) { fast_print('0'); return; }
// if (x < 0) {
// fast_print('-');
// x = -x;
// }
// __int128 p = 1;
// while (x / (p * 10)) p *= 10;
// while (p) {
// __int128 symb = x / p;
// fast_print((int)symb);
// x -= p * symb;
// p /= 10;
// }
// };
void fast_print(const double &x) { printf("%.15lf", x); }
void fast_print(const long double &x) { printf("%.15Lf", x); }
void fast_print(const string &x) { printf("%s", x.c_str()); }
void fast_print(const char v[]) { fast_print((string)v); }
template <class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
fast_print(p.first);
fast_print(' ');
fast_print(p.second);
}
template <class T> void fast_print(const vector<T> &v) {
if (v.empty())
return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print(' ');
fast_print(v[i]);
}
}
template <class T> void fast_print(const vector<vector<T>> &v) {
if (v.empty())
return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print('\n');
fast_print(v[i]);
}
}
template <class T> void fast_print(const T &v) {
for (const auto &x : v) {
fast_print(x);
fast_print(' ');
}
}
using namespace std;
namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;
void precall_print() {
fast_print(print_start);
print_start = "\n";
first_print = true;
}
void _print(deque<string>) {}
template <class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
if (!first_print) {
fast_print("\n");
} else {
first_print = false;
}
fast_print(names.front());
fast_print(" = ");
fast_print(elem);
names.pop_front();
_print(names, args...);
}
} // namespace smart_io
template <class T> ostream &operator,(ostream &os, const T &object) {
if (!smart_io::first_print) {
fast_print(smart_io::sep);
} else {
smart_io::first_print = false;
}
fast_print(object);
return os;
}
template <class T> istream &operator,(istream &is, T &object) {
fast_scan(object);
return is;
}
namespace random {
using namespace std::chrono;
mt19937 rng(duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count());
uniform_real_distribution<> prob_dist(0.0, 1.0);
}; // namespace random
namespace typedefs {
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long double ld;
} // namespace typedefs
/*
check every int.
check every array bound.
*/
namespace numbers_operation {
template <class T> inline T floor_mod(T a, const T &b) {
a %= b;
if (a < 0)
a += b;
return a;
}
} // namespace numbers_operation
using namespace numbers_operation;
using namespace typedefs;
using namespace random;
int n;
vector<int> div1[20][20];
signed main(signed argc, char *argv[]) {
scan n;
vector<tuple<int, int, int>> v;
f(i, n) {
string s;
scan s;
int div = 0;
for (int j = le(s) - 1; j >= 0; j--) {
if (s[j] == '.') {
div = le(s) - j - 1;
s.erase(s.begin() + j);
}
}
ll x = stoll(s);
int two = 0;
int five = 0;
while (x % 2 == 0) {
x /= 2;
two++;
}
while (x % 5 == 0) {
x /= 5;
five++;
}
// {div, two, five}
two = min(two, 19);
five = min(five, 19);
v.emplace_back(div, two, five);
}
sort(v.begin(), v.end());
ll rez = 0;
f(i, n) {
int div, two, five;
tie(div, two, five) = v[i];
for (int two1 = 0; two1 < 20; two1++) {
for (int five1 = 0; five1 < 20; five1++) {
int X = min(two + two1, five + five1);
rez += upper_bound(div1[two1][five1].begin(), div1[two1][five1].end(),
X - div) -
div1[two1][five1].begin();
}
}
div1[two][five].pb(div);
}
print rez;
} | insert | 387 | 387 | 387 | 389 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define pb push_back
#define pi pair<int, int>
#define l first
#define r second
#define all(x) x.begin(), x.end()
#define fori(a, b, step) for (int i = a; i < b; i += step)
#define forj(a, b, step) for (int j = a; j < b; j += step)
#define int long long
const int maxn = 1e5 + 1, mod = 1e9 + 7;
using namespace std;
int n;
int a[maxn];
int c2[maxn], c5[maxn];
int cnt[65][25];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
string ca;
cin >> ca;
int dc = 9, bf = 0;
for (int j = 0; j < ca.size(); j++) {
if (ca[j] == '.') {
bf = 1;
continue;
}
a[i] = 10 * a[i] + ca[j] - '0';
if (bf)
--dc;
}
for (; dc; --dc, a[i] *= 10)
;
for (int x = a[i]; x % 2 == 0; x /= 2, ++c2[i])
;
for (int x = a[i]; x % 5 == 0; x /= 5, ++c5[i])
;
++cnt[c2[i]][c5[i]];
}
int ans = 0;
for (int i = 0; i < 65; i++)
for (int j = 0; j < 25; j++) {
if (!cnt[i][j])
continue;
for (int l = max(0ll, 18 - i); l < 65; l++)
for (int k = max(0ll, 18 - j); k < 25; k++) {
if (!cnt[l][k])
continue;
if (i == l && j == k) {
ans += cnt[i][j] * (cnt[i][j] - 1);
continue;
}
ans += cnt[i][j] * cnt[l][k];
}
}
cout << ans / 2;
return 0;
}
| #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define pb push_back
#define pi pair<int, int>
#define l first
#define r second
#define all(x) x.begin(), x.end()
#define fori(a, b, step) for (int i = a; i < b; i += step)
#define forj(a, b, step) for (int j = a; j < b; j += step)
#define int long long
const int maxn = 2e5 + 1, mod = 1e9 + 7;
using namespace std;
int n;
int a[maxn];
int c2[maxn], c5[maxn];
int cnt[65][25];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
string ca;
cin >> ca;
int dc = 9, bf = 0;
for (int j = 0; j < ca.size(); j++) {
if (ca[j] == '.') {
bf = 1;
continue;
}
a[i] = 10 * a[i] + ca[j] - '0';
if (bf)
--dc;
}
for (; dc; --dc, a[i] *= 10)
;
for (int x = a[i]; x % 2 == 0; x /= 2, ++c2[i])
;
for (int x = a[i]; x % 5 == 0; x /= 5, ++c5[i])
;
++cnt[c2[i]][c5[i]];
}
int ans = 0;
for (int i = 0; i < 65; i++)
for (int j = 0; j < 25; j++) {
if (!cnt[i][j])
continue;
for (int l = max(0ll, 18 - i); l < 65; l++)
for (int k = max(0ll, 18 - j); k < 25; k++) {
if (!cnt[l][k])
continue;
if (i == l && j == k) {
ans += cnt[i][j] * (cnt[i][j] - 1);
continue;
}
ans += cnt[i][j] * cnt[l][k];
}
}
cout << ans / 2;
return 0;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v) \
cerr << #v << ":"; \
for (int i = 0; i < v.size(); i++) \
cerr << " " << v[i]; \
cerr << endl;
#define DEBUG_MAT(v) \
cerr << #v << endl; \
for (int i = 0; i < v.size(); i++) { \
for (int j = 0; j < v[i].size(); j++) { \
cerr << v[i][j] << " "; \
} \
cerr << endl; \
}
typedef long long ll;
#define int ll
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
#define X first
#define Y second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i > 0; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(), c.end()
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
#define Sp(p) cout << setprecision(25) << fixed << p << endl;
// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
// vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };
#define fio() \
cin.tie(0); \
ios::sync_with_stdio(false);
const ll MOD = 1000000007;
// const ll MOD = 998244353;
// #define mp make_pair
// #define endl '\n'
/*
struct dice {
mt19937 mt;
dice() : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
// [0, x)の一様乱数
ll operator()(ll x) { return this->operator()(0, x); }
// [x, y)の一様乱数
ll operator()(ll x, ll y) {
uniform_int_distribution<ll> dist(x, y - 1);
return dist(mt);
}
vl operator()(int n, ll x, ll y) {
vl res(n);
for (int i = 0; i < n; i++) res[i] = this->operator()(x, y);
return res;
}
} rnd;
using ull = unsigned long long;
const ull MASK30 = (1UL << 30) - 1;
const ull MASK31 = (1UL << 31) - 1;
const ull M = (1UL << 61) - 1;
const ull B = rnd(2, M >> 1);
const ull POSITIVIZER = M * 3;;
class RollingHash {
public:
vector<ull> hash;
vector<ull> Bpower;
RollingHash(string s) {
int n = s.size();
hash.resize(n + 1); Bpower.resize(n + 1);
Bpower[0] = 1;
for (int i = 0; i < n; i++) {
hash[i + 1] = _calc_mod(_mul(hash[i], B) + s[i]);
Bpower[i + 1] = _calc_mod(_mul(Bpower[i], B));
}
}
//S[l, r)
ull part(int l, int r) {
return _calc_mod(hash[r] + POSITIVIZER - _mul(hash[l], Bpower[r - l]));
}
ull _mul(ull a, ull b) {
ull au = a >> 31;
ull ad = a & MASK31;
ull bu = b >> 31;
ull bd = b & MASK31;
ull mid = ad * bu + au * bd;
ull midu = mid >> 30;
ull midd = mid & MASK30;
return au * bu * 2 + midu + (midd << 31) + ad * bd;
}
ull _calc_mod(ull val) {
val = (val & M) + (val >> 61);
if (val > M) val -= M;
return val;
}
};
signed main() {
fio();
int n;
cin >> n;
vector<string> s(n);
rep (i, n) cin >> s[i];
sort(all(s), [](string &s1, string& s2) {
return s1.size() < s2.size();
});
// DEBUG_VEC(s);
vector<deque<char>> head(n);
vector<deque<char>> tail(n);
vector<RollingHash> hs;
rep (i, n) {
hs.push_back(RollingHash(s[i]));
}
vii num(n, vi(26));
rep (i, n) {
rep (j, s[i].size()) {
head[i].push_back(s[i][j]);
num[i][s[i][j] - 'a']++;
}
}
map<ull, vi> mp;
mp[0] = vi(26);
int start_idx = -1;
rep (i, n) {
if (s[i].size() == 1) continue;
if (start_idx == -1) start_idx = i;
rep (j, 26) {
if (num[i][j] > 0) mp[0][j]++;
}
}
ll ans = 0;
int i = 0;
for (i = 0; i < n; i++) {
if (s[i].size() == 1) {
ans += mp[0][s[i][0] - 'a'];
}
else break;
}
int pre_len = 1;
for (; i < n; i++) {
if (s[i].size() > pre_len) {
pre_len = s[i].size();
while (s[start_idx].size() == s[i].size()) start_idx++;
map<ull, vi> mp2;
for (int j = start_idx; j < n; j++) {
while (tail[j].size() < pre_len - 1) {
tail[j].push_front(head[j].back());
num[j][head[j].back() - 'a']--;
head[j].pop_back();
}
ull hash = hs[j].part((int)s[j].size() - (pre_len - 1),
s[j].size()); if (mp2.count(hash) == 0) mp2[hash] = vi(26); rep (k, 26) { if
(num[j][k] > 0) mp2[hash][k]++;
}
}
swap(mp, mp2);
}
ull hash = hs[i].part(1, s[i].size());
if (mp.count(hash)) {
ans += mp[hash][s[i][0] - 'a'];
}
}
cout << ans << endl;
}
//*/
//*
signed main() {
fio();
int n;
cin >> n;
vector<pii> tf;
int mi2 = inf, mi5 = inf;
int ma2 = -inf, ma5 = -inf;
rep(i, n) {
string s;
cin >> s;
ll bo = 1;
rrep(i, s.size()) {
if (s[i] == '.') {
for (int j = i + 1; j < s.size(); j++)
bo++;
s.erase(s.begin() + i);
break;
}
}
ll si = stoll(s);
// DEBUG(pll(si, bo));
int two = 0, fi = 0;
while (si > 0 and si % 2 == 0) {
two++;
si /= 2;
}
while (si > 0 and si % 5 == 0) {
fi++;
si /= 5;
}
two -= bo - 1;
fi -= bo - 1;
tf.push_back(pii(two, fi));
chmin(mi2, two);
chmin(mi5, fi);
chmax(ma2, two);
chmax(ma5, fi);
}
// DEBUG_VEC(tf);
int N = 20;
vii a(N, vi(N));
rep(i, n) {
int two = tf[i].first, fi = tf[i].second;
a[two - mi2][fi - mi5]++;
}
vll sum(N + 1, vl(N + 1));
rep(i, N) {
rep(j, N) { sum[i + 1][j + 1] = sum[i + 1][j] + a[i][j]; }
}
rep(i, N) {
rep(j, N + 1) { sum[i + 1][j] += sum[i][j]; }
}
// DEBUG_MAT(sum);
ll ans = 0;
rep(i, n) {
int two = tf[i].first, fi = tf[i].second;
if (tf[i].first >= 0 and tf[i].second >= 0)
ans--;
// DEBUG(ans);
int ii = -two - mi2, jj = -fi - mi5;
chmax(ii, 0LL);
chmax(jj, 0LL);
ans += sum[N][N] - sum[ii][N] - sum[N][jj] + sum[ii][jj];
// DEBUG(ans);
}
assert(ans % 2 == 0);
ans /= 2;
cout << ans << endl;
}
//*/ | #include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v) \
cerr << #v << ":"; \
for (int i = 0; i < v.size(); i++) \
cerr << " " << v[i]; \
cerr << endl;
#define DEBUG_MAT(v) \
cerr << #v << endl; \
for (int i = 0; i < v.size(); i++) { \
for (int j = 0; j < v[i].size(); j++) { \
cerr << v[i][j] << " "; \
} \
cerr << endl; \
}
typedef long long ll;
#define int ll
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
#define X first
#define Y second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i > 0; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(), c.end()
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
#define Sp(p) cout << setprecision(25) << fixed << p << endl;
// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
// vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };
#define fio() \
cin.tie(0); \
ios::sync_with_stdio(false);
const ll MOD = 1000000007;
// const ll MOD = 998244353;
// #define mp make_pair
// #define endl '\n'
/*
struct dice {
mt19937 mt;
dice() : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
// [0, x)の一様乱数
ll operator()(ll x) { return this->operator()(0, x); }
// [x, y)の一様乱数
ll operator()(ll x, ll y) {
uniform_int_distribution<ll> dist(x, y - 1);
return dist(mt);
}
vl operator()(int n, ll x, ll y) {
vl res(n);
for (int i = 0; i < n; i++) res[i] = this->operator()(x, y);
return res;
}
} rnd;
using ull = unsigned long long;
const ull MASK30 = (1UL << 30) - 1;
const ull MASK31 = (1UL << 31) - 1;
const ull M = (1UL << 61) - 1;
const ull B = rnd(2, M >> 1);
const ull POSITIVIZER = M * 3;;
class RollingHash {
public:
vector<ull> hash;
vector<ull> Bpower;
RollingHash(string s) {
int n = s.size();
hash.resize(n + 1); Bpower.resize(n + 1);
Bpower[0] = 1;
for (int i = 0; i < n; i++) {
hash[i + 1] = _calc_mod(_mul(hash[i], B) + s[i]);
Bpower[i + 1] = _calc_mod(_mul(Bpower[i], B));
}
}
//S[l, r)
ull part(int l, int r) {
return _calc_mod(hash[r] + POSITIVIZER - _mul(hash[l], Bpower[r - l]));
}
ull _mul(ull a, ull b) {
ull au = a >> 31;
ull ad = a & MASK31;
ull bu = b >> 31;
ull bd = b & MASK31;
ull mid = ad * bu + au * bd;
ull midu = mid >> 30;
ull midd = mid & MASK30;
return au * bu * 2 + midu + (midd << 31) + ad * bd;
}
ull _calc_mod(ull val) {
val = (val & M) + (val >> 61);
if (val > M) val -= M;
return val;
}
};
signed main() {
fio();
int n;
cin >> n;
vector<string> s(n);
rep (i, n) cin >> s[i];
sort(all(s), [](string &s1, string& s2) {
return s1.size() < s2.size();
});
// DEBUG_VEC(s);
vector<deque<char>> head(n);
vector<deque<char>> tail(n);
vector<RollingHash> hs;
rep (i, n) {
hs.push_back(RollingHash(s[i]));
}
vii num(n, vi(26));
rep (i, n) {
rep (j, s[i].size()) {
head[i].push_back(s[i][j]);
num[i][s[i][j] - 'a']++;
}
}
map<ull, vi> mp;
mp[0] = vi(26);
int start_idx = -1;
rep (i, n) {
if (s[i].size() == 1) continue;
if (start_idx == -1) start_idx = i;
rep (j, 26) {
if (num[i][j] > 0) mp[0][j]++;
}
}
ll ans = 0;
int i = 0;
for (i = 0; i < n; i++) {
if (s[i].size() == 1) {
ans += mp[0][s[i][0] - 'a'];
}
else break;
}
int pre_len = 1;
for (; i < n; i++) {
if (s[i].size() > pre_len) {
pre_len = s[i].size();
while (s[start_idx].size() == s[i].size()) start_idx++;
map<ull, vi> mp2;
for (int j = start_idx; j < n; j++) {
while (tail[j].size() < pre_len - 1) {
tail[j].push_front(head[j].back());
num[j][head[j].back() - 'a']--;
head[j].pop_back();
}
ull hash = hs[j].part((int)s[j].size() - (pre_len - 1),
s[j].size()); if (mp2.count(hash) == 0) mp2[hash] = vi(26); rep (k, 26) { if
(num[j][k] > 0) mp2[hash][k]++;
}
}
swap(mp, mp2);
}
ull hash = hs[i].part(1, s[i].size());
if (mp.count(hash)) {
ans += mp[hash][s[i][0] - 'a'];
}
}
cout << ans << endl;
}
//*/
//*
signed main() {
fio();
int n;
cin >> n;
vector<pii> tf;
int mi2 = inf, mi5 = inf;
int ma2 = -inf, ma5 = -inf;
rep(i, n) {
string s;
cin >> s;
ll bo = 1;
rrep(i, s.size()) {
if (s[i] == '.') {
for (int j = i + 1; j < s.size(); j++)
bo++;
s.erase(s.begin() + i);
break;
}
}
ll si = stoll(s);
// DEBUG(pll(si, bo));
int two = 0, fi = 0;
while (si > 0 and si % 2 == 0) {
two++;
si /= 2;
}
while (si > 0 and si % 5 == 0) {
fi++;
si /= 5;
}
two -= bo - 1;
fi -= bo - 1;
tf.push_back(pii(two, fi));
chmin(mi2, two);
chmin(mi5, fi);
chmax(ma2, two);
chmax(ma5, fi);
}
// DEBUG_VEC(tf);
int N = 400;
vii a(N, vi(N));
rep(i, n) {
int two = tf[i].first, fi = tf[i].second;
a[two - mi2][fi - mi5]++;
}
vll sum(N + 1, vl(N + 1));
rep(i, N) {
rep(j, N) { sum[i + 1][j + 1] = sum[i + 1][j] + a[i][j]; }
}
rep(i, N) {
rep(j, N + 1) { sum[i + 1][j] += sum[i][j]; }
}
// DEBUG_MAT(sum);
ll ans = 0;
rep(i, n) {
int two = tf[i].first, fi = tf[i].second;
if (tf[i].first >= 0 and tf[i].second >= 0)
ans--;
// DEBUG(ans);
int ii = -two - mi2, jj = -fi - mi5;
chmax(ii, 0LL);
chmax(jj, 0LL);
ans += sum[N][N] - sum[ii][N] - sum[N][jj] + sum[ii][jj];
// DEBUG(ans);
}
assert(ans % 2 == 0);
ans /= 2;
cout << ans << endl;
}
//*/ | replace | 268 | 269 | 268 | 269 | 0 | |
p02588 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MOD (1000000000 + 7)
using namespace std;
typedef long long ll;
const int MAX = 3000000;
long long fac[MAX], finv[MAX], inv[MAX];
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
map<ll, ll> prime; // 素因数分解でそれぞれの素数がいくつ出てきたかを保存するmap
// O(√n)
// 整列済み(mapはkeyで自動で整列される)
void prime_factorize(ll n) {
if (n <= 1)
return;
ll l = sqrt(n);
for (ll i = 2; i <= (ll)(l); i++) { //
if (n % i == 0) {
prime_factorize(i);
prime_factorize(ll(n / i));
return;
}
}
// mapでは存在しないkeyの場合も自動で構築される
prime[n]++;
return;
}
// 約数列挙?
vector<ll> divisor;
void list_divisor(ll n) {
divisor.clear();
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
divisor.push_back(i);
if (i * i != n)
divisor.push_back(n / i);
}
}
sort(divisor.begin(), divisor.end());
}
vector<bool> Eratosthenes(ll N) {
vector<bool> primes(N, true);
vector<ll> ret;
primes[0] = false;
primes[1] = false;
for (ll i = 2; pow(i, 2) <= N; i++) {
if (primes[i]) {
for (int j = 2; i * j <= N; j++)
primes[i * j] = false;
ret.push_back(primes[i]);
}
}
return primes; // ret;
}
template <typename T>
bool next_combination(const T first, const T last, int k) {
// ** Sample **
// vector<int> v{1, 2, 3, 4, 5, 6, 7};
// do {
// for (int i = 0; i < k; i++) {
// cout << v[i] << " ";
// }
// cout << "| ";
// for (int i = k; i < v.size(); i++) {
// cout << v[i] << " ";
// }
// cout << endl;
// } while(next_combination(v.begin(), v.end(), k));
const T subset = first + k;
// empty container | k = 0 | k == n
if (first == last || first == subset || last == subset) {
return false;
}
T src = subset;
while (first != src) {
src--;
if (*src < *(last - 1)) {
T dest = subset;
while (*src >= *dest) {
dest++;
}
iter_swap(src, dest);
rotate(src + 1, dest + 1, last);
rotate(subset, subset + (last - dest) - 1, last);
return true;
}
}
// restore
rotate(first, subset, last);
return false;
}
// union by size + path having
class UnionFind {
public:
vector<ll> par; // 各元の親を表す配列
vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)
// Constructor
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
};
int main() {
ll N;
cin >> N;
vector<ll> A(N, 0);
vector<ll> five(N, 0);
vector<ll> two(N, 0);
vector<vector<ll>> count(20, vector<ll>(20, 0));
vector<vector<ll>> cum(20, vector<ll>(20, 0));
for (ll i = 0; i < N; i++) {
string d;
cin >> d;
ll small = 0;
if (d.find('.') != string::npos) {
small = d.length() - d.find('.') - 1;
d.erase(remove(d.begin(), d.end(), '.'), d.end());
}
A[i] = stoll(d);
for (int j = 1; j <= 9 - small; j++) {
A[i] *= 10;
}
prime.clear();
prime_factorize(A[i]);
five[i] = prime[5] >= 20 ? 19 : prime[5];
two[i] = prime[2] >= 20 ? 19 : prime[2];
// cout << d << "->" << stoll(d) << " -> " << A[i] << ":" << two[i] <<"," <<
// five[i] << endl;
count[two[i]][five[i]]++;
}
// return 0;
for (ll i = 19; i >= 0; i--) {
for (ll j = 19; j >= 0; j--) {
if (i == 19 && j == 19)
cum[i][j] = count[i][j];
else if (i == 19)
cum[i][j] = cum[i][j + 1] + count[i][j];
else if (j == 19)
cum[i][j] = cum[i + 1][j] + count[i][j];
else
cum[i][j] =
cum[i][j + 1] + cum[i + 1][j] + count[i][j] - cum[i + 1][j + 1];
}
}
// cout << "test " << endl;
ll ret = 0;
for (ll i = 0; i < N; i++) {
ll res = (two[i] >= 18 - two[i] && five[i] >= 18 - five[i]) ? 1 : 0;
ret += cum[max((ll)0, 18 - two[i])][max((ll)0, 18 - five[i])] - res;
// cout << A[i] << "," << cum[18-two[i]][18-five[i]] - res << endl;
}
cout << ret / 2 << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MOD (1000000000 + 7)
using namespace std;
typedef long long ll;
const int MAX = 3000000;
long long fac[MAX], finv[MAX], inv[MAX];
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
map<ll, ll> prime; // 素因数分解でそれぞれの素数がいくつ出てきたかを保存するmap
// O(√n)
// 整列済み(mapはkeyで自動で整列される)
void prime_factorize(ll n) {
if (n <= 1)
return;
ll l = sqrt(n);
for (ll i = 2; i <= (ll)(l); i++) { //
if (n % i == 0) {
prime_factorize(i);
prime_factorize(ll(n / i));
return;
}
}
// mapでは存在しないkeyの場合も自動で構築される
prime[n]++;
return;
}
// 約数列挙?
vector<ll> divisor;
void list_divisor(ll n) {
divisor.clear();
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
divisor.push_back(i);
if (i * i != n)
divisor.push_back(n / i);
}
}
sort(divisor.begin(), divisor.end());
}
vector<bool> Eratosthenes(ll N) {
vector<bool> primes(N, true);
vector<ll> ret;
primes[0] = false;
primes[1] = false;
for (ll i = 2; pow(i, 2) <= N; i++) {
if (primes[i]) {
for (int j = 2; i * j <= N; j++)
primes[i * j] = false;
ret.push_back(primes[i]);
}
}
return primes; // ret;
}
template <typename T>
bool next_combination(const T first, const T last, int k) {
// ** Sample **
// vector<int> v{1, 2, 3, 4, 5, 6, 7};
// do {
// for (int i = 0; i < k; i++) {
// cout << v[i] << " ";
// }
// cout << "| ";
// for (int i = k; i < v.size(); i++) {
// cout << v[i] << " ";
// }
// cout << endl;
// } while(next_combination(v.begin(), v.end(), k));
const T subset = first + k;
// empty container | k = 0 | k == n
if (first == last || first == subset || last == subset) {
return false;
}
T src = subset;
while (first != src) {
src--;
if (*src < *(last - 1)) {
T dest = subset;
while (*src >= *dest) {
dest++;
}
iter_swap(src, dest);
rotate(src + 1, dest + 1, last);
rotate(subset, subset + (last - dest) - 1, last);
return true;
}
}
// restore
rotate(first, subset, last);
return false;
}
// union by size + path having
class UnionFind {
public:
vector<ll> par; // 各元の親を表す配列
vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)
// Constructor
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i)
par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
};
int main() {
ll N;
cin >> N;
vector<ll> A(N, 0);
vector<ll> five(N, 0);
vector<ll> two(N, 0);
vector<vector<ll>> count(20, vector<ll>(20, 0));
vector<vector<ll>> cum(20, vector<ll>(20, 0));
for (ll i = 0; i < N; i++) {
string d;
cin >> d;
ll small = 0;
if (d.find('.') != string::npos) {
small = d.length() - d.find('.') - 1;
d.erase(remove(d.begin(), d.end(), '.'), d.end());
}
A[i] = stoll(d);
for (int j = 1; j <= 9 - small; j++) {
A[i] *= 10;
}
// prime.clear();
// prime_factorize(A[i]);
ll twov = 0;
ll fivev = 0;
while (true) {
if (A[i] % 2 != 0)
break;
A[i] /= 2;
twov++;
}
while (true) {
if (A[i] % 5 != 0)
break;
A[i] /= 5;
fivev++;
}
five[i] = fivev >= 20 ? 19 : fivev;
two[i] = twov >= 20 ? 19 : twov;
// cout << d << "->" << stoll(d) << " -> " << A[i] << ":" << two[i] <<"," <<
// five[i] << endl;
count[two[i]][five[i]]++;
}
// return 0;
for (ll i = 19; i >= 0; i--) {
for (ll j = 19; j >= 0; j--) {
if (i == 19 && j == 19)
cum[i][j] = count[i][j];
else if (i == 19)
cum[i][j] = cum[i][j + 1] + count[i][j];
else if (j == 19)
cum[i][j] = cum[i + 1][j] + count[i][j];
else
cum[i][j] =
cum[i][j + 1] + cum[i + 1][j] + count[i][j] - cum[i + 1][j + 1];
}
}
// cout << "test " << endl;
ll ret = 0;
for (ll i = 0; i < N; i++) {
ll res = (two[i] >= 18 - two[i] && five[i] >= 18 - five[i]) ? 1 : 0;
ret += cum[max((ll)0, 18 - two[i])][max((ll)0, 18 - five[i])] - res;
// cout << A[i] << "," << cum[18-two[i]][18-five[i]] - res << endl;
}
cout << ret / 2 << endl;
return 0;
}
| replace | 231 | 235 | 231 | 251 | TLE | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<ll, ll>
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
int inf = 1000001000;
ll INF = 1e18;
ll mod = 1000000007;
ll MOD = INF;
int Pow(int b, int e) {
if (e < 0)
e += MOD - 1;
int ret = 1;
while (e) {
if (e & 1)
ret = 1LL * ret * b % MOD;
b = 1LL * b * b % MOD;
e >>= 1;
}
return ret;
}
int main() {
int n;
cin >> n;
vector<string> a(n);
vector<ll> b(n, 0);
rep(i, n) cin >> a[i];
rep(i, n) {
ll u = 9;
bool t = false;
rep(j, a[i].size()) {
if (t)
u--;
if (a[i][j] != '.') {
b[i] *= 10;
b[i] += a[i][j] - '0';
} else
t = true;
}
b[i] *= Pow(10, u);
}
vector<vector<ll>> c(20, vector<ll>(20, 0));
ll ans = 0;
rep(i, n) {
int c2 = 0, c5 = 0;
while (true) {
if (b[i] % 2 == 0)
c2++;
else
break;
b[i] /= 2;
}
while (true) {
if (b[i] % 5 == 0)
c5++;
else
break;
b[i] /= 5;
}
ans += c[18 - c2][18 - c5];
rep(j, min(c2 + 1, 20)) {
rep(k, min(c5 + 1, 20)) { c[j][k]++; }
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<ll, ll>
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
int inf = 1000001000;
ll INF = 1e18;
ll mod = 1000000007;
ll MOD = INF;
int Pow(int b, int e) {
if (e < 0)
e += MOD - 1;
int ret = 1;
while (e) {
if (e & 1)
ret = 1LL * ret * b % MOD;
b = 1LL * b * b % MOD;
e >>= 1;
}
return ret;
}
int main() {
int n;
cin >> n;
vector<string> a(n);
vector<ll> b(n, 0);
rep(i, n) cin >> a[i];
rep(i, n) {
ll u = 9;
bool t = false;
rep(j, a[i].size()) {
if (t)
u--;
if (a[i][j] != '.') {
b[i] *= 10;
b[i] += a[i][j] - '0';
} else
t = true;
}
b[i] *= Pow(10, u);
}
vector<vector<ll>> c(20, vector<ll>(20, 0));
ll ans = 0;
rep(i, n) {
int c2 = 0, c5 = 0;
while (true) {
if (b[i] % 2 == 0)
c2++;
else
break;
b[i] /= 2;
}
while (true) {
if (b[i] % 5 == 0)
c5++;
else
break;
b[i] /= 5;
}
ans += c[max(0, 18 - c2)][max(0, 18 - c5)];
rep(j, min(c2 + 1, 20)) {
rep(k, min(c5 + 1, 20)) { c[j][k]++; }
}
}
cout << ans << endl;
}
| replace | 64 | 65 | 64 | 65 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int N, cnt[100][100];
char sir[100];
const int maxE = 9;
long long ans = 0;
int getExp(long long &N, int p) {
int cnt = 0;
while (N % p == 0)
N /= p, cnt++;
return cnt;
}
void add(int x, int y) {
// printf ("%d %d\n", x, y);
for (int i = 2 * maxE - x; i <= 2 * maxE; i++)
for (int j = 2 * maxE - y; j <= 2 * maxE; j++)
ans += cnt[i][j];
cnt[x][y]++;
}
int main() {
// freopen ("input", "r", stdin);
// freopen ("output", "w", stdout);
scanf("%d\n", &N);
for (int i = 1; i <= N; i++) {
scanf("%s", sir + 1);
int L = strlen(sir + 1), p = 1;
long long curr = 0;
while (p <= L && sir[p] != '.')
curr = curr * 10LL + sir[p] - '0', p++;
for (int i = p + 1; i <= L; i++)
curr = curr * 10LL + sir[i] - '0';
int after = (p > L ? 0 : L - p);
assert(after <= maxE);
int e2 = getExp(curr, 2), e5 = getExp(curr, 5);
add(e2 + maxE - after, e5 + maxE - after);
}
printf("%lld\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int N, cnt[100][100];
char sir[100];
const int maxE = 9;
long long ans = 0;
int getExp(long long &N, int p) {
int cnt = 0;
while (N % p == 0)
N /= p, cnt++;
return cnt;
}
void add(int x, int y) {
x = min(x, 2 * maxE);
y = min(y, 2 * maxE);
// printf ("%d %d\n", x, y);
for (int i = 2 * maxE - x; i <= 2 * maxE; i++)
for (int j = 2 * maxE - y; j <= 2 * maxE; j++)
ans += cnt[i][j];
cnt[x][y]++;
}
int main() {
// freopen ("input", "r", stdin);
// freopen ("output", "w", stdout);
scanf("%d\n", &N);
for (int i = 1; i <= N; i++) {
scanf("%s", sir + 1);
int L = strlen(sir + 1), p = 1;
long long curr = 0;
while (p <= L && sir[p] != '.')
curr = curr * 10LL + sir[p] - '0', p++;
for (int i = p + 1; i <= L; i++)
curr = curr * 10LL + sir[i] - '0';
int after = (p > L ? 0 : L - p);
assert(after <= maxE);
int e2 = getExp(curr, 2), e5 = getExp(curr, 5);
add(e2 + maxE - after, e5 + maxE - after);
}
printf("%lld\n", ans);
return 0;
}
| insert | 17 | 17 | 17 | 19 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const long long INF = 1e9;
// const ll mod = 1000000007;
ll A[201000];
ll N;
ll f(string S) {
int idx = -1;
for (int i = 0; i < S.size(); i++) {
if (S[i] == '.')
idx = i;
}
if (idx == -1) {
ll tmp = stoll(S);
tmp *= INF;
return tmp;
}
ll num = S.size() - idx - 1;
string T = S.substr(0, idx);
string Q = S.substr(idx + 1, num);
ll tmp = stoll(T);
for (int i = 0; i < num; i++)
tmp *= 10;
tmp += stoll(Q);
for (int i = num; i < 9; i++)
tmp *= 10;
return tmp;
}
vector<ll> Div;
ll num[20][20];
ll five[201000], two[201000];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
for (int i = 0; i < N; i++) {
string S;
cin >> S;
A[i] = f(S);
while (A[i] % 2 == 0) {
A[i] /= 2;
two[i]++;
}
while (A[i] % 5 == 0) {
A[i] /= 5;
five[i]++;
}
num[two[i]][five[i]]++;
// cerr << A[i] << " ";
}
ll ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < 20; j++) {
for (int k = 0; k < 20; k++) {
if (two[i] + j >= 18 and five[i] + k >= 18) {
ans += num[j][k];
}
}
}
if (2 * two[i] >= 18 and 2 * five[i] >= 18) {
ans -= 1;
}
}
ans /= 2;
cout << ans << endl;
// cerr << endl;
return 0;
}
| #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const long long INF = 1e9;
// const ll mod = 1000000007;
ll A[201000];
ll N;
ll f(string S) {
int idx = -1;
for (int i = 0; i < S.size(); i++) {
if (S[i] == '.')
idx = i;
}
if (idx == -1) {
ll tmp = stoll(S);
tmp *= INF;
return tmp;
}
ll num = S.size() - idx - 1;
string T = S.substr(0, idx);
string Q = S.substr(idx + 1, num);
ll tmp = stoll(T);
for (int i = 0; i < num; i++)
tmp *= 10;
tmp += stoll(Q);
for (int i = num; i < 9; i++)
tmp *= 10;
return tmp;
}
vector<ll> Div;
ll num[20][20];
ll five[201000], two[201000];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
for (int i = 0; i < N; i++) {
string S;
cin >> S;
A[i] = f(S);
while (A[i] % 2 == 0) {
A[i] /= 2;
two[i]++;
}
while (A[i] % 5 == 0) {
A[i] /= 5;
five[i]++;
}
chmin(two[i], 18LL);
chmin(five[i], 18LL);
num[two[i]][five[i]]++;
// cerr << A[i] << " ";
}
ll ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < 20; j++) {
for (int k = 0; k < 20; k++) {
if (two[i] + j >= 18 and five[i] + k >= 18) {
ans += num[j][k];
}
}
}
if (2 * two[i] >= 18 and 2 * five[i] >= 18) {
ans -= 1;
}
}
ans /= 2;
cout << ans << endl;
// cerr << endl;
return 0;
}
| insert | 70 | 70 | 70 | 72 | 0 | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
#define M_PI 3.14159265358979323846 // pi
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;
#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)
using namespace std;
static const ll INF = 1e15;
int main() {
int n;
cin >> n;
vector<vector<ll>> grid(20, vector<ll>(20, 0));
vector<P> ss(n);
rep(i, n) {
double item;
cin >> item;
double t = item * 1e9 + 0.1;
ll v = (ll)t;
int a = 0;
while (v % 2 == 0) {
a++;
v /= 2;
}
int b = 0;
while (v % 5 == 0) {
b++;
v /= 5;
}
a = min(20, a);
b = min(20, b);
grid[a][b]++;
ss[i] = make_pair(a, b);
}
ll sum = 0;
rep(i, n) {
auto f = ss[i].first;
auto s = ss[i].second;
grid[f][s]--;
for (auto f2 = max(0LL, 18 - f); f2 < 20; f2++) {
for (auto s2 = max(0LL, 18 - s); s2 < 20; s2++) {
sum += grid[f2][s2];
}
}
}
cout << sum << endl;
return 0;
}
| #include <bits/stdc++.h>
#define M_PI 3.14159265358979323846 // pi
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;
#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)
using namespace std;
static const ll INF = 1e15;
int main() {
int n;
cin >> n;
vector<vector<ll>> grid(20, vector<ll>(20, 0));
vector<P> ss(n);
rep(i, n) {
double item;
cin >> item;
double t = item * 1e9 + 0.1;
ll v = (ll)t;
int a = 0;
while (v % 2 == 0) {
a++;
v /= 2;
}
int b = 0;
while (v % 5 == 0) {
b++;
v /= 5;
}
a = min(19, a);
b = min(19, b);
grid[a][b]++;
ss[i] = make_pair(a, b);
}
ll sum = 0;
rep(i, n) {
auto f = ss[i].first;
auto s = ss[i].second;
grid[f][s]--;
for (auto f2 = max(0LL, 18 - f); f2 < 20; f2++) {
for (auto s2 = max(0LL, 18 - s); s2 < 20; s2++) {
sum += grid[f2][s2];
}
}
}
cout << sum << endl;
return 0;
}
| replace | 40 | 42 | 40 | 42 | 0 | |
p02588 | C++ | Runtime Error | #include "bits/stdc++.h"
#include <random>
using namespace std;
using ll = long long;
using dd = double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vdd = vector<dd>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using vvll = vector<vll>;
using vvdd = vector<vdd>;
using vvpll = vector<vpll>;
using vvtll = vector<vtll>;
using vvvll = vector<vvll>;
using vvvdd = vector<vvdd>;
using vvvpll = vector<vvpll>;
using vvvtll = vector<vvtll>;
using vvvvll = vector<vvvll>;
using vvvvdd = vector<vvvdd>;
using vvvvpll = vector<vvvpll>;
using vvvvtll = vector<vvvtll>;
constexpr ll INF = 1LL << 60;
constexpr dd EPS = 1e-11;
constexpr dd PI = 3.1415926535897932;
// cin,cout高速化のおまじない+桁数指定
struct Fast {
Fast() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
}
} fast;
#define REPS(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, S, E) for (ll i = (S); i <= (E); i++)
#define REP(i, N) REPS(i, 0, (N)-1)
#define DEPS(i, S, E) for (ll i = (E); i >= (S); i--)
#define dep(i, E, S) for (ll i = (E); i >= (S); i--)
#define DEP(i, N) DEPS(i, 0, (N)-1)
#define EACH(e, v) for (auto &&e : v)
#define each(e, v) for (auto &&e : v)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline T MaxE(vector<T> &v, ll S, ll E) {
T m = v[S];
REPS(i, S, E) chmax(m, v[i]);
return m;
} // v[S]~v[E]の最大値
template <class T> inline T MinE(vector<T> &v, ll S, ll E) {
T m = v[S];
REPS(i, S, E) chmin(m, v[i]);
return m;
} // v[S]~v[E]の最小値
template <class T> inline T MaxE(vector<T> &v, ll N) {
return MaxE(v, 0, N - 1);
} // 先頭N個中の最大値
template <class T> inline T MinE(vector<T> &v, ll N) {
return MinE(v, 0, N - 1);
}
template <class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); }
template <class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); }
template <class T> inline ll MaxI(vector<T> &v, ll S, ll E) {
ll m = S;
REPS(i, S, E) {
if (v[i] > v[m])
m = i;
}
return m;
}
template <class T> inline ll MinI(vector<T> &v, ll S, ll E) {
ll m = S;
REPS(i, S, E) {
if (v[i] < v[m])
m = i;
}
return m;
}
template <class T> inline ll MaxI(vector<T> &v, ll N) {
return MaxI(v, 0, N - 1);
}
template <class T> inline ll MinI(vector<T> &v, ll N) {
return MinI(v, 0, N - 1);
}
template <class T> inline ll MaxI(vector<T> &v) {
return MaxI(v, (ll)v.size());
}
template <class T> inline ll MinI(vector<T> &v) {
return MinI(v, (ll)v.size());
}
template <class T> inline T Sum(vector<T> &v, ll S, ll E) {
T s = v[S];
REPS(i, S + 1, E) s += v[i];
return s;
}
template <class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); }
template <class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); }
template <class T> inline ll sz(T &v) { return (ll)v.size(); }
template <class T> inline T POW(T a, ll n) {
T r = 1;
for (; n > 0; n >>= 1, a *= a) {
if (n & 1)
r *= a;
}
return r;
}
inline ll POW(int a, ll n) { return POW((ll)a, n); }
inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }
ll Gcd(ll a, ll b) { return (b == 0) ? a : Gcd(b, a % b); }
ll Lcm(ll a, ll b) { return a * b / Gcd(a, b); }
inline ll MSB(ll a) {
for (ll o = 63, x = -1;;) {
ll m = (o + x) / 2;
if (a < (1LL << m))
o = m;
else
x = m;
if (o - x == 1)
return x;
}
}
inline ll Upper2N(ll s) { return 1LL << MSB(s) << 1; } // s<2^nなる最小の2^n
inline vll Bit2Idx(ll m) {
vll v;
for (ll i = 0; m; m >>= 1, i++)
if (m & 1)
v.push_back(i);
return move(v);
}
inline ll BitNum(ll m) {
for (ll c = 0;; m >>= 1) {
c += m & 1;
if (!m)
return c;
}
}
inline ll Bit(ll s, ll i) { return (s >> i) & 1; } // sの第ibit i=0-63
[[nodiscard]] inline ll BitOn(ll s, ll i) {
return s | (1LL << i);
} // sの第ibitON i=0-63
[[nodiscard]] inline ll BitOff(ll s, ll i) {
return s & ~(1LL << i);
} // sの第ibitOFF i=0-63
[[nodiscard]] inline ll BitCut(ll s, ll i) {
return s & (1LL << ++i) - 1;
} // sの第0~第ibitを抽出
inline vll str2num(string &s, ll N) {
vll v(N);
REP(i, N) v[i] = (ll)(s[i] - '0');
return move(v);
}
inline vll ALP2num(string &s, ll N) {
vll v(N);
REP(i, N) v[i] = (ll)(s[i] - 'A');
return move(v);
}
inline vll alp2num(string &s, ll N) {
vll v(N);
REP(i, N) v[i] = (ll)(s[i] - 'a');
return move(v);
}
template <class T> inline ll FloorIdx(vector<T> &v, T x) {
return upper_bound(ALL(v), x) - v.begin() - 1;
}
template <class T> inline ll CeilIdx(vector<T> &v, T x) {
return lower_bound(ALL(v), x) - v.begin();
}
template <class T>
inline ll UnderNumOf(vector<T> &v, T x) { // sort済vのx以下の個数を得る
return upper_bound(v.begin(), v.end(), x) - v.begin();
}
template <class T, class Pr>
inline ll UnderNumOf(vector<T> &v, T x, Pr pred) { // x以下個数
return upper_bound(v.begin(), v.end(), x, pred) - v.begin();
}
template <class T>
inline ll OverNumOf(vector<T> &v, T x) { // sort済vのx以上の個数を得る
return (ll)v.size() - (lower_bound(v.begin(), v.end(), x) - v.begin());
}
template <class T, class Pr>
inline ll OverNumOf(vector<T> &v, T x, Pr pred) { // x以上の個数
return (ll)v.size() - (lower_bound(v.begin(), v.end(), x, pred) - v.begin());
}
template <class T = ll> inline vector<T> cinv(ll N) {
vector<T> v(N);
REP(i, N) cin >> v[i];
return move(v);
}
template <class T, class S> inline vector<pair<T, S>> cinv2(ll N) {
vector<pair<T, S>> v(N);
REP(i, N) { cin >> v[i].first >> v[i].second; }
return move(v);
}
template <class T> inline vector<T> cinv2(ll N) {
vector<T> v(N);
REP(i, N) {
ll a, b;
cin >> a >> b;
v[i] = {a, b};
}
return move(v);
}
template <class T, class S, class R> inline vector<tuple<T, S, R>> cinv3(ll N) {
vector<tuple<T, S, R>> v(N);
REP(i, N) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]); }
return move(v);
}
template <class T> inline vector<T> cinv3(ll N) {
vector<T> v(N);
REP(i, N) {
ll a, b, c;
cin >> a >> b >> c;
v[i] = {a, b, c};
}
return move(v);
}
template <class T, class S, class R, class Q>
inline vector<tuple<T, S, R, Q>> cinv4(ll N) {
vector<tuple<T, S, R, Q>> v(N);
REP(i, N) {
cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]) >> get<3>(v[i]);
}
return move(v);
}
template <class T> inline vector<T> cinv4(ll N) {
vector<T> v(N);
REP(i, N) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
v[i] = {a, b, c, d};
}
return move(v);
}
template <class T = ll> inline vector<vector<T>> cinvv(ll N, ll M) {
vector<vector<T>> vv(N);
REP(i, N) vv[i] = cinv(M);
return move(vv);
}
template <class T> inline void coutv(vector<T> &v, string d = " ") {
ll N = (ll)v.size();
REP(i, N) { cout << v[i] << ((i == N - 1) ? "" : d); }
cout << '\n';
}
template <class T> inline void coutv(deque<T> &v, string d = " ") {
ll N = (ll)v.size();
REP(i, N) { cout << v[i] << ((i == N - 1) ? "" : d); }
cout << '\n';
}
template <class T> void bye(T a) {
cout << a << '\n';
exit(0);
}
#if defined(_DEBUG)
ll dumpW = 5;
template <class T> void Dump(vector<T> &v) {
REP(i, (ll)v.size()) {
cerr << ((i == 0) ? "[" : " ") << setw(dumpW) << v[i];
}
cerr << "]";
}
void Dump(vll &v) {
REP(i, (ll)v.size()) {
cerr << ((i == 0) ? "[" : " ") << setw(dumpW);
if (v[i] == INF)
cerr << "INF";
else if (v[i] == -INF)
cerr << "-INF";
else
cerr << v[i];
}
cerr << "]";
}
template <class T> void Dump(vector<vector<T>> &v) {
ll N = (ll)v.size();
string d[2][2] = {{" ", "["}, {"\n", "]\n"}};
REP(i, N) {
cerr << d[0][i == 0];
Dump(v[i]);
cerr << d[1][i == N - 1];
}
}
template <class T> void Dump(vector<vector<vector<T>>> &v) {
ll N = (ll)v.size();
string d[2] = {" <", "[ <"};
REP(i, N) {
cerr << d[i == 0] << i << ">\n";
Dump(v[i]);
}
cerr << "]\n";
}
template <class T> void Dump(vector<vector<vector<vector<T>>>> &v) {
ll N = (ll)v.size();
string d[2] = {" ---<", "[ ---<"};
REP(i, N) {
cerr << d[i == 0] << i << ">---\n";
Dump(v[i]);
}
cerr << "]\n";
}
template <class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v) {
ll N = (ll)v.size();
string d[2] = {" ======<", "[ ======<"};
REP(i, N) {
cerr << d[i == 0] << i << ">======\n";
Dump(v[i]);
}
cerr << "]\n";
}
template <class T, class S> void Dump(map<T, S> &m) {
for (auto e : m)
cout << e.first << " : " << e.second << '\n';
}
#else
template <class T> void Dump(vector<T> &v) {}
template <class T> void Dump(vector<vector<T>> &v) {}
template <class T> void Dump(vector<vector<vector<T>>> &v) {}
template <class T> void Dump(vector<vector<vector<vector<T>>>> &v) {}
template <class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v) {}
#endif
template <ll MOD> struct mll_ {
ll val;
mll_(ll v = 0) : val(v % MOD) {
if (val < 0)
val += MOD;
}
mll_ operator-() const { return -val; }
mll_ operator+(const mll_ &b) const { return val + b.val; }
mll_ operator-(const mll_ &b) const { return val - b.val; }
mll_ operator*(const mll_ &b) const { return val * b.val; }
mll_ operator/(const mll_ &b) const { return mll_(*this) /= b; }
mll_ operator+(ll b) const { return *this + mll_(b); }
mll_ operator-(ll b) const { return *this - mll_(b); }
mll_ operator*(ll b) const { return *this * mll_(b); }
friend mll_ operator+(ll a, const mll_ &b) { return b + a; }
friend mll_ operator-(ll a, const mll_ &b) { return -b + a; }
friend mll_ operator*(ll a, const mll_ &b) { return b * a; }
friend mll_ operator/(ll a, const mll_ &b) { return mll_(a) / b; }
mll_ &operator+=(const mll_ &b) {
val = (val + b.val) % MOD;
return *this;
}
mll_ &operator-=(const mll_ &b) {
val = (val + MOD - b.val) % MOD;
return *this;
}
mll_ &operator*=(const mll_ &b) {
val = (val * b.val) % MOD;
return *this;
}
mll_ &operator/=(const mll_ &b) {
ll c = b.val, d = MOD, u = 1, v = 0;
while (d) {
ll t = c / d;
c -= t * d;
swap(c, d);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
mll_ &operator+=(ll b) { return *this += mll_(b); }
mll_ &operator-=(ll b) { return *this -= mll_(b); }
mll_ &operator*=(ll b) { return *this *= mll_(b); }
mll_ &operator/=(ll b) { return *this /= mll_(b); }
bool operator==(const mll_ &b) { return val == b.val; }
bool operator!=(const mll_ &b) { return val != b.val; }
bool operator==(ll b) { return *this == mll_(b); }
bool operator!=(ll b) { return *this != mll_(b); }
friend bool operator==(ll a, const mll_ &b) { return mll_(a) == b.val; }
friend bool operator!=(ll a, const mll_ &b) { return mll_(a) != b.val; }
friend ostream &operator<<(ostream &os, const mll_ &a) { return os << a.val; }
friend istream &operator>>(istream &is, mll_ &a) { return is >> a.val; }
static mll_ Combination(ll a, ll b) {
chmin(b, a - b);
if (b < 0)
return mll_(0);
mll_ c = 1;
REP(i, b) c *= a - i;
REP(i, b) c /= i + 1;
return c;
}
};
using mll = mll_<1000000007LL>; // 1000000007LL;// 998244353LL;
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;
using vvvvvmll = std::vector<vvvvmll>;
void solve() {
ll n;
cin >> n;
vector<dd> a_ = cinv<dd>(n);
each(e, a_) e *= 1000000000.0;
vll aa(n);
rep(i, 0, n - 1) { aa[i] = (ll)(a_[i] + .1); }
const ll M = 18;
vvll cnt(M + 1, vll(M + 1));
auto Cnt = [&](ll i, ll j) -> ll & {
if (i > M)
i = M;
if (j > M)
j = M;
return cnt[M - i][M - j];
};
vpll buf;
rep(i, 0, n - 1) {
ll v = aa[i];
ll cn2 = 0;
while (true) {
if (v % 2 == 0) {
v /= 2;
cn2++;
} else
break;
}
ll cn5 = 0;
while (true) {
if (v % 5 == 0) {
v /= 5;
cn5++;
} else
break;
}
Cnt(cn2, cn5)++;
buf.emplace_back(cn2, cn5);
}
rep(i, 0, M) {
rep(j, 1, M) { cnt[i][j] += cnt[i][j - 1]; }
}
rep(j, 0, M) rep(i, 1, M) { cnt[i][j] += cnt[i - 1][j]; }
ll ans = 0;
each(e, buf) {
ll cn2;
ll cn5;
tie(cn2, cn5) = e;
ll r2 = M - cn2;
ll r5 = M - cn5;
ll val = Cnt(r2, r5);
ans += val;
if (cn2 >= M / 2 and cn5 >= M / 2)
ans--;
}
ans /= 2;
cout << ans << '\n';
}
int main() {
#if 1
solve();
#else
ll t;
cin >> t;
rep(i, 0, t - 1) { solve(); }
#endif
return 0;
}
| #include "bits/stdc++.h"
#include <random>
using namespace std;
using ll = long long;
using dd = double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vdd = vector<dd>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using vvll = vector<vll>;
using vvdd = vector<vdd>;
using vvpll = vector<vpll>;
using vvtll = vector<vtll>;
using vvvll = vector<vvll>;
using vvvdd = vector<vvdd>;
using vvvpll = vector<vvpll>;
using vvvtll = vector<vvtll>;
using vvvvll = vector<vvvll>;
using vvvvdd = vector<vvvdd>;
using vvvvpll = vector<vvvpll>;
using vvvvtll = vector<vvvtll>;
constexpr ll INF = 1LL << 60;
constexpr dd EPS = 1e-11;
constexpr dd PI = 3.1415926535897932;
// cin,cout高速化のおまじない+桁数指定
struct Fast {
Fast() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
}
} fast;
#define REPS(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, S, E) for (ll i = (S); i <= (E); i++)
#define REP(i, N) REPS(i, 0, (N)-1)
#define DEPS(i, S, E) for (ll i = (E); i >= (S); i--)
#define dep(i, E, S) for (ll i = (E); i >= (S); i--)
#define DEP(i, N) DEPS(i, 0, (N)-1)
#define EACH(e, v) for (auto &&e : v)
#define each(e, v) for (auto &&e : v)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline T MaxE(vector<T> &v, ll S, ll E) {
T m = v[S];
REPS(i, S, E) chmax(m, v[i]);
return m;
} // v[S]~v[E]の最大値
template <class T> inline T MinE(vector<T> &v, ll S, ll E) {
T m = v[S];
REPS(i, S, E) chmin(m, v[i]);
return m;
} // v[S]~v[E]の最小値
template <class T> inline T MaxE(vector<T> &v, ll N) {
return MaxE(v, 0, N - 1);
} // 先頭N個中の最大値
template <class T> inline T MinE(vector<T> &v, ll N) {
return MinE(v, 0, N - 1);
}
template <class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); }
template <class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); }
template <class T> inline ll MaxI(vector<T> &v, ll S, ll E) {
ll m = S;
REPS(i, S, E) {
if (v[i] > v[m])
m = i;
}
return m;
}
template <class T> inline ll MinI(vector<T> &v, ll S, ll E) {
ll m = S;
REPS(i, S, E) {
if (v[i] < v[m])
m = i;
}
return m;
}
template <class T> inline ll MaxI(vector<T> &v, ll N) {
return MaxI(v, 0, N - 1);
}
template <class T> inline ll MinI(vector<T> &v, ll N) {
return MinI(v, 0, N - 1);
}
template <class T> inline ll MaxI(vector<T> &v) {
return MaxI(v, (ll)v.size());
}
template <class T> inline ll MinI(vector<T> &v) {
return MinI(v, (ll)v.size());
}
template <class T> inline T Sum(vector<T> &v, ll S, ll E) {
T s = v[S];
REPS(i, S + 1, E) s += v[i];
return s;
}
template <class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); }
template <class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); }
template <class T> inline ll sz(T &v) { return (ll)v.size(); }
template <class T> inline T POW(T a, ll n) {
T r = 1;
for (; n > 0; n >>= 1, a *= a) {
if (n & 1)
r *= a;
}
return r;
}
inline ll POW(int a, ll n) { return POW((ll)a, n); }
inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }
ll Gcd(ll a, ll b) { return (b == 0) ? a : Gcd(b, a % b); }
ll Lcm(ll a, ll b) { return a * b / Gcd(a, b); }
inline ll MSB(ll a) {
for (ll o = 63, x = -1;;) {
ll m = (o + x) / 2;
if (a < (1LL << m))
o = m;
else
x = m;
if (o - x == 1)
return x;
}
}
inline ll Upper2N(ll s) { return 1LL << MSB(s) << 1; } // s<2^nなる最小の2^n
inline vll Bit2Idx(ll m) {
vll v;
for (ll i = 0; m; m >>= 1, i++)
if (m & 1)
v.push_back(i);
return move(v);
}
inline ll BitNum(ll m) {
for (ll c = 0;; m >>= 1) {
c += m & 1;
if (!m)
return c;
}
}
inline ll Bit(ll s, ll i) { return (s >> i) & 1; } // sの第ibit i=0-63
[[nodiscard]] inline ll BitOn(ll s, ll i) {
return s | (1LL << i);
} // sの第ibitON i=0-63
[[nodiscard]] inline ll BitOff(ll s, ll i) {
return s & ~(1LL << i);
} // sの第ibitOFF i=0-63
[[nodiscard]] inline ll BitCut(ll s, ll i) {
return s & (1LL << ++i) - 1;
} // sの第0~第ibitを抽出
inline vll str2num(string &s, ll N) {
vll v(N);
REP(i, N) v[i] = (ll)(s[i] - '0');
return move(v);
}
inline vll ALP2num(string &s, ll N) {
vll v(N);
REP(i, N) v[i] = (ll)(s[i] - 'A');
return move(v);
}
inline vll alp2num(string &s, ll N) {
vll v(N);
REP(i, N) v[i] = (ll)(s[i] - 'a');
return move(v);
}
template <class T> inline ll FloorIdx(vector<T> &v, T x) {
return upper_bound(ALL(v), x) - v.begin() - 1;
}
template <class T> inline ll CeilIdx(vector<T> &v, T x) {
return lower_bound(ALL(v), x) - v.begin();
}
template <class T>
inline ll UnderNumOf(vector<T> &v, T x) { // sort済vのx以下の個数を得る
return upper_bound(v.begin(), v.end(), x) - v.begin();
}
template <class T, class Pr>
inline ll UnderNumOf(vector<T> &v, T x, Pr pred) { // x以下個数
return upper_bound(v.begin(), v.end(), x, pred) - v.begin();
}
template <class T>
inline ll OverNumOf(vector<T> &v, T x) { // sort済vのx以上の個数を得る
return (ll)v.size() - (lower_bound(v.begin(), v.end(), x) - v.begin());
}
template <class T, class Pr>
inline ll OverNumOf(vector<T> &v, T x, Pr pred) { // x以上の個数
return (ll)v.size() - (lower_bound(v.begin(), v.end(), x, pred) - v.begin());
}
template <class T = ll> inline vector<T> cinv(ll N) {
vector<T> v(N);
REP(i, N) cin >> v[i];
return move(v);
}
template <class T, class S> inline vector<pair<T, S>> cinv2(ll N) {
vector<pair<T, S>> v(N);
REP(i, N) { cin >> v[i].first >> v[i].second; }
return move(v);
}
template <class T> inline vector<T> cinv2(ll N) {
vector<T> v(N);
REP(i, N) {
ll a, b;
cin >> a >> b;
v[i] = {a, b};
}
return move(v);
}
template <class T, class S, class R> inline vector<tuple<T, S, R>> cinv3(ll N) {
vector<tuple<T, S, R>> v(N);
REP(i, N) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]); }
return move(v);
}
template <class T> inline vector<T> cinv3(ll N) {
vector<T> v(N);
REP(i, N) {
ll a, b, c;
cin >> a >> b >> c;
v[i] = {a, b, c};
}
return move(v);
}
template <class T, class S, class R, class Q>
inline vector<tuple<T, S, R, Q>> cinv4(ll N) {
vector<tuple<T, S, R, Q>> v(N);
REP(i, N) {
cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]) >> get<3>(v[i]);
}
return move(v);
}
template <class T> inline vector<T> cinv4(ll N) {
vector<T> v(N);
REP(i, N) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
v[i] = {a, b, c, d};
}
return move(v);
}
template <class T = ll> inline vector<vector<T>> cinvv(ll N, ll M) {
vector<vector<T>> vv(N);
REP(i, N) vv[i] = cinv(M);
return move(vv);
}
template <class T> inline void coutv(vector<T> &v, string d = " ") {
ll N = (ll)v.size();
REP(i, N) { cout << v[i] << ((i == N - 1) ? "" : d); }
cout << '\n';
}
template <class T> inline void coutv(deque<T> &v, string d = " ") {
ll N = (ll)v.size();
REP(i, N) { cout << v[i] << ((i == N - 1) ? "" : d); }
cout << '\n';
}
template <class T> void bye(T a) {
cout << a << '\n';
exit(0);
}
#if defined(_DEBUG)
ll dumpW = 5;
template <class T> void Dump(vector<T> &v) {
REP(i, (ll)v.size()) {
cerr << ((i == 0) ? "[" : " ") << setw(dumpW) << v[i];
}
cerr << "]";
}
void Dump(vll &v) {
REP(i, (ll)v.size()) {
cerr << ((i == 0) ? "[" : " ") << setw(dumpW);
if (v[i] == INF)
cerr << "INF";
else if (v[i] == -INF)
cerr << "-INF";
else
cerr << v[i];
}
cerr << "]";
}
template <class T> void Dump(vector<vector<T>> &v) {
ll N = (ll)v.size();
string d[2][2] = {{" ", "["}, {"\n", "]\n"}};
REP(i, N) {
cerr << d[0][i == 0];
Dump(v[i]);
cerr << d[1][i == N - 1];
}
}
template <class T> void Dump(vector<vector<vector<T>>> &v) {
ll N = (ll)v.size();
string d[2] = {" <", "[ <"};
REP(i, N) {
cerr << d[i == 0] << i << ">\n";
Dump(v[i]);
}
cerr << "]\n";
}
template <class T> void Dump(vector<vector<vector<vector<T>>>> &v) {
ll N = (ll)v.size();
string d[2] = {" ---<", "[ ---<"};
REP(i, N) {
cerr << d[i == 0] << i << ">---\n";
Dump(v[i]);
}
cerr << "]\n";
}
template <class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v) {
ll N = (ll)v.size();
string d[2] = {" ======<", "[ ======<"};
REP(i, N) {
cerr << d[i == 0] << i << ">======\n";
Dump(v[i]);
}
cerr << "]\n";
}
template <class T, class S> void Dump(map<T, S> &m) {
for (auto e : m)
cout << e.first << " : " << e.second << '\n';
}
#else
template <class T> void Dump(vector<T> &v) {}
template <class T> void Dump(vector<vector<T>> &v) {}
template <class T> void Dump(vector<vector<vector<T>>> &v) {}
template <class T> void Dump(vector<vector<vector<vector<T>>>> &v) {}
template <class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v) {}
#endif
template <ll MOD> struct mll_ {
ll val;
mll_(ll v = 0) : val(v % MOD) {
if (val < 0)
val += MOD;
}
mll_ operator-() const { return -val; }
mll_ operator+(const mll_ &b) const { return val + b.val; }
mll_ operator-(const mll_ &b) const { return val - b.val; }
mll_ operator*(const mll_ &b) const { return val * b.val; }
mll_ operator/(const mll_ &b) const { return mll_(*this) /= b; }
mll_ operator+(ll b) const { return *this + mll_(b); }
mll_ operator-(ll b) const { return *this - mll_(b); }
mll_ operator*(ll b) const { return *this * mll_(b); }
friend mll_ operator+(ll a, const mll_ &b) { return b + a; }
friend mll_ operator-(ll a, const mll_ &b) { return -b + a; }
friend mll_ operator*(ll a, const mll_ &b) { return b * a; }
friend mll_ operator/(ll a, const mll_ &b) { return mll_(a) / b; }
mll_ &operator+=(const mll_ &b) {
val = (val + b.val) % MOD;
return *this;
}
mll_ &operator-=(const mll_ &b) {
val = (val + MOD - b.val) % MOD;
return *this;
}
mll_ &operator*=(const mll_ &b) {
val = (val * b.val) % MOD;
return *this;
}
mll_ &operator/=(const mll_ &b) {
ll c = b.val, d = MOD, u = 1, v = 0;
while (d) {
ll t = c / d;
c -= t * d;
swap(c, d);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
mll_ &operator+=(ll b) { return *this += mll_(b); }
mll_ &operator-=(ll b) { return *this -= mll_(b); }
mll_ &operator*=(ll b) { return *this *= mll_(b); }
mll_ &operator/=(ll b) { return *this /= mll_(b); }
bool operator==(const mll_ &b) { return val == b.val; }
bool operator!=(const mll_ &b) { return val != b.val; }
bool operator==(ll b) { return *this == mll_(b); }
bool operator!=(ll b) { return *this != mll_(b); }
friend bool operator==(ll a, const mll_ &b) { return mll_(a) == b.val; }
friend bool operator!=(ll a, const mll_ &b) { return mll_(a) != b.val; }
friend ostream &operator<<(ostream &os, const mll_ &a) { return os << a.val; }
friend istream &operator>>(istream &is, mll_ &a) { return is >> a.val; }
static mll_ Combination(ll a, ll b) {
chmin(b, a - b);
if (b < 0)
return mll_(0);
mll_ c = 1;
REP(i, b) c *= a - i;
REP(i, b) c /= i + 1;
return c;
}
};
using mll = mll_<1000000007LL>; // 1000000007LL;// 998244353LL;
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;
using vvvvvmll = std::vector<vvvvmll>;
void solve() {
ll n;
cin >> n;
vector<dd> a_ = cinv<dd>(n);
each(e, a_) e *= 1000000000.0;
vll aa(n);
rep(i, 0, n - 1) { aa[i] = (ll)(a_[i] + .1); }
const ll M = 18;
vvll cnt(M + 1, vll(M + 1));
auto Cnt = [&](ll i, ll j) -> ll & {
if (i > M)
i = M;
if (j > M)
j = M;
return cnt[M - i][M - j];
};
vpll buf;
rep(i, 0, n - 1) {
ll v = aa[i];
ll cn2 = 0;
while (true) {
if (v % 2 == 0) {
v /= 2;
cn2++;
} else
break;
}
ll cn5 = 0;
while (true) {
if (v % 5 == 0) {
v /= 5;
cn5++;
} else
break;
}
Cnt(cn2, cn5)++;
buf.emplace_back(cn2, cn5);
}
rep(i, 0, M) {
rep(j, 1, M) { cnt[i][j] += cnt[i][j - 1]; }
}
rep(j, 0, M) rep(i, 1, M) { cnt[i][j] += cnt[i - 1][j]; }
ll ans = 0;
each(e, buf) {
ll cn2;
ll cn5;
tie(cn2, cn5) = e;
ll r2 = M - cn2;
ll r5 = M - cn5;
if (r2 < 0)
r2 = 0;
if (r5 < 0)
r5 = 0;
ll val = Cnt(r2, r5);
ans += val;
if (cn2 >= M / 2 and cn5 >= M / 2)
ans--;
}
ans /= 2;
cout << ans << '\n';
}
int main() {
#if 1
solve();
#else
ll t;
cin >> t;
rep(i, 0, t - 1) { solve(); }
#endif
return 0;
}
| insert | 467 | 467 | 467 | 471 | 0 | |
p02588 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX2 50
#define MAX5 30
int g[10][MAX2][MAX5];
char s[20];
int32_t main() {
memset(g, 0, sizeof g);
int pot10[10];
pot10[0] = 1;
for (int i = 1; i <= 9; ++i)
pot10[i] = pot10[i - 1] * 10;
int n;
scanf("%lld", &n);
int ans = 0;
for (int i = 0; i < n; ++i) {
scanf("%s", s);
string a, b;
bool ponto = false;
for (int j = 0; s[j]; ++j) {
if (s[j] == '.')
ponto = true;
else {
if (!ponto)
a.push_back(s[j]);
else
b.push_back(s[j]);
}
}
string c = a + b;
int x = stoll(c);
int qntdFats2 = 0;
int qntdFats5 = 0;
while (x) {
if (x % 2 == 0) {
qntdFats2++;
x /= 2;
} else if (x % 5 == 0) {
qntdFats5++;
x /= 5;
} else {
break;
}
}
int numCasas = b.size();
int qntdZeros = min(qntdFats2, qntdFats5);
if (numCasas > 0 && qntdZeros > 0) {
qntdFats2 -= min(numCasas, qntdZeros);
qntdFats5 -= min(numCasas, qntdZeros);
numCasas -= min(numCasas, qntdZeros);
}
// cerr << "String fats2 fats5 casas\n";
// cerr << s << " " << qntdFats2 << " " << qntdFats5 << " " << numCasas <<
// endl;
// query
// se numCasas > 0, ou qntdFats2 > 0 ou qntdFats5 > 0
// quero
for (int i = 0; i < MAX2; ++i) {
for (int j = 0; j < MAX5; ++j) {
// i + qntdFats2 eh a qntd de fats2 no prod
// j + qntdFats5 eh a qntd de fats5 no prod
for (int k = 0; k < 10; ++k) {
if (g[k][i][j] == 0)
continue;
int prodFats2 = i + qntdFats2;
int prodFats5 = j + qntdFats5;
int casas = numCasas + k;
// cerr << casas << " " << prodFats2 << " " << prodFats5 << endl;
// cerr << g[k][i][j] << endl;
if (min(prodFats2, prodFats5) >= casas)
ans += g[k][i][j];
}
}
}
// update
for (int i = qntdFats2; i <= qntdFats2; ++i) {
for (int j = qntdFats5; j <= qntdFats5; ++j) {
g[numCasas][i][j]++;
}
}
}
printf("%lld\n", ans);
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX2 47
#define MAX5 23
int g[10][MAX2][MAX5];
char s[20];
int32_t main() {
memset(g, 0, sizeof g);
int pot10[10];
pot10[0] = 1;
for (int i = 1; i <= 9; ++i)
pot10[i] = pot10[i - 1] * 10;
int n;
scanf("%lld", &n);
int ans = 0;
for (int i = 0; i < n; ++i) {
scanf("%s", s);
string a, b;
bool ponto = false;
for (int j = 0; s[j]; ++j) {
if (s[j] == '.')
ponto = true;
else {
if (!ponto)
a.push_back(s[j]);
else
b.push_back(s[j]);
}
}
string c = a + b;
int x = stoll(c);
int qntdFats2 = 0;
int qntdFats5 = 0;
while (x) {
if (x % 2 == 0) {
qntdFats2++;
x /= 2;
} else if (x % 5 == 0) {
qntdFats5++;
x /= 5;
} else {
break;
}
}
int numCasas = b.size();
int qntdZeros = min(qntdFats2, qntdFats5);
if (numCasas > 0 && qntdZeros > 0) {
qntdFats2 -= min(numCasas, qntdZeros);
qntdFats5 -= min(numCasas, qntdZeros);
numCasas -= min(numCasas, qntdZeros);
}
// cerr << "String fats2 fats5 casas\n";
// cerr << s << " " << qntdFats2 << " " << qntdFats5 << " " << numCasas <<
// endl;
// query
// se numCasas > 0, ou qntdFats2 > 0 ou qntdFats5 > 0
// quero
for (int i = 0; i < MAX2; ++i) {
for (int j = 0; j < MAX5; ++j) {
// i + qntdFats2 eh a qntd de fats2 no prod
// j + qntdFats5 eh a qntd de fats5 no prod
for (int k = 0; k < 10; ++k) {
if (g[k][i][j] == 0)
continue;
int prodFats2 = i + qntdFats2;
int prodFats5 = j + qntdFats5;
int casas = numCasas + k;
// cerr << casas << " " << prodFats2 << " " << prodFats5 << endl;
// cerr << g[k][i][j] << endl;
if (min(prodFats2, prodFats5) >= casas)
ans += g[k][i][j];
}
}
}
// update
for (int i = qntdFats2; i <= qntdFats2; ++i) {
for (int j = qntdFats5; j <= qntdFats5; ++j) {
g[numCasas][i][j]++;
}
}
}
printf("%lld\n", ans);
}
| replace | 4 | 6 | 4 | 6 | TLE | |
p02588 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <functional>
#include <iostream>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll, ll>
#define mod 1000000007 // 998244353
#define f(i, a, b) for (ll i = a; i < b; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll, vector<ll>, greater<ll>()>
using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
tree_order_statistics_node_update>
ost;
ll pow_mod(ll a, ll b) {
ll res = 1;
while (b != 0) {
if (b & 1) {
res = (res * a) % mod;
}
a = (a * a) % mod;
b /= 2;
}
return res;
}
void solve() {
ll n;
cin >> n;
ll a[n];
ll res = 0;
map<pii, ll> mp;
for (ll i = 0; i < n; i++) {
string s;
cin >> s;
ll cnt = 0;
ll sm = 0;
ll flag = 0;
for (ll j = 0; j < s.size(); j++) {
if (s[j] == '.') {
flag = 1;
continue;
}
if (flag)
cnt++;
ll x = (ll)(s[j] - '0');
sm *= 10;
sm += x;
}
ll p, q;
p = sm;
q = 1;
for (ll kk = 0; kk < cnt; kk++)
q *= 10;
ll p_2 = 0;
ll p_5 = 0;
ll q_2 = 0;
ll q_5 = 0;
while (p % 2 == 0) {
p_2++;
p /= 2;
}
while (p % 5 == 0) {
p_5++;
p /= 5;
}
while (q % 2 == 0) {
q_2++;
q /= 2;
}
while (q % 5 == 0) {
q_5++;
q /= 5;
}
ll del2 = q_2 - p_2;
ll del5 = q_5 - p_5;
ll ans = 0;
for (ll j = del2; j <= 30; j++) {
for (ll h = del5; h <= 30; h++) {
ans += mp[{j, h}];
}
}
res += ans;
mp[{p_2 - q_2, p_5 - q_5}]++;
}
cout << res << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// Start from Here.
ll t;
t = 1;
// cin>>t;
while (t--)
solve();
// Good Bye!
return 0;
} | #include <bits/stdc++.h>
#include <functional>
#include <iostream>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll, ll>
#define mod 1000000007 // 998244353
#define f(i, a, b) for (ll i = a; i < b; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll, vector<ll>, greater<ll>()>
using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
tree_order_statistics_node_update>
ost;
ll pow_mod(ll a, ll b) {
ll res = 1;
while (b != 0) {
if (b & 1) {
res = (res * a) % mod;
}
a = (a * a) % mod;
b /= 2;
}
return res;
}
void solve() {
ll n;
cin >> n;
ll a[n];
ll res = 0;
map<pii, ll> mp;
for (ll i = 0; i < n; i++) {
string s;
cin >> s;
ll cnt = 0;
ll sm = 0;
ll flag = 0;
for (ll j = 0; j < s.size(); j++) {
if (s[j] == '.') {
flag = 1;
continue;
}
if (flag)
cnt++;
ll x = (ll)(s[j] - '0');
sm *= 10;
sm += x;
}
ll p, q;
p = sm;
q = 1;
for (ll kk = 0; kk < cnt; kk++)
q *= 10;
ll p_2 = 0;
ll p_5 = 0;
ll q_2 = 0;
ll q_5 = 0;
while (p % 2 == 0) {
p_2++;
p /= 2;
}
while (p % 5 == 0) {
p_5++;
p /= 5;
}
while (q % 2 == 0) {
q_2++;
q /= 2;
}
while (q % 5 == 0) {
q_5++;
q /= 5;
}
ll del2 = q_2 - p_2;
ll del5 = q_5 - p_5;
ll ans = 0;
for (ll j = del2; j <= 30; j++) {
for (ll h = del5; h <= 9; h++) {
ans += mp[{j, h}];
}
}
res += ans;
mp[{p_2 - q_2, p_5 - q_5}]++;
}
cout << res << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// Start from Here.
ll t;
t = 1;
// cin>>t;
while (t--)
solve();
// Good Bye!
return 0;
}
| replace | 103 | 104 | 103 | 104 | TLE | |
p02588 | C++ | Runtime Error | #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
using namespace std;
const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;
double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; }
std::istream &operator>>(std::istream &in, set<int> &o) {
int a;
in >> a;
o.insert(a);
return in;
}
std::istream &operator>>(std::istream &in, queue<int> &o) {
ll a;
in >> a;
o.push(a);
return in;
}
bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }
// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
// std::cout << std::bitset<8>(9);
// const ll mod = 1e10;
typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;
bool nine(string &s) {
int i = distance(find(s.begin(), s.end(), '.'), s.end());
return i == 10;
}
vector<ll> up(vector<string> &v) {
vector<ll> res;
for (string s : v) {
if (find(s.begin(), s.end(), '.') == s.end()) {
s += ".0";
}
while (!nine(s)) {
s += "0";
}
ll a = 0;
for (char c : s) {
if (c == '.')
continue;
ll i = c - '0';
a *= 10;
a += i;
}
res.push_back(a);
}
return res;
}
vector<ll> sp(vector<ll> v, ll s) {
vector<ll> res;
for (ll l : v) {
int ans = 0;
while (l % s == 0) {
ans++;
l /= s;
}
res.push_back(ans);
}
return res;
}
// 2と5の両方が足して18になる組み合わせ
//
class MatrixSum {
vector<vector<ll>> sum;
public:
MatrixSum(ll x, ll y) { sum = vector<vector<ll>>(x, vector<ll>(y)); }
void add(ll x, ll y) { sum[x][y]++; }
ll get(ll x, ll y) {
if (x == -1 || y == -1) {
return 0;
}
if (x == sum.size() || y == sum[x].size()) {
return 0;
}
return sum[x][y];
}
void setUp() {
for (ll x = 0; x < sum.size(); x++) {
for (ll y = 0; y < sum[x].size(); y++) {
sum[x][y] += get(x - 1, y) + get(x, y - 1) - get(x - 1, y - 1);
}
}
}
ll getSum(ll xs, ll ys, ll xe, ll ye) {
return get(xe, ye) - get(xs - 1, ye) - get(xe, ys - 1) +
get(xs - 1, ys - 1);
}
};
int main() {
int n;
cin >> n;
vector<string> v(n);
rep(i, n) cin >> v[i];
vector<ll> v2 = up(v);
vector<ll> tows = sp(v2, 2);
vector<ll> fives = sp(v2, 5);
MatrixSum ms(200, 200);
rep(i, n) { ms.add(tows[i], fives[i]); }
ms.setUp();
ll ans = 0;
rep(i, n) {
int now = ms.getSum(18 - tows[i], 18 - fives[i], 150, 150);
ans += now;
}
// rep(i, n) {
// rep(j, n) {
// if (tows[i] + tows[j] >= 18 && fives[i] + fives[j] >= 18) {
// ans++;
// }
// }
// }
rep(i, n) {
if (tows[i] + tows[i] >= 18 && fives[i] + fives[i] >= 18) {
ans--;
}
}
ans /= 2;
cout << ans << endl;
}
| #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
using namespace std;
const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;
double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; }
std::istream &operator>>(std::istream &in, set<int> &o) {
int a;
in >> a;
o.insert(a);
return in;
}
std::istream &operator>>(std::istream &in, queue<int> &o) {
ll a;
in >> a;
o.push(a);
return in;
}
bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }
// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
// std::cout << std::bitset<8>(9);
// const ll mod = 1e10;
typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;
bool nine(string &s) {
int i = distance(find(s.begin(), s.end(), '.'), s.end());
return i == 10;
}
vector<ll> up(vector<string> &v) {
vector<ll> res;
for (string s : v) {
if (find(s.begin(), s.end(), '.') == s.end()) {
s += ".0";
}
while (!nine(s)) {
s += "0";
}
ll a = 0;
for (char c : s) {
if (c == '.')
continue;
ll i = c - '0';
a *= 10;
a += i;
}
res.push_back(a);
}
return res;
}
vector<ll> sp(vector<ll> v, ll s) {
vector<ll> res;
for (ll l : v) {
int ans = 0;
while (l % s == 0) {
ans++;
l /= s;
}
res.push_back(ans);
}
return res;
}
// 2と5の両方が足して18になる組み合わせ
//
class MatrixSum {
vector<vector<ll>> sum;
public:
MatrixSum(ll x, ll y) { sum = vector<vector<ll>>(x, vector<ll>(y)); }
void add(ll x, ll y) { sum[x][y]++; }
ll get(ll x, ll y) {
if (x == -1 || y == -1) {
return 0;
}
if (x == sum.size() || y == sum[x].size()) {
return 0;
}
return sum[x][y];
}
void setUp() {
for (ll x = 0; x < sum.size(); x++) {
for (ll y = 0; y < sum[x].size(); y++) {
sum[x][y] += get(x - 1, y) + get(x, y - 1) - get(x - 1, y - 1);
}
}
}
ll getSum(ll xs, ll ys, ll xe, ll ye) {
return get(xe, ye) - get(xs - 1, ye) - get(xe, ys - 1) +
get(xs - 1, ys - 1);
}
};
int main() {
int n;
cin >> n;
vector<string> v(n);
rep(i, n) cin >> v[i];
vector<ll> v2 = up(v);
vector<ll> tows = sp(v2, 2);
vector<ll> fives = sp(v2, 5);
MatrixSum ms(200, 200);
rep(i, n) { ms.add(tows[i], fives[i]); }
ms.setUp();
ll ans = 0;
rep(i, n) {
int now = ms.getSum(max<int>(18 - tows[i], 0), max<int>(18 - fives[i], 0),
150, 150);
ans += now;
}
// rep(i, n) {
// rep(j, n) {
// if (tows[i] + tows[j] >= 18 && fives[i] + fives[j] >= 18) {
// ans++;
// }
// }
// }
rep(i, n) {
if (tows[i] + tows[i] >= 18 && fives[i] + fives[i] >= 18) {
ans--;
}
}
ans /= 2;
cout << ans << endl;
}
| replace | 137 | 138 | 137 | 140 | 0 | |
p02589 | C++ | Runtime Error | #ifdef DBG1
#define _GLIBCXX_DEBUG
#endif
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#ifdef DBG1
#define dbg(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int P = 200003;
int sum(int a, int b) {
if ((a += b) >= P) {
a -= P;
}
return a;
}
int diff(int a, int b) {
if ((a -= b) < 0) {
a += P;
}
return a;
}
int prod(int a, int b) { return int(ll(a) * b % P); }
int power(int a, int n) {
int b = 1;
while (n) {
if (n & 1) {
b = prod(a, b);
}
a = prod(a, a);
n /= 2;
}
return b;
}
int inv(int a) { return power(a, P - 2); }
int quotient(int a, int b) { return prod(a, inv(b)); }
int &add(int &a, int b) { return a = sum(a, b); }
int &subtract(int &a, int b) { return a = diff(a, b); }
int &mult(int &a, int b) { return a = prod(a, b); }
int &div(int &a, int b) { return a = quotient(a, b); }
ll gcd(ll a, ll b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
bool solve() {
int n;
if (scanf("%d", &n) != 1) {
return false;
}
vector<string> w;
for (int i = 0; i < n; ++i) {
static char s[200100];
scanf("%s", s);
w.emplace_back(s);
}
auto calc_suffix_hash = [](const string &s, int len) -> ll {
const int P1 = int(1e6) + 3;
const int P2 = 997;
const int MOD = int(1e9) + 7;
ll hash1 = 0;
ll hash2 = 0;
assert(len < s.length());
for (int i = 0; i < len; ++i) {
hash1 = (hash1 * P1 + s[s.length() - i - 1]) % MOD;
hash2 = (hash2 * P2 + s[s.length() - i - 1]) % MOD;
}
return (hash1 << 32) | hash2;
};
sort(w.begin(), w.end(), [](const auto &s1, const auto &s2) {
return s1.length() < s2.length();
});
ll res = 0;
vector<vector<int>> mask(n);
for (int i = 0; i < n; ++i) {
auto &s = w[i];
auto &v = mask[i];
v.resize(s.length());
for (int j = 0; j < (int)s.length(); ++j) {
if (j)
v[j] = v[j - 1];
v[j] |= (1 << (s[j] - 'a'));
}
}
for (int i = 0; i < n;) {
int j = i;
while (j < n && w[i].length() == w[j].length()) {
j++;
}
int len = (int)w[i].length();
vector<pair<pair<ll, char>, int>> hashes;
hashes.reserve(j - i);
for (int k = i; k < j; ++k) {
hashes.emplace_back(make_pair(calc_suffix_hash(w[k], len - 1), w[k][0]),
1);
}
sort(hashes.begin(), hashes.end());
int size = 1;
for (int k = 1; k < int(hashes.size()); ++k) {
if (hashes[size - 1].first == hashes[k].first) {
hashes[size - 1].second++;
} else {
hashes[size++] = hashes[k];
}
}
hashes.resize(size);
for (int k = j; k < n; ++k) {
auto suffix_hash = calc_suffix_hash(w[k], len - 1);
auto ptr = lower_bound(hashes.begin(), hashes.end(),
make_pair(make_pair(suffix_hash, 'a'), 0));
while (ptr != hashes.end() && ptr->first.first == suffix_hash) {
if (mask[k][w[k].length() - len] & (1 << (ptr->first.second - 'a'))) {
res += ptr->second;
}
++ptr;
}
}
i = j;
}
printf("%lld\n", res);
return true;
}
int main() {
while (solve()) {
}
return 0;
}
| #ifdef DBG1
#define _GLIBCXX_DEBUG
#endif
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#ifdef DBG1
#define dbg(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int P = 200003;
int sum(int a, int b) {
if ((a += b) >= P) {
a -= P;
}
return a;
}
int diff(int a, int b) {
if ((a -= b) < 0) {
a += P;
}
return a;
}
int prod(int a, int b) { return int(ll(a) * b % P); }
int power(int a, int n) {
int b = 1;
while (n) {
if (n & 1) {
b = prod(a, b);
}
a = prod(a, a);
n /= 2;
}
return b;
}
int inv(int a) { return power(a, P - 2); }
int quotient(int a, int b) { return prod(a, inv(b)); }
int &add(int &a, int b) { return a = sum(a, b); }
int &subtract(int &a, int b) { return a = diff(a, b); }
int &mult(int &a, int b) { return a = prod(a, b); }
int &div(int &a, int b) { return a = quotient(a, b); }
ll gcd(ll a, ll b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
bool solve() {
int n;
if (scanf("%d", &n) != 1) {
return false;
}
vector<string> w;
for (int i = 0; i < n; ++i) {
static char s[1000100];
scanf("%s", s);
w.emplace_back(s);
}
auto calc_suffix_hash = [](const string &s, int len) -> ll {
const int P1 = int(1e6) + 3;
const int P2 = 997;
const int MOD = int(1e9) + 7;
ll hash1 = 0;
ll hash2 = 0;
assert(len < s.length());
for (int i = 0; i < len; ++i) {
hash1 = (hash1 * P1 + s[s.length() - i - 1]) % MOD;
hash2 = (hash2 * P2 + s[s.length() - i - 1]) % MOD;
}
return (hash1 << 32) | hash2;
};
sort(w.begin(), w.end(), [](const auto &s1, const auto &s2) {
return s1.length() < s2.length();
});
ll res = 0;
vector<vector<int>> mask(n);
for (int i = 0; i < n; ++i) {
auto &s = w[i];
auto &v = mask[i];
v.resize(s.length());
for (int j = 0; j < (int)s.length(); ++j) {
if (j)
v[j] = v[j - 1];
v[j] |= (1 << (s[j] - 'a'));
}
}
for (int i = 0; i < n;) {
int j = i;
while (j < n && w[i].length() == w[j].length()) {
j++;
}
int len = (int)w[i].length();
vector<pair<pair<ll, char>, int>> hashes;
hashes.reserve(j - i);
for (int k = i; k < j; ++k) {
hashes.emplace_back(make_pair(calc_suffix_hash(w[k], len - 1), w[k][0]),
1);
}
sort(hashes.begin(), hashes.end());
int size = 1;
for (int k = 1; k < int(hashes.size()); ++k) {
if (hashes[size - 1].first == hashes[k].first) {
hashes[size - 1].second++;
} else {
hashes[size++] = hashes[k];
}
}
hashes.resize(size);
for (int k = j; k < n; ++k) {
auto suffix_hash = calc_suffix_hash(w[k], len - 1);
auto ptr = lower_bound(hashes.begin(), hashes.end(),
make_pair(make_pair(suffix_hash, 'a'), 0));
while (ptr != hashes.end() && ptr->first.first == suffix_hash) {
if (mask[k][w[k].length() - len] & (1 << (ptr->first.second - 'a'))) {
res += ptr->second;
}
++ptr;
}
}
i = j;
}
printf("%lld\n", res);
return true;
}
int main() {
while (solve()) {
}
return 0;
}
| replace | 86 | 87 | 86 | 87 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_poizcy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)
using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<double, double> iidb;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
const ll MOD = 1e9 + 7;
#define initseg ll new_nodo = (nodo * 2), mid = (iz + der) / 2;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 400
#define offset 200
#define ptr nodo *
struct nodo {
map<char, ptr> hijos;
int mapa[30];
};
nodo basura[300000];
int cnodo = 0;
ptr getnodo() {
memset(basura[cnodo].mapa, 0, sizeof basura[cnodo].mapa);
return &basura[cnodo++];
}
int mapa[tam];
void add(ptr root) {
forr(i, 0, 27) {
if (mapa[i] != -1)
root->mapa[i]++;
}
}
void insertar(ptr root, string &txt) {
//<<txt<<endl;
forr(i, 0, txt.size()) {
// print();
//<<root<<":";
add(root);
if (mapa[txt[i]] == i)
mapa[txt[i]] = -1;
if (root->hijos.find(txt[i]) == root->hijos.end())
root->hijos[txt[i]] = getnodo();
root = root->hijos[txt[i]];
}
//<<endl<<endl;
}
ll tot = 0;
void query(ptr root, string &txt) {
forr(i, 0, txt.size() - 1) { root = root->hijos[txt[i]]; }
tot += max(0, root->mapa[txt.back()] - 1);
//<<int(txt.back())<<" "<<root->mapa[txt.back()]<<" ";
//<<tot<<"endl\n";
}
int main() {
int n;
cin >> n;
vector<string> lst;
ptr root = getnodo();
forr(i, 0, n) {
string txt;
cin >> txt;
forr(j, 0, txt.size()) txt[j] -= 'a';
reverse(all(txt));
memset(mapa, -1, sizeof mapa);
forr(j, 0, txt.size()) mapa[txt[j]] = j;
insertar(root, txt);
lst.pb(txt);
}
for (string txt : lst) {
query(root, txt);
}
cout << tot << endl;
} | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_poizcy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)
using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<double, double> iidb;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
const ll MOD = 1e9 + 7;
#define initseg ll new_nodo = (nodo * 2), mid = (iz + der) / 2;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 400
#define offset 200
#define ptr nodo *
struct nodo {
map<char, ptr> hijos;
int mapa[30];
};
nodo basura[1200000];
int cnodo = 0;
ptr getnodo() {
memset(basura[cnodo].mapa, 0, sizeof basura[cnodo].mapa);
return &basura[cnodo++];
}
int mapa[tam];
void add(ptr root) {
forr(i, 0, 27) {
if (mapa[i] != -1)
root->mapa[i]++;
}
}
void insertar(ptr root, string &txt) {
//<<txt<<endl;
forr(i, 0, txt.size()) {
// print();
//<<root<<":";
add(root);
if (mapa[txt[i]] == i)
mapa[txt[i]] = -1;
if (root->hijos.find(txt[i]) == root->hijos.end())
root->hijos[txt[i]] = getnodo();
root = root->hijos[txt[i]];
}
//<<endl<<endl;
}
ll tot = 0;
void query(ptr root, string &txt) {
forr(i, 0, txt.size() - 1) { root = root->hijos[txt[i]]; }
tot += max(0, root->mapa[txt.back()] - 1);
//<<int(txt.back())<<" "<<root->mapa[txt.back()]<<" ";
//<<tot<<"endl\n";
}
int main() {
int n;
cin >> n;
vector<string> lst;
ptr root = getnodo();
forr(i, 0, n) {
string txt;
cin >> txt;
forr(j, 0, txt.size()) txt[j] -= 'a';
reverse(all(txt));
memset(mapa, -1, sizeof mapa);
forr(j, 0, txt.size()) mapa[txt[j]] = j;
insertar(root, txt);
lst.pb(txt);
}
for (string txt : lst) {
query(root, txt);
}
cout << tot << endl;
} | replace | 41 | 42 | 41 | 42 | 0 | |
p02589 | C++ | Runtime Error | #include <cstdio>
int n;
int trieroot;
int trien;
int triep[1001000];
int trier[1001000][26];
int triec[1001000];
int triecnt[1001000];
int triercnt[1001000][26];
char s[1001000];
int pd[100100];
int newtn(int prt, int c) {
triep[trien] = prt;
triec[trien] = c;
trien++;
return trien - 1;
}
int main() {
scanf("%d", &n);
trien = 1;
trieroot = newtn(0, 0);
for (int i = 0; i < n; i++) {
scanf("%s", s);
int x = trieroot;
int len = 0;
for (len = 0; s[len]; len++)
;
for (int j = len - 1; j >= 0; j--) {
int d = s[j] - 'a';
if (trier[x][d] == 0) {
trier[x][d] = newtn(x, d);
}
x = trier[x][d];
}
triecnt[x]++;
pd[i] = x;
}
for (int i = trien - 1; i >= 1; i--) {
triecnt[triep[i]] += triecnt[i];
for (int j = 0; j < 26; j++) {
if (j == triec[i]) {
triercnt[triep[i]][j] += triecnt[i];
} else {
triercnt[triep[i]][j] += triercnt[i][j];
}
}
}
long long int ans = 0;
for (int i = 0; i < n; i++) {
int x = pd[i];
int y = triep[x];
int d = triec[x];
ans += triercnt[y][d] - 1;
}
printf("%lld\n", ans);
return 0;
}
| #include <cstdio>
int n;
int trieroot;
int trien;
int triep[1001000];
int trier[1001000][26];
int triec[1001000];
int triecnt[1001000];
int triercnt[1001000][26];
char s[1001000];
int pd[200100];
int newtn(int prt, int c) {
triep[trien] = prt;
triec[trien] = c;
trien++;
return trien - 1;
}
int main() {
scanf("%d", &n);
trien = 1;
trieroot = newtn(0, 0);
for (int i = 0; i < n; i++) {
scanf("%s", s);
int x = trieroot;
int len = 0;
for (len = 0; s[len]; len++)
;
for (int j = len - 1; j >= 0; j--) {
int d = s[j] - 'a';
if (trier[x][d] == 0) {
trier[x][d] = newtn(x, d);
}
x = trier[x][d];
}
triecnt[x]++;
pd[i] = x;
}
for (int i = trien - 1; i >= 1; i--) {
triecnt[triep[i]] += triecnt[i];
for (int j = 0; j < 26; j++) {
if (j == triec[i]) {
triercnt[triep[i]][j] += triecnt[i];
} else {
triercnt[triep[i]][j] += triercnt[i][j];
}
}
}
long long int ans = 0;
for (int i = 0; i < n; i++) {
int x = pd[i];
int y = triep[x];
int d = triec[x];
ans += triercnt[y][d] - 1;
}
printf("%lld\n", ans);
return 0;
}
| replace | 10 | 11 | 10 | 11 | -11 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
#define sz(x) ((int)(x).size())
using namespace std;
typedef long long ll;
const int n = 1e6 + 5;
int N, M, K;
struct node {
int eos = 0, chi = 0;
int cc[26];
int c[26];
};
int root, NC = 0;
node nodes[n];
inline int getIndex(char c) { return c - 'a'; }
int createNode() {
int r = NC++;
for (int i = 0; i < 26; i++)
nodes[r].c[i] = -1, nodes[r].cc[i] = 0;
return r;
}
void insertString(string &s, int i, int n = root) {
nodes[n].chi++;
if (i == -1) {
nodes[n].eos++;
return;
}
if (nodes[n].c[getIndex(s[i])] == -1)
nodes[n].c[getIndex(s[i])] = createNode();
// cerr << "insert " << s << " i: " << i << " cc: " << (n->chi) <<endl;
insertString(s, i - 1, nodes[n].c[getIndex(s[i])]);
// n->cc[getIndex(s[i])]++;
}
void init() { root = createNode(); }
void doDP(int n = root) {
for (int i = 0; i < 26; ++i) {
if (nodes[n].c[i] != -1) {
doDP(nodes[n].c[i]);
for (int j = 0; j < 26; ++j) {
int cv = nodes[nodes[n].c[i]].cc[j];
if (j == i)
cv = nodes[nodes[n].c[i]].chi;
// if(cv > 0) cerr << "cv: " << cv << endl;
nodes[n].cc[j] += cv;
}
}
}
}
int query(string s, int i, int n = root) {
if (i == 0) {
return nodes[n].cc[getIndex(s[i])];
}
if (nodes[n].c[getIndex(s[i])] == -1)
return 0;
else
return query(s, i - 1, nodes[n].c[getIndex(s[i])]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;
vector<string> V;
init();
for (int i = 0; i < N; ++i) {
string s;
cin >> s;
V.push_back(s);
insertString(s, sz(s) - 1);
}
doDP();
ll cc = 0;
for (string s : V) {
int v = query(s, sz(s) - 1);
// cerr << s << ": " << v << endl;
cc += v - 1;
}
cout << cc << endl;
return 0;
}
| #include <bits/stdc++.h>
#define sz(x) ((int)(x).size())
using namespace std;
typedef long long ll;
const int n = 1e6 + 5;
int N, M, K;
struct node {
int eos = 0, chi = 0;
int cc[26];
int c[26];
};
int root, NC = 0;
node nodes[n];
inline int getIndex(char c) { return c - 'a'; }
int createNode() {
int r = NC++;
for (int i = 0; i < 26; i++)
nodes[r].c[i] = -1, nodes[r].cc[i] = 0;
return r;
}
void insertString(string &s, int i, int n = root) {
nodes[n].chi++;
if (i == -1) {
nodes[n].eos++;
return;
}
if (nodes[n].c[getIndex(s[i])] == -1)
nodes[n].c[getIndex(s[i])] = createNode();
// cerr << "insert " << s << " i: " << i << " cc: " << (n->chi) <<endl;
insertString(s, i - 1, nodes[n].c[getIndex(s[i])]);
// n->cc[getIndex(s[i])]++;
}
void init() { root = createNode(); }
void doDP(int n = root) {
for (int i = 0; i < 26; ++i) {
if (nodes[n].c[i] != -1) {
doDP(nodes[n].c[i]);
for (int j = 0; j < 26; ++j) {
int cv = nodes[nodes[n].c[i]].cc[j];
if (j == i)
cv = nodes[nodes[n].c[i]].chi;
// if(cv > 0) cerr << "cv: " << cv << endl;
nodes[n].cc[j] += cv;
}
}
}
}
int query(string &s, int i, int n = root) {
if (i == 0) {
return nodes[n].cc[getIndex(s[i])];
}
if (nodes[n].c[getIndex(s[i])] == -1)
return 0;
else
return query(s, i - 1, nodes[n].c[getIndex(s[i])]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;
vector<string> V;
init();
for (int i = 0; i < N; ++i) {
string s;
cin >> s;
V.push_back(s);
insertString(s, sz(s) - 1);
}
doDP();
ll cc = 0;
for (string s : V) {
int v = query(s, sz(s) - 1);
// cerr << s << ": " << v << endl;
cc += v - 1;
}
cout << cc << endl;
return 0;
}
| replace | 56 | 57 | 56 | 57 | -11 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MAXN 200010
#define LEN 1000010
int n, tot = 1;
int son[LEN][26], cnt[LEN], alpha[MAXN][26];
unordered_map<int, int> val[LEN];
char tmp[MAXN];
string s[MAXN];
long long ans, mul;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", tmp + 1);
s[i] = (tmp + 1);
int m = strlen(tmp + 1), now = 1, las;
for (int j = m; j >= 1; --j) {
las = now;
if (!son[now][tmp[j] - 'a'])
son[now][tmp[j] - 'a'] = ++tot;
now = son[now][tmp[j] - 'a'];
++alpha[i][tmp[j] - 'a'];
}
++val[las][tmp[1] - 'a'];
++cnt[now];
}
for (int i = 1; i <= n; ++i) {
int m = s[i].length(), now = 1;
for (auto v : val[now]) {
if (alpha[i][v.first])
ans += v.second;
}
for (int j = m - 1; j >= 0; --j) {
now = son[now][s[i][j] - 'a'];
--alpha[i][s[i][j] - 'a'];
for (auto v : val[now]) {
if (alpha[i][v.first])
ans += v.second;
}
}
mul += cnt[now];
}
printf("%lld\n", ans - n);
} | #include <bits/stdc++.h>
using namespace std;
#define MAXN 200010
#define LEN 1000010
int n, tot = 1;
int son[LEN][26], cnt[LEN], alpha[MAXN][26];
unordered_map<int, int> val[LEN];
char tmp[LEN];
string s[MAXN];
long long ans, mul;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", tmp + 1);
s[i] = (tmp + 1);
int m = strlen(tmp + 1), now = 1, las;
for (int j = m; j >= 1; --j) {
las = now;
if (!son[now][tmp[j] - 'a'])
son[now][tmp[j] - 'a'] = ++tot;
now = son[now][tmp[j] - 'a'];
++alpha[i][tmp[j] - 'a'];
}
++val[las][tmp[1] - 'a'];
++cnt[now];
}
for (int i = 1; i <= n; ++i) {
int m = s[i].length(), now = 1;
for (auto v : val[now]) {
if (alpha[i][v.first])
ans += v.second;
}
for (int j = m - 1; j >= 0; --j) {
now = son[now][s[i][j] - 'a'];
--alpha[i][s[i][j] - 'a'];
for (auto v : val[now]) {
if (alpha[i][v.first])
ans += v.second;
}
}
mul += cnt[now];
}
printf("%lld\n", ans - n);
} | replace | 9 | 10 | 9 | 10 | -11 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(), (x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int NMAX = 200005;
bool isend[NMAX];
int child[NMAX][26];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<string> s(n);
rep(i, n) cin >> s[i];
rep(i, n) reverse(whole(s[i]));
auto comp = [](string a, string b) {
if (a.size() == b.size())
return a < b;
return a.size() < b.size();
};
sort(whole(s), comp);
// TRIE
int nxt = 1;
vector<vector<int>> path(n);
rep(i, n) {
int node = 0;
for (char c : s[i]) {
path[i].pb(node);
if (child[node][c - 'a'] == 0) {
child[node][c - 'a'] = nxt++;
}
node = child[node][c - 'a'];
}
isend[node] = true;
}
ll ans = 0;
rep(i, n) {
set<char> suf;
rrep(j, s[i].size()) {
suf.insert(s[i][j]);
int node = path[i][j];
for (char c : suf) {
if (isend[child[node][c - 'a']])
ans++;
}
}
}
ans -= n;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(), (x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int NMAX = 1000005;
bool isend[NMAX];
int child[NMAX][26];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<string> s(n);
rep(i, n) cin >> s[i];
rep(i, n) reverse(whole(s[i]));
auto comp = [](string a, string b) {
if (a.size() == b.size())
return a < b;
return a.size() < b.size();
};
sort(whole(s), comp);
// TRIE
int nxt = 1;
vector<vector<int>> path(n);
rep(i, n) {
int node = 0;
for (char c : s[i]) {
path[i].pb(node);
if (child[node][c - 'a'] == 0) {
child[node][c - 'a'] = nxt++;
}
node = child[node][c - 'a'];
}
isend[node] = true;
}
ll ans = 0;
rep(i, n) {
set<char> suf;
rrep(j, s[i].size()) {
suf.insert(s[i][j]);
int node = path[i][j];
for (char c : suf) {
if (isend[child[node][c - 'a']])
ans++;
}
}
}
ans -= n;
cout << ans << endl;
return 0;
}
| replace | 22 | 23 | 22 | 23 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define LL long long
const int N = 2e5 + 5;
int n;
vector<string> v;
vector<int> c[N][26];
LL ans;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
reverse(s.begin(), s.end());
v.pb(s);
}
sort(v.begin(), v.end());
for (int i = 0; i < n; i++)
for (int j = 0; j < 26; j++) {
c[i][j].resize(v[i].size());
for (int k = v[i].size() - 1; k >= 0; k--)
if (k != v[i].size() - 1 && c[i][j][k + 1] || v[i][k] == j + 'a')
c[i][j][k] = 1;
else
c[i][j][k] = 0;
}
for (int i = 1; i < n; i++)
for (int j = 0; j < 26; j++)
for (int k = 0; k < c[i][j].size(); k++)
if (k < c[i - 1][j].size())
c[i][j][k] += c[i - 1][j][k];
for (int i = 0; i < n; i++) {
string s = v[i].substr(0, v[i].size() - 1);
int u1 = upper_bound(v.begin(), v.end(), s) - v.begin();
if (s.size())
s[s.size() - 1]++;
else
s = "|位巨佬!";
int u2 = lower_bound(v.begin(), v.end(), s) - v.begin();
int last = v[i][v[i].size() - 1] - 'a';
ans += c[u2 - 1][last][v[i].size() - 1];
if (v[i].size() <= c[u1 - 1][last].size())
ans -= c[u1 - 1][last][v[i].size() - 1];
}
cout << ans - n << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define LL long long
const int N = 2e5 + 5;
int n;
vector<string> v;
vector<int> c[N][26];
LL ans;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
reverse(s.begin(), s.end());
v.pb(s);
}
sort(v.begin(), v.end());
for (int i = 0; i < n; i++)
for (int j = 0; j < 26; j++) {
c[i][j].resize(v[i].size());
for (int k = v[i].size() - 1; k >= 0; k--)
if (k != v[i].size() - 1 && c[i][j][k + 1] || v[i][k] == j + 'a')
c[i][j][k] = 1;
else
c[i][j][k] = 0;
}
for (int i = 1; i < n; i++)
for (int j = 0; j < 26; j++)
for (int k = 0; k < c[i][j].size(); k++)
if (k < c[i - 1][j].size())
c[i][j][k] += c[i - 1][j][k];
for (int i = 0; i < n; i++) {
string s = v[i].substr(0, v[i].size() - 1);
int u1 = upper_bound(v.begin(), v.end(), s) - v.begin();
if (s.size())
s[s.size() - 1]++;
else
s = "|位巨佬!";
int u2 = lower_bound(v.begin(), v.end(), s) - v.begin();
int last = v[i][v[i].size() - 1] - 'a';
ans += c[u2 - 1][last][v[i].size() - 1];
if (u1 && v[i].size() <= c[u1 - 1][last].size())
ans -= c[u1 - 1][last][v[i].size() - 1];
}
cout << ans - n << '\n';
return 0;
} | replace | 46 | 47 | 46 | 47 | -11 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MAXN 200005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
string str[MAXN];
int tot = 1, n;
int trie[MAXN][26], cnt[MAXN][26];
bool ed[MAXN];
bool has[26];
void insert(string s, int rt) {
vector<int> nodes;
nodes.clear();
for (int i = 0; i < (int)s.size(); i++) {
int x = s[i] - 'a';
if (trie[rt][x] == 0)
trie[rt][x] = ++tot;
nodes.push_back(rt);
rt = trie[rt][x];
}
memset(has, false, sizeof(has));
for (int i = (int)s.size() - 1; i >= 0; i--) {
has[s[i] - 'a'] = true;
int cur = nodes[i];
for (int j = 0; j < 26; j++)
if (has[j])
cnt[cur][j]++;
}
}
int get_ans(string s, int rt) {
for (int i = 0; i < (int)s.size() - 1; i++) {
int x = s[i] - 'a';
if (trie[rt][x] == 0)
return false;
rt = trie[rt][x];
}
int x = s[s.size() - 1] - 'a';
return cnt[rt][x];
}
map<string, int> mp;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cin >> str[i];
reverse(str[i].begin(), str[i].end());
insert(str[i], 1);
}
ll ans = 0;
for (int i = 0; i < n; i++)
ans += get_ans(str[i], 1) - 1;
for (int i = 0; i < n; i++)
mp[str[i]]++;
for (auto p : mp)
ans -= 1LL * p.S * (p.S - 1) / 2;
printf("%lld\n", ans);
} | #include <bits/stdc++.h>
#define MAXN 2000005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
string str[MAXN];
int tot = 1, n;
int trie[MAXN][26], cnt[MAXN][26];
bool ed[MAXN];
bool has[26];
void insert(string s, int rt) {
vector<int> nodes;
nodes.clear();
for (int i = 0; i < (int)s.size(); i++) {
int x = s[i] - 'a';
if (trie[rt][x] == 0)
trie[rt][x] = ++tot;
nodes.push_back(rt);
rt = trie[rt][x];
}
memset(has, false, sizeof(has));
for (int i = (int)s.size() - 1; i >= 0; i--) {
has[s[i] - 'a'] = true;
int cur = nodes[i];
for (int j = 0; j < 26; j++)
if (has[j])
cnt[cur][j]++;
}
}
int get_ans(string s, int rt) {
for (int i = 0; i < (int)s.size() - 1; i++) {
int x = s[i] - 'a';
if (trie[rt][x] == 0)
return false;
rt = trie[rt][x];
}
int x = s[s.size() - 1] - 'a';
return cnt[rt][x];
}
map<string, int> mp;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cin >> str[i];
reverse(str[i].begin(), str[i].end());
insert(str[i], 1);
}
ll ans = 0;
for (int i = 0; i < n; i++)
ans += get_ans(str[i], 1) - 1;
for (int i = 0; i < n; i++)
mp[str[i]]++;
for (auto p : mp)
ans -= 1LL * p.S * (p.S - 1) / 2;
printf("%lld\n", ans);
} | replace | 1 | 2 | 1 | 2 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
const int N = 2e5 + 5;
int trie[N][60];
string s[N];
int sz;
int zero = 30;
void add(int idx) {
int root = 1;
int z = s[idx].size();
int cnt[33] = {};
for (int i = 0; i < z; i++) {
cnt[s[idx][i] - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (cnt[i] > 0)
trie[root][i + zero]++;
}
for (int i = 0; i < z; i++) {
if (trie[root][s[idx][i] - 'a'] == 0) {
trie[root][s[idx][i] - 'a'] = ++sz;
}
root = trie[root][s[idx][i] - 'a'];
cnt[s[idx][i] - 'a']--;
for (int j = 0; j < 26; j++) {
if (cnt[j] > 0)
trie[root][j + zero]++;
}
}
}
ll ans = 0;
void get(int idx) {
int root = 1;
int z = s[idx].size();
for (int i = 0; i < z - 1; i++) {
root = trie[root][s[idx][i] - 'a'];
}
int x = s[idx][z - 1] - 'a';
ans += trie[root][x + zero];
}
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
}
sz = 1;
for (int i = 1; i <= n; i++) {
add(i);
}
for (int i = 1; i <= n; i++) {
get(i);
}
cout << ans - n << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while (t--)
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
const int N = 2e6 + 5;
int trie[N][60];
string s[N];
int sz;
int zero = 30;
void add(int idx) {
int root = 1;
int z = s[idx].size();
int cnt[33] = {};
for (int i = 0; i < z; i++) {
cnt[s[idx][i] - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (cnt[i] > 0)
trie[root][i + zero]++;
}
for (int i = 0; i < z; i++) {
if (trie[root][s[idx][i] - 'a'] == 0) {
trie[root][s[idx][i] - 'a'] = ++sz;
}
root = trie[root][s[idx][i] - 'a'];
cnt[s[idx][i] - 'a']--;
for (int j = 0; j < 26; j++) {
if (cnt[j] > 0)
trie[root][j + zero]++;
}
}
}
ll ans = 0;
void get(int idx) {
int root = 1;
int z = s[idx].size();
for (int i = 0; i < z - 1; i++) {
root = trie[root][s[idx][i] - 'a'];
}
int x = s[idx][z - 1] - 'a';
ans += trie[root][x + zero];
}
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
}
sz = 1;
for (int i = 1; i <= n; i++) {
add(i);
}
for (int i = 1; i <= n; i++) {
get(i);
}
cout << ans - n << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while (t--)
solve();
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353
#define MEM_SIZE 101010
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()
template <typename T> void DEBUG(T e) {
if (DEBUG_OUT == false)
return;
std::cout << e << " ";
}
template <typename T> void DEBUG(const std::vector<T> &v) {
if (DEBUG_OUT == false)
return;
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) {
if (DEBUG_OUT == false)
return;
for (const auto &v : vv) {
DEBUG(v);
}
}
template <class T, class... Ts> void DEBUG(T d, Ts... e) {
if (DEBUG_OUT == false)
return;
DEBUG(d);
DEBUG(e...);
}
template <class T> void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
abort();
}
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
// BELOW
template <int K, class T> struct Trie {
struct Node {
int c;
std::array<int, K> nxt;
std::array<int, K> cnt; // 文字cを含むような子孫の数
int sz; // 部分木のサイズ
explicit Node(int c) : c(c), sz(0) {
nxt.fill(-1);
cnt.fill(0);
}
};
std::vector<Node> nodes;
std::function<int(T)> enc;
explicit Trie(T base) {
nodes.emplace_back(-1);
enc = [=](T c) { return c - base; };
}
explicit Trie(const std::function<int(T)> &enc) : enc(enc) {
nodes.emplace_back(-1);
}
template <class Container> void add(const Container &s) {
int pos = 0;
for (T c : s) {
int ci = enc(c);
int npos = nodes[pos].nxt[ci];
if (npos == -1) {
npos = nodes.size();
nodes[pos].nxt[ci] = npos;
nodes.emplace_back(ci);
}
pos = npos;
}
++nodes[pos].sz;
}
template <class Container> int find(const Container &s) const {
int pos = 0;
for (char c : s) {
int ci = enc(c);
pos = nodes[pos].nxt[ci];
if (pos == -1)
return -1;
}
return pos;
}
// cntを埋める
void fillcnt() {
for (int pos = (int)nodes.size() - 1; pos >= 0; --pos) {
auto &node = nodes[pos];
for (int c = 0; c < 26; ++c) {
int npos = node.nxt[c];
if (npos == -1)
continue;
const auto &cnode = nodes[npos];
node.sz += cnode.sz;
for (int d = 0; d < 26; ++d) {
node.cnt[d] += cnode.cnt[d];
}
}
// 全ての子孫はcを含む
if (node.c != -1)
node.cnt[node.c] = node.sz;
}
}
};
// BELOW
void solve(void) {
int n;
cin >> n;
vector<string> vs(n, 0);
for (int i = 0; i < n; i++) {
cin >> vs[i];
reverse(ALL(vs[i]));
}
Trie<26, char> trie('a');
for (auto &&s : vs) {
trie.add(s);
}
trie.fillcnt();
int res = 0;
for (auto &&s : vs) {
int last = s.back() - 'a';
s.pop_back();
int pos = trie.find(s);
for (int ci = 0; ci < 26; ci++) {
int npos = trie.nodes[pos].nxt[ci];
if (npos == -1)
continue;
res += trie.nodes[npos].cnt[last];
if (ci == last)
res--;
}
}
cout << res << endl;
return;
}
int32_t main(int32_t argc, const char *argv[]) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout << std::fixed;
std::cout << std::setprecision(11);
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353
#define MEM_SIZE 101010
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()
template <typename T> void DEBUG(T e) {
if (DEBUG_OUT == false)
return;
std::cout << e << " ";
}
template <typename T> void DEBUG(const std::vector<T> &v) {
if (DEBUG_OUT == false)
return;
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) {
if (DEBUG_OUT == false)
return;
for (const auto &v : vv) {
DEBUG(v);
}
}
template <class T, class... Ts> void DEBUG(T d, Ts... e) {
if (DEBUG_OUT == false)
return;
DEBUG(d);
DEBUG(e...);
}
template <class T> void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
abort();
}
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
// BELOW
template <int K, class T> struct Trie {
struct Node {
int c;
std::array<int, K> nxt;
std::array<int, K> cnt; // 文字cを含むような子孫の数
int sz; // 部分木のサイズ
explicit Node(int c) : c(c), sz(0) {
nxt.fill(-1);
cnt.fill(0);
}
};
std::vector<Node> nodes;
std::function<int(T)> enc;
explicit Trie(T base) {
nodes.emplace_back(-1);
enc = [=](T c) { return c - base; };
}
explicit Trie(const std::function<int(T)> &enc) : enc(enc) {
nodes.emplace_back(-1);
}
template <class Container> void add(const Container &s) {
int pos = 0;
for (T c : s) {
int ci = enc(c);
int npos = nodes[pos].nxt[ci];
if (npos == -1) {
npos = nodes.size();
nodes[pos].nxt[ci] = npos;
nodes.emplace_back(ci);
}
pos = npos;
}
++nodes[pos].sz;
}
template <class Container> int find(const Container &s) const {
int pos = 0;
for (char c : s) {
int ci = enc(c);
pos = nodes[pos].nxt[ci];
if (pos == -1)
return -1;
}
return pos;
}
// cntを埋める
void fillcnt() {
for (int pos = (int)nodes.size() - 1; pos >= 0; --pos) {
auto &node = nodes[pos];
for (int c = 0; c < 26; ++c) {
int npos = node.nxt[c];
if (npos == -1)
continue;
const auto &cnode = nodes[npos];
node.sz += cnode.sz;
for (int d = 0; d < 26; ++d) {
node.cnt[d] += cnode.cnt[d];
}
}
// 全ての子孫はcを含む
if (node.c != -1)
node.cnt[node.c] = node.sz;
}
}
};
// BELOW
void solve(void) {
int n;
cin >> n;
vector<string> vs(n);
for (int i = 0; i < n; i++) {
cin >> vs[i];
reverse(ALL(vs[i]));
}
Trie<26, char> trie('a');
for (auto &&s : vs) {
trie.add(s);
}
trie.fillcnt();
int res = 0;
for (auto &&s : vs) {
int last = s.back() - 'a';
s.pop_back();
int pos = trie.find(s);
for (int ci = 0; ci < 26; ci++) {
int npos = trie.nodes[pos].nxt[ci];
if (npos == -1)
continue;
res += trie.nodes[npos].cnt[last];
if (ci == last)
res--;
}
}
cout << res << endl;
return;
}
int32_t main(int32_t argc, const char *argv[]) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout << std::fixed;
std::cout << std::setprecision(11);
solve();
return 0;
}
| replace | 135 | 136 | 135 | 136 | -6 | terminate called after throwing an instance of 'std::logic_error'
what(): basic_string: construction from null is not valid
|
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define IN freopen("alchemy_input.txt", "r", stdin)
#define OUT freopen("output.txt", "w", stdout)
#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for (i = a; i <= b; i++)
#define DBG printf("Hi\n")
#define i64 long long int
#define ui64 unsigned long long int
#define xx first
#define yy second
#define ln 17
#define off 2
#define sq(x) ((x) * (x))
#define FASTIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
using namespace __gnu_pbds;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<i64, null_type, less<i64>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef pair<int, int> pii;
// #define log 20
#define mod1 1000000007LL
#define mod2 1000000009LL
#define INF 2000000000
#define maxn 200005
const long double eps = 1e-9;
vector<string> vs;
bool comp(string s1, string s2) { return s1.size() > s2.size(); }
map<pair<pair<int, int>, int>, int> Map;
int cnt[maxn][28];
int yes[maxn][28];
int main() {
FASTIO;
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
reverse(s.begin(), s.end());
vs.pb(s);
}
sort(vs.begin(), vs.end(), comp);
// reverse(vs.begin(),vs.end()) ;
int cur = 0;
i64 ans = 0;
for (int i = 0; i < n; i++) {
int sz = vs[i].size();
for (int j = sz - 1; j >= 0; j--)
for (int k = 0; k < 26; k++)
yes[j][k] = 0;
for (int j = sz - 1; j >= 0; j--) {
for (int k = 0; k < 26; k++)
yes[j][k] |= yes[j + 1][k];
yes[j][vs[i][j] - 'a'] = 1;
}
int b1 = 29, b2 = 43;
int h1 = 0, h2 = 0;
for (int j = 0; j < sz - 1; j++) {
h1 = (1LL * h1 * b1 + vs[i][j] - 'a') % mod1;
h2 = (1LL * h2 * b2 + vs[i][j] - 'a') % mod2;
}
if (Map.find(mp(mp(h1, h2), sz - 1)) != Map.end()) {
int idx = Map[mp(mp(h1, h2), sz - 1)];
ans += cnt[idx][vs[i][sz - 1] - 'a'];
}
h1 = 0;
h2 = 0;
for (int j = 0; j < sz; j++) {
if (Map.find(mp(mp(h1, h2), j)) == Map.end()) {
Map[mp(mp(h1, h2), j)] = ++cur;
}
int c = Map[mp(mp(h1, h2), j)];
for (int k = 0; k < 26; k++)
cnt[c][k] += yes[j][k];
h1 = (1LL * h1 * b1 + vs[i][j] - 'a') % mod1;
h2 = (1LL * h2 * b2 + vs[i][j] - 'a') % mod2;
}
for (int j = sz - 1; j >= 0; j--) {
for (int k = 0; k < 26; k++)
yes[j][k] = 0;
}
// cout<<vs[i]<<" "<<ans<<endl ;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define IN freopen("alchemy_input.txt", "r", stdin)
#define OUT freopen("output.txt", "w", stdout)
#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for (i = a; i <= b; i++)
#define DBG printf("Hi\n")
#define i64 long long int
#define ui64 unsigned long long int
#define xx first
#define yy second
#define ln 17
#define off 2
#define sq(x) ((x) * (x))
#define FASTIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
using namespace __gnu_pbds;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<i64, null_type, less<i64>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef pair<int, int> pii;
// #define log 20
#define mod1 1000000007LL
#define mod2 1000000009LL
#define INF 2000000000
#define maxn 1000005
const long double eps = 1e-9;
vector<string> vs;
bool comp(string s1, string s2) { return s1.size() > s2.size(); }
map<pair<pair<int, int>, int>, int> Map;
int cnt[maxn][28];
int yes[maxn][28];
int main() {
FASTIO;
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
reverse(s.begin(), s.end());
vs.pb(s);
}
sort(vs.begin(), vs.end(), comp);
// reverse(vs.begin(),vs.end()) ;
int cur = 0;
i64 ans = 0;
for (int i = 0; i < n; i++) {
int sz = vs[i].size();
for (int j = sz - 1; j >= 0; j--)
for (int k = 0; k < 26; k++)
yes[j][k] = 0;
for (int j = sz - 1; j >= 0; j--) {
for (int k = 0; k < 26; k++)
yes[j][k] |= yes[j + 1][k];
yes[j][vs[i][j] - 'a'] = 1;
}
int b1 = 29, b2 = 43;
int h1 = 0, h2 = 0;
for (int j = 0; j < sz - 1; j++) {
h1 = (1LL * h1 * b1 + vs[i][j] - 'a') % mod1;
h2 = (1LL * h2 * b2 + vs[i][j] - 'a') % mod2;
}
if (Map.find(mp(mp(h1, h2), sz - 1)) != Map.end()) {
int idx = Map[mp(mp(h1, h2), sz - 1)];
ans += cnt[idx][vs[i][sz - 1] - 'a'];
}
h1 = 0;
h2 = 0;
for (int j = 0; j < sz; j++) {
if (Map.find(mp(mp(h1, h2), j)) == Map.end()) {
Map[mp(mp(h1, h2), j)] = ++cur;
}
int c = Map[mp(mp(h1, h2), j)];
for (int k = 0; k < 26; k++)
cnt[c][k] += yes[j][k];
h1 = (1LL * h1 * b1 + vs[i][j] - 'a') % mod1;
h2 = (1LL * h2 * b2 + vs[i][j] - 'a') % mod2;
}
for (int j = sz - 1; j >= 0; j--) {
for (int k = 0; k < 26; k++)
yes[j][k] = 0;
}
// cout<<vs[i]<<" "<<ans<<endl ;
}
cout << ans << endl;
return 0;
}
| replace | 38 | 39 | 38 | 39 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const int INF = 1e9 + 7;
const ll LINF = 9223372036854775807;
const ll MOD = 1e9 + 7;
const ld PI = acos(-1);
const ld EPS = 1e-10; // 微調整用(EPSより小さいと0と判定など)
int ii() {
int x;
if (scanf("%d", &x) == 1)
return x;
else
return 0;
}
long long il() {
long long x;
if (scanf("%lld", &x) == 1)
return x;
else
return 0;
}
string is() {
string x;
cin >> x;
return x;
}
char ic() {
char x;
cin >> x;
return x;
}
void oi(int x) { printf("%d ", x); }
void ol(long long x) { printf("%lld ", x); }
void od_nosp(double x) { printf("%.15f", x); } // 古い問題用
void od(double x) { printf("%.15f ", x); }
// long doubleで受け取り、fをLfなどに変えて出力すると、変な数値が出る
// それをなんとかするには独自の出力を作らなければならなそう
void os(const string &s) { printf("%s ", s.c_str()); }
void oc(const char &c) { printf("%c ", c); }
#define o_map(v) \
{ \
cerr << #v << endl; \
for (const auto &xxx : v) { \
cout << xxx.first << " " << xxx.second << "\n"; \
} \
} // 動作未確認
void br() { putchar('\n'); }
// #define gcd __gcd //llは受け取らない C++17~のgcdと違うので注意
// int lcm(int a, int b){return a / gcd(a, b) * b;}
#define begin_end(a) a.begin(), a.end() // sort(begin_end(vec));
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define DREP(i, m, n) for (ll i = (ll)(m); i > (ll)(n); i--)
#define rep(i, n) REP(i, 0, n)
#define m_p(a, b) make_pair(a, b)
#define SORT_UNIQUE(c) \
(sort(c.begin(), c.end()), \
c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define p_b push_back
#define SZ(x) ((ll)(x).size()) // size()がunsignedなのでエラー避けに
#define endk '\n'
// coutによるpairの出力(空白区切り)
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
return s << "(" << p.first << " " << p.second << ")";
}
// coutによるvectorの出力(空白区切り)
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
int len = v.size();
for (int i = 0; i < len; ++i) {
s << v[i];
if (i < len - 1)
s << " "; //"\t"に変えるとTabで見やすく区切る
}
return s;
}
// coutによる多次元vectorの出力(空白区切り)
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
int len = vv.size();
for (int i = 0; i < len; ++i) {
s << vv[i] << endl;
}
return s;
}
// 最大値、最小値の更新。更新したor等しければtrueを返す
template <typename T> bool chmax(T &a, T b) { return (a = max(a, b)) == b; }
template <typename T> bool chmin(T &a, T b) { return (a = min(a, b)) == b; }
// 4近傍(上下左右) rep(i, 2) にすると右・下だけに進む
vector<int> dx_4 = {1, 0, -1, 0};
vector<int> dy_4 = {0, 1, 0, -1};
// -------- template end - //
// - library ------------- //
ll ans = 0;
// existence[i][j][k] := S[i]の [j]文字目以降に アルファベット[k:a~z]
// が何個あるか
vector<vector<vector<ll>>> existence;
// Trie 木
// O(|S|) で文字列を挿入する
// O(max(Trie木に入っている文字列 & 検索キーとなる文字列) )
// なので、検索キー文字列が長くても、辞書内の文字列が短ければすぐ終わる
// ex)天下一2016-C たんごたくさん メモリ領域は最悪でも
// max(|S|)*(文字種数)*(文字種数)+(文字列数) か? Trie<26, 'a'> trie;
// などとして構築 void insert(str word); 文字列の挿入 bool
// search(str word); 指定した文字列そのものがあるか bool
// search_prefix(str prefix); 指定したprefixを持つ文字列があるか int
// word_count(); 挿入した単語の数 int node_count();
// 頂点の総数(根を含む) accept
// 変数を使えば、条件に沿う文字列が「いくつあるか」への変更は容易と思われる
template <int char_size, int base> struct Trie {
struct Node { // 頂点の構造体を定義
vector<int> next; // 子の頂点番号を格納。存在しなければ-1
// next[c] := 次の文字が c となる子頂点の頂点番号
vector<int> accept; // 末端がこの頂点になる文字列の str_id を保存
// str_id := その文字列が何番目に入ったか(0-indexed)
int c; // base からの間隔をint型で表現したもの
int common; // いくつの文字列がこの頂点を共有しているか(根は全員が共有)
Node(int c_) : c(c_), common(0) { // <- なぜ0で生成? 1では?
next.assign(char_size, -1); // 生成時は子がいないので、next[全文字種] = -1
}
};
vector<Node> nodes; // trie 木本体
int root; // 根(の頂点番号か?
// と思いきや、Node(root)としているから、頂点の文字種になっちゃってないか?
// どういうこと?)
Trie() : root(0) {
// 初期化。はじめは根しか無い
nodes.push_back(Node(root)); // rootノード.cが0とはどういうことか?
// 文字種'a'ってことになっちゃわないか?
}
// 単語を追加 ※使う時は、void insert(const string &word) の方を使う
void insert(const string &word,
int word_id) { // 単語:str と 単語の番号: str_id
int node_id = 0; // 今見ている頂点番号 最初は根(0)
// 今の頂点から、次の頂点(子)を探す
// 深さ1辿るごとに必ず1文字進むので、文字数と同じ回数ループすれば良い
for (int i = 0; i < (int)word.size(); i++) {
int c = (int)(word[i] - base); // i文字目(0-indexed)を探す
// 今の頂点の子の中で、次に文字種cが来る頂点番号(-1なら更新したいので参照渡し)
int &next_id = nodes[node_id].next[c];
if (next_id == -1) { // 次の頂点が存在しなければ追加
next_id = (int)nodes.size(); // 現在ある頂点数 = 新しい頂点の頂点番号
nodes.push_back(Node(c)); // 文字種 c の頂点を追加
}
++nodes[node_id].common; // 今の頂点を共有する文字列を1つ増やす
node_id = next_id; // 次の頂点(子)に移動
}
// 最後の頂点(最後の文字)の処理を忘れずに行う
++nodes[node_id].common;
nodes[node_id].accept.push_back(
word_id); // 単語の終端なので、頂点に番号を入れておく
}
void insert(const string &word) {
insert(word,
nodes[0]
.common); // 根を共有する文字列の数 = 挿入済みの文字列数 = str_id
}
// 単語 もしくは prefixの検索
// 第二引数として 今回見始めるのは何文字目(0-indexed)からかを渡す
vector<int> search(const string &word, int s_idx) {
vector<int> res; // 見つけた単語のstr_id
int node_id = 0; // 今見ている頂点番号 最初は根(0)
// 今の頂点から、次の頂点(子)を探す
// 深さ1辿るごとに必ず1文字進むので、文字数と同じ回数ループすれば良い
for (int i = s_idx; i < (int)word.size(); i++) {
int c = (int)(word[i] - base);
int &next_id = nodes[node_id].next[c];
if (next_id == -1) { // 次の頂点が存在しなければ終了
return res;
}
node_id = next_id;
// ここで次の頂点に移っているので、ここで文字列が見つかったか判定する
for (int id_found : nodes[node_id].accept)
res.p_b(id_found);
}
return res;
}
// 挿入した単語の数
int word_count() const { return (nodes[0].common); }
// Trie木のノード数
int node_count() const { return ((int)nodes.size()); }
};
// --------- library end - //
int main() {
ll N = il();
// existence[i][j][k] := S[i]の [j]文字目以降に アルファベット[k:a~z]
// が何個あるか [i][len] の部分には番兵が入っている
existence = vector<vector<vector<ll>>>(N, vector<vector<ll>>());
vector<string> ss;
rep(i, N) {
ss.push_back(is());
reverse(begin_end(ss[i]));
existence[i] = vector<vector<ll>>(SZ(ss[i]) + 1, vector<ll>(26, 0));
}
// existence の前計算
rep(i, N) {
DREP(j, SZ(ss[i]) - 1, -1) {
// j : SZ(ss[i])-1 ~ 0
rep(k, 26) { existence[i][j][k] = existence[i][j + 1][k]; }
existence[i][j][ss[i][j] - 'a'] += 1;
}
}
// Trie木を構築
Trie<26, 'a'> trie;
rep(i, N) trie.insert(ss[i]);
// S[i] を用いて作れる S[i] 以外の文字列がいくつあるか を各iについて見ていく
// Trie木の search() を今回用に改造するのは大変そうなので、データだけ利用する
rep(i, N) {
int node = 0; // 今見ている prefix の末尾
rep(j, SZ(ss[i])) {
// j : 0 ~ Siの長さ - 1
// 「1文字はprefixから離れていてもいい」という1文字について判定するために、「Siのj文字目以降に存在する文字」について判定してループするが、その
// j 文字目 を表す(0-indexed)
rep(k, 26) {
// k : 0 ~ 25 (a ~ z)
// trie.nodes[node].next[k]が-1となって、SZ(...)の部分が配列外参照を起こすことはない(S[i]自身が入っているため
// & S[i]に存在しないパターンは existence[i][j][k]>0
// の判定で除外されるため)
if (trie.nodes[node].next[k] != -1) {
cout << "aaaa" << endk;
return 0;
}
if (existence[i][j][k] > 0 &&
SZ(trie.nodes[trie.nodes[node].next[k]].accept) > 0) {
ans++;
}
}
// 次の文字に移る(次はj文字目をprefixに入れる)
if (trie.nodes[node].next[ss[i][j] - 'a'] == -1)
break;
else
node = trie.nodes[node].next[ss[i][j] - 'a'];
}
}
ans -= N; // 各ループで自分自身も検出しているので、それを引く
cout << ans << endk;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const int INF = 1e9 + 7;
const ll LINF = 9223372036854775807;
const ll MOD = 1e9 + 7;
const ld PI = acos(-1);
const ld EPS = 1e-10; // 微調整用(EPSより小さいと0と判定など)
int ii() {
int x;
if (scanf("%d", &x) == 1)
return x;
else
return 0;
}
long long il() {
long long x;
if (scanf("%lld", &x) == 1)
return x;
else
return 0;
}
string is() {
string x;
cin >> x;
return x;
}
char ic() {
char x;
cin >> x;
return x;
}
void oi(int x) { printf("%d ", x); }
void ol(long long x) { printf("%lld ", x); }
void od_nosp(double x) { printf("%.15f", x); } // 古い問題用
void od(double x) { printf("%.15f ", x); }
// long doubleで受け取り、fをLfなどに変えて出力すると、変な数値が出る
// それをなんとかするには独自の出力を作らなければならなそう
void os(const string &s) { printf("%s ", s.c_str()); }
void oc(const char &c) { printf("%c ", c); }
#define o_map(v) \
{ \
cerr << #v << endl; \
for (const auto &xxx : v) { \
cout << xxx.first << " " << xxx.second << "\n"; \
} \
} // 動作未確認
void br() { putchar('\n'); }
// #define gcd __gcd //llは受け取らない C++17~のgcdと違うので注意
// int lcm(int a, int b){return a / gcd(a, b) * b;}
#define begin_end(a) a.begin(), a.end() // sort(begin_end(vec));
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define DREP(i, m, n) for (ll i = (ll)(m); i > (ll)(n); i--)
#define rep(i, n) REP(i, 0, n)
#define m_p(a, b) make_pair(a, b)
#define SORT_UNIQUE(c) \
(sort(c.begin(), c.end()), \
c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define p_b push_back
#define SZ(x) ((ll)(x).size()) // size()がunsignedなのでエラー避けに
#define endk '\n'
// coutによるpairの出力(空白区切り)
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
return s << "(" << p.first << " " << p.second << ")";
}
// coutによるvectorの出力(空白区切り)
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
int len = v.size();
for (int i = 0; i < len; ++i) {
s << v[i];
if (i < len - 1)
s << " "; //"\t"に変えるとTabで見やすく区切る
}
return s;
}
// coutによる多次元vectorの出力(空白区切り)
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
int len = vv.size();
for (int i = 0; i < len; ++i) {
s << vv[i] << endl;
}
return s;
}
// 最大値、最小値の更新。更新したor等しければtrueを返す
template <typename T> bool chmax(T &a, T b) { return (a = max(a, b)) == b; }
template <typename T> bool chmin(T &a, T b) { return (a = min(a, b)) == b; }
// 4近傍(上下左右) rep(i, 2) にすると右・下だけに進む
vector<int> dx_4 = {1, 0, -1, 0};
vector<int> dy_4 = {0, 1, 0, -1};
// -------- template end - //
// - library ------------- //
ll ans = 0;
// existence[i][j][k] := S[i]の [j]文字目以降に アルファベット[k:a~z]
// が何個あるか
vector<vector<vector<ll>>> existence;
// Trie 木
// O(|S|) で文字列を挿入する
// O(max(Trie木に入っている文字列 & 検索キーとなる文字列) )
// なので、検索キー文字列が長くても、辞書内の文字列が短ければすぐ終わる
// ex)天下一2016-C たんごたくさん メモリ領域は最悪でも
// max(|S|)*(文字種数)*(文字種数)+(文字列数) か? Trie<26, 'a'> trie;
// などとして構築 void insert(str word); 文字列の挿入 bool
// search(str word); 指定した文字列そのものがあるか bool
// search_prefix(str prefix); 指定したprefixを持つ文字列があるか int
// word_count(); 挿入した単語の数 int node_count();
// 頂点の総数(根を含む) accept
// 変数を使えば、条件に沿う文字列が「いくつあるか」への変更は容易と思われる
template <int char_size, int base> struct Trie {
struct Node { // 頂点の構造体を定義
vector<int> next; // 子の頂点番号を格納。存在しなければ-1
// next[c] := 次の文字が c となる子頂点の頂点番号
vector<int> accept; // 末端がこの頂点になる文字列の str_id を保存
// str_id := その文字列が何番目に入ったか(0-indexed)
int c; // base からの間隔をint型で表現したもの
int common; // いくつの文字列がこの頂点を共有しているか(根は全員が共有)
Node(int c_) : c(c_), common(0) { // <- なぜ0で生成? 1では?
next.assign(char_size, -1); // 生成時は子がいないので、next[全文字種] = -1
}
};
vector<Node> nodes; // trie 木本体
int root; // 根(の頂点番号か?
// と思いきや、Node(root)としているから、頂点の文字種になっちゃってないか?
// どういうこと?)
Trie() : root(0) {
// 初期化。はじめは根しか無い
nodes.push_back(Node(root)); // rootノード.cが0とはどういうことか?
// 文字種'a'ってことになっちゃわないか?
}
// 単語を追加 ※使う時は、void insert(const string &word) の方を使う
void insert(const string &word,
int word_id) { // 単語:str と 単語の番号: str_id
int node_id = 0; // 今見ている頂点番号 最初は根(0)
// 今の頂点から、次の頂点(子)を探す
// 深さ1辿るごとに必ず1文字進むので、文字数と同じ回数ループすれば良い
for (int i = 0; i < (int)word.size(); i++) {
int c = (int)(word[i] - base); // i文字目(0-indexed)を探す
// 今の頂点の子の中で、次に文字種cが来る頂点番号(-1なら更新したいので参照渡し)
int &next_id = nodes[node_id].next[c];
if (next_id == -1) { // 次の頂点が存在しなければ追加
next_id = (int)nodes.size(); // 現在ある頂点数 = 新しい頂点の頂点番号
nodes.push_back(Node(c)); // 文字種 c の頂点を追加
}
++nodes[node_id].common; // 今の頂点を共有する文字列を1つ増やす
node_id = next_id; // 次の頂点(子)に移動
}
// 最後の頂点(最後の文字)の処理を忘れずに行う
++nodes[node_id].common;
nodes[node_id].accept.push_back(
word_id); // 単語の終端なので、頂点に番号を入れておく
}
void insert(const string &word) {
insert(word,
nodes[0]
.common); // 根を共有する文字列の数 = 挿入済みの文字列数 = str_id
}
// 単語 もしくは prefixの検索
// 第二引数として 今回見始めるのは何文字目(0-indexed)からかを渡す
vector<int> search(const string &word, int s_idx) {
vector<int> res; // 見つけた単語のstr_id
int node_id = 0; // 今見ている頂点番号 最初は根(0)
// 今の頂点から、次の頂点(子)を探す
// 深さ1辿るごとに必ず1文字進むので、文字数と同じ回数ループすれば良い
for (int i = s_idx; i < (int)word.size(); i++) {
int c = (int)(word[i] - base);
int &next_id = nodes[node_id].next[c];
if (next_id == -1) { // 次の頂点が存在しなければ終了
return res;
}
node_id = next_id;
// ここで次の頂点に移っているので、ここで文字列が見つかったか判定する
for (int id_found : nodes[node_id].accept)
res.p_b(id_found);
}
return res;
}
// 挿入した単語の数
int word_count() const { return (nodes[0].common); }
// Trie木のノード数
int node_count() const { return ((int)nodes.size()); }
};
// --------- library end - //
int main() {
ll N = il();
// existence[i][j][k] := S[i]の [j]文字目以降に アルファベット[k:a~z]
// が何個あるか [i][len] の部分には番兵が入っている
existence = vector<vector<vector<ll>>>(N, vector<vector<ll>>());
vector<string> ss;
rep(i, N) {
ss.push_back(is());
reverse(begin_end(ss[i]));
existence[i] = vector<vector<ll>>(SZ(ss[i]) + 1, vector<ll>(26, 0));
}
// existence の前計算
rep(i, N) {
DREP(j, SZ(ss[i]) - 1, -1) {
// j : SZ(ss[i])-1 ~ 0
rep(k, 26) { existence[i][j][k] = existence[i][j + 1][k]; }
existence[i][j][ss[i][j] - 'a'] += 1;
}
}
// Trie木を構築
Trie<26, 'a'> trie;
rep(i, N) trie.insert(ss[i]);
// S[i] を用いて作れる S[i] 以外の文字列がいくつあるか を各iについて見ていく
// Trie木の search() を今回用に改造するのは大変そうなので、データだけ利用する
rep(i, N) {
int node = 0; // 今見ている prefix の末尾
rep(j, SZ(ss[i])) {
// j : 0 ~ Siの長さ - 1
// 「1文字はprefixから離れていてもいい」という1文字について判定するために、「Siのj文字目以降に存在する文字」について判定してループするが、その
// j 文字目 を表す(0-indexed)
rep(k, 26) {
// k : 0 ~ 25 (a ~ z)
// trie.nodes[node].next[k]が-1
// でなければ、S[i]以外に、j文字目に文字種kが入っているものがあることが分かる
// また、その頂点の.acceptが1つ以上あれば、そこで単語が終わっている=一致する単語があるということが分かる
if (existence[i][j][k] > 0 && trie.nodes[node].next[k] != -1 &&
SZ(trie.nodes[trie.nodes[node].next[k]].accept) > 0) {
ans++;
}
}
// 次の文字に移る(次はj文字目をprefixに入れる)
if (trie.nodes[node].next[ss[i][j] - 'a'] == -1)
break;
else
node = trie.nodes[node].next[ss[i][j] - 'a'];
}
}
ans -= N; // 各ループで自分自身も検出しているので、それを引く
cout << ans << endk;
} | replace | 241 | 249 | 241 | 245 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int ALPHABET_SIZE = 26;
int glob_ans = 0;
int trie[1000001][26];
std::vector<int> v[200000];
int term[200000] = {0};
int nxt = 1;
void insert(string key, int k) {
int i, lvl = 0;
for (i = 0; i < key.size(); i++) {
v[k].push_back(lvl);
if (trie[lvl][key[i] - 'a'] == 0) {
trie[lvl][key[i] - 'a'] = nxt++;
}
lvl = trie[lvl][key[i] - 'a'];
}
term[lvl]++;
}
int main() {
int i, j, n;
cin >> n;
string arr[n];
for (i = 0; i < n; i++)
cin >> arr[i];
for (i = 0; i < n; i++) {
reverse(arr[i].begin(), arr[i].end());
insert(arr[i], i);
}
int ans = 0;
for (i = 0; i < n; i++) {
set<int> st;
for (int j = (int)arr[i].size() - 1; j >= 0; j--) {
int node = v[i][j];
st.insert(arr[i][j] - 'a');
for (int c = 0; c < ALPHABET_SIZE; c++)
if (st.find(c) != st.end() && term[trie[node][c]])
ans++;
}
}
cout << ans - n << endl;
}
| #include <bits/stdc++.h>
using namespace std;
const int ALPHABET_SIZE = 26;
int glob_ans = 0;
int trie[1000001][26];
std::vector<int> v[200000];
int term[1000001] = {0};
int nxt = 1;
void insert(string key, int k) {
int i, lvl = 0;
for (i = 0; i < key.size(); i++) {
v[k].push_back(lvl);
if (trie[lvl][key[i] - 'a'] == 0) {
trie[lvl][key[i] - 'a'] = nxt++;
}
lvl = trie[lvl][key[i] - 'a'];
}
term[lvl]++;
}
int main() {
int i, j, n;
cin >> n;
string arr[n];
for (i = 0; i < n; i++)
cin >> arr[i];
for (i = 0; i < n; i++) {
reverse(arr[i].begin(), arr[i].end());
insert(arr[i], i);
}
int ans = 0;
for (i = 0; i < n; i++) {
set<int> st;
for (int j = (int)arr[i].size() - 1; j >= 0; j--) {
int node = v[i][j];
st.insert(arr[i][j] - 'a');
for (int c = 0; c < ALPHABET_SIZE; c++)
if (st.find(c) != st.end() && term[trie[node][c]])
ans++;
}
}
cout << ans - n << endl;
}
| replace | 8 | 9 | 8 | 9 | -11 | |
p02589 | C++ | Runtime Error | // #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2")
// 293206GT
#include <algorithm>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
typedef long long ll;
#define int ll
using namespace std;
#define put(a) cout << (a) << '\n'
#define sqr(x) (x) * (x)
typedef pair<int, int> pii;
typedef long double ld;
typedef pair<ld, ld> pld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pii> vpii;
#define rep(x, y, a) for (int x = (y); x < (int)(a); ++x)
#define all(a) a.begin(), a.end()
#define chkmax(a, b) a = max(a, (b))
#define chkmin(a, b) a = min(a, (b))
namespace IO {
template <class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> a);
template <class A> ostream &operator<<(ostream &out, vector<A> a);
template <class A, class B> ostream &operator<<(ostream &out, pair<A, B> a) {
out << a.first << " " << a.second;
return out;
}
template <class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> a) {
for (pair<A, B> x : a)
out << x.first << " " << x.second << '\n';
return out;
}
template <class A> ostream &operator<<(ostream &out, vector<A> a) {
for (A x : a)
out << x << ' ';
return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
in >> a.first >> a.second;
return in;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
for (A &x : a)
in >> x;
return in;
}
} // namespace IO
using namespace IO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct Node {
vector<int> next;
int is_term;
Node() {
next.resize(26);
next.assign(26, -1);
is_term = 0;
}
};
const int N = 1e6 + 3e5;
// const int N = 13;
vector<Node> trie(N);
int sz = 0;
void add(string s) {
int v = 0;
for (char c2 : s) {
char c = c2 - 'a';
if (trie[v].next[c] == -1) {
trie[v].next[c] = ++sz;
}
v = trie[v].next[c];
}
++trie[v].is_term;
}
void solve() {
int n;
cin >> n;
vector<string> s(n);
cin >> s;
rep(i, 0, n) {
reverse(all(s[i]));
add(s[i]);
}
int ans = 0;
rep(i, 0, n) {
int v = 0;
vi cnt(26);
rep(j, 0, s[i].size())++ cnt[s[i][j] - 'a'];
rep(j, 0, s[i].size()) {
rep(k, 0, 26) {
if (trie[v].next[k] != -1 && cnt[k])
ans += trie[trie[v].next[k]].is_term;
}
v = trie[v].next[s[i][j] - 'a'];
--cnt[s[i][j]];
}
}
put(ans - n);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(30);
// freopen("input.txt", "r", stdin);
int q = 1;
// cin >> q;
while (q--)
solve();
}
| // #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2")
// 293206GT
#include <algorithm>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
typedef long long ll;
#define int ll
using namespace std;
#define put(a) cout << (a) << '\n'
#define sqr(x) (x) * (x)
typedef pair<int, int> pii;
typedef long double ld;
typedef pair<ld, ld> pld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pii> vpii;
#define rep(x, y, a) for (int x = (y); x < (int)(a); ++x)
#define all(a) a.begin(), a.end()
#define chkmax(a, b) a = max(a, (b))
#define chkmin(a, b) a = min(a, (b))
namespace IO {
template <class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> a);
template <class A> ostream &operator<<(ostream &out, vector<A> a);
template <class A, class B> ostream &operator<<(ostream &out, pair<A, B> a) {
out << a.first << " " << a.second;
return out;
}
template <class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> a) {
for (pair<A, B> x : a)
out << x.first << " " << x.second << '\n';
return out;
}
template <class A> ostream &operator<<(ostream &out, vector<A> a) {
for (A x : a)
out << x << ' ';
return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
in >> a.first >> a.second;
return in;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
for (A &x : a)
in >> x;
return in;
}
} // namespace IO
using namespace IO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct Node {
vector<int> next;
int is_term;
Node() {
next.resize(26);
next.assign(26, -1);
is_term = 0;
}
};
const int N = 1e6 + 3e5;
// const int N = 13;
vector<Node> trie(N);
int sz = 0;
void add(string s) {
int v = 0;
for (char c2 : s) {
char c = c2 - 'a';
if (trie[v].next[c] == -1) {
trie[v].next[c] = ++sz;
}
v = trie[v].next[c];
}
++trie[v].is_term;
}
void solve() {
int n;
cin >> n;
vector<string> s(n);
cin >> s;
rep(i, 0, n) {
reverse(all(s[i]));
add(s[i]);
}
int ans = 0;
rep(i, 0, n) {
int v = 0;
vi cnt(26);
rep(j, 0, s[i].size())++ cnt[s[i][j] - 'a'];
rep(j, 0, s[i].size()) {
rep(k, 0, 26) {
if (trie[v].next[k] != -1 && cnt[k])
ans += trie[trie[v].next[k]].is_term;
}
v = trie[v].next[s[i][j] - 'a'];
--cnt[s[i][j] - 'a'];
}
}
put(ans - n);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(30);
// freopen("input.txt", "r", stdin);
int q = 1;
// cin >> q;
while (q--)
solve();
}
| replace | 121 | 122 | 121 | 122 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02589 | C++ | Time Limit Exceeded | #pragma region header
// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define mp make_pair
#define mt make_tuple
#define rep1(i, n) for (decltype(+n) i = 0; i < (n); i++)
#define rrep1(i, n) for (auto i = n - 1; i > static_cast<decltype(i)>(-1); i--)
#define rep2(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep2(i, a, b) for (auto i = b - 1; i >= a; i--)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define rrep(...) GET_MACRO(__VA_ARGS__, rrep2, rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &&i : (a))
using namespace std;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using vp = vector<pii>;
using vvp = vector<vp>;
using mii = map<int, int>;
using vm = vector<mii>;
using vvm = vector<vm>;
template <class T, class U> using umap = unordered_map<T, U>;
using umii = umap<int, int>;
using seti = set<int>;
template <class T> using uset = unordered_set<T>;
using useti = uset<int>;
template <class T> using less_queue = priority_queue<T>;
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> void SORT(T &a) { stable_sort(all(a)); }
template <class T> void RSORT(T &a) { stable_sort(rall(a)); }
template <class T> void rev(T &a) { reverse(rall(a)); }
template <class T> void uniq(T &a) { a.erase(unique(all(a)), end(a)); }
template <class T> auto min_of(T a) { return *min_element(all(a)); }
template <class T> auto max_of(T a) { return *max_element(all(a)); }
template <class T> int sum_of(T a) { return accumulate(all(a), 0ll); }
template <class T, class U> auto sum_of(T a, U init) {
return accumulate(all(a), init);
}
template <class T, class U> int count_of(T a, U i) { return count(all(a), i); }
template <class T, class U> bool has(T a, U i) {
return find(all(a), i) != end(a);
}
template <class T> int sz(T a) { return a.size(); };
template <class T> void COUT(T x) { cout << x << endl; }
template <class T, class U> void COUT(T x, U y) {
cout << x << ' ' << y << endl;
}
template <class T, class U, class V> void COUT(T x, U y, V z) {
cout << x << ' ' << y << ' ' << z << endl;
}
template <class T> void CSP(T x) { cout << x << ' '; }
template <class T> void CVEC(T v) {
int c = v.size() - 1;
for (size_t i = 0; i < c; i++)
cout << v[i] << ' ';
if (c > -1)
cout << v[c];
cout << endl;
}
template <class T> bool amin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> bool amax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
constexpr int lshift(int x) noexcept { return 1ll << x; }
constexpr int popcount(unsigned int x) noexcept {
return __builtin_popcountll(x);
}
constexpr int least1(unsigned int x) noexcept { return __builtin_ffsll(x); }
constexpr int ceil_div(int x, int y) noexcept { return (x - 1) / y + 1; }
#pragma endregion header
struct rolling_hash {
static const uint64_t mod = (1ull << 61ull) - 1;
using uint128_t = __uint128_t;
vector<uint64_t> hashed;
static vector<uint64_t> power;
const uint64_t base;
static inline uint64_t add(uint64_t a, uint64_t b) {
if ((a += b) >= mod)
a -= mod;
return a;
}
static inline uint64_t mul(uint64_t a, uint64_t b) {
uint128_t c = (uint128_t)a * b;
return add(c >> 61, c & mod);
}
static inline uint64_t generate_base() {
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<uint64_t> rand(1, rolling_hash::mod - 1);
return rand(mt);
}
static void init_power(size_t sz, uint64_t base) {
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++)
power[i + 1] = mul(power[i], base);
}
rolling_hash() = default;
rolling_hash(const string &s, uint64_t base) : base(base) {
size_t sz = s.size();
hashed.assign(sz + 1, 0);
for (int i = 0; i < sz; i++) {
hashed[i + 1] = add(mul(hashed[i], base), s[i]);
}
}
template <typename T>
rolling_hash(const vector<T> &s, uint64_t base) : base(base) {
size_t sz = s.size();
hashed.assign(sz + 1, 0);
for (int i = 0; i < sz; i++) {
hashed[i + 1] = add(mul(hashed[i], base), s[i]);
}
}
uint64_t query(int l, int r) const {
return add(hashed[r], mod - mul(hashed[l], power[r - l]));
}
static inline uint64_t combine(uint64_t h1, uint64_t h2, size_t h2len) {
return add(mul(h1, power[h2len]), h2);
}
int lcp(const rolling_hash &b, int l1, int r1, int l2, int r2) const {
assert(base == b.base);
int len = min(r1 - l1, r2 - l2);
int low = 0, high = len + 1;
while (high - low > 1) {
int mid = (low + high) / 2;
if (query(l1, l1 + mid) == b.query(l2, l2 + mid))
low = mid;
else
high = mid;
}
return low;
}
};
vector<uint64_t> rolling_hash::power;
/*
auto rh_base = rolling_hash::generate_base();
rolling_hash::init_power(1000000, rh_base);
*/
void solve(int n, vector<string> s) {
auto rh_base = rolling_hash::generate_base();
rolling_hash::init_power(1000000, rh_base);
vector<u_int64_t> as(26);
rep(i, 26) as[i] = rolling_hash(string(1, 'a' + i), rh_base).query(0, 1);
umap<u_int64_t, int> hs;
vector<rolling_hash> rhs;
each(x, s) {
rolling_hash rh(x, rh_base);
rhs.push_back(rh);
hs[rh.query(0, sz(x))]++;
}
int ans = 0;
rep(i, n) {
string x = s[i];
int l = sz(x);
rolling_hash rh = rhs[i];
int cm = 0;
rep(j, l) {
cm |= lshift(x[j] - 'a');
auto h = rh.query(j + 1, l);
int l2 = l - j - 1;
rep(k, 26) {
if ((cm >> k) & 1) {
auto kh = rolling_hash::combine(as[k], h, l2);
ans += hs[kh];
}
}
}
}
COUT(ans - n);
}
#pragma region main
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(15);
int n;
cin >> n;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
solve(n, move(s));
}
#pragma endregion main | #pragma region header
// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define mp make_pair
#define mt make_tuple
#define rep1(i, n) for (decltype(+n) i = 0; i < (n); i++)
#define rrep1(i, n) for (auto i = n - 1; i > static_cast<decltype(i)>(-1); i--)
#define rep2(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep2(i, a, b) for (auto i = b - 1; i >= a; i--)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define rrep(...) GET_MACRO(__VA_ARGS__, rrep2, rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &&i : (a))
using namespace std;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using vp = vector<pii>;
using vvp = vector<vp>;
using mii = map<int, int>;
using vm = vector<mii>;
using vvm = vector<vm>;
template <class T, class U> using umap = unordered_map<T, U>;
using umii = umap<int, int>;
using seti = set<int>;
template <class T> using uset = unordered_set<T>;
using useti = uset<int>;
template <class T> using less_queue = priority_queue<T>;
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> void SORT(T &a) { stable_sort(all(a)); }
template <class T> void RSORT(T &a) { stable_sort(rall(a)); }
template <class T> void rev(T &a) { reverse(rall(a)); }
template <class T> void uniq(T &a) { a.erase(unique(all(a)), end(a)); }
template <class T> auto min_of(T a) { return *min_element(all(a)); }
template <class T> auto max_of(T a) { return *max_element(all(a)); }
template <class T> int sum_of(T a) { return accumulate(all(a), 0ll); }
template <class T, class U> auto sum_of(T a, U init) {
return accumulate(all(a), init);
}
template <class T, class U> int count_of(T a, U i) { return count(all(a), i); }
template <class T, class U> bool has(T a, U i) {
return find(all(a), i) != end(a);
}
template <class T> int sz(T a) { return a.size(); };
template <class T> void COUT(T x) { cout << x << endl; }
template <class T, class U> void COUT(T x, U y) {
cout << x << ' ' << y << endl;
}
template <class T, class U, class V> void COUT(T x, U y, V z) {
cout << x << ' ' << y << ' ' << z << endl;
}
template <class T> void CSP(T x) { cout << x << ' '; }
template <class T> void CVEC(T v) {
int c = v.size() - 1;
for (size_t i = 0; i < c; i++)
cout << v[i] << ' ';
if (c > -1)
cout << v[c];
cout << endl;
}
template <class T> bool amin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> bool amax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
constexpr int lshift(int x) noexcept { return 1ll << x; }
constexpr int popcount(unsigned int x) noexcept {
return __builtin_popcountll(x);
}
constexpr int least1(unsigned int x) noexcept { return __builtin_ffsll(x); }
constexpr int ceil_div(int x, int y) noexcept { return (x - 1) / y + 1; }
#pragma endregion header
struct rolling_hash {
static const uint64_t mod = (1ull << 61ull) - 1;
using uint128_t = __uint128_t;
vector<uint64_t> hashed;
static vector<uint64_t> power;
const uint64_t base;
static inline uint64_t add(uint64_t a, uint64_t b) {
if ((a += b) >= mod)
a -= mod;
return a;
}
static inline uint64_t mul(uint64_t a, uint64_t b) {
uint128_t c = (uint128_t)a * b;
return add(c >> 61, c & mod);
}
static inline uint64_t generate_base() {
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<uint64_t> rand(1, rolling_hash::mod - 1);
return rand(mt);
}
static void init_power(size_t sz, uint64_t base) {
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++)
power[i + 1] = mul(power[i], base);
}
rolling_hash() = default;
rolling_hash(const string &s, uint64_t base) : base(base) {
size_t sz = s.size();
hashed.assign(sz + 1, 0);
for (int i = 0; i < sz; i++) {
hashed[i + 1] = add(mul(hashed[i], base), s[i]);
}
}
template <typename T>
rolling_hash(const vector<T> &s, uint64_t base) : base(base) {
size_t sz = s.size();
hashed.assign(sz + 1, 0);
for (int i = 0; i < sz; i++) {
hashed[i + 1] = add(mul(hashed[i], base), s[i]);
}
}
uint64_t query(int l, int r) const {
return add(hashed[r], mod - mul(hashed[l], power[r - l]));
}
static inline uint64_t combine(uint64_t h1, uint64_t h2, size_t h2len) {
return add(mul(h1, power[h2len]), h2);
}
int lcp(const rolling_hash &b, int l1, int r1, int l2, int r2) const {
assert(base == b.base);
int len = min(r1 - l1, r2 - l2);
int low = 0, high = len + 1;
while (high - low > 1) {
int mid = (low + high) / 2;
if (query(l1, l1 + mid) == b.query(l2, l2 + mid))
low = mid;
else
high = mid;
}
return low;
}
};
vector<uint64_t> rolling_hash::power;
/*
auto rh_base = rolling_hash::generate_base();
rolling_hash::init_power(1000000, rh_base);
*/
void solve(int n, vector<string> s) {
auto rh_base = rolling_hash::generate_base();
rolling_hash::init_power(1000000, rh_base);
vector<u_int64_t> as(26);
rep(i, 26) as[i] = rolling_hash(string(1, 'a' + i), rh_base).query(0, 1);
umap<u_int64_t, int> hs;
vector<rolling_hash> rhs;
each(x, s) {
rolling_hash rh(x, rh_base);
rhs.push_back(rh);
hs[rh.query(0, sz(x))]++;
}
int ans = 0;
rep(i, n) {
string x = s[i];
int l = sz(x);
rolling_hash rh = rhs[i];
int cm = 0;
rep(j, l) {
cm |= lshift(x[j] - 'a');
auto h = rh.query(j + 1, l);
int l2 = l - j - 1;
rep(k, 26) {
if ((cm >> k) & 1) {
auto it = hs.find(rolling_hash::combine(as[k], h, l2));
if (it != hs.end())
ans += it->second;
}
}
}
}
COUT(ans - n);
}
#pragma region main
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(15);
int n;
cin >> n;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
solve(n, move(s));
}
#pragma endregion main | replace | 200 | 202 | 200 | 203 | TLE | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
/*
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble =
boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>; using
namespace boost::multiprecision; #endif
*/
#ifdef LOCAL_TEST
#define BOOST_STACKTRACE_USE_ADDR2LINE
#define BOOST_STACKTRACE_ADDR2LINE_LOCATION \
/ usr / local / opt / binutils / bin / addr2line
#define _GNU_SOURCE 1
#include <boost/stacktrace.hpp>
namespace std {
template <typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {}
dvector(const std::vector<T> &v) : std::vector<T>(v) {}
dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
dvector(const std::string::iterator first, const std::string::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::iterator first,
const typename std::vector<T>::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::reverse_iterator first,
const typename std::vector<T>::reverse_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_iterator first,
const typename std::vector<T>::const_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_reverse_iterator first,
const typename std::vector<T>::const_reverse_iterator last)
: std::vector<T>(first, last) {}
T &operator[](size_t n) {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
const T &operator[](size_t n) const {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
};
} // namespace std
class dbool {
private:
bool boolvalue;
public:
dbool() : boolvalue(false) {}
dbool(bool b) : boolvalue(b) {}
operator bool &() { return boolvalue; }
operator const bool &() const { return boolvalue; }
};
#define vector dvector
#define bool dbool
class SIGFPE_exception : std::exception {};
class SIGSEGV_exception : std::exception {};
void catch_SIGFPE([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGFPE_exception();
}
void catch_SIGSEGV([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGSEGV_exception();
}
signed convertedmain();
signed main() {
signal(SIGFPE, catch_SIGFPE);
signal(SIGSEGV, catch_SIGSEGV);
return convertedmain();
}
#define main() convertedmain()
#endif
#ifdef LOCAL_DEV
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::vector<std::vector<T>> &vv) {
s << "\\\n";
for (size_t i = 0; i < vv.size(); ++i) {
s << vv[i] << "\n";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) {
s << "{ ";
for (size_t i = 0; i < N; ++i) {
s << a[i] << "\t";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) {
s << "{\n";
for (auto itr = m.begin(); itr != m.end(); ++itr) {
s << "\t" << (*itr).first << " : " << (*itr).second << "\n";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
void debug_impl() { std::cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
std::cerr << " " << head << (sizeof...(tail) ? "," : "");
debug_impl(tail...);
}
#define debug(...) \
do { \
std::cerr << "(" << #__VA_ARGS__ << ") ="; \
debug_impl(__VA_ARGS__); \
} while (false)
#else
#define debug(...) \
do { \
} while (false)
#endif
// #define int long long
using ll = long long;
// constexpr int INF = (ll)1e9 + 7;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
#define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrepeq(i, n) for (ll i = (n); i >= 1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail> void p(Head head, Tail... tail) {
std::cout << head << (sizeof...(tail) ? " " : "");
p(tail...);
}
template <typename T> inline void pv(std::vector<T> &v) {
for (ll i = 0, N = v.size(); i < N; i++)
std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T> inline bool chmax(T &a, T b) {
return a < b && (a = b, true);
}
template <typename T> inline bool chmin(T &a, T b) {
return a > b && (a = b, true);
}
template <typename T> inline void uniq(std::vector<T> &v) {
v.erase(std::unique(v.begin(), v.end()), v.end());
}
/*-----8<-----template-----8<-----*/
template <ll char_size> class TrieNode {
public:
ll nxt[char_size];
ll exist;
vector<ll> accept;
TrieNode() : exist(0) { memset(nxt, -1, sizeof(nxt)); }
};
/*
あるノードの配下の全てのノードには自身に対応する文字列に共通するプレフィックスがあり,
ルートには空の文字列が対応している。
計算量:
add O(|S|)
query O(|S|)
exist は子供以下に追加された文字列の個数,
accept はそのノードにマッチする全ての追加された文字列の番号が格納される。
Trie< char_size, margin >():
文字列の種類数 char_size, 開始文字が margin のトライ木を作成する。
add(S): 文字列 S をトライ木に追加する
query(S, f): 文字列 S のプレフィックスに一致する文字列を検索する。
一致した文字列ごとに関数 f が呼び出される。
size(): ノード数を返す
count(): 存在する文字列の個数を返す
*/
template <ll char_size, ll margin> class Trie {
public:
using Node = TrieNode<char_size>;
vector<Node> nodes;
ll root;
Trie() : root(0) { nodes.push_back(Node()); }
void update_direct(ll node, ll id) { nodes[node].accept.push_back(id); }
void update_child(ll node, [[maybe_unused]] ll child,
[[maybe_unused]] ll id) {
++nodes[node].exist;
}
void add(const string &str, ll str_index, ll node_index, ll id) {
if (str_index == (ll)str.size()) {
update_direct(node_index, id);
} else {
const ll c = str[str_index] - margin;
if (nodes[node_index].nxt[c] == -1) {
nodes[node_index].nxt[c] = (ll)nodes.size();
nodes.push_back(Node());
}
add(str, str_index + 1, nodes[node_index].nxt[c], id);
update_child(node_index, nodes[node_index].nxt[c], id);
}
}
void add(const string &str, ll id) { add(str, 0, 0, id); }
void add(const string &str) { add(str, nodes[0].exist); }
template <typename F>
void query(const string &str, const F &f, ll str_index, ll node_index) {
for (auto &idx : nodes[node_index].accept)
f(idx);
if (str_index == (ll)str.size()) {
return;
} else {
const ll c = str[str_index] - margin;
if (nodes[node_index].nxt[c] == -1)
return;
query(str, f, str_index + 1, nodes[node_index].nxt[c]);
}
}
template <typename F> void query(const string &str, const F &f) {
query(str, f, 0, 0);
}
ll count() const { return (nodes[0].exist); }
size_t size() const { return nodes.size(); }
};
/*-----8<-----library-----8<-----*/
void solve() {
ll N;
cin >> N;
vector<string> a(N);
rep(i, N) {
cin >> a[i];
reverse(all(a[i]));
}
/*
ll N=2;
vector<string> a(N);
rep(i,N){
string t(1e6,'a');
if(i==0)t+='b';
a[i]=t;
}
*/
sort(a.begin(), a.end(),
[](const string &x, const string &y) { return x.size() > y.size(); });
vector<vector<ll>> pos(N, vector<ll>(26, -INF));
rep(i, N) { rep(j, a[i].size()) chmax(pos[i][a[i][j] - 'a'], j); }
Trie<26, 'a'> trie;
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
string t = a[i].substr(0, a[i].size() - 1);
trie.add(t);
}
ll ans = 0;
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
// index = trie木に追加した単語のindex
auto update = [&](ll index) {
debug(i, index);
if (index == i)
return;
ll x = a[index].back() - 'a';
if (pos[i][x] >= (ll)(a[index].size()) - 1)
ans++;
};
// Sのi文字目から一致する単語それぞれに対して関数 update が呼ばれる
debug(a[i]);
trie.query(a[i], update, 0, 0);
}
debug(ans);
vector<ll> c(26, 0);
rep(i, N) {
if (a[i].size() != (size_t)1)
continue;
c[a[i][0] - 'a']++;
}
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
vector<ll> t(26, false);
rep(j, a[i].size()) t[a[i][j] - 'a'] = true;
rep(j, 26) {
if (t[j])
ans += c[j];
}
}
p(ans);
}
#define BEGIN_STACK_EXTEND \
int size = 128 * 1024 * 1024; \
void *stack_extend_memory_ = malloc(size); \
void *stack_extend_origin_memory_; \
char *stack_extend_dummy_memory_ = (char *)alloca( \
(1 + (int)(((long long)stack_extend_memory_) & 127)) * 16); \
*stack_extend_dummy_memory_ = 0; \
asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp" \
: "=b"(stack_extend_origin_memory_) \
: "a"((char *)stack_extend_memory_ + (size)-1024));
#define END_STACK_EXTEND \
asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_)); \
free(stack_extend_memory_);
signed main() {
BEGIN_STACK_EXTEND
std::cin.tie(0);
ios::sync_with_stdio(false);
// ll Q; cin >> Q; while(Q--)solve();
solve();
END_STACK_EXTEND
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
/*
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble =
boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>; using
namespace boost::multiprecision; #endif
*/
#ifdef LOCAL_TEST
#define BOOST_STACKTRACE_USE_ADDR2LINE
#define BOOST_STACKTRACE_ADDR2LINE_LOCATION \
/ usr / local / opt / binutils / bin / addr2line
#define _GNU_SOURCE 1
#include <boost/stacktrace.hpp>
namespace std {
template <typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {}
dvector(const std::vector<T> &v) : std::vector<T>(v) {}
dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
dvector(const std::string::iterator first, const std::string::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::iterator first,
const typename std::vector<T>::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::reverse_iterator first,
const typename std::vector<T>::reverse_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_iterator first,
const typename std::vector<T>::const_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_reverse_iterator first,
const typename std::vector<T>::const_reverse_iterator last)
: std::vector<T>(first, last) {}
T &operator[](size_t n) {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
const T &operator[](size_t n) const {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
};
} // namespace std
class dbool {
private:
bool boolvalue;
public:
dbool() : boolvalue(false) {}
dbool(bool b) : boolvalue(b) {}
operator bool &() { return boolvalue; }
operator const bool &() const { return boolvalue; }
};
#define vector dvector
#define bool dbool
class SIGFPE_exception : std::exception {};
class SIGSEGV_exception : std::exception {};
void catch_SIGFPE([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGFPE_exception();
}
void catch_SIGSEGV([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGSEGV_exception();
}
signed convertedmain();
signed main() {
signal(SIGFPE, catch_SIGFPE);
signal(SIGSEGV, catch_SIGSEGV);
return convertedmain();
}
#define main() convertedmain()
#endif
#ifdef LOCAL_DEV
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::vector<std::vector<T>> &vv) {
s << "\\\n";
for (size_t i = 0; i < vv.size(); ++i) {
s << vv[i] << "\n";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) {
s << "{ ";
for (size_t i = 0; i < N; ++i) {
s << a[i] << "\t";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) {
s << "{\n";
for (auto itr = m.begin(); itr != m.end(); ++itr) {
s << "\t" << (*itr).first << " : " << (*itr).second << "\n";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
void debug_impl() { std::cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
std::cerr << " " << head << (sizeof...(tail) ? "," : "");
debug_impl(tail...);
}
#define debug(...) \
do { \
std::cerr << "(" << #__VA_ARGS__ << ") ="; \
debug_impl(__VA_ARGS__); \
} while (false)
#else
#define debug(...) \
do { \
} while (false)
#endif
// #define int long long
using ll = long long;
// constexpr int INF = (ll)1e9 + 7;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
#define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrepeq(i, n) for (ll i = (n); i >= 1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail> void p(Head head, Tail... tail) {
std::cout << head << (sizeof...(tail) ? " " : "");
p(tail...);
}
template <typename T> inline void pv(std::vector<T> &v) {
for (ll i = 0, N = v.size(); i < N; i++)
std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T> inline bool chmax(T &a, T b) {
return a < b && (a = b, true);
}
template <typename T> inline bool chmin(T &a, T b) {
return a > b && (a = b, true);
}
template <typename T> inline void uniq(std::vector<T> &v) {
v.erase(std::unique(v.begin(), v.end()), v.end());
}
/*-----8<-----template-----8<-----*/
template <ll char_size> class TrieNode {
public:
ll nxt[char_size];
ll exist;
vector<ll> accept;
TrieNode() : exist(0) { memset(nxt, -1, sizeof(nxt)); }
};
/*
あるノードの配下の全てのノードには自身に対応する文字列に共通するプレフィックスがあり,
ルートには空の文字列が対応している。
計算量:
add O(|S|)
query O(|S|)
exist は子供以下に追加された文字列の個数,
accept はそのノードにマッチする全ての追加された文字列の番号が格納される。
Trie< char_size, margin >():
文字列の種類数 char_size, 開始文字が margin のトライ木を作成する。
add(S): 文字列 S をトライ木に追加する
query(S, f): 文字列 S のプレフィックスに一致する文字列を検索する。
一致した文字列ごとに関数 f が呼び出される。
size(): ノード数を返す
count(): 存在する文字列の個数を返す
*/
template <ll char_size, ll margin> class Trie {
public:
using Node = TrieNode<char_size>;
vector<Node> nodes;
ll root;
Trie() : root(0) { nodes.push_back(Node()); }
void update_direct(ll node, ll id) { nodes[node].accept.push_back(id); }
void update_child(ll node, [[maybe_unused]] ll child,
[[maybe_unused]] ll id) {
++nodes[node].exist;
}
void add(const string &str, ll str_index, ll node_index, ll id) {
if (str_index == (ll)str.size()) {
update_direct(node_index, id);
} else {
const ll c = str[str_index] - margin;
if (nodes[node_index].nxt[c] == -1) {
nodes[node_index].nxt[c] = (ll)nodes.size();
nodes.push_back(Node());
}
add(str, str_index + 1, nodes[node_index].nxt[c], id);
update_child(node_index, nodes[node_index].nxt[c], id);
}
}
void add(const string &str, ll id) { add(str, 0, 0, id); }
void add(const string &str) { add(str, nodes[0].exist); }
template <typename F>
void query(const string &str, const F &f, ll str_index, ll node_index) {
for (auto &idx : nodes[node_index].accept)
f(idx);
if (str_index == (ll)str.size()) {
return;
} else {
const ll c = str[str_index] - margin;
if (nodes[node_index].nxt[c] == -1)
return;
query(str, f, str_index + 1, nodes[node_index].nxt[c]);
}
}
template <typename F> void query(const string &str, const F &f) {
query(str, f, 0, 0);
}
ll count() const { return (nodes[0].exist); }
size_t size() const { return nodes.size(); }
};
/*-----8<-----library-----8<-----*/
void solve() {
ll N;
cin >> N;
vector<string> a(N);
rep(i, N) {
cin >> a[i];
reverse(all(a[i]));
}
/*
ll N=2;
vector<string> a(N);
rep(i,N){
string t(1e6,'a');
if(i==0)t+='b';
a[i]=t;
}
*/
sort(a.begin(), a.end(),
[](const string &x, const string &y) { return x.size() > y.size(); });
vector<vector<ll>> pos(N, vector<ll>(26, -INF));
rep(i, N) { rep(j, a[i].size()) chmax(pos[i][a[i][j] - 'a'], j); }
Trie<26, 'a'> trie;
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
string t = a[i].substr(0, a[i].size() - 1);
trie.add(t);
}
ll ans = 0;
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
// index = trie木に追加した単語のindex
auto update = [&](ll index) {
debug(i, index);
if (index == i)
return;
ll x = a[index].back() - 'a';
if (pos[i][x] >= (ll)(a[index].size()) - 1)
ans++;
};
// Sのi文字目から一致する単語それぞれに対して関数 update が呼ばれる
debug(a[i]);
trie.query(a[i], update, 0, 0);
}
debug(ans);
vector<ll> c(26, 0);
rep(i, N) {
if (a[i].size() != (size_t)1)
continue;
c[a[i][0] - 'a']++;
}
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
vector<ll> t(26, false);
rep(j, a[i].size()) t[a[i][j] - 'a'] = true;
rep(j, 26) {
if (t[j])
ans += c[j];
}
}
p(ans);
}
#define BEGIN_STACK_EXTEND \
int size = 1024 * 1024 * 1024; \
void *stack_extend_memory_ = malloc(size); \
void *stack_extend_origin_memory_; \
char *stack_extend_dummy_memory_ = (char *)alloca( \
(1 + (int)(((long long)stack_extend_memory_) & 127)) * 16); \
*stack_extend_dummy_memory_ = 0; \
asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp" \
: "=b"(stack_extend_origin_memory_) \
: "a"((char *)stack_extend_memory_ + (size)-1024));
#define END_STACK_EXTEND \
asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_)); \
free(stack_extend_memory_);
signed main() {
BEGIN_STACK_EXTEND
std::cin.tie(0);
ios::sync_with_stdio(false);
// ll Q; cin >> Q; while(Q--)solve();
solve();
END_STACK_EXTEND
return 0;
}
| replace | 368 | 369 | 368 | 369 | -11 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
/*
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble =
boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>; using
namespace boost::multiprecision; #endif
*/
#ifdef LOCAL_TEST
#define BOOST_STACKTRACE_USE_ADDR2LINE
#define BOOST_STACKTRACE_ADDR2LINE_LOCATION \
/ usr / local / opt / binutils / bin / addr2line
#define _GNU_SOURCE 1
#include <boost/stacktrace.hpp>
namespace std {
template <typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {}
dvector(const std::vector<T> &v) : std::vector<T>(v) {}
dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
dvector(const std::string::iterator first, const std::string::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::iterator first,
const typename std::vector<T>::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::reverse_iterator first,
const typename std::vector<T>::reverse_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_iterator first,
const typename std::vector<T>::const_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_reverse_iterator first,
const typename std::vector<T>::const_reverse_iterator last)
: std::vector<T>(first, last) {}
T &operator[](size_t n) {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
const T &operator[](size_t n) const {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
};
} // namespace std
class dbool {
private:
bool boolvalue;
public:
dbool() : boolvalue(false) {}
dbool(bool b) : boolvalue(b) {}
operator bool &() { return boolvalue; }
operator const bool &() const { return boolvalue; }
};
#define vector dvector
#define bool dbool
class SIGFPE_exception : std::exception {};
class SIGSEGV_exception : std::exception {};
void catch_SIGFPE([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGFPE_exception();
}
void catch_SIGSEGV([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGSEGV_exception();
}
signed convertedmain();
signed main() {
signal(SIGFPE, catch_SIGFPE);
signal(SIGSEGV, catch_SIGSEGV);
return convertedmain();
}
#define main() convertedmain()
#endif
#ifdef LOCAL_DEV
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::vector<std::vector<T>> &vv) {
s << "\\\n";
for (size_t i = 0; i < vv.size(); ++i) {
s << vv[i] << "\n";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) {
s << "{ ";
for (size_t i = 0; i < N; ++i) {
s << a[i] << "\t";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) {
s << "{\n";
for (auto itr = m.begin(); itr != m.end(); ++itr) {
s << "\t" << (*itr).first << " : " << (*itr).second << "\n";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
void debug_impl() { std::cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
std::cerr << " " << head << (sizeof...(tail) ? "," : "");
debug_impl(tail...);
}
#define debug(...) \
do { \
std::cerr << "(" << #__VA_ARGS__ << ") ="; \
debug_impl(__VA_ARGS__); \
} while (false)
#else
#define debug(...) \
do { \
} while (false)
#endif
// #define int long long
using ll = long long;
// constexpr int INF = (ll)1e9 + 7;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
#define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrepeq(i, n) for (ll i = (n); i >= 1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail> void p(Head head, Tail... tail) {
std::cout << head << (sizeof...(tail) ? " " : "");
p(tail...);
}
template <typename T> inline void pv(std::vector<T> &v) {
for (ll i = 0, N = v.size(); i < N; i++)
std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T> inline bool chmax(T &a, T b) {
return a < b && (a = b, true);
}
template <typename T> inline bool chmin(T &a, T b) {
return a > b && (a = b, true);
}
template <typename T> inline void uniq(std::vector<T> &v) {
v.erase(std::unique(v.begin(), v.end()), v.end());
}
/*-----8<-----template-----8<-----*/
template <ll char_size> class TrieNode {
public:
ll nxt[char_size];
ll exist;
vector<ll> accept;
TrieNode() : exist(0) { memset(nxt, -1, sizeof(nxt)); }
};
/*
あるノードの配下の全てのノードには自身に対応する文字列に共通するプレフィックスがあり,
ルートには空の文字列が対応している。
計算量:
add O(|S|)
query O(|S|)
exist は子供以下に追加された文字列の個数,
accept はそのノードにマッチする全ての追加された文字列の番号が格納される。
Trie< char_size, margin >():
文字列の種類数 char_size, 開始文字が margin のトライ木を作成する。
add(S): 文字列 S をトライ木に追加する
query(S, f): 文字列 S のプレフィックスに一致する文字列を検索する。
一致した文字列ごとに関数 f が呼び出される。
size(): ノード数を返す
count(): 存在する文字列の個数を返す
*/
template <ll char_size, ll margin> class Trie {
public:
using Node = TrieNode<char_size>;
vector<Node> nodes;
ll root;
Trie() : root(0) { nodes.push_back(Node()); }
void update_direct(ll node, ll id) { nodes[node].accept.push_back(id); }
void update_child(ll node, [[maybe_unused]] ll child,
[[maybe_unused]] ll id) {
++nodes[node].exist;
}
void add(const string &str, ll str_index, ll node_index, ll id) {
if (str_index == (ll)str.size()) {
update_direct(node_index, id);
} else {
const ll c = str[str_index] - margin;
if (nodes[node_index].nxt[c] == -1) {
nodes[node_index].nxt[c] = (ll)nodes.size();
nodes.push_back(Node());
}
add(str, str_index + 1, nodes[node_index].nxt[c], id);
update_child(node_index, nodes[node_index].nxt[c], id);
}
}
void add(const string &str, ll id) { add(str, 0, 0, id); }
void add(const string &str) { add(str, nodes[0].exist); }
template <typename F>
void query(const string &str, const F &f, ll str_index, ll node_index) {
for (auto &idx : nodes[node_index].accept)
f(idx);
if (str_index == (ll)str.size()) {
return;
} else {
const ll c = str[str_index] - margin;
if (nodes[node_index].nxt[c] == -1)
return;
query(str, f, str_index + 1, nodes[node_index].nxt[c]);
}
}
template <typename F> void query(const string &str, const F &f) {
query(str, f, 0, 0);
}
ll count() const { return (nodes[0].exist); }
size_t size() const { return nodes.size(); }
};
/*-----8<-----library-----8<-----*/
void solve() {
ll N;
cin >> N;
vector<string> a(N);
rep(i, N) {
cin >> a[i];
reverse(all(a[i]));
}
sort(a.begin(), a.end(),
[](const string &x, const string &y) { return x.size() > y.size(); });
vector<vector<ll>> pos(N, vector<ll>(26, -INF));
rep(i, N) { rep(j, a[i].size()) chmax(pos[i][a[i][j] - 'a'], j); }
Trie<26, 'a'> trie;
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
string t = a[i].substr(0, a[i].size() - 1);
trie.add(t);
}
ll ans = 0;
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
// index = trie木に追加した単語のindex
auto update = [&](ll index) {
debug(i, index);
if (index == i)
return;
ll x = a[index].back() - 'a';
if (pos[i][x] >= (ll)(a[index].size()) - 1)
ans++;
};
// Sのi文字目から一致する単語それぞれに対して関数 update が呼ばれる
debug(a[i]);
trie.query(a[i], update, 0, 0);
}
debug(ans);
vector<ll> c(26, 0);
rep(i, N) {
if (a[i].size() != (size_t)1)
continue;
c[a[i][0] - 'a']++;
}
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
vector<ll> t(26, false);
rep(j, a[i].size()) t[a[i][j] - 'a'] = true;
rep(j, 26) {
if (t[j])
ans += c[j];
}
}
p(ans);
}
#define BEGIN_STACK_EXTEND \
long long size = 2048 * 1024 * 1024; \
void *stack_extend_memory_ = malloc(size); \
void *stack_extend_origin_memory_; \
char *stack_extend_dummy_memory_ = (char *)alloca( \
(1 + (int)(((long long)stack_extend_memory_) & 127)) * 16); \
*stack_extend_dummy_memory_ = 0; \
asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp" \
: "=b"(stack_extend_origin_memory_) \
: "a"((char *)stack_extend_memory_ + (size)-1024));
#define END_STACK_EXTEND \
asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_)); \
free(stack_extend_memory_);
signed main() {
BEGIN_STACK_EXTEND
std::cin.tie(0);
ios::sync_with_stdio(false);
// ll Q; cin >> Q; while(Q--)solve();
solve();
END_STACK_EXTEND
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
/*
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble =
boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>; using
namespace boost::multiprecision; #endif
*/
#ifdef LOCAL_TEST
#define BOOST_STACKTRACE_USE_ADDR2LINE
#define BOOST_STACKTRACE_ADDR2LINE_LOCATION \
/ usr / local / opt / binutils / bin / addr2line
#define _GNU_SOURCE 1
#include <boost/stacktrace.hpp>
namespace std {
template <typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {}
dvector(const std::vector<T> &v) : std::vector<T>(v) {}
dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
dvector(const std::string::iterator first, const std::string::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::iterator first,
const typename std::vector<T>::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::reverse_iterator first,
const typename std::vector<T>::reverse_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_iterator first,
const typename std::vector<T>::const_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_reverse_iterator first,
const typename std::vector<T>::const_reverse_iterator last)
: std::vector<T>(first, last) {}
T &operator[](size_t n) {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
const T &operator[](size_t n) const {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
};
} // namespace std
class dbool {
private:
bool boolvalue;
public:
dbool() : boolvalue(false) {}
dbool(bool b) : boolvalue(b) {}
operator bool &() { return boolvalue; }
operator const bool &() const { return boolvalue; }
};
#define vector dvector
#define bool dbool
class SIGFPE_exception : std::exception {};
class SIGSEGV_exception : std::exception {};
void catch_SIGFPE([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGFPE_exception();
}
void catch_SIGSEGV([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGSEGV_exception();
}
signed convertedmain();
signed main() {
signal(SIGFPE, catch_SIGFPE);
signal(SIGSEGV, catch_SIGSEGV);
return convertedmain();
}
#define main() convertedmain()
#endif
#ifdef LOCAL_DEV
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::vector<std::vector<T>> &vv) {
s << "\\\n";
for (size_t i = 0; i < vv.size(); ++i) {
s << vv[i] << "\n";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) {
s << "{ ";
for (size_t i = 0; i < N; ++i) {
s << a[i] << "\t";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) {
s << "{\n";
for (auto itr = m.begin(); itr != m.end(); ++itr) {
s << "\t" << (*itr).first << " : " << (*itr).second << "\n";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
void debug_impl() { std::cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
std::cerr << " " << head << (sizeof...(tail) ? "," : "");
debug_impl(tail...);
}
#define debug(...) \
do { \
std::cerr << "(" << #__VA_ARGS__ << ") ="; \
debug_impl(__VA_ARGS__); \
} while (false)
#else
#define debug(...) \
do { \
} while (false)
#endif
// #define int long long
using ll = long long;
// constexpr int INF = (ll)1e9 + 7;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
#define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrepeq(i, n) for (ll i = (n); i >= 1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail> void p(Head head, Tail... tail) {
std::cout << head << (sizeof...(tail) ? " " : "");
p(tail...);
}
template <typename T> inline void pv(std::vector<T> &v) {
for (ll i = 0, N = v.size(); i < N; i++)
std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T> inline bool chmax(T &a, T b) {
return a < b && (a = b, true);
}
template <typename T> inline bool chmin(T &a, T b) {
return a > b && (a = b, true);
}
template <typename T> inline void uniq(std::vector<T> &v) {
v.erase(std::unique(v.begin(), v.end()), v.end());
}
/*-----8<-----template-----8<-----*/
template <ll char_size> class TrieNode {
public:
ll nxt[char_size];
ll exist;
vector<ll> accept;
TrieNode() : exist(0) { memset(nxt, -1, sizeof(nxt)); }
};
/*
あるノードの配下の全てのノードには自身に対応する文字列に共通するプレフィックスがあり,
ルートには空の文字列が対応している。
計算量:
add O(|S|)
query O(|S|)
exist は子供以下に追加された文字列の個数,
accept はそのノードにマッチする全ての追加された文字列の番号が格納される。
Trie< char_size, margin >():
文字列の種類数 char_size, 開始文字が margin のトライ木を作成する。
add(S): 文字列 S をトライ木に追加する
query(S, f): 文字列 S のプレフィックスに一致する文字列を検索する。
一致した文字列ごとに関数 f が呼び出される。
size(): ノード数を返す
count(): 存在する文字列の個数を返す
*/
template <ll char_size, ll margin> class Trie {
public:
using Node = TrieNode<char_size>;
vector<Node> nodes;
ll root;
Trie() : root(0) { nodes.push_back(Node()); }
void update_direct(ll node, ll id) { nodes[node].accept.push_back(id); }
void update_child(ll node, [[maybe_unused]] ll child,
[[maybe_unused]] ll id) {
++nodes[node].exist;
}
void add(const string &str, ll str_index, ll node_index, ll id) {
if (str_index == (ll)str.size()) {
update_direct(node_index, id);
} else {
const ll c = str[str_index] - margin;
if (nodes[node_index].nxt[c] == -1) {
nodes[node_index].nxt[c] = (ll)nodes.size();
nodes.push_back(Node());
}
add(str, str_index + 1, nodes[node_index].nxt[c], id);
update_child(node_index, nodes[node_index].nxt[c], id);
}
}
void add(const string &str, ll id) { add(str, 0, 0, id); }
void add(const string &str) { add(str, nodes[0].exist); }
template <typename F>
void query(const string &str, const F &f, ll str_index, ll node_index) {
for (auto &idx : nodes[node_index].accept)
f(idx);
if (str_index == (ll)str.size()) {
return;
} else {
const ll c = str[str_index] - margin;
if (nodes[node_index].nxt[c] == -1)
return;
query(str, f, str_index + 1, nodes[node_index].nxt[c]);
}
}
template <typename F> void query(const string &str, const F &f) {
query(str, f, 0, 0);
}
ll count() const { return (nodes[0].exist); }
size_t size() const { return nodes.size(); }
};
/*-----8<-----library-----8<-----*/
void solve() {
ll N;
cin >> N;
vector<string> a(N);
rep(i, N) {
cin >> a[i];
reverse(all(a[i]));
}
sort(a.begin(), a.end(),
[](const string &x, const string &y) { return x.size() > y.size(); });
vector<vector<ll>> pos(N, vector<ll>(26, -INF));
rep(i, N) { rep(j, a[i].size()) chmax(pos[i][a[i][j] - 'a'], j); }
Trie<26, 'a'> trie;
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
string t = a[i].substr(0, a[i].size() - 1);
trie.add(t);
}
ll ans = 0;
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
// index = trie木に追加した単語のindex
auto update = [&](ll index) {
debug(i, index);
if (index == i)
return;
ll x = a[index].back() - 'a';
if (pos[i][x] >= (ll)(a[index].size()) - 1)
ans++;
};
// Sのi文字目から一致する単語それぞれに対して関数 update が呼ばれる
debug(a[i]);
trie.query(a[i], update, 0, 0);
}
debug(ans);
vector<ll> c(26, 0);
rep(i, N) {
if (a[i].size() != (size_t)1)
continue;
c[a[i][0] - 'a']++;
}
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
vector<ll> t(26, false);
rep(j, a[i].size()) t[a[i][j] - 'a'] = true;
rep(j, 26) {
if (t[j])
ans += c[j];
}
}
p(ans);
}
#define BEGIN_STACK_EXTEND \
long long size = 1024LL * 1024LL * 1024LL * 2; \
void *stack_extend_memory_ = malloc(size); \
void *stack_extend_origin_memory_; \
char *stack_extend_dummy_memory_ = (char *)alloca( \
(1 + (int)(((long long)stack_extend_memory_) & 127)) * 16); \
*stack_extend_dummy_memory_ = 0; \
asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp" \
: "=b"(stack_extend_origin_memory_) \
: "a"((char *)stack_extend_memory_ + (size)-1024));
#define END_STACK_EXTEND \
asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_)); \
free(stack_extend_memory_);
signed main() {
BEGIN_STACK_EXTEND
std::cin.tie(0);
ios::sync_with_stdio(false);
// ll Q; cin >> Q; while(Q--)solve();
solve();
END_STACK_EXTEND
return 0;
}
| replace | 358 | 359 | 358 | 359 | -11 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
/*
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble =
boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>; using
namespace boost::multiprecision; #endif
*/
#ifdef LOCAL_TEST
#define BOOST_STACKTRACE_USE_ADDR2LINE
#define BOOST_STACKTRACE_ADDR2LINE_LOCATION \
/ usr / local / opt / binutils / bin / addr2line
#define _GNU_SOURCE 1
#include <boost/stacktrace.hpp>
namespace std {
template <typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {}
dvector(const std::vector<T> &v) : std::vector<T>(v) {}
dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
dvector(const std::string::iterator first, const std::string::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::iterator first,
const typename std::vector<T>::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::reverse_iterator first,
const typename std::vector<T>::reverse_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_iterator first,
const typename std::vector<T>::const_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_reverse_iterator first,
const typename std::vector<T>::const_reverse_iterator last)
: std::vector<T>(first, last) {}
T &operator[](size_t n) {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
const T &operator[](size_t n) const {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
};
} // namespace std
class dbool {
private:
bool boolvalue;
public:
dbool() : boolvalue(false) {}
dbool(bool b) : boolvalue(b) {}
operator bool &() { return boolvalue; }
operator const bool &() const { return boolvalue; }
};
#define vector dvector
#define bool dbool
class SIGFPE_exception : std::exception {};
class SIGSEGV_exception : std::exception {};
void catch_SIGFPE([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGFPE_exception();
}
void catch_SIGSEGV([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGSEGV_exception();
}
signed convertedmain();
signed main() {
signal(SIGFPE, catch_SIGFPE);
signal(SIGSEGV, catch_SIGSEGV);
return convertedmain();
}
#define main() convertedmain()
#endif
#ifdef LOCAL_DEV
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::vector<std::vector<T>> &vv) {
s << "\\\n";
for (size_t i = 0; i < vv.size(); ++i) {
s << vv[i] << "\n";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) {
s << "{ ";
for (size_t i = 0; i < N; ++i) {
s << a[i] << "\t";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) {
s << "{\n";
for (auto itr = m.begin(); itr != m.end(); ++itr) {
s << "\t" << (*itr).first << " : " << (*itr).second << "\n";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
void debug_impl() { std::cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
std::cerr << " " << head << (sizeof...(tail) ? "," : "");
debug_impl(tail...);
}
#define debug(...) \
do { \
std::cerr << "(" << #__VA_ARGS__ << ") ="; \
debug_impl(__VA_ARGS__); \
} while (false)
#else
#define debug(...) \
do { \
} while (false)
#endif
// #define int long long
using ll = long long;
// constexpr int INF = (ll)1e9 + 7;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
#define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrepeq(i, n) for (ll i = (n); i >= 1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail> void p(Head head, Tail... tail) {
std::cout << head << (sizeof...(tail) ? " " : "");
p(tail...);
}
template <typename T> inline void pv(std::vector<T> &v) {
for (ll i = 0, N = v.size(); i < N; i++)
std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T> inline bool chmax(T &a, T b) {
return a < b && (a = b, true);
}
template <typename T> inline bool chmin(T &a, T b) {
return a > b && (a = b, true);
}
template <typename T> inline void uniq(std::vector<T> &v) {
v.erase(std::unique(v.begin(), v.end()), v.end());
}
/*-----8<-----template-----8<-----*/
/*-----8<-----library-----8<-----*/
void solve() {
ll N;
cin >> N;
vector<string> a(N);
rep(i, N) {
cin >> a[i];
reverse(all(a[i]));
}
sort(a.begin(), a.end(),
[](const string &x, const string &y) { return x.size() > y.size(); });
vector<vector<ll>> pos(N, vector<ll>(26, -INF));
rep(i, N) { rep(j, a[i].size()) chmax(pos[i][a[i][j] - 'a'], j); }
set<ll> stringlen;
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
stringlen.insert(a[i].size() - 1);
}
map<string, vector<ll>> hashmap;
rep(i, N) {
for (ll len : stringlen) {
if (len > (ll)a[i].size())
break;
string h = a[i].substr(i, len);
hashmap[h].emplace_back(i);
}
}
ll ans = 0;
rep(index, N) {
if (a[index].size() == (size_t)1)
continue;
auto update = [&](ll i) {
if (index == i)
return;
ll x = a[index].back() - 'a';
if (pos[i][x] >= (ll)(a[index].size()) - 1)
ans++;
};
string h = a[index].substr(0, a[index].size() - 1);
for (auto &&i : hashmap[h])
update(i);
}
vector<ll> c(26, 0);
rep(i, N) {
if (a[i].size() != (size_t)1)
continue;
c[a[i][0] - 'a']++;
}
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
vector<ll> t(26, false);
rep(j, a[i].size()) t[a[i][j] - 'a'] = true;
rep(j, 26) {
if (t[j])
ans += c[j];
}
}
p(ans);
}
signed main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
// ll Q; cin >> Q; while(Q--)solve();
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
/*
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble =
boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>; using
namespace boost::multiprecision; #endif
*/
#ifdef LOCAL_TEST
#define BOOST_STACKTRACE_USE_ADDR2LINE
#define BOOST_STACKTRACE_ADDR2LINE_LOCATION \
/ usr / local / opt / binutils / bin / addr2line
#define _GNU_SOURCE 1
#include <boost/stacktrace.hpp>
namespace std {
template <typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {}
dvector(const std::vector<T> &v) : std::vector<T>(v) {}
dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
dvector(const std::string::iterator first, const std::string::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::iterator first,
const typename std::vector<T>::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::reverse_iterator first,
const typename std::vector<T>::reverse_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_iterator first,
const typename std::vector<T>::const_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_reverse_iterator first,
const typename std::vector<T>::const_reverse_iterator last)
: std::vector<T>(first, last) {}
T &operator[](size_t n) {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
const T &operator[](size_t n) const {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
};
} // namespace std
class dbool {
private:
bool boolvalue;
public:
dbool() : boolvalue(false) {}
dbool(bool b) : boolvalue(b) {}
operator bool &() { return boolvalue; }
operator const bool &() const { return boolvalue; }
};
#define vector dvector
#define bool dbool
class SIGFPE_exception : std::exception {};
class SIGSEGV_exception : std::exception {};
void catch_SIGFPE([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGFPE_exception();
}
void catch_SIGSEGV([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGSEGV_exception();
}
signed convertedmain();
signed main() {
signal(SIGFPE, catch_SIGFPE);
signal(SIGSEGV, catch_SIGSEGV);
return convertedmain();
}
#define main() convertedmain()
#endif
#ifdef LOCAL_DEV
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::vector<std::vector<T>> &vv) {
s << "\\\n";
for (size_t i = 0; i < vv.size(); ++i) {
s << vv[i] << "\n";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) {
s << "{ ";
for (size_t i = 0; i < N; ++i) {
s << a[i] << "\t";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) {
s << "{\n";
for (auto itr = m.begin(); itr != m.end(); ++itr) {
s << "\t" << (*itr).first << " : " << (*itr).second << "\n";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
void debug_impl() { std::cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
std::cerr << " " << head << (sizeof...(tail) ? "," : "");
debug_impl(tail...);
}
#define debug(...) \
do { \
std::cerr << "(" << #__VA_ARGS__ << ") ="; \
debug_impl(__VA_ARGS__); \
} while (false)
#else
#define debug(...) \
do { \
} while (false)
#endif
// #define int long long
using ll = long long;
// constexpr int INF = (ll)1e9 + 7;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
#define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrepeq(i, n) for (ll i = (n); i >= 1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail> void p(Head head, Tail... tail) {
std::cout << head << (sizeof...(tail) ? " " : "");
p(tail...);
}
template <typename T> inline void pv(std::vector<T> &v) {
for (ll i = 0, N = v.size(); i < N; i++)
std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T> inline bool chmax(T &a, T b) {
return a < b && (a = b, true);
}
template <typename T> inline bool chmin(T &a, T b) {
return a > b && (a = b, true);
}
template <typename T> inline void uniq(std::vector<T> &v) {
v.erase(std::unique(v.begin(), v.end()), v.end());
}
/*-----8<-----template-----8<-----*/
/*-----8<-----library-----8<-----*/
void solve() {
ll N;
cin >> N;
vector<string> a(N);
rep(i, N) {
cin >> a[i];
reverse(all(a[i]));
}
sort(a.begin(), a.end(),
[](const string &x, const string &y) { return x.size() > y.size(); });
vector<vector<ll>> pos(N, vector<ll>(26, -INF));
rep(i, N) { rep(j, a[i].size()) chmax(pos[i][a[i][j] - 'a'], j); }
set<ll> stringlen;
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
stringlen.insert(a[i].size() - 1);
}
map<string, vector<ll>> hashmap;
rep(i, N) {
for (ll len : stringlen) {
if (len > (ll)a[i].size())
break;
string h = a[i].substr(0, len);
hashmap[h].emplace_back(i);
}
}
ll ans = 0;
rep(index, N) {
if (a[index].size() == (size_t)1)
continue;
auto update = [&](ll i) {
if (index == i)
return;
ll x = a[index].back() - 'a';
if (pos[i][x] >= (ll)(a[index].size()) - 1)
ans++;
};
string h = a[index].substr(0, a[index].size() - 1);
for (auto &&i : hashmap[h])
update(i);
}
vector<ll> c(26, 0);
rep(i, N) {
if (a[i].size() != (size_t)1)
continue;
c[a[i][0] - 'a']++;
}
rep(i, N) {
if (a[i].size() == (size_t)1)
continue;
vector<ll> t(26, false);
rep(j, a[i].size()) t[a[i][j] - 'a'] = true;
rep(j, 26) {
if (t[j])
ans += c[j];
}
}
p(ans);
}
signed main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
// ll Q; cin >> Q; while(Q--)solve();
solve();
return 0;
}
| replace | 236 | 237 | 236 | 237 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef OJ
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
#define all(x) (x).begin(), (x).end()
#define rsort(x) sort((x).rbegin(), (x).rend())
#define sz(x) ((int)x.size())
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define ford(i, n, e) for (int i = (int)(n); i >= (int)(e); --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
template <typename T> bool mmax(T &m, const T q) {
if (m < q) {
m = q;
return true;
} else
return false;
}
template <typename T> bool mmin(T &m, const T q) {
if (m > q) {
m = q;
return true;
} else
return false;
}
#define RETURN(x) \
do { \
cout << (x) << '\n'; \
return 0; \
} while (0)
const char en = '\n';
const int MOD = 0;
const int MAXN = 0;
const int INF = 1e9;
struct stNode {
vector<stNode *> child = vector<stNode *>(26, NULL);
vector<int> has = vector<int>(26);
void add(const string &s, const vector<int> &lst, int i) {
if (i == sz(s))
return;
int c = s[i] - 'a';
if (child[c] == NULL)
child[c] = new stNode();
fori(k, 26) {
if (lst[k] >= i) {
has[k]++;
}
}
child[c]->add(s, lst, i + 1);
}
int check(const string &s, int i) {
int c = s[i] - 'a';
if (i == sz(s) - 1) {
return has[c];
}
if (child[c] == NULL)
return false;
return child[c]->check(s, i + 1);
}
};
int solve() {
int n;
cin >> n;
vector<string> v(n);
map<string, int> m;
fori(i, n) {
cin >> v[i];
reverse(all(v[i]));
m[v[i]]++;
}
stNode *root1 = new stNode();
LL ans = 0;
fori(i, n) {
string &s = v[i];
ans += root1->check(s, 0);
debug(s, ans);
vector<int> lst(26, -1);
fori(j, sz(s)) lst[s[j] - 'a'] = j;
root1->add(s, lst, 0);
}
stNode *root2 = new stNode();
ford(i, n - 1, 0) {
string &s = v[i];
ans += root2->check(s, 0);
vector<int> lst(26, -1);
fori(j, sz(s)) lst[s[j] - 'a'] = j;
root2->add(s, lst, 0);
}
for (auto &it : m) {
ans -= 1LL * it.se * (it.se - 1) / 2;
}
cout << ans << en;
}
int main(int argc, char **argv) {
#ifdef OJ
freopen("input.txt", "rt", stdin);
// freopen("output.txt", "wt", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int ntest = 1;
// cin >> ntest;
fori(test, ntest) {
// cout << "Case #" << test+1 << ": ";
solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef OJ
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
#define all(x) (x).begin(), (x).end()
#define rsort(x) sort((x).rbegin(), (x).rend())
#define sz(x) ((int)x.size())
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define ford(i, n, e) for (int i = (int)(n); i >= (int)(e); --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
template <typename T> bool mmax(T &m, const T q) {
if (m < q) {
m = q;
return true;
} else
return false;
}
template <typename T> bool mmin(T &m, const T q) {
if (m > q) {
m = q;
return true;
} else
return false;
}
#define RETURN(x) \
do { \
cout << (x) << '\n'; \
return 0; \
} while (0)
const char en = '\n';
const int MOD = 0;
const int MAXN = 0;
const int INF = 1e9;
struct stNode {
vector<stNode *> child = vector<stNode *>(26, NULL);
vector<int> has = vector<int>(26);
void add(const string &s, const vector<int> &lst, int i) {
if (i == sz(s))
return;
int c = s[i] - 'a';
if (child[c] == NULL)
child[c] = new stNode();
fori(k, 26) {
if (lst[k] >= i) {
has[k]++;
}
}
child[c]->add(s, lst, i + 1);
}
int check(const string &s, int i) {
int c = s[i] - 'a';
if (i == sz(s) - 1) {
return has[c];
}
if (child[c] == NULL)
return false;
return child[c]->check(s, i + 1);
}
};
int solve() {
int n;
cin >> n;
vector<string> v(n);
map<string, int> m;
fori(i, n) {
cin >> v[i];
reverse(all(v[i]));
m[v[i]]++;
}
stNode *root1 = new stNode();
LL ans = 0;
fori(i, n) {
string &s = v[i];
ans += root1->check(s, 0);
debug(s, ans);
vector<int> lst(26, -1);
fori(j, sz(s)) lst[s[j] - 'a'] = j;
root1->add(s, lst, 0);
}
stNode *root2 = new stNode();
ford(i, n - 1, 0) {
string &s = v[i];
ans += root2->check(s, 0);
vector<int> lst(26, -1);
fori(j, sz(s)) lst[s[j] - 'a'] = j;
root2->add(s, lst, 0);
}
for (auto &it : m) {
ans -= 1LL * it.se * (it.se - 1) / 2;
}
cout << ans << en;
return 0;
}
int main(int argc, char **argv) {
#ifdef OJ
freopen("input.txt", "rt", stdin);
// freopen("output.txt", "wt", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int ntest = 1;
// cin >> ntest;
fori(test, ntest) {
// cout << "Case #" << test+1 << ": ";
solve();
}
return 0;
}
| insert | 156 | 156 | 156 | 157 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.