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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02976 | C++ | Runtime Error | #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;
std::vector<std::vector<LL>> G1, G2;
std::set<std::pair<LL, LL>> path;
std::vector<bool> vis;
std::vector<LL> outs;
std::vector<std::pair<LL, LL>> ans;
void dfs1(LL v) {
for (LL u : G1[v]) {
if (!vis[u]) {
path.insert(std::make_pair(v, u));
vis[u] = true;
dfs1(u);
}
}
}
void dfs2(LL v, LL p) {
if (vis[v])
return;
vis[v] = true;
for (LL u : G2[v])
dfs2(u, v);
if (G2[v].size() == 0) {
if (p != -1) {
G2[p].erase(std::lower_bound(G2[p].begin(), G2[p].end(), v));
if (outs[v] % 2 == 0) {
++outs[p];
ans.push_back(std::make_pair(p, v));
} else {
++outs[v];
ans.push_back(std::make_pair(v, p));
}
}
}
}
int main() {
LL N, M;
in >> N >> M;
std::vector<LL> A(M), B(M);
rep(i, M) in >> A[i] >> B[i];
G1.resize(N + 1);
vis.resize(N + 1);
rep(i, M) {
G1[A[i]].push_back(B[i]);
G1[B[i]].push_back(A[i]);
}
vis[1] = true;
dfs1(1);
G2.resize(N + 1);
vis.clear();
vis.resize(N + 1);
outs.resize(N + 1);
rep(i, M) {
auto it1 = path.find(std::make_pair(A[i], B[i]));
auto it2 = path.find(std::make_pair(B[i], A[i]));
if (it1 != path.end())
G2[A[i]].push_back(B[i]);
else if (it2 != path.end())
G2[B[i]].push_back(A[i]);
else {
++outs[A[i]];
ans.push_back(std::make_pair(A[i], B[i]));
}
}
dfs2(1, -1);
if (M % 2 == 1)
out << -1 << std::endl;
else {
rep(i, ans.size()) out << ans[i].first << " " << ans[i].second << std::endl;
}
}
| #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;
std::vector<std::vector<LL>> G1, G2;
std::set<std::pair<LL, LL>> path;
std::vector<bool> vis;
std::vector<LL> outs;
std::vector<std::pair<LL, LL>> ans;
void dfs1(LL v) {
for (LL u : G1[v]) {
if (!vis[u]) {
path.insert(std::make_pair(v, u));
vis[u] = true;
dfs1(u);
}
}
}
void dfs2(LL v, LL p) {
if (vis[v])
return;
vis[v] = true;
for (LL u : G2[v])
dfs2(u, v);
if (p != -1) {
if (outs[v] % 2 == 0) {
++outs[p];
ans.push_back(std::make_pair(p, v));
} else {
++outs[v];
ans.push_back(std::make_pair(v, p));
}
}
}
int main() {
LL N, M;
in >> N >> M;
std::vector<LL> A(M), B(M);
rep(i, M) in >> A[i] >> B[i];
G1.resize(N + 1);
vis.resize(N + 1);
rep(i, M) {
G1[A[i]].push_back(B[i]);
G1[B[i]].push_back(A[i]);
}
vis[1] = true;
dfs1(1);
G2.resize(N + 1);
vis.clear();
vis.resize(N + 1);
outs.resize(N + 1);
rep(i, M) {
auto it1 = path.find(std::make_pair(A[i], B[i]));
auto it2 = path.find(std::make_pair(B[i], A[i]));
if (it1 != path.end())
G2[A[i]].push_back(B[i]);
else if (it2 != path.end())
G2[B[i]].push_back(A[i]);
else {
++outs[A[i]];
ans.push_back(std::make_pair(A[i], B[i]));
}
}
dfs2(1, -1);
if (M % 2 == 1)
out << -1 << std::endl;
else {
rep(i, ans.size()) out << ans[i].first << " " << ans[i].second << std::endl;
}
}
| replace | 28 | 38 | 28 | 35 | 0 | |
p02976 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define start_routine() int begtime = clock();
#define end_routine() \
int endtime = clock(); \
cerr << endl \
<< "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC \
<< " ms"; \
return 0
#define speed() cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false)
// #define exit(a, b) return cout << a, b;
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define print(stuff) cout << stuff << endl
#define printc(stuff) \
for (auto x : stuff) \
cout << x << " "; \
cout << endl;
#define printPrec(stuff) cout << fixed << setprecision(15) << stuff << endl;
#define debug(stuff) cout << #stuff << ": " << stuff << endl
#define debugc(stuff) \
cout << #stuff << ": "; \
for (auto x : stuff) \
cout << x << " "; \
cout << endl;
#define len length
#define ret0 return 0
#define ret return
#define ll long long
#define ld long double
#define fi first
#define endl '\n'
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fill(ar, x) memset(ar, x, sizeof ar)
#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>
#define inf (long long int)1e18
#define eps 0.000001
#define mod 1000000007
#define mod1 998244353
#define MAXN 100001
vl adj[100005];
bool vis[100005];
ll cnt[100005];
bool edge[100005];
vl tree[100005];
ll ans[100005];
ll in[100005];
map<pll, ll> m1;
map<ll, pll> m2;
void dfs(ll s) {
vis[s] = true;
for (auto x : adj[s]) {
if (vis[x] == false) {
dfs(x);
edge[m1[mp(x, s)]] = true;
tree[x].pb(s), tree[s].pb(x);
}
}
}
void dfs2(ll s, ll e) {
for (auto x : tree[s]) {
if (x != e) {
dfs2(x, s);
if (cnt[x] % 2 == 0) {
if (in[x] % 2 == 0) {
in[s]++;
ll rr = m1[{s, x}];
pll pp = m2[rr];
if (pp.fi == x)
ans[rr] = 0;
else if (pp.fi == s)
ans[rr] = 1;
} else {
in[x]++;
ll rr = m1[{x, s}];
pll pp = m2[rr];
if (pp.fi == x)
ans[rr] = 1;
else if (pp.fi == s)
ans[rr] = 0;
}
} else {
if (in[x] % 2 == 0) {
in[x]++;
ll rr = m1[{s, x}];
pll pp = m2[rr];
if (pp.fi == x)
ans[rr] = 1;
else if (pp.fi == s)
ans[rr] = 0;
} else {
in[s]++;
ll rr = m1[{x, s}];
pll pp = m2[rr];
if (pp.fi == x)
ans[rr] = 0;
else if (pp.fi == s)
ans[rr] = 1;
}
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n, m;
cin >> n >> m;
loop(i, 1, n + 1) vis[i] = false;
loop(i, 1, n + 1) cnt[i] = 0;
loop(i, 1, m + 1) {
edge[i] = false;
ans[i] = -1;
}
loop(i, 1, n + 1) {
in[i] = 0;
adj[i].clear();
tree[i].clear();
}
m1.clear();
m2.clear();
loop(i, 1, m + 1) {
ll u, v;
cin >> u >> v;
adj[u].pb(v), adj[v].pb(u);
m1[mp(u, v)] = m1[mp(v, u)] = i;
m2[i] = mp(u, v);
}
dfs(1);
loop(i, 1, m + 1) {
if (edge[i] == false) {
pll p = m2[i];
ll x = p.fi, y = p.se;
if (x < y)
cnt[x]++;
else
cnt[y]++;
}
}
dfs2(1, 0);
loop(i, 1, m + 1) {
if (edge[i] == false) {
pll ppp = m2[i];
if (ppp.fi < ppp.se) {
ans[i] = 1;
in[ppp.fi]++;
} else {
ans[i] = 0;
in[ppp.se]++;
}
}
}
bool done = true;
loop(i, 1, n + 1) {
if (in[i] % 2 == 1) {
done = false;
break;
}
}
if (done == true) {
loop(i, 1, m + 1) {
if (ans[i] == 0) {
ll x = m2[i].fi, y = m2[i].se;
cout << y << " " << x << endl;
} else if (ans[i] == 1) {
ll x = m2[i].fi, y = m2[i].se;
cout << x << " " << y << endl;
}
}
} else {
cout << -1 << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define start_routine() int begtime = clock();
#define end_routine() \
int endtime = clock(); \
cerr << endl \
<< "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC \
<< " ms"; \
return 0
#define speed() cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false)
// #define exit(a, b) return cout << a, b;
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define print(stuff) cout << stuff << endl
#define printc(stuff) \
for (auto x : stuff) \
cout << x << " "; \
cout << endl;
#define printPrec(stuff) cout << fixed << setprecision(15) << stuff << endl;
#define debug(stuff) cout << #stuff << ": " << stuff << endl
#define debugc(stuff) \
cout << #stuff << ": "; \
for (auto x : stuff) \
cout << x << " "; \
cout << endl;
#define len length
#define ret0 return 0
#define ret return
#define ll long long
#define ld long double
#define fi first
#define endl '\n'
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fill(ar, x) memset(ar, x, sizeof ar)
#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>
#define inf (long long int)1e18
#define eps 0.000001
#define mod 1000000007
#define mod1 998244353
#define MAXN 100001
vl adj[100005];
bool vis[100005];
ll cnt[100005];
bool edge[100005];
vl tree[100005];
ll ans[100005];
ll in[100005];
map<pll, ll> m1;
map<ll, pll> m2;
void dfs(ll s) {
vis[s] = true;
for (auto x : adj[s]) {
if (vis[x] == false) {
dfs(x);
edge[m1[mp(x, s)]] = true;
tree[x].pb(s), tree[s].pb(x);
}
}
}
void dfs2(ll s, ll e) {
for (auto x : tree[s]) {
if (x != e) {
dfs2(x, s);
if (cnt[x] % 2 == 0) {
if (in[x] % 2 == 0) {
in[s]++;
ll rr = m1[{s, x}];
pll pp = m2[rr];
if (pp.fi == x)
ans[rr] = 0;
else if (pp.fi == s)
ans[rr] = 1;
} else {
in[x]++;
ll rr = m1[{x, s}];
pll pp = m2[rr];
if (pp.fi == x)
ans[rr] = 1;
else if (pp.fi == s)
ans[rr] = 0;
}
} else {
if (in[x] % 2 == 0) {
in[x]++;
ll rr = m1[{s, x}];
pll pp = m2[rr];
if (pp.fi == x)
ans[rr] = 1;
else if (pp.fi == s)
ans[rr] = 0;
} else {
in[s]++;
ll rr = m1[{x, s}];
pll pp = m2[rr];
if (pp.fi == x)
ans[rr] = 0;
else if (pp.fi == s)
ans[rr] = 1;
}
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n, m;
cin >> n >> m;
loop(i, 1, n + 1) vis[i] = false;
loop(i, 1, n + 1) cnt[i] = 0;
loop(i, 1, m + 1) {
edge[i] = false;
ans[i] = -1;
}
loop(i, 1, n + 1) {
in[i] = 0;
adj[i].clear();
tree[i].clear();
}
m1.clear();
m2.clear();
loop(i, 1, m + 1) {
ll u, v;
cin >> u >> v;
adj[u].pb(v), adj[v].pb(u);
m1[mp(u, v)] = m1[mp(v, u)] = i;
m2[i] = mp(u, v);
}
dfs(1);
loop(i, 1, m + 1) {
if (edge[i] == false) {
pll p = m2[i];
ll x = p.fi, y = p.se;
if (x < y)
cnt[x]++;
else
cnt[y]++;
}
}
dfs2(1, 0);
loop(i, 1, m + 1) {
if (edge[i] == false) {
pll ppp = m2[i];
if (ppp.fi < ppp.se) {
ans[i] = 1;
in[ppp.fi]++;
} else {
ans[i] = 0;
in[ppp.se]++;
}
}
}
bool done = true;
loop(i, 1, n + 1) {
if (in[i] % 2 == 1) {
done = false;
break;
}
}
if (done == true) {
loop(i, 1, m + 1) {
if (ans[i] == 0) {
ll x = m2[i].fi, y = m2[i].se;
cout << y << " " << x << endl;
} else if (ans[i] == 1) {
ll x = m2[i].fi, y = m2[i].se;
cout << x << " " << y << endl;
}
}
} else {
cout << -1 << endl;
}
}
| delete | 128 | 132 | 128 | 128 | -11 | |
p02976 | C++ | Runtime Error | // #pragma GCC optimize ("-O3","unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; 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
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
#define int long long
#define all(x) (x).begin(), (x).end()
#define print_space(v) \
REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
typedef pair<int, int> P;
const int MOD = 998244353;
const int MAX = 200020;
const double pi = acos(-1);
const double EPS = 1e-12;
const ll INF = 1e18;
int siz[MAX]; // 錬結成分のサイズ
int par[MAX]; // 親
int Rank[MAX]; // 木の深さ
// n要素で初期化
void init(int n) {
REP(i, n) {
par[i] = i;
Rank[i] = 0;
siz[i] = 1;
}
}
// 木の根を求める
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]); // 経路圧縮(根に直接つなぎなおす)もしつつ
}
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
// 低いほうを高いほうにつなげる(効率化)
if (Rank[x] < Rank[y]) {
par[x] = y;
siz[x] += siz[y];
siz[y] = siz[x];
} else {
siz[y] += siz[x];
siz[x] = siz[y];
par[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
vector<P> ans;
vector<int> G[100010];
int score[100010];
set<P> edges;
void dfs(int n, int pre) {
for (auto nxt : G[n]) {
if (pre == nxt)
continue;
dfs(nxt, n);
}
if (n != 0) {
if (!(score[n] & 1)) {
score[pre] ^= 1;
ans.push_back({pre, n});
} else {
ans.push_back({n, pre});
}
}
}
void solve() {
int N, M;
cin >> N >> M;
REP(i, M) {
int u, v;
cin >> u >> v;
u--, v--;
edges.insert({u, v});
}
if (M & 1) {
print(-1);
return;
}
init(N + 1);
auto itr = edges.begin();
// while (itr != edges.end()) {
for (auto p : edges) {
// int u = itr->first, v = itr->second;
int u = p.first, v = p.second;
if (same(u, v)) {
score[u] ^= 1;
itr++;
ans.push_back({u, v});
} else {
G[u].push_back(v);
unite(u, v);
itr++;
}
}
dfs(0, 0);
if (score[0] & 1) {
print(-1);
return;
} else {
REP(i, M) {
pe(ans[i].first + 1);
print(ans[i].second + 1);
}
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
}
| // #pragma GCC optimize ("-O3","unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; 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
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
#define int long long
#define all(x) (x).begin(), (x).end()
#define print_space(v) \
REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
typedef pair<int, int> P;
const int MOD = 998244353;
const int MAX = 200020;
const double pi = acos(-1);
const double EPS = 1e-12;
const ll INF = 1e18;
int siz[MAX]; // 錬結成分のサイズ
int par[MAX]; // 親
int Rank[MAX]; // 木の深さ
// n要素で初期化
void init(int n) {
REP(i, n) {
par[i] = i;
Rank[i] = 0;
siz[i] = 1;
}
}
// 木の根を求める
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]); // 経路圧縮(根に直接つなぎなおす)もしつつ
}
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
// 低いほうを高いほうにつなげる(効率化)
if (Rank[x] < Rank[y]) {
par[x] = y;
siz[x] += siz[y];
siz[y] = siz[x];
} else {
siz[y] += siz[x];
siz[x] = siz[y];
par[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
vector<P> ans;
vector<int> G[100010];
int score[100010];
set<P> edges;
void dfs(int n, int pre) {
for (auto nxt : G[n]) {
if (pre == nxt)
continue;
dfs(nxt, n);
}
if (n != 0) {
if (!(score[n] & 1)) {
score[pre] ^= 1;
ans.push_back({pre, n});
} else {
ans.push_back({n, pre});
}
}
}
void solve() {
int N, M;
cin >> N >> M;
REP(i, M) {
int u, v;
cin >> u >> v;
u--, v--;
edges.insert({u, v});
}
if (M & 1) {
print(-1);
return;
}
init(N + 1);
auto itr = edges.begin();
// while (itr != edges.end()) {
for (auto p : edges) {
// int u = itr->first, v = itr->second;
int u = p.first, v = p.second;
if (same(u, v)) {
score[u] ^= 1;
itr++;
ans.push_back({u, v});
} else {
G[u].push_back(v);
G[v].push_back(u);
unite(u, v);
itr++;
}
}
dfs(0, 0);
if (score[0] & 1) {
print(-1);
return;
} else {
REP(i, M) {
pe(ans[i].first + 1);
print(ans[i].second + 1);
}
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
}
| insert | 150 | 150 | 150 | 151 | 0 | |
p02976 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const int N = 1e5 + 8;
vector<int> g[N];
int c[N];
int used[N];
set<pair<int, int>> ans;
void inv(int u, int v) {
if (ans.count({u, v})) {
ans.erase({u, v});
ans.insert({v, u});
} else if (ans.count({v, u})) {
ans.erase({v, u});
ans.insert({u, v});
} else {
assert(false);
}
}
int dfs(int cur) {
int cv = 0;
used[cur] = 1;
for (auto t : g[cur]) {
if (!used[t]) {
int v = dfs(t);
if (v & 1)
inv(cur, v);
cv += v;
}
}
cv += c[cur];
return cv;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
if (m & 1) {
cout << -1 << endl;
return 0;
}
while (m--) {
int u, v;
cin >> u >> v;
u--;
v--;
c[u] ^= 1;
ans.insert({u, v});
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0);
for (auto t : ans) {
cout << t.first + 1 << ' ' << t.second + 1 << '\n';
}
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const int N = 1e5 + 8;
vector<int> g[N];
int c[N];
int used[N];
set<pair<int, int>> ans;
void inv(int u, int v) {
if (ans.count({u, v})) {
ans.erase({u, v});
ans.insert({v, u});
} else if (ans.count({v, u})) {
ans.erase({v, u});
ans.insert({u, v});
} else {
assert(false);
}
}
int dfs(int cur) {
int cv = 0;
used[cur] = 1;
for (auto t : g[cur]) {
if (!used[t]) {
int v = dfs(t);
if (v & 1)
inv(cur, t);
cv += v;
}
}
cv += c[cur];
return cv;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
if (m & 1) {
cout << -1 << endl;
return 0;
}
while (m--) {
int u, v;
cin >> u >> v;
u--;
v--;
c[u] ^= 1;
ans.insert({u, v});
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0);
for (auto t : ans) {
cout << t.first + 1 << ' ' << t.second + 1 << '\n';
}
}
| replace | 52 | 53 | 52 | 53 | 0 | |
p02976 | C++ | Runtime Error | #include <bits/stdc++.h>
#ifdef LOCAL_DEBUG
#define DEBUG 1
#define CERR \
if (DEBUG) \
cerr
#define MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
#define pr(...) \
CERR << MACRO(__VA_ARGS__, pr10, pr9, pr8, pr7, pr6, pr5, pr4, pr3, pr2, \
pr1)(__VA_ARGS__) \
<< endl
#define pr1(a) (#a) << "=" << (a) << " "
#define pr2(a, b) pr1(a) << pr1(b)
#define pr3(a, b, c) pr1(a) << pr2(b, c)
#define pr4(a, b, c, d) pr1(a) << pr3(b, c, d)
#define pr5(a, b, c, d, e) pr1(a) << pr4(b, c, d, e)
#define pr6(a, b, c, d, e, f) pr1(a) << pr5(b, c, d, e, f)
#define pr7(a, b, c, d, e, f, g) pr1(a) << pr6(b, c, d, e, f, g)
#define pr8(a, b, c, d, e, f, g, h) pr1(a) << pr7(b, c, d, e, f, g, h)
#define pr9(a, b, c, d, e, f, g, h, i) pr1(a) << pr8(b, c, d, e, f, g, h, i)
#define pr10(a, b, c, d, e, f, g, h, i, j) \
pr1(a) << pr9(b, c, d, e, f, g, h, i, j)
#define prArr(a) \
{ \
CERR << (#a) << "={"; \
int i = 0; \
for (auto t : (a)) \
CERR << (i++ ? ", " : "") << t; \
CERR << "}" << endl; \
}
#else
#define DEBUG 0
#define pr(...)
#define prArr(a)
#endif
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL << 60) + 1e9; // ~ 1.15 * 1e18
const Int mod = (1e9) + 7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int, Int>;
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
template <class T1, class T2> ostream &operator<<(ostream &o, pair<T1, T2> p) {
return o << "(" << p.first << "," << p.second << ")";
}
template <class T1, class T2, class T3>
ostream &operator<<(ostream &o, tuple<T1, T2, T3> t) {
return o << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")";
}
template <class T1, class T2> istream &operator>>(istream &i, pair<T1, T2> &p) {
return i >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &o, vector<T> a) {
Int i = 0;
for (T t : a)
o << (i++ ? " " : "") << t;
return o;
}
template <class T> istream &operator>>(istream &i, vector<T> &a) {
for (T &t : a)
i >> t;
return i;
}
// INSERT ABOVE HERE
Int N;
vector<vector<Int>> G;
vector<set<Int>> T;
void makeTree(Int pos) {
static Int used[100010];
used[pos] = 1;
for (Int to : G[pos]) {
if (used[to])
continue;
used[to] = 1;
makeTree(to);
T[pos].insert(to);
}
}
set<P> ans;
vector<Int> indeg, outdeg;
void dfs(Int pos, Int pre) {
for (Int to : G[pos]) {
if (to == pre)
continue;
if (T[pos].count(to))
continue;
if (ans.count(P(pos, to)))
continue;
if (ans.count(P(to, pos)))
continue;
ans.emplace(pos, to);
outdeg[pos]++;
indeg[to]++;
}
for (Int to : T[pos]) {
if (to == pre)
continue;
dfs(to, pos);
}
if (pre == -1)
return;
if (indeg[pos] % 2 == 1) {
ans.emplace(pre, pos);
indeg[pos]++;
outdeg[pre]++;
} else {
ans.emplace(pos, pre);
outdeg[pos]++;
indeg[pre]++;
}
}
signed main() {
srand((unsigned)time(NULL));
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
Int M;
cin >> N >> M;
G.resize(N);
T.resize(N);
indeg.resize(N);
outdeg.resize(N);
for (Int i = 0; i < M; i++) {
Int a, b;
cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
if (M % 2) {
cout << -1 << endl;
return 0;
}
makeTree(0);
dfs(0, -1);
for (auto p : ans)
cout << p.first + 1 << " " << p.second + 1 << endl;
vector<Int> in(N), out(N);
for (auto p : ans)
out[p.first]++, in[p.second]++;
for (Int i = 0; i < N; i++) {
assert(out[i] % 2 == 0);
}
return 0;
}
| #include <bits/stdc++.h>
#ifdef LOCAL_DEBUG
#define DEBUG 1
#define CERR \
if (DEBUG) \
cerr
#define MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
#define pr(...) \
CERR << MACRO(__VA_ARGS__, pr10, pr9, pr8, pr7, pr6, pr5, pr4, pr3, pr2, \
pr1)(__VA_ARGS__) \
<< endl
#define pr1(a) (#a) << "=" << (a) << " "
#define pr2(a, b) pr1(a) << pr1(b)
#define pr3(a, b, c) pr1(a) << pr2(b, c)
#define pr4(a, b, c, d) pr1(a) << pr3(b, c, d)
#define pr5(a, b, c, d, e) pr1(a) << pr4(b, c, d, e)
#define pr6(a, b, c, d, e, f) pr1(a) << pr5(b, c, d, e, f)
#define pr7(a, b, c, d, e, f, g) pr1(a) << pr6(b, c, d, e, f, g)
#define pr8(a, b, c, d, e, f, g, h) pr1(a) << pr7(b, c, d, e, f, g, h)
#define pr9(a, b, c, d, e, f, g, h, i) pr1(a) << pr8(b, c, d, e, f, g, h, i)
#define pr10(a, b, c, d, e, f, g, h, i, j) \
pr1(a) << pr9(b, c, d, e, f, g, h, i, j)
#define prArr(a) \
{ \
CERR << (#a) << "={"; \
int i = 0; \
for (auto t : (a)) \
CERR << (i++ ? ", " : "") << t; \
CERR << "}" << endl; \
}
#else
#define DEBUG 0
#define pr(...)
#define prArr(a)
#endif
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL << 60) + 1e9; // ~ 1.15 * 1e18
const Int mod = (1e9) + 7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int, Int>;
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
template <class T1, class T2> ostream &operator<<(ostream &o, pair<T1, T2> p) {
return o << "(" << p.first << "," << p.second << ")";
}
template <class T1, class T2, class T3>
ostream &operator<<(ostream &o, tuple<T1, T2, T3> t) {
return o << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")";
}
template <class T1, class T2> istream &operator>>(istream &i, pair<T1, T2> &p) {
return i >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &o, vector<T> a) {
Int i = 0;
for (T t : a)
o << (i++ ? " " : "") << t;
return o;
}
template <class T> istream &operator>>(istream &i, vector<T> &a) {
for (T &t : a)
i >> t;
return i;
}
// INSERT ABOVE HERE
Int N;
vector<vector<Int>> G;
vector<set<Int>> T;
void makeTree(Int pos) {
static Int used[100010];
used[pos] = 1;
for (Int to : G[pos]) {
if (used[to])
continue;
used[to] = 1;
makeTree(to);
T[pos].insert(to);
}
}
set<P> ans;
vector<Int> indeg, outdeg;
void dfs(Int pos, Int pre) {
for (Int to : G[pos]) {
if (to == pre)
continue;
if (T[pos].count(to))
continue;
if (ans.count(P(pos, to)))
continue;
if (ans.count(P(to, pos)))
continue;
ans.emplace(pos, to);
outdeg[pos]++;
indeg[to]++;
}
for (Int to : T[pos]) {
if (to == pre)
continue;
dfs(to, pos);
}
if (pre == -1)
return;
if (outdeg[pos] % 2 == 0) {
ans.emplace(pre, pos);
indeg[pos]++;
outdeg[pre]++;
} else {
ans.emplace(pos, pre);
outdeg[pos]++;
indeg[pre]++;
}
}
signed main() {
srand((unsigned)time(NULL));
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
Int M;
cin >> N >> M;
G.resize(N);
T.resize(N);
indeg.resize(N);
outdeg.resize(N);
for (Int i = 0; i < M; i++) {
Int a, b;
cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
if (M % 2) {
cout << -1 << endl;
return 0;
}
makeTree(0);
dfs(0, -1);
for (auto p : ans)
cout << p.first + 1 << " " << p.second + 1 << endl;
vector<Int> in(N), out(N);
for (auto p : ans)
out[p.first]++, in[p.second]++;
for (Int i = 0; i < N; i++) {
assert(out[i] % 2 == 0);
}
return 0;
}
| replace | 113 | 114 | 113 | 114 | 0 | |
p02976 | C++ | Runtime Error | // #define DEBUGGING
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
using ll = int64_t;
using ull = uint64_t;
using PLL = pair<ll, ll>;
template <typename T> const T &var_min(const T &t) { return t; }
template <typename T> const T &var_max(const T &t) { return t; }
template <typename T, typename... Tail>
const T &var_min(const T &t, const Tail &...tail) {
return min(t, var_min(tail...));
}
template <typename T, typename... Tail>
const T &var_max(const T &t, const Tail &...tail) {
return max(t, var_max(tail...));
}
template <typename T, typename... Tail> void chmin(T &t, const Tail &...tail) {
t = var_min(t, tail...);
}
template <typename T, typename... Tail> void chmax(T &t, const Tail &...tail) {
t = var_max(t, tail...);
}
template <typename T> const T &clamp(const T &t, const T &low, const T &high) {
return max(low, min(high, t));
}
template <typename T> void chclamp(T &t, const T &low, const T &high) {
t = clamp(t, low, high);
}
namespace init__ {
struct InitIO {
InitIO() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(30);
}
} init_io;
} // namespace init__
#ifdef DEBUGGING
// #include "../debug/debug.cpp"
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
return V<decltype(rec)>(s, rec);
#undef rec
}
struct UnionFind {
V<ll> rank;
V<ll> parent;
UnionFind(ll N) : rank(N, 0), parent(N) {
iota(parent.begin(), parent.end(), 0ll);
}
ll find(ll child) {
return (child == parent[child] ? child
: parent[child] = find(parent[child]));
}
void unit(ll x, ll y) {
ll px = find(x);
ll py = find(y);
if (px == py) {
return;
}
if (rank[px] < rank[py]) {
swap(px, py);
}
parent[py] = px;
rank[px] += (rank[px] == rank[py]);
}
bool same(ll x, ll y) { return (find(x) == find(y)); }
};
V<set<ll>> edges;
VV<ll> tree_edges;
V<ll> tree_parents;
V<ll> depth;
using PQ = priority_queue<PLL, V<PLL>, less<PLL>>;
void dfs(ll now, ll pre, PQ &que, ll d = 0) {
bool leaf = true;
depth[now] = d;
for (ll nxt : tree_edges[now]) {
if (nxt == pre)
continue;
leaf = false;
dfs(nxt, now, que, d + 1);
}
tree_parents[now] = pre;
if (leaf)
que.emplace(depth[now], now);
}
int main() {
ll N, M;
cin >> N >> M;
edges.resize(N);
tree_edges.resize(N);
tree_parents.resize(N);
V<PLL> all_edges(M);
depth.resize(N);
for (ll i = 0; i < M; i++) {
ll a, b;
cin >> a >> b;
a--;
b--;
edges[a].insert(b);
edges[b].insert(a);
all_edges[i] = PLL(a, b);
}
UnionFind uf(N);
for (auto &&ele : all_edges) {
ll a, b;
tie(a, b) = ele;
if (uf.same(a, b))
continue;
DEBUG(make_tuple(a, b));
tree_edges[a].push_back(b);
tree_edges[b].push_back(a);
uf.unit(a, b);
}
PQ leaves;
dfs(0, -1, leaves);
V<ll> cnt_in(N);
set<PLL> ans;
auto update_ans = [&](ll from, ll to) {
DEBUG(PLL(from, to));
PLL e(from, to), re(to, from);
if (ans.find(e) != ans.end() || ans.find(re) != ans.end())
return;
cnt_in[from]++;
ans.emplace(from + 1, to + 1);
};
V<bool> visited(N);
while (leaves.size()) {
ll now = leaves.top().second;
leaves.pop();
ll par = tree_parents[now];
visited[now] = true;
for (auto &&ele : edges[now]) {
if (ele == par)
continue;
update_ans(now, ele);
edges[ele].erase(now);
}
if (now == 0) {
assert(leaves.size() == 0);
break;
}
if (cnt_in[now] % 2 == 0)
update_ans(par, now);
else
update_ans(now, par);
edges[par].erase(now);
if (!visited[par]) {
leaves.emplace(depth[par], par);
visited[par] = true;
}
}
DEBUG(cnt_in);
assert(ans.size() == M);
if (cnt_in[0] % 2 == 0) {
for (auto &&ele : ans)
cout << ele.first << ' ' << ele.second << endl;
} else {
cout << -1 << endl;
}
return 0;
}
| // #define DEBUGGING
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
using ll = int64_t;
using ull = uint64_t;
using PLL = pair<ll, ll>;
template <typename T> const T &var_min(const T &t) { return t; }
template <typename T> const T &var_max(const T &t) { return t; }
template <typename T, typename... Tail>
const T &var_min(const T &t, const Tail &...tail) {
return min(t, var_min(tail...));
}
template <typename T, typename... Tail>
const T &var_max(const T &t, const Tail &...tail) {
return max(t, var_max(tail...));
}
template <typename T, typename... Tail> void chmin(T &t, const Tail &...tail) {
t = var_min(t, tail...);
}
template <typename T, typename... Tail> void chmax(T &t, const Tail &...tail) {
t = var_max(t, tail...);
}
template <typename T> const T &clamp(const T &t, const T &low, const T &high) {
return max(low, min(high, t));
}
template <typename T> void chclamp(T &t, const T &low, const T &high) {
t = clamp(t, low, high);
}
namespace init__ {
struct InitIO {
InitIO() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(30);
}
} init_io;
} // namespace init__
#ifdef DEBUGGING
// #include "../debug/debug.cpp"
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
return V<decltype(rec)>(s, rec);
#undef rec
}
struct UnionFind {
V<ll> rank;
V<ll> parent;
UnionFind(ll N) : rank(N, 0), parent(N) {
iota(parent.begin(), parent.end(), 0ll);
}
ll find(ll child) {
return (child == parent[child] ? child
: parent[child] = find(parent[child]));
}
void unit(ll x, ll y) {
ll px = find(x);
ll py = find(y);
if (px == py) {
return;
}
if (rank[px] < rank[py]) {
swap(px, py);
}
parent[py] = px;
rank[px] += (rank[px] == rank[py]);
}
bool same(ll x, ll y) { return (find(x) == find(y)); }
};
V<set<ll>> edges;
VV<ll> tree_edges;
V<ll> tree_parents;
V<ll> depth;
using PQ = priority_queue<PLL, V<PLL>, less<PLL>>;
void dfs(ll now, ll pre, PQ &que, ll d = 0) {
bool leaf = true;
depth[now] = d;
for (ll nxt : tree_edges[now]) {
if (nxt == pre)
continue;
leaf = false;
dfs(nxt, now, que, d + 1);
}
tree_parents[now] = pre;
if (leaf)
que.emplace(depth[now], now);
}
int main() {
ll N, M;
cin >> N >> M;
edges.resize(N);
tree_edges.resize(N);
tree_parents.resize(N);
V<PLL> all_edges(M);
depth.resize(N);
for (ll i = 0; i < M; i++) {
ll a, b;
cin >> a >> b;
a--;
b--;
edges[a].insert(b);
edges[b].insert(a);
all_edges[i] = PLL(a, b);
}
UnionFind uf(N);
for (auto &&ele : all_edges) {
ll a, b;
tie(a, b) = ele;
if (uf.same(a, b))
continue;
DEBUG(make_tuple(a, b));
tree_edges[a].push_back(b);
tree_edges[b].push_back(a);
uf.unit(a, b);
}
PQ leaves;
dfs(0, -1, leaves);
V<ll> cnt_in(N);
set<PLL> ans;
auto update_ans = [&](ll from, ll to) {
DEBUG(PLL(from, to));
PLL e(from, to), re(to, from);
cnt_in[from]++;
ans.emplace(from + 1, to + 1);
};
V<bool> visited(N);
while (leaves.size()) {
ll now = leaves.top().second;
leaves.pop();
ll par = tree_parents[now];
visited[now] = true;
for (auto &&ele : edges[now]) {
if (ele == par)
continue;
update_ans(now, ele);
edges[ele].erase(now);
}
if (now == 0) {
assert(leaves.size() == 0);
break;
}
if (cnt_in[now] % 2 == 0)
update_ans(par, now);
else
update_ans(now, par);
edges[par].erase(now);
if (!visited[par]) {
leaves.emplace(depth[par], par);
visited[par] = true;
}
}
DEBUG(cnt_in);
assert(ans.size() == M);
if (cnt_in[0] % 2 == 0) {
for (auto &&ele : ans)
cout << ele.first << ' ' << ele.second << endl;
} else {
cout << -1 << endl;
}
return 0;
}
| delete | 153 | 155 | 153 | 153 | 0 | |
p02976 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
pair<int, int> edge[maxn];
int taken[maxn], deg[maxn], ansu[maxn], ansv[maxn], fa[maxn], tot;
vector<int> adj[maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void dfs(int u, int parent) {
for (auto v : adj[u]) {
if (v != parent)
dfs(v, u);
}
if (parent == -1)
return;
if (deg[u] & 1)
ansu[tot] = u, ansv[tot] = parent, ++tot;
else
ansu[tot] = parent, ansv[tot] = u, deg[parent] ^= 1, ++tot;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
if (m & 1) {
cout << -1;
return 0;
}
for (int i = 0; i < m; ++i) {
cin >> edge[i].first >> edge[i].second;
}
for (int i = 1; i <= n; ++i)
fa[i] = i;
int root;
try {
for (int i = 0; i < m; ++i)
if (find(edge[i].first) != find(edge[i].second)) {
fa[edge[i].second] = find(edge[i].first);
root = edge[i].first;
adj[edge[i].first].push_back(edge[i].second);
adj[edge[i].second].push_back(edge[i].first);
} else {
ansu[tot] = edge[i].first;
ansv[tot] = edge[i].second;
++tot;
deg[edge[i].first] ^= 1;
}
} catch (exception &e) {
while (1)
;
}
dfs(root, -1);
for (int i = 0; i < m; ++i)
cout << ansu[i] << ' ' << ansv[i] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
pair<int, int> edge[maxn];
int taken[maxn], deg[maxn], ansu[maxn], ansv[maxn], fa[maxn], tot;
vector<int> adj[maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void dfs(int u, int parent) {
for (auto v : adj[u]) {
if (v != parent)
dfs(v, u);
}
if (parent == -1)
return;
if (deg[u] & 1)
ansu[tot] = u, ansv[tot] = parent, ++tot;
else
ansu[tot] = parent, ansv[tot] = u, deg[parent] ^= 1, ++tot;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
if (m & 1) {
cout << -1;
return 0;
}
for (int i = 0; i < m; ++i) {
cin >> edge[i].first >> edge[i].second;
}
for (int i = 1; i <= n; ++i)
fa[i] = i;
int root;
for (int i = 0; i < m; ++i)
if (find(edge[i].first) != find(edge[i].second)) {
fa[find(edge[i].second)] = find(edge[i].first);
root = edge[i].first;
adj[edge[i].first].push_back(edge[i].second);
adj[edge[i].second].push_back(edge[i].first);
} else {
ansu[tot] = edge[i].first;
ansv[tot] = edge[i].second;
++tot;
deg[edge[i].first] ^= 1;
}
dfs(root, -1);
for (int i = 0; i < m; ++i)
cout << ansu[i] << ' ' << ansv[i] << endl;
return 0;
}
| replace | 41 | 58 | 41 | 53 | 0 | |
p02976 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
const int MAXN = 1e5 + 5;
#define ii pair<int, int>
int n, m;
int lvl[MAXN], p[MAXN], par[MAXN];
bool visited[MAXN];
vector<int> adj[MAXN];
vector<ii> e;
int getParent(int x) { return (p[x] == x) ? x : getParent(p[x]); }
void conn(int a, int b) {
int pa = getParent(a), pb = getParent(b);
if (pa != pb) {
if (lvl[pa] < pb)
p[pa] = pb;
else
p[pb] = pa;
if (lvl[pa] == lvl[pb])
lvl[pa]++;
}
}
void dfs(int pos, int prev) {
visited[pos] = true;
for (int i : adj[pos]) {
if (visited[i])
continue;
dfs(i, pos);
}
if (par[pos] == 1) {
e.push_back(ii(pos, prev));
par[pos] = 1 - par[pos];
} else if (pos != 0) {
e.push_back(ii(prev, pos));
par[prev] = 1 - par[prev];
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i) {
p[i] = i;
lvl[i] = 0;
par[i] = 0;
}
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
if (getParent(u) != getParent(v)) {
conn(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
} else {
e.push_back(ii(u, v));
par[u] = 1 - par[u];
}
}
dfs(0, -1);
if (m % 2 == 1) {
printf("-1\n");
} else {
for (ii i : e) {
printf("%d %d\n", i.first + 1, i.second + 1);
}
}
} | #include "bits/stdc++.h"
using namespace std;
const int MAXN = 1e5 + 5;
#define ii pair<int, int>
int n, m;
int lvl[MAXN], p[MAXN], par[MAXN];
bool visited[MAXN];
vector<int> adj[MAXN];
vector<ii> e;
int getParent(int x) { return (p[x] == x) ? x : getParent(p[x]); }
void conn(int a, int b) {
int pa = getParent(a), pb = getParent(b);
if (pa != pb) {
if (lvl[pa] < lvl[pb])
p[pa] = pb;
else
p[pb] = pa;
if (lvl[pa] == lvl[pb])
lvl[pa]++;
}
}
void dfs(int pos, int prev) {
visited[pos] = true;
for (int i : adj[pos]) {
if (visited[i])
continue;
dfs(i, pos);
}
if (par[pos] == 1) {
e.push_back(ii(pos, prev));
par[pos] = 1 - par[pos];
} else if (pos != 0) {
e.push_back(ii(prev, pos));
par[prev] = 1 - par[prev];
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i) {
p[i] = i;
lvl[i] = 0;
par[i] = 0;
}
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
if (getParent(u) != getParent(v)) {
conn(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
} else {
e.push_back(ii(u, v));
par[u] = 1 - par[u];
}
}
dfs(0, -1);
if (m % 2 == 1) {
printf("-1\n");
} else {
for (ii i : e) {
printf("%d %d\n", i.first + 1, i.second + 1);
}
}
} | replace | 17 | 18 | 17 | 18 | TLE | |
p02976 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define INF bit(60)
#define pb push_back
#define mod 1000000007
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;
}
using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
using tTree = __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
int qp(int a, ll b) {
int ans = 1;
do {
if (b & 1)
ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod;
} while (b >>= 1);
return ans;
}
int qp(int a, ll b, int mo) {
int ans = 1;
do {
if (b & 1)
ans = 1ll * ans * a % mo;
a = 1ll * a * a % mo;
} while (b >>= 1);
return ans;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
#define FACSIZE 2002
ll invfac[FACSIZE];
ll fac[FACSIZE];
ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
void make_fact(ll);
void make_invfact(ll);
struct graph {
// ll rest;
// vector<graph*> next;
unordered_set<ll> next;
vector<ll> in;
vector<ll> out;
// vector<graph*> in;
// vector<graph*> out;
// graph() : rest(0) {};
};
int main(void) {
ll n, m;
cin >> n >> m;
vector<ll> a(m);
vector<ll> b(m);
rep(i, m) cin >> a[i] >> b[i];
if (m % 2 == 1) {
cout << -1 << endl;
return 0;
}
vector<graph> G(n + 1);
rep(i, m) {
ll A = a[i], B = b[i];
G[A].next.insert(B);
// G[A].rest++;
G[B].next.insert(A);
// G[B].rest++;
}
using pa = pair<ll, ll>;
priority_queue<pa, vector<pa>, greater<pa>> pque;
rep1(i, n) { pque.push(make_pair(G[i].next.size(), -i)); }
while (!pque.empty()) {
auto pq = pque.top();
pque.pop();
auto cur = pq.second;
// cout << pq.first << " " << pq.second << endl;
if (G[cur].next.size() != pq.first)
continue;
if (pq.first == 1) {
ll nex = *begin(G[cur].next);
ll outsize = G[cur].out.size();
if (outsize % 2 == 0) {
G[cur].in.push_back(nex);
G[nex].out.push_back(cur);
} else {
G[cur].out.push_back(nex);
G[nex].in.push_back(cur);
}
G[cur].next.erase(nex);
G[nex].next.erase(cur);
if (G[nex].next.size() > 0)
pque.push(make_pair(G[nex].next.size(), -nex));
} else {
ll nex = *(begin(G[cur].next));
G[cur].out.push_back(nex);
G[nex].in.push_back(cur);
G[cur].next.erase(nex);
G[nex].next.erase(cur);
if (G[nex].next.size() > 0)
pque.push(make_pair(G[nex].next.size(), -nex));
if (G[cur].next.size() > 0)
pque.push(make_pair(G[cur].next.size(), -cur));
}
}
rep1(i, n) {
for (auto x : G[i].out) {
cout << i << " " << x << endl;
}
}
return 0;
}
ll mod_pow(ll x, ll r) {
if (r == 0)
return 1;
else if (r == 1)
return x % mod;
else if (r % 2 == 0) {
ll t = mod_pow(x, r / 2) % mod;
return t * t % mod;
} else {
ll t = mod_pow(x, r / 2) % mod;
ll k = t * t % mod;
return k * x % mod;
}
}
ll mod_add(ll a, ll b) { return (a + b) % mod; }
ll mod_mul(ll a, ll b) { return (a * b) % mod; }
ll mod_div(ll a, ll b) { return mod_mul(a, mod_pow(b, mod - 2)); }
void make_fact(ll n) {
fac[0] = 1;
rep(i, n) { fac[i + 1] = mod_mul(fac[i], i + 1); }
}
void make_invfact(ll n) {
invfac[n] = mod_pow(fac[n], mod - 2);
for (int i = n - 1; i >= 0; i--) {
invfac[i] = mod_mul(invfac[i + 1], i + 1);
}
} | #include <bits/stdc++.h>
#include <iostream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define INF bit(60)
#define pb push_back
#define mod 1000000007
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;
}
using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
using tTree = __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
int qp(int a, ll b) {
int ans = 1;
do {
if (b & 1)
ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod;
} while (b >>= 1);
return ans;
}
int qp(int a, ll b, int mo) {
int ans = 1;
do {
if (b & 1)
ans = 1ll * ans * a % mo;
a = 1ll * a * a % mo;
} while (b >>= 1);
return ans;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
#define FACSIZE 2002
ll invfac[FACSIZE];
ll fac[FACSIZE];
ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
void make_fact(ll);
void make_invfact(ll);
struct graph {
// ll rest;
// vector<graph*> next;
unordered_set<ll> next;
vector<ll> in;
vector<ll> out;
// vector<graph*> in;
// vector<graph*> out;
// graph() : rest(0) {};
};
int main(void) {
ll n, m;
cin >> n >> m;
vector<ll> a(m);
vector<ll> b(m);
rep(i, m) cin >> a[i] >> b[i];
if (m % 2 == 1) {
cout << -1 << endl;
return 0;
}
vector<graph> G(n + 1);
rep(i, m) {
ll A = a[i], B = b[i];
G[A].next.insert(B);
// G[A].rest++;
G[B].next.insert(A);
// G[B].rest++;
}
using pa = pair<ll, ll>;
priority_queue<pa, vector<pa>, greater<pa>> pque;
rep1(i, n) { pque.push(make_pair(G[i].next.size(), -i)); }
while (!pque.empty()) {
auto pq = pque.top();
pque.pop();
auto cur = -pq.second;
// cout << pq.first << " " << pq.second << endl;
if (G[cur].next.size() != pq.first)
continue;
if (pq.first == 1) {
ll nex = *begin(G[cur].next);
ll outsize = G[cur].out.size();
if (outsize % 2 == 0) {
G[cur].in.push_back(nex);
G[nex].out.push_back(cur);
} else {
G[cur].out.push_back(nex);
G[nex].in.push_back(cur);
}
G[cur].next.erase(nex);
G[nex].next.erase(cur);
if (G[nex].next.size() > 0)
pque.push(make_pair(G[nex].next.size(), -nex));
} else {
ll nex = *(begin(G[cur].next));
G[cur].out.push_back(nex);
G[nex].in.push_back(cur);
G[cur].next.erase(nex);
G[nex].next.erase(cur);
if (G[nex].next.size() > 0)
pque.push(make_pair(G[nex].next.size(), -nex));
if (G[cur].next.size() > 0)
pque.push(make_pair(G[cur].next.size(), -cur));
}
}
rep1(i, n) {
for (auto x : G[i].out) {
cout << i << " " << x << endl;
}
}
return 0;
}
ll mod_pow(ll x, ll r) {
if (r == 0)
return 1;
else if (r == 1)
return x % mod;
else if (r % 2 == 0) {
ll t = mod_pow(x, r / 2) % mod;
return t * t % mod;
} else {
ll t = mod_pow(x, r / 2) % mod;
ll k = t * t % mod;
return k * x % mod;
}
}
ll mod_add(ll a, ll b) { return (a + b) % mod; }
ll mod_mul(ll a, ll b) { return (a * b) % mod; }
ll mod_div(ll a, ll b) { return mod_mul(a, mod_pow(b, mod - 2)); }
void make_fact(ll n) {
fac[0] = 1;
rep(i, n) { fac[i + 1] = mod_mul(fac[i], i + 1); }
}
void make_invfact(ll n) {
invfac[n] = mod_pow(fac[n], mod - 2);
for (int i = n - 1; i >= 0; i--) {
invfac[i] = mod_mul(invfac[i + 1], i + 1);
}
} | replace | 115 | 116 | 115 | 116 | 0 | |
p02976 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <vector>
using namespace std;
const int maxn = 2e5 + 7;
vector<int> g[maxn];
int xx[maxn], yy[maxn], len = 0;
int dep[maxn], in[maxn];
void dfs(int u, int pre) {
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == pre)
continue;
if (dep[v] == 0) {
dfs(v, u);
} else if (dep[v] > dep[u]) {
in[u]++;
xx[++len] = u;
yy[len] = v;
}
}
if (in[u] & 1) {
assert(pre != 0);
len++;
in[u]++;
xx[len] = u;
yy[len] = pre;
} else {
len++;
in[pre]++;
xx[len] = pre;
yy[len] = u;
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
if (m & 1) {
printf("-1\n");
return 0;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
assert((in[i] & 1) == 0);
for (int i = 1; i <= m; i++)
printf("%d %d\n", xx[i], yy[i]);
return 0;
}
| #include <bits/stdc++.h>
#include <vector>
using namespace std;
const int maxn = 2e5 + 7;
vector<int> g[maxn];
int xx[maxn], yy[maxn], len = 0;
int dep[maxn], in[maxn];
void dfs(int u, int pre) {
dep[u] = dep[pre] + 1;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == pre)
continue;
if (dep[v] == 0) {
dfs(v, u);
} else if (dep[v] > dep[u]) {
in[u]++;
xx[++len] = u;
yy[len] = v;
}
}
if (in[u] & 1) {
assert(pre != 0);
len++;
in[u]++;
xx[len] = u;
yy[len] = pre;
} else {
len++;
in[pre]++;
xx[len] = pre;
yy[len] = u;
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
if (m & 1) {
printf("-1\n");
return 0;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
assert((in[i] & 1) == 0);
for (int i = 1; i <= m; i++)
printf("%d %d\n", xx[i], yy[i]);
return 0;
}
| insert | 10 | 10 | 10 | 11 | -11 | |
p02976 | C++ | Runtime Error | #include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
int len = v.size();
for (int i = 0; i < len; ++i) {
s << v[i];
if (i < len - 1)
s << "\t";
}
return s;
}
// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
int len = vv.size();
for (int i = 0; i < len; ++i) {
s << vv[i] << endl;
}
return s;
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
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 (b < a) {
a = b;
return true;
}
return false;
}
const int M = 100001;
vector<int> graph[M];
int par[M];
int visit[M];
int out[M]; // 出て行く辺の本数
vector<int> order; // 親子の順
set<ll> dir;
void bfs(int start) {
queue<int> remain;
remain.push(start);
visit[start] = 1;
while (remain.size() > 0) {
ll from = remain.front();
remain.pop();
order.push_back(from);
for (ll to : graph[from]) {
if (visit[to] != 0) {
if (par[from] == to)
continue;
if (dir.find(to * M + from) != dir.end())
continue;
dir.insert(from * M + to);
out[from] += 1;
continue;
}
remain.push(to);
par[to] = from;
visit[to] = 1;
}
}
}
int main() {
memset(par, 0, sizeof(par));
memset(visit, 0, sizeof(visit));
memset(out, 0, sizeof(out));
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int from, to;
cin >> from >> to;
graph[from].push_back(to);
graph[to].push_back(from);
}
if (m % 2 == 1) {
cout << -1 << endl;
return 0;
}
bfs(1);
for (int i = m - 1; i > 0; --i) {
ll v = order[i];
ll p = par[v];
if (out[v] % 2 == 0) {
dir.insert(p * M + v);
out[p] += 1;
} else {
dir.insert(v * M + p);
out[v] += 1;
}
}
// cout << order << endl;
// for (int i = 1; i <= m; ++i) {
// cout << par[i] << " " << visit[i] << " " << out[i] << " " << order[i] <<
// endl;
// }
for (ll e : dir) {
cout << (int)(e / M) << " " << (int)(e % M) << endl;
}
}
| #include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
int len = v.size();
for (int i = 0; i < len; ++i) {
s << v[i];
if (i < len - 1)
s << "\t";
}
return s;
}
// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
int len = vv.size();
for (int i = 0; i < len; ++i) {
s << vv[i] << endl;
}
return s;
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
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 (b < a) {
a = b;
return true;
}
return false;
}
const int M = 100001;
vector<int> graph[M];
int par[M];
int visit[M];
int out[M]; // 出て行く辺の本数
vector<int> order; // 親子の順
set<ll> dir;
void bfs(int start) {
queue<int> remain;
remain.push(start);
visit[start] = 1;
while (remain.size() > 0) {
ll from = remain.front();
remain.pop();
order.push_back(from);
for (ll to : graph[from]) {
if (visit[to] != 0) {
if (par[from] == to)
continue;
if (dir.find(to * M + from) != dir.end())
continue;
dir.insert(from * M + to);
out[from] += 1;
continue;
}
remain.push(to);
par[to] = from;
visit[to] = 1;
}
}
}
int main() {
memset(par, 0, sizeof(par));
memset(visit, 0, sizeof(visit));
memset(out, 0, sizeof(out));
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int from, to;
cin >> from >> to;
graph[from].push_back(to);
graph[to].push_back(from);
}
if (m % 2 == 1) {
cout << -1 << endl;
return 0;
}
bfs(1);
for (int i = n - 1; i > 0; --i) {
ll v = order[i];
ll p = par[v];
if (out[v] % 2 == 0) {
dir.insert(p * M + v);
out[p] += 1;
} else {
dir.insert(v * M + p);
out[v] += 1;
}
}
// cout << order << endl;
// for (int i = 1; i <= m; ++i) {
// cout << par[i] << " " << visit[i] << " " << out[i] << " " << order[i] <<
// endl;
// }
for (ll e : dir) {
cout << (int)(e / M) << " " << (int)(e % M) << endl;
}
}
| replace | 99 | 100 | 99 | 100 | 0 | |
p02976 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define LL long long
using namespace std;
int read() {
bool f = 0;
int x = 0;
char c = getchar();
while (c < '0' || '9' < c) {
if (c == '-')
f = 1;
c = getchar();
}
while ('0' <= c && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return !f ? x : -x;
}
#define MAXN 100000
#define INF 0x3f3f3f3f
int fa[MAXN + 5];
vector<int> G[MAXN + 5];
int Find(int u) { return fa[u] == u ? u : fa[u] = Find(fa[u]); }
int out[MAXN + 5];
void DFS(int u, int f) {
for (int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if (v == f)
continue;
DFS(v, u);
if (out[v] & 1) {
out[v]++;
printf("%d %d\n", v, u);
} else {
out[u]++;
printf("%d %d\n", u, v);
}
}
return;
}
int main() {
int n = read(), m = read();
if (m & 1) {
puts("-1");
return 0;
}
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
if (Find(u) != Find(v)) {
G[Find(u)].push_back(Find(v));
G[Find(v)].push_back(Find(u));
} else {
out[u]++;
printf("%d %d\n", u, v);
}
}
DFS(1, 0);
return 0;
}
| #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define LL long long
using namespace std;
int read() {
bool f = 0;
int x = 0;
char c = getchar();
while (c < '0' || '9' < c) {
if (c == '-')
f = 1;
c = getchar();
}
while ('0' <= c && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return !f ? x : -x;
}
#define MAXN 100000
#define INF 0x3f3f3f3f
int fa[MAXN + 5];
vector<int> G[MAXN + 5];
int Find(int u) { return fa[u] == u ? u : fa[u] = Find(fa[u]); }
int out[MAXN + 5];
void DFS(int u, int f) {
for (int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if (v == f)
continue;
DFS(v, u);
if (out[v] & 1) {
out[v]++;
printf("%d %d\n", v, u);
} else {
out[u]++;
printf("%d %d\n", u, v);
}
}
return;
}
int main() {
int n = read(), m = read();
if (m & 1) {
puts("-1");
return 0;
}
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
if (Find(u) != Find(v)) {
G[u].push_back(v);
G[v].push_back(u);
fa[Find(u)] = Find(v);
} else {
out[u]++;
printf("%d %d\n", u, v);
}
}
DFS(1, 0);
return 0;
}
| replace | 60 | 62 | 60 | 63 | -11 | |
p02976 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define rn return
#define fi first
#define se second
typedef std::pair<int, int> P;
using namespace std;
using ll = long long;
int main() {
int N, M;
cin >> N >> M;
if (M % 2 == 1) {
cout << "-1";
rn 0;
}
vector<vector<int>> g(N);
rep(i, M) {
int x, y;
cin >> x >> y;
x--;
y--;
g[y].push_back(x);
g[x].push_back(y);
}
vector<pair<int, int>> ans;
vector<int> degree(N, 0);
vector<int> depth(N, -1);
function<void(int, int)> DFS = [&](int v, int pr) {
for (int u : g[v]) {
if (depth[u] == -1) {
depth[u] = depth[v] + 1;
DFS(u, v);
} else if (depth[u] > depth[v]) {
degree[v]++;
ans.emplace_back(v, u);
}
}
if (degree[v] % 2 == 1) {
assert(pr != -1);
ans.emplace_back(v, pr);
degree[v]++;
} else {
assert(pr != -1);
ans.emplace_back(pr, v);
degree[pr]++;
}
};
depth[0] = 0;
DFS(0, -1);
for (auto &p : ans) {
cout << p.first + 1 << " " << p.second + 1 << endl;
}
rn 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define rn return
#define fi first
#define se second
typedef std::pair<int, int> P;
using namespace std;
using ll = long long;
int main() {
int N, M;
cin >> N >> M;
if (M % 2 == 1) {
cout << "-1";
rn 0;
}
vector<vector<int>> g(N);
rep(i, M) {
int x, y;
cin >> x >> y;
x--;
y--;
g[y].push_back(x);
g[x].push_back(y);
}
vector<pair<int, int>> ans;
vector<int> degree(N, 0);
vector<int> depth(N, -1);
function<void(int, int)> DFS = [&](int v, int pr) {
for (int u : g[v]) {
if (depth[u] == -1) {
depth[u] = depth[v] + 1;
DFS(u, v);
} else if (depth[u] > depth[v]) {
degree[v]++;
ans.emplace_back(v, u);
}
}
if (degree[v] % 2 == 1) {
assert(pr != -1);
ans.emplace_back(v, pr);
degree[v]++;
} else {
if (pr != -1) {
ans.emplace_back(pr, v);
degree[pr]++;
}
}
};
depth[0] = 0;
DFS(0, -1);
for (auto &p : ans) {
cout << p.first + 1 << " " << p.second + 1 << endl;
}
rn 0;
} | replace | 57 | 60 | 57 | 61 | -6 | fade01b3-540a-42e3-b647-9489e73370bb.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02976/C++/s381861975.cpp:55: main()::<lambda(int, int)>: Assertion `pr!=-1' failed.
|
p02976 | C++ | Runtime Error | #include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> A(M), B(M);
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
}
vector<int> D(N);
vector<vector<int>> E(N);
vector<vector<int>> EI(N);
vector<int> C(N);
for (int i = 0; i < M; i++) {
E[A[i]].push_back(B[i]);
E[B[i]].push_back(A[i]);
EI[A[i]].push_back(i);
EI[B[i]].push_back(i);
C[A[i]] ^= 1;
}
int c = 0;
for (int i = 0; i < N; i++)
c += C[i];
if (c % 2 != 0) {
cout << -1 << endl;
return 0;
}
vector<int> PV;
vector<int> PE;
vector<int> V(N);
function<void(int)> f = [&](int c) {
PV.push_back(c);
V[c] = true;
for (int i = 0; i < (int)E[c].size(); i++)
if (!V[E[c][i]]) {
PE.push_back(EI[c][i]);
f(E[c][i]);
PE.push_back(EI[c][i]);
PV.push_back(c);
}
};
f(0);
for (int i = 0; i < (int)PV.size() - 1;) {
if (C[PV[i]] == 0) {
i++;
} else {
while (C[PV[i]] != 0 && i < (int)PV.size() - 1) {
C[PV[i]] ^= 1;
C[PV[i + 1]] ^= 1;
D[PE[i]] ^= 1;
i++;
}
}
}
for (int i = 0; i < M; i++) {
if (D[i] == 0)
cout << A[i] + 1 << " " << B[i] + 1 << endl;
else
cout << B[i] + 1 << " " << A[i] + 1 << endl;
}
}
| #include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> A(M), B(M);
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
}
vector<int> D(M);
vector<vector<int>> E(N);
vector<vector<int>> EI(N);
vector<int> C(N);
for (int i = 0; i < M; i++) {
E[A[i]].push_back(B[i]);
E[B[i]].push_back(A[i]);
EI[A[i]].push_back(i);
EI[B[i]].push_back(i);
C[A[i]] ^= 1;
}
int c = 0;
for (int i = 0; i < N; i++)
c += C[i];
if (c % 2 != 0) {
cout << -1 << endl;
return 0;
}
vector<int> PV;
vector<int> PE;
vector<int> V(N);
function<void(int)> f = [&](int c) {
PV.push_back(c);
V[c] = true;
for (int i = 0; i < (int)E[c].size(); i++)
if (!V[E[c][i]]) {
PE.push_back(EI[c][i]);
f(E[c][i]);
PE.push_back(EI[c][i]);
PV.push_back(c);
}
};
f(0);
for (int i = 0; i < (int)PV.size() - 1;) {
if (C[PV[i]] == 0) {
i++;
} else {
while (C[PV[i]] != 0 && i < (int)PV.size() - 1) {
C[PV[i]] ^= 1;
C[PV[i + 1]] ^= 1;
D[PE[i]] ^= 1;
i++;
}
}
}
for (int i = 0; i < M; i++) {
if (D[i] == 0)
cout << A[i] + 1 << " " << B[i] + 1 << endl;
else
cout << B[i] + 1 << " " << A[i] + 1 << endl;
}
}
| replace | 15 | 16 | 15 | 16 | 0 | |
p02976 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;
template <typename T> std::vector<T> make_v(size_t a) {
return std::vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
std::vector<int> a(n), b(n);
std::vector<std::vector<std::pair<int, int>>> g(n);
std::map<std::pair<int, int>, int> mp;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a[i], &b[i]);
a[i]--;
b[i]--;
g[a[i]].push_back({b[i], i});
g[b[i]].push_back({a[i], i});
mp[{a[i], b[i]}] = mp[{b[i], a[i]}] = i;
}
if (m % 2) {
printf("-1\n");
return 0;
}
std::vector<bool> latte(m, false);
std::vector<bool> used(n, false);
std::vector<std::pair<int, int>> edges;
std::function<int(int, int)> solve = [&](int v, int par) {
used[v] = true;
std::vector<int> vec;
for (auto e : g[v]) {
if (e.first == par or latte[e.second])
continue;
if (used[e.first]) {
vec.push_back(e.second);
continue;
}
if (solve(e.first, v))
vec.push_back(e.second);
}
if (vec.size() == 0)
return 1;
if (vec.size() % 2) {
vec.push_back(mp[{v, par}]);
for (auto p : vec) {
int x = a[p], y = b[p];
if (x == v)
edges.push_back({x, y});
else
edges.push_back({y, x});
latte[p] = true;
}
return 0;
} else {
for (auto p : vec) {
int x = a[p], y = b[p];
if (x == v)
edges.push_back({x, y});
else
edges.push_back({y, x});
latte[p] = true;
}
return 1;
}
};
solve(0, -1);
for (auto v : edges)
printf("%d %d\n", v.first + 1, v.second + 1);
return 0;
}
| #include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;
template <typename T> std::vector<T> make_v(size_t a) {
return std::vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
std::vector<int> a(m), b(m);
std::vector<std::vector<std::pair<int, int>>> g(n);
std::map<std::pair<int, int>, int> mp;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a[i], &b[i]);
a[i]--;
b[i]--;
g[a[i]].push_back({b[i], i});
g[b[i]].push_back({a[i], i});
mp[{a[i], b[i]}] = mp[{b[i], a[i]}] = i;
}
if (m % 2) {
printf("-1\n");
return 0;
}
std::vector<bool> latte(m, false);
std::vector<bool> used(n, false);
std::vector<std::pair<int, int>> edges;
std::function<int(int, int)> solve = [&](int v, int par) {
used[v] = true;
std::vector<int> vec;
for (auto e : g[v]) {
if (e.first == par or latte[e.second])
continue;
if (used[e.first]) {
vec.push_back(e.second);
continue;
}
if (solve(e.first, v))
vec.push_back(e.second);
}
if (vec.size() == 0)
return 1;
if (vec.size() % 2) {
vec.push_back(mp[{v, par}]);
for (auto p : vec) {
int x = a[p], y = b[p];
if (x == v)
edges.push_back({x, y});
else
edges.push_back({y, x});
latte[p] = true;
}
return 0;
} else {
for (auto p : vec) {
int x = a[p], y = b[p];
if (x == v)
edges.push_back({x, y});
else
edges.push_back({y, x});
latte[p] = true;
}
return 1;
}
};
solve(0, -1);
for (auto v : edges)
printf("%d %d\n", v.first + 1, v.second + 1);
return 0;
}
| replace | 18 | 19 | 18 | 19 | 0 | |
p02976 | C++ | Runtime Error | #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <stdio.h>
#include <time.h>
#include <unordered_set>
#include <vector>
#define prev askdnsajd
#define rank asasdmdnasd
#define next lsjndajbs
#define hash asmdklansd
#define index asdklnas
#define right sld
#define left sldl
using namespace std;
typedef long long ll;
typedef long double dbl;
template <class T> void print(vector<T> s) {
for (T x : s)
cout << x << " ";
cout << endl;
}
const int maxn = 1e5 + 7;
vector<int> s[maxn];
int color[maxn], h[maxn];
int parent[maxn];
void dfs(int v) {
color[v] = 1;
for (int u : s[v]) {
if (color[u] == 0) {
h[u] = h[v] + 1;
parent[u] = v;
dfs(u);
}
}
}
int in[maxn];
int main() {
srand(17);
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
if (m & 1) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
s[a - 1].push_back(b - 1);
s[b - 1].push_back(a - 1);
}
dfs(0);
vector<int> lst(n);
for (int i = 0; i < n; i++)
lst[i] = i;
sort(lst.begin(), lst.end(), [](int a, int b) { return h[a] > h[b]; });
for (int u : lst) {
if (u == 0)
break;
for (int v : s[u]) {
if (h[v] + 1 < h[u]) {
cout << u + 1 << " " << v + 1 << "\n";
in[u]++;
}
}
if (in[u] & 1)
cout << u + 1 << " " << parent[u] + 1 << "\n";
else {
cout << parent[u] + 1 << " " << u + 1 << "\n";
in[parent[u]]++;
}
}
return 0;
} | // #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <stdio.h>
#include <time.h>
#include <unordered_set>
#include <vector>
#define prev askdnsajd
#define rank asasdmdnasd
#define next lsjndajbs
#define hash asmdklansd
#define index asdklnas
#define right sld
#define left sldl
using namespace std;
typedef long long ll;
typedef long double dbl;
template <class T> void print(vector<T> s) {
for (T x : s)
cout << x << " ";
cout << endl;
}
const int maxn = 1e5 + 7;
vector<int> s[maxn];
int color[maxn], h[maxn];
int parent[maxn];
void dfs(int v) {
color[v] = 1;
for (int u : s[v]) {
if (color[u] == 0) {
h[u] = h[v] + 1;
parent[u] = v;
dfs(u);
}
}
}
int in[maxn];
int main() {
srand(17);
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
if (m & 1) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
s[a - 1].push_back(b - 1);
s[b - 1].push_back(a - 1);
}
dfs(0);
vector<int> lst(n);
for (int i = 0; i < n; i++)
lst[i] = i;
sort(lst.begin(), lst.end(), [](int a, int b) { return h[a] > h[b]; });
for (int u : lst) {
if (u == 0)
break;
for (int v : s[u]) {
if (h[v] + 1 < h[u]) {
cout << u + 1 << " " << v + 1 << "\n";
in[u]++;
}
}
if (in[u] & 1)
cout << u + 1 << " " << parent[u] + 1 << "\n";
else {
cout << parent[u] + 1 << " " << u + 1 << "\n";
in[parent[u]]++;
}
}
return 0;
} | replace | 0 | 3 | 0 | 3 | 0 | |
p02977 | C++ | Time Limit Exceeded | #pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize
const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 200005;
const ld PI = 4 * atan((ld)1);
template <class T> void ckmin(T &a, T b) { a = min(a, b); }
template <class T> void ckmax(T &a, T b) { a = max(a, b); }
namespace input {
template <class T> void re(complex<T> &x);
template <class T1, class T2> void re(pair<T1, T2> &p);
template <class T> void re(vector<T> &a);
template <class T, size_t SZ> void re(array<T, SZ> &a);
template <class T> void re(T &x) { cin >> x; }
void re(double &x) {
string t;
re(t);
x = stod(t);
}
void re(ld &x) {
string t;
re(t);
x = stold(t);
}
template <class Arg, class... Args> void re(Arg &first, Args &...rest) {
re(first);
re(rest...);
}
template <class T> void re(complex<T> &x) {
T a, b;
re(a, b);
x = cd(a, b);
}
template <class T1, class T2> void re(pair<T1, T2> &p) { re(p.f, p.s); }
template <class T> void re(vector<T> &a) { F0R(i, sz(a)) re(a[i]); }
template <class T, size_t SZ> void re(array<T, SZ> &a) { F0R(i, SZ) re(a[i]); }
} // namespace input
using namespace input;
namespace output {
template <class T1, class T2> void pr(const pair<T1, T2> &x);
template <class T, size_t SZ> void pr(const array<T, SZ> &x);
template <class T> void pr(const vector<T> &x);
template <class T> void pr(const set<T> &x);
template <class T1, class T2> void pr(const map<T1, T2> &x);
template <class T> void pr(const T &x) { cout << x; }
template <class Arg, class... Args>
void pr(const Arg &first, const Args &...rest) {
pr(first);
pr(rest...);
}
template <class T1, class T2> void pr(const pair<T1, T2> &x) {
pr("{", x.f, ", ", x.s, "}");
}
template <class T> void prContain(const T &x) {
pr("{");
bool fst = 1;
for (const auto &a : x)
pr(!fst ? ", " : "", a), fst = 0; // const needed for vector<bool>
pr("}");
}
template <class T, size_t SZ> void pr(const array<T, SZ> &x) { prContain(x); }
template <class T> void pr(const vector<T> &x) { prContain(x); }
template <class T> void pr(const set<T> &x) { prContain(x); }
template <class T1, class T2> void pr(const map<T1, T2> &x) { prContain(x); }
void ps() { pr("\n"); }
template <class Arg> void ps(const Arg &first) {
pr(first);
ps(); // no space at end of line
}
template <class Arg, class... Args>
void ps(const Arg &first, const Args &...rest) {
pr(first, " ");
ps(rest...); // print w/ spaces
}
} // namespace output
using namespace output;
namespace io {
void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0);
cin.tie(0); // fast I/O
if (sz(s)) {
setIn(s + ".in"), setOut(s + ".out");
} // for USACO
}
} // namespace io
using namespace io;
template <class T> T invGeneral(T a, T b) {
a %= b;
if (a == 0)
return b == 1 ? 0 : -1;
T x = invGeneral(b, a);
return x == -1 ? -1 : ((1 - (ll)b * x) / a + b) % b;
}
template <class T> struct modular {
T val;
explicit operator T() const { return val; }
modular() { val = 0; }
modular(const ll &v) {
val = (-MOD <= v && v <= MOD) ? v : v % MOD;
if (val < 0)
val += MOD;
}
friend ostream &operator<<(ostream &os, const modular &a) {
return os << a.val;
}
friend bool operator==(const modular &a, const modular &b) {
return a.val == b.val;
}
friend bool operator!=(const modular &a, const modular &b) {
return !(a == b);
}
modular operator-() const { return modular(-val); }
modular &operator+=(const modular &m) {
if ((val += m.val) >= MOD)
val -= MOD;
return *this;
}
modular &operator-=(const modular &m) {
if ((val -= m.val) < 0)
val += MOD;
return *this;
}
modular &operator*=(const modular &m) {
val = (ll)val * m.val % MOD;
return *this;
}
friend modular pow(modular a, ll p) {
modular ans = 1;
for (; p; p /= 2, a *= a)
if (p & 1)
ans *= a;
return ans;
}
friend modular inv(const modular &a) {
auto i = invGeneral(a.val, MOD);
assert(i != -1);
return i;
} // equivalent to return exp(b,MOD-2) if MOD is prime
modular &operator/=(const modular &m) { return (*this) *= inv(m); }
friend modular operator+(modular a, const modular &b) { return a += b; }
friend modular operator-(modular a, const modular &b) { return a -= b; }
friend modular operator*(modular a, const modular &b) { return a *= b; }
friend modular operator/(modular a, const modular &b) { return a /= b; }
};
typedef modular<int> mi;
typedef pair<mi, mi> pmi;
typedef vector<mi> vmi;
typedef vector<pmi> vpmi;
int N, val[MX];
vi adj[MX];
vpi ed;
void addEdge(int a, int b) { ed.pb({a, b}); }
void dfs(int a, int b) {
trav(t, adj[a]) if (t != b) {
val[t] = val[a];
if (t > N)
val[t] ^= t - N;
else
val[t] ^= t;
dfs(t, a);
}
}
void solve() {
ed.clear();
int k = 1;
while (2 * k + 1 <= N)
k = 2 * k + 1;
if (N == 1 || N == k + 1) {
ps("No");
return;
}
vi seq = {1};
int des = -1;
if (N % 2 == 0) {
des = N - k;
seq.pb(des);
}
FOR(i, 2, k + 1) if (i != des) seq.pb(i);
int z = sz(seq);
F0R(i, z) seq.pb(seq[i] + N);
F0R(i, sz(seq) - 1) addEdge(seq[i], seq[i + 1]);
for (int i = k + 1; i + 1 <= N; i += 2) {
addEdge(1, i);
addEdge(i, i + 1);
addEdge(1, i + 1 + N);
addEdge(i + 1 + N, i + N);
}
if (N % 2 == 0) {
addEdge(N, k + 1);
addEdge(des, 2 * N);
}
ps("Yes");
assert(sz(ed) == 2 * N - 1);
FOR(i, 1, MX) adj[i].clear();
trav(t, ed) {
ps(t.f, t.s);
adj[t.f].pb(t.s), adj[t.s].pb(t.f);
}
FOR(i, 1, N + 1) {
val[i] = i;
dfs(i, 0);
assert(val[N + i] == i);
}
}
int main() {
setIO();
re(N);
solve();
/*FOR(z,1,17) {
N = z; ps(N);
solve();
}*/
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?), set tle
* do smth instead of nothing and stay organized
*/ | #pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize
const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 200005;
const ld PI = 4 * atan((ld)1);
template <class T> void ckmin(T &a, T b) { a = min(a, b); }
template <class T> void ckmax(T &a, T b) { a = max(a, b); }
namespace input {
template <class T> void re(complex<T> &x);
template <class T1, class T2> void re(pair<T1, T2> &p);
template <class T> void re(vector<T> &a);
template <class T, size_t SZ> void re(array<T, SZ> &a);
template <class T> void re(T &x) { cin >> x; }
void re(double &x) {
string t;
re(t);
x = stod(t);
}
void re(ld &x) {
string t;
re(t);
x = stold(t);
}
template <class Arg, class... Args> void re(Arg &first, Args &...rest) {
re(first);
re(rest...);
}
template <class T> void re(complex<T> &x) {
T a, b;
re(a, b);
x = cd(a, b);
}
template <class T1, class T2> void re(pair<T1, T2> &p) { re(p.f, p.s); }
template <class T> void re(vector<T> &a) { F0R(i, sz(a)) re(a[i]); }
template <class T, size_t SZ> void re(array<T, SZ> &a) { F0R(i, SZ) re(a[i]); }
} // namespace input
using namespace input;
namespace output {
template <class T1, class T2> void pr(const pair<T1, T2> &x);
template <class T, size_t SZ> void pr(const array<T, SZ> &x);
template <class T> void pr(const vector<T> &x);
template <class T> void pr(const set<T> &x);
template <class T1, class T2> void pr(const map<T1, T2> &x);
template <class T> void pr(const T &x) { cout << x; }
template <class Arg, class... Args>
void pr(const Arg &first, const Args &...rest) {
pr(first);
pr(rest...);
}
template <class T1, class T2> void pr(const pair<T1, T2> &x) {
pr("{", x.f, ", ", x.s, "}");
}
template <class T> void prContain(const T &x) {
pr("{");
bool fst = 1;
for (const auto &a : x)
pr(!fst ? ", " : "", a), fst = 0; // const needed for vector<bool>
pr("}");
}
template <class T, size_t SZ> void pr(const array<T, SZ> &x) { prContain(x); }
template <class T> void pr(const vector<T> &x) { prContain(x); }
template <class T> void pr(const set<T> &x) { prContain(x); }
template <class T1, class T2> void pr(const map<T1, T2> &x) { prContain(x); }
void ps() { pr("\n"); }
template <class Arg> void ps(const Arg &first) {
pr(first);
ps(); // no space at end of line
}
template <class Arg, class... Args>
void ps(const Arg &first, const Args &...rest) {
pr(first, " ");
ps(rest...); // print w/ spaces
}
} // namespace output
using namespace output;
namespace io {
void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0);
cin.tie(0); // fast I/O
if (sz(s)) {
setIn(s + ".in"), setOut(s + ".out");
} // for USACO
}
} // namespace io
using namespace io;
template <class T> T invGeneral(T a, T b) {
a %= b;
if (a == 0)
return b == 1 ? 0 : -1;
T x = invGeneral(b, a);
return x == -1 ? -1 : ((1 - (ll)b * x) / a + b) % b;
}
template <class T> struct modular {
T val;
explicit operator T() const { return val; }
modular() { val = 0; }
modular(const ll &v) {
val = (-MOD <= v && v <= MOD) ? v : v % MOD;
if (val < 0)
val += MOD;
}
friend ostream &operator<<(ostream &os, const modular &a) {
return os << a.val;
}
friend bool operator==(const modular &a, const modular &b) {
return a.val == b.val;
}
friend bool operator!=(const modular &a, const modular &b) {
return !(a == b);
}
modular operator-() const { return modular(-val); }
modular &operator+=(const modular &m) {
if ((val += m.val) >= MOD)
val -= MOD;
return *this;
}
modular &operator-=(const modular &m) {
if ((val -= m.val) < 0)
val += MOD;
return *this;
}
modular &operator*=(const modular &m) {
val = (ll)val * m.val % MOD;
return *this;
}
friend modular pow(modular a, ll p) {
modular ans = 1;
for (; p; p /= 2, a *= a)
if (p & 1)
ans *= a;
return ans;
}
friend modular inv(const modular &a) {
auto i = invGeneral(a.val, MOD);
assert(i != -1);
return i;
} // equivalent to return exp(b,MOD-2) if MOD is prime
modular &operator/=(const modular &m) { return (*this) *= inv(m); }
friend modular operator+(modular a, const modular &b) { return a += b; }
friend modular operator-(modular a, const modular &b) { return a -= b; }
friend modular operator*(modular a, const modular &b) { return a *= b; }
friend modular operator/(modular a, const modular &b) { return a /= b; }
};
typedef modular<int> mi;
typedef pair<mi, mi> pmi;
typedef vector<mi> vmi;
typedef vector<pmi> vpmi;
int N, val[MX];
vi adj[MX];
vpi ed;
void addEdge(int a, int b) { ed.pb({a, b}); }
void dfs(int a, int b) {
trav(t, adj[a]) if (t != b) {
val[t] = val[a];
if (t > N)
val[t] ^= t - N;
else
val[t] ^= t;
dfs(t, a);
}
}
void solve() {
ed.clear();
int k = 1;
while (2 * k + 1 <= N)
k = 2 * k + 1;
if (N == 1 || N == k + 1) {
ps("No");
return;
}
vi seq = {1};
int des = -1;
if (N % 2 == 0) {
des = N - k;
seq.pb(des);
}
FOR(i, 2, k + 1) if (i != des) seq.pb(i);
int z = sz(seq);
F0R(i, z) seq.pb(seq[i] + N);
F0R(i, sz(seq) - 1) addEdge(seq[i], seq[i + 1]);
for (int i = k + 1; i + 1 <= N; i += 2) {
addEdge(1, i);
addEdge(i, i + 1);
addEdge(1, i + 1 + N);
addEdge(i + 1 + N, i + N);
}
if (N % 2 == 0) {
addEdge(N, k + 1);
addEdge(des, 2 * N);
}
ps("Yes");
assert(sz(ed) == 2 * N - 1);
FOR(i, 1, MX) adj[i].clear();
trav(t, ed) {
ps(t.f, t.s);
adj[t.f].pb(t.s), adj[t.s].pb(t.f);
}
/*FOR(i,1,N+1) {
val[i] = i; dfs(i,0);
assert(val[N+i] == i);
}*/
}
int main() {
setIO();
re(N);
solve();
/*FOR(z,1,17) {
N = z; ps(N);
solve();
}*/
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?), set tle
* do smth instead of nothing and stay organized
*/ | replace | 269 | 274 | 269 | 273 | TLE | |
p02977 | C++ | Time Limit Exceeded | // #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma warning(disable : 4786)
#pragma warning(disable : 4996)
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
template <class c>
typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
cerr << boolalpha << i;
return *this;
}
template <class c, int = 0>
typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
return *this << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
};
#define watch(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#define S(X) ((X) * (X))
#define SZ(V) (int)V.size()
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORAB(i, a, b) for (int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C) ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))
// typedef int LL;
typedef long long int LL;
// typedef __int128 LLL;
typedef long long LLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VP;
typedef vector<double> VD;
typedef long double ld;
VI adj[200005];
int n;
void add(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void Print(int a, int b) {
for (int i = a; i < b; i++) {
printf("%d %d\n", i, i + 1);
add(i, i + 1);
}
printf("%d %d\n", b, n + a);
add(b, n + a);
for (int i = n + a; i < n + b; i++) {
printf("%d %d\n", i, i + 1);
add(i, i + 1);
}
}
int vis[200005];
void dfs(int at, int par = 0) {
vis[at] = 1;
for (int u : adj[at]) {
if (u == par)
continue;
if (vis[u])
assert(0);
dfs(u, at);
}
}
void check(int a, int b, int me, int prev = -1) {
int exp = (a <= n ? a : a - n);
if (a == b) {
if (me != exp)
printf(">>%d %d %d\n", me, exp, b - n);
assert(me == exp);
}
for (int u : adj[a]) {
if (u == prev)
continue;
check(u, b, me ^ (u <= n ? u : u - n), a);
}
}
void solve(int ks) {
scanf("%d", &n);
if (!(n & (n - 1))) {
printf("No\n");
return;
}
printf("Yes\n");
int last = 3;
while (last + 4 <= n)
last += 4;
// Print line.
Print(1, 3);
for (int i = 4; i <= last; i += 4) {
printf("%d %d\n", n + i - 1, i);
add(n + i - 1, i);
Print(i, i + 3);
}
if ((last & (last + 1)) || (n - last == 0)) {
// last+1 ... n
if (last + 1 <= n) {
int x = (last + 1) ^ (last - 3);
printf("%d %d\n", n + x + 1, last + 1);
printf("%d %d\n", last - 3, n + last + 1);
add(n + x + 1, last + 1);
add(last - 3, n + last + 1);
}
if (last + 2 <= n) {
printf("%d %d\n", last + 1, last + 2);
printf("1 %d\n", n + last + 2);
add(last + 1, last + 2);
add(1, n + last + 2);
}
if (last + 3 <= n) {
printf("%d %d\n", last + 1, last + 3);
printf("3 %d\n", n + last + 3);
add(last + 1, last + 3);
add(3, n + last + 3);
}
} else {
int x = last + 1;
printf("%d %d\n", x, 1);
add(x, 1);
printf("%d %d\n", x + 1, 1);
add(x + 1, 1);
printf("%d %d\n", n + x, x + 1);
add(n + x, x + 1);
printf("%d %d\n", x, n + x + 1);
add(x, n + x + 1);
if (last + 3 <= n) {
printf("%d %d\n", x + 1, last + 3);
add(x + 1, last + 3);
printf("%d %d\n", 2, last + 3 + n);
add(2, last + 3 + n);
}
}
dfs(1);
for (int i = 1; i <= 2 * n; i++)
assert(vis[i]);
for (int i = 1; i <= n; i++)
check(i, i + n, i);
}
int main() {
#ifdef LOCAL
double start_time = clock();
freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
if (0) {
int T;
scanf("%d", &T);
// AIN(T, 1, 25);
for (int ks = 1; ks <= T; ks++) {
solve(ks);
fprintf(stderr, "%d done\n", ks);
}
} else {
solve(0);
}
#ifdef LOCAL
double end_time = clock();
fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);
#endif
return 0;
}
| // #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma warning(disable : 4786)
#pragma warning(disable : 4996)
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
template <class c>
typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
cerr << boolalpha << i;
return *this;
}
template <class c, int = 0>
typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
return *this << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
};
#define watch(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#define S(X) ((X) * (X))
#define SZ(V) (int)V.size()
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORAB(i, a, b) for (int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C) ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))
// typedef int LL;
typedef long long int LL;
// typedef __int128 LLL;
typedef long long LLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VP;
typedef vector<double> VD;
typedef long double ld;
VI adj[200005];
int n;
void add(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void Print(int a, int b) {
for (int i = a; i < b; i++) {
printf("%d %d\n", i, i + 1);
add(i, i + 1);
}
printf("%d %d\n", b, n + a);
add(b, n + a);
for (int i = n + a; i < n + b; i++) {
printf("%d %d\n", i, i + 1);
add(i, i + 1);
}
}
int vis[200005];
void dfs(int at, int par = 0) {
vis[at] = 1;
for (int u : adj[at]) {
if (u == par)
continue;
if (vis[u])
assert(0);
dfs(u, at);
}
}
void check(int a, int b, int me, int prev = -1) {
int exp = (a <= n ? a : a - n);
if (a == b) {
if (me != exp)
printf(">>%d %d %d\n", me, exp, b - n);
assert(me == exp);
}
for (int u : adj[a]) {
if (u == prev)
continue;
check(u, b, me ^ (u <= n ? u : u - n), a);
}
}
void solve(int ks) {
scanf("%d", &n);
if (!(n & (n - 1))) {
printf("No\n");
return;
}
printf("Yes\n");
int last = 3;
while (last + 4 <= n)
last += 4;
// Print line.
Print(1, 3);
for (int i = 4; i <= last; i += 4) {
printf("%d %d\n", n + i - 1, i);
add(n + i - 1, i);
Print(i, i + 3);
}
if ((last & (last + 1)) || (n - last == 0)) {
// last+1 ... n
if (last + 1 <= n) {
int x = (last + 1) ^ (last - 3);
printf("%d %d\n", n + x + 1, last + 1);
printf("%d %d\n", last - 3, n + last + 1);
add(n + x + 1, last + 1);
add(last - 3, n + last + 1);
}
if (last + 2 <= n) {
printf("%d %d\n", last + 1, last + 2);
printf("1 %d\n", n + last + 2);
add(last + 1, last + 2);
add(1, n + last + 2);
}
if (last + 3 <= n) {
printf("%d %d\n", last + 1, last + 3);
printf("3 %d\n", n + last + 3);
add(last + 1, last + 3);
add(3, n + last + 3);
}
} else {
int x = last + 1;
printf("%d %d\n", x, 1);
add(x, 1);
printf("%d %d\n", x + 1, 1);
add(x + 1, 1);
printf("%d %d\n", n + x, x + 1);
add(n + x, x + 1);
printf("%d %d\n", x, n + x + 1);
add(x, n + x + 1);
if (last + 3 <= n) {
printf("%d %d\n", x + 1, last + 3);
add(x + 1, last + 3);
printf("%d %d\n", 2, last + 3 + n);
add(2, last + 3 + n);
}
}
}
int main() {
#ifdef LOCAL
double start_time = clock();
freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
if (0) {
int T;
scanf("%d", &T);
// AIN(T, 1, 25);
for (int ks = 1; ks <= T; ks++) {
solve(ks);
fprintf(stderr, "%d done\n", ks);
}
} else {
solve(0);
}
#ifdef LOCAL
double end_time = clock();
fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);
#endif
return 0;
}
| delete | 203 | 209 | 203 | 203 | TLE | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...) \
do { \
cerr << __LINE__ << ": "; \
dbgprint(#__VA_ARGS__, __VA_ARGS__); \
} while (0);
using namespace std;
namespace std {
template <class S, class T> struct hash<pair<S, T>> {
size_t operator()(const pair<S, T> &p) const {
return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
}
};
template <class T> struct hash<vector<T>> {
size_t operator()(const vector<T> &v) const {
size_t h = 0;
for (auto i : v)
h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
return h;
}
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[ ";
rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
os << "{ ";
for (const auto &i : v)
os << i << ", ";
return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
os << "{";
for (const auto &i : v)
os << " " << i.first << ": " << i.second << ",";
return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
int n;
cin >> n;
if (n < 3 || __builtin_popcount(n) == 1) {
cout << "No" << endl;
return 0;
}
assert(n % 4);
cout << "Yes" << endl;
vector<pi> ans;
ans.emplace_back(1, 2);
ans.emplace_back(2, 3);
ans.emplace_back(3, 1 + n);
ans.emplace_back(1 + n, 2 + n);
ans.emplace_back(2 + n, 3 + n);
int N = (n + 3) / 4 * 4;
if (n % 4 == 0) {
N--;
ans.emplace_back(n, n - 3);
ans.emplace_back(n ^ (n - 3) ^ 1, n + n);
}
for (int i = 4; i <= N; i += 4) {
if (i <= n)
ans.emplace_back(1, i);
if (i + 1 <= n)
ans.emplace_back(1, i + 1);
if (i + 1 <= n)
ans.emplace_back(i, i + 1 + n);
if (i + 1 <= n)
ans.emplace_back(i + 1, i + n);
if (i + 2 <= n)
ans.emplace_back(i + 1, i + 2 + n);
if (i + 2 <= n)
ans.emplace_back(2, i + 2);
if (i + 3 <= n)
ans.emplace_back(3, i + 3);
if (i + 3 <= n)
ans.emplace_back(i + 2, i + 3 + n);
}
int cnt = 0;
for (pi i : ans) {
cnt++;
cout << i.first << " " << i.second << endl;
}
dbg(n, cnt);
assert(cnt == 2 * n - 1);
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...) \
do { \
cerr << __LINE__ << ": "; \
dbgprint(#__VA_ARGS__, __VA_ARGS__); \
} while (0);
using namespace std;
namespace std {
template <class S, class T> struct hash<pair<S, T>> {
size_t operator()(const pair<S, T> &p) const {
return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
}
};
template <class T> struct hash<vector<T>> {
size_t operator()(const vector<T> &v) const {
size_t h = 0;
for (auto i : v)
h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
return h;
}
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[ ";
rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
os << "{ ";
for (const auto &i : v)
os << i << ", ";
return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
os << "{";
for (const auto &i : v)
os << " " << i.first << ": " << i.second << ",";
return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
int n;
cin >> n;
if (n < 3 || __builtin_popcount(n) == 1) {
cout << "No" << endl;
return 0;
}
// assert(n % 4);
cout << "Yes" << endl;
vector<pi> ans;
ans.emplace_back(1, 2);
ans.emplace_back(2, 3);
ans.emplace_back(3, 1 + n);
ans.emplace_back(1 + n, 2 + n);
ans.emplace_back(2 + n, 3 + n);
int N = (n + 3) / 4 * 4;
if (n % 4 == 0) {
N--;
ans.emplace_back(n, n - 3);
ans.emplace_back(n ^ (n - 3) ^ 1, n + n);
}
for (int i = 4; i <= N; i += 4) {
if (i <= n)
ans.emplace_back(1, i);
if (i + 1 <= n)
ans.emplace_back(1, i + 1);
if (i + 1 <= n)
ans.emplace_back(i, i + 1 + n);
if (i + 1 <= n)
ans.emplace_back(i + 1, i + n);
if (i + 2 <= n)
ans.emplace_back(i + 1, i + 2 + n);
if (i + 2 <= n)
ans.emplace_back(2, i + 2);
if (i + 3 <= n)
ans.emplace_back(3, i + 3);
if (i + 3 <= n)
ans.emplace_back(i + 2, i + 3 + n);
}
int cnt = 0;
for (pi i : ans) {
cnt++;
cout << i.first << " " << i.second << endl;
}
dbg(n, cnt);
assert(cnt == 2 * n - 1);
return 0;
} | replace | 71 | 72 | 71 | 72 | 0 | 58: n= 3 cnt= 5
|
p02977 | C++ | Runtime Error | #include <iostream>
using namespace std;
int n, par[100005];
int find(int x) {
if (par[x] != x)
par[x] = find(par[x]);
return par[x];
}
bool Union(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return 0;
par[x] = y;
return 1;
}
void add(int x, int y) {
if (Union(x, y))
printf("%d %d\n", x, y);
}
void one(int x, int y) {
add(x, y);
add(y, 1);
add(1, x + n);
add(x + n, y + n);
}
void bits(int x) {
int l = -1;
for (int i = 0; i < 18; i++) {
if (x & (1 << i)) {
if (l == -1)
add(x, (1 << i));
else
add((1 << l), (1 << i));
l = i;
}
}
add((1 << l), x + n);
}
int main() {
scanf("%d", &n);
if (!(n & (n - 1))) {
printf("No");
return 0;
}
printf("Yes\n");
for (int i = 1; i <= 2 * n; i++)
par[i] = i;
add(1, 2);
add(2, 3);
add(3, n + 1);
add(n + 1, n + 2);
add(n + 2, n + 3);
if (n % 4 == 0)
bits(n);
if (n % 4 == 1)
one(n - 1, n);
if (n % 4 == 2) {
add(n - 1, 1);
add(n, 3);
add(n - 2, n);
add(2 * n - 2, n + 1);
add(2 * n - 1, 2 * n - 2);
add(2 * n, 2 * n - 2);
}
for (int i = 4; i <= n - 3; i += 4) {
add(i, i + 1);
add(i + 1, i + 2);
add(i + 2, i + 3);
add(i + 3, n + i);
add(n + i, n + i + 1);
add(n + i + 1, n + i + 2);
add(n + i + 2, n + i + 3);
add(i, i - 4 + (i == 4));
}
} | #include <iostream>
using namespace std;
int n, par[200005];
int find(int x) {
if (par[x] != x)
par[x] = find(par[x]);
return par[x];
}
bool Union(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return 0;
par[x] = y;
return 1;
}
void add(int x, int y) {
if (Union(x, y))
printf("%d %d\n", x, y);
}
void one(int x, int y) {
add(x, y);
add(y, 1);
add(1, x + n);
add(x + n, y + n);
}
void bits(int x) {
int l = -1;
for (int i = 0; i < 18; i++) {
if (x & (1 << i)) {
if (l == -1)
add(x, (1 << i));
else
add((1 << l), (1 << i));
l = i;
}
}
add((1 << l), x + n);
}
int main() {
scanf("%d", &n);
if (!(n & (n - 1))) {
printf("No");
return 0;
}
printf("Yes\n");
for (int i = 1; i <= 2 * n; i++)
par[i] = i;
add(1, 2);
add(2, 3);
add(3, n + 1);
add(n + 1, n + 2);
add(n + 2, n + 3);
if (n % 4 == 0)
bits(n);
if (n % 4 == 1)
one(n - 1, n);
if (n % 4 == 2) {
add(n - 1, 1);
add(n, 3);
add(n - 2, n);
add(2 * n - 2, n + 1);
add(2 * n - 1, 2 * n - 2);
add(2 * n, 2 * n - 2);
}
for (int i = 4; i <= n - 3; i += 4) {
add(i, i + 1);
add(i + 1, i + 2);
add(i + 2, i + 3);
add(i + 3, n + i);
add(n + i, n + i + 1);
add(n + i + 1, n + i + 2);
add(n + i + 2, n + i + 3);
add(i, i - 4 + (i == 4));
}
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02977 | C++ | Runtime Error | #include "bits/stdc++.h"
#include <assert.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
const int inf = 1e17;
const int mod = 1000000007;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int cnt[200006];
int n;
void disp(vector<int> V) {
rep(i, V.size()) {
if (cnt[V[i]]) {
V[i] += n;
}
cnt[V[i]]++;
}
rep(i, V.size() - 1) cout << V[i] << ' ' << V[i + 1] << endl;
}
signed main() {
cin >> n;
rep(i, 20) {
if (n == (1 << i)) {
puts("No");
return 0;
}
}
puts("Yes");
vector<int> V;
for (int i = 3; i <= n; i += 4) {
rep(k, 2) {
rep(j, 4) {
if (i - 3 + j)
V.push_back(i - 3 + j);
}
}
}
if (n % 4 == 0) {
assert(0);
V.clear();
int k1, k2;
rep(i, 20) {
if (n & (1 << i)) {
k1 = 1 << i;
k2 = n - k1;
break;
}
}
rep(j, 2) rep(i, 4) V.push_back(k1 + 3 - i);
rep(j, 2) rep(i, 4) V.push_back(k2 + i);
for (int i = 0; i < n; i += 4) {
if (i != k1 && i != k2) {
rep(j, 2) {
rep(k, 4) {
if (i + k)
V.push_back(i + k);
}
}
}
}
disp(V);
cout << n << ' ' << k1 + n << endl;
cout << n + n << ' ' << k2 << endl;
} else if (n % 4 == 1) {
disp(V);
int k = n / 4 * 4;
cout << 1 << ' ' << k << endl;
cout << k << ' ' << k + 1 << endl;
cout << 1 << ' ' << n + k + 1 << endl;
cout << n + k << ' ' << n + k + 1 << endl;
} else if (n % 4 == 2) {
disp(V);
int k = n / 4 * 4;
cout << 1 << ' ' << k << endl;
cout << k << ' ' << k + 1 << endl;
cout << 1 << ' ' << k + n + 1 << endl;
cout << k + n + 1 << ' ' << k + 2 << endl;
cout << k + n + 1 << ' ' << k + n << endl;
cout << 2 << ' ' << k + n + 2 << endl;
} else {
disp(V);
}
}
| #include "bits/stdc++.h"
#include <assert.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
const int inf = 1e17;
const int mod = 1000000007;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int cnt[200006];
int n;
void disp(vector<int> V) {
rep(i, V.size()) {
if (cnt[V[i]]) {
V[i] += n;
}
cnt[V[i]]++;
}
rep(i, V.size() - 1) cout << V[i] << ' ' << V[i + 1] << endl;
}
signed main() {
cin >> n;
rep(i, 20) {
if (n == (1 << i)) {
puts("No");
return 0;
}
}
puts("Yes");
vector<int> V;
for (int i = 3; i <= n; i += 4) {
rep(k, 2) {
rep(j, 4) {
if (i - 3 + j)
V.push_back(i - 3 + j);
}
}
}
if (n % 4 == 0) {
// assert(0);
V.clear();
int k1, k2;
rep(i, 20) {
if (n & (1 << i)) {
k1 = 1 << i;
k2 = n - k1;
break;
}
}
rep(j, 2) rep(i, 4) V.push_back(k1 + 3 - i);
rep(j, 2) rep(i, 4) V.push_back(k2 + i);
for (int i = 0; i < n; i += 4) {
if (i != k1 && i != k2) {
rep(j, 2) {
rep(k, 4) {
if (i + k)
V.push_back(i + k);
}
}
}
}
disp(V);
cout << n << ' ' << k1 + n << endl;
cout << n + n << ' ' << k2 << endl;
} else if (n % 4 == 1) {
disp(V);
int k = n / 4 * 4;
cout << 1 << ' ' << k << endl;
cout << k << ' ' << k + 1 << endl;
cout << 1 << ' ' << n + k + 1 << endl;
cout << n + k << ' ' << n + k + 1 << endl;
} else if (n % 4 == 2) {
disp(V);
int k = n / 4 * 4;
cout << 1 << ' ' << k << endl;
cout << k << ' ' << k + 1 << endl;
cout << 1 << ' ' << k + n + 1 << endl;
cout << k + n + 1 << ' ' << k + 2 << endl;
cout << k + n + 1 << ' ' << k + n << endl;
cout << 2 << ' ' << k + n + 2 << endl;
} else {
disp(V);
}
}
| replace | 44 | 45 | 44 | 45 | 0 | |
p02977 | C++ | Runtime Error | #include <cstdio>
int main() {
int N;
scanf("%d", &N);
if (!(N & (N - 1)))
return 1;
puts("No");
} | #include <cstdio>
int main() {
int N;
scanf("%d", &N);
if (!(N & (N - 1)))
return puts("No"), 0;
puts("Yes");
printf("%d %d\n", N + 1, 3);
for (int i = 2; i < N; i += 2)
printf("%d %d\n%d %d\n%d %d\n%d %d\n", i + 1, i, i, 1, 1, N + i + 1,
N + i + 1, N + i);
if (N % 2 == 0)
for (int i = 2; i < N; ++i)
if ((i ^ N ^ 1) >= 2 && (i ^ N ^ 1) < N) {
printf("%d %d\n%d %d\n", i % 2 == 0 ? i : N + i, N,
(i ^ N ^ 1) % 2 == 0 ? i ^ N ^ 1 : N + (i ^ N ^ 1), 2 * N);
break;
}
return 0;
}
| replace | 6 | 8 | 6 | 20 | 0 | |
p02977 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define prev prev228
#define all(x) (x).begin(), (x).end()
vector<pair<int, int>> edges;
int main() {
int n;
cin >> n;
if (__builtin_popcount(n) == 1) {
cout << "No";
return 0;
}
cout << "Yes\n";
if (n % 4 == 3) {
edges.push_back({1, 3});
edges.push_back({3, 2});
edges.push_back({2, n + 1});
edges.push_back({n + 1, n + 3});
edges.push_back({n + 3, n + 2});
int lst = n + 2;
for (int i = 4; i + 3 <= n; i += 4) {
edges.push_back({lst, i});
edges.push_back({i, i + 1});
edges.push_back({i + 1, i + 2});
edges.push_back({i + 2, i + 3});
edges.push_back({i + 3, n + i});
edges.push_back({n + i, n + i + 1});
edges.push_back({n + i + 1, n + i + 2});
edges.push_back({n + i + 2, n + i + 3});
lst = n + i + 3;
}
} else {
edges.push_back({1, 3});
edges.push_back({3, 2});
edges.push_back({2, n + 1});
edges.push_back({n + 1, n + 3});
edges.push_back({n + 3, n + 2});
int t = 0;
int N = (n / 4) * 4;
for (int k = 0; k < 30; k++) {
if (N & (1 << k)) {
t = (1 << k);
break;
}
}
int v1 = t;
int v2 = N ^ t;
int lst = -1;
for (int i = 4; i + 3 <= n; i += 4) {
if (i == v2)
lst = v1;
if (lst != -1)
edges.push_back({lst, i});
edges.push_back({i, i + 1});
edges.push_back({i + 1, i + 2});
edges.push_back({i + 2, i + 3});
edges.push_back({i + 3, n + i});
edges.push_back({n + i, n + i + 1});
edges.push_back({n + i + 1, n + i + 2});
edges.push_back({n + i + 2, n + i + 3});
lst = n + i + 3;
}
edges.push_back({v1, N});
edges.push_back({v2, n + N});
edges.push_back({N, 1});
if (n % 4 >= 1) {
edges.push_back({N + 1 + n, N});
edges.push_back({1, N + 1});
}
if (n % 4 >= 2) {
edges.push_back({N + 2 + n, N});
edges.push_back({3, N + 2});
}
}
if (edges.size() + 1 != 2 * n) {
return 1;
}
for (auto e : edges)
cout << e.first << " " << e.second << endl;
}
| #include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define prev prev228
#define all(x) (x).begin(), (x).end()
vector<pair<int, int>> edges;
int main() {
int n;
cin >> n;
if (__builtin_popcount(n) == 1) {
cout << "No";
return 0;
}
cout << "Yes\n";
if (n % 4 == 3) {
edges.push_back({1, 3});
edges.push_back({3, 2});
edges.push_back({2, n + 1});
edges.push_back({n + 1, n + 3});
edges.push_back({n + 3, n + 2});
int lst = n + 2;
for (int i = 4; i + 3 <= n; i += 4) {
edges.push_back({lst, i});
edges.push_back({i, i + 1});
edges.push_back({i + 1, i + 2});
edges.push_back({i + 2, i + 3});
edges.push_back({i + 3, n + i});
edges.push_back({n + i, n + i + 1});
edges.push_back({n + i + 1, n + i + 2});
edges.push_back({n + i + 2, n + i + 3});
lst = n + i + 3;
}
} else {
edges.push_back({1, 3});
edges.push_back({3, 2});
edges.push_back({2, n + 1});
edges.push_back({n + 1, n + 3});
edges.push_back({n + 3, n + 2});
int t = 0;
int N = (n / 4) * 4;
for (int k = 0; k < 30; k++) {
if (N & (1 << k)) {
t = (1 << k);
break;
}
}
int v1 = t;
int v2 = N ^ t;
int lst = -1;
for (int i = 4; i + 3 <= n; i += 4) {
if (i == v2)
lst = v1;
if (lst != -1)
edges.push_back({lst, i});
edges.push_back({i, i + 1});
edges.push_back({i + 1, i + 2});
edges.push_back({i + 2, i + 3});
edges.push_back({i + 3, n + i});
edges.push_back({n + i, n + i + 1});
edges.push_back({n + i + 1, n + i + 2});
edges.push_back({n + i + 2, n + i + 3});
lst = n + i + 3;
}
edges.push_back({v1, N});
edges.push_back({v2, n + N});
edges.push_back({N, 1});
if (n % 4 >= 1) {
edges.push_back({N + 1 + n, N});
edges.push_back({1, N + 1});
}
if (n % 4 >= 2) {
edges.push_back({N + 2 + n, N});
edges.push_back({3, N + 2});
}
}
if (n == 5) {
edges.clear();
edges.push_back({1, 2});
edges.push_back({3, 2});
edges.push_back({3, n + 1});
edges.push_back({n + 1, n + 2});
edges.push_back({n + 3, n + 2});
edges.push_back({4, 1});
edges.push_back({5, 4});
edges.push_back({5 + n, 1});
edges.push_back({5 + n, 4 + n});
}
if (n == 6) {
edges.clear();
edges.push_back({1, 2});
edges.push_back({3, 2});
edges.push_back({3, n + 1});
edges.push_back({n + 1, n + 2});
edges.push_back({n + 3, n + 2});
edges.push_back({4, 1});
edges.push_back({5, 4});
edges.push_back({5 + n, 1});
edges.push_back({5 + n, 4 + n});
edges.push_back({6, 2});
edges.push_back({n + 6, 5 + n});
}
if (n == 9) {
edges.clear();
edges.push_back({1, 2});
edges.push_back({3, 2});
edges.push_back({3, n + 1});
edges.push_back({n + 1, n + 2});
edges.push_back({n + 3, n + 2});
int lst = n + 2;
for (int i = 4; i + 3 <= 7; i += 4) {
edges.push_back({lst, i});
edges.push_back({i, i + 1});
edges.push_back({i + 1, i + 2});
edges.push_back({i + 2, i + 3});
edges.push_back({i + 3, n + i});
edges.push_back({n + i, n + i + 1});
edges.push_back({n + i + 1, n + i + 2});
edges.push_back({n + i + 2, n + i + 3});
lst = n + i + 3;
}
edges.push_back({9, 1});
edges.push_back({8, 9});
edges.push_back({1, n + 8});
edges.push_back({n + 9, n + 8});
}
if (n == 10) {
edges.clear();
edges.push_back({1, 2});
edges.push_back({3, 2});
edges.push_back({3, n + 1});
edges.push_back({n + 1, n + 2});
edges.push_back({n + 3, n + 2});
int lst = n + 2;
for (int i = 4; i + 3 <= 7; i += 4) {
edges.push_back({lst, i});
edges.push_back({i, i + 1});
edges.push_back({i + 1, i + 2});
edges.push_back({i + 2, i + 3});
edges.push_back({i + 3, n + i});
edges.push_back({n + i, n + i + 1});
edges.push_back({n + i + 1, n + i + 2});
edges.push_back({n + i + 2, n + i + 3});
lst = n + i + 3;
}
edges.push_back({9, 1});
edges.push_back({8, 9});
edges.push_back({1, n + 8});
edges.push_back({n + 9, n + 8});
edges.push_back({10, 9});
edges.push_back({n + 10, 2});
}
for (auto e : edges)
cout << e.first << " " << e.second << endl;
}
| replace | 81 | 83 | 81 | 156 | 0 | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound
signed main() {
int N;
cin >> N;
int a = 1;
rep(i, 20) {
if (N == a) {
cout << "No" << endl;
return 0;
}
a *= 2;
}
return 1;
}
| #include <bits/stdc++.h>
#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound
signed main() {
int N;
cin >> N;
int a = 1;
rep(i, 20) {
if (N == a) {
cout << "No" << endl;
return 0;
}
a *= 2;
}
if (N % 2 == 1) {
cout << "Yes" << endl;
rep(i, N / 2) {
int a = 2 * i + 2;
int b = 2 * i + 3;
cout << 1 << " " << a << endl;
cout << a << " " << b << endl;
cout << 1 << " " << b + N << endl;
cout << b + N << " " << a + N << endl;
}
cout << 3 << " " << 1 + N << endl;
} else {
cout << "Yes" << endl;
rep(i, (N - 1) / 2) {
int a = 2 * i + 2;
int b = 2 * i + 3;
cout << 1 << " " << a << endl;
cout << a << " " << b << endl;
cout << 1 << " " << b + N << endl;
cout << b + N << " " << a + N << endl;
}
cout << 3 << " " << 1 + N << endl;
// Nと2Nの配置
int n2 = N & -N;
int n1 = N - n2;
cout << n1 << " " << N << endl;
cout << n2 + 1 + N << " " << 2 * N << endl;
}
return 0;
}
| replace | 21 | 22 | 21 | 50 | 1 | |
p02977 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
#define all(c) begin(c), end(c)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
// 改造
typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 // デバッグする時1にしてね
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007
// ここから編集する
class Solve {
public:
void solve() {
int N;
cin >> N;
if (N <= 2 || N == 4) {
cout << "No" << endl;
return;
}
int bsize = (N + 1) / 4 * 4 - 1;
vector<int> base;
for (int i = 0; i + 3 <= bsize; i += 4) {
for (int j = 0; j < 4; ++j) {
if (i + j > 0)
base.push_back(i + j);
}
for (int j = 0; j < 4; ++j) {
if (i + j > 0)
base.push_back(i + j + N);
}
}
if (N % 4 != 0) {
for (int i = 0; i < 6; ++i) {
base.push_back(*base.begin());
base.erase(base.begin());
}
} else {
int target = N ^ (N - 1);
for (int val = target; val >= target - 3; --val) {
auto pt = find(all(base), val);
base.erase(pt);
base.push_back(val);
}
for (int val = target + N; val >= target - 3 + N; --val) {
auto pt = find(all(base), val);
base.erase(pt);
base.push_back(val);
}
}
vector<pair<int, int>> edges;
for (int i = 0; i + 1 < base.size(); ++i) {
edges.emplace_back(base[i], base[i + 1]);
}
int k = N / 4;
if (N % 4 == 0) {
if (__builtin_popcount(N) == 1) {
cout << "No" << endl;
return;
}
int target = N ^ (N - 1);
edges.emplace_back(2 * N - 1, 2 * N);
edges.emplace_back(target, N);
} else if (N % 4 != 3) {
edges.emplace_back(1, 4 * k + 1);
edges.emplace_back(4 * k + 1, 4 * k);
edges.emplace_back(1, 4 * k + N);
edges.emplace_back(4 * k + N, 4 * k + 1 + N);
if (N % 4 == 2) {
edges.emplace_back(2, 4 * k + 2);
edges.emplace_back(4 * k + 1, 4 * k + 2 + N);
}
}
cout << "Yes" << endl;
for (auto edge : edges) {
cout << edge.first << " " << edge.second << endl;
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
Solve().solve();
return 0;
}
| #include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
#define all(c) begin(c), end(c)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
// 改造
typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 // デバッグする時1にしてね
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007
// ここから編集する
class Solve {
public:
void solve() {
int N;
cin >> N;
if (N <= 2 || N == 4) {
cout << "No" << endl;
return;
}
int bsize = (N + 1) / 4 * 4 - 1;
vector<int> base;
for (int i = 0; i + 3 <= bsize; i += 4) {
for (int j = 0; j < 4; ++j) {
if (i + j > 0)
base.push_back(i + j);
}
for (int j = 0; j < 4; ++j) {
if (i + j > 0)
base.push_back(i + j + N);
}
}
if (N % 4 != 0) {
for (int i = 0; i < 6; ++i) {
base.push_back(*base.begin());
base.erase(base.begin());
}
} else {
int target = N ^ (N - 1);
if (target > N) {
cout << "No" << endl;
return;
}
for (int val = target; val >= target - 3; --val) {
auto pt = find(all(base), val);
base.erase(pt);
base.push_back(val);
}
for (int val = target + N; val >= target - 3 + N; --val) {
auto pt = find(all(base), val);
base.erase(pt);
base.push_back(val);
}
}
vector<pair<int, int>> edges;
for (int i = 0; i + 1 < base.size(); ++i) {
edges.emplace_back(base[i], base[i + 1]);
}
int k = N / 4;
if (N % 4 == 0) {
if (__builtin_popcount(N) == 1) {
cout << "No" << endl;
return;
}
int target = N ^ (N - 1);
edges.emplace_back(2 * N - 1, 2 * N);
edges.emplace_back(target, N);
} else if (N % 4 != 3) {
edges.emplace_back(1, 4 * k + 1);
edges.emplace_back(4 * k + 1, 4 * k);
edges.emplace_back(1, 4 * k + N);
edges.emplace_back(4 * k + N, 4 * k + 1 + N);
if (N % 4 == 2) {
edges.emplace_back(2, 4 * k + 2);
edges.emplace_back(4 * k + 1, 4 * k + 2 + N);
}
}
cout << "Yes" << endl;
for (auto edge : edges) {
cout << edge.first << " " << edge.second << endl;
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
Solve().solve();
return 0;
}
| insert | 91 | 91 | 91 | 95 | 0 | |
p02977 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define next Next
#define last Last
const int N = 1e5 + 10;
int n, Low[N];
struct node {
int x, y;
};
vector<node> g;
/*char buf[1<<21],*p1=buf,*p2=buf;
inline int gc(){return
p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}*/
#define gc getchar
inline int read() {
int ret = 0, f = 0;
char c = gc();
while (!isdigit(c)) {
if (c == '-')
f = 1;
c = gc();
}
while (isdigit(c)) {
ret = ret * 10 + c - 48;
c = gc();
}
if (f)
return -ret;
return ret;
}
signed main() {
freopen("toad.in", "r", stdin);
freopen("toad.out", "w", stdout);
Low[0] = -1;
for (int i = 1; i <= 100001; i++)
Low[i] = Low[i / 2] + 1;
n = read();
if (n == 1) {
puts("No");
return 0;
}
if ((1 << Low[n]) == n) {
puts("No");
return 0;
}
puts("Yes");
int m = 2;
for (int i = m; i < n; i += 2) {
g.push_back((node){1, i});
g.push_back((node){i, i + 1});
g.push_back((node){1, n + i + 1});
g.push_back((node){n + i + 1, n + i});
}
g.push_back((node){3, n + 1});
if (n % 2 == 0) {
g.push_back((node){n, (1 << Low[n])});
g.push_back((node){(n + n - (1 << Low[n])) ^ 1, n + n});
}
for (int i = 0; i < g.size(); i++)
printf("%d %d\n", g[i].x, g[i].y);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define next Next
#define last Last
const int N = 1e5 + 10;
int n, Low[N];
struct node {
int x, y;
};
vector<node> g;
/*char buf[1<<21],*p1=buf,*p2=buf;
inline int gc(){return
p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}*/
#define gc getchar
inline int read() {
int ret = 0, f = 0;
char c = gc();
while (!isdigit(c)) {
if (c == '-')
f = 1;
c = gc();
}
while (isdigit(c)) {
ret = ret * 10 + c - 48;
c = gc();
}
if (f)
return -ret;
return ret;
}
signed main() {
Low[0] = -1;
for (int i = 1; i <= 100001; i++)
Low[i] = Low[i / 2] + 1;
n = read();
if (n == 1) {
puts("No");
return 0;
}
if ((1 << Low[n]) == n) {
puts("No");
return 0;
}
puts("Yes");
int m = 2;
for (int i = m; i < n; i += 2) {
g.push_back((node){1, i});
g.push_back((node){i, i + 1});
g.push_back((node){1, n + i + 1});
g.push_back((node){n + i + 1, n + i});
}
g.push_back((node){3, n + 1});
if (n % 2 == 0) {
g.push_back((node){n, (1 << Low[n])});
g.push_back((node){(n + n - (1 << Low[n])) ^ 1, n + n});
}
for (int i = 0; i < g.size(); i++)
printf("%d %d\n", g[i].x, g[i].y);
return 0;
} | delete | 31 | 33 | 31 | 31 | TLE | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
// #pragma GCC optimize("no-stack-protector,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugp(x) \
cerr << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define debug2(x, y) \
cerr << "{" << #x << ", " << #y << "} = {" << (x) << ", " << (y) << "}" \
<< endl;
#define debugv(v) \
{ \
cerr << #v << " : "; \
for (auto x : v) \
cerr << x << ' '; \
cerr << endl; \
}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout << x << '\n', 0;
const ld eps = 1e-7;
const int inf = 1000000010;
const ll INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010, LOG = 20;
int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n;
if ((n & -n) == n)
kill("No") cout << "Yes\n";
cout << 1 << ' ' << 2 << '\n';
cout << 2 << ' ' << 3 << '\n';
cout << 3 << ' ' << 1 + n << '\n';
cout << 1 + n << ' ' << 2 + n << '\n';
cout << 2 + n << ' ' << 3 + n << '\n';
for (int i = 5; i <= n; i += 2) {
cout << "1 " << i - 1 << '\n';
cout << "1 " << i << '\n';
cout << i - 1 << ' ' << i + n << '\n';
cout << i << ' ' << i - 1 + n << '\n';
}
if (n % 2 == 0) {
cout << 1 / 0;
cout << 1 << ' ' << n + n << '\n';
cout << n - 2 << ' ' << n << '\n';
}
return 0;
}
| #include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
// #pragma GCC optimize("no-stack-protector,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugp(x) \
cerr << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define debug2(x, y) \
cerr << "{" << #x << ", " << #y << "} = {" << (x) << ", " << (y) << "}" \
<< endl;
#define debugv(v) \
{ \
cerr << #v << " : "; \
for (auto x : v) \
cerr << x << ' '; \
cerr << endl; \
}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout << x << '\n', 0;
const ld eps = 1e-7;
const int inf = 1000000010;
const ll INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010, LOG = 20;
int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n;
if ((n & -n) == n)
kill("No") cout << "Yes\n";
cout << 1 << ' ' << 2 << '\n';
cout << 2 << ' ' << 3 << '\n';
cout << 3 << ' ' << 1 + n << '\n';
cout << 1 + n << ' ' << 2 + n << '\n';
cout << 2 + n << ' ' << 3 + n << '\n';
for (int i = 5; i <= n; i += 2) {
cout << "1 " << i - 1 << '\n';
cout << "1 " << i << '\n';
cout << i - 1 << ' ' << i + n << '\n';
cout << i << ' ' << i - 1 + n << '\n';
}
if (n % 2 == 0) {
int lb = (n & -n);
cout << lb << ' ' << n + n << '\n';
cout << (n ^ lb ^ 1) << ' ' << n << '\n';
}
return 0;
}
| replace | 58 | 61 | 58 | 61 | 0 | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int n;
int main() {
ios::sync_with_stdio(false);
cin >> n;
if (__builtin_popcount(n) == 1) {
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
cout << 1 << ' ' << 2 << endl;
cout << 2 << ' ' << 3 << endl;
cout << 3 << ' ' << n + 1 << endl;
cout << n + 1 << ' ' << n + 2 << endl;
cout << n + 2 << ' ' << n + 3 << endl;
for (int i = 4; i < n; i += 2) {
cout << n + 1 << ' ' << i << endl;
cout << n + 1 << ' ' << i + n + 1 << endl;
cout << i << ' ' << i + 1 << endl;
cout << i + n + 1 << ' ' << i + n << endl;
}
if (n % 2 == 0) {
return 1;
cout << 3 << ' ' << n << endl;
int to = (n ^ 3 ^ 1);
if (to & 1)
to += n;
cout << 2 * n << ' ' << to << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int n;
int main() {
ios::sync_with_stdio(false);
cin >> n;
if (__builtin_popcount(n) == 1) {
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
cout << 1 << ' ' << 2 << endl;
cout << 2 << ' ' << 3 << endl;
cout << 3 << ' ' << n + 1 << endl;
cout << n + 1 << ' ' << n + 2 << endl;
cout << n + 2 << ' ' << n + 3 << endl;
for (int i = 4; i < n; i += 2) {
cout << n + 1 << ' ' << i << endl;
cout << n + 1 << ' ' << i + n + 1 << endl;
cout << i << ' ' << i + 1 << endl;
cout << i + n + 1 << ' ' << i + n << endl;
}
if (n % 2 == 0) {
for (int i = 2; i <= n; i++) {
int u = (i == 2 || i % 2 == 1) ? i + n : i;
if ((n ^ i ^ 1) < n) {
int v = ((n ^ i ^ 1) == 2 || (n ^ i ^ 1) % 2 == 1) ? (n ^ i ^ 1) + n
: (n ^ i ^ 1);
cout << u << ' ' << n << endl;
cout << 2 * n << ' ' << v << endl;
break;
}
}
}
return 0;
}
| replace | 26 | 32 | 26 | 36 | 0 | |
p02977 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int mod = 1000000007;
typedef long long LL;
int n, m, u[N], v[N], du[N], a[N];
set<int> G[N];
set<pair<int, int>> s;
pair<int, int> ans[N];
int main() {
#ifdef TEST
freopen("input.txt", "r", stdin);
#endif
scanf("%d", &n);
int fst = 1;
while (fst < n)
fst <<= 1;
if (fst == n) {
printf("No\n");
return 0;
}
printf("Yes\n");
printf("1 2\n2 3\n3 %d\n", n + 1);
printf("%d 1\n%d %d\n", n + 3, n + 3, n + 2);
if (n & 1) {
for (int i = 4; i <= n; i += 2) {
printf("%d %d\n", n + i, n + i + 1);
printf("%d %d\n", n + i + 1, 3);
printf("%d %d\n", i, 2);
printf("%d %d\n", i + 1, i);
}
} else {
int t = n;
fst = fst >> 1;
vector<int> q;
while (t) {
if (n >= fst) {
q.push_back(fst);
t -= fst;
}
fst >>= 1;
}
reverse(q.begin(), q.end());
int head = q[0] == 2;
for (int i = 4; i < n; i += 2) {
if (head < q.size() && q[head] == i) {
int pre = head == 0 ? 2 : q[head - 1];
printf("%d %d\n", pre, i);
printf("%d %d\n", i, i + 1);
printf("%d %d\n", pre + 1, n + i + 1);
printf("%d %d\n", n + i + 1, n + i);
head++;
} else {
printf("%d %d\n", n + i, n + i + 1);
printf("%d %d\n", n + i + 1, 3);
printf("%d %d\n", i, 2);
printf("%d %d\n", i + 1, i);
}
}
printf("%d %d\n", q[0], n);
printf("%d %d\n", q[head - 1], n + n);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int mod = 1000000007;
typedef long long LL;
int n, m, u[N], v[N], du[N], a[N];
set<int> G[N];
set<pair<int, int>> s;
pair<int, int> ans[N];
int main() {
#ifdef TEST
freopen("input.txt", "r", stdin);
#endif
scanf("%d", &n);
int fst = 1;
while (fst < n)
fst <<= 1;
if (fst == n) {
printf("No\n");
return 0;
}
printf("Yes\n");
printf("1 2\n2 3\n3 %d\n", n + 1);
printf("%d 1\n%d %d\n", n + 3, n + 3, n + 2);
if (n & 1) {
for (int i = 4; i <= n; i += 2) {
printf("%d %d\n", n + i, n + i + 1);
printf("%d %d\n", n + i + 1, 3);
printf("%d %d\n", i, 2);
printf("%d %d\n", i + 1, i);
}
} else {
int t = n;
fst = fst >> 1;
vector<int> q;
while (t) {
if (t >= fst) {
q.push_back(fst);
t -= fst;
}
fst >>= 1;
}
reverse(q.begin(), q.end());
int head = q[0] == 2;
for (int i = 4; i < n; i += 2) {
if (head < q.size() && q[head] == i) {
int pre = head == 0 ? 2 : q[head - 1];
printf("%d %d\n", pre, i);
printf("%d %d\n", i, i + 1);
printf("%d %d\n", pre + 1, n + i + 1);
printf("%d %d\n", n + i + 1, n + i);
head++;
} else {
printf("%d %d\n", n + i, n + i + 1);
printf("%d %d\n", n + i + 1, 3);
printf("%d %d\n", i, 2);
printf("%d %d\n", i + 1, i);
}
}
printf("%d %d\n", q[0], n);
printf("%d %d\n", q[head - 1], n + n);
}
return 0;
} | replace | 36 | 37 | 36 | 37 | TLE | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
int main() {
using namespace std;
size_t N;
cin >> N;
if (__builtin_popcountll(N) == 1)
return 0 & puts("No");
puts("Yes\n1 2");
for (unsigned long i = 2; i < N; i += 2)
cout << i << " " << i + 1 << "\n"
<< i + 1 << " " << N + 1 << "\n"
<< N + 1 << " " << N + i << "\n"
<< N + i << " " << N + i + 1 << endl;
if (~N & 1) {
assert(0);
cout << N << " " << N + 2 << "\n" << (3 ^ N) << " " << 2 * N << endl;
}
return 0;
} | #include <bits/stdc++.h>
int main() {
using namespace std;
size_t N;
cin >> N;
if (__builtin_popcountll(N) == 1)
return 0 & puts("No");
puts("Yes\n1 2");
for (unsigned long i = 2; i < N; i += 2)
cout << i << " " << i + 1 << "\n"
<< i + 1 << " " << N + 1 << "\n"
<< N + 1 << " " << N + i << "\n"
<< N + i << " " << N + i + 1 << endl;
if (N % 2 == 0)
cout << N << " " << N + (N & -N) << "\n"
<< ((N & -N) ^ 1 ^ N) << " " << 2 * N << endl;
return 0;
} | replace | 14 | 18 | 14 | 17 | 0 | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------
int N;
vector<pair<int, int>> E;
vector<int> EE[101010];
map<int, int> M;
void add(int x, int y) {
E.push_back({x, y});
EE[x].push_back(y);
EE[y].push_back(x);
}
void dfs(int cur, int pre, int v) {
if (cur > N)
v ^= (cur - N);
else
v ^= cur;
M[v] = cur;
FORR(e, EE[cur]) if (e != pre) dfs(e, cur, v);
}
void solve() {
int i, j, k, l, r, x, y;
string s;
cin >> N;
if ((N & (N - 1)) == 0)
return _P("No\n");
x = 1;
while (x * 2 <= N)
x *= 2;
cout << "Yes" << endl;
if (N + 1 == x * 2) {
for (i = 1; i < x; i++) {
add(i, i + x);
add(i + x, x);
add(x, i + N);
add(i + N, i + x + N);
}
add(1, x + N);
} else {
y = x / 2;
for (i = 1; i < y; i++) {
add(i, i + y);
add(i + y, y);
add(y, i + N);
add(i + N, i + y + N);
}
add(1, y + N);
add(x, x + 1);
add(x + 1, 1);
add(1, x + N);
add(x + N, x + 1 + N);
dfs(x + N, -1, 0);
for (i = x + 2; i <= N; i++) {
add(i, x + N);
assert(M.count(i));
add(i + N, M[i]);
}
}
FORR(e, E) cout << e.first << " " << e.second << endl;
}
int main(int argc, char **argv) {
string s;
int i;
if (argc == 1)
ios::sync_with_stdio(false), cin.tie(0);
FOR(i, argc - 1) s += argv[i + 1], s += '\n';
FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------
int N;
vector<pair<int, int>> E;
vector<int> EE[201010];
map<int, int> M;
void add(int x, int y) {
E.push_back({x, y});
EE[x].push_back(y);
EE[y].push_back(x);
}
void dfs(int cur, int pre, int v) {
if (cur > N)
v ^= (cur - N);
else
v ^= cur;
M[v] = cur;
FORR(e, EE[cur]) if (e != pre) dfs(e, cur, v);
}
void solve() {
int i, j, k, l, r, x, y;
string s;
cin >> N;
if ((N & (N - 1)) == 0)
return _P("No\n");
x = 1;
while (x * 2 <= N)
x *= 2;
cout << "Yes" << endl;
if (N + 1 == x * 2) {
for (i = 1; i < x; i++) {
add(i, i + x);
add(i + x, x);
add(x, i + N);
add(i + N, i + x + N);
}
add(1, x + N);
} else {
y = x / 2;
for (i = 1; i < y; i++) {
add(i, i + y);
add(i + y, y);
add(y, i + N);
add(i + N, i + y + N);
}
add(1, y + N);
add(x, x + 1);
add(x + 1, 1);
add(1, x + N);
add(x + N, x + 1 + N);
dfs(x + N, -1, 0);
for (i = x + 2; i <= N; i++) {
add(i, x + N);
assert(M.count(i));
add(i + N, M[i]);
}
}
FORR(e, E) cout << e.first << " " << e.second << endl;
}
int main(int argc, char **argv) {
string s;
int i;
if (argc == 1)
ios::sync_with_stdio(false), cin.tie(0);
FOR(i, argc - 1) s += argv[i + 1], s += '\n';
FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p02977 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
void Check(int n, vector<pair<int, int>> sol) {
// return;
assert(sol.size() + 1 == 2 * n);
for (int i = 1; i <= n; ++i) {
vector<int> vis(2 * n + 1, -1);
auto val = [&](int node) {
if (node > n)
return node - n;
else
return node;
};
vis[i] = val(i);
while (true) {
bool ch = false;
for (auto p : sol) {
if (vis[p.first] == -1)
swap(p.first, p.second);
if (vis[p.first] == -1 || vis[p.second] != -1)
continue;
vis[p.second] = val(p.second) ^ vis[p.first];
ch = true;
}
if (!ch)
break;
}
// cerr << i << endl;
// cerr << "!!!" << vis[n + i] << endl;
assert(vis[n + i] == i);
}
}
int main() {
int n;
cin >> n;
if (n < 3) {
cout << "No\n";
return 0;
}
int m = 1;
while (m <= n)
m *= 2;
if (m > n + 1)
m /= 2;
assert(m <= n + 1);
if (m == n) {
cout << "No\n";
} else {
if (n % 2 == 1) {
cout << "Yes\n";
vector<pair<int, int>> sol;
for (int i = 1; i + 1 < m; ++i) {
sol.emplace_back(i, i + 1);
sol.emplace_back(n + i, n + i + 1);
}
sol.emplace_back(m - 1, n + 1);
if (m <= n) {
int xor_sum = 0;
for (int i = m; i < n; ++i) {
sol.emplace_back(i, i + 1);
sol.emplace_back(i + n, i + n + 1);
xor_sum ^= i;
}
xor_sum ^= n;
if (xor_sum == 0) {
sol.emplace_back(n, 1);
sol.emplace_back(n, m + n);
} else {
assert(xor_sum < m);
sol.emplace_back(n, xor_sum);
sol.emplace_back(xor_sum, m + n);
}
}
Check(n, sol);
for (auto x : sol) {
cout << x.first << " " << x.second << '\n';
}
} else {
vector<pair<int, int>> sol;
assert(m + 2 <= n);
sol.emplace_back(m + 2, 2);
sol.emplace_back(2, m);
sol.emplace_back(m, m + 1);
sol.emplace_back(m, 1);
sol.emplace_back(1, m + n + 1);
sol.emplace_back(m + n + 1, m + n);
sol.emplace_back(m + n, m + n + 2);
sol.emplace_back(m + n, n + 1);
sol.emplace_back(n + 1, n + 3);
sol.emplace_back(m + n + 2, n + 2);
sol.emplace_back(m + n + 2, 3);
for (int b = 4, e = 8; e <= m; b *= 2, e *= 2) {
for (int i = b; i + 1 < e; ++i) {
sol.emplace_back(i, i + 1);
sol.emplace_back(i + n, i + n + 1);
}
sol.emplace_back(e - 1, b + n);
sol.emplace_back(b, 1);
}
if (m + 2 < n) {
int xor_sum = 0;
for (int i = m + 3; i <= n; ++i)
xor_sum ^= i;
for (int i = m + 3; i < n; ++i) {
sol.emplace_back(i, i + 1);
sol.emplace_back(i + n, i + n + 1);
}
if (xor_sum == 0) {
sol.emplace_back(n, m + n + 3);
sol.emplace_back(n, 1);
} else {
assert(xor_sum < m);
sol.emplace_back(n, xor_sum);
sol.emplace_back(xor_sum, m + n + 3);
}
}
Check(n, sol);
cout << "Yes\n";
for (auto x : sol) {
cout << x.first << " " << x.second << '\n';
}
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
void Check(int n, vector<pair<int, int>> sol) {
return;
assert(sol.size() + 1 == 2 * n);
for (int i = 1; i <= n; ++i) {
vector<int> vis(2 * n + 1, -1);
auto val = [&](int node) {
if (node > n)
return node - n;
else
return node;
};
vis[i] = val(i);
while (true) {
bool ch = false;
for (auto p : sol) {
if (vis[p.first] == -1)
swap(p.first, p.second);
if (vis[p.first] == -1 || vis[p.second] != -1)
continue;
vis[p.second] = val(p.second) ^ vis[p.first];
ch = true;
}
if (!ch)
break;
}
// cerr << i << endl;
// cerr << "!!!" << vis[n + i] << endl;
assert(vis[n + i] == i);
}
}
int main() {
int n;
cin >> n;
if (n < 3) {
cout << "No\n";
return 0;
}
int m = 1;
while (m <= n)
m *= 2;
if (m > n + 1)
m /= 2;
assert(m <= n + 1);
if (m == n) {
cout << "No\n";
} else {
if (n % 2 == 1) {
cout << "Yes\n";
vector<pair<int, int>> sol;
for (int i = 1; i + 1 < m; ++i) {
sol.emplace_back(i, i + 1);
sol.emplace_back(n + i, n + i + 1);
}
sol.emplace_back(m - 1, n + 1);
if (m <= n) {
int xor_sum = 0;
for (int i = m; i < n; ++i) {
sol.emplace_back(i, i + 1);
sol.emplace_back(i + n, i + n + 1);
xor_sum ^= i;
}
xor_sum ^= n;
if (xor_sum == 0) {
sol.emplace_back(n, 1);
sol.emplace_back(n, m + n);
} else {
assert(xor_sum < m);
sol.emplace_back(n, xor_sum);
sol.emplace_back(xor_sum, m + n);
}
}
Check(n, sol);
for (auto x : sol) {
cout << x.first << " " << x.second << '\n';
}
} else {
vector<pair<int, int>> sol;
assert(m + 2 <= n);
sol.emplace_back(m + 2, 2);
sol.emplace_back(2, m);
sol.emplace_back(m, m + 1);
sol.emplace_back(m, 1);
sol.emplace_back(1, m + n + 1);
sol.emplace_back(m + n + 1, m + n);
sol.emplace_back(m + n, m + n + 2);
sol.emplace_back(m + n, n + 1);
sol.emplace_back(n + 1, n + 3);
sol.emplace_back(m + n + 2, n + 2);
sol.emplace_back(m + n + 2, 3);
for (int b = 4, e = 8; e <= m; b *= 2, e *= 2) {
for (int i = b; i + 1 < e; ++i) {
sol.emplace_back(i, i + 1);
sol.emplace_back(i + n, i + n + 1);
}
sol.emplace_back(e - 1, b + n);
sol.emplace_back(b, 1);
}
if (m + 2 < n) {
int xor_sum = 0;
for (int i = m + 3; i <= n; ++i)
xor_sum ^= i;
for (int i = m + 3; i < n; ++i) {
sol.emplace_back(i, i + 1);
sol.emplace_back(i + n, i + n + 1);
}
if (xor_sum == 0) {
sol.emplace_back(n, m + n + 3);
sol.emplace_back(n, 1);
} else {
assert(xor_sum < m);
sol.emplace_back(n, xor_sum);
sol.emplace_back(xor_sum, m + n + 3);
}
}
Check(n, sol);
cout << "Yes\n";
for (auto x : sol) {
cout << x.first << " " << x.second << '\n';
}
}
}
return 0;
} | replace | 5 | 6 | 5 | 6 | TLE | |
p02977 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/timeb.h>
#include <vector>
using namespace std;
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define reprrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) reprrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define chmin(mi, val) mi = min(mi, val)
#define chmax(ma, val) ma = max(ma, val)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mp make_pair
#define mt make_tuple
#define INF 1050000000
#define INFR INT_MAX
#define INFL (long long)(4e18)
#define INFLR LLONG_MAX
#define EPS (1e-10)
#define MOD 1000000007
// #define MOD 998244353
#define PI 3.141592653589793238
#define RMAX 4294967295
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vs = vector<string>;
using vvs = vector<vector<string>>;
using Pi = pair<int, int>;
using vPi = vector<Pi>;
using vvPi = vector<vector<Pi>>;
using vvvPi = vector<vector<vector<Pi>>>;
using vvvvPi = vector<vector<vector<vector<Pi>>>>;
using Pll = pair<ll, ll>;
using vPll = vector<Pll>;
using Pd = pair<double, double>;
using vPd = vector<Pd>;
template <class T> using vec = vector<T>;
template <class T> using pql = priority_queue<T, vector<T>, greater<T>>;
// vvvvvvvvvvvvvvvvvvvvvvv debug output vvvvvvvvvvvvvvvvvvvvvvv
// vector
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
// vector
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
}
os << "}";
return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
os << "{";
repi(itr, map_var) {
os << *itr;
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
// set
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
os << "{";
repi(itr, set_var) {
os << *itr;
itr++;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) > 0) {
DUMPOUT << ", ";
}
dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define dump(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
<< " ", \
dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif
// ^^^^^^^^^^^^^^^^^^^^^^^ debug output ^^^^^^^^^^^^^^^^^^^^^^^
string YN(bool y, int id = 0) {
if (id)
cout << id;
return (y ? "YES" : "NO");
}
string yn(bool y, int id = 0) {
if (id)
cout << id;
return (y ? "Yes" : "No");
}
string ON(bool y, int id = 0) {
if (id)
cout << id;
return (y ? "OK" : "NG");
}
int dir4[4][2] = {{0, -1}, {-1, 0}, {1, 0}, {0, 1}};
int dir8[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0},
{1, 0}, {-1, 1}, {0, 1}, {1, 1}};
char dirchar[4] = {'<', '^', '>', 'v'};
// [a,b)
int irand(int a, int b) {
static mt19937 Rand(static_cast<unsigned int>(time(nullptr)));
uniform_int_distribution<int> dist(a, b - 1);
return dist(Rand);
}
// [a,b)
double drand(int a, int b) {
static mt19937 Rand(static_cast<unsigned int>(time(nullptr)));
uniform_real_distribution<double> dist(a, b);
return dist(Rand);
}
// https://qiita.com/IgnorantCoder/items/3101d6276e9bdddf872c
template <typename A, typename F> inline auto transform(const A &v, F &&f) {
using result_type =
decltype(std::declval<F>()(std::declval<typename A::value_type>()));
vector<result_type> y(v.size());
std::transform(std::cbegin(v), std::cend(v), std::begin(y), f);
return y;
}
// 多次元vector生成
template <class T> vector<T> make_v(size_t size, const T &init) {
return vector<T>(size, init);
}
template <class... Ts> auto make_v(size_t size, Ts... rest) {
return vector<decltype(make_v(rest...))>(size, make_v(rest...));
}
template <typename T> T Max(vector<T> a) { return *max_element(all(a)); }
template <typename T> T Min(vector<T> a) { return *min_element(all(a)); }
template <typename T> T Sum(vector<T> a) { return accumulate(all(a), (T)0); }
// mapでカウントとかする
template <typename T> void Add(map<T, int> &m, T item) {
if (m.find(item) == m.end()) {
m[item] = 1;
} else {
m[item]++;
}
}
// デフォルト値つきのmapのget
template <typename T, typename U> U Get(map<T, U> m, T key, U def) {
if (m.find(key) == m.end()) {
return def;
} else {
return m[key];
}
}
template <typename T> bool Contains(set<T> t, const T &key) {
return t.find(key) != t.end();
}
template <typename T, typename U> bool Contains(map<T, U> t, const T &key) {
return t.find(key) != t.end();
}
template <class T> struct Edge {
int from, to;
T cost;
bool operator<(Edge e) { return cost < e.cost; }
};
template <class T> ostream &operator<<(ostream &os, Edge<T> &edge) {
os << "(" << edge.from << "->" << edge.to << ":" << edge.cost << ")";
return os;
}
template <class T = int> class Graph {
int n;
bool directed;
vector<vector<Edge<T>>> edges;
public:
Graph(int n, bool directed = false)
: n(n), directed(directed), edges(vector<vector<Edge<T>>>(n)) {}
void add_edge(int s, int t, T cost) {
edges[s].push_back(Edge<T>{s, t, cost});
if (!directed) {
edges[t].push_back(Edge<T>{t, s, cost});
}
}
Graph() {}
vector<Edge<T>> operator[](size_t i) const { return edges[i]; }
int size() const { return n; }
};
//======================================================
int digit(int n) {
int ans = 0;
while (n > 0) {
n >>= 1;
ans++;
}
return ans;
}
void answer(const vPi &edges) {
cout << "Yes" << endl;
dump(edges.size());
for (auto p : edges) {
cout << p.first << " " << p.second << endl;
}
return;
}
int main(void) {
int N;
cin >> N;
if (N <= 2) {
cout << "No" << endl;
return 0;
}
vPi edges;
edges.reserve(N - 1);
edges.push_back(mp(1, 2));
edges.push_back(mp(2, 3));
edges.push_back(mp(3, N + 1));
edges.push_back(mp(N + 1, N + 2));
edges.push_back(mp(N + 2, N + 3));
if (N == 3) {
answer(edges);
return 0;
}
repr(i, 1, (N + 1) / 4) {
edges.push_back(mp(i * 4, i * 4 + 1));
edges.push_back(mp(i * 4 + 1, i * 4 + 2));
edges.push_back(mp(i * 4 + 2, i * 4 + 3));
edges.push_back(mp(i * 4 + 3, N + i * 4));
edges.push_back(mp(N + i * 4, N + i * 4 + 1));
edges.push_back(mp(N + i * 4 + 1, N + i * 4 + 2));
edges.push_back(mp(N + i * 4 + 2, N + i * 4 + 3));
}
if (N % 4 == 3) {
// 並べるだけ
repr(i, 1, (N + 3) / 4) {
// i*4 - i*4+3 のブロック
edges.push_back(mp(1, i * 4));
}
} else if (N % 4 == 0) {
// 2数で条件を満たす数を探す
// 無理な場合がある
if (digit(N) != digit(N - 1)) {
cout << "No" << endl;
return 0;
}
int target = N ^ (N - 1);
edges.push_back(mp(target, N - 1));
edges.push_back(mp(N, target));
edges.push_back(mp(N - 1, 2 * N));
repr(i, 1, N / 4 - 1) {
// i*4 - i*4+3 のブロック
edges.push_back(mp(1, i * 4));
}
} else if (N % 4 == 1) {
// 間の数を計算して差し込む
int target = N ^ (N - 1);
edges.push_back(mp(N - 1, N));
edges.push_back(mp(N, target));
edges.push_back(mp(target, N + N - 1));
edges.push_back(mp(N + N - 1, N + N));
repr(i, 1, N / 4) {
// i*4 - i*4+3 のブロック
edges.push_back(mp(1, i * 4));
}
} else if (N % 4 == 2) {
// 無理な場合があるっぽい
if (N == 6) {
assert(false);
cout << "No" << endl;
return 0;
}
if (digit(N) != digit(N - 3)) {
edges.push_back(mp(N - 2, N));
edges.push_back(mp(N, 2));
edges.push_back(mp(2, N + N - 2));
edges.push_back(mp(N + N - 2, N + N));
edges.push_back(mp(N - 1, N - 2));
edges.push_back(mp(N - 2, N - 4));
edges.push_back(mp(N - 3, N + N - 1));
repr(i, 1, N / 4 - 1) {
// i*4 - i*4+3 のブロック
edges.push_back(mp(1, i * 4));
}
} else {
int target = N ^ (N - 1) ^ (N - 2) ^ (N - 3);
edges.push_back(mp(N - 2, N - 1));
edges.push_back(mp(N - 1, N));
edges.push_back(mp(N, target));
edges.push_back(mp(target, N + N - 3));
edges.push_back(mp(N + N - 3, N + N - 2));
edges.push_back(mp(N + N - 2, N + N - 1));
edges.push_back(mp(N + N - 1, N + N));
repr(i, 1, N / 4 - 1) {
// i*4 - i*4+3 のブロック
edges.push_back(mp(1, i * 4));
}
}
}
answer(edges);
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/timeb.h>
#include <vector>
using namespace std;
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define reprrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) reprrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define chmin(mi, val) mi = min(mi, val)
#define chmax(ma, val) ma = max(ma, val)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mp make_pair
#define mt make_tuple
#define INF 1050000000
#define INFR INT_MAX
#define INFL (long long)(4e18)
#define INFLR LLONG_MAX
#define EPS (1e-10)
#define MOD 1000000007
// #define MOD 998244353
#define PI 3.141592653589793238
#define RMAX 4294967295
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vs = vector<string>;
using vvs = vector<vector<string>>;
using Pi = pair<int, int>;
using vPi = vector<Pi>;
using vvPi = vector<vector<Pi>>;
using vvvPi = vector<vector<vector<Pi>>>;
using vvvvPi = vector<vector<vector<vector<Pi>>>>;
using Pll = pair<ll, ll>;
using vPll = vector<Pll>;
using Pd = pair<double, double>;
using vPd = vector<Pd>;
template <class T> using vec = vector<T>;
template <class T> using pql = priority_queue<T, vector<T>, greater<T>>;
// vvvvvvvvvvvvvvvvvvvvvvv debug output vvvvvvvvvvvvvvvvvvvvvvv
// vector
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
// vector
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
}
os << "}";
return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
os << "{";
repi(itr, map_var) {
os << *itr;
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
// set
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
os << "{";
repi(itr, set_var) {
os << *itr;
itr++;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) > 0) {
DUMPOUT << ", ";
}
dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define dump(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
<< " ", \
dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif
// ^^^^^^^^^^^^^^^^^^^^^^^ debug output ^^^^^^^^^^^^^^^^^^^^^^^
string YN(bool y, int id = 0) {
if (id)
cout << id;
return (y ? "YES" : "NO");
}
string yn(bool y, int id = 0) {
if (id)
cout << id;
return (y ? "Yes" : "No");
}
string ON(bool y, int id = 0) {
if (id)
cout << id;
return (y ? "OK" : "NG");
}
int dir4[4][2] = {{0, -1}, {-1, 0}, {1, 0}, {0, 1}};
int dir8[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0},
{1, 0}, {-1, 1}, {0, 1}, {1, 1}};
char dirchar[4] = {'<', '^', '>', 'v'};
// [a,b)
int irand(int a, int b) {
static mt19937 Rand(static_cast<unsigned int>(time(nullptr)));
uniform_int_distribution<int> dist(a, b - 1);
return dist(Rand);
}
// [a,b)
double drand(int a, int b) {
static mt19937 Rand(static_cast<unsigned int>(time(nullptr)));
uniform_real_distribution<double> dist(a, b);
return dist(Rand);
}
// https://qiita.com/IgnorantCoder/items/3101d6276e9bdddf872c
template <typename A, typename F> inline auto transform(const A &v, F &&f) {
using result_type =
decltype(std::declval<F>()(std::declval<typename A::value_type>()));
vector<result_type> y(v.size());
std::transform(std::cbegin(v), std::cend(v), std::begin(y), f);
return y;
}
// 多次元vector生成
template <class T> vector<T> make_v(size_t size, const T &init) {
return vector<T>(size, init);
}
template <class... Ts> auto make_v(size_t size, Ts... rest) {
return vector<decltype(make_v(rest...))>(size, make_v(rest...));
}
template <typename T> T Max(vector<T> a) { return *max_element(all(a)); }
template <typename T> T Min(vector<T> a) { return *min_element(all(a)); }
template <typename T> T Sum(vector<T> a) { return accumulate(all(a), (T)0); }
// mapでカウントとかする
template <typename T> void Add(map<T, int> &m, T item) {
if (m.find(item) == m.end()) {
m[item] = 1;
} else {
m[item]++;
}
}
// デフォルト値つきのmapのget
template <typename T, typename U> U Get(map<T, U> m, T key, U def) {
if (m.find(key) == m.end()) {
return def;
} else {
return m[key];
}
}
template <typename T> bool Contains(set<T> t, const T &key) {
return t.find(key) != t.end();
}
template <typename T, typename U> bool Contains(map<T, U> t, const T &key) {
return t.find(key) != t.end();
}
template <class T> struct Edge {
int from, to;
T cost;
bool operator<(Edge e) { return cost < e.cost; }
};
template <class T> ostream &operator<<(ostream &os, Edge<T> &edge) {
os << "(" << edge.from << "->" << edge.to << ":" << edge.cost << ")";
return os;
}
template <class T = int> class Graph {
int n;
bool directed;
vector<vector<Edge<T>>> edges;
public:
Graph(int n, bool directed = false)
: n(n), directed(directed), edges(vector<vector<Edge<T>>>(n)) {}
void add_edge(int s, int t, T cost) {
edges[s].push_back(Edge<T>{s, t, cost});
if (!directed) {
edges[t].push_back(Edge<T>{t, s, cost});
}
}
Graph() {}
vector<Edge<T>> operator[](size_t i) const { return edges[i]; }
int size() const { return n; }
};
//======================================================
int digit(int n) {
int ans = 0;
while (n > 0) {
n >>= 1;
ans++;
}
return ans;
}
void answer(const vPi &edges) {
cout << "Yes" << endl;
dump(edges.size());
for (auto p : edges) {
cout << p.first << " " << p.second << endl;
}
return;
}
int main(void) {
int N;
cin >> N;
if (N <= 2) {
cout << "No" << endl;
return 0;
}
vPi edges;
edges.reserve(N - 1);
edges.push_back(mp(1, 2));
edges.push_back(mp(2, 3));
edges.push_back(mp(3, N + 1));
edges.push_back(mp(N + 1, N + 2));
edges.push_back(mp(N + 2, N + 3));
if (N == 3) {
answer(edges);
return 0;
}
repr(i, 1, (N + 1) / 4) {
edges.push_back(mp(i * 4, i * 4 + 1));
edges.push_back(mp(i * 4 + 1, i * 4 + 2));
edges.push_back(mp(i * 4 + 2, i * 4 + 3));
edges.push_back(mp(i * 4 + 3, N + i * 4));
edges.push_back(mp(N + i * 4, N + i * 4 + 1));
edges.push_back(mp(N + i * 4 + 1, N + i * 4 + 2));
edges.push_back(mp(N + i * 4 + 2, N + i * 4 + 3));
}
if (N % 4 == 3) {
// 並べるだけ
repr(i, 1, (N + 3) / 4) {
// i*4 - i*4+3 のブロック
edges.push_back(mp(1, i * 4));
}
} else if (N % 4 == 0) {
// 2数で条件を満たす数を探す
// 無理な場合がある
if (digit(N) != digit(N - 1)) {
cout << "No" << endl;
return 0;
}
int target = N ^ (N - 1);
edges.push_back(mp(target, N - 1));
edges.push_back(mp(N, target));
edges.push_back(mp(N - 1, 2 * N));
repr(i, 1, N / 4 - 1) {
// i*4 - i*4+3 のブロック
edges.push_back(mp(1, i * 4));
}
} else if (N % 4 == 1) {
// 間の数を計算して差し込む
int target = N ^ (N - 1);
edges.push_back(mp(N - 1, N));
edges.push_back(mp(N, target));
edges.push_back(mp(target, N + N - 1));
edges.push_back(mp(N + N - 1, N + N));
repr(i, 1, N / 4) {
// i*4 - i*4+3 のブロック
edges.push_back(mp(1, i * 4));
}
} else if (N % 4 == 2) {
// 無理な場合があるっぽい
if (N == 6) {
edges.push_back(mp(4, 5));
edges.push_back(mp(5, 1));
edges.push_back(mp(1, 10));
edges.push_back(mp(10, 11));
edges.push_back(mp(6, 5));
edges.push_back(mp(2, 12));
answer(edges);
return 0;
}
if (digit(N) != digit(N - 3)) {
edges.push_back(mp(N - 2, N));
edges.push_back(mp(N, 2));
edges.push_back(mp(2, N + N - 2));
edges.push_back(mp(N + N - 2, N + N));
edges.push_back(mp(N - 1, N - 2));
edges.push_back(mp(N - 2, N - 4));
edges.push_back(mp(N - 3, N + N - 1));
repr(i, 1, N / 4 - 1) {
// i*4 - i*4+3 のブロック
edges.push_back(mp(1, i * 4));
}
} else {
int target = N ^ (N - 1) ^ (N - 2) ^ (N - 3);
edges.push_back(mp(N - 2, N - 1));
edges.push_back(mp(N - 1, N));
edges.push_back(mp(N, target));
edges.push_back(mp(target, N + N - 3));
edges.push_back(mp(N + N - 3, N + N - 2));
edges.push_back(mp(N + N - 2, N + N - 1));
edges.push_back(mp(N + N - 1, N + N));
repr(i, 1, N / 4 - 1) {
// i*4 - i*4+3 のブロック
edges.push_back(mp(1, i * 4));
}
}
}
answer(edges);
return 0;
}
| replace | 347 | 349 | 347 | 354 | 0 | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
using namespace std;
/////////////////////////////////////////////////////
ll N, n;
vector<pll> vn;
ll mk[100], mk2;
pll ab[100];
pll ans[101010];
ll ans2;
ll i, j;
int main() {
scanf("%lld", &N);
if (N == 3) {
printf("Yes\n1 2\n2 3\n3 4\n4 5\n5 6\n");
return 0;
}
for (n = 1; n <= N; n *= 2)
;
n /= 2;
if (N == n) {
printf("No");
return 0;
}
ans2 = 0;
fornum(i, 1, n - 1) {
ans[ans2++] = {i, i + 1};
ans[ans2++] = {i + N, i + N + 1};
}
ans[ans2++] = {n - 1, N + 1};
if (N % 2 == 1) {
fornum(i, n, N + 1) {
ans[ans2++] = {i, i + 1};
ans[ans2++] = {i + 1, 1};
ans[ans2++] = {1, N + i};
ans[ans2++] = {N + i, N + i + 1};
i++;
}
} else {
ll a = N & (n - 1);
fornum(i, n, N) {
ans[ans2++] = {i + 1, i};
ans[ans2++] = {i, a};
ans[ans2++] = {a + 1, N + i + 1};
ans[ans2++] = {N + i + 1, N + i};
i++;
}
ans[ans2++] = {N, n};
ans[ans2++] = {a, N * 2};
}
printf("Yes\n");
fornum(i, 0, ans2) { printf("%lld %lld\n", ans[i].first, ans[i].second); }
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
using namespace std;
/////////////////////////////////////////////////////
ll N, n;
pll ans[202020];
ll ans2;
ll i, j;
int main() {
scanf("%lld", &N);
if (N == 3) {
printf("Yes\n1 2\n2 3\n3 4\n4 5\n5 6\n");
return 0;
}
for (n = 1; n <= N; n *= 2)
;
n /= 2;
if (N == n) {
printf("No");
return 0;
}
ans2 = 0;
fornum(i, 1, n - 1) {
ans[ans2++] = {i, i + 1};
ans[ans2++] = {i + N, i + N + 1};
}
ans[ans2++] = {n - 1, N + 1};
if (N % 2 == 1) {
fornum(i, n, N + 1) {
ans[ans2++] = {i, i + 1};
ans[ans2++] = {i + 1, 1};
ans[ans2++] = {1, N + i};
ans[ans2++] = {N + i, N + i + 1};
i++;
}
} else {
ll a = N & (n - 1);
fornum(i, n, N) {
ans[ans2++] = {i + 1, i};
ans[ans2++] = {i, a};
ans[ans2++] = {a + 1, N + i + 1};
ans[ans2++] = {N + i + 1, N + i};
i++;
}
ans[ans2++] = {N, n};
ans[ans2++] = {a, N * 2};
}
printf("Yes\n");
fornum(i, 0, ans2) { printf("%lld %lld\n", ans[i].first, ans[i].second); }
return 0;
} | replace | 12 | 16 | 12 | 13 | 0 | |
p02977 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;
// #define int long long
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;
#define X first
#define Y second
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
int gcd(int x, int y) { return (!y ? x : gcd(y, x % y)); }
const int maxn = 1e5 + 10;
vi adj[maxn];
void ad_e(int u, int v) { adj[u].push_back(v); }
int32_t main() {
IOS;
int n;
cin >> n;
if (__builtin_popcount(n) == 1) {
cout << "No\n";
return 0;
}
int m = (n % 2 == 0 ? n - 1 : n);
int st = 1;
ad_e(1 + n, 2 + n), ad_e(2 + n, 3), ad_e(3, 1);
ad_e(1, 2), ad_e(2, 3 + n);
for (int i = 4; i <= m; i++) {
ad_e(st, i);
ad_e(i, (i ^ 1) + n);
}
if (m != n) {
for (int u = 2; u <= m; u++) {
int v = n ^ 1 ^ u;
if (v != u && v <= m) {
ad_e(v, n);
ad_e(u, 2 * n);
break;
}
}
}
cout << "Yes\n";
for (int i = 1; i <= 2 * n; i++)
for (auto j : adj[i]) {
cout << i << " " << j << endl;
}
}
| #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;
// #define int long long
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;
#define X first
#define Y second
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
int gcd(int x, int y) { return (!y ? x : gcd(y, x % y)); }
const int maxn = 1e6 + 10;
vi adj[maxn];
void ad_e(int u, int v) { adj[u].push_back(v); }
int32_t main() {
IOS;
int n;
cin >> n;
if (__builtin_popcount(n) == 1) {
cout << "No\n";
return 0;
}
int m = (n % 2 == 0 ? n - 1 : n);
int st = 1;
ad_e(1 + n, 2 + n), ad_e(2 + n, 3), ad_e(3, 1);
ad_e(1, 2), ad_e(2, 3 + n);
for (int i = 4; i <= m; i++) {
ad_e(st, i);
ad_e(i, (i ^ 1) + n);
}
if (m != n) {
for (int u = 2; u <= m; u++) {
int v = n ^ 1 ^ u;
if (v != u && v <= m) {
ad_e(v, n);
ad_e(u, 2 * n);
break;
}
}
}
cout << "Yes\n";
for (int i = 1; i <= 2 * n; i++)
for (auto j : adj[i]) {
cout << i << " " << j << endl;
}
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
// #define co(x) cout << (x) << "\n"
// #define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please
const int dm = 1 << 21;
char dn[dm], *di = dn;
inline void cosp(int X) {
int keta = 0;
char C[10];
while (X) {
*(C + keta) = '0' + X % 10;
X /= 10;
keta++;
}
for (int i = keta - 1; i >= 0; i--)
*di++ = (*(C + i));
*di++ = ' ';
}
inline void co(int X) {
int keta = 0;
char C[10];
while (X) {
*(C + keta) = '0' + X % 10;
X /= 10;
keta++;
}
for (int i = keta - 1; i >= 0; i--)
*di++ = (*(C + i));
*di++ = '\n';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
if (N == 1)
*di++ = 'N', *di++ = 'o', *di++ = '\n';
else {
bool dame = false;
int k = 1;
rep(i, 20) {
k *= 2;
if (N == k)
dame = true;
}
if (dame)
*di++ = 'N', *di++ = 'o', *di++ = '\n';
else if (N % 2 == 0) {
*di++ = 'Y';
*di++ = 'e';
*di++ = 's';
*di++ = '\n';
int xs = 0;
rep1(i, N) { xs ^= i; }
int a = N - 1;
int b = xs ^ a;
int c = a ^ 1;
int d = b ^ 1;
cosp(c);
co(1);
cosp(a);
co(c);
cosp(1);
co(a + N);
cosp(1);
co(d);
cosp(b);
co(d);
cosp(1);
co(b + N);
int mae = d;
rep1(i, N) {
if (i != a && i != b && i != c && i != d && i != 1) {
cosp(mae);
co(i);
mae = i;
}
}
cosp(mae);
co(c + N);
mae = c + N;
cosp(mae);
co(1 + N);
mae = 1 + N;
cosp(mae);
co(d + N);
mae = d + N;
rep1(i, N) {
if (i != a && i != b && i != c && i != d && i != 1) {
cosp(mae);
co(i + N);
mae = i + N;
}
}
} else {
*di++ = 'Y';
*di++ = 'e';
*di++ = 's';
*di++ = '\n';
rep1(i, N / 2) {
int k = i * 2;
cosp(1);
co(k);
cosp(k);
co(k + 1);
cosp(1);
co(k + N + 1);
cosp(k + N);
co(k + N + 1);
}
cosp(1 + N);
co(2 + N);
}
}
fwrite_unlocked(dn, di - dn, 1, stdout);
Would you please return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
// #define co(x) cout << (x) << "\n"
// #define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please
const int dm = 1 << 22;
char dn[dm], *di = dn;
inline void cosp(int X) {
int keta = 0;
char C[10];
while (X) {
*(C + keta) = '0' + X % 10;
X /= 10;
keta++;
}
for (int i = keta - 1; i >= 0; i--)
*di++ = (*(C + i));
*di++ = ' ';
}
inline void co(int X) {
int keta = 0;
char C[10];
while (X) {
*(C + keta) = '0' + X % 10;
X /= 10;
keta++;
}
for (int i = keta - 1; i >= 0; i--)
*di++ = (*(C + i));
*di++ = '\n';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
if (N == 1)
*di++ = 'N', *di++ = 'o', *di++ = '\n';
else {
bool dame = false;
int k = 1;
rep(i, 20) {
k *= 2;
if (N == k)
dame = true;
}
if (dame)
*di++ = 'N', *di++ = 'o', *di++ = '\n';
else if (N % 2 == 0) {
*di++ = 'Y';
*di++ = 'e';
*di++ = 's';
*di++ = '\n';
int xs = 0;
rep1(i, N) { xs ^= i; }
int a = N - 1;
int b = xs ^ a;
int c = a ^ 1;
int d = b ^ 1;
cosp(c);
co(1);
cosp(a);
co(c);
cosp(1);
co(a + N);
cosp(1);
co(d);
cosp(b);
co(d);
cosp(1);
co(b + N);
int mae = d;
rep1(i, N) {
if (i != a && i != b && i != c && i != d && i != 1) {
cosp(mae);
co(i);
mae = i;
}
}
cosp(mae);
co(c + N);
mae = c + N;
cosp(mae);
co(1 + N);
mae = 1 + N;
cosp(mae);
co(d + N);
mae = d + N;
rep1(i, N) {
if (i != a && i != b && i != c && i != d && i != 1) {
cosp(mae);
co(i + N);
mae = i + N;
}
}
} else {
*di++ = 'Y';
*di++ = 'e';
*di++ = 's';
*di++ = '\n';
rep1(i, N / 2) {
int k = i * 2;
cosp(1);
co(k);
cosp(k);
co(k + 1);
cosp(1);
co(k + N + 1);
cosp(k + N);
co(k + N + 1);
}
cosp(1 + N);
co(2 + N);
}
}
fwrite_unlocked(dn, di - dn, 1, stdout);
Would you please return 0;
} | replace | 15 | 16 | 15 | 16 | 0 | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
typedef long long ll;
using namespace std;
int used[100010];
int main() {
int N;
cin >> N;
if (N % 2 == 0 && N < 25)
cout << 1 / 0 << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
typedef long long ll;
using namespace std;
int used[100010];
int main() {
int N;
cin >> N;
if (N == 1) {
cout << "No"
<< "\n";
return 0;
}
if (N == 6) {
cout << "Yes"
<< "\n";
cout << 3 << " " << 5 << "\n";
cout << 4 << " " << 5 << "\n";
cout << 4 << " " << 2 << "\n";
cout << 9 << " " << 2 << "\n";
cout << 9 << " " << 11 << "\n";
cout << 10 << " " << 11 << "\n";
cout << 10 << " " << 8 << "\n";
cout << 1 << " " << 5 << "\n";
cout << 7 << " " << 4 << "\n";
cout << 4 << " " << 6 << "\n";
cout << 2 << " " << 12 << "\n";
return 0;
}
if (N % 4 == 1) {
cout << "Yes"
<< "\n";
cout << 1 << " " << 2 << "\n";
cout << N + 1 << " " << 3 << "\n";
cout << N + 2 << " " << N << "\n";
rep(i, N - 2) {
cout << 2 + i << " " << 3 + i << "\n";
cout << 2 + i + N << " " << 3 + i + N << "\n";
}
} else if (N % 4 == 3) {
cout << "Yes"
<< "\n";
rep(i, 2 * N - 1) { cout << i + 1 << " " << i + 2 << "\n"; }
} else {
int n = N, f = 0;
int ch;
for (int i = 0; f == 0; i++) {
if (N & (1 << i)) {
ch = (1 << i);
f = 1;
n -= (1 << i);
}
}
if (n == 0) {
cout << "No" << endl;
return 0;
}
cout << "Yes"
<< "\n";
cout << N << " " << ch << "\n";
cout << n << " " << ch << "\n";
cout << 2 * N << " " << n << "\n";
int pr = -1;
if ((N - 1) % 4 == 1) {
int p = (ch + 2) / 4, q = (n + 2) / 4;
for (int i = 1; i <= (N - 2) / 4; i++) {
if (i == p || i == q) {
rep(j, 3) {
cout << 4 * i - 2 + j << " " << 4 * i - 1 + j << "\n";
cout << 4 * i - 2 + j + N << " " << 4 * i - 1 + j + N << "\n";
}
cout << 4 * i + 1 << " " << 4 * i - 2 + N << "\n";
} else {
rep(j, 3) {
cout << 4 * i - 2 + j << " " << 4 * i - 1 + j << "\n";
cout << 4 * i - 2 + j + N << " " << 4 * i - 1 + j + N << "\n";
}
cout << 4 * i + 1 << " " << 4 * i - 2 + N << "\n";
if (pr == -1) {
pr = i;
} else {
cout << 4 * pr + 1 + N << " " << 4 * i - 2 << "\n";
pr = i;
}
}
}
if (pr != -1) {
cout << N << " " << 4 * pr + 1 + N << "\n";
}
cout << 1 << " " << 2 << "\n";
cout << N + 1 << " " << 3 << "\n";
} else {
int p = ch / 4, q = n / 4;
for (int i = 0; i < (N / 4); i++) {
if (i == p || i == q) {
rep(j, 3) {
if (i != 0 || j != 0) {
cout << 4 * i + j << " " << 4 * i + 1 + j << "\n";
cout << 4 * i + j + N << " " << 4 * i + 1 + j + N << "\n";
}
}
if (i != 0) {
cout << 4 * i + 3 << " " << 4 * i + N << "\n";
} else {
cout << 3 << " " << N + 1 << "\n";
}
} else {
rep(j, 3) {
if (i != 0 || j != 0) {
cout << 4 * i + j << " " << 4 * i + 1 + j << "\n";
cout << 4 * i + j + N << " " << 4 * i + 1 + j + N << "\n";
}
}
if (i != 0) {
cout << 4 * i + 3 << " " << 4 * i + N << "\n";
} else {
cout << 3 << " " << N + 1 << "\n";
}
if (pr == -1) {
pr = i;
} else {
cout << 4 * pr + 3 + N << " " << 4 * i << "\n";
pr = i;
}
}
}
if (pr != -1) {
cout << N << " " << 4 * pr + 3 + N << "\n";
}
}
}
return 0;
}
| replace | 10 | 12 | 10 | 133 | 0 | |
p02977 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
const int MAXN = 100005;
int N, Root = 1;
vector<int> P[MAXN];
int main() {
// freopen("tree.in","r",stdin);
// freopen("tree.out","w",stdout);
scanf("%d", &N);
if (int(log2(N)) == log2(N)) {
printf("No\n");
return 0;
}
P[1].push_back(2);
P[2].push_back(3);
P[3].push_back(1 + N);
P[1 + N].push_back(2 + N);
P[2 + N].push_back(3 + N);
for (int i = 4; i < N; i += 2) {
P[Root].push_back(i);
P[Root].push_back(i + N + 1);
P[i].push_back(i + 1);
P[i + N + 1].push_back(i + N);
}
if (N % 2 == 0) {
int ed = N, f = 0, Sum = 0;
for (int i = 25; i >= 0; i--)
if (ed & (1 << i))
f = (1 << i), Sum += f;
int t1, t2;
t1 = f, t2 = Sum - t1 + N + 1;
P[ed].push_back(t1);
P[ed + N].push_back(t2);
}
printf("Yes\n");
for (int i = 1; i <= 2 * N; i++) {
int size = P[i].size();
for (int j = 0; j < size; j++)
printf("%d %d\n", i, P[i][j]);
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
const int MAXN = 200005;
int N, Root = 1;
vector<int> P[MAXN];
int main() {
// freopen("tree.in","r",stdin);
// freopen("tree.out","w",stdout);
scanf("%d", &N);
if (int(log2(N)) == log2(N)) {
printf("No\n");
return 0;
}
P[1].push_back(2);
P[2].push_back(3);
P[3].push_back(1 + N);
P[1 + N].push_back(2 + N);
P[2 + N].push_back(3 + N);
for (int i = 4; i < N; i += 2) {
P[Root].push_back(i);
P[Root].push_back(i + N + 1);
P[i].push_back(i + 1);
P[i + N + 1].push_back(i + N);
}
if (N % 2 == 0) {
int ed = N, f = 0, Sum = 0;
for (int i = 25; i >= 0; i--)
if (ed & (1 << i))
f = (1 << i), Sum += f;
int t1, t2;
t1 = f, t2 = Sum - t1 + N + 1;
P[ed].push_back(t1);
P[ed + N].push_back(t2);
}
printf("Yes\n");
for (int i = 1; i <= 2 * N; i++) {
int size = P[i].size();
for (int j = 0; j < size; j++)
printf("%d %d\n", i, P[i][j]);
}
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(), v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) \
distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define bit(n) (1LL << (n))
#define bit_exist(x, n) ((x >> n) & 1)
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) \
cout << #v << "=" << endl; \
REP(i_debug, v.size()) { cout << v[i_debug] << ","; } \
cout << endl;
#define mdebug(m) \
cout << #m << "=" << endl; \
REP(i_debug, m.size()) { \
REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } \
cout << endl; \
}
#define pb push_back
#define f first
#define s second
#define int long long
#define INF 1000000000000000000
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &x : v)
is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i];
if (i != v.size() - 1)
cout << endl;
};
return os;
}
template <typename T> void Out(T x) { cout << x << endl; }
template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) {
if (f)
Out(y);
else
Out(n);
}
using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using PiP = pair<int, Pii>;
using PPi = pair<Pii, int>;
using bools = vector<bool>;
using pairs = vector<Pii>;
// int dx[4] = {1,0,-1,0};
// int dy[4] = {0,1,0,-1};
// char d[4] = {'D','R','U','L'};
const int mod = 1000000007;
// const int mod = 998244353;
#define Add(x, y) x = (x + (y)) % mod
#define Mult(x, y) x = (x * (y)) % mod
signed main() {
int N;
cin >> N;
int d;
REP(i, 100) {
if (N <= bit(i)) {
d = i;
break;
}
}
if (N == bit(d)) {
Out("No");
return 0;
}
// N>=3
pairs ans;
ans.pb(Pii(1, 3));
ans.pb(Pii(3, 2));
ans.pb(Pii(2, 1 + N));
ans.pb(Pii(1 + N, 3 + N));
ans.pb(Pii(3 + N, 2 + N));
for (int i = 4; i <= N; i += 2) {
if (i + 2 == N) {
if ((N >> 1) & 1) {
ans.pb(Pii(i, 1));
ans.pb(Pii(i + 1, i));
ans.pb(Pii(i + 1 + N, i + N));
ans.pb(Pii(i + 1 + N, 1 + N));
ans.pb(Pii(i + 2, i));
ans.pb(Pii(i + 2 + N, 3));
} else {
ans.pb(Pii(i + 2, 3));
ans.pb(Pii(i + 1, i + 2));
ans.pb(Pii(i + 1 + N, i + 2 + N));
ans.pb(Pii(i + 1 + N, 3 + N));
ans.pb(Pii(i, i + 2));
ans.pb(Pii(i + N, 1));
assert(false);
}
break;
} else {
ans.pb(Pii(i, 1));
ans.pb(Pii(i + 1, i));
ans.pb(Pii(i + 1 + N, i + N));
ans.pb(Pii(i + 1 + N, 1 + N));
}
}
Out("Yes");
REP(i, 2 * N - 1) cout << ans[i].f << " " << ans[i].s << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(), v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) \
distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define bit(n) (1LL << (n))
#define bit_exist(x, n) ((x >> n) & 1)
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) \
cout << #v << "=" << endl; \
REP(i_debug, v.size()) { cout << v[i_debug] << ","; } \
cout << endl;
#define mdebug(m) \
cout << #m << "=" << endl; \
REP(i_debug, m.size()) { \
REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } \
cout << endl; \
}
#define pb push_back
#define f first
#define s second
#define int long long
#define INF 1000000000000000000
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &x : v)
is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i];
if (i != v.size() - 1)
cout << endl;
};
return os;
}
template <typename T> void Out(T x) { cout << x << endl; }
template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) {
if (f)
Out(y);
else
Out(n);
}
using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using PiP = pair<int, Pii>;
using PPi = pair<Pii, int>;
using bools = vector<bool>;
using pairs = vector<Pii>;
// int dx[4] = {1,0,-1,0};
// int dy[4] = {0,1,0,-1};
// char d[4] = {'D','R','U','L'};
const int mod = 1000000007;
// const int mod = 998244353;
#define Add(x, y) x = (x + (y)) % mod
#define Mult(x, y) x = (x * (y)) % mod
signed main() {
int N;
cin >> N;
int d;
REP(i, 100) {
if (N <= bit(i)) {
d = i;
break;
}
}
if (N == bit(d)) {
Out("No");
return 0;
}
// N>=3
pairs ans;
ans.pb(Pii(1, 3));
ans.pb(Pii(3, 2));
ans.pb(Pii(2, 1 + N));
ans.pb(Pii(1 + N, 3 + N));
ans.pb(Pii(3 + N, 2 + N));
for (int i = 4; i <= N; i += 2) {
if (i == N) {
ans.pb(Pii(i, bit(d - 1)));
int r = N ^ bit(d - 1) ^ 1;
ans.pb(Pii(i + N, r + N));
} else {
ans.pb(Pii(i, 1));
ans.pb(Pii(i + 1, i));
ans.pb(Pii(i + 1 + N, i + N));
ans.pb(Pii(i + 1 + N, 1 + N));
}
}
Out("Yes");
REP(i, 2 * N - 1) cout << ans[i].f << " " << ans[i].s << endl;
return 0;
}
| replace | 104 | 122 | 104 | 108 | 0 | |
p02977 | C++ | Runtime Error | #include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i <= 30; i++)
if (n == (1 << i)) {
std::cout << "No" << std::endl;
return 0;
}
int nn;
if (n % 4 == 3)
nn = n;
else
nn = n / 4 * 4 - 1;
std::vector<int> all;
for (int i = 1; i <= nn; i++)
all.push_back(i);
std::vector<int> xorsum(nn + 1);
for (int i = 0; i < nn; i++)
xorsum[i + 1] = xorsum[i] ^ all[i];
std::vector<std::pair<int, int>> add;
if (n % 4 == 3) {
} else if (n % 4 == 0) {
int r0 = nn;
int r1 = -1;
for (int i = 0; i <= nn; i++) {
if (xorsum[i] == ((nn + 1) ^ nn)) {
r1 = i ? n + i : nn;
break;
}
}
if (r1 == -1) {
std::cout << "No" << std::endl;
return 0;
}
add.push_back({r0, nn + 1});
add.push_back({r1, nn + n + 1});
} else {
add.push_back({n + 1, nn + 1});
add.push_back({n + 1, nn + 2});
add.push_back({nn + 1, n + nn + 2});
add.push_back({nn + 2, n + nn + 1});
if (n % 4 == 3) {
int r0 = nn;
int r1 = -1;
for (int i = 0; i <= nn; i++) {
if (xorsum[i] == ((nn + 3) ^ nn)) {
r1 = i ? n + i : nn;
break;
}
}
if (r1 == -1) {
std::cout << "No" << std::endl;
return 0;
}
add.push_back({r0, nn + 3});
add.push_back({r1, nn + 3 + n});
}
}
std::cout << "Yes" << std::endl;
for (int i = 1; i < nn; i++)
add.push_back({i, i + 1});
add.push_back({nn, n + 1});
for (int i = n + 1; i < n + nn; i++)
add.push_back({i, i + 1});
assert(add.size() == 2 * n - 1);
for (auto i : add)
std::cout << i.first << " " << i.second << std::endl;
return 0;
}
| #include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i <= 30; i++)
if (n == (1 << i)) {
std::cout << "No" << std::endl;
return 0;
}
int nn;
if (n % 4 == 3)
nn = n;
else
nn = n / 4 * 4 - 1;
std::vector<int> all;
for (int i = 1; i <= nn; i++)
all.push_back(i);
std::vector<int> xorsum(nn + 1);
for (int i = 0; i < nn; i++)
xorsum[i + 1] = xorsum[i] ^ all[i];
std::vector<std::pair<int, int>> add;
if (n % 4 == 3) {
} else if (n % 4 == 0) {
int r0 = nn;
int r1 = -1;
for (int i = 0; i <= nn; i++) {
if (xorsum[i] == ((nn + 1) ^ nn)) {
r1 = i ? n + i : nn;
break;
}
}
if (r1 == -1) {
std::cout << "No" << std::endl;
return 0;
}
add.push_back({r0, nn + 1});
add.push_back({r1, nn + n + 1});
} else {
add.push_back({n + 1, nn + 1});
add.push_back({n + 1, nn + 2});
add.push_back({nn + 1, n + nn + 2});
add.push_back({nn + 2, n + nn + 1});
if (n % 4 == 2) {
add.push_back({nn + 3, n + 2});
add.push_back({n + nn + 3, nn + 2});
}
}
std::cout << "Yes" << std::endl;
for (int i = 1; i < nn; i++)
add.push_back({i, i + 1});
add.push_back({nn, n + 1});
for (int i = n + 1; i < n + nn; i++)
add.push_back({i, i + 1});
assert(add.size() == 2 * n - 1);
for (auto i : add)
std::cout << i.first << " " << i.second << std::endl;
return 0;
}
| replace | 43 | 58 | 43 | 46 | 0 | |
p02978 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ri register int
typedef long long ll;
#define rll register ll
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
qu[55];
int f, qr;
// getchar
#define gc() \
(iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), \
(iS == iT ? EOF : *iS++)) \
: *iS++)
// print the remaining part
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc(char x) {
*oS++ = x;
if (oS == oT)
flush();
}
// input a signed integer
template <class I> inline void gi(I &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc())
if (c == '-')
f = -1;
for (x = 0; c <= '9' && c >= '0'; c = gc())
x = (x << 1) + (x << 3) + (c & 15);
x *= f;
}
// input a signed integer
template <class I> inline void read(I &x, int p) {
for (c = gc(); c < '0' || c > '9'; c = gc())
;
for (x = 0; c <= '9' && c >= '0'; c = gc())
x = ((x << 1) + (x << 3) + (c & 15)) % p;
}
// print a signed integer
template <class I> inline void print(I x) {
if (!x)
putc('0');
if (x < 0)
putc('-'), x = -x;
while (x)
qu[++qr] = x % 10 + '0', x /= 10;
while (qr)
putc(qu[qr--]);
}
// no need to call flush at the end manually!
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::gi;
using io ::print;
using io ::putc;
using io ::read;
const int N = 20;
const ll inf = 1e18;
int a[N];
#define pii pair<int, int>
#define mp make_pair
map<pii, ll> f[N][N];
inline ll dfs(ri i, ri j, ri x, ri y) {
if (f[i][j][mp(x, y)])
return f[i][j][mp(x, y)];
if (i + 1 == j)
return 0;
rll ans = inf;
for (ri k = i + 1; k <= j - 1; ++k)
ans = min(ans,
(ll)a[k] * (x + y) + dfs(i, k, x, x + y) + dfs(k, j, x + y, y));
return f[i][j][mp(x, y)] = ans;
}
int main() {
ri n, i;
gi(n);
for (i = 1; i <= n; ++i)
gi(a[i]);
printf("%lld\n", dfs(1, n, 1, 1) + a[1] + a[n]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ri register int
typedef long long ll;
#define rll register ll
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
qu[55];
int f, qr;
// getchar
#define gc() \
(iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), \
(iS == iT ? EOF : *iS++)) \
: *iS++)
// print the remaining part
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc(char x) {
*oS++ = x;
if (oS == oT)
flush();
}
// input a signed integer
template <class I> inline void gi(I &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc())
if (c == '-')
f = -1;
for (x = 0; c <= '9' && c >= '0'; c = gc())
x = (x << 1) + (x << 3) + (c & 15);
x *= f;
}
// input a signed integer
template <class I> inline void read(I &x, int p) {
for (c = gc(); c < '0' || c > '9'; c = gc())
;
for (x = 0; c <= '9' && c >= '0'; c = gc())
x = ((x << 1) + (x << 3) + (c & 15)) % p;
}
// print a signed integer
template <class I> inline void print(I x) {
if (!x)
putc('0');
if (x < 0)
putc('-'), x = -x;
while (x)
qu[++qr] = x % 10 + '0', x /= 10;
while (qr)
putc(qu[qr--]);
}
// no need to call flush at the end manually!
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::gi;
using io ::print;
using io ::putc;
using io ::read;
const int N = 20;
const ll inf = 1e18;
int a[N];
#define pii pair<int, int>
#define mp make_pair
map<pii, ll> f[N][N];
inline ll dfs(ri i, ri j, ri x, ri y) {
if (f[i][j].find(mp(x, y)) != f[i][j].end())
return f[i][j][mp(x, y)];
if (i + 1 == j)
return 0;
rll ans = inf;
for (ri k = i + 1; k <= j - 1; ++k)
ans = min(ans,
(ll)a[k] * (x + y) + dfs(i, k, x, x + y) + dfs(k, j, x + y, y));
return f[i][j][mp(x, y)] = ans;
}
int main() {
ri n, i;
gi(n);
for (i = 1; i <= n; ++i)
gi(a[i]);
printf("%lld\n", dfs(1, n, 1, 1) + a[1] + a[n]);
return 0;
}
| replace | 69 | 70 | 69 | 70 | TLE | |
p02978 | C++ | Time Limit Exceeded | #include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <tuple>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int64_t> A(N);
for (auto &a : A)
cin >> a;
function<int64_t(vector<int64_t>)> dfs = [&](vector<int64_t> A) {
if (A.size() == 2)
return A[0] + A[1];
int64_t ret = numeric_limits<int64_t>::max();
for (int i = 1; i <= A.size() - 2; i++) {
vector<int64_t> AA;
for (int j = 0; j < A.size(); j++) {
if (j != i)
AA.push_back(A[j]);
}
AA[i - 1] += A[i];
AA[i] += A[i];
ret = min(ret, dfs(AA));
}
return ret;
};
cout << dfs(A) << endl;
return 0;
} | #include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <tuple>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int64_t> A(N);
for (auto &a : A)
cin >> a;
function<int64_t(int64_t, int64_t, int64_t, int64_t)> rec =
[&](int64_t l, int64_t r, int64_t lx, int64_t rx) {
if (r - l == 1)
return int64_t(0);
int64_t ret = numeric_limits<int64_t>::max();
for (int k = l + 1; k < r; k++) {
ret = min(ret, rec(l, k, lx, lx + rx) + rec(k, r, lx + rx, rx) +
A[k] * (lx + rx));
}
return ret;
};
cout << rec(0, N - 1, 1, 1) + A[0] + A[N - 1] << endl;
return 0;
} | replace | 14 | 31 | 14 | 26 | TLE | |
p02978 | C++ | Runtime Error | #include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#define NDEBUG
#define SHOW(...) static_cast<void>(0)
//!===========================================================!//
//! dP dP dP !//
//! 88 88 88 !//
//! 88aaaaa88a .d8888b. .d8888b. .d888b88 .d8888b. 88d888b. !//
//! 88 88 88ooood8 88' '88 88' '88 88ooood8 88' '88 !//
//! 88 88 88. ... 88. .88 88. .88 88. ... 88 !//
//! dP dP '88888P' '88888P8 '88888P8 '88888P' dP !//
//!===========================================================!//
template <typename T> T read() {
T v;
return std::cin >> v, v;
}
template <typename T> std::vector<T> readVec(const std::size_t l) {
std::vector<T> v(l);
for (auto &e : v) {
std::cin >> e;
}
return v;
}
using ld = long double;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr unsigned int MOD = 1000000007;
template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 4;
template <typename F> constexpr F PI = static_cast<F>(3.1415926535897932385);
std::mt19937 mt{std::random_device{}()};
template <typename T> bool chmin(T &a, const T &b) {
return (a > b ? a = b, true : false);
}
template <typename T> bool chmax(T &a, const T &b) {
return (a < b ? a = b, true : false);
}
template <typename T> std::vector<T> Vec(const std::size_t n, T v) {
return std::vector<T>(n, v);
}
template <class... Args> auto Vec(const std::size_t n, Args... args) {
return std::vector<decltype(Vec(args...))>(n, Vec(args...));
}
template <typename T> constexpr T popCount(const T u) {
#ifdef __has_builtin
return u == 0 ? T(0) : (T)__builtin_popcountll(u);
#else
unsigned long long v = static_cast<unsigned long long>(u);
return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL),
v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL),
v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL,
static_cast<T>(v * 0x0101010101010101ULL >> 56 & 0x7f);
#endif
}
template <typename T> constexpr T log2p1(const T u) {
#ifdef __has_builtin
return u == 0 ? T(0) : T(64 - __builtin_clzll(u));
#else
unsigned long long v = static_cast<unsigned long long>(u);
return v = static_cast<unsigned long long>(v), v |= (v >> 1), v |= (v >> 2),
v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32),
popCount(v);
#endif
}
template <typename T> constexpr T clog(const T v) {
return v == 0 ? T(0) : log2p1(v - 1);
}
template <typename T> constexpr T msbp1(const T v) { return log2p1(v); }
template <typename T> constexpr T lsbp1(const T v) {
#ifdef __has_builtin
return __builtin_ffsll(v);
#else
return v == 0 ? T(0) : popCount((v & (-v)) - T(1)) + T(1);
#endif
}
template <typename T> constexpr bool ispow2(const T v) {
return popCount(v) == 1;
}
template <typename T> constexpr T ceil2(const T v) {
return v == 0 ? T(1) : T(1) << log2p1(v - 1);
}
template <typename T> constexpr T floor2(const T v) {
return v == 0 ? T(0) : T(1) << (log2p1(v) - 1);
}
//!=====================================!//
//! 8888ba.88ba oo !//
//! 88 '8b '8b !//
//! 88 88 88 .d8888b. dP 88d888b. !//
//! 88 88 88 88' '88 88 88' '88 !//
//! 88 88 88 88. .88 88 88 88 !//
//! dP dP dP '88888P8 dP dP dP !//
//!=====================================!//
using MP = std::map<ll, ll>;
using P = std::pair<ll, ll>;
MP memo[17][17];
bool calced[17][17];
int main() {
const int N = read<int>();
const auto A = readVec<ll>(N);
if (N == 2) {
return std::cout << A[0] + A[1] << std::endl, 0;
}
auto dfs = [&, N](auto &&self, const int l, const int r) -> MP {
if (r - l == 0) {
return MP{{0, 0}};
}
if (calced[l][r]) {
return memo[l][r];
}
MP l2r, r2l;
for (int i = l; i < r; i++) {
const auto lmp = self(self, l, i);
const auto rmp = self(self, i + 1, r);
for (const auto &lp : lmp) {
const ll al = lp.first, ar = lp.second;
for (const auto &rp : rmp) {
const ll bl = rp.first, br = rp.second;
const ll L = A[i] + ar + bl + al;
const ll R = A[i] + ar + bl + br;
if (l2r.find(L) != l2r.end()) {
const ll oldR = l2r[L];
if (oldR <= R) {
continue;
}
}
if (r2l.find(R) != r2l.end()) {
const ll oldL = r2l[R];
if (oldL <= L) {
continue;
}
}
l2r[L] = R, r2l[R] = L;
}
}
}
ll prevR = INF<ll>;
MP ans;
for (const auto &p : l2r) {
if (prevR > p.second) {
ans.insert(p), prevR = p.second;
}
}
return calced[l][r] = true, memo[l][r] = ans;
};
const auto mp = dfs(dfs, 1, N - 1);
ll min = INF<ll>;
for (const auto &p : mp) {
chmin(min, p.first + p.second);
}
std::cout << min + A[0] + A[N - 1] << std::endl;
return 0;
}
| #include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#define NDEBUG
#define SHOW(...) static_cast<void>(0)
//!===========================================================!//
//! dP dP dP !//
//! 88 88 88 !//
//! 88aaaaa88a .d8888b. .d8888b. .d888b88 .d8888b. 88d888b. !//
//! 88 88 88ooood8 88' '88 88' '88 88ooood8 88' '88 !//
//! 88 88 88. ... 88. .88 88. .88 88. ... 88 !//
//! dP dP '88888P' '88888P8 '88888P8 '88888P' dP !//
//!===========================================================!//
template <typename T> T read() {
T v;
return std::cin >> v, v;
}
template <typename T> std::vector<T> readVec(const std::size_t l) {
std::vector<T> v(l);
for (auto &e : v) {
std::cin >> e;
}
return v;
}
using ld = long double;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr unsigned int MOD = 1000000007;
template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 4;
template <typename F> constexpr F PI = static_cast<F>(3.1415926535897932385);
std::mt19937 mt{std::random_device{}()};
template <typename T> bool chmin(T &a, const T &b) {
return (a > b ? a = b, true : false);
}
template <typename T> bool chmax(T &a, const T &b) {
return (a < b ? a = b, true : false);
}
template <typename T> std::vector<T> Vec(const std::size_t n, T v) {
return std::vector<T>(n, v);
}
template <class... Args> auto Vec(const std::size_t n, Args... args) {
return std::vector<decltype(Vec(args...))>(n, Vec(args...));
}
template <typename T> constexpr T popCount(const T u) {
#ifdef __has_builtin
return u == 0 ? T(0) : (T)__builtin_popcountll(u);
#else
unsigned long long v = static_cast<unsigned long long>(u);
return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL),
v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL),
v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL,
static_cast<T>(v * 0x0101010101010101ULL >> 56 & 0x7f);
#endif
}
template <typename T> constexpr T log2p1(const T u) {
#ifdef __has_builtin
return u == 0 ? T(0) : T(64 - __builtin_clzll(u));
#else
unsigned long long v = static_cast<unsigned long long>(u);
return v = static_cast<unsigned long long>(v), v |= (v >> 1), v |= (v >> 2),
v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32),
popCount(v);
#endif
}
template <typename T> constexpr T clog(const T v) {
return v == 0 ? T(0) : log2p1(v - 1);
}
template <typename T> constexpr T msbp1(const T v) { return log2p1(v); }
template <typename T> constexpr T lsbp1(const T v) {
#ifdef __has_builtin
return __builtin_ffsll(v);
#else
return v == 0 ? T(0) : popCount((v & (-v)) - T(1)) + T(1);
#endif
}
template <typename T> constexpr bool ispow2(const T v) {
return popCount(v) == 1;
}
template <typename T> constexpr T ceil2(const T v) {
return v == 0 ? T(1) : T(1) << log2p1(v - 1);
}
template <typename T> constexpr T floor2(const T v) {
return v == 0 ? T(0) : T(1) << (log2p1(v) - 1);
}
//!=====================================!//
//! 8888ba.88ba oo !//
//! 88 '8b '8b !//
//! 88 88 88 .d8888b. dP 88d888b. !//
//! 88 88 88 88' '88 88 88' '88 !//
//! 88 88 88 88. .88 88 88 88 !//
//! dP dP dP '88888P8 dP dP dP !//
//!=====================================!//
using MP = std::map<ll, ll>;
using P = std::pair<ll, ll>;
MP memo[19][19];
bool calced[19][19];
int main() {
const int N = read<int>();
const auto A = readVec<ll>(N);
if (N == 2) {
return std::cout << A[0] + A[1] << std::endl, 0;
}
auto dfs = [&, N](auto &&self, const int l, const int r) -> MP {
if (r - l == 0) {
return MP{{0, 0}};
}
if (calced[l][r]) {
return memo[l][r];
}
MP l2r, r2l;
for (int i = l; i < r; i++) {
const auto lmp = self(self, l, i);
const auto rmp = self(self, i + 1, r);
for (const auto &lp : lmp) {
const ll al = lp.first, ar = lp.second;
for (const auto &rp : rmp) {
const ll bl = rp.first, br = rp.second;
const ll L = A[i] + ar + bl + al;
const ll R = A[i] + ar + bl + br;
if (l2r.find(L) != l2r.end()) {
const ll oldR = l2r[L];
if (oldR <= R) {
continue;
}
}
if (r2l.find(R) != r2l.end()) {
const ll oldL = r2l[R];
if (oldL <= L) {
continue;
}
}
l2r[L] = R, r2l[R] = L;
}
}
}
ll prevR = INF<ll>;
MP ans;
for (const auto &p : l2r) {
if (prevR > p.second) {
ans.insert(p), prevR = p.second;
}
}
return calced[l][r] = true, memo[l][r] = ans;
};
const auto mp = dfs(dfs, 1, N - 1);
ll min = INF<ll>;
for (const auto &p : mp) {
chmin(min, p.first + p.second);
}
std::cout << min + A[0] + A[N - 1] << std::endl;
return 0;
}
| replace | 95 | 97 | 95 | 97 | 0 | |
p02978 | C++ | Time Limit Exceeded | // https://atcoder.jp/contests/agc035/tasks/agc035_d
#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define CIN_ONLY
#define DECIMAL_DIGITS 10
#ifdef BTK
/*<head>*/
#include "Template.hpp"
/*</head>*/
#else
/*<body>*/
/* #region header */
/* #region 1*/
/**
* @file Macro.hpp
* @author btk
* @brief マクロとか,LLとか
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*
*/
using namespace std;
//! LL
using LL = long long;
/**
* @def DEBUG
* @brief デバッグ用のif文 提出時はif(0)で実行されない
*/
/*</head>*/
#ifdef BTK
#define DEBUG if (1)
#else
#ifdef CIN_ONLY
#define FAST_IO
#endif
#define DEBUG if (0)
#endif
/**
* @def ALL(v)
* @brief
* ALLマクロ
*/
#define ALL(v) (v).begin(), (v).end()
/**
* @def REC(ret, ...)
* @brief
* 再帰ラムダをするためのマクロ
*/
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
/**
* @brief
* rangeで生まれる使わない変数を消す用(警告消し)
*/
template <typename T> inline T &unused_var(T &v) { return v; }
/* #endregion */
/* #region 2*/
/**
* @file IO.hpp
* @author btk
* @brief cin高速化とか,出力の小数桁固定とか
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*/
/**
* @brief 入出力の設定を行うための構造体
*/
struct cww {
/**
* @brief Construct a new cww::cww object
* @details
* CIN_ONLYを定義すると,submit時にcinとscanfの同期を切る設定が走る
* DECIMAL_DIGITSを定義すると,doubleの出力時指定した桁数分小数部を吐くようになる
*/
cww() {
#ifdef FAST_IO
ios::sync_with_stdio(false);
cin.tie(0);
#endif
#ifdef DECIMAL_DIGITS
cout << fixed;
cout << setprecision(DECIMAL_DIGITS);
#endif
}
};
//! 入出力設定構造体を実体化
cww star;
/**
* @brief
* vectorに直接cin流すためのやつ
* @tparam T
* @param is
* @param v
* @return istream&
*/
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &it : v)
is >> it;
return is;
}
/* #endregion */
/* #region 3*/
/**
* @file Loop.hpp
* @author btk
* @brief rangeとかループ系のクラス
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*
*/
/**
* @brief
* rangeを逆向きに操作したいとき用
* @details
* ループの範囲は[bg,ed)なので注意
* @see range
*/
class reverse_range {
private:
struct I {
int x;
int operator*() { return x - 1; }
bool operator!=(I &lhs) { return x > lhs.x; }
void operator++() { --x; }
};
I i, n;
public:
/**
* @brief Construct a new reverse range object
*
* @param n
*/
reverse_range(int n) : i({0}), n({n}) {}
/**
* @brief Construct a new reverse range object
*
* @param i
* @param n
*/
reverse_range(int i, int n) : i({i}), n({n}) {}
/**
* @brief
* begin iterator
* @return I&
*/
I &begin() { return n; }
/**
* @brief
* end iterator
* @return I&
*/
I &end() { return i; }
};
/**
* @brief
* python みたいな range-based for を実現
* @details
* ループの範囲は[bg,ed)なので注意
* !つけると逆向きにループが回る (reverse_range)
* 空間計算量はO(1)
* 使わない変数ができて警告が出がちなので,unused_varとかを使って警告消しするとよい
*/
class range {
private:
struct I {
int x;
int operator*() { return x; }
bool operator!=(I &lhs) { return x < lhs.x; }
void operator++() { ++x; }
};
I i, n;
public:
/**
* @brief Construct a new range object
*
* @param n
*/
range(int n) : i({0}), n({n}) {}
/**
* @brief Construct a new range object
*
* @param i
* @param n
*/
range(int i, int n) : i({i}), n({n}) {}
/**
* @brief
* begin iterator
* @return I&
*/
I &begin() { return i; }
/**
* @brief
* end iterator
* @return I&
*/
I &end() { return n; }
/**
* @brief
* 逆向きに参照するrange(reverse_rangeを取得できるs)
* @return reverse_range
*/
reverse_range operator!() { return reverse_range(*i, *n); }
};
/* #endregion */
/* #region 4*/
/**
* @file MinMaxOperation.hpp
* @author btk
* @brief 最大値とか最小値を求める
* @version 0.1
* @date 2019-07-04
*
* @copyright Copyright (c) 2019
*
*/
/**
* @brief 2項の最小値求める
*
* @tparam T
*/
template <typename T> struct min_op {
/**
* @brief 本体
*
* @param l
* @param r
* @return T
*/
static T exec(const T l, const T r) { return l < r ? l : r; }
};
/**
* @brief 2項の最大値求める
*
* @tparam T
*/
template <typename T> struct max_op {
/**
* @brief 本体
*
* @param l
* @param r
* @return T
*/
static T exec(const T l, const T r) { return l > r ? l : r; }
};
/**
* @brief テンプレート再帰の末尾
*
* @tparam F 二項演算
* @tparam T
* @param v
* @return T
*/
template <typename F, typename T> inline T multi_op(T &&v) { return v; }
/**
* @brief 複数項における演算の結果を返す
*
* @tparam F
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename F, typename T, typename... Ts>
inline T multi_op(const T head, Ts &&...tail) {
return F::exec(head, multi_op<F>(tail...));
}
/**
* @brief 複数項の最小値
* @see multi_op
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename T, typename... Ts>
inline T multi_min(T &&head, Ts &&...tail) {
return multi_op<min_op<T>>(head, tail...);
}
/**
* @brief 複数項の最大値
* @see multi_op
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename T, typename... Ts>
inline T multi_max(T &&head, Ts &&...tail) {
return multi_op<max_op<T>>(head, tail...);
}
/**
* @brief
* 先頭の値をFで参照する関数に基づいて変更できたらする
* @tparam F
* @tparam T
* @tparam Ts
* @param target
* @param candidates
* @return true
* @return false
*/
template <typename F, typename T, typename... Ts>
inline bool ch_op(T &target, Ts &&...candidates) {
const T old = target;
target = multi_op<F>(target, candidates...);
return old != target;
}
/**
* @brief change min
* @tparam T 型
* @param target 変更する値
* @param candidates
* @return 更新があればtrue
*/
template <typename T, typename... Ts>
inline bool chmin(T &target, Ts &&...candidates) {
return ch_op<min_op<T>>(target, candidates...);
}
/**
* @brief chminのmax版
* @see chmin
* @tparam T 型
* @param target 変更する値
* @param candidates
* @return 更新があればtrue
*/
template <typename T, typename... Ts>
inline bool chmax(T &target, Ts &&...candidates) {
return ch_op<max_op<T>>(target, candidates...);
}
/* #endregion */
/* #region 5*/
/**
* @file Random.hpp
* @author btk
* @brief 乱数生成系
* @version 0.1
* @date 2019-07-13
* @copyright Copyright (c) 2019
*/
//! 乱数のシード値をプロセスIDとして取得
const pid_t pid = getpid();
/**
* @brief XorShift32の実装
*/
class XorShift32 {
private:
//! XorShiftの現在の値
unsigned value;
/**
* @brief XorShift32のアルゴリズムに基づいて value を更新
*/
inline void update() {
value = value ^ (value << 13);
value = value ^ (value >> 17);
value = value ^ (value << 5);
}
/**
* @brief 値を更新し,更新前の値を返却
* @return unsigned 呼び出し時の value を用いる
*/
inline unsigned get() {
unsigned v = value;
update();
return v;
}
public:
/**
* @brief [0, 2^bit) の範囲の乱数値を取り出す
* @tparam デフォルトは31
* @return int
*/
template <int bit = 31> inline int next_int() {
return (int)(get() >> (32 - bit));
}
/**
* @brief [-2^bit,2^bit)の範囲の乱数値を取り出す
* @tparam デフォルトは31
* @return int
*/
template <int bit = 31> inline int next_signed() {
unsigned v = get();
return (int)((v >> (31 - bit)) - (1 << (bit)));
}
/**
* @brief next_int呼び出し時の最大値を取得
* @tparam 31
* @return int
*/
template <int bit = 31> inline int range_max() {
return (int)((1u << bit) - 1);
};
/**
* @brief Construct a new XorShift32 object
* @param seed
* @details 初期シードをvalueとするXorShift32のインスタンスを生成
*/
XorShift32(const unsigned seed) {
value = seed;
update();
}
/**
* @brief Construct a new XorShift 32 object
* @details 初期シードをプロセスIDとするXorShift32のインスタンスを生成
*/
XorShift32() : XorShift32(pid) {}
};
/* #endregion */
/* #region 6*/
/**
* @file Template.hpp
* @brief 競技プログラミング用テンプレート
* @author btk15049
* @date 2019/05/02
*/
/* #endregion */
/* #endregion */
/*</body>*/
#endif
int N;
LL A[20];
using P = pair<LL, LL>;
vector<P> dp[20][20];
int main() {
cin >> N;
for (int i : range(N)) {
cin >> A[i];
}
for (int i : range(N)) {
dp[i][i + 1].push_back({0ll, 0ll});
}
for (const int l : range(2, N)) {
for (const int i : range(N)) {
const int j = i + l;
if (j > N)
continue;
for (const int k : range(i + 1, j)) {
for (auto &p : dp[i][k]) {
for (auto &q : dp[k][j]) {
LL lv = p.first;
LL rv = q.second;
LL mv = p.second + A[k] + q.first;
dp[i][j].push_back({lv + mv, mv + rv});
// cerr << dp[i][j].back() << endl;
}
}
}
sort(ALL(dp[i][j]));
int num = 1;
for (int k : range(1, dp[i][j].size())) {
if (dp[i][j][num - 1].second < dp[i][j][k].second) {
} else {
dp[i][j][num] = dp[i][j][k];
num++;
}
}
dp[i][j].resize(num);
// cerr << i << " " << j << " " << dp[i][j].size() << endl;
}
}
LL ret = 1e18;
for (auto p : dp[0][N - 1]) {
chmin(ret, A[0] + A[N - 1] + p.first + p.second);
}
cout << ret << endl;
return 0;
} | // https://atcoder.jp/contests/agc035/tasks/agc035_d
#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define CIN_ONLY
#define DECIMAL_DIGITS 10
#ifdef BTK
/*<head>*/
#include "Template.hpp"
/*</head>*/
#else
/*<body>*/
/* #region header */
/* #region 1*/
/**
* @file Macro.hpp
* @author btk
* @brief マクロとか,LLとか
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*
*/
using namespace std;
//! LL
using LL = long long;
/**
* @def DEBUG
* @brief デバッグ用のif文 提出時はif(0)で実行されない
*/
/*</head>*/
#ifdef BTK
#define DEBUG if (1)
#else
#ifdef CIN_ONLY
#define FAST_IO
#endif
#define DEBUG if (0)
#endif
/**
* @def ALL(v)
* @brief
* ALLマクロ
*/
#define ALL(v) (v).begin(), (v).end()
/**
* @def REC(ret, ...)
* @brief
* 再帰ラムダをするためのマクロ
*/
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
/**
* @brief
* rangeで生まれる使わない変数を消す用(警告消し)
*/
template <typename T> inline T &unused_var(T &v) { return v; }
/* #endregion */
/* #region 2*/
/**
* @file IO.hpp
* @author btk
* @brief cin高速化とか,出力の小数桁固定とか
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*/
/**
* @brief 入出力の設定を行うための構造体
*/
struct cww {
/**
* @brief Construct a new cww::cww object
* @details
* CIN_ONLYを定義すると,submit時にcinとscanfの同期を切る設定が走る
* DECIMAL_DIGITSを定義すると,doubleの出力時指定した桁数分小数部を吐くようになる
*/
cww() {
#ifdef FAST_IO
ios::sync_with_stdio(false);
cin.tie(0);
#endif
#ifdef DECIMAL_DIGITS
cout << fixed;
cout << setprecision(DECIMAL_DIGITS);
#endif
}
};
//! 入出力設定構造体を実体化
cww star;
/**
* @brief
* vectorに直接cin流すためのやつ
* @tparam T
* @param is
* @param v
* @return istream&
*/
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &it : v)
is >> it;
return is;
}
/* #endregion */
/* #region 3*/
/**
* @file Loop.hpp
* @author btk
* @brief rangeとかループ系のクラス
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*
*/
/**
* @brief
* rangeを逆向きに操作したいとき用
* @details
* ループの範囲は[bg,ed)なので注意
* @see range
*/
class reverse_range {
private:
struct I {
int x;
int operator*() { return x - 1; }
bool operator!=(I &lhs) { return x > lhs.x; }
void operator++() { --x; }
};
I i, n;
public:
/**
* @brief Construct a new reverse range object
*
* @param n
*/
reverse_range(int n) : i({0}), n({n}) {}
/**
* @brief Construct a new reverse range object
*
* @param i
* @param n
*/
reverse_range(int i, int n) : i({i}), n({n}) {}
/**
* @brief
* begin iterator
* @return I&
*/
I &begin() { return n; }
/**
* @brief
* end iterator
* @return I&
*/
I &end() { return i; }
};
/**
* @brief
* python みたいな range-based for を実現
* @details
* ループの範囲は[bg,ed)なので注意
* !つけると逆向きにループが回る (reverse_range)
* 空間計算量はO(1)
* 使わない変数ができて警告が出がちなので,unused_varとかを使って警告消しするとよい
*/
class range {
private:
struct I {
int x;
int operator*() { return x; }
bool operator!=(I &lhs) { return x < lhs.x; }
void operator++() { ++x; }
};
I i, n;
public:
/**
* @brief Construct a new range object
*
* @param n
*/
range(int n) : i({0}), n({n}) {}
/**
* @brief Construct a new range object
*
* @param i
* @param n
*/
range(int i, int n) : i({i}), n({n}) {}
/**
* @brief
* begin iterator
* @return I&
*/
I &begin() { return i; }
/**
* @brief
* end iterator
* @return I&
*/
I &end() { return n; }
/**
* @brief
* 逆向きに参照するrange(reverse_rangeを取得できるs)
* @return reverse_range
*/
reverse_range operator!() { return reverse_range(*i, *n); }
};
/* #endregion */
/* #region 4*/
/**
* @file MinMaxOperation.hpp
* @author btk
* @brief 最大値とか最小値を求める
* @version 0.1
* @date 2019-07-04
*
* @copyright Copyright (c) 2019
*
*/
/**
* @brief 2項の最小値求める
*
* @tparam T
*/
template <typename T> struct min_op {
/**
* @brief 本体
*
* @param l
* @param r
* @return T
*/
static T exec(const T l, const T r) { return l < r ? l : r; }
};
/**
* @brief 2項の最大値求める
*
* @tparam T
*/
template <typename T> struct max_op {
/**
* @brief 本体
*
* @param l
* @param r
* @return T
*/
static T exec(const T l, const T r) { return l > r ? l : r; }
};
/**
* @brief テンプレート再帰の末尾
*
* @tparam F 二項演算
* @tparam T
* @param v
* @return T
*/
template <typename F, typename T> inline T multi_op(T &&v) { return v; }
/**
* @brief 複数項における演算の結果を返す
*
* @tparam F
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename F, typename T, typename... Ts>
inline T multi_op(const T head, Ts &&...tail) {
return F::exec(head, multi_op<F>(tail...));
}
/**
* @brief 複数項の最小値
* @see multi_op
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename T, typename... Ts>
inline T multi_min(T &&head, Ts &&...tail) {
return multi_op<min_op<T>>(head, tail...);
}
/**
* @brief 複数項の最大値
* @see multi_op
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename T, typename... Ts>
inline T multi_max(T &&head, Ts &&...tail) {
return multi_op<max_op<T>>(head, tail...);
}
/**
* @brief
* 先頭の値をFで参照する関数に基づいて変更できたらする
* @tparam F
* @tparam T
* @tparam Ts
* @param target
* @param candidates
* @return true
* @return false
*/
template <typename F, typename T, typename... Ts>
inline bool ch_op(T &target, Ts &&...candidates) {
const T old = target;
target = multi_op<F>(target, candidates...);
return old != target;
}
/**
* @brief change min
* @tparam T 型
* @param target 変更する値
* @param candidates
* @return 更新があればtrue
*/
template <typename T, typename... Ts>
inline bool chmin(T &target, Ts &&...candidates) {
return ch_op<min_op<T>>(target, candidates...);
}
/**
* @brief chminのmax版
* @see chmin
* @tparam T 型
* @param target 変更する値
* @param candidates
* @return 更新があればtrue
*/
template <typename T, typename... Ts>
inline bool chmax(T &target, Ts &&...candidates) {
return ch_op<max_op<T>>(target, candidates...);
}
/* #endregion */
/* #region 5*/
/**
* @file Random.hpp
* @author btk
* @brief 乱数生成系
* @version 0.1
* @date 2019-07-13
* @copyright Copyright (c) 2019
*/
//! 乱数のシード値をプロセスIDとして取得
const pid_t pid = getpid();
/**
* @brief XorShift32の実装
*/
class XorShift32 {
private:
//! XorShiftの現在の値
unsigned value;
/**
* @brief XorShift32のアルゴリズムに基づいて value を更新
*/
inline void update() {
value = value ^ (value << 13);
value = value ^ (value >> 17);
value = value ^ (value << 5);
}
/**
* @brief 値を更新し,更新前の値を返却
* @return unsigned 呼び出し時の value を用いる
*/
inline unsigned get() {
unsigned v = value;
update();
return v;
}
public:
/**
* @brief [0, 2^bit) の範囲の乱数値を取り出す
* @tparam デフォルトは31
* @return int
*/
template <int bit = 31> inline int next_int() {
return (int)(get() >> (32 - bit));
}
/**
* @brief [-2^bit,2^bit)の範囲の乱数値を取り出す
* @tparam デフォルトは31
* @return int
*/
template <int bit = 31> inline int next_signed() {
unsigned v = get();
return (int)((v >> (31 - bit)) - (1 << (bit)));
}
/**
* @brief next_int呼び出し時の最大値を取得
* @tparam 31
* @return int
*/
template <int bit = 31> inline int range_max() {
return (int)((1u << bit) - 1);
};
/**
* @brief Construct a new XorShift32 object
* @param seed
* @details 初期シードをvalueとするXorShift32のインスタンスを生成
*/
XorShift32(const unsigned seed) {
value = seed;
update();
}
/**
* @brief Construct a new XorShift 32 object
* @details 初期シードをプロセスIDとするXorShift32のインスタンスを生成
*/
XorShift32() : XorShift32(pid) {}
};
/* #endregion */
/* #region 6*/
/**
* @file Template.hpp
* @brief 競技プログラミング用テンプレート
* @author btk15049
* @date 2019/05/02
*/
/* #endregion */
/* #endregion */
/*</body>*/
#endif
int N;
LL A[20];
using P = pair<LL, LL>;
vector<P> dp[20][20];
int main() {
cin >> N;
for (int i : range(N)) {
cin >> A[i];
}
for (int i : range(N)) {
dp[i][i + 1].push_back({0ll, 0ll});
}
for (const int l : range(2, N)) {
for (const int i : range(N)) {
const int j = i + l;
if (j > N)
continue;
for (const int k : range(i + 1, j)) {
for (auto &p : dp[i][k]) {
for (auto &q : dp[k][j]) {
LL lv = p.first;
LL rv = q.second;
LL mv = p.second + A[k] + q.first;
dp[i][j].push_back({lv + mv, mv + rv});
// cerr << dp[i][j].back() << endl;
}
}
}
sort(ALL(dp[i][j]));
int num = 1;
for (int k : range(1, dp[i][j].size())) {
if (dp[i][j][num - 1].second <= dp[i][j][k].second) {
} else {
dp[i][j][num] = dp[i][j][k];
num++;
}
}
dp[i][j].resize(num);
// cerr << i << " " << j << " " << dp[i][j].size() << endl;
}
}
LL ret = 1e18;
for (auto p : dp[0][N - 1]) {
chmin(ret, A[0] + A[N - 1] + p.first + p.second);
}
cout << ret << endl;
return 0;
} | replace | 494 | 495 | 494 | 495 | TLE | |
p02978 | C++ | Time Limit Exceeded | #include <cstdio>
#include <iostream>
#include <map>
using namespace std;
const int N = 25;
const long long INF = 4557430888798830399;
int n;
int a[N];
map<pair<int, int>, long long> f[N][N];
long long dfs(int l, int r, int p, int q) {
if (l > r)
return 0;
if (f[l][r][{p, q}])
return f[l][r][{p, q}];
long long res = INF;
for (int i = l; i <= r; i++)
res = min(res, dfs(l, i - 1, p, p + q) + dfs(i + 1, r, p + q, q) +
1LL * (p + q) * a[i]);
return f[l][r][{p, q}] = res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf("%lld", dfs(2, n - 1, 1, 1) + a[1] + a[n]);
return 0;
}
| #include <cstdio>
#include <iostream>
#include <map>
using namespace std;
const int N = 25;
const long long INF = 4557430888798830399;
int n;
int a[N];
map<pair<int, int>, long long> f[N][N];
long long dfs(int l, int r, int p, int q) {
if (l > r)
return 0;
if (f[l][r].count({p, q}))
return f[l][r][{p, q}];
long long res = INF;
for (int i = l; i <= r; i++)
res = min(res, dfs(l, i - 1, p, p + q) + dfs(i + 1, r, p + q, q) +
1LL * (p + q) * a[i]);
return f[l][r][{p, q}] = res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf("%lld", dfs(2, n - 1, 1, 1) + a[1] + a[n]);
return 0;
}
| replace | 12 | 13 | 12 | 13 | TLE | |
p02978 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
template <typename T>
vector<vector<T>> Make2DVector(int d1, int d2, T default_value) {
return vector<vector<T>>(d1, vector<T>(d2, default_value));
}
struct PairHash {
template <class T1, class T2>
std::size_t operator()(const std::pair<T1, T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
// Mainly for demonstration purposes, i.e. works but is overly simple
// In the real world, use sth. like boost.hash_combine
return h1 ^ h2;
}
};
template <class T> inline bool UpdateMin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int64> xs(n);
for (int i = 0; i < n; i++)
cin >> xs[i];
// auto hash_fn = [](pair<int64, int64> p) {
// return hash<int64>()(p.first) * 1000000007 + hash<int64>()(p.second);
// };
auto dp = Make2DVector(n, n, vector<pair<int64, int64>>());
function<void(int, int)> solve = [&](int l, int r) {
// cout << "l: " << l << " r: " << r << endl;
if (!dp[l][r].empty())
return;
if (r - l == 1) {
dp[l][r].push_back(make_pair(0, 0));
return;
}
for (int m = l + 1; m <= r - 1; m++) {
solve(l, m);
solve(m, r);
for (const auto &pair1 : dp[l][m]) {
for (const auto &pair2 : dp[m][r]) {
int64 next_l = pair1.first + xs[m] + pair1.second + pair2.first;
int64 next_r = xs[m] + pair1.second + pair2.first + pair2.second;
dp[l][r].push_back(make_pair(next_l, next_r));
}
}
}
};
solve(0, n - 1);
int64 ans = INT64_MAX;
for (const auto &pair : dp[0][n - 1]) {
// cout << pair.first << " " << pair.second << endl;
UpdateMin(ans, xs[0] + pair.first + xs[n - 1] + pair.second);
}
// cout << "size: " << dp[0][n - 1].size() << endl;
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
template <typename T>
vector<vector<T>> Make2DVector(int d1, int d2, T default_value) {
return vector<vector<T>>(d1, vector<T>(d2, default_value));
}
struct PairHash {
template <class T1, class T2>
std::size_t operator()(const std::pair<T1, T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
// Mainly for demonstration purposes, i.e. works but is overly simple
// In the real world, use sth. like boost.hash_combine
return h1 ^ h2;
}
};
template <class T> inline bool UpdateMin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int64> xs(n);
for (int i = 0; i < n; i++)
cin >> xs[i];
// auto hash_fn = [](pair<int64, int64> p) {
// return hash<int64>()(p.first) * 1000000007 + hash<int64>()(p.second);
// };
auto dp = Make2DVector(n, n, vector<pair<int64, int64>>());
function<void(int, int)> solve = [&](int l, int r) {
// cout << "l: " << l << " r: " << r << endl;
if (!dp[l][r].empty())
return;
if (r - l == 1) {
dp[l][r].push_back(make_pair(0, 0));
return;
}
for (int m = l + 1; m <= r - 1; m++) {
solve(l, m);
solve(m, r);
for (const auto &pair1 : dp[l][m]) {
for (const auto &pair2 : dp[m][r]) {
int64 next_l = pair1.first + xs[m] + pair1.second + pair2.first;
int64 next_r = xs[m] + pair1.second + pair2.first + pair2.second;
dp[l][r].push_back(make_pair(next_l, next_r));
}
}
vector<pair<int64, int64>> &v = dp[l][r];
sort(v.begin(), v.end());
vector<pair<int64, int64>> new_v;
int64 min_second = INT64_MAX;
for (const auto &p : v) {
if (UpdateMin(min_second, p.second)) {
new_v.push_back(p);
}
}
dp[l][r] = new_v;
}
};
solve(0, n - 1);
int64 ans = INT64_MAX;
for (const auto &pair : dp[0][n - 1]) {
// cout << pair.first << " " << pair.second << endl;
UpdateMin(ans, xs[0] + pair.first + xs[n - 1] + pair.second);
}
// cout << "size: " << dp[0][n - 1].size() << endl;
cout << ans << endl;
}
| insert | 64 | 64 | 64 | 74 | MLE | |
p02978 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define PR pair<LL, LL>
const int N = 20;
LL a[N];
vector<PR> vp[N][N];
PR calpr(PR u, PR v, LL t) {
LL num = u.sc + v.fr + t;
return mp(u.fr + num, v.sc + num);
}
LL calval(PR t, LL u, LL v) { return t.fr * u + t.sc * v; }
void pus(int n, int l, int r, int u1, int v1, int u2, int v2) {
PR m1, m2;
LL val1 = 1e18, val2 = 1e18;
int len = r - l + 1;
for (int k = l; k <= r; k++) {
int l1 = k - l, l2 = r - k;
for (auto u : vp[l1][l])
for (auto v : vp[l2][k + 1]) {
PR t = calpr(u, v, a[k]);
LL valt1 = calval(t, u1, v1);
if (valt1 < val1)
m1 = t, val1 = valt1;
if (len > n - 2)
continue;
LL valt2 = calval(t, u2, v2);
if (valt2 < val2)
m2 = t, val2 = valt2;
}
}
vp[len][l].pb(m1);
if (len == n - 2)
vp[len][l].pb(m2);
}
void work(int n) {
pus(n, 1, n - 2, 1, 2, 1, 3);
pus(n, 2, n - 1, 2, 3, 3, 2);
pus(n, 3, n, 2, 1, 3, 1);
pus(n, 1, n - 1, 1, 2, 0, 0);
pus(n, 2, n, 2, 1, 0, 0);
pus(n, 1, n, 1, 1, 0, 0);
}
int main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n;
cin >> n;
for (int i = 0; i < n; i++)
scanf("%lld", a + i);
LL ans = a[0] + a[n - 1];
n -= 2;
for (int i = 1; i <= n; i++)
vp[1][i].pb(mp(a[i], a[i])), vp[0][i].pb(mp(0, 0));
vp[0][n + 1].pb(mp(0, 0));
for (int p = 2; p <= n - 3; p++) {
for (int i = 1; i + p - 1 <= n; i++) {
int j = i + p - 1;
for (int k = i; k <= j; k++) {
int l1 = k - i, l2 = j - k;
for (auto u : vp[l1][i])
for (auto v : vp[l2][k + 1]) {
vp[p][i].pb(calpr(u, v, a[k]));
}
}
}
}
work(n);
PR p = vp[n][1][0];
ans += p.fr + p.sc;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define PR pair<LL, LL>
const int N = 20;
LL a[N];
vector<PR> vp[N][N];
PR calpr(PR u, PR v, LL t) {
LL num = u.sc + v.fr + t;
return mp(u.fr + num, v.sc + num);
}
LL calval(PR t, LL u, LL v) { return t.fr * u + t.sc * v; }
void pus(int n, int l, int r, int u1, int v1, int u2, int v2) {
if (l > r)
return;
PR m1, m2;
LL val1 = 1e18, val2 = 1e18;
int len = r - l + 1;
for (int k = l; k <= r; k++) {
int l1 = k - l, l2 = r - k;
for (auto u : vp[l1][l])
for (auto v : vp[l2][k + 1]) {
PR t = calpr(u, v, a[k]);
LL valt1 = calval(t, u1, v1);
if (valt1 < val1)
m1 = t, val1 = valt1;
if (len > n - 2)
continue;
LL valt2 = calval(t, u2, v2);
if (valt2 < val2)
m2 = t, val2 = valt2;
}
}
vp[len][l].pb(m1);
if (len == n - 2)
vp[len][l].pb(m2);
}
void work(int n) {
pus(n, 1, n - 2, 1, 2, 1, 3);
pus(n, 2, n - 1, 2, 3, 3, 2);
pus(n, 3, n, 2, 1, 3, 1);
pus(n, 1, n - 1, 1, 2, 0, 0);
pus(n, 2, n, 2, 1, 0, 0);
pus(n, 1, n, 1, 1, 0, 0);
}
int main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n;
cin >> n;
for (int i = 0; i < n; i++)
scanf("%lld", a + i);
LL ans = a[0] + a[n - 1];
n -= 2;
for (int i = 1; i <= n; i++)
vp[1][i].pb(mp(a[i], a[i])), vp[0][i].pb(mp(0, 0));
vp[0][n + 1].pb(mp(0, 0));
for (int p = 2; p <= n - 3; p++) {
for (int i = 1; i + p - 1 <= n; i++) {
int j = i + p - 1;
for (int k = i; k <= j; k++) {
int l1 = k - i, l2 = j - k;
for (auto u : vp[l1][i])
for (auto v : vp[l2][k + 1]) {
vp[p][i].pb(calpr(u, v, a[k]));
}
}
}
}
work(n);
PR p = vp[n][1][0];
ans += p.fr + p.sc;
cout << ans << endl;
return 0;
}
| insert | 17 | 17 | 17 | 19 | 0 | |
p02978 | C++ | Runtime Error | #include <bits/stdc++.h>
#define meow(args...) fprintf(stderr, args)
template <class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {
return b < a ? (a = b, true) : false;
}
template <class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {
return a < b ? (a = b, true) : false;
}
template <class Type> Type read() {
Type a;
bool b;
unsigned char c;
while (c = getchar() - 48, (c > 9) & (c != 253))
;
for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c)
;
return b ? -a : a;
}
auto rd = read<int>;
int n, a[19], l[1 << 16], r[1 << 16], u[1 << 16], v[1 << 16];
long long f[1 << 16][19][19];
int main() {
n = rd();
for (int i = 1; i <= n; ++i)
a[i] = rd();
if (n == 2) {
printf("%d\n", a[1] + a[n]);
return 0;
}
u[1] = v[1] = l[1] = 1;
r[1] = n;
for (int i = 1; i < 1 << (n - 3); ++i) {
u[i << 1] = u[i];
v[i << 1] = u[i << 1 | 1] = u[i] + v[i];
v[i << 1 | 1] = v[i];
l[i << 1] = l[i], r[i << 1] = r[i] - 1;
l[i << 1 | 1] = l[i] + 1, r[i << 1 | 1] = r[i];
}
for (int x = 1 << (n - 2); --x;) {
for (int i = l[x]; i <= r[x]; ++i)
for (int j = i + 2; j <= r[x]; ++j)
f[x][i][j] = 4e18;
for (int i = l[x]; i < r[x]; ++i)
for (int j = i + 1; j < r[x]; ++j)
for (int k = j + 1; k <= r[x]; ++k)
cmin(f[x][i][k], f[x << 1][i][j] + f[x << 1 | 1][j][k] +
(long long)(u[x] + v[x]) * a[j]);
}
printf("%lld\n", a[1] + a[n] + f[1][1][n]);
return 0;
}
| #include <bits/stdc++.h>
#define meow(args...) fprintf(stderr, args)
template <class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {
return b < a ? (a = b, true) : false;
}
template <class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {
return a < b ? (a = b, true) : false;
}
template <class Type> Type read() {
Type a;
bool b;
unsigned char c;
while (c = getchar() - 48, (c > 9) & (c != 253))
;
for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c)
;
return b ? -a : a;
}
auto rd = read<int>;
int n, a[19], l[1 << 16], r[1 << 16], u[1 << 16], v[1 << 16];
long long f[1 << 17][19][19];
int main() {
n = rd();
for (int i = 1; i <= n; ++i)
a[i] = rd();
if (n == 2) {
printf("%d\n", a[1] + a[n]);
return 0;
}
u[1] = v[1] = l[1] = 1;
r[1] = n;
for (int i = 1; i < 1 << (n - 3); ++i) {
u[i << 1] = u[i];
v[i << 1] = u[i << 1 | 1] = u[i] + v[i];
v[i << 1 | 1] = v[i];
l[i << 1] = l[i], r[i << 1] = r[i] - 1;
l[i << 1 | 1] = l[i] + 1, r[i << 1 | 1] = r[i];
}
for (int x = 1 << (n - 2); --x;) {
for (int i = l[x]; i <= r[x]; ++i)
for (int j = i + 2; j <= r[x]; ++j)
f[x][i][j] = 4e18;
for (int i = l[x]; i < r[x]; ++i)
for (int j = i + 1; j < r[x]; ++j)
for (int k = j + 1; k <= r[x]; ++k)
cmin(f[x][i][k], f[x << 1][i][j] + f[x << 1 | 1][j][k] +
(long long)(u[x] + v[x]) * a[j]);
}
printf("%lld\n", a[1] + a[n] + f[1][1][n]);
return 0;
}
| replace | 21 | 22 | 21 | 22 | -11 | |
p02978 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
#define FOR(i, begin, end) \
for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec)
is >> v;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) {
os << "deq[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
os << "(" << pa.first << "," << pa.second << ")";
return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) {
os << "{";
for (auto v : mp)
os << v.first << "=>" << v.second << ",";
os << "}";
return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) {
os << "{";
for (auto v : mp)
os << v.first << "=>" << v.second << ",";
os << "}";
return os;
}
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args>
void ndarray(vector<T> &vec, int len, Args... args) {
vec.resize(len);
for (auto &v : vec)
ndarray(v, args...);
}
template <typename T> bool mmax(T &m, const T q) {
if (m < q) {
m = q;
return true;
} else
return false;
}
template <typename T> bool mmin(T &m, const T q) {
if (m > q) {
m = q;
return true;
} else
return false;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
return make_pair(l.first - r.first, l.second - r.second);
}
#define dbg(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
#define FI first
#define SE second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
int main() {
int N;
cin >> N;
vector<lint> A(N);
cin >> A;
vector<vector<vector<plint>>> dp;
dp.resize(N);
REP(i, N) {
dp[i].resize(N + 1);
dp[i][i].emplace_back(0, 0);
dp[i][i + 1].emplace_back(A[i], A[i]);
}
FOR(w, 1, N - 1) FOR(l, 1, N - w) {
// w: 消去区間長さ
int r = l + w;
vector<plint> t;
FOR(c, l, r) {
for (auto pl : dp[l][c])
for (auto pr : dp[c + 1][r]) {
lint v = pl.second + pr.first + A[c];
t.emplace_back(pl.first + v, pr.second + v);
}
}
dp[l][r] = t;
}
lint ret = 1e18;
for (auto p : dp[1][N - 1])
mmin(ret, p.first + p.second + A[0] + A.back());
cout << ret << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
#define FOR(i, begin, end) \
for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec)
is >> v;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) {
os << "deq[";
for (auto v : vec)
os << v << ",";
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
os << "(" << pa.first << "," << pa.second << ")";
return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) {
os << "{";
for (auto v : mp)
os << v.first << "=>" << v.second << ",";
os << "}";
return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) {
os << "{";
for (auto v : mp)
os << v.first << "=>" << v.second << ",";
os << "}";
return os;
}
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args>
void ndarray(vector<T> &vec, int len, Args... args) {
vec.resize(len);
for (auto &v : vec)
ndarray(v, args...);
}
template <typename T> bool mmax(T &m, const T q) {
if (m < q) {
m = q;
return true;
} else
return false;
}
template <typename T> bool mmin(T &m, const T q) {
if (m > q) {
m = q;
return true;
} else
return false;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
return make_pair(l.first - r.first, l.second - r.second);
}
#define dbg(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
#define FI first
#define SE second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
int main() {
int N;
cin >> N;
vector<lint> A(N);
cin >> A;
vector<vector<vector<plint>>> dp;
dp.resize(N);
REP(i, N) {
dp[i].resize(N + 1);
dp[i][i].emplace_back(0, 0);
dp[i][i + 1].emplace_back(A[i], A[i]);
}
FOR(w, 1, N - 1) FOR(l, 1, N - w) {
// w: 消去区間長さ
int r = l + w;
vector<plint> t;
FOR(c, l, r) {
for (auto pl : dp[l][c])
for (auto pr : dp[c + 1][r]) {
lint v = pl.second + pr.first + A[c];
t.emplace_back(pl.first + v, pr.second + v);
}
}
sort(ALL(t));
for (auto p : t) {
while (dp[l][r].empty() or dp[l][r].back().second > p.second)
dp[l][r].emplace_back(p);
}
}
lint ret = 1e18;
for (auto p : dp[1][N - 1])
mmin(ret, p.first + p.second + A[0] + A.back());
cout << ret << endl;
}
| replace | 148 | 149 | 148 | 153 | MLE | |
p02978 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define mod 1000000007
#define mod998 998244353
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
typedef long long ll;
using namespace std;
int N, A[18];
vector<pair<ll, ll>> DP[18][18];
ll RES;
vector<pair<ll, ll>> f(int l, int r) {
if (DP[l][r].empty()) {
vector<pair<ll, ll>> v;
for (int i = l + 1; i <= r - 1; ++i) {
vector<pair<ll, ll>> L = f(l, i);
vector<pair<ll, ll>> R = f(i, r);
for (int j = 0; j < L.size(); ++j) {
for (int k = 0; k < R.size(); ++k) {
v.push_back(mkp(L[j].first + L[j].second + R[k].first - A[i],
L[j].second + R[k].first - A[i] + R[k].second));
}
}
}
for (int i = 0; i < v.size(); ++i) {
bool f = true;
for (int j = 0; j < v.size(); ++j) {
if (v[i].first > v[j].first && v[i].second > v[j].second) {
f = false;
break;
}
}
if (f) {
DP[l][r].push_back(v[i]);
}
}
}
return DP[l][r];
}
int main() {
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
for (int i = 0; i < N - 1; ++i) {
DP[i][i + 1].push_back(mkp(A[i], A[i + 1]));
}
vector<pair<ll, ll>> res = f(0, N - 1);
RES = llmax;
for (pair<ll, ll> p : res) {
RES = min(RES, p.first + p.second);
}
cout << RES << endl;
} | #include <bits/stdc++.h>
#define mod 1000000007
#define mod998 998244353
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
typedef long long ll;
using namespace std;
int N, A[18];
vector<pair<ll, ll>> DP[18][18];
ll RES;
vector<pair<ll, ll>> f(int l, int r) {
if (DP[l][r].empty()) {
vector<pair<ll, ll>> v;
for (int i = l + 1; i <= r - 1; ++i) {
vector<pair<ll, ll>> L = f(l, i);
vector<pair<ll, ll>> R = f(i, r);
for (int j = 0; j < L.size(); ++j) {
for (int k = 0; k < R.size(); ++k) {
v.push_back(mkp(L[j].first + L[j].second + R[k].first - A[i],
L[j].second + R[k].first - A[i] + R[k].second));
}
}
}
for (int i = 0; i < v.size(); ++i) {
bool f = true;
for (int j = 0; j < v.size(); ++j) {
if (v[i].first > v[j].first && v[i].second > v[j].second ||
(v[i] == v[j] && i < j)) {
f = false;
break;
}
}
if (f) {
DP[l][r].push_back(v[i]);
}
}
}
return DP[l][r];
}
int main() {
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
for (int i = 0; i < N - 1; ++i) {
DP[i][i + 1].push_back(mkp(A[i], A[i + 1]));
}
vector<pair<ll, ll>> res = f(0, N - 1);
RES = llmax;
for (pair<ll, ll> p : res) {
RES = min(RES, p.first + p.second);
}
cout << RES << endl;
} | replace | 30 | 31 | 30 | 32 | TLE | |
p02978 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define range(i, m, n) for (int i = m; i < n; i++)
#define husk(i, m, n) for (int i = m; i > n; i--)
int n;
int main() {
cin >> n;
n -= 2;
long long res;
cin >> res;
vector<int> a(n);
range(i, 0, n) cin >> a[i];
int foo;
cin >> foo;
res += foo;
vector<pair<long long, long long>> cur;
vector<pair<int, int>> to((1 << (n + 1)) - 1, make_pair(-1, -1));
function<void(int, int, int)> dfs = [&](int u, int v, int dep) {
cur.emplace_back(u, v);
if (dep == n)
return;
int pos = cur.size() - 1;
to[pos].first = cur.size();
dfs(u, u + v, dep + 1);
to[pos].second = cur.size();
dfs(u + v, v, dep + 1);
};
dfs(1, 1, 0);
vector<vector<vector<long long>>> dp(
n, vector<vector<long long>>(
n, vector<long long>((1 << (n + 1)) - 1, LLONG_MAX / 3)));
range(s, 0, n) {
range(x, 0, n - s) {
int y = x + s;
if (x == y) {
range(z, 0, (1 << (n + 1)) - 1) dp[x][y][z] =
cur[z].first * a[x] + cur[z].second * a[y];
continue;
}
range(z, 0, (1 << (n + 1)) - 1) {
int u = to[z].first, v = to[z].second;
if (u == -1)
continue;
range(d, x, y + 1) {
long long l = (d == x ? 0 : dp[x][d - 1][u]),
r = (d == y ? 0 : dp[d + 1][y][v]);
dp[x][y][z] =
min(dp[x][y][z], l + r + (cur[z].first + cur[z].second) * a[d]);
}
}
}
}
res += dp[0][n - 1][0];
cout << res;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define range(i, m, n) for (int i = m; i < n; i++)
#define husk(i, m, n) for (int i = m; i > n; i--)
int n;
int main() {
cin >> n;
n -= 2;
long long res;
cin >> res;
vector<int> a(n);
range(i, 0, n) cin >> a[i];
int foo;
cin >> foo;
res += foo;
if (n == 0) {
cout << res;
return 0;
}
vector<pair<long long, long long>> cur;
vector<pair<int, int>> to((1 << (n + 1)) - 1, make_pair(-1, -1));
function<void(int, int, int)> dfs = [&](int u, int v, int dep) {
cur.emplace_back(u, v);
if (dep == n)
return;
int pos = cur.size() - 1;
to[pos].first = cur.size();
dfs(u, u + v, dep + 1);
to[pos].second = cur.size();
dfs(u + v, v, dep + 1);
};
dfs(1, 1, 0);
vector<vector<vector<long long>>> dp(
n, vector<vector<long long>>(
n, vector<long long>((1 << (n + 1)) - 1, LLONG_MAX / 3)));
range(s, 0, n) {
range(x, 0, n - s) {
int y = x + s;
if (x == y) {
range(z, 0, (1 << (n + 1)) - 1) dp[x][y][z] =
cur[z].first * a[x] + cur[z].second * a[y];
continue;
}
range(z, 0, (1 << (n + 1)) - 1) {
int u = to[z].first, v = to[z].second;
if (u == -1)
continue;
range(d, x, y + 1) {
long long l = (d == x ? 0 : dp[x][d - 1][u]),
r = (d == y ? 0 : dp[d + 1][y][v]);
dp[x][y][z] =
min(dp[x][y][z], l + r + (cur[z].first + cur[z].second) * a[d]);
}
}
}
}
res += dp[0][n - 1][0];
cout << res;
return 0;
}
| insert | 19 | 19 | 19 | 23 | 0 | |
p02978 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
int N;
ll A[20];
vector<l_l> dp[20][20];
void f(int start, int end) {
vector<l_l> tmp;
for (int eat = start; eat < end; eat++) {
for (int i = 0; i < dp[start][eat].size(); i++) {
for (int j = 0; j < dp[eat + 1][end].size(); j++) {
ll val1 = dp[start][eat][i].first;
val1 += dp[start][eat][i].second;
val1 += dp[eat + 1][end][j].first;
val1 += A[eat];
ll val2 = dp[start][eat][i].second;
val2 += dp[eat + 1][end][j].first;
val2 += dp[eat + 1][end][j].second;
val2 += A[eat];
tmp.emplace_back(val1, val2);
}
}
}
sort(tmp.begin(), tmp.end());
dp[start][end].push_back(tmp[0]);
ll minimum = tmp[0].second;
for (int i = 1; i < tmp.size(); i++) {
// if(chmin(minimum, tmp[i].second)) {
dp[start][end].push_back(tmp[i]);
//}
}
/*
cerr << "----" << start << "----" << end << "----" << endl;
for(int i = 0; i < dp[start][end].size(); i++) {
cerr << dp[start][end][i].first << " " << dp[start][end][i].second <<
endl;
}
*/
}
int main() {
// cout.precision(10);
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
for (int i = 1; i <= N; i++)
cin >> A[i];
for (int i = 1; i <= N; i++)
dp[i][i].push_back({0, 0});
for (int delta = 1; delta <= N - 2; delta++) {
for (int start = 2; start + delta <= N; start++) {
f(start, start + delta);
}
}
ll ans = LONG_MAX;
for (int i = 0; i < dp[2][N].size(); i++) {
chmin(ans, A[1] + A[N] + dp[2][N][i].first + dp[2][N][i].second);
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
int N;
ll A[20];
vector<l_l> dp[20][20];
void f(int start, int end) {
vector<l_l> tmp;
for (int eat = start; eat < end; eat++) {
for (int i = 0; i < dp[start][eat].size(); i++) {
for (int j = 0; j < dp[eat + 1][end].size(); j++) {
ll val1 = dp[start][eat][i].first;
val1 += dp[start][eat][i].second;
val1 += dp[eat + 1][end][j].first;
val1 += A[eat];
ll val2 = dp[start][eat][i].second;
val2 += dp[eat + 1][end][j].first;
val2 += dp[eat + 1][end][j].second;
val2 += A[eat];
tmp.emplace_back(val1, val2);
}
}
}
sort(tmp.begin(), tmp.end());
dp[start][end].push_back(tmp[0]);
ll minimum = tmp[0].second;
for (int i = 1; i < tmp.size(); i++) {
if (chmin(minimum, tmp[i].second)) {
dp[start][end].push_back(tmp[i]);
}
}
/*
cerr << "----" << start << "----" << end << "----" << endl;
for(int i = 0; i < dp[start][end].size(); i++) {
cerr << dp[start][end][i].first << " " << dp[start][end][i].second <<
endl;
}
*/
}
int main() {
// cout.precision(10);
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
for (int i = 1; i <= N; i++)
cin >> A[i];
for (int i = 1; i <= N; i++)
dp[i][i].push_back({0, 0});
for (int delta = 1; delta <= N - 2; delta++) {
for (int start = 2; start + delta <= N; start++) {
f(start, start + delta);
}
}
ll ans = LONG_MAX;
for (int i = 0; i < dp[2][N].size(); i++) {
chmin(ans, A[1] + A[N] + dp[2][N][i].first + dp[2][N][i].second);
}
cout << ans << endl;
return 0;
}
| replace | 50 | 53 | 50 | 53 | TLE | |
p02979 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define LL long long
#define LD long double
using namespace std;
const int NN = 200;
const int MM = +117;
int read() {
int fl = 1, x;
char c;
for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
;
if (c == '-') {
fl = -1;
c = getchar();
}
for (x = 0; c >= '0' && c <= '9'; c = getchar())
x = (x << 3) + (x << 1) + c - '0';
return x * fl;
}
void open() {
freopen("a.in", "r", stdin);
// freopen("a.out","w",stdout);
}
void close() {
fclose(stdin);
fclose(stdout);
}
int k, n;
LL dp[2][NN][NN] = {};
int mod;
LL ksm(LL a, LL b) {
LL ret = 1;
for (; b; b >>= 1, a = a * a % mod)
if (b & 1)
ret = ret * a % mod;
return ret;
}
void upd(LL &x, LL y) { x = (x + y) % mod; }
void solve1() {
dp[0][0][0] = 1;
int lim = k / 2;
for (int i = 1; i <= n; ++i) {
for (int a = 0; a <= lim; ++a) {
for (int b = 0; b <= lim; ++b) {
if (i & 1) {
upd(dp[i][a][0], dp[i - 1][a][b]);
upd(dp[i][a][b + 1], dp[i - 1][a][b]);
} else {
upd(dp[i][0][b], dp[i - 1][a][b]);
upd(dp[i][a + 1][b], dp[i - 1][a][b]);
}
}
}
}
LL ans = 0;
for (int a = 0; a <= lim; ++a) {
for (int b = 0; b <= lim; ++b) {
upd(ans, dp[n][a][b]);
}
}
printf("%lld\n", ans);
}
void empty(int now) {
for (int j = 0; j <= k + 2; ++j) {
for (int l = 0; l <= k + 2; ++l) {
dp[now][j][l] = 0;
}
}
}
void cut(int now) {
for (int j = 0; j <= k + 2; ++j) {
for (int l = k - j + 2; l <= k + 2; ++l) {
dp[now][j][l] = 0;
}
}
}
void pushodd(int now) {
for (int j = 0; j <= k + 1; ++j) {
for (int l = 0; l <= k + 1; ++l) {
upd(dp[now][j][0], dp[now ^ 1][j][l]);
upd(dp[now][j][l + 1], dp[now ^ 1][j][l]);
}
}
upd(dp[now][0][k + 1], dp[now ^ 1][0][k + 1]);
}
void pusheven(int now) {
for (int j = 1; j <= k + 2; ++j) {
for (int l = 0; l <= k + 2; ++l) {
upd(dp[now][j - 1][l], dp[now ^ 1][j][l]);
}
}
upd(dp[now][k + 1][0], dp[now ^ 1][k + 1][0]);
for (int j = 0; j <= k + 1; ++j) {
for (int l = 0; l <= k + 1; ++l) {
upd(dp[now][j][l], dp[now ^ 1][0][l]);
}
}
}
void solve2() {
int t = (k + 1) / 2;
int now = 0;
dp[0][0][0] = 1;
for (int i = 1; i <= k; i += 2) { // 2 4 6 | 1 3 5
now ^= 1;
empty(now);
pushodd(now);
cut(now);
}
for (int i = k + 2; i <= n; i += 2) {
now ^= 1;
empty(now);
pushodd(now);
now ^= 1;
empty(now);
pusheven(now);
cut(now);
}
int cur = (n + 1) / 2 * 2 - 1 - k;
int cnt = (n - cur) / 2;
LL ans = 0;
for (int j = 0; j <= cnt; ++j) {
for (int l = 0; l <= k + 1 - j; ++l) {
int rem = cnt - j;
upd(ans, dp[now][j][l] * ksm(2, rem));
}
}
for (int l = 0; l <= k + 1 - cnt - 1; ++l) {
upd(ans, dp[now][cnt + 1][l]);
}
/*for(int i=cur+2;i<=n;i+=2){
now^=1;
empty(now);
pusheven(now);
cut(now);
}
for(int i=0;i<=k+1;++i){
upd(ans,dp[now][0][i]);
if(i<=k)upd(ans,dp[now][1][i]);
}*/
printf("%lld\n", ans);
}
int main() {
// open();
n = read();
k = read();
mod = read();
if (k % 2 == 0) {
solve1();
} else
solve2();
close();
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define LL long long
#define LD long double
using namespace std;
const int NN = 200;
const int MM = +117;
int read() {
int fl = 1, x;
char c;
for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
;
if (c == '-') {
fl = -1;
c = getchar();
}
for (x = 0; c >= '0' && c <= '9'; c = getchar())
x = (x << 3) + (x << 1) + c - '0';
return x * fl;
}
void open() {
freopen("a.in", "r", stdin);
// freopen("a.out","w",stdout);
}
void close() {
fclose(stdin);
fclose(stdout);
}
int k, n;
LL dp[NN][NN][NN] = {};
int mod;
LL ksm(LL a, LL b) {
LL ret = 1;
for (; b; b >>= 1, a = a * a % mod)
if (b & 1)
ret = ret * a % mod;
return ret;
}
void upd(LL &x, LL y) { x = (x + y) % mod; }
void solve1() {
dp[0][0][0] = 1;
int lim = k / 2;
for (int i = 1; i <= n; ++i) {
for (int a = 0; a <= lim; ++a) {
for (int b = 0; b <= lim; ++b) {
if (i & 1) {
upd(dp[i][a][0], dp[i - 1][a][b]);
upd(dp[i][a][b + 1], dp[i - 1][a][b]);
} else {
upd(dp[i][0][b], dp[i - 1][a][b]);
upd(dp[i][a + 1][b], dp[i - 1][a][b]);
}
}
}
}
LL ans = 0;
for (int a = 0; a <= lim; ++a) {
for (int b = 0; b <= lim; ++b) {
upd(ans, dp[n][a][b]);
}
}
printf("%lld\n", ans);
}
void empty(int now) {
for (int j = 0; j <= k + 2; ++j) {
for (int l = 0; l <= k + 2; ++l) {
dp[now][j][l] = 0;
}
}
}
void cut(int now) {
for (int j = 0; j <= k + 2; ++j) {
for (int l = k - j + 2; l <= k + 2; ++l) {
dp[now][j][l] = 0;
}
}
}
void pushodd(int now) {
for (int j = 0; j <= k + 1; ++j) {
for (int l = 0; l <= k + 1; ++l) {
upd(dp[now][j][0], dp[now ^ 1][j][l]);
upd(dp[now][j][l + 1], dp[now ^ 1][j][l]);
}
}
upd(dp[now][0][k + 1], dp[now ^ 1][0][k + 1]);
}
void pusheven(int now) {
for (int j = 1; j <= k + 2; ++j) {
for (int l = 0; l <= k + 2; ++l) {
upd(dp[now][j - 1][l], dp[now ^ 1][j][l]);
}
}
upd(dp[now][k + 1][0], dp[now ^ 1][k + 1][0]);
for (int j = 0; j <= k + 1; ++j) {
for (int l = 0; l <= k + 1; ++l) {
upd(dp[now][j][l], dp[now ^ 1][0][l]);
}
}
}
void solve2() {
int t = (k + 1) / 2;
int now = 0;
dp[0][0][0] = 1;
for (int i = 1; i <= k; i += 2) { // 2 4 6 | 1 3 5
now ^= 1;
empty(now);
pushodd(now);
cut(now);
}
for (int i = k + 2; i <= n; i += 2) {
now ^= 1;
empty(now);
pushodd(now);
now ^= 1;
empty(now);
pusheven(now);
cut(now);
}
int cur = (n + 1) / 2 * 2 - 1 - k;
int cnt = (n - cur) / 2;
LL ans = 0;
for (int j = 0; j <= cnt; ++j) {
for (int l = 0; l <= k + 1 - j; ++l) {
int rem = cnt - j;
upd(ans, dp[now][j][l] * ksm(2, rem));
}
}
for (int l = 0; l <= k + 1 - cnt - 1; ++l) {
upd(ans, dp[now][cnt + 1][l]);
}
/*for(int i=cur+2;i<=n;i+=2){
now^=1;
empty(now);
pusheven(now);
cut(now);
}
for(int i=0;i<=k+1;++i){
upd(ans,dp[now][0][i]);
if(i<=k)upd(ans,dp[now][1][i]);
}*/
printf("%lld\n", ans);
}
int main() {
// open();
n = read();
k = read();
mod = read();
if (k % 2 == 0) {
solve1();
} else
solve2();
close();
return 0;
} | replace | 36 | 37 | 36 | 37 | 0 | |
p02979 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
using namespace std;
int read() {
char c = getchar();
int res = 0;
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
res = (res << 1) + (res << 3) + (c ^ 48), c = getchar();
return res;
}
int n, p, K, f[155][155], g[155][155][155];
void solve1() {
K /= 2;
f[0][0] = 1;
for (int i = 1; i <= (n + 1) / 2; i++) {
for (int j = 0; j <= K; j++)
f[i][0] = (f[i][0] + f[i - 1][j]) % p;
for (int j = 0; j < K; j++)
f[i][j + 1] = (f[i][j + 1] + f[i - 1][j]) % p;
}
int ans1 = 0, ans2 = 0;
for (int i = 0; i <= K; i++)
ans1 = (ans1 + f[n / 2][i]) % p;
for (int i = 0; i <= K; i++)
ans2 = (ans2 + f[(n + 1) / 2][i]) % p;
printf("%lld\n", 1ll * ans1 * ans2 % p);
}
void solve2() {
int last = 0;
g[0][0][0] = 1;
for (int i = 2; i - K <= n; i += 2) {
for (int j = 0; j <= n; j++)
for (int k = 0; k <= K + 1; k++)
g[i][0][0] = (g[i][0][0] + g[i - 2][j][k]) % p;
if (i <= n) {
for (int j = 0; j <= n; j++)
for (int k = 0; k <= K + 1; k++)
g[i][j + 1][0] = (g[i][j + 1][0] + g[i - 2][j][k]) % p;
}
if (i - K >= 1) {
for (int j = 0; j <= n; j++) {
for (int k = 1; k <= K; k++)
g[i][0][k + 1] = (g[i][0][k + 1] + g[i - 2][j][k]) % p;
g[i][0][0] = (g[i][0][0] + g[i - 2][j][0]) % p;
}
}
if (i <= n && i - K >= 1) {
for (int j = 0; j <= n; j++)
for (int k = 0; max(k, j + 1) <= K; k++) {
g[i][j + 1][max(k + 1, j + 2)] =
(g[i][j + 1][max(k + 1, j + 2)] + g[i - 2][j][k]) % p;
}
}
last = i;
}
int ans = 0;
for (int j = 0; j <= n; j++)
for (int k = 0; k <= K + 1; k++)
ans = (ans + g[last][j][k]) % p;
printf("%d\n", ans);
}
int main() {
n = read(), K = read(), p = read();
if (!(K & 1))
solve1();
else
solve2();
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
using namespace std;
int read() {
char c = getchar();
int res = 0;
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
res = (res << 1) + (res << 3) + (c ^ 48), c = getchar();
return res;
}
int n, p, K, f[155][155], g[305][155][155];
void solve1() {
K /= 2;
f[0][0] = 1;
for (int i = 1; i <= (n + 1) / 2; i++) {
for (int j = 0; j <= K; j++)
f[i][0] = (f[i][0] + f[i - 1][j]) % p;
for (int j = 0; j < K; j++)
f[i][j + 1] = (f[i][j + 1] + f[i - 1][j]) % p;
}
int ans1 = 0, ans2 = 0;
for (int i = 0; i <= K; i++)
ans1 = (ans1 + f[n / 2][i]) % p;
for (int i = 0; i <= K; i++)
ans2 = (ans2 + f[(n + 1) / 2][i]) % p;
printf("%lld\n", 1ll * ans1 * ans2 % p);
}
void solve2() {
int last = 0;
g[0][0][0] = 1;
for (int i = 2; i - K <= n; i += 2) {
for (int j = 0; j <= n; j++)
for (int k = 0; k <= K + 1; k++)
g[i][0][0] = (g[i][0][0] + g[i - 2][j][k]) % p;
if (i <= n) {
for (int j = 0; j <= n; j++)
for (int k = 0; k <= K + 1; k++)
g[i][j + 1][0] = (g[i][j + 1][0] + g[i - 2][j][k]) % p;
}
if (i - K >= 1) {
for (int j = 0; j <= n; j++) {
for (int k = 1; k <= K; k++)
g[i][0][k + 1] = (g[i][0][k + 1] + g[i - 2][j][k]) % p;
g[i][0][0] = (g[i][0][0] + g[i - 2][j][0]) % p;
}
}
if (i <= n && i - K >= 1) {
for (int j = 0; j <= n; j++)
for (int k = 0; max(k, j + 1) <= K; k++) {
g[i][j + 1][max(k + 1, j + 2)] =
(g[i][j + 1][max(k + 1, j + 2)] + g[i - 2][j][k]) % p;
}
}
last = i;
}
int ans = 0;
for (int j = 0; j <= n; j++)
for (int k = 0; k <= K + 1; k++)
ans = (ans + g[last][j][k]) % p;
printf("%d\n", ans);
}
int main() {
n = read(), K = read(), p = read();
if (!(K & 1))
solve1();
else
solve2();
} | replace | 17 | 18 | 17 | 18 | 0 | |
p02979 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb emplace_back
#define X first
#define Y second
const int N = 205;
int mod;
void add(int &a, int b) {
a += b;
if (a >= mod)
a -= mod;
}
void sub(int &a, int b) {
a -= b;
if (a < 0)
a += mod;
}
int mul(int a, int b) { return 1ll * a * b % mod; }
int Pow(int a, int b) {
int ans = 1;
while (b) {
if (b & 1)
ans = mul(ans, a);
a = mul(a, a);
b >>= 1;
}
return ans;
}
int inv(int a, int p) { return a == 1 ? 1 : p - 1ll * p * inv(p % a, a) / a; }
typedef pair<int, int> ii;
int f[N][N][N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int k;
cin >> k >> mod;
if (k % 2 == 0) {
k /= 2;
vector<int> f(n + 1, 0);
vector<int> g(n + 1, 0);
g[0] = 1;
for (int i = 1; i <= n; ++i) {
add(g[i], f[i - 1]);
add(g[i], g[i - 1]);
for (int j = 1; j <= k; ++j)
if (j <= i)
add(f[i], g[i - j]);
}
for (int i = 0; i <= n; ++i)
add(f[i], g[i]);
cout << mul(f[n / 2], f[n - n / 2]) << endl;
return 0;
}
f[0][0][0] = 1;
for (int i = 2; i <= n + k; i += 2) {
for (int j = 0; j <= n; ++j)
for (int t = 0; t <= k + 1; ++t)
add(f[i][0][0], f[i - 2][j][t]);
if (i <= n) {
for (int j = 0; j <= n; ++j)
for (int t = 0; t <= k + 1; ++t)
add(f[i][j + 1][0], f[i - 2][j][t]);
}
if (i > k) {
for (int j = 0; j <= n; add(f[i][0][0], f[i - 2][j++][0]))
for (int t = 1; t <= k; ++t)
add(f[i][0][t + 1], f[i - 2][j][t]);
}
if (i <= n && i > k) {
for (int j = 0; j <= n; ++j)
if (j < k)
for (int t = 0; t <= k; ++t)
add(f[i][j + 1][max(j + 2, t + 1)], f[i - 2][j][t]);
}
}
int ans = 0;
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= k + 1; ++j)
add(ans, f[(n + k) / 2 * 2][i][j]);
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb emplace_back
#define X first
#define Y second
const int N = 305;
int mod;
void add(int &a, int b) {
a += b;
if (a >= mod)
a -= mod;
}
void sub(int &a, int b) {
a -= b;
if (a < 0)
a += mod;
}
int mul(int a, int b) { return 1ll * a * b % mod; }
int Pow(int a, int b) {
int ans = 1;
while (b) {
if (b & 1)
ans = mul(ans, a);
a = mul(a, a);
b >>= 1;
}
return ans;
}
int inv(int a, int p) { return a == 1 ? 1 : p - 1ll * p * inv(p % a, a) / a; }
typedef pair<int, int> ii;
int f[N][N][N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int k;
cin >> k >> mod;
if (k % 2 == 0) {
k /= 2;
vector<int> f(n + 1, 0);
vector<int> g(n + 1, 0);
g[0] = 1;
for (int i = 1; i <= n; ++i) {
add(g[i], f[i - 1]);
add(g[i], g[i - 1]);
for (int j = 1; j <= k; ++j)
if (j <= i)
add(f[i], g[i - j]);
}
for (int i = 0; i <= n; ++i)
add(f[i], g[i]);
cout << mul(f[n / 2], f[n - n / 2]) << endl;
return 0;
}
f[0][0][0] = 1;
for (int i = 2; i <= n + k; i += 2) {
for (int j = 0; j <= n; ++j)
for (int t = 0; t <= k + 1; ++t)
add(f[i][0][0], f[i - 2][j][t]);
if (i <= n) {
for (int j = 0; j <= n; ++j)
for (int t = 0; t <= k + 1; ++t)
add(f[i][j + 1][0], f[i - 2][j][t]);
}
if (i > k) {
for (int j = 0; j <= n; add(f[i][0][0], f[i - 2][j++][0]))
for (int t = 1; t <= k; ++t)
add(f[i][0][t + 1], f[i - 2][j][t]);
}
if (i <= n && i > k) {
for (int j = 0; j <= n; ++j)
if (j < k)
for (int t = 0; t <= k; ++t)
add(f[i][j + 1][max(j + 2, t + 1)], f[i - 2][j][t]);
}
}
int ans = 0;
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= k + 1; ++j)
add(ans, f[(n + k) / 2 * 2][i][j]);
cout << ans << endl;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02979 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
o << "{";
for (const T &v : vc)
o << v << ",";
o << "}";
return o;
}
using ll = long long;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
#ifdef LOCAL
#define show(x) \
cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif
unsigned int mod = 1;
struct ModInt {
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
uint v;
ModInt() : v(0) {}
ModInt(ll _v) : v(normS(_v % mod + mod)) {}
explicit operator bool() const { return v != 0; }
static uint normS(const uint &x) {
return (x < mod) ? x : x - mod;
} // [0 , 2*mod-1] -> [0 , mod-1]
static ModInt make(const uint &x) {
ModInt m;
m.v = x;
return m;
}
ModInt operator+(const ModInt &b) const { return make(normS(v + b.v)); }
ModInt operator-(const ModInt &b) const { return make(normS(v + mod - b.v)); }
ModInt operator-() const { return make(normS(mod - v)); }
ModInt operator*(const ModInt &b) const { return make((ull)v * b.v % mod); }
ModInt operator/(const ModInt &b) const { return *this * b.inv(); }
ModInt &operator+=(const ModInt &b) { return *this = *this + b; }
ModInt &operator-=(const ModInt &b) { return *this = *this - b; }
ModInt &operator*=(const ModInt &b) { return *this = *this * b; }
ModInt &operator/=(const ModInt &b) { return *this = *this / b; }
ModInt &operator++(int) { return *this = *this + 1; }
ModInt &operator--(int) { return *this = *this - 1; }
ll extgcd(ll a, ll b, ll &x, ll &y) const {
ll p[] = {a, 1, 0}, q[] = {b, 0, 1};
while (*q) {
ll t = *p / *q;
rep(i, 3) swap(p[i] -= t * q[i], q[i]);
}
if (p[0] < 0)
rep(i, 3) p[i] = -p[i];
x = p[1], y = p[2];
return p[0];
}
ModInt inv() const {
ll x, y;
extgcd(v, mod, x, y);
return make(normS(x + mod));
}
ModInt pow(ll p) const {
if (p < 0)
return inv().pow(-p);
ModInt a = 1;
ModInt x = *this;
while (p) {
if (p & 1)
a *= x;
x *= x;
p >>= 1;
}
return a;
}
bool operator==(const ModInt &b) const { return v == b.v; }
bool operator!=(const ModInt &b) const { return v != b.v; }
friend istream &operator>>(istream &o, ModInt &x) {
ll tmp;
o >> tmp;
x = ModInt(tmp);
return o;
}
friend ostream &operator<<(ostream &o, const ModInt &x) { return o << x.v; }
};
using mint = ModInt;
int N, K;
mint dp[2][80][80][80][80];
int main() {
cin >> N >> K >> mod;
if (K % 2 == 0) {
K = K / 2 + 1; // o*K -> no
VV<mint> dp(N + 1, V<mint>(K));
dp[0][0] = 1;
rep(i, N) rep(k, K) {
dp[i + 1][0] += dp[i][k];
if (k + 1 != K)
dp[i + 1][k + 1] += dp[i][k];
}
mint res = 1;
for (int n : {N / 2, (N + 1) / 2}) {
mint s = 0;
rep(k, K) s += dp[n][k];
res *= s;
}
cout << res << endl;
return 0;
}
K = (K + 1) / 2;
dp[0][0][0][0][0] = 1;
rep(i, N) {
int t = i & 1;
int nt = 1 - t;
rep(a, K + 1) rep(b, K + 1) rep(c, K + 1) {
dp[nt][a][b][c][0] = 0;
dp[nt][a][b][0][c] = 0;
}
rep(a, K + 1) rep(b, K + 1) rep(v, K + 1) rep(_, 2) {
if (v == 0 && _ == 0)
continue;
int c = v, d = 0;
if (_)
swap(c, d);
if (!dp[t][a][b][c][d])
continue;
// printf("[%d][%d][%d][%d]\n",a,b,c,d);
// x
dp[nt][b][0][d][0] += dp[t][a][b][c][d];
// o
if (c == 1)
continue;
if (a == K) {
if (b >= K)
continue;
int nd = c == 0 ? 0 : c - 1;
if (b > 0) {
if (c == 0)
nd = K - b;
else
nd = min(c - 1, K - b);
}
dp[nt][b][K][d][nd] += dp[t][a][b][c][d];
} else {
dp[nt][b][a + 1][d][c == 0 ? 0 : c - 1] += dp[t][a][b][c][d];
}
}
}
mint res = 0;
rep(a, K + 1) rep(b, K + 1) rep(c, K + 1) rep(d, K + 1) res +=
dp[N & 1][a][b][c][d];
cout << res << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
o << "{";
for (const T &v : vc)
o << v << ",";
o << "}";
return o;
}
using ll = long long;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
#ifdef LOCAL
#define show(x) \
cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif
unsigned int mod = 1;
struct ModInt {
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
uint v;
ModInt() : v(0) {}
ModInt(ll _v) : v(normS(_v % mod + mod)) {}
explicit operator bool() const { return v != 0; }
static uint normS(const uint &x) {
return (x < mod) ? x : x - mod;
} // [0 , 2*mod-1] -> [0 , mod-1]
static ModInt make(const uint &x) {
ModInt m;
m.v = x;
return m;
}
ModInt operator+(const ModInt &b) const { return make(normS(v + b.v)); }
ModInt operator-(const ModInt &b) const { return make(normS(v + mod - b.v)); }
ModInt operator-() const { return make(normS(mod - v)); }
ModInt operator*(const ModInt &b) const { return make((ull)v * b.v % mod); }
ModInt operator/(const ModInt &b) const { return *this * b.inv(); }
ModInt &operator+=(const ModInt &b) { return *this = *this + b; }
ModInt &operator-=(const ModInt &b) { return *this = *this - b; }
ModInt &operator*=(const ModInt &b) { return *this = *this * b; }
ModInt &operator/=(const ModInt &b) { return *this = *this / b; }
ModInt &operator++(int) { return *this = *this + 1; }
ModInt &operator--(int) { return *this = *this - 1; }
ll extgcd(ll a, ll b, ll &x, ll &y) const {
ll p[] = {a, 1, 0}, q[] = {b, 0, 1};
while (*q) {
ll t = *p / *q;
rep(i, 3) swap(p[i] -= t * q[i], q[i]);
}
if (p[0] < 0)
rep(i, 3) p[i] = -p[i];
x = p[1], y = p[2];
return p[0];
}
ModInt inv() const {
ll x, y;
extgcd(v, mod, x, y);
return make(normS(x + mod));
}
ModInt pow(ll p) const {
if (p < 0)
return inv().pow(-p);
ModInt a = 1;
ModInt x = *this;
while (p) {
if (p & 1)
a *= x;
x *= x;
p >>= 1;
}
return a;
}
bool operator==(const ModInt &b) const { return v == b.v; }
bool operator!=(const ModInt &b) const { return v != b.v; }
friend istream &operator>>(istream &o, ModInt &x) {
ll tmp;
o >> tmp;
x = ModInt(tmp);
return o;
}
friend ostream &operator<<(ostream &o, const ModInt &x) { return o << x.v; }
};
using mint = ModInt;
int N, K;
mint dp[2][80][80][80][80];
int main() {
cin >> N >> K >> mod;
if (K % 2 == 0) {
K = K / 2 + 1; // o*K -> no
VV<mint> dp(N + 1, V<mint>(K));
dp[0][0] = 1;
rep(i, N) rep(k, K) {
dp[i + 1][0] += dp[i][k];
if (k + 1 != K)
dp[i + 1][k + 1] += dp[i][k];
}
mint res = 1;
for (int n : {N / 2, (N + 1) / 2}) {
mint s = 0;
rep(k, K) s += dp[n][k];
res *= s;
}
cout << res << endl;
return 0;
}
K = (K + 1) / 2;
dp[0][0][0][0][0] = 1;
rep(i, N) {
int t = i & 1;
int nt = 1 - t;
rep(a, K + 1) rep(b, K + 1) rep(c, K + 1) { dp[nt][a][b][c][0] = 0; }
rep(a, K + 1) rep(b, K + 1) rep(c, K + 1) { dp[nt][a][b][0][c] = 0; }
rep(a, K + 1) rep(b, K + 1) rep(v, K + 1) rep(_, 2) {
if (v == 0 && _ == 0)
continue;
int c = v, d = 0;
if (_)
swap(c, d);
if (!dp[t][a][b][c][d])
continue;
// printf("[%d][%d][%d][%d]\n",a,b,c,d);
// x
dp[nt][b][0][d][0] += dp[t][a][b][c][d];
// o
if (c == 1)
continue;
if (a == K) {
if (b >= K)
continue;
int nd = c == 0 ? 0 : c - 1;
if (b > 0) {
if (c == 0)
nd = K - b;
else
nd = min(c - 1, K - b);
}
dp[nt][b][K][d][nd] += dp[t][a][b][c][d];
} else {
dp[nt][b][a + 1][d][c == 0 ? 0 : c - 1] += dp[t][a][b][c][d];
}
}
}
mint res = 0;
rep(a, K + 1) rep(b, K + 1) rep(c, K + 1) rep(d, K + 1) res +=
dp[N & 1][a][b][c][d];
cout << res << endl;
} | replace | 132 | 136 | 132 | 134 | TLE | |
p02979 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 101;
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define nfr(i, a, b) for (int i = a; i >= b; --i)
int f[2][N];
int g[2][N][N][N];
int n, K, m;
inline void add(int &x, int y) {
x += y;
if (x >= m)
x -= m;
}
int main() {
scanf("%d%d%d", &n, &K, &m);
if (K % 2 == 0) {
int ans = 1;
fr(t, 0, 1) {
int len = (n + t) / 2;
int cur = 0;
memset(f, 0, sizeof f);
f[0][0] = 1;
fr(z, 1, len) {
cur ^= 1;
memset(f[cur], 0, sizeof f[cur]);
fr(j, 0, K / 2) {
if (j < K / 2)
add(f[cur][j + 1], f[cur ^ 1][j]);
add(f[cur][0], f[cur ^ 1][j]);
}
}
int res = 0;
fr(z, 0, len) add(res, f[cur][z]);
ans = 1LL * ans * res % m;
}
printf("%d\n", ans);
} else {
int cur = 0;
g[0][0][0][n] = 1;
int ans = 0;
fr(i, 1, n) {
cur ^= 1;
memset(g[cur], 0, sizeof g[cur]);
int odd = i / 2, even = (i + 1) / 2;
fr(j, 0, even) {
fr(k, 0, odd) {
fr(t, i - 1, n) {
int to = (t % 2 != i % 2) ? n : t;
add(g[cur][0][j][to], g[cur ^ 1][j][k][t]);
if (t != i - 1) {
to = t;
if (j * 2 > K)
to = min(to, i - k * 2 + K - 1);
add(g[cur][k + 1][j][to], g[cur ^ 1][j][k][t]);
}
}
}
}
}
fr(j, 0, n) {
fr(k, 0, n) { add(ans, g[cur][j][k][n]); }
}
printf("%d\n", ans);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 155;
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define nfr(i, a, b) for (int i = a; i >= b; --i)
int f[2][N];
int g[2][N][N][N];
int n, K, m;
inline void add(int &x, int y) {
x += y;
if (x >= m)
x -= m;
}
int main() {
scanf("%d%d%d", &n, &K, &m);
if (K % 2 == 0) {
int ans = 1;
fr(t, 0, 1) {
int len = (n + t) / 2;
int cur = 0;
memset(f, 0, sizeof f);
f[0][0] = 1;
fr(z, 1, len) {
cur ^= 1;
memset(f[cur], 0, sizeof f[cur]);
fr(j, 0, K / 2) {
if (j < K / 2)
add(f[cur][j + 1], f[cur ^ 1][j]);
add(f[cur][0], f[cur ^ 1][j]);
}
}
int res = 0;
fr(z, 0, len) add(res, f[cur][z]);
ans = 1LL * ans * res % m;
}
printf("%d\n", ans);
} else {
int cur = 0;
g[0][0][0][n] = 1;
int ans = 0;
fr(i, 1, n) {
cur ^= 1;
memset(g[cur], 0, sizeof g[cur]);
int odd = i / 2, even = (i + 1) / 2;
fr(j, 0, even) {
fr(k, 0, odd) {
fr(t, i - 1, n) {
int to = (t % 2 != i % 2) ? n : t;
add(g[cur][0][j][to], g[cur ^ 1][j][k][t]);
if (t != i - 1) {
to = t;
if (j * 2 > K)
to = min(to, i - k * 2 + K - 1);
add(g[cur][k + 1][j][to], g[cur ^ 1][j][k][t]);
}
}
}
}
}
fr(j, 0, n) {
fr(k, 0, n) { add(ans, g[cur][j][k][n]); }
}
printf("%d\n", ans);
}
return 0;
}
| replace | 2 | 3 | 2 | 3 | 0 | |
p02979 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
#define RI register int
#define CI const int &
using namespace std;
const int N = 150;
int n, k, mod;
inline void inc(int &x, CI y) {
if ((x += y) >= mod)
x -= mod;
}
namespace Case1 // Even number solver
{
int f[N][N];
inline int calc(CI n, CI k) {
RI i, j;
for (f[0][0] = i = 1; i <= n; ++i)
for (f[i][0] = f[i - 1][0], j = 1; j <= k; ++j)
inc(f[i][0], f[i - 1][j]), f[i][j] = f[i - 1][j - 1];
int ret = 0;
for (i = 0; i <= k; ++i)
inc(ret, f[n][i]);
return ret;
}
inline void solve(void) {
printf("%d", 1LL * calc(n >> 1, k >> 1) * calc(n - (n >> 1), k >> 1) % mod);
}
}; // namespace Case1
namespace Case2 // Odd number solver
{
int f[N][N][N], ans;
inline void solve(void) {
RI i, j, p;
for (f[0][0][0] = i = 1; i <= n; ++i) {
int r = i << 1, l = r - k, fl = 1 <= l && l <= n, fr = 1 <= r && r <= n;
for (j = 0; j <= n; ++j)
for (p = 0; p <= k + 1; ++p) {
inc(f[i][0][0], f[i - 1][j][p]);
if (fr)
inc(f[i][j + 1][0], f[i - 1][j][p]);
if (fl && p + 1 < k + 2)
inc(f[i][0][p ? p + 1 : p], f[i - 1][j][p]);
if (fl && fr && max(j + 2, p + 1) < k + 2)
inc(f[i][j + 1][max(j + 2, p + 1)], f[i - 1][j][p]);
}
}
for (i = 0; i <= n; ++i)
for (j = 0; j <= k + 1; ++j)
inc(ans, f[n][i][j]);
printf("%d", ans);
}
}; // namespace Case2
int main() {
scanf("%d%d%d", &n, &k, &mod);
if (k & 1)
Case2::solve();
else
Case1::solve();
return 0;
} | #include <cstdio>
#include <iostream>
#define RI register int
#define CI const int &
using namespace std;
const int N = 155;
int n, k, mod;
inline void inc(int &x, CI y) {
if ((x += y) >= mod)
x -= mod;
}
namespace Case1 // Even number solver
{
int f[N][N];
inline int calc(CI n, CI k) {
RI i, j;
for (f[0][0] = i = 1; i <= n; ++i)
for (f[i][0] = f[i - 1][0], j = 1; j <= k; ++j)
inc(f[i][0], f[i - 1][j]), f[i][j] = f[i - 1][j - 1];
int ret = 0;
for (i = 0; i <= k; ++i)
inc(ret, f[n][i]);
return ret;
}
inline void solve(void) {
printf("%d", 1LL * calc(n >> 1, k >> 1) * calc(n - (n >> 1), k >> 1) % mod);
}
}; // namespace Case1
namespace Case2 // Odd number solver
{
int f[N][N][N], ans;
inline void solve(void) {
RI i, j, p;
for (f[0][0][0] = i = 1; i <= n; ++i) {
int r = i << 1, l = r - k, fl = 1 <= l && l <= n, fr = 1 <= r && r <= n;
for (j = 0; j <= n; ++j)
for (p = 0; p <= k + 1; ++p) {
inc(f[i][0][0], f[i - 1][j][p]);
if (fr)
inc(f[i][j + 1][0], f[i - 1][j][p]);
if (fl && p + 1 < k + 2)
inc(f[i][0][p ? p + 1 : p], f[i - 1][j][p]);
if (fl && fr && max(j + 2, p + 1) < k + 2)
inc(f[i][j + 1][max(j + 2, p + 1)], f[i - 1][j][p]);
}
}
for (i = 0; i <= n; ++i)
for (j = 0; j <= k + 1; ++j)
inc(ans, f[n][i][j]);
printf("%d", ans);
}
}; // namespace Case2
int main() {
scanf("%d%d%d", &n, &k, &mod);
if (k & 1)
Case2::solve();
else
Case1::solve();
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02980 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
const int mo = 998244353, N = 1e5 + 5;
int ksm(int a, int b) {
int ans = 1;
while (b) {
if (b & 1)
ans = ans * a % mo;
a = a * a % mo;
b >>= 1;
}
return ans;
}
int fac[N], inv[N], f[N];
int C(int a, int b) { return fac[a] * inv[b] % mo * inv[a - b] % mo; }
signed main() {
int n, m, ans = 0;
scanf("%lld%lld", &n, &m);
fac[0] = 1;
for (int i = 1; i < N; ++i)
fac[i] = (fac[i - 1] * i) % mo;
inv[N - 1] = ksm(fac[N - 1], mo - 2);
for (int i = N - 2; i >= 0; i--)
inv[i] = (inv[i + 1] * (i + 1)) % mo;
if (n > m)
std::swap(n, m);
for (int i = 0; i <= n; ++i)
f[i] = C(n, i) * C(m, i) % mo * fac[i] % mo * ksm(n + 1, m - i) % mo *
ksm(m + 1, n - i) % mo;
for (int i = 0; i <= n; ++i)
ans = ((ans + (1 - ((i & 1) << 1)) * f[i]) % mo + mo) % mo;
printf("%lld\n", ans);
} | #include <bits/stdc++.h>
#define int long long
const int mo = 998244353, N = 1e6 + 5;
int ksm(int a, int b) {
int ans = 1;
while (b) {
if (b & 1)
ans = ans * a % mo;
a = a * a % mo;
b >>= 1;
}
return ans;
}
int fac[N], inv[N], f[N];
int C(int a, int b) { return fac[a] * inv[b] % mo * inv[a - b] % mo; }
signed main() {
int n, m, ans = 0;
scanf("%lld%lld", &n, &m);
fac[0] = 1;
for (int i = 1; i < N; ++i)
fac[i] = (fac[i - 1] * i) % mo;
inv[N - 1] = ksm(fac[N - 1], mo - 2);
for (int i = N - 2; i >= 0; i--)
inv[i] = (inv[i + 1] * (i + 1)) % mo;
if (n > m)
std::swap(n, m);
for (int i = 0; i <= n; ++i)
f[i] = C(n, i) * C(m, i) % mo * fac[i] % mo * ksm(n + 1, m - i) % mo *
ksm(m + 1, n - i) % mo;
for (int i = 0; i <= n; ++i)
ans = ((ans + (1 - ((i & 1) << 1)) * f[i]) % mo + mo) % mo;
printf("%lld\n", ans);
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02980 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
template <typename A, typename B> inline void chmin(A &a, B b) {
if (a > b)
a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
if (a < b)
a = b;
}
template <uint32_t mod> struct ModInt {
uint32_t a;
ModInt &s(uint32_t vv) {
a = vv < mod ? vv : vv - mod;
return *this;
}
ModInt(int64_t x = 0) { s(x % mod + mod); }
ModInt &operator+=(const ModInt &x) { return s(a + x.a); }
ModInt &operator-=(const ModInt &x) { return s(a + mod - x.a); }
ModInt &operator*=(const ModInt &x) {
a = uint64_t(a) * x.a % mod;
return *this;
}
ModInt &operator/=(const ModInt &x) {
*this *= x.inv();
return *this;
}
ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
bool operator==(const ModInt &x) const { return a == x.a; }
bool operator!=(const ModInt &x) const { return a != x.a; }
ModInt operator-() const { return ModInt() - *this; }
ModInt pow(int64_t n) const {
ModInt res(1), x(*this);
while (n) {
if (n & 1)
res *= x;
x *= x;
n >>= 1;
}
return res;
}
ModInt inv() const { return pow(mod - 2); }
};
using mint = ModInt<998244353>;
template <uint32_t mod> istream &operator>>(istream &in, ModInt<mod> &a) {
return (in >> a.a);
}
template <uint32_t mod>
ostream &operator<<(ostream &out, const ModInt<mod> &a) {
return (out << a.a);
}
using mint = ModInt<998244353>;
template <class Mint, int32_t N> struct ModIntTable {
vector<Mint> facts, finvs, invs;
ModIntTable() : facts(N), finvs(N), invs(N) {
const uint32_t mod = Mint(-1).a + 1;
invs[1] = 1;
for (int i = 2; i < N; i++)
invs[i] = invs[mod % i] * (mod - mod / i);
facts[0] = 1;
finvs[0] = 1;
for (int i = 1; i < N; i++) {
facts[i] = facts[i - 1] * i;
finvs[i] = finvs[i - 1] * invs[i];
}
}
inline Mint fact(int n) { return facts[n]; }
inline Mint finv(int n) { return finvs[n]; }
inline Mint inv(int n) { return invs[n]; }
inline Mint binom(int n, int k) { return facts[n] * finvs[k] * finvs[n - k]; }
};
ModIntTable<mint, 1 << 19> mtable;
template <class Mint, int32_t root> struct NumberTheoreticTransform {
static void ntt(vector<Mint> &f) {
int n = f.size();
int s = __lg(n);
for (int i = 0, j = 1; j < n - 1; j++) {
for (int k = n >> 1; k > (i ^= k); k >>= 1)
;
if (i > j)
swap(f[i], f[j]);
}
for (int m = 1; m <= s; m++) {
Mint wr = Mint(root).pow(Mint(-1).a >> m);
for (int i = 0; i < n; i += 1 << m) {
Mint w = 1;
for (int j = 0; j < 1 << m - 1; j++) {
Mint f0 = f[i + j], f1 = w * f[i + j + (1 << m - 1)];
f[i + j] = f0 + f1;
f[i + j + (1 << m - 1)] = f0 - f1;
w *= wr;
}
}
}
}
static void intt(vector<Mint> &f) {
reverse(f.begin() + 1, f.end());
ntt(f);
Mint in = Mint(f.size()).inv();
for (int i = 0; i < f.size(); i++)
f[i] *= in;
}
static vector<Mint> convolute(const vector<Mint> &A, const vector<Mint> &B) {
if (A.size() == 0 || B.size() == 0)
return {};
int n = 1 << __lg(A.size() + B.size() - 2) + 1;
vector<Mint> a = A, b = B;
a.resize(n);
b.resize(n);
ntt(a);
ntt(b);
for (int i = 0; i < n; i++)
a[i] *= b[i];
intt(a);
a.resize(A.size() + B.size() - 1);
return a;
}
};
using NTT = NumberTheoreticTransform<mint, 3>;
const uint32_t mod = 998244353;
const mint prim_root = 3;
static const int LG = 19;
mint roots[1 << (LG + 1)], iroots[1 << (LG + 1)];
struct PrepareRoots {
PrepareRoots() {
rep(w, LG + 1) {
const int s = (1 << w) - 1;
const mint g = prim_root.pow((mod - 1) / (1 << w)), ig = g.inv();
mint p = 1, ip = 1;
rep(i, 1 << w) {
roots[s + i] = p;
p *= g;
iroots[s + i] = ip;
ip *= ig;
}
}
}
} PrepareRootsDummy;
void broken_fmt(vector<mint> &f) {
const int n = f.size();
for (int b = n / 2; b >= 1; b /= 2) {
for (int i = 0; i < n; i += b * 2) {
rep(j, b) {
mint tmp = f[i + j] - f[i + j + b];
f[i + j] += f[i + j + b];
f[i + j + b] = tmp * roots[b * 2 - 1 + j];
}
}
}
}
void broken_ifmt(vector<mint> &f) {
const int n = f.size();
for (int b = 1; b <= n / 2; b *= 2) {
for (int i = 0; i < n; i += b * 2) {
rep(j, b) {
f[i + j + b] *= iroots[b * 2 - 1 + j];
mint tmp = f[i + j] - f[i + j + b];
f[i + j] += f[i + j + b];
f[i + j + b] = tmp;
}
}
}
mint in = mint(n).inv();
rep(i, n) f[i] *= in;
}
void inplace_fmt(vector<mint> &f, const bool i = false) {
if (!i)
broken_fmt(f);
else
broken_ifmt(f);
}
vector<mint> multiply(vector<mint> x, vector<mint> y, bool same = false) {
int n = x.size() + y.size() - 1;
int s = 1;
while (s < n)
s *= 2;
x.resize(s);
inplace_fmt(x, false);
if (!same) {
y.resize(s);
inplace_fmt(y, false);
} else
y = x;
rep(i, s) x[i] *= y[i];
inplace_fmt(x, true);
x.resize(n);
return x;
}
template <class Mint> struct NTTFriendlyPoly {
void ntt(vector<Mint> &f) {
int n = f.size();
int s = __lg(n);
for (int i = 0, j = 1; j < n - 1; j++) {
for (int k = n >> 1; k > (i ^= k); k >>= 1)
;
if (i > j)
swap(f[i], f[j]);
}
for (int m = 1; m <= s; m++) {
Mint wr = Mint(3).pow(Mint(-1).a >> m);
for (int i = 0; i < n; i += 1 << m) {
Mint w = 1;
for (int j = 0; j < 1 << m - 1; j++) {
Mint f0 = f[i + j], f1 = w * f[i + j + (1 << m - 1)];
f[i + j] = f0 + f1;
f[i + j + (1 << m - 1)] = f0 - f1;
w *= wr;
}
}
}
}
void intt(vector<Mint> &f) {
reverse(f.begin() + 1, f.end());
ntt(f);
Mint in = Mint(f.size()).inv();
for (int i = 0; i < f.size(); i++)
f[i] *= in;
}
vector<Mint> convolute(const vector<Mint> &A, const vector<Mint> &B) {
if (A.size() == 0 || B.size() == 0)
return {};
int n = 1 << __lg(A.size() + B.size() - 2) + 1;
vector<Mint> a = A, b = B;
a.resize(n);
b.resize(n);
ntt(a);
ntt(b);
for (int i = 0; i < n; i++)
a[i] *= b[i];
intt(a);
a.resize(A.size() + B.size() - 1);
return a;
}
vector<Mint> v;
template <class... Args> NTTFriendlyPoly(Args... args) : v(args...) {}
NTTFriendlyPoly(const initializer_list<Mint> &in) : v(in.begin(), in.end()) {}
int size() const { return v.size(); }
inline Mint coef(const int i) const {
return (i < v.size()) ? v[i] : Mint(0);
}
NTTFriendlyPoly operator+(const NTTFriendlyPoly &x) {
int n = max(size(), x.size());
NTTFriendlyPoly<Mint> res(n);
for (int i = 0; i < n; i++)
res[i] = coef(i) + x.coef(i);
return res;
}
NTTFriendlyPoly operator-(const NTTFriendlyPoly &x) {
int n = max(size(), x.size());
NTTFriendlyPoly<Mint> res(n);
for (int i = 0; i < n; i++)
res[i] = coef(i) - x.coef(i);
return res;
}
NTTFriendlyPoly operator*(const NTTFriendlyPoly &x) {
return multiply(v, x.v);
}
NTTFriendlyPoly operator*(const Mint &x) {
int n = size();
vector<Mint> res(n);
for (int i = 0; i < n; i++)
res[i] = v[i] * x;
return res;
}
NTTFriendlyPoly operator/(const Mint &x) { return (*this) * x.inv(); }
NTTFriendlyPoly &operator+=(const NTTFriendlyPoly &x) {
return *this = (*this) + x;
}
NTTFriendlyPoly &operator-=(const NTTFriendlyPoly &x) {
return *this = (*this) - x;
}
NTTFriendlyPoly &operator*=(const NTTFriendlyPoly &x) {
return *this = (*this) * x;
}
NTTFriendlyPoly &operator*=(const Mint &x) { return *this = (*this) * x; }
NTTFriendlyPoly &operator/=(const Mint &x) { return *this = (*this) / x; }
NTTFriendlyPoly operator-() { return NTTFriendlyPoly() - *this; }
NTTFriendlyPoly pre(int n) {
NTTFriendlyPoly<Mint> res(n);
for (int i = 0; i < n && i < size(); i++)
res[i] = v[i];
return res;
}
NTTFriendlyPoly rev() {
vector<Mint> res = v;
while (res.size() && res.back() == 0)
res.pop_back();
reverse(res.begin(), res.end());
return res;
}
NTTFriendlyPoly diff(int n) {
NTTFriendlyPoly<Mint> res(n);
for (int i = 1; i < size() && i <= n; i++)
res[i - 1] = v[i] * i;
return res;
}
NTTFriendlyPoly inte(int n) {
NTTFriendlyPoly<Mint> res(n);
for (int i = 0; i < size() && i + 1 < n; i++)
res[i + 1] = v[i] * mtable.inv(i + 1);
return res;
}
NTTFriendlyPoly inv(int n) {
vector<mint> res{coef(0).inv()};
for (int d = 1; d < n; d <<= 1) {
vector<Mint> f(2 * d), g(2 * d);
for (int j = 0; j < 2 * d; j++)
f[j] = coef(j);
for (int j = 0; j < d; j++)
g[j] = res[j];
inplace_fmt(f);
inplace_fmt(g);
for (int j = 0; j < 2 * d; j++)
f[j] *= g[j];
inplace_fmt(f, true);
for (int j = 0; j < d; j++) {
f[j] = 0;
f[j + d] = -f[j + d];
}
inplace_fmt(f);
for (int j = 0; j < 2 * d; j++)
f[j] *= g[j];
inplace_fmt(f, true);
for (int j = 0; j < d; j++)
f[j] = res[j];
res = f;
}
return NTTFriendlyPoly(res).pre(n);
}
NTTFriendlyPoly inv2(int n) {
NTTFriendlyPoly res{coef(0).inv()};
for (int i = 1; i < n; i *= 2) {
res = (res * Mint(2) - res * res * pre(2 * i)).pre(2 * i);
}
return res.pre(n);
}
NTTFriendlyPoly exp(int n) {
NTTFriendlyPoly f0{1}, g0{1};
vector<Mint> F0{1};
for (int d = 1; d < n; d <<= 1) {
vector<Mint> G0 = g0.v;
inplace_fmt(G0);
vector<Mint> Delta(d);
for (int j = 0; j < d; j++)
Delta[j] = F0[j] * G0[j];
inplace_fmt(Delta, true);
Delta[0] -= 1;
NTTFriendlyPoly delta(2 * d);
for (int j = 0; j < d; j++)
delta[d + j] = Delta[j];
NTTFriendlyPoly epsilon(2 * d);
vector<Mint> DF0 = f0.diff(d - 1).v;
DF0.push_back(0);
inplace_fmt(DF0);
for (int j = 0; j < d; j++)
DF0[j] *= G0[j];
inplace_fmt(DF0, true);
for (int j = 0; j < d - 1; j++) {
epsilon[j] += coef(j + 1) * (j + 1);
epsilon[j + d] += DF0[j] - coef(j + 1) * (j + 1);
}
epsilon[d - 1] += DF0[d - 1];
Delta = delta.v;
inplace_fmt(Delta);
vector<Mint> DH0 = diff(d - 1).v;
DH0.resize(2 * d);
inplace_fmt(DH0);
for (int j = 0; j < 2 * d; j++)
Delta[j] *= DH0[j];
inplace_fmt(Delta, true);
for (int j = 0; j < d; j++)
epsilon[j + d] -= Delta[j + d];
epsilon = epsilon.inte(2 * d) - pre(2 * d);
vector<Mint> Epsilon = epsilon.v;
inplace_fmt(Epsilon);
rep(j, d) DH0[j] = f0[j], DH0[j + d] = 0;
inplace_fmt(DH0);
rep(j, 2 * d) Epsilon[j] *= DH0[j];
inplace_fmt(Epsilon, true);
f0.v.resize(2 * d);
rep(j, d) f0[j + d] -= Epsilon[j + d];
// f0=(f0-epsilon*f0).pre(2*d);
G0.resize(2 * d);
rep(j, d) G0[j] = g0[j];
inplace_fmt(G0);
F0 = f0.v;
inplace_fmt(F0);
vector<Mint> T(2 * d);
rep(j, 2 * d) T[j] = F0[j] * G0[j];
inplace_fmt(T, true);
rep(j, d) {
T[j] = 0;
T[j + d] = -T[j + d];
}
inplace_fmt(T);
rep(j, 2 * d) T[j] *= G0[j];
inplace_fmt(T, true);
rep(j, d) T[j] = g0[j];
g0 = T;
}
return f0.pre(n);
}
NTTFriendlyPoly exp2(int n) {
NTTFriendlyPoly f{1};
for (int i = 1; i < n; i *= 2) {
f = (f * (pre(2 * i) - f.log(2 * i)) + f).pre(2 * i);
}
return f.pre(n);
}
NTTFriendlyPoly exp3(int n) {
NTTFriendlyPoly f{1}, g{1};
for (int d = 1; d < n; d <<= 1) {
g = g * Mint(2) - (g * g * f).pre(d);
NTTFriendlyPoly q = diff(d - 1);
q = q + g * (f.diff(d - 1) - f * q).pre(2 * d - 1);
f = f + (f * (pre(2 * d) - q.inte(2 * d))).pre(2 * d);
}
return f.pre(n);
}
NTTFriendlyPoly log(int n) { return (diff(n - 1) * inv(n - 1)).inte(n); }
NTTFriendlyPoly pow(int n, mint k) {
auto res = log(n);
res *= k;
return res.exp(n);
}
Mint &operator[](const int i) { return v[i]; }
};
using poly = NTTFriendlyPoly<mint>;
template <class Mint>
ostream &operator<<(ostream &ost, NTTFriendlyPoly<Mint> a) {
for (int i = 0; i < a.size(); i++) {
if (i)
cout << " ";
cout << a.v[i];
}
return ost;
}
signed main() {
int N, M;
cin >> N >> M;
poly p(N + 1);
auto in = mint(N + 1).inv();
for (int i = 0; i <= N; i++) {
p[i] = mtable.finv(i) * in * (N + 1 - i);
}
p = p.pow(N + 1, M);
mint ans = 0;
for (int i = 0; i <= N; i++) {
ans += p[i] * mtable.finv(N - i);
}
ans *= mtable.fact(N);
ans *= mint(N + 1).pow(M);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
template <typename A, typename B> inline void chmin(A &a, B b) {
if (a > b)
a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
if (a < b)
a = b;
}
template <uint32_t mod> struct ModInt {
uint32_t a;
ModInt &s(uint32_t vv) {
a = vv < mod ? vv : vv - mod;
return *this;
}
ModInt(int64_t x = 0) { s(x % mod + mod); }
ModInt &operator+=(const ModInt &x) { return s(a + x.a); }
ModInt &operator-=(const ModInt &x) { return s(a + mod - x.a); }
ModInt &operator*=(const ModInt &x) {
a = uint64_t(a) * x.a % mod;
return *this;
}
ModInt &operator/=(const ModInt &x) {
*this *= x.inv();
return *this;
}
ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
bool operator==(const ModInt &x) const { return a == x.a; }
bool operator!=(const ModInt &x) const { return a != x.a; }
ModInt operator-() const { return ModInt() - *this; }
ModInt pow(int64_t n) const {
ModInt res(1), x(*this);
while (n) {
if (n & 1)
res *= x;
x *= x;
n >>= 1;
}
return res;
}
ModInt inv() const { return pow(mod - 2); }
};
using mint = ModInt<998244353>;
template <uint32_t mod> istream &operator>>(istream &in, ModInt<mod> &a) {
return (in >> a.a);
}
template <uint32_t mod>
ostream &operator<<(ostream &out, const ModInt<mod> &a) {
return (out << a.a);
}
using mint = ModInt<998244353>;
template <class Mint, int32_t N> struct ModIntTable {
vector<Mint> facts, finvs, invs;
ModIntTable() : facts(N), finvs(N), invs(N) {
const uint32_t mod = Mint(-1).a + 1;
invs[1] = 1;
for (int i = 2; i < N; i++)
invs[i] = invs[mod % i] * (mod - mod / i);
facts[0] = 1;
finvs[0] = 1;
for (int i = 1; i < N; i++) {
facts[i] = facts[i - 1] * i;
finvs[i] = finvs[i - 1] * invs[i];
}
}
inline Mint fact(int n) { return facts[n]; }
inline Mint finv(int n) { return finvs[n]; }
inline Mint inv(int n) { return invs[n]; }
inline Mint binom(int n, int k) { return facts[n] * finvs[k] * finvs[n - k]; }
};
ModIntTable<mint, 1 << 19> mtable;
template <class Mint, int32_t root> struct NumberTheoreticTransform {
static void ntt(vector<Mint> &f) {
int n = f.size();
int s = __lg(n);
for (int i = 0, j = 1; j < n - 1; j++) {
for (int k = n >> 1; k > (i ^= k); k >>= 1)
;
if (i > j)
swap(f[i], f[j]);
}
for (int m = 1; m <= s; m++) {
Mint wr = Mint(root).pow(Mint(-1).a >> m);
for (int i = 0; i < n; i += 1 << m) {
Mint w = 1;
for (int j = 0; j < 1 << m - 1; j++) {
Mint f0 = f[i + j], f1 = w * f[i + j + (1 << m - 1)];
f[i + j] = f0 + f1;
f[i + j + (1 << m - 1)] = f0 - f1;
w *= wr;
}
}
}
}
static void intt(vector<Mint> &f) {
reverse(f.begin() + 1, f.end());
ntt(f);
Mint in = Mint(f.size()).inv();
for (int i = 0; i < f.size(); i++)
f[i] *= in;
}
static vector<Mint> convolute(const vector<Mint> &A, const vector<Mint> &B) {
if (A.size() == 0 || B.size() == 0)
return {};
int n = 1 << __lg(A.size() + B.size() - 2) + 1;
vector<Mint> a = A, b = B;
a.resize(n);
b.resize(n);
ntt(a);
ntt(b);
for (int i = 0; i < n; i++)
a[i] *= b[i];
intt(a);
a.resize(A.size() + B.size() - 1);
return a;
}
};
using NTT = NumberTheoreticTransform<mint, 3>;
const uint32_t mod = 998244353;
const mint prim_root = 3;
static const int LG = 21;
mint roots[1 << (LG + 1)], iroots[1 << (LG + 1)];
struct PrepareRoots {
PrepareRoots() {
rep(w, LG + 1) {
const int s = (1 << w) - 1;
const mint g = prim_root.pow((mod - 1) / (1 << w)), ig = g.inv();
mint p = 1, ip = 1;
rep(i, 1 << w) {
roots[s + i] = p;
p *= g;
iroots[s + i] = ip;
ip *= ig;
}
}
}
} PrepareRootsDummy;
void broken_fmt(vector<mint> &f) {
const int n = f.size();
for (int b = n / 2; b >= 1; b /= 2) {
for (int i = 0; i < n; i += b * 2) {
rep(j, b) {
mint tmp = f[i + j] - f[i + j + b];
f[i + j] += f[i + j + b];
f[i + j + b] = tmp * roots[b * 2 - 1 + j];
}
}
}
}
void broken_ifmt(vector<mint> &f) {
const int n = f.size();
for (int b = 1; b <= n / 2; b *= 2) {
for (int i = 0; i < n; i += b * 2) {
rep(j, b) {
f[i + j + b] *= iroots[b * 2 - 1 + j];
mint tmp = f[i + j] - f[i + j + b];
f[i + j] += f[i + j + b];
f[i + j + b] = tmp;
}
}
}
mint in = mint(n).inv();
rep(i, n) f[i] *= in;
}
void inplace_fmt(vector<mint> &f, const bool i = false) {
if (!i)
broken_fmt(f);
else
broken_ifmt(f);
}
vector<mint> multiply(vector<mint> x, vector<mint> y, bool same = false) {
int n = x.size() + y.size() - 1;
int s = 1;
while (s < n)
s *= 2;
x.resize(s);
inplace_fmt(x, false);
if (!same) {
y.resize(s);
inplace_fmt(y, false);
} else
y = x;
rep(i, s) x[i] *= y[i];
inplace_fmt(x, true);
x.resize(n);
return x;
}
template <class Mint> struct NTTFriendlyPoly {
void ntt(vector<Mint> &f) {
int n = f.size();
int s = __lg(n);
for (int i = 0, j = 1; j < n - 1; j++) {
for (int k = n >> 1; k > (i ^= k); k >>= 1)
;
if (i > j)
swap(f[i], f[j]);
}
for (int m = 1; m <= s; m++) {
Mint wr = Mint(3).pow(Mint(-1).a >> m);
for (int i = 0; i < n; i += 1 << m) {
Mint w = 1;
for (int j = 0; j < 1 << m - 1; j++) {
Mint f0 = f[i + j], f1 = w * f[i + j + (1 << m - 1)];
f[i + j] = f0 + f1;
f[i + j + (1 << m - 1)] = f0 - f1;
w *= wr;
}
}
}
}
void intt(vector<Mint> &f) {
reverse(f.begin() + 1, f.end());
ntt(f);
Mint in = Mint(f.size()).inv();
for (int i = 0; i < f.size(); i++)
f[i] *= in;
}
vector<Mint> convolute(const vector<Mint> &A, const vector<Mint> &B) {
if (A.size() == 0 || B.size() == 0)
return {};
int n = 1 << __lg(A.size() + B.size() - 2) + 1;
vector<Mint> a = A, b = B;
a.resize(n);
b.resize(n);
ntt(a);
ntt(b);
for (int i = 0; i < n; i++)
a[i] *= b[i];
intt(a);
a.resize(A.size() + B.size() - 1);
return a;
}
vector<Mint> v;
template <class... Args> NTTFriendlyPoly(Args... args) : v(args...) {}
NTTFriendlyPoly(const initializer_list<Mint> &in) : v(in.begin(), in.end()) {}
int size() const { return v.size(); }
inline Mint coef(const int i) const {
return (i < v.size()) ? v[i] : Mint(0);
}
NTTFriendlyPoly operator+(const NTTFriendlyPoly &x) {
int n = max(size(), x.size());
NTTFriendlyPoly<Mint> res(n);
for (int i = 0; i < n; i++)
res[i] = coef(i) + x.coef(i);
return res;
}
NTTFriendlyPoly operator-(const NTTFriendlyPoly &x) {
int n = max(size(), x.size());
NTTFriendlyPoly<Mint> res(n);
for (int i = 0; i < n; i++)
res[i] = coef(i) - x.coef(i);
return res;
}
NTTFriendlyPoly operator*(const NTTFriendlyPoly &x) {
return multiply(v, x.v);
}
NTTFriendlyPoly operator*(const Mint &x) {
int n = size();
vector<Mint> res(n);
for (int i = 0; i < n; i++)
res[i] = v[i] * x;
return res;
}
NTTFriendlyPoly operator/(const Mint &x) { return (*this) * x.inv(); }
NTTFriendlyPoly &operator+=(const NTTFriendlyPoly &x) {
return *this = (*this) + x;
}
NTTFriendlyPoly &operator-=(const NTTFriendlyPoly &x) {
return *this = (*this) - x;
}
NTTFriendlyPoly &operator*=(const NTTFriendlyPoly &x) {
return *this = (*this) * x;
}
NTTFriendlyPoly &operator*=(const Mint &x) { return *this = (*this) * x; }
NTTFriendlyPoly &operator/=(const Mint &x) { return *this = (*this) / x; }
NTTFriendlyPoly operator-() { return NTTFriendlyPoly() - *this; }
NTTFriendlyPoly pre(int n) {
NTTFriendlyPoly<Mint> res(n);
for (int i = 0; i < n && i < size(); i++)
res[i] = v[i];
return res;
}
NTTFriendlyPoly rev() {
vector<Mint> res = v;
while (res.size() && res.back() == 0)
res.pop_back();
reverse(res.begin(), res.end());
return res;
}
NTTFriendlyPoly diff(int n) {
NTTFriendlyPoly<Mint> res(n);
for (int i = 1; i < size() && i <= n; i++)
res[i - 1] = v[i] * i;
return res;
}
NTTFriendlyPoly inte(int n) {
NTTFriendlyPoly<Mint> res(n);
for (int i = 0; i < size() && i + 1 < n; i++)
res[i + 1] = v[i] * mtable.inv(i + 1);
return res;
}
NTTFriendlyPoly inv(int n) {
vector<mint> res{coef(0).inv()};
for (int d = 1; d < n; d <<= 1) {
vector<Mint> f(2 * d), g(2 * d);
for (int j = 0; j < 2 * d; j++)
f[j] = coef(j);
for (int j = 0; j < d; j++)
g[j] = res[j];
inplace_fmt(f);
inplace_fmt(g);
for (int j = 0; j < 2 * d; j++)
f[j] *= g[j];
inplace_fmt(f, true);
for (int j = 0; j < d; j++) {
f[j] = 0;
f[j + d] = -f[j + d];
}
inplace_fmt(f);
for (int j = 0; j < 2 * d; j++)
f[j] *= g[j];
inplace_fmt(f, true);
for (int j = 0; j < d; j++)
f[j] = res[j];
res = f;
}
return NTTFriendlyPoly(res).pre(n);
}
NTTFriendlyPoly inv2(int n) {
NTTFriendlyPoly res{coef(0).inv()};
for (int i = 1; i < n; i *= 2) {
res = (res * Mint(2) - res * res * pre(2 * i)).pre(2 * i);
}
return res.pre(n);
}
NTTFriendlyPoly exp(int n) {
NTTFriendlyPoly f0{1}, g0{1};
vector<Mint> F0{1};
for (int d = 1; d < n; d <<= 1) {
vector<Mint> G0 = g0.v;
inplace_fmt(G0);
vector<Mint> Delta(d);
for (int j = 0; j < d; j++)
Delta[j] = F0[j] * G0[j];
inplace_fmt(Delta, true);
Delta[0] -= 1;
NTTFriendlyPoly delta(2 * d);
for (int j = 0; j < d; j++)
delta[d + j] = Delta[j];
NTTFriendlyPoly epsilon(2 * d);
vector<Mint> DF0 = f0.diff(d - 1).v;
DF0.push_back(0);
inplace_fmt(DF0);
for (int j = 0; j < d; j++)
DF0[j] *= G0[j];
inplace_fmt(DF0, true);
for (int j = 0; j < d - 1; j++) {
epsilon[j] += coef(j + 1) * (j + 1);
epsilon[j + d] += DF0[j] - coef(j + 1) * (j + 1);
}
epsilon[d - 1] += DF0[d - 1];
Delta = delta.v;
inplace_fmt(Delta);
vector<Mint> DH0 = diff(d - 1).v;
DH0.resize(2 * d);
inplace_fmt(DH0);
for (int j = 0; j < 2 * d; j++)
Delta[j] *= DH0[j];
inplace_fmt(Delta, true);
for (int j = 0; j < d; j++)
epsilon[j + d] -= Delta[j + d];
epsilon = epsilon.inte(2 * d) - pre(2 * d);
vector<Mint> Epsilon = epsilon.v;
inplace_fmt(Epsilon);
rep(j, d) DH0[j] = f0[j], DH0[j + d] = 0;
inplace_fmt(DH0);
rep(j, 2 * d) Epsilon[j] *= DH0[j];
inplace_fmt(Epsilon, true);
f0.v.resize(2 * d);
rep(j, d) f0[j + d] -= Epsilon[j + d];
// f0=(f0-epsilon*f0).pre(2*d);
G0.resize(2 * d);
rep(j, d) G0[j] = g0[j];
inplace_fmt(G0);
F0 = f0.v;
inplace_fmt(F0);
vector<Mint> T(2 * d);
rep(j, 2 * d) T[j] = F0[j] * G0[j];
inplace_fmt(T, true);
rep(j, d) {
T[j] = 0;
T[j + d] = -T[j + d];
}
inplace_fmt(T);
rep(j, 2 * d) T[j] *= G0[j];
inplace_fmt(T, true);
rep(j, d) T[j] = g0[j];
g0 = T;
}
return f0.pre(n);
}
NTTFriendlyPoly exp2(int n) {
NTTFriendlyPoly f{1};
for (int i = 1; i < n; i *= 2) {
f = (f * (pre(2 * i) - f.log(2 * i)) + f).pre(2 * i);
}
return f.pre(n);
}
NTTFriendlyPoly exp3(int n) {
NTTFriendlyPoly f{1}, g{1};
for (int d = 1; d < n; d <<= 1) {
g = g * Mint(2) - (g * g * f).pre(d);
NTTFriendlyPoly q = diff(d - 1);
q = q + g * (f.diff(d - 1) - f * q).pre(2 * d - 1);
f = f + (f * (pre(2 * d) - q.inte(2 * d))).pre(2 * d);
}
return f.pre(n);
}
NTTFriendlyPoly log(int n) { return (diff(n - 1) * inv(n - 1)).inte(n); }
NTTFriendlyPoly pow(int n, mint k) {
auto res = log(n);
res *= k;
return res.exp(n);
}
Mint &operator[](const int i) { return v[i]; }
};
using poly = NTTFriendlyPoly<mint>;
template <class Mint>
ostream &operator<<(ostream &ost, NTTFriendlyPoly<Mint> a) {
for (int i = 0; i < a.size(); i++) {
if (i)
cout << " ";
cout << a.v[i];
}
return ost;
}
signed main() {
int N, M;
cin >> N >> M;
poly p(N + 1);
auto in = mint(N + 1).inv();
for (int i = 0; i <= N; i++) {
p[i] = mtable.finv(i) * in * (N + 1 - i);
}
p = p.pow(N + 1, M);
mint ans = 0;
for (int i = 0; i <= N; i++) {
ans += p[i] * mtable.finv(N - i);
}
ans *= mtable.fact(N);
ans *= mint(N + 1).pow(M);
cout << ans << endl;
return 0;
}
| replace | 151 | 152 | 151 | 152 | 0 | |
p02981 | C++ | Runtime Error | #include <stdio.h>
#include <stdlib.h>
int main() {
int n, a, b;
scanf("%d %d %d", &n, &a, &b);
if (n * a > b) {
printf("%d", b);
} else {
printf("%d", a * n);
}
return b;
}
| #include <stdio.h>
#include <stdlib.h>
int main() {
int n, a, b;
scanf("%d %d %d", &n, &a, &b);
if (n * a > b) {
printf("%d", b);
} else {
printf("%d", a * n);
}
return 0;
}
| replace | 14 | 15 | 14 | 15 | 9 | |
p02981 | Python | Runtime Error | N, A, B = map(int, input().split())
ans = []
for i in range(1, N):
ans.append(A * i + B / N * (N - i))
print(min(ans))
| N, A, B = map(int, input().split())
if A * N < B:
print(A * N)
else:
print(B)
| replace | 1 | 5 | 1 | 5 | 0 | |
p02981 | C++ | Runtime Error | /*ربى اصلح لى شأنى*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define el '\n';
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
long long fact(long long n) { return !n ? 1 : n * fact(n - 1); }
using namespace std;
const double PI = acos(-1);
/* smile ^_^ */
using lli = long long int;
using namespace std;
void ezzo() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int main() {
ezzo();
lli n, a, b;
cin >> n >> a >> b;
if ((a * n) > b) {
cout << b << el;
} else {
cout << (a * n) << el;
}
// memset(a, 0, sizeof a);//تصفير الاراى
// cout << fixed << setprecision(9) << a << el;
} | /*ربى اصلح لى شأنى*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define el '\n';
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
long long fact(long long n) { return !n ? 1 : n * fact(n - 1); }
using namespace std;
const double PI = acos(-1);
/* smile ^_^ */
using lli = long long int;
using namespace std;
void ezzo() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
int main() {
ezzo();
lli n, a, b;
cin >> n >> a >> b;
if ((a * n) > b) {
cout << b << el;
} else {
cout << (a * n) << el;
}
// memset(a, 0, sizeof a);//تصفير الاراى
// cout << fixed << setprecision(9) << a << el;
} | delete | 16 | 21 | 16 | 16 | 0 | |
p02981 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define INF 1001001001
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using namespace std;
int main() {
int n, a, b;
cin >> n >> a >> b;
return min(n * a, b);
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define INF 1001001001
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using namespace std;
int main() {
int n, a, b;
cin >> n >> a >> b;
cout << min(n * a, b) << endl;
}
| replace | 24 | 25 | 24 | 25 | 8 | |
p02981 | C++ | Runtime Error | #include <stdio.h>
int main(void) {
int N, A, B;
scanf("%d %d %d", N, A, B);
if (A * N >= B)
printf("%d", B);
else
printf("%d", A * N);
return 0;
} | #include <stdio.h>
int main(void) {
int N, A, B;
scanf("%d %d %d", &N, &A, &B);
if (A * N >= B)
printf("%d", B);
else
printf("%d", A * N);
return 0;
} | replace | 3 | 4 | 3 | 4 | -11 | |
p02981 | C++ | Runtime Error | /*
Templatka c++ Kacper Fis
//
*/
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000001;
const float PI = 3.14;
typedef long long LL;
typedef vector<int> vi;
typedef list<int> li;
typedef queue<int> qi;
typedef pair<int, int> ii;
typedef map<string, int> msi; // np msi['nazwa'] = 'liczba'
typedef vector<ii> vii; // vector par<int>, do tworzenia tablicy dla grafów
// wagowych #dijkstra #Bellman_Ford
typedef priority_queue<ii, vector<ii>, greater<ii>>
pq; // kolejka priorytetowa vectorów par(top()=min) #dijkstra
vector<ii>::iterator iter;
#define print_list(x) \
for (it = (x).begin(); it != (x).end(); it++) { \
cout << *it << " "; \
}
#define print_vector(x) \
for (it2 = (x).begin(); it2 != (x).end(); it2++) { \
cout << *it2 << " "; \
}
#define search_list(x) for (it = (x).begin(); it != (x).end(); it++)
#define search_vector(x) \
for (it2 = (x).begin(); it2 != (x).end(); it2++) \
;
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x, y) make_pair(x, y)
///////////////////////////////////////////////GLOBAL
///DEFINITIONS////////////////////////////////////////////////////////
///////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////MAIN////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LL l, r;
cin >> l >> r;
vector<LL> tab;
vector<LL>::iterator it2;
if (l == r)
cout << (r * l) % 2019 << endl;
else {
if ((r - l) > 2019) {
for (LL i = l; i <= 2020; i++) {
for (LL j = l; j <= 2020; j++) {
if (i != j)
tab.pb((i * j) % 2019);
}
}
} else {
for (LL i = l; i <= r; i++) {
for (LL j = l; j <= r; j++) {
if (i != j)
tab.pb((i * j) % 2019);
}
}
}
sort(tab.begin(), tab.end());
// for(it2 = tab.begin(); it2 != tab.end(); it2++){
// cout << *it2 << " ";
//}
// cout << endl;
cout << *tab.begin() << endl;
}
return 0;
}
| /*
Templatka c++ Kacper Fis
//
*/
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000001;
const float PI = 3.14;
typedef long long LL;
typedef vector<int> vi;
typedef list<int> li;
typedef queue<int> qi;
typedef pair<int, int> ii;
typedef map<string, int> msi; // np msi['nazwa'] = 'liczba'
typedef vector<ii> vii; // vector par<int>, do tworzenia tablicy dla grafów
// wagowych #dijkstra #Bellman_Ford
typedef priority_queue<ii, vector<ii>, greater<ii>>
pq; // kolejka priorytetowa vectorów par(top()=min) #dijkstra
vector<ii>::iterator iter;
#define print_list(x) \
for (it = (x).begin(); it != (x).end(); it++) { \
cout << *it << " "; \
}
#define print_vector(x) \
for (it2 = (x).begin(); it2 != (x).end(); it2++) { \
cout << *it2 << " "; \
}
#define search_list(x) for (it = (x).begin(); it != (x).end(); it++)
#define search_vector(x) \
for (it2 = (x).begin(); it2 != (x).end(); it2++) \
;
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x, y) make_pair(x, y)
///////////////////////////////////////////////GLOBAL
///DEFINITIONS////////////////////////////////////////////////////////
///////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////MAIN////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int n, a, b;
cin >> n >> a >> b;
if (n * a < b) {
cout << n * a << endl;
} else
cout << b << '\n';
return 0;
}
| replace | 52 | 84 | 52 | 58 | -11 | |
p02981 | C++ | Runtime Error | #include <stdio.h>
int main() {
int N, A, B;
scanf("%d%d%d", N, A, B);
if (N * A <= B)
printf("%d\n", N * A);
else
printf("%d\n", B);
return 0;
} | #include <stdio.h>
int main() {
int N, A, B;
scanf("%d%d%d", &N, &A, &B);
if (N * A <= B)
printf("%d\n", N * A);
else
printf("%d\n", B);
return 0;
}
| replace | 3 | 4 | 3 | 4 | -11 | |
p02981 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
return min(a * b, c);
} | #include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
cout << min(a * b, c) << endl;
}
| replace | 6 | 7 | 6 | 7 | 8 | |
p02981 | C++ | Runtime Error | #include <stdio.h>
using namespace std;
int main() {
int N, A, B;
int sc = scanf("%d %d %d", &N, &A, &B);
return A * N < B ? A * N : B;
} | #include <stdio.h>
using namespace std;
int main() {
int N, A, B;
int sc = scanf("%d %d %d", &N, &A, &B);
printf("%d\n", A * N < B ? A * N : B);
} | replace | 5 | 6 | 5 | 6 | 8 | |
p02981 | C++ | Runtime Error | #include <stdio.h>
int main(void) {
int n, a, b, c, d;
scanf("%d", &n);
scanf("%d", &a);
scanf("%d", &b);
d = n * a;
if (d > b) {
c = b;
} else {
c = d;
}
printf("%d\n", c);
return 9;
} | #include <stdio.h>
int main(void) {
int n, a, b, c, d;
scanf("%d", &n);
scanf("%d", &a);
scanf("%d", &b);
d = n * a;
if (d > b) {
c = b;
} else {
c = d;
}
printf("%d\n", c);
return 0;
} | replace | 13 | 14 | 13 | 14 | 9 | |
p02981 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265
#define OO 1e9
#define SS second
#define FF first
#define Trace(n) cout << #n << " = " << n << endl;
#define ll long long
#define endl "\n"
//@formatter:off
int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[] = {-1, 1, 0, 0, 1, -1, 1, -1};
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
//@formatter:on
int main() {
fast();
int a, b, c;
cin >> a >> b >> c;
cout << min(a * b, c);
}
| #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265
#define OO 1e9
#define SS second
#define FF first
#define Trace(n) cout << #n << " = " << n << endl;
#define ll long long
#define endl "\n"
//@formatter:off
int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[] = {-1, 1, 0, 0, 1, -1, 1, -1};
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
//@formatter:on
int main() {
fast();
int a, b, c;
cin >> a >> b >> c;
cout << min(a * b, c);
}
| delete | 22 | 27 | 22 | 22 | 0 | |
p02982 | C++ | Runtime Error | #include <assert.h>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char const *argv[]) {
int n, d;
cin >> n >> d;
vector<vector<int>> x(n, vector<int>(4));
for (int i = 0; i < n; i++) {
for (int j = 0; j < d; j++) {
cin >> x.at(i).at(j);
}
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double result = 0;
for (int k = 0; k < d; k++) {
if (x.at(i).at(k) - x.at(j).at(k) != 0) {
result += pow(x.at(i).at(k) - x.at(j).at(k), 2.0);
}
}
result = sqrt(result);
if (ceil(result) == result)
count++;
}
}
cout << count;
return 0;
} | #include <assert.h>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char const *argv[]) {
int n, d;
cin >> n >> d;
vector<vector<int>> x(n, vector<int>(d));
for (int i = 0; i < n; i++) {
for (int j = 0; j < d; j++) {
cin >> x.at(i).at(j);
}
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double result = 0;
for (int k = 0; k < d; k++) {
if (x.at(i).at(k) - x.at(j).at(k) != 0) {
result += pow(x.at(i).at(k) - x.at(j).at(k), 2.0);
}
}
result = sqrt(result);
if (ceil(result) == result)
count++;
}
}
cout << count;
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
cin >> N >> D;
vector<vector<int>> X(N, vector<int>(D));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> X[i][j];
}
}
int ans = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
int norm = 0;
for (int k = 0; k < D; k++) {
int diff = abs(X[i][k] - X[j][k]);
norm += diff * diff;
}
bool flag = false;
for (int k = 0; k <= norm; k++) {
if (k * k == norm)
flag = true;
}
if (flag)
ans++;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
cin >> N >> D;
vector<vector<int>> X(N, vector<int>(D));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> X[i][j];
}
}
int ans = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
int norm = 0;
for (int k = 0; k < D; k++) {
int diff = abs(X[i][k] - X[j][k]);
norm += diff * diff;
}
bool flag = false;
for (int k = 0; k <= norm; k++) {
if (k * k == norm)
flag = true;
}
if (flag)
ans++;
}
}
cout << ans << endl;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
cin >> N >> D;
vector<vector<double>> Vec(N, vector<double>(4));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> Vec.at(i).at(j);
}
}
int count = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
double DisDbl = 0;
for (int k = 0; k < D; k++) {
DisDbl += pow(Vec.at(i).at(k) - Vec.at(j).at(k), 2.0);
}
int DisDblDwn = pow(DisDbl, 0.5);
if (DisDbl == DisDblDwn * DisDblDwn) {
count++;
}
}
}
cout << count << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
cin >> N >> D;
vector<vector<double>> Vec(N, vector<double>(D));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> Vec.at(i).at(j);
}
}
int count = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
double DisDbl = 0;
for (int k = 0; k < D; k++) {
DisDbl += pow(Vec.at(i).at(k) - Vec.at(j).at(k), 2.0);
}
int DisDblDwn = pow(DisDbl, 0.5);
if (DisDbl == DisDblDwn * DisDblDwn) {
count++;
}
}
}
cout << count << endl;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02982 | Python | Runtime Error | N, D = map(int, input().split())
X = list(map(int, input().split()))
count = 0
for i in range(N - 1):
for j in range(i + 1, N):
r = 0
for k in range(D):
r += (X[i][k] - X[j][k]) ** 2
if r**0.5 % 1 == 0:
count += 1
print(count)
| N, D = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]
count = 0
for i in range(N - 1):
for j in range(i + 1, N):
r = 0
for k in range(D):
r += (X[i][k] - X[j][k]) ** 2
if r**0.5 % 1 == 0:
count += 1
print(count)
| replace | 1 | 2 | 1 | 2 | TypeError: 'int' object is not subscriptable | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02982/Python/s418821927.py", line 8, in <module>
r += (X[i][k] - X[j][k]) ** 2
TypeError: 'int' object is not subscriptable
|
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const ll mod = (ll)1e9 + 7;
const ll INF = 0x3f3f3f3f;
int a[11][11];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("A.txt", "r", stdin);
#endif
int n, r, ans = 0;
cin >> n >> r;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= r; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int x = 1; i + x <= n; x++) {
double d = 0;
for (int j = 1; j <= r; j++) {
d += (a[i + x][j] - a[i][j]) * (a[i + x][j] - a[i][j]);
}
if (sqrt(d) == floor(sqrt(d)))
ans++;
}
}
cout << ans;
}
| #include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const ll mod = (ll)1e9 + 7;
const ll INF = 0x3f3f3f3f;
int a[11][11];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
// freopen("A.txt", "r", stdin);
#endif
int n, r, ans = 0;
cin >> n >> r;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= r; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int x = 1; i + x <= n; x++) {
double d = 0;
for (int j = 1; j <= r; j++) {
d += (a[i + x][j] - a[i][j]) * (a[i + x][j] - a[i][j]);
}
if (sqrt(d) == floor(sqrt(d)))
ans++;
}
}
cout << ans;
}
| replace | 13 | 14 | 13 | 14 | TLE | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, d, a[11][11], i, j, k, count = 0, ans = 0;
double s[50] = {0}, b[i][j];
cin >> n >> d;
for (i = 0; i < n; i++) {
for (j = 0; j < d; j++) {
cin >> a[i][j];
b[i][j] = a[i][j];
}
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
for (k = 0; k < d; k++) {
s[ans] = s[ans] + (pow((b[i][k] - b[j][k]), 2));
}
s[ans] = sqrt(s[ans]);
if ((round(s[ans]) - (s[ans])) == 0) {
count++;
}
ans++;
}
}
cout << count << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, d, a[11][11], i, j, k, count = 0, ans = 0;
double s[50] = {0}, b[11][11];
cin >> n >> d;
for (i = 0; i < n; i++) {
for (j = 0; j < d; j++) {
cin >> a[i][j];
b[i][j] = a[i][j];
}
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
for (k = 0; k < d; k++) {
s[ans] = s[ans] + (pow((b[i][k] - b[j][k]), 2));
}
s[ans] = sqrt(s[ans]);
if ((round(s[ans]) - (s[ans])) == 0) {
count++;
}
ans++;
}
}
cout << count << endl;
return 0;
}
| replace | 5 | 6 | 5 | 6 | -11 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <math.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int mINF = -1001001001;
int main() {
int n, d;
cin >> n >> d;
int x[n][d];
REP(i, n) {
REP(j, d) { cin >> x[i][j]; }
}
int ans = 0;
int sum;
REP(i, n - 1) {
for (int j = i + 1; j < n; j++) {
sum = 0;
REP(k, d) { sum += pow((x[i][k] - x[j][k]), 2); }
REP(l, 130) {
if (sum % l == 0 && sum / l == l)
ans++;
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <math.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int mINF = -1001001001;
int main() {
int n, d;
cin >> n >> d;
int x[n][d];
REP(i, n) {
REP(j, d) { cin >> x[i][j]; }
}
int ans = 0;
int sum;
REP(i, n - 1) {
for (int j = i + 1; j < n; j++) {
sum = 0;
for (int k = 0; k < d; k++) {
sum += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]);
}
for (int l = 0; l < 130; l++) {
if (sum == l * l)
ans++;
}
}
}
cout << ans << endl;
return 0;
}
| replace | 22 | 25 | 22 | 27 | -8 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
cin >> N >> D;
vector<vector<int>> vec(N, vector<int>(D));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> vec.at(i).at(j);
}
}
int count = 0;
double sum = 0;
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < D; j++) {
sum = 0;
for (int k = 0; k < D; k++) {
sum = sum + std::pow((vec.at(i).at(k) - vec.at(j).at(k)), 2);
}
sum = std::pow(sum, 0.5);
if (ceil(sum) == floor(sum)) {
count++;
}
}
}
cout << count << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
cin >> N >> D;
vector<vector<int>> vec(N, vector<int>(D));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> vec.at(i).at(j);
}
}
int count = 0;
double sum = 0;
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
sum = 0;
for (int k = 0; k < D; k++) {
sum = sum + std::pow((vec.at(i).at(k) - vec.at(j).at(k)), 2);
}
sum = std::pow(sum, 0.5);
if (ceil(sum) == floor(sum)) {
count++;
}
}
}
cout << count << endl;
} | replace | 16 | 17 | 16 | 17 | 0 | |
p02982 | C++ | Runtime Error | #include "bits/stdc++.h"
#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define debug_print(var) cerr << #var << "=" << var << endl;
#define DUMP(i, v) \
for (ll i = 0; i < v.size(); i++) \
cerr << v[i] << " "
using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge {
ll x, c;
};
ll mod(ll a, ll mod) {
ll res = a % mod;
if (res < 0)
res = res + mod;
return res;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }
ll gcd(ll a, ll b) {
ll r = a % b;
if (r == 0)
return b;
else
return gcd(b, a % b);
}
bool is_prime(ll n) {
ll i = 2;
if (n == 1)
return false;
if (n == 2)
return true;
bool res = true;
while (i * i < n) {
if (n % i == 0) {
res = false;
}
i = i + 1;
}
// if(i==1)res = false;
if (n % i == 0)
res = false;
return res;
}
struct UnionFind {
ll N;
llvec p;
llvec cnt;
UnionFind(ll n) {
N = n;
p = llvec(N);
cnt = llvec(N, 0);
rep(i, N) {
p[i] = i;
cnt[i] = 1;
}
}
void con(ll a, ll b) {
P at = root(a);
P bt = root(b);
if (at.second != bt.second) {
if (at.first > bt.first) {
swap(at, bt);
swap(a, b);
}
p[at.second] = bt.second;
cnt[bt.second] += cnt[at.second];
p[a] = bt.second;
}
}
P root(ll a) {
ll atmp = a;
ll c = 0;
while (atmp != p[atmp]) {
atmp = p[atmp];
c++;
}
p[a] = atmp;
return {c, atmp};
}
bool is_con(ll a, ll b) {
P at = root(a);
P bt = root(b);
return at.second == bt.second;
}
};
struct dijkstra {
ll N;
llvec d;
vector<vector<edge>> e;
dijkstra(ll n) {
N = n;
// d = llvec(N, 1e18);
e = vector<vector<edge>>(N);
}
void add_edge(ll from, ll to, ll cost) { e[from].push_back({to, cost}); }
void run(ll start) {
priority_queue<P, vector<P>, greater<P>> que;
que.push({0, start});
d = llvec(N, 1e18);
d[start] = 0;
while (!que.empty()) {
P q = que.top();
que.pop();
ll dc = q.first;
ll x = q.second;
if (dc > d[x]) {
continue;
} else {
for (auto ip : e[x]) {
if (d[ip.x] <= d[x] + ip.c) {
continue;
} else {
d[ip.x] = d[x] + ip.c;
que.push({d[ip.x], ip.x});
}
}
}
}
}
};
/**************************************
** A main function starts from here **
***************************************/
int main() {
ll N, D;
cin >> N >> D;
vector<llvec> x(N, llvec(D));
rep(i, N) { rep(j, D) cin >> x[i][D]; }
set<ll> d;
rep(i, 10000) { d.insert(i * i); }
ll cnt = 0;
rep(i, N) {
FOR(j, i + 1, N) {
ll tmp = 0;
rep(k, D) { tmp += pow(x[i][k] - x[j][k], 2); }
if (d.count(tmp) > 0)
cnt++;
}
}
cout << cnt;
return 0;
}
| #include "bits/stdc++.h"
#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define debug_print(var) cerr << #var << "=" << var << endl;
#define DUMP(i, v) \
for (ll i = 0; i < v.size(); i++) \
cerr << v[i] << " "
using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge {
ll x, c;
};
ll mod(ll a, ll mod) {
ll res = a % mod;
if (res < 0)
res = res + mod;
return res;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }
ll gcd(ll a, ll b) {
ll r = a % b;
if (r == 0)
return b;
else
return gcd(b, a % b);
}
bool is_prime(ll n) {
ll i = 2;
if (n == 1)
return false;
if (n == 2)
return true;
bool res = true;
while (i * i < n) {
if (n % i == 0) {
res = false;
}
i = i + 1;
}
// if(i==1)res = false;
if (n % i == 0)
res = false;
return res;
}
struct UnionFind {
ll N;
llvec p;
llvec cnt;
UnionFind(ll n) {
N = n;
p = llvec(N);
cnt = llvec(N, 0);
rep(i, N) {
p[i] = i;
cnt[i] = 1;
}
}
void con(ll a, ll b) {
P at = root(a);
P bt = root(b);
if (at.second != bt.second) {
if (at.first > bt.first) {
swap(at, bt);
swap(a, b);
}
p[at.second] = bt.second;
cnt[bt.second] += cnt[at.second];
p[a] = bt.second;
}
}
P root(ll a) {
ll atmp = a;
ll c = 0;
while (atmp != p[atmp]) {
atmp = p[atmp];
c++;
}
p[a] = atmp;
return {c, atmp};
}
bool is_con(ll a, ll b) {
P at = root(a);
P bt = root(b);
return at.second == bt.second;
}
};
struct dijkstra {
ll N;
llvec d;
vector<vector<edge>> e;
dijkstra(ll n) {
N = n;
// d = llvec(N, 1e18);
e = vector<vector<edge>>(N);
}
void add_edge(ll from, ll to, ll cost) { e[from].push_back({to, cost}); }
void run(ll start) {
priority_queue<P, vector<P>, greater<P>> que;
que.push({0, start});
d = llvec(N, 1e18);
d[start] = 0;
while (!que.empty()) {
P q = que.top();
que.pop();
ll dc = q.first;
ll x = q.second;
if (dc > d[x]) {
continue;
} else {
for (auto ip : e[x]) {
if (d[ip.x] <= d[x] + ip.c) {
continue;
} else {
d[ip.x] = d[x] + ip.c;
que.push({d[ip.x], ip.x});
}
}
}
}
}
};
/**************************************
** A main function starts from here **
***************************************/
int main() {
ll N, D;
cin >> N >> D;
vector<llvec> x(N, llvec(D));
rep(i, N) { rep(j, D) cin >> x[i][j]; }
set<ll> d;
rep(i, 10000) { d.insert(i * i); }
ll cnt = 0;
rep(i, N) {
FOR(j, i + 1, N) {
ll tmp = 0;
rep(k, D) { tmp += pow(x[i][k] - x[j][k], 2); }
if (d.count(tmp) > 0)
cnt++;
}
}
cout << cnt;
return 0;
}
| replace | 162 | 163 | 162 | 163 | 0 | |
p02982 | C++ | Runtime Error | #pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vi2 = vector<vector<int>>;
using vb = vector<bool>;
using vb2 = vector<vector<bool>>;
using vc = vector<char>;
using vc2 = vector<vector<char>>;
using vs = vector<string>;
using vs2 = vector<vector<string>>;
using pii = pair<int, int>;
using mii = map<int, int>;
const int INF = 1e9;
const ll LINF = 1e18;
#define MP(a, b) make_pair((a), (b))
#define MT(...) make_tuple(__VA_ARGS__)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define IN(type, a) \
type a; \
cin >> a
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define COUT(x) cout << (x) << endl
#define DCOUT(x, n) cout << fixed << setprecision(n) << (x) << endl
int gcd(int x, int y) {
if ((x == 1) || (y == 1)) {
return 1;
}
if (x > y) {
if (x % y == 0) {
return y;
} else {
return gcd(y, x % y);
}
} else {
if (y % x == 0) {
return x;
} else {
return gcd(x, y % x);
}
}
}
bool sqc(int x) {
for (int i = 0; i * i <= x; i++) {
if (i * i == x) {
return true;
}
}
return false;
}
int main() {
IN(int, n);
IN(int, d);
vi2 a(n, vi(d));
REP(i, n) {
REP(j, d) { cin >> a[i][j]; }
}
int ans = 0;
REP(i, n - 1) {
FOR(j, i + 1, n) {
int sum = 0;
REP(k, d) { sum += (a[i][k] - a[j][k]) * (a[i][k] - a[j][k]); }
if (sqc(sum)) {
ans++;
}
}
}
COUT(ans);
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vi2 = vector<vector<int>>;
using vb = vector<bool>;
using vb2 = vector<vector<bool>>;
using vc = vector<char>;
using vc2 = vector<vector<char>>;
using vs = vector<string>;
using vs2 = vector<vector<string>>;
using pii = pair<int, int>;
using mii = map<int, int>;
const int INF = 1e9;
const ll LINF = 1e18;
#define MP(a, b) make_pair((a), (b))
#define MT(...) make_tuple(__VA_ARGS__)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define IN(type, a) \
type a; \
cin >> a
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define COUT(x) cout << (x) << endl
#define DCOUT(x, n) cout << fixed << setprecision(n) << (x) << endl
int gcd(int x, int y) {
if ((x == 1) || (y == 1)) {
return 1;
}
if (x > y) {
if (x % y == 0) {
return y;
} else {
return gcd(y, x % y);
}
} else {
if (y % x == 0) {
return x;
} else {
return gcd(x, y % x);
}
}
}
bool sqc(int x) {
for (int i = 0; i * i <= x; i++) {
if (i * i == x) {
return true;
}
}
return false;
}
int main() {
IN(int, n);
IN(int, d);
vi2 a(n, vi(d));
REP(i, n) {
REP(j, d) { cin >> a[i][j]; }
}
int ans = 0;
REP(i, n - 1) {
FOR(j, i + 1, n) {
int sum = 0;
REP(k, d) { sum += (a[i][k] - a[j][k]) * (a[i][k] - a[j][k]); }
if (sqc(sum)) {
ans++;
}
}
}
COUT(ans);
}
| replace | 0 | 3 | 0 | 1 | -4 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
vector<vector<int>> x(n, vector<int>(d));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> x[i][j];
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int norm = 0;
for (int k = 0; k < d; k++) {
int diff = abs(x[i][k] - x[j][k]);
norm += diff * diff;
}
bool flag = false;
for (int k = 0; k <= norm; k++) {
if (k * k == norm) {
flag = true;
}
}
if (flag) {
ans++;
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
vector<vector<int>> x(n, vector<int>(d));
for (int i = 0; i < n; i++) {
for (int j = 0; j < d; j++) {
cin >> x[i][j];
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int norm = 0;
for (int k = 0; k < d; k++) {
int diff = abs(x[i][k] - x[j][k]);
norm += diff * diff;
}
bool flag = false;
for (int k = 0; k <= norm; k++) {
if (k * k == norm) {
flag = true;
}
}
if (flag) {
ans++;
}
}
}
cout << ans << endl;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<unsigned long long> next_p_mod(unsigned N,
long long mod) { // next_permutation
vector<unsigned long long> Next_p(N);
unsigned long long Next_p_buff = 1;
for (int i = 0; i < N; i++) {
Next_p_buff *= (i + 1) % mod;
Next_p.at(i) = Next_p_buff % mod;
}
return Next_p;
}
unsigned long long modpow(unsigned long long a, unsigned long long n,
unsigned long long mod) {
unsigned long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
void comb(vector<vector<long long int>> &v, unsigned long long mod) {
// vector<入れたい型> v(配列の個数(x),vector<入れたい型> (配列の個数,初期値));
for (int i = 0; i < v.size(); i++) {
v[i][0] = 1;
v[i][i] = 1;
}
for (int k = 1; k < v.size(); k++) {
for (int j = 1; j < k; j++) {
v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]) % mod;
}
}
}
// a^{-1} mod を計算する
unsigned long long modinv(unsigned long long a, unsigned long long mod) {
return modpow(a, mod - 2, mod);
}
// modinv
/*void modinv(unsigned long long N, unsigned long long mod,unsigned long long
*inv) {
// unsigned long long inv[N] ;
for (unsigned long long i = 1; i < N; i++) {
inv[i] = (i == 1) ? 1 : (mod - (mod / i) * inv[mod % i] % mod);
assert(inv[i] * i % mod == 1);
}
//return 0;
}*/
int main() {
int N, D;
ll chk = 0;
long chk2 = 0;
double chk3 = 0;
unsigned long long mod = 1000000000 + 7;
cin >> N >> D;
int C = 0;
vector<vector<int>> v(N, vector<int>(2));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> v[i][j];
}
}
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
for (int k = 0; k < D; k++) {
chk += pow(v[i][k] - v[j][k], 2.0);
}
// cout<<chk<<" : ";
chk2 = sqrt(chk);
chk3 = sqrt(chk * 1.0);
// cout<<chk<<endl;
if (chk2 == chk3) {
C++;
}
chk = 0;
}
}
cout << C << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<unsigned long long> next_p_mod(unsigned N,
long long mod) { // next_permutation
vector<unsigned long long> Next_p(N);
unsigned long long Next_p_buff = 1;
for (int i = 0; i < N; i++) {
Next_p_buff *= (i + 1) % mod;
Next_p.at(i) = Next_p_buff % mod;
}
return Next_p;
}
unsigned long long modpow(unsigned long long a, unsigned long long n,
unsigned long long mod) {
unsigned long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
void comb(vector<vector<long long int>> &v, unsigned long long mod) {
// vector<入れたい型> v(配列の個数(x),vector<入れたい型> (配列の個数,初期値));
for (int i = 0; i < v.size(); i++) {
v[i][0] = 1;
v[i][i] = 1;
}
for (int k = 1; k < v.size(); k++) {
for (int j = 1; j < k; j++) {
v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]) % mod;
}
}
}
// a^{-1} mod を計算する
unsigned long long modinv(unsigned long long a, unsigned long long mod) {
return modpow(a, mod - 2, mod);
}
// modinv
/*void modinv(unsigned long long N, unsigned long long mod,unsigned long long
*inv) {
// unsigned long long inv[N] ;
for (unsigned long long i = 1; i < N; i++) {
inv[i] = (i == 1) ? 1 : (mod - (mod / i) * inv[mod % i] % mod);
assert(inv[i] * i % mod == 1);
}
//return 0;
}*/
int main() {
int N, D;
ll chk = 0;
long chk2 = 0;
double chk3 = 0;
unsigned long long mod = 1000000000 + 7;
cin >> N >> D;
long C = 0;
vector<vector<int>> v(N, vector<int>(D));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> v[i][j];
}
}
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
for (int k = 0; k < D; k++) {
chk += pow(v[i][k] - v[j][k], 2.0);
}
// cout<<chk<<" : ";
chk2 = sqrt(chk);
chk3 = sqrt(chk * 1.0);
// cout<<chk<<endl;
if (chk2 == chk3) {
C++;
}
chk = 0;
}
}
cout << C << endl;
return 0;
} | replace | 60 | 62 | 60 | 62 | 0 | |
p02982 | C++ | Runtime Error | // include
//------------------------------------------
#include <bits/stdc++.h>
using namespace std;
// typedef
//------------------------------------------
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int MOD = 1000000007;
// grid
//--------------------------------------------
VL dx = {0, 1, 0, -1};
VL dy = {1, 0, -1, 0};
VL dx2 = {-1, 0, 1, -1, 1, -1, 0, 1};
VL dy2 = {-1, -1, -1, 0, 0, 1, 1, 1};
// debug
//--------------------------------------------
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
// IO accelerate
//--------------------------------------------
struct InitIO {
InitIO() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(30);
}
} init_io;
// template
//--------------------------------------------
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 (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : "\t");
}
return os;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
for (int i = 0; i < vv.size(); ++i) {
s << vv[i] << endl;
}
return s;
}
// main code
int main(int argc, char const *argv[]) {
int n, d;
cin >> n >> d;
VVL x(n, VL(4));
for (int i = 0; i < n; i++) {
for (int j = 0; j < d; j++) {
cin >> x[i][j];
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
LL d2 = 0;
for (int z = 0; z < d; z++) {
d2 += (x[i][z] - x[j][z]) * (x[i][z] - x[j][z]);
}
for (LL m = 0; m < 200; m++) {
if (m * m == d2) {
ans++;
}
}
}
}
cout << ans << endl;
return 0;
}
| // include
//------------------------------------------
#include <bits/stdc++.h>
using namespace std;
// typedef
//------------------------------------------
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int MOD = 1000000007;
// grid
//--------------------------------------------
VL dx = {0, 1, 0, -1};
VL dy = {1, 0, -1, 0};
VL dx2 = {-1, 0, 1, -1, 1, -1, 0, 1};
VL dy2 = {-1, -1, -1, 0, 0, 1, 1, 1};
// debug
//--------------------------------------------
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
// IO accelerate
//--------------------------------------------
struct InitIO {
InitIO() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(30);
}
} init_io;
// template
//--------------------------------------------
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 (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : "\t");
}
return os;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
for (int i = 0; i < vv.size(); ++i) {
s << vv[i] << endl;
}
return s;
}
// main code
int main(int argc, char const *argv[]) {
int n, d;
cin >> n >> d;
VVL x(n, VL(d));
for (int i = 0; i < n; i++) {
for (int j = 0; j < d; j++) {
cin >> x[i][j];
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
LL d2 = 0;
for (int z = 0; z < d; z++) {
d2 += (x[i][z] - x[j][z]) * (x[i][z] - x[j][z]);
}
for (LL m = 0; m < 200; m++) {
if (m * m == d2) {
ans++;
}
}
}
}
cout << ans << endl;
return 0;
}
| replace | 81 | 82 | 81 | 82 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
long double dist(int i, int j, vector<vector<int>> p, int d) {
long long sum = 0;
for (int k = 0; i < d; i++)
sum += pow(p.at(i).at(k) - p.at(j).at(k), 2);
return sqrt(sum);
}
int main() {
int n, d;
cin >> n >> d;
vector<vector<int>> p(n, vector<int>(d));
for (int i = 0; i < n; i++)
for (int j = 0; j < d; j++)
cin >> p.at(i).at(j);
int cnt = 0;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (dist(i, j, p, d) == (int)dist(i, j, p, d))
cnt++;
cout << cnt << endl;
} | #include <bits/stdc++.h>
using namespace std;
long double dist(int i, int j, vector<vector<int>> p, int d) {
long long sum = 0;
for (int k = 0; k < d; k++)
sum += pow(p.at(i).at(k) - p.at(j).at(k), 2);
return sqrt(sum);
}
int main() {
int n, d;
cin >> n >> d;
vector<vector<int>> p(n, vector<int>(d));
for (int i = 0; i < n; i++)
for (int j = 0; j < d; j++)
cin >> p.at(i).at(j);
int cnt = 0;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (dist(i, j, p, d) == (int)dist(i, j, p, d))
cnt++;
cout << cnt << endl;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
vector<vector<int>> a(n, vector<int>(d));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a.at(i).at(j);
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
int sq = 0;
for (int k = 0; k < d; k++) {
int s = a.at(i).at(k) - a.at(j).at(k);
sq += s * s;
}
int s = sqrt(sq) + 0.5;
if (s * s == sq)
ans++;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
vector<vector<int>> a(n, vector<int>(d));
for (int i = 0; i < n; i++) {
for (int j = 0; j < d; j++) {
cin >> a.at(i).at(j);
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
int sq = 0;
for (int k = 0; k < d; k++) {
int s = a.at(i).at(k) - a.at(j).at(k);
sq += s * s;
}
int s = sqrt(sq) + 0.5;
if (s * s == sq)
ans++;
}
}
cout << ans << endl;
} | replace | 8 | 9 | 8 | 9 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 2) >= this->size() (which is 2)
|
p02982 | C++ | Runtime Error | #include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"
using namespace std;
// const long long int MOD = 1000000007;
const int MOD = 1000000007;
// const int MOD = 998244353;
// const long long int MOD = 998244353;
const double EPS = 1e-8;
// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
vector<vector<int>> v(M, vector<int>(M));
for (auto &i : v)
for (auto &j : i)
cin >> j;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
int box = 0;
for (int k = 0; k < M; k++)
box += pow(v[i][k] - v[j][k], 2);
int bag = sqrt(box);
if (bag * bag == box)
K++;
}
}
cout << K << endl;
} | #include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"
using namespace std;
// const long long int MOD = 1000000007;
const int MOD = 1000000007;
// const int MOD = 998244353;
// const long long int MOD = 998244353;
const double EPS = 1e-8;
// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
vector<vector<int>> v(N, vector<int>(M));
for (auto &i : v)
for (auto &j : i)
cin >> j;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
int box = 0;
for (int k = 0; k < M; k++)
box += pow(v[i][k] - v[j][k], 2);
int bag = sqrt(box);
if (bag * bag == box)
K++;
}
}
cout << K << endl;
} | replace | 36 | 37 | 36 | 37 | -11 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef vector<pair<ll, ll>> vp;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
#define inputv(v, n) \
rep(i, n) { \
ll x; \
cin >> x; \
v.push_back(x); \
}
const ll INF = 99999999999999;
const ll MOD = 1000000007;
int main() {
ll n, d;
cin >> n >> d;
double a[n][d];
rep(i, n) {
rep(j, d) { cin >> a[i][j]; }
}
int count = 0;
double sum = 0;
rep(i, n) {
for (int j = i + 1; j < n; i++) {
for (int k = 0; k < d; k++) {
sum += (a[i][k] - a[j][k]) * (a[i][k] - a[j][k]);
}
if (floor(sqrt(sum)) == sqrt(sum)) {
count++;
}
sum = 0;
}
}
cout << count;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef vector<pair<ll, ll>> vp;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
#define inputv(v, n) \
rep(i, n) { \
ll x; \
cin >> x; \
v.push_back(x); \
}
const ll INF = 99999999999999;
const ll MOD = 1000000007;
int main() {
ll n, d;
cin >> n >> d;
double a[n][d];
rep(i, n) {
rep(j, d) { cin >> a[i][j]; }
}
int count = 0;
double sum = 0;
rep(i, n) {
for (int j = i + 1; j < n; j++) {
rep(k, d) { sum += (a[i][k] - a[j][k]) * (a[i][k] - a[j][k]); }
if (floor(sqrt(sum)) == sqrt(sum)) {
count++;
}
sum = 0;
}
}
cout << count;
return 0;
}
| replace | 26 | 30 | 26 | 28 | -11 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define ll long long
#define pp pair<int, int>
#define v(T) vector<T>
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
const int INF = 1001001001;
// int 2×10の9乗
// vector<vector<int>> a(n, vector<int>(m));
// rep(i,n) rep(j,m) cin >> a[i][j];
// cout << fixed << setprecision(10);
// int gcd(int x, int y){
// if(y==0) return x;
// return gcd(y, x%y);
//}
// sort小さい順(昇順)
int main() {
int n, d;
cin >> n >> d;
vector<vector<int>> x(n, vector<int>(d));
rep(i, n) rep(j, d) cin >> x[i][j];
int cnt = 0;
rep(i, n) {
rrep(j, n) {
rep(k, d) {
int dis = 0;
dis += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]);
rep(l, 150) {
if (dis == l * l)
cnt++;
}
}
}
}
cout << cnt << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define ll long long
#define pp pair<int, int>
#define v(T) vector<T>
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
const int INF = 1001001001;
// int 2×10の9乗
// vector<vector<int>> a(n, vector<int>(m));
// rep(i,n) rep(j,m) cin >> a[i][j];
// cout << fixed << setprecision(10);
// int gcd(int x, int y){
// if(y==0) return x;
// return gcd(y, x%y);
//}
// sort小さい順(昇順)
int main() {
int n, d;
cin >> n >> d;
vector<vector<int>> x(n, vector<int>(d));
rep(i, n) rep(j, d) cin >> x[i][j];
int cnt = 0;
rep(i, n) {
for (int j = i + 1; j < n; j++) {
int dis = 0;
rep(k, d) { dis += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]); }
rep(l, 150) {
if (dis == l * l)
cnt++;
}
}
}
cout << cnt << endl;
} | replace | 27 | 35 | 27 | 33 | -11 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, s, n) for (int i = (s); i < (n); i++)
#ifdef LOCAL
#define INPUT_FILE \
ifstream in("atcoder-problems/abc133_b.txt"); \
cin.rdbuf(in.rdbuf());
#define print_vec(v) \
rep(l, v.size()) { cout << v[l] << " "; } \
cout << endl;
#else
#define INPUT_FILE
#define print_vec(v)
#endif
#define CIN_OPTIMIZE \
cin.tie(0); \
ios::sync_with_stdio(false);
typedef pair<int, int> P;
typedef long long ll;
typedef pair<ll, ll> pl;
const int INF = 100100100;
const ll LINF = 1e18 + 100;
const int MOD = 1e9 + 7;
int main() {
INPUT_FILE CIN_OPTIMIZE;
int N, D;
cin >> N >> D;
vector<vector<int>> x(N, vector<int>(N, 0));
rep(i, N) rep(j, D) { cin >> x[i][j]; }
ll ans = 0;
rep(i, N - 1) repi(j, i + 1, N) {
int tmp = 0;
rep(k, D) { tmp += pow(x[i][k] - x[j][k], 2); }
int dis = sqrt(tmp);
ans += pow(dis, 2) == tmp;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, s, n) for (int i = (s); i < (n); i++)
#ifdef LOCAL
#define INPUT_FILE \
ifstream in("atcoder-problems/abc133_b.txt"); \
cin.rdbuf(in.rdbuf());
#define print_vec(v) \
rep(l, v.size()) { cout << v[l] << " "; } \
cout << endl;
#else
#define INPUT_FILE
#define print_vec(v)
#endif
#define CIN_OPTIMIZE \
cin.tie(0); \
ios::sync_with_stdio(false);
typedef pair<int, int> P;
typedef long long ll;
typedef pair<ll, ll> pl;
const int INF = 100100100;
const ll LINF = 1e18 + 100;
const int MOD = 1e9 + 7;
int main() {
INPUT_FILE CIN_OPTIMIZE;
int N, D;
cin >> N >> D;
vector<vector<int>> x(N, vector<int>(D, 0));
rep(i, N) rep(j, D) { cin >> x[i][j]; }
ll ans = 0;
rep(i, N - 1) repi(j, i + 1, N) {
int tmp = 0;
rep(k, D) { tmp += pow(x[i][k] - x[j][k], 2); }
int dis = sqrt(tmp);
ans += pow(dis, 2) == tmp;
}
cout << ans << endl;
} | replace | 31 | 32 | 31 | 32 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define Abs(x) ((x) < 0 ? (x) * -1 : (x))
#define rep(x, y) for ((x) = 0; (x) < (y); (x)++)
#define repin(x, y) for ((x) = 0; (x) <= (y); (x)++)
#define nep(x, y) for ((x) = (y)-1; 0 <= (x); (x)--)
#define nepi(x, y, z) for ((x) = (y)-1; (z) <= (x); (x)--)
#define repi(x, y, z) for ((x) = (z); (x) < (y); (x)++)
#define repiin(x, y, z) for ((x) = (z); (x) <= (y); (x)++)
#define reps(x, y, z) for ((x) = 0; (x) < (y); (x) += (z))
#define repis(x, y, z, s) for ((x) = (z); (x) < (y); (x) += (s))
#define repiins(x, y, z, s) for ((x) = (z); (x) <= (y); (x) += (s))
#define repit(x) \
for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define repit2(x) \
for (__typeof((x).begin()) it2 = (x).begin(); it2 != (x).end(); it2++)
#define nepit(x) \
for (__typeof((x).rbegin()) it = (x).rbegin(); it != (x).rend(); it++)
#define All(x) (x).begin(), (x).end()
#define Mem0(x) memset(x, 0, sizeof(x))
#define Mem1(x) memset(x, -1, sizeof(x))
// can be applied to string type
#define Unique(v) v.resize(unique(All(v)) - v.begin())
#define peq(p0, p1) (p0.first == p1.first && p0.second == p1.second)
#define End '\n'
#define Out(x) cout << (x) << End
template <typename T> void OutN(T x) {
size_t i, len = x.size() - 1;
for (i = 0; i < len; i++)
cout << x[i] << " ";
cout << x[len] << '\n';
}
#define OutaN(x) \
do { \
size_t i, len = sizeof(x) / sizeof(__typeof(x[0])) - 1; \
for (i = 0; i < len; i++) \
cout << x[i] << " "; \
cout << x[len] << '\n'; \
} while (0);
template <typename T> void Outit(T x) {
auto end = x.end();
end--;
for (auto it = x.begin(); it != end; it++)
cout << *it << " ";
cout << *end << '\n';
}
template <typename T> void Debug(const T &val) { cerr << val << End; }
template <typename T, typename... Args> void Debug(const T &val, Args... args) {
cerr << val << ' ';
Debug(std::forward<Args>(args)...);
}
template <typename T> inline bool Max(T &x, const T &y) {
return x < y ? x = y, 1 : 0;
}
template <typename T> inline bool Min(T &x, const T &y) {
return x > y ? x = y, 1 : 0;
}
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
// can be applied to string type
#define Sort(v) sort(All(v))
#define SortR(v) sort(All(v), std::greater<__typeof(v[0])>())
// array sorters
#define Sart(a) sort(a, a + sizeof(a) / sizeof(__typeof(a[0])));
#define SartR(a) \
sort(a, a + sizeof(a) / sizeof(__typeof(a[0])), \
std::greater<__typeof(a[0])>())
#define pb push_back
#define mp make_pair
#define a first
#define b second
#define lb std::lower_bound
#define ub std::upper_bound
#define lbi(v, x) lb(All(v), (x)) - v.begin()
#define ubi(v, x) ub(All(v), (x)) - v.begin()
static const ll MOD = 1e9 + 7;
static const double PI = 3.141592653589793;
#define LOCAL 1
int main() {
#if LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("debug.txt", "w", stderr);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
// std::cout.precision(18);
int n, d;
cin >> n >> d;
int x[n][d];
int ans = 0;
double sum, rt;
int i, j, k;
rep(i, n) rep(j, d) cin >> x[i][j];
rep(i, n) {
repi(j, n, i + 1) {
sum = 0.0;
rep(k, d) sum += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]);
rt = floor(sqrt(sum));
sum = sqrt(sum);
if (rt == sum)
ans++;
}
}
Out(ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define Abs(x) ((x) < 0 ? (x) * -1 : (x))
#define rep(x, y) for ((x) = 0; (x) < (y); (x)++)
#define repin(x, y) for ((x) = 0; (x) <= (y); (x)++)
#define nep(x, y) for ((x) = (y)-1; 0 <= (x); (x)--)
#define nepi(x, y, z) for ((x) = (y)-1; (z) <= (x); (x)--)
#define repi(x, y, z) for ((x) = (z); (x) < (y); (x)++)
#define repiin(x, y, z) for ((x) = (z); (x) <= (y); (x)++)
#define reps(x, y, z) for ((x) = 0; (x) < (y); (x) += (z))
#define repis(x, y, z, s) for ((x) = (z); (x) < (y); (x) += (s))
#define repiins(x, y, z, s) for ((x) = (z); (x) <= (y); (x) += (s))
#define repit(x) \
for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define repit2(x) \
for (__typeof((x).begin()) it2 = (x).begin(); it2 != (x).end(); it2++)
#define nepit(x) \
for (__typeof((x).rbegin()) it = (x).rbegin(); it != (x).rend(); it++)
#define All(x) (x).begin(), (x).end()
#define Mem0(x) memset(x, 0, sizeof(x))
#define Mem1(x) memset(x, -1, sizeof(x))
// can be applied to string type
#define Unique(v) v.resize(unique(All(v)) - v.begin())
#define peq(p0, p1) (p0.first == p1.first && p0.second == p1.second)
#define End '\n'
#define Out(x) cout << (x) << End
template <typename T> void OutN(T x) {
size_t i, len = x.size() - 1;
for (i = 0; i < len; i++)
cout << x[i] << " ";
cout << x[len] << '\n';
}
#define OutaN(x) \
do { \
size_t i, len = sizeof(x) / sizeof(__typeof(x[0])) - 1; \
for (i = 0; i < len; i++) \
cout << x[i] << " "; \
cout << x[len] << '\n'; \
} while (0);
template <typename T> void Outit(T x) {
auto end = x.end();
end--;
for (auto it = x.begin(); it != end; it++)
cout << *it << " ";
cout << *end << '\n';
}
template <typename T> void Debug(const T &val) { cerr << val << End; }
template <typename T, typename... Args> void Debug(const T &val, Args... args) {
cerr << val << ' ';
Debug(std::forward<Args>(args)...);
}
template <typename T> inline bool Max(T &x, const T &y) {
return x < y ? x = y, 1 : 0;
}
template <typename T> inline bool Min(T &x, const T &y) {
return x > y ? x = y, 1 : 0;
}
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
// can be applied to string type
#define Sort(v) sort(All(v))
#define SortR(v) sort(All(v), std::greater<__typeof(v[0])>())
// array sorters
#define Sart(a) sort(a, a + sizeof(a) / sizeof(__typeof(a[0])));
#define SartR(a) \
sort(a, a + sizeof(a) / sizeof(__typeof(a[0])), \
std::greater<__typeof(a[0])>())
#define pb push_back
#define mp make_pair
#define a first
#define b second
#define lb std::lower_bound
#define ub std::upper_bound
#define lbi(v, x) lb(All(v), (x)) - v.begin()
#define ubi(v, x) ub(All(v), (x)) - v.begin()
static const ll MOD = 1e9 + 7;
static const double PI = 3.141592653589793;
#define LOCAL 0
int main() {
#if LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("debug.txt", "w", stderr);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
// std::cout.precision(18);
int n, d;
cin >> n >> d;
int x[n][d];
int ans = 0;
double sum, rt;
int i, j, k;
rep(i, n) rep(j, d) cin >> x[i][j];
rep(i, n) {
repi(j, n, i + 1) {
sum = 0.0;
rep(k, d) sum += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]);
rt = floor(sqrt(sum));
sum = sqrt(sum);
if (rt == sum)
ans++;
}
}
Out(ans);
return 0;
}
| replace | 98 | 99 | 98 | 99 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define CHAL_BC_APNE_BAAP_KO_MAT_SIKHA \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define fr first
#define se second
signed main() {
CHAL_BC_APNE_BAAP_KO_MAT_SIKHA;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, d;
cin >> n >> d;
vector<vector<int>> v;
for (int i = 1; i <= n; i++) {
vector<int> vec;
for (int j = 1; j <= d; j++) {
int tmp;
cin >> tmp;
vec.pb(tmp);
}
v.pb(vec);
}
int cnt = 0;
for (int i = 0; i < v.size(); i++) {
for (int j = i + 1; j < v.size(); j++) {
int sum = 0;
for (int k = 0; k < d; k++) {
sum += pow(abs(v[i][k] - v[j][k]), 2);
}
int u = sqrt(sum);
int y = ceil(1.0 * sqrt(sum));
if (u == y) {
cnt++;
}
}
}
cout << cnt << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define CHAL_BC_APNE_BAAP_KO_MAT_SIKHA \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define fr first
#define se second
signed main() {
CHAL_BC_APNE_BAAP_KO_MAT_SIKHA;
int n, d;
cin >> n >> d;
vector<vector<int>> v;
for (int i = 1; i <= n; i++) {
vector<int> vec;
for (int j = 1; j <= d; j++) {
int tmp;
cin >> tmp;
vec.pb(tmp);
}
v.pb(vec);
}
int cnt = 0;
for (int i = 0; i < v.size(); i++) {
for (int j = i + 1; j < v.size(); j++) {
int sum = 0;
for (int k = 0; k < d; k++) {
sum += pow(abs(v[i][k] - v[j][k]), 2);
}
int u = sqrt(sum);
int y = ceil(1.0 * sqrt(sum));
if (u == y) {
cnt++;
}
}
}
cout << cnt << endl;
}
| replace | 12 | 16 | 12 | 13 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define repk(i, k, n) for (ll i = k; i < (ll)(n); ++i)
#define INF 5000000000
#define MOD 1000000007
typedef long long ll;
using namespace std;
int ans;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, D;
cin >> N >> D;
vector<vector<int>> X(N, vector<int>(N));
rep(i, N) {
rep(j, D) { cin >> X[i][j]; }
}
rep(i, N) {
repk(j, i + 1, N) {
double d = 0;
rep(k, D) { d += pow(X[i][k] - X[j][k], 2); }
d = sqrt(d);
if (d == floor(d)) {
// cout << i << " " << j << endl;
ans++;
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define repk(i, k, n) for (ll i = k; i < (ll)(n); ++i)
#define INF 5000000000
#define MOD 1000000007
typedef long long ll;
using namespace std;
int ans;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, D;
cin >> N >> D;
vector<vector<int>> X(N, vector<int>(D));
rep(i, N) {
rep(j, D) { cin >> X[i][j]; }
}
rep(i, N) {
repk(j, i + 1, N) {
double d = 0;
rep(k, D) { d += pow(X[i][k] - X[j][k], 2); }
d = sqrt(d);
if (d == floor(d)) {
// cout << i << " " << j << endl;
ans++;
}
}
}
cout << ans << endl;
return 0;
}
| replace | 18 | 19 | 18 | 19 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
cin >> N >> D;
vector<vector<int>> X(N, vector<int>(D));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> X[i][j];
}
}
int ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
int norm = 0;
for (int k = 0; k < D; k++) {
int diff = abs(X[i][k] - X[j][k]);
norm += diff * diff;
}
bool flag = false;
for (int k = 0; k <= norm; k++) {
if (k * k == norm) {
flag = true;
}
}
if (flag)
ans++;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
cin >> N >> D;
vector<vector<int>> X(N, vector<int>(D));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> X[i][j];
}
}
int ans = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
int norm = 0;
for (int k = 0; k < D; k++) {
int diff = abs(X[i][k] - X[j][k]);
norm += diff * diff;
}
bool flag = false;
for (int k = 0; k <= norm; k++) {
if (k * k == norm) {
flag = true;
}
}
if (flag)
ans++;
}
}
cout << ans << endl;
return 0;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02982 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
int main() {
int N, D;
cin >> N >> D;
vector<vector<int>> vec(N, vector<int>(D, 0));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> vec[i][j];
}
}
int cnt = 0;
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
ll a = 0;
for (int k = 0; k < D; i++) {
a += (vec[i][k] - vec[j][k]) * (vec[i][k] - vec[j][k]);
}
for (int b = 0; b < 200; b++) {
if (a == b * b) {
cnt++;
}
}
}
}
cout << cnt << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
int main() {
int N, D;
cin >> N >> D;
vector<vector<int>> vec(N, vector<int>(D, 0));
for (int i = 0; i < N; i++) {
for (int j = 0; j < D; j++) {
cin >> vec[i][j];
}
}
int cnt = 0;
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
ll a = 0;
for (int k = 0; k < D; k++) {
a += (vec[i][k] - vec[j][k]) * (vec[i][k] - vec[j][k]);
}
for (int b = 0; b < 200; b++) {
if (a == b * b) {
cnt++;
}
}
}
}
cout << cnt << endl;
}
| replace | 19 | 20 | 19 | 20 | TLE | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
int main(void) {
int n, d;
cin >> n >> d;
int a;
bool p2[10000] = {};
rep(i, 100) p2[i * i] = true;
int x[n][d];
int res = 0;
rep(i, n) rep(j, d) cin >> x[i][j];
rep(i, n) for (int j = i + 1; j < n; j++) {
a = 0;
rep(k, d) { a += abs(x[i][k] - x[j][k]) * abs(x[i][k] - x[j][k]); }
DEBUG { cout << a << endl; }
if (p2[a])
res++;
}
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
int main(void) {
int n, d;
cin >> n >> d;
int a;
bool p2[100000] = {};
rep(i, 300) p2[i * i] = true;
int x[n][d];
int res = 0;
rep(i, n) rep(j, d) cin >> x[i][j];
rep(i, n) for (int j = i + 1; j < n; j++) {
a = 0;
rep(k, d) { a += abs(x[i][k] - x[j][k]) * abs(x[i][k] - x[j][k]); }
DEBUG { cout << a << endl; }
if (p2[a])
res++;
}
cout << res << endl;
return 0;
} | replace | 14 | 16 | 14 | 16 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define forn(i, n) for (ll i = 0; i < n; i++)
#define fore(i, a, b) for (ll i = a; i <= b; i++)
#define ford(i, n) for (ll i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define endl "\n"
#define all(a) a.begin(), a.end()
#define sync \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define PI 3.14159265
const ll maxn = 1e3 + 1, mod = 1e9 + 7;
int memo[maxn][maxn][2];
string a;
int n;
vector<int> alpha(26);
bool f(vector<int> a, vector<int> b) {
ld sum = 0;
forn(i, a.size()) {
ll temp = a[i] - b[i];
temp *= temp;
sum += temp;
}
// cout<<sum<<endl;
ld s = sqrt(sum);
// cout<<fixed<<setprecision(10)<<s<<endl
if (s - int(s) == 0)
return true;
return false;
}
int main() {
sync
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
int n, d;
cin >> n >> d;
vector<int> v[n];
forn(i, n) {
forn(j, d) {
int x;
cin >> x;
v[i].pb(x);
}
}
int ans = 0;
forn(i, n) {
fore(j, i + 1, n - 1) {
if (f(v[i], v[j]))
ans++;
}
}
cout << ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define forn(i, n) for (ll i = 0; i < n; i++)
#define fore(i, a, b) for (ll i = a; i <= b; i++)
#define ford(i, n) for (ll i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define endl "\n"
#define all(a) a.begin(), a.end()
#define sync \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define PI 3.14159265
const ll maxn = 1e3 + 1, mod = 1e9 + 7;
int memo[maxn][maxn][2];
string a;
int n;
vector<int> alpha(26);
bool f(vector<int> a, vector<int> b) {
ld sum = 0;
forn(i, a.size()) {
ll temp = a[i] - b[i];
temp *= temp;
sum += temp;
}
// cout<<sum<<endl;
ld s = sqrt(sum);
// cout<<fixed<<setprecision(10)<<s<<endl
if (s - int(s) == 0)
return true;
return false;
}
int main() {
sync
int n,
d;
cin >> n >> d;
vector<int> v[n];
forn(i, n) {
forn(j, d) {
int x;
cin >> x;
v[i].pb(x);
}
}
int ans = 0;
forn(i, n) {
fore(j, i + 1, n - 1) {
if (f(v[i], v[j]))
ans++;
}
}
cout << ans;
return 0;
} | replace | 44 | 51 | 44 | 47 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MOD 1000000007
#define MAX 10005
#define pb push_back
#define int long long
#define vi vector<int>
#define len(x) (x.size())
#define f(i, a, b) for (int i = a; i < b; i++)
#define debug(x) cerr << #x << ": " << x << endl
#define T \
int tc; \
cin >> tc; \
for (int t = 1; t <= tc; t++)
#define set_arr(arr, i) memset(arr, i, sizeof(arr))
using namespace std;
double distance(int points[][100], int x, int y, int d) {
double ans = 0;
for (int i = 0; i < d; i++) {
ans += pow((points[x][i] - points[y][i]), 2);
}
return sqrt(ans);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, d;
cin >> n >> d;
int points[100][100];
for (int i = 0; i < n; i++) {
for (int j = 0; j < d; j++) {
cin >> points[i][j];
}
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double dist = distance(points, i, j, d);
if (dist - (int)(dist) == 0)
count++;
}
}
cout << count;
return 0;
}
| #include <bits/stdc++.h>
#define MOD 1000000007
#define MAX 10005
#define pb push_back
#define int long long
#define vi vector<int>
#define len(x) (x.size())
#define f(i, a, b) for (int i = a; i < b; i++)
#define debug(x) cerr << #x << ": " << x << endl
#define T \
int tc; \
cin >> tc; \
for (int t = 1; t <= tc; t++)
#define set_arr(arr, i) memset(arr, i, sizeof(arr))
using namespace std;
double distance(int points[][100], int x, int y, int d) {
double ans = 0;
for (int i = 0; i < d; i++) {
ans += pow((points[x][i] - points[y][i]), 2);
}
return sqrt(ans);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, d;
cin >> n >> d;
int points[100][100];
for (int i = 0; i < n; i++) {
for (int j = 0; j < d; j++) {
cin >> points[i][j];
}
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double dist = distance(points, i, j, d);
if (dist - (int)(dist) == 0)
count++;
}
}
cout << count;
return 0;
}
| replace | 28 | 32 | 28 | 29 | 0 | |
p02982 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)
#define lf double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert
using namespace std;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, d;
cin >> n >> d;
vector<vll> x(n, vll(n));
rep(i, n) rep(j, d) { cin >> x[i][j]; }
ll ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ll sum = 0;
rep(k, d) { sum += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]); }
bool sq = true;
for (int k = 2; k * k <= sum; k++) {
ll cnt = 0;
while (sum % k == 0) {
sum /= k;
cnt++;
}
if (cnt % 2 != 0)
sq = false;
}
if (sum != 1)
sq = false;
if (sq)
ans++;
}
}
cout << ans << "\n";
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)
#define lf double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert
using namespace std;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, d;
cin >> n >> d;
vector<vll> x(n, vll(d));
rep(i, n) rep(j, d) { cin >> x[i][j]; }
ll ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ll sum = 0;
rep(k, d) { sum += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]); }
bool sq = true;
for (int k = 2; k * k <= sum; k++) {
ll cnt = 0;
while (sum % k == 0) {
sum /= k;
cnt++;
}
if (cnt % 2 != 0)
sq = false;
}
if (sum != 1)
sq = false;
if (sq)
ans++;
}
}
cout << ans << "\n";
return 0;
} | replace | 29 | 30 | 29 | 30 | 0 | |
p02982 | C++ | Runtime Error | // #define NDEBUG
#include <cstddef>
#include <cstdint>
#include <vector>
namespace n91 {
using i8 = std::int_fast8_t;
using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
using u8 = std::uint_fast8_t;
using u32 = std::uint_fast32_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
constexpr usize operator"" _z(unsigned long long x) noexcept {
return static_cast<usize>(x);
}
class rep {
const usize f, l;
public:
class itr {
friend rep;
usize i;
constexpr itr(const usize x) noexcept : i(x) {}
public:
void operator++() noexcept { ++i; }
constexpr usize operator*() const noexcept { return i; }
constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
};
constexpr rep(const usize first, const usize last) noexcept
: f(first), l(last) {}
constexpr itr begin() const noexcept { return itr(f); }
constexpr itr end() const noexcept { return itr(l); }
};
class revrep {
const usize f, l;
public:
class itr {
friend revrep;
usize i;
constexpr itr(usize x) noexcept : i(x) {}
public:
void operator++() noexcept { --i; }
constexpr usize operator*() const noexcept { return i; }
constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
};
constexpr revrep(usize first, usize last) noexcept : f(--first), l(--last) {}
constexpr itr begin() const noexcept { return itr(l); }
constexpr itr end() const noexcept { return itr(f); }
};
template <class T> using vec_alias = std::vector<T>;
template <class T> auto md_vec(const usize n, const T &value) {
return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) {
return a < b ? b - a : a - b;
}
} // namespace n91
#include <algorithm>
#include <bitset>
#include <iostream>
#include <numeric>
#include <utility>
namespace n91 {
void main_() {
std::bitset<1601_z> is_sq;
for (const auto i : rep(0_z, 41_z)) {
is_sq.set(i * i);
}
usize n, d;
std::cin >> n >> d;
auto x = md_vec(n, d, i32());
for (auto &v : x) {
for (auto &e : v) {
std::cin >> e;
}
}
usize cnt = 0_z;
for (const auto i : rep(0_z, n)) {
for (const auto j : rep(0_z, i)) {
i32 sum = static_cast<i32>(0);
for (const auto k : rep(0_z, d)) {
sum += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]);
}
if (is_sq.test(static_cast<usize>(sum))) {
++cnt;
}
}
}
std::cout << cnt << std::endl;
}
} // namespace n91
int main() {
n91::main_();
return 0;
}
| // #define NDEBUG
#include <cstddef>
#include <cstdint>
#include <vector>
namespace n91 {
using i8 = std::int_fast8_t;
using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
using u8 = std::uint_fast8_t;
using u32 = std::uint_fast32_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
constexpr usize operator"" _z(unsigned long long x) noexcept {
return static_cast<usize>(x);
}
class rep {
const usize f, l;
public:
class itr {
friend rep;
usize i;
constexpr itr(const usize x) noexcept : i(x) {}
public:
void operator++() noexcept { ++i; }
constexpr usize operator*() const noexcept { return i; }
constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
};
constexpr rep(const usize first, const usize last) noexcept
: f(first), l(last) {}
constexpr itr begin() const noexcept { return itr(f); }
constexpr itr end() const noexcept { return itr(l); }
};
class revrep {
const usize f, l;
public:
class itr {
friend revrep;
usize i;
constexpr itr(usize x) noexcept : i(x) {}
public:
void operator++() noexcept { --i; }
constexpr usize operator*() const noexcept { return i; }
constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
};
constexpr revrep(usize first, usize last) noexcept : f(--first), l(--last) {}
constexpr itr begin() const noexcept { return itr(l); }
constexpr itr end() const noexcept { return itr(f); }
};
template <class T> using vec_alias = std::vector<T>;
template <class T> auto md_vec(const usize n, const T &value) {
return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) {
return a < b ? b - a : a - b;
}
} // namespace n91
#include <algorithm>
#include <bitset>
#include <iostream>
#include <numeric>
#include <utility>
namespace n91 {
void main_() {
std::bitset<16001_z> is_sq;
for (const auto i : rep(0_z, 127_z)) {
is_sq.set(i * i);
}
usize n, d;
std::cin >> n >> d;
auto x = md_vec(n, d, i32());
for (auto &v : x) {
for (auto &e : v) {
std::cin >> e;
}
}
usize cnt = 0_z;
for (const auto i : rep(0_z, n)) {
for (const auto j : rep(0_z, i)) {
i32 sum = static_cast<i32>(0);
for (const auto k : rep(0_z, d)) {
sum += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]);
}
if (is_sq.test(static_cast<usize>(sum))) {
++cnt;
}
}
}
std::cout << cnt << std::endl;
}
} // namespace n91
int main() {
n91::main_();
return 0;
}
| replace | 79 | 81 | 79 | 81 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.