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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02964 | C++ | Runtime Error | // #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <time.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define mpa make_pair
#define all(a) a.begin(), a.end()
template <typename T> void vdeb(vector<T> &da) {
int size = da.size();
for (int i = 0; i < size; i++) {
if (i == size - 1) {
cout << da[i] << endl;
} else {
cout << da[i] << ' ';
}
}
}
template <typename T> void vvdeb(vector<T> &da) {
int size = da.size();
for (int i = 0; i < size; i++) {
vdeb(da[i]);
}
}
void gene(vector<int> &da, vector<int> &ans, int be) {
vector<bool> ju(201000, false);
rip(i, da.size(), be) {
if (ju[da[i]]) {
while (ans.back() != da[i]) {
ju[ans.back()] = false;
ans.pop_back();
}
ans.pop_back();
ju[da[i]] = false;
} else {
ans.push_back(da[i]);
ju[da[i]] = true;
}
}
return;
}
const int M = 11;
int main() {
ll n, k;
cin >> n >> k;
vector<int> da(n);
rip(i, n, 0) cin >> da[i];
vector<int> to(M), ot(M);
rip(i, M, 0) {
to[i] = i;
ot[i] = i;
}
// vdeb(da);
rip(i, n, 0) {
to[ot[da[i]]] = 0;
to[ot[0]] = da[i];
swap(ot[da[i]], ot[0]);
}
int co = 1, now = to[0];
while (now) {
co++;
now = to[now];
}
co = k % co;
now = 0;
rip(i, co, 0) { now = to[now]; }
int be = n - 1;
while (be > -1) {
if (da[be] == now) {
break;
}
be--;
}
// vdeb(to);
if (be == -1) {
cout << endl;
return 0;
}
vector<int> ans(0);
gene(da, ans, be);
vdeb(ans);
// vdeb(ot);
} | // #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <time.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define mpa make_pair
#define all(a) a.begin(), a.end()
template <typename T> void vdeb(vector<T> &da) {
int size = da.size();
for (int i = 0; i < size; i++) {
if (i == size - 1) {
cout << da[i] << endl;
} else {
cout << da[i] << ' ';
}
}
}
template <typename T> void vvdeb(vector<T> &da) {
int size = da.size();
for (int i = 0; i < size; i++) {
vdeb(da[i]);
}
}
void gene(vector<int> &da, vector<int> &ans, int be) {
vector<bool> ju(201000, false);
rip(i, da.size(), be) {
if (ju[da[i]]) {
while (ans.back() != da[i]) {
ju[ans.back()] = false;
ans.pop_back();
}
ans.pop_back();
ju[da[i]] = false;
} else {
ans.push_back(da[i]);
ju[da[i]] = true;
}
}
return;
}
const int M = 201000;
int main() {
ll n, k;
cin >> n >> k;
vector<int> da(n);
rip(i, n, 0) cin >> da[i];
vector<int> to(M), ot(M);
rip(i, M, 0) {
to[i] = i;
ot[i] = i;
}
// vdeb(da);
rip(i, n, 0) {
to[ot[da[i]]] = 0;
to[ot[0]] = da[i];
swap(ot[da[i]], ot[0]);
}
int co = 1, now = to[0];
while (now) {
co++;
now = to[now];
}
co = k % co;
now = 0;
rip(i, co, 0) { now = to[now]; }
int be = n - 1;
while (be > -1) {
if (da[be] == now) {
break;
}
be--;
}
// vdeb(to);
if (be == -1) {
cout << endl;
return 0;
}
vector<int> ans(0);
gene(da, ans, be);
vdeb(ans);
// vdeb(ot);
} | replace | 57 | 58 | 57 | 58 | 0 | |
p02964 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
// #define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
// #define int long long
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> std::string printVector(const std::vector<T> &data) {
std::stringstream ss;
std::ostream_iterator<T> out_it(ss, ", ");
ss << "[";
std::copy(data.begin(), data.end() - 1, out_it);
ss << data.back() << "]";
return ss.str();
}
const int MOD = 1e9 + 7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
LL nex[60][212345] = {};
signed main(int argc, char const *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
LL K;
cin >> K;
VL A(N);
map<int, VI> idx;
REP(i, N) {
cin >> A[i];
idx[A[i]].eb(i);
}
for (auto kv : idx) {
int s = kv.second.size();
kv.second.eb(kv.second[0] + N);
REP(i, s) { nex[0][kv.second[i]] = kv.second[i + 1] - kv.second[i] + 1; }
}
for (int k = 1; k < 60; k++) {
for (int i = 0; i < N; i++) {
nex[k][i] = nex[k - 1][i] + nex[k - 1][(i + nex[k - 1][i]) % N];
}
}
// REP(k,3){
// dump(k)
// REP(i,N){
// cout << nex[k][i] <<" ";
// }
// cout << endl;
// }
LL dest = N * K;
LL cur = 0;
for (int i = 59; i >= 0; i--) {
if (cur + nex[i][cur % N] <= dest) {
cur += nex[i][cur % N];
}
}
if (cur == dest) {
cout << endl;
return 0;
} else {
VI ans;
set<int> has;
for (int i = cur % N; i < N; i++) {
if (has.count(A[i])) {
while (1) {
int v = ans.back();
ans.pop_back();
has.erase(v);
if (v == A[i])
break;
}
} else {
ans.eb(A[i]);
has.insert(A[i]);
}
}
for (int x : ans) {
cout << x << " ";
}
cout << endl;
}
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
// #define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
// #define int long long
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> std::string printVector(const std::vector<T> &data) {
std::stringstream ss;
std::ostream_iterator<T> out_it(ss, ", ");
ss << "[";
std::copy(data.begin(), data.end() - 1, out_it);
ss << data.back() << "]";
return ss.str();
}
const int MOD = 1e9 + 7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
LL nex[60][212345] = {};
signed main(int argc, char const *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
LL K;
cin >> K;
VL A(N);
map<int, VI> idx;
REP(i, N) {
cin >> A[i];
idx[A[i]].eb(i);
}
for (auto kv : idx) {
int s = kv.second.size();
kv.second.eb(kv.second[0] + N);
REP(i, s) { nex[0][kv.second[i]] = kv.second[i + 1] - kv.second[i] + 1; }
}
for (int k = 1; k < 60; k++) {
for (int i = 0; i < N; i++) {
nex[k][i] =
min(LINF, nex[k - 1][i] + nex[k - 1][(i + nex[k - 1][i]) % N]);
// dump(nex[k][i])
}
}
// REP(k,3){
// dump(k)
// REP(i,N){
// cout << nex[k][i] <<" ";
// }
// cout << endl;
// }
LL dest = N * K;
LL cur = 0;
for (int i = 59; i >= 0; i--) {
if (cur + nex[i][cur % N] <= dest) {
cur += nex[i][cur % N];
}
}
if (cur == dest) {
cout << endl;
return 0;
} else {
VI ans;
set<int> has;
for (int i = cur % N; i < N; i++) {
if (has.count(A[i])) {
while (1) {
int v = ans.back();
ans.pop_back();
has.erase(v);
if (v == A[i])
break;
}
} else {
ans.eb(A[i]);
has.insert(A[i]);
}
}
for (int x : ans) {
cout << x << " ";
}
cout << endl;
}
}
| replace | 80 | 81 | 80 | 83 | 127 | /tmp/cd53750b-a4aa-4349-aae6-b682e12ee07b.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
|
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
int N;
int64_t K;
std::vector<int> A;
void calcAns(int, int64_t);
int main() {
scanf("%d%lld", &N, &K);
K *= N;
A.resize(N);
for (int &e : A) {
scanf("%d", &e);
e--;
}
std::vector<int> to(N), dp_table(200'000);
for (int i{N - 1}; i >= 0; i--)
dp_table[A[i]] = N + i;
for (int i{N - 1}; i >= 0; i--) {
to[i] = dp_table[A[i]] + 1;
dp_table[A[i]] = i;
}
std::vector<int> order(N, -1);
int begin, loop_size, now_i{};
for (int i{}; i < N; i++) {
order[now_i] = i;
int next{to[now_i] % N};
if (order[next] >= 0) {
begin = next;
loop_size = i + 1 - order[next];
break;
}
now_i = next;
}
// ループに入る前の挙動
now_i = 0;
int64_t now_dist{};
while (now_i != begin) {
int64_t delta{to[now_i] - now_i};
if (now_dist + delta > K) {
calcAns(now_i, K - now_dist);
return 0;
}
now_dist += delta;
now_i = to[now_i] % N;
}
// 今now_dist <= K
// ループの長さ
int64_t loop_length{to[begin] - begin};
now_i = to[begin] % N;
while (now_i != begin) {
int64_t delta{to[now_i] - now_i};
loop_length += delta;
now_i = (now_i + delta) % N;
}
// K == (beginからの距離)
K = (K - now_dist) % loop_length;
now_i = begin;
now_dist = 0;
while (true) {
int64_t delta{to[now_i] - now_i};
if (now_dist + delta > K) {
calcAns(now_i, K - now_dist);
break;
}
now_dist += delta;
now_i = to[now_i] % N;
}
return 0;
}
void calcAns(int begin, int64_t dist) {
std::vector<int> count(200'000), st;
for (int64_t i{}; i < dist; i++) {
int num{A[(begin + i) % N]};
if (count[num] == 0) {
st.push_back(num);
count[num]++;
} else
while (count[num] > 0) {
if (st.back() == num)
count[num]--;
st.pop_back();
}
}
for (int i{}; i < (int)st.size(); i++) {
printf("%d", st[i] + 1);
if (i + 1 != (int)st.size())
putchar(' ');
}
putchar('\n');
return;
} | #include <bits/stdc++.h>
int N;
int64_t K;
std::vector<int> A;
void calcAns(int, int64_t);
int main() {
scanf("%d%lld", &N, &K);
K *= N;
A.resize(N);
for (int &e : A) {
scanf("%d", &e);
e--;
}
std::vector<int> to(N), dp_table(200'000);
for (int i{N - 1}; i >= 0; i--)
dp_table[A[i]] = N + i;
for (int i{N - 1}; i >= 0; i--) {
to[i] = dp_table[A[i]] + 1;
dp_table[A[i]] = i;
}
std::vector<int> order(N, -1);
int begin, loop_size, now_i{};
for (int i{}; i < N; i++) {
order[now_i] = i;
int next{to[now_i] % N};
if (order[next] >= 0) {
begin = next;
loop_size = i + 1 - order[next];
break;
}
now_i = next;
}
// ループに入る前の挙動
now_i = 0;
int64_t now_dist{};
while (now_i != begin) {
int64_t delta{to[now_i] - now_i};
if (now_dist + delta > K) {
calcAns(now_i, K - now_dist);
return 0;
}
now_dist += delta;
now_i = to[now_i] % N;
}
// 今now_dist <= K
// ループの長さ
int64_t loop_length{to[begin] - begin};
now_i = to[begin] % N;
while (now_i != begin) {
int64_t delta{to[now_i] - now_i};
loop_length += delta;
now_i = (now_i + delta) % N;
}
// K == (beginからの距離)
K = (K - now_dist) % loop_length;
now_i = begin;
now_dist = 0;
while (true) {
int64_t delta{to[now_i] - now_i};
if (now_dist + delta > K) {
calcAns(now_i, K - now_dist);
break;
}
now_dist += delta;
now_i = to[now_i] % N;
}
return 0;
}
void calcAns(int begin, int64_t dist) {
std::vector<int> count(200'000), st;
for (int64_t i{}; i < dist; i++) {
int num{A[(begin + i) % N]};
if (count[num] == 0) {
st.push_back(num);
count[num]++;
} else
while (count[num] > 0) {
count[st.back()]--;
st.pop_back();
}
}
for (int i{}; i < (int)st.size(); i++) {
printf("%d", st[i] + 1);
if (i + 1 != (int)st.size())
putchar(' ');
}
putchar('\n');
return;
} | replace | 82 | 84 | 82 | 83 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = (int)2 * 1e5 + 5;
int n;
long long len;
int a[N];
long long jump[N][70], gap[N][70];
int rem[N];
int main() {
freopen("input.txt", "r", stdin);
iostream::sync_with_stdio(0);
cin >> n >> len;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = n; i >= 1; i--)
jump[i][0] = rem[a[i]], rem[a[i]] = i;
for (int i = n; i >= 1; i--)
if (jump[i][0] == 0)
jump[i][0] = rem[a[i]], gap[i][0] = 1;
#define nxt(x) ((x) == n ? 1 : (x) + 1)
for (int k = 1; k < 70; k++)
for (int i = 1; i <= n; i++)
jump[i][k] = jump[nxt(jump[i][k - 1])][k - 1],
gap[i][k] = gap[i][k - 1] + gap[nxt(jump[i][k - 1])][k - 1] +
(jump[i][k - 1] == n),
gap[i][k] = min(gap[i][k], (long long)1e18);
long long tmp = len;
int cur = 1;
while (len > 1) {
for (int _ = 69; _ >= 0; _--) {
if (len > gap[cur][_] + (jump[cur][_] == n)) {
len -= gap[cur][_] + (jump[cur][_] == n);
cur = nxt(jump[cur][_]);
break;
}
}
// cout << cur << ' ' << len << endl;
}
// for(int i=1;i<=n;i++) cout << jump[i][0] << ' ' << gap[i][0] << endl;
// cout << jump[1][2] << endl;
assert(len == 1);
vector<int> st;
map<int, int> mp;
for (int i = cur; i <= n; i++) {
if (mp[a[i]] == 0) {
st.push_back(a[i]);
mp[a[i]] = 1;
} else {
while (st.back() != a[i])
mp[st.back()] = 0, st.pop_back();
st.pop_back();
mp[a[i]] = 0;
}
}
for (auto v : st)
cout << v << ' ';
// cout << "asd" << endl;
// st.clear();
// mp.clear();
// for(int i=1;i<=tmp;i++){
// for(int j=1;j<=n;j++){
// if (mp[a[j]] == 0){
// st.push_back(a[j]);
// mp[a[j]] = 1;
// }
// else{
// while(st.back() != a[j]) mp[st.back()] = 0, st.pop_back();
// st.pop_back();
// mp[a[j]] = 0;
// }
// if (st.size() == 0) cout << j << ' ' << tmp+1-i << endl;
// //for(auto v: st) cout << v << ' '; cout << endl;
// }
// }
}
| #include <bits/stdc++.h>
using namespace std;
const int N = (int)2 * 1e5 + 5;
int n;
long long len;
int a[N];
long long jump[N][70], gap[N][70];
int rem[N];
int main() {
iostream::sync_with_stdio(0);
cin >> n >> len;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = n; i >= 1; i--)
jump[i][0] = rem[a[i]], rem[a[i]] = i;
for (int i = n; i >= 1; i--)
if (jump[i][0] == 0)
jump[i][0] = rem[a[i]], gap[i][0] = 1;
#define nxt(x) ((x) == n ? 1 : (x) + 1)
for (int k = 1; k < 70; k++)
for (int i = 1; i <= n; i++)
jump[i][k] = jump[nxt(jump[i][k - 1])][k - 1],
gap[i][k] = gap[i][k - 1] + gap[nxt(jump[i][k - 1])][k - 1] +
(jump[i][k - 1] == n),
gap[i][k] = min(gap[i][k], (long long)1e18);
long long tmp = len;
int cur = 1;
while (len > 1) {
for (int _ = 69; _ >= 0; _--) {
if (len > gap[cur][_] + (jump[cur][_] == n)) {
len -= gap[cur][_] + (jump[cur][_] == n);
cur = nxt(jump[cur][_]);
break;
}
}
// cout << cur << ' ' << len << endl;
}
// for(int i=1;i<=n;i++) cout << jump[i][0] << ' ' << gap[i][0] << endl;
// cout << jump[1][2] << endl;
assert(len == 1);
vector<int> st;
map<int, int> mp;
for (int i = cur; i <= n; i++) {
if (mp[a[i]] == 0) {
st.push_back(a[i]);
mp[a[i]] = 1;
} else {
while (st.back() != a[i])
mp[st.back()] = 0, st.pop_back();
st.pop_back();
mp[a[i]] = 0;
}
}
for (auto v : st)
cout << v << ' ';
// cout << "asd" << endl;
// st.clear();
// mp.clear();
// for(int i=1;i<=tmp;i++){
// for(int j=1;j<=n;j++){
// if (mp[a[j]] == 0){
// st.push_back(a[j]);
// mp[a[j]] = 1;
// }
// else{
// while(st.back() != a[j]) mp[st.back()] = 0, st.pop_back();
// st.pop_back();
// mp[a[j]] = 0;
// }
// if (st.size() == 0) cout << j << ' ' << tmp+1-i << endl;
// //for(auto v: st) cout << v << ' '; cout << endl;
// }
// }
}
| delete | 11 | 12 | 11 | 11 | -11 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
template <class T> inline void YES(T condition) {
if (condition)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
template <class T> inline void Yes(T condition) {
if (condition)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
template <class T = string, class U = char>
int character_count(T text, U character) {
int ans = 0;
for (U i : text) {
ans += (i == character);
}
return ans;
}
lint power(lint base, lint exponent, lint module) {
if (exponent % 2) {
return power(base, exponent - 1, module) * base % module;
} else if (exponent) {
lint root_ans = power(base, exponent / 2, module);
return root_ans * root_ans % module;
} else {
return 1;
}
}
struct position {
int y, x;
};
position mv[4] = {
{0, -1},
{1, 0},
{0, 1},
{-1, 0}}; // double euclidean(position first, position second){ return
// sqrt((second.x - first.x) * (second.x - first.x) + (second.y -
// first.y) * (second.y - first.y)); }
template <class T, class U> string to_string(pair<T, U> x) {
return to_string(x.first) + "," + to_string(x.second);
}
string to_string(string x) { return x; }
template <class itr> void array_output(itr start, itr goal) {
string ans;
for (auto i = start; i != goal; i++)
ans += to_string(*i) + " ";
if (!ans.empty())
ans.pop_back();
cout << ans << endl;
}
template <class itr> void cins(itr first, itr last) {
for (auto i = first; i != last; i++) {
cin >> (*i);
}
}
template <class T> T gcd(T a, T b) {
if (a && b) {
return gcd(min(a, b), max(a, b) % min(a, b));
} else {
return a;
}
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
struct combination {
vector<lint> fact, inv;
combination(int sz) : fact(sz + 1), inv(sz + 1) {
fact[0] = 1;
for (int i = 1; i <= sz; i++) {
fact[i] = fact[i - 1] * i % mod;
}
inv[sz] = power(fact[sz], mod - 2, mod);
for (int i = sz - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
lint C(int p, int q) const {
if (q < 0 || p < q)
return 0;
return (fact[p] * inv[q] % mod * inv[p - q] % mod);
}
};
template <class itr> bool next_sequence(itr first, itr last, int max_bound) {
itr now = last;
while (now != first) {
now--;
(*now)++;
if ((*now) == max_bound) {
(*now) = 0;
} else {
return true;
}
}
return false;
}
int main() {
int N;
lint K;
cin >> N >> K;
int A[N];
cins(A, A + N);
vector<int> nums(2e5 + 100, -1);
int ptr[N];
for (int i = 0; i < N * 2; i++) {
int a = A[i % N];
if (nums[a] != -1 && nums[a] < N) {
ptr[nums[a]] = i + 1;
}
nums[a] = i;
}
int loop = 0, now = 0;
while (true) {
loop += ptr[now] / N;
now = ptr[now] % N;
if (now == 0) {
break;
}
}
K %= loop;
if (K == 0) {
cout << endl;
return 0;
}
now = 0;
int loop_cnt = 0;
while (true) {
loop_cnt += ptr[now] / N;
if (loop_cnt >= K) {
break;
}
now = ptr[now] % N;
}
vector<int> nums_cnt(2e5 + 100, 0), que;
for (int i = now; i < N; i++) {
if (nums_cnt[A[i]]) {
while (que.back() != A[i]) {
que.pop_back();
}
que.pop_back();
} else {
que.push_back(A[i]);
}
nums_cnt[A[i]] ^= 1;
}
array_output(all(que));
}
| #include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
template <class T> inline void YES(T condition) {
if (condition)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
template <class T> inline void Yes(T condition) {
if (condition)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
template <class T = string, class U = char>
int character_count(T text, U character) {
int ans = 0;
for (U i : text) {
ans += (i == character);
}
return ans;
}
lint power(lint base, lint exponent, lint module) {
if (exponent % 2) {
return power(base, exponent - 1, module) * base % module;
} else if (exponent) {
lint root_ans = power(base, exponent / 2, module);
return root_ans * root_ans % module;
} else {
return 1;
}
}
struct position {
int y, x;
};
position mv[4] = {
{0, -1},
{1, 0},
{0, 1},
{-1, 0}}; // double euclidean(position first, position second){ return
// sqrt((second.x - first.x) * (second.x - first.x) + (second.y -
// first.y) * (second.y - first.y)); }
template <class T, class U> string to_string(pair<T, U> x) {
return to_string(x.first) + "," + to_string(x.second);
}
string to_string(string x) { return x; }
template <class itr> void array_output(itr start, itr goal) {
string ans;
for (auto i = start; i != goal; i++)
ans += to_string(*i) + " ";
if (!ans.empty())
ans.pop_back();
cout << ans << endl;
}
template <class itr> void cins(itr first, itr last) {
for (auto i = first; i != last; i++) {
cin >> (*i);
}
}
template <class T> T gcd(T a, T b) {
if (a && b) {
return gcd(min(a, b), max(a, b) % min(a, b));
} else {
return a;
}
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
struct combination {
vector<lint> fact, inv;
combination(int sz) : fact(sz + 1), inv(sz + 1) {
fact[0] = 1;
for (int i = 1; i <= sz; i++) {
fact[i] = fact[i - 1] * i % mod;
}
inv[sz] = power(fact[sz], mod - 2, mod);
for (int i = sz - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
lint C(int p, int q) const {
if (q < 0 || p < q)
return 0;
return (fact[p] * inv[q] % mod * inv[p - q] % mod);
}
};
template <class itr> bool next_sequence(itr first, itr last, int max_bound) {
itr now = last;
while (now != first) {
now--;
(*now)++;
if ((*now) == max_bound) {
(*now) = 0;
} else {
return true;
}
}
return false;
}
int main() {
int N;
lint K;
cin >> N >> K;
int A[N];
cins(A, A + N);
vector<int> nums(2e5 + 100, -1);
int ptr[N];
for (int i = 0; i < N * 2; i++) {
int a = A[i % N];
if (nums[a] != -1 && nums[a] < N) {
ptr[nums[a]] = i + 1;
}
nums[a] = i;
}
int loop = 0, now = 0;
while (true) {
loop += ptr[now] / N;
now = ptr[now] % N;
if (now == 0) {
break;
}
}
K %= loop;
if (K == 0) {
cout << endl;
return 0;
}
now = 0;
int loop_cnt = 0;
while (true) {
loop_cnt += ptr[now] / N;
if (loop_cnt >= K) {
break;
}
now = ptr[now] % N;
}
vector<int> nums_cnt(2e5 + 100, 0), que;
for (int i = now; i < N; i++) {
if (nums_cnt[A[i]]) {
while (que.back() != A[i]) {
nums_cnt[que.back()] ^= 1;
que.pop_back();
}
que.pop_back();
} else {
que.push_back(A[i]);
}
nums_cnt[A[i]] ^= 1;
}
array_output(all(que));
}
| insert | 146 | 146 | 146 | 147 | 0 | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n) \
cerr << __LINE__ << ": " << #x << " = {"; \
for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \
cerr << ((hoge) ? "," : "") << x[hoge]; \
cerr << "}" << '\n'
#define debugMatrix(x, h, w) \
cerr << __LINE__ << ": " << #x << " =\n"; \
for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) { \
cerr << ((hoge ? " {" : "{{")); \
for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga)) \
cerr << ((fuga ? ", " : "")) << x[hoge][fuga]; \
cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n'; \
}
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
long long N, K;
cin >> N >> K;
long long Y = N * K;
int A[N];
for (int i = 0; i < N; i++)
cin >> A[i];
int next[N], cost[N];
const int M = 212345;
int pos[M];
fill(pos, pos + M, -1);
for (int i = N * 2 - 1; i >= 0; i--) {
if (i < N) {
next[i] = (pos[A[i]] + 1) % N;
cost[i] = pos[A[i]] - i + 1;
}
pos[A[i % N]] = i;
}
debugArray(next, N);
debugArray(cost, N);
long long dist[N];
fill(dist, dist + N, -1);
long long sum = 0;
int cur = 0, prev = 0;
while (true) {
if (dist[cur] != -1) {
break;
}
dist[cur] = sum;
if (dist[cur] > Y) {
break;
}
sum += cost[cur];
prev = cur;
cur = next[cur];
}
if (Y < dist[cur]) {
cur = prev;
Y -= dist[prev];
} else {
Y -= dist[cur];
Y %= sum - dist[cur];
}
vector<int> s;
bool exist[M];
fill(exist, exist + M, false);
while (Y--) {
if (exist[A[cur]]) {
while (s.back() != A[cur])
exist[s.back()] = false, s.pop_back();
s.pop_back();
exist[A[cur]] = false;
} else {
s.push_back(A[cur]);
exist[A[cur]] = true;
}
cur = (cur + 1) % N;
}
for (size_t i = 0; i < s.size(); i++)
cout << s[i] << (i == s.size() - 1 ? '\n' : ' ');
return 0;
}
| #include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n) \
cerr << __LINE__ << ": " << #x << " = {"; \
for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \
cerr << ((hoge) ? "," : "") << x[hoge]; \
cerr << "}" << '\n'
#define debugMatrix(x, h, w) \
cerr << __LINE__ << ": " << #x << " =\n"; \
for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) { \
cerr << ((hoge ? " {" : "{{")); \
for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga)) \
cerr << ((fuga ? ", " : "")) << x[hoge][fuga]; \
cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n'; \
}
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
long long N, K;
cin >> N >> K;
long long Y = N * K;
int A[N];
for (int i = 0; i < N; i++)
cin >> A[i];
int next[N], cost[N];
const int M = 212345;
int pos[M];
fill(pos, pos + M, -1);
for (int i = N * 2 - 1; i >= 0; i--) {
if (i < N) {
next[i] = (pos[A[i]] + 1) % N;
cost[i] = pos[A[i]] - i + 1;
}
pos[A[i % N]] = i;
}
debugArray(next, N);
debugArray(cost, N);
long long dist[N];
fill(dist, dist + N, -1);
long long sum = 0;
int cur = 0, prev = 0;
while (true) {
if (dist[cur] != -1) {
break;
}
dist[cur] = sum;
if (dist[cur] > Y) {
break;
}
sum += cost[cur];
prev = cur;
cur = next[cur];
}
if (Y < dist[cur]) {
cur = prev;
Y -= dist[prev];
} else {
Y -= dist[cur];
Y %= sum - dist[cur];
}
fill(dist, dist + N, -1);
sum = 0;
while (true) {
if (dist[cur] != -1) {
break;
}
dist[cur] = sum;
if (dist[cur] > Y) {
break;
}
sum += cost[cur];
prev = cur;
cur = next[cur];
}
if (Y < dist[cur]) {
cur = prev;
Y -= dist[prev];
}
vector<int> s;
bool exist[M];
fill(exist, exist + M, false);
while (Y--) {
if (exist[A[cur]]) {
while (s.back() != A[cur])
exist[s.back()] = false, s.pop_back();
s.pop_back();
exist[A[cur]] = false;
} else {
s.push_back(A[cur]);
exist[A[cur]] = true;
}
cur = (cur + 1) % N;
}
for (size_t i = 0; i < s.size(); i++)
cout << s[i] << (i == s.size() - 1 ? '\n' : ' ');
return 0;
}
| insert | 75 | 75 | 75 | 93 | TLE | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MIN_INT -2147483648
#define MAX_INT 2147483647
#define MIN_LONG -9223372036854775808L
#define MAX_LONG 9223372036854775807L
#define long long long int
using namespace std;
// @author: pashka
int main() {
ios::sync_with_stdio(false);
int n;
long k;
cin >> n >> k;
vector<int> a(n * 2);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i + n] = a[i];
}
int MAX = 100000;
vector<int> last(MAX + 1);
vector<int> next(n * 2);
for (int i = a.size() - 1; i >= 0; i--) {
next[i] = last[a[i]];
last[a[i]] = i;
}
int x = 0;
vector<long> pk(n, -1);
while (true) {
if (k <= 2) {
vector<int> s;
vector<bool> z(MAX + 1);
for (int i = x; i < k * n; i++) {
if (z[a[i % n]]) {
while (z[a[i % n]]) {
int t = s.back();
s.pop_back();
z[t] = false;
}
} else {
z[a[i % n]] = true;
s.push_back(a[i % n]);
}
}
for (int x : s) {
cout << x << " ";
}
break;
}
x = next[x] + 1;
while (x >= n) {
k--;
x -= n;
}
if (pk[x] != -1) {
long d = pk[x] - k;
k %= d;
}
// cout << x << " " << k << "\n";
pk[x] = k;
}
return 0;
} | #include <bits/stdc++.h>
#define MIN_INT -2147483648
#define MAX_INT 2147483647
#define MIN_LONG -9223372036854775808L
#define MAX_LONG 9223372036854775807L
#define long long long int
using namespace std;
// @author: pashka
int main() {
ios::sync_with_stdio(false);
int n;
long k;
cin >> n >> k;
vector<int> a(n * 2);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i + n] = a[i];
}
int MAX = 1000000;
vector<int> last(MAX + 1);
vector<int> next(n * 2);
for (int i = a.size() - 1; i >= 0; i--) {
next[i] = last[a[i]];
last[a[i]] = i;
}
int x = 0;
vector<long> pk(n, -1);
while (true) {
if (k <= 2) {
vector<int> s;
vector<bool> z(MAX + 1);
for (int i = x; i < k * n; i++) {
if (z[a[i % n]]) {
while (z[a[i % n]]) {
int t = s.back();
s.pop_back();
z[t] = false;
}
} else {
z[a[i % n]] = true;
s.push_back(a[i % n]);
}
}
for (int x : s) {
cout << x << " ";
}
break;
}
x = next[x] + 1;
while (x >= n) {
k--;
x -= n;
}
if (pk[x] != -1) {
long d = pk[x] - k;
k %= d;
}
// cout << x << " " << k << "\n";
pk[x] = k;
}
return 0;
} | replace | 24 | 25 | 24 | 25 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for (ll i = (a); i > (b); --i)
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x) \
for (auto &youso_ : (x)) \
cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
cin >> head;
CIN(move(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
ll __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v) \
{ \
FOREACH(x, v) { cout << x << " "; } \
cout << "\n"; \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<double> VD;
const int INF = 1e9;
// const int MOD = 1e9 + 7;
const int MOD = 998244353;
const ll LINF = 1e18;
const double PI = atan(1.0) * 4.0;
const ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238
ll N, K;
VL A(200000);
VVL doubling(ll N, const VL &A) {
VL used(200010, -1);
VVL ret(61, VL(N));
REP(k, 2) REP(i, N) {
if (used[A[i]] != -1) {
ret[0][used[A[i]] % N] = k * N + i - used[A[i]] + 1;
}
used[A[i]] = k * N + i;
}
FOR(k, 1, 60) REP(i, N) {
ret[k][i] = ret[k - 1][i] + ret[k - 1][(i + ret[k - 1][i]) % N];
}
return ret;
}
signed main() {
cin >> N >> K;
REP(i, N) cin >> A[i];
VVL dbl = doubling(N, A);
ll now = 0, sum = 0, C = N * K;
while (1) {
ll num = 0;
REP(k, 60) {
if (dbl[k][now] + sum >= C) {
num = k - 1;
break;
}
}
if (num == -1)
break;
sum += dbl[num][now];
now = sum % N;
}
vector<bool> exist(N, false);
stack<ll> st;
while (sum < C) {
if (!exist[A[now]]) {
st.emplace(A[now]);
exist[A[now]] = true;
} else {
while (exist[A[now]]) {
exist[st.top()] = false;
st.pop();
}
}
now++;
if (now == N)
break;
}
VL ans;
while (st.size()) {
ans.emplace_back(st.top());
st.pop();
}
REPR(i, ans.size() - 1) cout << ans[i] << (i ? " " : "\n");
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for (ll i = (a); i > (b); --i)
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x) \
for (auto &youso_ : (x)) \
cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
cin >> head;
CIN(move(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
ll __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v) \
{ \
FOREACH(x, v) { cout << x << " "; } \
cout << "\n"; \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<double> VD;
const int INF = 1e9;
// const int MOD = 1e9 + 7;
const int MOD = 998244353;
const ll LINF = 1e18;
const double PI = atan(1.0) * 4.0;
const ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238
ll N, K;
VL A(200000);
VVL doubling(ll N, const VL &A) {
VL used(200010, -1);
VVL ret(61, VL(N));
REP(k, 2) REP(i, N) {
if (used[A[i]] != -1) {
ret[0][used[A[i]] % N] = k * N + i - used[A[i]] + 1;
}
used[A[i]] = k * N + i;
}
FOR(k, 1, 60) REP(i, N) {
ret[k][i] = ret[k - 1][i] + ret[k - 1][(i + ret[k - 1][i]) % N];
}
return ret;
}
signed main() {
cin >> N >> K;
REP(i, N) cin >> A[i];
VVL dbl = doubling(N, A);
ll now = 0, sum = 0, C = N * K;
while (1) {
ll num = 0;
REP(k, 60) {
if (dbl[k][now] + sum >= C) {
num = k - 1;
break;
}
}
if (num == -1)
break;
sum += dbl[num][now];
now = sum % N;
}
vector<bool> exist(200010, false);
stack<ll> st;
while (sum < C) {
if (!exist[A[now]]) {
st.emplace(A[now]);
exist[A[now]] = true;
} else {
while (exist[A[now]]) {
exist[st.top()] = false;
st.pop();
}
}
now++;
if (now == N)
break;
}
VL ans;
while (st.size()) {
ans.emplace_back(st.top());
st.pop();
}
REPR(i, ans.size() - 1) cout << ans[i] << (i ? " " : "\n");
} | replace | 112 | 113 | 112 | 113 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int N, A[200009], nx[200009], c[200009], P[200009];
long long K, L, fi[200009], a, b;
void putans(int x) {
vector<int> S;
vector<bool> chk(200009, 0);
for (int i = x; i < N; i++) {
if (chk[A[i]]) {
while (chk[A[i]]) {
chk[S.back()] = 0;
S.pop_back();
}
} else {
chk[A[i]] = 1;
S.push_back(A[i]);
}
}
for (auto &it : S)
printf("%d ", it);
exit(0);
}
void go(int i, long long d) {
if (fi[i] != -1) {
a = fi[i];
b = d - fi[i];
return;
}
fi[i] = d;
go(nx[i], d + c[i]);
}
void Do(int i, long long d) {
if (d + c[i] <= L)
Do(nx[i], d + c[i]);
else
putans(i);
}
int main() {
scanf("%d%lld", &N, &K);
L = K * N;
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
A[i + N] = A[i];
fi[i] = -1;
}
for (int i = 2 * N - 1; i >= 0; i--) {
if (i < N)
nx[i] = (P[A[i]] + 1) % N, c[i] = P[A[i]] - i + 1;
P[A[i]] = i;
}
go(0, 0);
if (L <= a)
Do(0, 0);
else {
L -= a;
L %= b;
if (L == 0)
return 0;
Do(0, 0);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int N, A[400009], nx[200009], c[200009], P[200009];
long long K, L, fi[200009], a, b;
void putans(int x) {
vector<int> S;
vector<bool> chk(200009, 0);
for (int i = x; i < N; i++) {
if (chk[A[i]]) {
while (chk[A[i]]) {
chk[S.back()] = 0;
S.pop_back();
}
} else {
chk[A[i]] = 1;
S.push_back(A[i]);
}
}
for (auto &it : S)
printf("%d ", it);
exit(0);
}
void go(int i, long long d) {
if (fi[i] != -1) {
a = fi[i];
b = d - fi[i];
return;
}
fi[i] = d;
go(nx[i], d + c[i]);
}
void Do(int i, long long d) {
if (d + c[i] <= L)
Do(nx[i], d + c[i]);
else
putans(i);
}
int main() {
scanf("%d%lld", &N, &K);
L = K * N;
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
A[i + N] = A[i];
fi[i] = -1;
}
for (int i = 2 * N - 1; i >= 0; i--) {
if (i < N)
nx[i] = (P[A[i]] + 1) % N, c[i] = P[A[i]] - i + 1;
P[A[i]] = i;
}
go(0, 0);
if (L <= a)
Do(0, 0);
else {
L -= a;
L %= b;
if (L == 0)
return 0;
Do(0, 0);
}
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<int> pos[200005];
int A[200005];
int prev1[400005];
int next1[400005];
bool present[200005];
vector<int> stack1;
int main() {
int N;
long long K;
scanf("%lld%lld", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
A[N + i] = A[i];
}
A[2 * N] = A[0];
memset(prev1, -1, sizeof(prev1));
for (int i = 0; i <= 2 * N; i++) {
if (prev1[A[i]] == -1) {
prev1[A[i]] = i;
} else {
next1[prev1[A[i]]] = i;
prev1[A[i]] = i;
}
}
long long cur_pos = 0;
long long P = N + 1;
while (true) {
long long temp = cur_pos / N * N;
long long next1_pos = temp + next1[cur_pos % N] + 1;
if (next1_pos % N == 0) {
P = next1_pos / N;
break;
} else {
cur_pos = next1_pos;
}
}
K %= P;
cur_pos = 0;
while (true) {
long long temp = cur_pos / N * N;
long long next1_pos = temp + next1[cur_pos % N] + 1;
if (next1_pos >= N * K) {
break;
} else {
cur_pos = next1_pos;
}
}
cur_pos %= N;
memset(present, 0, sizeof(present));
for (int i = cur_pos; i < N; i++) {
if (!present[A[i]]) {
stack1.push_back(A[i]);
present[A[i]] = true;
} else {
while (!stack1.empty()) {
int k = stack1.back();
stack1.pop_back();
present[k] = false;
if (!present[A[i]]) {
break;
}
}
}
}
for (int i = 0; i < (int)stack1.size(); i++) {
printf("%d", stack1[i]);
if (i != stack1.size() - 1) {
printf(" ");
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int A[400005];
int prev1[400005];
int next1[400005];
bool present[200005];
vector<int> stack1;
int main() {
int N;
long long K;
scanf("%lld%lld", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
A[N + i] = A[i];
}
A[2 * N] = A[0];
memset(prev1, -1, sizeof(prev1));
for (int i = 0; i <= 2 * N; i++) {
if (prev1[A[i]] == -1) {
prev1[A[i]] = i;
} else {
next1[prev1[A[i]]] = i;
prev1[A[i]] = i;
}
}
long long cur_pos = 0;
long long P = N + 1;
while (true) {
long long temp = cur_pos / N * N;
long long next1_pos = temp + next1[cur_pos % N] + 1;
if (next1_pos % N == 0) {
P = next1_pos / N;
break;
} else {
cur_pos = next1_pos;
}
}
K %= P;
cur_pos = 0;
while (true) {
long long temp = cur_pos / N * N;
long long next1_pos = temp + next1[cur_pos % N] + 1;
if (next1_pos >= N * K) {
break;
} else {
cur_pos = next1_pos;
}
}
cur_pos %= N;
memset(present, 0, sizeof(present));
for (int i = cur_pos; i < N; i++) {
if (!present[A[i]]) {
stack1.push_back(A[i]);
present[A[i]] = true;
} else {
while (!stack1.empty()) {
int k = stack1.back();
stack1.pop_back();
present[k] = false;
if (!present[A[i]]) {
break;
}
}
}
}
for (int i = 0; i < (int)stack1.size(); i++) {
printf("%d", stack1[i]);
if (i != stack1.size() - 1) {
printf(" ");
}
}
return 0;
}
| replace | 3 | 6 | 3 | 4 | 0 | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define pb push_back
#define pll pair<ll, ll>
#define mp make_pair
#define pyshnapyshnakaa \
ios_base ::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#define plll pair<ll, pair<ll, ll>>
#define pllll pair<pair<ll, ll>, pair<ll, ll>>
#define psl pair<string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector<vector<ll>>
typedef long long ll;
typedef long double ld;
using namespace std;
const ll maxn = 1e6 + 100;
const ll maxc = 1e9;
ll n, m, k, t;
ll A[maxn];
vector<ll> I[maxn];
ll J[maxn];
ll F[maxn];
void solve(ll i) {
vector<ll> ANS;
// cout << " SOLVING " << i << endl;
ll q, w;
while (1) {
ll j = J[i] + 1;
// cout << i << " " << j << endl;
if (I[A[i]].back() == i) {
// cur++;
// j = 0;
j--;
ANS.pb(A[i]);
}
i = (I[A[i]][j] + 1);
if (i >= n) {
break;
}
}
for (q = 0; q < ANS.size(); q++) {
cout << ANS[q] << " ";
}
exit(0);
}
int main() {
pyshnapyshnakaa;
ll q, w, e, a, b, c;
cin >> n >> k;
if (n == 1) {
while (1) {
n++;
}
}
for (q = 0; q < n; q++) {
cin >> A[q];
J[q] = I[A[q]].size();
I[A[q]].pb(q);
}
// cout << I[1].size() << endl;
// if (n == 1) {
// if (k % 2) {
// cout << A[0];
// }
// return 0;
// }
vector<ll> starts = {0};
for (q = 1; q < n; q++) {
F[q] = -1;
}
ll i = 0;
ll cur = 0;
while (1) {
// cout << i << " " << cur << " I CUR" << endl;
if (cur == k - 1) {
solve(i);
}
ll j = J[i] + 1;
if (I[A[i]].back() == i) {
cur++;
j = 0;
// cout << "HERRE " << j << endl;
i = (I[A[i]][j] + 1) % n;
starts.pb(i);
F[i] = cur;
} else {
// cout << "ANOTHER WAY OUT" << endl;
i = (I[A[i]][j] + 1) % n;
}
if (cur == k - 1 && i == 0) {
break;
}
if (i == 0) {
cur++;
// cout << cur << " " << k << endl;
starts.pb(i);
// if (F[i] != -1) {
// cout << "PYMAV 2 " << i << " " << cur << " " << F[i] << endl;
ll k1 = k - 1;
ll c = k1 % (cur);
// cout << c << " C" << endl;
ll ind = starts[c];
// cout << "IND " << ind << endl;
solve(ind);
// }
// F[i] = cur;
}
}
return 0;
}
| #include <bits/stdc++.h>
#define pb push_back
#define pll pair<ll, ll>
#define mp make_pair
#define pyshnapyshnakaa \
ios_base ::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#define plll pair<ll, pair<ll, ll>>
#define pllll pair<pair<ll, ll>, pair<ll, ll>>
#define psl pair<string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector<vector<ll>>
typedef long long ll;
typedef long double ld;
using namespace std;
const ll maxn = 1e6 + 100;
const ll maxc = 1e9;
ll n, m, k, t;
ll A[maxn];
vector<ll> I[maxn];
ll J[maxn];
ll F[maxn];
void solve(ll i) {
vector<ll> ANS;
// cout << " SOLVING " << i << endl;
ll q, w;
while (1) {
ll j = J[i] + 1;
// cout << i << " " << j << endl;
if (I[A[i]].back() == i) {
// cur++;
// j = 0;
j--;
ANS.pb(A[i]);
}
i = (I[A[i]][j] + 1);
if (i >= n) {
break;
}
}
for (q = 0; q < ANS.size(); q++) {
cout << ANS[q] << " ";
}
exit(0);
}
int main() {
pyshnapyshnakaa;
ll q, w, e, a, b, c;
cin >> n >> k;
// if (n == 1) {
// while(1) {
// n++;
// }
// }
for (q = 0; q < n; q++) {
cin >> A[q];
J[q] = I[A[q]].size();
I[A[q]].pb(q);
}
// cout << I[1].size() << endl;
// if (n == 1) {
// if (k % 2) {
// cout << A[0];
// }
// return 0;
// }
vector<ll> starts = {0};
for (q = 1; q < n; q++) {
F[q] = -1;
}
ll i = 0;
ll cur = 0;
while (1) {
// cout << i << " " << cur << " I CUR" << endl;
if (cur == k - 1) {
solve(i);
}
ll j = J[i] + 1;
if (I[A[i]].back() == i) {
cur++;
j = 0;
// cout << "HERRE " << j << endl;
i = (I[A[i]][j] + 1) % n;
starts.pb(i);
F[i] = cur;
} else {
// cout << "ANOTHER WAY OUT" << endl;
i = (I[A[i]][j] + 1) % n;
}
if (cur == k - 1 && i == 0) {
break;
}
if (i == 0) {
cur++;
// cout << cur << " " << k << endl;
starts.pb(i);
// if (F[i] != -1) {
// cout << "PYMAV 2 " << i << " " << cur << " " << F[i] << endl;
ll k1 = k - 1;
ll c = k1 % (cur);
// cout << c << " C" << endl;
ll ind = starts[c];
// cout << "IND " << ind << endl;
solve(ind);
// }
// F[i] = cur;
}
}
return 0;
}
| replace | 66 | 71 | 66 | 71 | TLE | |
p02964 | 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 <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define print(x) cout << x << '\n';
#define pe(x) cout << x << " ";
#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 double long double
using namespace std;
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;
}
const int MOD = 1e9 + 7;
const ll INF = 1e17;
const int INT_INF = 1e9;
const double pi = acos(-1);
const double EPS = 1e-10;
typedef pair<int, int> P;
const int MAX = 200020;
int A[400020], nxt[200020], idx[200020];
int a[60][200020]; // a[k][i]=A[i]の数字を先頭とする2^k個のブロックの要素数
int N, K;
int func(int k, int i) {
if (a[k][i] > 0)
return a[k][i];
int res = func(k - 1, i) + func(k - 1, (i + func(k - 1, i)) % N);
a[k][i] = res;
return res;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
// clock_t start = clock();
cin >> N >> K;
REP(i, N) {
cin >> A[i];
A[i + N] = A[i];
}
REP(i, 200020) idx[i] = -1;
REPR(i, N * 2 - 1) {
if (idx[A[i]] == -1) {
idx[A[i]] = i;
} else {
nxt[i] = idx[A[i]];
}
nxt[i] = idx[A[i]];
if (i < N) {
a[0][i] = nxt[i] - i + 1;
}
idx[A[i]] = i;
}
/*REP(i, N) {
pe(a[0][i]);
}cout << endl;*/
ll nex = 0;
ll pos = 0;
while (pos < (N) * (K - 1)) {
REP(k, 60) {
if (pos + func(k, pos % N) > N * K) {
pos = pos + func(k - 1, pos % N);
break;
}
}
// print(pos);
}
pos %= N;
// print(pos);
REP(i, N) { nxt[i] %= N; }
vector<int> ans;
for (int i = pos; i < N; i++) {
if (nxt[i] <= i) {
ans.pb(A[i]);
} else {
i = nxt[i];
}
}
REP(i, ans.size()) { pe(ans[i]); }
cout << endl;
}
| // #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 <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define print(x) cout << x << '\n';
#define pe(x) cout << x << " ";
#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 double long double
using namespace std;
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;
}
const int MOD = 1e9 + 7;
const ll INF = 1e17;
const int INT_INF = 1e9;
const double pi = acos(-1);
const double EPS = 1e-10;
typedef pair<int, int> P;
const int MAX = 200020;
int A[400020], nxt[400020], idx[200020];
int a[60][200020]; // a[k][i]=A[i]の数字を先頭とする2^k個のブロックの要素数
int N, K;
int func(int k, int i) {
if (a[k][i] > 0)
return a[k][i];
int res = func(k - 1, i) + func(k - 1, (i + func(k - 1, i)) % N);
a[k][i] = res;
return res;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
// clock_t start = clock();
cin >> N >> K;
REP(i, N) {
cin >> A[i];
A[i + N] = A[i];
}
REP(i, 200020) idx[i] = -1;
REPR(i, N * 2 - 1) {
if (idx[A[i]] == -1) {
idx[A[i]] = i;
} else {
nxt[i] = idx[A[i]];
}
nxt[i] = idx[A[i]];
if (i < N) {
a[0][i] = nxt[i] - i + 1;
}
idx[A[i]] = i;
}
/*REP(i, N) {
pe(a[0][i]);
}cout << endl;*/
ll nex = 0;
ll pos = 0;
while (pos < (N) * (K - 1)) {
REP(k, 60) {
if (pos + func(k, pos % N) > N * K) {
pos = pos + func(k - 1, pos % N);
break;
}
}
// print(pos);
}
pos %= N;
// print(pos);
REP(i, N) { nxt[i] %= N; }
vector<int> ans;
for (int i = pos; i < N; i++) {
if (nxt[i] <= i) {
ans.pb(A[i]);
} else {
i = nxt[i];
}
}
REP(i, ans.size()) { pe(ans[i]); }
cout << endl;
}
| replace | 61 | 62 | 61 | 62 | 127 | /tmp/fe040f05-f383-4917-9610-eb37a4a5c9b0.out: error while loading shared libraries: libstdc++.so.6: failed to map segment from shared object
|
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;
namespace Problem {
using namespace std;
#define int LL
class Solver2 {
public:
LL n, k;
vector<LL> a;
vector<vector<int>> A;
vector<vector<LL>> tr;
Solver2(LL n, LL k)
: n(n), k(k), a(n), A(200001), tr(57, vector<LL>(n, -1)){};
void nextRow(int i) {
if (tr[0][i] >= 0)
return;
int now = a[i];
auto nxtp = upper_bound(A[now].begin(), A[now].end(), i);
if (nxtp < A[now].end()) {
tr[0][i] = *nxtp - i + 1;
} else {
tr[0][i] = A[now][0] + n - i + 1;
}
}
void solve() {
for (int i = 0; i < n; ++i) {
cin >> a[i];
A[a[i]].push_back(i);
}
for (int i = 0; i < n; ++i) {
nextRow(i);
}
DBG(a)
for (int i = 1; i < tr.size(); ++i) {
for (int j = 0; j < n; ++j) {
int nxt = (tr[i - 1][j] + j) % n;
tr[i][j] = tr[i - 1][j] + tr[i - 1][nxt];
}
DBG(i, tr[i])
}
DBG(a)
LL sum = 0;
LL now = 0;
while (sum < n * k) {
int P = 0;
for (int i = tr.size() - 1; i >= 0; --i) {
if (sum + tr[i][now] <= n * k) {
P = i;
break;
}
if (i == 0)
P = -1;
}
if (P == -1)
break;
sum += tr[P][now];
DBG(P, sum)
now = sum % n;
}
DBG(a)
DBG(now)
if (sum == n * k) {
cout << endl;
return;
}
vector<int> ans = calc(now);
DBG(ans)
for (int i = 0; i < ans.size(); ++i) {
cout << ans[i];
if (i < ans.size() - 1)
cout << ' ';
}
cout << endl;
}
vector<int> calc(int p) {
vector<vector<int>> memo(200001);
set<int> st;
int size = 0;
for (int i = p; i < n; ++i) {
int now = a[i];
if (memo[now].size() == 0) {
memo[now].push_back(size);
size++;
st.insert(now);
} else {
size = memo[now][0];
set<int> er;
for (auto x : st) {
DBG(x, memo[x], size)
while (memo[x].size() > 0 && memo[x].back() >= size)
memo[x].pop_back();
if (memo[x].size() == 0)
er.insert(x);
}
for (auto x : er)
st.erase(x);
}
}
vector<int> ret(size);
for (int i = 1; i <= 200000; ++i) {
for (auto j : memo[i]) {
ret[j] = i;
}
}
return ret;
}
};
} // namespace Problem
signed main() {
std::cin.tie(0);
std::ios_base::sync_with_stdio(false);
// std::cout << std::fixed << std::setprecision(12);
long long n = 0, k;
std::cin >> n >> k;
Problem::Solver2 sol(n, k);
sol.solve();
return 0;
}
| #include <bits/stdc++.h>
#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;
namespace Problem {
using namespace std;
#define int LL
class Solver2 {
public:
LL n, k;
vector<LL> a;
vector<vector<int>> A;
vector<vector<LL>> tr;
Solver2(LL n, LL k)
: n(n), k(k), a(n), A(200001), tr(42, vector<LL>(n, -1)){};
void nextRow(int i) {
if (tr[0][i] >= 0)
return;
int now = a[i];
auto nxtp = upper_bound(A[now].begin(), A[now].end(), i);
if (nxtp < A[now].end()) {
tr[0][i] = *nxtp - i + 1;
} else {
tr[0][i] = A[now][0] + n - i + 1;
}
}
void solve() {
for (int i = 0; i < n; ++i) {
cin >> a[i];
A[a[i]].push_back(i);
}
for (int i = 0; i < n; ++i) {
nextRow(i);
}
DBG(a)
for (int i = 1; i < tr.size(); ++i) {
for (int j = 0; j < n; ++j) {
int nxt = (tr[i - 1][j] + j) % n;
tr[i][j] = tr[i - 1][j] + tr[i - 1][nxt];
}
DBG(i, tr[i])
}
DBG(a)
LL sum = 0;
LL now = 0;
while (sum < n * k) {
int P = 0;
for (int i = tr.size() - 1; i >= 0; --i) {
if (sum + tr[i][now] <= n * k) {
P = i;
break;
}
if (i == 0)
P = -1;
}
if (P == -1)
break;
sum += tr[P][now];
DBG(P, sum)
now = sum % n;
}
DBG(a)
DBG(now)
if (sum == n * k) {
cout << endl;
return;
}
vector<int> ans = calc(now);
DBG(ans)
for (int i = 0; i < ans.size(); ++i) {
cout << ans[i];
if (i < ans.size() - 1)
cout << ' ';
}
cout << endl;
}
vector<int> calc(int p) {
vector<vector<int>> memo(200001);
set<int> st;
int size = 0;
for (int i = p; i < n; ++i) {
int now = a[i];
if (memo[now].size() == 0) {
memo[now].push_back(size);
size++;
st.insert(now);
} else {
size = memo[now][0];
set<int> er;
for (auto x : st) {
DBG(x, memo[x], size)
while (memo[x].size() > 0 && memo[x].back() >= size)
memo[x].pop_back();
if (memo[x].size() == 0)
er.insert(x);
}
for (auto x : er)
st.erase(x);
}
}
vector<int> ret(size);
for (int i = 1; i <= 200000; ++i) {
for (auto j : memo[i]) {
ret[j] = i;
}
}
return ret;
}
};
} // namespace Problem
signed main() {
std::cin.tie(0);
std::ios_base::sync_with_stdio(false);
// std::cout << std::fixed << std::setprecision(12);
long long n = 0, k;
std::cin >> n >> k;
Problem::Solver2 sol(n, k);
sol.solve();
return 0;
}
| replace | 28 | 29 | 28 | 29 | 0 | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;
ll MAX = pow(10, 5) * 2 + 1;
ll N, K;
vector<ll> calc(vector<ll> &ans) {
vector<vector<ll>> place(MAX);
for (ll i = 0; i < ans.size(); ++i) {
place.at(ans.at(i)).push_back(i);
}
vector<ll> res;
ll index = 0;
while (index < ans.size()) {
if (lower_bound(place.at(ans.at(index)).begin(),
place.at(ans.at(index)).end(),
index + 1) == place.at(ans.at(index)).end()) {
res.push_back(ans.at(index));
} else {
index = *lower_bound(place.at(ans.at(index)).begin(),
place.at(ans.at(index)).end(), index + 1);
}
index += 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> K;
vector<ll> A(N);
vector<vector<ll>> place(MAX);
for (ll i = 0; i < N; ++i) {
cin >> A.at(i);
place.at(A.at(i)).push_back(i);
}
// A = calc(A);
// for (ll i = 0; i < A.size(); ++i) {
// cout << A.at(i) << " ";
// }
// cout << "\n";
ll power = 30;
vector<vector<ll>> d(power, vector<ll>(N));
for (ll i = 0; i < N; ++i) {
ll index =
lower_bound(place.at(A.at(i)).begin(), place.at(A.at(i)).end(), i + 1) -
place.at(A.at(i)).begin();
if (index == place.at(A.at(i)).size()) {
index = 0;
}
d.at(0).at(i) = (place.at(A.at(i)).at(index) + 1) % N;
}
for (ll i = 0; i < power - 1; ++i) {
for (ll j = 0; j < N; ++j) {
d.at(i + 1).at(j) = d.at(i).at(d.at(i).at(j));
}
}
vector<vector<ll>> len(power, vector<ll>(N));
for (ll i = 0; i < N; ++i) {
len.at(0).at(i) = (d.at(0).at(i) - 1) - i;
if (len.at(0).at(i) <= 0) {
len.at(0).at(i) += N;
}
if (len.at(0).at(i) <= 0) {
len.at(0).at(i) += N;
}
len.at(0).at(i) += 1;
}
for (ll i = 0; i < power - 1; ++i) {
for (ll j = 0; j < N; ++j) {
len.at(i + 1).at(j) = len.at(i).at(d.at(i).at(j)) + len.at(i).at(j);
}
}
// for (ll i = 0; i < power; ++i) {
// for (ll j = 0; j < N; ++j) {
// cout << len.at(i).at(j) << " ";
// }
// cout << "\n";
// }
ll now = 0;
ll num = 0;
K *= N;
while (true) {
bool update = false;
for (ll i = power - 1; i >= 0; --i) {
if (len.at(i).at(now) <= K) {
update = true;
K -= len.at(i).at(now);
now = d.at(i).at(now);
break;
}
}
if (!update) {
break;
}
}
// cout << now << "\n";
// cout << K << "\n";
vector<ll> ans;
for (ll i = 0; i < K; ++i) {
ans.push_back(A.at(now + i));
}
ans = calc(ans);
for (ll i = 0; i < ans.size(); ++i) {
cout << ans.at(i) << " ";
}
cout << "\n";
}
| #include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;
ll MAX = pow(10, 5) * 2 + 1;
ll N, K;
vector<ll> calc(vector<ll> &ans) {
vector<vector<ll>> place(MAX);
for (ll i = 0; i < ans.size(); ++i) {
place.at(ans.at(i)).push_back(i);
}
vector<ll> res;
ll index = 0;
while (index < ans.size()) {
if (lower_bound(place.at(ans.at(index)).begin(),
place.at(ans.at(index)).end(),
index + 1) == place.at(ans.at(index)).end()) {
res.push_back(ans.at(index));
} else {
index = *lower_bound(place.at(ans.at(index)).begin(),
place.at(ans.at(index)).end(), index + 1);
}
index += 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> K;
vector<ll> A(N);
vector<vector<ll>> place(MAX);
for (ll i = 0; i < N; ++i) {
cin >> A.at(i);
place.at(A.at(i)).push_back(i);
}
// A = calc(A);
// for (ll i = 0; i < A.size(); ++i) {
// cout << A.at(i) << " ";
// }
// cout << "\n";
ll power = 40;
vector<vector<ll>> d(power, vector<ll>(N));
for (ll i = 0; i < N; ++i) {
ll index =
lower_bound(place.at(A.at(i)).begin(), place.at(A.at(i)).end(), i + 1) -
place.at(A.at(i)).begin();
if (index == place.at(A.at(i)).size()) {
index = 0;
}
d.at(0).at(i) = (place.at(A.at(i)).at(index) + 1) % N;
}
for (ll i = 0; i < power - 1; ++i) {
for (ll j = 0; j < N; ++j) {
d.at(i + 1).at(j) = d.at(i).at(d.at(i).at(j));
}
}
vector<vector<ll>> len(power, vector<ll>(N));
for (ll i = 0; i < N; ++i) {
len.at(0).at(i) = (d.at(0).at(i) - 1) - i;
if (len.at(0).at(i) <= 0) {
len.at(0).at(i) += N;
}
if (len.at(0).at(i) <= 0) {
len.at(0).at(i) += N;
}
len.at(0).at(i) += 1;
}
for (ll i = 0; i < power - 1; ++i) {
for (ll j = 0; j < N; ++j) {
len.at(i + 1).at(j) = len.at(i).at(d.at(i).at(j)) + len.at(i).at(j);
}
}
// for (ll i = 0; i < power; ++i) {
// for (ll j = 0; j < N; ++j) {
// cout << len.at(i).at(j) << " ";
// }
// cout << "\n";
// }
ll now = 0;
ll num = 0;
K *= N;
while (true) {
bool update = false;
for (ll i = power - 1; i >= 0; --i) {
if (len.at(i).at(now) <= K) {
update = true;
K -= len.at(i).at(now);
now = d.at(i).at(now);
break;
}
}
if (!update) {
break;
}
}
// cout << now << "\n";
// cout << K << "\n";
vector<ll> ans;
for (ll i = 0; i < K; ++i) {
ans.push_back(A.at(now + i));
}
ans = calc(ans);
for (ll i = 0; i < ans.size(); ++i) {
cout << ans.at(i) << " ";
}
cout << "\n";
}
| replace | 44 | 45 | 44 | 45 | TLE | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
using ll = long long;
void naive() {
int n, k;
cin >> n >> k;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<int> v;
rep(i, k) {
rep(j, n) {
if (find(v.begin(), v.end(), a[j]) == v.end()) {
v.push_back(a[j]);
} else {
while (v.back() != a[j])
v.pop_back();
v.pop_back();
}
}
for (int x : v)
cout << x << ' ';
cout << endl;
}
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n, k;
cin >> n >> k;
vector<int> a(n * 2);
rep(i, n) cin >> a[i], a[i + n] = a[i];
vector<vector<int>> succ(62, vector<int>(n));
vector<vector<ll>> sum(62, vector<ll>(n));
vector<int> pos(200001, -1);
repr(i, n * 2) {
if (i < n) {
succ[0][i] = (pos[a[i]] + 1) % n;
sum[0][i] = pos[a[i]] - i + 1;
}
pos[a[i]] = i;
}
const ll INF = 3e18;
rep(i, 61) rep(j, n) {
succ[i + 1][j] = succ[i][succ[i][j]];
sum[i + 1][j] = min(INF, sum[i][j] + sum[i][succ[i][j]]);
}
ll x = 0;
repr(i, 62) {
if (x + sum[i][x % n] < n * k) {
x += sum[i][x % n];
}
}
vector<bool> in(200001);
vector<int> ans;
while (x < n * k) {
ll v = a[x % n];
if (in[v]) {
while (ans.back() != v) {
ans.pop_back();
}
ans.pop_back();
in[v] = false;
} else {
in[v] = true;
ans.push_back(v);
}
x++;
}
for (int x : ans)
cout << x << ' ';
cout << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
using ll = long long;
void naive() {
int n, k;
cin >> n >> k;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<int> v;
rep(i, k) {
rep(j, n) {
if (find(v.begin(), v.end(), a[j]) == v.end()) {
v.push_back(a[j]);
} else {
while (v.back() != a[j])
v.pop_back();
v.pop_back();
}
}
for (int x : v)
cout << x << ' ';
cout << endl;
}
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n, k;
cin >> n >> k;
vector<int> a(n * 2);
rep(i, n) cin >> a[i], a[i + n] = a[i];
vector<vector<int>> succ(62, vector<int>(n));
vector<vector<ll>> sum(62, vector<ll>(n));
vector<int> pos(200001, -1);
repr(i, n * 2) {
if (i < n) {
succ[0][i] = (pos[a[i]] + 1) % n;
sum[0][i] = pos[a[i]] - i + 1;
}
pos[a[i]] = i;
}
const ll INF = 3e18;
rep(i, 61) rep(j, n) {
succ[i + 1][j] = succ[i][succ[i][j]];
sum[i + 1][j] = min(INF, sum[i][j] + sum[i][succ[i][j]]);
}
ll x = 0;
repr(i, 62) {
if (x + sum[i][x % n] < n * k) {
x += sum[i][x % n];
}
}
vector<bool> in(200001);
vector<int> ans;
while (x < n * k) {
ll v = a[x % n];
if (in[v]) {
while (ans.back() != v) {
in[ans.back()] = false;
ans.pop_back();
}
ans.pop_back();
in[v] = false;
} else {
in[v] = true;
ans.push_back(v);
}
x++;
}
for (int x : ans)
cout << x << ' ';
cout << endl;
} | insert | 64 | 64 | 64 | 65 | 0 | |
p02964 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define _USE_MATH_DEFINES
#include <complex>
#include <functional>
#include <math.h>
using namespace std;
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
typedef long long ll;
const ll INF = 1e17;
const ll MOD = 998244353;
const ll MAX = 4000001;
const double eps = 1E-18;
ll max(ll a, ll b) {
if (a > b) {
return a;
}
return b;
}
ll min(ll a, ll b) {
if (a > b) {
return b;
}
return a;
}
ll gcd(ll a, ll b) {
if (b == 0) {
return a;
}
if (a < b) {
return gcd(b, a);
}
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
struct edge {
ll ind;
ll fr;
ll to;
ll d;
};
class mint {
long long x;
public:
mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint &a) {
if ((x += a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator-=(const mint &a) {
if ((x += MOD - a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator*=(const mint &a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint &a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint &a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint &a) const {
mint res(*this);
return res *= a;
}
mint operator^(const ll &t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime MOD
mint inv() const { return pow(MOD - 2); }
mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
mint operator/(const mint &a) const {
mint res(*this);
return res /= a;
}
friend ostream &operator<<(ostream &os, const mint &m) {
os << m.x;
return os;
}
};
///////////////////////////
int main() {
ll N, K;
cin >> N >> K;
vector<ll> A(N);
rep(i, N) cin >> A[i];
vector<ll> nx(N);
vector<ll> use(300000, -1);
rep(i, 2 * N) {
ll x = A[i % N];
if (use[x] == -1) {
use[x] = i;
} else if (use[x] < N) {
nx[use[x]] = i - use[x];
use[x] = i;
}
}
// rep(i, N) cout << nx[i] << endl;
vector<ll> emp;
ll v = nx[0];
emp.push_back(-1);
emp.push_back(nx[0]);
while ((v + 1) % N != 0) {
v = v + 1 + nx[(v + 1) % N];
emp.push_back(v);
}
// rep(i, emp.size()) { cout << emp[i] << endl; }
ll t = (K * N - 1) % (v + 1);
auto itr = upper_bound(emp.begin(), emp.end(), t);
ll e = *(--itr);
// cout << t << e << endl;
vector<bool> ct(30000, 0);
stack<ll> st;
for (ll i = e + 1; i <= t; i++) {
ll x = A[i % N];
if (ct[x] == 0) {
st.push(x);
ct[x] = 1;
}
else {
while (st.top() != x) {
ct[st.top()] = 0;
st.pop();
}
ct[x] = 0;
st.pop();
}
}
vector<ll> rvans;
while (!st.empty()) {
rvans.push_back(st.top());
st.pop();
}
reverse(rvans.begin(), rvans.end());
rep(i, rvans.size()) { cout << rvans[i] << " "; }
system("PAUSE");
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define _USE_MATH_DEFINES
#include <complex>
#include <functional>
#include <math.h>
using namespace std;
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
typedef long long ll;
const ll INF = 1e17;
const ll MOD = 998244353;
const ll MAX = 4000001;
const double eps = 1E-18;
ll max(ll a, ll b) {
if (a > b) {
return a;
}
return b;
}
ll min(ll a, ll b) {
if (a > b) {
return b;
}
return a;
}
ll gcd(ll a, ll b) {
if (b == 0) {
return a;
}
if (a < b) {
return gcd(b, a);
}
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
struct edge {
ll ind;
ll fr;
ll to;
ll d;
};
class mint {
long long x;
public:
mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint &a) {
if ((x += a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator-=(const mint &a) {
if ((x += MOD - a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator*=(const mint &a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint &a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint &a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint &a) const {
mint res(*this);
return res *= a;
}
mint operator^(const ll &t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime MOD
mint inv() const { return pow(MOD - 2); }
mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
mint operator/(const mint &a) const {
mint res(*this);
return res /= a;
}
friend ostream &operator<<(ostream &os, const mint &m) {
os << m.x;
return os;
}
};
///////////////////////////
int main() {
ll N, K;
cin >> N >> K;
vector<ll> A(N);
rep(i, N) cin >> A[i];
vector<ll> nx(N);
vector<ll> use(300000, -1);
rep(i, 2 * N) {
ll x = A[i % N];
if (use[x] == -1) {
use[x] = i;
} else if (use[x] < N) {
nx[use[x]] = i - use[x];
use[x] = i;
}
}
// rep(i, N) cout << nx[i] << endl;
vector<ll> emp;
ll v = nx[0];
emp.push_back(-1);
emp.push_back(nx[0]);
while ((v + 1) % N != 0) {
v = v + 1 + nx[(v + 1) % N];
emp.push_back(v);
}
// rep(i, emp.size()) { cout << emp[i] << endl; }
ll t = (K * N - 1) % (v + 1);
auto itr = upper_bound(emp.begin(), emp.end(), t);
ll e = *(--itr);
// cout << t << e << endl;
vector<bool> ct(300000, 0);
stack<ll> st;
for (ll i = e + 1; i <= t; i++) {
ll x = A[i % N];
if (ct[x] == 0) {
st.push(x);
ct[x] = 1;
}
else {
while (st.top() != x) {
ct[st.top()] = 0;
st.pop();
}
ct[x] = 0;
st.pop();
}
}
vector<ll> rvans;
while (!st.empty()) {
rvans.push_back(st.top());
st.pop();
}
reverse(rvans.begin(), rvans.end());
rep(i, rvans.size()) { cout << rvans[i] << " "; }
system("PAUSE");
}
| replace | 168 | 169 | 168 | 169 | 0 | sh: 1: PAUSE: not found
|
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
template <size_t N, typename T, typename U = T,
std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, T t) {
return std::vector<U>(sz, t);
}
template <size_t N, typename T,
std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {
return std::vector<T>(sz);
}
template <size_t N, typename T, typename... Args,
std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {
return make_vector<1>(a, make_vector<N - 1, T>(args...));
}
template <typename T, typename Size_t> auto &at(T &t, Size_t i) {
return t.at(i);
}
template <typename T, typename Size_t, typename... Args>
auto &at(T &t, Size_t i, Args... args) {
return at(t.at(i), args...);
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int n;
long long k;
std::cin >> n >> k;
std::vector<int> a(n);
for (auto &x : a) {
std::cin >> x;
x--;
}
constexpr int max = 2e5;
auto m = make_vector<2, int>(max, 0);
for (int i = 0; i < n; i++) {
m.at(a.at(i)).push_back(i);
}
auto add = std::vector<int>(n);
for (auto &v : m) {
if (v.empty())
continue;
int sz = v.size();
v.push_back(n + v.front());
for (int i = 0; i < sz; i++) {
auto crr = v.at(i), nxt = v.at(i + 1);
assert(0 <= crr && crr < n && crr < nxt);
add.at(crr) = nxt - crr + 1;
}
}
auto moved = [&](auto x) { return x + add.at(x % n); };
k %= [&] {
int ret = 0;
for (int i = 0; i < n; i++) {
ret = moved(ret);
if (ret % n == 0)
break;
}
assert(ret % n == 0);
return ret / n;
}();
long long crr = 0, goal = n * k;
std::vector<int> ans;
while (crr < goal) {
auto nxt = moved(crr);
if (nxt <= goal) {
crr = nxt;
continue;
}
ans.push_back(a.at((crr++ % n)));
}
int sz = ans.size();
for (int i = 0; i < sz; i++) {
std::cout << (i > 0 ? " " : "") << ans.at(i) + 1;
}
std::cout << std::endl;
return 0;
} | #include <bits/stdc++.h>
template <size_t N, typename T, typename U = T,
std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, T t) {
return std::vector<U>(sz, t);
}
template <size_t N, typename T,
std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {
return std::vector<T>(sz);
}
template <size_t N, typename T, typename... Args,
std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {
return make_vector<1>(a, make_vector<N - 1, T>(args...));
}
template <typename T, typename Size_t> auto &at(T &t, Size_t i) {
return t.at(i);
}
template <typename T, typename Size_t, typename... Args>
auto &at(T &t, Size_t i, Args... args) {
return at(t.at(i), args...);
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int n;
long long k;
std::cin >> n >> k;
std::vector<int> a(n);
for (auto &x : a) {
std::cin >> x;
x--;
}
constexpr int max = 2e5;
auto m = make_vector<2, int>(max, 0);
for (int i = 0; i < n; i++) {
m.at(a.at(i)).push_back(i);
}
auto add = std::vector<int>(n);
for (auto &v : m) {
if (v.empty())
continue;
int sz = v.size();
v.push_back(n + v.front());
for (int i = 0; i < sz; i++) {
auto crr = v.at(i), nxt = v.at(i + 1);
assert(0 <= crr && crr < n && crr < nxt);
add.at(crr) = nxt - crr + 1;
}
}
auto moved = [&](auto x) { return x + add.at(x % n); };
k %= [&] {
long long ret = 0;
for (int i = 0; i < n; i++) {
ret = moved(ret);
if (ret % n == 0)
break;
}
assert(ret % n == 0);
return ret / n;
}();
long long crr = 0, goal = n * k;
std::vector<int> ans;
while (crr < goal) {
auto nxt = moved(crr);
if (nxt <= goal) {
crr = nxt;
continue;
}
ans.push_back(a.at((crr++ % n)));
}
int sz = ans.size();
for (int i = 0; i < sz; i++) {
std::cout << (i > 0 ? " " : "") << ans.at(i) + 1;
}
std::cout << std::endl;
return 0;
} | replace | 55 | 56 | 55 | 56 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for (ll i = (a); i > (b); --i)
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x) \
for (auto &youso_ : (x)) \
cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
cin >> head;
CIN(move(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
ll __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v) \
{ \
FOREACH(x, v) { cout << x << " "; } \
cout << "\n"; \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<double> VD;
const int INF = 1e9;
// const int MOD = 1e9 + 7;
const int MOD = 998244353;
const ll LINF = 1e18;
const double PI = atan(1.0) * 4.0;
const ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238
ll N, K;
VL A(200000);
VVL doubling(ll N, const VL &A) {
VL used(200010, -1);
VVL ret(61, VL(N));
REP(k, 2) REP(i, N) {
if (used[A[i]] != -1) {
ret[0][used[A[i]] % N] = k * N + i - used[A[i]] + 1;
}
used[A[i]] = k * N + i;
}
REP(k, 60) REP(i, N) {
ret[k][i] = ret[k - 1][i] + ret[k - 1][(i + ret[k - 1][i]) % N];
}
return ret;
}
signed main() {
cin >> N >> K;
REP(i, N) cin >> A[i];
VVL dbl = doubling(N, A);
ll now = 0, sum = 0, C = N * K;
while (1) {
ll num = 0;
REP(k, 60) {
if (dbl[k][now] + sum >= C) {
num = k - 1;
break;
}
}
if (num == -1)
break;
sum += dbl[num][now];
now = sum % N;
}
vector<bool> exist(200010, false);
deque<ll> dq;
while (sum < C) {
if (!exist[A[now]]) {
dq.emplace_back(A[now]);
exist[A[now]] = true;
} else {
while (exist[A[now]]) {
exist[dq.back()] = false;
dq.pop_back();
}
}
now++;
sum++;
}
REP(i, dq.size()) cout << dq[i] << (i == N - 1 ? "\n" : " ");
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for (ll i = (a); i > (b); --i)
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x) \
for (auto &youso_ : (x)) \
cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
cin >> head;
CIN(move(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
ll __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v) \
{ \
FOREACH(x, v) { cout << x << " "; } \
cout << "\n"; \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<double> VD;
const int INF = 1e9;
// const int MOD = 1e9 + 7;
const int MOD = 998244353;
const ll LINF = 1e18;
const double PI = atan(1.0) * 4.0;
const ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238
ll N, K;
VL A(200000);
VVL doubling(ll N, const VL &A) {
VL used(200010, -1);
VVL ret(61, VL(N));
REP(k, 2) REP(i, N) {
if (used[A[i]] != -1) {
ret[0][used[A[i]] % N] = k * N + i - used[A[i]] + 1;
}
used[A[i]] = k * N + i;
}
REP(k, 60) REP(i, N) {
ret[k + 1][i] = ret[k][i] + ret[k][(i + ret[k][i]) % N];
}
return ret;
}
signed main() {
cin >> N >> K;
REP(i, N) cin >> A[i];
VVL dbl = doubling(N, A);
ll now = 0, sum = 0, C = N * K;
while (1) {
ll num = 0;
REP(k, 60) {
if (dbl[k][now] + sum >= C) {
num = k - 1;
break;
}
}
if (num == -1)
break;
sum += dbl[num][now];
now = sum % N;
}
vector<bool> exist(200010, false);
deque<ll> dq;
while (sum < C) {
if (!exist[A[now]]) {
dq.emplace_back(A[now]);
exist[A[now]] = true;
} else {
while (exist[A[now]]) {
exist[dq.back()] = false;
dq.pop_back();
}
}
now++;
sum++;
}
REP(i, dq.size()) cout << dq[i] << (i == N - 1 ? "\n" : " ");
} | replace | 89 | 90 | 89 | 90 | -11 | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
void pre() {}
int a[400009], ans[400009];
int n, lst[400009], nxt[400009];
bool vis[400009];
void solve(int i) {
if (vis[i])
return;
if (nxt[i] + 1 >= n) {
ans[i] = (nxt[i] + 1) - n;
vis[i] = 1;
return;
}
solve(nxt[i] + 1);
ans[i] = ans[nxt[i] + 1];
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
pre();
cin >> n;
ll k;
cin >> k;
rep(i, n) {
cin >> a[i];
a[n + i] = a[i];
}
repD(i, 2 * n - 1, n) lst[a[i]] = i;
repD(i, n - 1, 0) {
nxt[i] = lst[a[i]];
lst[a[i]] = i;
}
rep(i, n) {
if (!vis[i])
solve(i);
}
ans[n] = 0;
vi v;
v.pb(0);
memset(lst, -1, sizeof(lst));
lst[0] = 0;
int cur = 0;
while (lst[ans[cur]] == -1) {
cur = ans[cur];
lst[cur] = sz(v);
v.pb(cur);
}
cur = ans[cur];
k--;
if (k >= sz(v)) {
k -= lst[cur];
k %= (sz(v) - lst[cur]);
k += lst[cur];
}
fill(vis);
vi fns;
repA(i, v[k], n - 1) {
if (!vis[a[i]])
fns.pb(a[i]), vis[a[i]] = 1;
else {
while (fns.back() != a[i])
vis[fns.back()] = 0, fns.pop_back();
vis[a[i]] = 0;
fns.pop_back();
}
}
trav(i, fns) cout << i << ' ';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
void pre() {}
int a[400009], ans[400009];
int n, lst[400009], nxt[400009];
bool vis[400009];
void solve(int i) {
if (vis[i])
return;
if (nxt[i] + 1 >= n) {
ans[i] = (nxt[i] + 1) - n;
vis[i] = 1;
return;
}
solve(nxt[i] + 1);
ans[i] = ans[nxt[i] + 1];
vis[i] = 1;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
pre();
cin >> n;
ll k;
cin >> k;
rep(i, n) {
cin >> a[i];
a[n + i] = a[i];
}
repD(i, 2 * n - 1, n) lst[a[i]] = i;
repD(i, n - 1, 0) {
nxt[i] = lst[a[i]];
lst[a[i]] = i;
}
rep(i, n) {
if (!vis[i])
solve(i);
}
ans[n] = 0;
vi v;
v.pb(0);
memset(lst, -1, sizeof(lst));
lst[0] = 0;
int cur = 0;
while (lst[ans[cur]] == -1) {
cur = ans[cur];
lst[cur] = sz(v);
v.pb(cur);
}
cur = ans[cur];
k--;
if (k >= sz(v)) {
k -= lst[cur];
k %= (sz(v) - lst[cur]);
k += lst[cur];
}
fill(vis);
vi fns;
repA(i, v[k], n - 1) {
if (!vis[a[i]])
fns.pb(a[i]), vis[a[i]] = 1;
else {
while (fns.back() != a[i])
vis[fns.back()] = 0, fns.pop_back();
vis[a[i]] = 0;
fns.pop_back();
}
}
trav(i, fns) cout << i << ' ';
return 0;
}
| insert | 51 | 51 | 51 | 52 | TLE | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
int a[maxn * 2];
int pre[maxn];
int net[maxn];
vector<int> s;
int vis[maxn];
ll stute[maxn];
int main() {
int n;
ll k;
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i], a[i + n] = a[i];
for (int i = 2 * n; i >= 1; i--) {
net[i] = pre[a[i]];
pre[a[i]] = i;
}
int now = 1;
memset(stute, -1, sizeof stute);
while (1) {
if (k <= 2) {
for (int i = now; i <= k * n; i++) {
if (i > n)
i = i - n, k--;
int temp = a[i];
if (vis[a[i]] == 1) {
while (s.back() != temp) {
vis[s.back()] = 0;
s.pop_back();
}
s.pop_back();
vis[a[i]] = 0;
} else {
s.push_back(a[i]);
vis[a[i]] = 1;
}
}
break;
}
now = net[now] + 1;
while (now > n) {
now -= n;
k--;
}
if (stute[now] != -1) {
k = k % (stute[now] - k);
}
stute[now] = k;
}
for (int i = 0; i < s.size(); i++)
cout << s[i] << ' ';
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e5 + 10;
int a[maxn];
int pre[maxn];
int net[maxn];
vector<int> s;
int vis[maxn];
ll stute[maxn];
int main() {
int n;
ll k;
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i], a[i + n] = a[i];
for (int i = 2 * n; i >= 1; i--) {
net[i] = pre[a[i]];
pre[a[i]] = i;
}
int now = 1;
memset(stute, -1, sizeof stute);
while (1) {
if (k <= 2) {
for (int i = now; i <= k * n; i++) {
if (i > n)
i = i - n, k--;
int temp = a[i];
if (vis[a[i]] == 1) {
while (s.back() != temp) {
vis[s.back()] = 0;
s.pop_back();
}
s.pop_back();
vis[a[i]] = 0;
} else {
s.push_back(a[i]);
vis[a[i]] = 1;
}
}
break;
}
now = net[now] + 1;
while (now > n) {
now -= n;
k--;
}
if (stute[now] != -1) {
k = k % (stute[now] - k);
}
stute[now] = k;
}
for (int i = 0; i < s.size(); i++)
cout << s[i] << ' ';
cout << endl;
return 0;
}
| replace | 3 | 5 | 3 | 5 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define pb push_back
#define speed \
ios_base::sync_with_stdio(0); \
cin.tie(0);
using namespace std;
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
speed int n, k;
cin >> n >> k;
vector<int> inp(n);
map<int, vector<int>> mp;
for (int i = 0; i < n; ++i) {
cin >> inp[i];
mp[inp[i]].pb(i);
}
vector<bool> status(n + 1, 0);
status[n] = 1;
map<int, int> next;
next[-1] = -1;
for (auto it : mp) {
int len = (it.y).size();
for (int i = 0; i < len - 1; ++i) {
next[(it.y)[i]] = (it.y)[i + 1];
}
next[(it.y)[len - 1]] = (it.y)[0];
status[(it.y)[len - 1]] = 1;
}
int pres = -1;
vector<int> fin;
do {
pres = (next[pres] + 1);
while (!status[pres]) {
pres = (next[pres] + 1);
}
fin.pb(pres);
} while (pres != n);
int dis = fin.size();
int r = (k % dis);
if (r == 0) {
return 0;
}
r = inp[fin[r - 1]];
int six = mp[r].back();
stack<int> st;
map<int, bool> done;
for (int i = six; i < n; ++i) {
if (!done[inp[i]]) {
st.push(inp[i]);
done[inp[i]] = 1;
} else {
while (st.top() != inp[i]) {
done[st.top()] = 0;
st.pop();
}
done[inp[i]] = 0;
st.pop();
}
}
vector<int> sol;
while (!st.empty()) {
sol.pb(st.top());
st.pop();
}
reverse(sol.begin(), sol.end());
for (auto it : sol) {
cout << it << " ";
}
} | #include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define pb push_back
#define speed \
ios_base::sync_with_stdio(0); \
cin.tie(0);
using namespace std;
signed main() {
speed int n, k;
cin >> n >> k;
vector<int> inp(n);
map<int, vector<int>> mp;
for (int i = 0; i < n; ++i) {
cin >> inp[i];
mp[inp[i]].pb(i);
}
vector<bool> status(n + 1, 0);
status[n] = 1;
map<int, int> next;
next[-1] = -1;
for (auto it : mp) {
int len = (it.y).size();
for (int i = 0; i < len - 1; ++i) {
next[(it.y)[i]] = (it.y)[i + 1];
}
next[(it.y)[len - 1]] = (it.y)[0];
status[(it.y)[len - 1]] = 1;
}
int pres = -1;
vector<int> fin;
do {
pres = (next[pres] + 1);
while (!status[pres]) {
pres = (next[pres] + 1);
}
fin.pb(pres);
} while (pres != n);
int dis = fin.size();
int r = (k % dis);
if (r == 0) {
return 0;
}
r = inp[fin[r - 1]];
int six = mp[r].back();
stack<int> st;
map<int, bool> done;
for (int i = six; i < n; ++i) {
if (!done[inp[i]]) {
st.push(inp[i]);
done[inp[i]] = 1;
} else {
while (st.top() != inp[i]) {
done[st.top()] = 0;
st.pop();
}
done[inp[i]] = 0;
st.pop();
}
}
vector<int> sol;
while (!st.empty()) {
sol.pb(st.top());
st.pop();
}
reverse(sol.begin(), sol.end());
for (auto it : sol) {
cout << it << " ";
}
} | delete | 11 | 15 | 11 | 11 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02964 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
int nextd[64][200010];
int A[400010];
vector<int> v[200010];
signed main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
rep(i, N) cin >> A[i];
rep(i, N) A[N + i] = A[i];
rep(i, N * 2) { v[A[i]].push_back(i); }
rep(i, 60) rep(j, 200010) nextd[i][j] = INF2;
// rep(i, N) {
// cout << i + 1 << ": ";
// rep(j, v[i + 1].size()) {
// cout << v[i + 1][j] << " ";
// }
// cout << endl;
// }
rep(i, N) {
auto head = lower_bound(v[A[i]].begin(), v[A[i]].end(), i);
auto nxt = head;
++nxt;
// cout << *head << " " << *nxt << endl;
nextd[0][i] = *nxt - *head + 1;
}
rep(i, 59) {
rep(j, N) {
if (nextd[i][j] != INF2) {
int nxt = j;
nxt += nextd[i][nxt];
nxt += nextd[i][nxt % N];
if (nxt - j <= N * K)
nextd[i + 1][j] = nxt - j;
}
}
}
int now = 0;
while (true) {
int nxt = now;
rep(i, 60) {
if (now + nextd[i][now % N] <= N * K) {
nxt = now + nextd[i][now % N];
} else {
break;
}
}
if (nxt == now)
break;
now = nxt;
// cout << now << " " << now % N << " "<< N * K - now << endl;
}
int s = now % N;
int c = N * K - now;
int cnt[200010];
vector<int> v2;
rep(i, c) {
if (cnt[A[(s + i) % N]] == 0) {
cnt[A[(s + i) % N]]++;
v2.push_back(A[(s + i) % N]);
} else {
while (cnt[A[(s + i) % N]] > 0) {
cnt[v2[v2.size() - 1]]--;
v2.pop_back();
}
}
}
rep(i, v2.size()) { cout << v2[i] << " "; }
cout << endl;
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
int nextd[64][200010];
int A[400010];
vector<int> v[200010];
signed main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
rep(i, N) cin >> A[i];
rep(i, N) A[N + i] = A[i];
rep(i, N * 2) { v[A[i]].push_back(i); }
rep(i, 60) rep(j, 200010) nextd[i][j] = INF2;
// rep(i, N) {
// cout << i + 1 << ": ";
// rep(j, v[i + 1].size()) {
// cout << v[i + 1][j] << " ";
// }
// cout << endl;
// }
rep(i, N) {
auto head = lower_bound(v[A[i]].begin(), v[A[i]].end(), i);
auto nxt = head;
++nxt;
// cout << *head << " " << *nxt << endl;
nextd[0][i] = *nxt - *head + 1;
}
rep(i, 59) {
rep(j, N) {
if (nextd[i][j] != INF2) {
int nxt = j;
nxt += nextd[i][nxt];
nxt += nextd[i][nxt % N];
if (nxt - j <= N * K)
nextd[i + 1][j] = nxt - j;
}
}
}
int now = 0;
while (true) {
int nxt = now;
rep(i, 60) {
if (now + nextd[i][now % N] <= N * K) {
nxt = now + nextd[i][now % N];
} else {
break;
}
}
if (nxt == now)
break;
now = nxt;
// cout << now << " " << now % N << " "<< N * K - now << endl;
}
int s = now % N;
int c = N * K - now;
int cnt[200010] = {};
vector<int> v2;
rep(i, c) {
if (cnt[A[(s + i) % N]] == 0) {
cnt[A[(s + i) % N]]++;
v2.push_back(A[(s + i) % N]);
} else {
while (cnt[A[(s + i) % N]] > 0) {
cnt[v2[v2.size() - 1]]--;
v2.pop_back();
}
}
}
rep(i, v2.size()) { cout << v2[i] << " "; }
cout << endl;
return 0;
}
| replace | 69 | 70 | 69 | 70 | -11 | |
p02964 | C++ | Runtime Error | #include <cstdio>
#include <fstream>
#include <iostream>
using namespace std;
int a[500005], g[200005], ans[200005];
long long f[200005][70];
int main() {
int n = 0;
long long k = 0;
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), a[n + i] = a[i];
for (int i = 1; i <= n + n; i++) {
if (0 < g[a[i]] && g[a[i]] <= n)
f[g[a[i]]][0] = i - g[a[i]] + 1;
g[a[i]] = i;
}
for (int j = 1; j <= 65; j++)
for (int i = 1; i <= n; i++) {
int s = ((i + f[i][j - 1]) % n == 0) ? n : (i + f[i][j - 1]) % n;
if (f[i][j - 1] == -1 || f[s][j - 1] == -1)
f[i][j] = -1;
else
f[i][j] = f[i][j - 1] + f[s][j - 1];
if (f[i][j] < 0)
f[i][j] = -1;
}
long long c = 1;
for (int i = 65; i >= 0; i--) {
int s = (c % n == 0) ? n : c % n;
long long h = c + f[s][i];
if (h < 0 || h > n * k)
h = -1;
if (f[s][i] != -1 && h != -1)
c += f[s][i];
}
int cnt = 0;
c = n * k - c + 1;
if (c > n)
c = 0;
for (int i = 1; i <= n; i++)
g[i] = 0;
for (int i = n - c + 1; i <= n; i++)
if (g[a[i]])
while (g[a[i]])
g[ans[cnt--]] = 0;
else
g[a[i]] = 1, ans[++cnt] = a[i];
for (int i = 1; i <= cnt; i++)
printf("%d ", ans[i]);
return 0;
} | #include <cstdio>
#include <fstream>
#include <iostream>
using namespace std;
int a[500005], g[200005], ans[200005];
long long f[200005][70];
int main() {
int n = 0;
long long k = 0;
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), a[n + i] = a[i];
for (int i = 1; i <= n + n; i++) {
if (0 < g[a[i]] && g[a[i]] <= n)
f[g[a[i]]][0] = i - g[a[i]] + 1;
g[a[i]] = i;
}
for (int j = 1; j <= 65; j++)
for (int i = 1; i <= n; i++) {
int s = ((i + f[i][j - 1]) % n == 0) ? n : (i + f[i][j - 1]) % n;
if (f[i][j - 1] == -1 || f[s][j - 1] == -1)
f[i][j] = -1;
else
f[i][j] = f[i][j - 1] + f[s][j - 1];
if (f[i][j] < 0)
f[i][j] = -1;
}
long long c = 1;
for (int i = 65; i >= 0; i--) {
int s = (c % n == 0) ? n : c % n;
long long h = c + f[s][i];
if (h < 0 || h > n * k)
h = -1;
if (f[s][i] != -1 && h != -1)
c += f[s][i];
}
int cnt = 0;
c = n * k - c + 1;
if (c > n)
c = 0;
for (int i = 1; i <= n; i++)
g[a[i]] = 0;
for (int i = n - c + 1; i <= n; i++)
if (g[a[i]])
while (g[a[i]])
g[ans[cnt--]] = 0;
else
g[a[i]] = 1, ans[++cnt] = a[i];
for (int i = 1; i <= cnt; i++)
printf("%d ", ans[i]);
return 0;
} | replace | 41 | 42 | 41 | 42 | -11 | |
p02964 | C++ | Runtime Error | #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))
int firstappear[205000];
int lastappear[205000];
ll N, K;
ll A[205000];
l_l dp[205000][102];
vector<int> ans;
bool used[205000];
l_l now;
void f(int index) {
if (index == -1)
return;
if (dp[now.first][index].second + now.second < K) {
now.second += dp[now.first][index].second;
now.first = dp[now.first][index].first;
}
f(index - 1);
}
int main() {
cin >> N >> K;
for (int i = 0; i < N; i++)
cin >> A[i];
for (int val = 0; val <= 2e5; val++) {
firstappear[val] = -1;
lastappear[val] = -1;
}
for (int i = 0; i < N; i++) {
if (firstappear[A[i]] != -1) {
dp[lastappear[A[i]]][0].first = (i + 1) % N;
if (i == (N - 1))
dp[lastappear[A[i]]][0].second++;
lastappear[A[i]] = i;
continue;
} else {
firstappear[A[i]] = i;
lastappear[A[i]] = i;
continue;
}
}
for (int val = 1; val <= 2e5; val++) {
if (firstappear[val] == -1)
continue;
dp[lastappear[val]][0] = {(firstappear[val] + 1) % N,
1 + (firstappear[val] / (N - 1))};
}
/*
for(int i = 0; i < N; i++) {
cerr << i << " " << dp[i][0].first << " " << dp[i][0].second << endl;
}
*/
for (int timer = 1; timer <= 100; timer++) {
for (int index = 0; index < N; index++) {
dp[index][timer].second += dp[index][timer - 1].second;
int now = dp[index][timer - 1].first;
dp[index][timer].second += dp[now][timer - 1].second;
chmin(dp[index][timer].second, (ll)1e17);
dp[index][timer].first = dp[now][timer - 1].first;
}
}
now = {0, 0};
f(100);
// cerr << now.first << " " << now.second << endl;
while (now.second < K) {
int pos = now.first;
if (used[A[pos]]) {
while (used[A[pos]]) {
used[ans.back()] = false;
ans.pop_back();
}
} else {
used[A[pos]] = true;
ans.push_back(A[pos]);
}
now.first++;
if (now.first == N) {
now.first = 0;
now.second++;
}
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << 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))
int firstappear[205000];
int lastappear[205000];
ll N, K;
ll A[205000];
l_l dp[205000][102];
vector<int> ans;
bool used[205000];
l_l now;
void f(int index) {
if (index == -1)
return;
if (dp[now.first][index].second + now.second < K) {
now.second += dp[now.first][index].second;
now.first = dp[now.first][index].first;
}
f(index - 1);
}
int main() {
cin >> N >> K;
for (int i = 0; i < N; i++)
cin >> A[i];
for (int val = 0; val <= 2e5; val++) {
firstappear[val] = -1;
lastappear[val] = -1;
}
for (int i = 0; i < N; i++) {
if (firstappear[A[i]] != -1) {
dp[lastappear[A[i]]][0].first = (i + 1) % N;
if (i == (N - 1))
dp[lastappear[A[i]]][0].second++;
lastappear[A[i]] = i;
continue;
} else {
firstappear[A[i]] = i;
lastappear[A[i]] = i;
continue;
}
}
for (int val = 1; val <= 2e5; val++) {
if (firstappear[val] == -1)
continue;
dp[lastappear[val]][0] = {(firstappear[val] + 1) % N,
1 + ((firstappear[val] + 1) / (N))};
}
/*
for(int i = 0; i < N; i++) {
cerr << i << " " << dp[i][0].first << " " << dp[i][0].second << endl;
}
*/
for (int timer = 1; timer <= 100; timer++) {
for (int index = 0; index < N; index++) {
dp[index][timer].second += dp[index][timer - 1].second;
int now = dp[index][timer - 1].first;
dp[index][timer].second += dp[now][timer - 1].second;
chmin(dp[index][timer].second, (ll)1e17);
dp[index][timer].first = dp[now][timer - 1].first;
}
}
now = {0, 0};
f(100);
// cerr << now.first << " " << now.second << endl;
while (now.second < K) {
int pos = now.first;
if (used[A[pos]]) {
while (used[A[pos]]) {
used[ans.back()] = false;
ans.pop_back();
}
} else {
used[A[pos]] = true;
ans.push_back(A[pos]);
}
now.first++;
if (now.first == N) {
now.first = 0;
now.second++;
}
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
| replace | 70 | 71 | 70 | 71 | -11 | |
p02964 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
ll mpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = mpow(a, b / 2);
return memo * memo % MOD;
} else
return mpow(a, b - 1) * a % MOD;
}
ll lpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = lpow(a, b / 2);
return memo * memo;
} else
return lpow(a, b - 1) * a;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n) {
if (kaijo_memo.size() > n)
return kaijo_memo[n];
if (kaijo_memo.size() == 0)
kaijo_memo.push_back(1);
while (kaijo_memo.size() <= n)
kaijo_memo.push_back(kaijo_memo[kaijo_memo.size() - 1] * kaijo_memo.size() %
MOD);
return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n) {
if (gyaku_kaijo_memo.size() > n)
return gyaku_kaijo_memo[n];
if (gyaku_kaijo_memo.size() == 0)
gyaku_kaijo_memo.push_back(1);
while (gyaku_kaijo_memo.size() <= n)
gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size() - 1] *
mpow(gyaku_kaijo_memo.size(), MOD - 2) % MOD);
return gyaku_kaijo_memo[n];
}
ll nCr(ll n, ll r) {
if (n == r)
return 1; // 0個の丸と-1個の棒みたいな時に時に効く?不安.
if (n < r || r < 0)
return 0;
ll ret = 1;
ret *= kaijo(n);
ret %= MOD;
ret *= gyaku_kaijo(r);
ret %= MOD;
ret *= gyaku_kaijo(n - r);
ret %= MOD;
return ret;
}
bool used[(int)2e5 + 3];
void simulate(ll k, int now, vector<ll> &A) {
rep(i, 2e5 + 3) { used[i] = false; }
vector<ll> ans;
while (now < A.size()) {
ll a = A[now];
if (used[a]) {
while (used[a]) {
used[ans.back()] = false;
ans.pop_back();
}
} else {
ans.push_back(a);
used[a] = true;
}
now++;
}
rep(i, ans.size()) { cout << ans[i] + 1 << " "; }
cout << endl;
}
int main(void) {
fast_io cout << fixed << setprecision(15);
ll n, k;
cin >> n >> k;
vector<ll> A(n), Next(n), memo(n);
rep(i, n) {
cin >> A[i];
A[i]--;
}
REP(i, n) { memo[A[i]] = i; }
REP(i, n) {
Next[i] = memo[A[i]];
memo[A[i]] = i;
}
vector<bool> checked(n, false);
ll now = 0;
while (k >= 2) {
ll next = Next[now];
if (next <= now)
k--;
if (next == n - 1)
k--;
now = (next + 1) % n;
if (checked[next])
break;
checked[next] = true;
}
if (k == 0) {
return 0;
}
if (k == 1) {
simulate(k, now, A);
return 0;
}
ll goal = now;
ll count = 0;
while (k >= 2) {
ll next = Next[now];
if (next <= now)
count++;
if (next == n - 1)
count++;
now = (next + 1) % n;
if (now == goal)
break;
}
if (k == 0) {
return 0;
}
if (k == 1) {
simulate(k, now, A);
return 0;
}
k = k % count;
while (k >= 2) {
ll next = Next[now];
if (next <= now)
k--;
if (next == n - 1)
k--;
now = (next + 1) % n;
}
if (k == 0) {
return 0;
}
if (k == 1) {
simulate(k, now, A);
return 0;
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
ll mpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = mpow(a, b / 2);
return memo * memo % MOD;
} else
return mpow(a, b - 1) * a % MOD;
}
ll lpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = lpow(a, b / 2);
return memo * memo;
} else
return lpow(a, b - 1) * a;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n) {
if (kaijo_memo.size() > n)
return kaijo_memo[n];
if (kaijo_memo.size() == 0)
kaijo_memo.push_back(1);
while (kaijo_memo.size() <= n)
kaijo_memo.push_back(kaijo_memo[kaijo_memo.size() - 1] * kaijo_memo.size() %
MOD);
return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n) {
if (gyaku_kaijo_memo.size() > n)
return gyaku_kaijo_memo[n];
if (gyaku_kaijo_memo.size() == 0)
gyaku_kaijo_memo.push_back(1);
while (gyaku_kaijo_memo.size() <= n)
gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size() - 1] *
mpow(gyaku_kaijo_memo.size(), MOD - 2) % MOD);
return gyaku_kaijo_memo[n];
}
ll nCr(ll n, ll r) {
if (n == r)
return 1; // 0個の丸と-1個の棒みたいな時に時に効く?不安.
if (n < r || r < 0)
return 0;
ll ret = 1;
ret *= kaijo(n);
ret %= MOD;
ret *= gyaku_kaijo(r);
ret %= MOD;
ret *= gyaku_kaijo(n - r);
ret %= MOD;
return ret;
}
bool used[(int)2e5 + 3];
void simulate(ll k, int now, vector<ll> &A) {
rep(i, 2e5 + 3) { used[i] = false; }
vector<ll> ans;
while (now < A.size()) {
ll a = A[now];
if (used[a]) {
while (used[a]) {
used[ans.back()] = false;
ans.pop_back();
}
} else {
ans.push_back(a);
used[a] = true;
}
now++;
}
rep(i, ans.size()) { cout << ans[i] + 1 << " "; }
cout << endl;
}
int main(void) {
fast_io cout << fixed << setprecision(15);
ll n, k;
cin >> n >> k;
vector<ll> A(n), Next(n), memo((int)2e5 + 4);
rep(i, n) {
cin >> A[i];
A[i]--;
}
REP(i, n) { memo[A[i]] = i; }
REP(i, n) {
Next[i] = memo[A[i]];
memo[A[i]] = i;
}
vector<bool> checked(n, false);
ll now = 0;
while (k >= 2) {
ll next = Next[now];
if (next <= now)
k--;
if (next == n - 1)
k--;
now = (next + 1) % n;
if (checked[next])
break;
checked[next] = true;
}
if (k == 0) {
return 0;
}
if (k == 1) {
simulate(k, now, A);
return 0;
}
ll goal = now;
ll count = 0;
while (k >= 2) {
ll next = Next[now];
if (next <= now)
count++;
if (next == n - 1)
count++;
now = (next + 1) % n;
if (now == goal)
break;
}
if (k == 0) {
return 0;
}
if (k == 1) {
simulate(k, now, A);
return 0;
}
k = k % count;
while (k >= 2) {
ll next = Next[now];
if (next <= now)
k--;
if (next == n - 1)
k--;
now = (next + 1) % n;
}
if (k == 0) {
return 0;
}
if (k == 1) {
simulate(k, now, A);
return 0;
}
return 0;
}
| replace | 124 | 125 | 124 | 125 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);
using namespace std;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll N, K;
cin >> N >> K;
vector<int> A(N);
vector<int> spot(N, -1);
vector<vector<int>> data(2e5 + 1);
// data[i][j] := iがj番目に出現する時の座標 (0-indexed);
// spot[i] := i番目の数と同じ数が次に出現する時の座標(0-indexd);
rep(i, N) {
cin >> A[i];
data[A[i]].push_back(i);
}
rep(i, N) {
// i番目の数と同じ数が次に出現する座標を求めていく
int now = A[i];
for (auto x : data[now]) {
if (x > i) {
spot[i] = x;
break;
}
}
if (spot[i] == -1) {
// 右側にi番目の数と同じ数が出現しない場合は左側で1番最初に出現した数にする
for (auto x : data[now]) {
spot[i] = x;
break;
}
}
if (spot[i] == -1) {
// 左側にも右側にも出現しない場合は1周させる
spot[i] = i;
}
}
// どれだけの数を進んだら、sが空で開始地点が0になるかを調べる
ll dis = 0;
ll X = 0;
while (1) {
ll next;
// 現在地点はXで次にXと同じ数が出現する座標はspot[X]にある
if (spot[X] > X) {
// もしも右側にXと同じ数があった場合は
next = spot[X];
dis += next - X + 1;
X = next;
} else if (spot[X] <= X) {
// 左側にXと同じ数があった場合は
next = (spot[X] + N);
dis += next - X + 1;
X = next;
}
X++;
X %= N;
if (X == 0)
break;
}
ll length = ((N * K) % dis) / N;
vector<int> ans;
if (length == 0)
cout << "" << endl;
else {
length = length * N;
for (ll i = 0; i < length; i++) {
int next = spot[i % N] + (i / N) * N;
if (next % N <= i % N)
next += N;
if (next < length)
i = next;
else {
ans.push_back(A[i % N]);
}
}
}
for (auto x : ans)
cout << x << " ";
} | #include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);
using namespace std;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll N, K;
cin >> N >> K;
vector<int> A(N);
vector<int> spot(N, -1);
vector<vector<int>> data(2e5 + 1);
// data[i][j] := iがj番目に出現する時の座標 (0-indexed);
// spot[i] := i番目の数と同じ数が次に出現する時の座標(0-indexd);
rep(i, N) {
cin >> A[i];
data[A[i]].push_back(i);
}
rep(i, N) {
// i番目の数と同じ数が次に出現する座標を求めていく
int now = A[i];
for (auto x : data[now]) {
if (x > i) {
spot[i] = x;
break;
}
}
if (spot[i] == -1) {
// 右側にi番目の数と同じ数が出現しない場合は左側で1番最初に出現した数にする
for (auto x : data[now]) {
spot[i] = x;
break;
}
}
if (spot[i] == -1) {
// 左側にも右側にも出現しない場合は1周させる
spot[i] = i;
}
}
// どれだけの数を進んだら、sが空で開始地点が0になるかを調べる
ll dis = 0;
ll X = 0;
while (1) {
ll next;
// 現在地点はXで次にXと同じ数が出現する座標はspot[X]にある
if (spot[X] > X) {
// もしも右側にXと同じ数があった場合は
next = spot[X];
dis += next - X + 1;
X = next;
} else if (spot[X] <= X) {
// 左側にXと同じ数があった場合は
next = (spot[X] + N);
dis += next - X + 1;
X = next;
}
X++;
X %= N;
if (X == 0)
break;
}
ll length = ((N * K) % dis) / N;
vector<int> ans;
if (length == 0)
cout << "" << endl;
else {
length = length * N;
for (ll i = 0; i < length; i++) {
ll next = spot[i % N] + (i / N) * N;
if (next % N <= i % N)
next += N;
if (next < length)
i = next;
else {
ans.push_back(A[i % N]);
}
}
}
for (auto x : ans)
cout << x << " ";
} | replace | 79 | 80 | 79 | 80 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#define last last2
using namespace std;
typedef long long ll;
int f[200005][60];
ll g[200005][60];
int num[400005], last[200005];
void pre(int n) {
for (int i = 0; i < n; i++)
num[n + i] = num[i];
for (int i = 2 * n - 1; i >= 0; i--) {
if (i < n) {
f[i][0] = (last[num[i]] + 1) % n;
g[i][0] = last[num[i]] - i + 1;
}
last[num[i]] = i;
}
for (int i = 1; i < 60; i++)
for (int j = 0; j < n; j++) {
f[j][i] = f[f[j][i - 1]][i - 1];
g[j][i] = g[j][i - 1] + g[f[j][i - 1]][i - 1];
}
}
int query(ll k) {
int x = 0;
for (int i = 59; i >= 0; i--)
if (g[x][i] <= k) {
k -= g[x][i];
x = f[x][i];
}
return k;
}
int st[200005];
bool vis[200005];
int main() {
int n;
ll k;
scanf("%d%lld", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d", &num[i]);
pre(n);
int d = query(n * k), top = 0;
for (int i = n - d; i < n; i++) {
int x = num[i];
if (vis[x]) {
while (vis[x]) {
vis[st[top]] = 0;
top--;
}
} else {
vis[x] = 1;
st[++top] = x;
}
}
for (int i = 1; i <= top; i++)
printf("%d ", st[i]);
printf("\n");
return 0;
} | #include <bits/stdc++.h>
#define last last2
using namespace std;
typedef long long ll;
int f[200005][60];
ll g[200005][60];
int num[400005], last[200005];
void pre(int n) {
for (int i = 0; i < n; i++)
num[n + i] = num[i];
for (int i = 2 * n - 1; i >= 0; i--) {
if (i < n) {
f[i][0] = (last[num[i]] + 1) % n;
g[i][0] = last[num[i]] - i + 1;
}
last[num[i]] = i;
}
for (int i = 1; i < 60; i++)
for (int j = 0; j < n; j++) {
f[j][i] = f[f[j][i - 1]][i - 1];
g[j][i] = min(g[j][i - 1] + g[f[j][i - 1]][i - 1], (ll)1e18);
}
}
int query(ll k) {
int x = 0;
for (int i = 59; i >= 0; i--)
if (g[x][i] <= k) {
k -= g[x][i];
x = f[x][i];
}
return k;
}
int st[200005];
bool vis[200005];
int main() {
int n;
ll k;
scanf("%d%lld", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d", &num[i]);
pre(n);
int d = query(n * k), top = 0;
for (int i = n - d; i < n; i++) {
int x = num[i];
if (vis[x]) {
while (vis[x]) {
vis[st[top]] = 0;
top--;
}
} else {
vis[x] = 1;
st[++top] = x;
}
}
for (int i = 1; i <= top; i++)
printf("%d ", st[i]);
printf("\n");
return 0;
} | replace | 25 | 26 | 25 | 26 | -11 | |
p02964 | C++ | Runtime Error | /* cerberus97 - Hanit Banga */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define pb push_back
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 2e5 + 10;
int a[N], ctr[N], pos[N], nxt[N];
int g[N], depth[N], c_len, c_st;
bool seen[N];
void dfs(int u);
int main() {
fast_cin();
int n;
ll k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i];
a[i + n] = a[i];
}
for (int i = 2 * n - 1; i >= 0; --i) {
if (pos[a[i]]) {
nxt[i] = pos[a[i]];
} else {
nxt[i] = 2 * n;
}
pos[a[i]] = i;
}
g[n] = 0;
for (int i = n - 1; i >= 0; --i) {
if ((nxt[i] + 1) >= n) {
g[i] = nxt[i] + 1;
} else {
g[i] = g[nxt[i] + 1];
}
if (g[i] < 2 * n) {
g[i] %= n;
} else {
g[i] = n;
}
}
dfs(0);
--k;
int s = 0;
while (k) {
if (s == c_st) {
k %= c_len;
}
if (k) {
s = g[s];
--k;
}
}
vector<int> ans;
for (int i = s; i < n; ++i) {
if (ctr[a[i]]) {
while (ctr[a[i]]) {
--ctr[ans.back()];
ans.pop_back();
}
} else {
++ctr[a[i]];
ans.pb(a[i]);
}
}
for (auto &i : ans) {
cout << i << ' ';
}
}
void dfs(int u) {
seen[u] = true;
if (seen[g[u]]) {
c_st = g[u];
c_len = depth[u] - depth[g[u]] + 1;
return;
}
depth[g[u]] = depth[u] + 1;
dfs(g[u]);
} | /* cerberus97 - Hanit Banga */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define pb push_back
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 4e5 + 10;
int a[N], ctr[N], pos[N], nxt[N];
int g[N], depth[N], c_len, c_st;
bool seen[N];
void dfs(int u);
int main() {
fast_cin();
int n;
ll k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i];
a[i + n] = a[i];
}
for (int i = 2 * n - 1; i >= 0; --i) {
if (pos[a[i]]) {
nxt[i] = pos[a[i]];
} else {
nxt[i] = 2 * n;
}
pos[a[i]] = i;
}
g[n] = 0;
for (int i = n - 1; i >= 0; --i) {
if ((nxt[i] + 1) >= n) {
g[i] = nxt[i] + 1;
} else {
g[i] = g[nxt[i] + 1];
}
if (g[i] < 2 * n) {
g[i] %= n;
} else {
g[i] = n;
}
}
dfs(0);
--k;
int s = 0;
while (k) {
if (s == c_st) {
k %= c_len;
}
if (k) {
s = g[s];
--k;
}
}
vector<int> ans;
for (int i = s; i < n; ++i) {
if (ctr[a[i]]) {
while (ctr[a[i]]) {
--ctr[ans.back()];
ans.pop_back();
}
} else {
++ctr[a[i]];
ans.pb(a[i]);
}
}
for (auto &i : ans) {
cout << i << ' ';
}
}
void dfs(int u) {
seen[u] = true;
if (seen[g[u]]) {
c_st = g[u];
c_len = depth[u] - depth[g[u]] + 1;
return;
}
depth[g[u]] = depth[u] + 1;
dfs(g[u]);
} | replace | 28 | 29 | 28 | 29 | 0 | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
#define Mod 1000000007
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
#define INF 1ll << 59
#pragma GCC optimize("Ofast")
int main() {
ll i, j;
ll n, k;
cin >> n >> k;
ll a[2 * n + 1];
for (i = 1; i <= n; i++) {
cin >> a[i];
a[i + n] = a[i];
}
vector<ll> m(202020, 0);
vector<ll> v(n + 1);
for (i = 1; i <= 2 * n; i++) {
if (m[a[i]] == 0)
m[a[i]] = i;
else if (m[a[i]] <= n) {
v[m[a[i]]] = i - m[a[i]] + 1;
m[a[i]] = i;
}
}
ll po = 1 + v[1];
while (po % n != 1) {
if (po % n == 0)
po = po + v[n];
else
po = po + v[po % n];
}
ll loop = (po - 1) / n;
k %= loop;
po = 1;
ll prev;
while (po <= k * n) {
prev = po;
if (po % n == 0)
po = po + v[n];
else
po = po + v[po % n];
}
if (po == k * n + 1) {
return 0;
}
if (prev % n == 0)
prev = n;
else
prev = prev % n;
vector<ll> ans;
while (prev <= n) {
if (prev + v[prev] <= n + 1) {
prev = prev + v[prev];
} else {
ans.pb(a[prev]);
prev++;
}
}
if (ans.size() == 0)
return 0;
printf("%d", ans[0]);
for (i = 1; i < ans.size(); i++)
printf(" %d", ans[i]);
cout << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
#define Mod 1000000007
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
#define INF 1ll << 59
#pragma GCC optimize("Ofast")
int main() {
ll i, j;
ll n, k;
cin >> n >> k;
if (n == 1) {
ll x;
cin >> x;
if (k % 2 == 0)
return 0;
else {
cout << x << endl;
return 0;
}
}
ll a[2 * n + 1];
for (i = 1; i <= n; i++) {
cin >> a[i];
a[i + n] = a[i];
}
vector<ll> m(202020, 0);
vector<ll> v(n + 1);
for (i = 1; i <= 2 * n; i++) {
if (m[a[i]] == 0)
m[a[i]] = i;
else if (m[a[i]] <= n) {
v[m[a[i]]] = i - m[a[i]] + 1;
m[a[i]] = i;
}
}
ll po = 1 + v[1];
while (po % n != 1) {
if (po % n == 0)
po = po + v[n];
else
po = po + v[po % n];
}
ll loop = (po - 1) / n;
k %= loop;
po = 1;
ll prev;
while (po <= k * n) {
prev = po;
if (po % n == 0)
po = po + v[n];
else
po = po + v[po % n];
}
if (po == k * n + 1) {
return 0;
}
if (prev % n == 0)
prev = n;
else
prev = prev % n;
vector<ll> ans;
while (prev <= n) {
if (prev + v[prev] <= n + 1) {
prev = prev + v[prev];
} else {
ans.pb(a[prev]);
prev++;
}
}
if (ans.size() == 0)
return 0;
printf("%d", ans[0]);
for (i = 1; i < ans.size(); i++)
printf(" %d", ans[i]);
cout << endl;
return 0;
} | insert | 21 | 21 | 21 | 31 | TLE | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
using ll = long long;
// 1ブロックごとに必ず1回は全部消すことになる→全部消した後の始点の推移を書く?
vector<int> v[20001]; // v[n]には A_k = n となるkが入ってる,
// ソート済みなので二分探索で値がlog n
// v[i] の k = j の index を二分探索で取得
int biSearch(int i, int j) {
int lb = 0, ub = v[i].size() - 1;
while (v[i][ub] != j) {
int mid = (lb + ub) / 2;
if (v[i][mid] >= j) {
ub = mid;
} else {
lb = mid;
}
}
return ub;
}
int main() {
int N;
ll K;
cin >> N >> K;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
v[A[i]].push_back(i);
}
vector<int> ans; // 最初に1周するまで各kの出力の始点を保存(線形で出力が求まる)
int s = 0; // 始点
while (true) {
int index = biSearch(A[s], s);
if (index == v[A[s]].size() - 1) {
ans.push_back(s);
s = v[A[s]][0] + 1;
// 単独で末尾の場合
if (s == N) {
ans.push_back(-1);
break;
}
} else {
if (v[A[s]][index + 1] + 1 == N) {
ans.push_back(-1);
break;
} else {
s = v[A[s]][index + 1] + 1;
}
}
}
K = (K - 1) % ans.size();
int i = ans[K];
if (i != -1) {
while (i != N) {
int index = biSearch(A[i], i);
if (index != v[A[i]].size() - 1) {
i = v[A[i]][index + 1] + 1;
} else {
cout << A[i] << " ";
i++;
}
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
using ll = long long;
// 1ブロックごとに必ず1回は全部消すことになる→全部消した後の始点の推移を書く?
vector<int> v[200001]; // v[n]には A_k = n となるkが入ってる,
// ソート済みなので二分探索で値がlog n
// v[i] の k = j の index を二分探索で取得
int biSearch(int i, int j) {
int lb = 0, ub = v[i].size() - 1;
while (v[i][ub] != j) {
int mid = (lb + ub) / 2;
if (v[i][mid] >= j) {
ub = mid;
} else {
lb = mid;
}
}
return ub;
}
int main() {
int N;
ll K;
cin >> N >> K;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
v[A[i]].push_back(i);
}
vector<int> ans; // 最初に1周するまで各kの出力の始点を保存(線形で出力が求まる)
int s = 0; // 始点
while (true) {
int index = biSearch(A[s], s);
if (index == v[A[s]].size() - 1) {
ans.push_back(s);
s = v[A[s]][0] + 1;
// 単独で末尾の場合
if (s == N) {
ans.push_back(-1);
break;
}
} else {
if (v[A[s]][index + 1] + 1 == N) {
ans.push_back(-1);
break;
} else {
s = v[A[s]][index + 1] + 1;
}
}
}
K = (K - 1) % ans.size();
int i = ans[K];
if (i != -1) {
while (i != N) {
int index = biSearch(A[i], i);
if (index != v[A[i]].size() - 1) {
i = v[A[i]][index + 1] + 1;
} else {
cout << A[i] << " ";
i++;
}
}
}
return 0;
} | replace | 7 | 9 | 7 | 9 | 0 | |
p02964 | C++ | Runtime Error | #ifdef LOCAL
// #define _GLIBCXX_DEBUG
#endif
// #pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<char> Vc;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;
typedef vector<vector<char>> VVc;
typedef vector<vector<vector<ll>>> VVV;
typedef vector<vector<vector<vector<ll>>>> VVVV;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF = 1e18 + 18;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define Each(a, b) for (auto &a : b)
#define rEach(i, mp) for (auto i = mp.rbegin(); i != mp.rend(); ++i)
#ifdef LOCAL
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) \
cerr << #mp << ":" << endl; \
for (auto i = mp.begin(); i != mp.end(); ++i) { \
cerr << i->first << ":" << i->second << endl; \
}
#define dbgset(st) \
cerr << #st << ":" << endl; \
for (auto i = st.begin(); i != st.end(); ++i) { \
cerr << *i << " "; \
} \
cerr << endl;
#define dbgarr(n, m, arr) \
rep(i, n) { \
rep(j, m) { cerr << arr[i][j] << " "; } \
cerr << endl; \
}
#define dbgdp(n, arr) \
rep(i, n) { cerr << arr[i] << " "; } \
cerr << endl;
#else
#define dbg(...)
#define dbgmap(...)
#define dbgset(...)
#define dbgarr(...)
#define dbgdp(...)
#endif
#define out(a) cout << a << endl
#define out2(a, b) cout << a << " " << b << endl
#define vout(v) \
rep(i, v.size()) { cout << v[i] << " "; } \
cout << endl
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second
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;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T> istream &operator>>(istream &i, vector<T> &v) {
rep(j, v.size()) i >> v[j];
return i;
}
// 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 << " ";
}
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;
}
// mint
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime MOD
mint inv() const { return pow(MOD - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
typedef vector<mint> Vmint;
typedef vector<vector<mint>> VVmint;
typedef vector<vector<vector<mint>>> VVVmint;
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < MOD);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
int solve() {
ll n, k;
cin >> n >> k;
Vec a(n);
cin >> a;
ll mx = 200005;
mx = 10;
VV g(mx);
rep(i, n) { g[a[i]].pb(i); }
rep(i, n) { g[a[i]].pb(i + n); }
Vec pos(2 * n + 1, -1);
auto dfs = [&](auto dfs, ll x) -> ll {
if (pos[x] != -1)
return pos[x];
if (x == 2 * n)
return pos[x] = n;
auto itr = upper_bound(ALL(g[a[x % n]]), x);
if (itr == g[a[x % n]].end()) {
return pos[x] = x % n;
} else {
return pos[x] = dfs(dfs, *itr + 1);
}
};
rep(i, 2 * n + 1) { dfs(dfs, i); }
VV dp(45, Vec(n + 1));
rep(i, n + 1) { dp[0][i] = pos[i]; }
REP(j, 1, 45) {
rep(i, n + 1) { dp[j][i] = dp[j - 1][dp[j - 1][i]]; }
}
k--;
ll now = 0;
dbg(dp);
rep(bit, 45) {
if (k & (1LL << bit)) {
now = dp[bit][now];
}
}
set<ll> st;
Vec v;
REP(i, now, n) {
ll x = a[i];
if (st.count(x) > 0) {
while (v.back() != x) {
st.erase(v.back());
v.pop_back();
}
st.erase(x);
v.pop_back();
} else {
st.insert(x);
v.pb(x);
}
}
vout(v);
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
// ll T;
// cin>>T;
// while(T--)
solve();
}
| #ifdef LOCAL
// #define _GLIBCXX_DEBUG
#endif
// #pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<char> Vc;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;
typedef vector<vector<char>> VVc;
typedef vector<vector<vector<ll>>> VVV;
typedef vector<vector<vector<vector<ll>>>> VVVV;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF = 1e18 + 18;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define Each(a, b) for (auto &a : b)
#define rEach(i, mp) for (auto i = mp.rbegin(); i != mp.rend(); ++i)
#ifdef LOCAL
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) \
cerr << #mp << ":" << endl; \
for (auto i = mp.begin(); i != mp.end(); ++i) { \
cerr << i->first << ":" << i->second << endl; \
}
#define dbgset(st) \
cerr << #st << ":" << endl; \
for (auto i = st.begin(); i != st.end(); ++i) { \
cerr << *i << " "; \
} \
cerr << endl;
#define dbgarr(n, m, arr) \
rep(i, n) { \
rep(j, m) { cerr << arr[i][j] << " "; } \
cerr << endl; \
}
#define dbgdp(n, arr) \
rep(i, n) { cerr << arr[i] << " "; } \
cerr << endl;
#else
#define dbg(...)
#define dbgmap(...)
#define dbgset(...)
#define dbgarr(...)
#define dbgdp(...)
#endif
#define out(a) cout << a << endl
#define out2(a, b) cout << a << " " << b << endl
#define vout(v) \
rep(i, v.size()) { cout << v[i] << " "; } \
cout << endl
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second
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;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T> istream &operator>>(istream &i, vector<T> &v) {
rep(j, v.size()) i >> v[j];
return i;
}
// 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 << " ";
}
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;
}
// mint
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD)
x -= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime MOD
mint inv() const { return pow(MOD - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
typedef vector<mint> Vmint;
typedef vector<vector<mint>> VVmint;
typedef vector<vector<vector<mint>>> VVVmint;
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < MOD);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
int solve() {
ll n, k;
cin >> n >> k;
Vec a(n);
cin >> a;
ll mx = 200005;
// mx = 10;
VV g(mx);
rep(i, n) { g[a[i]].pb(i); }
rep(i, n) { g[a[i]].pb(i + n); }
Vec pos(2 * n + 1, -1);
auto dfs = [&](auto dfs, ll x) -> ll {
if (pos[x] != -1)
return pos[x];
if (x == 2 * n)
return pos[x] = n;
auto itr = upper_bound(ALL(g[a[x % n]]), x);
if (itr == g[a[x % n]].end()) {
return pos[x] = x % n;
} else {
return pos[x] = dfs(dfs, *itr + 1);
}
};
rep(i, 2 * n + 1) { dfs(dfs, i); }
VV dp(45, Vec(n + 1));
rep(i, n + 1) { dp[0][i] = pos[i]; }
REP(j, 1, 45) {
rep(i, n + 1) { dp[j][i] = dp[j - 1][dp[j - 1][i]]; }
}
k--;
ll now = 0;
dbg(dp);
rep(bit, 45) {
if (k & (1LL << bit)) {
now = dp[bit][now];
}
}
set<ll> st;
Vec v;
REP(i, now, n) {
ll x = a[i];
if (st.count(x) > 0) {
while (v.back() != x) {
st.erase(v.back());
v.pop_back();
}
st.erase(x);
v.pop_back();
} else {
st.insert(x);
v.pb(x);
}
}
vout(v);
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
// ll T;
// cin>>T;
// while(T--)
solve();
}
| replace | 189 | 190 | 189 | 190 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ff(i, x, y) for (int i = x; i <= y; i++)
#define fs(i, x, y) for (int i = x; i >= y; i--)
using namespace std;
typedef long long ll;
int n, vis[300010], a[200010], ans[400010];
ll dp[400010][60], sum, d;
int main() {
scanf("%d%lld", &n, &sum);
d = n * sum;
memset(vis, -1, sizeof(vis));
ff(i, 0, n - 1) {
scanf("%d", &a[i]);
a[i + n] = a[i];
}
ff(i, 0, 2 * n - 1) {
if (vis[a[i]] == -1) {
vis[a[i]] = i;
dp[i][0] = n;
} else {
dp[vis[a[i]]][0] = i - vis[a[i]];
vis[a[i]] = i;
dp[i][0] = n;
}
}
ff(i, 1, 59) {
ff(j, 0, n - 1) {
if (dp[j][i - 1] == -1 ||
dp[(j + dp[j][i - 1] + 1) % (ll)n][i - 1] == -1) {
dp[j][i] = -1;
} else
dp[j][i] = dp[j][i - 1] + dp[(j + dp[j][i - 1] + 1) % (ll)n][i - 1] + 1;
if (dp[j][i] >= d)
dp[j][i] = -1;
}
}
int now = 0;
ll up = d;
fs(i, 59, 0) {
if (dp[now][i] != -1) {
if (dp[now][i] + 1 <= up) {
up -= (dp[now][i] + 1);
now = (dp[now][i] + now + 1) % n;
}
}
}
int s = 0;
memset(vis, -1, sizeof(vis));
ff(i, n - up, n - 1) {
if (vis[a[i]] == -1) {
vis[a[i]] = i;
s++;
ans[s] = a[i];
} else {
while (ans[s] != a[i]) {
vis[ans[s]] = -1;
s--;
}
vis[ans[s]] = -1;
s--;
}
}
ff(i, 1, s) { printf("%d ", ans[i]); }
return 0;
}
| #include <bits/stdc++.h>
#define ff(i, x, y) for (int i = x; i <= y; i++)
#define fs(i, x, y) for (int i = x; i >= y; i--)
using namespace std;
typedef long long ll;
int n, vis[300010], a[400010], ans[200010];
ll dp[400010][60], sum, d;
int main() {
scanf("%d%lld", &n, &sum);
d = n * sum;
memset(vis, -1, sizeof(vis));
ff(i, 0, n - 1) {
scanf("%d", &a[i]);
a[i + n] = a[i];
}
ff(i, 0, 2 * n - 1) {
if (vis[a[i]] == -1) {
vis[a[i]] = i;
dp[i][0] = n;
} else {
dp[vis[a[i]]][0] = i - vis[a[i]];
vis[a[i]] = i;
dp[i][0] = n;
}
}
ff(i, 1, 59) {
ff(j, 0, n - 1) {
if (dp[j][i - 1] == -1 ||
dp[(j + dp[j][i - 1] + 1) % (ll)n][i - 1] == -1) {
dp[j][i] = -1;
} else
dp[j][i] = dp[j][i - 1] + dp[(j + dp[j][i - 1] + 1) % (ll)n][i - 1] + 1;
if (dp[j][i] >= d)
dp[j][i] = -1;
}
}
int now = 0;
ll up = d;
fs(i, 59, 0) {
if (dp[now][i] != -1) {
if (dp[now][i] + 1 <= up) {
up -= (dp[now][i] + 1);
now = (dp[now][i] + now + 1) % n;
}
}
}
int s = 0;
memset(vis, -1, sizeof(vis));
ff(i, n - up, n - 1) {
if (vis[a[i]] == -1) {
vis[a[i]] = i;
s++;
ans[s] = a[i];
} else {
while (ans[s] != a[i]) {
vis[ans[s]] = -1;
s--;
}
vis[ans[s]] = -1;
s--;
}
}
ff(i, 1, s) { printf("%d ", ans[i]); }
return 0;
}
| replace | 5 | 6 | 5 | 6 | -11 | |
p02964 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
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;
}
int main() {
int n;
ll k;
cin >> n >> k;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<vector<ll>> next(60, vector<ll>(n)); // ダブリング
vector<int> prev(200010, -1);
rep(i, 2) {
rep(j, n) {
if (prev[a[j]] != -1)
next[0][prev[a[j]] % n] =
n * i + j - prev[a[j]] + 1; // 同じ値までの距離+1
prev[a[j]] = n * i + j; // 座標
}
}
for (int i = 1; i < 60; i++) {
rep(j, n) {
next[i][j] = next[i - 1][j] + next[i - 1][(next[i - 1][j] + j) % n];
}
}
ll start = 0, sum = 0;
while (1) {
int power = 0;
rep(i, 60) {
if (next[i][start] + sum >= n * k) {
power = i - 1;
break;
}
}
if (power == -1)
break;
sum += next[power][start];
start = sum % n;
}
vector<bool> used(200010);
deque<int> ans;
for (int i = start; i < n; i++) { // 残りの長さはn以下
if (!used[a[i]]) {
ans.push_back(a[i]);
used[a[i]] = true;
} else {
while (1) {
int tmp = ans.back();
ans.pop_back();
if (tmp == a[i])
break;
}
}
}
while (!ans.empty()) {
printf("%d%c", ans.front(), ans.size() > 1 ? ' ' : '\n');
ans.pop_front();
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
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;
}
int main() {
int n;
ll k;
cin >> n >> k;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<vector<ll>> next(60, vector<ll>(n)); // ダブリング
vector<int> prev(200010, -1);
rep(i, 2) {
rep(j, n) {
if (prev[a[j]] != -1)
next[0][prev[a[j]] % n] =
n * i + j - prev[a[j]] + 1; // 同じ値までの距離+1
prev[a[j]] = n * i + j; // 座標
}
}
for (int i = 1; i < 60; i++) {
rep(j, n) {
next[i][j] = next[i - 1][j] + next[i - 1][(next[i - 1][j] + j) % n];
}
}
ll start = 0, sum = 0;
while (1) {
int power = 0;
rep(i, 60) {
if (next[i][start] + sum >= n * k) {
power = i - 1;
break;
}
}
if (power == -1)
break;
sum += next[power][start];
start = sum % n;
}
vector<bool> used(200010);
deque<int> ans;
for (int i = start; i < n; i++) { // 残りの長さはn以下
if (!used[a[i]]) {
ans.push_back(a[i]);
used[a[i]] = true;
} else {
while (1) {
int tmp = ans.back();
ans.pop_back();
used[tmp] = false;
if (tmp == a[i])
break;
}
}
}
while (!ans.empty()) {
printf("%d%c", ans.front(), ans.size() > 1 ? ' ' : '\n');
ans.pop_front();
}
return 0;
} | insert | 81 | 81 | 81 | 82 | 0 | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 200005;
int A[MAXN];
ll nxt[MAXN][64];
vector<int> p[MAXN];
bitset<MAXN> inv;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll N, K;
cin >> N >> K;
for (int i = 0; i < N; ++i)
cin >> A[i];
for (int i = 0; i < N; ++i)
p[A[i]].push_back(i);
for (int i = 0; i < MAXN; ++i) {
if (!p[i].empty()) {
for (int j = 0; j < p[i].size() - 1; ++j)
nxt[p[i][j]][0] = p[i][j + 1] + 1 - p[i][j];
nxt[p[i].back()][0] = p[i].front() + N + 1 - p[i].back();
}
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < 63; ++j) {
if (nxt[i][j] <= N * K)
nxt[i][j + 1] = nxt[i][j] + nxt[(i + nxt[i][j]) % N][j];
else
nxt[i][j + 1] = N * K + 1;
}
}
ll x = 0;
int cnt = 0;
while (1) {
int l = 0, h = 63;
while (l < h) {
int m = (l + h) >> 1;
x + nxt[x % N][m] >= N *K ? h = m : l = m + 1;
}
if (h <= 0)
break;
x += nxt[x % N][h - 1];
}
vector<int> v;
for (int i = x % N; i < N; ++i) {
if (inv[A[i]]) {
while (v.back() != A[i]) {
inv[v.back()] = 0;
v.pop_back();
}
inv[A[i]] = 0;
v.pop_back();
} else {
inv[A[i]] = 1;
v.push_back(A[i]);
}
}
for (auto &i : v)
cout << i << ' ';
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 200005;
int A[MAXN];
ll nxt[MAXN][64];
vector<int> p[MAXN];
bitset<MAXN> inv;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll N, K;
cin >> N >> K;
for (int i = 0; i < N; ++i)
cin >> A[i];
for (int i = 0; i < N; ++i)
p[A[i]].push_back(i);
for (int i = 0; i < MAXN; ++i) {
if (!p[i].empty()) {
for (int j = 0; j < p[i].size() - 1; ++j)
nxt[p[i][j]][0] = p[i][j + 1] + 1 - p[i][j];
nxt[p[i].back()][0] = p[i].front() + N + 1 - p[i].back();
}
}
for (int j = 0; j < 63; ++j) {
for (int i = 0; i < N; ++i) {
if (nxt[i][j] <= N * K)
nxt[i][j + 1] = nxt[i][j] + nxt[(i + nxt[i][j]) % N][j];
else
nxt[i][j + 1] = N * K + 1;
}
}
ll x = 0;
int cnt = 0;
while (1) {
int l = 0, h = 63;
while (l < h) {
int m = (l + h) >> 1;
x + nxt[x % N][m] >= N *K ? h = m : l = m + 1;
}
if (h <= 0)
break;
x += nxt[x % N][h - 1];
}
vector<int> v;
for (int i = x % N; i < N; ++i) {
if (inv[A[i]]) {
while (v.back() != A[i]) {
inv[v.back()] = 0;
v.pop_back();
}
inv[A[i]] = 0;
v.pop_back();
} else {
inv[A[i]] = 1;
v.push_back(A[i]);
}
}
for (auto &i : v)
cout << i << ' ';
} | replace | 29 | 31 | 29 | 31 | TLE | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int tot, n, a[N << 1], b[N], nex[N], bk[N], pre[N];
ll k, A, B;
bool vis[N];
ll getdis(int s, int t) {
if (s == 0)
s = n;
ll ans = 0;
while (s != t) {
ans += nex[s];
s = (s + nex[s]) % n;
if (s == 0)
s = n;
}
return ans;
}
void getAns(int now) {
for (int i = now; i <= n; i++) {
int x = a[i];
if (pre[x]) {
while (b[tot] != x)
pre[b[tot]]--, tot--;
pre[x]--;
tot--;
} else
b[++tot] = x, pre[x]++;
}
}
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), a[i + n] = a[i];
for (int i = n << 1; i >= 1; i--) {
if (i <= n)
nex[i] = bk[a[i]] - i + 1;
bk[a[i]] = i;
}
int now = 1;
while (!vis[now]) {
vis[now] = true;
now = (now + nex[now]) % n;
if (now == 0)
now = n;
}
A = getdis(1, now);
B = getdis(now + nex[now], now) + nex[now];
k *= n;
k--;
if (k <= A) {
int now = 1;
while (k >= ((nex[now] - now) % n + n) % n)
k -= ((nex[now] - now) % n + n) % n, now = nex[now];
getAns(now);
} else {
k -= A;
k %= B;
while (k >= nex[now]) {
k -= nex[now];
now = (now + nex[now]) % n;
if (now == 0)
now = n;
}
getAns(now);
}
for (int i = 1; i <= tot; i++)
printf(i == tot ? "%d\n" : "%d ", b[i]);
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int tot, n, a[N << 1], b[N], nex[N], bk[N], pre[N];
ll k, A, B;
bool vis[N];
ll getdis(int s, int t) {
if (s == 0)
s = n;
ll ans = 0;
while (s != t) {
ans += nex[s];
s = (s + nex[s]) % n;
if (s == 0)
s = n;
}
return ans;
}
void getAns(int now) {
for (int i = now; i <= n; i++) {
int x = a[i];
if (pre[x]) {
while (b[tot] != x)
pre[b[tot]]--, tot--;
pre[x]--;
tot--;
} else
b[++tot] = x, pre[x]++;
}
}
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), a[i + n] = a[i];
for (int i = n << 1; i >= 1; i--) {
if (i <= n)
nex[i] = bk[a[i]] - i + 1;
bk[a[i]] = i;
}
int now = 1;
while (!vis[now]) {
vis[now] = true;
now = (now + nex[now]) % n;
if (now == 0)
now = n;
}
A = getdis(1, now);
B = getdis(now + nex[now], now) + nex[now];
k *= n;
k--;
if (k <= A) {
int now = 1;
while (k >= nex[now]) {
k -= nex[now];
now = (now + nex[now]) % n;
if (now == 0)
now = n;
}
getAns(now);
} else {
k -= A;
k %= B;
while (k >= nex[now]) {
k -= nex[now];
now = (now + nex[now]) % n;
if (now == 0)
now = n;
}
getAns(now);
}
for (int i = 1; i <= tot; i++)
printf(i == tot ? "%d\n" : "%d ", b[i]);
}
| replace | 53 | 55 | 53 | 59 | TLE | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int ret = 0;
char c = getchar();
while (c < 48 || c > 57)
c = getchar();
while (c >= 48 && c <= 57)
ret = ret * 10 + c - 48, c = getchar();
return ret;
}
#define N 200093
long long k;
int T, f[N], g[N], u, v, st[N], t, n, a[N], h[N], tt, hh[N];
inline void pr(int u) {
while (u <= n) {
if (f[u] > u)
u = f[u];
else
printf("%d ", a[u]);
++u;
}
exit(0);
}
int main() {
n = read();
cin >> k;
for (int i = 1; i <= n; ++i)
a[i] = read();
for (int i = 1; i <= n; ++i) {
if (!h[a[i]])
h[a[i]] = i;
}
for (int i = n; i; --i) {
if (!g[a[i]])
f[i] = h[a[i]];
else
f[i] = g[a[i]];
g[a[i]] = i;
}
u = 1;
while (1) {
v = f[u] + 1;
if (v <= u + 1)
++T;
if (v == n + 1)
break;
u = v;
}
/*
T=1;
while(1){
if(f[u]<=u)++T;
v=f[u]+1;
if(v==n+1)break;
u=v;
}*/
u = 1;
--k;
k %= T;
if (!k)
pr(u);
while (1) {
v = f[u] + 1;
if (v > n)
v = 1;
if (v <= u + 1)
--k;
if (!k)
pr(v);
u = v;
}
} | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int ret = 0;
char c = getchar();
while (c < 48 || c > 57)
c = getchar();
while (c >= 48 && c <= 57)
ret = ret * 10 + c - 48, c = getchar();
return ret;
}
#define N 200093
long long k;
int T, f[N], g[N], u, v, st[N], t, n, a[N], h[N], tt, hh[N];
inline void pr(int u) {
while (u <= n) {
if (f[u] > u)
u = f[u];
else
printf("%d ", a[u]);
++u;
}
exit(0);
}
int main() {
n = read();
cin >> k;
for (int i = 1; i <= n; ++i)
a[i] = read();
for (int i = 1; i <= n; ++i) {
if (!h[a[i]])
h[a[i]] = i;
}
for (int i = n; i; --i) {
if (!g[a[i]])
f[i] = h[a[i]];
else
f[i] = g[a[i]];
g[a[i]] = i;
}
u = 1;
T = 1;
while (1) {
v = f[u] + 1;
if (v <= u + 1)
++T;
if (v == n + 1)
break;
u = v;
}
/*
T=1;
while(1){
if(f[u]<=u)++T;
v=f[u]+1;
if(v==n+1)break;
u=v;
}*/
u = 1;
--k;
k %= T;
if (!k)
pr(u);
while (1) {
v = f[u] + 1;
if (v > n)
v = 1;
if (v <= u + 1)
--k;
if (!k)
pr(v);
u = v;
}
} | insert | 45 | 45 | 45 | 46 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define pii pair<int, int>
#define fi first
#define se second
#define PB push_back
#define ll long long
using namespace std;
const int N = 200005;
int a[N], las[N], q[N], n;
ll b[N][60], k;
int main() {
scanf("%d%lld", &n, &k);
For(i, 1, n) scanf("%d", &a[i]), a[i + n] = a[i];
Rep(i, n + n, n + 1) las[a[i]] = i;
Rep(i, n, 1) {
b[i][0] = las[a[i]];
las[a[i]] = i;
}
For(i, 1, 58) For(j, 1, n) if (b[j][i - 1] == (1ll << 60)) b[j][i] = 1ll
<< 60;
else {
ll S = b[j][i - 1] / n, id = b[j][i - 1] + 1 - S * n;
b[j][i] = min(1ll << 60, b[id][i - 1] + S * n);
}
ll now = 0;
Rep(i, 58, 0) {
ll S = now / n, id = now + 1 - S * n;
if (b[id][i] + S * n <= k * n)
now = b[id][i] + S * n;
}
For(i, 1, N - 1) las[i] = 0;
for (now++; now <= k * n; now++) {
ll S = (now - 1) / n, id = now - S * n;
if (!las[a[id]])
q[++*q] = a[id], las[a[id]] = 1;
else {
for (; q[*q] != a[id]; --*q)
las[q[*q]] = 0;
--*q;
las[a[id]] = 0;
}
}
For(i, 1, *q) printf("%d ", q[i]);
puts("");
} | #include <bits/stdc++.h>
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define pii pair<int, int>
#define fi first
#define se second
#define PB push_back
#define ll long long
using namespace std;
const int N = 400005;
int a[N], las[N], q[N], n;
ll b[N][60], k;
int main() {
scanf("%d%lld", &n, &k);
For(i, 1, n) scanf("%d", &a[i]), a[i + n] = a[i];
Rep(i, n + n, n + 1) las[a[i]] = i;
Rep(i, n, 1) {
b[i][0] = las[a[i]];
las[a[i]] = i;
}
For(i, 1, 58) For(j, 1, n) if (b[j][i - 1] == (1ll << 60)) b[j][i] = 1ll
<< 60;
else {
ll S = b[j][i - 1] / n, id = b[j][i - 1] + 1 - S * n;
b[j][i] = min(1ll << 60, b[id][i - 1] + S * n);
}
ll now = 0;
Rep(i, 58, 0) {
ll S = now / n, id = now + 1 - S * n;
if (b[id][i] + S * n <= k * n)
now = b[id][i] + S * n;
}
For(i, 1, N - 1) las[i] = 0;
for (now++; now <= k * n; now++) {
ll S = (now - 1) / n, id = now - S * n;
if (!las[a[id]])
q[++*q] = a[id], las[a[id]] = 1;
else {
for (; q[*q] != a[id]; --*q)
las[q[*q]] = 0;
--*q;
las[a[id]] = 0;
}
}
For(i, 1, *q) printf("%d ", q[i]);
puts("");
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define per(i, s, e) for (int i = (int)(s)-1; i >= (int)(e); --i)
using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T> bool ChangeMax(T &a, const T &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> bool ChangeMin(T &a, const T &b) {
if (a <= b)
return false;
a = b;
return true;
}
template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
fill((U *)a, (U *)(a + N), v);
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v)
is >> e;
return is;
}
int main() {
int n;
cin >> n;
ll k;
cin >> k;
vector<int> v(n);
cin >> v;
vector<set<int>> sv(n + 1);
rep(i, 0, n) sv[v[i]].insert(i);
int cnt = 1;
{
int p = n - 1;
while (1) {
int val = v[p];
auto it = lower_bound(sv[val].begin(), sv[val].end(), p);
if (it == sv[val].begin()) {
cnt++;
it = sv[val].end();
}
it--;
p = *it;
if (p == 0)
break;
p--;
}
}
int t = k % cnt;
cnt = 1;
int p = 0;
vector<int> ans;
while (1) {
int val = v[p];
auto it = upper_bound(sv[val].begin(), sv[val].end(), p);
if (it == sv[val].end()) {
if (cnt >= t) {
p++;
ans.push_back(val);
if (p >= n)
break;
continue;
} else {
cnt++;
it = sv[val].begin();
}
}
p = *it;
p++;
if (p >= n) {
if (cnt >= t) {
break;
}
cnt++;
p = 0;
}
}
rep(i, 0, ans.size()) {
if (i)
cout << ' ';
cout << ans[i];
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define per(i, s, e) for (int i = (int)(s)-1; i >= (int)(e); --i)
using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T> bool ChangeMax(T &a, const T &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> bool ChangeMin(T &a, const T &b) {
if (a <= b)
return false;
a = b;
return true;
}
template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
fill((U *)a, (U *)(a + N), v);
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v)
is >> e;
return is;
}
int main() {
int n;
cin >> n;
ll k;
cin >> k;
vector<int> v(n);
cin >> v;
vector<set<int>> sv(200001);
rep(i, 0, n) sv[v[i]].insert(i);
int cnt = 1;
{
int p = n - 1;
while (1) {
int val = v[p];
auto it = lower_bound(sv[val].begin(), sv[val].end(), p);
if (it == sv[val].begin()) {
cnt++;
it = sv[val].end();
}
it--;
p = *it;
if (p == 0)
break;
p--;
}
}
int t = k % cnt;
cnt = 1;
int p = 0;
vector<int> ans;
while (1) {
int val = v[p];
auto it = upper_bound(sv[val].begin(), sv[val].end(), p);
if (it == sv[val].end()) {
if (cnt >= t) {
p++;
ans.push_back(val);
if (p >= n)
break;
continue;
} else {
cnt++;
it = sv[val].begin();
}
}
p = *it;
p++;
if (p >= n) {
if (cnt >= t) {
break;
}
cnt++;
p = 0;
}
}
rep(i, 0, ans.size()) {
if (i)
cout << ' ';
cout << ans[i];
}
cout << endl;
return 0;
}
| replace | 45 | 46 | 45 | 46 | 0 | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF 9223372036854775807
int n, k, a[200000], z, memo[200001];
vector<int> v[200001];
vector<int> ANS;
bool f;
signed main() {
cin >> n >> k;
rep(i, n) {
cin >> a[i];
v[a[i]].pb(i);
if (!memo[a[i]])
memo[a[i]] = i + 1;
}
while (n) {
if (z == n - 1)
f = true;
repo(i, z, n) {
auto itr = upper_bound(all(v[a[i]]), i);
if (itr != v[a[i]].end()) {
i = *itr;
if (i == n - 1)
f = true;
} else {
ANS.pb(i + 1);
z = memo[a[i]];
break;
}
}
if (f) {
int hoge = k % (ANS.size() + 1);
if (hoge == 0) {
cout << endl;
return 0;
}
hoge--;
z = ANS[hoge];
z--;
repo(i, z, n) {
auto itr = upper_bound(all(v[a[i]]), i);
if (itr != v[a[i]].end())
i = *itr;
else
cout << a[i] << " ";
}
cout << endl;
return 0;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF 9223372036854775807
int n, k, a[200000], z, memo[200001];
vector<int> v[200001];
vector<int> ANS;
bool f;
signed main() {
cin >> n >> k;
rep(i, n) {
cin >> a[i];
v[a[i]].pb(i);
if (!memo[a[i]])
memo[a[i]] = i + 1;
}
while (n) {
if (z >= n - 1)
f = true;
repo(i, z, n) {
auto itr = upper_bound(all(v[a[i]]), i);
if (itr != v[a[i]].end()) {
i = *itr;
if (i == n - 1)
f = true;
} else {
ANS.pb(i + 1);
z = memo[a[i]];
break;
}
}
if (f) {
int hoge = k % (ANS.size() + 1);
if (hoge == 0) {
cout << endl;
return 0;
}
hoge--;
z = ANS[hoge];
z--;
repo(i, z, n) {
auto itr = upper_bound(all(v[a[i]]), i);
if (itr != v[a[i]].end())
i = *itr;
else
cout << a[i] << " ";
}
cout << endl;
return 0;
}
}
}
| replace | 34 | 35 | 34 | 35 | TLE | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2001000;
int n, a[maxn], nxt[maxn], flag[maxn];
long long k, f[maxn][100];
vector<int> pos[maxn];
vector<int> ans;
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", a + i), pos[a[i]].push_back(i);
for (int i = 1; i <= 200000; i++)
for (int j = 0; j < (int)pos[i].size(); j++) {
if (j + 1 == (int)pos[i].size())
nxt[pos[i][j]] = pos[i][0] + n - pos[i][j] + 1;
else
nxt[pos[i][j]] = pos[i][j + 1] - pos[i][j] + 1;
}
for (int i = 1; i <= n; i++)
f[i][0] = nxt[i];
for (int j = 1; j < 70; j++)
for (int i = 1; i <= n; i++)
f[i][j] =
min(n * k + 1, f[i][j - 1] + f[(i + f[i][j - 1] - 1) % n + 1][j - 1]);
long long now = 1;
for (int i = 69; i >= 0; i--)
if (f[(now - 1) % n + 1][i] + now <= n * k + 1)
now += f[(now - 1) % n + 1][i];
for (long long i = now; i <= n * k; i++) {
int val = a[(i - 1) % n + 1];
if (flag[val]) {
while (ans[ans.size() - 1] != val)
ans.pop_back();
ans.pop_back();
flag[val] = 0;
} else {
ans.push_back(val);
flag[val] = 1;
}
}
for (int i = 0; i < (int)ans.size(); i++)
cout << ans[i] << ' ';
} | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2001000;
int n, a[maxn], nxt[maxn], flag[maxn];
long long k, f[maxn][100];
vector<int> pos[maxn];
vector<int> ans;
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", a + i), pos[a[i]].push_back(i);
for (int i = 1; i <= 200000; i++)
for (int j = 0; j < (int)pos[i].size(); j++) {
if (j + 1 == (int)pos[i].size())
nxt[pos[i][j]] = pos[i][0] + n - pos[i][j] + 1;
else
nxt[pos[i][j]] = pos[i][j + 1] - pos[i][j] + 1;
}
for (int i = 1; i <= n; i++)
f[i][0] = nxt[i];
for (int j = 1; j < 70; j++)
for (int i = 1; i <= n; i++)
f[i][j] =
min(n * k + 1, f[i][j - 1] + f[(i + f[i][j - 1] - 1) % n + 1][j - 1]);
long long now = 1;
for (int i = 69; i >= 0; i--)
if (f[(now - 1) % n + 1][i] + now <= n * k + 1)
now += f[(now - 1) % n + 1][i];
for (long long i = now; i <= n * k; i++) {
int val = a[(i - 1) % n + 1];
if (flag[val]) {
while (ans[ans.size() - 1] != val)
flag[ans[ans.size() - 1]] = 0, ans.pop_back();
ans.pop_back();
flag[val] = 0;
} else {
ans.push_back(val);
flag[val] = 1;
}
}
for (int i = 0; i < (int)ans.size(); i++)
cout << ans[i] << ' ';
} | replace | 32 | 33 | 32 | 33 | -11 | |
p02964 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
return (b < a) ? (a = b, 1) : 0;
}
int main() {
ll n, k;
cin >> n >> k;
vl a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vl s;
map<int, int> as;
vvi pos(10); /// 200001!!!
for (int i = 0; i < n; ++i) {
pos[a[i]].push_back(i);
}
int idx = 0;
ll ks = 0;
while (true) {
auto currpositr = find(pos[a[idx]].begin(), pos[a[idx]].end(), idx);
auto nextpositr = next(currpositr);
if (nextpositr == pos[a[idx]].end()) { // loop
nextpositr = pos[a[idx]].begin();
ks++;
}
idx = *nextpositr + 1;
if (idx == n) {
ks++;
idx = 0;
break;
}
}
k %= ks;
idx = 0;
ks = 0;
while (true) {
auto currpositr = find(pos[a[idx]].begin(), pos[a[idx]].end(), idx);
auto nextpositr = next(currpositr);
if (nextpositr == pos[a[idx]].end()) { // loop
nextpositr = pos[a[idx]].begin();
ks++;
if (ks == k) {
break;
}
}
idx = *nextpositr;
if (idx == n - 1) {
ks++;
idx = 0;
break;
} else {
idx++;
}
}
for (int i = idx; i < n; ++i) {
int j = i; // % n;
if (as[a[j]] == 0) {
s.push_back(a[j]);
as[a[j]]++;
} else {
while (true) {
if (s.back() == a[j]) {
as[s.back()]--;
s.pop_back();
break;
} else {
as[s.back()]--;
s.pop_back();
}
}
}
}
for (int i = 0; i < s.size(); ++i) {
cout << s[i];
if (i == s.size() - 1)
cout << endl;
else
cout << " ";
}
return 0;
} | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
return (b < a) ? (a = b, 1) : 0;
}
int main() {
ll n, k;
cin >> n >> k;
vl a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vl s;
map<int, int> as;
vvi pos(200001); /// 200001!!!
for (int i = 0; i < n; ++i) {
pos[a[i]].push_back(i);
}
int idx = 0;
ll ks = 0;
while (true) {
auto currpositr = find(pos[a[idx]].begin(), pos[a[idx]].end(), idx);
auto nextpositr = next(currpositr);
if (nextpositr == pos[a[idx]].end()) { // loop
nextpositr = pos[a[idx]].begin();
ks++;
}
idx = *nextpositr + 1;
if (idx == n) {
ks++;
idx = 0;
break;
}
}
k %= ks;
idx = 0;
ks = 0;
while (true) {
auto currpositr = find(pos[a[idx]].begin(), pos[a[idx]].end(), idx);
auto nextpositr = next(currpositr);
if (nextpositr == pos[a[idx]].end()) { // loop
nextpositr = pos[a[idx]].begin();
ks++;
if (ks == k) {
break;
}
}
idx = *nextpositr;
if (idx == n - 1) {
ks++;
idx = 0;
break;
} else {
idx++;
}
}
for (int i = idx; i < n; ++i) {
int j = i; // % n;
if (as[a[j]] == 0) {
s.push_back(a[j]);
as[a[j]]++;
} else {
while (true) {
if (s.back() == a[j]) {
as[s.back()]--;
s.pop_back();
break;
} else {
as[s.back()]--;
s.pop_back();
}
}
}
}
for (int i = 0; i < s.size(); ++i) {
cout << s[i];
if (i == s.size() - 1)
cout << endl;
else
cout << " ";
}
return 0;
} | replace | 27 | 28 | 27 | 28 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define mod 1000000007ll
#define INF 1000000000000000000ll
#define MAX_N 200010
int tab[MAX_N];
vector<int> v[MAX_N];
int skok[MAX_N][50];
int dist[MAX_N][50];
bool jest[MAX_N];
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n = 200000, k = 1000000000000ll;
cin >> n >> k;
for (int i = 0; i < n; i++) {
tab[i] = i;
cin >> tab[i];
v[tab[i]].push_back(i);
}
for (int i = 0; i < n; i++) {
int a = tab[i];
auto it = upper_bound(v[a].begin(), v[a].end(), i);
if (it == v[a].end()) {
dist[i][0] = n - i + *v[a].begin() + 1;
} else {
dist[i][0] = *it - i + 1;
}
// cout << dist[i][0] << " ";
}
// cout << endl;
for (int j = 1; j < 50; j++) {
for (int i = 0; i < n; i++) {
dist[i][j] = dist[i][j - 1] + dist[(i + dist[i][j - 1]) % n][j - 1];
}
}
int suma = 0;
bool czy = true;
while (czy) {
czy = false;
int gdzie = suma % n;
for (int j = 49; j >= 0; j--) {
if (suma + dist[gdzie][j] < n * k && suma + dist[gdzie][j] > suma) {
suma += dist[gdzie][j];
czy = true;
break;
}
}
}
int pocz = suma % n;
// cout << pocz;
vector<int> res;
for (int i = pocz; i < n; i++) {
if (!jest[tab[i]]) {
res.push_back(tab[i]);
jest[tab[i]] = true;
} else {
while (jest[tab[i]]) {
jest[res.back()] = false;
res.pop_back();
}
}
}
for (int i : res) {
cout << i << " ";
}
return 0;
}
/*
*/
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define mod 1000000007ll
#define INF 1000000000000000000ll
#define MAX_N 200010
int tab[MAX_N];
vector<int> v[MAX_N];
int skok[MAX_N][50];
int dist[MAX_N][50];
bool jest[MAX_N];
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n = 200000, k = 1000000000000ll;
cin >> n >> k;
for (int i = 0; i < n; i++) {
tab[i] = i;
cin >> tab[i];
v[tab[i]].push_back(i);
}
for (int i = 0; i < n; i++) {
int a = tab[i];
auto it = upper_bound(v[a].begin(), v[a].end(), i);
if (it == v[a].end()) {
dist[i][0] = n - i + *v[a].begin() + 1;
} else {
dist[i][0] = *it - i + 1;
}
// cout << dist[i][0] << " ";
}
// cout << endl;
for (int j = 1; j < 50; j++) {
for (int i = 0; i < n; i++) {
int a = dist[i][j - 1], b = dist[(i + dist[i][j - 1]) % n][j - 1];
dist[i][j] = a;
if (a + b > a && a + b > b) {
dist[i][j] = a + b;
}
}
}
int suma = 0;
bool czy = true;
while (czy) {
czy = false;
int gdzie = suma % n;
for (int j = 49; j >= 0; j--) {
if (suma + dist[gdzie][j] < n * k && suma + dist[gdzie][j] > suma) {
suma += dist[gdzie][j];
czy = true;
break;
}
}
}
int pocz = suma % n;
// cout << pocz;
vector<int> res;
for (int i = pocz; i < n; i++) {
if (!jest[tab[i]]) {
res.push_back(tab[i]);
jest[tab[i]] = true;
} else {
while (jest[tab[i]]) {
jest[res.back()] = false;
res.pop_back();
}
}
}
for (int i : res) {
cout << i << " ";
}
return 0;
}
/*
*/
| replace | 45 | 46 | 45 | 50 | -11 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repr(i, n) for (ll i = (n); i >= 0; --i)
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define FORR(i, m, n) for (ll i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(25);
ll n, k;
cin >> n >> k;
vector<ll> a(n);
rep(i, n) cin >> a[i];
map<ll, ll> mp;
rep(i, n) mp[a[i]] = -1;
vector<vector<ll>> dp;
vector<ll> res(n);
rep(i, 2 * n) {
if (mp[a[i % n]] != -1 && mp[a[i % n]] < n)
res[mp[a[i % n]]] = i - mp[a[i % n]] + 1;
mp[a[i % n]] = i;
}
dp.push_back(res);
while (res[0] < n * k) {
vector<ll> nxt(n, 0);
rep(i, n) nxt[i] = res[i] + res[(res[i] + i) % n];
res = nxt;
dp.push_back(res);
}
ll m = dp.size();
ll pos = 0;
repr(i, m - 1) {
if (pos + dp[i][pos % n] < n * k)
pos += dp[i][pos % n];
}
pos %= n;
stack<ll> st;
rep(i, n) mp[a[i]] = 0;
FOR(i, pos, n) {
// cout << i << ' ' << mp[a[i]] << '\n';
if (mp[a[i]] == 1) {
while (!st.empty() && st.top() != a[i]) {
mp[st.top()] = 0;
st.pop();
}
mp[a[i]] = 0;
st.pop();
} else {
st.push(a[i]);
mp[a[i]] = 1;
}
}
if (st.empty()) {
return 0;
}
vector<ll> ans;
while (!st.empty()) {
ans.push_back(st.top());
st.pop();
}
reverse(ans.begin(), ans.end());
rep(i, ans.size()) cout << ans[i] << ' ';
cout << '\n';
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repr(i, n) for (ll i = (n); i >= 0; --i)
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define FORR(i, m, n) for (ll i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(25);
ll n, k;
cin >> n >> k;
vector<ll> a(n);
rep(i, n) cin >> a[i];
if (n % 2 == 0) {
bool f = true;
rep(i, n / 2) {
if (a[2 * i] != a[2 * i + 1]) {
f = false;
}
}
if (f)
return 0;
}
map<ll, ll> mp;
rep(i, n) mp[a[i]] = -1;
vector<vector<ll>> dp;
vector<ll> res(n);
rep(i, 2 * n) {
if (mp[a[i % n]] != -1 && mp[a[i % n]] < n)
res[mp[a[i % n]]] = i - mp[a[i % n]] + 1;
mp[a[i % n]] = i;
}
dp.push_back(res);
while (res[0] < n * k) {
vector<ll> nxt(n, 0);
rep(i, n) nxt[i] = res[i] + res[(res[i] + i) % n];
res = nxt;
dp.push_back(res);
}
ll m = dp.size();
ll pos = 0;
repr(i, m - 1) {
if (pos + dp[i][pos % n] < n * k)
pos += dp[i][pos % n];
}
pos %= n;
stack<ll> st;
rep(i, n) mp[a[i]] = 0;
FOR(i, pos, n) {
// cout << i << ' ' << mp[a[i]] << '\n';
if (mp[a[i]] == 1) {
while (!st.empty() && st.top() != a[i]) {
mp[st.top()] = 0;
st.pop();
}
mp[a[i]] = 0;
st.pop();
} else {
st.push(a[i]);
mp[a[i]] = 1;
}
}
if (st.empty()) {
return 0;
}
vector<ll> ans;
while (!st.empty()) {
ans.push_back(st.top());
st.pop();
}
reverse(ans.begin(), ans.end());
rep(i, ans.size()) cout << ans[i] << ' ';
cout << '\n';
return 0;
}
| insert | 42 | 42 | 42 | 52 | 0 | |
p02964 | C++ | Runtime Error | #pragma GCC optimize("Ofast")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
using namespace std;
#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i, n) for (long long i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (long long i = (n)-1; i >= 0; i--)
#define foreach(it, ar) \
for (typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++)
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define fill0(ar) fill((ar), 0)
#define fillinf(ar, n) fr(i, 0, (n)) ar[i] = INF
#define debug(x) cout << #x << ": " << x << endl
#define arr1d(a, n) \
cout << #a << " : "; \
fr(_, 1, n) cout << a[_] << ' '; \
cout << endl;
#define arr1d0(a, n) \
cout << #a << " : "; \
rep(_, n) cout << a[_] << ' '; \
cout << endl;
#define arr2d(a, n, m) \
cout << #a << " :" << endl; \
fr(_, 1, n) { \
fr(__, 1, m) cout << a[_][__] << ' '; \
cout << endl; \
}
#define arr2d0(a, n, m) \
cout << #a << " :" << endl; \
rep(_, n) { \
rep(__, m) cout << a[_][__] << ' '; \
cout << endl; \
}
/*Author Ritick Goenka || ritick(codechef) ||ritick(codeforces) */
/*IIT Roorkee = <3 */
#define ull unsigned long long
#define ld long double
#define ui unsigned int
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define y0 yyyyyy0
auto clk = clock();
#define y1 yyyyyy1
#define setb __builtin_popcount
#define BIT(n) (1 << (n))
#define SQR(x) ((x) * (x))
#define CUBE(x) ((x) * (x) * (x))
#define LSOne(S) (S) & (-S)
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<string> vs;
template <typename T> inline T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// template<typename T>inline T lcm(T a, T b){return (a * b) / gcd(a, b);}
template <typename T> string toStr(T x) {
stringstream st;
st << x;
string s;
st >> s;
return s;
}
template <class T> void splitStr(const string &s, vector<T> &out) {
istringstream in(s);
out.clear();
copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
inline int two(int n) { return 1 << n; }
inline int isOnBit(int n, int b) { return (n >> b) & 1; }
inline void onBit(int &n, int b) { n |= two(b); }
inline void offBit(int &n, int b) { n &= ~two(b); }
inline int lastBit(int n) { return n & (-n); }
inline int cntBit(int n) {
int res = 0;
while (n && ++res)
n -= n & (-n);
return res;
}
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};
#define INP "test.inp"
#define OUT "test.out"
#define PI 3.1415926535897932385
#define INF 10000000000000000ll
#define EPS 1e-7
#define MAXN 100000
#define MOD 998244353
#define dec decr
#define endl '\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll gcd(ll a, ll b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
ll modexp(ll x, ll y, ll p) {
ll res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
map<ll, ll> m;
const ll N = 2005;
int f[N];
int l[N];
int st[N];
bool pre[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed;
cout << setprecision(9);
#ifdef rg
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n;
cin >> n;
ll k;
cin >> k;
vector<ll> v;
v.pb(0);
rep(i, n) {
ll x;
cin >> x;
v.pb(x);
if (f[x] == 0) {
f[x] = i + 1;
}
if (l[x] != 0) {
m[l[x]] = i + 1;
}
l[x] = i + 1;
}
for (ll i = 1; i <= n; i++) {
if (l[v[i]] != 0)
m[l[v[i]]] = f[v[i]];
}
// debug(v[3]);
ll z = 1;
ll cur = 0;
bool fl = 1;
while (cur != 1) {
if (fl) {
fl = 0;
cur = 1;
}
ll pt = m[cur];
if (pt <= cur) {
z++;
}
pt++;
cur = pt;
if (cur > n) {
cur = 1;
}
// debug(z);
}
// debug(z);
k = k % z;
z = 1;
stack<ll> ans;
while (z < k) {
ll pt = m[cur];
if (pt <= cur) {
z++;
}
pt++;
cur = pt;
if (cur > n) {
cur = 1;
}
}
// debug(cur);
for (ll i = cur; i <= n; i++) {
if (pre[v[i]] == 0) {
ans.push(v[i]);
pre[v[i]] = 1;
} else {
while (pre[v[i]]) {
ll x = ans.top();
ans.pop();
pre[x] = 0;
}
}
}
vector<ll> an;
while (!ans.empty()) {
an.pb(ans.top());
ans.pop();
}
reverse(all(an));
for (auto x : an) {
cout << x << ' ';
}
#ifdef rg
cout << endl
<< endl
<< "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;
#endif
return 0;
} | #pragma GCC optimize("Ofast")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
using namespace std;
#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i, n) for (long long i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (long long i = (n)-1; i >= 0; i--)
#define foreach(it, ar) \
for (typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++)
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define fill0(ar) fill((ar), 0)
#define fillinf(ar, n) fr(i, 0, (n)) ar[i] = INF
#define debug(x) cout << #x << ": " << x << endl
#define arr1d(a, n) \
cout << #a << " : "; \
fr(_, 1, n) cout << a[_] << ' '; \
cout << endl;
#define arr1d0(a, n) \
cout << #a << " : "; \
rep(_, n) cout << a[_] << ' '; \
cout << endl;
#define arr2d(a, n, m) \
cout << #a << " :" << endl; \
fr(_, 1, n) { \
fr(__, 1, m) cout << a[_][__] << ' '; \
cout << endl; \
}
#define arr2d0(a, n, m) \
cout << #a << " :" << endl; \
rep(_, n) { \
rep(__, m) cout << a[_][__] << ' '; \
cout << endl; \
}
/*Author Ritick Goenka || ritick(codechef) ||ritick(codeforces) */
/*IIT Roorkee = <3 */
#define ull unsigned long long
#define ld long double
#define ui unsigned int
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define y0 yyyyyy0
auto clk = clock();
#define y1 yyyyyy1
#define setb __builtin_popcount
#define BIT(n) (1 << (n))
#define SQR(x) ((x) * (x))
#define CUBE(x) ((x) * (x) * (x))
#define LSOne(S) (S) & (-S)
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<string> vs;
template <typename T> inline T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// template<typename T>inline T lcm(T a, T b){return (a * b) / gcd(a, b);}
template <typename T> string toStr(T x) {
stringstream st;
st << x;
string s;
st >> s;
return s;
}
template <class T> void splitStr(const string &s, vector<T> &out) {
istringstream in(s);
out.clear();
copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
inline int two(int n) { return 1 << n; }
inline int isOnBit(int n, int b) { return (n >> b) & 1; }
inline void onBit(int &n, int b) { n |= two(b); }
inline void offBit(int &n, int b) { n &= ~two(b); }
inline int lastBit(int n) { return n & (-n); }
inline int cntBit(int n) {
int res = 0;
while (n && ++res)
n -= n & (-n);
return res;
}
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};
#define INP "test.inp"
#define OUT "test.out"
#define PI 3.1415926535897932385
#define INF 10000000000000000ll
#define EPS 1e-7
#define MAXN 100000
#define MOD 998244353
#define dec decr
#define endl '\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll gcd(ll a, ll b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
ll modexp(ll x, ll y, ll p) {
ll res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
map<ll, ll> m;
const ll N = 2000005;
int f[N];
int l[N];
int st[N];
bool pre[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed;
cout << setprecision(9);
#ifdef rg
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n;
cin >> n;
ll k;
cin >> k;
vector<ll> v;
v.pb(0);
rep(i, n) {
ll x;
cin >> x;
v.pb(x);
if (f[x] == 0) {
f[x] = i + 1;
}
if (l[x] != 0) {
m[l[x]] = i + 1;
}
l[x] = i + 1;
}
for (ll i = 1; i <= n; i++) {
if (l[v[i]] != 0)
m[l[v[i]]] = f[v[i]];
}
// debug(v[3]);
ll z = 1;
ll cur = 0;
bool fl = 1;
while (cur != 1) {
if (fl) {
fl = 0;
cur = 1;
}
ll pt = m[cur];
if (pt <= cur) {
z++;
}
pt++;
cur = pt;
if (cur > n) {
cur = 1;
}
// debug(z);
}
// debug(z);
k = k % z;
z = 1;
stack<ll> ans;
while (z < k) {
ll pt = m[cur];
if (pt <= cur) {
z++;
}
pt++;
cur = pt;
if (cur > n) {
cur = 1;
}
}
// debug(cur);
for (ll i = cur; i <= n; i++) {
if (pre[v[i]] == 0) {
ans.push(v[i]);
pre[v[i]] = 1;
} else {
while (pre[v[i]]) {
ll x = ans.top();
ans.pop();
pre[x] = 0;
}
}
}
vector<ll> an;
while (!ans.empty()) {
an.pb(ans.top());
ans.pop();
}
reverse(all(an));
for (auto x : an) {
cout << x << ' ';
}
#ifdef rg
cout << endl
<< endl
<< "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;
#endif
return 0;
} | replace | 138 | 139 | 138 | 139 | 0 | |
p02964 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define all(s) s.begin(), s.end()
#define fo(i, start, finish) for (int i = start; i <= finish; i++)
#define y1 kdfjsalsdkf
const long long MXN = 2e5 + 10;
const long long MNN = 5e3 + 1;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long long OO = 1e9 + 500;
typedef long long llong;
typedef unsigned long long ullong;
using namespace std;
llong n, k, b[MXN], pos[MXN], d[64][MXN], contain[MXN];
vector<llong> v;
int main() {
ios;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> b[i];
b[i + n] = b[i];
}
for (int i = 2 * n - 1; i >= 0; i--) {
if (i < n)
d[0][i] = pos[b[i]] - i + 1;
pos[b[i]] = i;
}
for (int i = 1; i <= 60; i++) {
for (int j = 0; j < n; j++) {
d[i][j] = min(INF, d[i - 1][j] + d[i - 1][(j + d[i - 1][j]) % n]);
}
}
llong cur = 0;
for (int i = 60; i >= 0; i--) {
if (cur + d[i][cur % n] <= n * k)
cur += d[i][cur % n];
}
for (; cur < n * k; cur++) {
if (contain[b[cur % n]] == 0) {
v.pb(b[cur % n]);
contain[b[cur % n]]++;
} else {
while (contain[b[cur % n]] != 0) {
contain[v[v.size() - 1]]--;
v.pop_back();
}
}
}
for (int i = 0; i < v.size(); i++) {
cout << v[i] << ' ';
}
return 0;
}
//
//
//
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define all(s) s.begin(), s.end()
#define fo(i, start, finish) for (int i = start; i <= finish; i++)
#define y1 kdfjsalsdkf
const long long MXN = 2e5 + 10;
const long long MNN = 5e3 + 1;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long long OO = 1e9 + 500;
typedef long long llong;
typedef unsigned long long ullong;
using namespace std;
llong n, k, b[MXN * 2], pos[MXN], d[64][MXN], contain[MXN];
vector<llong> v;
int main() {
ios;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> b[i];
b[i + n] = b[i];
}
for (int i = 2 * n - 1; i >= 0; i--) {
if (i < n)
d[0][i] = pos[b[i]] - i + 1;
pos[b[i]] = i;
}
for (int i = 1; i <= 60; i++) {
for (int j = 0; j < n; j++) {
d[i][j] = min(INF, d[i - 1][j] + d[i - 1][(j + d[i - 1][j]) % n]);
}
}
llong cur = 0;
for (int i = 60; i >= 0; i--) {
if (cur + d[i][cur % n] <= n * k)
cur += d[i][cur % n];
}
for (; cur < n * k; cur++) {
if (contain[b[cur % n]] == 0) {
v.pb(b[cur % n]);
contain[b[cur % n]]++;
} else {
while (contain[b[cur % n]] != 0) {
contain[v[v.size() - 1]]--;
v.pop_back();
}
}
}
for (int i = 0; i < v.size(); i++) {
cout << v[i] << ' ';
}
return 0;
}
//
//
//
| replace | 44 | 45 | 44 | 45 | -11 | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n;
long long k;
int a[maxn << 1], nxt[maxn], b[maxn << 1];
bool inq[maxn];
long long vis[maxn];
vector<pair<long long, int>> v;
vector<int> u;
int main() {
cin >> n >> k;
k *= n;
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
a[i + n] = a[i];
}
memset(nxt, -1, sizeof(nxt));
memset(b, -1, sizeof(b));
for (int i = (n << 1) - 1; ~i; --i) {
b[i] = nxt[a[i]] + 1;
nxt[a[i]] = i;
}
// for (int i = 0; i < n; ++i) printf("a[%d]=%d, b[i]=%d\n", i, a[i],
//b[i]);
memset(vis, -1, sizeof(vis));
int i = 0;
long long loop = 0;
while (1) {
if (~vis[i])
break;
vis[i] = loop;
v.push_back(make_pair(vis[i], i));
// printf("%d(%d): %lld\n", i, a[i], loop);
loop += b[i] - i;
i = b[i] % n;
}
loop -= vis[i];
// printf("i=%d, loop=%d\n", i, loop);
if (k >= vis[i])
k = (k - vis[i]) % loop + vis[i];
int pos = (--upper_bound(v.begin(), v.end(), make_pair(k, 0)))->second;
// printf("pos=%d\n", pos);
for (int j = vis[pos] + 1, l = pos; j <= k; ++j) {
if (inq[a[l]]) {
while (inq[a[l]]) {
inq[u.back()] = false;
u.pop_back();
}
} else {
u.push_back(a[l]);
inq[a[l]] = true;
}
l = (l + 1) % n;
// for (int _ = 0; _ < u.size(); ++_) printf("%d ", u[_]);
//puts("");
}
for (int j = 0; j < u.size(); ++j)
printf("%d ", u[j]);
puts("");
}
| #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n;
long long k;
int a[maxn << 1], nxt[maxn], b[maxn << 1];
bool inq[maxn];
long long vis[maxn];
vector<pair<long long, int>> v;
vector<int> u;
int main() {
cin >> n >> k;
k *= n;
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
a[i + n] = a[i];
}
memset(nxt, -1, sizeof(nxt));
memset(b, -1, sizeof(b));
for (int i = (n << 1) - 1; ~i; --i) {
b[i] = nxt[a[i]] + 1;
nxt[a[i]] = i;
}
// for (int i = 0; i < n; ++i) printf("a[%d]=%d, b[i]=%d\n", i, a[i],
//b[i]);
memset(vis, -1, sizeof(vis));
int i = 0;
long long loop = 0;
while (1) {
if (~vis[i])
break;
vis[i] = loop;
v.push_back(make_pair(vis[i], i));
// printf("%d(%d): %lld\n", i, a[i], loop);
loop += b[i] - i;
i = b[i] % n;
}
loop -= vis[i];
// printf("i=%d, loop=%d\n", i, loop);
if (k >= vis[i])
k = (k - vis[i]) % loop + vis[i];
int pos = (--upper_bound(v.begin(), v.end(), make_pair(k, 0)))->second;
// printf("pos=%d\n", pos);
for (long long j = vis[pos] + 1, l = pos; j <= k; ++j) {
if (inq[a[l]]) {
while (inq[a[l]]) {
inq[u.back()] = false;
u.pop_back();
}
} else {
u.push_back(a[l]);
inq[a[l]] = true;
}
l = (l + 1) % n;
// for (int _ = 0; _ < u.size(); ++_) printf("%d ", u[_]);
//puts("");
}
for (int j = 0; j < u.size(); ++j)
printf("%d ", u[j]);
puts("");
}
| replace | 49 | 50 | 49 | 50 | TLE | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;
typedef signed long long ll;
mt19937 mt(time(nullptr));
// uniform_int_distribution< int > rand_int(0, 10) // rand[0, 10] inclusive
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed;
ll n, k;
cin >> n >> k;
vector<ll> a(n);
for (ll i = 0; i < n; ++i)
cin >> a[i];
unordered_map<ll, ll> near;
vector<ll> cstartidx(n + 1, n);
// cstartidx[n-1] = n-1;
// near[a[n-1]] = n-1;
for (ll i = n - 1; i >= 0; --i) {
if (near.count(a[i]) > 0) {
cstartidx[i] = cstartidx[near[a[i]] + 1];
} else
cstartidx[i] = i;
// cerr << cstartidx[i] << " ";
near[a[i]] = i;
}
// cerr << endl;
vector<ll> nstartidx(n);
for (ll i = 0; i < n; ++i) {
if (cstartidx[i] == n)
nstartidx[i] = n;
else
nstartidx[i] = (cstartidx[near[a[cstartidx[i]]] + 1]);
// cerr << nstartidx[i] << " ";
}
// cerr << endl;
ul start = 0;
vector<ll> v(n + 1, -1);
bool l_flg = false;
for (ll i = 0; i < k - 1; ++i) {
if (start == n) {
start = 0;
continue;
}
v[start] = i;
/*
if (!l_flg)
cerr << i << " " << start << endl;
*/
start = nstartidx[start];
if (!l_flg) {
if (v[start] >= 0) {
l_flg = true;
k -= (k - 2 - i) / (i + 1 - v[start]) * (i + 1 - v[start]);
// cerr << start << ":" << v[start] << " " << i << endl;
}
}
}
vector<ll> s;
unordered_map<ll, ll> cnt;
for (ll i = start; i < n; ++i) {
if (cnt.count(a[i]) == 0 || cnt[a[i]] == 0) {
s.push_back(a[i]);
cnt[a[i]] = 1;
} else {
while (cnt[a[i]] > 0) {
cnt[s[s.size() - 1]]--;
s.pop_back();
}
}
}
if (s.size() == 0)
return 0;
for (ll i = 0; i < s.size(); ++i)
cout << s[i] << " ";
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;
typedef signed long long ll;
mt19937 mt(time(nullptr));
// uniform_int_distribution< int > rand_int(0, 10) // rand[0, 10] inclusive
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed;
ll n, k;
cin >> n >> k;
vector<ll> a(n);
for (ll i = 0; i < n; ++i)
cin >> a[i];
unordered_map<ll, ll> near;
vector<ll> cstartidx(n + 1, n);
// cstartidx[n-1] = n-1;
// near[a[n-1]] = n-1;
for (ll i = n - 1; i >= 0; --i) {
if (near.count(a[i]) > 0) {
cstartidx[i] = cstartidx[near[a[i]] + 1];
} else
cstartidx[i] = i;
// cerr << cstartidx[i] << " ";
near[a[i]] = i;
}
// cerr << endl;
vector<ll> nstartidx(n);
for (ll i = 0; i < n; ++i) {
if (cstartidx[i] == n)
nstartidx[i] = n;
else
nstartidx[i] = (cstartidx[near[a[cstartidx[i]]] + 1]);
// cerr << nstartidx[i] << " ";
}
// cerr << endl;
ul start = 0;
vector<ll> v(n + 1, -1);
bool l_flg = false;
for (ll i = 0; i < k - 1; ++i) {
if (start == n) {
k -= (k - 2 - i) / (i + 1) * (i + 1);
start = 0;
continue;
}
v[start] = i;
/*
if (!l_flg)
cerr << i << " " << start << endl;
*/
start = nstartidx[start];
if (!l_flg) {
if (v[start] >= 0) {
l_flg = true;
k -= (k - 2 - i) / (i + 1 - v[start]) * (i + 1 - v[start]);
// cerr << start << ":" << v[start] << " " << i << endl;
}
}
}
vector<ll> s;
unordered_map<ll, ll> cnt;
for (ll i = start; i < n; ++i) {
if (cnt.count(a[i]) == 0 || cnt[a[i]] == 0) {
s.push_back(a[i]);
cnt[a[i]] = 1;
} else {
while (cnt[a[i]] > 0) {
cnt[s[s.size() - 1]]--;
s.pop_back();
}
}
}
if (s.size() == 0)
return 0;
for (ll i = 0; i < s.size(); ++i)
cout << s[i] << " ";
cout << endl;
return 0;
}
| insert | 44 | 44 | 44 | 45 | TLE | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 10;
int a[maxn], n, r[maxn], c[maxn], f[maxn], d[maxn];
ll k;
bool vis[maxn];
int ask() {
int l = 1, cost = 0;
while (1) {
cost += c[l];
l = r[l];
if (l == n)
return cost + 1;
l++;
if (l == n + 1)
l = 1, cost++;
}
return cost + 1;
}
bool is[maxn];
void print(int i) {
stack<int> P, Q;
for (; i <= n; i++) {
if (!is[a[i]]) {
P.push(a[i]);
is[a[i]] = 1;
} else {
while (1) {
int v = P.top();
is[a[v]] = 0;
P.pop();
if (v == a[i])
break;
}
}
}
while (!P.empty()) {
Q.push(P.top());
P.pop();
}
while (!Q.empty()) {
printf("%d ", Q.top());
Q.pop();
}
}
void work(int K) {
int l = 1, cost = 0;
while (1) {
if (cost + c[l] >= k) {
print(l);
return;
}
cost += c[l];
l = r[l];
l++;
if (l == n + 1) {
if (cost + 1 >= k) {
printf("\n");
return;
}
l = 1, cost++;
}
}
}
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (vis[a[i]] == 0) {
vis[a[i]] = 1;
f[a[i]] = i;
}
}
for (int i = n; i >= 1; i--) {
if (d[a[i]]) {
r[i] = d[a[i]];
} else {
r[i] = f[a[i]];
c[i] = 1;
}
d[a[i]] = i;
}
int mod = ask();
k %= mod;
// if(k==0)
// printf("\n");
// else
{ work(k); }
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 10;
int a[maxn], n, r[maxn], c[maxn], f[maxn], d[maxn];
ll k;
bool vis[maxn];
int ask() {
int l = 1, cost = 0;
while (1) {
cost += c[l];
l = r[l];
if (l == n)
return cost + 1;
l++;
if (l == n + 1)
l = 1, cost++;
}
return cost + 1;
}
bool is[maxn];
void print(int i) {
stack<int> P, Q;
for (; i <= n; i++) {
if (!is[a[i]]) {
P.push(a[i]);
is[a[i]] = 1;
} else {
while (1) {
int v = P.top();
is[v] = 0;
P.pop();
if (v == a[i])
break;
}
}
}
while (!P.empty()) {
Q.push(P.top());
P.pop();
}
while (!Q.empty()) {
printf("%d ", Q.top());
Q.pop();
}
}
void work(int K) {
int l = 1, cost = 0;
while (1) {
if (cost + c[l] >= k) {
print(l);
return;
}
cost += c[l];
l = r[l];
l++;
if (l == n + 1) {
if (cost + 1 >= k) {
printf("\n");
return;
}
l = 1, cost++;
}
}
}
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (vis[a[i]] == 0) {
vis[a[i]] = 1;
f[a[i]] = i;
}
}
for (int i = n; i >= 1; i--) {
if (d[a[i]]) {
r[i] = d[a[i]];
} else {
r[i] = f[a[i]];
c[i] = 1;
}
d[a[i]] = i;
}
int mod = ask();
k %= mod;
// if(k==0)
// printf("\n");
// else
{ work(k); }
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
for (T i = a; i != b; ++i)
cout << (*i) << " ";
cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, const T &b) {
if (a < b)
a = b;
}
int nextInt() {
int x;
scanf("%d", &x);
return x;
}
ll nextLong() {
ll x;
scanf("%lld", &x);
return x;
}
int main2() {
int N = nextInt();
ll K = nextLong();
vector<int> A(N);
REP(i, N) A[i] = nextInt();
ll P = N * K;
const int L = 50;
vector<ll> nxt[L];
REP(i, L) nxt[i] = vector<ll>(N);
map<int, int> last;
for (int i = 2 * N - 1; i >= 0; i--) {
if (i < N) {
nxt[0][i] = last[A[i]] - i + 1;
}
last[A[i % N]] = i;
}
for (int l = 1; l < L; l++) {
REP(i, N) {
nxt[l][i] = nxt[l - 1][(i + nxt[l - 1][i]) % N] + nxt[l - 1][i];
}
}
ll pos = 0;
for (;;) {
int l = L - 1;
while (l >= 0 && nxt[l][pos % N] + pos > P)
l--;
if (l == -1)
break;
pos += nxt[l][pos % N];
}
vector<int> stack;
set<int> in;
for (; pos < P; pos++) {
if (in.count(A[pos % N])) {
do {
in.erase(stack.back());
stack.pop_back();
} while (in.count(A[pos % N]));
} else {
stack.push_back(A[pos % N]);
in.insert(A[pos % N]);
}
}
pv(ALL(stack));
return 0;
}
int main() {
#ifdef LOCAL
for (; !cin.eof(); cin >> ws)
#endif
main2();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
for (T i = a; i != b; ++i)
cout << (*i) << " ";
cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, const T &b) {
if (a < b)
a = b;
}
int nextInt() {
int x;
scanf("%d", &x);
return x;
}
ll nextLong() {
ll x;
scanf("%lld", &x);
return x;
}
int main2() {
int N = nextInt();
ll K = nextLong();
vector<int> A(N);
REP(i, N) A[i] = nextInt();
ll P = N * K;
const int L = 40;
vector<ll> nxt[L];
REP(i, L) nxt[i] = vector<ll>(N);
map<int, int> last;
for (int i = 2 * N - 1; i >= 0; i--) {
if (i < N) {
nxt[0][i] = last[A[i]] - i + 1;
}
last[A[i % N]] = i;
}
for (int l = 1; l < L; l++) {
REP(i, N) {
nxt[l][i] = nxt[l - 1][(i + nxt[l - 1][i]) % N] + nxt[l - 1][i];
}
}
ll pos = 0;
for (;;) {
int l = L - 1;
while (l >= 0 && nxt[l][pos % N] + pos > P)
l--;
if (l == -1)
break;
pos += nxt[l][pos % N];
}
vector<int> stack;
set<int> in;
for (; pos < P; pos++) {
if (in.count(A[pos % N])) {
do {
in.erase(stack.back());
stack.pop_back();
} while (in.count(A[pos % N]));
} else {
stack.push_back(A[pos % N]);
in.insert(A[pos % N]);
}
}
pv(ALL(stack));
return 0;
}
int main() {
#ifdef LOCAL
for (; !cin.eof(); cin >> ws)
#endif
main2();
return 0;
} | replace | 44 | 45 | 44 | 45 | 0 | |
p02964 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop \
char nyaa; \
cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int n, pos;
ll k, T = 1;
vector<vector<int>> table;
vector<int> as, used, route = {};
vector<P> to;
void solve() {
cin >> n >> k;
table.resize(1000000, {});
used.resize(n + 1, 0);
to.resize(n + 1, P(0, 0));
rep(i, n) {
int a;
cin >> a;
as.push_back(a - 1);
table[a - 1].push_back(i);
}
rep(i, n) {
// cout << as[i] << " " << used[as[i]] << endl;
to[i].first = table[as[i]][(used[as[i]] + 1) % table[as[i]].size()] + 1;
if (used[as[i]] + 1 >= table[as[i]].size()) {
to[i].second = 1;
}
used[as[i]] += 1;
}
pos = 0;
while (pos != n) {
route.push_back(pos);
T += to[pos].second;
pos = to[pos].first;
}
route.push_back(n);
// cout << " " << endl;
// rep(i,n) cout << to[i].first << " " << to[i].second << endl;
// rep(i,route.size()) cout << route[i] << " ";
// cout << " " << endl;
// cout << T << endl;
int a = 0, pos = 0;
while (a < (k - 1) % T) {
P p = to[pos];
pos = p.first;
a += p.second;
// cout << pos << " " << a << endl;
}
Rep(i, pos, n) {
if (to[i].second)
cout << as[i] + 1 << " ";
else
i = to[i].first - 1;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
} | #include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop \
char nyaa; \
cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int n, pos;
ll k, T = 1;
vector<vector<int>> table;
vector<int> as, used, route = {};
vector<P> to;
void solve() {
cin >> n >> k;
table.resize(1000000, {});
used.resize(1000000, 0);
to.resize(n + 1, P(0, 0));
rep(i, n) {
int a;
cin >> a;
as.push_back(a - 1);
table[a - 1].push_back(i);
}
rep(i, n) {
// cout << as[i] << " " << used[as[i]] << endl;
to[i].first = table[as[i]][(used[as[i]] + 1) % table[as[i]].size()] + 1;
if (used[as[i]] + 1 >= table[as[i]].size()) {
to[i].second = 1;
}
used[as[i]] += 1;
}
pos = 0;
while (pos != n) {
route.push_back(pos);
T += to[pos].second;
pos = to[pos].first;
}
route.push_back(n);
// cout << " " << endl;
// rep(i,n) cout << to[i].first << " " << to[i].second << endl;
// rep(i,route.size()) cout << route[i] << " ";
// cout << " " << endl;
// cout << T << endl;
int a = 0, pos = 0;
while (a < (k - 1) % T) {
P p = to[pos];
pos = p.first;
a += p.second;
// cout << pos << " " << a << endl;
}
Rep(i, pos, n) {
if (to[i].second)
cout << as[i] + 1 << " ";
else
i = to[i].first - 1;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
} | replace | 48 | 49 | 48 | 49 | 0 | |
p02964 | C++ | Runtime Error | #pragma GCC optimize("O3")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#include <stdio.h>
#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to... Red
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
inline bool isvowel(char c) {
c = tolower(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u')
return 1;
return 0;
}
const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 2e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int n;
ll k;
int lst[N];
int a[N];
int in[N];
int nxt[N];
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
lst[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
nxt[lst[a[i]]] = i;
lst[a[i]] = i;
}
int p = 1;
vector<int> pos;
while (p <= n) {
if (nxt[p] <= p) {
pos.pb(p);
}
p = nxt[p] + 1;
}
k = (k - 1) % (1 + pos.size());
vector<int> ans;
for (int i = pos[k]; i <= n; i++) {
if (in[a[i]]) {
while (ans.back() != a[i]) {
in[ans.back()] = 0;
ans.pop_back();
}
in[ans.back()] = 0;
ans.pop_back();
} else {
ans.pb(a[i]);
in[a[i]] = 1;
}
}
for (auto x : ans)
cout << x << ' ';
return Accepted;
}
// B...a | #pragma GCC optimize("O3")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#include <stdio.h>
#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to... Red
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
inline bool isvowel(char c) {
c = tolower(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u')
return 1;
return 0;
}
const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 2e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int n;
ll k;
int lst[N];
int a[N];
int in[N];
int nxt[N];
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
lst[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
nxt[lst[a[i]]] = i;
lst[a[i]] = i;
}
int p = 1;
vector<int> pos;
while (p <= n) {
if (nxt[p] <= p) {
pos.pb(p);
}
p = nxt[p] + 1;
}
pos.pb(1);
k = (k - 1) % pos.size();
vector<int> ans;
for (int i = pos[k]; i <= n; i++) {
if (in[a[i]]) {
while (ans.back() != a[i]) {
in[ans.back()] = 0;
ans.pop_back();
}
in[ans.back()] = 0;
ans.pop_back();
} else {
ans.pb(a[i]);
in[a[i]] = 1;
}
}
for (auto x : ans)
cout << x << ' ';
return Accepted;
}
// B...a | replace | 80 | 81 | 80 | 82 | 0 | |
p02964 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define int long long
#define lint long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const lint inf = 1e18 + 7;
const int MOD = 1000000007;
struct Node {
lint no;
lint ind;
vector<P> nxt;
void input(int i, int in_ind) {
no = i;
nxt.push_back({in_ind, 0});
this->ind = in_ind;
}
};
signed main() {
lint n, K;
cin >> n >> K;
vector<Node> node(n);
unordered_map<int, int> memo;
rep(i, n) {
int a;
cin >> a;
node[i].input(a, i);
if (a == node[0].no && i > 0) {
if (node[0].nxt.size() == 1) {
if (i == n - 1) {
node[0].nxt.push_back({0, 1});
} else
node[0].nxt.push_back({i + 1, 0});
}
}
}
if (n == 1) {
if (K % 2 != 0) {
cout << node[0].no << endl;
return 0;
} else {
return 0;
}
}
if (node[0].nxt.size() == 1) {
node[0].nxt.push_back({1, 1});
}
rep(i, n) {
if (memo[node[i].no] == 0) {
memo[node[i].no] = i;
} else {
int buf = memo[node[i].no];
memo[node[i].no] = i;
if (node[buf].nxt.size() == 1) {
if (i == n - 1) {
node[buf].nxt.push_back({0, 1});
} else
node[buf].nxt.push_back({i + 1, 0});
}
}
}
rep(i, n) {
if (memo[node[i].no] == 0) {
memo[node[i].no] = i;
} else {
int buf = memo[node[i].no];
memo[node[i].no] = i;
if (node[buf].nxt.size() == 1) {
if (i == n - 1) {
node[buf].nxt.push_back({0, 2});
} else
node[buf].nxt.push_back({i + 1, 1});
}
}
}
int sup = 0;
rep1(k, 200) {
int mx = 0;
rep(i, n) {
node[i].nxt.push_back(
{node[node[i].nxt[k].first].nxt[k].first,
node[node[i].nxt[k].first].nxt[k].second + node[i].nxt[k].second});
mx = max(mx, node[i].nxt[k].second +
node[node[i].nxt[k].first].nxt[k].second);
}
if (mx > K) {
sup = k;
// break;
}
}
rep(i, n) {
// printf("i = %lld, nxt = %lld\n",i,node[i].nxt[1].first);
}
int k = K - 1;
int cur = 0;
// return 0;
while (1) {
if (k <= 1) {
break;
}
for (int kkk = 0; kkk < 1; ++kkk) {
int right = 100;
int left = 0;
while (right - left > 1) {
int mid = (right + left) / 2;
if (node[cur].nxt[mid].second <= k) {
left = mid;
} else
right = mid;
}
int i = left;
if (node[cur].nxt[i].second <= k) {
k -= node[cur].nxt[i].second;
cur = node[cur].nxt[i].first;
break;
}
}
}
vector<int> res;
for (cur; cur < n; ++cur) {
res.push_back(node[cur].no);
}
if (k == 1) {
rep(i, n) { res.push_back(node[i].no); }
}
unordered_map<int, int> hoge;
int nr = res.size();
/*
rep(i,nr){
cout<<res[i]<<" ";
}
cout<<endl;*/
rep(i, nr) { hoge[res[i]] += 1; }
int last = inf;
rep(i, nr) {
if (last == inf) {
if (hoge[res[i]] == 1) {
cout << res[i] << " ";
} else {
last = res[i];
}
hoge[res[i]] -= 1;
} else if (last != inf && res[i] != last) {
hoge[res[i]] -= 1;
} else if (last != inf && res[i] == last) {
last = inf;
hoge[res[i]] -= 1;
}
}
cout << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define int long long
#define lint long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const lint inf = 1e18 + 7;
const int MOD = 1000000007;
struct Node {
lint no;
lint ind;
vector<P> nxt;
void input(int i, int in_ind) {
no = i;
nxt.push_back({in_ind, 0});
this->ind = in_ind;
}
};
signed main() {
lint n, K;
cin >> n >> K;
vector<Node> node(n);
unordered_map<int, int> memo;
rep(i, n) {
int a;
cin >> a;
node[i].input(a, i);
if (a == node[0].no && i > 0) {
if (node[0].nxt.size() == 1) {
if (i == n - 1) {
node[0].nxt.push_back({0, 1});
} else
node[0].nxt.push_back({i + 1, 0});
}
}
}
if (n == 1) {
if (K % 2 != 0) {
cout << node[0].no << endl;
return 0;
} else {
return 0;
}
}
if (node[0].nxt.size() == 1) {
node[0].nxt.push_back({1, 1});
}
rep(i, n) {
if (memo[node[i].no] == 0) {
memo[node[i].no] = i;
} else {
int buf = memo[node[i].no];
memo[node[i].no] = i;
if (node[buf].nxt.size() == 1) {
if (i == n - 1) {
node[buf].nxt.push_back({0, 1});
} else
node[buf].nxt.push_back({i + 1, 0});
}
}
}
rep(i, n) {
if (memo[node[i].no] == 0) {
memo[node[i].no] = i;
} else {
int buf = memo[node[i].no];
memo[node[i].no] = i;
if (node[buf].nxt.size() == 1) {
if (i == n - 1) {
node[buf].nxt.push_back({0, 2});
} else
node[buf].nxt.push_back({i + 1, 1});
}
}
}
int sup = 0;
rep1(k, 100) {
int mx = 0;
rep(i, n) {
node[i].nxt.push_back(
{node[node[i].nxt[k].first].nxt[k].first,
node[node[i].nxt[k].first].nxt[k].second + node[i].nxt[k].second});
mx = max(mx, node[i].nxt[k].second +
node[node[i].nxt[k].first].nxt[k].second);
}
if (mx > K) {
sup = k;
// break;
}
}
rep(i, n) {
// printf("i = %lld, nxt = %lld\n",i,node[i].nxt[1].first);
}
int k = K - 1;
int cur = 0;
// return 0;
while (1) {
if (k <= 1) {
break;
}
for (int kkk = 0; kkk < 1; ++kkk) {
int right = 100;
int left = 0;
while (right - left > 1) {
int mid = (right + left) / 2;
if (node[cur].nxt[mid].second <= k) {
left = mid;
} else
right = mid;
}
int i = left;
if (node[cur].nxt[i].second <= k) {
k -= node[cur].nxt[i].second;
cur = node[cur].nxt[i].first;
break;
}
}
}
vector<int> res;
for (cur; cur < n; ++cur) {
res.push_back(node[cur].no);
}
if (k == 1) {
rep(i, n) { res.push_back(node[i].no); }
}
unordered_map<int, int> hoge;
int nr = res.size();
/*
rep(i,nr){
cout<<res[i]<<" ";
}
cout<<endl;*/
rep(i, nr) { hoge[res[i]] += 1; }
int last = inf;
rep(i, nr) {
if (last == inf) {
if (hoge[res[i]] == 1) {
cout << res[i] << " ";
} else {
last = res[i];
}
hoge[res[i]] -= 1;
} else if (last != inf && res[i] != last) {
hoge[res[i]] -= 1;
} else if (last != inf && res[i] == last) {
last = inf;
hoge[res[i]] -= 1;
}
}
cout << endl;
return 0;
} | replace | 91 | 92 | 91 | 92 | TLE | |
p02964 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
int A[400000];
ll d[61][200000];
int pos[200001];
int main() {
ll N, K;
cin >> N >> K;
for (int i = 0; i < N; i++) {
cin >> A[i];
A[i + N] = A[i];
}
for (int i = 2 * N - 1; i >= 0; i--) {
if (i < N)
d[0][i] = pos[A[i]] - i + 1;
pos[A[i]] = i;
}
for (int i = 1; i <= 60; i++) {
for (int j = 0; j < N; j++) {
d[i][j] = min(INF, d[i - 1][j] + d[i - 1][(j + d[i - 1][j]) % N]);
}
}
ll now = 0;
for (int i = 0; i <= 60; i++) {
if (now + d[i][now % N] <= N * K)
now += d[i][now % N];
}
deque<int> q;
set<int> S;
for (ll i = now; i < N * K; i++) {
if (S.count(A[i % N])) {
while (q.back() != A[i % N]) {
S.erase(q.back());
q.pop_back();
}
S.erase(A[i % N]);
q.pop_back();
} else {
S.insert(A[i % N]);
q.push_back(A[i % N]);
}
}
for (int i = 0; i < q.size(); i++)
cout << q[i] << " ";
cout << endl;
} | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
int A[400000];
ll d[61][200000];
int pos[200001];
int main() {
ll N, K;
cin >> N >> K;
for (int i = 0; i < N; i++) {
cin >> A[i];
A[i + N] = A[i];
}
for (int i = 2 * N - 1; i >= 0; i--) {
if (i < N)
d[0][i] = pos[A[i]] - i + 1;
pos[A[i]] = i;
}
for (int i = 1; i <= 60; i++) {
for (int j = 0; j < N; j++) {
d[i][j] = min(INF, d[i - 1][j] + d[i - 1][(j + d[i - 1][j]) % N]);
}
}
ll now = 0;
for (int i = 60; i >= 0; i--) {
if (now + d[i][now % N] <= N * K)
now += d[i][now % N];
}
deque<int> q;
set<int> S;
for (ll i = now; i < N * K; i++) {
if (S.count(A[i % N])) {
while (q.back() != A[i % N]) {
S.erase(q.back());
q.pop_back();
}
S.erase(A[i % N]);
q.pop_back();
} else {
S.insert(A[i % N]);
q.push_back(A[i % N]);
}
}
for (int i = 0; i < q.size(); i++)
cout << q[i] << " ";
cout << endl;
} | replace | 26 | 27 | 26 | 27 | TLE | |
p02964 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#define REP(i, x, y) for (int i = (x); i < (y); i++)
#define RREP(i, x, y) for (int i = (y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#define int long long
using namespace std;
// conversion
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
#else
#define eprintf(...) 42
#define dump(x) 42
#define debug(x) 42
#endif
typedef long long ll;
const ll MOD = 1e9 + 7;
struct Queue {
Queue() {}
Queue(int N) : s(vector<int>(N)), last(0) {}
vector<int> s;
int last;
};
void simulate(const int N, ll currentIndex, const ll limit, vector<int> &A,
Queue &queue) {
unordered_map<int, int> positions; // <number, position>
unordered_map<int, int> numbers; // <position, number>
while (currentIndex < limit) {
const int a = A[currentIndex % N];
// cout << "a:" << a << ", queue.last:" << queue.last << '\n';
if (positions.find(a) != positions.end() && numbers[positions[a]] == a &&
positions[a] < queue.last) {
queue.last = positions[a];
} else {
queue.s[queue.last] = a;
positions[a] = queue.last;
numbers[queue.last] = a;
queue.last++;
}
currentIndex++;
// REP(i,0,queue.last) {
// cout << queue.s[i] << " ";
// }
// cout << "\n";
}
}
vector<int> generateDistanceListToNextIndex(int N, vector<int> &A) {
vector<int> list(N, 0);
unordered_map<int, int> previousIndex; // <number, index>
REP(i, 0, 2 * N) {
// debug(i);
const int currentNumber = A[i % N];
if (previousIndex.find(currentNumber) != previousIndex.end() &&
list[previousIndex[currentNumber] % N] == 0) {
list[previousIndex[currentNumber] % N] = i - previousIndex[currentNumber];
// debug(previousIndex[currentNumber]);
assert(list[previousIndex[currentNumber] % N] > 0);
}
previousIndex[currentNumber] = i;
}
REP(i, 0, N) {
if (list[i] == 0)
list[i] = N;
}
return list;
}
void solve(int N, ll K, vector<int> &A) {
Queue queue(N);
ll currentIndex = 0;
const ll limit = N * K;
if (K > 1) {
const auto distanceListToNextIndex = generateDistanceListToNextIndex(N, A);
ll cycle = 0;
vector<ll> visitedWhen(N, -1);
const ll preLimit = N * (K - 1);
int count = 0;
while (currentIndex < preLimit) {
if (visitedWhen[currentIndex % N] != -1) {
cycle = currentIndex - visitedWhen[currentIndex % N];
break;
}
visitedWhen[currentIndex % N] = currentIndex;
currentIndex += distanceListToNextIndex[currentIndex % N] + 1;
count++;
// assert(currentIndex >= 0);
}
if (cycle != 0 && currentIndex < preLimit) {
currentIndex += (N * K - currentIndex) / cycle * cycle;
}
}
// assert(limit - currentIndex <= 2 * N);
simulate(N, currentIndex, limit, A, queue);
stringstream ss;
REP(i, 0, queue.last - 1) { ss << queue.s[i] << " "; }
if (queue.last != 0)
ss << queue.s[queue.last - 1];
cout << ss.str() << '\n';
return;
}
signed main() {
ll N, K;
cin >> N >> K;
vector<int> A(N);
REP(i, 0, N) cin >> A[i];
solve(N, K, A);
return 0;
}
| #include "bits/stdc++.h"
#define REP(i, x, y) for (int i = (x); i < (y); i++)
#define RREP(i, x, y) for (int i = (y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#define int long long
using namespace std;
// conversion
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
#else
#define eprintf(...) 42
#define dump(x) 42
#define debug(x) 42
#endif
typedef long long ll;
const ll MOD = 1e9 + 7;
struct Queue {
Queue() {}
Queue(int N) : s(vector<int>(N)), last(0) {}
vector<int> s;
int last;
};
void simulate(const int N, ll currentIndex, const ll limit, vector<int> &A,
Queue &queue) {
unordered_map<int, int> positions; // <number, position>
unordered_map<int, int> numbers; // <position, number>
while (currentIndex < limit) {
const int a = A[currentIndex % N];
// cout << "a:" << a << ", queue.last:" << queue.last << '\n';
if (positions.find(a) != positions.end() && numbers[positions[a]] == a &&
positions[a] < queue.last) {
queue.last = positions[a];
} else {
queue.s[queue.last] = a;
positions[a] = queue.last;
numbers[queue.last] = a;
queue.last++;
}
currentIndex++;
// REP(i,0,queue.last) {
// cout << queue.s[i] << " ";
// }
// cout << "\n";
}
}
vector<int> generateDistanceListToNextIndex(int N, vector<int> &A) {
vector<int> list(N, 0);
unordered_map<int, int> previousIndex; // <number, index>
REP(i, 0, 2 * N) {
// debug(i);
const int currentNumber = A[i % N];
if (previousIndex.find(currentNumber) != previousIndex.end() &&
list[previousIndex[currentNumber] % N] == 0) {
list[previousIndex[currentNumber] % N] = i - previousIndex[currentNumber];
// debug(previousIndex[currentNumber]);
assert(list[previousIndex[currentNumber] % N] > 0);
}
previousIndex[currentNumber] = i;
}
REP(i, 0, N) {
if (list[i] == 0)
list[i] = N;
}
return list;
}
void solve(int N, ll K, vector<int> &A) {
Queue queue(N);
ll currentIndex = 0;
const ll limit = N * K;
if (K > 1) {
const auto distanceListToNextIndex = generateDistanceListToNextIndex(N, A);
ll cycle = 0;
vector<ll> visitedWhen(N, -1);
const ll preLimit = N * (K - 1);
int count = 0;
while (currentIndex < preLimit) {
if (visitedWhen[currentIndex % N] != -1) {
cycle = currentIndex - visitedWhen[currentIndex % N];
break;
}
visitedWhen[currentIndex % N] = currentIndex;
currentIndex += distanceListToNextIndex[currentIndex % N] + 1;
count++;
// assert(currentIndex >= 0);
}
if (cycle != 0 && currentIndex < preLimit) {
currentIndex += (N * K - currentIndex) / cycle * cycle;
}
while (currentIndex < preLimit) {
currentIndex += distanceListToNextIndex[currentIndex % N] + 1;
count++;
}
}
// assert(limit - currentIndex <= 2 * N);
simulate(N, currentIndex, limit, A, queue);
stringstream ss;
REP(i, 0, queue.last - 1) { ss << queue.s[i] << " "; }
if (queue.last != 0)
ss << queue.s[queue.last - 1];
cout << ss.str() << '\n';
return;
}
signed main() {
ll N, K;
cin >> N >> K;
vector<int> A(N);
REP(i, 0, N) cin >> A[i];
solve(N, K, A);
return 0;
}
| insert | 122 | 122 | 122 | 127 | TLE | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<int> v[200001];
long long int vis[200001];
int a[200001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
long long int k;
cin >> n >> k;
for (int i = 0, x; i < n; i++) {
cin >> x;
a[i] = x;
v[x].emplace_back(i);
}
memset(vis, -1, sizeof vis);
int cx = 0, nx;
long long int ck = 0, nk;
while (true) {
if (vis[cx] != -1) {
ck = vis[cx] + (k - 1 - vis[cx]) / (ck - vis[cx]) * (ck - vis[cx]);
break;
}
vis[cx] = ck;
auto it = upper_bound(v[a[cx]].begin(), v[a[cx]].end(), cx);
if (it == v[a[cx]].end()) {
nx = v[a[cx]][0];
nk = ck + 1;
} else {
nx = *it;
nk = ck;
}
nx++;
if (nx == n)
nx = 0, nk++;
if (nk >= k)
break;
cx = nx, ck = nk;
}
while (true) {
auto it = upper_bound(v[a[cx]].begin(), v[a[cx]].end(), cx);
if (it == v[a[cx]].end()) {
nx = v[a[cx]][0];
nk = ck + 1;
} else {
nx = *it;
nk = ck;
}
nx++;
if (nx == n)
nx = 0, nk++;
if (nk >= k)
break;
cx = nx, ck = nk;
}
vector<int> res;
set<int> st;
while (ck < k) {
int x = a[cx];
if (st.count(x)) {
while (res.back() != x)
res.pop_back();
res.pop_back();
st.erase(x);
} else {
st.insert(x);
res.emplace_back(x);
}
cx++;
if (cx == n)
cx = 0, ck++;
}
for (int it : res)
cout << it << ' ';
cout << endl;
}
| #include <bits/stdc++.h>
using namespace std;
vector<int> v[200001];
long long int vis[200001];
int a[200001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
long long int k;
cin >> n >> k;
for (int i = 0, x; i < n; i++) {
cin >> x;
a[i] = x;
v[x].emplace_back(i);
}
memset(vis, -1, sizeof vis);
int cx = 0, nx;
long long int ck = 0, nk;
while (true) {
if (vis[cx] != -1) {
ck = vis[cx] + (k - 1 - vis[cx]) / (ck - vis[cx]) * (ck - vis[cx]);
break;
}
vis[cx] = ck;
auto it = upper_bound(v[a[cx]].begin(), v[a[cx]].end(), cx);
if (it == v[a[cx]].end()) {
nx = v[a[cx]][0];
nk = ck + 1;
} else {
nx = *it;
nk = ck;
}
nx++;
if (nx == n)
nx = 0, nk++;
if (nk >= k)
break;
cx = nx, ck = nk;
}
while (true) {
auto it = upper_bound(v[a[cx]].begin(), v[a[cx]].end(), cx);
if (it == v[a[cx]].end()) {
nx = v[a[cx]][0];
nk = ck + 1;
} else {
nx = *it;
nk = ck;
}
nx++;
if (nx == n)
nx = 0, nk++;
if (nk >= k)
break;
cx = nx, ck = nk;
}
vector<int> res;
set<int> st;
while (ck < k) {
int x = a[cx];
if (st.count(x)) {
while (res.back() != x)
st.erase(res.back()), res.pop_back();
res.pop_back();
st.erase(x);
} else {
st.insert(x);
res.emplace_back(x);
}
cx++;
if (cx == n)
cx = 0, ck++;
}
for (int it : res)
cout << it << ' ';
cout << endl;
}
| replace | 65 | 66 | 65 | 66 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
int n;
long long k;
int a[N], las[N], pos[N], b[N], len[N];
bool c[N];
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = n; i >= 1; i--)
las[i] = pos[a[i]], pos[a[i]] = i;
for (int i = 1; i <= n; i++)
if (!las[i])
las[i] = pos[a[i]], len[i] = 1;
for (int i = 1; i <= n; i++)
if (las[i] == n)
len[i]++;
for (int i = 1; i <= n; i++)
las[i] = las[i] % n + 1;
int now = 1, l = 0;
k--;
while (k > 0) {
k -= (ll)len[now];
now = las[now];
if (c[now])
break;
c[now] = 1;
b[++l] = now;
}
if (k < 0)
return 0;
if (k > 0) {
int s = 0;
for (int i = 1; i <= l; i++)
if (b[i] == now) {
for (int j = i; j <= l; j++)
s += len[j];
break;
}
k = k % (ll)s;
while (k > 0) {
k -= (ll)len[now];
now = las[now];
}
if (k < 0)
return 0;
}
memset(pos, 0, sizeof(pos));
l = 0;
for (int i = now; i <= n; i++)
if (!pos[a[i]]) {
pos[a[i]] = i;
b[++l] = a[i];
} else {
while (b[l] != a[i])
pos[b[l]] = 0, l--;
pos[a[i]] = 0;
l--;
}
for (int i = 1; i <= l; i++)
printf("%d ", b[i]);
} | #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
int n;
long long k;
int a[N], las[N], pos[N], b[N], len[N];
bool c[N];
int main() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = n; i >= 1; i--)
las[i] = pos[a[i]], pos[a[i]] = i;
for (int i = 1; i <= n; i++)
if (!las[i])
las[i] = pos[a[i]], len[i] = 1;
for (int i = 1; i <= n; i++)
if (las[i] == n)
len[i]++;
for (int i = 1; i <= n; i++)
las[i] = las[i] % n + 1;
int now = 1, l = 0;
k--;
while (k > 0) {
k -= (ll)len[now];
now = las[now];
if (c[now])
break;
c[now] = 1;
b[++l] = now;
}
if (k < 0)
return 0;
if (k > 0) {
int s = 0;
for (int i = 1; i <= l; i++)
if (b[i] == now) {
for (int j = i; j <= l; j++)
s += len[b[j]];
break;
}
k = k % (ll)s;
while (k > 0) {
k -= (ll)len[now];
now = las[now];
}
if (k < 0)
return 0;
}
memset(pos, 0, sizeof(pos));
l = 0;
for (int i = now; i <= n; i++)
if (!pos[a[i]]) {
pos[a[i]] = i;
b[++l] = a[i];
} else {
while (b[l] != a[i])
pos[b[l]] = 0, l--;
pos[a[i]] = 0;
l--;
}
for (int i = 1; i <= l; i++)
printf("%d ", b[i]);
} | replace | 39 | 40 | 39 | 40 | 0 | |
p02964 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
#define maxn 200 // 100
using namespace std;
vector<long long> q[maxn];
int main() {
long long N, K;
long long a[maxn];
long long ma[maxn], L[maxn];
scanf("%lld%lld", &N, &K);
for (long long i = 0; i < N; i++) {
scanf("%lld", &a[i]);
q[a[i]].push_back(i);
}
for (long long i = 0; i < maxn; i++)
if (!q[i].empty())
L[i] = q[i].size();
long long o = a[N - 1], p = 1, sign = N - 1;
while (1) {
int S = lower_bound(q[o].begin(), q[o].end(), sign) - q[o].begin();
if (S == 0) {
S = L[o] - 1;
p++;
} else
S--;
if (q[o][S] == 0)
break;
else {
sign = (q[o][S] + N - 1) % N;
o = a[sign];
}
}
// printf("%lld\n",p);
K = K % p;
stack<long long> U;
for (long long j = 0; j < maxn; j++)
ma[j] = 0;
o = a[0];
p = 1;
sign = 0;
while (1 && K != 0) {
// printf("1\n");
int S = lower_bound(q[o].begin(), q[o].end(), sign) - q[o].begin();
// printf("1\n");
// printf("%d %lld\n",S,L[o]-1);
if (S == L[o] - 1) {
// printf("%lld %lld\n",p,K);
if (p == K)
U.push(o);
else {
// printf("1\n");
S = 0;
p++;
}
// printf("1\n");
} else
S++;
// printf("1\n");
sign = q[o][S] + 1;
if (p == K && sign == N)
break;
else {
sign %= N;
o = a[sign];
}
// printf("%lld %lld\n",o,sign);
// printf("%d\n",U.size());
}
long long f[maxn];
long long l = U.size();
for (long long i = l; i > 0; i--) {
f[i] = U.top();
U.pop();
}
for (long long i = 1; i <= l; i++)
printf("%lld ", f[i]);
printf("\n");
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
#define maxn 200100
using namespace std;
vector<long long> q[maxn];
int main() {
long long N, K;
long long a[maxn];
long long ma[maxn], L[maxn];
scanf("%lld%lld", &N, &K);
for (long long i = 0; i < N; i++) {
scanf("%lld", &a[i]);
q[a[i]].push_back(i);
}
for (long long i = 0; i < maxn; i++)
if (!q[i].empty())
L[i] = q[i].size();
long long o = a[N - 1], p = 1, sign = N - 1;
while (1) {
int S = lower_bound(q[o].begin(), q[o].end(), sign) - q[o].begin();
if (S == 0) {
S = L[o] - 1;
p++;
} else
S--;
if (q[o][S] == 0)
break;
else {
sign = (q[o][S] + N - 1) % N;
o = a[sign];
}
}
// printf("%lld\n",p);
K = K % p;
stack<long long> U;
for (long long j = 0; j < maxn; j++)
ma[j] = 0;
o = a[0];
p = 1;
sign = 0;
while (1 && K != 0) {
// printf("1\n");
int S = lower_bound(q[o].begin(), q[o].end(), sign) - q[o].begin();
// printf("1\n");
// printf("%d %lld\n",S,L[o]-1);
if (S == L[o] - 1) {
// printf("%lld %lld\n",p,K);
if (p == K)
U.push(o);
else {
// printf("1\n");
S = 0;
p++;
}
// printf("1\n");
} else
S++;
// printf("1\n");
sign = q[o][S] + 1;
if (p == K && sign == N)
break;
else {
sign %= N;
o = a[sign];
}
// printf("%lld %lld\n",o,sign);
// printf("%d\n",U.size());
}
long long f[maxn];
long long l = U.size();
for (long long i = l; i > 0; i--) {
f[i] = U.top();
U.pop();
}
for (long long i = 1; i <= l; i++)
printf("%lld ", f[i]);
printf("\n");
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p02964 | C++ | Runtime Error |
#include <algorithm>
#include <array>
#include <bitset>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <vector>
long long mod = 1e9 + 7;
class Mint {
public:
long long x;
Mint(){}; // 引数なしでも定義できるように引数なしコンストラクタも用意しておく
Mint(long long a) {
x = a % mod;
while (x < 0) {
x += mod;
}
};
Mint &operator+=(const Mint &a) {
x += a.x;
x %= mod;
return *this;
}
Mint &operator-=(const Mint &a) {
x += (mod - a.x);
x %= mod;
return *this;
}
Mint &operator*=(const Mint &a) {
x *= a.x;
x %= mod;
return *this;
}
// a^n mod を計算する
// Useaeg: Mint z = Mint(2).pow(n);
Mint pow(long long n) const {
if (n == 0)
return Mint(1);
Mint y = pow(n >> 1); // pow(n/2)を計算する
y *= y;
if (n % 2 == 1)
y *= *this;
return y;
}
// a^{-1} mod を計算する
Mint modinv(const Mint &a) const { return a.pow(mod - 2); }
Mint &operator/=(const Mint &a) {
x *= modinv(a).x;
x %= mod;
return *this;
}
Mint operator+(Mint &a) const {
Mint y(*this);
y += a;
return y;
}
Mint operator-(Mint &a) const {
Mint y(*this);
y -= a;
return y;
}
Mint operator*(Mint &a) const {
Mint y(*this);
y *= a;
return y;
}
Mint operator/(Mint &a) const {
Mint y(*this);
y /= a;
return y.x;
}
// nCk @mod を計算する
Mint nCk(Mint &n, const long long k) const {
Mint y = Mint(1);
Mint iy = Mint(1);
Mint one = Mint(1);
// 割り算の演算は時間がかかるので最後に1度だけ行うようにすること
for (Mint i(0); (i.x) < k; i.x++) {
y *= (n - i);
iy *= (i + one);
}
return y / iy;
}
// nPk @mod を計算する
Mint nPk(Mint &n, long long k) const {
Mint y(1);
for (Mint i(0); (i.x) < k; i.x++) {
y *= (n - i);
}
return y;
}
};
class UnionFind {
private:
int n;
std::vector<int> parent;
public:
UnionFind(int num) : n(num) {
parent = std::vector<int>(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
int GetRoot(int i) {
int n = i;
while (parent[n] != n) {
n = parent[n];
}
parent[i] = n;
return n;
}
bool SameGroup(int n, int m) {
n = this->GetRoot(n);
m = this->GetRoot(m);
return n == m;
}
bool Unite(int n, int m) {
n = this->GetRoot(n);
m = this->GetRoot(m);
if (n == m)
return false;
parent[n] = m;
return true;
}
};
int getgcd(int a, int b) {
int max = std::max(a, b);
int min = std::min(a, b);
if (min == 0)
return max;
return getgcd(min, max % min);
}
Mint mynCk(int n, int k) {
if (2 * k > n)
return mynCk(n, n - k);
Mint retval(1);
Mint iretval(1);
for (int i = 0; i < k; i++) {
retval *= Mint(n - i);
}
for (int i = 1; i <= k; i++) {
iretval *= Mint(i);
}
return retval / iretval;
}
int main() {
int N;
std::cin >> N;
long long K;
std::cin >> K;
std::vector<int> A(N);
std::vector<int> parent(N + 4, -1);
std::vector<int> pos(200004, -1);
std::vector<int> AA(2 * N);
for (int i = 0; i < N; i++) {
std::cin >> A[i];
AA[i] = A[i];
AA[i + N] = A[i];
}
// printf("read end\n");
for (int i = 0; i < 2 * N; i++) {
// printf("i=%d\n", i);
if (pos[AA[i]] != -1) {
if (pos[AA[i]] < N)
parent[pos[AA[i]]] = i;
}
pos[AA[i]] = i;
}
/*for (int i=0; i<N; i++) {
printf("parent[%d] = %d\n", i, parent[i]);
}*/
std::vector<long long> visit(N + 4, -1);
int now = 0;
visit[now] = 0;
long long cycle;
while (true) {
int step = parent[now] - now + 1;
int next = (parent[now] + 1) % N;
cycle = visit[now] + step;
if (visit[next] != -1)
break;
visit[next] = cycle;
now = next;
cycle = visit[now];
}
// printf("cycle=%lld\n", cycle);
long long num = K * N;
num %= cycle;
now = 0;
long long sum = 0;
while (true) {
int step = parent[now] - now + 1;
int next = (parent[now] + 1) % N;
if (sum + step > num)
break;
if (next == 0)
break;
sum += step;
now = next;
}
num -= sum;
// printf("now=%d, num=%lld\n", now, num);
// nowをスタートとしてnum回文字を処理する
std::vector<int> ans;
std::map<int, int> mp;
for (int i = 0; i < num; i++) {
int index = (i + now) % N;
if (mp[A[index]] == 1) {
while (ans[ans.size() - 1] != A[index]) {
mp[ans.size() - 1] = 0;
ans.pop_back();
}
mp[A[index]] = 0;
ans.pop_back();
} else {
ans.push_back(A[index]);
mp[A[index]] = 1;
}
}
for (int p : ans) {
std::cout << p << std::endl;
}
return 0;
} |
#include <algorithm>
#include <array>
#include <bitset>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <vector>
long long mod = 1e9 + 7;
class Mint {
public:
long long x;
Mint(){}; // 引数なしでも定義できるように引数なしコンストラクタも用意しておく
Mint(long long a) {
x = a % mod;
while (x < 0) {
x += mod;
}
};
Mint &operator+=(const Mint &a) {
x += a.x;
x %= mod;
return *this;
}
Mint &operator-=(const Mint &a) {
x += (mod - a.x);
x %= mod;
return *this;
}
Mint &operator*=(const Mint &a) {
x *= a.x;
x %= mod;
return *this;
}
// a^n mod を計算する
// Useaeg: Mint z = Mint(2).pow(n);
Mint pow(long long n) const {
if (n == 0)
return Mint(1);
Mint y = pow(n >> 1); // pow(n/2)を計算する
y *= y;
if (n % 2 == 1)
y *= *this;
return y;
}
// a^{-1} mod を計算する
Mint modinv(const Mint &a) const { return a.pow(mod - 2); }
Mint &operator/=(const Mint &a) {
x *= modinv(a).x;
x %= mod;
return *this;
}
Mint operator+(Mint &a) const {
Mint y(*this);
y += a;
return y;
}
Mint operator-(Mint &a) const {
Mint y(*this);
y -= a;
return y;
}
Mint operator*(Mint &a) const {
Mint y(*this);
y *= a;
return y;
}
Mint operator/(Mint &a) const {
Mint y(*this);
y /= a;
return y.x;
}
// nCk @mod を計算する
Mint nCk(Mint &n, const long long k) const {
Mint y = Mint(1);
Mint iy = Mint(1);
Mint one = Mint(1);
// 割り算の演算は時間がかかるので最後に1度だけ行うようにすること
for (Mint i(0); (i.x) < k; i.x++) {
y *= (n - i);
iy *= (i + one);
}
return y / iy;
}
// nPk @mod を計算する
Mint nPk(Mint &n, long long k) const {
Mint y(1);
for (Mint i(0); (i.x) < k; i.x++) {
y *= (n - i);
}
return y;
}
};
class UnionFind {
private:
int n;
std::vector<int> parent;
public:
UnionFind(int num) : n(num) {
parent = std::vector<int>(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
int GetRoot(int i) {
int n = i;
while (parent[n] != n) {
n = parent[n];
}
parent[i] = n;
return n;
}
bool SameGroup(int n, int m) {
n = this->GetRoot(n);
m = this->GetRoot(m);
return n == m;
}
bool Unite(int n, int m) {
n = this->GetRoot(n);
m = this->GetRoot(m);
if (n == m)
return false;
parent[n] = m;
return true;
}
};
int getgcd(int a, int b) {
int max = std::max(a, b);
int min = std::min(a, b);
if (min == 0)
return max;
return getgcd(min, max % min);
}
Mint mynCk(int n, int k) {
if (2 * k > n)
return mynCk(n, n - k);
Mint retval(1);
Mint iretval(1);
for (int i = 0; i < k; i++) {
retval *= Mint(n - i);
}
for (int i = 1; i <= k; i++) {
iretval *= Mint(i);
}
return retval / iretval;
}
int main() {
int N;
std::cin >> N;
long long K;
std::cin >> K;
std::vector<int> A(N);
std::vector<int> parent(N + 4, -1);
std::vector<int> pos(200004, -1);
std::vector<int> AA(2 * N);
for (int i = 0; i < N; i++) {
std::cin >> A[i];
AA[i] = A[i];
AA[i + N] = A[i];
}
// printf("read end\n");
for (int i = 0; i < 2 * N; i++) {
// printf("i=%d\n", i);
if (pos[AA[i]] != -1) {
if (pos[AA[i]] < N)
parent[pos[AA[i]]] = i;
}
pos[AA[i]] = i;
}
/*for (int i=0; i<N; i++) {
printf("parent[%d] = %d\n", i, parent[i]);
}*/
std::vector<long long> visit(N + 4, -1);
int now = 0;
visit[now] = 0;
long long cycle;
while (true) {
int step = parent[now] - now + 1;
int next = (parent[now] + 1) % N;
cycle = visit[now] + step;
if (visit[next] != -1)
break;
visit[next] = cycle;
now = next;
cycle = visit[now];
}
// printf("cycle=%lld\n", cycle);
long long num = K * N;
num %= cycle;
now = 0;
long long sum = 0;
while (true) {
int step = parent[now] - now + 1;
int next = (parent[now] + 1) % N;
if (sum + step > num)
break;
if (next == 0)
break;
sum += step;
now = next;
}
num -= sum;
// printf("now=%d, num=%lld\n", now, num);
// nowをスタートとしてnum回文字を処理する
std::vector<int> ans;
std::map<int, int> mp;
for (int i = 0; i < num; i++) {
int index = (i + now) % N;
if (mp[A[index]] == 1) {
while (ans[ans.size() - 1] != A[index]) {
mp[ans[ans.size() - 1]] = 0;
ans.pop_back();
}
mp[A[index]] = 0;
ans.pop_back();
} else {
ans.push_back(A[index]);
mp[A[index]] = 1;
}
}
for (int p : ans) {
std::cout << p << std::endl;
}
return 0;
} | replace | 235 | 236 | 235 | 236 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;
#define MAX_A 200000
#define MAX 50
signed main() {
ll n, k;
cin >> n >> k;
int a[n];
rep(i, n) { cin >> a[i]; }
vector<vector<ll>> dp(MAX + 1, vector<ll>(n));
int nxt[MAX_A + 1];
for (int i = 2 * n - 1; i >= 0; i--) {
dp[0][i % n] = nxt[a[i % n]] - i + 1;
nxt[a[i % n]] = i;
}
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < n; j++) {
dp[i + 1][j] = dp[i][j] + dp[i][(dp[i][j] + j) % n];
}
}
int now = 0;
ll sum = n * k;
while (sum > 0) {
for (int i = MAX; i >= 0; i--) {
if (dp[i][now] < sum) {
sum -= dp[i][now];
now = (now + dp[i][now]) % n;
goto ok;
}
}
break;
ok:;
}
bool exist[MAX_A + 1] = {};
vector<int> ans;
for (int i = now; i < n; i++) {
if (exist[a[i]]) {
while (exist[a[i]]) {
exist[ans.back()] = false;
ans.pop_back();
}
} else {
ans.emplace_back(a[i]);
exist[a[i]] = true;
}
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;
#define MAX_A 200000
#define MAX 40
signed main() {
ll n, k;
cin >> n >> k;
int a[n];
rep(i, n) { cin >> a[i]; }
vector<vector<ll>> dp(MAX + 1, vector<ll>(n));
int nxt[MAX_A + 1];
for (int i = 2 * n - 1; i >= 0; i--) {
dp[0][i % n] = nxt[a[i % n]] - i + 1;
nxt[a[i % n]] = i;
}
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < n; j++) {
dp[i + 1][j] = dp[i][j] + dp[i][(dp[i][j] + j) % n];
}
}
int now = 0;
ll sum = n * k;
while (sum > 0) {
for (int i = MAX; i >= 0; i--) {
if (dp[i][now] < sum) {
sum -= dp[i][now];
now = (now + dp[i][now]) % n;
goto ok;
}
}
break;
ok:;
}
bool exist[MAX_A + 1] = {};
vector<int> ans;
for (int i = now; i < n; i++) {
if (exist[a[i]]) {
while (exist[a[i]]) {
exist[ans.back()] = false;
ans.pop_back();
}
} else {
ans.emplace_back(a[i]);
exist[a[i]] = true;
}
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p02964 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P, P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(2e5) + 5;
constexpr double EPS = 1e-8;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()
int main() {
ll n, k;
cin >> n >> k;
vector<ll> a(n * 2), nxt(n * 2), len(n * 2);
vector<vector<int>> pos(MAX_N);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i + n] = a[i];
pos[a[i]].push_back(i);
pos[a[i]].push_back(i + n);
}
for (int i = 0; i < MAX_N; i++) {
sort(pos[i].begin(), pos[i].end());
}
for (int id = 0; id != n;) {
nxt[id] = *upper_bound(pos[a[id]].begin(), pos[a[id]].end(), id) + 1;
ll l = nxt[id] - id;
if (nxt[id] > n)
nxt[id] %= n;
len[nxt[id]] = len[id] + l;
id = nxt[id];
}
ll m = (k * n) % len[n];
int bg = 0;
while (m >= len[nxt[bg]]) {
bg = nxt[bg];
}
m -= len[bg];
vector<int> s;
vector<bool> used(MAX_N, false);
for (int i = 0, j = bg; i < m; i++, j = (j + 1) % n) {
if (used[a[j]]) {
while (s.back() != a[j]) {
used[s.back()] = false;
s.pop_back();
}
used[a[j]] = false;
s.pop_back();
} else {
used[a[j]] = true;
s.push_back(a[j]);
}
}
for (int i = 0; i < s.size(); i++) {
cout << s[i] << " \n"[i == int(s.size()) - 1];
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P, P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(2e5) + 5;
constexpr double EPS = 1e-8;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()
int main() {
ll n, k;
cin >> n >> k;
vector<ll> a(n * 2), nxt(n * 2), len(n * 2);
vector<vector<int>> pos(MAX_N);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i + n] = a[i];
pos[a[i]].push_back(i);
pos[a[i]].push_back(i + n);
}
for (int i = 0; i < MAX_N; i++) {
sort(pos[i].begin(), pos[i].end());
}
for (int id = 0; id != n;) {
nxt[id] = *upper_bound(pos[a[id]].begin(), pos[a[id]].end(), id) + 1;
ll l = nxt[id] - id;
while (nxt[id] > n)
nxt[id] -= n;
len[nxt[id]] = len[id] + l;
id = nxt[id];
}
ll m = (k * n) % len[n];
int bg = 0;
while (m >= len[nxt[bg]]) {
bg = nxt[bg];
}
m -= len[bg];
vector<int> s;
vector<bool> used(MAX_N, false);
for (int i = 0, j = bg; i < m; i++, j = (j + 1) % n) {
if (used[a[j]]) {
while (s.back() != a[j]) {
used[s.back()] = false;
s.pop_back();
}
used[a[j]] = false;
s.pop_back();
} else {
used[a[j]] = true;
s.push_back(a[j]);
}
}
for (int i = 0; i < s.size(); i++) {
cout << s[i] << " \n"[i == int(s.size()) - 1];
}
return 0;
}
| replace | 36 | 38 | 36 | 38 | TLE | |
p02964 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)
struct Permutation {
vector<int64_t> container;
Permutation(size_t size) : container(size) {
for (int i = 0; i < size; i++) {
container[i] = 0;
}
}
size_t size() const { return container.size(); }
int64_t &operator[](size_t i) { return container[i]; }
const int64_t &operator[](size_t i) const { return container[i]; }
Permutation operator*(const Permutation &other) const {
int64_t n = container.size();
Permutation result(n);
for (int64_t i = 0; i < n; i++) {
result[i] = (*this)[i] + other[(i + (*this)[i]) % n];
}
return result;
}
};
Permutation ppow(const Permutation &p, int64_t x) {
if (x == 0) {
return Permutation(p.size());
} else if (x % 2 == 0) {
return ppow(p * p, x / 2);
} else {
return p * ppow(p, x - 1);
}
}
class UFTree {
vector<int> parent;
vector<int> height;
public:
UFTree(int size) {
for (int i = 0; i < size; i++) {
parent.push_back(i);
height.push_back(0);
}
}
int root(int node) {
if (parent[node] == node) {
return node;
}
return parent[node] = root(parent[node]);
}
void merge(int n0, int n1) {
int r0 = root(n0), r1 = root(n1);
if (r0 == r1) {
return;
}
if (height[r0] < height[r1])
swap(r0, r1);
if (height[r0] == height[r1])
height[r0]++;
parent[r1] = r0;
}
};
int main() {
int n;
int64_t k;
cin >> n >> k;
vector<int> a(n);
rep(i, n) {
cin >> a[i];
a[i]--;
}
vector<vector<int>> idx(200'000);
rep(i, n) { idx[a[i]].push_back(i); }
Permutation p(n);
rep(i, idx.size()) {
int m = idx[i].size();
rep(j, m) { p[idx[i][j]] = (idx[i][(j + 1) % m] - idx[i][j] + 1 + n) % n; }
}
rep(i, n) {
if (p[i] == 1) {
p[i] = n + 1;
}
}
UFTree uft(n);
rep(i, n) { uft.merge(i, (i + p[i]) % n); }
int64_t T = 0;
int cnt = 0;
rep(i, n) {
if (uft.root(0) == uft.root(i)) {
T += p[i];
cnt++;
}
}
int64_t M = (k * n) % T;
int64_t lb = 0, ub = cnt;
while (ub > lb + 1) {
int64_t mid = (lb + ub) / 2;
Permutation q = ppow(p, mid);
if (q[0] <= M) {
lb = mid;
} else {
ub = mid;
}
}
Permutation q = ppow(p, lb);
vector<int> result;
int64_t j = q[0];
while (j < M) {
if (j + p[j % n] <= M) {
j += p[j % n];
} else {
result.push_back(a[j % n]);
j++;
}
}
rep(i, result.size()) { cout << result[i] + 1 << " "; }
cout << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)
struct Permutation {
vector<int64_t> container;
Permutation(size_t size) : container(size) {
for (int i = 0; i < size; i++) {
container[i] = 0;
}
}
size_t size() const { return container.size(); }
int64_t &operator[](size_t i) { return container[i]; }
const int64_t &operator[](size_t i) const { return container[i]; }
Permutation operator*(const Permutation &other) const {
int64_t n = container.size();
Permutation result(n);
for (int64_t i = 0; i < n; i++) {
result[i] = (*this)[i] + other[(i + (*this)[i]) % n];
}
return result;
}
};
Permutation ppow(const Permutation &p, int64_t x) {
if (x == 0) {
return Permutation(p.size());
} else if (x % 2 == 0) {
return ppow(p * p, x / 2);
} else {
return p * ppow(p, x - 1);
}
}
class UFTree {
vector<int> parent;
vector<int> height;
public:
UFTree(int size) {
for (int i = 0; i < size; i++) {
parent.push_back(i);
height.push_back(0);
}
}
int root(int node) {
if (parent[node] == node) {
return node;
}
return parent[node] = root(parent[node]);
}
void merge(int n0, int n1) {
int r0 = root(n0), r1 = root(n1);
if (r0 == r1) {
return;
}
if (height[r0] < height[r1])
swap(r0, r1);
if (height[r0] == height[r1])
height[r0]++;
parent[r1] = r0;
}
};
int main() {
int n;
int64_t k;
cin >> n >> k;
vector<int> a(n);
rep(i, n) {
cin >> a[i];
a[i]--;
}
if (n == 1) {
if (k % 2 == 0) {
cout << endl;
} else {
cout << a[0] + 1 << endl;
}
return 0;
}
vector<vector<int>> idx(200'000);
rep(i, n) { idx[a[i]].push_back(i); }
Permutation p(n);
rep(i, idx.size()) {
int m = idx[i].size();
rep(j, m) { p[idx[i][j]] = (idx[i][(j + 1) % m] - idx[i][j] + 1 + n) % n; }
}
rep(i, n) {
if (p[i] == 1) {
p[i] = n + 1;
}
}
UFTree uft(n);
rep(i, n) { uft.merge(i, (i + p[i]) % n); }
int64_t T = 0;
int cnt = 0;
rep(i, n) {
if (uft.root(0) == uft.root(i)) {
T += p[i];
cnt++;
}
}
int64_t M = (k * n) % T;
int64_t lb = 0, ub = cnt;
while (ub > lb + 1) {
int64_t mid = (lb + ub) / 2;
Permutation q = ppow(p, mid);
if (q[0] <= M) {
lb = mid;
} else {
ub = mid;
}
}
Permutation q = ppow(p, lb);
vector<int> result;
int64_t j = q[0];
while (j < M) {
if (j + p[j % n] <= M) {
j += p[j % n];
} else {
result.push_back(a[j % n]);
j++;
}
}
rep(i, result.size()) { cout << result[i] + 1 << " "; }
cout << endl;
return 0;
} | insert | 90 | 90 | 90 | 99 | 0 | |
p02964 | C++ | Runtime Error | #include <iomanip>
#include <iostream>
// #include<cstdio>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int Nmax = 2e5;
int N, A[Nmax];
ll K;
int main() {
cin >> N >> K;
for (int i = 0; i < N; i++)
cin >> A[i];
vector<int> next(N, -1), pos(N + 1, -1);
for (int i = 0; i < N; i++) {
if (pos[A[i]] >= 0) {
next[pos[A[i]]] = i;
}
pos[A[i]] = i;
}
for (int i = 0; i < N; i++) {
if (next[pos[A[i]]] < 0) {
next[pos[A[i]]] = i;
}
}
int interval = 1;
int idx = 0;
while (idx != N) {
if (next[idx] <= idx)
interval++;
idx = next[idx] + 1;
}
K %= interval;
// cout << K << endl;
idx = 0;
while (K > 1) {
if (next[idx] <= idx)
K--;
idx = next[idx] + 1;
// cout << idx << " " << K << endl;
}
vector<int> ans;
while (idx < N) {
if (next[idx] > idx)
idx = next[idx] + 1;
else {
ans.push_back(A[idx]);
idx++;
}
}
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << (i == ans.size() - 1 ? '\n' : ' ');
return 0;
}
| #include <iomanip>
#include <iostream>
// #include<cstdio>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int Nmax = 2e5;
int N, A[Nmax];
ll K;
int main() {
cin >> N >> K;
for (int i = 0; i < N; i++)
cin >> A[i];
vector<int> next(N, -1), pos(Nmax + 1, -1);
for (int i = 0; i < N; i++) {
if (pos[A[i]] >= 0) {
next[pos[A[i]]] = i;
}
pos[A[i]] = i;
}
for (int i = 0; i < N; i++) {
if (next[pos[A[i]]] < 0) {
next[pos[A[i]]] = i;
}
}
int interval = 1;
int idx = 0;
while (idx != N) {
if (next[idx] <= idx)
interval++;
idx = next[idx] + 1;
}
K %= interval;
// cout << K << endl;
idx = 0;
while (K > 1) {
if (next[idx] <= idx)
K--;
idx = next[idx] + 1;
// cout << idx << " " << K << endl;
}
vector<int> ans;
while (idx < N) {
if (next[idx] > idx)
idx = next[idx] + 1;
else {
ans.push_back(A[idx]);
idx++;
}
}
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << (i == ans.size() - 1 ? '\n' : ' ');
return 0;
}
| replace | 20 | 21 | 20 | 21 | 0 | |
p02964 | C++ | Runtime Error | #define DEBUG 0
/**
* File : B2.cpp
* Author : Kazune Takahashi
* Created : 7/22/2019, 3:04:34 AM
* Powered by Visual Studio Code
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint {
public:
static ll MOD;
ll x;
mint() : x(0) {}
mint(ll x) : x(x % MOD) {}
mint operator-() const { return x ? MOD - x : 0; }
mint &operator+=(const mint &a) {
if ((x += a.x) >= MOD) {
x -= MOD;
}
return *this;
}
mint &operator-=(const mint &a) { return *this += -a; }
mint &operator*=(const mint &a) {
(x *= a.x) %= MOD;
return *this;
}
mint &operator/=(const mint &a) {
mint b{a};
return *this *= b.power(MOD - 2);
}
mint operator+(const mint &a) const { return mint(*this) += a; }
mint operator-(const mint &a) const { return mint(*this) -= a; }
mint operator*(const mint &a) const { return mint(*this) *= a; }
mint operator/(const mint &a) const { return mint(*this) /= a; }
bool operator<(const mint &a) const { return x < a.x; }
bool operator==(const mint &a) const { return x == a.x; }
const mint power(ll N) {
if (N == 0) {
return 1;
} else if (N % 2 == 1) {
return *this * power(N - 1);
} else {
mint half = power(N / 2);
return half * half;
}
}
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination {
public:
vector<mint> inv, fact, factinv;
static int MAX_SIZE;
combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE) {
inv[1] = 1;
for (auto i = 2; i < MAX_SIZE; i++) {
inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
}
fact[0] = factinv[0] = 1;
for (auto i = 1; i < MAX_SIZE; i++) {
fact[i] = mint(i) * fact[i - 1];
factinv[i] = inv[i] * factinv[i - 1];
}
}
mint operator()(int n, int k) {
if (n >= 0 && k >= 0 && n - k >= 0) {
return fact[n] * factinv[k] * factinv[n - k];
}
return 0;
}
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes() {
cout << "Yes" << endl;
exit(0);
}
void No() {
cout << "No" << endl;
exit(0);
}
ll N, K;
ll C;
int A[200010];
ll a[70][200010];
int main() {
cin >> N >> K;
C = N * K;
for (auto i = 0; i < N; i++) {
cin >> A[i];
}
for (auto i = 0; i < N; i++) {
a[i][0] = -1;
}
vector<int> used(200010, -1);
for (auto k = 0; k < 10; k++) {
for (auto i = 0; i < N; i++) {
if (used[A[i]] != -1) {
a[0][used[A[i]] % N] = k * N + i - used[A[i]] + 1;
}
used[A[i]] = k * N + i;
}
}
#if DEBUG == 1
for (auto i = 0; i < N; i++) {
cerr << "a[0][" << i << "] = " << a[0][i] << endl;
}
#endif
for (auto k = 1; k < 70; k++) {
for (auto i = 0; i < N; i++) {
a[k][i] = a[k - 1][i] + a[k - 1][(a[k - 1][i] + i) % N];
#if DEBUG == 1
if (k < 5) {
cerr << "a[" << k << "][" << i << "] = " << a[k][i] << endl;
}
#endif
}
}
ll sum = 0;
ll now = 0;
while (true) {
int ind = 0;
for (auto k = 0; k < 70; k++) {
if (a[k][now] + sum >= C) {
ind = k - 1;
break;
}
}
if (ind == -1) {
break;
}
sum += a[ind][now];
now = sum % N;
}
#if DEBUG == 1
cerr << "sum = " << sum << endl;
cerr << "now = " << now << endl;
#endif
deque<int> D;
vector<bool> stacked(200010, false);
for (auto i = now; i < N; i++) {
if (!stacked[A[i]]) {
D.push_back(A[i]);
stacked[A[i]] = true;
} else {
while (true) {
int x = *D.rbegin();
D.pop_back();
stacked[x] = false;
if (x == A[i]) {
break;
}
}
}
}
for (auto i = 0u; i < D.size(); i++) {
cout << D[i];
if (i < D.size() - 1) {
cout << " ";
}
}
cout << endl;
} | #define DEBUG 0
/**
* File : B2.cpp
* Author : Kazune Takahashi
* Created : 7/22/2019, 3:04:34 AM
* Powered by Visual Studio Code
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint {
public:
static ll MOD;
ll x;
mint() : x(0) {}
mint(ll x) : x(x % MOD) {}
mint operator-() const { return x ? MOD - x : 0; }
mint &operator+=(const mint &a) {
if ((x += a.x) >= MOD) {
x -= MOD;
}
return *this;
}
mint &operator-=(const mint &a) { return *this += -a; }
mint &operator*=(const mint &a) {
(x *= a.x) %= MOD;
return *this;
}
mint &operator/=(const mint &a) {
mint b{a};
return *this *= b.power(MOD - 2);
}
mint operator+(const mint &a) const { return mint(*this) += a; }
mint operator-(const mint &a) const { return mint(*this) -= a; }
mint operator*(const mint &a) const { return mint(*this) *= a; }
mint operator/(const mint &a) const { return mint(*this) /= a; }
bool operator<(const mint &a) const { return x < a.x; }
bool operator==(const mint &a) const { return x == a.x; }
const mint power(ll N) {
if (N == 0) {
return 1;
} else if (N % 2 == 1) {
return *this * power(N - 1);
} else {
mint half = power(N / 2);
return half * half;
}
}
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination {
public:
vector<mint> inv, fact, factinv;
static int MAX_SIZE;
combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE) {
inv[1] = 1;
for (auto i = 2; i < MAX_SIZE; i++) {
inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
}
fact[0] = factinv[0] = 1;
for (auto i = 1; i < MAX_SIZE; i++) {
fact[i] = mint(i) * fact[i - 1];
factinv[i] = inv[i] * factinv[i - 1];
}
}
mint operator()(int n, int k) {
if (n >= 0 && k >= 0 && n - k >= 0) {
return fact[n] * factinv[k] * factinv[n - k];
}
return 0;
}
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes() {
cout << "Yes" << endl;
exit(0);
}
void No() {
cout << "No" << endl;
exit(0);
}
ll N, K;
ll C;
int A[200010];
ll a[70][200010];
int main() {
cin >> N >> K;
C = N * K;
for (auto i = 0; i < N; i++) {
cin >> A[i];
}
vector<int> used(200010, -1);
for (auto k = 0; k < 10; k++) {
for (auto i = 0; i < N; i++) {
if (used[A[i]] != -1) {
a[0][used[A[i]] % N] = k * N + i - used[A[i]] + 1;
}
used[A[i]] = k * N + i;
}
}
#if DEBUG == 1
for (auto i = 0; i < N; i++) {
cerr << "a[0][" << i << "] = " << a[0][i] << endl;
}
#endif
for (auto k = 1; k < 70; k++) {
for (auto i = 0; i < N; i++) {
a[k][i] = a[k - 1][i] + a[k - 1][(a[k - 1][i] + i) % N];
#if DEBUG == 1
if (k < 5) {
cerr << "a[" << k << "][" << i << "] = " << a[k][i] << endl;
}
#endif
}
}
ll sum = 0;
ll now = 0;
while (true) {
int ind = 0;
for (auto k = 0; k < 70; k++) {
if (a[k][now] + sum >= C) {
ind = k - 1;
break;
}
}
if (ind == -1) {
break;
}
sum += a[ind][now];
now = sum % N;
}
#if DEBUG == 1
cerr << "sum = " << sum << endl;
cerr << "now = " << now << endl;
#endif
deque<int> D;
vector<bool> stacked(200010, false);
for (auto i = now; i < N; i++) {
if (!stacked[A[i]]) {
D.push_back(A[i]);
stacked[A[i]] = true;
} else {
while (true) {
int x = *D.rbegin();
D.pop_back();
stacked[x] = false;
if (x == A[i]) {
break;
}
}
}
}
for (auto i = 0u; i < D.size(); i++) {
cout << D[i];
if (i < D.size() - 1) {
cout << " ";
}
}
cout << endl;
} | delete | 123 | 126 | 123 | 123 | -11 | |
p02964 | C++ | Runtime Error | #define DEBUG 0
/**
* File : B.cpp
* Author : Kazune Takahashi
* Created : 7/21/2019, 9:18:18 PM
* Powered by Visual Studio Code
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint {
public:
static ll MOD;
ll x;
mint() : x(0) {}
mint(ll x) : x(x % MOD) {}
mint operator-() const { return x ? MOD - x : 0; }
mint &operator+=(const mint &a) {
if ((x += a.x) >= MOD) {
x -= MOD;
}
return *this;
}
mint &operator-=(const mint &a) { return *this += -a; }
mint &operator*=(const mint &a) {
(x *= a.x) %= MOD;
return *this;
}
mint &operator/=(const mint &a) {
mint b{a};
return *this *= b.power(MOD - 2);
}
mint operator+(const mint &a) const { return mint(*this) += a; }
mint operator-(const mint &a) const { return mint(*this) -= a; }
mint operator*(const mint &a) const { return mint(*this) *= a; }
mint operator/(const mint &a) const { return mint(*this) /= a; }
bool operator<(const mint &a) const { return x < a.x; }
bool operator==(const mint &a) const { return x == a.x; }
const mint power(ll N) {
if (N == 0) {
return 1;
} else if (N % 2 == 1) {
return *this * power(N - 1);
} else {
mint half = power(N / 2);
return half * half;
}
}
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination {
public:
vector<mint> inv, fact, factinv;
static int MAX_SIZE;
combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE) {
inv[1] = 1;
for (auto i = 2; i < MAX_SIZE; i++) {
inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
}
fact[0] = factinv[0] = 1;
for (auto i = 1; i < MAX_SIZE; i++) {
fact[i] = mint(i) * fact[i - 1];
factinv[i] = inv[i] * factinv[i - 1];
}
}
mint operator()(int n, int k) {
if (n >= 0 && k >= 0 && n - k >= 0) {
return fact[n] * factinv[k] * factinv[n - k];
}
return 0;
}
};
int combination::MAX_SIZE = 1e6 + 10;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes() {
cout << "Yes" << endl;
exit(0);
}
void No() {
cout << "No" << endl;
exit(0);
}
int N;
ll K;
int A[200010];
int B[200010];
int T[200010];
void test() {
if (N * K > 100) {
assert(false);
}
vector<int> V(N * K);
for (auto k = 0; k < K; k++) {
for (auto i = 0; i < N; i++) {
V[k * N + i] = A[i];
}
}
deque<int> X;
vector<bool> used(200010, false);
for (auto i = 0; i < N * K; i++) {
#if DEBUG == 1
cerr << "V[" << i << "] = " << V[i] << endl;
#endif
if (!used[V[i]]) {
used[V[i]] = true;
X.push_back(V[i]);
} else {
while (*X.rbegin() != V[i]) {
assert(used[*X.rbegin()]);
used[*X.rbegin()] = false;
X.pop_back();
}
assert(*X.rbegin());
used[*X.rbegin()] = false;
X.pop_back();
}
#if DEBUG == 1
cerr << "{ ";
for (auto x : X) {
cerr << x << ", ";
}
cerr << "}" << endl;
#endif
}
}
void make_B() {
fill(B, B + N, -1);
vector<int> T(200010, -1);
for (auto k = 0; k < 2; k++) {
for (auto i = 0; i < N; i++) {
if (T[A[i]] != -1) {
B[i] = T[A[i]];
}
T[A[i]] = i;
}
}
#if DEBUG == 1
for (auto i = 0; i < N; i++) {
cerr << "B[" << i << "] = " << B[i] << endl;
}
#endif
}
int circ() {
int cnt = 1;
int now = N - 1;
while (true) {
if (B[now] >= now) {
++cnt;
}
now = B[now];
T[now] = -cnt;
if (now > 0) {
--now;
} else {
break;
}
}
return cnt;
}
int main() {
cin >> N >> K;
for (auto i = 0; i < N; i++) {
cin >> A[i];
}
make_B();
int C = circ();
#if DEBUG == 1
cerr << "circ() = " << C << endl;
#endif
K %= C;
for (auto i = 0; i < N; i++) {
T[i] += C + 1;
}
int start = -1;
for (auto i = N - 1; i >= 0; i--) {
if (T[i] == K) {
start = i;
break;
}
}
assert(start >= 0);
deque<int> X;
vector<bool> used(200010, false);
for (auto i = start; i < N; i++) {
#if DEBUG == 1
cerr << "A[" << i << "] = " << A[i] << endl;
#endif
if (!used[A[i]]) {
used[A[i]] = true;
X.push_back(A[i]);
} else {
while (*X.rbegin() != A[i]) {
assert(used[*X.rbegin()]);
used[*X.rbegin()] = false;
X.pop_back();
}
assert(*X.rbegin());
used[*X.rbegin()] = false;
X.pop_back();
}
#if DEBUG == 1
cerr << "{ ";
for (auto x : X) {
cerr << x << ", ";
}
cerr << "}" << endl;
#endif
}
if (X.empty()) {
return 0;
}
for (auto i = 0u; i < X.size(); i++) {
cout << X[i];
if (i < X.size() - 1) {
cout << " ";
} else {
cout << endl;
}
}
} | #define DEBUG 0
/**
* File : B.cpp
* Author : Kazune Takahashi
* Created : 7/21/2019, 9:18:18 PM
* Powered by Visual Studio Code
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint {
public:
static ll MOD;
ll x;
mint() : x(0) {}
mint(ll x) : x(x % MOD) {}
mint operator-() const { return x ? MOD - x : 0; }
mint &operator+=(const mint &a) {
if ((x += a.x) >= MOD) {
x -= MOD;
}
return *this;
}
mint &operator-=(const mint &a) { return *this += -a; }
mint &operator*=(const mint &a) {
(x *= a.x) %= MOD;
return *this;
}
mint &operator/=(const mint &a) {
mint b{a};
return *this *= b.power(MOD - 2);
}
mint operator+(const mint &a) const { return mint(*this) += a; }
mint operator-(const mint &a) const { return mint(*this) -= a; }
mint operator*(const mint &a) const { return mint(*this) *= a; }
mint operator/(const mint &a) const { return mint(*this) /= a; }
bool operator<(const mint &a) const { return x < a.x; }
bool operator==(const mint &a) const { return x == a.x; }
const mint power(ll N) {
if (N == 0) {
return 1;
} else if (N % 2 == 1) {
return *this * power(N - 1);
} else {
mint half = power(N / 2);
return half * half;
}
}
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination {
public:
vector<mint> inv, fact, factinv;
static int MAX_SIZE;
combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE) {
inv[1] = 1;
for (auto i = 2; i < MAX_SIZE; i++) {
inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
}
fact[0] = factinv[0] = 1;
for (auto i = 1; i < MAX_SIZE; i++) {
fact[i] = mint(i) * fact[i - 1];
factinv[i] = inv[i] * factinv[i - 1];
}
}
mint operator()(int n, int k) {
if (n >= 0 && k >= 0 && n - k >= 0) {
return fact[n] * factinv[k] * factinv[n - k];
}
return 0;
}
};
int combination::MAX_SIZE = 1e6 + 10;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes() {
cout << "Yes" << endl;
exit(0);
}
void No() {
cout << "No" << endl;
exit(0);
}
int N;
ll K;
int A[200010];
int B[200010];
int T[200010];
void test() {
if (N * K > 100) {
assert(false);
}
vector<int> V(N * K);
for (auto k = 0; k < K; k++) {
for (auto i = 0; i < N; i++) {
V[k * N + i] = A[i];
}
}
deque<int> X;
vector<bool> used(200010, false);
for (auto i = 0; i < N * K; i++) {
#if DEBUG == 1
cerr << "V[" << i << "] = " << V[i] << endl;
#endif
if (!used[V[i]]) {
used[V[i]] = true;
X.push_back(V[i]);
} else {
while (*X.rbegin() != V[i]) {
assert(used[*X.rbegin()]);
used[*X.rbegin()] = false;
X.pop_back();
}
assert(*X.rbegin());
used[*X.rbegin()] = false;
X.pop_back();
}
#if DEBUG == 1
cerr << "{ ";
for (auto x : X) {
cerr << x << ", ";
}
cerr << "}" << endl;
#endif
}
}
void make_B() {
fill(B, B + N, -1);
vector<int> T(200010, -1);
for (auto k = 0; k < 2; k++) {
for (auto i = 0; i < N; i++) {
if (T[A[i]] != -1) {
B[i] = T[A[i]];
}
T[A[i]] = i;
}
}
#if DEBUG == 1
for (auto i = 0; i < N; i++) {
cerr << "B[" << i << "] = " << B[i] << endl;
}
#endif
}
int circ() {
int cnt = 1;
int now = N - 1;
while (true) {
if (B[now] >= now) {
++cnt;
}
now = B[now];
T[now] = -cnt;
if (now > 0) {
--now;
} else {
break;
}
}
return cnt;
}
int main() {
cin >> N >> K;
for (auto i = 0; i < N; i++) {
cin >> A[i];
}
make_B();
int C = circ();
#if DEBUG == 1
cerr << "circ() = " << C << endl;
#endif
K %= C;
for (auto i = 0; i < N; i++) {
T[i] += C + 1;
}
int start = -1;
for (auto i = N - 1; i >= 0; i--) {
if (T[i] == K) {
start = i;
break;
}
}
if (start == -1) {
cout << endl;
return 0;
}
deque<int> X;
vector<bool> used(200010, false);
for (auto i = start; i < N; i++) {
#if DEBUG == 1
cerr << "A[" << i << "] = " << A[i] << endl;
#endif
if (!used[A[i]]) {
used[A[i]] = true;
X.push_back(A[i]);
} else {
while (*X.rbegin() != A[i]) {
assert(used[*X.rbegin()]);
used[*X.rbegin()] = false;
X.pop_back();
}
assert(*X.rbegin());
used[*X.rbegin()] = false;
X.pop_back();
}
#if DEBUG == 1
cerr << "{ ";
for (auto x : X) {
cerr << x << ", ";
}
cerr << "}" << endl;
#endif
}
if (X.empty()) {
return 0;
}
for (auto i = 0u; i < X.size(); i++) {
cout << X[i];
if (i < X.size() - 1) {
cout << " ";
} else {
cout << endl;
}
}
} | replace | 214 | 215 | 214 | 218 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
inline void prep() {
cin.tie(0);
cin.sync_with_stdio(0);
};
const double pi = acos(-1.);
// printf(%.12lf\n,mid);
// __builtin_popcount(int) count 1's in binary
// get decimal part of number
/*
double intpart;
double thiss = modf((double)(n*m)/(double)k, &intpart);
*/
// round = (int)(num+.5)
long long mod = 1000000007;
long long mymod(long long num) {
if (num < 0 && !(num % mod == 0)) {
if (mod == 1) {
return 0;
}
return mod - abs(num) % mod;
} else {
return num % mod;
}
}
/*string sentence = And I feel fine...;
istringstream iss(sentence);
*/
double eps = .000000001;
set<int> nums[200001];
int main() {
prep();
int n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
nums[arr[i]].insert(i);
}
if (n == 1) {
if (k % 2 == 1) {
cout << arr[0];
}
return 0;
}
int graph[200001];
int graph2[200001];
memset(graph, -1, sizeof(int) * 200001);
memset(graph2, -1, sizeof(int) * 200001);
for (int i = 0; i < n; i++) {
set<int> mine = nums[arr[i]];
set<int>::iterator it = mine.upper_bound(i);
if (it == mine.end()) {
graph[i] = *(mine.begin());
} else {
graph[i] = *it;
graph2[i] = *it;
}
}
int cur = (graph[0] + 1) % n;
int times = 1 + (cur == 1);
while (cur != 0) {
int before = cur;
if (graph[cur] <= before) {
// cout << graph[cur] << " " << before << endl;
times++;
}
cur = (graph[cur] + 1) % n;
}
// cout << times << endl;
if (k % times == 0) {
return 0;
}
int other = k % times;
cur = 0;
times = 0;
while (times < other - 1) {
int before = cur;
if (graph[cur] <= before) {
times++;
}
cur = (graph[cur] + 1) % n;
}
vector<int> res;
while (cur < n) {
int next = graph2[cur];
if (next == -1) {
res.push_back(arr[cur]);
cur++;
} else {
cur = graph2[cur] + 1;
}
}
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
inline void prep() {
cin.tie(0);
cin.sync_with_stdio(0);
};
const double pi = acos(-1.);
// printf(%.12lf\n,mid);
// __builtin_popcount(int) count 1's in binary
// get decimal part of number
/*
double intpart;
double thiss = modf((double)(n*m)/(double)k, &intpart);
*/
// round = (int)(num+.5)
long long mod = 1000000007;
long long mymod(long long num) {
if (num < 0 && !(num % mod == 0)) {
if (mod == 1) {
return 0;
}
return mod - abs(num) % mod;
} else {
return num % mod;
}
}
/*string sentence = And I feel fine...;
istringstream iss(sentence);
*/
double eps = .000000001;
set<int> nums[200001];
int main() {
prep();
long long n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
nums[arr[i]].insert(i);
}
if (n == 1) {
if (k % 2 == 1) {
cout << arr[0];
}
return 0;
}
int graph[200001];
int graph2[200001];
memset(graph, -1, sizeof(int) * 200001);
memset(graph2, -1, sizeof(int) * 200001);
for (int i = 0; i < n; i++) {
set<int> mine = nums[arr[i]];
set<int>::iterator it = mine.upper_bound(i);
if (it == mine.end()) {
graph[i] = *(mine.begin());
} else {
graph[i] = *it;
graph2[i] = *it;
}
}
int cur = (graph[0] + 1) % n;
int times = 1 + (cur == 1);
while (cur != 0) {
int before = cur;
if (graph[cur] <= before) {
// cout << graph[cur] << " " << before << endl;
times++;
}
cur = (graph[cur] + 1) % n;
}
// cout << times << endl;
if (k % times == 0) {
return 0;
}
int other = k % times;
cur = 0;
times = 0;
while (times < other - 1) {
int before = cur;
if (graph[cur] <= before) {
times++;
}
cur = (graph[cur] + 1) % n;
}
vector<int> res;
while (cur < n) {
int next = graph2[cur];
if (next == -1) {
res.push_back(arr[cur]);
cur++;
} else {
cur = graph2[cur] + 1;
}
}
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
return 0;
} | replace | 37 | 38 | 37 | 38 | 0 | |
p02964 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long N, K, A[1 << 18], P[1 << 18], nex[1 << 18];
long long par[1 << 18][66];
bool used[1 << 18];
int main() {
cin >> N >> K;
for (int i = 0; i < N; i++) {
cin >> A[i];
A[N + i] = A[i];
}
for (int i = N * 2 - 1; i >= 0; i--) {
nex[i] = P[A[i]] + 1;
P[A[i]] = i;
}
for (int i = 0; i < N; i++)
par[i][0] = (nex[i] - i);
for (int i = 1; i < 66; i++) {
for (int j = 0; j < N; j++) {
long long v1 = par[j][i - 1];
long long pos = (j + v1) % N;
long long v2 = par[pos][i - 1];
long long v3 = v1 + v2;
v3 = min(v3, (1LL << 61));
par[j][i] = v3;
}
}
long long cx = 0, px = N * K;
for (int i = 65; i >= 0; i--) {
if (px >= par[cx][i]) {
px -= par[cx][i];
cx = (cx + par[cx][i]) % N;
}
}
vector<int> vec;
for (long long i = N * K - px; i < N * K; i++) {
if (used[A[i % N]] == false) {
vec.push_back(A[i % N]);
used[A[i % N]] = true;
} else {
while (used[A[i % N]] == true) {
used[vec[vec.size() - 1]] = false;
vec.pop_back();
}
}
}
for (int i = 0; i < vec.size(); i++) {
if (i)
printf(" ");
printf("%d", vec[i]);
}
printf("\n");
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long N, K, A[1 << 19], P[1 << 19], nex[1 << 19];
long long par[1 << 19][66];
bool used[1 << 19];
int main() {
cin >> N >> K;
for (int i = 0; i < N; i++) {
cin >> A[i];
A[N + i] = A[i];
}
for (int i = N * 2 - 1; i >= 0; i--) {
nex[i] = P[A[i]] + 1;
P[A[i]] = i;
}
for (int i = 0; i < N; i++)
par[i][0] = (nex[i] - i);
for (int i = 1; i < 66; i++) {
for (int j = 0; j < N; j++) {
long long v1 = par[j][i - 1];
long long pos = (j + v1) % N;
long long v2 = par[pos][i - 1];
long long v3 = v1 + v2;
v3 = min(v3, (1LL << 61));
par[j][i] = v3;
}
}
long long cx = 0, px = N * K;
for (int i = 65; i >= 0; i--) {
if (px >= par[cx][i]) {
px -= par[cx][i];
cx = (cx + par[cx][i]) % N;
}
}
vector<int> vec;
for (long long i = N * K - px; i < N * K; i++) {
if (used[A[i % N]] == false) {
vec.push_back(A[i % N]);
used[A[i % N]] = true;
} else {
while (used[A[i % N]] == true) {
used[vec[vec.size() - 1]] = false;
vec.pop_back();
}
}
}
for (int i = 0; i < vec.size(); i++) {
if (i)
printf(" ");
printf("%d", vec[i]);
}
printf("\n");
return 0;
} | replace | 5 | 8 | 5 | 8 | -11 | |
p02964 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define all(v) begin(v), end(v)
#define debug(x) // std::cerr<<#x<<": "<<x<<"\n"
#define debug2(x, y) // std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n"
#define debug3( \
x, y, \
z) // std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n"
using ll = long long;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
template <typename T> using vvec = std::vector<std::vector<T>>;
template <typename T> auto make_v(size_t sz) { return std::vector<T>(sz); }
template <typename T, typename... Ts> auto make_v(size_t sz, Ts... ts) {
return std::vector<decltype(make_v<T>(ts...))>(sz, make_v<T>(ts...));
}
template <typename T> void fill_v(T &var, const T &x) { var = x; }
template <typename V, typename T> void fill_v(V &v, const T &x) {
for (auto &&w : v) {
fill_v(w, x);
}
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
int sz = v.size();
s << "\n";
rep(i, sz) {
s << v[i];
if (i < sz - 1) {
s << "\t";
}
}
s << "\n";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::vector<std::vector<T>> &v) {
for (auto &&w : v) {
s << w;
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
int sz = v.size();
s << "\n";
rep(i, sz) {
s << v[i];
if (i < sz - 1) {
s << "\t";
}
}
s << "\n";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<std::deque<T>> &v) {
for (auto &&w : v) {
s << w;
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &v) {
s << "\n";
for (auto &&elm : v) {
s << elm << "\t";
}
s << "\n";
return s;
}
inline void scan(int &a) { scanf("%d", &a); }
inline void scan(ll &a) { scanf("%lld", &a); }
inline void scan(char &a) { scanf(" %c", &a); }
inline void scan(double &a) { scanf("%lf", &a); }
inline void scan(std::string &s) {
char BUF[3000000];
scanf(" %s", BUF);
s = std::string(BUF);
}
template <typename T> inline void scan(std::vector<T> &v) {
for (auto &&sv : v) {
scan(sv);
}
}
template <typename First, typename... Args>
inline void scan(First &f, Args &...args) {
scan(f);
scan(args...);
}
inline void print(int a) { printf("%d\n", a); }
inline void print(ll a) { printf("%lld\n", a); }
inline void print(double a) { printf("%.12f\n", a); }
inline void print(std::string s) { std::cout << s << "\n"; }
using namespace std;
struct Monoid {
using T = int; // type or class of monoid
static T operate(const T &left, const T &right) {
if (left != -1)
return left;
else
return right;
}
static constexpr T identity() { return T(-1); } // 単位元
};
struct M_act {
using T = int;
using OperatorMonoid = Monoid;
static T act(const OperatorMonoid::T &m, const T &a) {
if (m != -1)
return m;
else
return a;
}
};
template <class M_act> class SegTree {
private:
using T = typename M_act::T;
using OM = typename M_act::OperatorMonoid;
using M = typename OM::T;
const int n, h;
std::vector<T> uv; // underlying values
std::vector<M> deferred_action;
void propagate(const M &m, int i) {
if (i < n)
deferred_action[i] =
OM::operate(m, deferred_action[i]); // if not leaves, unite actions
else {
i -= n;
uv[i] = M_act::act(m, uv[i]); // if leaves, act immediately
}
}
void propagate_from(int i) {
if (deferred_action[i] == OM::identity()) {
return;
}
propagate(deferred_action[i], i << 1);
propagate(deferred_action[i], i << 1 | 1);
deferred_action[i] = OM::identity();
}
void propagate_to_uv(int i) {
i += n;
for (int j = h; j > 0; --j)
propagate_from(i >> j);
}
int lg(int n_) {
n_ -= 1;
int h_ = 0;
while (n_ >> h_)
++h_;
return h_;
}
public:
SegTree(int n_, const T &x)
: n(n_), h(lg(n)), uv(n, x), deferred_action(n, OM::identity()) {}
SegTree(const std::vector<T> &v)
: n(v.size()), h(lg(n)), uv(v), deferred_action(n, OM::identity()) {}
template <class Itr>
SegTree(Itr first, Itr last)
: n(std::distance(first, last)), h(lg(n)), uv(first, last),
deferred_action(n, OM::identity()) {}
void act(int l, int r, const M &m) {
propagate_to_uv(l);
propagate_to_uv(r - 1);
l += n, r += n;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1)
propagate(m, l++);
if (r & 1)
propagate(m, r - 1);
}
}
void propagate_all() {
for (int i = 1; i < n; ++i)
propagate_from(i);
}
void set(int i, const T &x) {
propagate_to_uv(i);
uv[i] = x;
}
T get(int i) {
T ret = uv[i];
i += n;
while (i >>= 1)
ret = M_act::act(deferred_action[i], ret);
return ret;
}
std::vector<T> get_v() { return uv; }
};
int main() {
int n;
ll k;
scan(n, k);
vi a(n);
scan(a);
int m = 200001;
m = 10;
vvi indices(m);
rep(i, n) { indices[a[i]].push_back(i); }
debug(indices);
vi remain(n + 1, n);
{
vi wolfi;
vi lefti;
set<int> si;
for (int i = n - 1; i >= 0; --i) {
if (si.find(a[i]) == end(si)) {
wolfi.push_back(i);
si.insert(a[i]);
}
}
reverse(all(wolfi));
debug(wolfi);
for (int ind : wolfi) {
int lb = lower_bound(all(indices[a[ind]]), ind) - begin(indices[a[ind]]);
if (lb == 0) {
lefti.push_back(0);
} else {
lefti.push_back(indices[a[ind]][lb - 1] + 1);
}
}
debug(lefti);
int sz = wolfi.size();
auto segt = SegTree<M_act>(n, n);
for (int i = sz - 1; i >= 0; --i) {
remain[wolfi[i]] = wolfi[i];
}
debug(remain);
for (int i = n - 1; i >= 0; --i) {
if (remain[i] != n) {
continue;
}
auto ub = upper_bound(all(indices[a[i]]), i);
assert(ub != end(indices[a[i]]));
remain[i] = remain[(*ub) + 1];
}
debug(remain);
}
vi first_index(m, -1);
rep(i, n) {
if (first_index[a[i]] == -1) {
first_index[a[i]] = i;
}
}
debug(first_index);
int t;
int prev = -1; // first index remaining
vi prevs;
for (int i = 1; true; ++i) {
t = i;
if (prev == -1) {
prev = remain[0];
} else {
int fi = first_index[a[prev]];
prev = remain[fi + 1];
}
prevs.push_back(prev);
debug(prev);
if (prev == n) {
break;
}
}
debug(t);
int target = k % t;
if (target == 0) {
return 0;
}
int start_index;
if (target == 1) {
start_index = 0;
} else {
int prev = prevs[target - 2];
start_index = first_index[a[prev]] + 1;
}
vi ans;
for (int i = start_index;;) {
if (remain[i] == n) {
break;
}
ans.push_back(remain[i]);
i = remain[i] + 1;
}
debug(ans);
int sz = ans.size();
rep(i, sz) { printf("%d%c", a[ans[i]], " \n"[i == sz - 1]); }
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define all(v) begin(v), end(v)
#define debug(x) // std::cerr<<#x<<": "<<x<<"\n"
#define debug2(x, y) // std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n"
#define debug3( \
x, y, \
z) // std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n"
using ll = long long;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
template <typename T> using vvec = std::vector<std::vector<T>>;
template <typename T> auto make_v(size_t sz) { return std::vector<T>(sz); }
template <typename T, typename... Ts> auto make_v(size_t sz, Ts... ts) {
return std::vector<decltype(make_v<T>(ts...))>(sz, make_v<T>(ts...));
}
template <typename T> void fill_v(T &var, const T &x) { var = x; }
template <typename V, typename T> void fill_v(V &v, const T &x) {
for (auto &&w : v) {
fill_v(w, x);
}
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
int sz = v.size();
s << "\n";
rep(i, sz) {
s << v[i];
if (i < sz - 1) {
s << "\t";
}
}
s << "\n";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::vector<std::vector<T>> &v) {
for (auto &&w : v) {
s << w;
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
int sz = v.size();
s << "\n";
rep(i, sz) {
s << v[i];
if (i < sz - 1) {
s << "\t";
}
}
s << "\n";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<std::deque<T>> &v) {
for (auto &&w : v) {
s << w;
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &v) {
s << "\n";
for (auto &&elm : v) {
s << elm << "\t";
}
s << "\n";
return s;
}
inline void scan(int &a) { scanf("%d", &a); }
inline void scan(ll &a) { scanf("%lld", &a); }
inline void scan(char &a) { scanf(" %c", &a); }
inline void scan(double &a) { scanf("%lf", &a); }
inline void scan(std::string &s) {
char BUF[3000000];
scanf(" %s", BUF);
s = std::string(BUF);
}
template <typename T> inline void scan(std::vector<T> &v) {
for (auto &&sv : v) {
scan(sv);
}
}
template <typename First, typename... Args>
inline void scan(First &f, Args &...args) {
scan(f);
scan(args...);
}
inline void print(int a) { printf("%d\n", a); }
inline void print(ll a) { printf("%lld\n", a); }
inline void print(double a) { printf("%.12f\n", a); }
inline void print(std::string s) { std::cout << s << "\n"; }
using namespace std;
struct Monoid {
using T = int; // type or class of monoid
static T operate(const T &left, const T &right) {
if (left != -1)
return left;
else
return right;
}
static constexpr T identity() { return T(-1); } // 単位元
};
struct M_act {
using T = int;
using OperatorMonoid = Monoid;
static T act(const OperatorMonoid::T &m, const T &a) {
if (m != -1)
return m;
else
return a;
}
};
template <class M_act> class SegTree {
private:
using T = typename M_act::T;
using OM = typename M_act::OperatorMonoid;
using M = typename OM::T;
const int n, h;
std::vector<T> uv; // underlying values
std::vector<M> deferred_action;
void propagate(const M &m, int i) {
if (i < n)
deferred_action[i] =
OM::operate(m, deferred_action[i]); // if not leaves, unite actions
else {
i -= n;
uv[i] = M_act::act(m, uv[i]); // if leaves, act immediately
}
}
void propagate_from(int i) {
if (deferred_action[i] == OM::identity()) {
return;
}
propagate(deferred_action[i], i << 1);
propagate(deferred_action[i], i << 1 | 1);
deferred_action[i] = OM::identity();
}
void propagate_to_uv(int i) {
i += n;
for (int j = h; j > 0; --j)
propagate_from(i >> j);
}
int lg(int n_) {
n_ -= 1;
int h_ = 0;
while (n_ >> h_)
++h_;
return h_;
}
public:
SegTree(int n_, const T &x)
: n(n_), h(lg(n)), uv(n, x), deferred_action(n, OM::identity()) {}
SegTree(const std::vector<T> &v)
: n(v.size()), h(lg(n)), uv(v), deferred_action(n, OM::identity()) {}
template <class Itr>
SegTree(Itr first, Itr last)
: n(std::distance(first, last)), h(lg(n)), uv(first, last),
deferred_action(n, OM::identity()) {}
void act(int l, int r, const M &m) {
propagate_to_uv(l);
propagate_to_uv(r - 1);
l += n, r += n;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1)
propagate(m, l++);
if (r & 1)
propagate(m, r - 1);
}
}
void propagate_all() {
for (int i = 1; i < n; ++i)
propagate_from(i);
}
void set(int i, const T &x) {
propagate_to_uv(i);
uv[i] = x;
}
T get(int i) {
T ret = uv[i];
i += n;
while (i >>= 1)
ret = M_act::act(deferred_action[i], ret);
return ret;
}
std::vector<T> get_v() { return uv; }
};
int main() {
int n;
ll k;
scan(n, k);
vi a(n);
scan(a);
int m = 200001;
// m = 10;
vvi indices(m);
rep(i, n) { indices[a[i]].push_back(i); }
debug(indices);
vi remain(n + 1, n);
{
vi wolfi;
vi lefti;
set<int> si;
for (int i = n - 1; i >= 0; --i) {
if (si.find(a[i]) == end(si)) {
wolfi.push_back(i);
si.insert(a[i]);
}
}
reverse(all(wolfi));
debug(wolfi);
for (int ind : wolfi) {
int lb = lower_bound(all(indices[a[ind]]), ind) - begin(indices[a[ind]]);
if (lb == 0) {
lefti.push_back(0);
} else {
lefti.push_back(indices[a[ind]][lb - 1] + 1);
}
}
debug(lefti);
int sz = wolfi.size();
auto segt = SegTree<M_act>(n, n);
for (int i = sz - 1; i >= 0; --i) {
remain[wolfi[i]] = wolfi[i];
}
debug(remain);
for (int i = n - 1; i >= 0; --i) {
if (remain[i] != n) {
continue;
}
auto ub = upper_bound(all(indices[a[i]]), i);
assert(ub != end(indices[a[i]]));
remain[i] = remain[(*ub) + 1];
}
debug(remain);
}
vi first_index(m, -1);
rep(i, n) {
if (first_index[a[i]] == -1) {
first_index[a[i]] = i;
}
}
debug(first_index);
int t;
int prev = -1; // first index remaining
vi prevs;
for (int i = 1; true; ++i) {
t = i;
if (prev == -1) {
prev = remain[0];
} else {
int fi = first_index[a[prev]];
prev = remain[fi + 1];
}
prevs.push_back(prev);
debug(prev);
if (prev == n) {
break;
}
}
debug(t);
int target = k % t;
if (target == 0) {
return 0;
}
int start_index;
if (target == 1) {
start_index = 0;
} else {
int prev = prevs[target - 2];
start_index = first_index[a[prev]] + 1;
}
vi ans;
for (int i = start_index;;) {
if (remain[i] == n) {
break;
}
ans.push_back(remain[i]);
i = remain[i] + 1;
}
debug(ans);
int sz = ans.size();
rep(i, sz) { printf("%d%c", a[ans[i]], " \n"[i == sz - 1]); }
return 0;
}
| replace | 241 | 242 | 241 | 242 | 0 | |
p02964 | C++ | Runtime Error | // #define DEB1 //input from input.txt
// #define NDEBUG
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <vector>
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define ALL(t) (t).begin(), (t).end()
#define FILL(a, c) fill(a, a + sizeof(a) / sizeof(*a), c)
#define FILL2D(A, c) fill(A[0], A[0] + sizeof(A) / sizeof(**A), c)
#define FILL3D(A, c) fill(A[0][0], A[0][0] + sizeof(A) / sizeof(***A), c)
#define REP(i, m) for (int i = 0; i < (m); ++i)
// closed interval, inverse repeat
#define CIREP(i, start, fin) for (int i = (start); i >= (fin); i--)
#define CREP(i, start, fin) for (int i = (start); i <= (fin); i++)
#define RNG(i, start, fin) for (int i = (start); i < (fin); i++)
#define IN(a, x, b) (a <= x && x < b)
#define PB push_back
#define SZ(x) (int)(x).size()
using namespace std;
using LL = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<LL, LL>;
using VI = vector<int>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VVI = vector<VI>;
const LL LINF = 1001002003004005006ll;
/*
template<typename T>inline istream& operator>>(istream&i,vector<T>&v)
{REP(j,SZ(v))i>>v[j];return i;}
template<typename T>string join(const vector<T>&v)
{stringstream s;REP(i,SZ(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const vector<T>&v)
{if(SZ(v))o<<join(v);return o;}
*/
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
/*
constant values
*/
// const int dx[] = {1,0,-1,0};
// const int dy[] = {0,1,0,-1};
// const int MX = 31001;
// const ll INF= 1LL<<60 ;//INF = 1001001001001; //1e9
// Main program
void solve() {
LL N;
cin >> N;
LL K;
cin >> K;
K *= N;
// int max_k = log2(N*K);
map<int, VLL> key_id; // key_id
// input, next[0][]
VLL A(N);
VLL W(N);
// VI B(N);
cerr << "error test" << endl;
REP(i, 2 * N) {
if (i < N) {
cin >> A[i];
// A[N+i]=B[i]; // 1 2 3 2 3 1 2 3 2 3
}
// same exist before
int a = A[i % N];
if (key_id.count(a) != 0) {
int id = key_id[a].back(); //- - - 1 2, 0 3 4 ..
if (id < N)
W[id] = i - id + 1;
}
// add key
key_id[a].PB(i);
// several id for one key
}
/*
REP(i,N){
cin >> A[i];
key_id[A[i]].PB(i);
}
VLL W(N);
for (auto kv:key_id){
int s = kv.second.size();
kv.second.PB(kv.second[0]+N);
REP(i,s){
W[kv.second[i]] = kv.second[i+1] - kv.second[i] +1;
}
}
*/
// doubling
const int L = 60;
// const int inf = LLONG_MAX/3;
VVLL nx(L, W);
/*
REP(_,L){
VLL&x=nx.back();
VLL Y(N);
REP(i,N) Y[i]=min(LINF,x[i] + x[(i+x[i])%N]);
nx.PB(Y);
}
*/
REP(k, L) {
REP(i, N) {
LL nowid = i + nx[k][i];
LL nextid = nowid + nx[k][nowid % N];
nx[k + 1][i] = min(LINF, nextid - i); //= next[k][i] + next[k][nowid%N]
}
}
LL cur = 0;
CIREP(i, L, 0) {
if (cur + nx[i][cur % N] <= K)
cur += nx[i][cur % N];
}
// do as the task
if (cur == K)
return;
VLL ans;
set<int> has;
CREP(i, cur % N, N - 1) {
if (has.count(A[i])) {
while (1) {
int v = ans.back();
ans.pop_back();
has.erase(v);
if (v == A[i])
break;
}
} else {
ans.PB(A[i]);
has.insert(A[i]);
}
}
REP(i, SZ(ans)) {
cout << ans[i];
if (i < SZ(ans) - 1)
cout << " ";
else
cout << endl;
}
return;
}
// ----------------------------------------------
int main() {
#ifdef DEB1
cout << "DEBUG MODE" << endl;
ifstream in("input.txt"); // for debug
cin.rdbuf(in.rdbuf()); // for debug
#endif
int T = 1;
for (int i = 0; i < T; ++i) {
solve();
}
return 0;
} | // #define DEB1 //input from input.txt
// #define NDEBUG
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <vector>
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define ALL(t) (t).begin(), (t).end()
#define FILL(a, c) fill(a, a + sizeof(a) / sizeof(*a), c)
#define FILL2D(A, c) fill(A[0], A[0] + sizeof(A) / sizeof(**A), c)
#define FILL3D(A, c) fill(A[0][0], A[0][0] + sizeof(A) / sizeof(***A), c)
#define REP(i, m) for (int i = 0; i < (m); ++i)
// closed interval, inverse repeat
#define CIREP(i, start, fin) for (int i = (start); i >= (fin); i--)
#define CREP(i, start, fin) for (int i = (start); i <= (fin); i++)
#define RNG(i, start, fin) for (int i = (start); i < (fin); i++)
#define IN(a, x, b) (a <= x && x < b)
#define PB push_back
#define SZ(x) (int)(x).size()
using namespace std;
using LL = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<LL, LL>;
using VI = vector<int>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VVI = vector<VI>;
const LL LINF = 1001002003004005006ll;
/*
template<typename T>inline istream& operator>>(istream&i,vector<T>&v)
{REP(j,SZ(v))i>>v[j];return i;}
template<typename T>string join(const vector<T>&v)
{stringstream s;REP(i,SZ(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const vector<T>&v)
{if(SZ(v))o<<join(v);return o;}
*/
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
/*
constant values
*/
// const int dx[] = {1,0,-1,0};
// const int dy[] = {0,1,0,-1};
// const int MX = 31001;
// const ll INF= 1LL<<60 ;//INF = 1001001001001; //1e9
// Main program
void solve() {
LL N;
cin >> N;
LL K;
cin >> K;
K *= N;
// int max_k = log2(N*K);
map<int, VLL> key_id; // key_id
// input, next[0][]
VLL A(N);
VLL W(N);
// VI B(N);
cerr << "error test" << endl;
REP(i, 2 * N) {
if (i < N) {
cin >> A[i];
// A[N+i]=B[i]; // 1 2 3 2 3 1 2 3 2 3
}
// same exist before
int a = A[i % N];
if (key_id.count(a) != 0) {
int id = key_id[a].back(); //- - - 1 2, 0 3 4 ..
if (id < N)
W[id] = i - id + 1;
}
// add key
key_id[a].PB(i);
// several id for one key
}
/*
REP(i,N){
cin >> A[i];
key_id[A[i]].PB(i);
}
VLL W(N);
for (auto kv:key_id){
int s = kv.second.size();
kv.second.PB(kv.second[0]+N);
REP(i,s){
W[kv.second[i]] = kv.second[i+1] - kv.second[i] +1;
}
}
*/
// doubling
const int L = 60;
// const int inf = LLONG_MAX/3;
VVLL nx(L + 1, W);
/*
REP(_,L){
VLL&x=nx.back();
VLL Y(N);
REP(i,N) Y[i]=min(LINF,x[i] + x[(i+x[i])%N]);
nx.PB(Y);
}
*/
REP(k, L) {
REP(i, N) {
LL nowid = i + nx[k][i];
LL nextid = nowid + nx[k][nowid % N];
nx[k + 1][i] = min(LINF, nextid - i); //= next[k][i] + next[k][nowid%N]
}
}
LL cur = 0;
CIREP(i, L, 0) {
if (cur + nx[i][cur % N] <= K)
cur += nx[i][cur % N];
}
// do as the task
if (cur == K)
return;
VLL ans;
set<int> has;
CREP(i, cur % N, N - 1) {
if (has.count(A[i])) {
while (1) {
int v = ans.back();
ans.pop_back();
has.erase(v);
if (v == A[i])
break;
}
} else {
ans.PB(A[i]);
has.insert(A[i]);
}
}
REP(i, SZ(ans)) {
cout << ans[i];
if (i < SZ(ans) - 1)
cout << " ";
else
cout << endl;
}
return;
}
// ----------------------------------------------
int main() {
#ifdef DEB1
cout << "DEBUG MODE" << endl;
ifstream in("input.txt"); // for debug
cin.rdbuf(in.rdbuf()); // for debug
#endif
int T = 1;
for (int i = 0; i < T; ++i) {
solve();
}
return 0;
} | replace | 141 | 142 | 141 | 142 | -11 | error test
|
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll INF = 1e9 + 7;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
vector<ll> ar = a;
reverse(ar.begin(), ar.end());
vector<set<ll>> pos(2 * 1e5);
for (int i = 0; i < n; i++) {
pos[ar[i]].insert(i + 1);
if (pos[ar[i]].size() == 1) {
pos[ar[i]].insert(i + 1 + n);
}
}
vector<ll> start(n + 1);
int p = 1;
ll v = ar[0];
ll ans = 0;
while (true) {
ll next = *(pos[v].lower_bound(p + 1));
if (next == n) {
start[ans] = next;
break;
} else if (next == 2 * n) {
ans++;
start[ans] = next - n;
break;
} else if (next > n) {
p = next + 1 - n;
ans++;
start[ans] = p - 1;
} else {
p = next + 1;
start[ans] = next;
}
v = ar[p - 1];
}
ans++;
ll count = k % ans;
if (count == 0)
return 0;
ll s = start[ans - count];
stack<ll> sta;
set<ll> tmp;
for (int i = n - s; i < n; i++) {
if (tmp.find(a[i]) == tmp.end()) {
tmp.insert(a[i]);
sta.push(a[i]);
} else {
while (true) {
ll cand = sta.top();
sta.pop();
tmp.erase(cand);
if (cand == a[i]) {
break;
}
}
}
}
stack<ll> ansq;
ll size = sta.size();
for (int i = 0; i < size; i++) {
ll cand = sta.top();
sta.pop();
ansq.push(cand);
}
ll anss = ansq.size();
for (int i = 0; i < anss; i++) {
ll cand = ansq.top();
ansq.pop();
cout << cand << " ";
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll INF = 1e9 + 7;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
vector<ll> ar = a;
reverse(ar.begin(), ar.end());
vector<set<ll>> pos(2 * 1e5 + 10);
for (int i = 0; i < n; i++) {
pos[ar[i]].insert(i + 1);
if (pos[ar[i]].size() == 1) {
pos[ar[i]].insert(i + 1 + n);
}
}
vector<ll> start(n + 1);
int p = 1;
ll v = ar[0];
ll ans = 0;
while (true) {
ll next = *(pos[v].lower_bound(p + 1));
if (next == n) {
start[ans] = next;
break;
} else if (next == 2 * n) {
ans++;
start[ans] = next - n;
break;
} else if (next > n) {
p = next + 1 - n;
ans++;
start[ans] = p - 1;
} else {
p = next + 1;
start[ans] = next;
}
v = ar[p - 1];
}
ans++;
ll count = k % ans;
if (count == 0)
return 0;
ll s = start[ans - count];
stack<ll> sta;
set<ll> tmp;
for (int i = n - s; i < n; i++) {
if (tmp.find(a[i]) == tmp.end()) {
tmp.insert(a[i]);
sta.push(a[i]);
} else {
while (true) {
ll cand = sta.top();
sta.pop();
tmp.erase(cand);
if (cand == a[i]) {
break;
}
}
}
}
stack<ll> ansq;
ll size = sta.size();
for (int i = 0; i < size; i++) {
ll cand = sta.top();
sta.pop();
ansq.push(cand);
}
ll anss = ansq.size();
for (int i = 0; i < anss; i++) {
ll cand = ansq.top();
ansq.pop();
cout << cand << " ";
}
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < n; i++)
/*
* Modint struct
* library author : @snuke
* */
const int mod = 998244353;
struct mint {
long long x; // typedef long long ll;
mint(long long x = 0) : x((x % mod + mod) % mod) {}
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
struct Combination {
const int MAX = 1000050;
vector<mint> fact;
vector<mint> finv;
Combination() : fact(MAX), finv(MAX) {
for (int i = 0; i < MAX; i++) {
if (i == 0)
fact[i] = (mint)1;
else
fact[i] = fact[i - 1] * (mint)i;
}
finv[MAX - 1] = fact[MAX - 1].inv();
for (int i = MAX - 2; i >= 0; i--) {
finv[i] = finv[i + 1] * (mint)(i + 1);
}
}
mint comb(int n, int r) {
if (n < 0 || r < 0 || n < r)
return 0;
else
return fact[n] * finv[r] * finv[n - r];
}
} C;
ll n, m;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m;
mint ans = 0;
for (int k = 0; k <= m; k++) {
if ((3 * m - k) % 2 != 0)
continue;
ans +=
C.comb(n, k) * (C.comb((3 * m - k) / 2 + n - 1, n - 1) -
(mint)k * C.comb((m - k) / 2 + n - 1, n - 1) -
(mint)(n - k) * C.comb((m - 2 - k) / 2 + n - 1, n - 1));
}
cout << ans << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < n; i++)
/*
* Modint struct
* library author : @snuke
* */
const int mod = 998244353;
struct mint {
long long x; // typedef long long ll;
mint(long long x = 0) : x((x % mod + mod) % mod) {}
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
struct Combination {
const int MAX = 2000050;
vector<mint> fact;
vector<mint> finv;
Combination() : fact(MAX), finv(MAX) {
for (int i = 0; i < MAX; i++) {
if (i == 0)
fact[i] = (mint)1;
else
fact[i] = fact[i - 1] * (mint)i;
}
finv[MAX - 1] = fact[MAX - 1].inv();
for (int i = MAX - 2; i >= 0; i--) {
finv[i] = finv[i + 1] * (mint)(i + 1);
}
}
mint comb(int n, int r) {
if (n < 0 || r < 0 || n < r)
return 0;
else
return fact[n] * finv[r] * finv[n - r];
}
} C;
ll n, m;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m;
mint ans = 0;
for (int k = 0; k <= m; k++) {
if ((3 * m - k) % 2 != 0)
continue;
ans +=
C.comb(n, k) * (C.comb((3 * m - k) / 2 + n - 1, n - 1) -
(mint)k * C.comb((m - k) / 2 + n - 1, n - 1) -
(mint)(n - k) * C.comb((m - 2 - k) / 2 + n - 1, n - 1));
}
cout << ans << '\n';
}
| replace | 56 | 57 | 56 | 57 | 0 | |
p02965 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
constexpr long long int mod = 998244353LL;
struct Factorial {
std::vector<long long int> factorial, inversed_factorial;
Factorial(const int max)
: factorial(max + 1, 1), inversed_factorial(max + 1, 1) {
std::vector<long long int> inv(max + 1, 1);
for (auto i = 2; i <= max; ++i) {
factorial[i] = (factorial[i - 1] * i) % mod;
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
inversed_factorial[i] = inv[i] * inversed_factorial[i - 1] % mod;
}
}
long long int fact(const int n) const { return factorial[n]; }
long long int comb(const int n, const int r) const {
return factorial[n] * inversed_factorial[r] % mod *
inversed_factorial[n - r] % mod;
}
};
int main() {
int n, m;
std::cin >> n >> m;
Factorial factorial(2000000);
auto all = factorial.comb(n - 1 + 3 * m, 3 * m);
auto over = factorial.comb(n + m - 2, m - 1) * n % mod;
long long int odd = 0;
for (auto i = m + 1; i <= n && i <= 3 * m; ++i) {
if ((3 * m - i) % 2 == 0) {
odd += factorial.comb(n - 1 + (3 * m - i) / 2, (3 * m - i) / 2) *
factorial.comb(n, i) % mod;
odd %= mod;
}
}
std::cout << (all - over - odd + 2 * mod) % mod << std::endl;
}
| #include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
constexpr long long int mod = 998244353LL;
struct Factorial {
std::vector<long long int> factorial, inversed_factorial;
Factorial(const int max)
: factorial(max + 1, 1), inversed_factorial(max + 1, 1) {
std::vector<long long int> inv(max + 1, 1);
for (auto i = 2; i <= max; ++i) {
factorial[i] = (factorial[i - 1] * i) % mod;
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
inversed_factorial[i] = inv[i] * inversed_factorial[i - 1] % mod;
}
}
long long int fact(const int n) const { return factorial[n]; }
long long int comb(const int n, const int r) const {
return factorial[n] * inversed_factorial[r] % mod *
inversed_factorial[n - r] % mod;
}
};
int main() {
int n, m;
std::cin >> n >> m;
Factorial factorial(n + 3 * m);
auto all = factorial.comb(n - 1 + 3 * m, 3 * m);
auto over = factorial.comb(n + m - 2, m - 1) * n % mod;
long long int odd = 0;
for (auto i = m + 1; i <= n && i <= 3 * m; ++i) {
if ((3 * m - i) % 2 == 0) {
odd += factorial.comb(n - 1 + (3 * m - i) / 2, (3 * m - i) / 2) *
factorial.comb(n, i) % mod;
odd %= mod;
}
}
std::cout << (all - over - odd + 2 * mod) % mod << std::endl;
}
| replace | 42 | 43 | 42 | 43 | 0 | |
p02965 | C++ | Runtime Error | /*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 2e6;
const ll MOD = 998244353;
ll Efac[N + 3], Einv[N + 3];
ll QPow(ll Va, ll Vb) {
ll Rr = 1;
while (Vb) {
if (Vb & 1)
Rr = Va * Rr % MOD;
Va = Va * Va % MOD;
Vb >>= 1;
}
return Rr;
}
void Process() {
Efac[0] = 1;
for (int i = 1; i <= N; i++)
Efac[i] = Efac[i - 1] * i % MOD;
Einv[N] = QPow(Efac[N], MOD - 2);
for (int i = N - 1; i >= 0; i--)
Einv[i] = Einv[i + 1] * (i + 1) % MOD;
}
ll fC(int Va, int Vb) {
return Efac[Vb] * Einv[Va] % MOD * Einv[Vb - Va] % MOD;
}
int main() {
Process();
int n, m;
scanf("%d%d", &n, &m);
ll Vans = fC(n - 1, 3 * m + n - 1);
Vans = (Vans - fC(n - 1, m + n - 2) * n % MOD + MOD) % MOD;
for (int i = m + 1; i <= n && i <= 3 * m; i++)
if ((3 * m - i) % 2 == 0) {
Vans =
(Vans - fC(i, n) * fC(n - 1, (3 * m - i) / 2 + n - 1) % MOD + MOD) %
MOD;
}
printf("%lld\n", (Vans % MOD + MOD) % MOD);
return 0;
} | /*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 3e6;
const ll MOD = 998244353;
ll Efac[N + 3], Einv[N + 3];
ll QPow(ll Va, ll Vb) {
ll Rr = 1;
while (Vb) {
if (Vb & 1)
Rr = Va * Rr % MOD;
Va = Va * Va % MOD;
Vb >>= 1;
}
return Rr;
}
void Process() {
Efac[0] = 1;
for (int i = 1; i <= N; i++)
Efac[i] = Efac[i - 1] * i % MOD;
Einv[N] = QPow(Efac[N], MOD - 2);
for (int i = N - 1; i >= 0; i--)
Einv[i] = Einv[i + 1] * (i + 1) % MOD;
}
ll fC(int Va, int Vb) {
return Efac[Vb] * Einv[Va] % MOD * Einv[Vb - Va] % MOD;
}
int main() {
Process();
int n, m;
scanf("%d%d", &n, &m);
ll Vans = fC(n - 1, 3 * m + n - 1);
Vans = (Vans - fC(n - 1, m + n - 2) * n % MOD + MOD) % MOD;
for (int i = m + 1; i <= n && i <= 3 * m; i++)
if ((3 * m - i) % 2 == 0) {
Vans =
(Vans - fC(i, n) * fC(n - 1, (3 * m - i) / 2 + n - 1) % MOD + MOD) %
MOD;
}
printf("%lld\n", (Vans % MOD + MOD) % MOD);
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p02965 | C++ | Runtime Error | #include <iostream>
#define llint long long
#define mod 998244353
using namespace std;
llint n, m;
const int FACT_MAX = 4000005;
llint fact[FACT_MAX], fact_inv[FACT_MAX];
llint modpow(llint a, llint n) {
if (n == 0)
return 1;
if (n % 2) {
return ((a % mod) * (modpow(a, n - 1) % mod)) % mod;
} else {
return modpow((a * a) % mod, n / 2) % mod;
}
}
void make_fact() {
llint val = 1;
fact[0] = 1;
for (int i = 1; i < FACT_MAX; i++) {
val *= i;
val %= mod;
fact[i] = val;
}
fact_inv[FACT_MAX - 1] = modpow(fact[FACT_MAX - 1], mod - 2);
for (int i = FACT_MAX - 2; i >= 0; i--) {
fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod;
}
}
llint comb(llint n, llint k) {
llint ret = 1;
ret *= fact[n];
ret *= fact_inv[k], ret %= mod;
ret *= fact_inv[n - k], ret %= mod;
return ret;
}
int main(void) {
cin >> n >> m;
make_fact();
llint ans = 0;
for (int i = 0; i <= m; i++) {
if (m % 2 != i % 2)
continue;
llint rem = (3 * m - i) / 2;
llint tmp = comb(rem + n - 1, n - 1);
if (rem - (m + 1) >= 0)
tmp += mod - (n - i) * comb(rem - (m + 1) + n - 1, n - 1) % mod,
tmp %= mod;
if (rem - m >= 0)
tmp += mod - i * comb(rem - m + n - 1, n - 1) % mod, tmp %= mod;
ans += tmp * comb(n, i) % mod, ans %= mod;
}
cout << ans << endl;
return 0;
} | #include <iostream>
#define llint long long
#define mod 998244353
using namespace std;
llint n, m;
const int FACT_MAX = 4000005;
llint fact[FACT_MAX], fact_inv[FACT_MAX];
llint modpow(llint a, llint n) {
if (n == 0)
return 1;
if (n % 2) {
return ((a % mod) * (modpow(a, n - 1) % mod)) % mod;
} else {
return modpow((a * a) % mod, n / 2) % mod;
}
}
void make_fact() {
llint val = 1;
fact[0] = 1;
for (int i = 1; i < FACT_MAX; i++) {
val *= i;
val %= mod;
fact[i] = val;
}
fact_inv[FACT_MAX - 1] = modpow(fact[FACT_MAX - 1], mod - 2);
for (int i = FACT_MAX - 2; i >= 0; i--) {
fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod;
}
}
llint comb(llint n, llint k) {
llint ret = 1;
ret *= fact[n];
ret *= fact_inv[k], ret %= mod;
ret *= fact_inv[n - k], ret %= mod;
return ret;
}
int main(void) {
cin >> n >> m;
make_fact();
llint ans = 0;
for (int i = 0; i <= min(m, n); i++) {
if (m % 2 != i % 2)
continue;
llint rem = (3 * m - i) / 2;
llint tmp = comb(rem + n - 1, n - 1);
if (rem - (m + 1) >= 0)
tmp += mod - (n - i) * comb(rem - (m + 1) + n - 1, n - 1) % mod,
tmp %= mod;
if (rem - m >= 0)
tmp += mod - i * comb(rem - m + n - 1, n - 1) % mod, tmp %= mod;
ans += tmp * comb(n, i) % mod, ans %= mod;
}
cout << ans << endl;
return 0;
} | replace | 47 | 48 | 47 | 48 | 0 | |
p02965 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"
using namespace std;
const long long MOD = 998244353;
int N, M;
long long dat[2000009];
long long quickpow(long long b, long long e) {
return e ? ((e & 1 ? b : 1) * quickpow((b * b) % MOD, e >> 1)) % MOD : 1;
}
long long combi(int n, int m) {
long long retval = dat[n];
retval = retval * quickpow(dat[n - m], MOD - 2);
retval = retval % MOD;
retval = retval * quickpow(dat[m], MOD - 2);
retval = retval % MOD;
return retval;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
dat[0] = 1;
for (int i = 1; i <= 2000009; i++) {
dat[i] = dat[i - 1] * i;
dat[i] = dat[i] % MOD;
}
int lb = min(N, M);
long long ans = 0, tmp;
for (int i = 0; i <= lb; i++) {
if (3 * M - i >= 0 && (3 * M - i) % 2 == 0) {
tmp = combi((3 * M - i) / 2 + N - 1, N - 1);
tmp = tmp * combi(N, i);
tmp = tmp % MOD;
ans += tmp;
ans = ans % MOD;
}
}
tmp = combi(M + N - 1, N - 1);
tmp = tmp + MOD - combi(M + N - 2, N - 2);
tmp = tmp * N;
tmp = tmp % MOD;
ans = ans + MOD - tmp;
ans = ans % MOD;
cout << ans << E;
// system("pause");
return 0;
} | #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"
using namespace std;
const long long MOD = 998244353;
int N, M;
long long dat[2000009];
long long quickpow(long long b, long long e) {
return e ? ((e & 1 ? b : 1) * quickpow((b * b) % MOD, e >> 1)) % MOD : 1;
}
long long combi(int n, int m) {
long long retval = dat[n];
retval = retval * quickpow(dat[n - m], MOD - 2);
retval = retval % MOD;
retval = retval * quickpow(dat[m], MOD - 2);
retval = retval % MOD;
return retval;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
dat[0] = 1;
for (int i = 1; i < 2000009; i++) {
dat[i] = dat[i - 1] * i;
dat[i] = dat[i] % MOD;
}
int lb = min(N, M);
long long ans = 0, tmp;
for (int i = 0; i <= lb; i++) {
if (3 * M - i >= 0 && (3 * M - i) % 2 == 0) {
tmp = combi((3 * M - i) / 2 + N - 1, N - 1);
tmp = tmp * combi(N, i);
tmp = tmp % MOD;
ans += tmp;
ans = ans % MOD;
}
}
tmp = combi(M + N - 1, N - 1);
tmp = tmp + MOD - combi(M + N - 2, N - 2);
tmp = tmp * N;
tmp = tmp % MOD;
ans = ans + MOD - tmp;
ans = ans % MOD;
cout << ans << E;
// system("pause");
return 0;
} | replace | 32 | 33 | 32 | 33 | 0 | |
p02965 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (lint)(n); i++)
#define REP(i, n) for (int i = 1; i <= (lint)(n); i++)
#define all(V) V.begin(), V.end()
typedef long long lint;
typedef std::pair<lint, lint> P;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;
constexpr double eps = 1e-9;
constexpr double PI = 3.141592653589793238462643383279;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
inline lint gcd(lint a, lint b) {
while (b) {
lint c = a;
a = b;
b = c % b;
}
return a;
}
inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
bool isprime(lint n) {
if (n == 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
template <typename T> T mypow(T a, unsigned int b) {
if (!b)
return T(1);
if (b & 1)
return mypow(a, b - 1) * a;
T memo = mypow(a, b >> 1);
return memo * memo;
}
lint modpow(lint a, lint b, lint m) {
if (!b)
return 1;
if (b & 1)
return modpow(a, b - 1, m) * a % m;
lint memo = modpow(a, b >> 1, m);
return memo * memo % m;
}
template <typename T> void printArray(std::vector<T> &vec) {
rep(i, vec.size() - 1) std::cout << vec[i] << " ";
std::cout << vec.back() << std::endl;
}
template <typename T> void printArray(T l, T r) {
T rprev = r;
rprev--;
for (T i = l; i != rprev; i++) {
std::cout << *i << " ";
}
std::cout << *rprev << std::endl;
}
template <unsigned int modulo = 998244353> class ModInt {
lint value;
public:
ModInt() : value(0) {}
template <typename T> ModInt(T value = 0) : value(value) {
if (value < 0)
value = -(lint)(-value % modulo) + modulo;
this->value = value % modulo;
}
inline operator int() const { return value; }
inline ModInt &operator+=(const ModInt &x) {
value += x.value;
if (value >= modulo)
value -= modulo;
return *this;
}
inline ModInt &operator++() {
if (value == modulo - 1)
value = 0;
else
value++;
return *this;
}
inline ModInt operator-() const { return ModInt(0) -= *this; }
inline ModInt &operator-=(const ModInt &x) {
value -= x.value;
if (value < 0)
value += modulo;
return *this;
}
inline ModInt &operator--() {
if (value == 0)
value = modulo - 1;
else
value--;
return *this;
}
inline ModInt &operator*=(const ModInt &x) {
value = value * x.value % modulo;
return *this;
}
inline ModInt &operator/=(ModInt rhs) {
int exp = modulo - 2;
while (exp) {
if (exp & 1)
*this *= rhs;
rhs *= rhs;
exp >>= 1;
}
return *this;
}
template <typename T> ModInt operator+(const T &rhs) const {
return ModInt(*this) += rhs;
}
template <typename T> ModInt &operator+=(const T &rhs) {
return operator+=(ModInt(rhs));
}
template <typename T> ModInt operator-(const T &rhs) const {
return ModInt(*this) -= rhs;
}
template <typename T> ModInt &operator-=(const T &rhs) {
return operator-=(ModInt(rhs));
}
template <typename T> ModInt operator*(const T &rhs) const {
return ModInt(*this) *= rhs;
}
template <typename T> ModInt &operator*=(const T &rhs) {
return operator*=(ModInt(rhs));
}
template <typename T> ModInt operator/(const T &rhs) const {
return ModInt(*this) /= rhs;
}
template <typename T> ModInt &operator/=(const T &rhs) {
return operator/=(ModInt(rhs));
}
};
template <unsigned int T>
std::istream &operator>>(std::istream &ist, ModInt<T> &x) {
lint a;
ist >> a;
x = a;
return ist;
}
int n, m;
ModInt<> fact[2000010], inv[2000010];
ModInt<> comb(int a, int b) { return fact[a] * inv[a - b] * inv[b]; }
ModInt<> comb2(int a, int b) { return comb(a + b - 1, b - 1); }
int main() {
std::cin >> n >> m;
fact[0] = 1;
REP(i, 2000000) fact[i] = fact[i - 1] * i;
inv[2000000] = ModInt<>(1) / fact[2000000];
for (int i = 1999999; i >= 0; i--)
inv[i] = inv[i + 1] * (i + 1);
ModInt<> ans = 0;
for (int i = m; i >= 0; i -= 2) {
ans += comb(n, i) * comb2((3 * m - i) / 2, n) -
(comb(n, i) * comb2((m - i) / 2, n) -
comb(n - 1, i) * comb2((m - i) / 2, n - 1)) *
n;
}
std::cout << ans << std::endl;
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (lint)(n); i++)
#define REP(i, n) for (int i = 1; i <= (lint)(n); i++)
#define all(V) V.begin(), V.end()
typedef long long lint;
typedef std::pair<lint, lint> P;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;
constexpr double eps = 1e-9;
constexpr double PI = 3.141592653589793238462643383279;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
inline lint gcd(lint a, lint b) {
while (b) {
lint c = a;
a = b;
b = c % b;
}
return a;
}
inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
bool isprime(lint n) {
if (n == 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
template <typename T> T mypow(T a, unsigned int b) {
if (!b)
return T(1);
if (b & 1)
return mypow(a, b - 1) * a;
T memo = mypow(a, b >> 1);
return memo * memo;
}
lint modpow(lint a, lint b, lint m) {
if (!b)
return 1;
if (b & 1)
return modpow(a, b - 1, m) * a % m;
lint memo = modpow(a, b >> 1, m);
return memo * memo % m;
}
template <typename T> void printArray(std::vector<T> &vec) {
rep(i, vec.size() - 1) std::cout << vec[i] << " ";
std::cout << vec.back() << std::endl;
}
template <typename T> void printArray(T l, T r) {
T rprev = r;
rprev--;
for (T i = l; i != rprev; i++) {
std::cout << *i << " ";
}
std::cout << *rprev << std::endl;
}
template <unsigned int modulo = 998244353> class ModInt {
lint value;
public:
ModInt() : value(0) {}
template <typename T> ModInt(T value = 0) : value(value) {
if (value < 0)
value = -(lint)(-value % modulo) + modulo;
this->value = value % modulo;
}
inline operator int() const { return value; }
inline ModInt &operator+=(const ModInt &x) {
value += x.value;
if (value >= modulo)
value -= modulo;
return *this;
}
inline ModInt &operator++() {
if (value == modulo - 1)
value = 0;
else
value++;
return *this;
}
inline ModInt operator-() const { return ModInt(0) -= *this; }
inline ModInt &operator-=(const ModInt &x) {
value -= x.value;
if (value < 0)
value += modulo;
return *this;
}
inline ModInt &operator--() {
if (value == 0)
value = modulo - 1;
else
value--;
return *this;
}
inline ModInt &operator*=(const ModInt &x) {
value = value * x.value % modulo;
return *this;
}
inline ModInt &operator/=(ModInt rhs) {
int exp = modulo - 2;
while (exp) {
if (exp & 1)
*this *= rhs;
rhs *= rhs;
exp >>= 1;
}
return *this;
}
template <typename T> ModInt operator+(const T &rhs) const {
return ModInt(*this) += rhs;
}
template <typename T> ModInt &operator+=(const T &rhs) {
return operator+=(ModInt(rhs));
}
template <typename T> ModInt operator-(const T &rhs) const {
return ModInt(*this) -= rhs;
}
template <typename T> ModInt &operator-=(const T &rhs) {
return operator-=(ModInt(rhs));
}
template <typename T> ModInt operator*(const T &rhs) const {
return ModInt(*this) *= rhs;
}
template <typename T> ModInt &operator*=(const T &rhs) {
return operator*=(ModInt(rhs));
}
template <typename T> ModInt operator/(const T &rhs) const {
return ModInt(*this) /= rhs;
}
template <typename T> ModInt &operator/=(const T &rhs) {
return operator/=(ModInt(rhs));
}
};
template <unsigned int T>
std::istream &operator>>(std::istream &ist, ModInt<T> &x) {
lint a;
ist >> a;
x = a;
return ist;
}
int n, m;
ModInt<> fact[2000010], inv[2000010];
ModInt<> comb(int a, int b) {
if (a < b)
return 0;
return fact[a] * inv[a - b] * inv[b];
}
ModInt<> comb2(int a, int b) { return comb(a + b - 1, b - 1); }
int main() {
std::cin >> n >> m;
fact[0] = 1;
REP(i, 2000000) fact[i] = fact[i - 1] * i;
inv[2000000] = ModInt<>(1) / fact[2000000];
for (int i = 1999999; i >= 0; i--)
inv[i] = inv[i + 1] * (i + 1);
ModInt<> ans = 0;
for (int i = m; i >= 0; i -= 2) {
ans += comb(n, i) * comb2((3 * m - i) / 2, n) -
(comb(n, i) * comb2((m - i) / 2, n) -
comb(n - 1, i) * comb2((m - i) / 2, n - 1)) *
n;
}
std::cout << ans << std::endl;
return 0;
} | replace | 188 | 189 | 188 | 193 | 0 | |
p02965 | C++ | Runtime Error | #define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__, __j__;
#define printLine(l) \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << "-"; \
} \
cout << endl
#define printLine2(l, c) \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << c; \
} \
cout << endl
#define printVar(n) cout << #n << ": " << n << endl
#define printArr(a, l) \
cout << #a << ": "; \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << a[__i__] << " "; \
} \
cout << endl
#define print2dArr(a, r, c) \
cout << #a << ":\n"; \
for (__i__ = 0; __i__ < r; __i__++) { \
for (__j__ = 0; __j__ < c; __j__++) { \
cout << a[__i__][__j__] << " "; \
} \
cout << endl; \
}
#define print2dArr2(a, r, c, l) \
cout << #a << ":\n"; \
for (__i__ = 0; __i__ < r; __i__++) { \
for (__j__ = 0; __j__ < c; __j__++) { \
cout << setw(l) << setfill(' ') << a[__i__][__j__] << " "; \
} \
cout << endl; \
}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template <typename T> _Debug &operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l, c)
#define printVar(n)
#define printArr(a, l)
#define print2dArr(a, r, c)
#define print2dArr2(a, r, c, l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int, int> pii;
typedef pair<LLI, LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#define MOD 998244353
int inv(LLI n) {
int e = MOD - 2;
LLI r = 1;
while (e > 0) {
if (e & 1)
r *= n, r %= MOD;
e >>= 1;
n *= n, n %= MOD;
}
return r;
}
int fact[3000000];
int invfact[3000000];
int choose(int n, int k) {
return ((((LLI)fact[n] * invfact[k]) % MOD) * invfact[n - k]) % MOD;
}
int main() {
int i;
int N, M;
cin >> N >> M;
fact[0] = 1;
for (i = 1; i < 3000000; i++)
fact[i] = ((LLI)fact[i - 1] * i) % MOD;
invfact[2999999] = inv(fact[2999999]);
for (i = 2999998; i >= 0; i--)
invfact[i] = ((LLI)invfact[i + 1] * (i + 1)) % MOD;
LLI ans = choose(3 * M + N - 1, N - 1);
for (i = 2 * M + 1; i <= 3 * M; i++)
ans -= ((LLI)N * choose(3 * M - i + N - 2, N - 2)) % MOD;
for (i = M + 1; i <= N; i++) {
if (!((3 * M - i) & 1))
ans -= ((LLI)choose(N, i) * choose((3 * M - i) / 2 + N - 1, N - 1)) % MOD;
}
ans %= MOD;
if (ans < 0)
ans += MOD;
cout << ans << endl;
return 0;
}
| #define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__, __j__;
#define printLine(l) \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << "-"; \
} \
cout << endl
#define printLine2(l, c) \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << c; \
} \
cout << endl
#define printVar(n) cout << #n << ": " << n << endl
#define printArr(a, l) \
cout << #a << ": "; \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << a[__i__] << " "; \
} \
cout << endl
#define print2dArr(a, r, c) \
cout << #a << ":\n"; \
for (__i__ = 0; __i__ < r; __i__++) { \
for (__j__ = 0; __j__ < c; __j__++) { \
cout << a[__i__][__j__] << " "; \
} \
cout << endl; \
}
#define print2dArr2(a, r, c, l) \
cout << #a << ":\n"; \
for (__i__ = 0; __i__ < r; __i__++) { \
for (__j__ = 0; __j__ < c; __j__++) { \
cout << setw(l) << setfill(' ') << a[__i__][__j__] << " "; \
} \
cout << endl; \
}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template <typename T> _Debug &operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l, c)
#define printVar(n)
#define printArr(a, l)
#define print2dArr(a, r, c)
#define print2dArr2(a, r, c, l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int, int> pii;
typedef pair<LLI, LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#define MOD 998244353
int inv(LLI n) {
int e = MOD - 2;
LLI r = 1;
while (e > 0) {
if (e & 1)
r *= n, r %= MOD;
e >>= 1;
n *= n, n %= MOD;
}
return r;
}
int fact[3000000];
int invfact[3000000];
int choose(int n, int k) {
return ((((LLI)fact[n] * invfact[k]) % MOD) * invfact[n - k]) % MOD;
}
int main() {
int i;
int N, M;
cin >> N >> M;
fact[0] = 1;
for (i = 1; i < 3000000; i++)
fact[i] = ((LLI)fact[i - 1] * i) % MOD;
invfact[2999999] = inv(fact[2999999]);
for (i = 2999998; i >= 0; i--)
invfact[i] = ((LLI)invfact[i + 1] * (i + 1)) % MOD;
LLI ans = choose(3 * M + N - 1, N - 1);
for (i = 2 * M + 1; i <= 3 * M; i++)
ans -= ((LLI)N * choose(3 * M - i + N - 2, N - 2)) % MOD;
for (i = M + 1; i <= min(N, 3 * M); i++) {
if (!((3 * M - i) & 1))
ans -= ((LLI)choose(N, i) * choose((3 * M - i) / 2 + N - 1, N - 1)) % MOD;
}
ans %= MOD;
if (ans < 0)
ans += MOD;
cout << ans << endl;
return 0;
}
| replace | 114 | 115 | 114 | 115 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<ll, pll> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const ll mod = 998244353;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll Pow(ll n, ll p) {
ll r = 1;
for (; p > 0; p >>= 1) {
if (p & 1)
r = (r * n) % mod;
n = (n * n) % mod;
}
return r;
}
const int M = 4000005;
ll F[M];
void Init() {
F[0] = 1;
for (int i = 1; i < M; i++)
F[i] = F[i - 1] * i % mod;
}
ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; }
ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); }
ll nHk(ll n, ll k) { return nCk(n + k - 1, k); }
int n, m;
int main() {
Init();
cin >> n >> m;
ll res = nHk(n, 3 * m);
for (int i = m + 1; i <= n; i++)
if ((3 * m - i) % 2 == 0)
res = (res - nHk(n, (3 * m - i) / 2) * nCk(n, i) % mod + mod) % mod;
for (int i = 2 * m + 1; i <= 3 * m; i++)
res = (res - n * nHk(n - 1, 3 * m - i) % mod + mod) % mod;
cout << res << endl;
} | #include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<ll, pll> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const ll mod = 998244353;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll Pow(ll n, ll p) {
ll r = 1;
for (; p > 0; p >>= 1) {
if (p & 1)
r = (r * n) % mod;
n = (n * n) % mod;
}
return r;
}
const int M = 4000005;
ll F[M];
void Init() {
F[0] = 1;
for (int i = 1; i < M; i++)
F[i] = F[i - 1] * i % mod;
}
ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; }
ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); }
ll nHk(ll n, ll k) { return nCk(n + k - 1, k); }
int n, m;
int main() {
Init();
cin >> n >> m;
ll res = nHk(n, 3 * m);
for (int i = m + 1; i <= min(3 * m, n); i++)
if ((3 * m - i) % 2 == 0)
res = (res - nHk(n, (3 * m - i) / 2) * nCk(n, i) % mod + mod) % mod;
for (int i = 2 * m + 1; i <= 3 * m; i++)
res = (res - n * nHk(n - 1, 3 * m - i) % mod + mod) % mod;
cout << res << endl;
} | replace | 58 | 59 | 58 | 59 | 0 | |
p02965 | C++ | Runtime Error | // #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) \
for (int i = 0; i < (n); i++) \
ni(a[i])
#define nal(a, n) \
for (int i = 0; i < (n); i++) \
nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
oset;
const double pi = acos(-1);
const int MOD = 998244353;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
ll fac[MAXN], ifac[MAXN];
ll pw(ll a, ll b) {
ll r = 1;
while (b) {
if (b & 1)
r = (r * a) % MOD, b--;
else
a = (a * a) % MOD, b /= 2;
}
return r;
}
ll ncr(int n, int k) { return (fac[n] * ifac[k] % MOD) * ifac[n - k] % MOD; }
int main() {
fac[0] = 1;
ifac[0] = 1;
for (ll i = 1; i < MAXN; i++) {
fac[i] = (fac[i - 1] * i) % MOD;
ifac[i] = pw(fac[i], MOD - 2);
}
int n, m;
scanf("%d %d", &n, &m);
if (n == 2)
return !pri(m + 1);
ll ans = ncr(3 * m + n - 1, n - 1);
for (int i = 2 * m + 1; i <= 3 * m; i++)
ans = (ans - (ll)n * ncr(3 * m - i + n - 2, n - 2) % MOD + MOD) % MOD;
for (int i = 0; i < m; i++)
if (n >= 3 * m - 2 * i)
ans = (ans - ncr(i + n - 1, n - 1) * ncr(n, 3 * m - i * 2) % MOD + MOD) %
MOD;
prl(ans);
return 0;
}
| // #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) \
for (int i = 0; i < (n); i++) \
ni(a[i])
#define nal(a, n) \
for (int i = 0; i < (n); i++) \
nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
oset;
const double pi = acos(-1);
const int MOD = 998244353;
const int INF = 1e9 + 7;
const int MAXN = 3e6 + 5;
const double eps = 1e-9;
ll fac[MAXN], ifac[MAXN];
ll pw(ll a, ll b) {
ll r = 1;
while (b) {
if (b & 1)
r = (r * a) % MOD, b--;
else
a = (a * a) % MOD, b /= 2;
}
return r;
}
ll ncr(int n, int k) { return (fac[n] * ifac[k] % MOD) * ifac[n - k] % MOD; }
int main() {
fac[0] = 1;
ifac[0] = 1;
for (ll i = 1; i < MAXN; i++) {
fac[i] = (fac[i - 1] * i) % MOD;
ifac[i] = pw(fac[i], MOD - 2);
}
int n, m;
scanf("%d %d", &n, &m);
if (n == 2)
return !pri(m + 1);
ll ans = ncr(3 * m + n - 1, n - 1);
for (int i = 2 * m + 1; i <= 3 * m; i++)
ans = (ans - (ll)n * ncr(3 * m - i + n - 2, n - 2) % MOD + MOD) % MOD;
for (int i = 0; i < m; i++)
if (n >= 3 * m - 2 * i)
ans = (ans - ncr(i + n - 1, n - 1) * ncr(n, 3 * m - i * 2) % MOD + MOD) %
MOD;
prl(ans);
return 0;
}
| replace | 40 | 41 | 40 | 41 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
const int INF = 1ll << 50;
const int MOD = 998244353;
#define dump(x) \
if (dbg) { \
cerr << #x << " = " << (x) << endl; \
}
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
} else
return 0;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
} else
return 0;
}
void Yes(bool flag = true) {
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void No(bool flag = true) { Yes(!flag); }
void YES(bool flag = true) {
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void NO(bool flag = true) { YES(!flag); }
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
// #define P pair<int, int>
// #define V vector<int>
// #define S set<int>
#define itn int
bool dbg = false;
template <uint MD> struct ModInt {
using M = ModInt;
const static M G;
uint v;
ModInt(ll _v = 0) { set_v(_v % MD + MD); }
M &set_v(uint _v) {
v = (_v < MD) ? _v : _v - MD;
return *this;
}
explicit operator bool() const { return v != 0; }
M operator-() const { return M() - *this; }
M operator+(const M &r) const { return M().set_v(v + r.v); }
M operator-(const M &r) const { return M().set_v(v + MD - r.v); }
M operator*(const M &r) const { return M().set_v(ull(v) * r.v % MD); }
M operator/(const M &r) const { return *this * r.inv(); }
M &operator+=(const M &r) { return *this = *this + r; }
M &operator-=(const M &r) { return *this = *this - r; }
M &operator*=(const M &r) { return *this = *this * r; }
M &operator/=(const M &r) { return *this = *this / r; }
bool operator==(const M &r) const { return v == r.v; }
M pow(ll n) const {
M x = *this, r = 1;
while (n) {
if (n & 1)
r *= x;
x *= x;
n >>= 1;
}
return r;
}
M inv() const { return pow(MD - 2); }
friend ostream &operator<<(ostream &os, const M &r) { return os << r.v; }
};
using Mint = ModInt<MOD>;
// Template by yosupo
const int MN = 1'501'000;
Mint fact[MN], iFac[MN];
void first() {
fact[0] = Mint(1);
for (int i = 1; i < MN; i++)
fact[i] = fact[i - 1] * Mint(i);
iFac[MN - 1] = fact[MN - 1].inv();
for (int i = MN - 1; i >= 1; i--) {
iFac[i - 1] = iFac[i] * Mint(i);
}
assert(fact[2345] * iFac[2345] == Mint(1));
}
Mint C(int n, int k) {
if (n < k || k < 0)
return Mint(0);
return fact[n] * iFac[k] * iFac[n - k];
}
int N, M;
void solve() {
first();
Mint ans = 0;
for (int m = M % 2; m <= min(M, N); m += 2) {
Mint tmp = C(N, m);
int R = (M - m) / 2 + M;
tmp *= C(N + R - 1, N - 1);
Mint tmp2 = C(N + R - M - 1, N - 1) * m * C(N, m);
Mint tmp3 = C(N + R - M - 2, N - 1) * (N - m) * C(N, m);
// cerr << tmp << " " << tmp2 << " " << tmp3 << endl;
tmp -= tmp2 + tmp3;
ans += tmp;
}
cout << ans << endl;
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N >> M;
solve();
return 0;
}
| #include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
const int INF = 1ll << 50;
const int MOD = 998244353;
#define dump(x) \
if (dbg) { \
cerr << #x << " = " << (x) << endl; \
}
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
} else
return 0;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
} else
return 0;
}
void Yes(bool flag = true) {
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void No(bool flag = true) { Yes(!flag); }
void YES(bool flag = true) {
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void NO(bool flag = true) { YES(!flag); }
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
// #define P pair<int, int>
// #define V vector<int>
// #define S set<int>
#define itn int
bool dbg = false;
template <uint MD> struct ModInt {
using M = ModInt;
const static M G;
uint v;
ModInt(ll _v = 0) { set_v(_v % MD + MD); }
M &set_v(uint _v) {
v = (_v < MD) ? _v : _v - MD;
return *this;
}
explicit operator bool() const { return v != 0; }
M operator-() const { return M() - *this; }
M operator+(const M &r) const { return M().set_v(v + r.v); }
M operator-(const M &r) const { return M().set_v(v + MD - r.v); }
M operator*(const M &r) const { return M().set_v(ull(v) * r.v % MD); }
M operator/(const M &r) const { return *this * r.inv(); }
M &operator+=(const M &r) { return *this = *this + r; }
M &operator-=(const M &r) { return *this = *this - r; }
M &operator*=(const M &r) { return *this = *this * r; }
M &operator/=(const M &r) { return *this = *this / r; }
bool operator==(const M &r) const { return v == r.v; }
M pow(ll n) const {
M x = *this, r = 1;
while (n) {
if (n & 1)
r *= x;
x *= x;
n >>= 1;
}
return r;
}
M inv() const { return pow(MD - 2); }
friend ostream &operator<<(ostream &os, const M &r) { return os << r.v; }
};
using Mint = ModInt<MOD>;
// Template by yosupo
const int MN = 2'501'000;
Mint fact[MN], iFac[MN];
void first() {
fact[0] = Mint(1);
for (int i = 1; i < MN; i++)
fact[i] = fact[i - 1] * Mint(i);
iFac[MN - 1] = fact[MN - 1].inv();
for (int i = MN - 1; i >= 1; i--) {
iFac[i - 1] = iFac[i] * Mint(i);
}
assert(fact[2345] * iFac[2345] == Mint(1));
}
Mint C(int n, int k) {
if (n < k || k < 0)
return Mint(0);
return fact[n] * iFac[k] * iFac[n - k];
}
int N, M;
void solve() {
first();
Mint ans = 0;
for (int m = M % 2; m <= min(M, N); m += 2) {
Mint tmp = C(N, m);
int R = (M - m) / 2 + M;
tmp *= C(N + R - 1, N - 1);
Mint tmp2 = C(N + R - M - 1, N - 1) * m * C(N, m);
Mint tmp3 = C(N + R - M - 2, N - 1) * (N - m) * C(N, m);
// cerr << tmp << " " << tmp2 << " " << tmp3 << endl;
tmp -= tmp2 + tmp3;
ans += tmp;
}
cout << ans << endl;
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N >> M;
solve();
return 0;
}
| replace | 95 | 96 | 95 | 96 | 0 | |
p02965 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define int long long
#define endl '\n'
#define INF 1000000000000000009
#define all(a) a.begin(), a.end()
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
template <class T, class S> bool chmax(T &a, const S &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T, class S> bool chmin(T &a, const S &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }
ll mod = 998244353;
ll modinv(ll a) {
ll b = mod, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= mod;
if (u < 0)
u += mod;
return u;
}
ll modpow(ll a, ll b) {
ll ans = 1;
a %= mod;
while (b) {
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
vector<ll> fac(10000010);
vector<ll> inv(1000010);
vector<ll> facinv(10000010);
void modcalc(int n) {
fac[0] = fac[1] = 1;
inv[1] = 1;
facinv[0] = facinv[1] = 1;
for (int i = 2; i <= n; i++) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
facinv[i] = facinv[i - 1] * inv[i] % mod;
}
}
ll modcomb(ll n, ll k) {
if (n < 0 || k < 0 || n < k)
return 0;
return fac[n] * facinv[k] % mod * facinv[n - k] % mod;
}
ll modperm(ll n, ll k) {
if (n < 0 || k < 0 || n < k)
return 0;
return fac[n] * facinv[n - k] % mod;
}
ll modhom(ll n, ll k) {
if (n < 0 || k < 0)
return 0;
if (n == 0 && k == 0)
return 1;
return modcomb(n + k - 1, k);
}
template <class T> class segtree {
int n;
vector<T> data;
T def;
function<T(T, T)> operation;
function<T(T, T)> update;
T _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return def;
if (a <= l && r <= b)
return data[k];
T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
return operation(c1, c2);
}
public:
segtree(size_t _n, T _def, function<T(T, T)> _operation,
function<T(T, T)> _update)
: def(_def), operation(_operation), update(_update) {
n = 1;
while (n < _n) {
n *= 2;
}
data = vector<T>(2 * n - 1, def);
}
void change(int i, T x) {
i += n - 1;
data[i] = update(data[i], x);
while (i > 0) {
i = (i - 1) / 2;
data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
}
}
T query(int a, int b) { return _query(a, b, 0, 0, n); }
T operator[](int i) { return data[i + n - 1]; }
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
modcalc(3000010);
ll n, m;
cin >> n >> m;
ll ans = modhom(n, 3 * m);
for (ll i = 0; i < m; i++) {
ans = (ans - n * modhom(n - 1, i) % mod + mod) % mod;
}
for (ll i = m + 1; i <= n; i++) {
if ((3 * m - i) % 2 == 0) {
ans =
(ans - modcomb(n, i) * modhom(n, (3 * m - i) / 2) % mod + mod) % mod;
}
}
cout << ans << endl;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define int long long
#define endl '\n'
#define INF 1000000000000000009
#define all(a) a.begin(), a.end()
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
template <class T, class S> bool chmax(T &a, const S &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T, class S> bool chmin(T &a, const S &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }
ll mod = 998244353;
ll modinv(ll a) {
ll b = mod, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= mod;
if (u < 0)
u += mod;
return u;
}
ll modpow(ll a, ll b) {
ll ans = 1;
a %= mod;
while (b) {
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
vector<ll> fac(10000010);
vector<ll> inv(10000010);
vector<ll> facinv(10000010);
void modcalc(int n) {
fac[0] = fac[1] = 1;
inv[1] = 1;
facinv[0] = facinv[1] = 1;
for (int i = 2; i <= n; i++) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
facinv[i] = facinv[i - 1] * inv[i] % mod;
}
}
ll modcomb(ll n, ll k) {
if (n < 0 || k < 0 || n < k)
return 0;
return fac[n] * facinv[k] % mod * facinv[n - k] % mod;
}
ll modperm(ll n, ll k) {
if (n < 0 || k < 0 || n < k)
return 0;
return fac[n] * facinv[n - k] % mod;
}
ll modhom(ll n, ll k) {
if (n < 0 || k < 0)
return 0;
if (n == 0 && k == 0)
return 1;
return modcomb(n + k - 1, k);
}
template <class T> class segtree {
int n;
vector<T> data;
T def;
function<T(T, T)> operation;
function<T(T, T)> update;
T _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return def;
if (a <= l && r <= b)
return data[k];
T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
return operation(c1, c2);
}
public:
segtree(size_t _n, T _def, function<T(T, T)> _operation,
function<T(T, T)> _update)
: def(_def), operation(_operation), update(_update) {
n = 1;
while (n < _n) {
n *= 2;
}
data = vector<T>(2 * n - 1, def);
}
void change(int i, T x) {
i += n - 1;
data[i] = update(data[i], x);
while (i > 0) {
i = (i - 1) / 2;
data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
}
}
T query(int a, int b) { return _query(a, b, 0, 0, n); }
T operator[](int i) { return data[i + n - 1]; }
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
modcalc(3000010);
ll n, m;
cin >> n >> m;
ll ans = modhom(n, 3 * m);
for (ll i = 0; i < m; i++) {
ans = (ans - n * modhom(n - 1, i) % mod + mod) % mod;
}
for (ll i = m + 1; i <= n; i++) {
if ((3 * m - i) % 2 == 0) {
ans =
(ans - modcomb(n, i) * modhom(n, (3 * m - i) / 2) % mod + mod) % mod;
}
}
cout << ans << endl;
}
| replace | 71 | 72 | 71 | 72 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02965 | C++ | Runtime Error | /*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
template <typename T1, typename T2> inline void chkmin(T1 &x, T2 y) {
if (x > y)
x = y;
}
template <typename T1, typename T2> inline void chkmax(T1 &x, T2 y) {
if (x < y)
x = y;
}
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left224
#define right right224
#define next next224
#define rank rank224
#define prev prev224
#define y1 y1224
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
const string FILENAME = "input";
const int MAXN = 3000228;
const int Mod = 998244353;
int mul(int a, int b) { return (1LL * a * b) % Mod; }
int sum(int a, int b) { return (a + b) % Mod; }
int powm(int a, int b) {
int res = 1;
while (b) {
if (b & 1) {
res = mul(res, a);
}
b >>= 1;
a = mul(a, a);
}
return res;
}
int n, m;
int fact[MAXN];
int rfact[MAXN];
int getc(int i, int j) { return mul(fact[i], mul(rfact[j], rfact[i - j])); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// read(FILENAME);
cin >> n >> m;
fact[0] = 1;
for (int i = 1; i <= 3000000; i++) {
fact[i] = mul(fact[i - 1], i);
}
rfact[3000000] = powm(fact[3000000], Mod - 2);
for (int i = 2999999; i >= 0; i--) {
rfact[i] = mul(rfact[i + 1], i + 1);
}
// n + 3 * m, n - 1
int ans = getc(n + 3 * m - 1, n - 1);
// cout << ans << endl;
for (int value = 2 * m + 1; value <= 3 * m; value++) {
int f = getc(n + 3 * m - value - 2, n - 2);
f = mul(f, n);
ans = sum(ans, Mod - f);
}
for (int cntodd = m + 1; cntodd <= n; cntodd++) {
int f = getc(n, cntodd);
int rest = 3 * m - cntodd;
if (rest % 2 != 0) {
continue;
}
int have = rest / 2;
f = mul(f, getc(have + n - 1, n - 1));
ans = sum(ans, Mod - f);
}
cout << ans << endl;
return 0;
}
| /*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
template <typename T1, typename T2> inline void chkmin(T1 &x, T2 y) {
if (x > y)
x = y;
}
template <typename T1, typename T2> inline void chkmax(T1 &x, T2 y) {
if (x < y)
x = y;
}
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left224
#define right right224
#define next next224
#define rank rank224
#define prev prev224
#define y1 y1224
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
const string FILENAME = "input";
const int MAXN = 3000228;
const int Mod = 998244353;
int mul(int a, int b) { return (1LL * a * b) % Mod; }
int sum(int a, int b) { return (a + b) % Mod; }
int powm(int a, int b) {
int res = 1;
while (b) {
if (b & 1) {
res = mul(res, a);
}
b >>= 1;
a = mul(a, a);
}
return res;
}
int n, m;
int fact[MAXN];
int rfact[MAXN];
int getc(int i, int j) { return mul(fact[i], mul(rfact[j], rfact[i - j])); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// read(FILENAME);
cin >> n >> m;
fact[0] = 1;
for (int i = 1; i <= 3000000; i++) {
fact[i] = mul(fact[i - 1], i);
}
rfact[3000000] = powm(fact[3000000], Mod - 2);
for (int i = 2999999; i >= 0; i--) {
rfact[i] = mul(rfact[i + 1], i + 1);
}
// n + 3 * m, n - 1
int ans = getc(n + 3 * m - 1, n - 1);
// cout << ans << endl;
for (int value = 2 * m + 1; value <= 3 * m; value++) {
int f = getc(n + 3 * m - value - 2, n - 2);
f = mul(f, n);
ans = sum(ans, Mod - f);
}
for (int cntodd = m + 1; cntodd <= n; cntodd++) {
int f = getc(n, cntodd);
int rest = 3 * m - cntodd;
if (rest % 2 != 0) {
continue;
}
if (cntodd > 3 * m) {
continue;
}
int have = rest / 2;
f = mul(f, getc(have + n - 1, n - 1));
ans = sum(ans, Mod - f);
}
cout << ans << endl;
return 0;
}
| insert | 116 | 116 | 116 | 119 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x) \
for (auto &youso_ : (x)) \
cin >> youso_
#define bitcnt(x) __builtin_popcount(x)
#define lbit(x) __builtin_ffsll(x)
#define rbit(x) (64 - __builtin_clzll(x))
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
#define cinfast() cin.tie(0), ios::sync_with_stdio(false)
#define PERM(c) \
sort(All(c)); \
for (bool cp = true; cp; cp = next_permutation(All(c)))
#define COMB(n, k) \
for (ll bit = (1LL << k) - 1; bit < (1LL << n); bit = next_combination(bit))
#define PERM2(n, k) \
COMB(n, k) { \
vector<int> sel; \
for (int bitindex = 0; bitindex < n; bitindex++) \
if (bit >> bitindex & 1) \
sel.emplace_back(bitindex); \
PERM(sel) { Printv(sel); } \
}
#define MKORDER(n) \
vector<int> od(n); \
iota(All(od), 0LL);
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
cin >> head;
CIN(move(tail)...);
}
template <class Head> inline void COUT(Head &&head) { cout << (head) << "\n"; }
template <class Head, class... Tail>
inline void COUT(Head &&head, Tail &&...tail) {
cout << (head) << " ";
COUT(forward<Tail>(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
long long __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Printv(v) \
{ \
FOREACH(x, v) { cout << x << " "; } \
cout << "\n"; \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
long long next_combination(long long sub) {
long long x = sub & -sub, y = sub + x;
return (((sub & ~y) / x) >> 1) | y;
}
// generic lambdas
template <typename F>
#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)
[[nodiscard]]
#elif defined(__GNUC__) && \
(__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)
__attribute__((warn_unused_result))
#endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)
static inline constexpr decltype(auto)
fix(F &&f) noexcept {
return [f = std::forward<F>(f)](auto &&...args) {
return f(f, std::forward<decltype(args)>(args)...);
};
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;
const int INF = 1e9;
// const int MOD = 1e9 + 7;
const int MOD = 998244353;
const ll LINF = 1e18;
const ll dw[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dh[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238
#define EPS 1e-7
// 1000000007 で割ったあまりを扱う構造体
template <int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0)
v += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
constexpr Fp &operator+=(const Fp &r) noexcept {
val += r.val;
if (val >= MOD)
val -= MOD;
return *this;
}
constexpr Fp &operator-=(const Fp &r) noexcept {
val -= r.val;
if (val < 0)
val += MOD;
return *this;
}
constexpr Fp &operator*=(const Fp &r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp &operator/=(const Fp &r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
constexpr bool operator==(const Fp &r) const noexcept {
return this->val == r.val;
}
constexpr bool operator!=(const Fp &r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
return os << x.val;
}
friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept {
return is >> x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0)
return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1)
t = t * a;
return t;
}
};
using mint = Fp<MOD>;
const ll NMAX = 1e6;
ll fac[NMAX + 1], inv[NMAX + 1], finv[NMAX + 1];
void cominit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
FOR(i, 2, NMAX + 1) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll comb(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll hcomb(ll n, ll k) { return comb(k + n - 1, k); }
signed main() {
LCIN(N, M);
cominit();
mint ans = hcomb(N, 3 * M);
ans -= (mint)N * hcomb(N, M - 1);
REP(i, M) { ans -= (mint)hcomb(N, i) * comb(N, 3 * M - 2 * i); }
cout << ans << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x) \
for (auto &youso_ : (x)) \
cin >> youso_
#define bitcnt(x) __builtin_popcount(x)
#define lbit(x) __builtin_ffsll(x)
#define rbit(x) (64 - __builtin_clzll(x))
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
#define cinfast() cin.tie(0), ios::sync_with_stdio(false)
#define PERM(c) \
sort(All(c)); \
for (bool cp = true; cp; cp = next_permutation(All(c)))
#define COMB(n, k) \
for (ll bit = (1LL << k) - 1; bit < (1LL << n); bit = next_combination(bit))
#define PERM2(n, k) \
COMB(n, k) { \
vector<int> sel; \
for (int bitindex = 0; bitindex < n; bitindex++) \
if (bit >> bitindex & 1) \
sel.emplace_back(bitindex); \
PERM(sel) { Printv(sel); } \
}
#define MKORDER(n) \
vector<int> od(n); \
iota(All(od), 0LL);
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
cin >> head;
CIN(move(tail)...);
}
template <class Head> inline void COUT(Head &&head) { cout << (head) << "\n"; }
template <class Head, class... Tail>
inline void COUT(Head &&head, Tail &&...tail) {
cout << (head) << " ";
COUT(forward<Tail>(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
long long __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Printv(v) \
{ \
FOREACH(x, v) { cout << x << " "; } \
cout << "\n"; \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
long long next_combination(long long sub) {
long long x = sub & -sub, y = sub + x;
return (((sub & ~y) / x) >> 1) | y;
}
// generic lambdas
template <typename F>
#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)
[[nodiscard]]
#elif defined(__GNUC__) && \
(__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)
__attribute__((warn_unused_result))
#endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)
static inline constexpr decltype(auto)
fix(F &&f) noexcept {
return [f = std::forward<F>(f)](auto &&...args) {
return f(f, std::forward<decltype(args)>(args)...);
};
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;
const int INF = 1e9;
// const int MOD = 1e9 + 7;
const int MOD = 998244353;
const ll LINF = 1e18;
const ll dw[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dh[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238
#define EPS 1e-7
// 1000000007 で割ったあまりを扱う構造体
template <int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0)
v += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
constexpr Fp &operator+=(const Fp &r) noexcept {
val += r.val;
if (val >= MOD)
val -= MOD;
return *this;
}
constexpr Fp &operator-=(const Fp &r) noexcept {
val -= r.val;
if (val < 0)
val += MOD;
return *this;
}
constexpr Fp &operator*=(const Fp &r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp &operator/=(const Fp &r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
constexpr bool operator==(const Fp &r) const noexcept {
return this->val == r.val;
}
constexpr bool operator!=(const Fp &r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
return os << x.val;
}
friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept {
return is >> x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0)
return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1)
t = t * a;
return t;
}
};
using mint = Fp<MOD>;
const ll NMAX = 5e6;
ll fac[NMAX + 1], inv[NMAX + 1], finv[NMAX + 1];
void cominit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
FOR(i, 2, NMAX + 1) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll comb(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll hcomb(ll n, ll k) { return comb(k + n - 1, k); }
signed main() {
LCIN(N, M);
cominit();
mint ans = hcomb(N, 3 * M);
ans -= (mint)N * hcomb(N, M - 1);
REP(i, M) { ans -= (mint)hcomb(N, i) * comb(N, 3 * M - 2 * i); }
cout << ans << "\n";
}
| replace | 192 | 193 | 192 | 193 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define modulo 998244353
#define mod(mod_x) ((((long long)mod_x) + modulo) % modulo)
#define Inf 1000000000000
// aのb乗
int beki(int a, int b, int M = modulo) {
int x = 1;
while (b != 0) {
if (b & 1) {
x = ((long long)x * a) % M;
}
a = ((long long)a * a) % M;
b >>= 1;
}
return x;
}
// aの逆元
int gyakugen(int a) { return beki(a, modulo - 2); }
struct combi {
deque<int> kaijou;
deque<int> kaijou_;
combi(int n) {
kaijou.push_back(1);
for (int i = 1; i <= n; i++) {
kaijou.push_back(mod(kaijou[i - 1] * i));
}
int b = gyakugen(kaijou[n]);
kaijou_.push_front(b);
for (int i = 1; i <= n; i++) {
int k = n + 1 - i;
kaijou_.push_front(mod(kaijou_[0] * k));
}
}
int combination(int n, int r) {
if (r > n)
return 0;
int a = mod(kaijou[n] * kaijou_[r]);
a = mod(a * kaijou_[n - r]);
return a;
}
int junretsu(int a, int b) {
int x = mod(kaijou_[a] * kaijou_[b]);
x = mod(x * kaijou[a + b]);
return x;
}
int catalan(int n) { return mod(combination(2 * n, n) * gyakugen(n + 1)); }
};
int main() {
int N, M;
cin >> N >> M;
combi C(3000000);
int ans = C.junretsu(3 * M, N - 1);
{
int temp = 0;
for (int i = 2 * M + 1; i <= 3 * M; i++) {
temp = mod(temp + C.junretsu(3 * M - i, N - 2));
}
temp = mod(temp * N);
ans = mod(ans - temp);
}
{
int temp = 0;
for (int i = M + 1; i <= N; i++) {
if ((3 * M - i) % 2 == 1)
continue;
int t = C.combination(N, i);
t = mod(t * C.junretsu(N - 1, (3 * M - i) / 2));
temp = mod(temp + t);
}
ans = mod(ans - temp);
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define modulo 998244353
#define mod(mod_x) ((((long long)mod_x) + modulo) % modulo)
#define Inf 1000000000000
// aのb乗
int beki(int a, int b, int M = modulo) {
int x = 1;
while (b != 0) {
if (b & 1) {
x = ((long long)x * a) % M;
}
a = ((long long)a * a) % M;
b >>= 1;
}
return x;
}
// aの逆元
int gyakugen(int a) { return beki(a, modulo - 2); }
struct combi {
deque<int> kaijou;
deque<int> kaijou_;
combi(int n) {
kaijou.push_back(1);
for (int i = 1; i <= n; i++) {
kaijou.push_back(mod(kaijou[i - 1] * i));
}
int b = gyakugen(kaijou[n]);
kaijou_.push_front(b);
for (int i = 1; i <= n; i++) {
int k = n + 1 - i;
kaijou_.push_front(mod(kaijou_[0] * k));
}
}
int combination(int n, int r) {
if (r > n)
return 0;
int a = mod(kaijou[n] * kaijou_[r]);
a = mod(a * kaijou_[n - r]);
return a;
}
int junretsu(int a, int b) {
int x = mod(kaijou_[a] * kaijou_[b]);
x = mod(x * kaijou[a + b]);
return x;
}
int catalan(int n) { return mod(combination(2 * n, n) * gyakugen(n + 1)); }
};
int main() {
int N, M;
cin >> N >> M;
combi C(3000000);
int ans = C.junretsu(3 * M, N - 1);
{
int temp = 0;
for (int i = 2 * M + 1; i <= 3 * M; i++) {
temp = mod(temp + C.junretsu(3 * M - i, N - 2));
}
temp = mod(temp * N);
ans = mod(ans - temp);
}
{
int temp = 0;
for (int i = M + 1; i <= min(3 * M, N); i++) {
if ((3 * M - i) % 2 == 1)
continue;
int t = C.combination(N, i);
t = mod(t * C.junretsu(N - 1, (3 * M - i) / 2));
temp = mod(temp + t);
}
ans = mod(ans - temp);
}
cout << ans << endl;
return 0;
}
| replace | 81 | 82 | 81 | 82 | 0 | |
p02965 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
using namespace chrono;
#ifdef DEBUG
//~ #define LOCAL_INPUT_FILE
#else
// #define USE_FILE_IO
#endif
#ifdef USE_FILE_IO
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define cin ____cin
#define cout ____cout
ifstream cin(INPUT_FILE);
ofstream cout(OUTPUT_FILE);
#else
#ifdef LOCAL_INPUT_FILE
#define cin ____cin
ifstream cin("input.txt");
#endif
#endif
const int infinity = (int)1e9 + 42;
const int64_t llInfinity = (int64_t)1e18 + 256;
const int mod = 998244353;
const long double eps = 1e-8;
mt19937_64 randGen(system_clock().now().time_since_epoch().count());
inline void raiseError(string errorCode) {
cerr << "Error : " << errorCode << endl;
exit(42);
}
const int fMax = 1500 * 1000;
int64_t f[fMax], rf[fMax];
int64_t extGcd(int64_t a, int64_t b, int64_t &x, int64_t &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
int64_t x1, y1;
int64_t g = extGcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return g;
}
inline int64_t inverse(int64_t a, int64_t m = mod) {
int64_t x, y;
int64_t g = extGcd(a, m, x, y);
assert(g == 1);
return (x % m + m) % m;
}
inline int64_t C(int64_t n, int64_t k) {
if (k < 0 || k > n) {
return 0;
}
return f[n] * rf[k] % mod * rf[n - k] % mod;
}
inline int64_t cntLim(int64_t n, int64_t sum, int64_t limit) {
int64_t res = 0;
for (int i = 0; i <= n; ++i) {
res +=
int64_t((i & 1) ? mod - 1 : 1) * C(n, i) % mod * C(n + sum - 1, n - 1);
res %= mod;
sum -= limit + 1;
if (sum < 0) {
return res;
}
}
return res;
}
signed main() {
#ifndef USE_FILE_IO
ios_base::sync_with_stdio(false);
#endif
f[0] = 1;
for (int i = 1; i < fMax; ++i) {
f[i] = f[i - 1] * i % mod;
}
for (int i = 0; i < fMax; ++i) {
rf[i] = inverse(f[i]);
}
int n, m;
cin >> n >> m;
int64_t res = 0;
// step I: don't have 2*m
res += [&]() -> int64_t {
int64_t res = 0;
for (int i = 0; i <= n && i <= m; ++i) {
if ((3 * m - i) & 1) {
continue;
}
res += C(n, i) * cntLim(n, (3 * m - i) / 2, m - 1) % mod;
res %= mod;
}
return res;
}();
// step II: have 2*m
res += [&]() -> int64_t {
int64_t res = 0;
for (int i = 0; i < n && i <= m; ++i) {
if ((m - i) & 1) {
continue;
}
res += C(n - 1, i) * cntLim(n - 1, (m - i) / 2, m - 1) % mod;
res %= mod;
}
res *= n;
res %= mod;
return res;
}();
res %= mod;
cout << res << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
using namespace chrono;
#ifdef DEBUG
//~ #define LOCAL_INPUT_FILE
#else
// #define USE_FILE_IO
#endif
#ifdef USE_FILE_IO
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define cin ____cin
#define cout ____cout
ifstream cin(INPUT_FILE);
ofstream cout(OUTPUT_FILE);
#else
#ifdef LOCAL_INPUT_FILE
#define cin ____cin
ifstream cin("input.txt");
#endif
#endif
const int infinity = (int)1e9 + 42;
const int64_t llInfinity = (int64_t)1e18 + 256;
const int mod = 998244353;
const long double eps = 1e-8;
mt19937_64 randGen(system_clock().now().time_since_epoch().count());
inline void raiseError(string errorCode) {
cerr << "Error : " << errorCode << endl;
exit(42);
}
const int fMax = 1800 * 1000;
int64_t f[fMax], rf[fMax];
int64_t extGcd(int64_t a, int64_t b, int64_t &x, int64_t &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
int64_t x1, y1;
int64_t g = extGcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return g;
}
inline int64_t inverse(int64_t a, int64_t m = mod) {
int64_t x, y;
int64_t g = extGcd(a, m, x, y);
assert(g == 1);
return (x % m + m) % m;
}
inline int64_t C(int64_t n, int64_t k) {
if (k < 0 || k > n) {
return 0;
}
return f[n] * rf[k] % mod * rf[n - k] % mod;
}
inline int64_t cntLim(int64_t n, int64_t sum, int64_t limit) {
int64_t res = 0;
for (int i = 0; i <= n; ++i) {
res +=
int64_t((i & 1) ? mod - 1 : 1) * C(n, i) % mod * C(n + sum - 1, n - 1);
res %= mod;
sum -= limit + 1;
if (sum < 0) {
return res;
}
}
return res;
}
signed main() {
#ifndef USE_FILE_IO
ios_base::sync_with_stdio(false);
#endif
f[0] = 1;
for (int i = 1; i < fMax; ++i) {
f[i] = f[i - 1] * i % mod;
}
for (int i = 0; i < fMax; ++i) {
rf[i] = inverse(f[i]);
}
int n, m;
cin >> n >> m;
int64_t res = 0;
// step I: don't have 2*m
res += [&]() -> int64_t {
int64_t res = 0;
for (int i = 0; i <= n && i <= m; ++i) {
if ((3 * m - i) & 1) {
continue;
}
res += C(n, i) * cntLim(n, (3 * m - i) / 2, m - 1) % mod;
res %= mod;
}
return res;
}();
// step II: have 2*m
res += [&]() -> int64_t {
int64_t res = 0;
for (int i = 0; i < n && i <= m; ++i) {
if ((m - i) & 1) {
continue;
}
res += C(n - 1, i) * cntLim(n - 1, (m - i) / 2, m - 1) % mod;
res %= mod;
}
res *= n;
res %= mod;
return res;
}();
res %= mod;
cout << res << endl;
return 0;
}
| replace | 60 | 61 | 60 | 61 | 0 | |
p02965 | C++ | Runtime Error | #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define SZ(x) ((int)((x).size()))
template <typename T1, typename T2>
string print_iterable(T1 begin_iter, T2 end_iter, int counter) {
bool done_something = false;
stringstream res;
res << "[";
for (; begin_iter != end_iter and counter; ++begin_iter) {
done_something = true;
counter--;
res << *begin_iter << ", ";
}
string str = res.str();
if (done_something) {
str.pop_back();
str.pop_back();
}
str += "]";
return str;
}
vector<int> SortIndex(int size, std::function<bool(int, int)> compare) {
vector<int> ord(size);
for (int i = 0; i < size; i++)
ord[i] = i;
sort(ord.begin(), ord.end(), compare);
return ord;
}
template <typename T> bool MinPlace(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> bool MaxPlace(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename S, typename T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
out << "[";
for (int i = 0; i < (int)v.size(); i++) {
out << v[i];
if (i != (int)v.size() - 1)
out << ", ";
}
out << "]";
return out;
}
template <class TH> void _dbg(const char *name, TH val) {
clog << name << ": " << val << endl;
}
template <class TH, class... TA>
void _dbg(const char *names, TH curr_val, TA... vals) {
while (*names != ',')
clog << *names++;
clog << ": " << curr_val << ", ";
_dbg(names + 1, vals...);
}
#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define dbg_arr(x, len) \
clog << #x << ": " << print_iterable(x, x + len, -1) << endl;
#else
#define dbg(...)
#define dbg_arr(x, len)
#endif
///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////
LL Inverse(LL n, LL m) {
n %= m;
if (n <= 1)
return n; // Handles properly (n = 0, m = 1).
return m - ((m * Inverse(m, n) - 1) / n);
}
const LL mod = 998244353;
const int MAXN = 4e6;
LL fact[MAXN];
LL binom(LL a, LL b) {
assert(MAXN > a and a >= b and b >= 0);
return fact[a] * Inverse(fact[b] * fact[a - b] % mod, mod) % mod;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); // Remove in problems with online queries!
fact[0] = 1;
for (int i = 1; i < MAXN; i++) {
fact[i] = i * fact[i - 1] % mod;
// invfact[i] = Inverse(fact[i], mod);
}
int N, M;
cin >> N >> M;
LL ans = N * binom(M + N - 2, N - 2) % mod;
for (int k = 0; k <= M; k++) {
LL q = 3 * M - k;
if (q % 2)
continue;
q /= 2;
LL ris = binom(q + N - 1, N - 1) - N * binom(q - M + N - 1, N - 1);
ris %= mod;
ris = ris * binom(N, k) % mod;
ans += ris;
ans %= mod;
}
if (ans < 0)
ans += mod;
cout << ans << "\n";
}
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define SZ(x) ((int)((x).size()))
template <typename T1, typename T2>
string print_iterable(T1 begin_iter, T2 end_iter, int counter) {
bool done_something = false;
stringstream res;
res << "[";
for (; begin_iter != end_iter and counter; ++begin_iter) {
done_something = true;
counter--;
res << *begin_iter << ", ";
}
string str = res.str();
if (done_something) {
str.pop_back();
str.pop_back();
}
str += "]";
return str;
}
vector<int> SortIndex(int size, std::function<bool(int, int)> compare) {
vector<int> ord(size);
for (int i = 0; i < size; i++)
ord[i] = i;
sort(ord.begin(), ord.end(), compare);
return ord;
}
template <typename T> bool MinPlace(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> bool MaxPlace(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename S, typename T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
out << "[";
for (int i = 0; i < (int)v.size(); i++) {
out << v[i];
if (i != (int)v.size() - 1)
out << ", ";
}
out << "]";
return out;
}
template <class TH> void _dbg(const char *name, TH val) {
clog << name << ": " << val << endl;
}
template <class TH, class... TA>
void _dbg(const char *names, TH curr_val, TA... vals) {
while (*names != ',')
clog << *names++;
clog << ": " << curr_val << ", ";
_dbg(names + 1, vals...);
}
#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define dbg_arr(x, len) \
clog << #x << ": " << print_iterable(x, x + len, -1) << endl;
#else
#define dbg(...)
#define dbg_arr(x, len)
#endif
///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////
LL Inverse(LL n, LL m) {
n %= m;
if (n <= 1)
return n; // Handles properly (n = 0, m = 1).
return m - ((m * Inverse(m, n) - 1) / n);
}
const LL mod = 998244353;
const int MAXN = 4e6;
LL fact[MAXN];
LL binom(LL a, LL b) {
assert(MAXN > a and a >= b and b >= 0);
return fact[a] * Inverse(fact[b] * fact[a - b] % mod, mod) % mod;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); // Remove in problems with online queries!
fact[0] = 1;
for (int i = 1; i < MAXN; i++) {
fact[i] = i * fact[i - 1] % mod;
// invfact[i] = Inverse(fact[i], mod);
}
int N, M;
cin >> N >> M;
LL ans = N * binom(M + N - 2, N - 2) % mod;
for (int k = 0; k <= min(N, M); k++) {
LL q = 3 * M - k;
if (q % 2)
continue;
q /= 2;
LL ris = binom(q + N - 1, N - 1) - N * binom(q - M + N - 1, N - 1);
ris %= mod;
ris = ris * binom(N, k) % mod;
ans += ris;
ans %= mod;
}
if (ans < 0)
ans += mod;
cout << ans << "\n";
}
| replace | 124 | 125 | 124 | 125 | 0 | |
p02965 | C++ | Runtime Error | // ios::sync_with_stdio(false);
#include <bits/stdc++.h>
#define LL long long
#define F(x, y, z) for (int x = y; x <= z; ++x)
#define D(x, y, z) for (int x = y; x >= z; --x)
using namespace std;
const int Mod = 998244353;
const int N = 3000010;
const int maxn = 3000001;
LL Min(LL x, LL y) { return x < y ? x : y; }
LL Max(LL x, LL y) { return x > y ? x : y; }
LL R() {
LL ans = 0, f = 1;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar())
if (c == '-')
f = -1;
for (; c >= '0' && c <= '9'; c = getchar())
ans = ans * 10 + c - '0';
return ans * f;
}
LL jc[N], ni[N], n, m;
LL C(LL n, LL m) { return jc[n] * ni[m] % Mod * ni[n - m] % Mod; }
LL Calc(LL n, LL m, LL js) {
LL ans = 0;
F(i, 0, m) {
if ((i + m) & 1)
continue;
ans += C(n, i) * C(js + (m - i) / 2 + n - 1, n - 1);
ans %= Mod;
}
return ans;
}
LL Pow(LL x, LL bs) {
LL an = 1;
for (; bs; bs >>= 1, x = x * x % Mod)
if (bs & 1)
an = an * x % Mod;
return an;
}
void Pre() {
jc[0] = ni[0] = 1;
F(i, 1, maxn) jc[i] = jc[i - 1] * i % Mod;
ni[maxn] = Pow(jc[maxn], Mod - 2);
D(i, maxn - 1, 1) ni[i] = ni[i + 1] * (i + 1) % Mod;
}
int main() {
Pre();
n = R();
m = R();
LL ans = Calc(n, m, m);
ans -= n * (Calc(n, m, 0) - Calc(n - 1, m, 0));
ans %= Mod;
ans += Mod;
ans %= Mod;
cout << ans << '\n';
return 0;
} | // ios::sync_with_stdio(false);
#include <bits/stdc++.h>
#define LL long long
#define F(x, y, z) for (int x = y; x <= z; ++x)
#define D(x, y, z) for (int x = y; x >= z; --x)
using namespace std;
const int Mod = 998244353;
const int N = 3000010;
const int maxn = 3000001;
LL Min(LL x, LL y) { return x < y ? x : y; }
LL Max(LL x, LL y) { return x > y ? x : y; }
LL R() {
LL ans = 0, f = 1;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar())
if (c == '-')
f = -1;
for (; c >= '0' && c <= '9'; c = getchar())
ans = ans * 10 + c - '0';
return ans * f;
}
LL jc[N], ni[N], n, m;
LL C(LL n, LL m) { return jc[n] * ni[m] % Mod * ni[n - m] % Mod; }
LL Calc(LL n, LL m, LL js) {
LL ans = 0;
int ma = min(n, m);
F(i, 0, ma) {
if ((i + m) & 1)
continue;
ans += C(n, i) * C(js + (m - i) / 2 + n - 1, n - 1);
ans %= Mod;
}
return ans;
}
LL Pow(LL x, LL bs) {
LL an = 1;
for (; bs; bs >>= 1, x = x * x % Mod)
if (bs & 1)
an = an * x % Mod;
return an;
}
void Pre() {
jc[0] = ni[0] = 1;
F(i, 1, maxn) jc[i] = jc[i - 1] * i % Mod;
ni[maxn] = Pow(jc[maxn], Mod - 2);
D(i, maxn - 1, 1) ni[i] = ni[i + 1] * (i + 1) % Mod;
}
int main() {
Pre();
n = R();
m = R();
LL ans = Calc(n, m, m);
ans -= n * (Calc(n, m, 0) - Calc(n - 1, m, 0));
ans %= Mod;
ans += Mod;
ans %= Mod;
cout << ans << '\n';
return 0;
} | replace | 25 | 26 | 25 | 27 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
using namespace std;
ll p = 998244353, jc[4000010], ny[4000010];
ll ksm(ll x, ll y) {
ll xlh = 1;
while (y) {
if (y & 1)
xlh = xlh * x % p;
x = x * x % p;
y /= 2;
}
return xlh;
}
ll C(ll x, ll y) { return jc[x] * ny[y] % p * ny[x - y] % p; }
ll solve(ll n, ll zo, ll m) {
ll i, ans = 0, xlh, sy;
for (i = 0; i <= m; i++)
if (zo % 2 == i % 2) {
xlh = C(n, i);
sy = zo - i;
sy /= 2;
xlh = xlh * C(sy - 1 + n, n - 1) % p;
ans = (ans + xlh) % p;
}
return ans;
}
int main() {
ll i, n, m;
jc[0] = ny[0] = 1;
for (i = 1; i <= 4000000; i++)
jc[i] = jc[i - 1] * i % p;
ny[4000000] = ksm(jc[4000000], p - 2);
for (i = 4000000 - 1; i; i--)
ny[i] = ny[i + 1] * (i + 1) % p;
scanf("%lld%lld", &n, &m);
printf("%lld", (solve(n, m * 3, m) -
n * (solve(n, m, m) - solve(n - 1, m, m) + p) % p + p) %
p);
} | #include <bits/stdc++.h>
#define ll long long
using namespace std;
ll p = 998244353, jc[4000010], ny[4000010];
ll ksm(ll x, ll y) {
ll xlh = 1;
while (y) {
if (y & 1)
xlh = xlh * x % p;
x = x * x % p;
y /= 2;
}
return xlh;
}
ll C(ll x, ll y) {
if (x < 0 || y < 0 || x < y)
return 0;
return jc[x] * ny[y] % p * ny[x - y] % p;
}
ll solve(ll n, ll zo, ll m) {
ll i, ans = 0, xlh, sy;
for (i = 0; i <= m; i++)
if (zo % 2 == i % 2) {
xlh = C(n, i);
sy = zo - i;
sy /= 2;
xlh = xlh * C(sy - 1 + n, n - 1) % p;
ans = (ans + xlh) % p;
}
return ans;
}
int main() {
ll i, n, m;
jc[0] = ny[0] = 1;
for (i = 1; i <= 4000000; i++)
jc[i] = jc[i - 1] * i % p;
ny[4000000] = ksm(jc[4000000], p - 2);
for (i = 4000000 - 1; i; i--)
ny[i] = ny[i + 1] * (i + 1) % p;
scanf("%lld%lld", &n, &m);
printf("%lld", (solve(n, m * 3, m) -
n * (solve(n, m, m) - solve(n - 1, m, m) + p) % p + p) %
p);
} | replace | 14 | 15 | 14 | 19 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MX = 3000000, md = 998244353;
long long inv[MX], f[MX], inv_f[MX];
long long c(int n, int k) {
if (k == -1)
return n == -1 ? 1 : 0;
return f[n] * inv_f[n - k] % md * inv_f[k] % md;
}
int main() {
inv[1] = f[0] = f[1] = inv_f[0] = inv_f[1] = 1;
for (int i = 2; i < MX; i++) {
inv[i] = md - inv[md % i] * (md / i) % md;
f[i] = f[i - 1] * i % md;
inv_f[i] = inv_f[i - 1] * inv[i] % md;
}
int n, m;
ignore = scanf("%d %d", &n, &m);
int ans = 0;
for (int k = m % 2; k <= m; k += 2) {
ans = (ans + c(n, k) * c((3 * m - k) / 2 + n - 1, n - 1)) % md;
}
for (int k = 2 * m + 1; k <= 3 * m; k++) {
ans = (ans - c(3 * m - k + n - 2, n - 2) * n) % md;
}
if (ans < 0)
ans += md;
printf("%d\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int MX = 3000000, md = 998244353;
long long inv[MX], f[MX], inv_f[MX];
long long c(int n, int k) {
if (k == -1)
return n == -1 ? 1 : 0;
return f[n] * inv_f[n - k] % md * inv_f[k] % md;
}
int main() {
inv[1] = f[0] = f[1] = inv_f[0] = inv_f[1] = 1;
for (int i = 2; i < MX; i++) {
inv[i] = md - inv[md % i] * (md / i) % md;
f[i] = f[i - 1] * i % md;
inv_f[i] = inv_f[i - 1] * inv[i] % md;
}
int n, m;
ignore = scanf("%d %d", &n, &m);
int ans = 0;
for (int k = m % 2; k <= m && k <= n; k += 2) {
ans = (ans + c(n, k) * c((3 * m - k) / 2 + n - 1, n - 1)) % md;
}
for (int k = 2 * m + 1; k <= 3 * m; k++) {
ans = (ans - c(3 * m - k + n - 2, n - 2) * n) % md;
}
if (ans < 0)
ans += md;
printf("%d\n", ans);
return 0;
}
| replace | 27 | 28 | 27 | 28 | 0 | |
p02965 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int p = 998244353;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define mmax(a, b) a = max(a, b)
#define mmin(a, b) a = min(a, b)
#define mkp make_pair
#define pin pair<int, int>
#define qin pair<pin, int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define sq(a) (a) * (a)
#define rev(s) reverse(s.begin(), s.end())
#define end_pr(s) \
cout << s << endl; \
return 0
#define itn int
#define bs bitset<5001>
int kai_size = 2600001;
vel kai(kai_size, 1);
vel ink(kai_size, 1);
int RE() {
vel v(3, 2);
return v.at(4);
}
int ru(int a, int r) {
if (r == 0) {
return 1;
}
int ans = ru(a, r / 2);
ans *= ans;
ans %= p;
if (r % 2 == 1) {
ans *= a;
}
return ans % p;
}
int inv(int a) { return ru(a, p - 2); }
void make_kai() {
rep(i, kai_size - 1) { kai[i + 1] = (kai[i] * (i + 1)) % p; }
rep(i, 1000001) { ink[i] = inv(kai[i]); }
}
int com(int n, int r) {
int ans = kai[n] * ink[r];
ans %= p;
ans *= ink[n - r];
ans %= p;
return ans;
}
vel dis(int mid1, vvel &way) {
int n = way.size();
vel dist(n, -1);
dist[mid1] = 0;
queue<int> q;
q.push(mid1);
while (!q.empty()) {
int st = q.front();
q.pop();
rep(i, way[st].size()) {
int to = way[st][i];
if (dist[to] == -1) {
dist[to] = dist[st] + 1;
q.push(to);
}
}
}
return dist;
}
pin most_far(int now, int n, vvel &way) {
vel dist1 = dis(now, way);
pin ans = mkp(-1, 0);
rep(i, n) {
if (dist1[i] > ans.first) {
ans = mkp(dist1[i], i);
}
}
return ans;
}
int per(int a, int b) {
int ans = a % b;
if (ans < 0) {
ans += b;
}
return ans;
}
V<pin> uni(V<pin> &v) {
sor(v);
V<pin> ans(1, v[0]);
for (int i = 1; i < v.size(); i++) {
if (v[i] != v[i - 1]) {
ans.push_back(v[i]);
}
}
v = ans;
return v;
}
int s_gcd(int a, int b) {
if (b == 0) {
return a;
}
return s_gcd(b, a % b);
}
int gcd(int a, int b) {
if (a < b) {
swap(a, b);
}
return s_gcd(a, b);
}
signed main() {
int n, m;
cin >> n >> m;
int sum = 3 * m;
make_kai();
int inv1 = inv(kai[sum]);
int qans = kai[sum + n - 1];
qans *= ink[n - 1];
qans %= p;
qans *= inv1;
qans %= p;
int mi1 = 0;
for (int b = 0; b < m; b++) {
int plus = com(b + n - 2, n - 2);
mi1 += plus;
}
mi1 %= p;
mi1 *= n;
mi1 %= p;
int mi2 = 0;
for (int a = m + 2; a <= n; a += 2) {
int maru = (3 * m) - a;
maru /= 2;
int plus = com(maru + n - 1, n - 1);
int mul = com(n, a);
plus *= mul;
plus %= p;
mi2 += plus;
}
mi2 %= p;
int ans = qans + 2 * p - mi1 - mi2;
cout << ans % p << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int p = 998244353;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define mmax(a, b) a = max(a, b)
#define mmin(a, b) a = min(a, b)
#define mkp make_pair
#define pin pair<int, int>
#define qin pair<pin, int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define sq(a) (a) * (a)
#define rev(s) reverse(s.begin(), s.end())
#define end_pr(s) \
cout << s << endl; \
return 0
#define itn int
#define bs bitset<5001>
int kai_size = 2600001;
vel kai(kai_size, 1);
vel ink(kai_size, 1);
int RE() {
vel v(3, 2);
return v.at(4);
}
int ru(int a, int r) {
if (r == 0) {
return 1;
}
int ans = ru(a, r / 2);
ans *= ans;
ans %= p;
if (r % 2 == 1) {
ans *= a;
}
return ans % p;
}
int inv(int a) { return ru(a, p - 2); }
void make_kai() {
rep(i, kai_size - 1) { kai[i + 1] = (kai[i] * (i + 1)) % p; }
rep(i, 1000001) { ink[i] = inv(kai[i]); }
}
int com(int n, int r) {
int ans = kai[n] * ink[r];
ans %= p;
ans *= ink[n - r];
ans %= p;
return ans;
}
vel dis(int mid1, vvel &way) {
int n = way.size();
vel dist(n, -1);
dist[mid1] = 0;
queue<int> q;
q.push(mid1);
while (!q.empty()) {
int st = q.front();
q.pop();
rep(i, way[st].size()) {
int to = way[st][i];
if (dist[to] == -1) {
dist[to] = dist[st] + 1;
q.push(to);
}
}
}
return dist;
}
pin most_far(int now, int n, vvel &way) {
vel dist1 = dis(now, way);
pin ans = mkp(-1, 0);
rep(i, n) {
if (dist1[i] > ans.first) {
ans = mkp(dist1[i], i);
}
}
return ans;
}
int per(int a, int b) {
int ans = a % b;
if (ans < 0) {
ans += b;
}
return ans;
}
V<pin> uni(V<pin> &v) {
sor(v);
V<pin> ans(1, v[0]);
for (int i = 1; i < v.size(); i++) {
if (v[i] != v[i - 1]) {
ans.push_back(v[i]);
}
}
v = ans;
return v;
}
int s_gcd(int a, int b) {
if (b == 0) {
return a;
}
return s_gcd(b, a % b);
}
int gcd(int a, int b) {
if (a < b) {
swap(a, b);
}
return s_gcd(a, b);
}
signed main() {
int n, m;
cin >> n >> m;
int sum = 3 * m;
make_kai();
int inv1 = inv(kai[sum]);
int qans = kai[sum + n - 1];
qans *= ink[n - 1];
qans %= p;
qans *= inv1;
qans %= p;
int mi1 = 0;
for (int b = 0; b < m; b++) {
int plus = com(b + n - 2, n - 2);
mi1 += plus;
}
mi1 %= p;
mi1 *= n;
mi1 %= p;
int mi2 = 0;
for (int a = m + 2; a <= n; a += 2) {
int maru = (3 * m) - a;
maru /= 2;
if (maru >= 0) {
int plus = com(maru + n - 1, n - 1);
int mul = com(n, a);
plus *= mul;
plus %= p;
mi2 += plus;
}
}
mi2 %= p;
int ans = qans + 2 * p - mi1 - mi2;
cout << ans % p << endl;
return 0;
} | replace | 147 | 152 | 147 | 154 | 0 | |
p02965 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop \
char nyaa; \
cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define Per(i, sta, n) for (int i = n - 1; i >= sta; i--)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
template <int mod> struct ModInt {
long long x;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
explicit operator int() const { return x; }
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return ModInt(u);
}
ModInt power(long long p) const {
int a = x;
if (p == 0)
return 1;
if (p == 1)
return ModInt(a);
if (p % 2 == 1)
return (ModInt(a) * ModInt(a)).power(p / 2) * ModInt(a);
else
return (ModInt(a) * ModInt(a)).power(p / 2);
}
ModInt power(const ModInt p) const { return ((ModInt)x).power(p.x); }
friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
using modint = ModInt<mod>;
struct ModFac {
public:
vector<modint> f, i_f;
int n;
ModFac(int n_) {
n = n_;
f.resize(n + 1, 1);
i_f.resize(n + 1, 1);
for (int i = 0; i < n; i++) {
f[i + 1] = f[i] * (modint)(i + 1);
}
i_f[n] = f[n].power(mod - 2);
for (int i = n - 1; i >= 0; i--) {
i_f[i] = i_f[i + 1] * (modint)(i + 1);
}
}
ModFac(modint n_) {
n = (int)n_;
f.resize(n + 1, 1);
i_f.resize(n + 1, 1);
for (int i = 0; i < n; i++) {
f[i + 1] = f[i] * (modint)(i + 1);
}
i_f[n] = f[n].power(mod - 2);
for (int i = n - 1; i >= 0; i--) {
i_f[i] = i_f[i + 1] * (modint)(i + 1);
}
}
modint factorial(int x) {
// cout << f.size() << endl;
return f[x];
}
modint inv_factorial(int x) { return i_f[x]; }
modint comb(int m, int k) {
if (m < 0 or k < 0)
return 0;
if (m < k)
return 0;
return f[m] * i_f[k] * i_f[m - k];
}
};
int n, m;
ModFac F(200010);
void solve() {
cin >> n >> m;
modint A = 0;
rep(i, m + 1) {
if (i % 2 != m % 2)
continue;
A += F.comb(n, i) * F.comb((3 * m - i) / 2 + n - 1, n - 1);
}
// cout << A << endl;
modint B = 0;
rep(i, m + 1) {
if (i + (m + i) % 2 > m)
continue;
if ((m + i) % 2 == 1)
B += (modint)n * F.comb(n - 1, i) * F.comb((m - i) / 2 + n - 1, n - 1);
else
B += (modint)n * F.comb(n - 1, i) * F.comb((m - i) / 2 + n - 2, n - 1);
}
// cout << B << endl;
cout << A - B << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} | #include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop \
char nyaa; \
cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define Per(i, sta, n) for (int i = n - 1; i >= sta; i--)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
template <int mod> struct ModInt {
long long x;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
explicit operator int() const { return x; }
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return ModInt(u);
}
ModInt power(long long p) const {
int a = x;
if (p == 0)
return 1;
if (p == 1)
return ModInt(a);
if (p % 2 == 1)
return (ModInt(a) * ModInt(a)).power(p / 2) * ModInt(a);
else
return (ModInt(a) * ModInt(a)).power(p / 2);
}
ModInt power(const ModInt p) const { return ((ModInt)x).power(p.x); }
friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
using modint = ModInt<mod>;
struct ModFac {
public:
vector<modint> f, i_f;
int n;
ModFac(int n_) {
n = n_;
f.resize(n + 1, 1);
i_f.resize(n + 1, 1);
for (int i = 0; i < n; i++) {
f[i + 1] = f[i] * (modint)(i + 1);
}
i_f[n] = f[n].power(mod - 2);
for (int i = n - 1; i >= 0; i--) {
i_f[i] = i_f[i + 1] * (modint)(i + 1);
}
}
ModFac(modint n_) {
n = (int)n_;
f.resize(n + 1, 1);
i_f.resize(n + 1, 1);
for (int i = 0; i < n; i++) {
f[i + 1] = f[i] * (modint)(i + 1);
}
i_f[n] = f[n].power(mod - 2);
for (int i = n - 1; i >= 0; i--) {
i_f[i] = i_f[i + 1] * (modint)(i + 1);
}
}
modint factorial(int x) {
// cout << f.size() << endl;
return f[x];
}
modint inv_factorial(int x) { return i_f[x]; }
modint comb(int m, int k) {
if (m < 0 or k < 0)
return 0;
if (m < k)
return 0;
return f[m] * i_f[k] * i_f[m - k];
}
};
int n, m;
ModFac F(4000010);
void solve() {
cin >> n >> m;
modint A = 0;
rep(i, m + 1) {
if (i % 2 != m % 2)
continue;
A += F.comb(n, i) * F.comb((3 * m - i) / 2 + n - 1, n - 1);
}
// cout << A << endl;
modint B = 0;
rep(i, m + 1) {
if (i + (m + i) % 2 > m)
continue;
if ((m + i) % 2 == 1)
B += (modint)n * F.comb(n - 1, i) * F.comb((m - i) / 2 + n - 1, n - 1);
else
B += (modint)n * F.comb(n - 1, i) * F.comb((m - i) / 2 + n - 2, n - 1);
}
// cout << B << endl;
cout << A - B << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} | replace | 164 | 165 | 164 | 165 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 1e6 + 100, mod = 998244353;
ll power(ll n, ll k) {
if (k == 0)
return 1;
if (k % 2 == 1) {
ll x = power(n, k / 2);
return x * x % mod * n % mod;
}
ll x = power(n, k / 2);
return x * x % mod;
}
ll fac[N], fm[N];
ll ent(ll k, ll n) {
if (k < 0 || k > n)
return 0;
if (k == 0 || k == n)
return 1;
return fac[n] * fm[k] % mod * fm[n - k] % mod;
}
int32_t main() {
fac[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = fac[i - 1] * i % mod;
fm[i] = power(fac[i], mod - 2);
}
// cout << ent(3,5) << endl;
ll ans = 0;
ll n, m;
cin >> n >> m;
ll z = 3 * m;
for (int i = 0; i <= min(n, m); i++) {
ll t = z - i;
if (t % 2 == 1)
continue;
ans += ent(i, n) * ent(n - 1, n - 1 + t / 2) % mod;
}
for (int i = 2 * m + 1; i <= 3 * m; i++) {
ll t = z - i;
ans -= n * ent(n - 2, n - 2 + t) % mod;
ans += mod;
ans %= mod;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 2e6 + 100, mod = 998244353;
ll power(ll n, ll k) {
if (k == 0)
return 1;
if (k % 2 == 1) {
ll x = power(n, k / 2);
return x * x % mod * n % mod;
}
ll x = power(n, k / 2);
return x * x % mod;
}
ll fac[N], fm[N];
ll ent(ll k, ll n) {
if (k < 0 || k > n)
return 0;
if (k == 0 || k == n)
return 1;
return fac[n] * fm[k] % mod * fm[n - k] % mod;
}
int32_t main() {
fac[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = fac[i - 1] * i % mod;
fm[i] = power(fac[i], mod - 2);
}
// cout << ent(3,5) << endl;
ll ans = 0;
ll n, m;
cin >> n >> m;
ll z = 3 * m;
for (int i = 0; i <= min(n, m); i++) {
ll t = z - i;
if (t % 2 == 1)
continue;
ans += ent(i, n) * ent(n - 1, n - 1 + t / 2) % mod;
}
for (int i = 2 * m + 1; i <= 3 * m; i++) {
ll t = z - i;
ans -= n * ent(n - 2, n - 2 + t) % mod;
ans += mod;
ans %= mod;
}
cout << ans << endl;
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p02965 | C++ | Runtime Error | #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string.h>
#define N 2000005
using namespace std;
typedef long long ll;
const int mod = 998244353;
int n, m;
ll f[N], inv[N];
ll C(int n, int m) { return f[n] * inv[m] % mod * inv[n - m] % mod; }
ll solve(int n, int m) {
ll ans = 0;
for (int i = m & 1; i <= m; i += 2) {
ll w = C(n, i);
int res = m * 3 - i >> 1;
ans = (ans + w * C(res + n - 1, n - 1)) % mod;
}
return ans;
}
int main() {
scanf("%d%d", &n, &m);
f[0] = f[1] = inv[0] = inv[1] = 1;
for (int i = 2; i <= 2000002; i++)
f[i] = f[i - 1] * i % mod, inv[i] = (mod - mod / i) * inv[mod % i] % mod;
for (int i = 1; i <= 2000002; i++)
inv[i] = inv[i - 1] * inv[i] % mod;
ll ans = (solve(n, m) - C(n + m - 2, n - 1) * n) % mod;
ans = (ans + mod) % mod;
printf("%lld", ans);
return 0;
}
| #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string.h>
#define N 2000005
using namespace std;
typedef long long ll;
const int mod = 998244353;
int n, m;
ll f[N], inv[N];
ll C(int n, int m) { return f[n] * inv[m] % mod * inv[n - m] % mod; }
ll solve(int n, int m) {
ll ans = 0;
for (int i = m & 1; i <= m && i <= n; i += 2) {
ll w = C(n, i);
int res = m * 3 - i >> 1;
ans = (ans + w * C(res + n - 1, n - 1)) % mod;
}
return ans;
}
int main() {
scanf("%d%d", &n, &m);
f[0] = f[1] = inv[0] = inv[1] = 1;
for (int i = 2; i <= 2000002; i++)
f[i] = f[i - 1] * i % mod, inv[i] = (mod - mod / i) * inv[mod % i] % mod;
for (int i = 1; i <= 2000002; i++)
inv[i] = inv[i - 1] * inv[i] % mod;
ll ans = (solve(n, m) - C(n + m - 2, n - 1) * n) % mod;
ans = (ans + mod) % mod;
printf("%lld", ans);
return 0;
}
| replace | 14 | 15 | 14 | 15 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 5;
const LL mo = 998244353;
int n, m;
LL mi(LL x, LL y) {
LL re = 1;
for (; y; y >>= 1, x = x * x % mo)
if (y & 1)
re = re * x % mo;
return re;
}
LL fac[4 * maxn], ny[4 * maxn];
void Pre(int n) {
fac[0] = 1;
fo(i, 1, n) fac[i] = fac[i - 1] * i % mo;
ny[n] = mi(fac[n], mo - 2);
fd(i, n - 1, 0) ny[i] = ny[i + 1] * (i + 1) % mo;
}
LL C(int n, int m) { return fac[n] * ny[m] % mo * ny[n - m] % mo; }
LL calc(int n, int sum, int m) {
LL re = 0;
fo(i, 0, m) if (!((sum - i) & 1))(
re += C(n, i) * C((sum - i) / 2 + n - 1, n - 1)) %= mo;
return re;
}
int main() {
scanf("%d %d", &n, &m);
Pre(3 * m + n);
LL ans = calc(n, 3 * m, m) - (calc(n, m, m) - calc(n - 1, m, m)) % mo * n;
printf("%lld\n", (ans % mo + mo) % mo);
} | #include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 5;
const LL mo = 998244353;
int n, m;
LL mi(LL x, LL y) {
LL re = 1;
for (; y; y >>= 1, x = x * x % mo)
if (y & 1)
re = re * x % mo;
return re;
}
LL fac[4 * maxn], ny[4 * maxn];
void Pre(int n) {
fac[0] = 1;
fo(i, 1, n) fac[i] = fac[i - 1] * i % mo;
ny[n] = mi(fac[n], mo - 2);
fd(i, n - 1, 0) ny[i] = ny[i + 1] * (i + 1) % mo;
}
LL C(int n, int m) {
return (n < m) ? 0 : fac[n] * ny[m] % mo * ny[n - m] % mo;
}
LL calc(int n, int sum, int m) {
LL re = 0;
fo(i, 0, m) if (!((sum - i) & 1))(
re += C(n, i) * C((sum - i) / 2 + n - 1, n - 1)) %= mo;
return re;
}
int main() {
scanf("%d %d", &n, &m);
Pre(3 * m + n);
LL ans = calc(n, 3 * m, m) - (calc(n, m, m) - calc(n - 1, m, m)) % mo * n;
printf("%lld\n", (ans % mo + mo) % mo);
} | replace | 27 | 28 | 27 | 30 | 0 | |
p02965 | C++ | Runtime Error | #ifndef __INTMOD_H__0001__
#define __INTMOD_H__0001__
#include <cassert>
#include <iostream>
#include <vector>
/* Modulus must be less than 0x80000000, and must not be 0. */
template <uint32_t Modulus> class IntMod {
typedef int Int;
typedef unsigned int UInt;
typedef long long Long;
typedef unsigned long long ULong;
public:
template <uint32_t Modulus_>
friend bool operator==(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
template <uint32_t Modulus_>
friend bool operator!=(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
template <uint32_t Modulus_>
friend bool operator<(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
template <uint32_t Modulus_>
friend bool operator<=(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
template <uint32_t Modulus_>
friend bool operator>(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
template <uint32_t Modulus_>
friend bool operator>=(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
private:
UInt value_m;
public:
IntMod() { value_m = 0; }
IntMod(UInt value) { value_m = value % Modulus; }
IntMod(ULong value) { value_m = value % Modulus; }
IntMod(Int value) {
Int tmp = value % (Int)Modulus;
value_m = tmp >= 0 ? tmp : Modulus - (unsigned int)(-tmp);
}
IntMod(Long value) {
Int tmp = value % (Long)Modulus;
value_m = tmp >= 0 ? tmp : Modulus - (unsigned int)(-tmp);
}
IntMod(const IntMod &other) : value_m(other.value_m) {}
IntMod &operator=(const IntMod &other) {
value_m = other.value_m;
return *this;
}
const IntMod &operator+() const { return *this; }
IntMod operator-() const { return IntMod(Modulus - value_m); }
IntMod &operator++() {
++value_m;
if (value_m == Modulus)
value_m = 0;
return *this;
}
IntMod &operator--() {
if (value_m == 0)
value_m = Modulus;
--value_m;
return *this;
}
IntMod operator++(int dummy) {
IntMod tmp(*this);
++(*this);
return tmp;
}
IntMod operator--(int dummy) {
IntMod tmp(*this);
--(*this);
return tmp;
}
IntMod &operator+=(const IntMod &right) {
value_m += right.value_m; // value_m < 0x80000000
if (value_m >= Modulus)
value_m -= Modulus;
return *this;
}
IntMod &operator-=(const IntMod &right) {
if (value_m < right.value_m)
value_m += Modulus;
value_m -= right.value_m;
return *this;
}
IntMod &operator*=(const IntMod &right) {
value_m = ((ULong)value_m * right.value_m) % Modulus;
return *this;
}
IntMod &operator/=(const IntMod &right) {
(*this) *= (right.Inverse());
return *this;
}
// for power
IntMod operator[](ULong exp) const { return Pow(exp); }
/* Modulus must be a prime. */
IntMod Inverse() const { return (*this).Pow(Modulus - 2); }
IntMod Pow(ULong exp) const {
IntMod product = 1;
IntMod factor(*this);
while (exp > 0) {
if (exp & 1)
product *= factor;
factor *= factor;
exp >>= 1;
}
return product;
}
UInt Get_value() const { return value_m; }
static IntMod Fact(UInt num) {
static std::vector<IntMod> table(1, 1);
if (table.size() > num)
return table[num];
int old_size = table.size();
table.resize(num + 1);
for (int i = old_size; i <= num; i++) {
table[i] = table[i - 1] * i;
}
return table[num];
}
static IntMod Combi(UInt n, UInt r) {
if (n < r)
throw "okashii";
return IntMod::Fact(n) / (IntMod::Fact(n - r) * IntMod::Fact(r));
}
static IntMod Permutation(UInt n, UInt r) {
if (n < r)
throw "okashii";
return IntMod::Fact(n) / IntMod::Fact(n - r);
}
static std::vector<IntMod> Inverse_list(int size) {
assert(size < Modulus);
std::vector<IntMod> ret_arr(size + 1);
ret_arr[1] = 1;
for (int i = 2; i <= size; ++i) {
ret_arr[i] =
((ULong)(Modulus - Modulus / i) * ret_arr[Modulus % i].Get_value()) %
Modulus;
}
return ret_arr;
}
};
template <uint32_t Modulus>
IntMod<Modulus> operator+(const IntMod<Modulus> &left,
const IntMod<Modulus> &right) {
IntMod<Modulus> ret(left);
ret += right;
return ret;
}
template <uint32_t Modulus>
IntMod<Modulus> operator-(const IntMod<Modulus> &left,
const IntMod<Modulus> &right) {
IntMod<Modulus> ret(left);
ret -= right;
return ret;
}
template <uint32_t Modulus>
IntMod<Modulus> operator*(const IntMod<Modulus> &left,
const IntMod<Modulus> &right) {
IntMod<Modulus> ret(left);
ret *= right;
return ret;
}
template <uint32_t Modulus>
IntMod<Modulus> operator/(const IntMod<Modulus> &left,
const IntMod<Modulus> &right) {
IntMod<Modulus> ret(left);
ret /= right;
return ret;
}
template <uint32_t Modulus>
bool operator==(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m == right.value_m;
}
template <uint32_t Modulus>
bool operator!=(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m != right.value_m;
}
/* for set/map */
template <uint32_t Modulus>
bool operator<(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m < right.value_m;
}
template <uint32_t Modulus>
bool operator<=(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m <= right.value_m;
}
template <uint32_t Modulus>
bool operator>(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m > right.value_m;
}
template <uint32_t Modulus>
bool operator>=(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m >= right.value_m;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator+(const IntMod<Modulus> &left, Integer right) {
return left + IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator+(Integer left, const IntMod<Modulus> &right) {
return IntMod<Modulus>(left) + right;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator-(const IntMod<Modulus> &left, Integer right) {
return left - IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator-(Integer left, const IntMod<Modulus> &right) {
return IntMod<Modulus>(left) - right;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator*(const IntMod<Modulus> &left, Integer right) {
return left * IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator*(Integer left, const IntMod<Modulus> &right) {
return IntMod<Modulus>(left) * right;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator/(const IntMod<Modulus> &left, Integer right) {
return left / IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator/(Integer left, const IntMod<Modulus> &right) {
return IntMod<Modulus>(left) / right;
}
template <uint32_t Modulus>
std::istream &operator<<(std::istream &ist, const IntMod<Modulus> &val) {
uint64_t tmp;
ist >> tmp;
val = tmp;
return ist;
}
template <uint32_t Modulus>
std::ostream &operator<<(std::ostream &ost, const IntMod<Modulus> &val) {
ost << val.Get_value();
return ost;
}
typedef IntMod<1000000007> MInt;
#if 1
MInt operator"" _m(unsigned long long num) { return MInt(num); }
#endif
#endif
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n) for (LL i = (a), i##_max = (n); i < i##_max; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##min = (a); i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP \
{ \
cin.tie(NULL); \
ios::sync_with_stdio(false); \
}
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
for (int i = 0; i < M; ++i)
Fill(dest[i], val);
}
template <class T> T Compare(T left, T right) {
return left > right ? 1 : (left < right ? -1 : 0);
}
istream &Ignore(istream &ist) {
string s;
ist >> s;
return ist;
}
bool Inside(int i, int j, int h, int w) {
return i >= 0 && i < h && j >= 0 && j < w;
}
template <class T> T Next() {
T buf;
cin >> buf;
return buf;
}
#ifdef ONLY_MY_ENVIR
#include "BIT.h"
#include "BinaryMatrix.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Graph.h"
#include "IntMod.h"
#include "LazySegmentTree.h"
#include "Math.h"
#include "Matrix.h"
#include "MinMax.h"
#include "Position.h"
#include "Range.h"
#include "Rational.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "SuffixArray.h"
#include "Tree.h"
#include "UnionFind.h"
#endif
#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
LL tmp;
ist >> tmp;
val = tmp;
return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
LL tmp = val;
ost << tmp;
return ost;
}
#endif
#if 1234567891
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
// template <class T>
// auto Is(const T& value) { return [value](const auto& comparand) -> bool {
// return comparand == value; }; }
#endif
using MP = IntMod<998244353>;
int N, M;
MP H(int n, int r) { return MP::Combi(n + r - 1, r - 1); }
int main() {
cin >> N >> M;
MP ans = H(3 * M, N) - N * MP::Combi(N + M - 2, N - 1);
REP(i, 0, N + 1) {
if ((3 * M + i) % 2 == 1)
continue;
int even = (3 * M - i) / 2;
if (even >= i)
continue;
ans -= MP::Combi(N, i) * H(even, N);
}
cout << ans << endl;
return 0;
}
| #ifndef __INTMOD_H__0001__
#define __INTMOD_H__0001__
#include <cassert>
#include <iostream>
#include <vector>
/* Modulus must be less than 0x80000000, and must not be 0. */
template <uint32_t Modulus> class IntMod {
typedef int Int;
typedef unsigned int UInt;
typedef long long Long;
typedef unsigned long long ULong;
public:
template <uint32_t Modulus_>
friend bool operator==(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
template <uint32_t Modulus_>
friend bool operator!=(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
template <uint32_t Modulus_>
friend bool operator<(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
template <uint32_t Modulus_>
friend bool operator<=(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
template <uint32_t Modulus_>
friend bool operator>(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
template <uint32_t Modulus_>
friend bool operator>=(const IntMod<Modulus_> &left,
const IntMod<Modulus_> &right);
private:
UInt value_m;
public:
IntMod() { value_m = 0; }
IntMod(UInt value) { value_m = value % Modulus; }
IntMod(ULong value) { value_m = value % Modulus; }
IntMod(Int value) {
Int tmp = value % (Int)Modulus;
value_m = tmp >= 0 ? tmp : Modulus - (unsigned int)(-tmp);
}
IntMod(Long value) {
Int tmp = value % (Long)Modulus;
value_m = tmp >= 0 ? tmp : Modulus - (unsigned int)(-tmp);
}
IntMod(const IntMod &other) : value_m(other.value_m) {}
IntMod &operator=(const IntMod &other) {
value_m = other.value_m;
return *this;
}
const IntMod &operator+() const { return *this; }
IntMod operator-() const { return IntMod(Modulus - value_m); }
IntMod &operator++() {
++value_m;
if (value_m == Modulus)
value_m = 0;
return *this;
}
IntMod &operator--() {
if (value_m == 0)
value_m = Modulus;
--value_m;
return *this;
}
IntMod operator++(int dummy) {
IntMod tmp(*this);
++(*this);
return tmp;
}
IntMod operator--(int dummy) {
IntMod tmp(*this);
--(*this);
return tmp;
}
IntMod &operator+=(const IntMod &right) {
value_m += right.value_m; // value_m < 0x80000000
if (value_m >= Modulus)
value_m -= Modulus;
return *this;
}
IntMod &operator-=(const IntMod &right) {
if (value_m < right.value_m)
value_m += Modulus;
value_m -= right.value_m;
return *this;
}
IntMod &operator*=(const IntMod &right) {
value_m = ((ULong)value_m * right.value_m) % Modulus;
return *this;
}
IntMod &operator/=(const IntMod &right) {
(*this) *= (right.Inverse());
return *this;
}
// for power
IntMod operator[](ULong exp) const { return Pow(exp); }
/* Modulus must be a prime. */
IntMod Inverse() const { return (*this).Pow(Modulus - 2); }
IntMod Pow(ULong exp) const {
IntMod product = 1;
IntMod factor(*this);
while (exp > 0) {
if (exp & 1)
product *= factor;
factor *= factor;
exp >>= 1;
}
return product;
}
UInt Get_value() const { return value_m; }
static IntMod Fact(UInt num) {
static std::vector<IntMod> table(1, 1);
if (table.size() > num)
return table[num];
int old_size = table.size();
table.resize(num + 1);
for (int i = old_size; i <= num; i++) {
table[i] = table[i - 1] * i;
}
return table[num];
}
static IntMod Combi(UInt n, UInt r) {
if (n < r)
throw "okashii";
return IntMod::Fact(n) / (IntMod::Fact(n - r) * IntMod::Fact(r));
}
static IntMod Permutation(UInt n, UInt r) {
if (n < r)
throw "okashii";
return IntMod::Fact(n) / IntMod::Fact(n - r);
}
static std::vector<IntMod> Inverse_list(int size) {
assert(size < Modulus);
std::vector<IntMod> ret_arr(size + 1);
ret_arr[1] = 1;
for (int i = 2; i <= size; ++i) {
ret_arr[i] =
((ULong)(Modulus - Modulus / i) * ret_arr[Modulus % i].Get_value()) %
Modulus;
}
return ret_arr;
}
};
template <uint32_t Modulus>
IntMod<Modulus> operator+(const IntMod<Modulus> &left,
const IntMod<Modulus> &right) {
IntMod<Modulus> ret(left);
ret += right;
return ret;
}
template <uint32_t Modulus>
IntMod<Modulus> operator-(const IntMod<Modulus> &left,
const IntMod<Modulus> &right) {
IntMod<Modulus> ret(left);
ret -= right;
return ret;
}
template <uint32_t Modulus>
IntMod<Modulus> operator*(const IntMod<Modulus> &left,
const IntMod<Modulus> &right) {
IntMod<Modulus> ret(left);
ret *= right;
return ret;
}
template <uint32_t Modulus>
IntMod<Modulus> operator/(const IntMod<Modulus> &left,
const IntMod<Modulus> &right) {
IntMod<Modulus> ret(left);
ret /= right;
return ret;
}
template <uint32_t Modulus>
bool operator==(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m == right.value_m;
}
template <uint32_t Modulus>
bool operator!=(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m != right.value_m;
}
/* for set/map */
template <uint32_t Modulus>
bool operator<(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m < right.value_m;
}
template <uint32_t Modulus>
bool operator<=(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m <= right.value_m;
}
template <uint32_t Modulus>
bool operator>(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m > right.value_m;
}
template <uint32_t Modulus>
bool operator>=(const IntMod<Modulus> &left, const IntMod<Modulus> &right) {
return left.value_m >= right.value_m;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator+(const IntMod<Modulus> &left, Integer right) {
return left + IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator+(Integer left, const IntMod<Modulus> &right) {
return IntMod<Modulus>(left) + right;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator-(const IntMod<Modulus> &left, Integer right) {
return left - IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator-(Integer left, const IntMod<Modulus> &right) {
return IntMod<Modulus>(left) - right;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator*(const IntMod<Modulus> &left, Integer right) {
return left * IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator*(Integer left, const IntMod<Modulus> &right) {
return IntMod<Modulus>(left) * right;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator/(const IntMod<Modulus> &left, Integer right) {
return left / IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator/(Integer left, const IntMod<Modulus> &right) {
return IntMod<Modulus>(left) / right;
}
template <uint32_t Modulus>
std::istream &operator<<(std::istream &ist, const IntMod<Modulus> &val) {
uint64_t tmp;
ist >> tmp;
val = tmp;
return ist;
}
template <uint32_t Modulus>
std::ostream &operator<<(std::ostream &ost, const IntMod<Modulus> &val) {
ost << val.Get_value();
return ost;
}
typedef IntMod<1000000007> MInt;
#if 1
MInt operator"" _m(unsigned long long num) { return MInt(num); }
#endif
#endif
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n) for (LL i = (a), i##_max = (n); i < i##_max; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##min = (a); i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP \
{ \
cin.tie(NULL); \
ios::sync_with_stdio(false); \
}
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
for (int i = 0; i < M; ++i)
Fill(dest[i], val);
}
template <class T> T Compare(T left, T right) {
return left > right ? 1 : (left < right ? -1 : 0);
}
istream &Ignore(istream &ist) {
string s;
ist >> s;
return ist;
}
bool Inside(int i, int j, int h, int w) {
return i >= 0 && i < h && j >= 0 && j < w;
}
template <class T> T Next() {
T buf;
cin >> buf;
return buf;
}
#ifdef ONLY_MY_ENVIR
#include "BIT.h"
#include "BinaryMatrix.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Graph.h"
#include "IntMod.h"
#include "LazySegmentTree.h"
#include "Math.h"
#include "Matrix.h"
#include "MinMax.h"
#include "Position.h"
#include "Range.h"
#include "Rational.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "SuffixArray.h"
#include "Tree.h"
#include "UnionFind.h"
#endif
#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
LL tmp;
ist >> tmp;
val = tmp;
return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
LL tmp = val;
ost << tmp;
return ost;
}
#endif
#if 1234567891
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
// template <class T>
// auto Is(const T& value) { return [value](const auto& comparand) -> bool {
// return comparand == value; }; }
#endif
using MP = IntMod<998244353>;
int N, M;
MP H(int n, int r) { return MP::Combi(n + r - 1, r - 1); }
int main() {
cin >> N >> M;
MP ans = H(3 * M, N) - N * MP::Combi(N + M - 2, N - 1);
REP(i, 0, N + 1) {
if ((3 * M + i) % 2 == 1)
continue;
int even = (3 * M - i) / 2;
if (even >= i || even < 0)
continue;
ans -= MP::Combi(N, i) * H(even, N);
}
cout << ans << endl;
return 0;
}
| replace | 409 | 410 | 409 | 410 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define N 1000005
int P[N];
int PI[N];
#define MOD 998244353
ll modInverse(ll a, ll m) {
ll m0 = m;
ll y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
int q = a / m;
int t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
void fun() {
P[0] = 1;
int i;
for (i = 1; i < N; i++) {
P[i] = (P[i - 1] * i) % MOD;
}
for (i = 0; i < N; i++) {
PI[i] = modInverse(P[i], MOD);
}
}
int ncr(int n, int r) {
int ans = P[n];
ans *= PI[r];
ans %= MOD;
ans *= PI[n - r];
ans %= MOD;
return ans;
}
int distribute(int x, int n) {
int ans = ncr(n + x - 1, n - 1);
return ans;
}
int dp[N];
int32_t main() {
ios_base::sync_with_stdio(false);
fun();
int n, m;
cin >> n >> m;
int ans = 0;
for (int i = 0; i <= min(n, m); i++) {
int left = m - i;
if (left % 2)
continue;
int temp = m + left / 2;
int dis = distribute(temp, n);
ans += dis * ncr(n, i);
ans %= MOD;
}
for (int i = 2 * m + 1; i <= 3 * m; i++) {
int temp = 3 * m - i;
temp = distribute(temp, n - 1);
ans -= temp * n;
ans %= MOD;
if (ans < 0)
ans += MOD;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define N 3000005
int P[N];
int PI[N];
#define MOD 998244353
ll modInverse(ll a, ll m) {
ll m0 = m;
ll y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
int q = a / m;
int t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
void fun() {
P[0] = 1;
int i;
for (i = 1; i < N; i++) {
P[i] = (P[i - 1] * i) % MOD;
}
for (i = 0; i < N; i++) {
PI[i] = modInverse(P[i], MOD);
}
}
int ncr(int n, int r) {
int ans = P[n];
ans *= PI[r];
ans %= MOD;
ans *= PI[n - r];
ans %= MOD;
return ans;
}
int distribute(int x, int n) {
int ans = ncr(n + x - 1, n - 1);
return ans;
}
int dp[N];
int32_t main() {
ios_base::sync_with_stdio(false);
fun();
int n, m;
cin >> n >> m;
int ans = 0;
for (int i = 0; i <= min(n, m); i++) {
int left = m - i;
if (left % 2)
continue;
int temp = m + left / 2;
int dis = distribute(temp, n);
ans += dis * ncr(n, i);
ans %= MOD;
}
for (int i = 2 * m + 1; i <= 3 * m; i++) {
int temp = 3 * m - i;
temp = distribute(temp, n - 1);
ans -= temp * n;
ans %= MOD;
if (ans < 0)
ans += MOD;
}
cout << ans << endl;
return 0;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p02965 | C++ | Runtime Error | #include <bits/stdc++.h>
#define itn int
#define all(x) (x).begin(), (x).end()
#define make_unique(x) \
sort(all((x))); \
(x).resize(unique(all((x))) - (x).begin())
#define random_shuffle(...) shuffle(__VA_ARGS__, rng)
#define rand() rng()
using namespace std;
inline int nxt() {
int x;
scanf("%d", &x);
return x;
}
const int N = 5111111;
long long fact[N], inv[N], invfact[N];
const int mod = 998244353;
long long C(int n, int k) {
return fact[n] * invfact[k] % mod * invfact[n - k] % mod;
}
int main() {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
fact[0] = invfact[0] = 1;
for (int i = 1; i < N; ++i) {
inv[i] = (i == 1) ? 1 : mod - inv[mod % i] * (mod / i) % mod;
fact[i] = fact[i - 1] * i % mod;
invfact[i] = invfact[i - 1] * inv[i] % mod;
}
int n = nxt(), m = nxt();
long long ans = C(3 * m + n - 1, n - 1) - n * C(m + n - 2, n - 1);
for (int i = m + 1; i <= n; ++i) {
if (i % 2 != m % 2) {
continue;
}
long long tmp = C((3 * m - i) / 2 + n - 1, n - 1) * C(n, i) % mod;
ans -= tmp;
}
ans %= mod;
cout << (ans + mod) % mod << "\n";
return 0;
} | #include <bits/stdc++.h>
#define itn int
#define all(x) (x).begin(), (x).end()
#define make_unique(x) \
sort(all((x))); \
(x).resize(unique(all((x))) - (x).begin())
#define random_shuffle(...) shuffle(__VA_ARGS__, rng)
#define rand() rng()
using namespace std;
inline int nxt() {
int x;
scanf("%d", &x);
return x;
}
const int N = 5111111;
long long fact[N], inv[N], invfact[N];
const int mod = 998244353;
long long C(int n, int k) {
if (n < k) {
return 0;
}
return fact[n] * invfact[k] % mod * invfact[n - k] % mod;
}
int main() {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
fact[0] = invfact[0] = 1;
for (int i = 1; i < N; ++i) {
inv[i] = (i == 1) ? 1 : mod - inv[mod % i] * (mod / i) % mod;
fact[i] = fact[i - 1] * i % mod;
invfact[i] = invfact[i - 1] * inv[i] % mod;
}
int n = nxt(), m = nxt();
long long ans = C(3 * m + n - 1, n - 1) - n * C(m + n - 2, n - 1);
for (int i = m + 1; i <= n; ++i) {
if (i % 2 != m % 2) {
continue;
}
long long tmp = C((3 * m - i) / 2 + n - 1, n - 1) * C(n, i) % mod;
ans -= tmp;
}
ans %= mod;
cout << (ans + mod) % mod << "\n";
return 0;
} | insert | 24 | 24 | 24 | 27 | -11 | |
p02965 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using ll = long long;
using namespace std;
#include <cassert>
#define MOD (long)(998244353)
#define MAX 1000000 // 階乗をいくつまで計算するか
class modlong {
long val;
static const long mod = MOD;
static long *invs, *facts, *finvs;
// 階乗, 逆元, 階乗の逆元をMAXまで求める
bool initModlong() {
invs[1] = facts[0] = facts[1] = finvs[0] = finvs[1] = 1;
for (int i = 2; i <= MAX; i++) {
invs[i] = -invs[MOD % i] * (MOD / i) % MOD;
facts[i] = facts[i - 1] * i % MOD;
finvs[i] = finvs[i - 1] * invs[i] % MOD;
}
return true;
}
public:
// 初期化 値を引数に与えなかった場合はval=0としておく
modlong(long init = 0) : val(init) {
static bool call_once =
initModlong(); // static変数の性質により一度だけ呼ばれる
assert(call_once); // unusedの回避
if (val < 0 || val >= mod)
val %= mod;
if (val < 0)
val += mod; // 0以上であることを保証
}
// longへのキャスト operator long()で定義すると modlong +
// longとかができなくなる
inline long tol() { return this->val; }
// 代入
void operator=(const modlong &r) { this->val = r.val; }
void operator=(const long &r) { *this = modlong(r); }
// 比較
bool operator<(const modlong &r) { return this->val < r.val; }
bool operator>(const modlong &r) { return this->val > r.val; }
bool operator==(const modlong &r) { return this->val == r.val; }
bool operator!=(const modlong &r) { return !(*this == r); }
bool operator<=(const modlong &r) { return !(*this > r); }
bool operator>=(const modlong &r) { return !(*this < r); }
// 足し算; 符号反転; 引き算
modlong operator+(const modlong &r) {
long ans = this->val + r.val;
if (ans >= mod)
ans -= mod;
return modlong(ans);
}
modlong operator-() {
long ans = mod - this->val;
return modlong(ans);
}
modlong operator-(const modlong &r) {
modlong rr = r;
return *this + (-rr);
}
// かけ算; 逆元; わり算
modlong operator*(const modlong &r) {
long ans = this->val * r.val;
return modlong(ans);
}
modlong inv() {
assert(*this != 0);
if (*this == 1)
return modlong(1);
modlong p, q = *this, m(0), n(1), r, c;
p.val = mod; // p=modとかくとp.val=mod%mod=0となってしまう
while (q > MAX) {
r = p.val % q.val; // r.val=p.val % q.val
// とかくよりもこのほうが代入時に%modされるので安全
c = m.val - n.val * (p.val / q.val);
p = q, q = r, m = n, n = c;
}
return n * invs[q.val];
}
modlong operator/(const modlong &r) { return *this * modlong(r).inv(); }
// ++ -- 前付きと後ろ付き
void operator++() { ++this->val; }
void operator++(int a) {
a = 0;
this->val++;
} // a使ってなくねっていうwarningをsilenceするためにaをいじってる
void operator--() { --this->val; }
void operator--(int a) {
a = 0;
this->val--;
}
// 四則演算&代入
void operator+=(const modlong &r) { *this = *this + r; }
void operator-=(const modlong &r) { *this = *this - r; }
void operator*=(const modlong &r) { *this = *this * r; }
void operator/=(const modlong &r) { *this = *this / r; }
// べき乗
modlong pow(long n) {
if (n < 0)
return inv().pow(-n); // 逆元の-n乗
else if (n == 0)
return modlong(1);
modlong half = pow(n / 2);
if (n % 2)
return *this * half * half;
else
return half * half;
}
modlong pow(modlong n) { return pow(n.val); }
// コンビネーション
modlong comb(modlong _k) {
assert(this->val <= MAX);
const long n = this->val, k = _k.val;
if (k < 0 || k > n)
return 0;
if (k == 0 || k == n)
return 1;
return modlong(facts[n]) * finvs[k] * finvs[n - k];
}
// 階乗
modlong fact() {
assert(this->val <= MAX);
return modlong(facts[this->val]);
}
friend ostream &operator<<(ostream &os, const modlong &out);
friend istream &operator>>(istream &is, modlong &out);
};
// cout、cerr、cin用の演算子たち
ostream &operator<<(ostream &os, const modlong &out) {
os << out.val;
return os;
}
istream &operator>>(istream &is, modlong &in) {
long inl;
is >> inl;
in.val = inl % MOD;
return is;
}
// コンビネーション
inline modlong modComb(long n, long k) { return modlong(n).comb(k); }
// 階乗
inline modlong modFact(long n) { return modlong(n).fact(); }
// static変数たち
long *modlong::invs = new long[MAX + 1], *modlong::facts = new long[MAX + 1],
*modlong::finvs = new long[MAX + 1];
int main() {
int n, m;
cin >> n >> m;
modlong ans = 0;
for (int i = m % 2; i <= n && i <= m; i += 2) { // 最終的な{x}のうちi個が奇数
modlong twos = m + (m - i) / 2;
ans += (twos + n - 1).comb(twos) * modlong(n).comb(i);
}
ans -= modlong(n) *
modlong(m - 1 + n - 1).comb(m - 1); // 2*mを超えるxiがいる場合を引く
cout << ans << '\n';
return 0;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using ll = long long;
using namespace std;
#include <cassert>
#define MOD (long)(998244353)
#define MAX 5000000 // 階乗をいくつまで計算するか
class modlong {
long val;
static const long mod = MOD;
static long *invs, *facts, *finvs;
// 階乗, 逆元, 階乗の逆元をMAXまで求める
bool initModlong() {
invs[1] = facts[0] = facts[1] = finvs[0] = finvs[1] = 1;
for (int i = 2; i <= MAX; i++) {
invs[i] = -invs[MOD % i] * (MOD / i) % MOD;
facts[i] = facts[i - 1] * i % MOD;
finvs[i] = finvs[i - 1] * invs[i] % MOD;
}
return true;
}
public:
// 初期化 値を引数に与えなかった場合はval=0としておく
modlong(long init = 0) : val(init) {
static bool call_once =
initModlong(); // static変数の性質により一度だけ呼ばれる
assert(call_once); // unusedの回避
if (val < 0 || val >= mod)
val %= mod;
if (val < 0)
val += mod; // 0以上であることを保証
}
// longへのキャスト operator long()で定義すると modlong +
// longとかができなくなる
inline long tol() { return this->val; }
// 代入
void operator=(const modlong &r) { this->val = r.val; }
void operator=(const long &r) { *this = modlong(r); }
// 比較
bool operator<(const modlong &r) { return this->val < r.val; }
bool operator>(const modlong &r) { return this->val > r.val; }
bool operator==(const modlong &r) { return this->val == r.val; }
bool operator!=(const modlong &r) { return !(*this == r); }
bool operator<=(const modlong &r) { return !(*this > r); }
bool operator>=(const modlong &r) { return !(*this < r); }
// 足し算; 符号反転; 引き算
modlong operator+(const modlong &r) {
long ans = this->val + r.val;
if (ans >= mod)
ans -= mod;
return modlong(ans);
}
modlong operator-() {
long ans = mod - this->val;
return modlong(ans);
}
modlong operator-(const modlong &r) {
modlong rr = r;
return *this + (-rr);
}
// かけ算; 逆元; わり算
modlong operator*(const modlong &r) {
long ans = this->val * r.val;
return modlong(ans);
}
modlong inv() {
assert(*this != 0);
if (*this == 1)
return modlong(1);
modlong p, q = *this, m(0), n(1), r, c;
p.val = mod; // p=modとかくとp.val=mod%mod=0となってしまう
while (q > MAX) {
r = p.val % q.val; // r.val=p.val % q.val
// とかくよりもこのほうが代入時に%modされるので安全
c = m.val - n.val * (p.val / q.val);
p = q, q = r, m = n, n = c;
}
return n * invs[q.val];
}
modlong operator/(const modlong &r) { return *this * modlong(r).inv(); }
// ++ -- 前付きと後ろ付き
void operator++() { ++this->val; }
void operator++(int a) {
a = 0;
this->val++;
} // a使ってなくねっていうwarningをsilenceするためにaをいじってる
void operator--() { --this->val; }
void operator--(int a) {
a = 0;
this->val--;
}
// 四則演算&代入
void operator+=(const modlong &r) { *this = *this + r; }
void operator-=(const modlong &r) { *this = *this - r; }
void operator*=(const modlong &r) { *this = *this * r; }
void operator/=(const modlong &r) { *this = *this / r; }
// べき乗
modlong pow(long n) {
if (n < 0)
return inv().pow(-n); // 逆元の-n乗
else if (n == 0)
return modlong(1);
modlong half = pow(n / 2);
if (n % 2)
return *this * half * half;
else
return half * half;
}
modlong pow(modlong n) { return pow(n.val); }
// コンビネーション
modlong comb(modlong _k) {
assert(this->val <= MAX);
const long n = this->val, k = _k.val;
if (k < 0 || k > n)
return 0;
if (k == 0 || k == n)
return 1;
return modlong(facts[n]) * finvs[k] * finvs[n - k];
}
// 階乗
modlong fact() {
assert(this->val <= MAX);
return modlong(facts[this->val]);
}
friend ostream &operator<<(ostream &os, const modlong &out);
friend istream &operator>>(istream &is, modlong &out);
};
// cout、cerr、cin用の演算子たち
ostream &operator<<(ostream &os, const modlong &out) {
os << out.val;
return os;
}
istream &operator>>(istream &is, modlong &in) {
long inl;
is >> inl;
in.val = inl % MOD;
return is;
}
// コンビネーション
inline modlong modComb(long n, long k) { return modlong(n).comb(k); }
// 階乗
inline modlong modFact(long n) { return modlong(n).fact(); }
// static変数たち
long *modlong::invs = new long[MAX + 1], *modlong::facts = new long[MAX + 1],
*modlong::finvs = new long[MAX + 1];
int main() {
int n, m;
cin >> n >> m;
modlong ans = 0;
for (int i = m % 2; i <= n && i <= m; i += 2) { // 最終的な{x}のうちi個が奇数
modlong twos = m + (m - i) / 2;
ans += (twos + n - 1).comb(twos) * modlong(n).comb(i);
}
ans -= modlong(n) *
modlong(m - 1 + n - 1).comb(m - 1); // 2*mを超えるxiがいる場合を引く
cout << ans << '\n';
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p02965 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int ll;
ll mod = 998244353;
ll mod_pow(ll a, ll b) {
a %= mod;
if (b == 0)
return 1;
if (b == 1)
return a;
ll res = mod_pow(a, b / 2) % mod;
res *= res;
res %= mod;
if (b % 2)
res *= a;
return res % mod;
}
struct perm {
private:
int sz;
vector<ll> p, invp;
public:
perm(int n) {
sz = n + 1;
p.resize(sz), invp.resize(sz);
p[0] = 1;
for (int i = 1; i <= sz - 1; i++) {
p[i] = p[i - 1] * i % mod;
}
invp[sz - 1] = mod_pow(p[sz - 1], mod - 2);
for (int i = sz - 2; i >= 0; i--) {
invp[i] = invp[i + 1] * (i + 1) % mod;
}
}
ll comb(ll x, ll y) {
if (x < y || y < 0)
return 0;
return (p[x] * invp[x - y] % mod) * invp[y] % mod;
}
};
perm p(1 << 20);
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
ll ans = 0;
for (int i = 0; i <= min(n, m); i++) {
if ((3 * m - i) % 2)
continue;
ll k = (3 * m - i) / 2;
(ans += p.comb(n, i) * p.comb(n - 1 + k, k) % mod) %= mod;
}
ll res = 0;
ll res2 = 0;
for (int i = 0; i <= (m, n); i++) {
if ((m - i) % 2)
continue;
ll k = (m - i) / 2;
(res += p.comb(n, i) * p.comb(n - 1 + k, k) % mod) %= mod;
}
for (int i = 0; i <= (m, n - 1); i++) {
if ((m - i) % 2)
continue;
ll k = (m - i) / 2;
(res2 += p.comb(n - 1, i) * p.comb(n - 2 + k, k) % mod) %= mod;
}
res -= res2;
res %= mod;
ans -= res * n;
ans %= mod;
if (ans < 0)
ans += mod;
cout << ans << endl;
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int ll;
ll mod = 998244353;
ll mod_pow(ll a, ll b) {
a %= mod;
if (b == 0)
return 1;
if (b == 1)
return a;
ll res = mod_pow(a, b / 2) % mod;
res *= res;
res %= mod;
if (b % 2)
res *= a;
return res % mod;
}
struct perm {
private:
int sz;
vector<ll> p, invp;
public:
perm(int n) {
sz = n + 1;
p.resize(sz), invp.resize(sz);
p[0] = 1;
for (int i = 1; i <= sz - 1; i++) {
p[i] = p[i - 1] * i % mod;
}
invp[sz - 1] = mod_pow(p[sz - 1], mod - 2);
for (int i = sz - 2; i >= 0; i--) {
invp[i] = invp[i + 1] * (i + 1) % mod;
}
}
ll comb(ll x, ll y) {
if (x < y || y < 0)
return 0;
return (p[x] * invp[x - y] % mod) * invp[y] % mod;
}
};
perm p(5000000);
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
ll ans = 0;
for (int i = 0; i <= min(n, m); i++) {
if ((3 * m - i) % 2)
continue;
ll k = (3 * m - i) / 2;
(ans += p.comb(n, i) * p.comb(n - 1 + k, k) % mod) %= mod;
}
ll res = 0;
ll res2 = 0;
for (int i = 0; i <= (m, n); i++) {
if ((m - i) % 2)
continue;
ll k = (m - i) / 2;
(res += p.comb(n, i) * p.comb(n - 1 + k, k) % mod) %= mod;
}
for (int i = 0; i <= (m, n - 1); i++) {
if ((m - i) % 2)
continue;
ll k = (m - i) / 2;
(res2 += p.comb(n - 1, i) * p.comb(n - 2 + k, k) % mod) %= mod;
}
res -= res2;
res %= mod;
ans -= res * n;
ans %= mod;
if (ans < 0)
ans += mod;
cout << ans << endl;
}
| replace | 47 | 48 | 47 | 48 | 0 | |
p02965 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define mod 998244353
#define mad(a, b) a = (a + b) % mod
#define mul(a, b) a = (a * b) % mod
#define N 1500010
ll fac[N], inv[N], ivf[N];
ll C(ll n, ll k) {
if (n < k)
return 0;
return fac[n] * ivf[n - k] % mod * ivf[k] % mod;
}
ll H(ll n, ll k) {
// if(k==0)return 1;
return C(n + k - 1, k);
}
ll P(ll n, ll k) { return fac[n] * ivf[n - k] % mod; }
ll po(ll x, ll y) {
ll res = 1;
for (; y; y >>= 1) {
if (y & 1)
res = res * x % mod;
x = x * x % mod;
}
return res;
}
int main() {
fac[0] = ivf[0] = inv[1] = 1;
for (int i = 1; i < N; i++) {
if (i > 1)
inv[i] = (mod - mod / i * inv[mod % i] % mod) % mod;
fac[i] = fac[i - 1] * i % mod;
ivf[i] = ivf[i - 1] * inv[i] % mod;
}
ll n, m;
cin >> n >> m;
ll ans = 0;
for (ll p = 0; p <= m; p++) {
if ((3 * m - p) % 2 == 1)
continue;
ll cur = H(n, (3 * m - p) / 2) * C(n, p);
mad(ans, cur);
// cout<<cur<<endl;
}
mad(ans, -n * H(n, m - 1));
if (ans < 0)
ans += mod;
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define mod 998244353
#define mad(a, b) a = (a + b) % mod
#define mul(a, b) a = (a * b) % mod
#define N 5000010
ll fac[N], inv[N], ivf[N];
ll C(ll n, ll k) {
if (n < k)
return 0;
return fac[n] * ivf[n - k] % mod * ivf[k] % mod;
}
ll H(ll n, ll k) {
// if(k==0)return 1;
return C(n + k - 1, k);
}
ll P(ll n, ll k) { return fac[n] * ivf[n - k] % mod; }
ll po(ll x, ll y) {
ll res = 1;
for (; y; y >>= 1) {
if (y & 1)
res = res * x % mod;
x = x * x % mod;
}
return res;
}
int main() {
fac[0] = ivf[0] = inv[1] = 1;
for (int i = 1; i < N; i++) {
if (i > 1)
inv[i] = (mod - mod / i * inv[mod % i] % mod) % mod;
fac[i] = fac[i - 1] * i % mod;
ivf[i] = ivf[i - 1] * inv[i] % mod;
}
ll n, m;
cin >> n >> m;
ll ans = 0;
for (ll p = 0; p <= m; p++) {
if ((3 * m - p) % 2 == 1)
continue;
ll cur = H(n, (3 * m - p) / 2) * C(n, p);
mad(ans, cur);
// cout<<cur<<endl;
}
mad(ans, -n * H(n, m - 1));
if (ans < 0)
ans += mod;
cout << ans << endl;
return 0;
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p02965 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef string S;
typedef queue<int> qi;
typedef pair<ll, ll> P;
typedef vector<P> vp;
// #define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define elif else if
#define unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define mp make_pair
#define fr(i, n) for (ll i = 0; i < (n); i++)
#define ifr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define ALL(a) (a).begin(), (a).end()
const int MAX = 510000;
const int MOD = 998244353;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
// 前処理
COMinit();
ll n, m;
cin >> n >> m;
ll ans = COM(3 * m + n - 1, n - 1);
fr(i, m) {
ans -= COM(n - 2 + i, n - 2) * n;
ans %= MOD;
if (ans < 0) {
ans += MOD;
}
}
for (ll i = m + 1; i <= min(3 * m, n); i++) {
if ((3 * m - i) % 2 == 0) {
ans -= COM(n, i) * COM((3 * m - i) / 2 + n - 1, n - 1);
ans %= MOD;
if (ans < 0) {
ans += MOD;
}
}
}
cout << ans << endl;
} | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef string S;
typedef queue<int> qi;
typedef pair<ll, ll> P;
typedef vector<P> vp;
// #define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define elif else if
#define unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define mp make_pair
#define fr(i, n) for (ll i = 0; i < (n); i++)
#define ifr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define ALL(a) (a).begin(), (a).end()
const int MAX = 2510000;
const int MOD = 998244353;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
// 前処理
COMinit();
ll n, m;
cin >> n >> m;
ll ans = COM(3 * m + n - 1, n - 1);
fr(i, m) {
ans -= COM(n - 2 + i, n - 2) * n;
ans %= MOD;
if (ans < 0) {
ans += MOD;
}
}
for (ll i = m + 1; i <= min(3 * m, n); i++) {
if ((3 * m - i) % 2 == 0) {
ans -= COM(n, i) * COM((3 * m - i) / 2 + n - 1, n - 1);
ans %= MOD;
if (ans < 0) {
ans += MOD;
}
}
}
cout << ans << endl;
} | replace | 39 | 40 | 39 | 40 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.