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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p03200 | C++ | Time Limit Exceeded | #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
using namespace std;
long long powerexp(long long base, long long exp, int mod) {
long long res = 1;
while (exp > 0) {
if (exp % 2 == 1)
res = (res * base) % mod;
exp = exp >> 1;
base = (base * base) % mod;
}
return res;
}
int gcd(int a, int b) {
int c = a % b;
while (c != 0) {
a = b;
b = c;
c = a % b;
}
return b;
}
void PrintVector(vector<long long int> v, long long int n) {
for (int i = v.size() - 1; i >= 0; i--) {
cout << v[i] << " ";
}
cout << endl;
}
void Takevector(vector<long long int> &v, long long int n) {
for (long long int i = 0; i < n; i++) {
long long int number;
cin >> number;
v.push_back(number);
}
}
typedef long long int ll;
typedef vector<long long int> vi;
#define sci(x) \
int x; \
scanf("%d", &x);
#define scii(x, y) \
int x, y; \
scanf("%d %d", &x, &y);
#define sciii(x, y, z) \
int x, y, z; \
scanf("%d %d %d", &x, &y, &z);
#define scl(x) \
ll x; \
scanf("%lld", &x);
#define scll(x, y) \
ll x, y; \
scanf("%lld %lld", &x, &y);
#define sclll(x, y, z) \
ll x, y, z; \
scanf("%lld %lld %lld", &x, &y, &z);
#define TC(x) \
sci(x); \
while (x--)
#define rep(i, x, y) for (__typeof(x) i = x; i <= y; i++)
#define repi(i, x, y) for (__typeof(x) i = x; i >= y; i--)
#define fore(itr, x) \
for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr++)
#define forei(itr, x) \
for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr--)
#define nl cout << "\n";
int min(int x, int y) { return (x < y) ? x : y; }
int main() {
int flag = 1;
ll sum = 0;
int flag1 = 0;
ll tot = 0;
map<int, int> m;
vector<string> v;
vector<string> v1;
multiset<string> st;
multiset<string> st2;
string s;
cin >> s;
rep(i, 0, s.size() - 2) {
if (s[i] == 'B' && s[i + 1] == 'W') {
swap(s[i], s[i + 1]);
tot++;
if (i >= 1)
i -= 2;
}
}
cout << tot;
return 0;
} | #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
using namespace std;
long long powerexp(long long base, long long exp, int mod) {
long long res = 1;
while (exp > 0) {
if (exp % 2 == 1)
res = (res * base) % mod;
exp = exp >> 1;
base = (base * base) % mod;
}
return res;
}
int gcd(int a, int b) {
int c = a % b;
while (c != 0) {
a = b;
b = c;
c = a % b;
}
return b;
}
void PrintVector(vector<long long int> v, long long int n) {
for (int i = v.size() - 1; i >= 0; i--) {
cout << v[i] << " ";
}
cout << endl;
}
void Takevector(vector<long long int> &v, long long int n) {
for (long long int i = 0; i < n; i++) {
long long int number;
cin >> number;
v.push_back(number);
}
}
typedef long long int ll;
typedef vector<long long int> vi;
#define sci(x) \
int x; \
scanf("%d", &x);
#define scii(x, y) \
int x, y; \
scanf("%d %d", &x, &y);
#define sciii(x, y, z) \
int x, y, z; \
scanf("%d %d %d", &x, &y, &z);
#define scl(x) \
ll x; \
scanf("%lld", &x);
#define scll(x, y) \
ll x, y; \
scanf("%lld %lld", &x, &y);
#define sclll(x, y, z) \
ll x, y, z; \
scanf("%lld %lld %lld", &x, &y, &z);
#define TC(x) \
sci(x); \
while (x--)
#define rep(i, x, y) for (__typeof(x) i = x; i <= y; i++)
#define repi(i, x, y) for (__typeof(x) i = x; i >= y; i--)
#define fore(itr, x) \
for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr++)
#define forei(itr, x) \
for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr--)
#define nl cout << "\n";
int min(int x, int y) { return (x < y) ? x : y; }
int main() {
int flag = 1;
ll sum = 0;
int flag1 = 0;
ll tot = 0;
map<int, int> m;
vector<string> v;
vector<string> v1;
multiset<string> st;
multiset<string> st2;
string s;
cin >> s;
int b = 0;
rep(i, 0, s.size() - 1) {
if (s[i] == 'B')
b++;
else {
tot += b;
}
}
cout << tot;
return 0;
} | replace | 84 | 90 | 84 | 92 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
ll MOD = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
string s;
cin >> s;
int ans = 0;
bool continue_flg = false;
while (1) {
continue_flg = false;
rep(i, s.length() - 1) {
if (s[i] == 'B' && s[i + 1] == 'W') {
++ans;
continue_flg = true;
s[i] = 'W';
s[i + 1] = 'B';
}
}
if (!continue_flg)
break;
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
ll MOD = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
string s;
cin >> s;
int cnt = 0;
ll ans = 0;
rep(i, s.length()) {
if (s[i] == 'B')
++cnt;
else
ans += cnt;
}
cout << ans << endl;
return 0;
}
| replace | 15 | 29 | 15 | 22 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg) \
if (flg) { \
cout << "YES" << endl; \
} else { \
cout << "NO" << endl; \
}
#define MAX_N 1002
#define i197 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P1;
typedef pair<int, int> Pi;
typedef pair<double, Pi> Pdi;
typedef pair<ll, int> Pli;
typedef pair<P1, ll> P2;
const ll INF = 100000000000000001;
struct edge {
int to, cost;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
struct Road {
double cost;
int a, b;
};
int main() {
string s;
ll total = 0;
cin >> s;
ll o = 0;
rep(i, s.size() - 1) {
o = 0;
rep(j, s.size() - 1) {
if (s[j] == 'B' && s[j + 1] == 'W') {
s[j] = 'W';
s[j + 1] = 'B';
total++;
} else {
o++;
}
}
if (o == s.size()) {
break;
}
}
cout << total << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg) \
if (flg) { \
cout << "YES" << endl; \
} else { \
cout << "NO" << endl; \
}
#define MAX_N 1002
#define i197 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P1;
typedef pair<int, int> Pi;
typedef pair<double, Pi> Pdi;
typedef pair<ll, int> Pli;
typedef pair<P1, ll> P2;
const ll INF = 100000000000000001;
struct edge {
int to, cost;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
struct Road {
double cost;
int a, b;
};
int main() {
string s;
ll total = 0;
cin >> s;
ll o = 0;
for (ll i = 0; i < s.size() + 1; i++) {
if (s[i] == 'W') {
o++;
total += (i + 1) - o;
}
}
cout << total << endl;
return 0;
}
| replace | 38 | 51 | 38 | 43 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
#define all(container) container.begin(), container.end()
#define sortall(container) (sort(container.begin(), container.end()))
#define yn(cond) ((cond) ? (cout << "Yes" << endl) : (cout << "No" << endl))
#define YN(cond) ((cond) ? (cout << "YES" << endl) : (cout << "NO" << endl))
#define LLONG_MAX numeric_limits<long long>::max()
#define INT_MAX numeric_limits<int>::max()
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef struct edge_ {
ll to;
ll cost;
} edge;
typedef struct point_ {
ll x, y;
point_(ll x = 0, ll y = 0) : x(x), y(y) {}
} point;
constexpr double PI = 3.14159265358979323846;
constexpr ll INF = 1 << 15;
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-7;
constexpr int dy4[4] = {0, 1, 0, -1};
constexpr int dx4[4] = {1, 0, -1, 0};
constexpr int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
for (const T &x : vec)
os << x << " ";
return os;
}
template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
is >> p.first >> p.second;
return is;
}
istream &operator>>(istream &is, point &p) {
is >> p.x >> p.y;
return is;
}
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
os << "{" << p.first << " " << p.second << "}";
return os;
}
ostream &operator<<(ostream &os, const point &p) {
os << "(" << p.x << " " << p.y << ")";
return os;
}
template <typename T> void vecOutPadding(vector<T> &vec, string pad = " ") {
for (const T &elem : vec)
cout << elem << pad;
}
template <typename T> void matOut(vector<vector<T>> &mat) {
for (const auto &row : mat) {
for (const T &col : row)
cout << col << " ";
cout << endl;
}
}
/******************************************************************************/
/********************** Don't make WA !! **********************/
/******************************************************************************/
int main() {
string S;
cin >> S;
ll ans = 0;
ll cur = 1;
while (cur != 0) {
cur = 0;
for (int i = S.length() - 2; i >= 0; i--) {
if (S[i] == 'B' && S[i + 1] == 'W') {
cur++;
S[i] = 'W';
S[i + 1] = 'B';
}
}
ans += cur;
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
#define all(container) container.begin(), container.end()
#define sortall(container) (sort(container.begin(), container.end()))
#define yn(cond) ((cond) ? (cout << "Yes" << endl) : (cout << "No" << endl))
#define YN(cond) ((cond) ? (cout << "YES" << endl) : (cout << "NO" << endl))
#define LLONG_MAX numeric_limits<long long>::max()
#define INT_MAX numeric_limits<int>::max()
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef struct edge_ {
ll to;
ll cost;
} edge;
typedef struct point_ {
ll x, y;
point_(ll x = 0, ll y = 0) : x(x), y(y) {}
} point;
constexpr double PI = 3.14159265358979323846;
constexpr ll INF = 1 << 15;
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-7;
constexpr int dy4[4] = {0, 1, 0, -1};
constexpr int dx4[4] = {1, 0, -1, 0};
constexpr int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
for (const T &x : vec)
os << x << " ";
return os;
}
template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
is >> p.first >> p.second;
return is;
}
istream &operator>>(istream &is, point &p) {
is >> p.x >> p.y;
return is;
}
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
os << "{" << p.first << " " << p.second << "}";
return os;
}
ostream &operator<<(ostream &os, const point &p) {
os << "(" << p.x << " " << p.y << ")";
return os;
}
template <typename T> void vecOutPadding(vector<T> &vec, string pad = " ") {
for (const T &elem : vec)
cout << elem << pad;
}
template <typename T> void matOut(vector<vector<T>> &mat) {
for (const auto &row : mat) {
for (const T &col : row)
cout << col << " ";
cout << endl;
}
}
/******************************************************************************/
/********************** Don't make WA !! **********************/
/******************************************************************************/
int main() {
string S;
cin >> S;
ll ans = 0;
ll white = 0;
for (int i = S.length() - 1; i >= 0; i--) {
if (S[i] == 'W')
white++;
else
ans += white;
}
cout << ans << endl;
return 0;
} | replace | 117 | 128 | 117 | 123 | TLE | |
p03200 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define poi 100010
#define int long long
using namespace std;
bool b[poi];
inline int re() {
char c = getchar();
int x = 0, k = 1;
while (c < '0' || c > '9') {
if (c == '-')
k = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * k;
}
void wr(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wr(x / 10);
putchar(x % 10 + '0');
}
signed main() {
int ans = 0, n = 0, c, sum = 0;
while ((c = getchar()) != '\n')
b[++n] = (c == 'B');
for (int i = 1; i <= n; i++)
if (!b[i])
ans += sum;
else
sum++;
wr(ans);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define poi 2001000
#define int long long
using namespace std;
bool b[poi];
inline int re() {
char c = getchar();
int x = 0, k = 1;
while (c < '0' || c > '9') {
if (c == '-')
k = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * k;
}
void wr(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wr(x / 10);
putchar(x % 10 + '0');
}
signed main() {
int ans = 0, n = 0, c, sum = 0;
while ((c = getchar()) != '\n')
b[++n] = (c == 'B');
for (int i = 1; i <= n; i++)
if (!b[i])
ans += sum;
else
sum++;
wr(ans);
return 0;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p03200 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
int N;
N = s.length();
int x[N];
for (int i = 0; i < N; i++) {
if (s[i] == 'B') {
x[i] = 0;
} else if (s[i] == 'W') {
x[i] = 1;
}
}
bool flag = true;
int a;
int start = 0;
int goal = 1;
int counter = 0;
while (flag) {
while (x[start] == 1) {
start += 1;
}
while (x[N - goal] == 0) {
goal += 1;
}
if (start > N - goal) {
break;
}
for (int i = start; i < N - goal; i++) {
a = x[i + 1] - x[i];
if (a == 1) {
x[i + 1] = 0;
x[i] = 1;
flag = true;
counter += 1;
break;
} else {
flag = false;
}
}
}
cout << counter;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
int N;
N = s.length();
int x[N];
for (int i = 0; i < N; i++) {
if (s[i] == 'B') {
x[i] = 0;
} else if (s[i] == 'W') {
x[i] = 1;
}
}
long a = 0;
long counter = 0;
for (int i = 0; i < N; i++) {
if (x[i] == 0) {
a += 1;
} else if (x[i] == 1) {
counter += a;
}
}
cout << counter;
return 0;
}
| replace | 18 | 44 | 18 | 25 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string s;
void solve() {
const int N = s.length();
int ans = 0;
while (true) {
bool f = false;
for (int i = 0; i + 1 < N; ++i)
if (s[i] == 'B' && s[i + 1] == 'W') {
swap(s[i], s[i + 1]);
f = true;
ans++;
break;
}
if (!f)
break;
}
cout << ans << endl;
}
int main() {
cin >> s;
solve();
return 0;
} | #include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string s;
void solve() {
const int N = s.length();
long long ans = 0;
int wcnt = 0;
for (int i = N - 1; i >= 0; --i) {
if (s[i] == 'B')
ans += wcnt;
else
wcnt++;
}
cout << ans << endl;
}
int main() {
cin >> s;
solve();
return 0;
} | replace | 10 | 22 | 10 | 17 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
string s, c;
cin >> s;
int count = 0;
do {
c = s;
int i = 0;
while (i < (int)s.length() - 1) {
if (s[i] == 'B') {
if (s[i + 1] == 'W') {
s[i + 1] = 'B';
s[i] = 'W';
count++;
i = i + 1;
} else
i = i + 1;
} else
i = i + 1;
// cout << s << endl;
}
} while (c != s);
cout << count;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string s, c;
cin >> s;
long long count = 0, gcount = 0;
for (int i = 0; i < (int)s.length(); i++) {
if (s[i] == 'B')
count++;
else
gcount += count;
}
cout << gcount;
}
| replace | 6 | 25 | 6 | 14 | TLE | |
p03200 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mp make_pair
#define pb push_back
#define inf INT_MAX
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define mod INT_MAX
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
IOS string s;
cin >> s;
int cntb = 0, sum = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'W')
sum += cntb;
else
cntb++;
}
cout << sum;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mp make_pair
#define pb push_back
#define inf INT_MAX
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define mod INT_MAX
int32_t main() {
IOS string s;
cin >> s;
int cntb = 0, sum = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'W')
sum += cntb;
else
cntb++;
}
cout << sum;
return 0;
}
| replace | 20 | 24 | 20 | 21 | 0 | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
ll ans = 0;
string s;
cin >> s;
ll pos = 0;
rep(i, 0, s.length()) {
ll cntb = 0, cntw = 0, tmp = 0;
if (s[i] == 'B') {
while (s[i] == 'B') {
tmp++;
cntb++;
i++;
}
while (s[i] == 'W') {
cntw++;
i++;
}
}
// cout<<pos<<" "<<cntb<<" "<<cntw<<endl;
ans += (cntb + pos) * cntw;
pos += tmp;
i--;
}
cout << ans;
} | #include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
ll ans = 0;
string s;
cin >> s;
ll pos = 0;
rep(i, 0, s.length()) {
ll cntb = 0, cntw = 0, tmp = 0;
if (s[i] == 'B') {
while (s[i] == 'B') {
tmp++;
cntb++;
i++;
}
while (s[i] == 'W') {
cntw++;
i++;
}
} else
continue;
// cout<<pos<<" "<<cntb<<" "<<cntw<<endl;
ans += (cntb + pos) * cntw;
pos += tmp;
i--;
}
cout << ans;
} | replace | 45 | 46 | 45 | 47 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)
#define ll long long
#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value) \
vector<vector<type>> name(length, vector<type>(width, value));
int main() {
string N;
cin >> N;
ll sl = N.length();
ll sw = 1, count = 0;
while (sw == 1) {
sw = 0;
froop1(0, sl - 1) {
if (N[i] == 'B' && N[i + 1] == 'W') {
count++;
N[i] = 'W';
N[i + 1] = 'B';
sw = 1;
}
}
}
cout << count << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)
#define ll long long
#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value) \
vector<vector<type>> name(length, vector<type>(width, value));
int main() {
string N;
cin >> N;
ll sl = N.length();
ll count = 0, c1 = 0;
froop1(0, sl) {
if (N[i] == 'B')
c1++;
else
count += c1;
}
cout << count << endl;
return 0;
} | replace | 17 | 28 | 17 | 23 | TLE | |
p03200 | C++ | Time Limit Exceeded | /*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define MOD 1000000007
void solve(int test_case) { // 220
string s;
cin >> s;
int cnt = 0;
while (1) {
int ccnt = 0;
for (int i = 0; i < (int)s.length() - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'W')
ccnt++, s[i] = 'W', s[i + 1] = 'B';
}
cnt += ccnt;
if (ccnt == 0)
break;
}
cout << cnt;
}
int main() {
////// FILE BASED IO////
// freopen("wormhole.in", "r", stdin);
// freopen("wormhole.out", "w", stdout);
///////////////
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin>>t;
REP(i, 1, t) { solve(i); }
return 0;
}
| /*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define MOD 1000000007
void solve(int test_case) { // 220
string s;
cin >> s;
ll cnt = 0;
int b = 0;
REP(i, 0, s.length() - 1) {
if (s[i] == 'W')
cnt += b;
else
b++;
}
cout << cnt;
}
int main() {
////// FILE BASED IO////
// freopen("wormhole.in", "r", stdin);
// freopen("wormhole.out", "w", stdout);
///////////////
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin>>t;
REP(i, 1, t) { solve(i); }
return 0;
}
| replace | 31 | 41 | 31 | 38 | TLE | |
p03200 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#define ll long long
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii, int>
#define rep(i, n) for (int(i) = (0); i < (n); i++)
#define repA(i, x, y) for (int i = (x); i <= (y); i++)
#define repD(i, x, y) for (int i = (x); i >= (y); i--)
#define mod (1000000000 + 7)
#define sz(a) int((a).size())
#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 ss second
#define ff first
using namespace std;
int lg2(ll n) {
int res = -1;
while (n) {
n >>= 1;
res++;
}
return res;
}
int main() {
// HOW DO I REDUCE THIS TO O(Lg(N)) ???
ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
string s;
cin >> s;
ll ans = 0;
std::vector<int> pos;
ll n = s.length();
rep(i, s.length()) if (s[i] == 'W') pos.pb(n - i);
sort(all(pos));
reverse(all(pos));
/*for(auto it : pos)
cout << it << " " ;
cout << endl ;*/
rep(i, pos.size()) ans += (n-- - pos[i]);
cout << ans;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#define ll long long
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii, int>
#define rep(i, n) for (int(i) = (0); i < (n); i++)
#define repA(i, x, y) for (int i = (x); i <= (y); i++)
#define repD(i, x, y) for (int i = (x); i >= (y); i--)
#define mod (1000000000 + 7)
#define sz(a) int((a).size())
#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 ss second
#define ff first
using namespace std;
int lg2(ll n) {
int res = -1;
while (n) {
n >>= 1;
res++;
}
return res;
}
int main() {
// HOW DO I REDUCE THIS TO O(Lg(N)) ???
// ios::sync_with_stdio(0);
string s;
cin >> s;
ll ans = 0;
std::vector<int> pos;
ll n = s.length();
rep(i, s.length()) if (s[i] == 'W') pos.pb(n - i);
sort(all(pos));
reverse(all(pos));
/*for(auto it : pos)
cout << it << " " ;
cout << endl ;*/
rep(i, pos.size()) ans += (n-- - pos[i]);
cout << ans;
}
| replace | 35 | 40 | 35 | 36 | 0 | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
const double pi = acos(-1.0);
const ll mod = pow(10, 9) + 7;
const ll INF = pow(2, 31) - 1;
typedef pair<int, int> P;
typedef vector<int> vi;
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 a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
ll lc = (a / g) * b;
return lc;
}
int main() {
ios::sync_with_stdio(false);
string s;
cin >> s;
int ans = 0;
while (s.find("BBW") != std::string::npos) {
int place0 = s.find("BBW");
s.replace(place0, 3, "WBB");
ans += 2;
}
while (s.find("BW") != std::string::npos) {
int place = s.find("BW");
s.replace(place, 2, "WB");
ans++;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
const double pi = acos(-1.0);
const ll mod = pow(10, 9) + 7;
const ll INF = pow(2, 31) - 1;
typedef pair<int, int> P;
typedef vector<int> vi;
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 a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
ll lc = (a / g) * b;
return lc;
}
int main() {
ios::sync_with_stdio(false);
string s;
cin >> s;
ll ans = 0;
ll cnt = 0;
rep(i, s.size()) {
if (s[i] == 'W') {
ans += i - cnt;
cnt++;
}
}
cout << ans << endl;
return 0;
} | replace | 41 | 51 | 41 | 48 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <tuple>
using namespace std;
typedef long long int ll;
int main() {
string S;
cin >> S;
int n = S.size();
ll ans = 0;
bool frag = 1;
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < n; j++) {
if (S[j] == 'B' && S[j + 1] == 'W') {
S[j] = 'W';
S[j + 1] = 'B';
// cout << S << endl;
ans++;
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#include <tuple>
using namespace std;
typedef long long int ll;
int main() {
string S;
cin >> S;
int n = S.size();
ll ans = 0;
vector<int> cnt(n);
for (int i = 0; i < n - 1; i++) {
if (S[i] == 'B') {
cnt[i + 1] = cnt[i] + 1;
} else {
cnt[i + 1] = cnt[i];
}
}
for (int i = 0; i < n; i++) {
if (S[i] == 'W') {
ans += cnt[i];
}
}
cout << ans << endl;
} | replace | 10 | 19 | 10 | 21 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
typedef long long LL;
using namespace std;
int main() {
string s;
cin >> s;
LL cnt = 0;
for (int i = 1; i < s.size(); i++) {
if (s.at(i) == 'W') {
for (int j = 0; j < i; j++) {
if (s.at(j) == 'B') {
cnt++;
}
}
}
}
cout << cnt << endl;
}
| #include <bits/stdc++.h>
typedef long long LL;
using namespace std;
int main() {
string s;
cin >> s;
LL cnt = 0, bcnt = 0;
for (int i = 0; i < s.size(); i++) {
if (s.at(i) == 'W' && i > 0) {
cnt += bcnt;
} else if (s.at(i) == 'B') {
bcnt++;
}
}
cout << cnt << endl;
}
| replace | 8 | 16 | 8 | 14 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
string S;
cin >> S;
int num = 0;
for (int i = 0; i < S.size() - 1; ++i) {
if (S[i] == 'B' && S[i + 1] == 'W') {
S[i] = 'W';
S[i + 1] = 'B';
++num;
i -= 2;
if (i == -2)
++i;
}
}
cout << num << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
string S;
cin >> S;
long long num = 0;
long long numW = 0;
for (long long i = 0; i < S.size(); ++i) {
if (S[i] == 'B') {
} else {
num += (i - numW);
++numW;
}
}
cout << num << endl;
return 0;
} | replace | 12 | 21 | 12 | 20 | TLE | |
p03200 | C++ | Runtime Error | // A - Irreversible operation
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define rep(i, m, n) for (lli i = m; i < (n); i++)
#define repe(i, m, n) for (lli i = m; i <= (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((lli)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define _GLIBCXX_DEBUG
const lli INF = 2147483647;
const lli MINF = -2147483648;
const lli LINF = 9223372036854775807;
const lli MOD = 1000000007; // 10^9+7
const double PI = acos(-1);
#define SORT(n) sort(n.begin(), n.end())
#define SORTR(n) sort(n.begin(), n.end(), greater<int>())
#define REV(n) reverse(n.begin(), n.end())
#define pb push_back
#define eb emplace_back
#define mp make_pair
#pragma GCC optimize("Ofast")
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vlli = vector<lli>;
using vs = vector<string>;
using ll = long long;
// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
// os << '{';
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : "");
}
// os << '}';
return os;
}
// aよりもbが大きいならばaをbで更新する(更新されたならばtrueを返す)
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b; // aをbで更新
return true;
}
return false;
}
// aよりもbが小さいならばaをbで更新する(更新されたならばtrueを返す)
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b; // aをbで更新
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
lli a, b, c, h, k, n, w, ans = 0, count = 0;
string s, t, v, x, y;
vector<pair<string, pair<lli, lli>>> pr;
cin >> s;
vlli d(110, 0), e(s.size() + 1, 0);
rep(j, 0, s.size()) {
y = s[j];
if (y == "W") {
d[j]++;
}
e[j + 1] = e[j] + d[j];
}
a = MAX(e);
rep(i, 1, s.size()) {
x = s[i - 1];
if (x == "B") {
ans += a - e[i];
}
}
// cout << d << endl;
// cout << e << endl;
cout << ans << endl;
// cout << "s: " << s << endl;
} | // A - Irreversible operation
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define rep(i, m, n) for (lli i = m; i < (n); i++)
#define repe(i, m, n) for (lli i = m; i <= (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((lli)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define _GLIBCXX_DEBUG
const lli INF = 2147483647;
const lli MINF = -2147483648;
const lli LINF = 9223372036854775807;
const lli MOD = 1000000007; // 10^9+7
const double PI = acos(-1);
#define SORT(n) sort(n.begin(), n.end())
#define SORTR(n) sort(n.begin(), n.end(), greater<int>())
#define REV(n) reverse(n.begin(), n.end())
#define pb push_back
#define eb emplace_back
#define mp make_pair
#pragma GCC optimize("Ofast")
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vlli = vector<lli>;
using vs = vector<string>;
using ll = long long;
// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
// os << '{';
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : "");
}
// os << '}';
return os;
}
// aよりもbが大きいならばaをbで更新する(更新されたならばtrueを返す)
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b; // aをbで更新
return true;
}
return false;
}
// aよりもbが小さいならばaをbで更新する(更新されたならばtrueを返す)
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b; // aをbで更新
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
lli a, b, c, h, k, n, w, ans = 0, count = 0;
string s, t, v, x, y;
vector<pair<string, pair<lli, lli>>> pr;
cin >> s;
vlli d(200010, 0), e(s.size() + 1, 0);
rep(j, 0, s.size()) {
y = s[j];
if (y == "W") {
d[j]++;
}
e[j + 1] = e[j] + d[j];
}
a = MAX(e);
rep(i, 1, s.size()) {
x = s[i - 1];
if (x == "B") {
ans += a - e[i];
}
}
// cout << d << endl;
// cout << e << endl;
cout << ans << endl;
// cout << "s: " << s << endl;
} | replace | 69 | 70 | 69 | 70 | 0 | |
p03200 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using lli = long long int;
ll MM = 1000000000;
ll mod = MM + 7;
#define INF (ll)1e18
#define pi acos(-1.0)
int main() {
string s;
cin >> s;
ll n = s.size();
vector<ll> a;
ll cnt = 0;
for (ll i = 0; i < n; i++) {
if (s[i] == 'B')
a.push_back(i + 1);
}
reverse(a.begin(), a.end());
for (ll i = 0; i < n; i++) {
cnt += n - a[i];
n--;
}
cout << cnt << endl;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using lli = long long int;
ll MM = 1000000000;
ll mod = MM + 7;
#define INF (ll)1e18
#define pi acos(-1.0)
int main() {
string s;
cin >> s;
ll n = s.size();
vector<ll> a;
ll cnt = 0;
for (ll i = 0; i < n; i++) {
if (s[i] == 'B')
a.push_back(i + 1);
}
reverse(a.begin(), a.end());
for (ll i = 0; i < a.size(); i++) {
cnt += n - a[i];
n--;
}
cout << cnt << endl;
} | replace | 25 | 26 | 25 | 26 | 0 | |
p03200 | C++ | Time Limit Exceeded |
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
int c = 0, change;
do {
change = 0;
for (int i = 0, n = s.length(); i < n - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'W') {
s[i] = 'W';
s[i + 1] = 'B';
c++;
change++;
}
}
} while (change);
cout << c << endl;
}
|
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
unsigned long long c = 0, cb = 0;
for (int i = 0, n = s.length(); i < n; i++) {
if (s[i] == 'B')
cb++;
else
c += cb;
}
cout << c << endl;
}
| replace | 8 | 21 | 8 | 15 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for (auto i = (m).begin(); i != (m).end(); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;
template <typename T> void printv(vector<T> const &s) {
REP(i, SZ(s)) { cout << s[i] << " "; }
cout << endl;
}
int main() {
cin.tie(0);
cout << setprecision(10);
string s;
cin >> s;
int cnt = 0;
bool upd = true;
while (upd) {
upd = false;
REP(i, SZ(s) - 1) {
if (s[i] == 'B' && s[i + 1] == 'W') {
s[i] = 'W';
s[i + 1] = 'B';
cnt++;
upd = true;
}
}
FOR(i, SZ(s) - 1, 1) {
if (s[i] == 'W' && s[i - 1] == 'B') {
s[i] = 'B';
s[i - 1] = 'W';
cnt++;
upd = true;
}
}
}
cout << cnt << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for (auto i = (m).begin(); i != (m).end(); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;
template <typename T> void printv(vector<T> const &s) {
REP(i, SZ(s)) { cout << s[i] << " "; }
cout << endl;
}
int main() {
cin.tie(0);
cout << setprecision(10);
string s;
cin >> s;
ll cnt = 0;
ll cntb = 0;
RFOR(i, SZ(s) - 1, 0) {
if (s[i] == 'B') {
cnt += SZ(s) - 1 - i - cntb;
cntb++;
}
}
cout << cnt << endl;
}
| replace | 40 | 59 | 40 | 46 | TLE | |
p03200 | C++ | Runtime Error | #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char s[12000];
int main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
int tot = 1;
long long ans = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == 'W') {
ans += i - tot;
tot++;
}
}
printf("%lld\n", ans);
} | #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char s[1200000];
int main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
int tot = 1;
long long ans = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == 'W') {
ans += i - tot;
tot++;
}
}
printf("%lld\n", ans);
} | replace | 4 | 5 | 4 | 5 | 0 | |
p03200 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 100000
#define LL long long
int n;
char s[MAXN + 5];
int cnt;
LL ans;
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
for (int i = 1; i <= n; i++) {
if (s[i] == 'W')
cnt++, ans += i - cnt;
}
printf("%lld", ans);
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 500000
#define LL long long
int n;
char s[MAXN + 5];
int cnt;
LL ans;
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
for (int i = 1; i <= n; i++) {
if (s[i] == 'W')
cnt++, ans += i - cnt;
}
printf("%lld", ans);
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p03200 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <list>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
string s;
cin >> s;
list<pair<char, int>> l;
int len = s.length();
int i = 0;
while (i < len) {
int c = 0;
char tmp = s[i];
while (s[i] == tmp) {
c++;
i++;
}
l.push_back(pair<char, int>(tmp, c));
}
l.pop_front();
l.pop_back();
int cnt = 0;
int tmpb = 0;
for (auto p = l.begin(); p != l.end(); p++) {
if (p->first == 'B') {
tmpb += p->second;
} else {
cnt += tmpb;
}
}
cout << cnt << endl;
return 0;
}
| #include <algorithm>
#include <cstring>
#include <iostream>
#include <list>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
string s;
cin >> s;
unsigned long long tmpb = 0;
unsigned long long cnt = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'B') {
tmpb++;
} else {
cnt += tmpb;
}
}
cout << cnt << endl;
return 0;
}
| replace | 14 | 36 | 14 | 19 | 0 | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define codefor \
int test; \
cin >> test; \
while (test--)
#define vec_input(v) \
for (auto it = v.begin(); it != v.end(); it++) { \
cin >> *it; \
}
#define vec_output(v) \
for (auto it = v.begin(); it != v.end(); it++) { \
if (it != v.begin()) \
cout << " "; \
cout << *it; \
} \
cout << endl;
#define vec_debug(v) \
for (auto it = v.begin(); it != v.end(); it++) { \
cerr << *it << " "; \
} \
cerr << endl;
#define yn(ans) cout << (ans ? "Yes" : "No") << endl
#define YN(ans) cout << (ans ? "YES" : "NO") << endl
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
int main() {
string s;
cin >> s;
ll ans = 0, l = 0;
rep(i, s.length() - 1) {
while (s[i] == 'B' && s[i + 1] == 'W') {
ans++;
s[i] = 'W';
s[i + 1] = 'B';
i--;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define codefor \
int test; \
cin >> test; \
while (test--)
#define vec_input(v) \
for (auto it = v.begin(); it != v.end(); it++) { \
cin >> *it; \
}
#define vec_output(v) \
for (auto it = v.begin(); it != v.end(); it++) { \
if (it != v.begin()) \
cout << " "; \
cout << *it; \
} \
cout << endl;
#define vec_debug(v) \
for (auto it = v.begin(); it != v.end(); it++) { \
cerr << *it << " "; \
} \
cerr << endl;
#define yn(ans) cout << (ans ? "Yes" : "No") << endl
#define YN(ans) cout << (ans ? "YES" : "NO") << endl
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
int main() {
string s;
cin >> s;
ll ans = 0, l = 0;
rep(i, s.length()) {
if (s[i] == 'W') {
ans += i - l;
l++;
}
}
cout << ans << endl;
} | replace | 35 | 41 | 35 | 39 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#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) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
inline void init() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
int main() {
init();
string s;
cin >> s;
bool is = true;
int ans = 0;
while (is) {
is = false;
rep(i, s.size() - 1) {
if (s[i] == 'B' && s[i + 1] == 'W') {
swap(s[i], s[i + 1]);
++ans;
is = true;
}
}
}
cout << ans << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#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) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
inline void init() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
int main() {
init();
string s;
cin >> s;
ll w = 0, ans = 0;
rep(i, s.size()) {
if (s[i] == 'W') {
ans += i - w;
++w;
}
}
cout << ans << "\n";
return 0;
} | replace | 39 | 49 | 39 | 44 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#ifdef ONPC
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
int main() {
#ifdef ONPC
freopen("a.in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n = (int)s.size();
int ans = 0;
for (int i = 0; i < n - 1; i++) {
if (i + 1 < n && s[i] == 'B' && s[i + 1] == 'W') {
s[i] = 'W', s[i + 1] = 'B';
i = max(-1, i - 2);
ans++;
}
}
cout << ans << '\n';
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#ifdef ONPC
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
int main() {
#ifdef ONPC
freopen("a.in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n = (int)s.size();
int cnt = 0;
ll ans = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == 'B') {
ans += n - i - cnt - 1;
cnt++;
}
}
cout << ans << '\n';
}
| replace | 40 | 46 | 40 | 46 | TLE | |
p03200 | 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 pii = pair<int, int>;
int main() {
string s;
cin >> s;
int cnt = 0;
bool a = true;
while (a) {
a = false;
rep(i, s.size()) {
if (s[i] == 'B' && s[i + 1] == 'W') {
s[i] = 'W';
s[i + 1] = 'B';
cnt++;
a = true;
}
}
}
cout << cnt << endl;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main() {
string s;
cin >> s;
ll cnt = 0;
int Bcount = 0;
rep(i, s.size()) {
if (s[i] == 'B')
Bcount++;
else
cnt += Bcount;
}
cout << cnt << endl;
}
| replace | 9 | 21 | 9 | 16 | TLE | |
p03200 | C++ | Runtime Error | // failed to generate code
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) sort((c).rbegin(), (c).rend())
#define UNIQUE(c) \
SORT(c); \
(c).erase(unique((c).begin(), (c).end()), (c).end());
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double INF = 1e7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
int main() {
string S;
cin >> S;
reverse(S.begin(), S.end());
LL i = 0;
while (S[i] != 'W') {
i++;
}
LL nw = 0, nb = 0;
LL sum = 0;
while (i < SZ(S)) {
while (i < SZ(S) && S[i] == 'W') {
i++;
nw++;
}
nb = 0;
while (i < SZ(S) && S[i] == 'B') {
i++;
nb++;
}
sum += nw * nb;
}
cout << sum << endl;
return 0;
}
| // failed to generate code
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) sort((c).rbegin(), (c).rend())
#define UNIQUE(c) \
SORT(c); \
(c).erase(unique((c).begin(), (c).end()), (c).end());
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double INF = 1e7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
int main() {
string S;
cin >> S;
reverse(S.begin(), S.end());
LL i = 0;
while (i < SZ(S) && S[i] != 'W') {
i++;
}
LL nw = 0, nb = 0;
LL sum = 0;
while (i < SZ(S)) {
while (i < SZ(S) && S[i] == 'W') {
i++;
nw++;
}
nb = 0;
while (i < SZ(S) && S[i] == 'B') {
i++;
nb++;
}
sum += nw * nb;
}
cout << sum << endl;
return 0;
}
| replace | 51 | 52 | 51 | 52 | 0 | |
p03200 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
char str[100010];
int main(void) {
scanf("%s", str);
int len = strlen(str);
int bcnt = 0;
int wcnt = 0;
for (int i = 0; i < len; i++) {
if (str[i] == 'B') {
bcnt++;
} else {
wcnt++;
}
}
if (!bcnt || !wcnt) {
printf("0");
return 0;
}
ll ans = 0;
for (int i = 0; i < len; i++) {
if (str[i] == 'W')
ans += i;
}
for (int i = 0; i < wcnt; i++) {
ans -= i;
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
char str[200010];
int main(void) {
scanf("%s", str);
int len = strlen(str);
int bcnt = 0;
int wcnt = 0;
for (int i = 0; i < len; i++) {
if (str[i] == 'B') {
bcnt++;
} else {
wcnt++;
}
}
if (!bcnt || !wcnt) {
printf("0");
return 0;
}
ll ans = 0;
for (int i = 0; i < len; i++) {
if (str[i] == 'W')
ans += i;
}
for (int i = 0; i < wcnt; i++) {
ans -= i;
}
cout << ans;
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p03200 | Python | Time Limit Exceeded | S = input()[::-1]
c = 0
while S.find("WB") != -1:
S = S.replace("WB", "BW", 1)
c += 1
print(c)
| S = input()
S = S[::-1]
y = -1
def x():
global y
y += 1
return y
c = [i - x() for i in range(len(S)) if S[i] == "B"]
print(sum(c))
| replace | 0 | 6 | 0 | 13 | TLE | |
p03200 | Python | Time Limit Exceeded | S = input()
ans = 0
prev = None
while prev != ans:
prev = ans
ans += S.count("BW")
S = S.replace("BW", "WB")
print(ans)
| S = input()
ans = 0
black = 0
for c in S:
if c == "B":
black += 1
else:
ans += black
print(ans)
| replace | 2 | 7 | 2 | 8 | TLE | |
p03200 | Python | Time Limit Exceeded | s = input()
reverse_count = 0
while "BW" in s:
reverse_count += s.count("BW")
s = s.replace("BW", "WB")
print(reverse_count)
| s = list(input())
W_count = 0
ans = 0
for i in range(len(s)):
if s[i] == "W":
ans += i - W_count
W_count += 1
print(ans)
| replace | 0 | 6 | 0 | 8 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
int sz = S.size();
int ans = 0;
for (int i = 0; i < sz - 1; i++) {
if (S[i] == 'B' && S[i + 1] == 'W') {
S[i] = 'W';
S[i + 1] = 'B';
if (i > 0 && S[i - 1] == 'B' && S[i] == 'W') {
i -= 2;
}
ans++;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
int sz = S.size();
long long ans = 0;
int bcnt = 0;
for (int i = 0; i < sz; i++) {
if (S[i] == 'B') {
bcnt++;
} else {
ans += bcnt;
}
}
cout << ans << endl;
return 0;
}
| replace | 9 | 18 | 9 | 16 | TLE | |
p03200 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
typedef unsigned uint;
typedef long long Int;
typedef unsigned long long UInt;
const int INF = 1001001001;
const Int INFLL = 1001001001001001001LL;
template <typename T> void pv(T a, T b) {
for (T i = a; i != b; ++i)
cout << *i << " ";
cout << endl;
}
template <typename T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <typename T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
int in() {
int x;
scanf("%d", &x);
return x;
}
double fin() {
double x;
scanf("%lf", &x);
return x;
}
Int lin() {
Int x;
scanf("%lld", &x);
return x;
}
char S[100050];
int main() {
scanf("%s", S);
const int n = strlen(S);
Int res = 0;
int blk = 0;
for (int i = 0; i < n; ++i) {
if (S[i] == 'B') {
++blk;
} else {
res += blk;
}
}
printf("%lld\n", res);
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
typedef unsigned uint;
typedef long long Int;
typedef unsigned long long UInt;
const int INF = 1001001001;
const Int INFLL = 1001001001001001001LL;
template <typename T> void pv(T a, T b) {
for (T i = a; i != b; ++i)
cout << *i << " ";
cout << endl;
}
template <typename T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <typename T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
int in() {
int x;
scanf("%d", &x);
return x;
}
double fin() {
double x;
scanf("%lf", &x);
return x;
}
Int lin() {
Int x;
scanf("%lld", &x);
return x;
}
char S[300050];
int main() {
scanf("%s", S);
const int n = strlen(S);
Int res = 0;
int blk = 0;
for (int i = 0; i < n; ++i) {
if (S[i] == 'B') {
++blk;
} else {
res += blk;
}
}
printf("%lld\n", res);
return 0;
}
| replace | 59 | 60 | 59 | 60 | 0 | |
p03200 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(A) A.begin(), A.end()
int main() {
string s;
cin >> s;
int counter = 0;
while (s.at(0) == 'W') {
s.erase(s.begin());
}
int j = 0;
for (int i = 0; i < s.size(); i++) {
if (s.at(i) == 'W') {
counter += (i - j);
j++;
}
}
cout << counter << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(A) A.begin(), A.end()
int main() {
string s;
cin >> s;
ll counter = 0;
int j = 0;
for (int i = 0; i < s.size(); i++) {
if (s.at(i) == 'W') {
counter += (i - j);
j++;
}
}
cout << counter << endl;
return 0;
} | replace | 12 | 17 | 12 | 13 | 0 | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define lli long long
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
#define rep0(i, j) for (int i = 0; i < j; i++)
#define rep1(i, j) for (int i = 1; i <= j; i++)
#define rep0d(i, j) for (int i = j - 1; i >= 0; i--)
#define MOD 1000000007
#define INF (int)1e7
class Graph {
typedef pair<int, int> p;
int V;
list<p> *adj;
public:
Graph(int V) {
this->V = V;
adj = new list<p>[V];
}
void addEdge(int u, int v, int w) { adj[u].push_back(make_pair(v, w)); }
bool route(lli from, lli to) { // vec[i]==j: j->iっていうもとに戻れる道がある
vector<lli> vec(V, -1);
dfs(1, from, vec);
return vec[to] >= 0;
}
void dfs(lli ed, lli num, vector<lli> &vec) {
for (auto e : adj[num]) {
if (vec[e.first] != -1)
continue;
vec[e.first] = num;
dfs(ed + 1, e.first, vec);
}
}
vector<int> bfs(int n) {
queue<int> q;
vector<int> vec(V, -1);
q.push(n);
vec[n] = 0;
while (!q.empty()) {
int p = q.front();
q.pop();
for (auto e : adj[p]) {
if (vec[e.first] == -1) {
q.push(e.first);
vec[e.first] = e.second + vec[p];
}
}
}
return vec;
}
vector<int> shortestPath(int src) {
priority_queue<p, vector<p>, greater<p>> pq;
vector<int> dist(V, INF);
pq.push(make_pair(0, src));
dist[src] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
list<pair<int, int>>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i) {
int v = (*i).first;
int weight = (*i).second;
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}
}
for (int i = 0; i < V; i++)
if (dist[i] == INF)
dist[i] = -1;
return dist;
}
};
bool cmpf(const pair<lli, lli> left, const pair<lli, lli> right) {
return left.first < right.first;
}
bool cmps(const pair<lli, lli> left, const pair<lli, lli> right) {
return left.second < right.second;
}
void dfs(lli ed, lli num, vector<map<lli, lli>> &vec) {
lli n = 1;
for (auto e : vec[num]) {
if (n == ed)
n++;
if (e.second != -1)
continue;
vec[num][e.first] = n;
vec[e.first][num] = n;
dfs(n, e.first, vec);
n++;
}
}
lli gcd(lli a, lli b) {
if (a < b)
return gcd(b, a);
lli temp;
while ((temp = a % b)) {
a = b;
b = temp;
}
return b;
}
struct nCr {
vector<lli> f, rf;
lli count = 0;
nCr(lli num) : f(num), rf(num) {
count = num;
f[0] = 1;
rep1(i, num) f[i] = (f[i - 1] * i) % MOD;
rep0(i, num + 1) rf[i] = inv(f[i]);
}
lli inv(lli x) {
lli res = 1;
lli k = MOD - 2;
lli y = x;
while (k) {
if (k & 1)
res = (res * y) % MOD;
y = (y * y) % MOD;
k /= 2;
}
return res;
}
lli C(lli n, lli k) {
lli a = f[n];
lli b = rf[n - k];
lli c = rf[k];
lli bc = (b * c) % MOD;
return (a * bc) % MOD;
}
};
struct UF {
vector<int> par, size;
UF(int N) : par(N), size(N, 1) { rep0(i, N) par[i] = i; }
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] = ry;
size[rx] += size[ry];
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int getSize(int x) { return size[x]; }
};
int main() {
string s;
cin >> s;
lli num = 0, ans = 0, wnum = 0;
while (wnum != s.length() && num < s.length()) {
while (s[num] == 'W' && num < s.length())
num++;
wnum = num;
while (s[wnum] != 'W' && wnum < s.length())
wnum++;
if (!(wnum != s.length() && num < s.length()))
break;
ans += wnum - num;
char c = s[wnum];
s[wnum] = s[num];
s[num] = c;
num++;
}
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
#define lli long long
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
#define rep0(i, j) for (int i = 0; i < j; i++)
#define rep1(i, j) for (int i = 1; i <= j; i++)
#define rep0d(i, j) for (int i = j - 1; i >= 0; i--)
#define MOD 1000000007
#define INF (int)1e7
class Graph {
typedef pair<int, int> p;
int V;
list<p> *adj;
public:
Graph(int V) {
this->V = V;
adj = new list<p>[V];
}
void addEdge(int u, int v, int w) { adj[u].push_back(make_pair(v, w)); }
bool route(lli from, lli to) { // vec[i]==j: j->iっていうもとに戻れる道がある
vector<lli> vec(V, -1);
dfs(1, from, vec);
return vec[to] >= 0;
}
void dfs(lli ed, lli num, vector<lli> &vec) {
for (auto e : adj[num]) {
if (vec[e.first] != -1)
continue;
vec[e.first] = num;
dfs(ed + 1, e.first, vec);
}
}
vector<int> bfs(int n) {
queue<int> q;
vector<int> vec(V, -1);
q.push(n);
vec[n] = 0;
while (!q.empty()) {
int p = q.front();
q.pop();
for (auto e : adj[p]) {
if (vec[e.first] == -1) {
q.push(e.first);
vec[e.first] = e.second + vec[p];
}
}
}
return vec;
}
vector<int> shortestPath(int src) {
priority_queue<p, vector<p>, greater<p>> pq;
vector<int> dist(V, INF);
pq.push(make_pair(0, src));
dist[src] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
list<pair<int, int>>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i) {
int v = (*i).first;
int weight = (*i).second;
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}
}
for (int i = 0; i < V; i++)
if (dist[i] == INF)
dist[i] = -1;
return dist;
}
};
bool cmpf(const pair<lli, lli> left, const pair<lli, lli> right) {
return left.first < right.first;
}
bool cmps(const pair<lli, lli> left, const pair<lli, lli> right) {
return left.second < right.second;
}
void dfs(lli ed, lli num, vector<map<lli, lli>> &vec) {
lli n = 1;
for (auto e : vec[num]) {
if (n == ed)
n++;
if (e.second != -1)
continue;
vec[num][e.first] = n;
vec[e.first][num] = n;
dfs(n, e.first, vec);
n++;
}
}
lli gcd(lli a, lli b) {
if (a < b)
return gcd(b, a);
lli temp;
while ((temp = a % b)) {
a = b;
b = temp;
}
return b;
}
struct nCr {
vector<lli> f, rf;
lli count = 0;
nCr(lli num) : f(num), rf(num) {
count = num;
f[0] = 1;
rep1(i, num) f[i] = (f[i - 1] * i) % MOD;
rep0(i, num + 1) rf[i] = inv(f[i]);
}
lli inv(lli x) {
lli res = 1;
lli k = MOD - 2;
lli y = x;
while (k) {
if (k & 1)
res = (res * y) % MOD;
y = (y * y) % MOD;
k /= 2;
}
return res;
}
lli C(lli n, lli k) {
lli a = f[n];
lli b = rf[n - k];
lli c = rf[k];
lli bc = (b * c) % MOD;
return (a * bc) % MOD;
}
};
struct UF {
vector<int> par, size;
UF(int N) : par(N), size(N, 1) { rep0(i, N) par[i] = i; }
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] = ry;
size[rx] += size[ry];
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int getSize(int x) { return size[x]; }
};
int main() {
string s;
cin >> s;
lli num = 0, ans = 0, wnum = 0;
while (wnum != s.length() && num < s.length()) {
while (s[num] == 'W' && num < s.length())
num++;
if (num > wnum)
wnum = num;
while (s[wnum] != 'W' && wnum < s.length())
wnum++;
if (!(wnum != s.length() && num < s.length()))
break;
ans += wnum - num;
char c = s[wnum];
s[wnum] = s[num];
s[num] = c;
num++;
}
cout << ans;
}
| replace | 188 | 189 | 188 | 190 | TLE | |
p03200 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;
#define int long long
#define rep(s, i, n) for (int i = s; i < n; i++)
#define c(n) cout << n << endl;
#define ic(n) \
int n; \
cin >> n;
#define sc(s) \
string s; \
cin >> s;
#define mod 1000000007
#define inf 1000000000000000007
#define f first
#define s second
#define mini(c, a, b) *min_element(c + a, c + b)
#define maxi(c, a, b) *max_element(c + a, c + b)
// printf("%.12Lf\n",);
int keta(int x) {
rep(0, i, 30) {
if (x < 10) {
return i + 1;
}
x = x / 10;
}
}
int gcd(int x, int y) {
int aa = x, bb = y;
rep(0, i, 1000) {
aa = aa % bb;
if (aa == 0) {
return bb;
}
bb = bb % aa;
if (bb == 0) {
return aa;
}
}
}
int lcm(int x, int y) {
int aa = x, bb = y;
rep(0, i, 1000) {
aa = aa % bb;
if (aa == 0) {
return x / bb * y;
}
bb = bb % aa;
if (bb == 0) {
return x / aa * y;
}
}
}
bool p(int x) {
if (x == 1)
return false;
rep(2, i, sqrt(x) + 1) {
if (x % i == 0 && x != i) {
return false;
}
}
return true;
}
int max(int a, int b) {
if (a >= b)
return a;
else
return b;
}
int min(int a, int b) {
if (a >= b)
return b;
else
return a;
}
int n2[41];
int nis[41];
int nia[41];
int mody[41];
int nn;
int com(int n, int y) {
int ni = 1;
for (int i = 0; i < 41; i++) {
n2[i] = ni;
ni *= 2;
}
int bunsi = 1, bunbo = 1;
rep(0, i, y) bunsi = (bunsi * (n - i)) % mod;
rep(0, i, y) bunbo = (bunbo * (i + 1)) % mod;
mody[0] = bunbo;
rep(1, i, 41) {
bunbo = (bunbo * bunbo) % mod;
mody[i] = bunbo;
}
rep(0, i, 41) nis[i] = 0;
nn = mod - 2;
for (int i = 40; i >= 0; i -= 1) {
if (nn > n2[i]) {
nis[i]++;
nn -= n2[i];
}
}
nis[0]++;
rep(0, i, 41) {
if (nis[i] == 1) {
bunsi = (bunsi * mody[i]) % mod;
}
}
return bunsi;
}
int yakuwa(int n) {
int sum = 0;
rep(1, i, sqrt(n + 1)) {
if (n % i == 0)
sum += i + n / i;
if (i * i == n)
sum -= i;
}
return sum;
}
bool hei(int n) {
rep(0, i, 100) {
if (i * i == n) {
return true;
}
}
return false;
}
int pow(int b, int a) {
int aa = 1;
rep(0, i, a) aa *= b;
return aa;
}
int sum[114514];
signed main() {
sc(s) int n = s.size();
int bcnt = 0;
int ans = 0;
rep(0, i, n) {
if (s[i] == 'B')
bcnt++;
else
sum[i - bcnt] = bcnt;
}
rep(0, i, n - bcnt) ans += sum[i];
c(ans)
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;
#define int long long
#define rep(s, i, n) for (int i = s; i < n; i++)
#define c(n) cout << n << endl;
#define ic(n) \
int n; \
cin >> n;
#define sc(s) \
string s; \
cin >> s;
#define mod 1000000007
#define inf 1000000000000000007
#define f first
#define s second
#define mini(c, a, b) *min_element(c + a, c + b)
#define maxi(c, a, b) *max_element(c + a, c + b)
// printf("%.12Lf\n",);
int keta(int x) {
rep(0, i, 30) {
if (x < 10) {
return i + 1;
}
x = x / 10;
}
}
int gcd(int x, int y) {
int aa = x, bb = y;
rep(0, i, 1000) {
aa = aa % bb;
if (aa == 0) {
return bb;
}
bb = bb % aa;
if (bb == 0) {
return aa;
}
}
}
int lcm(int x, int y) {
int aa = x, bb = y;
rep(0, i, 1000) {
aa = aa % bb;
if (aa == 0) {
return x / bb * y;
}
bb = bb % aa;
if (bb == 0) {
return x / aa * y;
}
}
}
bool p(int x) {
if (x == 1)
return false;
rep(2, i, sqrt(x) + 1) {
if (x % i == 0 && x != i) {
return false;
}
}
return true;
}
int max(int a, int b) {
if (a >= b)
return a;
else
return b;
}
int min(int a, int b) {
if (a >= b)
return b;
else
return a;
}
int n2[41];
int nis[41];
int nia[41];
int mody[41];
int nn;
int com(int n, int y) {
int ni = 1;
for (int i = 0; i < 41; i++) {
n2[i] = ni;
ni *= 2;
}
int bunsi = 1, bunbo = 1;
rep(0, i, y) bunsi = (bunsi * (n - i)) % mod;
rep(0, i, y) bunbo = (bunbo * (i + 1)) % mod;
mody[0] = bunbo;
rep(1, i, 41) {
bunbo = (bunbo * bunbo) % mod;
mody[i] = bunbo;
}
rep(0, i, 41) nis[i] = 0;
nn = mod - 2;
for (int i = 40; i >= 0; i -= 1) {
if (nn > n2[i]) {
nis[i]++;
nn -= n2[i];
}
}
nis[0]++;
rep(0, i, 41) {
if (nis[i] == 1) {
bunsi = (bunsi * mody[i]) % mod;
}
}
return bunsi;
}
int yakuwa(int n) {
int sum = 0;
rep(1, i, sqrt(n + 1)) {
if (n % i == 0)
sum += i + n / i;
if (i * i == n)
sum -= i;
}
return sum;
}
bool hei(int n) {
rep(0, i, 100) {
if (i * i == n) {
return true;
}
}
return false;
}
int pow(int b, int a) {
int aa = 1;
rep(0, i, a) aa *= b;
return aa;
}
int sum[214514];
signed main() {
sc(s) int n = s.size();
int bcnt = 0;
int ans = 0;
rep(0, i, n) {
if (s[i] == 'B')
bcnt++;
else
sum[i - bcnt] = bcnt;
}
rep(0, i, n - bcnt) ans += sum[i];
c(ans)
} | replace | 137 | 138 | 137 | 138 | 0 | |
p03200 | C++ | Runtime Error | #include <bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
template <class T> using vv = vector<vector<T>>;
template <class T> inline bool MX(T &l, const T &r) {
return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
return l > r ? l = r, 1 : 0;
}
const ll MOD = 1e9 + 7;
int N;
char buf[114514];
LL ans = 0;
LL cnt = 0;
int main() {
scanf("%s", buf);
N = strlen(buf);
rrep(i, N) {
if (buf[i] == 'B') {
ans += cnt;
} else if (buf[i] == 'W') {
cnt++;
} else
assert(0);
}
printf("%lld\n", ans);
}
| #include <bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
template <class T> using vv = vector<vector<T>>;
template <class T> inline bool MX(T &l, const T &r) {
return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
return l > r ? l = r, 1 : 0;
}
const ll MOD = 1e9 + 7;
int N;
char buf[314514];
LL ans = 0;
LL cnt = 0;
int main() {
scanf("%s", buf);
N = strlen(buf);
rrep(i, N) {
if (buf[i] == 'B') {
ans += cnt;
} else if (buf[i] == 'W') {
cnt++;
} else
assert(0);
}
printf("%lld\n", ans);
}
| replace | 32 | 33 | 32 | 33 | 0 | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
inline long long mod(long long n, long long m) { return (n % m + m) % m; }
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};
int main() {
string s;
cin >> s;
int n = s.size();
long long ans = 0;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'W') {
swap(s[i], s[i + 1]);
i -= 2;
ans++;
}
// cout<<s<<endl;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
inline long long mod(long long n, long long m) { return (n % m + m) % m; }
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};
int main() {
string s;
cin >> s;
int n = s.size();
long long ans = 0;
long long ww = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == 'W')
ww++;
if (s[i] == 'B')
ans += ww;
}
cout << ans << endl;
return 0;
}
| replace | 14 | 21 | 14 | 20 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define intt long long
int main() {
std::string s;
std::cin >> s;
intt N = s.size();
intt count = 0;
for (int i = N - 2; i >= 0; --i) {
if (s[i + 1] == 'W' && s[i] == 'B') {
std::swap(s[i], s[i + 1]);
++count;
i += 2;
}
}
std::cout << count << std::endl;
} | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define intt long long
int main() {
std::string s;
std::cin >> s;
intt N = s.size();
intt count = 0;
intt num = 0;
for (int i = 0; i < N; ++i) {
if (s[i] == 'W') {
count += num;
} else
++num;
}
std::cout << count << std::endl;
} | replace | 15 | 21 | 15 | 21 | TLE | |
p03200 | C++ | Runtime Error | /*----------------by syr----------------*/
/*
----- ----- -----
| | |---| _/
| | | \_ /
----- | \ -----
|---\ \ / |\ /|
| | \_/ | \/ |
| | / \ | |
|---/ / \ | |
*/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define PH push
#define MP make_pair
#define PB push_back
#define fst first
#define snd second
#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define x0 x0123456789
#define y0 y0123456789
#define x1 x1234567890
#define y1 y1234567890
#define x2 x2345678901
#define y2 y2345678901
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
const int INF = 1e9 + 7;
const int maxn = 1e5 + 5;
int n, cnt;
char s[maxn];
ll ans;
int main() {
scanf("%s", s);
n = strlen(s);
FOR(i, 0, n) if (s[i] == 'W') ans += cnt;
else ++cnt;
printf("%lld\n", ans);
return 0;
}
| /*----------------by syr----------------*/
/*
----- ----- -----
| | |---| _/
| | | \_ /
----- | \ -----
|---\ \ / |\ /|
| | \_/ | \/ |
| | / \ | |
|---/ / \ | |
*/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define PH push
#define MP make_pair
#define PB push_back
#define fst first
#define snd second
#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define x0 x0123456789
#define y0 y0123456789
#define x1 x1234567890
#define y1 y1234567890
#define x2 x2345678901
#define y2 y2345678901
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
const int INF = 1e9 + 7;
const int maxn = 2e5 + 5;
int n, cnt;
char s[maxn];
ll ans;
int main() {
scanf("%s", s);
n = strlen(s);
FOR(i, 0, n) if (s[i] == 'W') ans += cnt;
else ++cnt;
printf("%lld\n", ans);
return 0;
}
| replace | 59 | 60 | 59 | 60 | 0 | |
p03200 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define INF 10e10
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n, m) for (int i = m; i < n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
// 昇順sort
#define sorti(x) sort(x.begin(), x.end())
// 降順sort
#define sortd(x) sort(x.begin(), x.end(), std::greater<int>())
int main() {
string s;
cin >> s;
ll res = 0;
for (int i = 1; i < s.size(); ++i) {
if (s[i] == 'W' and s[i - 1] == 'B') {
for (int j = i; j >= 1; --j) {
if (s[j] == 'W' and s[j - 1] == 'B') {
s[j] = 'B';
s[j - 1] = 'W';
res++;
} else {
break;
}
}
}
}
cout << res << endl;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define INF 10e10
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n, m) for (int i = m; i < n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
// 昇順sort
#define sorti(x) sort(x.begin(), x.end())
// 降順sort
#define sortd(x) sort(x.begin(), x.end(), std::greater<int>())
int main() {
string s;
cin >> s;
ll res = 0, b = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == 'B')
b++;
else if (s[i] == 'W') {
res += b;
}
}
cout << res << endl;
}
| replace | 30 | 42 | 30 | 36 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int main() {
string S;
cin >> S;
ll ans = 0;
for (int i = 0; i < S.size() - 1; i++) {
if (S[i] == 'B' && S[i + 1] == 'W') {
S[i] = 'W', S[i + 1] = 'B';
i = max(-1, i - 2);
ans++;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int main() {
string S;
cin >> S;
ll ans = 0;
ll cnt = 0;
REP(i, S.size()) {
if (S[i] == 'B') {
cnt++;
} else {
ans += cnt;
}
}
cout << ans << endl;
} | replace | 13 | 18 | 13 | 19 | TLE | |
p03200 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
template <class t, class u> void chmax(t &a, u b) {
if (a < b)
a = b;
}
template <class t, class u> void chmin(t &a, u b) {
if (b < a)
a = b;
}
template <class t> using vc = vector<t>;
template <class t> using vvc = vc<vc<t>>;
using pi = pair<int, int>;
using vi = vc<int>;
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
int SIZE(string s) { return (int)s.size(); }
int main() {
cout << fixed << setprecision(20);
ll ans = 0;
string s;
cin >> s;
int si = SIZE(s);
rep(i, si - 1) {
rep(j, si - 1 - i) {
if (s[j] == 'B' && s[j + 1] == 'W') {
swap(s[j], s[j + 1]);
ans++;
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
template <class t, class u> void chmax(t &a, u b) {
if (a < b)
a = b;
}
template <class t, class u> void chmin(t &a, u b) {
if (b < a)
a = b;
}
template <class t> using vc = vector<t>;
template <class t> using vvc = vc<vc<t>>;
using pi = pair<int, int>;
using vi = vc<int>;
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
int SIZE(string s) { return (int)s.size(); }
int main() {
cout << fixed << setprecision(20);
ll ans = 0;
string s;
cin >> s;
int si = SIZE(s);
int cnt = 0;
rep(i, si) {
if (s[i] == 'B')
cnt++;
else
ans += cnt;
}
cout << ans << endl;
return 0;
}
| replace | 35 | 42 | 35 | 41 | TLE | |
p03200 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountl
#define m_p make_pair
#define inf 200000000000000
#define MAXN 1000001
#define eps 0.0000000001
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
string char_to_str(char c) {
string tem(1, c);
return tem;
}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
#define S second
#define F first
#define int long long
/////////////
signed main() {
fastio;
#ifndef ONLINE_JUDGE
freopen("inputf.in", "r", stdin);
freopen("outputf.in", "w", stdout);
#endif
string s;
cin >> s;
int ans = 0;
int w = 0;
int n = s.length();
for (int i = n - 1; i >= 0; --i) {
if (s[i] == 'W')
++w;
else {
ans += w;
}
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountl
#define m_p make_pair
#define inf 200000000000000
#define MAXN 1000001
#define eps 0.0000000001
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
string char_to_str(char c) {
string tem(1, c);
return tem;
}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
#define S second
#define F first
#define int long long
/////////////
signed main() {
fastio;
string s;
cin >> s;
int ans = 0;
int w = 0;
int n = s.length();
for (int i = n - 1; i >= 0; --i) {
if (s[i] == 'W')
++w;
else {
ans += w;
}
}
cout << ans;
} | delete | 32 | 36 | 32 | 32 | 0 | |
p03200 | C++ | Runtime Error | #include <iostream>
#include <string>
using namespace std;
int main() {
char str[20001];
char c;
int size = 0;
while (cin >> c) {
str[size] = c;
size++;
}
int black = 0, count = 0;
for (int i = 0; i < size; i++) {
if (str[i] == 'B')
black++;
else
count += black;
}
cout << count << endl;
return 0;
} | #include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cin >> str;
int size = str.size();
long black = 0, count = 0;
for (int i = 0; i < size; i++) {
if (str[i] == 'B')
black++;
else
count += black;
}
cout << count << endl;
return 0;
} | replace | 6 | 14 | 6 | 10 | 0 | |
p03200 | C++ | Time Limit Exceeded | // In the name of God
#include <bits/stdc++.h>
using namespace std;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define debug(x) cout << #x ": " << x << endl;
int main() {
IOS string s;
cin >> s;
int ans = 0;
int wn = 0;
for (int i = 0; s[i]; i++)
if (s[i] == 'W')
wn++;
bool flag = true;
while (flag) {
for (int i = 0; i < s.length() - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'W') {
ans++;
s[i] = 'W';
s[i + 1] = 'B';
}
}
flag = false;
for (int i = 0; i < wn; i++) {
if (s[i] == 'B') {
flag = true;
break;
}
}
}
cout << ans;
return 0;
}
| // In the name of God
#include <bits/stdc++.h>
using namespace std;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define debug(x) cout << #x ": " << x << endl;
int main() {
IOS string s;
cin >> s;
unsigned long long b = 0, ans = 0;
for (int i = 0; s[i]; i++) {
if (s[i] == 'B')
b++;
if (s[i] == 'W') {
ans += b;
}
}
cout << ans;
return 0;
}
| replace | 14 | 34 | 14 | 20 | TLE | |
p03200 | C++ | Runtime Error | #include <stdio.h>
int main() {
long i, count = 0, ans = 0;
char s[111111];
for (i = 0; i < 111111; i++)
s[i] = '\0';
scanf("%s", s);
i = 0;
while (s[i] != '\0') {
if (s[i] == 'B')
count++;
else
ans += count;
i++;
}
printf("%ld\n", ans);
return 0;
} | #include <stdio.h>
int main() {
long i, count = 0, ans = 0;
char s[222222];
for (i = 0; i < 222222; i++)
s[i] = '\0';
scanf("%s", s);
i = 0;
while (s[i] != '\0') {
if (s[i] == 'B')
count++;
else
ans += count;
i++;
}
printf("%ld\n", ans);
return 0;
}
| replace | 3 | 5 | 3 | 5 | 0 | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define MAX 1000000
using namespace std;
int n, a[MAX + 5], cnt;
map<int, int> rec;
int cmp(int a, int b) { return a > b; }
void run() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
rec[a[i]]++;
}
sort(a + 1, a + n + 1, cmp);
}
void solve() {
for (int i = 1; i <= n; i++) {
int target = (1 << 30);
for (int j = 30; j >= 1; j--) {
if (rec[target - a[i]] && rec[a[i]]) {
if (target - a[i] == a[i] && rec[a[i]] == 1)
continue;
rec[target - a[i]]--;
rec[a[i]]--;
cnt++;
break;
}
target /= 2;
}
}
}
int main() {
run();
solve();
printf("%d", cnt);
return 0;
} | #include <bits/stdc++.h>
#define MAX 1000000
using namespace std;
int n, a[MAX + 5], cnt;
map<int, int> rec;
int cmp(int a, int b) { return a > b; }
void run() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
rec[a[i]]++;
}
sort(a + 1, a + n + 1, cmp);
}
void solve() {
for (int i = 1; i <= n; i++) {
int target = 1073741824;
for (int j = 30; j >= 1; j--) {
if (rec[target - a[i]] && rec[a[i]]) {
if (target - a[i] == a[i] && rec[a[i]] == 1)
continue;
rec[target - a[i]]--;
rec[a[i]]--;
cnt++;
break;
}
target /= 2;
}
}
}
int main() {
run();
solve();
printf("%d", cnt);
return 0;
} | replace | 16 | 17 | 16 | 17 | TLE | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1E5 + 2;
int n;
vector<pair<long long, int>> a(MAXN);
vector<bool> b(MAXN, 0);
vector<pair<int, int>> v;
long long pairing(long long X) {
int l = 0, r = 60, msb = 60;
while (l <= r) {
int mid = (l + r) >> 1;
long long val = (1LL << mid);
if (val > X)
msb = mid, r = mid - 1;
else
l = mid + 1;
}
return (1LL << msb) - X;
}
int main() {
// your code goes here
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin() + 1, a.begin() + n + 1);
for (int i = n; i >= 1; --i) {
if (b[i])
continue;
long long X = pairing(a[i].first);
int l = 1, r = i - 1, j = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (a[mid].first < X)
l = mid + 1;
else if (a[mid].first > X)
r = mid - 1;
else if (b[mid])
l = mid + 1;
else
j = mid, r = mid - 1;
}
if (j != -1) {
v.push_back({a[i].second, a[j].second});
b[j] = 1;
}
}
cout << v.size() << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 2E5 + 2;
int n;
vector<pair<long long, int>> a(MAXN);
vector<bool> b(MAXN, 0);
vector<pair<int, int>> v;
long long pairing(long long X) {
int l = 0, r = 60, msb = 60;
while (l <= r) {
int mid = (l + r) >> 1;
long long val = (1LL << mid);
if (val > X)
msb = mid, r = mid - 1;
else
l = mid + 1;
}
return (1LL << msb) - X;
}
int main() {
// your code goes here
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin() + 1, a.begin() + n + 1);
for (int i = n; i >= 1; --i) {
if (b[i])
continue;
long long X = pairing(a[i].first);
int l = 1, r = i - 1, j = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (a[mid].first < X)
l = mid + 1;
else if (a[mid].first > X)
r = mid - 1;
else if (b[mid])
l = mid + 1;
else
j = mid, r = mid - 1;
}
if (j != -1) {
v.push_back({a[i].second, a[j].second});
b[j] = 1;
}
}
cout << v.size() << "\n";
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define foor(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, n) for (int i = (n); i--;)
#define roof(i, b, a) for (int i = (b); i >= (a); --i)
#define elsif else if
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x))
#define Reverse(x) reverse(all(x))
#define PQ priority_queue
#define NP(x) next_permutation(all(x))
#define M_PI 3.14159265358979323846
using namespace std;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef unsigned long long ull;
typedef vector<ull> vu;
typedef vector<vu> vvu;
typedef double dbl;
typedef vector<dbl> vd;
typedef vector<vd> vvd;
typedef string str;
typedef vector<str> vs;
typedef vector<vs> vvs;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<int, int> mii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> mll;
typedef pair<dbl, dbl> pdd;
typedef vector<pdd> vpdd;
typedef map<dbl, dbl> mdd;
typedef pair<str, str> pss;
typedef vector<pss> vpss;
typedef map<str, str> mss;
typedef pair<int, ll> pil;
typedef vector<pil> vpil;
typedef map<int, ll> mil;
typedef pair<ll, int> pli;
typedef vector<pli> vpli;
typedef map<ll, int> mli;
typedef pair<dbl, int> pdi;
typedef vector<pdi> vpdi;
typedef map<dbl, int> mdi;
template <typename T> vector<T> &operator<<(vector<T> &v, const T t) {
v.push_back(t);
return v;
}
template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) {
m.insert(t);
return m;
}
template <typename T> set<T> &operator<<(set<T> &s, const T t) {
s.insert(t);
return s;
}
template <typename T> stack<T> &operator<<(stack<T> &s, const T t) {
s.push(t);
return s;
}
template <typename T> queue<T> &operator<<(queue<T> &q, const T t) {
q.push(t);
return q;
}
template <typename T, typename U>
PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) {
q.push(t);
return q;
}
template <typename T, typename U>
istream &operator>>(istream &s, pair<T, U> &p) {
return s >> p.first >> p.second;
}
template <typename T> istream &operator>>(istream &s, vector<T> &v) {
fr(i, v.size()) { s >> v[i]; }
return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> p) {
return s << p.first << " " << p.second;
}
// template<typename T>ostream&operator<<(ostream&s,const vector<T>v){for(auto
// a:v){s<<a<<endl;}return s;}
template <typename T> ostream &operator<<(ostream &s, const vector<T> v) {
fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; }
return s;
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
return {a.first + b.first, a.second + b.second};
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
return {a.first - b.first, a.second - b.second};
}
void print(void) { cout << endl; }
template <typename T> void print(T t) { cout << t << endl; }
template <typename T, typename... U> void print(T &&t, U &&...u) {
cout << t << " ";
print(forward<U>(u)...);
}
bool YN(bool b) {
print(b ? "YES" : "NO");
return b;
}
bool PI(bool b) {
print(b ? "POSSIBLE" : "IMPOSSIBLE");
return b;
}
bool Yn(bool b) {
print(b ? "Yes" : "No");
return b;
}
bool Pi(bool b) {
print(b ? "Possible" : "Impossible");
return b;
}
bool yn(bool b) {
print(b ? "yes" : "no");
return b;
}
bool pi(bool b) {
print(b ? "possible" : "impossible");
return b;
}
const int MD = 1e9 + 7;
template <typename T> str to_string(const T &n) {
ostringstream s;
s << n;
return s.str();
}
template <typename T, typename U>
vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> E, const U s,
const T inf) {
using P = pair<T, U>;
vector<P> d;
fr(i, E.size()) { d << P{inf, i}; }
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
template <typename T, typename U>
map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s,
const T inf) {
using P = pair<T, U>;
map<U, P> d;
for (pair<U, vector<P>> e : E) {
d[e.first] = P{inf, e.first};
}
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
template <typename T> T distsq(pair<T, T> a, pair<T, T> b) {
return (a.first - b.first) * (a.first - b.first) +
(a.second - b.second) * (a.second - b.second);
}
template <typename T> T max(const vector<T> a) {
T m = a[0];
for (T e : a) {
m = max(m, e);
}
return m;
}
template <typename T> T min(const vector<T> a) {
T m = a[0];
for (T e : a) {
m = min(m, e);
}
return m;
}
template <typename T> T gcd(const T a, const T b) {
return a ? gcd(b % a, a) : b;
}
template <typename T> T gcd(const vector<T> a) {
T g = a[0];
for (T e : a) {
g = gcd(g, e);
}
return g;
}
template <typename T> vector<T> LIS(const vector<T> A) {
vi B;
for (T a : A) {
auto it = lower_bound(all(B), a);
if (it == B.end()) {
B << a;
} else {
*it = a;
}
}
return B;
}
template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) {
if (V.size() <= 3) {
return V;
}
Sort(V);
rf(i, V.size() - 1) V << V[i];
vector<pair<T, T>> r;
for (pair<T, T> p : V) {
int s = r.size();
while (s >= 2 &&
(p.second - r[s - 1].second) * (p.first - r[s - 2].first) <
(p.second - r[s - 2].second) * (p.first - r[s - 1].first)) {
r.pop_back();
--s;
}
r << p;
}
r.pop_back();
return r;
}
class UnionFind {
vi p, r, s;
public:
UnionFind(int N) {
p = r = vi(N);
s = vi(N, 1);
fr(i, N) { p[i] = i; }
}
int find(int i) { return p[i] = p[i] == i ? i : find(p[i]); }
void unite(int a, int b) {
if (r[a = find(a)] > r[b = find(b)]) {
swap(a, b);
}
s[b] += s[a];
r[p[a] = b] += r[a] == r[b];
}
bool same(int a, int b) { return find(a) == find(b); }
int size(int x) { return s[find(x)]; }
};
ll strmod(const str &s, const int m) {
ll x = 0;
fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; }
return x;
}
vvl mul(const vvl &A, const vvl &B, const int m) {
vvl C;
fr(y, A.size()) { C << vl(B[y].size()); }
fr(y, C.size()) {
fr(x, C[y].size()) {
fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; }
}
}
return C;
}
vvl pow(const vvl &A, const ll n, const int m) {
vvl B;
fr(y, A.size()) { B << vl(A.size()); }
if (n == 0) {
fr(i, B.size()) { B[i][i] = 1; }
}
elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); }
else {
vvl C = pow(A, n / 2, m);
B = mul(C, C, m);
}
return B;
}
ll pow(const ll a, const ll n, const int m) {
ll t;
return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) *
(t = pow(a, n >> 1, m), t * t % m) % m
: !!a;
}
ll inv(const ll x, const int p) { return pow(x, p - 2, p); }
ll inv(const ll x) { return inv(x, MD); }
vpll fact(const int n, const int p) {
vpll v(n + 1);
v[0].first = 1;
foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; }
v[n].second = inv(v[n].first, p);
roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; }
return v;
}
class Combination {
const vpll f;
const int M;
public:
Combination(int n, int m) : f(fact(n, m)), M(m) {}
Combination(int n) : Combination(n, MD) {}
ll P(int n, int k) {
return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M;
}
ll C(int n, int k) { return P(n, k) * f[k].second % M; }
ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); }
};
ll C2(const int n) { return (ll)n * ~-n / 2; }
ll sum(const vi a) {
ll s = 0;
for (int e : a) {
s += e;
}
return s;
}
ll sum(const vl a) {
ll s = 0;
for (ll e : a) {
s += e;
}
return s;
}
template <typename T> int MSB(T N) {
int r = -1;
for (; N > 0; N /= 2) {
++r;
}
return r;
}
template <typename T> class SegmentTree {
vector<T> S;
T (*const op)(T a, T b);
const T zero;
const int B;
public:
SegmentTree(int N, T (*f)(T a, T b), const T zero)
: S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero),
B(1 << MSB(N - 1) + 1) {}
SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero)
: SegmentTree(v.size(), f, zero) {
fr(i, v) { S[S.size() / 2 + i] = v[i]; }
roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); }
}
T calc(int l, int r) {
l += B;
r += B;
if (l > r) {
return zero;
}
if (l == r) {
return S[l];
}
T L = S[l], R = S[r];
for (; l / 2 < r / 2; l /= 2, r /= 2) {
if (l % 2 == 0) {
L = op(L, S[l + 1]);
}
if (r % 2 == 1) {
R = op(S[r - 1], R);
}
}
return op(L, R);
}
void replace(int i, T x) {
for (S[i += B] = x; i != 1; i /= 2) {
if (i % 2) {
S[i / 2] = op(S[i - 1], S[i]);
} else {
S[i / 2] = op(S[i], S[i + 1]);
}
}
}
void add(int i, T x) { replace(i, op(S[B + i], x)); }
T top() { return S[1]; }
};
ll BITsum(vl &B, int i) {
ll z = 0;
while (i > 0) {
z += B[i];
i -= i & -i;
}
return z;
}
void BITadd(vl &B, int i, ll x) {
while (i < B.size()) {
B[i] += x;
i += i & -i;
}
}
ll fib(const ll n, const int m) {
ll a, b, c, d, A, B, C, D;
a = 1;
b = 0;
c = 0;
d = 1;
rf(i, 63) {
A = a * a + b * c;
B = a * b + b * d;
C = c * a + d * c;
D = c * b + d * d;
if (n >> i & 1) {
a = A;
b = B;
c = C;
d = D;
A = a + b;
B = a;
C = c + d;
D = c;
}
a = A % m;
b = B % m;
c = C % m;
d = D % m;
}
return b;
}
vi primes(int n) {
vb b(n + 1);
vi p;
foor(i, 2, n) {
if (!b[i]) {
p << i;
for (int j = 2 * i; j <= n; j += i) {
b[j] = true;
}
}
}
return p;
}
vb isprime(const int n) {
vb v(n + 1, true);
v[0] = v[1] = false;
foor(i, 2, n) {
if (v[i]) {
for (int j = 2 * i; j <= n; j += i) {
v[j] = false;
}
}
}
return v;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vi A(N);
cin >> A;
Sort(A);
Reverse(A);
int z = 0;
mii m;
fr(i, N) { ++m[A[i]]; }
fr(i, N) {
if (m[A[i]]) {
rf(j, 31) {
int k = (1 << j) - A[i];
if (A[i] == k ? m[k] >= 2 : m[k]) {
--m[k];
--m[A[i]];
++z;
break;
}
}
}
}
print(z);
return 0;
}
| #include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define foor(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, n) for (int i = (n); i--;)
#define roof(i, b, a) for (int i = (b); i >= (a); --i)
#define elsif else if
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x))
#define Reverse(x) reverse(all(x))
#define PQ priority_queue
#define NP(x) next_permutation(all(x))
#define M_PI 3.14159265358979323846
using namespace std;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef unsigned long long ull;
typedef vector<ull> vu;
typedef vector<vu> vvu;
typedef double dbl;
typedef vector<dbl> vd;
typedef vector<vd> vvd;
typedef string str;
typedef vector<str> vs;
typedef vector<vs> vvs;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<int, int> mii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> mll;
typedef pair<dbl, dbl> pdd;
typedef vector<pdd> vpdd;
typedef map<dbl, dbl> mdd;
typedef pair<str, str> pss;
typedef vector<pss> vpss;
typedef map<str, str> mss;
typedef pair<int, ll> pil;
typedef vector<pil> vpil;
typedef map<int, ll> mil;
typedef pair<ll, int> pli;
typedef vector<pli> vpli;
typedef map<ll, int> mli;
typedef pair<dbl, int> pdi;
typedef vector<pdi> vpdi;
typedef map<dbl, int> mdi;
template <typename T> vector<T> &operator<<(vector<T> &v, const T t) {
v.push_back(t);
return v;
}
template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) {
m.insert(t);
return m;
}
template <typename T> set<T> &operator<<(set<T> &s, const T t) {
s.insert(t);
return s;
}
template <typename T> stack<T> &operator<<(stack<T> &s, const T t) {
s.push(t);
return s;
}
template <typename T> queue<T> &operator<<(queue<T> &q, const T t) {
q.push(t);
return q;
}
template <typename T, typename U>
PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) {
q.push(t);
return q;
}
template <typename T, typename U>
istream &operator>>(istream &s, pair<T, U> &p) {
return s >> p.first >> p.second;
}
template <typename T> istream &operator>>(istream &s, vector<T> &v) {
fr(i, v.size()) { s >> v[i]; }
return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> p) {
return s << p.first << " " << p.second;
}
// template<typename T>ostream&operator<<(ostream&s,const vector<T>v){for(auto
// a:v){s<<a<<endl;}return s;}
template <typename T> ostream &operator<<(ostream &s, const vector<T> v) {
fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; }
return s;
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
return {a.first + b.first, a.second + b.second};
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
return {a.first - b.first, a.second - b.second};
}
void print(void) { cout << endl; }
template <typename T> void print(T t) { cout << t << endl; }
template <typename T, typename... U> void print(T &&t, U &&...u) {
cout << t << " ";
print(forward<U>(u)...);
}
bool YN(bool b) {
print(b ? "YES" : "NO");
return b;
}
bool PI(bool b) {
print(b ? "POSSIBLE" : "IMPOSSIBLE");
return b;
}
bool Yn(bool b) {
print(b ? "Yes" : "No");
return b;
}
bool Pi(bool b) {
print(b ? "Possible" : "Impossible");
return b;
}
bool yn(bool b) {
print(b ? "yes" : "no");
return b;
}
bool pi(bool b) {
print(b ? "possible" : "impossible");
return b;
}
const int MD = 1e9 + 7;
template <typename T> str to_string(const T &n) {
ostringstream s;
s << n;
return s.str();
}
template <typename T, typename U>
vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> E, const U s,
const T inf) {
using P = pair<T, U>;
vector<P> d;
fr(i, E.size()) { d << P{inf, i}; }
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
template <typename T, typename U>
map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s,
const T inf) {
using P = pair<T, U>;
map<U, P> d;
for (pair<U, vector<P>> e : E) {
d[e.first] = P{inf, e.first};
}
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
template <typename T> T distsq(pair<T, T> a, pair<T, T> b) {
return (a.first - b.first) * (a.first - b.first) +
(a.second - b.second) * (a.second - b.second);
}
template <typename T> T max(const vector<T> a) {
T m = a[0];
for (T e : a) {
m = max(m, e);
}
return m;
}
template <typename T> T min(const vector<T> a) {
T m = a[0];
for (T e : a) {
m = min(m, e);
}
return m;
}
template <typename T> T gcd(const T a, const T b) {
return a ? gcd(b % a, a) : b;
}
template <typename T> T gcd(const vector<T> a) {
T g = a[0];
for (T e : a) {
g = gcd(g, e);
}
return g;
}
template <typename T> vector<T> LIS(const vector<T> A) {
vi B;
for (T a : A) {
auto it = lower_bound(all(B), a);
if (it == B.end()) {
B << a;
} else {
*it = a;
}
}
return B;
}
template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) {
if (V.size() <= 3) {
return V;
}
Sort(V);
rf(i, V.size() - 1) V << V[i];
vector<pair<T, T>> r;
for (pair<T, T> p : V) {
int s = r.size();
while (s >= 2 &&
(p.second - r[s - 1].second) * (p.first - r[s - 2].first) <
(p.second - r[s - 2].second) * (p.first - r[s - 1].first)) {
r.pop_back();
--s;
}
r << p;
}
r.pop_back();
return r;
}
class UnionFind {
vi p, r, s;
public:
UnionFind(int N) {
p = r = vi(N);
s = vi(N, 1);
fr(i, N) { p[i] = i; }
}
int find(int i) { return p[i] = p[i] == i ? i : find(p[i]); }
void unite(int a, int b) {
if (r[a = find(a)] > r[b = find(b)]) {
swap(a, b);
}
s[b] += s[a];
r[p[a] = b] += r[a] == r[b];
}
bool same(int a, int b) { return find(a) == find(b); }
int size(int x) { return s[find(x)]; }
};
ll strmod(const str &s, const int m) {
ll x = 0;
fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; }
return x;
}
vvl mul(const vvl &A, const vvl &B, const int m) {
vvl C;
fr(y, A.size()) { C << vl(B[y].size()); }
fr(y, C.size()) {
fr(x, C[y].size()) {
fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; }
}
}
return C;
}
vvl pow(const vvl &A, const ll n, const int m) {
vvl B;
fr(y, A.size()) { B << vl(A.size()); }
if (n == 0) {
fr(i, B.size()) { B[i][i] = 1; }
}
elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); }
else {
vvl C = pow(A, n / 2, m);
B = mul(C, C, m);
}
return B;
}
ll pow(const ll a, const ll n, const int m) {
ll t;
return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) *
(t = pow(a, n >> 1, m), t * t % m) % m
: !!a;
}
ll inv(const ll x, const int p) { return pow(x, p - 2, p); }
ll inv(const ll x) { return inv(x, MD); }
vpll fact(const int n, const int p) {
vpll v(n + 1);
v[0].first = 1;
foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; }
v[n].second = inv(v[n].first, p);
roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; }
return v;
}
class Combination {
const vpll f;
const int M;
public:
Combination(int n, int m) : f(fact(n, m)), M(m) {}
Combination(int n) : Combination(n, MD) {}
ll P(int n, int k) {
return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M;
}
ll C(int n, int k) { return P(n, k) * f[k].second % M; }
ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); }
};
ll C2(const int n) { return (ll)n * ~-n / 2; }
ll sum(const vi a) {
ll s = 0;
for (int e : a) {
s += e;
}
return s;
}
ll sum(const vl a) {
ll s = 0;
for (ll e : a) {
s += e;
}
return s;
}
template <typename T> int MSB(T N) {
int r = -1;
for (; N > 0; N /= 2) {
++r;
}
return r;
}
template <typename T> class SegmentTree {
vector<T> S;
T (*const op)(T a, T b);
const T zero;
const int B;
public:
SegmentTree(int N, T (*f)(T a, T b), const T zero)
: S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero),
B(1 << MSB(N - 1) + 1) {}
SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero)
: SegmentTree(v.size(), f, zero) {
fr(i, v) { S[S.size() / 2 + i] = v[i]; }
roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); }
}
T calc(int l, int r) {
l += B;
r += B;
if (l > r) {
return zero;
}
if (l == r) {
return S[l];
}
T L = S[l], R = S[r];
for (; l / 2 < r / 2; l /= 2, r /= 2) {
if (l % 2 == 0) {
L = op(L, S[l + 1]);
}
if (r % 2 == 1) {
R = op(S[r - 1], R);
}
}
return op(L, R);
}
void replace(int i, T x) {
for (S[i += B] = x; i != 1; i /= 2) {
if (i % 2) {
S[i / 2] = op(S[i - 1], S[i]);
} else {
S[i / 2] = op(S[i], S[i + 1]);
}
}
}
void add(int i, T x) { replace(i, op(S[B + i], x)); }
T top() { return S[1]; }
};
ll BITsum(vl &B, int i) {
ll z = 0;
while (i > 0) {
z += B[i];
i -= i & -i;
}
return z;
}
void BITadd(vl &B, int i, ll x) {
while (i < B.size()) {
B[i] += x;
i += i & -i;
}
}
ll fib(const ll n, const int m) {
ll a, b, c, d, A, B, C, D;
a = 1;
b = 0;
c = 0;
d = 1;
rf(i, 63) {
A = a * a + b * c;
B = a * b + b * d;
C = c * a + d * c;
D = c * b + d * d;
if (n >> i & 1) {
a = A;
b = B;
c = C;
d = D;
A = a + b;
B = a;
C = c + d;
D = c;
}
a = A % m;
b = B % m;
c = C % m;
d = D % m;
}
return b;
}
vi primes(int n) {
vb b(n + 1);
vi p;
foor(i, 2, n) {
if (!b[i]) {
p << i;
for (int j = 2 * i; j <= n; j += i) {
b[j] = true;
}
}
}
return p;
}
vb isprime(const int n) {
vb v(n + 1, true);
v[0] = v[1] = false;
foor(i, 2, n) {
if (v[i]) {
for (int j = 2 * i; j <= n; j += i) {
v[j] = false;
}
}
}
return v;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vi A(N);
cin >> A;
Sort(A);
Reverse(A);
int z = 0;
mii m;
fr(i, N) { ++m[A[i]]; }
fr(i, N) {
if (m[A[i]]) {
rf(j, 31) {
int k = (1 << j) - A[i];
if (k <= 0)
continue;
if (A[i] == k ? m[k] >= 2 : m[k]) {
--m[k];
--m[A[i]];
++z;
break;
}
}
}
}
print(z);
return 0;
}
| insert | 475 | 475 | 475 | 477 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)
#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
return (b < a) ? (a = b, 1) : 0;
}
using ll = long long;
using R = long double;
const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max(x, 0.0L)); }
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const pid_t pid = getpid();
// Problem Specific Parameter:
map<ll, int> cnt;
int main(void) {
int n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
rep(i, n) cnt[a[i]]++;
int ans = 0;
rrep(k, 31) {
const ll tar = (1LL << k);
for (auto &it : cnt) {
const ll val = it.first;
const ll nval = tar - it.first;
if (val < nval)
continue;
if (val == nval) {
const int num = it.second / 2;
ans += num;
it.second -= 2 * num;
} else {
const int num = min(it.second, cnt[nval]);
ans += num;
it.second -= num;
cnt[nval] -= num;
}
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)
#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
return (b < a) ? (a = b, 1) : 0;
}
using ll = long long;
using R = long double;
const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max(x, 0.0L)); }
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const pid_t pid = getpid();
// Problem Specific Parameter:
map<ll, int> cnt;
int main(void) {
int n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
rep(i, n) cnt[a[i]]++;
int ans = 0;
rrep(k, 31) {
const ll tar = (1LL << k);
for (auto &it : cnt) {
const ll val = it.first;
const ll nval = tar - it.first;
if (val < tar / 2 or tar <= val)
continue;
if (val == nval) {
const int num = it.second / 2;
ans += num;
it.second -= 2 * num;
} else {
const int num = min(it.second, cnt[nval]);
ans += num;
it.second -= num;
cnt[nval] -= num;
}
}
}
cout << ans << endl;
return 0;
} | replace | 56 | 57 | 56 | 57 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
long long a[100];
long long num[300005];
set<long long> s;
set<int> ss;
int cmp(int a, int b) { return a > b; }
map<int, int> m;
int main() {
// long long a[100];
set<int>::const_iterator iter;
set<int>::iterator first;
set<int>::iterator second;
a[0] = 1;
for (int i = 1; i <= 35; i++)
a[i] = a[i - 1] * 2;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]);
m[num[i]]++;
}
sort(num, num + n);
int cnt = 0;
for (int i = n - 1; i >= 0; i--) {
if (m[num[i]]-- > 0)
for (int j = 32; j >= 1; j--)
if (m[a[j] - num[i]]-- > 0) {
cnt++;
// printf("%d %d %d\n",a[j],num[i],a[j]-num[i]);
// break;
}
}
cout << cnt << endl;
} | #include <bits/stdc++.h>
using namespace std;
long long a[100];
long long num[300005];
set<long long> s;
set<int> ss;
int cmp(int a, int b) { return a > b; }
map<int, int> m;
int main() {
// long long a[100];
set<int>::const_iterator iter;
set<int>::iterator first;
set<int>::iterator second;
a[0] = 1;
for (int i = 1; i <= 35; i++)
a[i] = a[i - 1] * 2;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]);
m[num[i]]++;
}
sort(num, num + n);
int cnt = 0;
for (int i = n - 1; i >= 0; i--) {
if (m[num[i]]-- > 0)
for (int j = 32; j >= 1; j--)
if (m[a[j] - num[i]]-- > 0) {
cnt++;
// printf("%d %d %d\n",a[j],num[i],a[j]-num[i]);
// break;
} else if (a[j] <= num[i]) {
break;
}
}
cout << cnt << endl;
} | insert | 31 | 31 | 31 | 33 | TLE | |
p03201 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;
const int V = 100005;
ll dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};
int main() {
ll n;
cin >> n;
multiset<ll> se;
rep(i, n) {
ll a;
cin >> a;
se.insert(a);
}
ll ans = 0;
multiset<ll> se2;
for (ll bit = 35; bit >= 1; bit--) {
ll b = 1LL << bit;
se2 = se;
for (auto i : se2) {
auto itr = se.find(i);
auto itr2 = se.find(b - i);
if (itr == itr2)
itr2++;
if (itr2 != se.end() && *itr + *itr2 == b) {
se.erase(itr);
se.erase(itr2);
ans++;
}
if (se.size() == 0 || se.size() == 1)
break;
}
if (se.size() == 0 || se.size() == 1)
break;
}
cout << ans << endl;
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;
const int V = 100005;
ll dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};
int main() {
ll n;
cin >> n;
multiset<ll> se;
rep(i, n) {
ll a;
cin >> a;
se.insert(a);
}
ll ans = 0;
multiset<ll> se2;
for (ll bit = 35; bit >= 1; bit--) {
ll b = 1LL << bit;
se2 = se;
for (auto i : se2) {
auto itr = se.find(i);
if (itr == se.end())
continue;
auto itr2 = se.find(b - i);
if (itr == itr2)
itr2++;
if (itr2 != se.end() && *itr + *itr2 == b) {
se.erase(itr);
se.erase(itr2);
ans++;
}
if (se.size() == 0 || se.size() == 1)
break;
}
if (se.size() == 0 || se.size() == 1)
break;
}
cout << ans << endl;
}
| insert | 42 | 42 | 42 | 44 | 0 | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define int long long
#define f(i, n) for (int i = 0; i < n; i++)
#define F first
#define S second
#define mod 1000000007
#define P pair<int, int>
using namespace std;
vector<int> v;
int j[50];
signed main() {
int n, c, ans = 0;
cin >> n;
f(i, n) {
cin >> c;
v.push_back(c);
}
sort(v.begin(), v.end());
j[0] = 1;
for (int i = 1; i < 31; i++)
j[i] = j[i - 1] * 2;
for (int i = n - 1; i >= 1; i--) {
f(k, 31) {
if (j[k] > v[i]) {
if (binary_search(v.begin(), v.begin() + i, j[k] - v[i])) {
ans++;
auto Itr = upper_bound(v.begin(), v.begin() + i, j[k] - v[i]) - 1;
v.erase(v.begin() + (Itr - v.begin()));
i--;
}
break;
}
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define f(i, n) for (int i = 0; i < n; i++)
#define F first
#define S second
#define mod 1000000007
#define P pair<int, int>
using namespace std;
vector<int> v;
int j[50];
signed main() {
int n, c, ans = 0;
cin >> n;
f(i, n) {
cin >> c;
v.push_back(c);
}
sort(v.begin(), v.end());
j[0] = 1;
for (int i = 1; i < 31; i++)
j[i] = j[i - 1] * 2;
for (int i = n - 1; i >= 1; i--) {
f(k, 31) {
if (j[k] > v[i]) {
if (binary_search(v.begin(), v.begin() + i, j[k] - v[i])) {
ans++;
auto Itr = upper_bound(v.begin(), v.begin() + i, j[k] - v[i]) - 1;
v.erase(v.begin() + (Itr - v.begin()));
i--;
}
break;
}
}
}
cout << ans << endl;
}
| delete | 1 | 2 | 1 | 1 | TLE | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define fastcin() \
ios_base::sync_with_stdio(false); \
cin.tie(0)
using namespace std;
typedef pair<int, int> ii;
const int maxn = 100050;
int n, p[33], a[maxn];
bool vis[maxn];
map<int, vector<int>> b;
signed main() {
for (int i = 0; i <= 32; i++)
p[i] = (1 << i);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++)
b[a[i]].pb(i);
int ans = 0;
for (int i = n; i >= 1; i--) {
if (vis[i])
continue;
b[a[i]].pop_back();
for (int j = 32; j >= 0; j--) {
if (p[j] > 2 * a[i] or p[j] <= a[i] or
(!b.count(p[j] - a[i]) or b[p[j] - a[i]].empty()))
continue;
int id = *b[p[j] - a[i]].rbegin();
b[p[j] - a[i]].pop_back();
ans++;
vis[id] = 1;
break;
}
}
cout << ans;
}
| #include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define fastcin() \
ios_base::sync_with_stdio(false); \
cin.tie(0)
using namespace std;
typedef pair<int, int> ii;
const int maxn = 200050;
int n, p[33], a[maxn];
bool vis[maxn];
map<int, vector<int>> b;
signed main() {
for (int i = 0; i <= 32; i++)
p[i] = (1 << i);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++)
b[a[i]].pb(i);
int ans = 0;
for (int i = n; i >= 1; i--) {
if (vis[i])
continue;
b[a[i]].pop_back();
for (int j = 32; j >= 0; j--) {
if (p[j] > 2 * a[i] or p[j] <= a[i] or
(!b.count(p[j] - a[i]) or b[p[j] - a[i]].empty()))
continue;
int id = *b[p[j] - a[i]].rbegin();
b[p[j] - a[i]].pop_back();
ans++;
vis[id] = 1;
break;
}
}
cout << ans;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p03201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<int, int> mp;
int a[201010];
int main() {
int n, i, j;
int ans = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
for (i = 1; i <= n; i++)
mp[a[i]]++;
for (i = n; i; i--) {
// printf("%d %d\n",i,mp[a[i]]);
if (mp[a[i]] == 0)
continue;
for (j = 30; j >= 0; j--) {
if ((mp[(1 << j) - a[i]] > 0 && (1 << j) != a[i] * 2) ||
(mp[a[i]] > 1 && (1 << j) == a[i] * 2)) {
// printf("%d|\n",(1<<j));
ans++, mp[a[i]]--, mp[(1 << j) - a[i]]--;
break;
}
}
}
printf("%d\n", ans);
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<int, int> mp;
int a[201010];
int main() {
int n, i, j;
int ans = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
for (i = 1; i <= n; i++)
mp[a[i]]++;
for (i = n; i; i--) {
// printf("%d %d\n",i,mp[a[i]]);
if (mp[a[i]] == 0)
continue;
for (j = 30; j >= 0; j--) {
if ((1 << j) - a[i] < 0)
break;
if (!mp.count((1 << j) - a[i]))
continue;
if ((mp[(1 << j) - a[i]] > 0 && (1 << j) != a[i] * 2) ||
(mp[a[i]] > 1 && (1 << j) == a[i] * 2)) {
// printf("%d|\n",(1<<j));
ans++, mp[a[i]]--, mp[(1 << j) - a[i]]--;
break;
}
}
}
printf("%d\n", ans);
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
return 0;
}
| insert | 23 | 23 | 23 | 27 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(long long N, vector<long long> A) {
map<long long, ll> mp;
for (int i = 0; i < N; ++i) {
mp[A[i]]++;
}
sort(A.begin(), A.end());
A.erase(unique(A.begin(), A.end()), A.end());
long long res = 0;
for (int k = 30; k > 0; --k) {
long long t = 1LL << k;
for (auto a : A) {
ll b = t - a;
if (a != b) {
ll m = min(mp[a], mp[b]);
mp[a] -= m;
mp[b] -= m;
res += m;
} else {
res += mp[a] / 2;
mp[a] %= 2;
}
}
}
cout << res << endl;
}
int main() {
long long N;
scanf("%lld", &N);
vector<long long> A(N - 1 + 1);
for (int i = 0; i <= N - 1; i++) {
scanf("%lld", &A[i]);
}
solve(N, A);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(long long N, vector<long long> A) {
map<long long, ll> mp;
for (int i = 0; i < N; ++i) {
mp[A[i]]++;
}
sort(A.begin(), A.end());
A.erase(unique(A.begin(), A.end()), A.end());
long long res = 0;
for (int k = 30; k > 0; --k) {
long long t = 1LL << k;
for (int i = 0; i < A.size() && A[i] <= t / 2; ++i) {
ll a = A[i];
ll b = t - a;
if (a != b) {
ll m = min(mp[a], mp[b]);
mp[a] -= m;
mp[b] -= m;
res += m;
} else {
res += mp[a] / 2;
mp[a] %= 2;
}
}
}
cout << res << endl;
}
int main() {
long long N;
scanf("%lld", &N);
vector<long long> A(N - 1 + 1);
for (int i = 0; i <= N - 1; i++) {
scanf("%lld", &A[i]);
}
solve(N, A);
return 0;
}
| replace | 15 | 16 | 15 | 17 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0};
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
ll n;
cin >> n;
map<ll, ll> mp;
vl v(n);
rep(i, n) cin >> v[i], mp[v[i]]++;
sort(all(v));
ll res = 0;
per(i, n) {
if (!mp[v[i]])
continue;
mp[v[i]]--;
bool check = false;
rep(j, 31) {
if (mp[(1 << j) - v[i]]) {
check = true;
res++;
mp[(1 << j) - v[i]]--;
break;
}
}
if (!check)
mp[v[i]]++;
}
cout << res << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0};
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
ll n;
cin >> n;
map<ll, ll> mp;
vl v(n);
rep(i, n) cin >> v[i], mp[v[i]]++;
sort(all(v));
ll res = 0;
per(i, n) {
if (!mp[v[i]])
continue;
mp[v[i]]--;
bool check = false;
per(j, 31) {
if ((1 << j) - v[i] <= 0)
break;
if (mp[(1 << j) - v[i]]) {
check = true;
res++;
mp[(1 << j) - v[i]]--;
break;
}
}
if (!check)
mp[v[i]]++;
}
cout << res << endl;
} | replace | 55 | 56 | 55 | 58 | TLE | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int a[20010];
int main() {
int n, ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
mp[a[i]]++;
}
while (!mp.empty()) {
map<int, int>::iterator it = mp.end();
it--;
int x = it->first;
if (mp[x] == 0)
throw;
mp[x]--;
if (mp[x] == 0)
mp.erase(x);
int z = 1;
while (z <= x)
z <<= 1;
if (mp.count(z - x) > 0) {
mp[z - x]--;
if (mp[z - x] == 0)
mp.erase(z - x);
ans++;
}
}
cout << ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int a[200010];
int main() {
int n, ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
mp[a[i]]++;
}
while (!mp.empty()) {
map<int, int>::iterator it = mp.end();
it--;
int x = it->first;
if (mp[x] == 0)
throw;
mp[x]--;
if (mp[x] == 0)
mp.erase(x);
int z = 1;
while (z <= x)
z <<= 1;
if (mp.count(z - x) > 0) {
mp[z - x]--;
if (mp[z - x] == 0)
mp.erase(z - x);
ans++;
}
}
cout << ans;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p03201 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
#define int long long
#define ll long long
#define ll1 1ll
#define ONE 1ll
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using pii = pair<int, int>;
// template<class T> ostream &operator<<(ostream &os,T &t){dump(t);return os;}
template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T> void printvv(const vector<vector<T>> &v) {
cerr << endl;
rep(i, 0, v.size()) rep(j, 0, v[i].size()) {
if (typeid(v[i][j]).name() == typeid(INF).name() and v[i][j] == INF) {
cerr << "INF";
} else
cerr << v[i][j];
cerr << (j == v[i].size() - 1 ? '\n' : ' ');
}
cerr << endl;
}
/*
typedef __int128_t Int;
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
std::ostream::sentry s(dest);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) {
--d;
*d = '-';
}
int len = std::end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}
__int128 parse(string &s) {
__int128 ret = 0;
for (int i = 0; i < s.length(); i++)
if ('0' <= s[i] && s[i] <= '9')
ret = 10 * ret + s[i] - '0';
return ret;
}
*/
#ifndef _DEBUG
#define printvv(...)
#endif
void YES(bool f) { cout << (f ? "YES" : "NO") << endl; }
void Yes(bool f) { cout << (f ? "Yes" : "No") << endl; }
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
signed main(signed argc, char *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
int N;
cin >> N;
vector<int> A(N);
rep(i, 0, N) { cin >> A[i]; }
int ans = 0;
map<int, int> mp;
rep(i, 0, N) mp[A[i]]++;
sort(all(A), greater<int>());
rep(i, 0, N) {
if (mp[A[i]]) {
mp[A[i]]--;
rep(j, 0, 35) {
int x = (1ll << j) - A[i];
if (mp[x]) {
mp[x]--;
ans++;
}
}
}
}
dump(mp.size());
cout << ans << endl;
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
#define int long long
#define ll long long
#define ll1 1ll
#define ONE 1ll
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using pii = pair<int, int>;
// template<class T> ostream &operator<<(ostream &os,T &t){dump(t);return os;}
template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T> void printvv(const vector<vector<T>> &v) {
cerr << endl;
rep(i, 0, v.size()) rep(j, 0, v[i].size()) {
if (typeid(v[i][j]).name() == typeid(INF).name() and v[i][j] == INF) {
cerr << "INF";
} else
cerr << v[i][j];
cerr << (j == v[i].size() - 1 ? '\n' : ' ');
}
cerr << endl;
}
/*
typedef __int128_t Int;
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
std::ostream::sentry s(dest);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) {
--d;
*d = '-';
}
int len = std::end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}
__int128 parse(string &s) {
__int128 ret = 0;
for (int i = 0; i < s.length(); i++)
if ('0' <= s[i] && s[i] <= '9')
ret = 10 * ret + s[i] - '0';
return ret;
}
*/
#ifndef _DEBUG
#define printvv(...)
#endif
void YES(bool f) { cout << (f ? "YES" : "NO") << endl; }
void Yes(bool f) { cout << (f ? "Yes" : "No") << endl; }
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
signed main(signed argc, char *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
int N;
cin >> N;
vector<int> A(N);
rep(i, 0, N) { cin >> A[i]; }
int ans = 0;
map<int, int> mp;
rep(i, 0, N) mp[A[i]]++;
sort(all(A), greater<int>());
rep(i, 0, N) {
if (mp[A[i]]) {
mp[A[i]]--;
rep(j, 0, 35) {
int x = (1ll << j) - A[i];
if (mp.count(x) and mp[x]) {
mp[x]--;
ans++;
}
}
}
}
dump(mp.size());
cout << ans << endl;
return 0;
}
| replace | 124 | 125 | 124 | 125 | TLE | |
p03201 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int arr[100005];
map<int, int> cnt;
int main() {
int n, ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
cnt[arr[i]]++;
}
sort(arr, arr + n);
for (int i = n - 1; i >= 0; i--) {
if (!cnt[arr[i]])
continue;
cnt[arr[i]]--;
int pw = 1;
while (pw <= arr[i])
pw *= 2;
if (cnt[pw - arr[i]]) {
cnt[pw - arr[i]]--;
ans++;
}
}
printf("%d", ans);
} | #include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int arr[200005];
map<int, int> cnt;
int main() {
int n, ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
cnt[arr[i]]++;
}
sort(arr, arr + n);
for (int i = n - 1; i >= 0; i--) {
if (!cnt[arr[i]])
continue;
cnt[arr[i]]--;
int pw = 1;
while (pw <= arr[i])
pw *= 2;
if (cnt[pw - arr[i]]) {
cnt[pw - arr[i]]--;
ans++;
}
}
printf("%d", ans);
} | replace | 4 | 5 | 4 | 5 | 0 | |
p03201 | C++ | Runtime Error | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
struct __INIT {
__INIT() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} __init;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define MOD 1000000007
#define INF (1 << 30)
#define LINF (1LL << 60)
#define endl "\n"
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reprev(i, n) for (int(i) = (n - 1); (i) >= 0; (i)--)
#define Flag(x) (1 << (x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int, int>
#define pdouble pair<double, double>
#define plint pair<lint, lint>
typedef unsigned long long int ull;
typedef long long lint;
int main(void) {
int pow2[31];
pow2[0] = 1;
rep(i, 30) pow2[i + 1] = pow2[i] * 2;
int N, ans = 0;
cin >> N;
multiset<int> ball;
rep(i, N) {
int X;
cin >> X;
ball.insert(X);
}
while (ball.size() > 1) {
auto itr = ball.end();
itr--;
bool check = false;
rep(i, 30) {
if (ball.count(pow2[i + 1] - *itr)) {
auto itr2 = ball.find(pow2[i + 1] - *itr);
ans++;
ball.erase(itr);
ball.erase(itr2);
check = true;
break;
}
}
if (!check)
ball.erase(itr);
}
cout << ans << endl;
} | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
struct __INIT {
__INIT() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} __init;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define MOD 1000000007
#define INF (1 << 30)
#define LINF (1LL << 60)
#define endl "\n"
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reprev(i, n) for (int(i) = (n - 1); (i) >= 0; (i)--)
#define Flag(x) (1 << (x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int, int>
#define pdouble pair<double, double>
#define plint pair<lint, lint>
typedef unsigned long long int ull;
typedef long long lint;
int main(void) {
int pow2[31];
pow2[0] = 1;
rep(i, 30) pow2[i + 1] = pow2[i] * 2;
int N, ans = 0;
cin >> N;
multiset<int> ball;
rep(i, N) {
int X;
cin >> X;
ball.insert(X);
}
while (ball.size() > 1) {
auto itr = ball.end();
itr--;
bool check = false;
rep(i, 30) {
if (ball.count(pow2[i + 1] - *itr)) {
auto itr2 = ball.find(pow2[i + 1] - *itr);
if (itr == itr2 && ball.count(pow2[i + 1] - *itr) == 1)
continue;
else if (itr == itr2)
itr2++;
ans++;
ball.erase(itr);
ball.erase(itr2);
check = true;
break;
}
}
if (!check)
ball.erase(itr);
}
cout << ans << endl;
} | insert | 45 | 45 | 45 | 49 | 0 | |
p03201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <time.h>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 1000000007LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a) \
REP(d, a.size()) { \
cout << a[d]; \
if (d != a.size() - 1) \
cout << " "; \
else \
cout << endl; \
}
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v) \
sort(ALL(v)); \
v.erase(unique(ALL(v)), v.end());
#define pb push_back
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
vector<ll> a(n);
REP(i, n) cin >> a[i];
sort(ALL(a));
unordered_map<ll, ll> ma;
REP(i, n) ma[a[i]]++;
ll ans = 0;
for (ll i = n - 1; i >= 0; i--) {
if (ma[a[i]] == 0)
continue;
ma[a[i]]--;
for (ll j = 2; j <= 2000000020; j *= 2) {
if (ma[j - a[i]] > 0) {
ma[j - a[i]]--;
ans++;
break;
}
}
}
cout << ans << endl;
return 0;
}
/* --------------------------------------- */
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <time.h>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 1000000007LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a) \
REP(d, a.size()) { \
cout << a[d]; \
if (d != a.size() - 1) \
cout << " "; \
else \
cout << endl; \
}
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v) \
sort(ALL(v)); \
v.erase(unique(ALL(v)), v.end());
#define pb push_back
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
vector<ll> a(n);
REP(i, n) cin >> a[i];
sort(ALL(a));
unordered_map<ll, ll> ma;
REP(i, n) ma[a[i]]++;
ll ans = 0;
for (ll i = n - 1; i >= 0; i--) {
if (ma[a[i]] == 0)
continue;
ma[a[i]]--;
for (ll j = 2; j <= 2000000020; j *= 2) {
if (j > a[i]) {
if (ma[j - a[i]] > 0) {
ma[j - a[i]]--;
ans++;
}
break;
}
}
}
cout << ans << endl;
return 0;
}
/* --------------------------------------- */
| replace | 53 | 56 | 53 | 58 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <set>
#define mp make_pair
#define pii pair<int, int>
using namespace std;
set<pii> s;
int n;
int a[200010];
int res = 0;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
for (int i = 0; i < n; i++)
s.insert(mp(a[i], i));
for (int i = n - 1; i >= 0; i--) {
if (s.count(mp(a[i], i))) {
s.erase(mp(a[i], i));
for (int j = 0; j < 31; j++) {
if ((1 << j) > a[i]) {
int t = (1 << j) - a[i];
set<pii>::iterator tt = lower_bound(s.begin(), s.end(), mp(t, -1));
if (tt != s.end() && tt->first == t) {
res++;
s.erase(tt);
}
break;
}
}
}
}
printf("%d", res);
return 0;
} | #include <algorithm>
#include <iostream>
#include <set>
#define mp make_pair
#define pii pair<int, int>
using namespace std;
set<pii> s;
int n;
int a[200010];
int res = 0;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
for (int i = 0; i < n; i++)
s.insert(mp(a[i], i));
for (int i = n - 1; i >= 0; i--) {
if (s.count(mp(a[i], i))) {
s.erase(mp(a[i], i));
for (int j = 0; j < 31; j++) {
if ((1 << j) > a[i]) {
int t = (1 << j) - a[i];
set<pii>::iterator tt = s.lower_bound(mp(t, -1));
if (tt != s.end() && tt->first == t) {
res++;
s.erase(tt);
}
break;
}
}
}
}
printf("%d", res);
return 0;
} | replace | 23 | 24 | 23 | 24 | TLE | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
using ll = long long;
using ld = long double;
ll INF = LLONG_MAX;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(ll x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(ld x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char *x) { cout << x; }
void pr(const string &x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T> void pr(const complex<T> &x) { cout << x; }
template <class T1, class T2> void pr(const pair<T1, T2> &x);
template <class T> void pr(const T &x);
template <class T, class... Ts> void pr(const T &t, const Ts &...ts) {
pr(t);
pr(ts...);
}
template <class T1, class T2> void pr(const pair<T1, T2> &x) {
pr("{", x.f, ", ", x.s, "}");
}
template <class T> void pr(const T &x) {
pr("{"); // const iterator needed for vector<bool>
bool fst = 1;
for (const auto &a : x)
pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void print() { pr("\n"); } // print w/ spaces
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
pr(t);
if (sizeof...(ts))
pr(" ");
print(ts...);
}
} // namespace output
using namespace output;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int N;
cin >> N;
vi arr(N);
F0R(i, N) cin >> arr[i];
sort(arr.begin(), arr.end());
reverse(arr.begin(), arr.end());
int matches = 0;
for (int i = 0; i < arr.size(); ++i) {
int val = arr[i];
int pow = 1 << ((int)log2(val) + 1);
int l = i + 1;
int r = arr.size() - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (arr[mid] > pow - val) {
l = mid + 1;
} else {
r = mid;
}
}
if (l < N && arr[l] == pow - val) {
arr.erase(arr.begin() + l);
++matches;
}
}
print(matches);
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
using ll = long long;
using ld = long double;
ll INF = LLONG_MAX;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(ll x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(ld x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char *x) { cout << x; }
void pr(const string &x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T> void pr(const complex<T> &x) { cout << x; }
template <class T1, class T2> void pr(const pair<T1, T2> &x);
template <class T> void pr(const T &x);
template <class T, class... Ts> void pr(const T &t, const Ts &...ts) {
pr(t);
pr(ts...);
}
template <class T1, class T2> void pr(const pair<T1, T2> &x) {
pr("{", x.f, ", ", x.s, "}");
}
template <class T> void pr(const T &x) {
pr("{"); // const iterator needed for vector<bool>
bool fst = 1;
for (const auto &a : x)
pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void print() { pr("\n"); } // print w/ spaces
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
pr(t);
if (sizeof...(ts))
pr(" ");
print(ts...);
}
} // namespace output
using namespace output;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int N;
cin >> N;
vi arr(N);
F0R(i, N) cin >> arr[i];
sort(arr.begin(), arr.end());
reverse(arr.begin(), arr.end());
int matches = 0;
for (int i = 0; i < arr.size(); ++i) {
int val = arr[i];
int pow = 1 << ((int)log2(val) + 1);
int l = i + 1;
int r = arr.size() - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (arr[mid] > pow - val) {
l = mid + 1;
} else {
r = mid;
}
}
if (l < arr.size() && arr[l] == pow - val) {
arr.erase(arr.begin() + l);
++matches;
}
}
print(matches);
} | replace | 85 | 86 | 85 | 86 | 0 | |
p03201 | C++ | Time Limit Exceeded | // ヘッダー
#include <bits/stdc++.h>
using namespace std;
// 型定義
typedef long long ll;
// 定数
const int INF = 1e+9;
const int MOD = 1e+9 + 7;
// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)
// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size()) // size
#define MAX(x) *max_element(ALL(x)) // 最大値
#define MIN(x) *min_element(ALL(x)) // 最小値
int main() {
ll N;
cin >> N;
vector<ll> A(N);
map<ll, ll> mp;
REP(i, N) {
cin >> A[i];
mp[A[i]]++;
}
sort(ALL(A), greater<ll>());
ll ans = 0;
vector<ll> num(33, 0);
REP(i, 33) num[i] = pow(2, i);
REP(i, N) {
if (mp[A[i]] <= 0)
continue;
REP(j, 33) {
if (mp[num[j] - A[i]] > 0) {
if (!(mp[num[j] - A[i]] == 1 && num[j] - A[i] == A[i])) {
mp[num[j] - A[i]]--;
mp[A[i]]--;
ans++;
break;
}
}
}
}
cout << ans << endl;
} | // ヘッダー
#include <bits/stdc++.h>
using namespace std;
// 型定義
typedef long long ll;
// 定数
const int INF = 1e+9;
const int MOD = 1e+9 + 7;
// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)
// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size()) // size
#define MAX(x) *max_element(ALL(x)) // 最大値
#define MIN(x) *min_element(ALL(x)) // 最小値
int main() {
ll N;
cin >> N;
vector<ll> A(N);
map<ll, ll> mp;
REP(i, N) {
cin >> A[i];
mp[A[i]]++;
}
sort(ALL(A), greater<ll>());
ll ans = 0;
vector<ll> num(33, 0);
REP(i, 33) num[i] = pow(2, i);
REP(i, N) {
if (mp[A[i]] <= 0)
continue;
REPD2(j, 32, -1) {
if (num[j] <= A[i])
break;
if (mp[num[j] - A[i]] > 0) {
if (!(mp[num[j] - A[i]] == 1 && num[j] - A[i] == A[i])) {
mp[num[j] - A[i]]--;
mp[A[i]]--;
ans++;
break;
}
}
}
}
cout << ans << endl;
} | replace | 45 | 46 | 45 | 48 | TLE | |
p03201 | C++ | Runtime Error | // [email protected] @Ankit Verma
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#define ll long long
#define ld long double
//-------------------------------------------------------------------------------------------------------------------------------------------------
// Custom Hash Function for Unordered Maps
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
//-------------------------------------------------------------------------------------------------------------------------------------------------
// Debugger Tools
#ifndef ONLINE_JUDGE
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std ::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define debug(...)
#endif
#define n_l '\n'
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; }
template <typename T> int SIZE(const T &t) { return t.size(); }
string to_string(string s, int x1 = 0, int x2 = 1e9) {
return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char c) { return string({c}); }
template <size_t N> string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) {
string t = "";
for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
__iii__ <= __jjj__; ++__iii__) {
t += b[__iii__] + '0';
}
return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B> string to_string(pair<A, B> &p) {
l_v_l_v_l++;
string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
l_v_l_v_l--;
return res;
}
template <typename A, typename... C>
string to_string(A(&v), int x1, int x2, C... coords) {
int rnk = rank<A>::value;
string tab(t_a_b_s, ' ');
string res = "";
bool first = true;
if (l_v_l_v_l == 0)
res += n_l;
res += tab + "[";
x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
auto l = begin(v);
advance(l, x1);
auto r = l;
advance(r, (x2 - x1) + (x2 < SIZE(v)));
for (auto e = l; e != r; e = next(e)) {
if (!first) {
res += ", ";
}
first = false;
l_v_l_v_l++;
if (e != l) {
if (rnk > 1) {
res += n_l;
t_a_b_s = l_v_l_v_l;
};
} else {
t_a_b_s = 0;
}
res += to_string(*e, coords...);
l_v_l_v_l--;
}
res += "]";
if (l_v_l_v_l == 0)
res += n_l;
return res;
}
void dbgs() { ; }
template <typename Heads, typename... Tails> void dbgs(Heads H, Tails... T) {
cout << to_string(H) << " | ";
dbgs(T...);
}
#define dbgm(...) \
cout << "[" << #__VA_ARGS__ << "]: "; \
dbgs(__VA_ARGS__); \
cout << endl;
// Mathematical Constants
const ll MOD = 1000000007;
const ll MOD9 = 1000000009;
const double pie = 2 * acos(0.0);
const int C_SIZE = 121000;
const long long inf = MOD * MOD;
//-------------------------------------------------------------------------------------------------------------------------------------------------
// Utility Functions
#define eps 1e-8
#define ull unsigned long long
#define eof (scanf("%d", &n)) != EOF
#define bolt ios::sync_with_stdio(false)
#define prec(n) fixed << setprecision(n)
#define ms(s, n) memset(s, n, sizeof(s))
#define Unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EraseSpace(s) s.erase(remove(s.begin(), s.end(), ' '), s.end());
#define leftrotate(str, n) rotate(str.begin(), str.begin() + n, str.end());
#define rightrotate(str, n) \
rotate(str.begin(), str.begin() + str.size() - n, str.end());
//-------------------------------------------------------------------------------------------------------------------------------------------------
// Constants and Shortcut Functions
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vll vector<ll>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<ll>
#define pqs priority_queue<ll, vi, greater<ll>>
#define all(v) v.begin(), v.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
#define f(i, x, n) for (int i = x; i < n; i++)
#define fr(i, n, x) for (int i = n; i >= x; i--)
ll fact[C_SIZE];
ll finv[C_SIZE];
ll inv[C_SIZE];
ll Comb(int a, int b) {
if (a < b || b < 0)
return 0;
return fact[a] * finv[b] % MOD * finv[a - b] % MOD;
}
gp_hash_table<ll, ll, custom_hash> smp;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
pbds;
void c_p_c() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void run_case() {
int n;
cin >> n;
multiset<ll> mtl;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
mtl.insert(x);
}
int res = 0;
while (!mtl.empty()) {
auto ed = mtl.end();
ed--;
int p = floor((double)logb(*ed));
ll s = pow(2, p + 1) - *ed;
if (mtl.find(s) != mtl.end() && mtl.size() != 1) {
++res;
if (!mtl.empty())
mtl.erase(s);
if (!mtl.empty())
mtl.erase(ed);
} else {
if (!mtl.empty())
mtl.erase(ed);
}
}
cout << res << endl;
}
int main() {
// c_p_c();
bolt;
cin.tie(nullptr);
cout.tie(nullptr);
int tests = 1; // cin >> tests;
while (tests-- > 0) {
run_case();
}
return 0;
} | // [email protected] @Ankit Verma
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#define ll long long
#define ld long double
//-------------------------------------------------------------------------------------------------------------------------------------------------
// Custom Hash Function for Unordered Maps
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
//-------------------------------------------------------------------------------------------------------------------------------------------------
// Debugger Tools
#ifndef ONLINE_JUDGE
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std ::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define debug(...)
#endif
#define n_l '\n'
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; }
template <typename T> int SIZE(const T &t) { return t.size(); }
string to_string(string s, int x1 = 0, int x2 = 1e9) {
return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char c) { return string({c}); }
template <size_t N> string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) {
string t = "";
for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
__iii__ <= __jjj__; ++__iii__) {
t += b[__iii__] + '0';
}
return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B> string to_string(pair<A, B> &p) {
l_v_l_v_l++;
string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
l_v_l_v_l--;
return res;
}
template <typename A, typename... C>
string to_string(A(&v), int x1, int x2, C... coords) {
int rnk = rank<A>::value;
string tab(t_a_b_s, ' ');
string res = "";
bool first = true;
if (l_v_l_v_l == 0)
res += n_l;
res += tab + "[";
x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
auto l = begin(v);
advance(l, x1);
auto r = l;
advance(r, (x2 - x1) + (x2 < SIZE(v)));
for (auto e = l; e != r; e = next(e)) {
if (!first) {
res += ", ";
}
first = false;
l_v_l_v_l++;
if (e != l) {
if (rnk > 1) {
res += n_l;
t_a_b_s = l_v_l_v_l;
};
} else {
t_a_b_s = 0;
}
res += to_string(*e, coords...);
l_v_l_v_l--;
}
res += "]";
if (l_v_l_v_l == 0)
res += n_l;
return res;
}
void dbgs() { ; }
template <typename Heads, typename... Tails> void dbgs(Heads H, Tails... T) {
cout << to_string(H) << " | ";
dbgs(T...);
}
#define dbgm(...) \
cout << "[" << #__VA_ARGS__ << "]: "; \
dbgs(__VA_ARGS__); \
cout << endl;
// Mathematical Constants
const ll MOD = 1000000007;
const ll MOD9 = 1000000009;
const double pie = 2 * acos(0.0);
const int C_SIZE = 121000;
const long long inf = MOD * MOD;
//-------------------------------------------------------------------------------------------------------------------------------------------------
// Utility Functions
#define eps 1e-8
#define ull unsigned long long
#define eof (scanf("%d", &n)) != EOF
#define bolt ios::sync_with_stdio(false)
#define prec(n) fixed << setprecision(n)
#define ms(s, n) memset(s, n, sizeof(s))
#define Unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EraseSpace(s) s.erase(remove(s.begin(), s.end(), ' '), s.end());
#define leftrotate(str, n) rotate(str.begin(), str.begin() + n, str.end());
#define rightrotate(str, n) \
rotate(str.begin(), str.begin() + str.size() - n, str.end());
//-------------------------------------------------------------------------------------------------------------------------------------------------
// Constants and Shortcut Functions
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vll vector<ll>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<ll>
#define pqs priority_queue<ll, vi, greater<ll>>
#define all(v) v.begin(), v.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
#define f(i, x, n) for (int i = x; i < n; i++)
#define fr(i, n, x) for (int i = n; i >= x; i--)
ll fact[C_SIZE];
ll finv[C_SIZE];
ll inv[C_SIZE];
ll Comb(int a, int b) {
if (a < b || b < 0)
return 0;
return fact[a] * finv[b] % MOD * finv[a - b] % MOD;
}
gp_hash_table<ll, ll, custom_hash> smp;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
pbds;
void c_p_c() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void run_case() {
int n;
cin >> n;
multiset<ll> mtl;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
mtl.insert(x);
}
int res = 0;
while (!mtl.empty()) {
auto ed = *mtl.rbegin();
mtl.erase(mtl.find(ed));
ll step = pow(2, logb(ed) + 1);
auto it = mtl.find(step - ed);
if (it != mtl.end()) {
mtl.erase(it);
res++;
}
}
cout << res << endl;
}
int main() {
// c_p_c();
bolt;
cin.tie(nullptr);
cout.tie(nullptr);
int tests = 1; // cin >> tests;
while (tests-- > 0) {
run_case();
}
return 0;
} | replace | 196 | 209 | 196 | 203 | 0 | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define rep(i, a, b) for (auto i = (a); i != (b); ++i)
#define REP(i, n) rep(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
// sum to 2^30
const int maxn = 1e5;
int A[maxn];
map<int, int> cnt[31];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
scanf("%d", &n);
REP(i, n) scanf("%d", &A[i]);
sort(A, A + n);
ll ret = 0;
REP(i, n) {
int x = A[n - 1 - i];
// 2^j >= x
int y = -1;
int j = 0;
while ((1 << j) < x)
j++;
while (j < 31) {
int ty = (1 << j) - x;
if (cnt[j].find(ty) != cnt[j].end() && cnt[j][ty] > 0) {
y = ty;
break;
}
j++;
}
if (y >= 0)
ret++;
for (int k = 0; k < 31; k++) {
if (y >= 0) {
if (y < (1 << k))
cnt[k][y]--;
} else {
// cerr << "add " << x << endl;
if (x < (1 << k))
cnt[k][x]++;
}
}
}
/* for (int k = 0; k < 31; k++) {
cerr << k << ": ";
for (auto x : cnt[k]) cerr << x.x << "->"<<x.y << " ";
cerr << endl;
} */
cout << ret << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define rep(i, a, b) for (auto i = (a); i != (b); ++i)
#define REP(i, n) rep(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
// sum to 2^30
const int maxn = 2e5 + 10;
int A[maxn];
map<int, int> cnt[31];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
scanf("%d", &n);
REP(i, n) scanf("%d", &A[i]);
sort(A, A + n);
ll ret = 0;
REP(i, n) {
int x = A[n - 1 - i];
// 2^j >= x
int y = -1;
int j = 0;
while ((1 << j) < x)
j++;
while (j < 31) {
int ty = (1 << j) - x;
if (cnt[j].find(ty) != cnt[j].end() && cnt[j][ty] > 0) {
y = ty;
break;
}
j++;
}
if (y >= 0)
ret++;
for (int k = 0; k < 31; k++) {
if (y >= 0) {
if (y < (1 << k))
cnt[k][y]--;
} else {
// cerr << "add " << x << endl;
if (x < (1 << k))
cnt[k][x]++;
}
}
}
/* for (int k = 0; k < 31; k++) {
cerr << k << ": ";
for (auto x : cnt[k]) cerr << x.x << "->"<<x.y << " ";
cerr << endl;
} */
cout << ret << endl;
return 0;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p03201 | C++ | Runtime Error | /* code by AbhiTaker
____
/ \
/ __ \ _______ __ _ _ _____ _____
/ / \ \ |__ __| / \ | | / / | ___| | _ \
/ /____\ \ ___ | | / /\ \ | |/ / | |__ | |_) )
/ /______\ \ |___| | | / /__\ \ | / | __| | _/
/ / \ \ | | / /----\ \ | |\ \ | |___ | |\ \
/__/ \__\ |_| /_/ \_\ |_| \_\ |_____| |_| \_\
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000
ll cnt(ll x) {
ll val = 1;
while (x != 1) {
x /= 2;
val++;
}
return val;
}
ll power(ll x, ll n) {
if (n == 0)
return 1;
return n % 2 == 0 ? power(x * x, n / 2) : x * power(x * x, n / 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n, ans = 0, x;
unordered_multiset<ll> ms;
cin >> n;
vector<ll> v;
for (ll i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
ms.insert(x);
}
sort(v.begin(), v.end());
for (ll i = n - 1; i >= 0; i--) {
x = v[i];
if (!(ms.find(x) != ms.end()))
continue;
ll val = power(2, cnt(x));
if (ms.find(val - x) != ms.end()) {
ans++;
ms.erase(ms.find(val - x));
ms.erase(ms.find(x));
}
}
cout << ans << "\n";
return 0;
}
| /* code by AbhiTaker
____
/ \
/ __ \ _______ __ _ _ _____ _____
/ / \ \ |__ __| / \ | | / / | ___| | _ \
/ /____\ \ ___ | | / /\ \ | |/ / | |__ | |_) )
/ /______\ \ |___| | | / /__\ \ | / | __| | _/
/ / \ \ | | / /----\ \ | |\ \ | |___ | |\ \
/__/ \__\ |_| /_/ \_\ |_| \_\ |_____| |_| \_\
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000
ll cnt(ll x) {
ll val = 1;
while (x != 1) {
x /= 2;
val++;
}
return val;
}
ll power(ll x, ll n) {
if (n == 0)
return 1;
return n % 2 == 0 ? power(x * x, n / 2) : x * power(x * x, n / 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n, ans = 0, x;
unordered_multiset<ll> ms;
cin >> n;
vector<ll> v;
for (ll i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
ms.insert(x);
}
sort(v.begin(), v.end());
for (ll i = n - 1; i >= 0; i--) {
x = v[i];
if (!(ms.find(x) != ms.end()))
continue;
ll val = power(2, cnt(x));
if (ms.find(val - x) != ms.end()) {
ans++;
ms.erase(ms.find(val - x));
if (!(ms.find(x) != ms.end())) {
ans--;
ms.insert(val - x);
continue;
}
ms.erase(ms.find(x));
}
}
cout << ans << "\n";
return 0;
}
| insert | 65 | 65 | 65 | 70 | -11 | |
p03201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, s, e) for (int i = (s); i < (e); i++)
#define FOR64(i, s, e) for (ll i = (s); i < (e); i++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(b, e) sort((b), (e))
#define REV(b, e) reverse((b), (e))
#define VSORT(v) sort((v).begin(), (v).end())
#define VREV(v) reverse((v).begin(), (v).end())
#define pb(a) push_back(a)
#define INF 999999999
#define P(x) printf("%d\n", (x));
typedef long long ll;
int main() {
int N;
cin >> N;
vector<int> A(N);
map<int, int> M;
for (int &a : A) {
cin >> a;
M[a]++;
}
VSORT(A);
int ans = 0;
for (int j = N - 1; j >= 0; j--) {
int a = A[j];
if (M[a]) {
M[a]--;
// for(int k = 30; (1L << k) > a; k--){
for (int k = 30; k >= 0; k--) {
if (M[(1L << k) - a]) {
M[(1L << k) - a]--;
ans++;
break;
}
}
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, s, e) for (int i = (s); i < (e); i++)
#define FOR64(i, s, e) for (ll i = (s); i < (e); i++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(b, e) sort((b), (e))
#define REV(b, e) reverse((b), (e))
#define VSORT(v) sort((v).begin(), (v).end())
#define VREV(v) reverse((v).begin(), (v).end())
#define pb(a) push_back(a)
#define INF 999999999
#define P(x) printf("%d\n", (x));
typedef long long ll;
int main() {
int N;
cin >> N;
vector<int> A(N);
map<int, int> M;
for (int &a : A) {
cin >> a;
M[a]++;
}
VSORT(A);
int ans = 0;
for (int j = N - 1; j >= 0; j--) {
int a = A[j];
if (M[a]) {
M[a]--;
for (int k = 30; (1L << k) > a; k--) {
if (M[(1L << k) - a]) {
M[(1L << k) - a]--;
ans++;
break;
}
}
}
}
cout << ans << endl;
return 0;
}
| replace | 41 | 43 | 41 | 42 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define pr(x) cout << x << endl;
#define pr2(x, y) cout << x << " " << y << endl;
#define pr3(x, y, z) cout << x << " " << y << endl;
#define prv(v) \
for (auto x : v) \
cout << x << " ";
#define ffs fflush(stdout);
#define int ll
#define sz(x) (ll) x.size()
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ll LOG = 29;
#define PI 3.141592653589793238
const ll N = (1e5 + 5);
ll fact[N];
long long binpow(long long a, long long b) {
a %= MOD;
long long res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
res %= MOD;
return res;
}
void ini() {
fact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = (fact[i - 1] * i) % MOD;
}
}
ll ncr(ll n, ll r) {
ll ret = fact[n];
ret = (ret * binpow(fact[r], MOD - 2)) % MOD;
ret = (ret * binpow(fact[n - r], MOD - 2)) % MOD;
return ret;
}
ll a[N];
map<ll, ll> cnt;
void solve() {
ll n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
cnt[a[i]]++;
}
sort(a + 1, a + n + 1, greater<ll>());
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (cnt[a[i]] <= 0)
continue;
bool poss = false;
ll pp = -1;
cnt[a[i]]--;
for (ll p = 1; p <= 60; p++) {
if ((1ll << p) < a[i])
continue;
if (cnt[(1ll << p) - a[i]] > 0) {
poss = true;
pp = p;
break;
}
}
if (poss) {
ans++;
cnt[(1ll << pp) - a[i]]--;
// cnt[a[i]]--;
// cout << a[i] << " "<< (1ll<<pp)-a[i] << endl;
} else {
cnt[a[i]]++;
}
}
cout << ans << endl;
}
/*
2 3 4 5 6
3 6 10 15 21
4 10 20 35 56
5 15 35 70 126
6 21 56 126 252
*/
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
ll tt = 1;
while (tt--) {
solve();
}
} | #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define pr(x) cout << x << endl;
#define pr2(x, y) cout << x << " " << y << endl;
#define pr3(x, y, z) cout << x << " " << y << endl;
#define prv(v) \
for (auto x : v) \
cout << x << " ";
#define ffs fflush(stdout);
#define int ll
#define sz(x) (ll) x.size()
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ll LOG = 29;
#define PI 3.141592653589793238
const ll N = (1e5 + 5);
ll fact[N];
long long binpow(long long a, long long b) {
a %= MOD;
long long res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
res %= MOD;
return res;
}
void ini() {
fact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = (fact[i - 1] * i) % MOD;
}
}
ll ncr(ll n, ll r) {
ll ret = fact[n];
ret = (ret * binpow(fact[r], MOD - 2)) % MOD;
ret = (ret * binpow(fact[n - r], MOD - 2)) % MOD;
return ret;
}
ll a[N];
map<ll, ll> cnt;
void solve() {
ll n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
cnt[a[i]]++;
}
sort(a + 1, a + n + 1, greater<ll>());
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (cnt[a[i]] <= 0)
continue;
bool poss = false;
ll pp = -1;
cnt[a[i]]--;
for (ll p = 1; p <= 32; p++) {
if ((1ll << p) < a[i])
continue;
if (cnt[(1ll << p) - a[i]] > 0) {
poss = true;
pp = p;
break;
}
}
if (poss) {
ans++;
cnt[(1ll << pp) - a[i]]--;
// cnt[a[i]]--;
// cout << a[i] << " "<< (1ll<<pp)-a[i] << endl;
} else {
cnt[a[i]]++;
}
}
cout << ans << endl;
}
/*
2 3 4 5 6
3 6 10 15 21
4 10 20 35 56
5 15 35 70 126
6 21 56 126 252
*/
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
ll tt = 1;
while (tt--) {
solve();
}
} | replace | 75 | 76 | 75 | 76 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
int digit(int n) {
int count = 0;
while (n > 0) {
n /= 2;
count++;
}
return count;
}
int main() {
int N;
cin >> N;
int A[200010] = {};
for (int i = 0; i < N; i++) {
cin >> A[i];
}
sort(A, A + N, greater<int>());
int D[200010] = {};
for (int i = 0; i < N; i++) {
D[i] = digit(A[i]);
}
int B[200010] = {};
for (int i = 0; i < N; i++) {
B[i] = (1 << D[i]) - A[i];
}
int count = 0;
int r = N - 1;
for (int i = 0; i < N; i++) {
for (int j = r; j >= 0; j--) {
if ((B[i] == A[j] && B[i] > 0) && i != j) {
count++;
A[i] = -1, A[j] = -1;
B[i] = -1;
B[j] = -1;
r = j - 1;
break;
}
if (B[i] > 0 && A[j] > 0 && B[i] < A[j]) {
r = min(j + 1, N - 1);
break;
}
}
if ((i != (N - 1)) && D[i] != D[i + 1]) {
r = N - 1;
}
}
cout << count << endl;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
int digit(int n) {
int count = 0;
while (n > 0) {
n /= 2;
count++;
}
return count;
}
int main() {
int N;
cin >> N;
int A[200010] = {};
for (int i = 0; i < N; i++) {
cin >> A[i];
}
sort(A, A + N, greater<int>());
int D[200010] = {};
for (int i = 0; i < N; i++) {
D[i] = digit(A[i]);
}
int B[200010] = {};
for (int i = 0; i < N; i++) {
B[i] = (1 << D[i]) - A[i];
}
int count = 0;
int r = N - 1;
for (int i = 0; i < N; i++) {
for (int j = r; j >= 0; j--) {
if (B[i] == -1) {
break;
}
if ((B[i] == A[j] && B[i] > 0) && i != j) {
count++;
A[i] = -1, A[j] = -1;
B[i] = -1;
B[j] = -1;
r = j - 1;
break;
}
if (B[i] > 0 && A[j] > 0 && B[i] < A[j]) {
r = min(j + 1, N - 1);
break;
}
}
if ((i != (N - 1)) && D[i] != D[i + 1]) {
r = N - 1;
}
}
cout << count << endl;
} | insert | 32 | 32 | 32 | 35 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a) >= 0 ? (a) : -(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define mem(a, b) memset(a, b, sizeof(a))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, n, a) for (int i = n; i >= a; --i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
const int maxn = 1e6 + 200;
const int inf = 0x3f3f3f3f;
const double eps = 1e-7;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
inline int lowbit(int x) { return x & (-x); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
void ex_gcd(ll a, ll b, ll &d, ll &x, ll &y) {
if (!b) {
d = a, x = 1, y = 0;
} else {
ex_gcd(b, a % b, d, y, x);
y -= x * (a / b);
}
} // x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a, ll b, ll MOD = mod) {
ll res = 1;
a %= MOD;
while (b > 0) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
inline ll inv(ll x, ll p) { return qpow(x, p - 2, p); }
inline ll Jos(ll n, ll k, ll s = 1) {
ll res = 0;
rep(i, 1, n + 1) res = (res + k) % i;
return (res + s) % n;
}
inline ll read() {
ll f = 1;
ll x = 0;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
unordered_map<ll, ll> Map;
ll a[maxn];
int main() {
ll n = read();
rep(i, 1, n) a[i] = read(), Map[a[i]]++;
sort(a + 1, a + 1 + n);
ll ans = 0;
per(i, n, 1) {
for (ll j = 0; (1 << j) <= 1e10; j++) {
if ((1 << j) < a[i])
continue;
if (Map[(1 << j) - a[i]] >= 1 &&
((1 << j) - a[i] == a[i] ? Map[a[i]] >= 2 : Map[a[i]] >= 1)) {
Map[a[i]]--, Map[(1 << j) - a[i]]--, ans++;
break;
}
}
}
cout << ans << '\n';
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a) >= 0 ? (a) : -(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define mem(a, b) memset(a, b, sizeof(a))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, n, a) for (int i = n; i >= a; --i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
const int maxn = 1e6 + 200;
const int inf = 0x3f3f3f3f;
const double eps = 1e-7;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
inline int lowbit(int x) { return x & (-x); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
void ex_gcd(ll a, ll b, ll &d, ll &x, ll &y) {
if (!b) {
d = a, x = 1, y = 0;
} else {
ex_gcd(b, a % b, d, y, x);
y -= x * (a / b);
}
} // x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a, ll b, ll MOD = mod) {
ll res = 1;
a %= MOD;
while (b > 0) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
inline ll inv(ll x, ll p) { return qpow(x, p - 2, p); }
inline ll Jos(ll n, ll k, ll s = 1) {
ll res = 0;
rep(i, 1, n + 1) res = (res + k) % i;
return (res + s) % n;
}
inline ll read() {
ll f = 1;
ll x = 0;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
unordered_map<ll, ll> Map;
ll a[maxn];
int main() {
ll n = read();
rep(i, 1, n) a[i] = read(), Map[a[i]]++;
sort(a + 1, a + 1 + n);
ll ans = 0;
per(i, n, 1) {
for (ll j = 0; (1LL << j) <= 1e10; j++) {
if ((1 << j) < a[i])
continue;
if (Map[(1 << j) - a[i]] >= 1 &&
((1 << j) - a[i] == a[i] ? Map[a[i]] >= 2 : Map[a[i]] >= 1)) {
Map[a[i]]--, Map[(1 << j) - a[i]]--, ans++;
break;
}
}
}
cout << ans << '\n';
return 0;
}
| replace | 86 | 87 | 86 | 87 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define p(s) std::cout << s;
#define pl(s) std::cout << s << endl;
#define all(v) v.begin(), v.end()
#define showVector(v) \
REP(i, v.size()) { \
p(v[i]); \
p(" ") \
} \
pl("")
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
typedef long long int ll;
typedef pair<ll, ll> P_ii;
typedef pair<double, double> P_dd;
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
ll gcd(ll a, ll b) {
if (a < b)
swap(a, b);
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return (a / g) * b;
}
// 素数判定 O(√n)
bool is_prime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
// 約数列挙 O(√n)
vector<ll> divisor(ll n) {
vector<ll> res;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i != n / i)
res.push_back(n / i);
}
}
return res;
}
vector<pair<ll, ll>> prime_factorize(ll n) {
vector<pair<ll, ll>> res;
for (ll p = 2; p * p <= n; ++p) {
if (n % p != 0)
continue;
ll num = 0;
while (n % p == 0) {
++num;
n /= p;
}
res.push_back(make_pair(p, num));
}
if (n != 1)
res.push_back(make_pair(n, 1));
return res;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x(x % mod) {}
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
const int MOD = 1000000007;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
void addM(ll &a, ll b) {
a += b;
if (a >= MOD)
a -= MOD;
}
void mulM(ll &a, ll b) { a = ((a % MOD) * (b % MOD)) % MOD; }
ll powM(ll a, ll b) {
ll ret = 1;
ll tmp = a;
while (b > 0) {
if ((b & 1) == 1)
ret = (ret * tmp) % MOD;
tmp = (tmp * tmp) % MOD;
b = b >> 1;
}
return ret;
}
int get2beki(int a) {
int ret = 1;
while (ret <= a)
ret *= 2;
return ret;
}
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
multiset<ll> st;
vector<int> v(N);
REP(i, N) {
cin >> v[i];
st.insert(v[i]);
}
sort(all(v));
int ans = 0;
for (int i = N - 1; i >= 0; i--) {
if (!st.count(v[i]))
continue;
auto ite = st.find(v[i]);
st.erase(ite);
int b = get2beki(v[i]) - v[i];
if (!st.count(b))
continue;
auto it = st.find(b);
if (*it == b) {
ans++;
st.erase(it);
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define p(s) std::cout << s;
#define pl(s) std::cout << s << endl;
#define all(v) v.begin(), v.end()
#define showVector(v) \
REP(i, v.size()) { \
p(v[i]); \
p(" ") \
} \
pl("")
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
typedef long long int ll;
typedef pair<ll, ll> P_ii;
typedef pair<double, double> P_dd;
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
ll gcd(ll a, ll b) {
if (a < b)
swap(a, b);
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return (a / g) * b;
}
// 素数判定 O(√n)
bool is_prime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
// 約数列挙 O(√n)
vector<ll> divisor(ll n) {
vector<ll> res;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i != n / i)
res.push_back(n / i);
}
}
return res;
}
vector<pair<ll, ll>> prime_factorize(ll n) {
vector<pair<ll, ll>> res;
for (ll p = 2; p * p <= n; ++p) {
if (n % p != 0)
continue;
ll num = 0;
while (n % p == 0) {
++num;
n /= p;
}
res.push_back(make_pair(p, num));
}
if (n != 1)
res.push_back(make_pair(n, 1));
return res;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x(x % mod) {}
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
const int MOD = 1000000007;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
void addM(ll &a, ll b) {
a += b;
if (a >= MOD)
a -= MOD;
}
void mulM(ll &a, ll b) { a = ((a % MOD) * (b % MOD)) % MOD; }
ll powM(ll a, ll b) {
ll ret = 1;
ll tmp = a;
while (b > 0) {
if ((b & 1) == 1)
ret = (ret * tmp) % MOD;
tmp = (tmp * tmp) % MOD;
b = b >> 1;
}
return ret;
}
int get2beki(int a) {
int ret = 1;
while (ret <= a)
ret *= 2;
return ret;
}
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
multiset<ll> st;
vector<int> v(N);
REP(i, N) {
cin >> v[i];
st.insert(v[i]);
}
sort(all(v));
int ans = 0;
for (int i = N - 1; i >= 0; i--) {
if (!st.count(v[i]))
continue;
auto ite = st.find(v[i]);
st.erase(ite);
int b = get2beki(v[i]) - v[i];
auto it = st.lower_bound(b);
if (*it == b) {
ans++;
st.erase(it);
}
}
cout << ans << endl;
return 0;
}
| replace | 212 | 216 | 212 | 213 | TLE | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.sync_with_stdio(0);
int n;
cin >> n;
vector<int> a(n);
map<long, int> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
m[a[i]]++;
}
vector<int> c(n);
for (int i = 0; i < n; ++i) {
for (long j = 1; j < 1L << 31; j *= 2) {
if (m.count(j - a[i]) == 0)
continue;
c[i] += m[j - a[i]];
if (a[i] == j - a[i])
c[i]--;
}
}
vector<pair<int, int>> b;
for (int i = 0; i < n; ++i)
b.emplace_back(c[i], i);
sort(begin(b), end(b));
map<int, int> v;
for (auto &e : b)
v[a[e.second]] = e.first;
int res = 0;
for (auto &e : b) {
int i = e.second;
if (m[a[i]] == 0)
continue;
m[a[i]]--;
int k = -1, x = INT_MAX;
for (long j = 1; j < 1L << 31; j *= 2) {
if (m.count(j - a[i]) == 0)
continue;
if (m[j - a[i]]) {
if (k < 0 || x > v[j - a[j]])
k = j - a[j], x = v[k];
}
}
if (k >= 0) {
m[k]--;
++res;
}
}
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
cin.sync_with_stdio(0);
int n;
cin >> n;
vector<int> a(n);
map<long, int> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
m[a[i]]++;
}
vector<int> c(n);
for (int i = 0; i < n; ++i) {
for (long j = 1; j < 1L << 31; j *= 2) {
if (m.count(j - a[i]) == 0)
continue;
c[i] += m[j - a[i]];
if (a[i] == j - a[i])
c[i]--;
}
}
vector<pair<int, int>> b;
for (int i = 0; i < n; ++i)
b.emplace_back(c[i], i);
sort(begin(b), end(b));
map<int, int> v;
for (auto &e : b)
v[a[e.second]] = e.first;
int res = 0;
for (auto &e : b) {
int i = e.second;
if (m[a[i]] == 0)
continue;
m[a[i]]--;
int k = -1, x = INT_MAX;
for (long j = 1; j < 1L << 31; j *= 2) {
if (m.count(j - a[i]) == 0)
continue;
if (m[j - a[i]]) {
if (k < 0 || x > v[j - a[i]])
k = j - a[i], x = v[k];
}
}
if (k >= 0) {
m[k]--;
++res;
}
}
cout << res << endl;
}
| replace | 45 | 47 | 45 | 47 | 0 | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
int N;
cin >> N;
multiset<ll> A;
for (int i = 0; i < N; i++) {
ll a;
cin >> a;
A.insert(a);
}
int ans = 0;
while (!A.empty()) {
auto it = A.end();
it--;
ll t = 1;
while (t <= (*it))
t *= 2;
auto it2 = A.find(t - (*it));
if (it2 != it && (*it) + (*it2) == t) {
ans++;
A.erase(it2);
it = A.end();
it--;
A.erase(it);
} else {
A.erase(it);
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
int N;
cin >> N;
multiset<ll> A;
for (int i = 0; i < N; i++) {
ll a;
cin >> a;
A.insert(a);
}
int ans = 0;
while (!A.empty()) {
auto it = A.end();
it--;
ll t = 1;
while (t <= (*it))
t *= 2;
auto it2 = A.find(t - (*it));
if (it2 != it && it2 != A.end() && (*it) + (*it2) == t) {
ans++;
A.erase(it2);
it = A.end();
it--;
A.erase(it);
} else {
A.erase(it);
}
}
cout << ans << endl;
return 0;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define endl '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double INFD = 1e100;
constexpr ll mod2 = 1000000007;
constexpr ll mod = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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;
}
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------
int main() {
int N;
cin >> N;
vector<ll> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
sort(A.begin(), A.end(), greater<ll>());
map<ll, ll> mp;
for (int i = 0; i < N; i++) {
mp[A[i]]++;
}
int ans = 0;
for (int i = 0; i < N; i++) {
if (mp[A[i]] == 0)
continue;
mp[A[i]]--;
for (int j = 40; j >= 0; j--) {
if (!mp.count((1LL << j) - A[i]))
continue;
if (mp[(1LL << j) - A[i]] > 0) {
ans++;
mp[(1LL << j) - A[i]]--;
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define endl '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double INFD = 1e100;
constexpr ll mod2 = 1000000007;
constexpr ll mod = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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;
}
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------
int main() {
int N;
cin >> N;
vector<ll> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
sort(A.begin(), A.end(), greater<ll>());
unordered_map<ll, ll> mp;
for (int i = 0; i < N; i++) {
mp[A[i]]++;
}
int ans = 0;
for (int i = 0; i < N; i++) {
if (mp[A[i]] == 0)
continue;
mp[A[i]]--;
for (int j = 40; j >= 0; j--) {
if (!mp.count((1LL << j) - A[i]))
continue;
if (mp[(1LL << j) - A[i]] > 0) {
ans++;
mp[(1LL << j) - A[i]]--;
}
}
}
cout << ans << endl;
return 0;
}
| replace | 41 | 42 | 41 | 42 | TLE | |
p03201 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#pragma warning(disable : 4996)
using namespace std;
const int maxn = 200005;
typedef long long ll;
multiset<ll> q;
int n;
ll ans;
ll a[maxn];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
q.insert(a[i]);
}
while (!q.empty()) {
ll v = *--q.end();
q.erase(q.end());
for (int i = 0; i <= 32; i++) {
ll t = (1 << i) - v;
if (q.find(t) != q.end()) {
ans++;
q.erase(q.find(t));
break;
}
}
}
printf("%lld\n", ans);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#pragma warning(disable : 4996)
using namespace std;
const int maxn = 200005;
typedef long long ll;
multiset<ll> q;
int n;
ll ans;
ll a[maxn];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
q.insert(a[i]);
}
while (!q.empty()) {
ll v = *--q.end();
q.erase(--q.end());
for (int i = 0; i <= 32; i++) {
ll t = (1 << i) - v;
if (q.find(t) != q.end()) {
ans++;
q.erase(q.find(t));
break;
}
}
}
printf("%lld\n", ans);
return 0;
} | replace | 21 | 22 | 21 | 22 | -6 | free(): invalid pointer
|
p03201 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(0);
#define LLINF (long long)1e18 // 1234567890987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7) // 0.0000001 the value
#define PI (acos(-1))
#define MAXN 300006
#define MAXK 26
#define MAXX 15000006
#define ll long long int
#define ld long double
#define rep0(kk, l1, l2) for (ll kk = l1; kk < l2; kk++)
#define rep1(kk, l1, l2) for (ll kk = l1; kk <= l2; kk++)
#define foritr(itr, A) \
for (set<ll>::iterator itr = A.begin(); itr != A.end(); itr++)
mt19937
rng(chrono::steady_clock::now()
.time_since_epoch()
.count()); // can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for (ll ii = ss; ii < ee; ii++)
#define cr(x) cerr << #x << " = " << x << "\n";
#define crA(x, A) cerr << #x << " = " << A[x] << "\n";
#define spacing cout << " ";
#define mmst(x, v) memset((x), v, sizeof((x)));
#define bg(ms) (*ms.begin())
#define ed(ms) (*prev(ms.end(), 1))
#define addedge(a, b, c, v) \
v[(a)].pb(pi((b), (c))); \
v[(b)].pb(pi((a), (c)))
#define ph push
#define btinpct(x) __builtin_popcountll(x)
#define p2(x) (1LL << (x))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;
inline ll rand(ll x, ll y) {
++y;
return (rng() % (y - x)) + x;
} // inclusivesss
ll n, A[MAXN], val[MAXN];
vector<ll> v;
ll index_c(ll x) {
auto lb = lbd(v, x) - v.begin();
if (v[lb] != x)
return -LLINF;
else
return lb;
}
int main() {
cin >> n;
FOR(i, 0, n) {
cin >> A[i];
v.pb(A[i]);
}
sort(all(v));
v.resize(unique(all(v)) - v.begin());
FOR(i, 0, n) { val[index_c(A[i])]++; }
ll co = 1;
while (co <= (ll)1e14)
co *= 2;
while (co > 0) {
FOR(i, 0, n) {
if (val[index_c(A[i])] == 0 || A[i] > co)
continue;
val[index_c(A[i])]--;
if (index_c(co - A[i]) != -LLINF && val[index_c(co - A[i])] > 0) {
val[index_c(co - A[i])]--;
continue;
}
val[index_c(A[i])]++;
}
co /= 2;
}
ll ans = 0;
for (ll i = 0; i < v.size(); i++)
ans += val[i];
cout << (n - ans) / 2 << "\n";
}
| #include "bits/stdc++.h"
using namespace std;
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(0);
#define LLINF (long long)1e18 // 1234567890987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7) // 0.0000001 the value
#define PI (acos(-1))
#define MAXN 300006
#define MAXK 26
#define MAXX 15000006
#define ll long long int
#define ld long double
#define rep0(kk, l1, l2) for (ll kk = l1; kk < l2; kk++)
#define rep1(kk, l1, l2) for (ll kk = l1; kk <= l2; kk++)
#define foritr(itr, A) \
for (set<ll>::iterator itr = A.begin(); itr != A.end(); itr++)
mt19937
rng(chrono::steady_clock::now()
.time_since_epoch()
.count()); // can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for (ll ii = ss; ii < ee; ii++)
#define cr(x) cerr << #x << " = " << x << "\n";
#define crA(x, A) cerr << #x << " = " << A[x] << "\n";
#define spacing cout << " ";
#define mmst(x, v) memset((x), v, sizeof((x)));
#define bg(ms) (*ms.begin())
#define ed(ms) (*prev(ms.end(), 1))
#define addedge(a, b, c, v) \
v[(a)].pb(pi((b), (c))); \
v[(b)].pb(pi((a), (c)))
#define ph push
#define btinpct(x) __builtin_popcountll(x)
#define p2(x) (1LL << (x))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;
inline ll rand(ll x, ll y) {
++y;
return (rng() % (y - x)) + x;
} // inclusivesss
ll n, A[MAXN], val[MAXN];
vector<ll> v;
ll index_c(ll x) {
auto lb = lbd(v, x) - v.begin();
if (v[lb] != x)
return -LLINF;
else
return lb;
}
int main() {
FAST cin >> n;
FOR(i, 0, n) {
cin >> A[i];
v.pb(A[i]);
}
sort(all(v));
v.resize(unique(all(v)) - v.begin());
FOR(i, 0, n) { val[index_c(A[i])]++; }
ll co = 1;
while (co <= (ll)1e14)
co *= 2;
while (co > 0) {
FOR(i, 0, n) {
if (val[index_c(A[i])] == 0 || A[i] > co)
continue;
val[index_c(A[i])]--;
if (index_c(co - A[i]) != -LLINF && val[index_c(co - A[i])] > 0) {
val[index_c(co - A[i])]--;
continue;
}
val[index_c(A[i])]++;
}
co /= 2;
}
ll ans = 0;
for (ll i = 0; i < v.size(); i++)
ans += val[i];
cout << (n - ans) / 2 << "\n";
}
| replace | 60 | 61 | 60 | 61 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
ll INF = (1ll << 60);
int main() {
ll N;
cin >> N;
vi A(N);
for (ll i = 0; i < N; i++) {
cin >> A[i];
}
priority_queue<pii> pq;
for (ll i = 0; i < N; i++) {
pq.push({A[i], i});
}
unordered_map<ll, unordered_set<ll>> m;
for (ll i = 0; i < N; i++) {
m[A[i]].insert(i);
}
ll ans = 0;
while (pq.size()) {
ll i = pq.top().second;
ll j = pq.top().first;
pq.pop();
if (m[j].find(i) == m[j].end()) {
continue;
}
m[j].erase(find(m[j].begin(), m[j].end(), i));
ll x = [&]() {
for (ll i = 1;; i *= 2) {
if (i > j) {
return i;
}
}
}();
if (!m[x - j].size())
continue;
ans += 1;
m[x - j].erase(m[x - j].begin());
}
cout << ans << endl;
}
| #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
ll INF = (1ll << 60);
int main() {
ll N;
cin >> N;
vi A(N);
for (ll i = 0; i < N; i++) {
cin >> A[i];
}
priority_queue<pii> pq;
for (ll i = 0; i < N; i++) {
pq.push({A[i], i});
}
unordered_map<ll, unordered_set<ll>> m;
for (ll i = 0; i < N; i++) {
m[A[i]].insert(i);
}
ll ans = 0;
while (pq.size()) {
ll i = pq.top().second;
ll j = pq.top().first;
pq.pop();
if (m[j].find(i) == m[j].end()) {
continue;
}
m[j].erase(i);
ll x = [&]() {
for (ll i = 1;; i *= 2) {
if (i > j) {
return i;
}
}
}();
if (!m[x - j].size())
continue;
ans += 1;
m[x - j].erase(m[x - j].begin());
}
cout << ans << endl;
}
| replace | 43 | 44 | 43 | 44 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;
int main() {
int n;
cin >> n;
VLL a(n);
REP(i, n) cin >> a[i];
sort(ALL(a));
ll ans = 0;
vector<bool> b(n, true);
for (int i = n - 1; i >= 0; i--) {
if (!b[i])
continue;
b[i] = false;
ll j = 1;
while (j <= a[i])
j *= 2;
j -= a[i];
int m = lower_bound(ALL(a), j) - a.begin();
int l = upper_bound(ALL(a), j) - a.begin();
for (int k = l - 1; k >= m; k--) {
if (b[k]) {
b[k] = false;
ans++;
break;
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;
int main() {
int n;
cin >> n;
VLL a(n);
REP(i, n) cin >> a[i];
sort(ALL(a));
ll ans = 0;
vector<bool> b(n, true);
for (int i = n - 1; i >= 0; i--) {
if (!b[i])
continue;
b[i] = false;
ll j = 1;
while (j <= a[i])
j *= 2;
j -= a[i];
ll l = upper_bound(ALL(a), j) - a.begin();
for (int k = min(l - 1, (ll)i);; k--) {
if (a[k] != j)
break;
if (b[k]) {
b[k] = false;
ans++;
break;
}
}
}
cout << ans << endl;
return 0;
} | replace | 55 | 58 | 55 | 59 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define m0(x) memset(x, 0, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
using namespace std;
int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int dxx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dyy[8] = {1, -1, 0, 1, -1, 0, 1, -1};
ll gcd(ll x, ll y) {
ll m = max(x, y), n = min(x, y);
if (m % n == 0)
return n;
else
return gcd(m % n, n);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll myPow(ll x, ll n, ll m) {
if (n == 0)
return 1;
if (n % 2 == 0)
return myPow(x * x % m, n / 2, m);
else
return x * myPow(x, n - 1, m) % m;
}
ll pow2(int a, int n) { // aのn乗を計算します。
int x = 1;
while (n > 0) { // 全てのbitが捨てられるまで。
if (n & 1) { // 1番右のbitが1のとき。
x = x * a;
}
a = a * a;
n >>= 1; // bit全体を右に1つシフトして一番右を捨てる。
}
return x;
}
long long nCr(int n, int r) {
if (r > n / 2)
r = n - r; // because C(n, r) == C(n, n - r)
long long ans = 1;
int i;
for (i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}
const int MOD = 1000000007;
vector<int> v;
map<int, int> mp;
int main() {
int N;
cin >> N;
REP(i, N) {
int A;
cin >> A;
v.push_back(A);
mp[A]++;
}
VSORT(v);
ll ans = 0;
for (int i = 32; i > 0; i--) {
REP(j, N) {
if (mp[v[j]] > 0) {
ll x = pow2(2, i) - v[j];
if (binary_search(v.begin(), v.end(), x)) {
if (v[j] == x) {
if (mp[x] >= 2) {
ans++;
mp[x] -= 2;
}
} else {
if (mp[x] >= 1) {
ans++;
mp[x]--;
mp[v[j]]--;
}
}
}
}
}
}
int cnt = 0;
REP(i, 5000000000) cnt++;
cout << ans << endl;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define m0(x) memset(x, 0, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
using namespace std;
int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int dxx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dyy[8] = {1, -1, 0, 1, -1, 0, 1, -1};
ll gcd(ll x, ll y) {
ll m = max(x, y), n = min(x, y);
if (m % n == 0)
return n;
else
return gcd(m % n, n);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll myPow(ll x, ll n, ll m) {
if (n == 0)
return 1;
if (n % 2 == 0)
return myPow(x * x % m, n / 2, m);
else
return x * myPow(x, n - 1, m) % m;
}
ll pow2(int a, int n) { // aのn乗を計算します。
int x = 1;
while (n > 0) { // 全てのbitが捨てられるまで。
if (n & 1) { // 1番右のbitが1のとき。
x = x * a;
}
a = a * a;
n >>= 1; // bit全体を右に1つシフトして一番右を捨てる。
}
return x;
}
long long nCr(int n, int r) {
if (r > n / 2)
r = n - r; // because C(n, r) == C(n, n - r)
long long ans = 1;
int i;
for (i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}
const int MOD = 1000000007;
vector<int> v;
map<int, int> mp;
int main() {
int N;
cin >> N;
REP(i, N) {
int A;
cin >> A;
v.push_back(A);
mp[A]++;
}
VSORT(v);
ll ans = 0;
for (int i = 32; i > 0; i--) {
REP(j, N) {
if (mp[v[j]] > 0) {
ll x = pow2(2, i) - v[j];
if (binary_search(v.begin(), v.end(), x)) {
if (v[j] == x) {
if (mp[x] >= 2) {
ans++;
mp[x] -= 2;
}
} else {
if (mp[x] >= 1) {
ans++;
mp[x]--;
mp[v[j]]--;
}
}
}
}
}
}
int cnt = 0;
REP(i, 2000000000) cnt++;
cout << ans << endl;
} | replace | 115 | 116 | 115 | 116 | TLE | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define fi first
#define se second
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef long double ld;
typedef string st;
const double PI =
3.14159265358979323846264338327950288419716939937510582097494459230;
const ll MOD = 1e9 + 7;
const ll INF = 1e14;
ll mpow(ll a, ll b, ll p = MOD) {
a = a % p;
ll res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % p;
a = (a * a) % p;
b = b >> 1;
}
return res % p;
}
const ll N = 100005;
bool vis[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
ll a[n];
map<ll, ll> ma;
map<ll, vi> ind;
forn(i, n) cin >> a[i], ma[a[i]]++;
sort(a, a + n);
forn(i, n) ind[a[i]].pb(i);
ll ans = 0;
for (ll i = n - 1; i >= 0; i--) {
if (vis[i])
continue;
ll x = a[i];
ma[x]--;
for (ll j = 1; j <= 31; j++) {
ll z = (1LL << j);
if (z > x) {
ll diff = z - x;
if (ma[diff] > 0) {
ma[diff]--;
ans++;
// cout<<x<<" "<<diff<<" ";
ll pos = ind[diff].back();
ind[diff].pop_back();
// cout<<pos<<"\n";
vis[pos] = true;
break;
}
}
}
}
cout << ans;
}
// nothing is impossible | #include <bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define fi first
#define se second
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef long double ld;
typedef string st;
const double PI =
3.14159265358979323846264338327950288419716939937510582097494459230;
const ll MOD = 1e9 + 7;
const ll INF = 1e14;
ll mpow(ll a, ll b, ll p = MOD) {
a = a % p;
ll res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % p;
a = (a * a) % p;
b = b >> 1;
}
return res % p;
}
const ll N = 200005;
bool vis[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
ll a[n];
map<ll, ll> ma;
map<ll, vi> ind;
forn(i, n) cin >> a[i], ma[a[i]]++;
sort(a, a + n);
forn(i, n) ind[a[i]].pb(i);
ll ans = 0;
for (ll i = n - 1; i >= 0; i--) {
if (vis[i])
continue;
ll x = a[i];
ma[x]--;
for (ll j = 1; j <= 31; j++) {
ll z = (1LL << j);
if (z > x) {
ll diff = z - x;
if (ma[diff] > 0) {
ma[diff]--;
ans++;
// cout<<x<<" "<<diff<<" ";
ll pos = ind[diff].back();
ind[diff].pop_back();
// cout<<pos<<"\n";
vis[pos] = true;
break;
}
}
}
}
cout << ans;
}
// nothing is impossible | replace | 38 | 39 | 38 | 39 | 0 | |
p03201 | C++ | Time Limit Exceeded | // include,using,define等
#pragma region header
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// ===============================================================
// using系
#pragma region header
using namespace std;
using ll = long long;
using lint = long long;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using vm = vector<short>;
using vmm = vector<vector<short>>;
using pii = pair<int, int>;
using psi = pair<string, int>;
using ld = long double;
using ull = unsigned long long;
using ui = unsigned int;
using qul = queue<ll>;
using pql = priority_queue<ll>;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
constexpr long double pi = 3.141592653589793238462643383279;
#pragma endregion
// ========================================================================
// define
#pragma region header
// #define int long long
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i <= n; i++)
#define INF (ll)10000000000000000
#define mod (ll)1000000007
#define P pair<lint, lint>
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b; // aをbで更新
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b; // aをbで更新
return true;
}
return false;
}
#pragma endregion
#pragma endregion
lint n;
vl a;
bool isOK(int index, int key) {
if (a[index] >= key)
return true;
else
return false;
}
int binary_search(int key) {
int ng = -1;
int ok = (int)a.size();
while (abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (isOK(mid, key))
ok = mid;
else
ng = mid;
}
if (a[ok] == key)
return ok;
else
return -1;
}
int main(void) {
vl e = {1, 2, 4, 8, 16, 32,
64, 128, 256, 512, 1024, 2048,
4096, 8192, 16384, 32768, 65536, 131072,
262144, 524288, 1048576, 2097152, 4194304, 8388608,
16777216, 33554432, 67108864, 134217728, 268435456, 536870912,
1073741824, 2147483648, 4294967296, 8589934592, 17179869184};
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
lint ans = 0;
lint cnt = 0;
while (a.size() > 1) {
for (lint i = 30; i >= 0; i--) {
if (a[a.size() - 1] >= e[i]) {
cnt = e[i + 1];
break;
}
}
lint d = a[a.size() - 1];
a.pop_back();
lint x = binary_search(cnt - d);
if (x >= 0 && x < a.size()) {
ans++;
a.erase(a.begin() + x);
}
}
printf("%lld\n", ans);
return 0;
}
| // include,using,define等
#pragma region header
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// ===============================================================
// using系
#pragma region header
using namespace std;
using ll = long long;
using lint = long long;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using vm = vector<short>;
using vmm = vector<vector<short>>;
using pii = pair<int, int>;
using psi = pair<string, int>;
using ld = long double;
using ull = unsigned long long;
using ui = unsigned int;
using qul = queue<ll>;
using pql = priority_queue<ll>;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
constexpr long double pi = 3.141592653589793238462643383279;
#pragma endregion
// ========================================================================
// define
#pragma region header
// #define int long long
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i <= n; i++)
#define INF (ll)10000000000000000
#define mod (ll)1000000007
#define P pair<lint, lint>
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b; // aをbで更新
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b; // aをbで更新
return true;
}
return false;
}
#pragma endregion
#pragma endregion
lint n;
vl a;
bool isOK(int index, int key) {
if (a[index] >= key)
return true;
else
return false;
}
int binary_search(int key) {
int ng = -1;
int ok = (int)a.size();
while (abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (isOK(mid, key))
ok = mid;
else
ng = mid;
}
if (a[ok] == key)
return ok;
else
return -1;
}
int main(void) {
vl e = {1, 2, 4, 8, 16, 32,
64, 128, 256, 512, 1024, 2048,
4096, 8192, 16384, 32768, 65536, 131072,
262144, 524288, 1048576, 2097152, 4194304, 8388608,
16777216, 33554432, 67108864, 134217728, 268435456, 536870912,
1073741824, 2147483648, 4294967296, 8589934592, 17179869184};
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
lint ans = 0;
lint cnt = 0;
while (a.size() > 1) {
for (lint i = 30; i >= 0; i--) {
if (a[a.size() - 1] >= e[i]) {
cnt = e[i + 1];
break;
}
}
lint d = a[a.size() - 1];
a.pop_back();
lint x = binary_search(cnt - d);
if (x != -1 && x != a.size()) {
ans++;
a.erase(a.begin() + x);
}
}
printf("%lld\n", ans);
return 0;
}
| replace | 139 | 140 | 139 | 140 | TLE | |
p03201 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int A[100000];
map<int, int> elem_occurence;
int closest_two_power(int n) {
for (int i = 1; i < 32; i++) {
if ((1 << (i - 1)) <= n && n < (1 << i)) {
return (1 << i);
}
}
return -1;
}
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
for (int i = 0; i < N; i++) {
if (elem_occurence.find(A[i]) == elem_occurence.end()) {
elem_occurence[A[i]] = 1;
} else {
elem_occurence[A[i]]++;
}
}
sort(A, A + N, greater<int>());
int ans = 0;
for (int i = 0; i < N; i++) {
if (elem_occurence.find(A[i]) == elem_occurence.end()) {
continue;
}
int target = closest_two_power(A[i]) - A[i];
if (target != A[i]) {
if (elem_occurence.find(target) != elem_occurence.end()) {
ans++;
elem_occurence[target]--;
elem_occurence[A[i]]--;
if (elem_occurence[target] == 0) {
elem_occurence.erase(target);
}
if (elem_occurence[A[i]] == 0) {
elem_occurence.erase(A[i]);
}
}
} else {
if (elem_occurence[target] >= 2) {
ans++;
elem_occurence[target] -= 2;
if (elem_occurence[target] == 0) {
elem_occurence.erase(target);
}
}
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int A[200000];
map<int, int> elem_occurence;
int closest_two_power(int n) {
for (int i = 1; i < 32; i++) {
if ((1 << (i - 1)) <= n && n < (1 << i)) {
return (1 << i);
}
}
return -1;
}
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
for (int i = 0; i < N; i++) {
if (elem_occurence.find(A[i]) == elem_occurence.end()) {
elem_occurence[A[i]] = 1;
} else {
elem_occurence[A[i]]++;
}
}
sort(A, A + N, greater<int>());
int ans = 0;
for (int i = 0; i < N; i++) {
if (elem_occurence.find(A[i]) == elem_occurence.end()) {
continue;
}
int target = closest_two_power(A[i]) - A[i];
if (target != A[i]) {
if (elem_occurence.find(target) != elem_occurence.end()) {
ans++;
elem_occurence[target]--;
elem_occurence[A[i]]--;
if (elem_occurence[target] == 0) {
elem_occurence.erase(target);
}
if (elem_occurence[A[i]] == 0) {
elem_occurence.erase(A[i]);
}
}
} else {
if (elem_occurence[target] >= 2) {
ans++;
elem_occurence[target] -= 2;
if (elem_occurence[target] == 0) {
elem_occurence.erase(target);
}
}
}
}
cout << ans << endl;
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p03201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for (ll i = m, i##_len = (n); i < i##_len; ++i)
#define reprev(i, n) for (ll i = (n - 1LL); i >= 0; --i)
#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;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int n, ans = 0;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
sort(all(a) /*, [](int a, int b) {return a > b;}*/);
// rep (i, n) eprintf("%d ", a[i]); eprintf("\n");
vector<bool> used(n);
reprev(i, n) {
if (used[i])
continue;
used[i] = true;
int k = 0, tmp = a[i];
while (tmp != 0) {
tmp >>= 1;
k++;
}
tmp = (1 << k) - a[i];
eprintf("%d+%d=2^n\n", a[i], tmp);
auto p = equal_range(all(a), tmp);
int ng = (int)distance(a.begin(), p.second) - 1;
while (ng > 0 && a[ng - 1] == tmp && used[ng])
ng--;
if (ng >= 0 && a[ng] == tmp && !used[ng]) {
eprintf("(%lld,%d)\n", i, ng);
ans++;
used[ng] = true;
}
}
cout << ans << "\n";
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for (ll i = m, i##_len = (n); i < i##_len; ++i)
#define reprev(i, n) for (ll i = (n - 1LL); i >= 0; --i)
#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;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int n, ans = 0;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
sort(all(a) /*, [](int a, int b) {return a > b;}*/);
// rep (i, n) eprintf("%d ", a[i]); eprintf("\n");
vector<bool> used(n);
reprev(i, n) {
if (used[i])
continue;
used[i] = true;
int k = 0, tmp = a[i];
while (tmp != 0) {
tmp >>= 1;
k++;
}
tmp = (1 << k) - a[i];
eprintf("%d+%d=2^n\n", a[i], tmp);
auto p = equal_range(all(a), tmp);
int ng = min((int)i, (int)distance(a.begin(), p.second) - 1);
while (ng > 0 && a[ng - 1] == tmp && used[ng])
ng--;
if (ng >= 0 && a[ng] == tmp && !used[ng]) {
eprintf("(%lld,%d)\n", i, ng);
ans++;
used[ng] = true;
}
}
cout << ans << "\n";
return 0;
} | replace | 76 | 77 | 76 | 77 | TLE | |
p03201 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int ll;
int N, a[200001];
void solve() {
map<int, int> M;
for (int i = 0; i < N; i++) {
cin >> a[i];
M[a[i]]++;
}
sort(a, a + N);
int ret = 0;
int pw = 1 << 30;
for (int i = N - 1; i >= 0; i++) {
if (M[a[i]]) {
M[a[i]]--;
while (pw >= 2 && 2 * a[i] < pw)
pw >>= 1;
if (M.count(pw - a[i]) && M[pw - a[i]]) {
ret++;
M[pw - a[i]]--;
}
}
}
cout << ret << endl;
}
int main() {
while (cin >> N) {
solve();
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int ll;
int N, a[200001];
void solve() {
map<int, int> M;
for (int i = 0; i < N; i++) {
cin >> a[i];
M[a[i]]++;
}
sort(a, a + N);
int ret = 0;
int pw = 1 << 30;
for (int i = N - 1; i >= 0; i--) {
if (M[a[i]]) {
M[a[i]]--;
while (pw >= 2 && 2 * a[i] < pw)
pw >>= 1;
if (M.count(pw - a[i]) && M[pw - a[i]]) {
ret++;
M[pw - a[i]]--;
}
}
}
cout << ret << endl;
}
int main() {
while (cin >> N) {
solve();
}
return 0;
} | replace | 18 | 19 | 18 | 19 | -11 | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define PF push_front
#define MP make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 1e5 + 5;
int a[maxn];
map<int, int> mp;
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
mp[a[i]]++;
}
sort(a, a + n, greater<int>());
int ans = 0;
for (int i = 0; i < n; i++) {
if (mp[a[i]] != 0) {
mp[a[i]]--;
for (int j = 30; j >= 0; j--) {
if (mp[(1 << j) - a[i]] > 0) {
ans++;
mp[(1 << j) - a[i]]--;
}
}
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define PF push_front
#define MP make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 2e5 + 5;
int a[maxn];
map<int, int> mp;
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
mp[a[i]]++;
}
sort(a, a + n, greater<int>());
int ans = 0;
for (int i = 0; i < n; i++) {
if (mp[a[i]] != 0) {
mp[a[i]]--;
for (int j = 30; j >= 0; j--) {
if (mp[(1 << j) - a[i]] > 0) {
ans++;
mp[(1 << j) - a[i]]--;
}
}
}
}
cout << ans << endl;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define N 500005
using namespace std;
map<int, int> mp;
int n, ans, a[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), mp[a[i]]++;
sort(a + 1, a + n + 1);
for (int i = n; i; i--) {
if (!mp[a[i]])
continue;
for (int j = 1; j <= (1 << 30); j *= 2) {
if (!mp[a[i]])
break;
int x = j - a[i];
// if (x<0) continue;
if (mp[x]) {
if (x == a[i]) {
ans += mp[a[i]] / 2;
mp[a[i]] %= 2;
} else {
int t = min(mp[a[i]], mp[x]);
ans += t;
mp[a[i]] -= t;
mp[x] -= t;
}
}
if (j == (1 << 30))
break;
}
}
printf("%d\n", ans);
return 0;
} | #include <bits/stdc++.h>
#define N 500005
using namespace std;
map<int, int> mp;
int n, ans, a[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), mp[a[i]]++;
sort(a + 1, a + n + 1);
for (int i = n; i; i--) {
if (!mp[a[i]])
continue;
for (int j = 1; j <= (1 << 30); j *= 2) {
if (!mp[a[i]])
break;
int x = j - a[i];
if (x < 0)
continue;
if (mp[x]) {
if (x == a[i]) {
ans += mp[a[i]] / 2;
mp[a[i]] %= 2;
} else {
int t = min(mp[a[i]], mp[x]);
ans += t;
mp[a[i]] -= t;
mp[x] -= t;
}
}
if (j == (1 << 30))
break;
}
}
printf("%d\n", ans);
return 0;
} | replace | 17 | 18 | 17 | 19 | TLE | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
const ll MOD = 1e9 + 7;
int main() {
ll n, b[111111];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> b[i];
}
sort(b, b + n);
vector<ll> a;
vector<ll> cnt;
for (int i = 0; i < n;) {
int k = 0;
while (1) {
if (i + k >= n)
break;
if (b[i] != b[i + k])
break;
k++;
}
a.push_back(b[i]);
cnt.push_back(k);
i += k;
}
n = a.size();
ll ans = 0;
for (ll i = n - 1; i >= 0; i--) {
// cout<<a[i]<<" "<<cnt[i]<<endl;
if (cnt[i] == 0)
continue;
vector<ll> v;
ll t = a[i];
// cout<<"t "<<t<<endl;
while (t) {
v.push_back(t % 2);
t /= 2;
}
// for(int j=0;j<v.size();j++)cout<<"v "<<v[j]<<endl;
int l = v.size();
for (int j = (int)v.size() - 1; j >= 0; j--) {
if (v[j] == 0)
break;
l--;
}
ll k = 0;
for (ll j = 0; j < l; j++) {
if (k == 0 && v[j])
k += pow(2LL, j);
if (k && v[j] == 0)
k += pow(2LL, j);
}
// cout<<k<<endl;
if (k == 0 && v.size() == l + 1) {
ans += cnt[i] / 2;
continue;
}
if (k == 0)
k = pow(2LL, l);
// cout<<k<<endl;
ll ok = 0, ng = n, mid;
while (ng - ok > 1) {
mid = (ok + ng) / 2;
if (a[mid] <= k)
ok = mid;
else
ng = mid;
}
if (a[ok] == k) {
ans += min(cnt[ok], cnt[i]);
cnt[ok] = max(0LL, cnt[ok] - cnt[i]);
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
const ll MOD = 1e9 + 7;
int main() {
ll n, b[222222];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> b[i];
}
sort(b, b + n);
vector<ll> a;
vector<ll> cnt;
for (int i = 0; i < n;) {
int k = 0;
while (1) {
if (i + k >= n)
break;
if (b[i] != b[i + k])
break;
k++;
}
a.push_back(b[i]);
cnt.push_back(k);
i += k;
}
n = a.size();
ll ans = 0;
for (ll i = n - 1; i >= 0; i--) {
// cout<<a[i]<<" "<<cnt[i]<<endl;
if (cnt[i] == 0)
continue;
vector<ll> v;
ll t = a[i];
// cout<<"t "<<t<<endl;
while (t) {
v.push_back(t % 2);
t /= 2;
}
// for(int j=0;j<v.size();j++)cout<<"v "<<v[j]<<endl;
int l = v.size();
for (int j = (int)v.size() - 1; j >= 0; j--) {
if (v[j] == 0)
break;
l--;
}
ll k = 0;
for (ll j = 0; j < l; j++) {
if (k == 0 && v[j])
k += pow(2LL, j);
if (k && v[j] == 0)
k += pow(2LL, j);
}
// cout<<k<<endl;
if (k == 0 && v.size() == l + 1) {
ans += cnt[i] / 2;
continue;
}
if (k == 0)
k = pow(2LL, l);
// cout<<k<<endl;
ll ok = 0, ng = n, mid;
while (ng - ok > 1) {
mid = (ok + ng) / 2;
if (a[mid] <= k)
ok = mid;
else
ng = mid;
}
if (a[ok] == k) {
ans += min(cnt[ok], cnt[i]);
cnt[ok] = max(0LL, cnt[ok] - cnt[i]);
}
}
cout << ans << endl;
return 0;
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p03201 | C++ | Time Limit Exceeded | //{{{
#include <bits/stdc++.h>
using namespace std;
#define repX(a, b, c, x, ...) x
#define repN(a) repX a
#define rep(...) repN((__VA_ARGS__, rep3, rep2, loop))(__VA_ARGS__)
#define rrep(...) repN((__VA_ARGS__, rrep3, rrep2))(__VA_ARGS__)
#define loop(n) rep2(i_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, begin, end) \
for (int i = (int)(begin), i##_end = (int)(end); i < i##_end; ++i)
#define rrep2(i, n) rrep3(i, n, 0)
#define rrep3(i, begin, end) \
for (int i = (int)(begin - 1), i##_end = (int)(end); i >= i##_end; --i)
#define foreach(x, a) for (auto &x : a)
using ll = long long;
const ll mod = (ll)1e9 + 7;
//}}}
int a[200000];
map<int, int> mp;
int target[32];
int f(int n) {
rep(i, 32) {
int num = target[i] - n;
if (n != num && mp[n] && mp[num] || n == num && mp[n] >= 2) {
// cout << n << " "<< num << endl;
mp[n]--;
mp[num]--;
return 1;
}
}
return 0;
}
int main() {
int N;
cin >> N;
target[0] = 1;
rep(i, 1, 32) { target[i] = target[i - 1] * 2; }
rep(i, N) {
cin >> a[i];
mp[a[i]]++;
}
sort(a, a + N);
reverse(a, a + N);
ll ans = 0;
rep(i, N) { ans += f(a[i]); }
cout << ans << endl;
return 0;
}
| //{{{
#include <bits/stdc++.h>
using namespace std;
#define repX(a, b, c, x, ...) x
#define repN(a) repX a
#define rep(...) repN((__VA_ARGS__, rep3, rep2, loop))(__VA_ARGS__)
#define rrep(...) repN((__VA_ARGS__, rrep3, rrep2))(__VA_ARGS__)
#define loop(n) rep2(i_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, begin, end) \
for (int i = (int)(begin), i##_end = (int)(end); i < i##_end; ++i)
#define rrep2(i, n) rrep3(i, n, 0)
#define rrep3(i, begin, end) \
for (int i = (int)(begin - 1), i##_end = (int)(end); i >= i##_end; --i)
#define foreach(x, a) for (auto &x : a)
using ll = long long;
const ll mod = (ll)1e9 + 7;
//}}}
int a[200000];
map<int, int> mp;
int target[32];
int f(int n) {
rep(i, 32) {
int num = target[i] - n;
if (num > n)
continue;
if (num < 0)
continue;
if (n != num && mp[n] && mp[num] || n == num && mp[n] >= 2) {
// cout << n << " "<< num << endl;
mp[n]--;
mp[num]--;
return 1;
}
}
return 0;
}
int main() {
int N;
cin >> N;
target[0] = 1;
rep(i, 1, 32) { target[i] = target[i - 1] * 2; }
rep(i, N) {
cin >> a[i];
mp[a[i]]++;
}
sort(a, a + N);
reverse(a, a + N);
ll ans = 0;
rep(i, N) { ans += f(a[i]); }
cout << ans << endl;
return 0;
}
| insert | 27 | 27 | 27 | 31 | TLE | |
p03201 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;
typedef pair<int, int> P;
#define EPS (1e-7)
#define INF (1e12)
#define PI (acos(-1))
#define amari(x) ((x) % 1000000007)
#define fillInt(xs, x) \
for (int i = 0; i < (x); i++) \
scanf("%d", &xs[i]);
#define fillLong(xs, x) \
for (int i = 0; i < (x); i++) \
scanf("%ld", &xs[i]);
#define fillString(xs, x) \
for (int i = 0; i < (x); i++) \
cin >> xs[i];
#define sortv(xs) sort(xs.begin(), xs.end())
#define sortvdi(xs) sort(xs.begin(), xs.end(), std::greater<int>())
#define lbv(xs, x) lower_bound(xs.begin(), xs.end(), x) - xs.begin()
#define ubv(xs, x) upper_bound(xs.begin(), xs.end(), x) - xs.begin()
#define bs(xs, x) binary_search(xs.begin(), xs.end(), x)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define isValidPoint(x, y, mx, my) x >= 0 && x < mx &&y >= 0 && y < my
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long n;
cin >> n;
vector<long> a(n);
fillLong(a, n);
int cnts[1000000000] = {};
long cnt = 0;
sortv(a);
long m = 1;
while (m <= 1000000000 * 2) {
m *= 2;
}
rep(i, n) { cnts[a[i]]++; }
for (long i = n - 1; i > 0; i--) {
if (cnts[a[i]] == 0)
continue;
for (long j = m; j > 1; j = j / 2) {
if (bs(a, j - a[i]) && ((j - a[i] == a[i] && cnts[j - a[i]] > 1) ||
(j - a[i] != a[i] && cnts[j - a[i]] > 0))) {
cnt++;
cnts[a[i]]--;
cnts[j - a[i]]--;
break;
}
}
}
cout << cnt << endl;
return 0;
}
| #include <algorithm>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;
typedef pair<int, int> P;
#define EPS (1e-7)
#define INF (1e12)
#define PI (acos(-1))
#define amari(x) ((x) % 1000000007)
#define fillInt(xs, x) \
for (int i = 0; i < (x); i++) \
scanf("%d", &xs[i]);
#define fillLong(xs, x) \
for (int i = 0; i < (x); i++) \
scanf("%ld", &xs[i]);
#define fillString(xs, x) \
for (int i = 0; i < (x); i++) \
cin >> xs[i];
#define sortv(xs) sort(xs.begin(), xs.end())
#define sortvdi(xs) sort(xs.begin(), xs.end(), std::greater<int>())
#define lbv(xs, x) lower_bound(xs.begin(), xs.end(), x) - xs.begin()
#define ubv(xs, x) upper_bound(xs.begin(), xs.end(), x) - xs.begin()
#define bs(xs, x) binary_search(xs.begin(), xs.end(), x)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define isValidPoint(x, y, mx, my) x >= 0 && x < mx &&y >= 0 && y < my
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long n;
cin >> n;
vector<long> a(n);
fillLong(a, n);
map<long, int> cnts;
long cnt = 0;
sortv(a);
long m = 1;
while (m <= 1000000000 * 2) {
m *= 2;
}
rep(i, n) { cnts[a[i]]++; }
for (long i = n - 1; i > 0; i--) {
if (cnts[a[i]] == 0)
continue;
for (long j = m; j > 1; j = j / 2) {
if (bs(a, j - a[i]) && ((j - a[i] == a[i] && cnts[j - a[i]] > 1) ||
(j - a[i] != a[i] && cnts[j - a[i]] > 0))) {
cnt++;
cnts[a[i]]--;
cnts[j - a[i]]--;
break;
}
}
}
cout << cnt << endl;
return 0;
}
| replace | 56 | 57 | 56 | 58 | -11 | |
p03201 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define SIZE 300005
#define MOD 1000000007LL
#define EPS 1e-10
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define DOWN(i, b, a) for (int i = b; i >= a; i--)
#define SET(a, c) memset(a, c, sizeof a)
#define BIT(i, j) ((i) >> (j)) & 1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(), (o).end()), (o).end())
#define SQ(x) ((x) * (x))
using namespace std;
typedef int_fast64_t ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
typedef complex<double> dcomplex;
template <typename T> inline void priv(vector<T> a) {
REP(i, a.size()) { cerr << a[i] << ((i == a.size() - 1) ? "\n" : " "); }
}
inline void fastio() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
ll gcd(ll a, ll b) {
ll c = max(a, b);
ll d = min(a, b);
return c == 0 || d == 0 ? c : gcd(c % d, d);
}
ll lcm(ll a, ll b) { return a == 0 || b == 0 ? 0 : a * b / gcd(a, b); }
ll modfact(ll a) {
ll b = 1;
FOR(i, 2, a) b = b * i % MOD;
return b;
}
ll modpow(ll a, ll n) {
ll b = 1;
while (n > 0) {
if (n & 1)
b = b * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return b;
}
ll modcomb(ll n, ll k) {
ll b = 1;
k = min(n - k, k);
DOWN(i, n, n - k + 1) b = b * i % MOD;
return b * modpow(modfact(k), MOD - 2) % MOD;
}
int N, ans = 0;
ll A[200000];
bool B[200000] = {false};
int main() {
scanf("%d", &N);
REP(i, N) scanf("%d", A + i);
sort(A, A + N);
for (int i = N - 1; i >= 0; i--) {
int p = 0;
while (A[i] >= 1LL << p)
p++;
ll x = (1LL << p) - A[i];
int j = lower_bound(A, A + N, x) - A;
if (B[i])
continue;
while (j < i && A[j] == x) {
if (!B[j]) {
B[i] = true;
B[j] = true;
ans++;
break;
}
j++;
}
}
printf("%d", ans);
return 0;
} | #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define SIZE 300005
#define MOD 1000000007LL
#define EPS 1e-10
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define DOWN(i, b, a) for (int i = b; i >= a; i--)
#define SET(a, c) memset(a, c, sizeof a)
#define BIT(i, j) ((i) >> (j)) & 1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(), (o).end()), (o).end())
#define SQ(x) ((x) * (x))
using namespace std;
typedef int_fast64_t ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
typedef complex<double> dcomplex;
template <typename T> inline void priv(vector<T> a) {
REP(i, a.size()) { cerr << a[i] << ((i == a.size() - 1) ? "\n" : " "); }
}
inline void fastio() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
ll gcd(ll a, ll b) {
ll c = max(a, b);
ll d = min(a, b);
return c == 0 || d == 0 ? c : gcd(c % d, d);
}
ll lcm(ll a, ll b) { return a == 0 || b == 0 ? 0 : a * b / gcd(a, b); }
ll modfact(ll a) {
ll b = 1;
FOR(i, 2, a) b = b * i % MOD;
return b;
}
ll modpow(ll a, ll n) {
ll b = 1;
while (n > 0) {
if (n & 1)
b = b * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return b;
}
ll modcomb(ll n, ll k) {
ll b = 1;
k = min(n - k, k);
DOWN(i, n, n - k + 1) b = b * i % MOD;
return b * modpow(modfact(k), MOD - 2) % MOD;
}
int N, ans = 0;
ll A[200000];
bool B[200000] = {false};
int main() {
scanf("%d", &N);
REP(i, N) scanf("%d", A + i);
sort(A, A + N);
for (int i = N - 1; i >= 0; i--) {
int p = 0;
while (A[i] >= 1LL << p)
p++;
ll x = (1LL << p) - A[i];
int j = lower_bound(A, A + N, x) - A;
if (B[i])
continue;
while (j < i && A[j] == x) {
if (!B[j]) {
B[i] = true;
B[j] = true;
A[j]--;
ans++;
break;
}
j++;
}
}
printf("%d", ans);
return 0;
} | insert | 79 | 79 | 79 | 80 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(x) cout << #x << ": " << x << endl
// #define int long long
int powint(int a, int x) {
int res = 1;
REP(i, x) { res = res * a; }
return res;
}
#define vi vector<int>
using namespace std;
int target(int src) {
int tmp = 1;
while (tmp <= src) {
tmp *= 2;
}
return tmp;
}
signed main() {
int N;
cin >> N;
vi A(N);
REP(i, N) { cin >> A[i]; }
sort(A.begin(), A.end()); // 昇順
// REP(i,A.size()) cout<<A[i]<<endl;
int ans = 0;
vector<bool> used(N, false);
RREP(i, N) {
int t = target(A[i]);
int u = t - A[i];
int l = lower_bound(A.begin(), A.begin() + i, u) - A.begin();
int r = upper_bound(A.begin(), A.begin() + i, u) - A.begin();
// debug(t); debug(u); debug(l); debug(r);
for (auto j = r - 1; j >= l; --j) {
if (!used[j] && !used[i]) {
used[j] = true;
used[i] = true;
ans++;
break;
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(x) cout << #x << ": " << x << endl
// #define int long long
int powint(int a, int x) {
int res = 1;
REP(i, x) { res = res * a; }
return res;
}
#define vi vector<int>
using namespace std;
int target(int src) {
int tmp = 1;
while (tmp <= src) {
tmp *= 2;
}
return tmp;
}
signed main() {
int N;
cin >> N;
vi A(N);
REP(i, N) { cin >> A[i]; }
sort(A.begin(), A.end()); // 昇順
// REP(i,A.size()) cout<<A[i]<<endl;
int ans = 0;
vector<bool> used(N, false);
RREP(i, N) {
if (used[i])
continue;
int t = target(A[i]);
int u = t - A[i];
int l = lower_bound(A.begin(), A.begin() + i, u) - A.begin();
int r = upper_bound(A.begin(), A.begin() + i, u) - A.begin();
// debug(t); debug(u); debug(l); debug(r);
for (auto j = r - 1; j >= l; --j) {
if (!used[j] && !used[i]) {
used[j] = true;
used[i] = true;
ans++;
break;
}
}
}
cout << ans << endl;
return 0;
}
| insert | 34 | 34 | 34 | 36 | TLE | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define ll long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repeat(i, s, n) for (int(i) = s; (i) < (n); (i)++)
#define revrep(i, n) for (int(i) = (n)-1; i >= 0; i--)
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(std::numeric_limits<float>::max_digits10);
int n;
cin >> n;
multiset<ll> s;
rep(i, n) {
ll x;
cin >> x;
s.insert(x);
}
int ans = 0;
while (!s.empty()) {
auto m = max_element(s.begin(), s.end());
s.erase(m);
ll y = 1;
while (y <= *m)
y *= 2;
if (s.find(y - *m) != s.end()) {
s.erase(s.find(y - *m));
ans++;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define ll long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repeat(i, s, n) for (int(i) = s; (i) < (n); (i)++)
#define revrep(i, n) for (int(i) = (n)-1; i >= 0; i--)
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(std::numeric_limits<float>::max_digits10);
int n;
cin >> n;
multiset<ll> s;
rep(i, n) {
ll x;
cin >> x;
s.insert(x);
}
int ans = 0;
while (!s.empty()) {
auto m = s.end();
m--;
s.erase(m);
ll y = 1;
while (y <= *m)
y *= 2;
if (s.find(y - *m) != s.end()) {
s.erase(s.find(y - *m));
ans++;
}
}
cout << ans << endl;
return 0;
}
| replace | 22 | 23 | 22 | 24 | TLE | |
p03201 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
typedef int ll;
const int N = 200005;
const int E = 2000006;
vector<int> g[N];
int ne;
struct Edge {
int from, to;
ll flow, cap;
} edge[E];
int lvl[N], vis[N], pass, start = N - 2, target = N - 1;
int qu[N], qt, px[N];
ll run(int s, int sink, ll minE) {
if (s == sink)
return minE;
ll ans = 0;
for (; px[s] < (int)g[s].size(); px[s]++) {
int e = g[s][px[s]];
auto &v = edge[e], &rev = edge[e ^ 1];
if (lvl[v.to] != lvl[s] + 1 || v.flow >= v.cap)
continue;
ll tmp = run(v.to, sink, min(minE, v.cap - v.flow));
v.flow += tmp, rev.flow -= tmp;
ans += tmp, minE -= tmp;
if (minE == 0)
break;
}
return ans;
}
bool bfs(int source, int sink) {
qt = 0;
qu[qt++] = source;
lvl[source] = 1;
vis[source] = ++pass;
for (int i = 0; i < qt; i++) {
int u = qu[i];
px[u] = 0;
if (u == sink)
return true;
for (int e : g[u]) {
auto v = edge[e];
if (v.flow >= v.cap || vis[v.to] == pass)
continue;
vis[v.to] = pass;
lvl[v.to] = lvl[u] + 1;
qu[qt++] = v.to;
}
}
return false;
}
ll flow(int source = start, int sink = target) {
ll ans = 0;
while (bfs(source, sink))
ans += run(source, sink, N);
return ans;
}
void addEdge(int u, int v, ll c = 1, ll rc = 0) {
edge[ne] = {u, v, 0, c};
g[u].push_back(ne++);
edge[ne] = {v, u, 0, rc};
g[v].push_back(ne++);
}
int c[N];
int er[N];
void dfs(int u) {
for (int e : g[u]) {
auto v = edge[e];
if (er[v.to])
continue;
if (c[v.to] == c[u])
assert(0);
if (!c[v.to])
c[v.to] = 3 - c[u], dfs(v.to);
}
}
int n, v[N];
int deg[N];
// vector<pair<int, int>> edg;
map<int, vector<int>> cnt;
int build(int val) {
int ans = 0;
// cnt.clear();
for (int i = 0; i < n; i++)
if (!deg[i]) {
if (cnt.count(val - v[i])) {
int j;
do {
j = cnt[val - v[i]].back();
cnt[val - v[i]].pop_back();
if (i == j) {
if (!cnt[val - v[i]].empty()) {
j = cnt[val - v[i]].back();
cnt[val - v[i]].back() = i;
} else {
cnt[val - v[i]].push_back(i);
break;
}
}
} while (deg[j]);
if (i == j)
continue;
if (cnt[val - v[i]].empty())
cnt.erase(val - v[i]);
deg[i]++, deg[j]++;
// edge.emplace_back(j, i);
ans++;
// addEdge(i, j, 1, 1);
}
// else cnt[ v[i] ].push_back(i);
}
return ans;
}
int clean() {
int ans = 0;
queue<int> q;
for (int i = 0; i < n; i++)
if (deg[i] == 1) {
q.push(i);
}
int u;
while (!q.empty()) {
u = q.front();
q.pop();
if (er[u])
continue;
int e = g[u][0];
g[u].clear();
ans++;
er[u] = er[edge[e].to] = 1;
for (int e2 : g[edge[e].to]) {
if (er[edge[e2].to])
continue;
deg[edge[e2].to]--;
edge[e2 ^ 1].cap = 0;
if (deg[edge[e2].to] == 1) {
q.push(edge[e2].to);
}
}
g[edge[e].to].clear();
}
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", v + i);
for (int i = 0; i < n; i++)
cnt[v[i]].push_back(i);
int ans = 0;
for (int i = 30; i >= 0; i--)
ans += build(1 << i);
printf("%d\n", ans);
return 0;
// int ans = clean();
for (int i = 0; i < n; i++)
if (!c[i] && !er[i])
c[i] = 1, dfs(i);
for (int i = 0; i < n; i++)
if (!er[i]) {
if (c[i] == 1)
addEdge(start, i);
else
addEdge(i, target);
}
printf("%d\n", ans + flow());
}
| #include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
typedef int ll;
const int N = 200005;
const int E = 2000006;
vector<int> g[N];
int ne;
struct Edge {
int from, to;
ll flow, cap;
} edge[E];
int lvl[N], vis[N], pass, start = N - 2, target = N - 1;
int qu[N], qt, px[N];
ll run(int s, int sink, ll minE) {
if (s == sink)
return minE;
ll ans = 0;
for (; px[s] < (int)g[s].size(); px[s]++) {
int e = g[s][px[s]];
auto &v = edge[e], &rev = edge[e ^ 1];
if (lvl[v.to] != lvl[s] + 1 || v.flow >= v.cap)
continue;
ll tmp = run(v.to, sink, min(minE, v.cap - v.flow));
v.flow += tmp, rev.flow -= tmp;
ans += tmp, minE -= tmp;
if (minE == 0)
break;
}
return ans;
}
bool bfs(int source, int sink) {
qt = 0;
qu[qt++] = source;
lvl[source] = 1;
vis[source] = ++pass;
for (int i = 0; i < qt; i++) {
int u = qu[i];
px[u] = 0;
if (u == sink)
return true;
for (int e : g[u]) {
auto v = edge[e];
if (v.flow >= v.cap || vis[v.to] == pass)
continue;
vis[v.to] = pass;
lvl[v.to] = lvl[u] + 1;
qu[qt++] = v.to;
}
}
return false;
}
ll flow(int source = start, int sink = target) {
ll ans = 0;
while (bfs(source, sink))
ans += run(source, sink, N);
return ans;
}
void addEdge(int u, int v, ll c = 1, ll rc = 0) {
edge[ne] = {u, v, 0, c};
g[u].push_back(ne++);
edge[ne] = {v, u, 0, rc};
g[v].push_back(ne++);
}
int c[N];
int er[N];
void dfs(int u) {
for (int e : g[u]) {
auto v = edge[e];
if (er[v.to])
continue;
if (c[v.to] == c[u])
assert(0);
if (!c[v.to])
c[v.to] = 3 - c[u], dfs(v.to);
}
}
int n, v[N];
int deg[N];
// vector<pair<int, int>> edg;
map<int, vector<int>> cnt;
int build(int val) {
int ans = 0;
// cnt.clear();
for (int i = 0; i < n; i++)
if (!deg[i]) {
if (cnt.count(val - v[i])) {
int j;
do {
if (cnt[val - v[i]].empty()) {
j = i;
cnt.erase(val - v[i]);
break;
}
j = cnt[val - v[i]].back();
cnt[val - v[i]].pop_back();
if (i == j) {
if (!cnt[val - v[i]].empty()) {
j = cnt[val - v[i]].back();
cnt[val - v[i]].back() = i;
} else {
cnt[val - v[i]].push_back(i);
break;
}
}
} while (deg[j]);
if (i == j)
continue;
if (cnt[val - v[i]].empty())
cnt.erase(val - v[i]);
deg[i]++, deg[j]++;
// edge.emplace_back(j, i);
ans++;
// addEdge(i, j, 1, 1);
}
// else cnt[ v[i] ].push_back(i);
}
return ans;
}
int clean() {
int ans = 0;
queue<int> q;
for (int i = 0; i < n; i++)
if (deg[i] == 1) {
q.push(i);
}
int u;
while (!q.empty()) {
u = q.front();
q.pop();
if (er[u])
continue;
int e = g[u][0];
g[u].clear();
ans++;
er[u] = er[edge[e].to] = 1;
for (int e2 : g[edge[e].to]) {
if (er[edge[e2].to])
continue;
deg[edge[e2].to]--;
edge[e2 ^ 1].cap = 0;
if (deg[edge[e2].to] == 1) {
q.push(edge[e2].to);
}
}
g[edge[e].to].clear();
}
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", v + i);
for (int i = 0; i < n; i++)
cnt[v[i]].push_back(i);
int ans = 0;
for (int i = 30; i >= 0; i--)
ans += build(1 << i);
printf("%d\n", ans);
return 0;
// int ans = clean();
for (int i = 0; i < n; i++)
if (!c[i] && !er[i])
c[i] = 1, dfs(i);
for (int i = 0; i < n; i++)
if (!er[i]) {
if (c[i] == 1)
addEdge(start, i);
else
addEdge(i, target);
}
printf("%d\n", ans + flow());
}
| insert | 108 | 108 | 108 | 113 | 0 | |
p03201 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int to[N], n, x, cnt, ans;
map<int, int> Map;
int cmp(int x, int y) { return x > y; }
int main() {
scanf("%d", &n);
while (n--) {
scanf("%d", &x);
if (!Map.count(x))
to[++cnt] = x;
Map[x]++;
}
sort(to + 1, to + cnt + 1, cmp);
for (int i = 1; i <= cnt; i++)
for (int j = 0; j <= 30; j++)
while (Map[to[i]] > 0 && Map[(1 << j) - to[i]] > 0) {
if (to[i] * 2 == (1 << j) && Map[to[i]] == 1)
break;
Map[to[i]]--, Map[(1 << j) - to[i]]--, ans++;
}
printf("%d\n", ans);
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int to[N], n, x, cnt, ans;
map<int, int> Map;
int cmp(int x, int y) { return x > y; }
int main() {
scanf("%d", &n);
while (n--) {
scanf("%d", &x);
if (!Map.count(x))
to[++cnt] = x;
Map[x]++;
}
sort(to + 1, to + cnt + 1, cmp);
for (int i = 1; i <= cnt; i++)
for (int j = 0; j <= 30; j++)
if ((1 << j) > to[i])
while (Map[to[i]] > 0 && Map[(1 << j) - to[i]] > 0) {
if (to[i] * 2 == (1 << j) && Map[to[i]] == 1)
break;
Map[to[i]]--, Map[(1 << j) - to[i]]--, ans++;
}
printf("%d\n", ans);
}
| replace | 17 | 22 | 17 | 23 | TLE | |
p03201 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
using edge = struct {
ll to;
ll cost;
};
struct point {
ll x;
ll y;
bool operator<(const point &p) const {
if (x == p.x)
return y < p.y;
return x < p.x;
}
};
struct undirected_edge {
ll from;
ll to;
ll cost;
bool operator<(const undirected_edge &ue) const { return cost < ue.cost; }
};
typedef string str;
typedef std::pair<ll, ll> pl;
typedef std::pair<ll, pl> pl3;
typedef std::map<string, ll> msl;
typedef std::map<char, ll> mcl;
typedef std::map<ll, ll> mll;
typedef std::vector<ll> vl;
typedef std::vector<pl> vpl;
typedef std::vector<point> points;
typedef std::vector<pl3> vpl3;
typedef std::priority_queue<ll> pq;
typedef std::priority_queue<ll, vl, greater<ll>>
pql; // priority queue taking from the lower value.
typedef std::vector<edge> Graph;
const ll N_DIGITS = 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = MOD * MOD;
const long double EPS = 1e-9;
const long double PI = 3.14159265358979323846;
vpl dirs = {
{1, 0}, {0, 1}, {-1, 0}, {0, -1}, // four directions
{1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal
{0, 0} // self
};
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--)
#define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define isUpper(c) ('a' - c > 0)
#define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9)
#define toLower(c) char((c) + 0x20)
#define toUpper(c) char((c)-0x20)
#define pb push_back
#define mp make_pair
#define pr(a) cout << (a)
#define prl(a) cout << (a) << endl
#define prl2(a, b) cout << (a) << " " << (b) << endl
#define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define prl4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define prs(a) cout << (a) << " "
#define yn(condition) \
if ((condition)) \
prl("Yes"); \
else \
prl("No");
#define YN(condition) \
if ((condition)) \
prl("YES"); \
else \
prl("NO");
#define in1(a) cin >> (a)
#define in2(a, b) cin >> (a) >> (b)
#define in3(a, b, c) cin >> (a) >> (b) >> (c)
#define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d)
#define e1 first
#define e2 second
#define ctol(c) ll((c)) - ll('0')
#define ltos(n) to_string((n))
#define items(kv, v) for (auto &(kv) : (v))
#define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)]))
#define rsort(a, n) sort(a, a + n, greater<>())
#define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++)
#define cntchar(s, c) count(all((s)), c)
#define substring(s, start, end) s.substr((start), (end) - (start) + 1)
#define prl_nd(num, digits) \
cout << fixed << setprecision(digits) << (num) << endl;
#define XOR(a, b) (a) ^ (b)
#define prl_time(s) \
prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]");
#define char_to_str(c) string(1, (c))
#define DEBUG(x) cerr << #x << ": " << (x) << endl
#define DEBUG_VEC(v) \
cerr << #v << ": "; \
rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \
cerr << endl
struct MaxFlow {
struct F_edge {
ll to, rev, capacity;
F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {}
};
typedef vector<F_edge> F_edges;
vector<F_edges> graph;
ll n_vertex;
// level is the shortest path to get a given node from the source node.
vl level, iter;
MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); }
void add_edge(ll from, ll to, ll capacity) {
graph[from].pb({to, ll(graph[to].size()), capacity});
graph[to].pb({from, ll(graph[from].size()) - 1, 0});
}
void bfs(ll source) {
level = vl(n_vertex, -1);
level[source] = 0;
queue<ll> q;
q.push(source);
while (!q.empty()) {
ll vertex = q.front();
q.pop();
rep(i, graph[vertex].size()) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
// if the flow can be into the target node, implement below.
if (cap_target > 0 && level[target] < 0) {
level[target] = level[vertex] + 1;
q.push(target);
}
}
}
}
ll dfs(ll vertex, ll sink, ll flow) {
if (vertex == sink)
return flow;
for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
ll rev_target = graph[vertex][i].rev;
// if capasitiy is not full yet and target is farther,
// then assign current flow
if (cap_target > 0 && level[vertex] < level[target]) {
ll d = dfs(target, sink, min(cap_target, flow));
if (d > 0) { // if the flow successfully reaches the sink, reduce the
// flow from the capacity
graph[vertex][i].capacity -= d;
graph[target][rev_target].capacity += d;
return d;
}
}
}
return 0;
}
ll dinic(ll source, ll sink) {
ll flow = 0;
while (true) {
bfs(source);
// if there is no path leading to the sink, the maximum flow is 0.
if (level[sink] < 0)
return flow;
iter = vl(n_vertex, 0);
ll f;
while ((f = dfs(source, sink, INF)) > 0)
flow += f;
}
}
};
class UnionFind {
vl parents, set_size;
public:
UnionFind() {}
UnionFind(ll n) {
parents = set_size = vl(n);
rep(i, n) {
parents[i] = i;
set_size[i] = 1LL;
}
}
ll root_find(ll x) {
if (parents[x] == x)
return x;
return parents[x] = root_find(parents[x]);
}
void unite(ll x, ll y) {
// priority for x is larger than that of y
x = root_find(x);
y = root_find(y);
if (x == y)
return;
if (set_size[x] < set_size[y]) {
parents[y] = x;
set_size[x] += set_size[y];
} else {
parents[x] = y;
set_size[y] += set_size[x];
}
}
bool is_same(ll x, ll y) { // connected or not
return root_find(x) == root_find(y);
}
ll size(ll x) { return set_size[root_find(x)]; }
};
struct Doubling { // ABC167D
ll n;
ll sz;
vector<vl> next;
/*
next[k + 1][i] := next[k][next[k][i]]
next[0][i] := edge[i]
e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1
a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times)
Let the function repeatedly input a[i] m times be f[m](a[i])
- get(i, x) returns f[x](a[i])
- lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j.
if not possible returns n.
*/
// edge[i]: the step size for one iteration
Doubling(vl &edge) : n(edge.size()), sz(62) {
next.resize(sz, vl(n, -1));
rep(i, n) next[0][i] = edge[i];
rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]];
}
ll get(ll i, ll x) {
ll ret = i;
rep(bit, sz) {
if (!(x >> bit & 1))
continue;
ret = next[bit][ret];
}
return ret;
}
ll lower_bound(ll i, ll j) {
ll cur = i, acc = 0;
revrep(wid, sz) {
if (next[wid][cur] < j) {
acc += 1LL << wid;
cur = next[wid][cur];
}
}
return min(n, acc + 1);
}
};
/*
class LCA{
public:
ll N, logN;
vl depth, len;
Graph tree[200005]; // global declaration later.
vector<vl> parents;
LCA(ll n){
N = n;
logN = 0;
while (N > (1LL << logN)) logN++;
depth = vl(N); len = vl(N);
parents = vector<vl>(logN, vl(N));
init(0, -1, 0, 0);
build();
}
void init(ll source, ll parent, ll d, ll l){
depth[source] = d;
parents[0][source] = parent;
len[source] = l;
rep(i, tree[source].size()){
ll target = tree[source][i].to;
ll cost = tree[source][i].cost;
if (target == parent) continue;
init(target, source, d + 1, cost + l);
}
}
void build(){
rep(k, logN - 1) rep(n, N){
// if there is no parent, -1.
// otherwise, the parent of the parent is the parent.
if (parents[k][n] < 0) parents[k + 1][n] = -1;
else parents[k + 1][n] = parents[k][parents[k][n]];
}
}
ll query(ll u, ll v){
if (depth[u] > depth[v]) swap(u, v);
rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v];
if (u == v) return u;
revrep(k, logN){
if (parents[k][u] != parents[k][v]){
u = parents[k][u]; v = parents[k][v];
}
}
return parents[0][u];
}
ll distance(ll u, ll v){
ll w = query(u, v);
return len[u] + len[v] - 2 * len[w];
}
};
*/
struct BIT {
ll n;
vl tree_dat;
BIT(ll n, ll ini = 0) : tree_dat(n + 1, ini), n(n){};
// x: 1001 1010 1100 1011 1101 1111
// x & - x: 0001 0010 0100 0001 0001 0001
// ->: 1010 1100 10000 1100 1100 10000
ll update_func(ll val, ll dat) {
// if maximum -> max(val, dat)
// return max(val, dat);
// if cumulative sum
// return val + dat;
return min(val, dat);
}
ll query(ll i) {
/*
e.g.) i = 10101
itr1. 10101 -> 10100
itr2. 10100 -> 10000
itr3. 10000 -> 00000 (break)
*/
ll ret = 0;
for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) {
ret = update_func(ret, tree_dat[j]);
}
return ret;
}
ll lower_bound(ll key) {
if (key <= 0)
return 0;
ll left = 0, right = 1;
while (right <= n)
right *= 2;
for (ll i = right; i > 0; i /= 2) {
if (left + i <= n && tree_dat[left + i - 1] < key) {
key -= tree_dat[left + i - 1];
left += i;
}
}
return left;
}
void update(ll i, ll val) {
/*
e.g.) i = 10101, n = 11111
itr1. i: 10101, i+1: 10110 -> 10111
itr2. i: 10111, i+1: 11000 -> 11111 (break)
*/
if (i < 0)
return;
for (ll j = i; j < n; j |= j + 1) {
tree_dat[j] = update_func(val, tree_dat[j]);
}
}
};
template <class Target> class RerootingTreeDP {
public:
using T = typename Target::type;
struct DP_edge {
ll to, rev; // rev is the index to trace the source node.
T value; // objective value
};
private:
ll n;
void dfs_fwd(ll source, ll parent) {
ll par_idx = -1;
vector<T> values;
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent) {
par_idx = i;
continue;
}
dfs_fwd(e.to, source);
values.pb(e.value);
}
// If the parent != -1, update the value on edge from parent to source
if (par_idx != -1) {
ll src_idx = tree[source][par_idx].rev;
// update values on the edge from parent to source
tree[parent][src_idx].value = Target::merge(values);
}
}
void dfs_bwd(ll source, ll parent) {
vector<T> values;
for (auto &&e : tree[source])
values.pb(e.value);
values = Target::evaluate(values);
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent)
continue;
// tree[e.to][e.rev]: e.to -> source
tree[e.to][e.rev].value = values[i];
dfs_bwd(e.to, source);
}
}
public:
UnionFind uf;
vector<vector<DP_edge>> tree;
RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {}
void add_edge(ll u, ll v, T val) {
assert(!uf.is_same(u, v));
tree[u].pb({v, ll(tree[v].size()), val});
tree[v].pb({u, ll(tree[u].size()) - 1, val});
uf.unite(u, v);
}
void dp() {
vector<bool> visited(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_fwd(i, -1);
visited[uf.root_find(i)] = true;
}
visited.assign(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_bwd(i, -1);
visited[uf.root_find(i)] = true;
}
}
ll size() const { return tree.size(); }
};
// ABC160F is one example
// Modify the functions evaluate and merge based on given problems
struct Merger {
using type = ll;
static vector<type> evaluate(const vector<type> &value) {
const ll n = value.size();
vl L(n + 1, 1), R(n + 1, 1);
rep(i, n) L[i + 1] = (L[i] * value[i]) % MOD;
revrep(i, n) R[i] = (R[i + 1] * value[i]) % MOD;
vl ret(n);
/*
ret[i] = v[c0] * v[c1] * ... * v[ci-1] * v[ci+1] * ... * v[cn-1]
ci: the i-th child
Let's consider the case where the root of a given tree is 0. (0-indexed)
Here, we try to obtain the objective values for the edges from children to
parent. When we change the root of the tree into another, the parent node
here also changes the family structure. Since each node has ONLY one parent
and zero or one or multiple children, the value for edge on child i to
source is obtained using the value from source to child j. Here j is all the
integers which is not i.
In this case, child i is grandparent of child j for all j and
the computation for the value from child i to source is O(N) when we use
cumulative sum or product.
*/
rep(i, n) ret[i] = (L[i] * R[i + 1]) % MOD;
return ret;
}
static type merge(const vector<type> &value) {
ll ret;
for (auto &&v : value) {
}
return ret;
}
};
ll gcd(ll m, ll n) {
ll a = max(m, n);
ll b = min(m, n);
while (b != 1 && b != 0) {
a %= b;
swap(a, b);
}
return b == 1 ? 1 : a;
}
ll lcm(ll m, ll n) { return m / gcd(m, n) * n; }
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
ll power_mod(ll a, ll power, ll mod) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = (value * a) % mod;
a = (a * a) % mod;
power = power >> 1;
}
return value % mod;
}
ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); }
ll power_normal(ll a, ll power) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = value * a;
a = a * a;
power = power >> 1;
}
return value;
}
ll comb_memo[55][55];
ll pascal_triangle(ll n) {
comb_memo[0][0] = 1;
For(i, 1, n + 1) rep(j, i + 1) {
comb_memo[i][j] += comb_memo[i - 1][j];
if (j > 0)
comb_memo[i][j] += comb_memo[i - 1][j - 1];
}
}
ll combination(ll n, ll r, ll mod) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll numerator = 1;
ll denomenator = 1;
for (ll i = 0; i < r; i++) {
ll num = (n - i) % mod, den = (i + 1) % mod;
(numerator *= num) %= mod;
(denomenator *= modinv(den, mod)) %= mod;
}
return (numerator * denomenator) % mod;
}
ll combination_no_mod(ll n, ll r) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll numerator = 1;
ll denomenator = 1;
for (ll i = 0; i < r; i++) {
numerator *= n - i;
denomenator *= i + 1;
ll g = gcd(numerator, denomenator);
numerator /= g;
denomenator /= g;
}
return numerator;
}
ld log_combination(ll n, ll r) {
if (n == r && n == 0)
return 0;
else if (n <= 0 || r < 0 || r > n)
return -INF;
ld val = 0;
for (ll i = 0; i < r; i++) {
val += log(n - i);
val -= log(i + 1);
}
return val;
}
ll bin_search(ll key, ll A[], ll left, ll right) {
// return the index idx where A[idx] = key.
// A[left] is start and A[right] is end..
// In other words, A[right], not A[right - 1], must be defined.
while (right >= left) {
ll mid = left + (right - left) / 2;
if (A[mid] == key)
return mid;
else if (A[mid] > key)
right = mid - 1;
else if (A[mid] < key)
left = mid + 1;
}
return -1;
}
/*
ll bin_search_temp(ll left, ll right, callable judge){
while(right > left){
// when seeking lower bound
ll mid = (right + left) / 2;
if (judge(mid)) right = mid;
else left = mid + 1;
// when seeking upper bound
ll mid = (right + left + 1) / 2;
if (judge(mid)) left = mid;
else right = mid - 1;
}
return right;
}
trinary_search
// Care the value EPS!!! Compare to the condition
ld left = 0; ld right = p;
while(abs(right - left) > EPS){
ld left2 = (2 * left + right) / 3.0;
ld right2 = (left + 2 * right) / 3.0;
ld f1 = tak_func(left2);
ld f2 = tak_func(right2);
if (f1 <= f2) right = right2;
else if (f2 <= f1) left = left2;
}
*/
str bin_expression(ll n, ll dig) {
str s = "";
rep(i, dig) {
s += ltos(n % 2);
n /= 2;
}
reverse(all(s));
return s;
}
ll lower_bound_bin_search_temp(ll key, ll A[], ll left, ll right) {
// Pay attention to the return value -1.
// For example, sometimes you may want the value right instead of -1.
if (A[left] >= key)
return left;
ll mid = left + (right - left) / 2;
while (right >= left) {
mid = left + (right - left) / 2;
if (mid == left) {
if (A[left] < key && key <= A[left + 1])
return left + 1;
else
return -1;
}
if (A[mid - 1] < key && key <= A[mid])
return mid;
else if (A[mid - 1] >= key)
right = mid - 1;
else if (A[mid] < key)
left = mid;
}
return -1; // all the elements < key
}
ll inf_bin_search_temp(ll key, ll A[], ll left, ll right) {
// Pay attention to the return value -1.
// For example, sometimes you may want the value right instead of -1.
if (A[left] > key)
return left;
ll mid = left + (right - left) / 2;
while (right >= left) {
mid = left + (right - left) / 2;
if (mid == left) {
if (A[left] <= key && key < A[left + 1])
return left + 1;
else
return -1;
}
if (A[mid - 1] <= key && key < A[mid])
return mid;
else if (A[mid - 1] > key)
right = mid - 1;
else if (A[mid] <= key)
left = mid;
}
return -1; // all the elements <= key
}
ll upper_bound_bin_search_temp(ll key, ll A[], ll left, ll right) {
// Pay attention to the return value -1.
// For example, sometimes you may want the value left instead of -1.
if (A[right] <= key)
return right;
ll mid = left + (right - left) / 2;
while (right >= left) {
mid = left + (right - left) / 2;
if (mid == right) {
if (A[right - 1] <= key && key < A[right])
return right - 1;
else
return -1;
}
if (A[mid] <= key && key < A[mid + 1])
return mid;
else if (A[mid] > key)
right = mid;
else if (A[mid + 1] <= key)
left = mid + 1;
}
return -1; // all the elements > key
}
ll sup_bin_search_temp(ll key, ll A[], ll left, ll right) {
// Pay attention to the return value -1.
// For example, sometimes you may want the value left instead of -1.
if (A[right] < key)
return right;
ll mid = left + (right - left) / 2;
while (right >= left) {
mid = left + (right - left) / 2;
if (mid == right) {
if (A[right - 1] < key && key <= A[right])
return right - 1;
else
return -1;
}
if (A[mid] < key && key <= A[mid + 1])
return mid;
else if (A[mid] >= key)
right = mid;
else if (A[mid + 1] < key)
left = mid + 1;
}
return -1; // all the elements >= key
}
ll bin_search_vector(ll key, vl v, ll left, ll right) {
// return the index idx where v[idx] = key.
// v[left] is start and v[right] is end..
// In other words, v[right], not v[right - 1], must be defined.
while (right >= left) {
ll mid = left + (right - left) / 2;
if (v[mid] == key)
return mid;
else if (v[mid] > key)
right = mid - 1;
else if (v[mid] < key)
left = mid + 1;
}
return -1;
}
ll lower_bound_bin_search_vector(ll key, vl v) {
// the return value N satisfies
// v[N - 1] < key <= v[N]
// N == -1 if all the elements < key
return lower_bound(all(v), key) - v.begin();
}
ll inf_bin_search_vector(ll key, vl v) {
// the return value N satisfies
// v[N - 1] <= key < v[N] <= key + 1
// N == -1 if all the elements <= key
return lower_bound(all(v), key + 1) - v.begin();
}
ll upper_bound_bin_search_vector(ll key, vl v) {
// the return value N satisfies
// v[N] <= key < v[N + 1]
// N == -1 if all the elements > key
return upper_bound(all(v), key) - v.begin(); // (- 1)
}
ll sup_bin_search_vector(ll key, vl v) {
// the return value N satisfies
// v[N] <= key - 1 < key <= v[N + 1]
// N == -1 if all the elements >= key
return upper_bound(all(v), key - 1) - v.begin() - 1;
}
ll fact(ll n) {
if (n == 0)
return 1;
return n * fact(n - 1);
}
ll fact_mod(ll n, ll mod) {
if (n == 0)
return 1;
return n * fact_mod(n - 1, mod);
}
/*
ll facts[6000], facts_inv[6000], inv[6000];
void fact_memo(ll n, ll mod){
facts[0] = facts[1] = 1;
facts_inv[0] = facts_inv[1] = 1;
inv[1] = 1;
For(i, 2, n + 1){
facts[i] = (i * facts[i - 1]) % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
facts_inv[i] = facts_inv[i - 1] * inv[i] % mod;
}
}
ll nCk(ll n, ll r){
if (n == r && n == 0) return 1;
else if (n <= 0 || r < 0 || r > n) return 0;
ll val = (facts[n] * facts_inv[n - r]) % MOD;
val *= facts_inv[r];
return val % MOD;
}
*/
bool is_prime(ll n) {
if (n <= 1)
return false;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
ll bool_sum(ll a1, ll a2) {
if (a1 == 1 || a2 == 1)
return 1;
return 0;
}
mll prime_factorization(ll n) {
ll i = 2;
mll table;
while (i * i <= n) {
while (n % i == 0) {
table[i]++;
n /= i;
}
i++;
}
if (n > 1)
table[n] = 1;
return table;
}
vl divisor_table(ll n) {
vl table;
ll i = 1;
while (i * i <= n) {
if (n % i == 0) {
table.pb(i);
if (i * i != n)
table.pb(n / i);
}
i++;
}
sort(all(table));
return table;
}
ll char_to_idx(char c) {
ll idx = 0;
Forchar(cc, 'a', 'z') {
if (c == cc)
return idx;
else
idx++;
}
}
ll next_combination(ll sub) {
/*
### Attention ###
if k is 0 or 1 and n is 0, it does not work well.
ll n, k; ll bit = (1 << k) - 1;
for (; bit < (1 << n); bit = next_combination(bit)){
bool ith = bit & (1 << i);
procedures...
}
sub & -sub: the binary which shares the last digit whose value is 1 in sub
sub + x : carry up the last digit
~y : the binary whose digits are 1 if y's digit is 0.
(sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub &
~y).
*/
ll x = sub & -sub, y = sub + x;
return (((sub & ~y) / x) >> 1) | y;
}
vl z_algorithm(str s) {
ll n = s.length();
vl res(n);
res[0] = n;
ll i1 = 1, i2 = 0;
while (i1 < n) {
while (i1 + i2 < n && s[i2] == s[i1 + i2])
++i2;
res[i1] = i2;
if (i2 == 0) {
++i1;
continue;
}
ll i3 = 1;
while (i1 + i3 < n && i3 + res[i3] < i2) {
res[i1 + i3] = res[i3];
++i3;
}
i1 += i3, i2 -= i3;
}
return res;
}
ll big_number_mod(str s, ll mod) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth) % mod;
val %= mod;
tenth *= 10;
tenth %= mod;
idx++;
}
return val;
}
ll big_number_compare(str s1, str s2) {
if (s1.length() > s2.length())
return 1;
else if (s1.length() < s2.length())
return -1;
else if (s1 == s2)
return 0;
return 2 * (s1 > s2) - 1;
}
ll string_to_ll(str s) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth);
tenth *= 10;
idx++;
}
return val;
}
str reflected_string(str s) {
str t, u;
ll n = s.length();
t = s;
reverse(all(t));
u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1);
return u;
}
ld distance_between_point_line(point l_begin, point l_end, point p) {
ll xl1 = l_begin.x, yl1 = l_begin.y;
ll xl2 = l_end.x, yl2 = l_end.y;
ll xp = p.x, yp = p.y;
ll a = yl2 - yl1;
ll b = -xl2 + xl1;
ll c = -a * xl2 - b * yl2;
return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b));
}
bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) {
ll x1 = l1_begin.x, y1 = l1_begin.y;
ll x2 = l1_end.x, y2 = l1_end.y;
ll x3 = l2_begin.x, y3 = l2_begin.y;
ll x4 = l2_end.x, y4 = l2_end.y;
ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3);
ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4);
ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1);
ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2);
return val1 * val2 < 0 && val3 * val4 < 0;
}
ld space_of_triangle(point p1, point p2, point p3) {
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
ll v1 = x2 - x1;
ll u1 = y2 - y1;
ll v2 = x3 - x1;
ll u2 = y3 - y1;
ld s = ld(v1 * u2 - u1 * v2) / ld(2);
return abs(s);
}
pair<point, ll> center_2det_of_3points(point p1, point p2, point p3) {
// the center of circle on the given three points
// return the determinant value and the product of center points and 2 *
// determinant value
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
// center of 2 points
ll c2x_2 = x2 + x1, c2y_2 = y2 + y1;
ll c3x_2 = x3 + x1, c3y_2 = y3 + y1;
// vertical vector of 2 lines
ll b2y = x2 - x1, b2x = -y2 + y1;
ll b3y = x3 - x1, b3x = -y3 + y1;
ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x;
ll det = -b3y * b2x + b3x * b2y;
if (det == 0)
return {{INF, INF}, det};
ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2;
return {{cx_2det, cy_2det}, det};
}
ll inversion_number(vl a, ll a_max) {
/*
Paramters
---------
a: vector<ll>
All the elements must be non-negative.
Prefably the elements are compressed to reduce the computational cost.
a_max: ll
The maximum value of the vector a or the value bigger than the value
stated previously.
*/
BIT bit(a_max + 1);
ll val = 0;
rep(i, a.size()) {
// i is the number of elements that have lower index than a[i].
// call the number of elements that have lower value than a[i]
// by subtracting these two, the residual number is the number of elements
// that have larger value
val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1
bit.update(a[i], 1);
}
return val;
}
template <typename T> vector<T> compress(vector<T> v) {
// sort and remove all the duplicated values
sort(all(v));
v.erase(unique(all(v)), v.end());
return v;
}
template <typename T> map<T, ll> dict(const vector<T> &v) {
map<T, ll> d;
rep(i, v.size()) d[v[i]] = i;
return d;
}
void GaussJordanBitVector(vl &bs) {
ll n = bs.size();
ll rank = 0;
ll j = 0;
revrep(i, N_DIGITS) {
for (j = rank; j < n; j++)
if (bs[j] & (1LL << i))
break;
if (j == n)
continue;
if (j > rank)
bs[rank] ^= bs[j];
for (j = rank + 1; j < n; j++)
bs[j] = min(bs[j], bs[j] ^ bs[rank]);
rank++;
}
}
ll kruskal(vector<undirected_edge> &es, ll n_vertex) {
sort(all(es));
UnionFind uf(n_vertex);
ll min_cost = 0;
rep(i, es.size()) {
undirected_edge &e = es[i];
if (!uf.is_same(e.from, e.to)) {
min_cost += e.cost;
uf.unite(e.from, e.to);
}
}
return min_cost;
}
ll LongestIncreasedSequence(vl &v, ll n) {
vl dp(n, INF);
rep(i, n) * lower_bound(all(dp), v[i]) = v[i];
return find(all(dp), INF) - dp.begin();
}
/*
const ll N_VERTEX = 310;
ll a, b, t;
ll dist[N_VERTEX][N_VERTEX];
void warshall_floyd(ll n){
// rep(i, n) rep(j, n) dist[i][j] = INF * (i != j);
rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] +
dist[k][j]);
}
int main(void){
in2(n, m);
rep(i, n) rep(j, n) dist[i][j] = INF * (i != j);
rep(i, m){
in3(a, b, t);
a--; b--;
dist[a][b] = t;
dist[b][a] = t;
}
warshall_floyd(n);
}
ll dist[1005], vertex_pre[1005];
void dijkstra(ll start, ll n) {
priority_queue<pl, vector<pl>, greater<pl>> edge_costs;
fill(dist, dist + n, INF); // fill(vertex_pre, vertex_pre + n, -1);
dist[start] = 0;
edge_costs.push(pl(0, start));
while (!edge_costs.empty()) {
pl edge_cost = edge_costs.top();
edge_costs.pop();
ll idx = edge_cost.second;
ll cost = edge_cost.first;
if (dist[idx] < cost) continue;
rep(i, graph[idx].size()){
edge e = graph[idx][i];
if (dist[e.to] > dist[idx] + e.cost){
dist[e.to] = dist[idx] + e.cost;
// vertex_pre[e.to] = idx;
edge_costs.push(pl(dist[e.to], e.to));
}
}
}
}
vl get_predecessor(ll g){
vl path;
for (; g != -1; g = vertex_pre[g]) path.pb(g);
reverse(all(path));
return path;
}
int main(void){
in2(n, m);
rep(i, m){
in3(a, b, t);
a--; b--;
G[a].pb({b, t});
G[b].pb({a, t});
}
dijkstra(0, n);
}
# ABC061D
bool find_negative_cycle(ll goal){
rep(i, n) rep(v, n) rep(k, graph[v].size()){
edge e = graph[v][k];
if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost){
dist[e.to] = -INF;
if (goal == -1) return true;
else if (goal == e.to) return true;
}
}
return false;
}
bool bellman_ford(ll start, ll n, ll goal){
// if there is a closed circuit, it returns false. (when goal == -1)
// if the distance to goal cannot be obtained, it returns false (when goal
!= -1) fill(dist, dist + n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k,
graph[v].size()){ edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] >
dist[v] + e.cost) dist[e.to] = dist[v] + e.cost;
}
if (find_negative_cycle(goal)) return false;
return true;
}
*/
/*
# 1. The usage of map pair
map<pl, ll> cnt;
cnt[{i, j}] = 0;
items(kv, cnt){
prl2(kv.first, kv.second);
}
# 2. The usage of next_permutation and combination (factorial search)
ll a[8];
rep(i, 8) a[i] = i;
sort(a, a + 8);
do{
}while(next_permutation(a, a+n));
// here, combination
ll n, r;
ll i1, i2, ..., ir;
For(i1, n - r, n) For(i2, n - r - 1, i1) ... For(ir, n - 2 * r + 1, irr){
process;}
# 3. bit search
ll n;
in1(n);
const ll base = 3;
ll upper = power_normal(base, n);
rep(i, upper){
ll tmp = i;
rep(j, n){
rep(k, base) if (tmp % base == k) prl(k);
tmp /= base;
}
}
# 4. imos method
// used when we would like to count the number which
// shows how many times the numbers between l and r belongs to smt.
// This method is composed of three process.
ll n, m, s[MAX_M], l, r;
in2(n, m);
rep(i, m) s[i] = 0;
// 1st step
rep(i, n){
in3(l, r, c);
l--; r--; // if l starts from 1.
s[l] += c; s[r + 1] -= c;
}
// 2nd step
rep(i, m - 1) s[i + 1] += s[i];
// 3rd step: judgement...
#5. shakutori method (syakutori, two pointers technique)
// 1. strech right side while the condition is met.
// 2. renew the answer
// 3. increments left side
// 4. Back to 1. (l <= r must be satisfied all the time.)
ll l = 0; ll r = 0;
while (l < n){
r = max(r, l);
if (l == r) r++;
while(r < n && cond) r++;
answer += r - l; l++;
}
prl(answer);
#6. priority queue
pq q;
ll answer = 0;
ll v;
rep(i, n) q.push(a[i]);
rep(i, m){
v = q.top(); q.pop(); // get the top value and dump the value from queue
v /= 2; q.push(v); // add the new value
}
while(!q.empty()){
answer += q.top();
q.pop();
}
#7. The shortest path (distance) between the k-th edge and another edge
(Tree) Graph tree[MAX_N]; ll depth[MAX_N];
void dfs(ll source, ll parent, ll all_cost){
depth[source] = all_cost;
items(e, tree[source]){
if (e.to == parent) continue;
dfs(e.to, source, all_cost + e.cost);
}
}
ll n, k, a, b, c;
in2(n, k);
rep(i, n - 1){
in3(a, b, c);
a--; b--;
tree[a].pb({b, c});
tree[b].pb({a, c});
}
k--;
dfs(k, -1, 0);
#10. Visiting Subtree using recurrent function (ABC138D)
Graph tree[MAX_N];
ll c[MAX_N];
bool visited[MAX_N];
void dfs(ll source, ll parent, ll val){
visited[source] = true;
c[source] += val;
rep(i, tree[source].size()){
rep(i, m){ll res = n % match[i].e1;}
ll vertex = tree[source][i].to;
if (vertex == parent) continue;
dfs(vertex, source, c[source]);
}
}
#11. bfs ABC146D, ABC007C
1. first create a tree.
2. start searching from a node.
3. do some processes and push nodes connected with a given target node in
BFS.
4. repeat a series of procedure until queue is empty.
queue<pl> q;
void bfs(ll source, ll parents){
ll n_edge = graph[source].size();
if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1);
if (visited[source]) return;
visited[source] = true;
rep(idx, n_edge){
ll target = graph[source][idx].to;
if (target == parents) continue;
q.push(mp(target, source));
}
}
q.push(mp(sg.e1, -1));
while(!q.empty()){
pl source = q.front(); q.pop();
bfs(source.e1, source.e2);
}
#12. grid to distance matrix (dx, dy)
ll w, h;
ll pos_to_idx(ll x, ll y){
return y * w + x;
}
pl idx_to_pos(ll idx){
return mp(idx % w, idx / w);
}
rep(y, h){
in1(s);
rep(x, w){
if (s[x] == '#') wall[x][y] = true;
else wall[x][y] = false;
}
}
rep(i1, h * w)rep(i2, h * w) dist[i1][i2] = INF * (i1 != i2);
rep(x, w)rep(y, h){
ll idx1 = pos_to_idx(x, y); ll idx2;
if (wall[x][y]) continue;
if (x != 0 && !wall[x - 1][y]){
idx2 = pos_to_idx(x - 1, y);
// if warshall floyd
dist[idx1][idx2] = 1;
// if dijkstra
// graph[idx1].pb({idx2, 1});
}
if (x != w - 1 && !wall[x + 1][y]){
idx2 = pos_to_idx(x + 1, y);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
if (y != 0 && !wall[x][y - 1]){
idx2 = pos_to_idx(x, y - 1);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
if (y != h - 1 && !wall[x][y + 1]){
idx2 = pos_to_idx(x, y + 1);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
}
# Cumulative sum (2 dimension)
ll func(ll x, ll y, ll dx, ll dy){
if (x + dx > w || y + dy > h) return - INF;
ll val = cum[x + dx][y + dy];
val += cum[x][y];
val -= cum[x][y + dy];
val -= cum[x + dx][y];
return val;
}
rep(x, w + 1) cum[x][0] = 0;
rep(y, h + 1) cum[0][y] = 0;
rep(y, h + 1) rep(x, w)
cum[x + 1][y + 1] = cum[x][y + 1] + vs[x][y];
rep(x, w + 1) rep(y, h)
cum[x][y + 1] += cum[x][y];
# DEBUG_MATRIX
rep(i, n){
rep(j, n) prs(a[i][j]);
prl("");
}
*/
/*
# the operators regarding bit
& (AND), | (OR), ^ (XOR)
- (REVERSE), >> (SMALLER SHIFT)
<< (BIGGER SHIFT)
x1: 0000 0001 0010 0101 0110 0111 0111
x2: xxxx 0001 0011 0100 0101 1000 0110
x1 & x2: 0000 0001 0010 0100 0100 0000 0110
x: 1001 1010 1100 1011 1101 1111
x & - x: 0001 0010 0100 0001 0001 0001
sum: 1010 1100 10000 1100 1100 10000
x << y is x * 2 ** y
x >> y is rep(i, y) x = x // 2
####### Attention #######
1 << i and 1LL << i is different. If programs show WA, try switch to this one.
Let S be a bit sequence and i be a non-negative integer
S & (1 << i) -> if true, i in S
S | (1 << i) -> S union {i}
S & -(1 << i) -> S - {i}
__builtin_popcount(S) -> the number of elements in S
S = 0 -> S is an empty set
__builtin_popcountl(i) -> the number of 1 in binary
S = (1 << n) - 1 -> S includes all the elements up to the n-th
#Conditional Operator
condition ? true : false;
#iterator
type declaration: auto
value reference: *itr
increment: itr++
decrement: itr--
substitution of value: *itr = smt
# inclusion-exclusion principle
|U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n
- 1) |^[i = 1 to n]Ai|
*/
const ll MAX_N = 200005;
bool okay = false;
ll answer = 0;
ll n;
multiset<ll> s;
void solve() {
rep(i, n) {
ll a;
in1(a);
s.insert(a);
}
auto it = s.begin();
revrep(i, 31) {
ll target = 1LL << i;
while (!s.empty() && *it <= target / 2 && it != s.end()) {
auto lb = s.lower_bound(target - *it);
if (it == lb) {
if (*next(it) != *lb)
break;
else
it++;
}
if (*lb + *it == target) {
answer++;
s.erase(lb);
it++;
s.erase(prev(it));
} else
it++;
}
it = s.begin();
}
prl(answer);
}
int main(void) {
in1(n);
solve();
// assert(n <= 10000);
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
using edge = struct {
ll to;
ll cost;
};
struct point {
ll x;
ll y;
bool operator<(const point &p) const {
if (x == p.x)
return y < p.y;
return x < p.x;
}
};
struct undirected_edge {
ll from;
ll to;
ll cost;
bool operator<(const undirected_edge &ue) const { return cost < ue.cost; }
};
typedef string str;
typedef std::pair<ll, ll> pl;
typedef std::pair<ll, pl> pl3;
typedef std::map<string, ll> msl;
typedef std::map<char, ll> mcl;
typedef std::map<ll, ll> mll;
typedef std::vector<ll> vl;
typedef std::vector<pl> vpl;
typedef std::vector<point> points;
typedef std::vector<pl3> vpl3;
typedef std::priority_queue<ll> pq;
typedef std::priority_queue<ll, vl, greater<ll>>
pql; // priority queue taking from the lower value.
typedef std::vector<edge> Graph;
const ll N_DIGITS = 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = MOD * MOD;
const long double EPS = 1e-9;
const long double PI = 3.14159265358979323846;
vpl dirs = {
{1, 0}, {0, 1}, {-1, 0}, {0, -1}, // four directions
{1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal
{0, 0} // self
};
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--)
#define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define isUpper(c) ('a' - c > 0)
#define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9)
#define toLower(c) char((c) + 0x20)
#define toUpper(c) char((c)-0x20)
#define pb push_back
#define mp make_pair
#define pr(a) cout << (a)
#define prl(a) cout << (a) << endl
#define prl2(a, b) cout << (a) << " " << (b) << endl
#define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define prl4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define prs(a) cout << (a) << " "
#define yn(condition) \
if ((condition)) \
prl("Yes"); \
else \
prl("No");
#define YN(condition) \
if ((condition)) \
prl("YES"); \
else \
prl("NO");
#define in1(a) cin >> (a)
#define in2(a, b) cin >> (a) >> (b)
#define in3(a, b, c) cin >> (a) >> (b) >> (c)
#define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d)
#define e1 first
#define e2 second
#define ctol(c) ll((c)) - ll('0')
#define ltos(n) to_string((n))
#define items(kv, v) for (auto &(kv) : (v))
#define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)]))
#define rsort(a, n) sort(a, a + n, greater<>())
#define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++)
#define cntchar(s, c) count(all((s)), c)
#define substring(s, start, end) s.substr((start), (end) - (start) + 1)
#define prl_nd(num, digits) \
cout << fixed << setprecision(digits) << (num) << endl;
#define XOR(a, b) (a) ^ (b)
#define prl_time(s) \
prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]");
#define char_to_str(c) string(1, (c))
#define DEBUG(x) cerr << #x << ": " << (x) << endl
#define DEBUG_VEC(v) \
cerr << #v << ": "; \
rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \
cerr << endl
struct MaxFlow {
struct F_edge {
ll to, rev, capacity;
F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {}
};
typedef vector<F_edge> F_edges;
vector<F_edges> graph;
ll n_vertex;
// level is the shortest path to get a given node from the source node.
vl level, iter;
MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); }
void add_edge(ll from, ll to, ll capacity) {
graph[from].pb({to, ll(graph[to].size()), capacity});
graph[to].pb({from, ll(graph[from].size()) - 1, 0});
}
void bfs(ll source) {
level = vl(n_vertex, -1);
level[source] = 0;
queue<ll> q;
q.push(source);
while (!q.empty()) {
ll vertex = q.front();
q.pop();
rep(i, graph[vertex].size()) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
// if the flow can be into the target node, implement below.
if (cap_target > 0 && level[target] < 0) {
level[target] = level[vertex] + 1;
q.push(target);
}
}
}
}
ll dfs(ll vertex, ll sink, ll flow) {
if (vertex == sink)
return flow;
for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
ll rev_target = graph[vertex][i].rev;
// if capasitiy is not full yet and target is farther,
// then assign current flow
if (cap_target > 0 && level[vertex] < level[target]) {
ll d = dfs(target, sink, min(cap_target, flow));
if (d > 0) { // if the flow successfully reaches the sink, reduce the
// flow from the capacity
graph[vertex][i].capacity -= d;
graph[target][rev_target].capacity += d;
return d;
}
}
}
return 0;
}
ll dinic(ll source, ll sink) {
ll flow = 0;
while (true) {
bfs(source);
// if there is no path leading to the sink, the maximum flow is 0.
if (level[sink] < 0)
return flow;
iter = vl(n_vertex, 0);
ll f;
while ((f = dfs(source, sink, INF)) > 0)
flow += f;
}
}
};
class UnionFind {
vl parents, set_size;
public:
UnionFind() {}
UnionFind(ll n) {
parents = set_size = vl(n);
rep(i, n) {
parents[i] = i;
set_size[i] = 1LL;
}
}
ll root_find(ll x) {
if (parents[x] == x)
return x;
return parents[x] = root_find(parents[x]);
}
void unite(ll x, ll y) {
// priority for x is larger than that of y
x = root_find(x);
y = root_find(y);
if (x == y)
return;
if (set_size[x] < set_size[y]) {
parents[y] = x;
set_size[x] += set_size[y];
} else {
parents[x] = y;
set_size[y] += set_size[x];
}
}
bool is_same(ll x, ll y) { // connected or not
return root_find(x) == root_find(y);
}
ll size(ll x) { return set_size[root_find(x)]; }
};
struct Doubling { // ABC167D
ll n;
ll sz;
vector<vl> next;
/*
next[k + 1][i] := next[k][next[k][i]]
next[0][i] := edge[i]
e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1
a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times)
Let the function repeatedly input a[i] m times be f[m](a[i])
- get(i, x) returns f[x](a[i])
- lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j.
if not possible returns n.
*/
// edge[i]: the step size for one iteration
Doubling(vl &edge) : n(edge.size()), sz(62) {
next.resize(sz, vl(n, -1));
rep(i, n) next[0][i] = edge[i];
rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]];
}
ll get(ll i, ll x) {
ll ret = i;
rep(bit, sz) {
if (!(x >> bit & 1))
continue;
ret = next[bit][ret];
}
return ret;
}
ll lower_bound(ll i, ll j) {
ll cur = i, acc = 0;
revrep(wid, sz) {
if (next[wid][cur] < j) {
acc += 1LL << wid;
cur = next[wid][cur];
}
}
return min(n, acc + 1);
}
};
/*
class LCA{
public:
ll N, logN;
vl depth, len;
Graph tree[200005]; // global declaration later.
vector<vl> parents;
LCA(ll n){
N = n;
logN = 0;
while (N > (1LL << logN)) logN++;
depth = vl(N); len = vl(N);
parents = vector<vl>(logN, vl(N));
init(0, -1, 0, 0);
build();
}
void init(ll source, ll parent, ll d, ll l){
depth[source] = d;
parents[0][source] = parent;
len[source] = l;
rep(i, tree[source].size()){
ll target = tree[source][i].to;
ll cost = tree[source][i].cost;
if (target == parent) continue;
init(target, source, d + 1, cost + l);
}
}
void build(){
rep(k, logN - 1) rep(n, N){
// if there is no parent, -1.
// otherwise, the parent of the parent is the parent.
if (parents[k][n] < 0) parents[k + 1][n] = -1;
else parents[k + 1][n] = parents[k][parents[k][n]];
}
}
ll query(ll u, ll v){
if (depth[u] > depth[v]) swap(u, v);
rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v];
if (u == v) return u;
revrep(k, logN){
if (parents[k][u] != parents[k][v]){
u = parents[k][u]; v = parents[k][v];
}
}
return parents[0][u];
}
ll distance(ll u, ll v){
ll w = query(u, v);
return len[u] + len[v] - 2 * len[w];
}
};
*/
struct BIT {
ll n;
vl tree_dat;
BIT(ll n, ll ini = 0) : tree_dat(n + 1, ini), n(n){};
// x: 1001 1010 1100 1011 1101 1111
// x & - x: 0001 0010 0100 0001 0001 0001
// ->: 1010 1100 10000 1100 1100 10000
ll update_func(ll val, ll dat) {
// if maximum -> max(val, dat)
// return max(val, dat);
// if cumulative sum
// return val + dat;
return min(val, dat);
}
ll query(ll i) {
/*
e.g.) i = 10101
itr1. 10101 -> 10100
itr2. 10100 -> 10000
itr3. 10000 -> 00000 (break)
*/
ll ret = 0;
for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) {
ret = update_func(ret, tree_dat[j]);
}
return ret;
}
ll lower_bound(ll key) {
if (key <= 0)
return 0;
ll left = 0, right = 1;
while (right <= n)
right *= 2;
for (ll i = right; i > 0; i /= 2) {
if (left + i <= n && tree_dat[left + i - 1] < key) {
key -= tree_dat[left + i - 1];
left += i;
}
}
return left;
}
void update(ll i, ll val) {
/*
e.g.) i = 10101, n = 11111
itr1. i: 10101, i+1: 10110 -> 10111
itr2. i: 10111, i+1: 11000 -> 11111 (break)
*/
if (i < 0)
return;
for (ll j = i; j < n; j |= j + 1) {
tree_dat[j] = update_func(val, tree_dat[j]);
}
}
};
template <class Target> class RerootingTreeDP {
public:
using T = typename Target::type;
struct DP_edge {
ll to, rev; // rev is the index to trace the source node.
T value; // objective value
};
private:
ll n;
void dfs_fwd(ll source, ll parent) {
ll par_idx = -1;
vector<T> values;
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent) {
par_idx = i;
continue;
}
dfs_fwd(e.to, source);
values.pb(e.value);
}
// If the parent != -1, update the value on edge from parent to source
if (par_idx != -1) {
ll src_idx = tree[source][par_idx].rev;
// update values on the edge from parent to source
tree[parent][src_idx].value = Target::merge(values);
}
}
void dfs_bwd(ll source, ll parent) {
vector<T> values;
for (auto &&e : tree[source])
values.pb(e.value);
values = Target::evaluate(values);
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent)
continue;
// tree[e.to][e.rev]: e.to -> source
tree[e.to][e.rev].value = values[i];
dfs_bwd(e.to, source);
}
}
public:
UnionFind uf;
vector<vector<DP_edge>> tree;
RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {}
void add_edge(ll u, ll v, T val) {
assert(!uf.is_same(u, v));
tree[u].pb({v, ll(tree[v].size()), val});
tree[v].pb({u, ll(tree[u].size()) - 1, val});
uf.unite(u, v);
}
void dp() {
vector<bool> visited(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_fwd(i, -1);
visited[uf.root_find(i)] = true;
}
visited.assign(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_bwd(i, -1);
visited[uf.root_find(i)] = true;
}
}
ll size() const { return tree.size(); }
};
// ABC160F is one example
// Modify the functions evaluate and merge based on given problems
struct Merger {
using type = ll;
static vector<type> evaluate(const vector<type> &value) {
const ll n = value.size();
vl L(n + 1, 1), R(n + 1, 1);
rep(i, n) L[i + 1] = (L[i] * value[i]) % MOD;
revrep(i, n) R[i] = (R[i + 1] * value[i]) % MOD;
vl ret(n);
/*
ret[i] = v[c0] * v[c1] * ... * v[ci-1] * v[ci+1] * ... * v[cn-1]
ci: the i-th child
Let's consider the case where the root of a given tree is 0. (0-indexed)
Here, we try to obtain the objective values for the edges from children to
parent. When we change the root of the tree into another, the parent node
here also changes the family structure. Since each node has ONLY one parent
and zero or one or multiple children, the value for edge on child i to
source is obtained using the value from source to child j. Here j is all the
integers which is not i.
In this case, child i is grandparent of child j for all j and
the computation for the value from child i to source is O(N) when we use
cumulative sum or product.
*/
rep(i, n) ret[i] = (L[i] * R[i + 1]) % MOD;
return ret;
}
static type merge(const vector<type> &value) {
ll ret;
for (auto &&v : value) {
}
return ret;
}
};
ll gcd(ll m, ll n) {
ll a = max(m, n);
ll b = min(m, n);
while (b != 1 && b != 0) {
a %= b;
swap(a, b);
}
return b == 1 ? 1 : a;
}
ll lcm(ll m, ll n) { return m / gcd(m, n) * n; }
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
ll power_mod(ll a, ll power, ll mod) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = (value * a) % mod;
a = (a * a) % mod;
power = power >> 1;
}
return value % mod;
}
ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); }
ll power_normal(ll a, ll power) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = value * a;
a = a * a;
power = power >> 1;
}
return value;
}
ll comb_memo[55][55];
ll pascal_triangle(ll n) {
comb_memo[0][0] = 1;
For(i, 1, n + 1) rep(j, i + 1) {
comb_memo[i][j] += comb_memo[i - 1][j];
if (j > 0)
comb_memo[i][j] += comb_memo[i - 1][j - 1];
}
}
ll combination(ll n, ll r, ll mod) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll numerator = 1;
ll denomenator = 1;
for (ll i = 0; i < r; i++) {
ll num = (n - i) % mod, den = (i + 1) % mod;
(numerator *= num) %= mod;
(denomenator *= modinv(den, mod)) %= mod;
}
return (numerator * denomenator) % mod;
}
ll combination_no_mod(ll n, ll r) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll numerator = 1;
ll denomenator = 1;
for (ll i = 0; i < r; i++) {
numerator *= n - i;
denomenator *= i + 1;
ll g = gcd(numerator, denomenator);
numerator /= g;
denomenator /= g;
}
return numerator;
}
ld log_combination(ll n, ll r) {
if (n == r && n == 0)
return 0;
else if (n <= 0 || r < 0 || r > n)
return -INF;
ld val = 0;
for (ll i = 0; i < r; i++) {
val += log(n - i);
val -= log(i + 1);
}
return val;
}
ll bin_search(ll key, ll A[], ll left, ll right) {
// return the index idx where A[idx] = key.
// A[left] is start and A[right] is end..
// In other words, A[right], not A[right - 1], must be defined.
while (right >= left) {
ll mid = left + (right - left) / 2;
if (A[mid] == key)
return mid;
else if (A[mid] > key)
right = mid - 1;
else if (A[mid] < key)
left = mid + 1;
}
return -1;
}
/*
ll bin_search_temp(ll left, ll right, callable judge){
while(right > left){
// when seeking lower bound
ll mid = (right + left) / 2;
if (judge(mid)) right = mid;
else left = mid + 1;
// when seeking upper bound
ll mid = (right + left + 1) / 2;
if (judge(mid)) left = mid;
else right = mid - 1;
}
return right;
}
trinary_search
// Care the value EPS!!! Compare to the condition
ld left = 0; ld right = p;
while(abs(right - left) > EPS){
ld left2 = (2 * left + right) / 3.0;
ld right2 = (left + 2 * right) / 3.0;
ld f1 = tak_func(left2);
ld f2 = tak_func(right2);
if (f1 <= f2) right = right2;
else if (f2 <= f1) left = left2;
}
*/
str bin_expression(ll n, ll dig) {
str s = "";
rep(i, dig) {
s += ltos(n % 2);
n /= 2;
}
reverse(all(s));
return s;
}
ll lower_bound_bin_search_temp(ll key, ll A[], ll left, ll right) {
// Pay attention to the return value -1.
// For example, sometimes you may want the value right instead of -1.
if (A[left] >= key)
return left;
ll mid = left + (right - left) / 2;
while (right >= left) {
mid = left + (right - left) / 2;
if (mid == left) {
if (A[left] < key && key <= A[left + 1])
return left + 1;
else
return -1;
}
if (A[mid - 1] < key && key <= A[mid])
return mid;
else if (A[mid - 1] >= key)
right = mid - 1;
else if (A[mid] < key)
left = mid;
}
return -1; // all the elements < key
}
ll inf_bin_search_temp(ll key, ll A[], ll left, ll right) {
// Pay attention to the return value -1.
// For example, sometimes you may want the value right instead of -1.
if (A[left] > key)
return left;
ll mid = left + (right - left) / 2;
while (right >= left) {
mid = left + (right - left) / 2;
if (mid == left) {
if (A[left] <= key && key < A[left + 1])
return left + 1;
else
return -1;
}
if (A[mid - 1] <= key && key < A[mid])
return mid;
else if (A[mid - 1] > key)
right = mid - 1;
else if (A[mid] <= key)
left = mid;
}
return -1; // all the elements <= key
}
ll upper_bound_bin_search_temp(ll key, ll A[], ll left, ll right) {
// Pay attention to the return value -1.
// For example, sometimes you may want the value left instead of -1.
if (A[right] <= key)
return right;
ll mid = left + (right - left) / 2;
while (right >= left) {
mid = left + (right - left) / 2;
if (mid == right) {
if (A[right - 1] <= key && key < A[right])
return right - 1;
else
return -1;
}
if (A[mid] <= key && key < A[mid + 1])
return mid;
else if (A[mid] > key)
right = mid;
else if (A[mid + 1] <= key)
left = mid + 1;
}
return -1; // all the elements > key
}
ll sup_bin_search_temp(ll key, ll A[], ll left, ll right) {
// Pay attention to the return value -1.
// For example, sometimes you may want the value left instead of -1.
if (A[right] < key)
return right;
ll mid = left + (right - left) / 2;
while (right >= left) {
mid = left + (right - left) / 2;
if (mid == right) {
if (A[right - 1] < key && key <= A[right])
return right - 1;
else
return -1;
}
if (A[mid] < key && key <= A[mid + 1])
return mid;
else if (A[mid] >= key)
right = mid;
else if (A[mid + 1] < key)
left = mid + 1;
}
return -1; // all the elements >= key
}
ll bin_search_vector(ll key, vl v, ll left, ll right) {
// return the index idx where v[idx] = key.
// v[left] is start and v[right] is end..
// In other words, v[right], not v[right - 1], must be defined.
while (right >= left) {
ll mid = left + (right - left) / 2;
if (v[mid] == key)
return mid;
else if (v[mid] > key)
right = mid - 1;
else if (v[mid] < key)
left = mid + 1;
}
return -1;
}
ll lower_bound_bin_search_vector(ll key, vl v) {
// the return value N satisfies
// v[N - 1] < key <= v[N]
// N == -1 if all the elements < key
return lower_bound(all(v), key) - v.begin();
}
ll inf_bin_search_vector(ll key, vl v) {
// the return value N satisfies
// v[N - 1] <= key < v[N] <= key + 1
// N == -1 if all the elements <= key
return lower_bound(all(v), key + 1) - v.begin();
}
ll upper_bound_bin_search_vector(ll key, vl v) {
// the return value N satisfies
// v[N] <= key < v[N + 1]
// N == -1 if all the elements > key
return upper_bound(all(v), key) - v.begin(); // (- 1)
}
ll sup_bin_search_vector(ll key, vl v) {
// the return value N satisfies
// v[N] <= key - 1 < key <= v[N + 1]
// N == -1 if all the elements >= key
return upper_bound(all(v), key - 1) - v.begin() - 1;
}
ll fact(ll n) {
if (n == 0)
return 1;
return n * fact(n - 1);
}
ll fact_mod(ll n, ll mod) {
if (n == 0)
return 1;
return n * fact_mod(n - 1, mod);
}
/*
ll facts[6000], facts_inv[6000], inv[6000];
void fact_memo(ll n, ll mod){
facts[0] = facts[1] = 1;
facts_inv[0] = facts_inv[1] = 1;
inv[1] = 1;
For(i, 2, n + 1){
facts[i] = (i * facts[i - 1]) % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
facts_inv[i] = facts_inv[i - 1] * inv[i] % mod;
}
}
ll nCk(ll n, ll r){
if (n == r && n == 0) return 1;
else if (n <= 0 || r < 0 || r > n) return 0;
ll val = (facts[n] * facts_inv[n - r]) % MOD;
val *= facts_inv[r];
return val % MOD;
}
*/
bool is_prime(ll n) {
if (n <= 1)
return false;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
ll bool_sum(ll a1, ll a2) {
if (a1 == 1 || a2 == 1)
return 1;
return 0;
}
mll prime_factorization(ll n) {
ll i = 2;
mll table;
while (i * i <= n) {
while (n % i == 0) {
table[i]++;
n /= i;
}
i++;
}
if (n > 1)
table[n] = 1;
return table;
}
vl divisor_table(ll n) {
vl table;
ll i = 1;
while (i * i <= n) {
if (n % i == 0) {
table.pb(i);
if (i * i != n)
table.pb(n / i);
}
i++;
}
sort(all(table));
return table;
}
ll char_to_idx(char c) {
ll idx = 0;
Forchar(cc, 'a', 'z') {
if (c == cc)
return idx;
else
idx++;
}
}
ll next_combination(ll sub) {
/*
### Attention ###
if k is 0 or 1 and n is 0, it does not work well.
ll n, k; ll bit = (1 << k) - 1;
for (; bit < (1 << n); bit = next_combination(bit)){
bool ith = bit & (1 << i);
procedures...
}
sub & -sub: the binary which shares the last digit whose value is 1 in sub
sub + x : carry up the last digit
~y : the binary whose digits are 1 if y's digit is 0.
(sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub &
~y).
*/
ll x = sub & -sub, y = sub + x;
return (((sub & ~y) / x) >> 1) | y;
}
vl z_algorithm(str s) {
ll n = s.length();
vl res(n);
res[0] = n;
ll i1 = 1, i2 = 0;
while (i1 < n) {
while (i1 + i2 < n && s[i2] == s[i1 + i2])
++i2;
res[i1] = i2;
if (i2 == 0) {
++i1;
continue;
}
ll i3 = 1;
while (i1 + i3 < n && i3 + res[i3] < i2) {
res[i1 + i3] = res[i3];
++i3;
}
i1 += i3, i2 -= i3;
}
return res;
}
ll big_number_mod(str s, ll mod) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth) % mod;
val %= mod;
tenth *= 10;
tenth %= mod;
idx++;
}
return val;
}
ll big_number_compare(str s1, str s2) {
if (s1.length() > s2.length())
return 1;
else if (s1.length() < s2.length())
return -1;
else if (s1 == s2)
return 0;
return 2 * (s1 > s2) - 1;
}
ll string_to_ll(str s) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth);
tenth *= 10;
idx++;
}
return val;
}
str reflected_string(str s) {
str t, u;
ll n = s.length();
t = s;
reverse(all(t));
u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1);
return u;
}
ld distance_between_point_line(point l_begin, point l_end, point p) {
ll xl1 = l_begin.x, yl1 = l_begin.y;
ll xl2 = l_end.x, yl2 = l_end.y;
ll xp = p.x, yp = p.y;
ll a = yl2 - yl1;
ll b = -xl2 + xl1;
ll c = -a * xl2 - b * yl2;
return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b));
}
bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) {
ll x1 = l1_begin.x, y1 = l1_begin.y;
ll x2 = l1_end.x, y2 = l1_end.y;
ll x3 = l2_begin.x, y3 = l2_begin.y;
ll x4 = l2_end.x, y4 = l2_end.y;
ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3);
ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4);
ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1);
ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2);
return val1 * val2 < 0 && val3 * val4 < 0;
}
ld space_of_triangle(point p1, point p2, point p3) {
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
ll v1 = x2 - x1;
ll u1 = y2 - y1;
ll v2 = x3 - x1;
ll u2 = y3 - y1;
ld s = ld(v1 * u2 - u1 * v2) / ld(2);
return abs(s);
}
pair<point, ll> center_2det_of_3points(point p1, point p2, point p3) {
// the center of circle on the given three points
// return the determinant value and the product of center points and 2 *
// determinant value
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
// center of 2 points
ll c2x_2 = x2 + x1, c2y_2 = y2 + y1;
ll c3x_2 = x3 + x1, c3y_2 = y3 + y1;
// vertical vector of 2 lines
ll b2y = x2 - x1, b2x = -y2 + y1;
ll b3y = x3 - x1, b3x = -y3 + y1;
ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x;
ll det = -b3y * b2x + b3x * b2y;
if (det == 0)
return {{INF, INF}, det};
ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2;
return {{cx_2det, cy_2det}, det};
}
ll inversion_number(vl a, ll a_max) {
/*
Paramters
---------
a: vector<ll>
All the elements must be non-negative.
Prefably the elements are compressed to reduce the computational cost.
a_max: ll
The maximum value of the vector a or the value bigger than the value
stated previously.
*/
BIT bit(a_max + 1);
ll val = 0;
rep(i, a.size()) {
// i is the number of elements that have lower index than a[i].
// call the number of elements that have lower value than a[i]
// by subtracting these two, the residual number is the number of elements
// that have larger value
val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1
bit.update(a[i], 1);
}
return val;
}
template <typename T> vector<T> compress(vector<T> v) {
// sort and remove all the duplicated values
sort(all(v));
v.erase(unique(all(v)), v.end());
return v;
}
template <typename T> map<T, ll> dict(const vector<T> &v) {
map<T, ll> d;
rep(i, v.size()) d[v[i]] = i;
return d;
}
void GaussJordanBitVector(vl &bs) {
ll n = bs.size();
ll rank = 0;
ll j = 0;
revrep(i, N_DIGITS) {
for (j = rank; j < n; j++)
if (bs[j] & (1LL << i))
break;
if (j == n)
continue;
if (j > rank)
bs[rank] ^= bs[j];
for (j = rank + 1; j < n; j++)
bs[j] = min(bs[j], bs[j] ^ bs[rank]);
rank++;
}
}
ll kruskal(vector<undirected_edge> &es, ll n_vertex) {
sort(all(es));
UnionFind uf(n_vertex);
ll min_cost = 0;
rep(i, es.size()) {
undirected_edge &e = es[i];
if (!uf.is_same(e.from, e.to)) {
min_cost += e.cost;
uf.unite(e.from, e.to);
}
}
return min_cost;
}
ll LongestIncreasedSequence(vl &v, ll n) {
vl dp(n, INF);
rep(i, n) * lower_bound(all(dp), v[i]) = v[i];
return find(all(dp), INF) - dp.begin();
}
/*
const ll N_VERTEX = 310;
ll a, b, t;
ll dist[N_VERTEX][N_VERTEX];
void warshall_floyd(ll n){
// rep(i, n) rep(j, n) dist[i][j] = INF * (i != j);
rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] +
dist[k][j]);
}
int main(void){
in2(n, m);
rep(i, n) rep(j, n) dist[i][j] = INF * (i != j);
rep(i, m){
in3(a, b, t);
a--; b--;
dist[a][b] = t;
dist[b][a] = t;
}
warshall_floyd(n);
}
ll dist[1005], vertex_pre[1005];
void dijkstra(ll start, ll n) {
priority_queue<pl, vector<pl>, greater<pl>> edge_costs;
fill(dist, dist + n, INF); // fill(vertex_pre, vertex_pre + n, -1);
dist[start] = 0;
edge_costs.push(pl(0, start));
while (!edge_costs.empty()) {
pl edge_cost = edge_costs.top();
edge_costs.pop();
ll idx = edge_cost.second;
ll cost = edge_cost.first;
if (dist[idx] < cost) continue;
rep(i, graph[idx].size()){
edge e = graph[idx][i];
if (dist[e.to] > dist[idx] + e.cost){
dist[e.to] = dist[idx] + e.cost;
// vertex_pre[e.to] = idx;
edge_costs.push(pl(dist[e.to], e.to));
}
}
}
}
vl get_predecessor(ll g){
vl path;
for (; g != -1; g = vertex_pre[g]) path.pb(g);
reverse(all(path));
return path;
}
int main(void){
in2(n, m);
rep(i, m){
in3(a, b, t);
a--; b--;
G[a].pb({b, t});
G[b].pb({a, t});
}
dijkstra(0, n);
}
# ABC061D
bool find_negative_cycle(ll goal){
rep(i, n) rep(v, n) rep(k, graph[v].size()){
edge e = graph[v][k];
if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost){
dist[e.to] = -INF;
if (goal == -1) return true;
else if (goal == e.to) return true;
}
}
return false;
}
bool bellman_ford(ll start, ll n, ll goal){
// if there is a closed circuit, it returns false. (when goal == -1)
// if the distance to goal cannot be obtained, it returns false (when goal
!= -1) fill(dist, dist + n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k,
graph[v].size()){ edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] >
dist[v] + e.cost) dist[e.to] = dist[v] + e.cost;
}
if (find_negative_cycle(goal)) return false;
return true;
}
*/
/*
# 1. The usage of map pair
map<pl, ll> cnt;
cnt[{i, j}] = 0;
items(kv, cnt){
prl2(kv.first, kv.second);
}
# 2. The usage of next_permutation and combination (factorial search)
ll a[8];
rep(i, 8) a[i] = i;
sort(a, a + 8);
do{
}while(next_permutation(a, a+n));
// here, combination
ll n, r;
ll i1, i2, ..., ir;
For(i1, n - r, n) For(i2, n - r - 1, i1) ... For(ir, n - 2 * r + 1, irr){
process;}
# 3. bit search
ll n;
in1(n);
const ll base = 3;
ll upper = power_normal(base, n);
rep(i, upper){
ll tmp = i;
rep(j, n){
rep(k, base) if (tmp % base == k) prl(k);
tmp /= base;
}
}
# 4. imos method
// used when we would like to count the number which
// shows how many times the numbers between l and r belongs to smt.
// This method is composed of three process.
ll n, m, s[MAX_M], l, r;
in2(n, m);
rep(i, m) s[i] = 0;
// 1st step
rep(i, n){
in3(l, r, c);
l--; r--; // if l starts from 1.
s[l] += c; s[r + 1] -= c;
}
// 2nd step
rep(i, m - 1) s[i + 1] += s[i];
// 3rd step: judgement...
#5. shakutori method (syakutori, two pointers technique)
// 1. strech right side while the condition is met.
// 2. renew the answer
// 3. increments left side
// 4. Back to 1. (l <= r must be satisfied all the time.)
ll l = 0; ll r = 0;
while (l < n){
r = max(r, l);
if (l == r) r++;
while(r < n && cond) r++;
answer += r - l; l++;
}
prl(answer);
#6. priority queue
pq q;
ll answer = 0;
ll v;
rep(i, n) q.push(a[i]);
rep(i, m){
v = q.top(); q.pop(); // get the top value and dump the value from queue
v /= 2; q.push(v); // add the new value
}
while(!q.empty()){
answer += q.top();
q.pop();
}
#7. The shortest path (distance) between the k-th edge and another edge
(Tree) Graph tree[MAX_N]; ll depth[MAX_N];
void dfs(ll source, ll parent, ll all_cost){
depth[source] = all_cost;
items(e, tree[source]){
if (e.to == parent) continue;
dfs(e.to, source, all_cost + e.cost);
}
}
ll n, k, a, b, c;
in2(n, k);
rep(i, n - 1){
in3(a, b, c);
a--; b--;
tree[a].pb({b, c});
tree[b].pb({a, c});
}
k--;
dfs(k, -1, 0);
#10. Visiting Subtree using recurrent function (ABC138D)
Graph tree[MAX_N];
ll c[MAX_N];
bool visited[MAX_N];
void dfs(ll source, ll parent, ll val){
visited[source] = true;
c[source] += val;
rep(i, tree[source].size()){
rep(i, m){ll res = n % match[i].e1;}
ll vertex = tree[source][i].to;
if (vertex == parent) continue;
dfs(vertex, source, c[source]);
}
}
#11. bfs ABC146D, ABC007C
1. first create a tree.
2. start searching from a node.
3. do some processes and push nodes connected with a given target node in
BFS.
4. repeat a series of procedure until queue is empty.
queue<pl> q;
void bfs(ll source, ll parents){
ll n_edge = graph[source].size();
if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1);
if (visited[source]) return;
visited[source] = true;
rep(idx, n_edge){
ll target = graph[source][idx].to;
if (target == parents) continue;
q.push(mp(target, source));
}
}
q.push(mp(sg.e1, -1));
while(!q.empty()){
pl source = q.front(); q.pop();
bfs(source.e1, source.e2);
}
#12. grid to distance matrix (dx, dy)
ll w, h;
ll pos_to_idx(ll x, ll y){
return y * w + x;
}
pl idx_to_pos(ll idx){
return mp(idx % w, idx / w);
}
rep(y, h){
in1(s);
rep(x, w){
if (s[x] == '#') wall[x][y] = true;
else wall[x][y] = false;
}
}
rep(i1, h * w)rep(i2, h * w) dist[i1][i2] = INF * (i1 != i2);
rep(x, w)rep(y, h){
ll idx1 = pos_to_idx(x, y); ll idx2;
if (wall[x][y]) continue;
if (x != 0 && !wall[x - 1][y]){
idx2 = pos_to_idx(x - 1, y);
// if warshall floyd
dist[idx1][idx2] = 1;
// if dijkstra
// graph[idx1].pb({idx2, 1});
}
if (x != w - 1 && !wall[x + 1][y]){
idx2 = pos_to_idx(x + 1, y);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
if (y != 0 && !wall[x][y - 1]){
idx2 = pos_to_idx(x, y - 1);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
if (y != h - 1 && !wall[x][y + 1]){
idx2 = pos_to_idx(x, y + 1);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
}
# Cumulative sum (2 dimension)
ll func(ll x, ll y, ll dx, ll dy){
if (x + dx > w || y + dy > h) return - INF;
ll val = cum[x + dx][y + dy];
val += cum[x][y];
val -= cum[x][y + dy];
val -= cum[x + dx][y];
return val;
}
rep(x, w + 1) cum[x][0] = 0;
rep(y, h + 1) cum[0][y] = 0;
rep(y, h + 1) rep(x, w)
cum[x + 1][y + 1] = cum[x][y + 1] + vs[x][y];
rep(x, w + 1) rep(y, h)
cum[x][y + 1] += cum[x][y];
# DEBUG_MATRIX
rep(i, n){
rep(j, n) prs(a[i][j]);
prl("");
}
*/
/*
# the operators regarding bit
& (AND), | (OR), ^ (XOR)
- (REVERSE), >> (SMALLER SHIFT)
<< (BIGGER SHIFT)
x1: 0000 0001 0010 0101 0110 0111 0111
x2: xxxx 0001 0011 0100 0101 1000 0110
x1 & x2: 0000 0001 0010 0100 0100 0000 0110
x: 1001 1010 1100 1011 1101 1111
x & - x: 0001 0010 0100 0001 0001 0001
sum: 1010 1100 10000 1100 1100 10000
x << y is x * 2 ** y
x >> y is rep(i, y) x = x // 2
####### Attention #######
1 << i and 1LL << i is different. If programs show WA, try switch to this one.
Let S be a bit sequence and i be a non-negative integer
S & (1 << i) -> if true, i in S
S | (1 << i) -> S union {i}
S & -(1 << i) -> S - {i}
__builtin_popcount(S) -> the number of elements in S
S = 0 -> S is an empty set
__builtin_popcountl(i) -> the number of 1 in binary
S = (1 << n) - 1 -> S includes all the elements up to the n-th
#Conditional Operator
condition ? true : false;
#iterator
type declaration: auto
value reference: *itr
increment: itr++
decrement: itr--
substitution of value: *itr = smt
# inclusion-exclusion principle
|U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n
- 1) |^[i = 1 to n]Ai|
*/
const ll MAX_N = 200005;
bool okay = false;
ll answer = 0;
ll n;
multiset<ll> s;
void solve() {
rep(i, n) {
ll a;
in1(a);
s.insert(a);
}
s.insert(-INF);
s.insert(INF);
auto it = s.begin();
revrep(i, 31) {
ll target = 1LL << i;
while (!s.empty() && *it <= target / 2 && it != s.end()) {
auto lb = s.lower_bound(target - *it);
if (it == lb) {
if (*next(it) != *lb)
break;
else
it++;
}
if (*lb + *it == target) {
answer++;
s.erase(lb);
it++;
s.erase(prev(it));
} else
it++;
}
it = s.begin();
}
prl(answer);
}
int main(void) {
in1(n);
solve();
// assert(n <= 10000);
return 0;
}
| insert | 1,557 | 1,557 | 1,557 | 1,559 | 0 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.