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
p02755
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) using namespace std; typedef long long ll; ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } int main() { ll a, b, ans = 10001; cin >> a >> b; for (ll i = 10000; i > 0; i++) { if (i * 8 / 100 == a && i * 10 / 100 == b) { ans = i; } } cout << (ans == 10001 ? -1 : ans) << endl; return 0; }
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) using namespace std; typedef long long ll; ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } int main() { ll a, b, ans = 10001; cin >> a >> b; for (ll i = 10000; i > 0; i--) { if (i * 8 / 100 == a && i * 10 / 100 == b) { ans = i; } } cout << (ans == 10001 ? -1 : ans) << endl; return 0; }
replace
11
12
11
12
TLE
p02755
C++
Time Limit Exceeded
// SmartCoder #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace __gnu_pbds; using namespace std; #define sz(a) int((a).size()) #define pb push_back #define mp make_pair #define all(c) (c).begin(), (c).end() #define tr(c, i) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define present(c, x) ((c).find(x) != (c).end()) #define cpresent(c, x) (find(all(c), x) != (c).end()) #define minei(x) min_element(x.begin(), x.end()) - (x).begin() #define maxei(x) max_element(x.begin(), x.end()) - (x).begin() #define LSOne(S) (S & (-S)) #define uns(v) \ sort((v).begin(), (v).end()), v.erase(unique(v.begin(), v.end()), v.end()) #define acusum(x) accumulate(x.begin(), x.end(), 0) #define acumul(x) accumulate(x.begin(), x.end(), 1, multiplies<int>()); #define bits(x) __builtin_popcount(x) #define oo INT_MAX #define inf 1000000000 #define MAXN 1000007 #define MOD 1000000007 const double pi = acos(-1.0); const double eps = 1e-11; typedef long long ll; typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<int> vi; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; int main() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a, b; cin >> a >> b; for (int i = 1; max(a, b) * 100; i++) { double aa = floor(i * 0.08); double bb = floor(i * .1); if (aa == a && bb == b) { cout << i << '\n'; return 0; } } cout << "-1\n"; return 0; }
// SmartCoder #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace __gnu_pbds; using namespace std; #define sz(a) int((a).size()) #define pb push_back #define mp make_pair #define all(c) (c).begin(), (c).end() #define tr(c, i) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define present(c, x) ((c).find(x) != (c).end()) #define cpresent(c, x) (find(all(c), x) != (c).end()) #define minei(x) min_element(x.begin(), x.end()) - (x).begin() #define maxei(x) max_element(x.begin(), x.end()) - (x).begin() #define LSOne(S) (S & (-S)) #define uns(v) \ sort((v).begin(), (v).end()), v.erase(unique(v.begin(), v.end()), v.end()) #define acusum(x) accumulate(x.begin(), x.end(), 0) #define acumul(x) accumulate(x.begin(), x.end(), 1, multiplies<int>()); #define bits(x) __builtin_popcount(x) #define oo INT_MAX #define inf 1000000000 #define MAXN 1000007 #define MOD 1000000007 const double pi = acos(-1.0); const double eps = 1e-11; typedef long long ll; typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<int> vi; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; int main() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a, b; cin >> a >> b; for (int i = 1; i <= max(a, b) * 100; i++) { double aa = floor(i * 0.08); double bb = floor(i * .1); if (aa == a && bb == b) { cout << i << '\n'; return 0; } } cout << "-1\n"; return 0; }
replace
49
50
49
50
TLE
p02755
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; int ans = min(int(a / 0.08), int(b / 0.1)); while (int(ans * 0.08) != a || int(ans * 0.1) != b) { ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; int ans = min(int(a / 0.08), int(b / 0.1)); while (true) { if (int(ans * 0.08) == a && int(ans * 0.1) == b) { break; } if (int(ans * 0.08) > a || int(ans * 0.1) > b) { ans = -1; break; } ans++; } cout << ans << endl; return 0; }
replace
8
9
8
16
TLE
p02755
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) using namespace std; int main() { int a, b; cin >> a >> b; bool flag = true; int x = 1, x_a, x_b, ans; while (flag) { x_a = x * 0.08; x_b = x * 0.10; if (x_a == a && x_b == b) { flag = false; ans = x; } x++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) using namespace std; int main() { int a, b; cin >> a >> b; bool flag = true; int x = 1, x_a, x_b, ans; while (flag) { x_a = x * 0.08; x_b = x * 0.10; if (x_a == a && x_b == b) { flag = false; ans = x; } if (x >= 1009) { ans = -1; break; } x++; } cout << ans << endl; return 0; }
insert
16
16
16
20
TLE
p02755
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; bool flg = true; long double X; int a, b; for (long i = 0; i < 1000000000; i++) { X = i; a = X * 0.08; b = X * 0.1; if (a == A && b == B) { cout << i; flg = false; break; } } if (flg) cout << "-1"; cout << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; bool flg = true; long double X; int a, b; for (long i = 0; i < 400000000; i++) { X = i; a = X * 0.08; b = X * 0.1; if (a == A && b == B) { cout << i; flg = false; break; } } if (flg) cout << "-1"; cout << endl; }
replace
10
11
10
11
TLE
p02755
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; #define MOD 1000000007 #define FOR(i, m, n) for (ll i = m; i <= n; i++) #define FORR(i, m, n) for (ll i = m; i >= n; i--) ll gcd(ll a, ll b) { if (a < b) { a ^= b; b ^= a; a ^= b; } return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } int main() { ll A, B; cin >> A >> B; bool C[10000]; FOR(i, 1, 10000) { C[i] = i * 8 / 100 == A; } FOR(i, 1, 10000) { if (C[i] && i * 10 / 100 == B) { cout << i << endl; return 0; } } cout << -1 << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; #define MOD 1000000007 #define FOR(i, m, n) for (ll i = m; i <= n; i++) #define FORR(i, m, n) for (ll i = m; i >= n; i--) ll gcd(ll a, ll b) { if (a < b) { a ^= b; b ^= a; a ^= b; } return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } int main() { ll A, B; cin >> A >> B; bool C[10001]; FOR(i, 1, 10000) { C[i] = i * 8 / 100 == A; } FOR(i, 1, 10000) { if (C[i] && i * 10 / 100 == B) { cout << i << endl; return 0; } } cout << -1 << endl; return 0; }
replace
34
35
34
35
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { char res[100000] = {}; int q; deque<char> d; string s; cin >> s >> q; for (char &c : s) { d.push_back(c); } int rflag = 0; for (int i = 0; i < q; i++) { int t, f; char c; cin >> t; if (t == 1) { rflag ^= 1; continue; } cin >> f >> c; if (rflag == 0 && f == 1) { d.push_front(c); } else if (rflag == 0 && f == 2) { d.push_back(c); } else if (rflag == 1 && f == 1) { d.push_back(c); } else if (rflag == 1 && f == 2) { d.push_front(c); } } int i = 0; if (rflag) { for (auto di = d.rbegin(); di != d.rend(); ++di) { res[i++] = *di; } } else { for (auto di = d.begin(); di != d.end(); ++di) { res[i++] = *di; } } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; int main() { char res[300010] = {}; int q; deque<char> d; string s; cin >> s >> q; for (char &c : s) { d.push_back(c); } int rflag = 0; for (int i = 0; i < q; i++) { int t, f; char c; cin >> t; if (t == 1) { rflag ^= 1; continue; } cin >> f >> c; if (rflag == 0 && f == 1) { d.push_front(c); } else if (rflag == 0 && f == 2) { d.push_back(c); } else if (rflag == 1 && f == 1) { d.push_back(c); } else if (rflag == 1 && f == 2) { d.push_front(c); } } int i = 0; if (rflag) { for (auto di = d.rbegin(); di != d.rend(); ++di) { res[i++] = *di; } } else { for (auto di = d.begin(); di != d.end(); ++di) { res[i++] = *di; } } cout << res << endl; }
replace
4
5
4
5
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int maxn = 1e6 + 5; const ll mod = 1e9 + 7; char s[maxn]; int main() { scanf("%s", s + 100000); int beg = 100000; int end = strlen(s + beg) + beg; int flg = 1; int q; scanf("%d", &q); while (q--) { int t; scanf("%d", &t); if (t == 1) { flg = (3 - flg); } else { int k; char c; scanf("%d %c", &k, &c); if (flg == 1) { if (k == 1) { s[beg - 1] = c; beg--; } else { s[end++] = c; } } else { if (k == 2) { s[beg - 1] = c; beg--; } else { s[end++] = c; } } } } if (flg == 1) { for (int i = beg; i < end; i++) { printf("%c", s[i]); } } else { for (int i = end - 1; i >= beg; i--) { printf("%c", s[i]); } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int maxn = 1e6 + 5; const ll mod = 1e9 + 7; char s[maxn]; int main() { scanf("%s", s + 300000); int beg = 300000; int end = strlen(s + beg) + beg; int flg = 1; int q; scanf("%d", &q); while (q--) { int t; scanf("%d", &t); if (t == 1) { flg = (3 - flg); } else { int k; char c; scanf("%d %c", &k, &c); if (flg == 1) { if (k == 1) { s[beg - 1] = c; beg--; } else { s[end++] = c; } } else { if (k == 2) { s[beg - 1] = c; beg--; } else { s[end++] = c; } } } } if (flg == 1) { for (int i = beg; i < end; i++) { printf("%c", s[i]); } } else { for (int i = end - 1; i >= beg; i--) { printf("%c", s[i]); } } return 0; }
replace
14
16
14
16
0
p02756
C++
Runtime Error
/* Author: ankrypt */ #include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define ll long long int #define u64 unsigned long long int #define ff first #define ss second #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); ll N, K, A, B, C; char arr[1000005]; vector<ll> V; string S; ll cases; ll type; char X; int main() { IOS; cin >> S; ll ind = 150005; ll l = ind; for (int i = 0; i < S.size(); i++) { arr[ind] = S[i]; ind++; } ll r = ind - 1; // cout << l << " " << r << " " << arr[l] << " "; // scanf("%lld", &cases); cin >> cases; ll mark = 0; while (cases--) { cin >> A; if (A == 2) { cin >> B; cin >> X; if (!mark) { if (B == 2) { r++; arr[r] = X; } else { l--; arr[l] = X; } } else { if (B == 2) { r--; arr[r] = X; } else { l++; arr[l] = X; } } } else { mark ^= 1; swap(r, l); } } string ans = ""; if (mark) { for (int i = l; i >= r; i--) { ans.push_back(arr[i]); } } else { for (int i = l; i <= r; i++) { ans.push_back(arr[i]); } } cout << ans << " \n"; return 0; } /* Powered by Buggy Plugin */
/* Author: ankrypt */ #include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define ll long long int #define u64 unsigned long long int #define ff first #define ss second #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); ll N, K, A, B, C; char arr[1000005]; vector<ll> V; string S; ll cases; ll type; char X; int main() { IOS; cin >> S; ll ind = 550005; ll l = ind; for (int i = 0; i < S.size(); i++) { arr[ind] = S[i]; ind++; } ll r = ind - 1; // cout << l << " " << r << " " << arr[l] << " "; // scanf("%lld", &cases); cin >> cases; ll mark = 0; while (cases--) { cin >> A; if (A == 2) { cin >> B; cin >> X; if (!mark) { if (B == 2) { r++; arr[r] = X; } else { l--; arr[l] = X; } } else { if (B == 2) { r--; arr[r] = X; } else { l++; arr[l] = X; } } } else { mark ^= 1; swap(r, l); } } string ans = ""; if (mark) { for (int i = l; i >= r; i--) { ans.push_back(arr[i]); } } else { for (int i = l; i <= r; i++) { ans.push_back(arr[i]); } } cout << ans << " \n"; return 0; } /* Powered by Buggy Plugin */
replace
29
30
29
30
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; int main() { cin.tie(0); ios::sync_with_stdio(false); string S; int Q; cin >> S >> Q; vector<char> V(300000); int l = 100000; int r = l + S.size() - 1; for (int i = 0; i < S.size(); ++i) { V.at(l + i) = S.at(i); } int fr = 1; while (Q--) { int T; cin >> T; if (T == 1) { fr ^= 1; } else { int F; char C; cin >> F >> C; if (F == 1) { V.at((fr) ? --l : ++r) = C; } else { V.at((fr) ? ++r : --l) = C; } } } if (fr) { for (int i = l; i <= r; ++i) { cout << V.at(i); } } else { for (int i = r; i >= l; --i) { cout << V.at(i); } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; int main() { cin.tie(0); ios::sync_with_stdio(false); string S; int Q; cin >> S >> Q; vector<char> V(700000); int l = 300000; int r = l + S.size() - 1; for (int i = 0; i < S.size(); ++i) { V.at(l + i) = S.at(i); } int fr = 1; while (Q--) { int T; cin >> T; if (T == 1) { fr ^= 1; } else { int F; char C; cin >> F >> C; if (F == 1) { V.at((fr) ? --l : ++r) = C; } else { V.at((fr) ? ++r : --l) = C; } } } if (fr) { for (int i = l; i <= r; ++i) { cout << V.at(i); } } else { for (int i = r; i >= l; --i) { cout << V.at(i); } } cout << endl; return 0; }
replace
13
15
13
15
0
p02756
C++
Runtime Error
#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> char str[10010]; char str1[20010]; char str2[20010]; int main(int argc, char *argv[]) { int q = 0; int f = 0; int t; int ff; char c; int s1 = 0; int s2 = 0; int i; scanf("%s", str); scanf("%d", &q); for (i = 0; i < q; i++) { scanf("%d", &t); if (t == 1) { if (f) f = 0; else f = 1; } else { scanf("%d %c", &ff, &c); if ((f == 0 && ff == 1) || (f == 1 && ff == 2)) { str1[s1++] = c; } else { str2[s2++] = c; } } } // printf( "%d %s %s %s\n", f, str1, str, str2 ); if (f == 0) { int sss; for (i = 0; i < s1; i++) { printf("%c", str1[s1 - i - 1]); } sss = strlen(str); for (i = 0; i < sss; i++) { printf("%c", str[i]); } for (i = 0; i < s2; i++) { printf("%c", str2[i]); } } else { int sss; for (i = 0; i < s2; i++) { printf("%c", str2[s2 - i - 1]); } sss = strlen(str); for (i = 0; i < sss; i++) { printf("%c", str[sss - i - 1]); } for (i = 0; i < s1; i++) { printf("%c", str1[i]); } } }
#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> char str[100010]; char str1[200010]; char str2[200010]; int main(int argc, char *argv[]) { int q = 0; int f = 0; int t; int ff; char c; int s1 = 0; int s2 = 0; int i; scanf("%s", str); scanf("%d", &q); for (i = 0; i < q; i++) { scanf("%d", &t); if (t == 1) { if (f) f = 0; else f = 1; } else { scanf("%d %c", &ff, &c); if ((f == 0 && ff == 1) || (f == 1 && ff == 2)) { str1[s1++] = c; } else { str2[s2++] = c; } } } // printf( "%d %s %s %s\n", f, str1, str, str2 ); if (f == 0) { int sss; for (i = 0; i < s1; i++) { printf("%c", str1[s1 - i - 1]); } sss = strlen(str); for (i = 0; i < sss; i++) { printf("%c", str[i]); } for (i = 0; i < s2; i++) { printf("%c", str2[i]); } } else { int sss; for (i = 0; i < s2; i++) { printf("%c", str2[s2 - i - 1]); } sss = strlen(str); for (i = 0; i < sss; i++) { printf("%c", str[sss - i - 1]); } for (i = 0; i < s1; i++) { printf("%c", str1[i]); } } }
replace
7
10
7
10
0
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <cmath> #include <map> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define been(ix) (ix).begin(), (ix).end() using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vl; const ll INF = 1e18; #define foreach(ix, a) for(auto& (ix) : (a) int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int a; string s; cin >> s >> a; ll count = 0; rep(i, a) { int q; cin >> q; if (q == 1) { count++; count = count % 2; } else { int x; char y; cin >> x >> y; if ((x == 2 && count % 2 == 0)) s = s + y; else if ((x == 1 && count % 2 == 1)) s = s + y; else { s = y + s; } } } if (count % 2 == 1) reverse(been(s)); cout << s << endl; }
#include <bits/stdc++.h> #include <cmath> #include <map> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define been(ix) (ix).begin(), (ix).end() using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vl; const ll INF = 1e18; #define foreach(ix, a) for(auto& (ix) : (a) int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int a; string s; cin >> s >> a; ll count = 0; rep(i, a) { int q; cin >> q; if (q == 1) { count++; count = count % 2; } else { int x; cin >> x; if ((x == 2 && count % 2 == 0)) { char y; cin >> y; s.push_back(y); } else if ((x == 1 && count % 2 == 1)) { char y; cin >> y; s.push_back(y); } else { string y; cin >> y; s.insert(0, y); } } } if (count % 2 == 1) reverse(been(s)); cout << s << endl; }
replace
26
34
26
39
TLE
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; /* ll gcds(ll a,ll b){ if(a<b){ swap(a,b); } ll r=a%b; if(r==0){ return b; } while(r>0){ r=a%b; a=b; b=r; } return a; } ll lcms(ll a,ll b){ ll mmr=gcds(a,b); a=a/mmr; b=b/mmr; return a*b*mmr; } */ int main() { string S; cin >> S; int Q; cin >> Q; int rv = 0; for (int i = 0; i < Q; i++) { int t; cin >> t; if (t == 1) { rv++; } if (t == 2) { int f; cin >> f; char c; cin >> c; if ((f + rv) % 2 == 0) { S.push_back(c); } else { S = c + S; } } } if (rv % 2 == 1) { reverse(S.begin(), S.end()); } cout << S << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; /* ll gcds(ll a,ll b){ if(a<b){ swap(a,b); } ll r=a%b; if(r==0){ return b; } while(r>0){ r=a%b; a=b; b=r; } return a; } ll lcms(ll a,ll b){ ll mmr=gcds(a,b); a=a/mmr; b=b/mmr; return a*b*mmr; } */ int main() { string S; cin >> S; int Q; cin >> Q; int rv = 0; for (int i = 0; i < Q; i++) { int t; cin >> t; if (t == 1) { rv++; } if (t == 2) { int f; cin >> f; char c; cin >> c; if ((f + rv) % 2 == 0) { S.push_back(c); } else { S.insert(S.begin(), c); } } } if (rv % 2 == 1) { reverse(S.begin(), S.end()); } cout << S << endl; }
replace
50
51
50
51
TLE
p02756
C++
Time Limit Exceeded
#include <iostream> #include <stdio.h> // #include <bits/stdc++.h> #include <algorithm> #include <cassert> #include <cmath> #include <cmath> #include <cstdint> #include <cstring> #include <float.h> #include <iomanip> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> #define INF 1e9 #define rep(i, n) for (int i = 0; (i) < (int)(n); i++) #define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++) #define VEC(type, c, n) \ std::vector<type> c(n); \ for (auto &i : c) \ std::cin >> i; #define vec(type, n) vector<type>(n) #define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n)) #define ALL(a) (a).begin(), (a).end() using namespace std; using ll = long long; using Graph = vector<vector<int>>; using P = pair<int, int>; int main() { string s; int q; cin >> s >> q; int cnt = 0; int d = 0; rep(i, q) { int a; cin >> a; if (a == 1) cnt++; if (a == 2) { int b; char c; cin >> b >> c; if (cnt % 2 == 1) { d++; if (b == 1) b = 2; else if (b == 2) b = 1; } if (b == 1) s.insert(s.begin(), c); if (b == 2) s = s + c; } } if (cnt % 2 == 1) reverse(ALL(s)); cout << s << endl; }
#include <iostream> #include <stdio.h> // #include <bits/stdc++.h> #include <algorithm> #include <cassert> #include <cmath> #include <cmath> #include <cstdint> #include <cstring> #include <float.h> #include <iomanip> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> #define INF 1e9 #define rep(i, n) for (int i = 0; (i) < (int)(n); i++) #define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++) #define VEC(type, c, n) \ std::vector<type> c(n); \ for (auto &i : c) \ std::cin >> i; #define vec(type, n) vector<type>(n) #define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n)) #define ALL(a) (a).begin(), (a).end() using namespace std; using ll = long long; using Graph = vector<vector<int>>; using P = pair<int, int>; int main() { string s; int q; cin >> s >> q; int cnt = 0; int d = 0; rep(i, q) { int a; cin >> a; if (a == 1) cnt++; if (a == 2) { int b; char c; cin >> b >> c; if (cnt % 2 == 1) { d++; if (b == 1) b = 2; else if (b == 2) b = 1; } if (b == 1) s.insert(s.begin(), c); if (b == 2) s.push_back(c); } } if (cnt % 2 == 1) reverse(ALL(s)); cout << s << endl; }
replace
60
61
60
61
TLE
p02756
C++
Time Limit Exceeded
#define rep(i, n) for (ll i = 0; i < n; i++) #include "bits/stdc++.h" using namespace std; using ll = long long; using P = pair<int, int>; int main() { string s; int q, t, f; char c; cin >> s; cin >> q; bool is_rev = false; // 文字列が反転しているか否かを表す rep(i, q) { cin >> t; if (t == 1) { is_rev = !is_rev; } else { cin >> f >> c; // cout << f << c; if (((f == 1) && (is_rev == false)) || ((f == 2) && (is_rev == true))) { s = c + s; } else { s += c; } } } if (is_rev == true) { reverse(s.begin(), s.end()); } cout << s << endl; return 0; }
#define rep(i, n) for (ll i = 0; i < n; i++) #include "bits/stdc++.h" using namespace std; using ll = long long; using P = pair<int, int>; int main() { string s; int q, t, f; char c; cin >> s; cin >> q; bool is_rev = false; // 文字列が反転しているか否かを表す rep(i, q) { cin >> t; if (t == 1) { is_rev = !is_rev; } else { cin >> f >> c; // cout << f << c; if (((f == 1) && (is_rev == false)) || ((f == 2) && (is_rev == true))) { s.insert(s.begin(), c); } else { s += c; } } } if (is_rev == true) { reverse(s.begin(), s.end()); } cout << s << endl; return 0; }
replace
22
23
22
23
TLE
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define repr(i, n) for (int(i) = (n); (i) >= 0; (i)--) const ll MOD = 1e9 + 7; const int INF = (int)1e9 + 7; // vector<tuple<long,long>> items; // (value, weight) // auto item = items[i]; // long v = get<0>(item); // long w = get<1>(item); // long v, w; // for( int i = 0; i < N; i++ ){ // cin >> v >> w; // items.push_back( make_tuple(v, w) ); // item : (value, weight) // } int main() { deque<char> S; string s; cin >> s; rep(i, s.length()) { S.push_back(s[i]); } int Q; cin >> Q; int T; int F; char C; int revflag = -1; rep(i, Q) { cin >> T; if (T == 1) { revflag *= -1; } else if (T == 2) { cin >> F >> C; if (F == 1) { if (revflag == 1) S.push_back(C); else S.push_front(C); } else if (F == 2) { if (revflag == 1) S.push_front(C); else S.push_back(C); } } } if (revflag == 1) reverse(S.begin(), S.end()); rep(i, Q + s.length()) { cout << S[i]; } cout << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define repr(i, n) for (int(i) = (n); (i) >= 0; (i)--) const ll MOD = 1e9 + 7; const int INF = (int)1e9 + 7; // vector<tuple<long,long>> items; // (value, weight) // auto item = items[i]; // long v = get<0>(item); // long w = get<1>(item); // long v, w; // for( int i = 0; i < N; i++ ){ // cin >> v >> w; // items.push_back( make_tuple(v, w) ); // item : (value, weight) // } int main() { deque<char> S; string s; cin >> s; rep(i, s.length()) { S.push_back(s[i]); } int Q; cin >> Q; int T; int F; char C; int revflag = -1; rep(i, Q) { cin >> T; if (T == 1) { revflag *= -1; } else if (T == 2) { cin >> F >> C; if (F == 1) { if (revflag == 1) S.push_back(C); else S.push_front(C); } else if (F == 2) { if (revflag == 1) S.push_front(C); else S.push_back(C); } } } if (revflag == 1) reverse(S.begin(), S.end()); rep(i, S.size()) { cout << S[i]; } cout << endl; }
replace
51
52
51
52
0
p02756
Python
Runtime Error
from collections import deque s = input().strip() s = deque([*s]) q = int(input()) queries = [input().strip().split() for _ in range(q)] is_reversed = False for query in queries: t = query[0] if t == "1": is_reversed = not is_reversed else: f = query[1] c = query[2] if f == "1": if is_reversed: s.append(c) else: s.appendleft(c) else: if is_reversed: s.appendleft(c) else: s.append(c) str_list = list(s) if is_reversed: str_list = reversed(str_list) ans = "".join(str_list) print(ans)
from collections import deque s = input().strip() s = deque(list(s)) q = int(input()) queries = [input().strip().split() for _ in range(q)] is_reversed = False for query in queries: t = query[0] if t == "1": is_reversed = not is_reversed else: f = query[1] c = query[2] if f == "1": if is_reversed: s.append(c) else: s.appendleft(c) else: if is_reversed: s.appendleft(c) else: s.append(c) str_list = list(s) if is_reversed: str_list = reversed(str_list) ans = "".join(str_list) print(ans)
replace
3
4
3
4
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pi 3.14159265359 #define int long long #define pii pair<int, int> const int mod = 1e9 + 7; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif string s; cin >> s; int q; cin >> q; int st = 1, en = -1; int cnt = 0; while (q--) { int t; cin >> t; if (t == 1) { swap(st, en); cnt++; } else if (t == 2) { int x; cin >> x; string c; cin >> c; if (x == 1) { if (st == 1) { s.insert(0, c); } else s += c; } else { if (en == 1) { s.insert(0, c); } else s += c; } } } if (cnt & 1) reverse(s.begin(), s.end()); cout << s; }
#include <bits/stdc++.h> using namespace std; #define pi 3.14159265359 #define int long long #define pii pair<int, int> const int mod = 1e9 + 7; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; int q; cin >> q; int st = 1, en = -1; int cnt = 0; while (q--) { int t; cin >> t; if (t == 1) { swap(st, en); cnt++; } else if (t == 2) { int x; cin >> x; string c; cin >> c; if (x == 1) { if (st == 1) { s.insert(0, c); } else s += c; } else { if (en == 1) { s.insert(0, c); } else s += c; } } } if (cnt & 1) reverse(s.begin(), s.end()); cout << s; }
delete
14
19
14
14
TLE
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; string S, C; int Q, T, F; bool rev; int main() { cin >> S >> Q; for (int i = 0; i < Q; i++) { cin >> T; if (T == 1) rev ^= 1; else { cin >> F >> C; if (rev ^ (F == 1)) { S = C + S; } else { S += C; } } } if (rev) reverse(S.begin(), S.end()); cout << S << endl; }
#include <bits/stdc++.h> using namespace std; string S, C; int Q, T, F; bool rev; int main() { cin >> S >> Q; for (int i = 0; i < Q; i++) { cin >> T; if (T == 1) rev ^= 1; else { cin >> F >> C; if (rev ^ (F == 1)) { S.insert(0, C); } else { S += C; } } } if (rev) reverse(S.begin(), S.end()); cout << S << endl; }
replace
16
17
16
17
TLE
p02756
C++
Runtime Error
/*--------------------------"SABUJ-JANA"------"JADAVPUR UNIVERSITY"--------*/ /*-------------------------------@greenindia-----------------------------------*/ // // _____ _ _ _ // / ____| | | (_) | | // | (___ __ _| |__ _ _ _ | | __ _ _ __ __ _ // \___ \ / _` | '_ \| | | | | _ | |/ _` | '_ \ / _` | // ____) | (_| | |_) | |_| | | | |__| | (_| | | | | (_| | // |_____/ \__,_|_.__/ \__,_| | \____/ \__,_|_| |_|\__,_| // _/ | // |__/ /*---------------------- Magic. Do not touch.-----------------------------*/ /*------------------------------God is * Great/\---------------------------------*/ #include <bits/stdc++.h> using namespace std; #define crap \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) // cout<<fixed<<showpoint<<setprecision(12)<<ans<<endl; #define int long long int #define double long double #define PI acos(-1) void print1d(const vector<int> &vec) { for (auto val : vec) { cout << val << " "; } cout << endl; } void print2d(const vector<vector<int>> &vec) { for (auto row : vec) { for (auto val : row) { cout << val << " "; } cout << endl; } } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif crap; string str; cin >> str; deque<char> dq; for (auto x : str) dq.push_back(x); int q; cin >> q; int cnt = 0; while (q--) { int t; cin >> t; if (t == 2) { int f; char c; cin >> f >> c; // cout << f << c << endl; if (cnt % 2 == 0) { if (f == 1) dq.push_front(c); else dq.push_back(c); } else { if (f == 2) dq.push_front(c); else dq.push_back(c); } } else { cnt++; } } string ans = ""; for (auto x : dq) ans += x; if (cnt & 1) { reverse(ans.begin(), ans.end()); } cout << ans << endl; return 0; }
/*--------------------------"SABUJ-JANA"------"JADAVPUR UNIVERSITY"--------*/ /*-------------------------------@greenindia-----------------------------------*/ // // _____ _ _ _ // / ____| | | (_) | | // | (___ __ _| |__ _ _ _ | | __ _ _ __ __ _ // \___ \ / _` | '_ \| | | | | _ | |/ _` | '_ \ / _` | // ____) | (_| | |_) | |_| | | | |__| | (_| | | | | (_| | // |_____/ \__,_|_.__/ \__,_| | \____/ \__,_|_| |_|\__,_| // _/ | // |__/ /*---------------------- Magic. Do not touch.-----------------------------*/ /*------------------------------God is * Great/\---------------------------------*/ #include <bits/stdc++.h> using namespace std; #define crap \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) // cout<<fixed<<showpoint<<setprecision(12)<<ans<<endl; #define int long long int #define double long double #define PI acos(-1) void print1d(const vector<int> &vec) { for (auto val : vec) { cout << val << " "; } cout << endl; } void print2d(const vector<vector<int>> &vec) { for (auto row : vec) { for (auto val : row) { cout << val << " "; } cout << endl; } } signed main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // #endif // crap; string str; cin >> str; deque<char> dq; for (auto x : str) dq.push_back(x); int q; cin >> q; int cnt = 0; while (q--) { int t; cin >> t; if (t == 2) { int f; char c; cin >> f >> c; // cout << f << c << endl; if (cnt % 2 == 0) { if (f == 1) dq.push_front(c); else dq.push_back(c); } else { if (f == 2) dq.push_front(c); else dq.push_back(c); } } else { cnt++; } } string ans = ""; for (auto x : dq) ans += x; if (cnt & 1) { reverse(ans.begin(), ans.end()); } cout << ans << endl; return 0; }
replace
42
46
42
46
TLE
p02756
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main(void) { string S, beforeS = "", afterS = ""; int Q, input, count = 0; bool flag = false; char inputC; cin >> S; cin >> Q; for (int i = 0; i < Q; i++) { cin >> input; if (input == 2) { cin >> input; if (input == 1 && !flag) { cin >> inputC; beforeS += inputC; } else if (input == 1 && flag) { cin >> inputC; afterS += S; } else if (input == 2 && !flag) { cin >> inputC; afterS += inputC; } else { cin >> inputC; beforeS += inputC; } } else { count++; flag = !flag; } } reverse(beforeS.begin(), beforeS.end()); S = beforeS + S + afterS; if (count % 2 == 1) { reverse(S.begin(), S.end()); } cout << S << endl; return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main(void) { string S, beforeS = "", afterS = ""; int Q, input, count = 0; bool flag = false; char inputC; cin >> S; cin >> Q; for (int i = 0; i < Q; i++) { cin >> input; if (input == 2) { cin >> input; if (input == 1 && !flag) { cin >> inputC; beforeS += inputC; } else if (input == 1 && flag) { cin >> inputC; afterS += inputC; } else if (input == 2 && !flag) { cin >> inputC; afterS += inputC; } else { cin >> inputC; beforeS += inputC; } } else { count++; flag = !flag; } } reverse(beforeS.begin(), beforeS.end()); S = beforeS + S + afterS; if (count % 2 == 1) { reverse(S.begin(), S.end()); } cout << S << endl; return 0; }
replace
23
24
23
24
0
p02756
C++
Runtime Error
//----------------------------templates #pragma GCC optimize("Ofast") #ifdef ONLINE_JUDGE #pragma GCC target("avx512f") #else #pragma GCC target("avx2") #endif //---------------------------- #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int ll #define FOR(i, j, n) for (int i = (int)(j); i < (n); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPS(i, n) for (int i = 1; i <= (int)(n); i++) #define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REPNS(i, n) for (int i = (int)(n); i > 0; i--) #define I(n) scanf("%lld", &(n)) #define LL(n) scanf("%lld", &(n)) #define pb(n) push_back((n)) #define mp(i, j) make_pair((i), (j)) #define eb(i, j) emplace_back((i), (j)) #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; typedef vector<vvi> vvvi; const int mod = 1000000007; //-------------------------------------------- string s; deque<char> dc; bool revflag = false; int q, t, f; char c; signed main() { cin >> s; for (auto cs : s) dc.pb(cs); cin >> q; REP(i, q) { cin >> t; if (t == 1) { revflag ^= 1; } else { cin >> f >> c; if ((f == 1) ^ revflag) { dc.push_front(c); } else { dc.push_back(c); } } } if (revflag) { for (auto it = dc.rbegin(); it != dc.rend(); it++) cout << *it; } else { for (auto it = dc.begin(); it != dc.end(); it++) cout << *it; } cout << endl; }
//----------------------------templates #pragma GCC optimize("Ofast") #ifdef ONLINE_JUDGE #pragma GCC target("avx512f") #else #pragma GCC target("avx") #endif //---------------------------- #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int ll #define FOR(i, j, n) for (int i = (int)(j); i < (n); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPS(i, n) for (int i = 1; i <= (int)(n); i++) #define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REPNS(i, n) for (int i = (int)(n); i > 0; i--) #define I(n) scanf("%lld", &(n)) #define LL(n) scanf("%lld", &(n)) #define pb(n) push_back((n)) #define mp(i, j) make_pair((i), (j)) #define eb(i, j) emplace_back((i), (j)) #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; typedef vector<vvi> vvvi; const int mod = 1000000007; //-------------------------------------------- string s; deque<char> dc; bool revflag = false; int q, t, f; char c; signed main() { cin >> s; for (auto cs : s) dc.pb(cs); cin >> q; REP(i, q) { cin >> t; if (t == 1) { revflag ^= 1; } else { cin >> f >> c; if ((f == 1) ^ revflag) { dc.push_front(c); } else { dc.push_back(c); } } } if (revflag) { for (auto it = dc.rbegin(); it != dc.rend(); it++) cout << *it; } else { for (auto it = dc.begin(); it != dc.end(); it++) cout << *it; } cout << endl; }
replace
5
6
5
6
0
p02756
C++
Runtime Error
#include <algorithm> #include <iostream> #include <stdio.h> #include <string.h> #include <string> using namespace std; const int N = 3e5 + 10; const int INF = 1e5; char c[N]; string s; int main() { cin >> s; int l = INF, r = INF; for (int i = 0; i < s.size(); i++, r++) { c[r] = s[i]; } r--; int n; cin >> n; bool f = true; for (int i = 0; i < n; i++) { int t; cin >> t; if (t == 1) { f = !f; } else { int p; string str; cin >> p >> str; if (f) { if (p == 1) { l--; c[l] = str[0]; // s = c + s; } else { r++; c[r] = str[0]; // s = s + c; } } else { if (p == 1) { r++; c[r] = str[0]; // s = s + c; } else { l--; c[l] = str[0]; // s = c + s; } } } } if (f) { for (int i = l; i <= r; i++) cout << c[i]; cout << endl; } else { for (int i = r; i >= l; i--) cout << c[i]; cout << endl; } return 0; }
#include <algorithm> #include <iostream> #include <stdio.h> #include <string.h> #include <string> using namespace std; const int N = 5e5 + 10; const int INF = 3e5; char c[N]; string s; int main() { cin >> s; int l = INF, r = INF; for (int i = 0; i < s.size(); i++, r++) { c[r] = s[i]; } r--; int n; cin >> n; bool f = true; for (int i = 0; i < n; i++) { int t; cin >> t; if (t == 1) { f = !f; } else { int p; string str; cin >> p >> str; if (f) { if (p == 1) { l--; c[l] = str[0]; // s = c + s; } else { r++; c[r] = str[0]; // s = s + c; } } else { if (p == 1) { r++; c[r] = str[0]; // s = s + c; } else { l--; c[l] = str[0]; // s = c + s; } } } } if (f) { for (int i = l; i <= r; i++) cout << c[i]; cout << endl; } else { for (int i = r; i >= l; i--) cout << c[i]; cout << endl; } return 0; }
replace
7
9
7
9
0
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; list<char> lst; string s; int q; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s >> q; int n = s.size(); bool re = 0; for (int i = 0; i < n; i++) lst.push_back(s[i]); while (q--) { int t; cin >> t; if (t == 1) re = !re; else { int f; char c; cin >> f >> c; f--; f ^= re; if (f == 0) lst.push_front(c); else lst.push_back(c); } } while (lst.size()) if (re) { cout << lst.back(); lst.pop_back(); } else { cout << lst.front(); lst.pop_front(); } return 0; }
#include <bits/stdc++.h> using namespace std; list<char> lst; string s; int q; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s >> q; int n = s.size(); bool re = 0; for (int i = 0; i < n; i++) lst.push_back(s[i]); while (q--) { int t; cin >> t; if (t == 1) re = !re; else { int f; char c; cin >> f >> c; f--; f ^= re; if (f == 0) lst.push_front(c); else lst.push_back(c); } } if (re == 0) for (list<char>::iterator i = lst.begin(); i != lst.end(); i++) cout << *i; else for (list<char>::reverse_iterator i = lst.rbegin(); i != lst.rend(); i++) cout << *i; return 0; }
replace
31
39
31
37
TLE
p02756
C++
Runtime Error
#include <bits/stdc++.h> #include <cstdint> #include <vector> #define FOR(i, l, r) for (int i = (l); i < (r); ++i) #define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) RFOR(i, n - 1, 0) #define int long long using namespace std; const int MX = 1e6; const int inf = 1e9; vector<int> ra(MX, 0); vector<int> par(MX, 0); using P = pair<int, int>; using ll = long long; const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (ra[x] < ra[y]) { par[x] = y; } else { par[y] = x; if (ra[x] == ra[y]) { ra[x]++; } } } bool same(int x, int y) { return find(x) == find(y); } int a[MX]; int c[10][1000]; signed main() { string s; cin >> s; int q; cin >> q; bool isrev = false; deque<char> queue; rep(i, s.length()) queue.push_back(s[i]); rep(i, q) { int com; cin >> com; if (com == 1) isrev = !isrev; else { int pos; cin >> pos; char c; cin >> c; if (isrev) pos = (pos) % 2 + 1; if (pos == 1) queue.push_front(c); else queue.push_back(c); } } if (isrev) { rrep(i, s.length() + q) cout << queue[i] << flush; } else { rep(i, s.length() + q) cout << queue[i] << flush; } cout << "" << endl; return 0; }
#include <bits/stdc++.h> #include <cstdint> #include <vector> #define FOR(i, l, r) for (int i = (l); i < (r); ++i) #define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) RFOR(i, n - 1, 0) #define int long long using namespace std; const int MX = 1e6; const int inf = 1e9; vector<int> ra(MX, 0); vector<int> par(MX, 0); using P = pair<int, int>; using ll = long long; const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (ra[x] < ra[y]) { par[x] = y; } else { par[y] = x; if (ra[x] == ra[y]) { ra[x]++; } } } bool same(int x, int y) { return find(x) == find(y); } int a[MX]; int c[10][1000]; signed main() { string s; cin >> s; int q; cin >> q; bool isrev = false; deque<char> queue; rep(i, s.length()) queue.push_back(s[i]); rep(i, q) { int com; cin >> com; if (com == 1) isrev = !isrev; else { int pos; cin >> pos; char c; cin >> c; if (isrev) pos = (pos) % 2 + 1; if (pos == 1) queue.push_front(c); else queue.push_back(c); } } if (isrev) reverse(queue.begin(), queue.end()); rep(i, queue.size()) { cout << queue[i] << flush; } cout << "" << endl; return 0; }
replace
110
115
110
113
0
p02756
C++
Runtime Error
#include <algorithm> #include <bitset> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <string> #include <vector> #define i64 int64_t #define ff(ii, nn, mm) for (int ii = nn; ii < mm; ii++) #define ffrvs(ii, nn, mm) for (int ii = nn; ii >= mm; ii--) #define srt(vvv) sort(vvv.begin(), vvv.end()) #define srtrvs(vvv) reverse(vvv.begin(), vvv.end()) #define Graph vector<vector<int>> i64 inf = 1000000007; i64 giant = 4000000000000000037; using namespace std; i64 cup(i64 xxx, i64 yyy) { // 切り上げ return (xxx + yyy - 1) / yyy; } // メモリ置き場 deque<string> s; int q; // inputします void input() { cin >> s[0]; cin >> q; } // ライブラリ置き場 // main int main() { cin.tie(0); ios::sync_with_stdio(false); input(); int gk = 0; ff(i, 0, q) { int t; // scanf("%d", &t); cin >> t; if (t == 1) { gk++; } if (t == 2) { int f; // scanf("%d", &f); cin >> f; string cc; // scanf("%s", &cc, 1); cin >> cc; if ((f + gk) % 2 == 1) { s.push_front(cc); } if ((f + gk) % 2 == 0) { s.push_back(cc); } } } if (gk % 2 == 1) { ffrvs(i, s.size() - 1, 0) { ffrvs(j, s[i].size() - 1, 0) { cout << s[i][j]; } } return 0; } ff(i, 0, s.size()) { cout << s[i]; } return 0; }
#include <algorithm> #include <bitset> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <string> #include <vector> #define i64 int64_t #define ff(ii, nn, mm) for (int ii = nn; ii < mm; ii++) #define ffrvs(ii, nn, mm) for (int ii = nn; ii >= mm; ii--) #define srt(vvv) sort(vvv.begin(), vvv.end()) #define srtrvs(vvv) reverse(vvv.begin(), vvv.end()) #define Graph vector<vector<int>> i64 inf = 1000000007; i64 giant = 4000000000000000037; using namespace std; i64 cup(i64 xxx, i64 yyy) { // 切り上げ return (xxx + yyy - 1) / yyy; } // メモリ置き場 deque<string> s; int q; // inputします void input() { string ss; cin >> ss; s.push_back(ss); cin >> q; } // ライブラリ置き場 // main int main() { cin.tie(0); ios::sync_with_stdio(false); input(); int gk = 0; ff(i, 0, q) { int t; // scanf("%d", &t); cin >> t; if (t == 1) { gk++; } if (t == 2) { int f; // scanf("%d", &f); cin >> f; string cc; // scanf("%s", &cc, 1); cin >> cc; if ((f + gk) % 2 == 1) { s.push_front(cc); } if ((f + gk) % 2 == 0) { s.push_back(cc); } } } if (gk % 2 == 1) { ffrvs(i, s.size() - 1, 0) { ffrvs(j, s[i].size() - 1, 0) { cout << s[i][j]; } } return 0; } ff(i, 0, s.size()) { cout << s[i]; } return 0; }
replace
33
34
33
36
-11
p02756
C++
Time Limit Exceeded
#include <algorithm> #include <deque> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string s; cin >> s; deque<char> dq; for (int i = 0; i < s.size(); ++i) { dq.push_back(s[i]); } int q; cin >> q; bool rev = false; for (int i = 0; i < q; ++i) { int t; cin >> t; if (t == 1) { rev = !rev; } else { int f; char c; cin >> f; cin >> c; if (rev) f = 3 - f; if (f == 1) { dq.push_front(c); } else { dq.push_back(c); } string qq(dq.begin(), dq.end()); // cout << qq << endl; } } if (!rev) { cout << string(dq.begin(), dq.end()) << endl; } else { cout << string(dq.rbegin(), dq.rend()) << endl; } return 0; }
#include <algorithm> #include <deque> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string s; cin >> s; deque<char> dq; for (int i = 0; i < s.size(); ++i) { dq.push_back(s[i]); } int q; cin >> q; bool rev = false; for (int i = 0; i < q; ++i) { int t; cin >> t; if (t == 1) { rev = !rev; } else { int f; char c; cin >> f; cin >> c; if (rev) f = 3 - f; if (f == 1) { dq.push_front(c); } else { dq.push_back(c); } } } if (!rev) { cout << string(dq.begin(), dq.end()) << endl; } else { cout << string(dq.rbegin(), dq.rend()) << endl; } return 0; }
delete
36
38
36
36
TLE
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; using Pi = pair<int, int>; using Pl = pair<ll, ll>; int main() { cin.tie(0); ios::sync_with_stdio(false); string S; cin >> S; bool arr = false; int Q; cin >> Q; S.reserve(S.size() + Q + 10); REP(i, Q) { int T, F; char C; cin >> T; if (T == 1) arr = !arr; else { cin >> F >> C; if (F - 1 ^ arr == 0) S = C + S; else S.append(1, C); } } if (arr == true) reverse(S.begin(), S.end()); cout << S << "\n"; 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 Pi = pair<int, int>; using Pl = pair<ll, ll>; int main() { cin.tie(0); ios::sync_with_stdio(false); string S; cin >> S; bool arr = false; int Q; cin >> Q; S.reserve(S.size() + Q + 10); REP(i, Q) { int T, F; char C; cin >> T; if (T == 1) arr = !arr; else { cin >> F >> C; if (F - 1 ^ arr == 0) S.insert(S.begin(), C); else S.append(1, C); } } if (arr == true) reverse(S.begin(), S.end()); cout << S << "\n"; return 0; }
replace
25
26
25
26
TLE
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++) int main() { string s; ll q; cin >> s >> q; ll nrev = 0; ll fro = 50000, bck = 50001; char ans[100010]; fro -= s.size() / 2; bck += (s.size() + 1) / 2; rep(i, fro + 1, bck) ans[i] = s[i - fro - 1]; rep(i, 0, q) { int t; cin >> t; if (t == 1) { nrev++; } else { int f; char c; cin >> f >> c; if (nrev % 2 == 0) { if (f == 1) { ans[fro] = c; fro--; } else { ans[bck] = c; bck++; } } else { if (f == 1) { ans[bck] = c; bck++; } else { ans[fro] = c; fro--; } } } } if (nrev % 2 == 0) { rep(i, fro + 1, bck) { cout << ans[i]; } } else { for (ll i = bck - 1; i > fro; i--) { cout << ans[i]; } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++) int main() { string s; ll q; cin >> s >> q; ll nrev = 0; ll fro = 500000, bck = 500001; char ans[1000010]; fro -= s.size() / 2; bck += (s.size() + 1) / 2; rep(i, fro + 1, bck) ans[i] = s[i - fro - 1]; rep(i, 0, q) { int t; cin >> t; if (t == 1) { nrev++; } else { int f; char c; cin >> f >> c; if (nrev % 2 == 0) { if (f == 1) { ans[fro] = c; fro--; } else { ans[bck] = c; bck++; } } else { if (f == 1) { ans[bck] = c; bck++; } else { ans[fro] = c; fro--; } } } } if (nrev % 2 == 0) { rep(i, fro + 1, bck) { cout << ans[i]; } } else { for (ll i = bck - 1; i > fro; i--) { cout << ans[i]; } } cout << endl; return 0; }
replace
10
12
10
12
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> #include <deque> using namespace std; int n, q; string s; deque<char> str; int main() { // freopen("main.inp","r",stdin); // freopen("main.out","w",stdout); cin >> s; for (int i = 0; i < s.size(); ++i) str.push_back(s[i]); cin >> q; int dem = 0; while (q--) { int t; cin >> t; if (t == 1) { ++dem; } else { int r; char c; cin >> r >> c; if (dem % 2 == 0) { if (r == 1) str.push_front(c); else str.push_back(c); } else { if (r == 2) str.push_front(c); else str.push_back(c); } } } if (dem % 2 == 0) for (int i = 0; i < str.size(); ++i) cout << str[i]; else { for (int i = str.size() - 1; i >= 0; ++i) cout << str[i]; } return 0; }
#include <bits/stdc++.h> #include <deque> using namespace std; int n, q; string s; deque<char> str; int main() { // freopen("main.inp","r",stdin); // freopen("main.out","w",stdout); cin >> s; for (int i = 0; i < s.size(); ++i) str.push_back(s[i]); cin >> q; int dem = 0; while (q--) { int t; cin >> t; if (t == 1) { ++dem; } else { int r; char c; cin >> r >> c; if (dem % 2 == 0) { if (r == 1) str.push_front(c); else str.push_back(c); } else { if (r == 2) str.push_front(c); else str.push_back(c); } } } if (dem % 2 == 0) for (int i = 0; i < str.size(); ++i) cout << str[i]; else { reverse(str.begin(), str.end()); for (int i = 0; i < str.size(); ++i) cout << str[i]; } return 0; }
replace
40
41
40
42
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) (x).begin(), (x).end() using ll = long long; using namespace std; int main() { string S; int Q; cin >> S >> Q; bool rev = false; deque<char> deq; for (auto e : S) deq.push_back(e); rep(i, Q) { int q = 0; cin >> q; if (q == 1) { rev = !rev; continue; } int F; char C; cin >> F >> C; if (!rev) { if (F == 1) deq.push_front(C); else deq.push_back(C); } else { if (F == 1) deq.push_back(C); else deq.push_front(C); } } if (!rev) { for (auto e : deq) cout << e; } else { for (auto it = deq.rbegin(); it != deq.rend(); --it) { cout << *it; } } cout << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) (x).begin(), (x).end() using ll = long long; using namespace std; int main() { string S; int Q; cin >> S >> Q; bool rev = false; deque<char> deq; for (auto e : S) deq.push_back(e); rep(i, Q) { int q = 0; cin >> q; if (q == 1) { rev = !rev; continue; } int F; char C; cin >> F >> C; if (!rev) { if (F == 1) deq.push_front(C); else deq.push_back(C); } else { if (F == 1) deq.push_back(C); else deq.push_front(C); } } if (!rev) { for (auto e : deq) cout << e; } else { for (int i = deq.size() - 1; i >= 0; --i) { cout << deq[i]; } } cout << endl; }
replace
42
44
42
44
0
p02756
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdarg> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> using namespace std; #define INF 2e9 + 9 #define MOD 1000000007 #define ALL(v) v.begin(), v.end() #define Int int64_t #define pll pair<Int, Int> template <class T> using pql = priority_queue<T, vector<T>>; template <class T> using pq = priority_queue<T, vector<T>, greater<T>>; #define ll(n) \ Int n; \ cin >> n; #define LL(...) \ Int __VA_ARGS___; \ cin >> __VA_ARGS___; #define OUT(...) cout << __VA_ARGS___; #define RP(n, i, s) for (Int i = s; i < n; i++) #define REP(n, i) for (Int i = 0; i < n; i++) #define REPe(n, i) for (Int i = 1; i <= n; i++) #define RPe(n, i, s) for (Int i = s; i <= n; i++) #define upd_max(maxer, updater) \ if (maxer < updater) \ maxer = updater; void ansYes(bool flag) { if (flag) cout << "Yes" << endl; else cout << "No" << endl; } void ansYES(bool flag) { if (flag) cout << "YES" << endl; else cout << "NO" << endl; } struct node { Int to; Int length; }; vector<Int> divisor(Int n) { vector<Int> ret; for (Int i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } return (ret); } /* vector<vector<int>> to; vector<int> flag; vector<int> c; Int dfs(int from, int mae) { flag[from] = 1; Int mini = 0; for (auto next : to[from]) { if (next == mae) continue; if (flag[next] == 1) { return -1; } else { mini = min(mini, dfs(next, from)); } } return mini; }*/ int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1}; int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; struct datas { Int max; Int size; }; int main(void) { string s; int q; cin >> s >> q; int t, f; char c; bool mae = true; REP(q, i) { cin >> t; if (t == 1) mae = !mae; else { cin >> f >> c; if (mae) { if (f == 1) { s = c + s; } else { s.push_back(c); } } else { if (f == 1) { s.push_back(c); } else { s = c + s; } } } } if (!mae) reverse(s.begin(), s.end()); cout << s << endl; }
#include <algorithm> #include <cmath> #include <cstdarg> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> using namespace std; #define INF 2e9 + 9 #define MOD 1000000007 #define ALL(v) v.begin(), v.end() #define Int int64_t #define pll pair<Int, Int> template <class T> using pql = priority_queue<T, vector<T>>; template <class T> using pq = priority_queue<T, vector<T>, greater<T>>; #define ll(n) \ Int n; \ cin >> n; #define LL(...) \ Int __VA_ARGS___; \ cin >> __VA_ARGS___; #define OUT(...) cout << __VA_ARGS___; #define RP(n, i, s) for (Int i = s; i < n; i++) #define REP(n, i) for (Int i = 0; i < n; i++) #define REPe(n, i) for (Int i = 1; i <= n; i++) #define RPe(n, i, s) for (Int i = s; i <= n; i++) #define upd_max(maxer, updater) \ if (maxer < updater) \ maxer = updater; void ansYes(bool flag) { if (flag) cout << "Yes" << endl; else cout << "No" << endl; } void ansYES(bool flag) { if (flag) cout << "YES" << endl; else cout << "NO" << endl; } struct node { Int to; Int length; }; vector<Int> divisor(Int n) { vector<Int> ret; for (Int i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } return (ret); } /* vector<vector<int>> to; vector<int> flag; vector<int> c; Int dfs(int from, int mae) { flag[from] = 1; Int mini = 0; for (auto next : to[from]) { if (next == mae) continue; if (flag[next] == 1) { return -1; } else { mini = min(mini, dfs(next, from)); } } return mini; }*/ int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1}; int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; struct datas { Int max; Int size; }; int main(void) { string s; int q; cin >> s >> q; int t, f; char c; bool mae = true; REP(q, i) { cin >> t; if (t == 1) mae = !mae; else { cin >> f >> c; if (mae) { if (f == 1) { s.insert(s.begin(), c); } else { s.push_back(c); } } else { if (f == 1) { s.push_back(c); } else { s = c + s; } } } } if (!mae) reverse(s.begin(), s.end()); cout << s << endl; }
replace
107
108
107
108
TLE
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int i; string s; cin >> s; int n = s.length(); vector<char> characs; for (i = 0; i < n; i++) { characs.push_back(s[i]); } ll q; cin >> q; bool reversed = false; while (q--) { int c; cin >> c; if (c == 1) { if (reversed == true) reversed = false; else reversed = true; } else { int c1; char s1; cin >> c1; cin >> s1; if (c1 == 1) { if (reversed) characs.push_back(s1); else characs.insert(characs.begin(), s1); } else { if (reversed) characs.insert(characs.begin(), s1); else characs.push_back(s1); } } // cout<<reversed<<endl; } // cout<<s<<endl; if (reversed == true) { for (i = characs.size() - 1; i >= 0; i--) { cout << characs[i]; } } else { for (i = 0; i < characs.size(); i++) { cout << characs[i]; } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i; string s; cin >> s; int n = s.length(); vector<char> characs; for (i = 0; i < n; i++) { characs.push_back(s[i]); } ll q; cin >> q; bool reversed = false; while (q--) { int c; cin >> c; if (c == 1) { if (reversed == true) reversed = false; else reversed = true; } else { int c1; char s1; cin >> c1; cin >> s1; if (c1 == 1) { if (reversed) characs.push_back(s1); else characs.insert(characs.begin(), s1); } else { if (reversed) characs.insert(characs.begin(), s1); else characs.push_back(s1); } } // cout<<reversed<<endl; } // cout<<s<<endl; if (reversed == true) { for (i = characs.size() - 1; i >= 0; i--) { cout << characs[i]; } } else { for (i = 0; i < characs.size(); i++) { cout << characs[i]; } } cout << endl; return 0; }
delete
6
12
6
6
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(s, i, n) for (int i = (int)s; i < (int)(n); i++) int main() { string s; int Q; int len_s; int list_f[100001]; char list_c[100001]; int list_t[100001]; list<char> lst; cin >> s >> Q; len_s = s.length(); rep(i, len_s) { lst.push_back(s[i]); } bool muki = true; for (int i = 1; i <= Q; i++) { cin >> list_t[i]; if (list_t[i] == 2) cin >> list_f[i] >> list_c[i]; } for (int i = 1; i <= Q; i++) { if (list_t[i] == 1) { muki = !muki; } else if (list_t[i] == 2) { if (list_f[i] == 1) { if (muki) lst.push_front(list_c[i]); else { lst.push_back(list_c[i]); } } else { if (muki) lst.push_back(list_c[i]); else { lst.push_front(list_c[i]); } } } } if (muki) { for (auto itr = lst.begin(); itr != lst.end(); ++itr) { cout << *itr; } cout << endl; } else { for (auto itr = lst.rbegin(); itr != lst.rend(); ++itr) { cout << *itr; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(s, i, n) for (int i = (int)s; i < (int)(n); i++) int main() { string s; int Q; int len_s; int list_f[200001]; char list_c[200001]; int list_t[200001]; list<char> lst; cin >> s >> Q; len_s = s.length(); rep(i, len_s) { lst.push_back(s[i]); } bool muki = true; for (int i = 1; i <= Q; i++) { cin >> list_t[i]; if (list_t[i] == 2) cin >> list_f[i] >> list_c[i]; } for (int i = 1; i <= Q; i++) { if (list_t[i] == 1) { muki = !muki; } else if (list_t[i] == 2) { if (list_f[i] == 1) { if (muki) lst.push_front(list_c[i]); else { lst.push_back(list_c[i]); } } else { if (muki) lst.push_back(list_c[i]); else { lst.push_front(list_c[i]); } } } } if (muki) { for (auto itr = lst.begin(); itr != lst.end(); ++itr) { cout << *itr; } cout << endl; } else { for (auto itr = lst.rbegin(); itr != lst.rend(); ++itr) { cout << *itr; } cout << endl; } return 0; }
replace
9
12
9
12
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> // definitions #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define rep(i, l, r) for (long long i = (l); i < (long long)(r); i++) #define repb(i, r, l) for (long long i = (r); i > (long long)(l); i--) #define pb push_back #define pf push_front #define mod1 998244353 #define mod 1000000007 #define MAX_CHAR 256 using namespace std; typedef vector<long long> vl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<pair<long long, long long>> vp; typedef vector<tuple<long long, long long, long long>> vtup; typedef deque<long long> dql; typedef deque<char> dqc; typedef long double ld; typedef long long ll; typedef unsigned long long ull; // prevent collisions in unordered_map struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; typedef unordered_map<long long, long long, custom_hash> umap; // standard functions ll gcd(ll x, ll y) { if (x < y) return gcd(y, x); if (y == 0) return x; return gcd(y, x % y); } ll mystoi(string str) { stringstream ss(str); ll ans = 0; ss >> ans; return ans; } ll exp(ll x, ll ex) { ll ans = 1ll; while (ex > 0) { if (ex & 1ll) ans = (ans * x); ex >>= 1ll; x = (x * x); } return ans; } ll nCrModp(ll n, ll r, ll p) { ll C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (ll i = 1; i <= n; i++) { for (ll j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } bool isPrime(ll n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (ll i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } vl countFreq(ll arr[], ll n) { unordered_map<ll, ll> mp; vector<ll> v; for (ll i = 0; i < n; i++) mp[arr[i]]++; for (auto x : mp) v.pb(x.second); return v; } ll binarySearch(ll arr[], ll l, ll r, ll x) { while (l <= r) { ll m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } bool sort_cond(const pair<ll, ll> &a, const pair<ll, ll> &b) { return a.second > b.second; } const ll MAXSIEVE = (1e3); ll spf[MAXSIEVE]; void sieve() { spf[1] = 1; for (ll i = 2; i < MAXSIEVE; i++) spf[i] = i; for (ll i = 4; i < MAXSIEVE; i += 2) spf[i] = 2; for (ll i = 3; i * i < MAXSIEVE; i++) { if (spf[i] == i) { for (ll j = i * i; j < MAXSIEVE; j += i) if (spf[j] == j) spf[j] = i; } } } // code beigns in main int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t = 1; // cin >> t; while (t--) { string str; cin >> str; dqc start, end; ll q; cin >> q; ll rev = 0; ll count = 0; rep(i, 0, q) { ll type; cin >> type; if (type == 1) { count++; rev++; } else { ll ty; char ch; cin >> ty >> ch; if (rev % 2) { if (ty == 1) end.pb(ch); else start.pf(ch); } else { if (ty == 1) start.pf(ch); else end.pb(ch); } } } string ans = ""; rep(i, 0, start.size()) ans += string(1, start[i]); ans += str; rep(i, 0, end.size()) ans += string(1, end[i]); if (rev % 2 == 1) reverse(all(ans)); cout << ans << endl; } }
#include <bits/stdc++.h> // definitions #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define rep(i, l, r) for (long long i = (l); i < (long long)(r); i++) #define repb(i, r, l) for (long long i = (r); i > (long long)(l); i--) #define pb push_back #define pf push_front #define mod1 998244353 #define mod 1000000007 #define MAX_CHAR 256 using namespace std; typedef vector<long long> vl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<pair<long long, long long>> vp; typedef vector<tuple<long long, long long, long long>> vtup; typedef deque<long long> dql; typedef deque<char> dqc; typedef long double ld; typedef long long ll; typedef unsigned long long ull; // prevent collisions in unordered_map struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; typedef unordered_map<long long, long long, custom_hash> umap; // standard functions ll gcd(ll x, ll y) { if (x < y) return gcd(y, x); if (y == 0) return x; return gcd(y, x % y); } ll mystoi(string str) { stringstream ss(str); ll ans = 0; ss >> ans; return ans; } ll exp(ll x, ll ex) { ll ans = 1ll; while (ex > 0) { if (ex & 1ll) ans = (ans * x); ex >>= 1ll; x = (x * x); } return ans; } ll nCrModp(ll n, ll r, ll p) { ll C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (ll i = 1; i <= n; i++) { for (ll j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } bool isPrime(ll n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (ll i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } vl countFreq(ll arr[], ll n) { unordered_map<ll, ll> mp; vector<ll> v; for (ll i = 0; i < n; i++) mp[arr[i]]++; for (auto x : mp) v.pb(x.second); return v; } ll binarySearch(ll arr[], ll l, ll r, ll x) { while (l <= r) { ll m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } bool sort_cond(const pair<ll, ll> &a, const pair<ll, ll> &b) { return a.second > b.second; } const ll MAXSIEVE = (1e3); ll spf[MAXSIEVE]; void sieve() { spf[1] = 1; for (ll i = 2; i < MAXSIEVE; i++) spf[i] = i; for (ll i = 4; i < MAXSIEVE; i += 2) spf[i] = 2; for (ll i = 3; i * i < MAXSIEVE; i++) { if (spf[i] == i) { for (ll j = i * i; j < MAXSIEVE; j += i) if (spf[j] == j) spf[j] = i; } } } // code beigns in main int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t = 1; // cin >> t; while (t--) { string str; cin >> str; dqc start, end; ll q; cin >> q; ll rev = 0; ll count = 0; rep(i, 0, q) { ll type; cin >> type; if (type == 1) { count++; rev++; } else { ll ty; char ch; cin >> ty >> ch; if (rev % 2) { if (ty == 1) end.pb(ch); else start.pf(ch); } else { if (ty == 1) start.pf(ch); else end.pb(ch); } } } string ans = ""; rep(i, 0, start.size()) ans += string(1, start[i]); ans += str; rep(i, 0, end.size()) ans += string(1, end[i]); if (rev % 2 == 1) reverse(all(ans)); cout << ans << endl; } }
replace
152
156
152
156
0
p02756
Python
Runtime Error
from collections import deque S = list(input()) S = deque(S) Q = int(input()) c = 0 for _ in range(Q): q = list(input().split()) if int(q[0]) == 1: if c == 0: c = 1 else: c = 0 else: if c == 0: if int(q[1]) == 1: S.appendleft(q[2]) else: S.append[q[2]] else: if int(q[1]) == 2: S.appendleft(q[2]) else: S.append(q[2]) S = list(S) if c == 0: print("".join(S)) else: S = S[::-1] print("".join(S))
from collections import deque S = list(input()) S = deque(S) Q = int(input()) c = 0 for _ in range(Q): q = list(input().split()) if int(q[0]) == 1: if c == 0: c = 1 else: c = 0 else: if c == 0: if int(q[1]) == 1: S.appendleft(q[2]) else: S.append(q[2]) else: if int(q[1]) == 2: S.appendleft(q[2]) else: S.append(q[2]) S = list(S) if c == 0: print("".join(S)) else: S = S[::-1] print("".join(S))
replace
18
19
18
19
0
p02756
Python
Time Limit Exceeded
s = input() q = int(input()) counter = 0 for i in range(q): x = input().split() if str(x[0]) == "1": counter += 1 else: if counter % 2 != 0: s = s[::-1] counter = 0 if str(x[1]) == "1": s = str(x[2]) + s else: s = s + str(x[2]) if counter % 2 != 0: s = s[::-1] print(s)
s = input() q = int(input()) qs = [input().split() for i in range(q)] n = False left = right = "" for operation in qs: if operation[0] == "1": n = not n if operation[0] == "2": t, f, c = operation f = int(f) if n: f = 1 if f != 1 else 2 if f == 1: left = c + left elif f == 2: right += c s = left + s + right print(s[::-1] if n else s)
replace
2
23
2
19
TLE
p02756
Python
Runtime Error
from collections import deque S = deque(list(input())) q = int(input()) r = False for _ in range(q): t = input().split() if len(t) == 1: r = not r else: t[1] = int(t[1]) - 1 if (not t[1] and not r) or (t[1] and r): S.appendleft(t[2]) else: S.append(t[2]) if r: print("".join(S[::-1])) else: print("".join(S))
from collections import deque S = deque(list(input())) q = int(input()) r = False for _ in range(q): t = input().split() if len(t) == 1: r = not r else: t[1] = int(t[1]) - 1 if (not t[1] and not r) or (t[1] and r): S.appendleft(t[2]) else: S.append(t[2]) if r: S = list(S) print("".join(S[::-1])) else: print("".join(S))
insert
16
16
16
17
0
p02756
Python
Runtime Error
import sys from collections import deque input = sys.stdin.readline S = deque(input()[:-1]) reverse_flg = 0 Q = int(input()) qs = [input().split() for _ in range(Q)] for q in qs: if q[0] == "1": reverse_flg = reverse_flg ^ 1 else: flg = (int(q[1]) + reverse_flg) % 2 if flg == 1: S.appendleft(q[2]) else: S.append(q[2]) if reverse_flg: S = S[::-1] print("".join(S))
import sys from collections import deque input = sys.stdin.readline S = deque(input()[:-1]) reverse_flg = 0 Q = int(input()) qs = [input().split() for _ in range(Q)] for q in qs: if q[0] == "1": reverse_flg = reverse_flg ^ 1 else: flg = (int(q[1]) + reverse_flg) % 2 if flg == 1: S.appendleft(q[2]) else: S.append(q[2]) if reverse_flg: S.reverse() print("".join(S))
replace
20
21
20
21
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vii = vector<vi>; using in6 = int64_t; using ind = double; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { string s; cin >> s; int q; cin >> q; int size = s.size(); vector<char> data(50 * q * size); int left = 25 * q * size; int right = left + size - 1; rep(i, size) { data[left + i] = s[i]; } rep(i, q) { int a; cin >> a; if (a == 1) { int n, m; n = right; m = left; right = m; left = n; } else { size++; int b; char c; cin >> b >> c; if (b == 1) { if (left <= right) { data[left - 1] = c; left--; } else { data[left + 1] = c; left++; } } else { if (left <= right) { data[right + 1] = c; right++; } else { data[right - 1] = c; right--; } } } } if (left <= right) { rep(i, size) { cout << data[left + i]; } } else { rep(i, size) { cout << data[left - i]; } } cout << endl; }
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vii = vector<vi>; using in6 = int64_t; using ind = double; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { string s; cin >> s; int q; cin >> q; int size = s.size(); vector<char> data(50 * q + 50 * size); int left = 25 * q + 25 * size; int right = left + size - 1; rep(i, size) { data[left + i] = s[i]; } rep(i, q) { int a; cin >> a; if (a == 1) { int n, m; n = right; m = left; right = m; left = n; } else { size++; int b; char c; cin >> b >> c; if (b == 1) { if (left <= right) { data[left - 1] = c; left--; } else { data[left + 1] = c; left++; } } else { if (left <= right) { data[right + 1] = c; right++; } else { data[right - 1] = c; right--; } } } } if (left <= right) { rep(i, size) { cout << data[left + i]; } } else { rep(i, size) { cout << data[left - i]; } } cout << endl; }
replace
14
16
14
16
0
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int main() { deque<char> str; int Q, T, F, a, b; string S; char C; cin >> S >> Q; a = S.size(); rep(i, a) { str.push_back(S.at(i)); } bool rev = false; rep(i, Q) { cin >> T; if (T == 1) rev = !rev; else { cin >> F >> C; if (rev) { reverse(str.begin(), str.end()); rev = false; } if (F == 1) str.push_front(C); else str.push_back(C); } } if (rev) { reverse(str.begin(), str.end()); } a = str.size(); rep(i, a) { cout << str.at(i); } cout << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int main() { deque<char> str; int Q, T, F, a, b; string S; char C; cin >> S >> Q; a = S.size(); rep(i, a) { str.push_back(S.at(i)); } bool rev = false; rep(i, Q) { cin >> T; if (T == 1) rev = !rev; else { cin >> F >> C; if (rev) { F = 3 - F; } if (F == 1) str.push_front(C); else str.push_back(C); } } if (rev) { reverse(str.begin(), str.end()); } a = str.size(); rep(i, a) { cout << str.at(i); } cout << endl; }
replace
20
22
20
21
TLE
p02756
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <typeinfo> #include <utility> #include <vector> using namespace std; using Complex = complex<double>; using ll = long long; template <typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; const ll inf = 1145141919810364364LL; double PI = acos(-1.0); #define all(x) (x).begin(), (x).end() #define puts(x) cout << x << endl #define rep(i, m, n) for (ll i = m; i < n; ++i) #define pb push_back #define fore(i, a) for (auto &i : a) #define rrep(i, m, n) for (ll i = m; i >= n; --i) map<int, char> mp; int nxt[101010]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string S; cin >> S; int Q; cin >> Q; int tp = -1; int bk = -2; int cnt = 0; for (int i = 0; i < Q; i++) { int q; cin >> q; if (q == 1) { swap(tp, bk); } else { int f; char c; cin >> f >> c; mp[cnt] = c; if (f == 1) { nxt[cnt] = tp; tp = cnt; } else { nxt[cnt] = bk; bk = cnt; } cnt++; } } int fl = 0; string s1, s2; while (1) { if (tp < 0) { if (tp == -2) fl = 1; break; } s1.pb(mp[tp]); tp = nxt[tp]; } while (1) { if (bk < 0) break; s2.pb(mp[bk]); bk = nxt[bk]; } string ans; ans += s1; if (fl == 1) reverse(all(S)); ans += S; reverse(all(s2)); ans += s2; puts(ans); return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <typeinfo> #include <utility> #include <vector> using namespace std; using Complex = complex<double>; using ll = long long; template <typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; const ll inf = 1145141919810364364LL; double PI = acos(-1.0); #define all(x) (x).begin(), (x).end() #define puts(x) cout << x << endl #define rep(i, m, n) for (ll i = m; i < n; ++i) #define pb push_back #define fore(i, a) for (auto &i : a) #define rrep(i, m, n) for (ll i = m; i >= n; --i) map<int, char> mp; int nxt[202020]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string S; cin >> S; int Q; cin >> Q; int tp = -1; int bk = -2; int cnt = 0; for (int i = 0; i < Q; i++) { int q; cin >> q; if (q == 1) { swap(tp, bk); } else { int f; char c; cin >> f >> c; mp[cnt] = c; if (f == 1) { nxt[cnt] = tp; tp = cnt; } else { nxt[cnt] = bk; bk = cnt; } cnt++; } } int fl = 0; string s1, s2; while (1) { if (tp < 0) { if (tp == -2) fl = 1; break; } s1.pb(mp[tp]); tp = nxt[tp]; } while (1) { if (bk < 0) break; s2.pb(mp[bk]); bk = nxt[bk]; } string ans; ans += s1; if (fl == 1) reverse(all(S)); ans += S; reverse(all(s2)); ans += s2; puts(ans); return 0; }
replace
38
39
38
39
0
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define lli long long int #define uli unsigned long long int #define INF 999999999999999999 #define rep(i, m, n) for (lli i = m; i < n; i++) #define rrep(i, m, n) for (lli i = m - 1; i >= n; i--) #define pb(n) push_back(n) #define UE(N) N.erase(unique(N.begin(), N.end()), N.end()); #define Sort(n) sort(n.begin(), n.end()) #define Rev(n) reverse(n.begin(), n.end()) #define Out(S) cout << S << endl #define NeOut(S) cout << S #define HpOut(S) cout << setprecision(25) << S << endl #define Vec(K, L, N, S) vector<L> K(N, S) #define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S)) #define TV(K, L, N, M, R, S) \ vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S))) #define pint pair<lli, lli> #define paf(L, R) pair<L, R> #define mod 1000000007 #define MAX 510000 #define ALL(a) a.begin(), a.end() #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) int main() { lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0; string S, T; cin >> S >> N; rep(i, 0, N) { cin >> A; if (A == 1) sum++; else { cin >> B >> T; if (B == 1) { if (sum % 2 == 0) S = T + S; else S += T; } else { if (sum % 2 == 0) S += T; else S = T + S; } } } if (sum % 2 == 1) Rev(S); Out(S); }
#include <bits/stdc++.h> using namespace std; #define lli long long int #define uli unsigned long long int #define INF 999999999999999999 #define rep(i, m, n) for (lli i = m; i < n; i++) #define rrep(i, m, n) for (lli i = m - 1; i >= n; i--) #define pb(n) push_back(n) #define UE(N) N.erase(unique(N.begin(), N.end()), N.end()); #define Sort(n) sort(n.begin(), n.end()) #define Rev(n) reverse(n.begin(), n.end()) #define Out(S) cout << S << endl #define NeOut(S) cout << S #define HpOut(S) cout << setprecision(25) << S << endl #define Vec(K, L, N, S) vector<L> K(N, S) #define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S)) #define TV(K, L, N, M, R, S) \ vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S))) #define pint pair<lli, lli> #define paf(L, R) pair<L, R> #define mod 1000000007 #define MAX 510000 #define ALL(a) a.begin(), a.end() #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) int main() { lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0; string S, T; cin >> S >> N; rep(i, 0, N) { cin >> A; if (A == 1) sum++; else { cin >> B >> T; if ((sum + B) % 2 == 1) S.insert(0, T); else S += T; } } if (sum % 2 == 1) Rev(S); Out(S); }
replace
35
46
35
39
TLE
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { string S; int Q; cin >> S; cin >> Q; deque<char> S_deque; for (int i = 0; i < S.size(); i++) { S_deque.push_back(S[i]); } int t, f, t_sum; char c; t_sum = 0; for (int i = 0; i < Q; i++) { cin >> t; if (t == 1) { t_sum++; t_sum %= 2; } else { cin >> f >> c; if ((f + t_sum) % 2 == 1) { S_deque.push_front(c); } else { S_deque.push_back(c); } } } if (t_sum == 1) { for (int i = 0; i < S_deque.size(); i++) { cout << S_deque[S.size() - i + 1]; } } else { for (int i = 0; i < S_deque.size(); i++) { cout << S_deque[i]; } } cout << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { string S; int Q; cin >> S; cin >> Q; deque<char> S_deque; for (int i = 0; i < S.size(); i++) { S_deque.push_back(S[i]); } int t, f, t_sum; char c; t_sum = 0; for (int i = 0; i < Q; i++) { cin >> t; if (t == 1) { t_sum++; t_sum %= 2; } else { cin >> f >> c; if ((f + t_sum) % 2 == 1) { S_deque.push_front(c); } else { S_deque.push_back(c); } } } if (t_sum == 1) { for (int i = 0; i < S_deque.size(); i++) { cout << S_deque[S_deque.size() - i - 1]; } } else { for (int i = 0; i < S_deque.size(); i++) { cout << S_deque[i]; } } cout << endl; }
replace
33
34
33
34
0
p02756
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cmath> #include <functional> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> using namespace std; typedef long long ll; #define rep(i, a, b) for (int i = a; i < b; i++) int main() { char str[50001]; bool hanten = false; vector<char> ft, bk; int Q, T, F; char c[2]; scanf("%s", str); scanf("%d", &Q); rep(i, 0, Q) { scanf("%d", &T); if (T == 1) hanten = !hanten; else { scanf("%d", &F); scanf("%s", c); getchar(); (((F - 1) ^ hanten) ? bk : ft).push_back(c[0]); } } if (hanten) { int len = strlen(str); vector<char> str2(len + 1); str2[len] = 0; rep(i, 0, len) { str2[i] = str[len - i - 1]; } reverse(bk.begin(), bk.end()); bk.push_back(0); ft.push_back(0); printf("%s%s%s", bk.data(), str2.data(), ft.data()); } else { reverse(ft.begin(), ft.end()); ft.push_back(0); bk.push_back(0); printf("%s%s%s", ft.data(), str, bk.data()); } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cmath> #include <functional> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> using namespace std; typedef long long ll; #define rep(i, a, b) for (int i = a; i < b; i++) int main() { char str[100001]; bool hanten = false; vector<char> ft, bk; int Q, T, F; char c[2]; scanf("%s", str); scanf("%d", &Q); rep(i, 0, Q) { scanf("%d", &T); if (T == 1) hanten = !hanten; else { scanf("%d", &F); scanf("%s", c); getchar(); (((F - 1) ^ hanten) ? bk : ft).push_back(c[0]); } } if (hanten) { int len = strlen(str); vector<char> str2(len + 1); str2[len] = 0; rep(i, 0, len) { str2[i] = str[len - i - 1]; } reverse(bk.begin(), bk.end()); bk.push_back(0); ft.push_back(0); printf("%s%s%s", bk.data(), str2.data(), ft.data()); } else { reverse(ft.begin(), ft.end()); ft.push_back(0); bk.push_back(0); printf("%s%s%s", ft.data(), str, bk.data()); } return 0; }
replace
12
13
12
13
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int main() { string S; int Q; cin >> S >> Q; bool sei = true; vector<char> start(2 * 10 * 5); vector<char> end(2 * 10 * 5); int sIndex = 0; int eIndex = 0; rep(i, Q) { int r, f; char c; cin >> r; if (r == 2) { cin >> f >> c; } if (r == 1) { sei = !sei; } else { if (f == 1) { if (sei) { start[sIndex] = c; sIndex++; } else { end[eIndex] = c; eIndex++; } } else { if (sei) { end[eIndex] = c; eIndex++; } else { start[sIndex] = c; sIndex++; } } } } if (sei) { rep(i, start.size()) { if (!start[start.size() - 1 - i]) continue; cout << start[start.size() - 1 - i]; } cout << S; rep(i, end.size()) { if (!end[i]) break; cout << end[i]; } } else { rep(i, end.size()) { if (!end[end.size() - 1 - i]) continue; cout << end[end.size() - 1 - i]; } reverse(S.begin(), S.end()); cout << S; rep(i, start.size()) { if (!start[i]) break; cout << start[i]; } } cout << 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>; int main() { string S; int Q; cin >> S >> Q; bool sei = true; vector<char> start(1000000); vector<char> end(1000000); int sIndex = 0; int eIndex = 0; rep(i, Q) { int r, f; char c; cin >> r; if (r == 2) { cin >> f >> c; } if (r == 1) { sei = !sei; } else { if (f == 1) { if (sei) { start[sIndex] = c; sIndex++; } else { end[eIndex] = c; eIndex++; } } else { if (sei) { end[eIndex] = c; eIndex++; } else { start[sIndex] = c; sIndex++; } } } } if (sei) { rep(i, start.size()) { if (!start[start.size() - 1 - i]) continue; cout << start[start.size() - 1 - i]; } cout << S; rep(i, end.size()) { if (!end[i]) break; cout << end[i]; } } else { rep(i, end.size()) { if (!end[end.size() - 1 - i]) continue; cout << end[end.size() - 1 - i]; } reverse(S.begin(), S.end()); cout << S; rep(i, start.size()) { if (!start[i]) break; cout << start[i]; } } cout << endl; return 0; }
replace
12
14
12
14
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "dump.hpp" #else #define dump(...) #define dumpv(...) #endif #define rep(i, n) for (int i = 0; i < (n); i++) #define mins(x, y) (x = min(x, y)) #define maxs(x, y) (x = max(x, y)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; using P = pair<int, int>; const int MOD = 1e9 + 7; const int INF = 1001001001; const ll LINF = 1001002003004005006ll; const int N = 100005; void solve() { string s; cin >> s; vector<char> v(N * 2); rep(i, s.size()) v[N + i] = s[i]; int q; cin >> q; int l = N; int r = N + s.size() - 1; // for (int i = l; i <= r; i++) // cout << v[i] << endl; int mode = 1; rep(i, q) { int t; cin >> t; if (t == 1) { mode = mode == 1 ? 2 : 1; } else { int f; char c; cin >> f >> c; if (f != mode) { v[++r] = c; } else { v[--l] = c; } } // dump(l, r, mode); // for (int i = l; i <= r; i++) // cout << v[i] << endl; } string ans; if (mode == 1) { for (int i = l; i <= r; i++) ans += v[i]; } else { for (int i = r; l <= i; i--) ans += v[i]; } cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); // freopen("temp.1", "r", stdin); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "dump.hpp" #else #define dump(...) #define dumpv(...) #endif #define rep(i, n) for (int i = 0; i < (n); i++) #define mins(x, y) (x = min(x, y)) #define maxs(x, y) (x = max(x, y)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; using P = pair<int, int>; const int MOD = 1e9 + 7; const int INF = 1001001001; const ll LINF = 1001002003004005006ll; const int N = 200005; void solve() { string s; cin >> s; vector<char> v(N * 2); rep(i, s.size()) v[N + i] = s[i]; int q; cin >> q; int l = N; int r = N + s.size() - 1; // for (int i = l; i <= r; i++) // cout << v[i] << endl; int mode = 1; rep(i, q) { int t; cin >> t; if (t == 1) { mode = mode == 1 ? 2 : 1; } else { int f; char c; cin >> f >> c; if (f != mode) { v[++r] = c; } else { v[--l] = c; } } // dump(l, r, mode); // for (int i = l; i <= r; i++) // cout << v[i] << endl; } string ans; if (mode == 1) { for (int i = l; i <= r; i++) ans += v[i]; } else { for (int i = r; l <= i; i--) ans += v[i]; } cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); // freopen("temp.1", "r", stdin); solve(); return 0; }
replace
23
24
23
24
0
p02756
C++
Runtime Error
#include <chrono> #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <map> #include <ostream> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define int long long #define pb push_back #define all(a) (a).begin(), (a).end() #define pii pair<int, int> #define ld long double ostream &operator<<(ostream &a, const vector<int> &b) { for (auto k : b) cout << k << " "; return a; } #ifdef LOCAL #define dbg(x) cout << #x << " : " << (x) << "\n"; const int INF = 1e18; // const int mod = 2600000069; // const int p = 10; // const ld PI = 3.1415926535; #else #define dbg(x) const int INF = 1e18; // const int mod = 2600000069; // const int p = 179; // const ld PI = 3.1415926535; #endif // #pragma GCC optimize("Ofast,no-stack-protector") // #pragma GCC target("sse,sse2,sse3,sse3,sse4") // #pragma GCC optimize("unroll-loops") // #pragma GCC optimize("fast-math") // #pragma GCC target("avx2") // #pragma GCC optimize("section-anchors") // #pragma GCC optimize("profile-values,profile-reorder-functions,tracer") // #pragma GCC optimize("vpt") // #pragma GCC optimize("rename-registers") // #pragma GCC optimize("move-loop-invariants") // #pragma GCC optimize("unswitch-loops") // #pragma GCC optimize("function-sections") // #pragma GCC optimize("data-sections") signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL #else // freopen("capitals.in", "r", stdin); // freopen("capitals.out", "w", stdout); #endif string s; cin >> s; int n = s.size(); int q; cin >> q; int t, f; char c; bool rev = 0; vector<pair<bool, char>> a; for (int i = 0; i < q; i++) { cin >> t; if (t == 1) rev ^= 1; else { cin >> f >> c; if (f == 1) { if (rev) a.pb({1, c}); else a.pb({0, c}); } else { if (rev) a.pb({0, c}); else a.pb({1, c}); } } } if (rev) { reverse(all(s)); for (int i = 0; i < q; i++) a[i].first ^= 1; } for (int i = q - 1; i >= 0; i--) if (!a[i].first) cout << a[i].second; // cout << "\n"; cout << s; // cout << "\n"; for (int i = 0; i < q; i++) if (a[i].first) cout << a[i].second; } /* */
#include <chrono> #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <map> #include <ostream> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define int long long #define pb push_back #define all(a) (a).begin(), (a).end() #define pii pair<int, int> #define ld long double ostream &operator<<(ostream &a, const vector<int> &b) { for (auto k : b) cout << k << " "; return a; } #ifdef LOCAL #define dbg(x) cout << #x << " : " << (x) << "\n"; const int INF = 1e18; // const int mod = 2600000069; // const int p = 10; // const ld PI = 3.1415926535; #else #define dbg(x) const int INF = 1e18; // const int mod = 2600000069; // const int p = 179; // const ld PI = 3.1415926535; #endif // #pragma GCC optimize("Ofast,no-stack-protector") // #pragma GCC target("sse,sse2,sse3,sse3,sse4") // #pragma GCC optimize("unroll-loops") // #pragma GCC optimize("fast-math") // #pragma GCC target("avx2") // #pragma GCC optimize("section-anchors") // #pragma GCC optimize("profile-values,profile-reorder-functions,tracer") // #pragma GCC optimize("vpt") // #pragma GCC optimize("rename-registers") // #pragma GCC optimize("move-loop-invariants") // #pragma GCC optimize("unswitch-loops") // #pragma GCC optimize("function-sections") // #pragma GCC optimize("data-sections") signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL #else // freopen("capitals.in", "r", stdin); // freopen("capitals.out", "w", stdout); #endif string s; cin >> s; int n = s.size(); int q; cin >> q; int t, f; char c; bool rev = 0; vector<pair<bool, char>> a; for (int i = 0; i < q; i++) { cin >> t; if (t == 1) rev ^= 1; else { cin >> f >> c; if (f == 1) { if (rev) a.pb({1, c}); else a.pb({0, c}); } else { if (rev) a.pb({0, c}); else a.pb({1, c}); } } } q = a.size(); if (rev) { reverse(all(s)); for (int i = 0; i < q; i++) a[i].first ^= 1; } for (int i = q - 1; i >= 0; i--) if (!a[i].first) cout << a[i].second; // cout << "\n"; cout << s; // cout << "\n"; for (int i = 0; i < q; i++) if (a[i].first) cout << a[i].second; } /* */
insert
102
102
102
103
0
p02756
C++
Time Limit Exceeded
// https://atcoder.jp/contests/abc158/tasks/abc158_d #include <bits/stdc++.h> #if LOCAL #include "dump.hpp" #else #define dump(...) #endif using namespace std; using ll = long long; #define FOR(i, a, b) for (ll i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { string S; cin >> S; deque<char> lst; for (auto &c : S) { lst.push_back(c); } dump(lst); ll Q; cin >> Q; REP(i, Q) { ll t; cin >> t; if (t == 1) { // swap(lst.back(),*lst.begin()); std::reverse(lst.begin(), lst.end()); } else if (t == 2) { ll t2; cin >> t2; char c; cin >> c; if (t2 == 1) { lst.push_front(c); } else { lst.push_back(c); } } } for (auto &c : lst) { cout << c; } cout << endl; return 0; }
// https://atcoder.jp/contests/abc158/tasks/abc158_d #include <bits/stdc++.h> #if LOCAL #include "dump.hpp" #else #define dump(...) #endif using namespace std; using ll = long long; #define FOR(i, a, b) for (ll i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { #if LOCAL & 01 std::ifstream in("./test/sample-1.in"); // std::ifstream in("./input.txt"); std::cin.rdbuf(in.rdbuf()); #else cin.tie(0); ios::sync_with_stdio(false); #endif string S; cin >> S; ll Q; cin >> Q; struct info { ll type = 0; ll fb = 0; char c = 0; }; vector<info> ql(Q); for (auto &&q : ql) { cin >> q.type; if (q.type == 2) { cin >> q.fb >> q.c; } } bool sw_flag = false; for (ll i = Q - 1; i >= 0; --i) { auto &&q = ql.at(i); if (q.type == 1) { sw_flag = !sw_flag; // 反転 } else { if (sw_flag) { if (q.fb == 1) { q.fb = 2; } else { q.fb = 1; } } } } if (sw_flag) reverse(S.begin(), S.end()); deque<char> lst; for (auto &c : S) { lst.push_back(c); } dump(lst); REP(i, Q) { auto &q = ql.at(i); if (q.type == 1) { } else { ll t2 = q.fb; char c = q.c; if (t2 == 1) { lst.push_front(c); } else { lst.push_back(c); } } } for (auto &c : lst) { cout << c; } cout << endl; return 0; } int main_() { string S; cin >> S; deque<char> lst; for (auto &c : S) { lst.push_back(c); } dump(lst); ll Q; cin >> Q; REP(i, Q) { ll t; cin >> t; if (t == 1) { // swap(lst.back(),*lst.begin()); std::reverse(lst.begin(), lst.end()); } else if (t == 2) { ll t2; cin >> t2; char c; cin >> c; if (t2 == 1) { lst.push_front(c); } else { lst.push_back(c); } } } for (auto &c : lst) { cout << c; } cout << endl; return 0; }
insert
26
26
26
97
TLE
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; // since repeated reversing the string is time consuming, instead we save the // state of the string 1 or 2 short state = 1; int q = 0; cin >> q; while (q--) { int t, f; char c; cin >> t; if (t == 1) { state = (state == 1) ? 0 : 1; } else if (t == 2) { cin >> f >> c; if (state == 1) f == 1 ? s = c + s : s = s + c; else f == 1 ? s = s + c : s = c + s; } } if (state == 0) reverse(s.begin(), s.end()); cout << s; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; // since repeated reversing the string is time consuming, instead we save the // state of the string 1 or 2 short state = 1; int q = 0; cin >> q; while (q--) { int t, f; char c; cin >> t; if (t == 1) { state = (state == 1) ? 0 : 1; } else if (t == 2) { cin >> f >> c; if (state == 1) { if (f == 1) s.insert(s.begin(), c); else s.insert(s.end(), c); } else { if (f == 1) s.insert(s.end(), c); else s.insert(s.begin(), c); } } } if (state == 0) reverse(s.begin(), s.end()); cout << s; return 0; }
replace
26
30
26
37
TLE
p02756
C++
Runtime Error
//------------------------------------------------------------------------------MANAS //UNIYAL--------------------------------------------------------- NEVER GIVE UP #include <bits/stdc++.h> using namespace std; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define ll long long #define pb push_back #define fi first #define se second #define pii pair<ll, ll> #define INF 0x3f3f3f3f #define mod 100000007 #define testcases() \ ll t; \ cin >> t; \ while (t--) #define debug1(x) cout << #x << " " << x << "\n"; #define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << "\n"; #define debug3(x, y, z) \ cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z \ << "\n"; #define rep(i, start, end) for (ll i = start; i < end; i++) //---------------------------------------------------------------------------------------------------------------------------------------------------- int main() { fast; string str; cin >> str; cin.ignore(); int q; cin >> q; int x, y, start, end; vector<char> v(300000); start = 100005; end = 100004; for (int i = 0; i < (int)str.size(); i++) { v[++end] = str[i]; } char ch; while (q--) { cin >> x; if (x == 1) { int c = start; start = end; end = c; } else { cin >> y; cin >> ch; if (y == 1) { if (start <= end) { --start; } else { ++start; } v[start] = ch; } else { if (start <= end) { ++end; } else { --end; } v[end] = ch; } } } if (start <= end) { for (int i = start; i <= end; i++) { cout << v[i]; } } else { for (int i = start; i >= end; i--) { cout << v[i]; } } return 0; }
//------------------------------------------------------------------------------MANAS //UNIYAL--------------------------------------------------------- NEVER GIVE UP #include <bits/stdc++.h> using namespace std; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define ll long long #define pb push_back #define fi first #define se second #define pii pair<ll, ll> #define INF 0x3f3f3f3f #define mod 100000007 #define testcases() \ ll t; \ cin >> t; \ while (t--) #define debug1(x) cout << #x << " " << x << "\n"; #define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << "\n"; #define debug3(x, y, z) \ cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z \ << "\n"; #define rep(i, start, end) for (ll i = start; i < end; i++) //---------------------------------------------------------------------------------------------------------------------------------------------------- int main() { fast; string str; cin >> str; cin.ignore(); int q; cin >> q; int x, y, start, end; vector<char> v(600005); start = 200005; end = 200004; for (int i = 0; i < (int)str.size(); i++) { v[++end] = str[i]; } char ch; while (q--) { cin >> x; if (x == 1) { int c = start; start = end; end = c; } else { cin >> y; cin >> ch; if (y == 1) { if (start <= end) { --start; } else { ++start; } v[start] = ch; } else { if (start <= end) { ++end; } else { --end; } v[end] = ch; } } } if (start <= end) { for (int i = start; i <= end; i++) { cout << v[i]; } } else { for (int i = start; i >= end; i--) { cout << v[i]; } } return 0; }
replace
36
39
36
39
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<ll> vll; typedef vector<vector<int>> vvi; typedef vector<vector<string>> vvs; typedef vector<vector<char>> vvc; typedef vector<vector<bool>> vvb; typedef pair<int, int> P; #define vrep(v, n) \ for (int i = 0; i < n; i++) { \ cin >> v.at(i); \ } // 配列vにn個の要素を入力する #define rep(i, l, n) for (int i = l; i < (int)(n); i++) #define repn(i, l, n) for (int i = l; i <= (int)(n); i++) #define pb push_back const int mod = 1000000007; const int inf = 1e9; bool even(int n) { if (n % 2 == 0) return true; else return false; } string rev(string s) { int n = s.size(); string rev_s; rep(i, 0, n) { rev_s += s[n - 1 - i]; } return rev_s; } int main() { string s; deque<char> deq; cin >> s; rep(i, 0, s.length()) deq.push_back(s[i]); int cnt = 0; int n; cin >> n; rep(i, 0, n) { int q; cin >> q; if (q == 1) { cnt++; } if (q == 2) { int t; char c; cin >> t >> c; if (t == 1 && even(cnt)) deq.push_front(c); else if (t == 1 && !even(cnt)) deq.push_back(c); else if (t == 2 && even(cnt)) deq.push_back(c); else if (t == 2 && !even(cnt)) deq.push_front(c); } } if (even(cnt)) { rep(i, 0, deq.size()) { cout << deq.at(i); } } else { for (int i = deq.size(); i >= 0; i--) { cout << deq.at(i); } } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<ll> vll; typedef vector<vector<int>> vvi; typedef vector<vector<string>> vvs; typedef vector<vector<char>> vvc; typedef vector<vector<bool>> vvb; typedef pair<int, int> P; #define vrep(v, n) \ for (int i = 0; i < n; i++) { \ cin >> v.at(i); \ } // 配列vにn個の要素を入力する #define rep(i, l, n) for (int i = l; i < (int)(n); i++) #define repn(i, l, n) for (int i = l; i <= (int)(n); i++) #define pb push_back const int mod = 1000000007; const int inf = 1e9; bool even(int n) { if (n % 2 == 0) return true; else return false; } string rev(string s) { int n = s.size(); string rev_s; rep(i, 0, n) { rev_s += s[n - 1 - i]; } return rev_s; } int main() { string s; deque<char> deq; cin >> s; rep(i, 0, s.length()) deq.push_back(s[i]); int cnt = 0; int n; cin >> n; rep(i, 0, n) { int q; cin >> q; if (q == 1) { cnt++; } if (q == 2) { int t; char c; cin >> t >> c; if (t == 1 && even(cnt)) deq.push_front(c); else if (t == 1 && !even(cnt)) deq.push_back(c); else if (t == 2 && even(cnt)) deq.push_back(c); else if (t == 2 && !even(cnt)) deq.push_front(c); } } if (even(cnt)) { rep(i, 0, deq.size()) { cout << deq.at(i); } } else { for (int i = deq.size() - 1; i >= 0; i--) { cout << deq.at(i); } } }
replace
69
70
69
70
0
p02756
C++
Runtime Error
#include <algorithm> #include <iostream> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> using namespace std; #define MAX(x, y) (x > y ? x : y) #define MIN(x, y) (x < y ? x : y) #define ABS(x) (x > 0 ? x : -(x)) #define INF 1000000000 #define MOD 1000000007 void swap(int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; } void swap2(double *a, double *b) { double tmp; tmp = *a; *a = *b; *b = tmp; } int gcd(int a, int b) // 最大公約数 { if (a < b) swap(&a, &b); if (a % b == 0) return b; else return gcd(b, a % b); } int lcm(int a, int b) { return a * b / gcd(a, b); } long power(long x, int n) { // x^n 計算量O(logn) long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans = ans * x; } x = x * x; // 一周する度にx, x^2, x^4, x^8となる n >>= 1; // 桁をずらす n = n >> 1 } return ans; } int choose(int n, int k) { return k == 0 ? 1 : (long long)choose(n - 1, k - 1) * n % MOD * power(k, MOD - 2) % MOD; } void bubble_sort(int a[], int n) { int i, j, tmp; for (i = 0; i < n; i++) { for (int k = 0; k < n; k++) printf("%d ", a[k]); for (j = n - 1; j > i; j--) { if (a[j - 1] > a[j]) { tmp = a[j]; a[j] = a[j - 1]; a[j - 1] = tmp; } } printf("\n"); } } void selection_sort(int a[], int n) { int i, j, tmp, min, key; for (i = 0; i < n - 1; i++) { min = i; key = a[i]; for (int k = 0; k < n; k++) printf("%d ", a[k]); for (j = i + 1; j < n; j++) { if (a[j] < a[min]) { min = j; key = a[j]; } } tmp = a[i]; a[i] = a[min]; a[min] = tmp; printf("\n"); } for (int k = 0; k < n; k++) printf("%d ", a[k]); } void insertion_sort(int a[], int n) { int i, j, tmp; for (i = 1; i < n; i++) { j = i; while (j >= 1 && a[j - 1] > a[j]) { tmp = a[j]; a[j] = a[j - 1]; a[j - 1] = tmp; j--; } } } void shell_sort(int a[], int n) { int h, i, j; for (h = 1; h < n / 9; h = h * 3 + 1) ; for (; h > 0; h /= 3) { for (int k = 0; k < n; k++) printf("%d ", a[k]); for (i = h; i < n; i++) { j = i; while (j >= h && a[j - h] > a[j]) { swap(&a[j - h], &a[j]); j -= h; } } printf("\n"); } for (int k = 0; k < n; k++) printf("%d ", a[k]); } int partition(int a[], int l, int r) { int i, j, pivot; i = l - 1; j = r; pivot = a[r]; for (;;) { while (a[++i] < pivot) ; while (i < j-- && pivot < a[j]) ; if (i >= j) break; swap(&a[i], &a[j]); } swap(&a[i], &a[r]); return i; } void quick_sort1(int a[], int l, int r) { int v; if (l >= r) return; v = partition(a, l, r); quick_sort1(a, l, v - 1); quick_sort1(a, v + 1, r); } void quick_sort(int a[], int n) { quick_sort1(a, 0, n - 1); } int keta(int a, int n) { if (n == 1) return a % 10; for (int i = 0; i < n - 1; i++) a /= 10; return a % 10; } int main() { char S[100000]; char L[100000], R[100000]; char t, temp; bool flag = true; int Q; int T, F; int cnt1 = 0, cnt2 = 0; for (int i = 0; i < 100000; i++) { S[i] = L[i] = R[i] = '\0'; } scanf("%s%d", S, &Q); for (int i = 0; i < Q; i++) { scanf("%d", &T); if (T == 1) { flag = !flag; } else if (T == 2) { scanf("%d%c%c", &F, &temp, &t); if (F == 1) { if (flag) R[cnt1++] = t; else L[cnt2++] = t; } else if (F == 2) { if (flag) L[cnt2++] = t; else R[cnt1++] = t; } // printf("%c\n", t); } } int len1 = 0, len2 = 0, len3 = 0; for (int i = 0; L[i] != '\0'; i++) len1++; for (int i = 0; R[i] != '\0'; i++) len2++; for (int i = 0; S[i] != '\0'; i++) len3++; // if (flag) // printf("flag\n\n"); if (!flag) { for (int i = len1 - 1; i >= 0; i--) printf("%c", L[i]); for (int i = len3 - 1; i >= 0; i--) printf("%c", S[i]); for (int i = 0; R[i] != '\0'; i++) printf("%c", R[i]); } else { for (int i = len2 - 1; i >= 0; i--) printf("%c", R[i]); for (int i = 0; S[i] != '\0'; i++) printf("%c", S[i]); for (int i = 0; L[i] != '\0'; i++) printf("%c", L[i]); } }
#include <algorithm> #include <iostream> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> using namespace std; #define MAX(x, y) (x > y ? x : y) #define MIN(x, y) (x < y ? x : y) #define ABS(x) (x > 0 ? x : -(x)) #define INF 1000000000 #define MOD 1000000007 void swap(int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; } void swap2(double *a, double *b) { double tmp; tmp = *a; *a = *b; *b = tmp; } int gcd(int a, int b) // 最大公約数 { if (a < b) swap(&a, &b); if (a % b == 0) return b; else return gcd(b, a % b); } int lcm(int a, int b) { return a * b / gcd(a, b); } long power(long x, int n) { // x^n 計算量O(logn) long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans = ans * x; } x = x * x; // 一周する度にx, x^2, x^4, x^8となる n >>= 1; // 桁をずらす n = n >> 1 } return ans; } int choose(int n, int k) { return k == 0 ? 1 : (long long)choose(n - 1, k - 1) * n % MOD * power(k, MOD - 2) % MOD; } void bubble_sort(int a[], int n) { int i, j, tmp; for (i = 0; i < n; i++) { for (int k = 0; k < n; k++) printf("%d ", a[k]); for (j = n - 1; j > i; j--) { if (a[j - 1] > a[j]) { tmp = a[j]; a[j] = a[j - 1]; a[j - 1] = tmp; } } printf("\n"); } } void selection_sort(int a[], int n) { int i, j, tmp, min, key; for (i = 0; i < n - 1; i++) { min = i; key = a[i]; for (int k = 0; k < n; k++) printf("%d ", a[k]); for (j = i + 1; j < n; j++) { if (a[j] < a[min]) { min = j; key = a[j]; } } tmp = a[i]; a[i] = a[min]; a[min] = tmp; printf("\n"); } for (int k = 0; k < n; k++) printf("%d ", a[k]); } void insertion_sort(int a[], int n) { int i, j, tmp; for (i = 1; i < n; i++) { j = i; while (j >= 1 && a[j - 1] > a[j]) { tmp = a[j]; a[j] = a[j - 1]; a[j - 1] = tmp; j--; } } } void shell_sort(int a[], int n) { int h, i, j; for (h = 1; h < n / 9; h = h * 3 + 1) ; for (; h > 0; h /= 3) { for (int k = 0; k < n; k++) printf("%d ", a[k]); for (i = h; i < n; i++) { j = i; while (j >= h && a[j - h] > a[j]) { swap(&a[j - h], &a[j]); j -= h; } } printf("\n"); } for (int k = 0; k < n; k++) printf("%d ", a[k]); } int partition(int a[], int l, int r) { int i, j, pivot; i = l - 1; j = r; pivot = a[r]; for (;;) { while (a[++i] < pivot) ; while (i < j-- && pivot < a[j]) ; if (i >= j) break; swap(&a[i], &a[j]); } swap(&a[i], &a[r]); return i; } void quick_sort1(int a[], int l, int r) { int v; if (l >= r) return; v = partition(a, l, r); quick_sort1(a, l, v - 1); quick_sort1(a, v + 1, r); } void quick_sort(int a[], int n) { quick_sort1(a, 0, n - 1); } int keta(int a, int n) { if (n == 1) return a % 10; for (int i = 0; i < n - 1; i++) a /= 10; return a % 10; } int main() { char S[100000]; char L[200000], R[200000]; char t, temp; bool flag = true; int Q; int T, F; int cnt1 = 0, cnt2 = 0; for (int i = 0; i < 100000; i++) { S[i] = L[i] = R[i] = '\0'; } scanf("%s%d", S, &Q); for (int i = 0; i < Q; i++) { scanf("%d", &T); if (T == 1) { flag = !flag; } else if (T == 2) { scanf("%d%c%c", &F, &temp, &t); if (F == 1) { if (flag) R[cnt1++] = t; else L[cnt2++] = t; } else if (F == 2) { if (flag) L[cnt2++] = t; else R[cnt1++] = t; } // printf("%c\n", t); } } int len1 = 0, len2 = 0, len3 = 0; for (int i = 0; L[i] != '\0'; i++) len1++; for (int i = 0; R[i] != '\0'; i++) len2++; for (int i = 0; S[i] != '\0'; i++) len3++; // if (flag) // printf("flag\n\n"); if (!flag) { for (int i = len1 - 1; i >= 0; i--) printf("%c", L[i]); for (int i = len3 - 1; i >= 0; i--) printf("%c", S[i]); for (int i = 0; R[i] != '\0'; i++) printf("%c", R[i]); } else { for (int i = len2 - 1; i >= 0; i--) printf("%c", R[i]); for (int i = 0; S[i] != '\0'; i++) printf("%c", S[i]); for (int i = 0; L[i] != '\0'; i++) printf("%c", L[i]); } }
replace
196
197
196
197
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string S, D; int Q, i; bool b = true; cin >> S >> Q; for (i = 0; i < Q; ++i) { int que = 0; cin >> que; if (que == 1) if (b) b = false; else b = true; else { int F = 0; char C; cin >> F >> C; if (F == 1) { if (b) { D.push_back(C); } else { S.push_back(C); } } else { if (b) S.push_back(C); else { D.push_back(C); } } } } if (!b) { reverse(S.begin(), S.end()); cout << S << D; } else { reverse(D.begin(), S.end()); cout << D << S; } }
#include <bits/stdc++.h> using namespace std; int main() { string S, D; int Q, i; bool b = true; cin >> S >> Q; for (i = 0; i < Q; ++i) { int que = 0; cin >> que; if (que == 1) if (b) b = false; else b = true; else { int F = 0; char C; cin >> F >> C; if (F == 1) { if (b) { D.push_back(C); } else { S.push_back(C); } } else { if (b) S.push_back(C); else { D.push_back(C); } } } } if (!b) { reverse(S.begin(), S.end()); cout << S << D; } else { reverse(D.begin(), D.end()); cout << D << S; } }
replace
39
40
39
40
0
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #define fo(a, b) for (int a = 0; a < b; a++) #define Sort(a) sort(a.begin(), a.end()) #define rev(a) reverse(a.begin(), a.end()) #define fi first #define se second #define sz size() #define bgn begin() #define en end() #define pb push_back #define pp() pop_back() #define V vector #define P pair #define yuko(a) setprecision(a) #define uni(a) a.erase(unique(a.begin(), a.end()), a.end()) #define Q queue #define pri priority_queue #define Pri priority_queue<int, vector<int>, greater<int>> #define PriP \ priority_queue<P<int, int>, vector<P<int, int>>, greater<P<int, int>>> #define ff first.first #define fs first.second #define sf second.first #define ss second.second #define all(a) (a).begin(), (a).end() #define elif else if int low(V<int> &a, int b) { auto c = lower_bound(a.begin(), a.end(), b); int d = c - a.bgn; return d; } int upp(V<int> &a, int b) { auto c = upper_bound(a.begin(), a.end(), b); int d = c - a.bgn; return d; } template <class T> void cou(vector<vector<T>> a) { int b = a.size(); int c = a[0].size(); fo(i, b) { fo(j, c) { cout << a[i][j]; if (j == c - 1) cout << endl; else cout << ' '; } } } int wari(int a, int b) { if (a % b == 0) return a / b; else return a / b + 1; } int keta(int a) { double b = a; b = log10(b); int c = b; return c + 1; } int souwa(int a) { return a * (a + 1) / 2; } int gcm(int a, int b) { if (a % b == 0) return b; return gcm(b, a % b); } bool prime(int a) { if (a < 2) return false; else if (a == 2) return true; else if (a % 2 == 0) return false; for (int i = 3; i <= sqrt(a) + 1; i += 2) { if (a % i == 0) return false; } return true; } struct Union { vector<int> par; Union(int a) { par = vector<int>(a, -1); } int find(int a) { if (par[a] < 0) return a; else return par[a] = find(par[a]); } bool same(int a, int b) { return find(a) == find(b); } int Size(int a) { return -par[find(a)]; } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (Size(b) > Size(a)) swap<int>(a, b); par[a] += par[b]; par[b] = a; } }; int ketas(int a) { string b = to_string(a); int c = 0; fo(i, keta(a)) { c += b[i] - '0'; } return c; } bool fe(int a, int b) { a %= 10; b %= 10; if (a == 0) a = 10; if (b == 0) b = 10; if (a > b) return true; else return false; } int INF = 1000000007; struct edge { int s, t, d; }; V<int> mojisyu(string a) { V<int> b(26, 0); fo(i, a.sz) { b[a[i] - 'a']++; } return b; } int wa2(int a) { if (a % 2 == 1) return a / 2; return a / 2 - 1; } /*signed main(){ int a,b,c; cin>>a>>b>>c; V<V<edge>> d(a); fo(i,b){ edge e; cin>>e.s>>e.t>>e.d; d[e.s].pb(e); } V<int> e(a,INF); e[c]=0; priority_queue<P<int,int>,V<P<int,int>>,greater<P<int,int>>> f; f.push({0,c}); int h=INF; while(!f.empty()){ P<int,int> g; g=f.top(); f.pop(); int v = g.second, i = g.first; for(edge l : d[v]){ if(e[l.t] > i + l.d){ e[l.t] = i + l.d; f.push({i+l.d , l.t}); } } } fo(i,a){ if(e[i]==INF) cout<<"INF"<<endl; else cout<<e[i]<<endl; } } ?*/ int nCr(int n, int r) { int a = 1; r = min(r, n - r); for (int i = n; i > n - r; i--) { a *= i; a /= n - i + 1; } return a; } /*void sea(int x,int y){ if(x<0||a<=x||y<0||b<=y||c[x][y]=='#') return; if(d[x][y]) return; d[x][y]++; sea(x+1,y); sea(x-1,y); sea(x,y+1); sea(x,y-1); }*/ int kaijou(int a) { int b = 1; fo(i, a) b *= i + 1; return b; } int nPr(int a, int b) { if (a < b) return 0; if (b == 0) return 1; int c = 1; for (int i = a; i > a - b; i--) { c *= i; c %= INF; } return c; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a^{-1} mod を計算する long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int lcm(int a, int b) { int c = modinv(gcm(a, b), INF); return ((a * c) % INF) * (b % INF) % INF; } int MOD = INF; int fac[1000010], finv[1000010], inv[1000010]; // テーブルを作る前処理 // 先にCOMinit()で前処理をする // ABC145D void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < 1000010; 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; } } // 二項係数計算 int 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; } bool naka(int a, int b, V<V<char>> c) { return (a >= 0 && b >= 0 && a < c.sz && b < c[0].sz); } V<P<int, int>> mawari8 = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {-1, -1}, {1, 1}, {1, -1}, {-1, -1}}; int inf = 1000000000000000007; /* signed main(){ int a,b,c; cin>>a>>b>>c; V<V<edge>> d(a); fo(i,b){ edge e; cin>>e.s>>e.t>>e.d; d[e.s].pb(e); } V<int> e(a,INF); e[c]=0; priority_queue<P<int,int>,V<P<int,int>>,greater<P<int,int>>> f; f.push({0,c}); int h=INF; while(!f.empty()){ P<int,int> g; g=f.top(); f.pop(); int v = g.second, i = g.first; for(edge l : d[v]){ if(e[l.t] > i + l.d){ e[l.t] = i + l.d; f.push({i+l.d , l.t}); } } } fo(i,a){ if(e[i]==INF) cout<<"INF"<<endl; else cout<<e[i]<<endl; } }*/ V<P<int, int>> mawari4 = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; // 最短経路の表 a(全部INFで初期化) // 縦横 x,y // 迷路 f // スタートsx,sy // ゴールgx,gy // 文字はgから使おうね /*int bfs_haba(){ Q<P<int,int>> b; a[sx][sy]=0; b.push({sx,sy}); while(!b.empty()){ P<int,int> c=b.front(); b.pop(); if(c.fi==gx&&c.se==gy){ break; } fo(i,4){ int d=c.fi+mawari4[i].fi; int e=c.se+mawari4[i].se; if(0<=d&&0<=e&&d<x&&e<y&&f[d][e]!='#'&&a[d][e]==INF){ b.push({d,e}); a[d][e]=1+a[c.fi][c.se]; } } } return a[gx][gy]; }*/ V<int> onajibubun(string a) { V<int> b(a.sz); for (int i = 1, j = 0; i < a.sz; i++) { if (i + b[i - j] < j + b[j]) b[i] = b[i - j]; else { int c = max<int>(0, j + b[j] - i); while (i + c < a.sz && a[c] == a[i + c]) c++; b[i] = c; j = i; } } b[0] = a.sz; return b; } // 各頂点ごとにどこに辺が出てるかの表がc // 各頂点ごとの色を表すV<int>(頂点数max)のcolorを用意する // aはどこ塗るか、bは何で塗るかなので、(0,1,c)でよぶとおけ V<int> color(205); bool nibu_hantei(int a, int b, V<V<int>> c) { color[a] = b; fo(i, c[a].sz) { if (b == color[c[a][i]]) return false; if (color[c[a][i]] == 0 && !nibu_hantei(c[a][i], -b, c)) return false; } return true; } // aは頂点数 // nibu_hanteiの上にcolorを用意する // 各頂点ごとにどこに辺が出てるかの表がc bool renketujanai_nibu_hantei(int a, V<V<int>> c) { fo(i, a) { if (color[i] == 0) { if (!nibu_hantei(i, 1, c)) return false; } } return true; } struct segmin { vector<int> seg; int b; segmin(V<int> a) { b = 1; while (b < a.sz) b *= 2; seg = vector<int>(2 * b - 1, inf); fo(i, a.sz) { seg[i + b - 1] = a[i]; } for (int i = b - 2; i >= 0; i--) { seg[i] = min(seg[2 * i + 1], seg[2 * i + 2]); } } void update(int i, int a) { i += b - 1; seg[i] = a; while (i) { i = (i - 1) / 2; seg[i] = min(seg[2 * i + 1], seg[2 * i + 2]); } } // 最初呼び出すときは要求区間x,y+1(yは添え字+1)とa=0,l=0,r=INFで // l,rは探すところ int getmin(int x, int y, int a, int l, int r) { if (r == INF) r = b; if (r <= x || y <= l) return INF; if (x <= l && r <= y) return seg[a]; int a1 = getmin(x, y, 2 * a + 1, l, (l + r) / 2); int a2 = getmin(x, y, 2 * a + 2, (l + r) / 2, r); return min(a1, a2); } }; struct segadd { vector<int> seg; int b; segadd(V<int> a) { b = 1; while (b < a.sz) b *= 2; seg = vector<int>(2 * b - 1, 0); fo(i, a.sz) { seg[i + b - 1] = a[i]; } for (int i = b - 2; i >= 0; i--) { seg[i] = seg[2 * i + 1] + seg[2 * i + 2]; } } void update(int i, int a) { i += b - 1; seg[i] = a; while (i) { i = (i - 1) / 2; seg[i] = seg[2 * i + 1] + seg[2 * i + 2]; } } // 最初呼び出すときは要求区間x,y+1(yは添え字+1)とa=0,l=0,r=INFで // l,rは探すところ int getadd(int x, int y, int a, int l, int r) { if (r == INF) r = b; if (r <= x || y <= l) return 0; if (x <= l && r <= y) return seg[a]; int a1 = getadd(x, y, 2 * a + 1, l, (l + r) / 2); int a2 = getadd(x, y, 2 * a + 2, (l + r) / 2, r); return a1 + a2; } }; struct sege { vector<P<int, V<int>>> seg; int b; sege(string a) { b = 1; while (b < a.sz) b *= 2; seg = vector<P<int, V<int>>>(2 * b - 1); fo(i, a.sz) { seg[i + b - 1].fi = 1; seg[i + b - 1].se.pb(a[i] - 'a'); } for (int i = b - 2; i >= 0; i--) { V<int> d = seg[2 * i + 1].se; fo(j, seg[2 * i + 2].se.sz) { d.pb(seg[2 * i + 2].se[j]); } Sort(d); uni(d); seg[i].se = d; seg[i].fi = d.sz; } } V<int> mu; void update(int i, char a) { i += b - 1; seg[i].se = mu; seg[i].se.pb(a - 'a'); seg[i].fi = 1; while (i) { i = (i - 1) / 2; V<int> d = seg[2 * i + 1].se; fo(j, seg[2 * i + 2].se.sz) { d.pb(seg[2 * i + 2].se[j]); } Sort(d); uni(d); seg[i].se = d; seg[i].fi = d.sz; } } void unko() { fo(i, 2 * b - 1) cout << seg[i].fi << ' '; } // 最初呼び出すときは要求区間x,y+1(yは添え字+1)とa=0,l=0,r=INFで // l,rは探すところ P<int, V<int>> gete(int x, int y, int a, int l, int r) { if (r == INF) r = b; if (r <= x || y <= l) return {0, mu}; if (x <= l && r <= y) return seg[a]; P<int, V<int>> a1 = gete(x, y, 2 * a + 1, l, (l + r) / 2); P<int, V<int>> a2 = gete(x, y, 2 * a + 2, (l + r) / 2, r); fo(i, a2.se.sz) a1.se.pb(a2.se[i]); Sort(a1.se); uni(a1.se); return {a1.se.sz, a1.se}; } }; signed main() { string a; cin >> a; int b; cin >> b; int c = 0; fo(i, b) { int d; cin >> d; if (d == 1) c++; else { int e; cin >> e; e += c; if (e % 2) { string f; cin >> f; f += a; a = f; } else { string g; cin >> g; a += g; } } } if (c % 2) rev(a); cout << a << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #define fo(a, b) for (int a = 0; a < b; a++) #define Sort(a) sort(a.begin(), a.end()) #define rev(a) reverse(a.begin(), a.end()) #define fi first #define se second #define sz size() #define bgn begin() #define en end() #define pb push_back #define pp() pop_back() #define V vector #define P pair #define yuko(a) setprecision(a) #define uni(a) a.erase(unique(a.begin(), a.end()), a.end()) #define Q queue #define pri priority_queue #define Pri priority_queue<int, vector<int>, greater<int>> #define PriP \ priority_queue<P<int, int>, vector<P<int, int>>, greater<P<int, int>>> #define ff first.first #define fs first.second #define sf second.first #define ss second.second #define all(a) (a).begin(), (a).end() #define elif else if int low(V<int> &a, int b) { auto c = lower_bound(a.begin(), a.end(), b); int d = c - a.bgn; return d; } int upp(V<int> &a, int b) { auto c = upper_bound(a.begin(), a.end(), b); int d = c - a.bgn; return d; } template <class T> void cou(vector<vector<T>> a) { int b = a.size(); int c = a[0].size(); fo(i, b) { fo(j, c) { cout << a[i][j]; if (j == c - 1) cout << endl; else cout << ' '; } } } int wari(int a, int b) { if (a % b == 0) return a / b; else return a / b + 1; } int keta(int a) { double b = a; b = log10(b); int c = b; return c + 1; } int souwa(int a) { return a * (a + 1) / 2; } int gcm(int a, int b) { if (a % b == 0) return b; return gcm(b, a % b); } bool prime(int a) { if (a < 2) return false; else if (a == 2) return true; else if (a % 2 == 0) return false; for (int i = 3; i <= sqrt(a) + 1; i += 2) { if (a % i == 0) return false; } return true; } struct Union { vector<int> par; Union(int a) { par = vector<int>(a, -1); } int find(int a) { if (par[a] < 0) return a; else return par[a] = find(par[a]); } bool same(int a, int b) { return find(a) == find(b); } int Size(int a) { return -par[find(a)]; } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (Size(b) > Size(a)) swap<int>(a, b); par[a] += par[b]; par[b] = a; } }; int ketas(int a) { string b = to_string(a); int c = 0; fo(i, keta(a)) { c += b[i] - '0'; } return c; } bool fe(int a, int b) { a %= 10; b %= 10; if (a == 0) a = 10; if (b == 0) b = 10; if (a > b) return true; else return false; } int INF = 1000000007; struct edge { int s, t, d; }; V<int> mojisyu(string a) { V<int> b(26, 0); fo(i, a.sz) { b[a[i] - 'a']++; } return b; } int wa2(int a) { if (a % 2 == 1) return a / 2; return a / 2 - 1; } /*signed main(){ int a,b,c; cin>>a>>b>>c; V<V<edge>> d(a); fo(i,b){ edge e; cin>>e.s>>e.t>>e.d; d[e.s].pb(e); } V<int> e(a,INF); e[c]=0; priority_queue<P<int,int>,V<P<int,int>>,greater<P<int,int>>> f; f.push({0,c}); int h=INF; while(!f.empty()){ P<int,int> g; g=f.top(); f.pop(); int v = g.second, i = g.first; for(edge l : d[v]){ if(e[l.t] > i + l.d){ e[l.t] = i + l.d; f.push({i+l.d , l.t}); } } } fo(i,a){ if(e[i]==INF) cout<<"INF"<<endl; else cout<<e[i]<<endl; } } ?*/ int nCr(int n, int r) { int a = 1; r = min(r, n - r); for (int i = n; i > n - r; i--) { a *= i; a /= n - i + 1; } return a; } /*void sea(int x,int y){ if(x<0||a<=x||y<0||b<=y||c[x][y]=='#') return; if(d[x][y]) return; d[x][y]++; sea(x+1,y); sea(x-1,y); sea(x,y+1); sea(x,y-1); }*/ int kaijou(int a) { int b = 1; fo(i, a) b *= i + 1; return b; } int nPr(int a, int b) { if (a < b) return 0; if (b == 0) return 1; int c = 1; for (int i = a; i > a - b; i--) { c *= i; c %= INF; } return c; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a^{-1} mod を計算する long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int lcm(int a, int b) { int c = modinv(gcm(a, b), INF); return ((a * c) % INF) * (b % INF) % INF; } int MOD = INF; int fac[1000010], finv[1000010], inv[1000010]; // テーブルを作る前処理 // 先にCOMinit()で前処理をする // ABC145D void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < 1000010; 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; } } // 二項係数計算 int 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; } bool naka(int a, int b, V<V<char>> c) { return (a >= 0 && b >= 0 && a < c.sz && b < c[0].sz); } V<P<int, int>> mawari8 = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {-1, -1}, {1, 1}, {1, -1}, {-1, -1}}; int inf = 1000000000000000007; /* signed main(){ int a,b,c; cin>>a>>b>>c; V<V<edge>> d(a); fo(i,b){ edge e; cin>>e.s>>e.t>>e.d; d[e.s].pb(e); } V<int> e(a,INF); e[c]=0; priority_queue<P<int,int>,V<P<int,int>>,greater<P<int,int>>> f; f.push({0,c}); int h=INF; while(!f.empty()){ P<int,int> g; g=f.top(); f.pop(); int v = g.second, i = g.first; for(edge l : d[v]){ if(e[l.t] > i + l.d){ e[l.t] = i + l.d; f.push({i+l.d , l.t}); } } } fo(i,a){ if(e[i]==INF) cout<<"INF"<<endl; else cout<<e[i]<<endl; } }*/ V<P<int, int>> mawari4 = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; // 最短経路の表 a(全部INFで初期化) // 縦横 x,y // 迷路 f // スタートsx,sy // ゴールgx,gy // 文字はgから使おうね /*int bfs_haba(){ Q<P<int,int>> b; a[sx][sy]=0; b.push({sx,sy}); while(!b.empty()){ P<int,int> c=b.front(); b.pop(); if(c.fi==gx&&c.se==gy){ break; } fo(i,4){ int d=c.fi+mawari4[i].fi; int e=c.se+mawari4[i].se; if(0<=d&&0<=e&&d<x&&e<y&&f[d][e]!='#'&&a[d][e]==INF){ b.push({d,e}); a[d][e]=1+a[c.fi][c.se]; } } } return a[gx][gy]; }*/ V<int> onajibubun(string a) { V<int> b(a.sz); for (int i = 1, j = 0; i < a.sz; i++) { if (i + b[i - j] < j + b[j]) b[i] = b[i - j]; else { int c = max<int>(0, j + b[j] - i); while (i + c < a.sz && a[c] == a[i + c]) c++; b[i] = c; j = i; } } b[0] = a.sz; return b; } // 各頂点ごとにどこに辺が出てるかの表がc // 各頂点ごとの色を表すV<int>(頂点数max)のcolorを用意する // aはどこ塗るか、bは何で塗るかなので、(0,1,c)でよぶとおけ V<int> color(205); bool nibu_hantei(int a, int b, V<V<int>> c) { color[a] = b; fo(i, c[a].sz) { if (b == color[c[a][i]]) return false; if (color[c[a][i]] == 0 && !nibu_hantei(c[a][i], -b, c)) return false; } return true; } // aは頂点数 // nibu_hanteiの上にcolorを用意する // 各頂点ごとにどこに辺が出てるかの表がc bool renketujanai_nibu_hantei(int a, V<V<int>> c) { fo(i, a) { if (color[i] == 0) { if (!nibu_hantei(i, 1, c)) return false; } } return true; } struct segmin { vector<int> seg; int b; segmin(V<int> a) { b = 1; while (b < a.sz) b *= 2; seg = vector<int>(2 * b - 1, inf); fo(i, a.sz) { seg[i + b - 1] = a[i]; } for (int i = b - 2; i >= 0; i--) { seg[i] = min(seg[2 * i + 1], seg[2 * i + 2]); } } void update(int i, int a) { i += b - 1; seg[i] = a; while (i) { i = (i - 1) / 2; seg[i] = min(seg[2 * i + 1], seg[2 * i + 2]); } } // 最初呼び出すときは要求区間x,y+1(yは添え字+1)とa=0,l=0,r=INFで // l,rは探すところ int getmin(int x, int y, int a, int l, int r) { if (r == INF) r = b; if (r <= x || y <= l) return INF; if (x <= l && r <= y) return seg[a]; int a1 = getmin(x, y, 2 * a + 1, l, (l + r) / 2); int a2 = getmin(x, y, 2 * a + 2, (l + r) / 2, r); return min(a1, a2); } }; struct segadd { vector<int> seg; int b; segadd(V<int> a) { b = 1; while (b < a.sz) b *= 2; seg = vector<int>(2 * b - 1, 0); fo(i, a.sz) { seg[i + b - 1] = a[i]; } for (int i = b - 2; i >= 0; i--) { seg[i] = seg[2 * i + 1] + seg[2 * i + 2]; } } void update(int i, int a) { i += b - 1; seg[i] = a; while (i) { i = (i - 1) / 2; seg[i] = seg[2 * i + 1] + seg[2 * i + 2]; } } // 最初呼び出すときは要求区間x,y+1(yは添え字+1)とa=0,l=0,r=INFで // l,rは探すところ int getadd(int x, int y, int a, int l, int r) { if (r == INF) r = b; if (r <= x || y <= l) return 0; if (x <= l && r <= y) return seg[a]; int a1 = getadd(x, y, 2 * a + 1, l, (l + r) / 2); int a2 = getadd(x, y, 2 * a + 2, (l + r) / 2, r); return a1 + a2; } }; struct sege { vector<P<int, V<int>>> seg; int b; sege(string a) { b = 1; while (b < a.sz) b *= 2; seg = vector<P<int, V<int>>>(2 * b - 1); fo(i, a.sz) { seg[i + b - 1].fi = 1; seg[i + b - 1].se.pb(a[i] - 'a'); } for (int i = b - 2; i >= 0; i--) { V<int> d = seg[2 * i + 1].se; fo(j, seg[2 * i + 2].se.sz) { d.pb(seg[2 * i + 2].se[j]); } Sort(d); uni(d); seg[i].se = d; seg[i].fi = d.sz; } } V<int> mu; void update(int i, char a) { i += b - 1; seg[i].se = mu; seg[i].se.pb(a - 'a'); seg[i].fi = 1; while (i) { i = (i - 1) / 2; V<int> d = seg[2 * i + 1].se; fo(j, seg[2 * i + 2].se.sz) { d.pb(seg[2 * i + 2].se[j]); } Sort(d); uni(d); seg[i].se = d; seg[i].fi = d.sz; } } void unko() { fo(i, 2 * b - 1) cout << seg[i].fi << ' '; } // 最初呼び出すときは要求区間x,y+1(yは添え字+1)とa=0,l=0,r=INFで // l,rは探すところ P<int, V<int>> gete(int x, int y, int a, int l, int r) { if (r == INF) r = b; if (r <= x || y <= l) return {0, mu}; if (x <= l && r <= y) return seg[a]; P<int, V<int>> a1 = gete(x, y, 2 * a + 1, l, (l + r) / 2); P<int, V<int>> a2 = gete(x, y, 2 * a + 2, (l + r) / 2, r); fo(i, a2.se.sz) a1.se.pb(a2.se[i]); Sort(a1.se); uni(a1.se); return {a1.se.sz, a1.se}; } }; signed main() { string a; cin >> a; int b; cin >> b; int c = 0; fo(i, b) { int d; cin >> d; if (d == 1) c++; else { int e; cin >> e; e += c; if (e % 2) { string f; cin >> f; a.insert(0, f); } else { string g; cin >> g; a += g; } } } if (c % 2) rev(a); cout << a << endl; }
replace
503
505
503
504
TLE
p02756
Python
Runtime Error
from collections import deque S = deque(input()) Q = int(input()) Queries = [input().split() for _ in range(Q)] need_reverse = False for q in Queries: if q[0] == "1": need_reverse = not need_reverse else: if q[1] == "1": if not need_reverse: S.appendleft(q[2]) else: S.append(q[2]) else: if not need_reverse: S.append(q[2]) else: S.appendleft(q[2]) print(S if not need_reverse else S[::-1])
from collections import deque S = deque(input()) Q = int(input()) Queries = [input().split() for _ in range(Q)] need_reverse = False for q in Queries: if q[0] == "1": need_reverse = not need_reverse else: if q[1] == "1": if not need_reverse: S.appendleft(q[2]) else: S.append(q[2]) else: if not need_reverse: S.append(q[2]) else: S.appendleft(q[2]) S = "".join(S) print(S if not need_reverse else S[::-1])
insert
23
23
23
24
0
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; void textIO() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); } int main() { textIO(); fastIO(); string s; long long q, t, f; char c; cin >> s; cin >> q; bool rev = false; while (q--) { cin >> t; if (t == 1) { rev = !rev; } else { cin >> f >> c; if (rev) { if (f == 1) { s.append(1, c); } else { s.insert(s.begin(), c); } } else { if (f == 1) { s.insert(s.begin(), c); } else { s.append(1, c); } } } } if (rev) { reverse(s.begin(), s.end()); cout << s << '\n'; } else { cout << s << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; void textIO() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); } int main() { // textIO(); fastIO(); string s; long long q, t, f; char c; cin >> s; cin >> q; bool rev = false; while (q--) { cin >> t; if (t == 1) { rev = !rev; } else { cin >> f >> c; if (rev) { if (f == 1) { s.append(1, c); } else { s.insert(s.begin(), c); } } else { if (f == 1) { s.insert(s.begin(), c); } else { s.append(1, c); } } } } if (rev) { reverse(s.begin(), s.end()); cout << s << '\n'; } else { cout << s << '\n'; } return 0; }
replace
16
17
16
17
TLE
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; void textIO() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); } int main() { textIO(); fastIO(); string s, prefix, suffix; long long q, t, f; char c; cin >> s; cin >> q; bool rev = false; while (q--) { cin >> t; if (t == 1) { rev = !rev; } else { cin >> f >> c; if (rev) { if (f == 1) { suffix += c; } else { prefix += c; } } else { if (f == 1) { prefix += c; } else { suffix += c; } } } } if (rev) { reverse(suffix.begin(), suffix.end()); reverse(s.begin(), s.end()); cout << suffix << s << prefix << '\n'; } else { reverse(prefix.begin(), prefix.end()); cout << prefix << s << suffix << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; void textIO() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); } int main() { // textIO(); fastIO(); string s, prefix, suffix; long long q, t, f; char c; cin >> s; cin >> q; bool rev = false; while (q--) { cin >> t; if (t == 1) { rev = !rev; } else { cin >> f >> c; if (rev) { if (f == 1) { suffix += c; } else { prefix += c; } } else { if (f == 1) { prefix += c; } else { suffix += c; } } } } if (rev) { reverse(suffix.begin(), suffix.end()); reverse(s.begin(), s.end()); cout << suffix << s << prefix << '\n'; } else { reverse(prefix.begin(), prefix.end()); cout << prefix << s << suffix << '\n'; } return 0; }
replace
16
17
16
17
TLE
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; unordered_map<int, pair<int, char>> mp; int a[N], c[N]; char b[3 * N]; int main() { string s; string x; int t, k, f, q, sum = 0; cin >> s >> q; for (int i = 1; i <= q; i++) { scanf("%d", &t); if (t == 1) a[i] = 1, sum++; else { cin >> f >> x; mp[i] = {f, x[0]}; } } c[0] = sum % 2; for (int i = 1; i <= q; i++) c[i] = c[i - 1] + a[i]; int l = 1e5, r = l - 1; if (sum % 2) for (int i = s.size() - 1; i >= 0; i--) b[++r] = s[i]; else for (int i = 0; i < s.size(); i++) b[++r] = s[i]; // for(int i = 1; i <= q ; i ++) cout << c[i] << " " ; // puts("") ; for (int i = 1; i <= q; i++) { if (a[i]) continue; int f = mp[i].first; char h = mp[i].second; if (c[i] % 2 == f % 2) b[++r] = h; else b[--l] = h; } for (int i = l; i <= r; i++) cout << b[i]; puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; unordered_map<int, pair<int, char>> mp; int a[N], c[N]; char b[3 * N]; int main() { string s; string x; int t, k, f, q, sum = 0; cin >> s >> q; for (int i = 1; i <= q; i++) { scanf("%d", &t); if (t == 1) a[i] = 1, sum++; else { cin >> f >> x; mp[i] = {f, x[0]}; } } c[0] = sum % 2; for (int i = 1; i <= q; i++) c[i] = c[i - 1] + a[i]; int l = 3e5, r = l - 1; if (sum % 2) for (int i = s.size() - 1; i >= 0; i--) b[++r] = s[i]; else for (int i = 0; i < s.size(); i++) b[++r] = s[i]; // for(int i = 1; i <= q ; i ++) cout << c[i] << " " ; // puts("") ; for (int i = 1; i <= q; i++) { if (a[i]) continue; int f = mp[i].first; char h = mp[i].second; if (c[i] % 2 == f % 2) b[++r] = h; else b[--l] = h; } for (int i = l; i <= r; i++) cout << b[i]; puts(""); return 0; }
replace
23
24
23
24
0
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (long long i = 0; i < n; i++) #define Rep(i, n) for (long long i = 1; i < n; i++) #define ll long long #include <cmath> #include <iomanip> #include <iostream> #include <math.h> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; #define PI acos(-1) using P = pair<int, int>; ll ketasu(ll a) { ll num = 1; while (a / 10) { num++; a /= 10; } return num; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } ll kosuu(ll a) { ll sum = 0; for (ll i = 1; i * i <= a; i++) { if (a % i == 0) { if (a != 1 && i * i != a) { sum += 2; } else { sum++; } } } return sum; } ll ans = -1000000000; int main() { string s; cin >> s; ll n; cin >> n; ll cnt = 0; rep(i, n) { ll a; cin >> a; if (a == 1) cnt++; if (a == 2) { ll b; string c; cin >> b >> c; if (cnt % 2 == 1) { if (b == 1) b = 2; else b = 1; } if (b == 1) s = c + s; if (b == 2) s = s + c; } } if (cnt % 2 == 1) reverse(s.begin(), s.end()); cout << s << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (long long i = 0; i < n; i++) #define Rep(i, n) for (long long i = 1; i < n; i++) #define ll long long #include <cmath> #include <iomanip> #include <iostream> #include <math.h> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; #define PI acos(-1) using P = pair<int, int>; ll ketasu(ll a) { ll num = 1; while (a / 10) { num++; a /= 10; } return num; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } ll kosuu(ll a) { ll sum = 0; for (ll i = 1; i * i <= a; i++) { if (a % i == 0) { if (a != 1 && i * i != a) { sum += 2; } else { sum++; } } } return sum; } ll ans = -1000000000; int main() { string s; cin >> s; ll n; cin >> n; ll cnt = 0; rep(i, n) { ll a; cin >> a; if (a == 1) cnt++; if (a == 2) { ll b; string c; cin >> b >> c; if (cnt % 2 == 1) { if (b == 1) b = 2; else b = 1; } if (b == 1) s = c + s; if (b == 2) s += c; } } if (cnt % 2 == 1) reverse(s.begin(), s.end()); cout << s << endl; }
replace
68
69
68
69
TLE
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define fastinput \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); int main() { fastinput; LL n, i, j, k, t, temp; LL truth = 0; string s = ""; char str[300003]; LL now = 0; LL queries; cin >> s; LL len = s.length(); LL start, end; start = now - 1; for (i = 0; i < len; i++) { str[i + now] = s[i]; } end = now + len; LL f, c; cin >> queries; while (queries--) { cin >> t; if (t == 1) { truth ^= 1; } else { char ch; cin >> f >> ch; if ((f == 1 && truth == 0) || (f == 2 && truth == 1)) { str[start--] = ch; } else { str[end++] = ch; } } } if (truth == 1) { for (i = end - 1; i > start; i--) { cout << str[i]; } cout << endl; } else { for (i = start + 1; i < end; i++) { cout << str[i]; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define fastinput \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); int main() { fastinput; LL n, i, j, k, t, temp; LL truth = 0; string s = ""; char str[500003]; LL now = 200003; LL queries; cin >> s; LL len = s.length(); LL start, end; start = now - 1; for (i = 0; i < len; i++) { str[i + now] = s[i]; } end = now + len; LL f, c; cin >> queries; while (queries--) { cin >> t; if (t == 1) { truth ^= 1; } else { char ch; cin >> f >> ch; if ((f == 1 && truth == 0) || (f == 2 && truth == 1)) { str[start--] = ch; } else { str[end++] = ch; } } } if (truth == 1) { for (i = end - 1; i > start; i--) { cout << str[i]; } cout << endl; } else { for (i = start + 1; i < end; i++) { cout << str[i]; } cout << endl; } }
replace
13
15
13
15
0
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define mod 1000000007 int main() { string s; cin >> s; int q; scanf("%d", &q); bool rev = false; for (int i = 0; i < q; i++) { int t; scanf("%d", &t); if (t == 1) { rev = !rev; } else { int f; string c; scanf("%d", &f); cin >> c; if (rev) { if (f == 1) f = 2; else f = 1; } if (f == 1) { s = c + s; // s.insert(0,c, 1); // cout << s << endl; } else { // s = s+c; s.push_back(c[0]); } } } int len = s.size(); if (rev) { for (int i = len - 1; i >= 0; i--) cout << s[i]; cout << endl; } else cout << s << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define mod 1000000007 int main() { string s; cin >> s; int q; scanf("%d", &q); bool rev = false; for (int i = 0; i < q; i++) { int t; scanf("%d", &t); if (t == 1) { rev = !rev; } else { int f; string c; scanf("%d", &f); cin >> c; if (rev) { if (f == 1) f = 2; else f = 1; } if (f == 1) { // s = c+s; s.insert(0, c); } else { // s = s+c; s.push_back(c[0]); } } } int len = s.size(); if (rev) { for (int i = len - 1; i >= 0; i--) cout << s[i]; cout << endl; } else cout << s << endl; return 0; }
replace
30
33
30
32
TLE
p02756
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <vector> using namespace std; #define INF 0x3f3f3f3f const int maxn = 4e5 + 10; #define _rep(i, a, b) for (int i = (a); i <= (b); ++i) char S[maxn], Ci; int Q, Fi; int main() { #ifdef __LOCAL__ freopen("1.in", "r", stdin); // freopen("1.out","w",stdout); #endif string a; int pos = 10000, left = 10000, right = pos - 1; bool flag = true; cin >> a >> Q; for (unsigned int i = 0; i < a.size(); i++) S[++right] = a[i]; while (Q--) { // for(int i=left;i<=right;i++) // cout<<S[i]; // cout<<endl; // cout<<flag<<endl; int type; cin >> type; if (type == 1) { flag = !flag; } else if (type == 2) { cin >> Fi >> Ci; if (Fi == 1) { if (flag) S[--left] = Ci; else S[++right] = Ci; } else if (Fi == 2) { if (!flag) S[--left] = Ci; else S[++right] = Ci; } } } if (flag) { for (int i = left; i <= right; i++) cout << S[i]; cout << endl; } else { for (int i = right; i >= left; i--) cout << S[i]; cout << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <vector> using namespace std; #define INF 0x3f3f3f3f const int maxn = 4e5 + 10; #define _rep(i, a, b) for (int i = (a); i <= (b); ++i) char S[maxn], Ci; int Q, Fi; int main() { #ifdef __LOCAL__ freopen("1.in", "r", stdin); // freopen("1.out","w",stdout); #endif string a; int pos = 2e5, left = pos, right = pos - 1; bool flag = true; cin >> a >> Q; for (unsigned int i = 0; i < a.size(); i++) S[++right] = a[i]; while (Q--) { // for(int i=left;i<=right;i++) // cout<<S[i]; // cout<<endl; // cout<<flag<<endl; int type; cin >> type; if (type == 1) { flag = !flag; } else if (type == 2) { cin >> Fi >> Ci; if (Fi == 1) { if (flag) S[--left] = Ci; else S[++right] = Ci; } else if (Fi == 2) { if (!flag) S[--left] = Ci; else S[++right] = Ci; } } } if (flag) { for (int i = left; i <= right; i++) cout << S[i]; cout << endl; } else { for (int i = right; i >= left; i--) cout << S[i]; cout << endl; } return 0; }
replace
20
21
20
21
0
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { string s; int q; cin >> s >> q; vector<int> t(q), f(q, 0); vector<string> c(q, ""); for (int i = 0; i < q; i++) { cin >> t.at(i); if (t.at(i) == 2) { cin >> f.at(i) >> c.at(i); } } bool normal = true; for (int i = 0; i < q; i++) { if (t.at(i) == 1) { reverse(s.begin(), s.end()); } else if (t.at(i) == 2) { if (!normal) { if (f.at(i) == 1) f.at(i) = 2; else if (f.at(i) == 2) f.at(i) = 1; } if (f.at(i) == 1) { s.insert(0, c.at(i)); } else if (f.at(i) == 2) { s.append(c.at(i)); } } // cout << s << " process " << i << endl; } if (!normal) reverse(s.begin(), s.end()); cout << s << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { string s; int q; cin >> s >> q; vector<int> t(q), f(q, 0); vector<string> c(q, ""); for (int i = 0; i < q; i++) { cin >> t.at(i); if (t.at(i) == 2) { cin >> f.at(i) >> c.at(i); } } bool normal = true; for (int i = 0; i < q; i++) { if (t.at(i) == 1) { normal = !normal; } else if (t.at(i) == 2) { if (!normal) { if (f.at(i) == 1) f.at(i) = 2; else if (f.at(i) == 2) f.at(i) = 1; } if (f.at(i) == 1) { s.insert(0, c.at(i)); } else if (f.at(i) == 2) { s.append(c.at(i)); } } // cout << s << " process " << i << endl; } if (!normal) reverse(s.begin(), s.end()); cout << s << endl; }
replace
20
21
20
21
TLE
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int, int>; int main() { string s; cin >> s; ll q; cin >> q; int count = 0; for (ll i = 0; i < q; i++) { int t; cin >> t; if (t == 1) count++; if (t == 2) { int f; cin >> f; char c; cin >> c; if (f == 1 && count % 2 == 0 || f == 2 && count % 2 == 1) s = c + s; if (f == 2 && count % 2 == 0 || f == 1 && count % 2 == 1) s = s + c; } } if (count % 2 == 0) cout << s << endl; else { reverse(s.begin(), s.end()); cout << s << 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>; int main() { string s; cin >> s; ll q; cin >> q; int count = 0; for (ll i = 0; i < q; i++) { int t; cin >> t; if (t == 1) count++; if (t == 2) { int f; cin >> f; char c; cin >> c; if (((f == 1) && (count % 2 == 0)) || ((f == 2) && (count % 2 == 1))) { string u; u.push_back(c); s.insert(0, u); } if (((f == 2) && (count % 2 == 0)) || ((f == 1) && (count % 2 == 1))) s.push_back(c); } } if (count % 2 == 0) cout << s << endl; else { reverse(s.begin(), s.end()); cout << s << endl; } return 0; }
replace
22
26
22
29
TLE
p02756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 5; vector<char> fr, ba; int main() { string buff; cin >> buff; char tem[2]; int Q; cin >> Q; bool rev = 0; while (Q--) { int t, f, c; cin >> t; if (t == 1) { rev ^= 1; continue; } else { cin >> f >> tem; f--; f ^= rev; if (f == 1) ba.push_back(*tem); else fr.push_back(*tem); } } if (rev) { swap(fr, ba); reverse(buff.begin(), buff.end()); } for (auto x = fr.end() - 1;; x--) { cout << *x; if (x == fr.begin()) break; } cout << buff; for (auto x : ba) cout << x; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 5; vector<char> fr, ba; int main() { string buff; cin >> buff; char tem[2]; int Q; cin >> Q; bool rev = 0; while (Q--) { int t, f, c; cin >> t; if (t == 1) { rev ^= 1; continue; } else { cin >> f >> tem; f--; f ^= rev; if (f == 1) ba.push_back(*tem); else fr.push_back(*tem); } } if (rev) { swap(fr, ba); reverse(buff.begin(), buff.end()); } if (fr.size()) for (auto x = fr.end() - 1;; x--) { cout << *x; if (x == fr.begin()) break; } cout << buff; for (auto x : ba) cout << x; return 0; }
replace
33
38
33
39
0
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #include <iostream> #include <math.h> #include <set> #include <stdio.h> #include <vector> using namespace std; using ll = long long; using Graph = vector<vector<int>>; int main() { string s; cin >> s; int n; cin >> n; bool rev = false; int t; int f; char c; // vector<int>q; rep(i, n) { cin >> t; // q.push_back(t); if (t == 1) { rev = !rev; continue; } if (rev) { reverse(s.begin(), s.end()); rev = false; } cin >> f >> c; if (f == 1) { s.insert(s.begin(), c); } else { // f==2 s.push_back(c); } } if (rev) reverse(s.begin(), s.end()); cout << s << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #include <iostream> #include <math.h> #include <set> #include <stdio.h> #include <vector> using namespace std; using ll = long long; using Graph = vector<vector<int>>; int main() { string s; cin >> s; int n; cin >> n; bool rev = false; int t; int f; char c; // vector<int>q; rep(i, n) { cin >> t; // q.push_back(t); if (t == 1) { rev = !rev; } else { cin >> f >> c; if (f == 1) { if (rev) s.push_back(c); else s.insert(s.begin(), c); } else { // f==2 if (rev) s.insert(s.begin(), c); else s.push_back(c); } } } if (rev) reverse(s.begin(), s.end()); cout << s << endl; return 0; }
replace
29
41
29
42
TLE
p02756
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { string S; int Q; cin >> S >> Q; int T, F; string C; bool rev = false; rep(i, Q) { cin >> T; if (T == 1) { rev = !rev; } else { cin >> F >> C; if ((F == 1 && !rev) || (F == 2 && rev)) S.insert(0, C); else S = S + C; } } if (rev) reverse(S.begin(), S.end()); cout << S << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { string S; int Q; cin >> S >> Q; int T, F; string C; bool rev = false; rep(i, Q) { cin >> T; if (T == 1) { rev = !rev; } else { cin >> F >> C; if ((F == 1 && !rev) || (F == 2 && rev)) S.insert(0, C); else { char D = C.at(0); S.push_back(D); } } } if (rev) reverse(S.begin(), S.end()); cout << S << endl; }
replace
21
23
21
25
TLE
p02757
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pi 3.14159265359 #define int long long #define pii pair<int, int> const int mod = 1e9 + 7; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif int n, p; string s; cin >> n >> p >> s; int ans = 0; if (p == 2 || p == 5) { for (int i = 0; i < n; ++i) if ((s[i] - '0') % p == 0) ans += i + 1; cout << ans << "\n"; return 0; } map<int, int> mp; mp[0]++; int t = 0, p2 = 1; for (int i = n - 1; i >= 0; --i) { t = (t + (s[i] - '0') * p2) % p; ans += mp[t]; mp[t]++; p2 = p2 * 10 % p; } cout << ans; }
#include <bits/stdc++.h> using namespace std; #define pi 3.14159265359 #define int long long #define pii pair<int, int> const int mod = 1e9 + 7; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, p; string s; cin >> n >> p >> s; int ans = 0; if (p == 2 || p == 5) { for (int i = 0; i < n; ++i) if ((s[i] - '0') % p == 0) ans += i + 1; cout << ans << "\n"; return 0; } map<int, int> mp; mp[0]++; int t = 0, p2 = 1; for (int i = n - 1; i >= 0; --i) { t = (t + (s[i] - '0') * p2) % p; ans += mp[t]; mp[t]++; p2 = p2 * 10 % p; } cout << ans; }
delete
15
19
15
15
-11
p02757
C++
Runtime Error
#include <iostream> #include <map> #include <string> using namespace std; long long a[100010], ten[100010], sum[100010]; map<long long, int> mp; int main() { long long i, n, p, ans = 0; string s; cin >> n >> p >> s; if (p == 2) { for (i = 0; i < n; i++) { if ((s[i] - '0') % 2 == 0) { ans += (i + 1); } } } else if (p == 5) { for (i = 0; i < n; i++) { if ((s[i] - '0') % 5 == 0) { ans += (i + 1); } } } else { ten[0] = 1; for (i = 1; i <= n; i++) { ten[i] = (ten[i - 1] * 10) % p; } for (i = 0; i < n; i++) { long long x = s[i] - '0'; a[i] = (ten[n - 1 - i] * x) % p; } sum[0] = 0; mp[0]++; for (i = 0; i < n; i++) { sum[i + 1] = (sum[i] + a[i]) % p; ans += mp[sum[i + 1]]; mp[sum[i + 1]]++; } } cout << ans << endl; }
#include <iostream> #include <map> #include <string> using namespace std; long long a[200010], ten[200010], sum[200010]; map<long long, int> mp; int main() { long long i, n, p, ans = 0; string s; cin >> n >> p >> s; if (p == 2) { for (i = 0; i < n; i++) { if ((s[i] - '0') % 2 == 0) { ans += (i + 1); } } } else if (p == 5) { for (i = 0; i < n; i++) { if ((s[i] - '0') % 5 == 0) { ans += (i + 1); } } } else { ten[0] = 1; for (i = 1; i <= n; i++) { ten[i] = (ten[i - 1] * 10) % p; } for (i = 0; i < n; i++) { long long x = s[i] - '0'; a[i] = (ten[n - 1 - i] * x) % p; } sum[0] = 0; mp[0]++; for (i = 0; i < n; i++) { sum[i + 1] = (sum[i] + a[i]) % p; ans += mp[sum[i + 1]]; mp[sum[i + 1]]++; } } cout << ans << endl; }
replace
4
5
4
5
0
p02757
C++
Runtime Error
#include <bits/stdc++.h> #define endl "\n" using namespace std; #define ll long long #define ld long double #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repo(i, n) for (int i = 1; i < (int)(n); i++) #define pb push_back #define mp make_pair #define np next_permutation #define fi first #define se second #define all(x) (x).begin(), (x).end() #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define lb(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin()) #define ub(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin()) const ll mod = 1000000007; // const ll mod=998244353; const ld pi = acos(-1.0); const ll INF = 1LL << 61; 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; } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } ll jou(ll N, ll P) { if (P == 0) return 1; if (P % 2 == 0) { ll t = jou(N, P / 2); return t * t % mod; } return (N * jou(N, P - 1)) % mod; } // jou(n,mod-2)で逆元 // intの最大値2147483647 ≒ 2×10^9 // long longの最大値9223372036854775807 ≒ 9×10^18 //'大文字'+=32; で小文字に // cout << fixed << setprecision (20); 小数点以下20桁まで // 実行時間制約2秒では2×10^8回くらいまで計算できる int main() { cin.tie(0); ios::sync_with_stdio(false); int n, p; string s; cin >> n >> p >> s; ll ans = 0; if (p == 2) { rep(i, n) { if ((s[i] - '0') % 2 == 0) ans += i + 1; } cout << ans << endl; return 0; } if (p == 5) { rep(i, n) { if ((s[i] - '0') % 5 == 0) ans += i + 1; } cout << ans << endl; return 0; } vector<ll> q(p); q[0] = 1; ll now = 0; ll ju = 1; for (int i = n - 1; i >= 0; i--) { now += ((s[i] - '0') * ju); now %= p; q[now]++; ju = (ju * 10) % p; } rep(i, n) { ans += q[i] * (q[i] - 1) / 2; } cout << ans << endl; }
#include <bits/stdc++.h> #define endl "\n" using namespace std; #define ll long long #define ld long double #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repo(i, n) for (int i = 1; i < (int)(n); i++) #define pb push_back #define mp make_pair #define np next_permutation #define fi first #define se second #define all(x) (x).begin(), (x).end() #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define lb(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin()) #define ub(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin()) const ll mod = 1000000007; // const ll mod=998244353; const ld pi = acos(-1.0); const ll INF = 1LL << 61; 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; } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } ll jou(ll N, ll P) { if (P == 0) return 1; if (P % 2 == 0) { ll t = jou(N, P / 2); return t * t % mod; } return (N * jou(N, P - 1)) % mod; } // jou(n,mod-2)で逆元 // intの最大値2147483647 ≒ 2×10^9 // long longの最大値9223372036854775807 ≒ 9×10^18 //'大文字'+=32; で小文字に // cout << fixed << setprecision (20); 小数点以下20桁まで // 実行時間制約2秒では2×10^8回くらいまで計算できる int main() { cin.tie(0); ios::sync_with_stdio(false); int n, p; string s; cin >> n >> p >> s; ll ans = 0; if (p == 2) { rep(i, n) { if ((s[i] - '0') % 2 == 0) ans += i + 1; } cout << ans << endl; return 0; } if (p == 5) { rep(i, n) { if ((s[i] - '0') % 5 == 0) ans += i + 1; } cout << ans << endl; return 0; } vector<ll> q(p); q[0] = 1; ll now = 0; ll ju = 1; for (int i = n - 1; i >= 0; i--) { now += ((s[i] - '0') * ju); now %= p; q[now]++; ju = (ju * 10) % p; } rep(i, p) { ans += q[i] * (q[i] - 1) / 2; } cout << ans << endl; }
replace
89
90
89
90
0
p02757
C++
Runtime Error
// // Created by misclicked on 3/7/20. // #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; // #define ONLINE_JUDGE #ifdef ONLINE_JUDGE #define cerr \ if (false) \ cerr #endif int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("debug.txt", "w", stderr); #else ios_base::sync_with_stdio(false); cin.tie(); #endif ll n, p; cin >> n >> p; string s; cin >> s; ll ans = 0; if (p == 2 || p == 5) { for (int i = 0; i < n; i++) { if ((s[i] - '0') % p == 0) ans += i + 1; } } else { unordered_map<ll, ll> mape; mape[0]++; ll now = 0, p10 = 1; for (int i = n - 1; ~i; i--) { now = (now + (s[i] - '0') * p10) % p; ans += mape[now]; mape[now]++; p10 = (p10 * 10) % p; } } cout << ans << endl; }
// // Created by misclicked on 3/7/20. // #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define ONLINE_JUDGE #ifdef ONLINE_JUDGE #define cerr \ if (false) \ cerr #endif int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("debug.txt", "w", stderr); #else ios_base::sync_with_stdio(false); cin.tie(); #endif ll n, p; cin >> n >> p; string s; cin >> s; ll ans = 0; if (p == 2 || p == 5) { for (int i = 0; i < n; i++) { if ((s[i] - '0') % p == 0) ans += i + 1; } } else { unordered_map<ll, ll> mape; mape[0]++; ll now = 0, p10 = 1; for (int i = n - 1; ~i; i--) { now = (now + (s[i] - '0') * p10) % p; ans += mape[now]; mape[now]++; p10 = (p10 * 10) % p; } } cout << ans << endl; }
replace
10
11
10
11
-11
p02757
C++
Time Limit Exceeded
#ifdef DEBUG_IS_VALID #define DEB 1 #define _LIBCPP_DEBUG 0 #else #define DEB 0 #define NDEBUG #endif #include <bits/stdc++.h> using namespace std; #define ALL(g) (g).begin(), (g).end() #define REP(i, x, n) for (long long i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define RREP(i, x, n) for (long long i = x; i >= n; i--) #define rrep(i, n) RREP(i, n, 0) #define pb push_back #define fi first #define se second #pragma GCC optimize("-O3") using namespace std; #define DUMPOUT cout #define dump(...) \ if (DEB) \ DUMPOUT << " " << #__VA_ARGS__ << " :[" << __LINE__ << ":" \ << __FUNCTION__ << "]" << endl \ << " "; \ if (DEB) \ dump_func(__VA_ARGS__) template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { cout << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &st) { cout << "{"; for (auto itr = st.begin(); itr != st.end(); itr++) cout << *itr << (next(itr) != st.end() ? ", " : ""); cout << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, map<T1, T2> mp) { cout << "{"; for (auto itr = mp.begin(); itr != mp.end(); itr++) cout << "(" << (itr->first) << ", " << (itr->second) << ")" << (next(itr) != mp.end() ? "," : ""); cout << "}"; return os; } void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...); } template <class T> inline bool chmax(T &a, T const &b) { if (a >= b) return false; a = b; return true; } template <class T> inline bool chmin(T &a, T const &b) { if (a <= b) return false; a = b; return true; } using Int = long long; using ll = long long; using P = pair<Int, Int>; using vi = vector<Int>; using vvi = vector<vi>; using vp = vector<P>; using vvp = vector<vp>; const Int INF = (1 << 30) - 10; class DynamicModInt { public: static int MOD; unsigned x; DynamicModInt() : x(0) {} DynamicModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } DynamicModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } int get() const { return (int)x; } DynamicModInt &operator+=(DynamicModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } DynamicModInt &operator-=(DynamicModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } DynamicModInt &operator*=(DynamicModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } DynamicModInt &operator/=(DynamicModInt that) { return *this *= that.inverse(); } DynamicModInt operator+(DynamicModInt that) const { return DynamicModInt(*this) += that; } DynamicModInt operator-(DynamicModInt that) const { return DynamicModInt(*this) -= that; } DynamicModInt operator*(DynamicModInt that) const { return DynamicModInt(*this) *= that; } DynamicModInt operator/(DynamicModInt that) const { return DynamicModInt(*this) /= that; } DynamicModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return DynamicModInt(u); } bool operator==(DynamicModInt that) const { return x == that.x; } bool operator!=(DynamicModInt that) const { return x != that.x; } DynamicModInt operator-() const { DynamicModInt t; t.x = x == 0 ? 0 : MOD - x; return t; } }; ostream &operator<<(ostream &st, const DynamicModInt a) { st << a.get(); return st; }; DynamicModInt operator^(DynamicModInt a, unsigned long long k) { DynamicModInt r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; } // *より優先順位が高いため注意 int DynamicModInt::MOD; void set_mod(signed mod) { DynamicModInt::MOD = mod; } using mint = DynamicModInt; void solve(long long N, long long P, string S) { Int ans = 0; if (P <= 1000) { vector<Int> ary(P), nxt; rep(i, N) { dump(ary); Int ss = Int(S[i] - '0'); ary[ss % P]++; ans += ary[0]; nxt.clear(); nxt.resize(P); rep(p, P) { if (i == N - 1) break; ss = Int(S[i + 1] - '0'); nxt[(p * 10 + ss) % P] += ary[p]; } swap(ary, nxt); } } else { set_mod(P); mint inv = mint(1) / mint(10); mint co = mint(1); vector<mint> a(N), s(N + 1); rep(i, N) { a[i] = mint(int(S[i] - '0')) * co; co *= inv; } rep(i, N + 1) s[i + 1] = s[i] + a[i]; vector<Int> ary(P); rep(i, N + 1) { ans += ary[s[i].x]; ary[s[i].x]++; } } cout << ans << endl; } int main() { std::cout << std::fixed << std::setprecision(15); long long N; scanf("%lld", &N); long long P; scanf("%lld", &P); string S; cin >> S; solve(N, P, S); return 0; }
#ifdef DEBUG_IS_VALID #define DEB 1 #define _LIBCPP_DEBUG 0 #else #define DEB 0 #define NDEBUG #endif #include <bits/stdc++.h> using namespace std; #define ALL(g) (g).begin(), (g).end() #define REP(i, x, n) for (long long i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define RREP(i, x, n) for (long long i = x; i >= n; i--) #define rrep(i, n) RREP(i, n, 0) #define pb push_back #define fi first #define se second #pragma GCC optimize("-O3") using namespace std; #define DUMPOUT cout #define dump(...) \ if (DEB) \ DUMPOUT << " " << #__VA_ARGS__ << " :[" << __LINE__ << ":" \ << __FUNCTION__ << "]" << endl \ << " "; \ if (DEB) \ dump_func(__VA_ARGS__) template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { cout << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &st) { cout << "{"; for (auto itr = st.begin(); itr != st.end(); itr++) cout << *itr << (next(itr) != st.end() ? ", " : ""); cout << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, map<T1, T2> mp) { cout << "{"; for (auto itr = mp.begin(); itr != mp.end(); itr++) cout << "(" << (itr->first) << ", " << (itr->second) << ")" << (next(itr) != mp.end() ? "," : ""); cout << "}"; return os; } void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...); } template <class T> inline bool chmax(T &a, T const &b) { if (a >= b) return false; a = b; return true; } template <class T> inline bool chmin(T &a, T const &b) { if (a <= b) return false; a = b; return true; } using Int = long long; using ll = long long; using P = pair<Int, Int>; using vi = vector<Int>; using vvi = vector<vi>; using vp = vector<P>; using vvp = vector<vp>; const Int INF = (1 << 30) - 10; class DynamicModInt { public: static int MOD; unsigned x; DynamicModInt() : x(0) {} DynamicModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } DynamicModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } int get() const { return (int)x; } DynamicModInt &operator+=(DynamicModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } DynamicModInt &operator-=(DynamicModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } DynamicModInt &operator*=(DynamicModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } DynamicModInt &operator/=(DynamicModInt that) { return *this *= that.inverse(); } DynamicModInt operator+(DynamicModInt that) const { return DynamicModInt(*this) += that; } DynamicModInt operator-(DynamicModInt that) const { return DynamicModInt(*this) -= that; } DynamicModInt operator*(DynamicModInt that) const { return DynamicModInt(*this) *= that; } DynamicModInt operator/(DynamicModInt that) const { return DynamicModInt(*this) /= that; } DynamicModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return DynamicModInt(u); } bool operator==(DynamicModInt that) const { return x == that.x; } bool operator!=(DynamicModInt that) const { return x != that.x; } DynamicModInt operator-() const { DynamicModInt t; t.x = x == 0 ? 0 : MOD - x; return t; } }; ostream &operator<<(ostream &st, const DynamicModInt a) { st << a.get(); return st; }; DynamicModInt operator^(DynamicModInt a, unsigned long long k) { DynamicModInt r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; } // *より優先順位が高いため注意 int DynamicModInt::MOD; void set_mod(signed mod) { DynamicModInt::MOD = mod; } using mint = DynamicModInt; void solve(long long N, long long P, string S) { Int ans = 0; if (P <= 500) { vector<Int> ary(P), nxt; rep(i, N) { dump(ary); Int ss = Int(S[i] - '0'); ary[ss % P]++; ans += ary[0]; nxt.clear(); nxt.resize(P); rep(p, P) { if (i == N - 1) break; ss = Int(S[i + 1] - '0'); nxt[(p * 10 + ss) % P] += ary[p]; } swap(ary, nxt); } } else { set_mod(P); mint inv = mint(1) / mint(10); mint co = mint(1); vector<mint> a(N), s(N + 1); rep(i, N) { a[i] = mint(int(S[i] - '0')) * co; co *= inv; } rep(i, N + 1) s[i + 1] = s[i] + a[i]; vector<Int> ary(P); rep(i, N + 1) { ans += ary[s[i].x]; ary[s[i].x]++; } } cout << ans << endl; } int main() { std::cout << std::fixed << std::setprecision(15); long long N; scanf("%lld", &N); long long P; scanf("%lld", &P); string S; cin >> S; solve(N, P, S); return 0; }
replace
173
174
173
174
TLE
p02757
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; // この方法で関数を出せば多分できる // https://nlab.itmedia.co.jp/nl/articles/1805/04/news002.html const int MAX = 100 / 0.08; int main() { int n, p; cin >> n >> p; string s; cin >> s; if (10 % p == 0) { // pが2 or 5 ll ans = 0; // 2か5で割り切れるというのは、1桁目を見るだけで判断可能なので // 1桁目の余りが0だったらOK // 通り数は、桁数と同等なのでr+1をnまで足せばOK rep(r, n) { if ((s[r] - '0') % p == 0) { ans += r + 1; } } cout << ans << endl; return 0; } // 累積和の配列 // 各桁の余りの合計 vector<int> d(n + 1); int ten = 1; // 1桁目から計算する for (int i = n - 1; i >= 0; --i) { int a = (s[i] - '0') * ten % p; d[i] = (d[i + i] + a) % p; ten *= 10; ten %= p; } vector<int> cnt(p); ll ans = 0; for (int i = n; i >= 0; --i) { ans += cnt[d[i]]; cnt[d[i]]++; } cout << ans << 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; // この方法で関数を出せば多分できる // https://nlab.itmedia.co.jp/nl/articles/1805/04/news002.html const int MAX = 100 / 0.08; int main() { int n, p; cin >> n >> p; string s; cin >> s; if (10 % p == 0) { // pが2 or 5 ll ans = 0; // 2か5で割り切れるというのは、1桁目を見るだけで判断可能なので // 1桁目の余りが0だったらOK // 通り数は、桁数と同等なのでr+1をnまで足せばOK rep(r, n) { if ((s[r] - '0') % p == 0) { ans += r + 1; } } cout << ans << endl; return 0; } // 累積和の配列 // 各桁の余りの合計 vector<int> d(n + 1); int ten = 1; // 1桁目から計算する for (int i = n - 1; i >= 0; --i) { int a = (s[i] - '0') * ten % p; d[i] = (d[i + 1] + a) % p; ten *= 10; ten %= p; } vector<int> cnt(p); ll ans = 0; for (int i = n; i >= 0; --i) { ans += cnt[d[i]]; cnt[d[i]]++; } cout << ans << endl; return 0; }
replace
34
35
34
35
0
p02757
C++
Runtime Error
#include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define mod 1000000007 #define FOR(x, to) for (int x = 0; x < (to); x++) #define FORR(x, arr) for (auto &x : arr) #define ALL(a) (a.begin()), (a.end()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define long long long inline int rei() { int x; cin >> x; return x; } inline long rel() { long x; cin >> x; return x; } inline string res() { string x; cin >> x; return x; } //------------------------------------------------------- int A[10000]; int Mo[10001]; void Calc() { int N = rei(); int P = rei(); string S = res(); for (int i = 0; i < N; i++) { A[i] = S[i] - '0'; } if (P == 2 || P == 5) { long ans = 0; for (int i = 0; i < N; i++) { if (A[i] % P == 0) { ans += i + 1; } } cout << ans << endl; } else { int T = 1; Mo[N] = 0; for (int i = N - 1; i >= 0; i--) { Mo[i] = (Mo[i + 1] + T * A[i]) % P; T *= 10; T %= P; } sort(Mo, Mo + N + 1); long ans = 0; int c = 1; for (int i = 1; i <= N; i++) { if (Mo[i] == Mo[i - 1]) { c++; } else { ans += 1LL * c * (c - 1) / 2; c = 1; } } ans += 1LL * c * (c - 1) / 2; cout << ans << endl; } } int main(int argc, char **argv) { ios::sync_with_stdio(false), cin.tie(0); cout.tie(0); Calc(); return 0; }
#include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define mod 1000000007 #define FOR(x, to) for (int x = 0; x < (to); x++) #define FORR(x, arr) for (auto &x : arr) #define ALL(a) (a.begin()), (a.end()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define long long long inline int rei() { int x; cin >> x; return x; } inline long rel() { long x; cin >> x; return x; } inline string res() { string x; cin >> x; return x; } //------------------------------------------------------- int A[200000]; int Mo[200001]; void Calc() { int N = rei(); int P = rei(); string S = res(); for (int i = 0; i < N; i++) { A[i] = S[i] - '0'; } if (P == 2 || P == 5) { long ans = 0; for (int i = 0; i < N; i++) { if (A[i] % P == 0) { ans += i + 1; } } cout << ans << endl; } else { int T = 1; Mo[N] = 0; for (int i = N - 1; i >= 0; i--) { Mo[i] = (Mo[i + 1] + T * A[i]) % P; T *= 10; T %= P; } sort(Mo, Mo + N + 1); long ans = 0; int c = 1; for (int i = 1; i <= N; i++) { if (Mo[i] == Mo[i - 1]) { c++; } else { ans += 1LL * c * (c - 1) / 2; c = 1; } } ans += 1LL * c * (c - 1) / 2; cout << ans << endl; } } int main(int argc, char **argv) { ios::sync_with_stdio(false), cin.tie(0); cout.tie(0); Calc(); return 0; }
replace
43
45
43
45
0
p02757
C++
Time Limit Exceeded
/* * Heart beats fast * Colors and promises * How to be brave * How can I love when I am afraid to fall... */ #include <bits/stdc++.h> // #include<boost/multiprecision/cpp_int.hpp> // using namespace boost::multiprecision; using namespace std; #define int long long #define mod 1000000007 #define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define f(i, n) for (int i = 0; i < n; i++) #define fp(i, k, n) for (int i = k; i <= n; i++) #define fr(i, k, n) for (int i = k; i >= n; i--) #define pb push_back #define F first #define S second void solve() { int n, p; cin >> n >> p; string s; cin >> s; if (p == 2 || p == 5) { int ans = 0; for (int i = 0; i < n; i++) { if ((s[i] - '0') % p == 0) ans += i + 1; } cout << ans; return; } int cnt = 0; map<pair<int, int>, int> m; f(i, n) { int r = 0, pref = 0; fp(j, i, n - 1) { r = ((10 * r) + (s[j] - '0')) % p; if (m[{j, r}] == 1) break; m[{j, r}] = 1; if (r == 0) pref++; } cnt += ((pref * (pref + 1)) / 2); } cout << cnt; return; } signed main() { FAST; int T = 1; // cin >> T; while (T--) { solve(); } return 0; }
/* * Heart beats fast * Colors and promises * How to be brave * How can I love when I am afraid to fall... */ #include <bits/stdc++.h> // #include<boost/multiprecision/cpp_int.hpp> // using namespace boost::multiprecision; using namespace std; #define int long long #define mod 1000000007 #define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define f(i, n) for (int i = 0; i < n; i++) #define fp(i, k, n) for (int i = k; i <= n; i++) #define fr(i, k, n) for (int i = k; i >= n; i--) #define pb push_back #define F first #define S second void solve() { int n, p; cin >> n >> p; string s; cin >> s; if (p == 2 || p == 5) { int ans = 0; for (int i = 0; i < n; i++) { if ((s[i] - '0') % p == 0) ans += i + 1; } cout << ans; return; } int cnt = 0, r = 0; map<int, int> m; m[0] = 1; int q = 1; fr(i, n - 1, 0) { r = ((s[i] - '0') * q + r) % p; q *= 10; q %= p; cnt += m[r]; m[r]++; } cout << cnt; return; } signed main() { FAST; int T = 1; // cin >> T; while (T--) { solve(); } return 0; }
replace
34
47
34
44
TLE
p02757
C++
Runtime Error
// #pragma GCC optimize ('O3') #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef long double ld; #define mp make_pair #define f first #define s second #define pb push_back const int N = 1e5 + 5; const int M = 1560 + 5; const ll OO = 2e18; const int mod = 1e9 + 7; int n, p, pre[N]; string s; ll ans; map<int, int> m; int mul(int a, int b) { return (a * b) % p; } int pw(int b, int k) { if (!k) return 1; int res = pw(b, k >> 1); return mul(mul(res, res), (k & 1 ? b : 1)); } int modInv(int a) { return pw(a, p - 2); } void init() {} int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> p >> s; if (p == 2 || p == 5) { for (int i = 0; i < n; ++i) { if ((s[i] - '0') % p == 0) { ans += i + 1; } } } else { for (int i = 0; i < n; ++i) { pre[i] = ((i ? pre[i - 1] : 0) * 10 + (s[i] - '0')) % p; } m[0] = 1; for (int i = 0; i < n; ++i) { ans += m[mul(pre[i], modInv(pw(10, i)))]++; } } cout << ans << '\n'; return 0; }
// #pragma GCC optimize ('O3') #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef long double ld; #define mp make_pair #define f first #define s second #define pb push_back const int N = 2e5 + 5; const int M = 1560 + 5; const ll OO = 2e18; const int mod = 1e9 + 7; int n, p, pre[N]; string s; ll ans; map<int, int> m; int mul(int a, int b) { return (a * b) % p; } int pw(int b, int k) { if (!k) return 1; int res = pw(b, k >> 1); return mul(mul(res, res), (k & 1 ? b : 1)); } int modInv(int a) { return pw(a, p - 2); } void init() {} int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> p >> s; if (p == 2 || p == 5) { for (int i = 0; i < n; ++i) { if ((s[i] - '0') % p == 0) { ans += i + 1; } } } else { for (int i = 0; i < n; ++i) { pre[i] = ((i ? pre[i - 1] : 0) * 10 + (s[i] - '0')) % p; } m[0] = 1; for (int i = 0; i < n; ++i) { ans += m[mul(pre[i], modInv(pw(10, i)))]++; } } cout << ans << '\n'; return 0; }
replace
12
13
12
13
0
p02757
C++
Runtime Error
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #include <iostream> using namespace std; #define ll long long #define ld long double #define P pair<ll, ll> #define FOR(i, n, m) for (ll i = n; i < (ll)m; i++) #define FORr(i, m, n) for (ll i = n; i >= (ll)m; i--) #define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++) #define sortAl(v) sort(v.begin(), v.end()) #define sortAlr(v) \ sort(v.begin(), v.end()); \ reverse(v.begin(), v.end()) #define REV(v) reverse(v.begin(), v.end()) #define cout(n) cout << fixed << setprecision(n) #define dout(n) cout << #n << " = " << n << endl; #define v1out(v) \ FOR(j, 0, v.size()) { \ cout << v[j]; \ if (j != (ll)v.size() - 1) \ cout << " "; \ else \ cout << endl; \ } #define v2out(v) FOR(i, 0, v.size()) v1out(v[i]); int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, p; cin >> n >> p; string s; cin >> s; if (p == 2 || p == 5) return 3 / 0; REV(s); ll tmp = 0; map<ll, ll> mp; ll ans = 0; mp[0]++; ll ten = 1; FOR(i, 0, n) { tmp += ten * (s[i] - '0'); tmp %= p; ans += mp[tmp]; mp[tmp]++; ten *= 10; ten %= p; } cout << ans << endl; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #include <iostream> using namespace std; #define ll long long #define ld long double #define P pair<ll, ll> #define FOR(i, n, m) for (ll i = n; i < (ll)m; i++) #define FORr(i, m, n) for (ll i = n; i >= (ll)m; i--) #define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++) #define sortAl(v) sort(v.begin(), v.end()) #define sortAlr(v) \ sort(v.begin(), v.end()); \ reverse(v.begin(), v.end()) #define REV(v) reverse(v.begin(), v.end()) #define cout(n) cout << fixed << setprecision(n) #define dout(n) cout << #n << " = " << n << endl; #define v1out(v) \ FOR(j, 0, v.size()) { \ cout << v[j]; \ if (j != (ll)v.size() - 1) \ cout << " "; \ else \ cout << endl; \ } #define v2out(v) FOR(i, 0, v.size()) v1out(v[i]); int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, p; cin >> n >> p; string s; cin >> s; if (p == 2 || p == 5) { ll ret = 0; FOR(i, 0, s.size()) { if ((s[i] - '0') % p == 0) ret += i + 1; } cout << ret << endl; return 0; } REV(s); ll tmp = 0; map<ll, ll> mp; ll ans = 0; mp[0]++; ll ten = 1; FOR(i, 0, n) { tmp += ten * (s[i] - '0'); tmp %= p; ans += mp[tmp]; mp[tmp]++; ten *= 10; ten %= p; } cout << ans << endl; }
replace
35
37
35
44
0
p02757
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) #define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--) #define rrep(i, n) RREP(i, n - 1, 0) #define REPL(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++) #define repl(i, n) REPL(i, 0, n) #define all(v) v.begin(), v.end() #define c2i(c) ((int)c - '0') const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; int char2int(char c) { return int(c - '0'); } char s[200'009]; int f[10'009]; int main() { int n, p; scanf("%d%d%s", &n, &mod, s); if (p == 2 || p == 5) { ll ans = 0; rrep(i, n) if (c2i(s[i]) % p == 0) ans += i + 1; cout << ans << endl; return 0; } ll rem = 0; ll pow = 1; ll ans = 0; REP(i, 1, p) f[i] = 0; f[0] = 1; rrep(i, n) { rem = (rem + c2i(s[i]) * pow) % p; ans += f[rem]; f[rem]++; pow = pow * 10 % p; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) #define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--) #define rrep(i, n) RREP(i, n - 1, 0) #define REPL(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++) #define repl(i, n) REPL(i, 0, n) #define all(v) v.begin(), v.end() #define c2i(c) ((int)c - '0') const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; int char2int(char c) { return int(c - '0'); } char s[200'009]; int f[10'009]; int main() { int n, p; scanf("%d%d%s", &n, &p, s); if (p == 2 || p == 5) { ll ans = 0; rrep(i, n) if (c2i(s[i]) % p == 0) ans += i + 1; cout << ans << endl; return 0; } ll rem = 0; ll pow = 1; ll ans = 0; REP(i, 1, p) f[i] = 0; f[0] = 1; rrep(i, n) { rem = (rem + c2i(s[i]) * pow) % p; ans += f[rem]; f[rem]++; pow = pow * 10 % p; } cout << ans << endl; return 0; }
replace
19
20
19
20
-11
p02757
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define F first #define S second #define pb push_back #define sz(c) (int)c.size() #define len(c) (int)c.length() #define hcf(a, b) __gcd(a, b) #define all(c) c.begin(), c.end() #define vint vector<int> #define vLL vector<LL> #define vpint vector<pair<int, int>> #define vpLL vector<pair<LL, LL>> #define tr(container, it) \ for (typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) #define fr(n) for (int i = 0; i < n; i++) #define present(container, element) (container.find(element) != container.end()) #define cpresent(container, element) \ (find(all(container), element) != container.end()) #define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c)) #define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c)) using namespace std; typedef long long LL; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, p, prev = 0, k = 1; cin >> n >> p; string s; vector<int> a(n); cin >> s; for (int i = n - 1; i >= 0; i--) { a[i] = (k * (int)(s[i] - '0') + prev) % p; prev = a[i]; k = (k * 10) % p; } LL ans = 0; vector<int> M(n); if (p != 2 && p != 5) { for (int i = 0; i < n; i++) { if (i) { ans += M[a[i]]; } if (a[i] == 0) { ans++; } M[a[i]]++; } } else if (p == 2) { fr(n) { if ((int)(s[i] - '0') % 2 == 0) { ans += i + 1; } } } else { fr(n) { if ((int)(s[i] - '0') % 5 == 0) { ans += i + 1; } } } cout << ans << "\n"; return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define F first #define S second #define pb push_back #define sz(c) (int)c.size() #define len(c) (int)c.length() #define hcf(a, b) __gcd(a, b) #define all(c) c.begin(), c.end() #define vint vector<int> #define vLL vector<LL> #define vpint vector<pair<int, int>> #define vpLL vector<pair<LL, LL>> #define tr(container, it) \ for (typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) #define fr(n) for (int i = 0; i < n; i++) #define present(container, element) (container.find(element) != container.end()) #define cpresent(container, element) \ (find(all(container), element) != container.end()) #define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c)) #define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c)) using namespace std; typedef long long LL; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, p, prev = 0, k = 1; cin >> n >> p; string s; vector<int> a(n); cin >> s; for (int i = n - 1; i >= 0; i--) { a[i] = (k * (int)(s[i] - '0') + prev) % p; prev = a[i]; k = (k * 10) % p; } LL ans = 0; vector<int> M(p); if (p != 2 && p != 5) { for (int i = 0; i < n; i++) { if (i) { ans += M[a[i]]; } if (a[i] == 0) { ans++; } M[a[i]]++; } } else if (p == 2) { fr(n) { if ((int)(s[i] - '0') % 2 == 0) { ans += i + 1; } } } else { fr(n) { if ((int)(s[i] - '0') % 5 == 0) { ans += i + 1; } } } cout << ans << "\n"; return 0; }
replace
50
51
50
51
0
p02757
C++
Runtime Error
#include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> #define pb push_back #define mpr make_pair #define pii pair<int, int> #define pll pair<ll, ll> #define ll long long #define ld long double #define all(arr) arr.begin(), arr.end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define fo(i, l, r) for (int i = l; i <= r; i++) #define INF 1000000001 #define inf1 1000000000000000001 #define mod 1000000007 #define pie 3.14159265358979323846264338327950L #define N 100005 #define mid(l, r) l + (r - l) / 2 #define vec vector<int> #define vecl vector<ll> #define umap unordered_map<ll, ll> #define yes cout << "Yes" << endl; #define no cout << "No" << endl; #define endl "\n" using namespace std; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; ll gcd(ll a, ll b) { if (!a) return b; return gcd(b % a, a); } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } void test_case() { int n, p; cin >> n >> p; string s; cin >> s; if (10 % p == 0) { ll ans = 0; for (int i = n; i >= 1; i--) { ll val = s[i - 1] - '0'; if (val % p == 0) { ans += i; } } cout << ans << endl; return; } ll ans = 0; ll mul = 1; vecl L(n + 1, 0); L[0] = 1; ll r = 0; for (int i = n - 1; i >= 0; i--) { r = ((s[i] - '0') * mul + r) % p; ans += L[r]; L[r]++; mul = (mul * 10) % p; } cout << ans << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(20); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int t = 1; // cin >> t; while (t--) { test_case(); } }
#include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> #define pb push_back #define mpr make_pair #define pii pair<int, int> #define pll pair<ll, ll> #define ll long long #define ld long double #define all(arr) arr.begin(), arr.end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define fo(i, l, r) for (int i = l; i <= r; i++) #define INF 1000000001 #define inf1 1000000000000000001 #define mod 1000000007 #define pie 3.14159265358979323846264338327950L #define N 100005 #define mid(l, r) l + (r - l) / 2 #define vec vector<int> #define vecl vector<ll> #define umap unordered_map<ll, ll> #define yes cout << "Yes" << endl; #define no cout << "No" << endl; #define endl "\n" using namespace std; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; ll gcd(ll a, ll b) { if (!a) return b; return gcd(b % a, a); } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } void test_case() { int n, p; cin >> n >> p; string s; cin >> s; if (10 % p == 0) { ll ans = 0; for (int i = n; i >= 1; i--) { ll val = s[i - 1] - '0'; if (val % p == 0) { ans += i; } } cout << ans << endl; return; } ll ans = 0; ll mul = 1; vecl L(p + 1, 0); L[0] = 1; ll r = 0; for (int i = n - 1; i >= 0; i--) { r = ((s[i] - '0') * mul + r) % p; ans += L[r]; L[r]++; mul = (mul * 10) % p; } cout << ans << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(20); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int t = 1; // cin >> t; while (t--) { test_case(); } }
replace
61
62
61
62
0
p02757
C++
Runtime Error
/** * author : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏 * created : 2020-03-15 09:47:30 **/ #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using int64 = long long; template <class T> using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>; template <class T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) { os << '{'; size_t n = vec.size(); for (size_t i = 0; i < n; ++i) { os << vec[i]; if (i != n - 1) os << ','; } os << '}'; return os; } template <class T, class U> std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) { return os << '{' << p.first << " " << p.second << '}'; } template <class T, class U> std::ostream &operator<<(std::ostream &os, const std::map<T, U> &mp) { os << '{'; for (auto it = mp.begin(); it != mp.end(); ++it) { os << '{' << it->first << ':' << it->second << '}'; if (it != --mp.end()) os << ','; } os << '}'; return os; } template <class T> std::ostream &operator<<(std::ostream &os, const std::set<T> &st) { os << '{'; for (auto it = st.begin(); it != st.end(); ++it) { os << *it; if (it != --st.end()) os << ','; } os << '}'; return os; } template <class T> std::istream &operator>>(std::istream &is, std::vector<T> &vec) { size_t n = vec.size(); for (size_t i = 0; i < n; ++i) is >> vec[i]; return is; } #define all(_) begin(_), end(_) #define rall(_) rbegin(_), rend(_) #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__) #else #define debug(...) 1728 #endif void debug_out() { std::cerr << '\n'; } template <class Head, class... Tail> void debug_out(Head &&head, Tail &&...tail) { std::cerr << head; if (sizeof...(Tail) != 0) std::cerr << ", "; debug_out(std::forward<Tail>(tail)...); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, p; cin >> n >> p; string s; cin >> s; map<int, int> mp; int64 ans = 0; mp[0] = 1; int t = 0; int cur = 1; auto ModTen = [&](int x) -> int { return (x * 10) % p; }; if (p == 2 || p == 5) { for (int i = n - 1; i >= 0; --i) { abort(); } } else { for (int i = n - 1; i >= 0; --i) { int d = s[i] - '0'; t = (d * cur + t) % p; ans += mp[t]; ++mp[t]; cur = ModTen(cur); } } cout << ans << endl; return 0; }
/** * author : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏 * created : 2020-03-15 09:47:30 **/ #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using int64 = long long; template <class T> using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>; template <class T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) { os << '{'; size_t n = vec.size(); for (size_t i = 0; i < n; ++i) { os << vec[i]; if (i != n - 1) os << ','; } os << '}'; return os; } template <class T, class U> std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) { return os << '{' << p.first << " " << p.second << '}'; } template <class T, class U> std::ostream &operator<<(std::ostream &os, const std::map<T, U> &mp) { os << '{'; for (auto it = mp.begin(); it != mp.end(); ++it) { os << '{' << it->first << ':' << it->second << '}'; if (it != --mp.end()) os << ','; } os << '}'; return os; } template <class T> std::ostream &operator<<(std::ostream &os, const std::set<T> &st) { os << '{'; for (auto it = st.begin(); it != st.end(); ++it) { os << *it; if (it != --st.end()) os << ','; } os << '}'; return os; } template <class T> std::istream &operator>>(std::istream &is, std::vector<T> &vec) { size_t n = vec.size(); for (size_t i = 0; i < n; ++i) is >> vec[i]; return is; } #define all(_) begin(_), end(_) #define rall(_) rbegin(_), rend(_) #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__) #else #define debug(...) 1728 #endif void debug_out() { std::cerr << '\n'; } template <class Head, class... Tail> void debug_out(Head &&head, Tail &&...tail) { std::cerr << head; if (sizeof...(Tail) != 0) std::cerr << ", "; debug_out(std::forward<Tail>(tail)...); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, p; cin >> n >> p; string s; cin >> s; map<int, int> mp; int64 ans = 0; mp[0] = 1; int t = 0; int cur = 1; auto ModTen = [&](int x) -> int { return (x * 10) % p; }; if (p == 2) { for (int i = 0; i < n; ++i) { if (s[i] == '0' || s[i] == '2' || s[i] == '4' || s[i] == '6' || s[i] == '8') { ans += (1 + i); } } } else if (p == 5) { for (int i = 0; i < n; ++i) { if (s[i] == '0' || s[i] == '5') { ans += (1 + i); } } } else { for (int i = n - 1; i >= 0; --i) { int d = s[i] - '0'; t = (d * cur + t) % p; ans += mp[t]; ++mp[t]; cur = ModTen(cur); } } cout << ans << endl; return 0; }
replace
115
118
115
128
0
p02757
C++
Time Limit Exceeded
#include <bits/stdc++.h> using ll = long long int; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, p, i, j; string s; cin >> n >> p >> s; if (p < 10) { while (true) ; } vector<int> M(p); M[0] = 1; int sum = 0; int power = 1; ll ans = 0; for (i = n - 1; i >= 0; --i) { sum += ((s[i] - '0') * power) % p; sum %= p; ans += M[sum]; M[sum]++; power *= 10; power %= p; } cout << ans; return 0; }
#include <bits/stdc++.h> using ll = long long int; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, p, i, j; string s; cin >> n >> p >> s; if (p == 2) { ll ans = 0; for (i = 0; i < n; ++i) { if ((s[i] - '0') % 2 == 0) { ans += (i + 1); } } cout << ans; return 0; } if (p == 5) { ll ans = 0; for (i = 0; i < n; ++i) { if ((s[i] - '0') % 5 == 0) { ans += (i + 1); } } cout << ans; return 0; } vector<int> M(p); M[0] = 1; int sum = 0; int power = 1; ll ans = 0; for (i = n - 1; i >= 0; --i) { sum += ((s[i] - '0') * power) % p; sum %= p; ans += M[sum]; M[sum]++; power *= 10; power %= p; } cout << ans; return 0; }
replace
13
16
13
33
TLE
p02757
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, P; cin >> N >> P; string S; cin >> S; // Pが2か5の場合 if (P == 2 || P == 5) { long long answer = 0; for (int i = 0; i < N; i++) { if ((S[i] - '0') % P == 0) { answer += i + 1; } } cout << answer << endl; return 0; } // Pが2か5以外の場合 vector<int> d(N + 1); int count = 1; for (int i = N - 1; 0 <= i; i--) { int a = (S[i] - '0') * count % P; d[i] = (d[i + 1] + a) % P; count *= 10; count %= P; } vector<int> sum(P); long long answer = 0; for (int i = 0; i < N + 1; i--) { answer += sum[d[i]]; sum[d[i]]++; } cout << answer << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N, P; cin >> N >> P; string S; cin >> S; // Pが2か5の場合 if (P == 2 || P == 5) { long long answer = 0; for (int i = 0; i < N; i++) { if ((S[i] - '0') % P == 0) { answer += i + 1; } } cout << answer << endl; return 0; } // Pが2か5以外の場合 vector<int> d(N + 1); int count = 1; for (int i = N - 1; 0 <= i; i--) { int a = (S[i] - '0') * count % P; d[i] = (d[i + 1] + a) % P; count *= 10; count %= P; } vector<int> sum(P); long long answer = 0; for (int i = 0; i < N + 1; i++) { answer += sum[d[i]]; sum[d[i]]++; } cout << answer << endl; return 0; }
replace
35
36
35
36
-11
p02757
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; typedef long long ll; struct int_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; struct pair_hash { size_t operator()(pair<int, int> x) const { return int_hash{}(x.first) ^ (int_hash{}(x.second) << 16); } }; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1000000007; const ll INF = numeric_limits<ll>::max(); const int inf = 1e7; const int MX = 100001; // check the limits, dummy int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, p; cin >> n >> p; string s; cin >> s; ll ans = 0; if (p == 2 || p == 5) { for (int i = 0, cur = 0; i < n; i++) if ((s[i] - '0') % p == 0) ans += (i + 1); } else { unordered_map<int, int> mp{{0, 1}}; for (int i = n - 1, cur = 0, b = 1; i >= 0; i++) { cur = ((b * (s[i] - '0')) + cur) % p; ans += mp[cur]; mp[cur]++; b = (b * 10) % p; } } cout << ans << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; struct int_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; struct pair_hash { size_t operator()(pair<int, int> x) const { return int_hash{}(x.first) ^ (int_hash{}(x.second) << 16); } }; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1000000007; const ll INF = numeric_limits<ll>::max(); const int inf = 1e7; const int MX = 100001; // check the limits, dummy int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, p; cin >> n >> p; string s; cin >> s; ll ans = 0; if (p == 2 || p == 5) { for (int i = 0, cur = 0; i < n; i++) if ((s[i] - '0') % p == 0) ans += (i + 1); } else { unordered_map<int, int> mp{{0, 1}}; for (int i = n - 1, cur = 0, b = 1; i >= 0; i--) { cur = ((b * (s[i] - '0')) + cur) % p; ans += mp[cur]; mp[cur]++; b = (b * 10) % p; } } cout << ans << endl; }
replace
52
53
52
53
-11
p02757
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 100000 + 5; int n, m, p, blo, len, id[maxn], cnt[maxn]; long long tmp, no[maxn], mp[maxn], ans[maxn], sum[maxn], power[maxn]; char s[maxn]; struct M { int l, r, num; friend bool operator<(M a, M b) { if (id[a.l] != id[b.l]) return id[a.l] < id[b.l]; return a.r < b.r; } } q[maxn]; inline int read(void) { char ch = getchar(); int x = 0; while (!(ch >= '0' && ch <= '9')) ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x; } inline void change(int x, int val) { x = no[x]; if (cnt[x] > 1) tmp -= 1LL * cnt[x] * (cnt[x] - 1) / 2; cnt[x] += val; if (cnt[x] > 1) tmp += 1LL * cnt[x] * (cnt[x] - 1) / 2; } inline int check(char a) { int x = a - '0'; if (p == 2) { if (x & 1) return 0; return 1; } else if (p == 5) { if (x == 0 || x == 5) return 1; return 0; } return 0; } inline void solve1(void) { for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + check(s[i]) * i, no[i] = no[i - 1] + check(s[i]); for (int i = 1, l, r; i <= m; i++) l = 1, r = n, printf("%lld\n", sum[r] - sum[l - 1] - 1LL * (no[r] - no[l - 1]) * (l - 1)); } signed main(void) { n = read(), p = read(), scanf("%s", s + 1), m = 1, n = strlen(s + 1); blo = sqrt(n); if (p == 2 || p == 5) return solve1(), 0; for (int i = 1; i <= n; i++) id[i] = (i - 1) / blo + 1; power[n] = 1; for (int i = 1; i <= m; i++) q[i].l = 1, q[i].r = n + 1, q[i].num = i; for (int i = n - 1; i >= 1; i--) power[i] = 1LL * power[i + 1] * 10 % p; for (int i = n; i >= 1; i--) no[i] = (no[i + 1] + 1LL * power[i] * (s[i] - '0') % p) % p, mp[i] = no[i]; mp[n + 1] = 0; sort(mp + 1, mp + 1 + n + 1); len = unique(mp + 1, mp + n + 1 + 1) - mp - 1; sort(q + 1, q + m + 1); for (int i = 1; i <= n + 1; i++) no[i] = lower_bound(mp + 1, mp + len + 1, no[i]) - mp; for (int i = 1, l = 1, r = 0; i <= m; i++) { for (; l < q[i].l; l++) change(l, -1); for (; l > q[i].l; l--) change(l - 1, 1); for (; r < q[i].r; r++) change(r + 1, 1); for (; r > q[i].r; r--) change(r, -1); ans[q[i].num] = tmp; } for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 200000 + 5; int n, m, p, blo, len, id[maxn], cnt[maxn]; long long tmp, no[maxn], mp[maxn], ans[maxn], sum[maxn], power[maxn]; char s[maxn]; struct M { int l, r, num; friend bool operator<(M a, M b) { if (id[a.l] != id[b.l]) return id[a.l] < id[b.l]; return a.r < b.r; } } q[maxn]; inline int read(void) { char ch = getchar(); int x = 0; while (!(ch >= '0' && ch <= '9')) ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x; } inline void change(int x, int val) { x = no[x]; if (cnt[x] > 1) tmp -= 1LL * cnt[x] * (cnt[x] - 1) / 2; cnt[x] += val; if (cnt[x] > 1) tmp += 1LL * cnt[x] * (cnt[x] - 1) / 2; } inline int check(char a) { int x = a - '0'; if (p == 2) { if (x & 1) return 0; return 1; } else if (p == 5) { if (x == 0 || x == 5) return 1; return 0; } return 0; } inline void solve1(void) { for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + check(s[i]) * i, no[i] = no[i - 1] + check(s[i]); for (int i = 1, l, r; i <= m; i++) l = 1, r = n, printf("%lld\n", sum[r] - sum[l - 1] - 1LL * (no[r] - no[l - 1]) * (l - 1)); } signed main(void) { n = read(), p = read(), scanf("%s", s + 1), m = 1, n = strlen(s + 1); blo = sqrt(n); if (p == 2 || p == 5) return solve1(), 0; for (int i = 1; i <= n; i++) id[i] = (i - 1) / blo + 1; power[n] = 1; for (int i = 1; i <= m; i++) q[i].l = 1, q[i].r = n + 1, q[i].num = i; for (int i = n - 1; i >= 1; i--) power[i] = 1LL * power[i + 1] * 10 % p; for (int i = n; i >= 1; i--) no[i] = (no[i + 1] + 1LL * power[i] * (s[i] - '0') % p) % p, mp[i] = no[i]; mp[n + 1] = 0; sort(mp + 1, mp + 1 + n + 1); len = unique(mp + 1, mp + n + 1 + 1) - mp - 1; sort(q + 1, q + m + 1); for (int i = 1; i <= n + 1; i++) no[i] = lower_bound(mp + 1, mp + len + 1, no[i]) - mp; for (int i = 1, l = 1, r = 0; i <= m; i++) { for (; l < q[i].l; l++) change(l, -1); for (; l > q[i].l; l--) change(l - 1, 1); for (; r < q[i].r; r++) change(r + 1, 1); for (; r > q[i].r; r--) change(r, -1); ans[q[i].num] = tmp; } for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]); return 0; }
replace
6
7
6
7
0
p02757
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <queue> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) using ll = long long; using namespace std; int main() { int n, p; cin >> n >> p; string s; cin >> s; if (10 % p == 0) { ll ans = 0; rep(r, n) { if ((s[r] - '0') % p == 0) { ans += r + 1; } } cout << ans << endl; return 0; } vector<int> d(n + 1); int ten = 1; for (int i = n - 1; i >= 0; --i) { int a = (s[i] - '0') * ten % p; d[i] = (d[i + 1] + a) % p; ten *= 10; } vector<int> cnt(p); ll ans = 0; for (int i = n; i >= 0; --i) { ans += cnt[d[i]]; cnt[d[i]]++; } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <queue> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) using ll = long long; using namespace std; int main() { int n, p; cin >> n >> p; string s; cin >> s; if (10 % p == 0) { ll ans = 0; rep(r, n) { if ((s[r] - '0') % p == 0) { ans += r + 1; } } cout << ans << endl; return 0; } vector<int> d(n + 1); int ten = 1; for (int i = n - 1; i >= 0; --i) { int a = (s[i] - '0') * ten % p; d[i] = (d[i + 1] + a) % p; ten *= 10; ten %= p; } vector<int> cnt(p); ll ans = 0; for (int i = n; i >= 0; --i) { ans += cnt[d[i]]; cnt[d[i]]++; } cout << ans << endl; return 0; }
insert
33
33
33
34
0
p02757
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long long LL; typedef pair<LL, LL> P; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } const LL mod = 1000000007; const LL LINF = 1LL << 62; const int INF = 1 << 30; int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; int main() { int n, p; cin >> n; cin >> p; string s; cin >> s; LL ans = 0; if (p == 2 || p == 5) { int dp[n + 1][p]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { dp[i + 1][(s[i] - '0') % p]++; for (int k = 0; k < p; k++) { int nx = (p + 10 * k + s[i] - '0') % p; dp[i + 1][nx] += dp[i][k]; } ans += dp[i + 1][0]; } } else { map<LL, LL> ma; int now = 0; ma[0]++; vector<LL> t(n, 1); for (int i = 0; i < n; i++) { t[i + 1] = t[i] * 10 % p; } for (int i = n - 1; i >= 0; i--) { now = (now + t[n - 1 - i] * (s[i] - '0')) % p; ans += ma[now]++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long long LL; typedef pair<LL, LL> P; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } const LL mod = 1000000007; const LL LINF = 1LL << 62; const int INF = 1 << 30; int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; int main() { int n, p; cin >> n; cin >> p; string s; cin >> s; LL ans = 0; if (p == 2 || p == 5) { int dp[n + 1][p]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { dp[i + 1][(s[i] - '0') % p]++; for (int k = 0; k < p; k++) { int nx = (p + 10 * k + s[i] - '0') % p; dp[i + 1][nx] += dp[i][k]; } ans += dp[i + 1][0]; } } else { map<LL, LL> ma; int now = 0; ma[0]++; vector<LL> t(n + 1, 1); for (int i = 0; i < n; i++) { t[i + 1] = t[i] * 10 % p; } for (int i = n - 1; i >= 0; i--) { now = (now + t[n - 1 - i] * (s[i] - '0')) % p; ans += ma[now]++; } } cout << ans << endl; return 0; }
replace
54
55
54
55
0
p02757
Python
Time Limit Exceeded
N, P = map(int, input().split()) S = [int(_) for _ in list(input())] if P == 2 or P == 5: ans = 0 for n in range(N): if S[n] % P == 0: ans += n + 1 print(ans) else: S = [(S[n] * 10 ** (N - n - 1)) % P for n in range(N)] S.reverse() T = [0] for s in S: T.append((T[-1] + s) % P) lst = {i: 0 for i in range(P)} ans = 0 for t in T: ans += lst[t] lst[t] += 1 print(ans)
N, P = map(int, input().split()) S = [int(_) for _ in list(input())] if P == 2 or P == 5: ans = 0 for n in range(N): if S[n] % P == 0: ans += n + 1 print(ans) else: S = [(S[n] * pow(10, N - n - 1, P)) % P for n in range(N)] S.reverse() T = [0] for s in S: T.append((T[-1] + s) % P) lst = {i: 0 for i in range(P)} ans = 0 for t in T: ans += lst[t] lst[t] += 1 print(ans)
replace
9
10
9
10
TLE
p02757
Python
Time Limit Exceeded
from collections import Counter n, p = map(int, input().split()) s = input() if p == 2 or p == 5: ans = 0 for i, e in enumerate(s, 1): if int(e) % p == 0: ans += i print(ans) else: li = [0] for i, e in enumerate(s[::-1], 1): li.append((li[-1] + int(e) * 10**i) % p) c = Counter(li) ans = 0 for v in c.values(): ans += v * (v - 1) // 2 print(ans)
from collections import Counter n, p = map(int, input().split()) s = input() if p == 2 or p == 5: ans = 0 for i, e in enumerate(s, 1): if int(e) % p == 0: ans += i print(ans) else: li = [0] for i, e in enumerate(s[::-1], 1): li.append((li[-1] + int(e) * pow(10, i, p)) % p) c = Counter(li) ans = 0 for v in c.values(): ans += v * (v - 1) // 2 print(ans)
replace
16
17
16
17
TLE
p02757
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <complex> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; #define rep(i, n) for (int i = 0; i < (n); i++) #define revrep(i, n) for (int i = (n)-1; i >= 0; i--) #define pb push_back #define f first #define s second #define chmin(x, y) x = min(x, y); #define chmax(x, y) x = max(x, y); #define sz(x) ((int)(x).size()) // const ll INFL = LLONG_MAX;//10^18 = 2^60 const ll INFL = 1LL << 60; // const int INF = INT_MAX; const int INF = 1 << 30; // 10^9 ll MOD = 1000000007; // const ll MOD = 998244353; double EPS = 1e-10; vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0}; vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; void pres(double A) { printf("%.12lf\n", A); } void BinarySay(ll x, ll y = 60) { rep(i, y) cout << (x >> (y - 1 - i) & 1); cout << endl; } ll cnt_bit(ll x) { return __builtin_popcountll(x); } ll pow_long(ll x, ll k) { ll res = 1; while (k > 0) { if (k % 2) res *= x; x *= x; k /= 2; } return res; } ll pow_mod(ll x, ll k) { x %= MOD; ll res = 1; while (k > 0) { if (k % 2) { res *= x; res %= MOD; } x *= x; x %= MOD; k /= 2; } return res; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll x, ll y) { ll res = x / gcd(x, y); res *= y; return res; }; ll n; string s; ll rui[200010]; ll dp[20][200010]; void sub() { rep(i, n) { dp[i + 1][(s[i] - '0') % MOD]++; rep(j, MOD) { dp[i + 1][(j * 10 + s[i] - '0') % MOD] += dp[i][j]; } } ll ans = 0; rep(i, n + 1) { ans += dp[i][0]; } cout << ans << endl; } void solve() { cin >> n >> MOD; cin >> s; if (MOD < 20) { sub(); return; } rep(i, n) { rui[i + 1] = rui[i] + (s[i] - '0') * pow_mod(10, n - i - 1); rui[i + 1] %= MOD; } vector<ll> cnt(MOD, 0); rep(i, n + 1) { if (i == 0) continue; cnt[rui[i]]++; } ll ans = 0; rep(i, n) { ans += cnt[rui[i]]; cnt[rui[i + 1]]--; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); solve(); }
#include <algorithm> #include <bitset> #include <climits> #include <complex> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; #define rep(i, n) for (int i = 0; i < (n); i++) #define revrep(i, n) for (int i = (n)-1; i >= 0; i--) #define pb push_back #define f first #define s second #define chmin(x, y) x = min(x, y); #define chmax(x, y) x = max(x, y); #define sz(x) ((int)(x).size()) // const ll INFL = LLONG_MAX;//10^18 = 2^60 const ll INFL = 1LL << 60; // const int INF = INT_MAX; const int INF = 1 << 30; // 10^9 ll MOD = 1000000007; // const ll MOD = 998244353; double EPS = 1e-10; vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0}; vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; void pres(double A) { printf("%.12lf\n", A); } void BinarySay(ll x, ll y = 60) { rep(i, y) cout << (x >> (y - 1 - i) & 1); cout << endl; } ll cnt_bit(ll x) { return __builtin_popcountll(x); } ll pow_long(ll x, ll k) { ll res = 1; while (k > 0) { if (k % 2) res *= x; x *= x; k /= 2; } return res; } ll pow_mod(ll x, ll k) { x %= MOD; ll res = 1; while (k > 0) { if (k % 2) { res *= x; res %= MOD; } x *= x; x %= MOD; k /= 2; } return res; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll x, ll y) { ll res = x / gcd(x, y); res *= y; return res; }; ll n; string s; ll rui[200010]; ll dp[200010][20]; void sub() { rep(i, n) { dp[i + 1][(s[i] - '0') % MOD]++; rep(j, MOD) { dp[i + 1][(j * 10 + s[i] - '0') % MOD] += dp[i][j]; } } ll ans = 0; rep(i, n + 1) { ans += dp[i][0]; } cout << ans << endl; } void solve() { cin >> n >> MOD; cin >> s; if (MOD < 20) { sub(); return; } rep(i, n) { rui[i + 1] = rui[i] + (s[i] - '0') * pow_mod(10, n - i - 1); rui[i + 1] %= MOD; } vector<ll> cnt(MOD, 0); rep(i, n + 1) { if (i == 0) continue; cnt[rui[i]]++; } ll ans = 0; rep(i, n) { ans += cnt[rui[i]]; cnt[rui[i + 1]]--; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); solve(); }
replace
90
91
90
91
0
p02757
Python
Time Limit Exceeded
n, p = map(int, input().split()) s = input() if p == 2 or p == 5: ans = 0 for i in range(n): if int(s[i]) % p == 0: ans += i + 1 else: s2 = s[::-1] rems = [0] tmp = 0 tmp2 = 1 for i in range(n): tmp += int(s2[i]) * tmp2 tmp %= p tmp2 *= 10 rems.append(tmp) from collections import Counter counter = Counter(rems) ans = 0 for key in counter.keys(): v = counter[key] ans += v * (v - 1) // 2 print(ans)
n, p = map(int, input().split()) s = input() if p == 2 or p == 5: ans = 0 for i in range(n): if int(s[i]) % p == 0: ans += i + 1 else: s2 = s[::-1] rems = [0] tmp = 0 tmp2 = 1 for i in range(n): tmp += int(s2[i]) * tmp2 tmp %= p tmp2 *= 10 tmp2 %= p rems.append(tmp) from collections import Counter counter = Counter(rems) ans = 0 for key in counter.keys(): v = counter[key] ans += v * (v - 1) // 2 print(ans)
insert
17
17
17
18
TLE
p02757
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, p; string s; cin >> n >> p >> s; int ans = 0; unordered_map<int, int> m; if (p <= 1000) { for (int i = 0; i < n; ++i) { // Just s[i] int use = (s[i] - '0') % p; if (use == 0) { ++ans; } // New map unordered_map<int, int> nm; for (auto it : m) { int r1 = (10 * it.first + use) % p; if (r1 == 0) { ans += it.second; } nm[r1] += it.second; } ++nm[use]; m = nm; } cout << ans; } else { vector<int> pre(n); int ten = 1; for (int i = n - 1; i >= 0; --i) { pre[i] = (ten * (s[i] - '0')) % p; ten = (ten * 10) % p; } for (int i = 1; i < n; ++i) pre[i] = (pre[i] + pre[i - 1]) % p; m[0] = 1; for (int i = 0; i < n; ++i) { ans += m[pre[i]]; ++m[pre[i]]; } cout << ans; } return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, p; string s; cin >> n >> p >> s; int ans = 0; unordered_map<int, int> m; if (p <= 100) { for (int i = 0; i < n; ++i) { // Just s[i] int use = (s[i] - '0') % p; if (use == 0) { ++ans; } // New map unordered_map<int, int> nm; for (auto it : m) { int r1 = (10 * it.first + use) % p; if (r1 == 0) { ans += it.second; } nm[r1] += it.second; } ++nm[use]; m = nm; } cout << ans; } else { vector<int> pre(n); int ten = 1; for (int i = n - 1; i >= 0; --i) { pre[i] = (ten * (s[i] - '0')) % p; ten = (ten * 10) % p; } for (int i = 1; i < n; ++i) pre[i] = (pre[i] + pre[i - 1]) % p; m[0] = 1; for (int i = 0; i < n; ++i) { ans += m[pre[i]]; ++m[pre[i]]; } cout << ans; } return 0; }
replace
13
14
13
14
TLE
p02757
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> // std::numeric_limits<int>::max(), min() // std::setprecision(X) // std::islower(), std::isupper(), std::tolower, std::toupper // std::accumulate(m.begin(), m.end(), 0LL); // std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less); // std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt, // less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*")) // ) std::cout << "YES" << std::endl; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } typedef long long ll; typedef std::vector<int> vi; typedef std::vector<std::vector<int>> vvi; ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } int main(void) { std::cin.tie(0); std::ios::sync_with_stdio(false); ll N, P; std::cin >> N >> P; std::string S; std::cin >> S; ll ans = 0; if (P == 2 || P == 5) { rep(i, N) if ((S[i] - '0') % P == 0) ans += 1LL * (i + 1); } else { std::vector<ll> dp(N); ll d = 1, x = 0; for (int i = N - 1; i >= 0; i--) { x += (S[i] - '0') * d; x %= P; d *= 10; d %= P; ans += dp[x]; dp[x]++; if (x == 0) ans++; } } std::cout << ans << std::endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> // std::numeric_limits<int>::max(), min() // std::setprecision(X) // std::islower(), std::isupper(), std::tolower, std::toupper // std::accumulate(m.begin(), m.end(), 0LL); // std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less); // std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt, // less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*")) // ) std::cout << "YES" << std::endl; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } typedef long long ll; typedef std::vector<int> vi; typedef std::vector<std::vector<int>> vvi; ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } int main(void) { std::cin.tie(0); std::ios::sync_with_stdio(false); ll N, P; std::cin >> N >> P; std::string S; std::cin >> S; ll ans = 0; if (P == 2 || P == 5) { rep(i, N) if ((S[i] - '0') % P == 0) ans += 1LL * (i + 1); } else { std::vector<ll> dp(2e5 + 7); ll d = 1, x = 0; for (int i = N - 1; i >= 0; i--) { x += (S[i] - '0') * d; x %= P; d *= 10; d %= P; ans += dp[x]; dp[x]++; if (x == 0) ans++; } } std::cout << ans << std::endl; return 0; }
replace
70
71
70
71
0
p02757
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> ii; typedef tuple<ll, ll, ll> iii; typedef vector<ll> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<vi> vvi; typedef vector<vii> vvii; #define REP(i, n) for (ll i = 0; i < n; ++i) #define REPR(i, n) for (ll i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define FORR(i, m, n) for (ll i = n - 1; i >= m; --i) #define FORE(x, xs) for (const auto &x : xs) #define ALL(v) v.begin(), v.end() #define CHMIN(x, y) x = min(x, y) #define CHMAX(x, y) x = max(x, y) const int MAX = 1010; int N, P; string S; ll sub() { ll ans = 0; REP(i, N) { if ((S[i] - '0') % P == 0) ans += i + 1; } return ans; } ll solve() { if (P == 2 || P == 5) return sub(); vi count(MAX); ll x = 0, k = 1; REPR(i, N) { x += k * (S[i] - '0'); k = k * 10 % P; x %= P; count[x]++; } ll ans = count[0]; REP(i, MAX) { ans += count[i] * (count[i] - 1) / 2; } return ans; } int main() { cin >> N >> P >> S; cout << solve() << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> ii; typedef tuple<ll, ll, ll> iii; typedef vector<ll> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<vi> vvi; typedef vector<vii> vvii; #define REP(i, n) for (ll i = 0; i < n; ++i) #define REPR(i, n) for (ll i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define FORR(i, m, n) for (ll i = n - 1; i >= m; --i) #define FORE(x, xs) for (const auto &x : xs) #define ALL(v) v.begin(), v.end() #define CHMIN(x, y) x = min(x, y) #define CHMAX(x, y) x = max(x, y) const int MAX = 10010; int N, P; string S; ll sub() { ll ans = 0; REP(i, N) { if ((S[i] - '0') % P == 0) ans += i + 1; } return ans; } ll solve() { if (P == 2 || P == 5) return sub(); vi count(MAX); ll x = 0, k = 1; REPR(i, N) { x += k * (S[i] - '0'); k = k * 10 % P; x %= P; count[x]++; } ll ans = count[0]; REP(i, MAX) { ans += count[i] * (count[i] - 1) / 2; } return ans; } int main() { cin >> N >> P >> S; cout << solve() << endl; }
replace
21
22
21
22
0
p02757
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; static const double EPS = 1e-9; template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; } template <class T> void amin(T &a, T v) { if (a > v) a = v; } template <class T> void amax(T &a, T v) { if (a < v) a = v; } int ROUND(double x) { return (int)(x + 0.5); } bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; } bool ISEQUAL(double x, double y) { return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y))); } double SQSUM(double x, double y) { return x * x + y * y; } template <class T> vector<T> make_vector(int n, T t) { return vector<T>(n, t); } template <class... Ts> auto make_vector(int n, Ts... ts) { return vector<decltype(make_vector(ts...))>(n, make_vector(ts...)); } #define PI (acos(-1)) #define ARRAY_NUM(a) (sizeof(a) / sizeof(a[0])) #define NG (-1) #define BIG ((int)1e9 + 10) #define BIGLL ((ll)4e18) #define SZ(a) ((int)(a).size()) #define SQ(a) ((a) * (a)) #define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i) #define ALL(x) (x).begin(), (x).end() #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl #define BitSet(arg, posn) ((arg) |= (1LL << (posn))) #define BitClr(arg, posn) ((arg) &= ~(1LL << (posn))) #define BitFlp(arg, posn) ((arg) ^= (1LL << (posn))) #define IsBit(arg, posn) static_cast<bool>((arg) & (1LL << (posn))) // #define DEBUG(x) cerr<<#x<<": "<<(x)<<endl // #define DEBUG_VEC(v) cerr<<#v<<": ";REP(__i,(v).size())cerr<<((v)[__i])<<", // ";cerr<<endl typedef unsigned long long ull; typedef long long ll; struct in { int n; in() {} in(int n_) : n(n_){}; template <class T> operator T() { T ret; cin >> ret; return ret; } template <class T> operator vector<T>() { vector<T> ret(n); for (int i = 0; i < n; i++) cin >> ret[i]; return ret; } }; // modint: mod 計算を int を扱うように扱える構造体 template <int MOD> struct ModInt { long long val; constexpr ModInt(long long v = 0) noexcept : val(v % MOD) { if (val < 0) v += MOD; } constexpr int getmod() { return MOD; } constexpr ModInt operator-() const noexcept { return val ? MOD - val : 0; } constexpr ModInt operator+(const ModInt &r) const noexcept { return ModInt(*this) += r; } constexpr ModInt operator-(const ModInt &r) const noexcept { return ModInt(*this) -= r; } constexpr ModInt operator*(const ModInt &r) const noexcept { return ModInt(*this) *= r; } constexpr ModInt operator/(const ModInt &r) const noexcept { return ModInt(*this) /= r; } constexpr ModInt &operator+=(const ModInt &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr ModInt &operator-=(const ModInt &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr ModInt &operator*=(const ModInt &r) noexcept { val = val * r.val % MOD; return *this; } constexpr ModInt &operator/=(const ModInt &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const ModInt &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const ModInt &r) const noexcept { return this->val != r.val; } friend ostream &operator<<(ostream &os, const ModInt<MOD> &x) noexcept { return os << x.val; } friend istream &operator>>(istream &is, ModInt<MOD> &x) noexcept { return is >> x.val; } friend constexpr ModInt<MOD> modpow(const ModInt<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // 二項係数ライブラリ template <class T> struct BiCoef { constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : mFact(n, 1), mInv(n, 1), mFInv(n, 1) { Init(n); } constexpr void Init(int n) noexcept { mFact.assign(n, 1), mInv.assign(n, 1), mFInv.assign(n, 1); int factMod = mFact[0].getmod(); for (int i = 2; i < n; i++) { mFact[i] = mFact[i - 1] * i; mInv[i] = -mInv[factMod % i] * (factMod / i); mFInv[i] = mFInv[i - 1] * mInv[i]; } } constexpr T Fact(int n) const noexcept { if (n < 0) return 0; return mFact[n]; } constexpr T Comb(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return mFact[n] * mFInv[k] * mFInv[n - k]; } constexpr T Perm(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return mFact[n] * mFInv[k] * mFInv[n - k]; } constexpr T Homo(int n, int r) const { if (n < 0 || r < 0) return 0; return r == 0 ? 1 : Comb(n + r - 1, r); } constexpr T inv(int n) const noexcept { if (n < 0) return 0; return mInv[n]; } constexpr T finv(int n) const noexcept { if (n < 0) return 0; return mFInv[n]; } vector<T> mFact; vector<T> mInv; vector<T> mFInv; }; const int MOD = 1000000007; using mint = ModInt<MOD>; // BiCoef<mint> bc(2*1024*1024); // 2M * 3 * 8B(ll) = 48MB (0.1sec) int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll ret = 0; ll N, P; cin >> N >> P; string s; cin >> s; /* ll basemod[10005] = {}; basemod[SZ(s) - 1] = 1 % P; for (int i = SZ(s) - 2; i >= 0; --i) { basemod[i] = (basemod[i+1] * 10) % P; } */ ll rem[2][10005] = {}; int now = 0; for (int i = 0; i < SZ(s); ++i) { const int next = 1 - now; for (int k = 0; k < P; k++) { rem[next][k] = 0; } int v = s[i] - '0'; for (int k = 0; k < P; ++k) { rem[next][(k * 10 + v) % P] += rem[now][k]; } rem[next][v % P]++; now = next; ret += rem[now][0]; } cout << setprecision(20) << ret << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; static const double EPS = 1e-9; template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; } template <class T> void amin(T &a, T v) { if (a > v) a = v; } template <class T> void amax(T &a, T v) { if (a < v) a = v; } int ROUND(double x) { return (int)(x + 0.5); } bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; } bool ISEQUAL(double x, double y) { return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y))); } double SQSUM(double x, double y) { return x * x + y * y; } template <class T> vector<T> make_vector(int n, T t) { return vector<T>(n, t); } template <class... Ts> auto make_vector(int n, Ts... ts) { return vector<decltype(make_vector(ts...))>(n, make_vector(ts...)); } #define PI (acos(-1)) #define ARRAY_NUM(a) (sizeof(a) / sizeof(a[0])) #define NG (-1) #define BIG ((int)1e9 + 10) #define BIGLL ((ll)4e18) #define SZ(a) ((int)(a).size()) #define SQ(a) ((a) * (a)) #define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i) #define ALL(x) (x).begin(), (x).end() #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl #define BitSet(arg, posn) ((arg) |= (1LL << (posn))) #define BitClr(arg, posn) ((arg) &= ~(1LL << (posn))) #define BitFlp(arg, posn) ((arg) ^= (1LL << (posn))) #define IsBit(arg, posn) static_cast<bool>((arg) & (1LL << (posn))) // #define DEBUG(x) cerr<<#x<<": "<<(x)<<endl // #define DEBUG_VEC(v) cerr<<#v<<": ";REP(__i,(v).size())cerr<<((v)[__i])<<", // ";cerr<<endl typedef unsigned long long ull; typedef long long ll; struct in { int n; in() {} in(int n_) : n(n_){}; template <class T> operator T() { T ret; cin >> ret; return ret; } template <class T> operator vector<T>() { vector<T> ret(n); for (int i = 0; i < n; i++) cin >> ret[i]; return ret; } }; // modint: mod 計算を int を扱うように扱える構造体 template <int MOD> struct ModInt { long long val; constexpr ModInt(long long v = 0) noexcept : val(v % MOD) { if (val < 0) v += MOD; } constexpr int getmod() { return MOD; } constexpr ModInt operator-() const noexcept { return val ? MOD - val : 0; } constexpr ModInt operator+(const ModInt &r) const noexcept { return ModInt(*this) += r; } constexpr ModInt operator-(const ModInt &r) const noexcept { return ModInt(*this) -= r; } constexpr ModInt operator*(const ModInt &r) const noexcept { return ModInt(*this) *= r; } constexpr ModInt operator/(const ModInt &r) const noexcept { return ModInt(*this) /= r; } constexpr ModInt &operator+=(const ModInt &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr ModInt &operator-=(const ModInt &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr ModInt &operator*=(const ModInt &r) noexcept { val = val * r.val % MOD; return *this; } constexpr ModInt &operator/=(const ModInt &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const ModInt &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const ModInt &r) const noexcept { return this->val != r.val; } friend ostream &operator<<(ostream &os, const ModInt<MOD> &x) noexcept { return os << x.val; } friend istream &operator>>(istream &is, ModInt<MOD> &x) noexcept { return is >> x.val; } friend constexpr ModInt<MOD> modpow(const ModInt<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // 二項係数ライブラリ template <class T> struct BiCoef { constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : mFact(n, 1), mInv(n, 1), mFInv(n, 1) { Init(n); } constexpr void Init(int n) noexcept { mFact.assign(n, 1), mInv.assign(n, 1), mFInv.assign(n, 1); int factMod = mFact[0].getmod(); for (int i = 2; i < n; i++) { mFact[i] = mFact[i - 1] * i; mInv[i] = -mInv[factMod % i] * (factMod / i); mFInv[i] = mFInv[i - 1] * mInv[i]; } } constexpr T Fact(int n) const noexcept { if (n < 0) return 0; return mFact[n]; } constexpr T Comb(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return mFact[n] * mFInv[k] * mFInv[n - k]; } constexpr T Perm(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return mFact[n] * mFInv[k] * mFInv[n - k]; } constexpr T Homo(int n, int r) const { if (n < 0 || r < 0) return 0; return r == 0 ? 1 : Comb(n + r - 1, r); } constexpr T inv(int n) const noexcept { if (n < 0) return 0; return mInv[n]; } constexpr T finv(int n) const noexcept { if (n < 0) return 0; return mFInv[n]; } vector<T> mFact; vector<T> mInv; vector<T> mFInv; }; const int MOD = 1000000007; using mint = ModInt<MOD>; // BiCoef<mint> bc(2*1024*1024); // 2M * 3 * 8B(ll) = 48MB (0.1sec) int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll ret = 0; ll N, P; cin >> N >> P; string s; cin >> s; if (P == 2 || P == 5) { for (int i = 0; i < N; ++i) { int v = s[i] - '0'; if (v % P == 0) { ret += (i + 1); } } } else { map<int, ll> mp; mp[0] = 1; ll total = 0; ll base = 1; for (int i = N - 1; i >= 0; i--) { int v = (s[i] - '0') % P; total = ((base * v) % P + total) % P; ret += mp[total]; mp[total]++; base *= 10; base %= P; } } cout << setprecision(20) << ret << endl; return 0; }
replace
243
274
243
267
TLE
p02757
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define li long long int #define rep(i, to) for (li i = 0; i < ((li)(to)); i++) #define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++) #define pb push_back #define sz(v) ((li)(v).size()) #define bgn(v) ((v).begin()) #define eend(v) ((v).end()) #define allof(v) (v).begin(), (v).end() #define dodp(v, n) memset(v, (li)n, sizeof(v)) #define bit(n) (1ll << (li)(n)) #define mp(a, b) make_pair(a, b) #define rin rep(i, n) #define EPS 1e-12 #define ETOL 1e-8 #define MOD 1000000007 typedef pair<li, li> PI; #define INF bit(60) #define DBGP 1 #define idp if (DBGP) #define F first #define S second #define p2(a, b) idp cout << a << "\t" << b << endl #define p3(a, b, c) idp cout << a << "\t" << b << "\t" << c << endl #define p4(a, b, c, d) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << endl #define p5(a, b, c, d, e) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << endl #define p6(a, b, c, d, e, f) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \ << f << endl #define p7(a, b, c, d, e, f, g) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \ << f << "\t" << g << endl #define p8(a, b, c, d, e, f, g, h) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \ << f << "\t" << g << "\t" << h << endl #define p9(a, b, c, d, e, f, g, h, i) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \ << f << "\t" << g << "\t" << h << "\t" << i << endl #define p10(a, b, c, d, e, f, g, h, i, j) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \ << f << "\t" << g << "\t" << h << "\t" << i << "\t" << j << endl #define foreach(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) #define p2p(x) idp p2((x).F, (x).S) #define dump(x, n) \ idp { \ rep(i, n) { cout << x[i] << " "; } \ puts(""); \ } #define dump2(x, n) \ idp { \ rep(i, n) { cout << "[" << x[i].F << " , " << x[i].S << "] "; } \ puts(""); \ } #define dumpi(x) \ idp { \ foreach (it, x) { \ cout << (*it) << " "; \ } \ puts(""); \ } #define dumpi2(x) \ idp { \ foreach (it, x) { \ cout << "[" << (it)->F << " , " << (it)->S << "] "; \ } \ puts(""); \ } #define read2d(a, w, h) rep(i, h) rep(j, w) cin >> a[i][j] #define dump2d(a, w, h) \ rep(i, h) { \ rep(j, w) cout << a[i][j] << " "; \ puts(""); \ } typedef pair<li, li> PI; int main() { li n, p; cin >> n >> p; string s; cin >> s; if (p == 2) { throw; } if (p == 5) { throw; } map<li, li> cnt; cnt[0] = 1; li now = 0; li ju = 1; li res = 0; for (li i = n - 1; i >= 0; i--) { now = (now + (s[i] - '0') * ju) % p; res += cnt[now]; cnt[now]++; ju = ju * 10 % p; } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define li long long int #define rep(i, to) for (li i = 0; i < ((li)(to)); i++) #define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++) #define pb push_back #define sz(v) ((li)(v).size()) #define bgn(v) ((v).begin()) #define eend(v) ((v).end()) #define allof(v) (v).begin(), (v).end() #define dodp(v, n) memset(v, (li)n, sizeof(v)) #define bit(n) (1ll << (li)(n)) #define mp(a, b) make_pair(a, b) #define rin rep(i, n) #define EPS 1e-12 #define ETOL 1e-8 #define MOD 1000000007 typedef pair<li, li> PI; #define INF bit(60) #define DBGP 1 #define idp if (DBGP) #define F first #define S second #define p2(a, b) idp cout << a << "\t" << b << endl #define p3(a, b, c) idp cout << a << "\t" << b << "\t" << c << endl #define p4(a, b, c, d) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << endl #define p5(a, b, c, d, e) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << endl #define p6(a, b, c, d, e, f) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \ << f << endl #define p7(a, b, c, d, e, f, g) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \ << f << "\t" << g << endl #define p8(a, b, c, d, e, f, g, h) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \ << f << "\t" << g << "\t" << h << endl #define p9(a, b, c, d, e, f, g, h, i) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \ << f << "\t" << g << "\t" << h << "\t" << i << endl #define p10(a, b, c, d, e, f, g, h, i, j) \ idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \ << f << "\t" << g << "\t" << h << "\t" << i << "\t" << j << endl #define foreach(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) #define p2p(x) idp p2((x).F, (x).S) #define dump(x, n) \ idp { \ rep(i, n) { cout << x[i] << " "; } \ puts(""); \ } #define dump2(x, n) \ idp { \ rep(i, n) { cout << "[" << x[i].F << " , " << x[i].S << "] "; } \ puts(""); \ } #define dumpi(x) \ idp { \ foreach (it, x) { \ cout << (*it) << " "; \ } \ puts(""); \ } #define dumpi2(x) \ idp { \ foreach (it, x) { \ cout << "[" << (it)->F << " , " << (it)->S << "] "; \ } \ puts(""); \ } #define read2d(a, w, h) rep(i, h) rep(j, w) cin >> a[i][j] #define dump2d(a, w, h) \ rep(i, h) { \ rep(j, w) cout << a[i][j] << " "; \ puts(""); \ } typedef pair<li, li> PI; int main() { li n, p; cin >> n >> p; string s; cin >> s; if (p == 2 || p == 5) { li res = 0; rin { if ((s[i] - '0') % p == 0) { res += i + 1; } } cout << res << endl; return 0; } map<li, li> cnt; cnt[0] = 1; li now = 0; li ju = 1; li res = 0; for (li i = n - 1; i >= 0; i--) { now = (now + (s[i] - '0') * ju) % p; res += cnt[now]; cnt[now]++; ju = ju * 10 % p; } cout << res << endl; return 0; }
replace
91
96
91
100
0
p02757
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 200005; int n, p, a[N], ps[N]; int mul(int aa, int bb) { return int(1LL * aa * bb % p); } int fexp(int b, int e = p - 2) { int ans = 1; while (e) { if (e & 1) ans = mul(ans, b); b = mul(b, b); e /= 2; } return ans; } int cnt[N]; int main() { scanf("%d %d", &n, &p); for (int i = 1; i <= n; i++) scanf("%1d", a + i); if (__gcd(10, p) == 1) { ll ans = 0; cnt[0] = 1; int pw = 1; for (int i = n; i >= 1; i--) { ps[i] = (a[i] * pw + ps[i + 1]) % p; ans += cnt[ps[i]]; cnt[ps[i]]++; pw = 10 * pw % p; } printf("%lld\n", ans); return 0; } assert(0); }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 200005; int n, p, a[N], ps[N]; int mul(int aa, int bb) { return int(1LL * aa * bb % p); } int fexp(int b, int e = p - 2) { int ans = 1; while (e) { if (e & 1) ans = mul(ans, b); b = mul(b, b); e /= 2; } return ans; } int cnt[N]; int main() { scanf("%d %d", &n, &p); for (int i = 1; i <= n; i++) scanf("%1d", a + i); if (__gcd(10, p) == 1) { ll ans = 0; cnt[0] = 1; int pw = 1; for (int i = n; i >= 1; i--) { ps[i] = (a[i] * pw + ps[i + 1]) % p; ans += cnt[ps[i]]; cnt[ps[i]]++; pw = 10 * pw % p; } printf("%lld\n", ans); return 0; } ll ans = 0; for (int i = 1; i <= n; i++) { if (a[i] % p == 0) { ans += i; } } printf("%lld\n", ans); }
replace
42
43
42
49
0
p02757
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; // #define P pair<ll,ll> #define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I) #define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I) #define TO(x, t, f) ((x) ? (t) : (f)) #define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x, v) \ (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v x is sorted #define POSU(x, v) \ (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v x is sorted #define NUM(x, v) (POSU(x, v) - POSL(x, v)) // x is sorted #define REV(x) (reverse(x.begin(), x.end())) // reverse ll gcd(ll a, ll b) { if (a % b == 0) return b; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll c = gcd(a, b); return ((a / c) * (b / c) * c); } #define NEXTP(x) next_permutation(x.begin(), x.end()) const ll INF = ll(1e16) + ll(7); const ll MOD = 1000000007LL; #define out(a) cout << fixed << setprecision((a)) // tie(a,b,c) = make_tuple(10,9,87); #define pop(a) __builtin_popcount((a)) ll keta(ll a) { ll r = 0; while (a) { a /= 10; r++; } return r; } ll calc(ll a, ll b, ll p) { // a^(b) mod p if (b == 0) return 1; ll y = calc(a, b / 2, p); y = (y * y) % p; if (b & 1) y = (y * a) % p; return y; } int main() { ll N, P, ans = 0; cin >> N >> P; string S; cin >> S; if (P == 5 || P == 2) { P = 0; int x = 8 / P; } map<ll, ll> ma; ll x = 0; // ma[0] = 1; FOR(i, 0, N) { x *= 10; x += S[i] - '0'; x %= P; ll k = x * calc(calc(10, i, P), P - 2, P) % P; ans += ma[k]; ma[k]++; if (x == 0) ans++; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; // #define P pair<ll,ll> #define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I) #define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I) #define TO(x, t, f) ((x) ? (t) : (f)) #define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x, v) \ (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v x is sorted #define POSU(x, v) \ (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v x is sorted #define NUM(x, v) (POSU(x, v) - POSL(x, v)) // x is sorted #define REV(x) (reverse(x.begin(), x.end())) // reverse ll gcd(ll a, ll b) { if (a % b == 0) return b; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll c = gcd(a, b); return ((a / c) * (b / c) * c); } #define NEXTP(x) next_permutation(x.begin(), x.end()) const ll INF = ll(1e16) + ll(7); const ll MOD = 1000000007LL; #define out(a) cout << fixed << setprecision((a)) // tie(a,b,c) = make_tuple(10,9,87); #define pop(a) __builtin_popcount((a)) ll keta(ll a) { ll r = 0; while (a) { a /= 10; r++; } return r; } ll calc(ll a, ll b, ll p) { // a^(b) mod p if (b == 0) return 1; ll y = calc(a, b / 2, p); y = (y * y) % p; if (b & 1) y = (y * a) % p; return y; } int main() { ll N, P, ans = 0; cin >> N >> P; string S; cin >> S; if (P == 5) { FOR(i, 0, N) { if (S[i] == '5' || S[i] == '0') { ans += i + 1; } } cout << ans << endl; return 0; } if (P == 2) { FOR(i, 0, N) { if ((S[i] - '0') % 2 == 0) { ans += i + 1; } } cout << ans << endl; return 0; } map<ll, ll> ma; ll x = 0; // ma[0] = 1; FOR(i, 0, N) { x *= 10; x += S[i] - '0'; x %= P; ll k = x * calc(calc(10, i, P), P - 2, P) % P; ans += ma[k]; ma[k]++; if (x == 0) ans++; } cout << ans << endl; }
replace
54
57
54
71
0
p02757
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define fo(i, n) for (int i = 0; i < n; i++) #define Fo(i, k, n) for (int i = k; i < n; i++) #define endl "\n" #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define vec(x) vector<x> #define matrix(x) vector<vector<x>> #define mem(a, b) memset(a, b, sizeof a) #define vii vector<pair<int, int>> #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define MOD 998244353 #define deb(x) cerr << #x << " = " << x << "\n" #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define all(v) v.begin(), v.end() #define rep(i, a, b) for (int i = a; i < b; ++i) #define len(s) s.length() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; int power(int a, int b, int m = mod) { int ans = 1; while (b > 0) { if (b & 1) ans = (ans * a) % m; a = (a * a) % m; b >>= 1; } return ans; } void angshuGod() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } const int N = 1e5 + 1; const double pi = acos(-1); void solve() { int n, p; cin >> n >> p; string s; cin >> s; int curr = 0, ans = 0; int arr[n]; mem(arr, 0); arr[0]++; int po = 1; if (p == 2) { fo(i, n) { if ((int)(s[i] - '0') % 2 == 0) ans += i + 1; } } else if (p == 5) { fo(i, n) { if ((int)(s[i] - '0') % 5 == 0) ans += i + 1; } } else { for (int i = n - 1; i >= 0; i--) { curr = curr + (s[i] - '0') * po; curr = curr % p; po = (po * 10) % p; ans += arr[curr]; arr[curr]++; } } cout << ans; } int32_t main() { angshuGod(); int t; 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 fo(i, n) for (int i = 0; i < n; i++) #define Fo(i, k, n) for (int i = k; i < n; i++) #define endl "\n" #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define vec(x) vector<x> #define matrix(x) vector<vector<x>> #define mem(a, b) memset(a, b, sizeof a) #define vii vector<pair<int, int>> #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define MOD 998244353 #define deb(x) cerr << #x << " = " << x << "\n" #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define all(v) v.begin(), v.end() #define rep(i, a, b) for (int i = a; i < b; ++i) #define len(s) s.length() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; int power(int a, int b, int m = mod) { int ans = 1; while (b > 0) { if (b & 1) ans = (ans * a) % m; a = (a * a) % m; b >>= 1; } return ans; } void angshuGod() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } const int N = 1e5 + 1; const double pi = acos(-1); void solve() { int n, p; cin >> n >> p; string s; cin >> s; int curr = 0, ans = 0; int arr[N]; mem(arr, 0); arr[0]++; int po = 1; if (p == 2) { fo(i, n) { if ((int)(s[i] - '0') % 2 == 0) ans += i + 1; } } else if (p == 5) { fo(i, n) { if ((int)(s[i] - '0') % 5 == 0) ans += i + 1; } } else { for (int i = n - 1; i >= 0; i--) { curr = curr + (s[i] - '0') * po; curr = curr % p; po = (po * 10) % p; ans += arr[curr]; arr[curr]++; } } cout << ans; } int32_t main() { angshuGod(); int t; t = 1; // cin>>t; while (t--) { solve(); } return 0; }
replace
76
77
76
77
-11