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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02734 | C++ | Runtime Error | #include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
int main() {
// dp[i + 1][j] A[i]までで和がjにできるような場合の数
ll N, S, MOD = 998244353;
cin >> N >> S;
vector<ll> A(N);
rep(i, N) cin >> A[i];
vector<vector<ll>> dp(N + 1, vector<ll>(S + 1, 0));
rep(i, N) {
dp[i + 1][A[i]] += i + 1;
dp[i + 1][A[i]] %= MOD;
rep(j, S + 1) {
dp[i + 1][j] += dp[i][j];
dp[i + 1][j] %= MOD;
if (j + A[i] <= S) {
dp[i + 1][j + A[i]] += dp[i][j];
dp[i + 1][j + A[i]] %= MOD;
}
}
}
int ans = 0;
rep(i, N + 1) {
ans += dp[i][S];
ans %= MOD;
}
cout << ans << "\n";
/*
rep(i, N + 1) {
rep(j, S + 1) {
cout << dp[i][j] << " ";
}
cout << "\n";
}
*/
} | #include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
int main() {
// dp[i + 1][j] A[i]までで和がjにできるような場合の数
ll N, S, MOD = 998244353;
cin >> N >> S;
vector<ll> A(N);
rep(i, N) cin >> A[i];
vector<vector<ll>> dp(N + 1, vector<ll>(3001, 0));
rep(i, N) {
dp[i + 1][A[i]] += i + 1;
dp[i + 1][A[i]] %= MOD;
rep(j, S + 1) {
dp[i + 1][j] += dp[i][j];
dp[i + 1][j] %= MOD;
if (j + A[i] <= S) {
dp[i + 1][j + A[i]] += dp[i][j];
dp[i + 1][j + A[i]] %= MOD;
}
}
}
int ans = 0;
rep(i, N + 1) {
ans += dp[i][S];
ans %= MOD;
}
cout << ans << "\n";
/*
rep(i, N + 1) {
rep(j, S + 1) {
cout << dp[i][j] << " ";
}
cout << "\n";
}
*/
} | replace | 12 | 13 | 12 | 13 | 0 | |
p02734 | C++ | Time Limit Exceeded | #ifdef LOCAL
#pragma GCC optimize("O0")
#else
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx")
#endif
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VLL = vector<ll>;
using VVLL = vector<vector<ll>>;
using VB = vector<bool>;
using VVB = vector<vector<bool>>;
using PII = pair<int, int>;
template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
const int INF = 1e9 + 7;
const ll INF_LL = (ll)1e18 + 7;
#define __overload3(_1, _2, _3, name, ...) name
#define rep(...) \
__overload3(__VA_ARGS__, repFromUntil, repUntil, repeat)(__VA_ARGS__)
#define repeat(times) repFromUntil(__name, 0, times)
#define repUntil(name, times) repFromUntil(name, 0, times)
#define repFromUntil(name, from, until) \
for (int name = from, name##__until = (until); name < name##__until; name++)
#define repr(...) \
__overload3(__VA_ARGS__, reprFromUntil, reprUntil, repeat)(__VA_ARGS__)
#define reprUntil(name, times) reprFromUntil(name, 0, times)
#define reprFromUntil(name, from, until) \
for (int name = until - 1, name##__from = (from); name >= name##__from; \
name--)
#define EXIT(out) \
do { \
OUT(out); \
exit(0); \
} while (0)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define _1 first
#define _2 second
#define debug(v) \
do { \
debugos << "L" << __LINE__ << " " << #v << " > "; \
debugos << (v) << newl; \
} while (0)
#define debugv(v) \
do { \
debugos << "L" << __LINE__ << " " << #v << " > "; \
for (auto e : (v)) { \
debugos << e << " "; \
} \
debugos << newl; \
} while (0)
#define debuga(m, w) \
do { \
debugos << "L" << __LINE__ << " " << #m << " > "; \
for (int x = 0; x < (w); x++) { \
debugos << (m)[x] << " "; \
} \
debugos << newl; \
} while (0)
#define debugaa(m, h, w) \
do { \
debugos << "L" << __LINE__ << " " << #m << " > \n"; \
for (int y = 0; y < (h); y++) { \
for (int x = 0; x < (w); x++) { \
debugos << (m)[y][x] << " "; \
} \
debugos << newl; \
} \
} while (0)
#define newl "\n"
constexpr int dr[] = {1, -1, 0, 0};
constexpr int dc[] = {0, 0, 1, -1};
bool inside(int r, int c, int H, int W) {
return 0 <= r and r < H and 0 <= c and c < W;
}
template <typename T> bool chmin(T &var, T x) {
if (var > x) {
var = x;
return true;
} else
return false;
}
template <typename T> bool chmax(T &var, T x) {
if (var < x) {
var = x;
return true;
} else
return false;
}
template <typename T> struct minT {
T operator()(T a, T b) { return min(a, b); }
};
template <typename T> struct maxT {
T operator()(T a, T b) { return max(a, b); }
};
template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); }
ll power(ll e, int t, ll mod = INF_LL) {
ll res = 1;
while (t) {
if (t & 1)
res = (res * e) % mod;
t >>= 1;
e = (e * e) % mod;
}
return res;
}
template <typename T> T divceil(T, T);
template <typename T> T divfloor(T m, T d) {
if (sgn(m) * sgn(d) >= 0)
return m / d;
else
return -divceil(abs(m), abs(d));
}
template <typename T> T divceil(T m, T d) {
if (m >= 0 and d > 0)
return (m + d - 1) / d;
else if (m < 0 and d < 0)
return divceil(-m, -d);
else
return -divfloor(abs(m), abs(d));
}
template <typename T> vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
string operator*(const string &s, int times) {
string res = "";
rep(times) res += s;
return res;
}
class MyScanner {
public:
int offset = 0;
template <typename T> void input_integer(T &var) {
var = 0;
T sign = 1;
int cc = getchar();
for (; cc < '0' || '9' < cc; cc = getchar())
if (cc == '-')
sign = -1;
for (; '0' <= cc && cc <= '9'; cc = getchar())
var = (var << 3) + (var << 1) + cc - '0';
var = var * sign;
var += offset;
}
int c() {
char c;
while (c = getchar(), c == ' ' or c == '\n')
;
return c;
}
MyScanner &operator>>(char &var) {
var = c();
return *this;
}
MyScanner &operator>>(int &var) {
input_integer<int>(var);
return *this;
}
MyScanner &operator>>(ll &var) {
input_integer<ll>(var);
return *this;
}
MyScanner &operator>>(string &var) {
int cc = getchar();
for (; !isvisiblechar(cc); cc = getchar())
;
for (; isvisiblechar(cc); cc = getchar())
var.push_back(cc);
return *this;
}
template <typename T> operator T() {
T x;
*this >> x;
return x;
}
template <typename T> void operator()(T &t) { *this >> t; }
template <typename T, typename... Ts> void operator()(T &t, Ts &...ts) {
*this >> t;
this->operator()(ts...);
}
template <typename Iter> void iter(Iter first, Iter last) {
while (first != last)
*this >> *first, first++;
}
VI vi(int n) {
VI res(n);
iter(all(res));
return res;
}
VVI vvi(int n, int m) {
VVI res(n);
rep(i, n) res[i] = vi(m);
return res;
}
VLL vll(int n) {
VLL res(n);
iter(all(res));
return res;
}
VVLL vvll(int n, int m) {
VVLL res(n);
rep(i, n) res[i] = vll(m);
return res;
}
template <typename T> vector<T> v(int n) {
vector<T> res(n);
iter(all(res));
return res;
}
private:
int isvisiblechar(int c) { return 0x21 <= c && c <= 0x7E; }
} IN, IN1{-1};
class MyPrinter {
public:
template <typename T> void output_integer(T var) {
if (var == 0) {
putchar('0');
return;
}
if (var < 0)
putchar('-'), var = -var;
char stack[32];
int stack_p = 0;
while (var)
stack[stack_p++] = '0' + (var % 10), var /= 10;
while (stack_p)
putchar(stack[--stack_p]);
}
MyPrinter &operator<<(char c) {
putchar(c);
return *this;
}
template <typename T> MyPrinter &operator<<(T var) {
output_integer<T>(var);
return *this;
}
MyPrinter &operator<<(char *str_p) {
while (*str_p)
putchar(*(str_p++));
return *this;
}
MyPrinter &operator<<(const char *str_p) {
while (*str_p)
putchar(*(str_p++));
return *this;
}
MyPrinter &operator<<(const string &str) {
const char *p = str.c_str();
const char *l = p + str.size();
while (p < l)
putchar(*p++);
return *this;
}
// MyPrinter& operator<<(const modint& var) { output_integer<ll>(var.value);
// return *this; }
template <typename T> void operator()(T x) { *this << x << newl; }
template <typename T, typename... Ts> void operator()(T x, Ts... xs) {
*this << x << " ";
this->operator()(xs...);
}
template <typename Iter> void iter(Iter s, Iter t) {
if (s == t)
*this << "\n";
else {
for (; s != t; s++) {
*this << *s << " \n"[next(s, 1) == t];
}
}
}
template <typename Range> void range(const Range &r) {
iter(begin(r), end(r));
}
} OUT;
class DebugPrint {
public:
template <typename T> DebugPrint &operator<<(const T &v) {
#ifdef LOCAL
cerr << v;
#endif
return *this;
}
} debugos;
class modint;
constexpr modint pow(modint, size_t);
constexpr ll MOD = 998244353;
class modint {
public:
ll value;
constexpr modint(const ll x = 0) noexcept : value(x) {
value %= MOD;
if (value < 0)
value += MOD;
}
constexpr bool operator==(const modint &rhs) { return value == rhs.value; }
constexpr bool operator!=(const modint &rhs) { return value != rhs.value; }
constexpr modint operator+(const modint &rhs) const {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint &rhs) const {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint &rhs) const {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint &rhs) const {
return modint(*this) /= rhs;
}
constexpr modint &operator+=(const modint &rhs) {
value += rhs.value;
if (value >= MOD)
value -= MOD;
return *this;
}
constexpr modint &operator-=(const modint &rhs) {
if (value < rhs.value)
value += MOD;
value -= rhs.value;
return *this;
}
constexpr modint &operator*=(const modint &rhs) {
value = value * rhs.value % MOD;
return *this;
}
constexpr modint &operator/=(const modint &rhs) {
return *this *= pow(rhs, MOD - 2);
}
constexpr modint &operator++() { return *this += 1; }
constexpr modint operator++(int) {
modint tmp(*this);
++(*this);
return tmp;
}
constexpr modint &operator--() { return *this -= 1; }
constexpr modint operator--(int) {
modint tmp(*this);
--(*this);
return tmp;
}
constexpr operator int() const { return (int)value; }
constexpr operator ll() const { return value; }
};
MyPrinter &operator<<(MyPrinter &out, modint n) {
out << n.value;
return out;
}
DebugPrint &operator<<(DebugPrint &out, modint n) {
cerr << n.value;
return out;
}
constexpr modint pow(modint base, size_t exp) {
modint res = 1;
while (exp) {
if (exp % 2)
res *= base;
base *= base;
exp /= 2;
}
return res;
}
// O(r + log MOD)
modint choose(int n, int r) {
chmin(r, n - r);
if (r < 0)
return modint(0);
modint nu = 1, de = 1;
rep(i, r) nu *= n - i, de *= i + 1;
return nu / de;
}
int main() {
int n = IN, s = IN;
auto a = IN.vll(n);
vector<modint> dp(s);
dp[0] = modint(1);
modint res = 0;
rep(i, n) {
if (a[i] < s)
res += dp[s - a[i]] * modint(n - i);
if (a[i] == s)
res += modint(i + 1) * modint(n - i);
repr(s1, a[i] + 1, s) { dp[s1] += dp[s1 - a[i]]; }
if (a[i] < s) {
dp[a[i]] += modint(i + 1);
}
rep(s1, s) debugos << dp[s1] << " ";
debugos << newl << res << newl;
}
OUT(res);
// vector<bitset<3005>> dp(s+1);
// modint res = 0;
// rep(i, n) repr(s1, a[i], s+1) {
// if (s1 < s) {
// if (s1 == a[i]) dp[s1][i] = 1;
// dp[s1] |= dp[s1-a[i]];
// } else {
// bitset<3005> st = dp[s-a[i]];
// if (s1 == a[i]) st[i] = 1;
// rep(j, i+1) if (st[j]) res += modint(j+1) * modint(n-i);
// }
// }
// OUT(res);
} | #ifdef LOCAL
#pragma GCC optimize("O0")
#else
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx")
#endif
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VLL = vector<ll>;
using VVLL = vector<vector<ll>>;
using VB = vector<bool>;
using VVB = vector<vector<bool>>;
using PII = pair<int, int>;
template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
const int INF = 1e9 + 7;
const ll INF_LL = (ll)1e18 + 7;
#define __overload3(_1, _2, _3, name, ...) name
#define rep(...) \
__overload3(__VA_ARGS__, repFromUntil, repUntil, repeat)(__VA_ARGS__)
#define repeat(times) repFromUntil(__name, 0, times)
#define repUntil(name, times) repFromUntil(name, 0, times)
#define repFromUntil(name, from, until) \
for (int name = from, name##__until = (until); name < name##__until; name++)
#define repr(...) \
__overload3(__VA_ARGS__, reprFromUntil, reprUntil, repeat)(__VA_ARGS__)
#define reprUntil(name, times) reprFromUntil(name, 0, times)
#define reprFromUntil(name, from, until) \
for (int name = until - 1, name##__from = (from); name >= name##__from; \
name--)
#define EXIT(out) \
do { \
OUT(out); \
exit(0); \
} while (0)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define _1 first
#define _2 second
#define debug(v) \
do { \
debugos << "L" << __LINE__ << " " << #v << " > "; \
debugos << (v) << newl; \
} while (0)
#define debugv(v) \
do { \
debugos << "L" << __LINE__ << " " << #v << " > "; \
for (auto e : (v)) { \
debugos << e << " "; \
} \
debugos << newl; \
} while (0)
#define debuga(m, w) \
do { \
debugos << "L" << __LINE__ << " " << #m << " > "; \
for (int x = 0; x < (w); x++) { \
debugos << (m)[x] << " "; \
} \
debugos << newl; \
} while (0)
#define debugaa(m, h, w) \
do { \
debugos << "L" << __LINE__ << " " << #m << " > \n"; \
for (int y = 0; y < (h); y++) { \
for (int x = 0; x < (w); x++) { \
debugos << (m)[y][x] << " "; \
} \
debugos << newl; \
} \
} while (0)
#define newl "\n"
constexpr int dr[] = {1, -1, 0, 0};
constexpr int dc[] = {0, 0, 1, -1};
bool inside(int r, int c, int H, int W) {
return 0 <= r and r < H and 0 <= c and c < W;
}
template <typename T> bool chmin(T &var, T x) {
if (var > x) {
var = x;
return true;
} else
return false;
}
template <typename T> bool chmax(T &var, T x) {
if (var < x) {
var = x;
return true;
} else
return false;
}
template <typename T> struct minT {
T operator()(T a, T b) { return min(a, b); }
};
template <typename T> struct maxT {
T operator()(T a, T b) { return max(a, b); }
};
template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); }
ll power(ll e, int t, ll mod = INF_LL) {
ll res = 1;
while (t) {
if (t & 1)
res = (res * e) % mod;
t >>= 1;
e = (e * e) % mod;
}
return res;
}
template <typename T> T divceil(T, T);
template <typename T> T divfloor(T m, T d) {
if (sgn(m) * sgn(d) >= 0)
return m / d;
else
return -divceil(abs(m), abs(d));
}
template <typename T> T divceil(T m, T d) {
if (m >= 0 and d > 0)
return (m + d - 1) / d;
else if (m < 0 and d < 0)
return divceil(-m, -d);
else
return -divfloor(abs(m), abs(d));
}
template <typename T> vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
string operator*(const string &s, int times) {
string res = "";
rep(times) res += s;
return res;
}
class MyScanner {
public:
int offset = 0;
template <typename T> void input_integer(T &var) {
var = 0;
T sign = 1;
int cc = getchar();
for (; cc < '0' || '9' < cc; cc = getchar())
if (cc == '-')
sign = -1;
for (; '0' <= cc && cc <= '9'; cc = getchar())
var = (var << 3) + (var << 1) + cc - '0';
var = var * sign;
var += offset;
}
int c() {
char c;
while (c = getchar(), c == ' ' or c == '\n')
;
return c;
}
MyScanner &operator>>(char &var) {
var = c();
return *this;
}
MyScanner &operator>>(int &var) {
input_integer<int>(var);
return *this;
}
MyScanner &operator>>(ll &var) {
input_integer<ll>(var);
return *this;
}
MyScanner &operator>>(string &var) {
int cc = getchar();
for (; !isvisiblechar(cc); cc = getchar())
;
for (; isvisiblechar(cc); cc = getchar())
var.push_back(cc);
return *this;
}
template <typename T> operator T() {
T x;
*this >> x;
return x;
}
template <typename T> void operator()(T &t) { *this >> t; }
template <typename T, typename... Ts> void operator()(T &t, Ts &...ts) {
*this >> t;
this->operator()(ts...);
}
template <typename Iter> void iter(Iter first, Iter last) {
while (first != last)
*this >> *first, first++;
}
VI vi(int n) {
VI res(n);
iter(all(res));
return res;
}
VVI vvi(int n, int m) {
VVI res(n);
rep(i, n) res[i] = vi(m);
return res;
}
VLL vll(int n) {
VLL res(n);
iter(all(res));
return res;
}
VVLL vvll(int n, int m) {
VVLL res(n);
rep(i, n) res[i] = vll(m);
return res;
}
template <typename T> vector<T> v(int n) {
vector<T> res(n);
iter(all(res));
return res;
}
private:
int isvisiblechar(int c) { return 0x21 <= c && c <= 0x7E; }
} IN, IN1{-1};
class MyPrinter {
public:
template <typename T> void output_integer(T var) {
if (var == 0) {
putchar('0');
return;
}
if (var < 0)
putchar('-'), var = -var;
char stack[32];
int stack_p = 0;
while (var)
stack[stack_p++] = '0' + (var % 10), var /= 10;
while (stack_p)
putchar(stack[--stack_p]);
}
MyPrinter &operator<<(char c) {
putchar(c);
return *this;
}
template <typename T> MyPrinter &operator<<(T var) {
output_integer<T>(var);
return *this;
}
MyPrinter &operator<<(char *str_p) {
while (*str_p)
putchar(*(str_p++));
return *this;
}
MyPrinter &operator<<(const char *str_p) {
while (*str_p)
putchar(*(str_p++));
return *this;
}
MyPrinter &operator<<(const string &str) {
const char *p = str.c_str();
const char *l = p + str.size();
while (p < l)
putchar(*p++);
return *this;
}
// MyPrinter& operator<<(const modint& var) { output_integer<ll>(var.value);
// return *this; }
template <typename T> void operator()(T x) { *this << x << newl; }
template <typename T, typename... Ts> void operator()(T x, Ts... xs) {
*this << x << " ";
this->operator()(xs...);
}
template <typename Iter> void iter(Iter s, Iter t) {
if (s == t)
*this << "\n";
else {
for (; s != t; s++) {
*this << *s << " \n"[next(s, 1) == t];
}
}
}
template <typename Range> void range(const Range &r) {
iter(begin(r), end(r));
}
} OUT;
class DebugPrint {
public:
template <typename T> DebugPrint &operator<<(const T &v) {
#ifdef LOCAL
cerr << v;
#endif
return *this;
}
} debugos;
class modint;
constexpr modint pow(modint, size_t);
constexpr ll MOD = 998244353;
class modint {
public:
ll value;
constexpr modint(const ll x = 0) noexcept : value(x) {
value %= MOD;
if (value < 0)
value += MOD;
}
constexpr bool operator==(const modint &rhs) { return value == rhs.value; }
constexpr bool operator!=(const modint &rhs) { return value != rhs.value; }
constexpr modint operator+(const modint &rhs) const {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint &rhs) const {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint &rhs) const {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint &rhs) const {
return modint(*this) /= rhs;
}
constexpr modint &operator+=(const modint &rhs) {
value += rhs.value;
if (value >= MOD)
value -= MOD;
return *this;
}
constexpr modint &operator-=(const modint &rhs) {
if (value < rhs.value)
value += MOD;
value -= rhs.value;
return *this;
}
constexpr modint &operator*=(const modint &rhs) {
value = value * rhs.value % MOD;
return *this;
}
constexpr modint &operator/=(const modint &rhs) {
return *this *= pow(rhs, MOD - 2);
}
constexpr modint &operator++() { return *this += 1; }
constexpr modint operator++(int) {
modint tmp(*this);
++(*this);
return tmp;
}
constexpr modint &operator--() { return *this -= 1; }
constexpr modint operator--(int) {
modint tmp(*this);
--(*this);
return tmp;
}
constexpr operator int() const { return (int)value; }
constexpr operator ll() const { return value; }
};
MyPrinter &operator<<(MyPrinter &out, modint n) {
out << n.value;
return out;
}
DebugPrint &operator<<(DebugPrint &out, modint n) {
cerr << n.value;
return out;
}
constexpr modint pow(modint base, size_t exp) {
modint res = 1;
while (exp) {
if (exp % 2)
res *= base;
base *= base;
exp /= 2;
}
return res;
}
// O(r + log MOD)
modint choose(int n, int r) {
chmin(r, n - r);
if (r < 0)
return modint(0);
modint nu = 1, de = 1;
rep(i, r) nu *= n - i, de *= i + 1;
return nu / de;
}
int main() {
int n = IN, s = IN;
auto a = IN.vll(n);
vector<modint> dp(s);
dp[0] = modint(1);
modint res = 0;
rep(i, n) {
if (a[i] < s)
res += dp[s - a[i]] * modint(n - i);
if (a[i] == s)
res += modint(i + 1) * modint(n - i);
repr(s1, a[i] + 1, s) { dp[s1] += dp[s1 - a[i]]; }
if (a[i] < s) {
dp[a[i]] += modint(i + 1);
}
}
OUT(res);
// vector<bitset<3005>> dp(s+1);
// modint res = 0;
// rep(i, n) repr(s1, a[i], s+1) {
// if (s1 < s) {
// if (s1 == a[i]) dp[s1][i] = 1;
// dp[s1] |= dp[s1-a[i]];
// } else {
// bitset<3005> st = dp[s-a[i]];
// if (s1 == a[i]) st[i] = 1;
// rep(j, i+1) if (st[j]) res += modint(j+1) * modint(n-i);
// }
// }
// OUT(res);
} | delete | 434 | 436 | 434 | 434 | TLE | |
p02734 | C++ | Runtime Error | #pragma region template 2.4
#include <bits/stdc++.h>
using namespace std;
template <typename T> using pq_asc = priority_queue<T, vector<T>, greater<T>>;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP1(i, n) for (ll i = 1; i <= (n); ++i)
#define FOR(i, a) for (auto &i : a)
#define CH(f, x, y) x = f(x, y)
#define IN(T, x) \
T x; \
cin >> x;
#define AIN(T, a, n) \
vector<T> a(n); \
FOR(i, a) \
cin >> i;
#define A2IN(T1, a, T2, b, n) \
vector<T1> a(n); \
vector<T2> b(n); \
REP(i, n) \
cin >> a[i] >> b[i];
#define OUT(x) cout << (x) << endl;
#define FOUT(x) cout << fixed << setprecision(15) << (x) << endl;
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a) \
SORT(a); \
reverse(ALL(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define DUMPA(a) \
cout << #a << " = {"; \
JOUT(ALL(a), ", ", cout) << "}" << endl;
template <typename T>
ostream &JOUT(T s, T e, string sep = " ", ostream &os = cout) {
if (s != e) {
os << *s;
++s;
}
while (s != e) {
os << sep << *s;
++s;
}
return os;
}
ostream &YES(bool cond, string yes = "Yes", string no = "No",
ostream &os = cout) {
if (cond) {
os << yes << endl;
} else {
os << no << endl;
}
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << '(' << p.first << ", " << p.second << ')';
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '[';
JOUT(ALL(v), ", ", os) << ']';
return os;
}
const ll INF = 1e18;
const ll MOD = 998244353;
#pragma endregion template
#pragma region mint 1.1
struct mint {
ll a;
mint(ll a = 0) : a((a % MOD + MOD) % MOD) {}
mint pow(ll y) {
ll exp = y;
mint res = 1;
mint p = a;
while (exp) {
if (exp & 1) {
res *= p;
}
exp >>= 1;
p *= p;
}
return res;
}
mint pow(mint y) { return pow(y.a); }
mint &operator+=(ll y) {
*this = a + y;
return *this;
}
mint &operator+=(mint y) {
*this += y.a;
return *this;
}
mint &operator-=(ll y) {
*this = a - y;
return *this;
}
mint &operator-=(mint y) {
*this -= y.a;
return *this;
}
mint &operator*=(ll y) {
*this = a * y;
return *this;
}
mint &operator*=(mint y) {
*this *= y.a;
return *this;
}
mint &operator/=(ll y) {
*this *= mint(y).pow(MOD - 2);
return *this;
}
mint &operator/=(const mint y) {
*this /= y.a;
return *this;
}
mint &operator++() {
*this += 1;
return *this;
}
mint &operator++(int) {
*this += 1;
return *this;
}
mint &operator--() {
*this -= 1;
return *this;
}
mint &operator--(int) {
*this -= 1;
return *this;
}
};
mint operator+(const mint &x, const mint &y) { return mint(x.a) += y; }
mint operator+(const mint &x, const ll &y) { return mint(x.a) += y; }
mint operator+(const ll &x, mint &y) { return mint(x) += y; }
mint operator-(const mint &x, const mint &y) { return mint(x.a) -= y; }
mint operator-(const mint &x, const ll &y) { return mint(x.a) -= y; }
mint operator-(const ll &x, const mint &y) { return mint(x) -= y; }
mint operator*(const mint &x, const mint &y) { return mint(x.a) *= y; }
mint operator*(const mint &x, const ll &y) { return mint(x.a) *= y; }
mint operator*(const ll &x, const mint &y) { return mint(x) *= y; }
mint operator/(const mint &x, const mint &y) { return mint(x.a) /= y; }
mint operator/(const mint &x, const ll &y) { return mint(x.a) /= y; }
mint operator/(const ll &x, const mint &y) { return mint(x) /= y; }
bool operator==(const mint &x, const mint &y) { return x.a == y.a; }
bool operator==(const mint &x, const ll &y) { return x.a == y; }
bool operator==(const ll &x, mint &y) { return x == y.a; }
bool operator!=(const mint &x, const mint &y) { return !(x == y); }
bool operator!=(const mint &x, const ll &y) { return !(x == y); }
bool operator!=(const ll &x, mint &y) { return !(x == y); }
istream &operator>>(istream &is, mint &i) {
is >> i.a;
return is;
}
ostream &operator<<(ostream &os, const mint &i) {
os << i.a;
return os;
}
typedef vector<mint> vm;
#pragma endregion mint
int main() {
IN(ll, N);
IN(ll, S);
AIN(ll, A, N);
vector<vm> dp(N + 1, vm(S + 1));
REP(i, N) {
for (ll j = S; j >= 0; --j) {
dp[i + 1][j] = dp[i][j] + dp[i][max(0LL, j - A[i])];
}
dp[i + 1][A[i]] += i + 1;
}
mint ans;
REP1(i, N) { ans += dp[i][S]; }
OUT(ans);
} | #pragma region template 2.4
#include <bits/stdc++.h>
using namespace std;
template <typename T> using pq_asc = priority_queue<T, vector<T>, greater<T>>;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP1(i, n) for (ll i = 1; i <= (n); ++i)
#define FOR(i, a) for (auto &i : a)
#define CH(f, x, y) x = f(x, y)
#define IN(T, x) \
T x; \
cin >> x;
#define AIN(T, a, n) \
vector<T> a(n); \
FOR(i, a) \
cin >> i;
#define A2IN(T1, a, T2, b, n) \
vector<T1> a(n); \
vector<T2> b(n); \
REP(i, n) \
cin >> a[i] >> b[i];
#define OUT(x) cout << (x) << endl;
#define FOUT(x) cout << fixed << setprecision(15) << (x) << endl;
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a) \
SORT(a); \
reverse(ALL(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define DUMPA(a) \
cout << #a << " = {"; \
JOUT(ALL(a), ", ", cout) << "}" << endl;
template <typename T>
ostream &JOUT(T s, T e, string sep = " ", ostream &os = cout) {
if (s != e) {
os << *s;
++s;
}
while (s != e) {
os << sep << *s;
++s;
}
return os;
}
ostream &YES(bool cond, string yes = "Yes", string no = "No",
ostream &os = cout) {
if (cond) {
os << yes << endl;
} else {
os << no << endl;
}
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << '(' << p.first << ", " << p.second << ')';
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '[';
JOUT(ALL(v), ", ", os) << ']';
return os;
}
const ll INF = 1e18;
const ll MOD = 998244353;
#pragma endregion template
#pragma region mint 1.1
struct mint {
ll a;
mint(ll a = 0) : a((a % MOD + MOD) % MOD) {}
mint pow(ll y) {
ll exp = y;
mint res = 1;
mint p = a;
while (exp) {
if (exp & 1) {
res *= p;
}
exp >>= 1;
p *= p;
}
return res;
}
mint pow(mint y) { return pow(y.a); }
mint &operator+=(ll y) {
*this = a + y;
return *this;
}
mint &operator+=(mint y) {
*this += y.a;
return *this;
}
mint &operator-=(ll y) {
*this = a - y;
return *this;
}
mint &operator-=(mint y) {
*this -= y.a;
return *this;
}
mint &operator*=(ll y) {
*this = a * y;
return *this;
}
mint &operator*=(mint y) {
*this *= y.a;
return *this;
}
mint &operator/=(ll y) {
*this *= mint(y).pow(MOD - 2);
return *this;
}
mint &operator/=(const mint y) {
*this /= y.a;
return *this;
}
mint &operator++() {
*this += 1;
return *this;
}
mint &operator++(int) {
*this += 1;
return *this;
}
mint &operator--() {
*this -= 1;
return *this;
}
mint &operator--(int) {
*this -= 1;
return *this;
}
};
mint operator+(const mint &x, const mint &y) { return mint(x.a) += y; }
mint operator+(const mint &x, const ll &y) { return mint(x.a) += y; }
mint operator+(const ll &x, mint &y) { return mint(x) += y; }
mint operator-(const mint &x, const mint &y) { return mint(x.a) -= y; }
mint operator-(const mint &x, const ll &y) { return mint(x.a) -= y; }
mint operator-(const ll &x, const mint &y) { return mint(x) -= y; }
mint operator*(const mint &x, const mint &y) { return mint(x.a) *= y; }
mint operator*(const mint &x, const ll &y) { return mint(x.a) *= y; }
mint operator*(const ll &x, const mint &y) { return mint(x) *= y; }
mint operator/(const mint &x, const mint &y) { return mint(x.a) /= y; }
mint operator/(const mint &x, const ll &y) { return mint(x.a) /= y; }
mint operator/(const ll &x, const mint &y) { return mint(x) /= y; }
bool operator==(const mint &x, const mint &y) { return x.a == y.a; }
bool operator==(const mint &x, const ll &y) { return x.a == y; }
bool operator==(const ll &x, mint &y) { return x == y.a; }
bool operator!=(const mint &x, const mint &y) { return !(x == y); }
bool operator!=(const mint &x, const ll &y) { return !(x == y); }
bool operator!=(const ll &x, mint &y) { return !(x == y); }
istream &operator>>(istream &is, mint &i) {
is >> i.a;
return is;
}
ostream &operator<<(ostream &os, const mint &i) {
os << i.a;
return os;
}
typedef vector<mint> vm;
#pragma endregion mint
int main() {
IN(ll, N);
IN(ll, S);
AIN(ll, A, N);
vector<vm> dp(N + 1, vm(S + 1));
REP(i, N) {
for (ll j = S; j >= 0; --j) {
dp[i + 1][j] = dp[i][j] + dp[i][max(0LL, j - A[i])];
}
if (A[i] <= S) {
dp[i + 1][A[i]] += i + 1;
}
}
mint ans;
REP1(i, N) { ans += dp[i][S]; }
OUT(ans);
} | replace | 184 | 185 | 184 | 187 | 0 | |
p02734 | C++ | Runtime Error | // #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define FastRead \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define int long long int
#define ll int
#define bits_count __builtin_popcountll
#define endl '\n'
#define double long double
#define ld double
#define FOR(i, a, n) for (ll i = (a); i <= (n); ++i)
#define RFOR(i, a, n) for (ll i = (n); i >= (a); --i)
#define FI(i, n) for (ll i = 0; i < (n); ++i)
#define ZERO(a) memset((a), 0, sizeof((a)))
#define MINUS(a) memset((a), -1, sizeof((a)))
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define all(g) g.begin(), g.end()
#define sz(x) (ll) x.size()
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including
// tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll,
// null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set;
const int MAXN = 3005;
int n, S;
int a[MAXN];
int dp[MAXN][MAXN][2];
const int MOD = 998244353;
int rec(int idx, int sum, int flag) {
if (idx == n + 1)
return 0;
int &ans = dp[idx][sum][flag];
if (ans != -1)
return ans;
ans = 0;
ans = (ans + rec(idx + 1, sum, flag));
if (flag == 0) {
int n_S = sum + a[idx];
if (n_S < S)
ans = (ans + (idx * rec(idx + 1, n_S, 1) % MOD) % MOD) % MOD;
if (n_S == S)
ans = (ans + (idx * (n - idx + 1)) % MOD) % MOD;
} else {
int n_S = sum + a[idx];
if (n_S < S)
ans = (ans + rec(idx + 1, n_S, flag) % MOD) % MOD;
if (n_S == S)
ans = (ans + (n - idx + 1) % MOD) % MOD;
}
return ans;
}
void solve() {
cin >> n >> S;
FOR(i, 1, n) cin >> a[i];
MINUS(dp);
cout << rec(1, 0, 0) << endl;
}
signed main() {
FastRead;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
// cin>>t;
FOR(i, 1, t) {
// cout<<"Case #"<<i<<": ";
solve();
}
} | // #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define FastRead \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define int long long int
#define ll int
#define bits_count __builtin_popcountll
#define endl '\n'
#define double long double
#define ld double
#define FOR(i, a, n) for (ll i = (a); i <= (n); ++i)
#define RFOR(i, a, n) for (ll i = (n); i >= (a); --i)
#define FI(i, n) for (ll i = 0; i < (n); ++i)
#define ZERO(a) memset((a), 0, sizeof((a)))
#define MINUS(a) memset((a), -1, sizeof((a)))
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define all(g) g.begin(), g.end()
#define sz(x) (ll) x.size()
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including
// tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll,
// null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set;
const int MAXN = 3005;
int n, S;
int a[MAXN];
int dp[MAXN][MAXN][2];
const int MOD = 998244353;
int rec(int idx, int sum, int flag) {
if (idx == n + 1)
return 0;
int &ans = dp[idx][sum][flag];
if (ans != -1)
return ans;
ans = 0;
ans = (ans + rec(idx + 1, sum, flag));
if (flag == 0) {
int n_S = sum + a[idx];
if (n_S < S)
ans = (ans + (idx * rec(idx + 1, n_S, 1) % MOD) % MOD) % MOD;
if (n_S == S)
ans = (ans + (idx * (n - idx + 1)) % MOD) % MOD;
} else {
int n_S = sum + a[idx];
if (n_S < S)
ans = (ans + rec(idx + 1, n_S, flag) % MOD) % MOD;
if (n_S == S)
ans = (ans + (n - idx + 1) % MOD) % MOD;
}
return ans;
}
void solve() {
cin >> n >> S;
FOR(i, 1, n) cin >> a[i];
MINUS(dp);
cout << rec(1, 0, 0) << endl;
}
signed main() {
FastRead;
int t = 1;
// cin>>t;
FOR(i, 1, t) {
// cout<<"Case #"<<i<<": ";
solve();
}
} | delete | 85 | 89 | 85 | 85 | -11 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define pb push_back
int n, s;
int const N = 1e3 + 10, M = 998244353;
int a[N];
int dp[N][2 * N][3];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = 0;
dp[n + 1][s][2] = 1;
for (int i = n; i >= 1; i--) {
for (int sum = s; sum >= 0; sum--) {
for (int t = 0; t <= 2; t++) {
if (t == 0) {
dp[i][sum][0] = (dp[i + 1][sum][0] + dp[i + 1][sum + a[i]][1] +
dp[i + 1][sum][1]) %
M;
} else if (t == 1) {
dp[i][sum][1] = (dp[i + 1][sum][1] + dp[i + 1][sum + a[i]][1] +
dp[i + 1][sum + a[i]][2] + dp[i + 1][sum][2]) %
M;
} else {
dp[i][sum][2] = dp[i + 1][sum][2] % M;
}
}
}
(ans += dp[i][0][1]) %= M;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define pb push_back
int n, s;
int const N = 3e3 + 10, M = 998244353;
int a[N];
int dp[N][2 * N][3];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = 0;
dp[n + 1][s][2] = 1;
for (int i = n; i >= 1; i--) {
for (int sum = s; sum >= 0; sum--) {
for (int t = 0; t <= 2; t++) {
if (t == 0) {
dp[i][sum][0] = (dp[i + 1][sum][0] + dp[i + 1][sum + a[i]][1] +
dp[i + 1][sum][1]) %
M;
} else if (t == 1) {
dp[i][sum][1] = (dp[i + 1][sum][1] + dp[i + 1][sum + a[i]][1] +
dp[i + 1][sum + a[i]][2] + dp[i + 1][sum][2]) %
M;
} else {
dp[i][sum][2] = dp[i + 1][sum][2] % M;
}
}
}
(ans += dp[i][0][1]) %= M;
}
cout << ans << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02734 | C++ | Runtime Error | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,avx2,mmx,abm")
#pragma GCC optimize("O3,unroll-loops,inline")
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int MAXN = 3010;
int n, s;
int a[MAXN];
int dp[MAXN];
signed main() {
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int ans = 0;
for (int i = 0; i <= s; i++)
dp[i] = 0;
for (int i = 1; i <= n; i++) {
for (int w = s - a[i]; w >= 1; w--) {
dp[w + a[i]] += dp[w];
dp[w + a[i]] -= MOD * (dp[w + a[i]] >= MOD);
}
dp[a[i]] += i;
dp[a[i]] -= MOD * (dp[a[i]] >= MOD);
ans += dp[s];
ans -= MOD * (ans >= MOD);
}
printf("%d\n", ans);
return 0;
} | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,avx2,mmx,abm")
#pragma GCC optimize("unroll-loops,inline")
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int MAXN = 3010;
int n, s;
int a[MAXN];
int dp[MAXN];
signed main() {
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int ans = 0;
for (int i = 0; i <= s; i++)
dp[i] = 0;
for (int i = 1; i <= n; i++) {
for (int w = s - a[i]; w >= 1; w--) {
dp[w + a[i]] += dp[w];
dp[w + a[i]] -= MOD * (dp[w + a[i]] >= MOD);
}
dp[a[i]] += i;
dp[a[i]] -= MOD * (dp[a[i]] >= MOD);
ans += dp[s];
ans -= MOD * (ans >= MOD);
}
printf("%d\n", ans);
return 0;
} | replace | 1 | 2 | 1 | 2 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, S, dp[3030][3030], a[3030];
const ll Mod = 998244353;
void Add_Self(ll &x, ll y) { x = (x + y) % Mod; }
ll Rec(int i, int Have) {
if (i == n)
return (Have == S ? 1 : 0);
if (Have == S)
return n - (i - 1);
if (dp[i][Have] != -1)
return dp[i][Have];
ll Res = 0;
Add_Self(Res, Rec(i + 1, Have + a[i]));
Add_Self(Res, Rec(i + 1, Have));
return dp[i][Have] = Res;
}
int main() {
memset(dp, -1, sizeof(dp));
cin >> n >> S;
for (int i = 0; i < n; i++)
cin >> a[i];
ll Ans = 0;
for (int i = 0; i < n; i++)
Add_Self(Ans, Rec(i, 0));
cout << Ans;
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, S, dp[3030][3030], a[3030];
const ll Mod = 998244353;
void Add_Self(ll &x, ll y) { x = (x + y) % Mod; }
ll Rec(int i, int Have) {
if (i == n)
return (Have == S ? 1 : 0);
if (Have == S)
return n - (i - 1);
if (dp[i][Have] != -1)
return dp[i][Have];
ll Res = 0;
Add_Self(Res, Rec(i + 1, min((ll)3001, Have + a[i])));
Add_Self(Res, Rec(i + 1, Have));
return dp[i][Have] = Res;
}
int main() {
memset(dp, -1, sizeof(dp));
cin >> n >> S;
for (int i = 0; i < n; i++)
cin >> a[i];
ll Ans = 0;
for (int i = 0; i < n; i++)
Add_Self(Ans, Rec(i, 0));
cout << Ans;
return 0;
}
| replace | 20 | 21 | 20 | 21 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
const int MAX = 3005;
ll dp[MAX][MAX];
void solve() {
mod = 998244353;
ll n, s;
cin >> n >> s;
ll ans = 0;
ll a[n + 1];
dp[0][0] = 1;
for (ll i = 1; i <= n; i++) {
cin >> a[i];
memcpy(dp[i], dp[i - 1], sizeof dp[0]);
(dp[i][a[i]] += i) %= mod;
for (ll j = 1; j + a[i] <= s; j++) {
(dp[i][j + a[i]] += dp[i - 1][j]) %= mod;
}
ans += dp[i][s];
ans %= mod;
}
cout << ans << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input00.txt", "r", stdin);
#endif
IOS;
ll T = 1;
// cin>>T;
while (T--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
const int MAX = 3005;
ll dp[MAX][MAX];
void solve() {
mod = 998244353;
ll n, s;
cin >> n >> s;
ll ans = 0;
ll a[n + 1];
dp[0][0] = 1;
for (ll i = 1; i <= n; i++) {
cin >> a[i];
memcpy(dp[i], dp[i - 1], sizeof dp[0]);
(dp[i][a[i]] += i) %= mod;
for (ll j = 1; j + a[i] <= s; j++) {
(dp[i][j + a[i]] += dp[i - 1][j]) %= mod;
}
ans += dp[i][s];
ans %= mod;
}
cout << ans << endl;
}
int main() {
IOS;
ll T = 1;
// cin>>T;
while (T--) {
solve();
}
return 0;
} | replace | 43 | 46 | 43 | 44 | -11 | |
p02734 | C++ | Runtime Error | #include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
/* run this program using the console pauser or add your own getch,
* system("pause") or input loop */
#define ll long long
#define pb push_back
int a[3005];
ll dp[3005];
const ll mod = 998244353;
ll ad(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll ap(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
int main(int argc, char **argv) {
int n, s;
cin >> n >> s;
rep(i, 1, n) { scanf("%d", &a[i]); }
ll ans = 0;
rep(i, 1, n) {
if (s == a[i])
ans = ad(ans, ap(i, n - i + 1));
if (dp[s - a[i]])
ans = ad(ans, ap((dp[s - a[i]]), (n - i + 1)));
// cout<<s-a[i]<<" "<<dp[s-a[i]]<<" "<<i<<endl;
rep2(j, s, 1) {
if (j + a[i] > s)
continue;
dp[j + a[i]] = ad(dp[j + a[i]], dp[j]);
}
dp[a[i]] = ad(dp[a[i]], i);
}
cout << ans;
return 0;
} | #include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
/* run this program using the console pauser or add your own getch,
* system("pause") or input loop */
#define ll long long
#define pb push_back
int a[3005];
ll dp[3005];
const ll mod = 998244353;
ll ad(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll ap(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
int main(int argc, char **argv) {
int n, s;
cin >> n >> s;
rep(i, 1, n) { scanf("%d", &a[i]); }
ll ans = 0;
rep(i, 1, n) {
if (s < a[i])
continue;
if (s == a[i])
ans = ad(ans, ap(i, n - i + 1));
if (dp[s - a[i]])
ans = ad(ans, ap((dp[s - a[i]]), (n - i + 1)));
// cout<<s-a[i]<<" "<<dp[s-a[i]]<<" "<<i<<endl;
rep2(j, s, 1) {
if (j + a[i] > s)
continue;
dp[j + a[i]] = ad(dp[j + a[i]], dp[j]);
}
dp[a[i]] = ad(dp[a[i]], i);
}
cout << ans;
return 0;
} | insert | 22 | 22 | 22 | 24 | 0 | |
p02734 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
os << '(' << a.first << ',' << a.second << ')';
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
for (auto x : v)
os << x << ' ';
return os;
}
void debug() { cerr << '\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
cerr << a;
if (sizeof...(b))
cerr << ", ";
debug(b...);
}
// 負の数を掛けたりするとバグる
template <int MOD> class Modint {
public:
int a;
Modint(const long long v = 0) : a(v % MOD) {}
static constexpr int getmod() { return MOD; }
Modint operator+(const Modint rhs) const { return Modint(*this) += rhs; }
Modint operator-(const Modint rhs) const { return Modint(*this) -= rhs; }
Modint operator*(const Modint rhs) const { return Modint(*this) *= rhs; }
Modint operator/(const Modint rhs) const { return Modint(*this) /= rhs; }
Modint operator+(const long long rhs) const { return Modint(*this) += rhs; }
Modint operator-(const long long rhs) const { return Modint(*this) -= rhs; }
Modint operator*(const long long rhs) const { return Modint(*this) *= rhs; }
Modint operator/(const long long rhs) const { return Modint(*this) /= rhs; }
friend Modint operator+(const long long a, const Modint b) { return b + a; }
friend Modint operator-(const long long a, const Modint b) { return -b + a; }
friend Modint operator*(const long long a, const Modint b) { return b * a; }
friend Modint operator/(const long long a, const Modint b) {
return Modint(a) / b;
}
Modint &operator+=(const Modint rhs) {
a += rhs.a;
if (a >= MOD) {
a -= MOD;
}
return *this;
}
Modint &operator-=(const Modint rhs) {
if (a < rhs.a) {
a += MOD;
}
a -= rhs.a;
return *this;
}
Modint &operator*=(const Modint rhs) {
a = (long long)a * rhs.a % MOD;
return *this;
}
Modint &operator/=(Modint rhs) {
int x = MOD - 2;
while (x) {
if (x % 2) {
*this *= rhs;
}
rhs *= rhs;
x /= 2;
}
return *this;
}
Modint &operator++() {
*this += 1;
return *this;
}
Modint &operator--() {
*this -= 1;
return *this;
}
Modint operator++(int) {
Modint res = *this;
++(*this);
return res;
}
Modint operator--(int) {
Modint res = *this;
--(*this);
return res;
}
Modint &operator+=(const long long rhs) {
*this += Modint(rhs);
return *this;
}
Modint &operator-=(const long long rhs) {
*this -= Modint(rhs);
return *this;
}
Modint &operator*=(const long long rhs) {
*this *= Modint(rhs);
return *this;
}
Modint &operator/=(const long long rhs) {
*this /= Modint(rhs);
return *this;
}
Modint operator+() const { return *this; }
Modint operator-() const { return Modint() - *this; }
bool operator==(const Modint rhs) const { return a == rhs.a; }
bool operator==(const long long rhs) const { return a == rhs; }
friend bool operator==(const long long a, const Modint b) { return a == b.a; }
bool operator!=(const Modint rhs) const { return a != rhs.a; }
bool operator!=(const long long rhs) const { return a != rhs; }
friend ostream &operator<<(ostream &os, const Modint x) {
os << x.a;
return os;
}
friend istream &operator>>(istream &is, Modint &x) {
is >> x.a;
return is;
}
explicit operator bool() const { return a > 0; }
bool operator!() { return a == 0; }
explicit operator int() const { return a; }
explicit operator long long() const { return (long long)a; }
friend Modint pow(Modint a, long long b) {
Modint res = 1;
while (b) {
if (b % 2) {
res *= a;
}
a *= a;
b /= 2;
}
return res;
}
};
using mint = Modint<998244353>;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, s;
cin >> n >> s;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<vector<mint>> dp(n + 2, vector<mint>(s + 5, mint(0)));
dp[0][0] = 1;
vector<mint> cnt(n * 3000 + 1);
mint ans = 0;
for (int i = 0; i < n; i++) {
dp[i + 1][0] = 1;
dp[i + 1][a[i]] = i + 1;
for (int j = 1; j <= s; j++) {
if (dp[i][j]) {
dp[i + 1][j] += dp[i][j];
if (j + a[i] <= s)
dp[i + 1][j + a[i]] += dp[i][j];
}
}
ans += dp[i + 1][s];
}
cout << ans << endl;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
os << '(' << a.first << ',' << a.second << ')';
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
for (auto x : v)
os << x << ' ';
return os;
}
void debug() { cerr << '\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
cerr << a;
if (sizeof...(b))
cerr << ", ";
debug(b...);
}
// 負の数を掛けたりするとバグる
template <int MOD> class Modint {
public:
int a;
Modint(const long long v = 0) : a(v % MOD) {}
static constexpr int getmod() { return MOD; }
Modint operator+(const Modint rhs) const { return Modint(*this) += rhs; }
Modint operator-(const Modint rhs) const { return Modint(*this) -= rhs; }
Modint operator*(const Modint rhs) const { return Modint(*this) *= rhs; }
Modint operator/(const Modint rhs) const { return Modint(*this) /= rhs; }
Modint operator+(const long long rhs) const { return Modint(*this) += rhs; }
Modint operator-(const long long rhs) const { return Modint(*this) -= rhs; }
Modint operator*(const long long rhs) const { return Modint(*this) *= rhs; }
Modint operator/(const long long rhs) const { return Modint(*this) /= rhs; }
friend Modint operator+(const long long a, const Modint b) { return b + a; }
friend Modint operator-(const long long a, const Modint b) { return -b + a; }
friend Modint operator*(const long long a, const Modint b) { return b * a; }
friend Modint operator/(const long long a, const Modint b) {
return Modint(a) / b;
}
Modint &operator+=(const Modint rhs) {
a += rhs.a;
if (a >= MOD) {
a -= MOD;
}
return *this;
}
Modint &operator-=(const Modint rhs) {
if (a < rhs.a) {
a += MOD;
}
a -= rhs.a;
return *this;
}
Modint &operator*=(const Modint rhs) {
a = (long long)a * rhs.a % MOD;
return *this;
}
Modint &operator/=(Modint rhs) {
int x = MOD - 2;
while (x) {
if (x % 2) {
*this *= rhs;
}
rhs *= rhs;
x /= 2;
}
return *this;
}
Modint &operator++() {
*this += 1;
return *this;
}
Modint &operator--() {
*this -= 1;
return *this;
}
Modint operator++(int) {
Modint res = *this;
++(*this);
return res;
}
Modint operator--(int) {
Modint res = *this;
--(*this);
return res;
}
Modint &operator+=(const long long rhs) {
*this += Modint(rhs);
return *this;
}
Modint &operator-=(const long long rhs) {
*this -= Modint(rhs);
return *this;
}
Modint &operator*=(const long long rhs) {
*this *= Modint(rhs);
return *this;
}
Modint &operator/=(const long long rhs) {
*this /= Modint(rhs);
return *this;
}
Modint operator+() const { return *this; }
Modint operator-() const { return Modint() - *this; }
bool operator==(const Modint rhs) const { return a == rhs.a; }
bool operator==(const long long rhs) const { return a == rhs; }
friend bool operator==(const long long a, const Modint b) { return a == b.a; }
bool operator!=(const Modint rhs) const { return a != rhs.a; }
bool operator!=(const long long rhs) const { return a != rhs; }
friend ostream &operator<<(ostream &os, const Modint x) {
os << x.a;
return os;
}
friend istream &operator>>(istream &is, Modint &x) {
is >> x.a;
return is;
}
explicit operator bool() const { return a > 0; }
bool operator!() { return a == 0; }
explicit operator int() const { return a; }
explicit operator long long() const { return (long long)a; }
friend Modint pow(Modint a, long long b) {
Modint res = 1;
while (b) {
if (b % 2) {
res *= a;
}
a *= a;
b /= 2;
}
return res;
}
};
using mint = Modint<998244353>;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, s;
cin >> n >> s;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<vector<mint>> dp(n + 2, vector<mint>(3000 + 5, mint(0)));
dp[0][0] = 1;
vector<mint> cnt(n * 3000 + 1);
mint ans = 0;
for (int i = 0; i < n; i++) {
dp[i + 1][0] = 1;
dp[i + 1][a[i]] = i + 1;
for (int j = 1; j <= s; j++) {
if (dp[i][j]) {
dp[i + 1][j] += dp[i][j];
if (j + a[i] <= s)
dp[i + 1][j + a[i]] += dp[i][j];
}
}
ans += dp[i + 1][s];
}
cout << ans << endl;
}
| replace | 172 | 173 | 172 | 173 | 0 | |
p02734 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
#define LL long long
int main() {
// freopen("a.txt", "r", stdin);
int mod = 998244353;
int N, S, res = 0;
cin >> N >> S;
vector<int> A(N + 1, 0);
for (int i = 1; i <= N; ++i)
cin >> A[i];
vector<int> dp(S + 1, 0);
for (int i = 1; i <= N; ++i) {
for (int j = S; j >= A[i]; --j)
dp[j] = (dp[j] + dp[j - A[i]]) % mod;
dp[A[i]] = (dp[A[i]] + i) % mod;
res = (res + dp[S] * (N - i + 1)) % mod;
dp[S] = 0;
}
cout << res << endl;
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
#define LL long long
int main() {
// freopen("a.txt", "r", stdin);
int mod = 998244353;
int N, S, res = 0;
cin >> N >> S;
vector<int> A(N + 1, 0);
for (int i = 1; i <= N; ++i)
cin >> A[i];
vector<int> dp(S + 1, 0);
for (int i = 1; i <= N; ++i) {
for (int j = S; j >= A[i]; --j)
dp[j] = (dp[j] + dp[j - A[i]]) % mod;
if (S >= A[i])
dp[A[i]] = (dp[A[i]] + i) % mod;
res = (res + 1ll * dp[S] * (N - i + 1)) % mod;
dp[S] = 0;
}
cout << res << endl;
return 0;
}
| replace | 21 | 23 | 21 | 24 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 998244353;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
int main() {
int n, s;
cin >> n >> s;
vector<LL> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
LL ans = 0;
vector<LL> dp(s + 1, 0);
for (LL i = 0; i < n; i++) {
for (int j = s; j >= 0; j--) {
if (j + a[i] <= s)
dp[j + a[i]] = (dp[j + a[i]] + dp[j]) % mod;
}
dp[a[i]] = (dp[a[i]] + i + 1) % mod;
ans = (ans + dp[s]) % mod;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 998244353;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
int main() {
int n, s;
cin >> n >> s;
vector<LL> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
LL ans = 0;
vector<LL> dp(5000, 0);
for (LL i = 0; i < n; i++) {
for (int j = s; j >= 0; j--) {
if (j + a[i] <= s)
dp[j + a[i]] = (dp[j + a[i]] + dp[j]) % mod;
}
dp[a[i]] = (dp[a[i]] + i + 1) % mod;
ans = (ans + dp[s]) % mod;
}
cout << ans << endl;
return 0;
}
| replace | 40 | 41 | 40 | 41 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
#define pb push_back
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define db(x) (cerr << #x << ": " << (x) << '\n')
#define cps CLOCKS_PER_SEC
#define tests(t) \
int t; \
cin >> t; \
while (t--)
#define iceil(n, x) (((n) + (x)-1) / (x))
#define ll long long
#define ld double
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define fi first
#define se second
#define bitcount(x) __builtin_popcount(x)
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 998244353
#define bit(x) (1 << x)
#define par pair<int, int>
#define maxn 1005
using namespace std;
/// All indexing is 0-based
using namespace __gnu_pbds;
template <class key, class cmp = std::less<key>>
using ordered_set =
tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// methods: find_by_order(k); & order_of_key(k);
/// To make it an ordered_multiset, use pairs of (value, time_of_insertion)
/// to distinguish values which are similar
ll a, Dp[maxn], sol, N, S;
int main() {
#ifdef acm
freopen("a.in", "r", stdin);
#endif // acm
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> S;
for (int i = 1; i <= N; i++) {
cin >> a;
for (int j = S; j >= a; j--)
Dp[j] = (Dp[j] + Dp[j - a]) % mod;
Dp[a] = (Dp[a] + i) % mod;
sol = (sol + Dp[S]) % mod;
}
cout << sol;
}
| #include <bits/stdc++.h>
#define pb push_back
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define db(x) (cerr << #x << ": " << (x) << '\n')
#define cps CLOCKS_PER_SEC
#define tests(t) \
int t; \
cin >> t; \
while (t--)
#define iceil(n, x) (((n) + (x)-1) / (x))
#define ll long long
#define ld double
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define fi first
#define se second
#define bitcount(x) __builtin_popcount(x)
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 998244353
#define bit(x) (1 << x)
#define par pair<int, int>
#define maxn 4005
using namespace std;
/// All indexing is 0-based
using namespace __gnu_pbds;
template <class key, class cmp = std::less<key>>
using ordered_set =
tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// methods: find_by_order(k); & order_of_key(k);
/// To make it an ordered_multiset, use pairs of (value, time_of_insertion)
/// to distinguish values which are similar
ll a, Dp[maxn], sol, N, S;
int main() {
#ifdef acm
freopen("a.in", "r", stdin);
#endif // acm
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> S;
for (int i = 1; i <= N; i++) {
cin >> a;
for (int j = S; j >= a; j--)
Dp[j] = (Dp[j] + Dp[j - a]) % mod;
Dp[a] = (Dp[a] + i) % mod;
sol = (sol + Dp[S]) % mod;
}
cout << sol;
}
| replace | 29 | 30 | 29 | 30 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> void chmin(T &a, const T &b) noexcept {
if (b < a)
a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
if (a < b)
a = b;
}
void debug_out() { cout << "\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
cout << x << " ";
debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
// const int mod = 1000000007;
const int mod = 998244353;
struct mint {
ll x;
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; }
bool operator==(const mint rhs) const { return x == rhs.x; }
bool operator!=(const mint rhs) const { return x != rhs.x; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
int n, s;
cin >> n >> s;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<mint> F(s + 5, 0);
mint ans = 0;
rep(i, n) {
for (int j = s; j - a[i] >= 0; --j) {
F[j] += F[j - a[i]];
}
F[0] += 1;
F[a[i]] += 1;
ans += F[s];
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> void chmin(T &a, const T &b) noexcept {
if (b < a)
a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
if (a < b)
a = b;
}
void debug_out() { cout << "\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
cout << x << " ";
debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
// const int mod = 1000000007;
const int mod = 998244353;
struct mint {
ll x;
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; }
bool operator==(const mint rhs) const { return x == rhs.x; }
bool operator!=(const mint rhs) const { return x != rhs.x; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
int n, s;
cin >> n >> s;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<mint> F(s + 5, 0);
mint ans = 0;
rep(i, n) {
for (int j = s; j - a[i] >= 0; --j) {
F[j] += F[j - a[i]];
}
F[0] += 1;
if (a[i] <= s)
F[a[i]] += 1;
ans += F[s];
}
cout << ans << endl;
return 0;
} | replace | 83 | 84 | 83 | 85 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
using namespace std;
const int N = 3003, mod = 998244353;
int a[N], dp[2][N], n, s, ans;
int M(int x) {
int ret = x % mod;
ret += mod;
ret %= mod;
return ret;
}
signed main() {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
dp[i % 2][a[i]] = i;
for (int j = 1; j <= s; j++) {
if (j != a[i])
dp[i % 2][j] = 0;
dp[i % 2][j] += M(dp[1 - (i % 2)][j] + dp[1 - (i % 2)][j - a[i]]);
dp[i % 2][j] = M(dp[i % 2][j]);
// cout << dp[i%2][j] << " ";
}
// cout << endl;
ans += M((dp[i % 2][s] - dp[1 - (i % 2)][s]) * (n - i + 1));
ans = M(ans);
}
cout << ans;
} | #include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
using namespace std;
const int N = 3003, mod = 998244353;
int a[N], dp[2][N], n, s, ans;
int M(int x) {
int ret = x % mod;
ret += mod;
ret %= mod;
return ret;
}
signed main() {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
dp[i % 2][a[i]] = i;
for (int j = 1; j <= s; j++) {
if (j != a[i])
dp[i % 2][j] = 0;
dp[i % 2][j] += dp[1 - (i % 2)][j];
dp[i % 2][j] = M(dp[i % 2][j]);
if (j >= a[i])
dp[i % 2][j] += dp[1 - (i % 2)][j - a[i]];
dp[i % 2][j] = M(dp[i % 2][j]);
// cout << dp[i%2][j] << " ";
}
// cout << endl;
ans += M((dp[i % 2][s] - dp[1 - (i % 2)][s]) * (n - i + 1));
ans = M(ans);
}
cout << ans;
} | replace | 25 | 26 | 25 | 29 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
void solve(int TestCase) {
constexpr int MOD = 998244353;
int n, s;
cin >> n >> s;
auto A = vector<int>(n);
for (auto &x : A)
cin >> x;
auto dp = vector<vector<long long>>(n, vector<long long>(s + 1));
auto sum = vector<long long>(s + 1);
for (auto i = 0; i < n; ++i) {
dp[i][A[i]] = i + 1;
for (auto j = A[i] + 1; i && j < s + 1; ++j) {
dp[i][j] = sum[j - A[i]];
}
for (auto j = 0; j < s + 1; ++j) {
sum[j] = (sum[j] + dp[i][j]) % MOD;
}
}
long long ret = 0;
for (auto i = 0; i < n; ++i) {
ret = (ret + dp[i][s] * (n - i) % MOD) % MOD;
}
cout << ret << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
for (auto i = 1; i <= t; ++i) {
// cout << "Case #"<< i << ": ";
solve(i);
}
} | #include <bits/stdc++.h>
using namespace std;
void solve(int TestCase) {
constexpr int MOD = 998244353;
int n, s;
cin >> n >> s;
auto A = vector<int>(n);
for (auto &x : A)
cin >> x;
auto dp = vector<vector<long long>>(n, vector<long long>(3001));
auto sum = vector<long long>(3001);
for (auto i = 0; i < n; ++i) {
dp[i][A[i]] = i + 1;
for (auto j = A[i] + 1; i && j < s + 1; ++j) {
dp[i][j] = sum[j - A[i]];
}
for (auto j = 0; j < s + 1; ++j) {
sum[j] = (sum[j] + dp[i][j]) % MOD;
}
}
long long ret = 0;
for (auto i = 0; i < n; ++i) {
ret = (ret + dp[i][s] * (n - i) % MOD) % MOD;
}
cout << ret << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
for (auto i = 1; i <= t; ++i) {
// cout << "Case #"<< i << ": ";
solve(i);
}
} | replace | 12 | 14 | 12 | 14 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <int Mod> class ModInt {
int v;
public:
constexpr ModInt(const long long x = 0) noexcept : v((x % Mod + Mod) % Mod){};
constexpr ModInt &operator+=(const ModInt rhs) noexcept {
v += rhs.v;
if (v >= Mod)
v -= Mod;
return *this;
}
constexpr ModInt &operator-=(const ModInt rhs) noexcept {
if (v < rhs.v)
v += Mod;
v -= rhs.v;
return *this;
}
constexpr ModInt &operator*=(const ModInt rhs) noexcept {
v = int(1LL * v * rhs.v % Mod);
return *this;
}
constexpr ModInt &operator/=(ModInt rhs) noexcept {
long long exp = Mod - 2;
while (exp) {
if (exp & 1)
*this *= rhs;
rhs *= rhs;
exp >>= 1;
}
return *this;
}
constexpr ModInt operator-() const noexcept { return ModInt(-v); }
constexpr ModInt operator+(const ModInt rhs) const noexcept {
return ModInt(*this) += rhs;
}
constexpr ModInt operator-(const ModInt rhs) const noexcept {
return ModInt(*this) -= rhs;
}
constexpr ModInt operator*(const ModInt rhs) const noexcept {
return ModInt(*this) *= rhs;
}
constexpr ModInt operator/(const ModInt rhs) const noexcept {
return ModInt(*this) /= rhs;
}
constexpr bool operator==(const ModInt rhs) const noexcept {
return v == rhs.v;
}
constexpr bool operator!=(const ModInt rhs) const noexcept {
return v != rhs.v;
}
constexpr ModInt pow(long long exp) const noexcept {
ModInt res(1), mul(v);
while (exp) {
if (exp & 1)
res *= mul;
mul *= mul;
exp >>= 1;
}
return res;
}
friend ostream &operator<<(ostream &os, const ModInt obj) noexcept {
return os << obj.v;
}
friend istream &operator>>(istream &is, ModInt &obj) noexcept {
long long tmp;
is >> tmp;
obj = ModInt<Mod>(tmp);
return is;
}
};
constexpr int MOD = 998244353;
using mint = ModInt<MOD>;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, s;
cin >> n >> s;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<vector<mint>> dp(n + 1, vector<mint>(s + 1));
mint ans = 0;
for (int i = n - 1; i >= 0; i--) {
if (s - a[i] > 0)
ans += dp[i + 1][s - a[i]] * (i + 1);
else if (s - a[i] == 0)
ans += (i + 1) * (n - i);
rep(j, s) dp[i][j] = dp[i + 1][j];
for (int j = s - 1; j > 0; j--) {
if (j + a[i] <= s)
dp[i][j + a[i]] += dp[i + 1][j];
}
dp[i][a[i]] += n - i;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <int Mod> class ModInt {
int v;
public:
constexpr ModInt(const long long x = 0) noexcept : v((x % Mod + Mod) % Mod){};
constexpr ModInt &operator+=(const ModInt rhs) noexcept {
v += rhs.v;
if (v >= Mod)
v -= Mod;
return *this;
}
constexpr ModInt &operator-=(const ModInt rhs) noexcept {
if (v < rhs.v)
v += Mod;
v -= rhs.v;
return *this;
}
constexpr ModInt &operator*=(const ModInt rhs) noexcept {
v = int(1LL * v * rhs.v % Mod);
return *this;
}
constexpr ModInt &operator/=(ModInt rhs) noexcept {
long long exp = Mod - 2;
while (exp) {
if (exp & 1)
*this *= rhs;
rhs *= rhs;
exp >>= 1;
}
return *this;
}
constexpr ModInt operator-() const noexcept { return ModInt(-v); }
constexpr ModInt operator+(const ModInt rhs) const noexcept {
return ModInt(*this) += rhs;
}
constexpr ModInt operator-(const ModInt rhs) const noexcept {
return ModInt(*this) -= rhs;
}
constexpr ModInt operator*(const ModInt rhs) const noexcept {
return ModInt(*this) *= rhs;
}
constexpr ModInt operator/(const ModInt rhs) const noexcept {
return ModInt(*this) /= rhs;
}
constexpr bool operator==(const ModInt rhs) const noexcept {
return v == rhs.v;
}
constexpr bool operator!=(const ModInt rhs) const noexcept {
return v != rhs.v;
}
constexpr ModInt pow(long long exp) const noexcept {
ModInt res(1), mul(v);
while (exp) {
if (exp & 1)
res *= mul;
mul *= mul;
exp >>= 1;
}
return res;
}
friend ostream &operator<<(ostream &os, const ModInt obj) noexcept {
return os << obj.v;
}
friend istream &operator>>(istream &is, ModInt &obj) noexcept {
long long tmp;
is >> tmp;
obj = ModInt<Mod>(tmp);
return is;
}
};
constexpr int MOD = 998244353;
using mint = ModInt<MOD>;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, s;
cin >> n >> s;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<vector<mint>> dp(n + 1, vector<mint>(s + 1));
mint ans = 0;
for (int i = n - 1; i >= 0; i--) {
if (s - a[i] > 0)
ans += dp[i + 1][s - a[i]] * (i + 1);
else if (s - a[i] == 0)
ans += (i + 1) * (n - i);
rep(j, s) dp[i][j] = dp[i + 1][j];
for (int j = s - 1; j > 0; j--) {
if (j + a[i] <= s)
dp[i][j + a[i]] += dp[i + 1][j];
}
if (a[i] <= s)
dp[i][a[i]] += n - i;
}
cout << ans << endl;
return 0;
} | replace | 115 | 116 | 115 | 117 | 0 | |
p02734 | C++ | Runtime Error | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,avx2,mmx,abm")
#pragma GCC optimize("O3,unroll-loops,inline")
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int a, int b) {
a += b;
if (a >= MOD)
a -= MOD;
return a;
}
const int MAXN = 3010;
int n, s;
int a[MAXN];
int dp[MAXN];
signed main() {
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int ans = 0;
for (int i = 0; i <= s; i++)
dp[i] = 0;
for (int i = 1; i <= n; i++) {
for (int w = s - a[i]; w >= 1; w--) {
int nxt = w + a[i];
dp[nxt] = add(dp[nxt], dp[w]);
}
int nxt = a[i];
if (nxt <= s)
dp[nxt] = add(dp[nxt], i);
ans = add(ans, dp[s]);
}
printf("%d\n", ans);
return 0;
} | // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,avx2,mmx,abm")
// #pragma GCC optimize("O3,unroll-loops,inline")
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int a, int b) {
a += b;
if (a >= MOD)
a -= MOD;
return a;
}
const int MAXN = 3010;
int n, s;
int a[MAXN];
int dp[MAXN];
signed main() {
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int ans = 0;
for (int i = 0; i <= s; i++)
dp[i] = 0;
for (int i = 1; i <= n; i++) {
for (int w = s - a[i]; w >= 1; w--) {
int nxt = w + a[i];
dp[nxt] = add(dp[nxt], dp[w]);
}
int nxt = a[i];
if (nxt <= s)
dp[nxt] = add(dp[nxt], i);
ans = add(ans, dp[s]);
}
printf("%d\n", ans);
return 0;
} | replace | 0 | 2 | 0 | 2 | 0 | |
p02734 | Python | Runtime Error | import numpy as np
n, s = map(int, input().split())
aaa = list(map(int, input().split()))
fwd_acc = np.zeros((n + 1, s + 1), dtype=np.int64)
fwd_acc[0][0] = 1
ans = 0
MOD = 998244353
for i, a in enumerate(aaa, start=1):
fwd_acc[i] = fwd_acc[i - 1]
fwd_acc[i][0] = i
if a <= s:
fwd_acc[i][a:] += fwd_acc[i][:-a]
ans = (ans + fwd_acc[i - 1][s - a] * (n - i + 1)) % MOD
print(ans)
| import numpy as np
n, s = map(int, input().split())
aaa = list(map(int, input().split()))
fwd_acc = np.zeros((n + 1, s + 1), dtype=np.int64)
fwd_acc[0][0] = 1
ans = 0
MOD = 998244353
for i, a in enumerate(aaa, start=1):
fwd_acc[i] = fwd_acc[i - 1]
fwd_acc[i][0] = i
if a <= s:
fwd_acc[i][a:] = fwd_acc[i][a:] + fwd_acc[i][:-a]
fwd_acc[i] %= MOD
ans = (ans + fwd_acc[i][s]) % MOD
# print(fwd_acc)
# print(ans)
print(ans)
| replace | 14 | 16 | 14 | 19 | TLE | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define fr(i, j, k) for (i = j; i < (k); i++)
#define all(x) x.begin(), x.end()
#define el '\n'
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define sz(x) int32_t(x.size())
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpi;
// --------------------------------------------------------------------
const pii dxy[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
const int mod = 998244353;
const int inf = 2e18;
const ld eps = 1e-9;
const int NN = 1e5 + 2;
const int S = 1001;
int dp[S][S][3];
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
// #define TRACE
#ifdef TRACE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
void solve() {
int i = 0, j = 0, k = 0, n = 0, s = 0;
cin >> n >> s;
vi a(n);
fr(i, 0, n) { cin >> a[i]; }
debug(a);
fr(k, 0, 3) dp[0][a[0]][k] = 1;
debug(s, a[0], dp[0][s][2]);
for (i = 1; i < n; i++) {
// copy prev states
fr(j, 0, s + 1) {
fr(k, 0, 3) {
dp[i][j][k] = dp[i - 1][j][k];
if (k == 2)
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][1]) % mod;
}
}
// dp transitions
for (j = 0; j <= s - a[i]; j++) {
// Dim 0
dp[i][j + a[i]][0] += dp[i - 1][j][0];
dp[i][j + a[i]][0] %= mod;
// Dim 1
dp[i][j + a[i]][1] += dp[i - 1][j][1];
dp[i][j + a[i]][1] %= mod;
// Dim 2
dp[i][j + a[i]][2] += dp[i - 1][j][1];
dp[i][j + a[i]][2] %= mod;
}
dp[i][a[i]][0]++;
dp[i][a[i]][1] += i + 1;
dp[i][a[i]][2] += i + 1;
debug(i, dp[i][s][2]);
}
// cerr << dp[n-1][s][0] << ' ' << dp[n-1][s][1]<<endl;
cout << (dp[n - 1][s][2] % mod) << el;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define fr(i, j, k) for (i = j; i < (k); i++)
#define all(x) x.begin(), x.end()
#define el '\n'
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define sz(x) int32_t(x.size())
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpi;
// --------------------------------------------------------------------
const pii dxy[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
const int mod = 998244353;
const int inf = 2e18;
const ld eps = 1e-9;
const int NN = 1e5 + 2;
const int S = 3003;
int dp[S][S][3];
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
// #define TRACE
#ifdef TRACE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
void solve() {
int i = 0, j = 0, k = 0, n = 0, s = 0;
cin >> n >> s;
vi a(n);
fr(i, 0, n) { cin >> a[i]; }
debug(a);
fr(k, 0, 3) dp[0][a[0]][k] = 1;
debug(s, a[0], dp[0][s][2]);
for (i = 1; i < n; i++) {
// copy prev states
fr(j, 0, s + 1) {
fr(k, 0, 3) {
dp[i][j][k] = dp[i - 1][j][k];
if (k == 2)
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][1]) % mod;
}
}
// dp transitions
for (j = 0; j <= s - a[i]; j++) {
// Dim 0
dp[i][j + a[i]][0] += dp[i - 1][j][0];
dp[i][j + a[i]][0] %= mod;
// Dim 1
dp[i][j + a[i]][1] += dp[i - 1][j][1];
dp[i][j + a[i]][1] %= mod;
// Dim 2
dp[i][j + a[i]][2] += dp[i - 1][j][1];
dp[i][j + a[i]][2] %= mod;
}
dp[i][a[i]][0]++;
dp[i][a[i]][1] += i + 1;
dp[i][a[i]][2] += i + 1;
debug(i, dp[i][s][2]);
}
// cerr << dp[n-1][s][0] << ' ' << dp[n-1][s][1]<<endl;
cout << (dp[n - 1][s][2] % mod) << el;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
solve();
}
return 0;
}
| replace | 29 | 30 | 29 | 30 | 0 | |
p02734 | C++ | Runtime Error | /*
Author : N_o_o_B
Created : March 22 2020 18:30:12
*/
#include <bits/stdc++.h>
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
#define TRACE
#ifdef TRACE
#define trace(...) \
{ \
cerr << "[ "; \
__trace__(#__VA_ARGS__, __VA_ARGS__); \
}
#undef endl
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) {
return out << "(" << x.first << "," << x.second << ")";
}
template <typename Arg1>
ostream &operator<<(ostream &out, const vector<Arg1> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1> void __trace__(const string name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << " ] " << endl;
}
template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1 &&arg1, Args &&...args) {
const string name = names.substr(0, names.find(','));
cerr << name << " : " << arg1 << " | ";
__trace__(names.substr(1 + (int)name.size()), args...);
}
#else
#define trace(args...)
#endif
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<ll, ll>> vll;
typedef vector<vl> vvl;
// typedef tree<pii, null_type, less<pii>, rb_tree_tag,
// tree_order_statistics_node_update> oset;
#define fori(i, n) for (int i = 0; i < n; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define ford(i, n) for (int i = n - 1; i >= 0; i--)
#define trav(x, a) for (auto &x : a)
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define fi first
#define se second
clock_t time_p = clock();
void time_taken() {
time_p = clock() - time_p;
cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
const ll mod = 998244353;
// const ll mod=1e9+7;
const ll INF = 1e18;
// credits ---> ksun48
ll modinv(ll a, ll m) {
assert(m > 0);
if (m == 1)
return 0;
a %= m;
if (a < 0)
a += m;
assert(a != 0);
if (a == 1)
return 1;
return m - modinv(m, a) * m / a;
}
template <int MOD_> struct modnum {
private:
int v;
public:
static const int MOD = MOD_;
modnum() : v(0) {}
modnum(ll v_) : v(int(v_ % MOD)) {
if (v < 0)
v += MOD;
}
explicit operator int() const { return v; }
friend bool operator==(const modnum &a, const modnum &b) {
return a.v == b.v;
}
friend bool operator!=(const modnum &a, const modnum &b) {
return a.v != b.v;
}
modnum operator~() const {
modnum res;
res.v = modinv(v, MOD);
return res;
}
modnum &operator+=(const modnum &o) {
v += o.v;
if (v >= MOD)
v -= MOD;
return *this;
}
modnum &operator-=(const modnum &o) {
v -= o.v;
if (v < 0)
v += MOD;
return *this;
}
modnum &operator*=(const modnum &o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modnum &operator/=(const modnum &o) { return *this *= (~o); }
friend modnum operator+(const modnum &a, const modnum &b) {
return modnum(a) += b;
}
friend modnum operator-(const modnum &a, const modnum &b) {
return modnum(a) -= b;
}
friend modnum operator*(const modnum &a, const modnum &b) {
return modnum(a) *= b;
}
friend modnum operator/(const modnum &a, const modnum &b) {
return modnum(a) /= b;
}
};
using num = modnum<mod>;
vector<num> fact;
vector<num> ifact;
void init() {
fact = {1};
for (int i = 1; i < 100000; i++)
fact.push_back(i * fact[i - 1]);
for (num x : fact)
ifact.push_back(1 / x);
}
num ncr(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
num powmod(num x, int a) {
if (a == 0)
return 1;
if (a & 1)
return x * powmod(x, a - 1);
return powmod(x * x, a / 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(12);
cout << fixed;
int n;
cin >> n;
int s;
cin >> s;
vi a(n);
fori(i, n) cin >> a[i];
num ans = 0;
vector<vector<num>> dp(n + 1, vector<num>(s + 1));
vector<num> sum(s + 1);
ford(i, n) {
dp[i][a[i]] = (n - i);
rep(j, a[i] + 1, s) { dp[i][j] += sum[j - a[i]]; }
fori(j, s + 1) { sum[j] += dp[i][j]; }
ans += dp[i][s] * (i + 1);
}
cout << int(ans) << endl;
time_taken();
return 0;
} | /*
Author : N_o_o_B
Created : March 22 2020 18:30:12
*/
#include <bits/stdc++.h>
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
#define TRACE
#ifdef TRACE
#define trace(...) \
{ \
cerr << "[ "; \
__trace__(#__VA_ARGS__, __VA_ARGS__); \
}
#undef endl
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) {
return out << "(" << x.first << "," << x.second << ")";
}
template <typename Arg1>
ostream &operator<<(ostream &out, const vector<Arg1> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) {
out << "[";
for (const auto &x : a)
out << x << ",";
return out << "]";
}
template <typename Arg1> void __trace__(const string name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << " ] " << endl;
}
template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1 &&arg1, Args &&...args) {
const string name = names.substr(0, names.find(','));
cerr << name << " : " << arg1 << " | ";
__trace__(names.substr(1 + (int)name.size()), args...);
}
#else
#define trace(args...)
#endif
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<ll, ll>> vll;
typedef vector<vl> vvl;
// typedef tree<pii, null_type, less<pii>, rb_tree_tag,
// tree_order_statistics_node_update> oset;
#define fori(i, n) for (int i = 0; i < n; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define ford(i, n) for (int i = n - 1; i >= 0; i--)
#define trav(x, a) for (auto &x : a)
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define fi first
#define se second
clock_t time_p = clock();
void time_taken() {
time_p = clock() - time_p;
cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
const ll mod = 998244353;
// const ll mod=1e9+7;
const ll INF = 1e18;
// credits ---> ksun48
ll modinv(ll a, ll m) {
assert(m > 0);
if (m == 1)
return 0;
a %= m;
if (a < 0)
a += m;
assert(a != 0);
if (a == 1)
return 1;
return m - modinv(m, a) * m / a;
}
template <int MOD_> struct modnum {
private:
int v;
public:
static const int MOD = MOD_;
modnum() : v(0) {}
modnum(ll v_) : v(int(v_ % MOD)) {
if (v < 0)
v += MOD;
}
explicit operator int() const { return v; }
friend bool operator==(const modnum &a, const modnum &b) {
return a.v == b.v;
}
friend bool operator!=(const modnum &a, const modnum &b) {
return a.v != b.v;
}
modnum operator~() const {
modnum res;
res.v = modinv(v, MOD);
return res;
}
modnum &operator+=(const modnum &o) {
v += o.v;
if (v >= MOD)
v -= MOD;
return *this;
}
modnum &operator-=(const modnum &o) {
v -= o.v;
if (v < 0)
v += MOD;
return *this;
}
modnum &operator*=(const modnum &o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modnum &operator/=(const modnum &o) { return *this *= (~o); }
friend modnum operator+(const modnum &a, const modnum &b) {
return modnum(a) += b;
}
friend modnum operator-(const modnum &a, const modnum &b) {
return modnum(a) -= b;
}
friend modnum operator*(const modnum &a, const modnum &b) {
return modnum(a) *= b;
}
friend modnum operator/(const modnum &a, const modnum &b) {
return modnum(a) /= b;
}
};
using num = modnum<mod>;
vector<num> fact;
vector<num> ifact;
void init() {
fact = {1};
for (int i = 1; i < 100000; i++)
fact.push_back(i * fact[i - 1]);
for (num x : fact)
ifact.push_back(1 / x);
}
num ncr(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
num powmod(num x, int a) {
if (a == 0)
return 1;
if (a & 1)
return x * powmod(x, a - 1);
return powmod(x * x, a / 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(12);
cout << fixed;
int n;
cin >> n;
int s;
cin >> s;
vi a(n);
fori(i, n) cin >> a[i];
num ans = 0;
vector<vector<num>> dp(n + 1, vector<num>(s + 1));
vector<num> sum(s + 1);
ford(i, n) {
if (a[i] <= s)
dp[i][a[i]] = (n - i);
rep(j, a[i] + 1, s) { dp[i][j] += sum[j - a[i]]; }
fori(j, s + 1) { sum[j] += dp[i][j]; }
ans += dp[i][s] * (i + 1);
}
cout << int(ans) << endl;
time_taken();
return 0;
} | replace | 207 | 208 | 207 | 209 | 0 | Time Taken : 0.00012
|
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define int int64_t
const int MOD = 998244353;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, s;
cin >> n >> s;
int arr[n + 1];
arr[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int ans = 0;
int dp[n + 1][s + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= s; j++) {
dp[i][j] = 0;
}
}
for (int i = 1; i <= n; i++) {
dp[i][arr[i]] += i;
for (int j = 0; j <= s; j++) {
if (j + arr[i] <= s) {
dp[i][j + arr[i]] += dp[i - 1][j];
dp[i][j + arr[i]] %= MOD;
}
dp[i][j] += dp[i - 1][j];
dp[i][j] %= MOD;
// cout<<dp[i][j]<<" ";
}
// cout<<"\n";
ans += (dp[i][s] * (n - i + 1));
ans %= MOD;
dp[i][s] = 0;
}
cout << ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define int int64_t
const int MOD = 998244353;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, s;
cin >> n >> s;
int arr[n + 1];
arr[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int ans = 0;
int dp[n + 1][s + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= s; j++) {
dp[i][j] = 0;
}
}
for (int i = 1; i <= n; i++) {
if (arr[i] <= s)
dp[i][arr[i]] += i;
for (int j = 0; j <= s; j++) {
if (j + arr[i] <= s) {
dp[i][j + arr[i]] += dp[i - 1][j];
dp[i][j + arr[i]] %= MOD;
}
dp[i][j] += dp[i - 1][j];
dp[i][j] %= MOD;
// cout<<dp[i][j]<<" ";
}
// cout<<"\n";
ans += (dp[i][s] * (n - i + 1));
ans %= MOD;
dp[i][s] = 0;
}
cout << ans;
return 0;
} | replace | 30 | 31 | 30 | 32 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) { \
cout << x << ' '; \
} \
cout << endl;
#define INF 1000000000
#define mod 998244353
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
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;
}
ll n, s, a[3001], dp[3000][6100][3];
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n >> s;
rep(i, n) cin >> a[i];
rep(i, n) {
dp[i][0][0] = 1;
for (int j = 3000; j >= 0; j--) {
// 左端指定
dp[i + 1][j + a[i]][1] += dp[i][j][0];
dp[i + 1][j + a[i]][1] %= mod;
dp[i + 1][j][1] += dp[i][j][0];
dp[i + 1][j][1] %= mod;
// 区間伸ばし
dp[i + 1][j + a[i]][1] += dp[i][j][1];
dp[i + 1][j + a[i]][1] %= mod;
dp[i + 1][j][1] += dp[i][j][1];
dp[i + 1][j][1] %= mod;
// 区間閉じ
dp[i + 1][j + a[i]][2] += dp[i][j][0];
dp[i + 1][j + a[i]][2] %= mod;
dp[i + 1][j][2] += dp[i][j][0];
dp[i + 1][j][2] %= mod;
dp[i + 1][j + a[i]][2] += dp[i][j][1];
dp[i + 1][j + a[i]][2] %= mod;
dp[i + 1][j][2] += dp[i][j][1];
dp[i + 1][j][2] %= mod;
dp[i + 1][j][2] += dp[i][j][2];
dp[i + 1][j][2] %= mod;
// modint導入しておけばよかったと反省しています
}
}
cout << dp[n][s][2] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) { \
cout << x << ' '; \
} \
cout << endl;
#define INF 1000000000
#define mod 998244353
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
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;
}
ll n, s, a[3001], dp[3001][6100][3];
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n >> s;
rep(i, n) cin >> a[i];
rep(i, n) {
dp[i][0][0] = 1;
for (int j = 3000; j >= 0; j--) {
// 左端指定
dp[i + 1][j + a[i]][1] += dp[i][j][0];
dp[i + 1][j + a[i]][1] %= mod;
dp[i + 1][j][1] += dp[i][j][0];
dp[i + 1][j][1] %= mod;
// 区間伸ばし
dp[i + 1][j + a[i]][1] += dp[i][j][1];
dp[i + 1][j + a[i]][1] %= mod;
dp[i + 1][j][1] += dp[i][j][1];
dp[i + 1][j][1] %= mod;
// 区間閉じ
dp[i + 1][j + a[i]][2] += dp[i][j][0];
dp[i + 1][j + a[i]][2] %= mod;
dp[i + 1][j][2] += dp[i][j][0];
dp[i + 1][j][2] %= mod;
dp[i + 1][j + a[i]][2] += dp[i][j][1];
dp[i + 1][j + a[i]][2] %= mod;
dp[i + 1][j][2] += dp[i][j][1];
dp[i + 1][j][2] %= mod;
dp[i + 1][j][2] += dp[i][j][2];
dp[i + 1][j][2] %= mod;
// modint導入しておけばよかったと反省しています
}
}
cout << dp[n][s][2] << endl;
return 0;
}
| replace | 32 | 33 | 32 | 33 | -11 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define endl '\n'
#define ll long long
const int N = 3e2 + 5;
ll mod = 998244353;
ll a[N], dp[N][N], pre[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen(".INP", "r")) {
freopen(".INP", "r", stdin);
freopen(".OUT", "w", stdout);
}
ll n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = k; j >= 1; j--) {
if (j >= a[i]) {
(dp[i][j] = pre[j - a[i]]) %= mod;
(pre[j] += dp[i][j]) %= mod;
}
}
(pre[a[i]] += i) %= mod;
(dp[i][a[i]] += i) %= mod;
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans = (ans + dp[i][k] * (n - i + 1)) % mod;
}
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define endl '\n'
#define ll long long
const int N = 3e3 + 5;
ll mod = 998244353;
ll a[N], dp[N][N], pre[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen(".INP", "r")) {
freopen(".INP", "r", stdin);
freopen(".OUT", "w", stdout);
}
ll n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = k; j >= 1; j--) {
if (j >= a[i]) {
(dp[i][j] = pre[j - a[i]]) %= mod;
(pre[j] += dp[i][j]) %= mod;
}
}
(pre[a[i]] += i) %= mod;
(dp[i][a[i]] += i) %= mod;
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans = (ans + dp[i][k] * (n - i + 1)) % mod;
}
cout << ans;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X) \
({ \
if (1) \
cerr << "TR: " << (#X) << " = " << (X) << endl; \
})
const ll MOD = 998244353;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, s;
cin >> n >> s;
vl a(n);
FOR(i, 0, n) cin >> a[i];
ll res = 0;
vl dp(s + 1);
FOR(i, 0, n) {
vl ndp = dp;
ndp[a[i]] = (ndp[a[i]] + i + 1) % MOD;
FOR(j, a[i], s + 1) { ndp[j] = (ndp[j] + dp[j - a[i]]) % MOD; }
dp = ndp;
res = (res + dp[s]) % MOD;
}
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X) \
({ \
if (1) \
cerr << "TR: " << (#X) << " = " << (X) << endl; \
})
const ll MOD = 998244353;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, s;
cin >> n >> s;
vl a(n);
FOR(i, 0, n) cin >> a[i];
ll res = 0;
vl dp(s + 1);
FOR(i, 0, n) {
vl ndp = dp;
if (a[i] <= s)
ndp[a[i]] = (ndp[a[i]] + i + 1) % MOD;
FOR(j, a[i], s + 1) { ndp[j] = (ndp[j] + dp[j - a[i]]) % MOD; }
dp = ndp;
res = (res + dp[s]) % MOD;
}
cout << res << endl;
}
| replace | 42 | 43 | 42 | 44 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 3005;
int v[N];
long long dp[N];
const int MOD = 998244353;
int main() {
int n, s;
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
dp[0] = i % MOD;
ans += (1LL * dp[s - v[i]] * (n - i + 1) % MOD) % MOD;
ans %= MOD;
for (int ss = s - v[i]; ss >= 0; ss--) {
dp[ss + v[i]] += dp[ss];
dp[ss + v[i]] %= MOD;
}
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 3005;
int v[N];
long long dp[N];
const int MOD = 998244353;
int main() {
int n, s;
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
dp[0] = i % MOD;
if (v[i] <= s) {
ans += (1LL * dp[s - v[i]] * (n - i + 1) % MOD) % MOD;
ans %= MOD;
for (int ss = s - v[i]; ss >= 0; ss--) {
dp[ss + v[i]] += dp[ss];
dp[ss + v[i]] %= MOD;
}
}
}
cout << ans;
return 0;
}
| replace | 15 | 20 | 15 | 22 | 0 | |
p02734 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second
/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */
template <std::uint_fast64_t Modulus> class modint {
using u64 = std::uint_fast64_t;
public:
u64 a;
constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
constexpr u64 &value() noexcept { return a; }
constexpr const u64 &value() const noexcept { return a; }
constexpr modint operator+(const modint rhs) const noexcept {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint rhs) const noexcept {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint rhs) const noexcept {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint rhs) const noexcept {
return modint(*this) /= rhs;
}
constexpr modint &operator+=(const modint rhs) noexcept {
a += rhs.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr modint &operator-=(const modint rhs) noexcept {
if (a < rhs.a) {
a += Modulus;
}
a -= rhs.a;
return *this;
}
constexpr modint &operator*=(const modint rhs) noexcept {
a = a * rhs.a % Modulus;
return *this;
}
constexpr modint &operator/=(const modint rhs) noexcept {
return *this *= ~rhs;
}
constexpr modint power(u64 exp) const noexcept {
modint v = 1, x = *this;
while (exp) {
if (exp & 1) {
v *= x;
}
x *= x;
exp >>= 1;
}
return v;
}
constexpr modint operator~() const noexcept { return power(Modulus - 2); }
};
using mint = modint<998244353>;
int main() {
#ifdef DEBUG
ifstream ifs("in.txt");
cin.rdbuf(ifs.rdbuf());
#endif
int N, S;
while (cin >> N >> S) {
vector<int> A(N);
for (auto &x : A)
cin >> x;
vector<mint> dp(S + 1, 0);
mint res = 0;
for (int i = 0; i < N; i++) {
int x = A[i];
if (S - x > 0) {
res += dp[S - x] * (N - i);
}
for (int s = S; s >= x; s--) {
dp[s] += dp[s - x];
}
if (x == S) {
res += mint(i + 1) * mint(N - i);
} else {
dp[x] += i + 1;
}
}
cout << res.value() << endl;
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second
/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */
template <std::uint_fast64_t Modulus> class modint {
using u64 = std::uint_fast64_t;
public:
u64 a;
constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
constexpr u64 &value() noexcept { return a; }
constexpr const u64 &value() const noexcept { return a; }
constexpr modint operator+(const modint rhs) const noexcept {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint rhs) const noexcept {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint rhs) const noexcept {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint rhs) const noexcept {
return modint(*this) /= rhs;
}
constexpr modint &operator+=(const modint rhs) noexcept {
a += rhs.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr modint &operator-=(const modint rhs) noexcept {
if (a < rhs.a) {
a += Modulus;
}
a -= rhs.a;
return *this;
}
constexpr modint &operator*=(const modint rhs) noexcept {
a = a * rhs.a % Modulus;
return *this;
}
constexpr modint &operator/=(const modint rhs) noexcept {
return *this *= ~rhs;
}
constexpr modint power(u64 exp) const noexcept {
modint v = 1, x = *this;
while (exp) {
if (exp & 1) {
v *= x;
}
x *= x;
exp >>= 1;
}
return v;
}
constexpr modint operator~() const noexcept { return power(Modulus - 2); }
};
using mint = modint<998244353>;
int main() {
#ifdef DEBUG
ifstream ifs("in.txt");
cin.rdbuf(ifs.rdbuf());
#endif
int N, S;
while (cin >> N >> S) {
vector<int> A(N);
for (auto &x : A)
cin >> x;
vector<mint> dp(S + 1, 0);
mint res = 0;
for (int i = 0; i < N; i++) {
int x = A[i];
if (S - x > 0) {
res += dp[S - x] * (N - i);
}
for (int s = S; s >= x; s--) {
dp[s] += dp[s - x];
}
if (x == S) {
res += mint(i + 1) * mint(N - i);
} else if (x < S) {
dp[x] += i + 1;
}
}
cout << res.value() << endl;
}
return 0;
}
| replace | 113 | 114 | 113 | 114 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MAX_N 3000
using namespace std;
typedef long long lint;
const lint MOD = 998244353LL;
int n, s;
int v[MAX_N + 9];
lint ap[MAX_N + 9];
void modd(lint &a) {
if (a >= MOD)
a -= MOD;
if (a < 0)
a += MOD;
}
void modd(int &a) {
if (a >= MOD)
a -= MOD;
if (a < 0)
a += MOD;
}
int main() {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
lint rez = 0;
// ap[0] = 1;
for (int i = 1; i <= n; i++) {
// cout << " SUNTME " << i << "ADUNAM " << (n - i + 1) << " RO " << ap[s]
// << "\n";
if (v[i] != s)
rez += 1LL * (n - i + 1) * ap[s - v[i]] % MOD;
else if (v[i] == s)
rez += 1LL * (n - i + 1) * i % MOD;
modd(rez);
for (int j = s; j > v[i]; j--) {
ap[j] += ap[j - v[i]];
modd(ap[j]);
}
ap[v[i]] += i;
modd(ap[v[i]]);
}
cout << rez << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define MAX_N 3000
using namespace std;
typedef long long lint;
const lint MOD = 998244353LL;
int n, s;
int v[MAX_N + 9];
lint ap[MAX_N + 9];
void modd(lint &a) {
if (a >= MOD)
a -= MOD;
if (a < 0)
a += MOD;
}
void modd(int &a) {
if (a >= MOD)
a -= MOD;
if (a < 0)
a += MOD;
}
int main() {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
lint rez = 0;
// ap[0] = 1;
for (int i = 1; i <= n; i++) {
// cout << " SUNTME " << i << "ADUNAM " << (n - i + 1) << " RO " << ap[s]
// << "\n";
if (v[i] < s)
rez += 1LL * (n - i + 1) * ap[s - v[i]] % MOD;
else if (v[i] == s)
rez += 1LL * (n - i + 1) * i % MOD;
modd(rez);
for (int j = s; j > v[i]; j--) {
ap[j] += ap[j - v[i]];
modd(ap[j]);
}
ap[v[i]] += i;
modd(ap[v[i]]);
}
cout << rez << "\n";
return 0;
}
| replace | 44 | 45 | 44 | 45 | 0 | |
p02734 | C++ | Runtime Error | typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
int main() {
ll n, s;
std::cin >> n >> s;
vector<ll> a(n);
const ll MOD = 998244353;
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
vector<vector<ll>> dp(n + 1, vector<ll>(s + 1, 0));
dp[0][0] = 1;
ll ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= s; j++) {
dp[i + 1][j] = dp[i][j];
}
for (int j = a[i]; j <= s; j++) {
dp[i + 1][j] += dp[i][j - a[i]];
dp[i + 1][j] %= MOD;
}
dp[i + 1][a[i]] += i;
}
for (int i = 1; i <= n; i++) {
ans += dp[i][s];
ans %= MOD;
}
std::cout << ans << std::endl;
}
| typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
int main() {
ll n, s;
std::cin >> n >> s;
vector<ll> a(n);
const ll MOD = 998244353;
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
vector<vector<ll>> dp(n + 1, vector<ll>(3001, 0));
dp[0][0] = 1;
ll ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= s; j++) {
dp[i + 1][j] = dp[i][j];
}
for (int j = a[i]; j <= s; j++) {
dp[i + 1][j] += dp[i][j - a[i]];
dp[i + 1][j] %= MOD;
}
dp[i + 1][a[i]] += i;
}
for (int i = 1; i <= n; i++) {
ans += dp[i][s];
ans %= MOD;
}
std::cout << ans << std::endl;
}
| replace | 14 | 15 | 14 | 15 | 0 | |
p02734 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef HOME
#warning CHECK int:ll::INT_MAX:LLONG_MAX
#define maxn 20
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define maxn 2000006
#define debug(x...)
#endif
#define ff first
#define endl '\n'
#define ss second
#define inf 0x3f3f3f3f
#define MOD 998244353
#define f(i, x, n) for (int i = x; i <= n; i++)
#define fr(i, x, n) for (int i = x; i >= n; i--)
struct _ {
ios_base::Init i;
_() { cin.sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
int main() {
long long n, s;
cin >> n >> s;
vector<long long> a(n + 1, 0);
for (long long i = 1; i <= n; i++)
cin >> a[i];
vector<vector<long long>> dp(
n + 1,
vector<long long>(
s + 1,
0)); // dp[i][j]=no. of subsequences ending at i, that can make 'j'
dp[1][a[1]] = 1;
for (long long i = 0; i <= n; i++)
dp[i][0] = 1;
dp[1][0] = 2;
for (long long i = 2; i <= n; i++) {
for (long long j = a[i]; j <= s; j++)
dp[i][j] = dp[i - 1][j - a[i]];
for (long long j = 0; j <= s; j++)
dp[i][j] = ((dp[i][j] % MOD) + (dp[i - 1][j] % MOD)) % MOD;
}
debug(dp);
long long ans = 0;
for (long long i = 1; i <= n; i++) {
ans = ((ans % MOD) +
(((dp[i][s] - dp[i - 1][s] + MOD) % MOD) * ((n - i + 1) % MOD)) %
MOD) %
MOD;
}
cout << ans;
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef HOME
#warning CHECK int:ll::INT_MAX:LLONG_MAX
#define maxn 20
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define maxn 2000006
#define debug(x...)
#endif
#define ff first
#define endl '\n'
#define ss second
#define inf 0x3f3f3f3f
#define MOD 998244353
#define f(i, x, n) for (int i = x; i <= n; i++)
#define fr(i, x, n) for (int i = x; i >= n; i--)
struct _ {
ios_base::Init i;
_() { cin.sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
int main() {
long long n, s;
cin >> n >> s;
vector<long long> a(n + 1, 0);
for (long long i = 1; i <= n; i++)
cin >> a[i];
vector<vector<long long>> dp(
n + 1,
vector<long long>(
s + 1,
0)); // dp[i][j]=no. of subsequences ending at i, that can make 'j'
if (a[1] <= s)
dp[1][a[1]] = 1;
for (long long i = 0; i <= n; i++)
dp[i][0] = 1;
dp[1][0] = 2;
for (long long i = 2; i <= n; i++) {
for (long long j = a[i]; j <= s; j++)
dp[i][j] = dp[i - 1][j - a[i]];
for (long long j = 0; j <= s; j++)
dp[i][j] = ((dp[i][j] % MOD) + (dp[i - 1][j] % MOD)) % MOD;
}
debug(dp);
long long ans = 0;
for (long long i = 1; i <= n; i++) {
ans = ((ans % MOD) +
(((dp[i][s] - dp[i - 1][s] + MOD) % MOD) * ((n - i + 1) % MOD)) %
MOD) %
MOD;
}
cout << ans;
return 0;
} | replace | 94 | 95 | 94 | 96 | 0 | |
p02734 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 998244353;
constexpr bool debug = 0;
//---------------------------------//
template <int M> struct ModInt {
public:
using value_type = long long;
ModInt(value_type val = 0) : val(val < 0 ? (M - (-val % M)) % M : val % M) {}
explicit operator bool() const noexcept { return val; }
bool operator==(const ModInt &rhs) const noexcept { return val == rhs.val; }
bool operator!=(const ModInt &rhs) const noexcept { return !(*this == rhs); }
ModInt operator+() const noexcept { return ModInt(*this); }
ModInt operator-() const noexcept { return ModInt(0) -= *this; }
ModInt operator+(const ModInt &rhs) const noexcept {
return ModInt(*this) += rhs;
}
ModInt operator-(const ModInt &rhs) const noexcept {
return ModInt(*this) -= rhs;
}
ModInt operator*(const ModInt &rhs) const noexcept {
return ModInt(*this) *= rhs;
}
ModInt operator/(const ModInt &rhs) const noexcept {
return ModInt(*this) /= rhs;
}
ModInt &operator+=(const ModInt &rhs) noexcept {
val += rhs.val;
if (val >= M)
val -= M;
return *this;
}
ModInt &operator-=(const ModInt &rhs) noexcept {
if (val < rhs.val)
val += M;
val -= rhs.val;
return *this;
}
ModInt &operator*=(const ModInt &rhs) noexcept {
val = val * rhs.val % M;
return *this;
}
ModInt &operator/=(const ModInt &rhs) noexcept {
*this *= rhs.inverse();
return *this;
}
ModInt pow(value_type n) const {
ModInt res = 1, x = val;
if (n < 0) {
x = x.inverse();
n = -n;
}
while (n) {
if (n & 1)
res *= x;
x *= x;
n >>= 1;
}
return res;
}
ModInt inverse() const {
long long a = val, a1 = 1, a2 = 0, b = M, b1 = 0, b2 = 1;
while (b > 0) {
value_type q = a / b, r = a % b;
value_type nb1 = a1 - q * b1, nb2 = a2 - q * b2;
a = b;
b = r;
a1 = b1;
b1 = nb1;
a2 = b2;
b2 = nb2;
}
assert(a == 1);
return a1;
}
const value_type &get() const noexcept { return val; }
static decltype(M) get_mod() noexcept { return M; }
friend std::ostream &operator<<(std::ostream &os, const ModInt &rhs) {
return os << rhs.val;
}
friend std::istream &operator>>(std::istream &is, ModInt &rhs) {
value_type x;
is >> x;
rhs = ModInt(x);
return is;
}
private:
value_type val;
};
using mint = ModInt<MOD>;
int main() {
int N, S;
cin >> N >> S;
vector<int> A(N);
REP(i, N) scanf("%d", &A[i]);
vector<vector<vector<mint>>> dp(N + 1,
vector<vector<mint>>(S + 1, vector<mint>(3)));
dp[0][0][0] = 1;
REP(i, N + 1) {
REP(j, S + 1) {
REP(k, 3) {
if (i < N)
dp[i + 1][j][k] += dp[i][j][k];
if (k < 3)
dp[i][j][k + 1] += dp[i][j][k];
if (i < N && k == 1 && j + A[i] <= S)
dp[i + 1][j + A[i]][k] += dp[i][j][k];
}
}
}
cout << dp[N][S][2] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 998244353;
constexpr bool debug = 0;
//---------------------------------//
template <int M> struct ModInt {
public:
using value_type = long long;
ModInt(value_type val = 0) : val(val < 0 ? (M - (-val % M)) % M : val % M) {}
explicit operator bool() const noexcept { return val; }
bool operator==(const ModInt &rhs) const noexcept { return val == rhs.val; }
bool operator!=(const ModInt &rhs) const noexcept { return !(*this == rhs); }
ModInt operator+() const noexcept { return ModInt(*this); }
ModInt operator-() const noexcept { return ModInt(0) -= *this; }
ModInt operator+(const ModInt &rhs) const noexcept {
return ModInt(*this) += rhs;
}
ModInt operator-(const ModInt &rhs) const noexcept {
return ModInt(*this) -= rhs;
}
ModInt operator*(const ModInt &rhs) const noexcept {
return ModInt(*this) *= rhs;
}
ModInt operator/(const ModInt &rhs) const noexcept {
return ModInt(*this) /= rhs;
}
ModInt &operator+=(const ModInt &rhs) noexcept {
val += rhs.val;
if (val >= M)
val -= M;
return *this;
}
ModInt &operator-=(const ModInt &rhs) noexcept {
if (val < rhs.val)
val += M;
val -= rhs.val;
return *this;
}
ModInt &operator*=(const ModInt &rhs) noexcept {
val = val * rhs.val % M;
return *this;
}
ModInt &operator/=(const ModInt &rhs) noexcept {
*this *= rhs.inverse();
return *this;
}
ModInt pow(value_type n) const {
ModInt res = 1, x = val;
if (n < 0) {
x = x.inverse();
n = -n;
}
while (n) {
if (n & 1)
res *= x;
x *= x;
n >>= 1;
}
return res;
}
ModInt inverse() const {
long long a = val, a1 = 1, a2 = 0, b = M, b1 = 0, b2 = 1;
while (b > 0) {
value_type q = a / b, r = a % b;
value_type nb1 = a1 - q * b1, nb2 = a2 - q * b2;
a = b;
b = r;
a1 = b1;
b1 = nb1;
a2 = b2;
b2 = nb2;
}
assert(a == 1);
return a1;
}
const value_type &get() const noexcept { return val; }
static decltype(M) get_mod() noexcept { return M; }
friend std::ostream &operator<<(std::ostream &os, const ModInt &rhs) {
return os << rhs.val;
}
friend std::istream &operator>>(std::istream &is, ModInt &rhs) {
value_type x;
is >> x;
rhs = ModInt(x);
return is;
}
private:
value_type val;
};
using mint = ModInt<MOD>;
int main() {
int N, S;
cin >> N >> S;
vector<int> A(N);
REP(i, N) scanf("%d", &A[i]);
vector<vector<vector<mint>>> dp(N + 1,
vector<vector<mint>>(S + 1, vector<mint>(3)));
dp[0][0][0] = 1;
REP(i, N + 1) {
REP(j, S + 1) {
REP(k, 3) {
if (i < N)
dp[i + 1][j][k] += dp[i][j][k];
if (k < 2)
dp[i][j][k + 1] += dp[i][j][k];
if (i < N && k == 1 && j + A[i] <= S)
dp[i + 1][j + A[i]][k] += dp[i][j][k];
}
}
}
cout << dp[N][S][2] << endl;
return 0;
}
| replace | 137 | 138 | 137 | 138 | -6 | munmap_chunk(): invalid pointer
|
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define endl "\n"
#define inf 1000000007
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define bigboi 1e18
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUF(v) shuffle(all(v), RNG);
// Use mt19937_64 for 64 bit random numbers.
int dp[200][200];
void solve() {
int n, m;
cin >> n >> m;
char arr[n][m];
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> arr[i][j];
}
}
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= m; ++j) {
dp[i][j] = inf;
}
}
if (arr[1][1] == '#')
dp[1][1] = 1;
else
dp[1][1] = 0;
FOR(i, 1, n + 1) {
FOR(j, 1, m + 1) {
if (i == 1 && j == 1)
continue;
if (arr[i][j] == '#') {
dp[i][j] = min(dp[i][j], dp[i][j - 1] + (arr[i][j] != arr[i][j - 1]));
dp[i][j] = min(dp[i][j], dp[i - 1][j] + (arr[i][j] != arr[i - 1][j]));
} else {
dp[i][j] = min(dp[i][j], dp[i][j - 1]);
dp[i][j] = min(dp[i][j], dp[i - 1][j]);
}
}
}
cout << dp[n][m];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin>>t;
FOR(i, 1, t + 1) {
// cout<<"Case #"<<i<<": ";
solve();
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define endl "\n"
#define inf 1000000007
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define bigboi 1e18
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUF(v) shuffle(all(v), RNG);
// Use mt19937_64 for 64 bit random numbers.
int dp[200][200];
void solve() {
int n, m;
cin >> n >> m;
char arr[n + 12][m + 12];
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> arr[i][j];
}
}
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= m; ++j) {
dp[i][j] = inf;
}
}
if (arr[1][1] == '#')
dp[1][1] = 1;
else
dp[1][1] = 0;
FOR(i, 1, n + 1) {
FOR(j, 1, m + 1) {
if (i == 1 && j == 1)
continue;
if (arr[i][j] == '#') {
dp[i][j] = min(dp[i][j], dp[i][j - 1] + (arr[i][j] != arr[i][j - 1]));
dp[i][j] = min(dp[i][j], dp[i - 1][j] + (arr[i][j] != arr[i - 1][j]));
} else {
dp[i][j] = min(dp[i][j], dp[i][j - 1]);
dp[i][j] = min(dp[i][j], dp[i - 1][j]);
}
}
}
cout << dp[n][m];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin>>t;
FOR(i, 1, t + 1) {
// cout<<"Case #"<<i<<": ";
solve();
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| replace | 20 | 21 | 20 | 21 | 0 |
Time elapsed: 29ms
|
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int h, w;
cin >> h >> w;
vector<string> s(h);
int i, j;
for (i = 0; i < h; i++)
cin >> s[i];
int ans = 1 << 30;
int tmp[h][w];
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
tmp[i][j] = 1 << 30;
}
}
queue<pair<pair<int, int>, pair<int, char>>> que;
if (s[0][0] == '#') {
que.push(make_pair(make_pair(0, 0), make_pair(1, '#')));
} else {
que.push(make_pair(make_pair(0, 0), make_pair(0, '.')));
}
while (!que.empty()) {
int change = que.front().second.first;
char color = que.front().second.second;
int y = que.front().first.first;
int x = que.front().first.second;
char opp;
bool chk = true;
if (tmp[y][x] < change)
chk = false;
else {
tmp[y][x] = change;
}
if (color == '#')
opp = '.';
else
opp = '#';
que.pop();
int aa = 0;
if (color == '.')
aa = 1;
if (x == w - 1 && y == h - 1) {
if (ans > change)
ans = change;
} else if (change < ans && chk) {
if (x < w - 1) {
if (s[y][x + 1] == color)
que.push(make_pair(make_pair(y, x + 1), make_pair(change, color)));
else
que.push(make_pair(make_pair(y, x + 1), make_pair(change + aa, opp)));
}
if (y < h - 1) {
if (s[y + 1][x] == color)
que.push(make_pair(make_pair(y + 1, x), make_pair(change, color)));
else
que.push(make_pair(make_pair(y + 1, x), make_pair(change + aa, opp)));
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int h, w;
cin >> h >> w;
vector<string> s(h);
int i, j;
for (i = 0; i < h; i++)
cin >> s[i];
int ans = 1 << 30;
int tmp[h][w];
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
tmp[i][j] = 1 << 30;
}
}
queue<pair<pair<int, int>, pair<int, char>>> que;
if (s[0][0] == '#') {
que.push(make_pair(make_pair(0, 0), make_pair(1, '#')));
} else {
que.push(make_pair(make_pair(0, 0), make_pair(0, '.')));
}
while (!que.empty()) {
int change = que.front().second.first;
char color = que.front().second.second;
int y = que.front().first.first;
int x = que.front().first.second;
char opp;
bool chk = true;
if (tmp[y][x] <= change)
chk = false;
else {
tmp[y][x] = change;
}
if (color == '#')
opp = '.';
else
opp = '#';
que.pop();
int aa = 0;
if (color == '.')
aa = 1;
if (x == w - 1 && y == h - 1) {
if (ans > change)
ans = change;
} else if (change < ans && chk) {
if (x < w - 1) {
if (s[y][x + 1] == color)
que.push(make_pair(make_pair(y, x + 1), make_pair(change, color)));
else
que.push(make_pair(make_pair(y, x + 1), make_pair(change + aa, opp)));
}
if (y < h - 1) {
if (s[y + 1][x] == color)
que.push(make_pair(make_pair(y + 1, x), make_pair(change, color)));
else
que.push(make_pair(make_pair(y + 1, x), make_pair(change + aa, opp)));
}
}
}
cout << ans << endl;
} | replace | 29 | 30 | 29 | 30 | TLE | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int solve(int h, int w, vector<vector<int>> &k, vector<vector<int>> &u) {
if (h == 1 && w == 1) {
return u[1][1];
}
if (h == 0)
return 100000;
if (w == 0)
return 100000;
else {
int a = solve(h, w - 1, k, u);
if (u[h][w - 1] == 0 && u[h][w] == 1) {
a++;
}
int b = solve(h - 1, w, k, u);
if (u[h - 1][w] == 0 && u[h][w] == 1) {
b++;
}
k[h][w] = min(a, b);
}
return k[h][w];
}
int main() {
int h, w;
cin >> h >> w;
vector<vector<int>> k(h + 1, vector<int>(w + 1, 100000));
vector<vector<int>> u(h + 1, vector<int>(w + 1, 1));
rep(i, h) {
rep(j, w) {
char tmp;
cin >> tmp;
if (tmp == '#')
u[i + 1][j + 1] = 1;
else
u[i + 1][j + 1] = 0;
}
}
cout << solve(h, w, k, u) << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int solve(int h, int w, vector<vector<int>> &k, vector<vector<int>> &u) {
if (h == 1 && w == 1) {
return u[1][1];
}
if (k[h][w] <= 10000)
return k[h][w];
if (h == 0)
return 100000;
if (w == 0)
return 100000;
else {
int a = solve(h, w - 1, k, u);
if (u[h][w - 1] == 0 && u[h][w] == 1) {
a++;
}
int b = solve(h - 1, w, k, u);
if (u[h - 1][w] == 0 && u[h][w] == 1) {
b++;
}
k[h][w] = min(a, b);
}
return k[h][w];
}
int main() {
int h, w;
cin >> h >> w;
vector<vector<int>> k(h + 1, vector<int>(w + 1, 100000));
vector<vector<int>> u(h + 1, vector<int>(w + 1, 1));
rep(i, h) {
rep(j, w) {
char tmp;
cin >> tmp;
if (tmp == '#')
u[i + 1][j + 1] = 1;
else
u[i + 1][j + 1] = 0;
}
}
cout << solve(h, w, k, u) << endl;
} | insert | 7 | 7 | 7 | 9 | TLE | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
vector<vector<bool>> White(100, vector<bool>(100, false));
vector<vector<int>> Steps(100, vector<int>(100, 100000));
int H, W;
void search(int h, int w, int prevStep, bool prevBlack) {
if (h >= H || w >= W)
return;
if (Steps.at(h).at(w) < prevStep)
return;
if (White.at(h).at(w)) {
Steps.at(h).at(w) = prevStep;
search(h, w + 1, prevStep, false);
search(h + 1, w, prevStep, false);
} else {
if (prevBlack) {
Steps.at(h).at(w) = prevStep;
search(h, w + 1, prevStep, true);
search(h + 1, w, prevStep, true);
} else {
Steps.at(h).at(w) = prevStep + 1;
search(h, w + 1, prevStep + 1, true);
search(h + 1, w, prevStep + 1, true);
}
}
}
int main() {
cin >> H >> W;
char g;
for (int r = 0; r < H; r++) {
for (int c = 0; c < W; c++) {
cin >> g;
if (g == '.')
White.at(r).at(c) = true;
}
}
search(0, 0, 0, false);
cout << Steps.at(H - 1).at(W - 1) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
vector<vector<bool>> White(100, vector<bool>(100, false));
vector<vector<int>> Steps(100, vector<int>(100, 100000));
int H, W;
void search(int h, int w, int prevStep, bool prevBlack) {
if (h >= H || w >= W)
return;
if (Steps.at(h).at(w) <= prevStep)
return;
if (White.at(h).at(w)) {
Steps.at(h).at(w) = prevStep;
search(h, w + 1, prevStep, false);
search(h + 1, w, prevStep, false);
} else {
if (prevBlack) {
Steps.at(h).at(w) = prevStep;
search(h, w + 1, prevStep, true);
search(h + 1, w, prevStep, true);
} else {
Steps.at(h).at(w) = prevStep + 1;
search(h, w + 1, prevStep + 1, true);
search(h + 1, w, prevStep + 1, true);
}
}
}
int main() {
cin >> H >> W;
char g;
for (int r = 0; r < H; r++) {
for (int c = 0; c < W; c++) {
cin >> g;
if (g == '.')
White.at(r).at(c) = true;
}
}
search(0, 0, 0, false);
cout << Steps.at(H - 1).at(W - 1) << endl;
}
| replace | 9 | 10 | 9 | 10 | TLE | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#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 V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr auto INF = INT_MAX >> 1;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;
int main() {
int h, w;
cin >> h >> w;
V<string> s(h);
rep(i, h) cin >> s[i];
deque<P> dq;
dq.push_front(P(0, 0));
vvi dist(h, vi(w, INF));
dist[0][0] = s[0][0] == '#' ? 1 : 0;
while (!dq.empty()) {
int y = dq.front().first;
int x = dq.front().second;
dq.pop_front();
vi dy = {0, 1};
vi dx = {1, 0};
rep(i, 2) {
int ey = y + dy[i];
int ex = x + dx[i];
if (ey < 0 || h <= ey || ex < 0 || w <= ex)
continue;
int d = 0;
if (s[y][x] == '.' && s[ey][ex] == '#')
d = 1;
if (dist[ey][ex] < dist[y][x] + d)
continue;
dist[ey][ex] = dist[y][x] + d;
if (d)
dq.push_back(P(ey, ex));
else
dq.push_front(P(ey, ex));
}
}
cout << dist[h - 1][w - 1] << endl;
} | #include <bits/stdc++.h>
using namespace std;
#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 V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr auto INF = INT_MAX >> 1;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;
int main() {
int h, w;
cin >> h >> w;
V<string> s(h);
rep(i, h) cin >> s[i];
deque<P> dq;
dq.push_front(P(0, 0));
vvi dist(h, vi(w, INF));
dist[0][0] = s[0][0] == '#' ? 1 : 0;
while (!dq.empty()) {
int y = dq.front().first;
int x = dq.front().second;
dq.pop_front();
vi dy = {0, 1};
vi dx = {1, 0};
rep(i, 2) {
int ey = y + dy[i];
int ex = x + dx[i];
if (ey < 0 || h <= ey || ex < 0 || w <= ex)
continue;
int d = 0;
if (s[y][x] == '.' && s[ey][ex] == '#')
d = 1;
if (dist[ey][ex] <= dist[y][x] + d)
continue;
dist[ey][ex] = dist[y][x] + d;
if (d)
dq.push_back(P(ey, ex));
else
dq.push_front(P(ey, ex));
}
}
cout << dist[h - 1][w - 1] << endl;
} | replace | 41 | 42 | 41 | 42 | TLE | |
p02735 | C++ | Time Limit Exceeded | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const ll MOD = 1000000007;
const long long INF = 1LL << 60;
const double pi = acos(-1.0);
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
vector<string> grid;
ll H, W;
bool movable(ll sh, ll sw) {
if (sh < 0 || sh >= H || sw < 0 || sw >= W)
return false;
return true;
}
vector<vector<ll>> table;
ll dp(ll h, ll w) {
// table.at(h).at(w)-=1;
// 右移動
if (movable(h, w - 1)) {
if (grid.at(h).at(w - 1) == '.' && grid.at(h).at(w) == '#')
chmin(table.at(h).at(w), dp(h, w - 1) + 1);
else
chmin(table.at(h).at(w), dp(h, w - 1));
}
// 下移動
if (movable(h - 1, w)) {
if (grid.at(h - 1).at(w) == '.' && grid.at(h).at(w) == '#')
chmin(table.at(h).at(w), dp(h - 1, w) + 1);
else
chmin(table.at(h).at(w), dp(h - 1, w));
}
return table.at(h).at(w);
}
int main() {
cin >> H >> W;
grid = vector<string>(H);
rep(i, H) { cin >> grid.at(i); }
table = vector<vector<ll>>(H, vector<ll>(W, INF));
if (grid.at(0).at(0) == '.')
table.at(0).at(0) = 0;
else
table.at(0).at(0) = 1;
cout << dp(H - 1, W - 1) << endl;
return 0;
}
| #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const ll MOD = 1000000007;
const long long INF = 1LL << 60;
const double pi = acos(-1.0);
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
vector<string> grid;
ll H, W;
bool movable(ll sh, ll sw) {
if (sh < 0 || sh >= H || sw < 0 || sw >= W)
return false;
return true;
}
vector<vector<ll>> table;
ll dp(ll h, ll w) {
if (table.at(h).at(w) < INF)
return table.at(h).at(w);
table.at(h).at(w) -= 1;
// 右移動
if (movable(h, w - 1)) {
if (grid.at(h).at(w - 1) == '.' && grid.at(h).at(w) == '#')
chmin(table.at(h).at(w), dp(h, w - 1) + 1);
else
chmin(table.at(h).at(w), dp(h, w - 1));
}
// 下移動
if (movable(h - 1, w)) {
if (grid.at(h - 1).at(w) == '.' && grid.at(h).at(w) == '#')
chmin(table.at(h).at(w), dp(h - 1, w) + 1);
else
chmin(table.at(h).at(w), dp(h - 1, w));
}
return table.at(h).at(w);
}
int main() {
cin >> H >> W;
grid = vector<string>(H);
rep(i, H) { cin >> grid.at(i); }
table = vector<vector<ll>>(H, vector<ll>(W, INF));
if (grid.at(0).at(0) == '.')
table.at(0).at(0) = 0;
else
table.at(0).at(0) = 1;
cout << dp(H - 1, W - 1) << endl;
return 0;
}
| replace | 35 | 36 | 35 | 38 | TLE | |
p02735 | C++ | Runtime Error | #include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long
#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) \
do { \
} while (0)
#define dump(x) \
do { \
} while (0)
#define BR
#endif
using namespace std;
using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int dx[] = {1, 0};
int dy[] = {0, 1};
void solve() {
int h, w;
cin >> h >> w;
VS s(h);
REP(i, 0, h) cin >> s[i];
VVI d(h, VI(w, 1 << 30));
d[0][0] = (s[0][0] == '#');
deque<PII> q;
q.emplace_back(0, 0);
while (!q.empty()) {
auto p = q.front();
q.pop_front();
int x = p.first, y = p.second;
REP(i, 0, 4) {
int rx = x + dx[i];
int ry = y + dy[i];
if (rx < 0 || rx >= h)
continue;
if (ry < 0 || ry >= w)
continue;
if (d[rx][ry] <= (s[x][y] == '.' && s[rx][ry] == '#') + d[x][y])
continue;
d[rx][ry] = (s[x][y] == '.' && s[rx][ry] == '#') + d[x][y];
q.emplace_back(rx, ry);
}
}
cout << d[h - 1][w - 1] << endl;
}
signed main() {
solve();
return 0;
} | #include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long
#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) \
do { \
} while (0)
#define dump(x) \
do { \
} while (0)
#define BR
#endif
using namespace std;
using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int dx[] = {1, 0};
int dy[] = {0, 1};
void solve() {
int h, w;
cin >> h >> w;
VS s(h);
REP(i, 0, h) cin >> s[i];
VVI d(h, VI(w, 1 << 30));
d[0][0] = (s[0][0] == '#');
deque<PII> q;
q.emplace_back(0, 0);
while (!q.empty()) {
auto p = q.front();
q.pop_front();
int x = p.first, y = p.second;
REP(i, 0, 2) {
int rx = x + dx[i];
int ry = y + dy[i];
if (rx < 0 || rx >= h)
continue;
if (ry < 0 || ry >= w)
continue;
if (d[rx][ry] <= (s[x][y] == '.' && s[rx][ry] == '#') + d[x][y])
continue;
d[rx][ry] = (s[x][y] == '.' && s[rx][ry] == '#') + d[x][y];
q.emplace_back(rx, ry);
}
}
cout << d[h - 1][w - 1] << endl;
}
signed main() {
solve();
return 0;
} | replace | 85 | 86 | 85 | 86 | 0 | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const long long D = 1e9 + 7;
long long pow2(long long x, long long y) {
if (y == 0)
return 1;
long long ret = pow2(x, y / 2);
return ret * ret % D * (y % 2 == 1 ? x : 1) % D;
}
char a[105][105];
int n, m;
int dp[105][105];
int s[105][105];
bool b[101][101][101][101];
bool f(int x, int y, int xx, int yy) { return b[x][y][xx][yy]; }
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int i, j;
cin >> n >> m;
for (i = 0; i <= n; i++)
for (j = 0; j <= m; j++)
dp[i][j] = 1e9;
for (i = 1; i <= n; i++)
cin >> a[i] + 1;
dp[1][0] = dp[0][1] = 0;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
s[i][j] = s[i][j - 1] + s[i - 1][j] + (a[i][j] == '#') - s[i - 1][j - 1];
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (a[i][j] == '#')
b[i][j][i][j] = 1;
for (int ii = i; ii <= n; ii++) {
for (int jj = j; jj <= m; jj++) {
if (ii == i && jj == j)
continue;
b[i][j][ii][jj] = ((b[i][j][ii - 1][jj] | b[i][j][ii][jj - 1]) &
(a[ii][jj] == '#'));
/// cout<<i<<" "<<j<<" "<<ii<<" "<<jj<<" "<<b[i][j][ii][jj]<<"\n";
}
}
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
dp[i][j] = min({dp[i][j], dp[i - 1][j] + (a[i][j] == '#'),
dp[i][j - 1] + (a[i][j] == '#')});
for (int ii = 0; ii <= i; ii++) {
for (int jj = 0; jj <= j; jj++) {
if (f(ii + 1, jj, i, j) || f(ii, jj + 1, i, j))
dp[i][j] = min(dp[i][j], dp[ii][jj] + 1);
}
}
}
}
cout << dp[n][m];
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const long long D = 1e9 + 7;
long long pow2(long long x, long long y) {
if (y == 0)
return 1;
long long ret = pow2(x, y / 2);
return ret * ret % D * (y % 2 == 1 ? x : 1) % D;
}
char a[105][105];
int n, m;
int dp[105][105];
int s[105][105];
bool b[102][102][102][102];
bool f(int x, int y, int xx, int yy) {
if (x == 0 || y == 0 || x > xx || y > yy)
return 0;
return b[x][y][xx][yy];
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int i, j;
cin >> n >> m;
for (i = 0; i <= n; i++)
for (j = 0; j <= m; j++)
dp[i][j] = 1e9;
for (i = 1; i <= n; i++)
cin >> a[i] + 1;
dp[1][0] = dp[0][1] = 0;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
s[i][j] = s[i][j - 1] + s[i - 1][j] + (a[i][j] == '#') - s[i - 1][j - 1];
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (a[i][j] == '#')
b[i][j][i][j] = 1;
for (int ii = i; ii <= n; ii++) {
for (int jj = j; jj <= m; jj++) {
if (ii == i && jj == j)
continue;
b[i][j][ii][jj] = ((b[i][j][ii - 1][jj] | b[i][j][ii][jj - 1]) &
(a[ii][jj] == '#'));
/// cout<<i<<" "<<j<<" "<<ii<<" "<<jj<<" "<<b[i][j][ii][jj]<<"\n";
}
}
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
dp[i][j] = min({dp[i][j], dp[i - 1][j] + (a[i][j] == '#'),
dp[i][j - 1] + (a[i][j] == '#')});
for (int ii = 0; ii <= i; ii++) {
for (int jj = 0; jj <= j; jj++) {
if (f(ii + 1, jj, i, j) || f(ii, jj + 1, i, j))
dp[i][j] = min(dp[i][j], dp[ii][jj] + 1);
}
}
}
}
cout << dp[n][m];
return 0;
}
| replace | 16 | 18 | 16 | 22 | 127 | /tmp/4b94f909-f5fd-4ce4-9168-eadc3a8aae62.out: error while loading shared libraries: libstdc++.so.6: failed to map segment from shared object
|
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using maze = vector<string>;
const int NOT_VISITED = -1;
const char WALL = '#';
const char PATH = '.';
queue<pair<int, int>> q_path, q_wall;
int dh[] = {1, 0, -1, 0};
int dw[] = {0, 1, 0, -1};
void bfs(int &cnt, bool flip, maze &m, vector<vector<int>> &dist) {
int h = m.size(), w = m.front().size();
auto &q = !flip ? q_path : q_wall;
auto &q_next = !flip ? q_wall : q_path;
while (!q.empty()) {
int ch = q.front().first, cw = q.front().second;
q.pop();
for (int k = 0; k < 2; ++k) {
int nh = ch + dh[k], nw = cw + dw[k];
if (nh < 0 || h <= nh || nw < 0 || w <= nw)
continue;
if ((!flip && m[nh][nw] == WALL) || (flip && m[nh][nw] == PATH)) {
q_next.emplace(nh, nw);
dist[nh][nw] = cnt + 1;
continue;
}
if (dist[nh][nw] == NOT_VISITED) {
q.emplace(nh, nw);
dist[nh][nw] = cnt;
}
}
}
if (dist[h - 1][w - 1] == NOT_VISITED)
++cnt;
}
int main() {
int h, w;
cin >> h >> w;
maze m(h);
for (auto &x : m)
cin >> x;
int cnt = 0;
bool flip = false;
vector<vector<int>> dist(h, vector<int>(w, NOT_VISITED));
if (m[0][0] == WALL) {
cnt = 1;
flip = true;
}
dist[0][0] = cnt;
if (!flip)
q_path.emplace(0, 0);
else
q_wall.emplace(0, 0);
auto &goal = dist[h - 1][w - 1];
while (goal == NOT_VISITED) {
bfs(cnt, flip, m, dist);
flip = !flip;
}
cout << (goal + 1) / 2 << endl;
} | #include <bits/stdc++.h>
using namespace std;
using maze = vector<string>;
const int NOT_VISITED = -1;
const char WALL = '#';
const char PATH = '.';
queue<pair<int, int>> q_path, q_wall;
int dh[] = {1, 0, -1, 0};
int dw[] = {0, 1, 0, -1};
void bfs(int &cnt, bool flip, maze &m, vector<vector<int>> &dist) {
int h = m.size(), w = m.front().size();
auto &q = !flip ? q_path : q_wall;
auto &q_next = !flip ? q_wall : q_path;
while (!q.empty()) {
int ch = q.front().first, cw = q.front().second;
q.pop();
for (int k = 0; k < 2; ++k) {
int nh = ch + dh[k], nw = cw + dw[k];
if (nh < 0 || h <= nh || nw < 0 || w <= nw)
continue;
if ((!flip && m[nh][nw] == WALL) || (flip && m[nh][nw] == PATH)) {
if (dist[nh][nw] == NOT_VISITED) {
q_next.emplace(nh, nw);
dist[nh][nw] = cnt + 1;
}
continue;
}
if (dist[nh][nw] == NOT_VISITED) {
q.emplace(nh, nw);
dist[nh][nw] = cnt;
}
}
}
if (dist[h - 1][w - 1] == NOT_VISITED)
++cnt;
}
int main() {
int h, w;
cin >> h >> w;
maze m(h);
for (auto &x : m)
cin >> x;
int cnt = 0;
bool flip = false;
vector<vector<int>> dist(h, vector<int>(w, NOT_VISITED));
if (m[0][0] == WALL) {
cnt = 1;
flip = true;
}
dist[0][0] = cnt;
if (!flip)
q_path.emplace(0, 0);
else
q_wall.emplace(0, 0);
auto &goal = dist[h - 1][w - 1];
while (goal == NOT_VISITED) {
bfs(cnt, flip, m, dist);
flip = !flip;
}
cout << (goal + 1) / 2 << endl;
} | replace | 28 | 30 | 28 | 32 | TLE | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// 総数を1000000007(素数)で割った余り
const long long mod = 1e9 + 7;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair
#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)
ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }
bool mt[101][101];
int cnt[101][101];
int main() {
int h, w;
cin >> h >> w;
rep1(i, h) {
rep1(j, w) {
char m;
cin >> m;
if (m == '.')
mt[i][j] = true;
}
}
for (int i = 2; i <= w + h; i++) {
for (int ch = 1; ch < i; ch++) {
int cw = i - ch;
if (ch == 1 && cw == 1)
cnt[ch][cw] = mt[ch][cw] ? 0 : 1;
else if (ch == 1)
cnt[ch][cw] = (mt[ch][cw] == mt[ch][cw - 1]) ? cnt[ch][cw - 1]
: (cnt[ch][cw - 1] + 1);
else if (cw == 1)
cnt[ch][cw] = (mt[ch][cw] == mt[ch - 1][cw]) ? cnt[ch - 1][cw]
: (cnt[ch - 1][cw] + 1);
else {
int right = (mt[ch][cw] == mt[ch][cw - 1]) ? cnt[ch][cw - 1]
: (cnt[ch][cw - 1] + 1);
int height = (mt[ch][cw] == mt[ch - 1][cw]) ? cnt[ch - 1][cw]
: (cnt[ch - 1][cw] + 1);
cnt[ch][cw] = min(right, height);
}
}
}
int ans = (cnt[h][w] + 1) / 2;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// 総数を1000000007(素数)で割った余り
const long long mod = 1e9 + 7;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair
#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)
ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }
bool mt[101][101];
int cnt[101][101];
int main() {
int h, w;
cin >> h >> w;
rep1(i, h) {
rep1(j, w) {
char m;
cin >> m;
if (m == '.')
mt[i][j] = true;
}
}
for (int i = 2; i <= w + h; i++) {
for (int ch = 1; ch < i; ch++) {
int cw = i - ch;
if (cw > w || ch > h)
continue;
if (ch == 1 && cw == 1)
cnt[ch][cw] = mt[ch][cw] ? 0 : 1;
else if (ch == 1)
cnt[ch][cw] = (mt[ch][cw] == mt[ch][cw - 1]) ? cnt[ch][cw - 1]
: (cnt[ch][cw - 1] + 1);
else if (cw == 1)
cnt[ch][cw] = (mt[ch][cw] == mt[ch - 1][cw]) ? cnt[ch - 1][cw]
: (cnt[ch - 1][cw] + 1);
else {
int right = (mt[ch][cw] == mt[ch][cw - 1]) ? cnt[ch][cw - 1]
: (cnt[ch][cw - 1] + 1);
int height = (mt[ch][cw] == mt[ch - 1][cw]) ? cnt[ch - 1][cw]
: (cnt[ch - 1][cw] + 1);
cnt[ch][cw] = min(right, height);
}
}
}
int ans = (cnt[h][w] + 1) / 2;
cout << ans << endl;
return 0;
}
| insert | 57 | 57 | 57 | 59 | 0 | |
p02735 | C++ | Time Limit Exceeded | /*
ID: espr1t
TASK:
KEYWORDS:
*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
FILE *in = stdin;
FILE *out = stdout;
const int MAX = 101;
const int INF = 1000000001;
int n, m;
char a[MAX][MAX];
int dyn[MAX][MAX][2];
bool isOkay(int row, int col, int inv) {
return inv ? a[row][col] == '#' : a[row][col] == '.';
}
int recurse(int row, int col, int inv) {
if (row >= n)
return INF;
if (col >= m)
return INF;
if (row == n - 1 && col == m - 1)
return isOkay(row, col, inv) ? 0 : !inv;
int ans = INF, add = 0;
int ninv = inv;
if (!isOkay(row, col, inv))
ninv = !inv, add = !inv;
ans = min(ans, recurse(row + 1, col, ninv) + add);
ans = min(ans, recurse(row, col + 1, ninv) + add);
return dyn[row][col][inv] = ans;
}
int main(void) {
// in = fopen("A.in", "rt");
fscanf(in, "%d %d", &n, &m);
for (int row = 0; row < n; row++)
fscanf(in, "%s", a[row]);
memset(dyn, -1, sizeof(dyn));
fprintf(out, "%d\n", recurse(0, 0, 0));
return 0;
} | /*
ID: espr1t
TASK:
KEYWORDS:
*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
FILE *in = stdin;
FILE *out = stdout;
const int MAX = 101;
const int INF = 1000000001;
int n, m;
char a[MAX][MAX];
int dyn[MAX][MAX][2];
bool isOkay(int row, int col, int inv) {
return inv ? a[row][col] == '#' : a[row][col] == '.';
}
int recurse(int row, int col, int inv) {
if (row >= n)
return INF;
if (col >= m)
return INF;
if (row == n - 1 && col == m - 1)
return isOkay(row, col, inv) ? 0 : !inv;
if (dyn[row][col][inv] != -1)
return dyn[row][col][inv];
int ans = INF, add = 0;
int ninv = inv;
if (!isOkay(row, col, inv))
ninv = !inv, add = !inv;
ans = min(ans, recurse(row + 1, col, ninv) + add);
ans = min(ans, recurse(row, col + 1, ninv) + add);
return dyn[row][col][inv] = ans;
}
int main(void) {
// in = fopen("A.in", "rt");
fscanf(in, "%d %d", &n, &m);
for (int row = 0; row < n; row++)
fscanf(in, "%s", a[row]);
memset(dyn, -1, sizeof(dyn));
fprintf(out, "%d\n", recurse(0, 0, 0));
return 0;
} | insert | 34 | 34 | 34 | 36 | TLE | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define p_ary(ary, a, b) \
do { \
cout << "["; \
for (int count = (a); count < (b); ++count) \
cout << ary[count] << ((b)-1 == count ? "" : ", "); \
cout << "]\n"; \
} while (0)
#define p_map(map, it) \
do { \
cout << "{"; \
for (auto(it) = map.begin();; ++(it)) { \
if ((it) == map.end()) { \
cout << "}\n"; \
break; \
} else \
cout << "" << (it)->first << "=>" << (it)->second << ", "; \
} \
} while (0)
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
os << "(" << a.first << ", " << a.second << ")";
return os;
}
const char newl = '\n';
struct UnionFind {
private:
vector<int> par, depth;
public:
UnionFind(int n) {
par.resize(n);
depth = vector<int>(n, 0);
for (int i = 0; i < n; ++i)
par[i] = i;
}
int find(int x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (depth[x] < depth[y])
par[x] = y;
else {
par[y] = x;
if (depth[x] == depth[y])
depth[x]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
};
template <typename T>
void dijkstra(int s, vector<vector<pair<int, T>>> &edges, vector<T> &dist) {
priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;
dist[s] = 0;
que.push(make_pair(0, s));
while (!que.empty()) {
pair<ll, int> p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first)
continue;
for (pair<int, T> &e : edges[v]) {
if (dist[e.first] > dist[v] + e.second) {
dist[e.first] = dist[v] + e.second;
que.push(make_pair(dist[e.first], e.first));
}
}
}
}
int main() {
int INF = 1e6;
int h, w, d[2] = {0, 1};
cin >> h >> w;
int a = 0;
string s[100];
for (int i = 0; i < h; ++i)
cin >> s[i];
if (s[0][0] == '#')
a = 1;
vector<vector<int>> dist(h, vector<int>(w, INF));
dist[0][0] = 0;
priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> que;
que.push(make_pair(0, P(0, 0)));
while (!que.empty()) {
pair<int, P> f = que.top();
P t = f.second;
que.pop();
for (int i = 0; i < 2; ++i) {
int x = t.first + d[i], y = t.second + d[i ^ 1];
if (x >= h || y >= w)
continue;
int b = f.first + (s[t.first][t.second] == '.' && s[x][y] == '#');
if (dist[x][y] < b)
continue;
dist[x][y] = b;
que.push(make_pair(b, P(x, y)));
}
}
cout << dist[h - 1][w - 1] + a << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define p_ary(ary, a, b) \
do { \
cout << "["; \
for (int count = (a); count < (b); ++count) \
cout << ary[count] << ((b)-1 == count ? "" : ", "); \
cout << "]\n"; \
} while (0)
#define p_map(map, it) \
do { \
cout << "{"; \
for (auto(it) = map.begin();; ++(it)) { \
if ((it) == map.end()) { \
cout << "}\n"; \
break; \
} else \
cout << "" << (it)->first << "=>" << (it)->second << ", "; \
} \
} while (0)
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
os << "(" << a.first << ", " << a.second << ")";
return os;
}
const char newl = '\n';
struct UnionFind {
private:
vector<int> par, depth;
public:
UnionFind(int n) {
par.resize(n);
depth = vector<int>(n, 0);
for (int i = 0; i < n; ++i)
par[i] = i;
}
int find(int x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (depth[x] < depth[y])
par[x] = y;
else {
par[y] = x;
if (depth[x] == depth[y])
depth[x]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
};
template <typename T>
void dijkstra(int s, vector<vector<pair<int, T>>> &edges, vector<T> &dist) {
priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;
dist[s] = 0;
que.push(make_pair(0, s));
while (!que.empty()) {
pair<ll, int> p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first)
continue;
for (pair<int, T> &e : edges[v]) {
if (dist[e.first] > dist[v] + e.second) {
dist[e.first] = dist[v] + e.second;
que.push(make_pair(dist[e.first], e.first));
}
}
}
}
int main() {
int INF = 1e6;
int h, w, d[2] = {0, 1};
cin >> h >> w;
int a = 0;
string s[100];
for (int i = 0; i < h; ++i)
cin >> s[i];
if (s[0][0] == '#')
a = 1;
vector<vector<int>> dist(h, vector<int>(w, INF));
dist[0][0] = 0;
priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> que;
que.push(make_pair(0, P(0, 0)));
while (!que.empty()) {
pair<int, P> f = que.top();
P t = f.second;
que.pop();
for (int i = 0; i < 2; ++i) {
int x = t.first + d[i], y = t.second + d[i ^ 1];
if (x >= h || y >= w)
continue;
int b = f.first + (s[t.first][t.second] == '.' && s[x][y] == '#');
if (dist[x][y] > b) {
dist[x][y] = b;
que.push(make_pair(b, P(x, y)));
}
}
}
cout << dist[h - 1][w - 1] + a << endl;
} | replace | 112 | 116 | 112 | 116 | TLE | |
p02735 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
#define pause cout << " press ansy key to continue...", cin >> chh
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)
#define lowbit(x) ((x) & (-x))
#define repit(i, c) \
for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repe(i, u) for (int i = head[u]; i != -1; i = nxt[i])
#define repd(i, n) for (int i = (n - 1); i >= 0; i--)
#define FOR(i, n, m) for (int i = (n); i <= (m); i++)
#define FORD(i, n, m) for (int i = (n); i >= (m); i--)
#define pb push_back
#define X first
#define Y second
#define ins insert
#define rb rbegin
#define be begin
#define er erase
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define SZ(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
#define sqr(r) ((LL)(r) * (r))
#define dis(x1, y1, x2, y2) \
(((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)))
#define FASTIO \
ios::sync_with_stdio(false); \
cin.tie(0)
#define sc(x) cout << #x " = " << x << endl, system("pause")
#define sc2(x, y) \
cout << #x " = " << x << " " << #y " = " << y << endl, system("pause")
#define sc3(x, y, z) \
cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z \
<< endl, \
system("pause")
#define sc4(x, y, z, w) \
cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z << " " \
<< #w " = " << w << endl, \
system("pause")
#define in(n) scanf("%d", &n)
#define in2(n, m) scanf("%d %d", &n, &m)
#define in3(x, y, z) scanf("%d %d %d", &x, &y, &z)
using namespace std;
int chh;
typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<int, pii> pi3;
typedef vector<pair<int, int>> vpii;
typedef long long LL;
const int N = 105;
int n, m;
int dp[N][N], f[N][N];
char g[N][N];
int dx[2] = {0, 1};
int dy[2] = {1, 0};
void solve() {
queue<pii> q;
memset(dp, 127, sizeof(dp));
memset(f, 0, sizeof(f));
dp[0][0] = (g[0][0] != '.');
q.push(mp(0, 0));
while (!q.empty()) {
int x = q.front().X, y = q.front().Y;
q.pop(), f[x][y] = 0;
rep(k, 4) {
int u = x + dx[k], v = y + dy[k];
if (u < 0 || u == n || v < 0 || v == m)
continue;
int w = dp[x][y] + (g[x][y] == '.' && g[u][v] == '#');
if (dp[u][v] > w) {
dp[u][v] = w;
if (!f[u][v])
f[u][v] = 1, q.push(mp(u, v));
}
}
}
}
int main() {
while (~in2(n, m)) {
rep(i, n) scanf("%s", g[i]);
solve();
printf("%d\n", dp[n - 1][m - 1]);
}
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
#define pause cout << " press ansy key to continue...", cin >> chh
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)
#define lowbit(x) ((x) & (-x))
#define repit(i, c) \
for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repe(i, u) for (int i = head[u]; i != -1; i = nxt[i])
#define repd(i, n) for (int i = (n - 1); i >= 0; i--)
#define FOR(i, n, m) for (int i = (n); i <= (m); i++)
#define FORD(i, n, m) for (int i = (n); i >= (m); i--)
#define pb push_back
#define X first
#define Y second
#define ins insert
#define rb rbegin
#define be begin
#define er erase
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define SZ(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
#define sqr(r) ((LL)(r) * (r))
#define dis(x1, y1, x2, y2) \
(((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)))
#define FASTIO \
ios::sync_with_stdio(false); \
cin.tie(0)
#define sc(x) cout << #x " = " << x << endl, system("pause")
#define sc2(x, y) \
cout << #x " = " << x << " " << #y " = " << y << endl, system("pause")
#define sc3(x, y, z) \
cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z \
<< endl, \
system("pause")
#define sc4(x, y, z, w) \
cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z << " " \
<< #w " = " << w << endl, \
system("pause")
#define in(n) scanf("%d", &n)
#define in2(n, m) scanf("%d %d", &n, &m)
#define in3(x, y, z) scanf("%d %d %d", &x, &y, &z)
using namespace std;
int chh;
typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<int, pii> pi3;
typedef vector<pair<int, int>> vpii;
typedef long long LL;
const int N = 105;
int n, m;
int dp[N][N], f[N][N];
char g[N][N];
int dx[2] = {0, 1};
int dy[2] = {1, 0};
void solve() {
queue<pii> q;
memset(dp, 127, sizeof(dp));
memset(f, 0, sizeof(f));
dp[0][0] = (g[0][0] != '.');
q.push(mp(0, 0));
while (!q.empty()) {
int x = q.front().X, y = q.front().Y;
q.pop(), f[x][y] = 0;
rep(k, 2) {
int u = x + dx[k], v = y + dy[k];
if (u < 0 || u == n || v < 0 || v == m)
continue;
int w = dp[x][y] + (g[x][y] == '.' && g[u][v] == '#');
if (dp[u][v] > w) {
dp[u][v] = w;
if (!f[u][v])
f[u][v] = 1, q.push(mp(u, v));
}
}
}
}
int main() {
while (~in2(n, m)) {
rep(i, n) scanf("%s", g[i]);
solve();
printf("%d\n", dp[n - 1][m - 1]);
}
return 0;
}
| replace | 86 | 87 | 86 | 87 | -11 | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int h, w;
cin >> h >> w;
vector<vector<bool>> grid(h, vector<bool>(w));
vector<vector<vector<pair<ii, ii>>>> sol(h, vector<vector<pair<ii, ii>>>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
char tmp;
cin >> tmp;
grid[i][j] = tmp == '#';
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
bool equal = false;
pair<ii, ii> p1, p2;
if (i > 0 && j > 0) {
if (sol[i - 1][j].size() == sol[i][j - 1].size()) {
if (grid[i][j]) {
equal = true;
p1 = sol[i - 1][j].back();
p2 = sol[i][j - 1].back();
} else {
sol[i][j] = sol[i - 1][j];
}
} else if (sol[i - 1][j].size() > sol[i][j - 1].size()) {
sol[i][j] = sol[i][j - 1];
} else {
sol[i][j] = sol[i - 1][j];
}
} else if (j > 0) {
sol[i][j] = sol[i][j - 1];
} else if (i > 0) {
sol[i][j] = sol[i - 1][j];
}
if (grid[i][j]) {
if (equal) {
if ((p1.second.first == i - 1 && p1.second.second == j) ||
(p1.second.first == i && p1.second.second == j - 1)) {
sol[i][j] = sol[i - 1][j];
sol[i][j].back() = make_pair(p1.first, ii(i, j));
} else if ((p2.second.first - 1 == i && p2.second.second == j) ||
(p2.second.first == i && p2.second.second == j - 1)) {
sol[i][j] = sol[i][j - 1];
sol[i][j].back() = make_pair(p2.first, ii(i, j));
} else {
sol[i][j] = sol[i - 1][j];
sol[i][j].emplace_back(ii(i, j), ii(i, j));
}
equal = false;
} else if (sol[i][j].empty()) {
sol[i][j].emplace_back(ii(i, j), ii(i, j));
} else {
pair<ii, ii> p;
p = sol[i][j].back();
if ((p.second.first == i - 1 && p.second.second == j) ||
(p.second.first == i && p.second.second == j - 1))
sol[i][j].back() = make_pair(p.first, ii(i, j));
else
sol[i][j].emplace_back(ii(i, j), ii(i, j));
}
}
}
}
/*
for ( auto l : sol ) {
for ( auto v : l ) {
cout << v.size() << " ";
}
cout << "\n";
}
*/
cout << sol.back().back().size() << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int h, w;
cin >> h >> w;
vector<vector<bool>> grid(h, vector<bool>(w));
vector<vector<vector<pair<ii, ii>>>> sol(h, vector<vector<pair<ii, ii>>>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
char tmp;
cin >> tmp;
grid[i][j] = tmp == '#';
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
bool equal = false;
pair<ii, ii> p1, p2;
if (i > 0 && j > 0) {
if (sol[i - 1][j].size() == sol[i][j - 1].size()) {
if (grid[i][j] && !sol[i - 1][j].empty()) {
equal = true;
p1 = sol[i - 1][j].back();
p2 = sol[i][j - 1].back();
} else {
sol[i][j] = sol[i - 1][j];
}
} else if (sol[i - 1][j].size() > sol[i][j - 1].size()) {
sol[i][j] = sol[i][j - 1];
} else {
sol[i][j] = sol[i - 1][j];
}
} else if (j > 0) {
sol[i][j] = sol[i][j - 1];
} else if (i > 0) {
sol[i][j] = sol[i - 1][j];
}
if (grid[i][j]) {
if (equal) {
if ((p1.second.first == i - 1 && p1.second.second == j) ||
(p1.second.first == i && p1.second.second == j - 1)) {
sol[i][j] = sol[i - 1][j];
sol[i][j].back() = make_pair(p1.first, ii(i, j));
} else if ((p2.second.first - 1 == i && p2.second.second == j) ||
(p2.second.first == i && p2.second.second == j - 1)) {
sol[i][j] = sol[i][j - 1];
sol[i][j].back() = make_pair(p2.first, ii(i, j));
} else {
sol[i][j] = sol[i - 1][j];
sol[i][j].emplace_back(ii(i, j), ii(i, j));
}
equal = false;
} else if (sol[i][j].empty()) {
sol[i][j].emplace_back(ii(i, j), ii(i, j));
} else {
pair<ii, ii> p;
p = sol[i][j].back();
if ((p.second.first == i - 1 && p.second.second == j) ||
(p.second.first == i && p.second.second == j - 1))
sol[i][j].back() = make_pair(p.first, ii(i, j));
else
sol[i][j].emplace_back(ii(i, j), ii(i, j));
}
}
}
}
/*
for ( auto l : sol ) {
for ( auto v : l ) {
cout << v.size() << " ";
}
cout << "\n";
}
*/
cout << sol.back().back().size() << "\n";
return 0;
}
| replace | 33 | 34 | 33 | 34 | 0 | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int MD = (int)1e9 + 7;
typedef vector<ll> VL;
void dbg() { cerr << "\n"; }
template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) {
cerr << fst << ": ";
dbg(rst...);
}
template <class T, class T2> void amin(T &a, T2 b) {
if (a > b)
a = b;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int h, w;
;
cin >> h >> w;
vector<string> ba(h);
rep(i, h) cin >> ba[i];
vector<VI> dp(h + 2, VI(w + 2, MD));
dp[0][0] = (ba[0][0] == '#');
rep(i, h) rep(j, w) {
amin(dp[i + 1][j], dp[i][j] + (ba[i][j] != ba[i + 1][j]));
amin(dp[i][j + 1], dp[i][j] + (ba[i][j] != ba[i][j + 1]));
}
// for(auto xl: dp){for(auto x :xl) cerr<<x<<" ";dbg();}
int ans = (dp[h - 1][w - 1] + 1) / 2;
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int MD = (int)1e9 + 7;
typedef vector<ll> VL;
void dbg() { cerr << "\n"; }
template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) {
cerr << fst << ": ";
dbg(rst...);
}
template <class T, class T2> void amin(T &a, T2 b) {
if (a > b)
a = b;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int h, w;
;
cin >> h >> w;
vector<string> ba(h);
rep(i, h) cin >> ba[i];
vector<VI> dp(h + 2, VI(w + 2, MD));
dp[0][0] = (ba[0][0] == '#');
rep(i, h) rep(j, w) {
if (i)
amin(dp[i][j], dp[i - 1][j] + (ba[i][j] != ba[i - 1][j]));
if (j)
amin(dp[i][j], dp[i][j - 1] + (ba[i][j] != ba[i][j - 1]));
}
// for(auto xl: dp){for(auto x :xl) cerr<<x<<" ";dbg();}
int ans = (dp[h - 1][w - 1] + 1) / 2;
cout << ans << "\n";
return 0;
}
| replace | 35 | 37 | 35 | 39 | -11 | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = m; i < (n); ++i)
using namespace std;
using ll = long long;
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> wh(h, vector<char>(w));
vector<vector<int>> dp(h, vector<int>(w));
rep(i, h) {
rep(j, w) { cin >> wh.at(i).at(j); }
}
if (wh[0][0] == '.') {
dp[0][0] = 0;
} else {
dp[0][0] = 1;
}
repp(k, w, 1) {
if (wh[0][k] == '.') {
dp[0][k] = dp[0][k - 1];
} else if (wh[0][k] == '#' && wh[0][k - 1] == '.') {
dp[0][k] = dp[0][k - 1] + 1;
} else {
dp[0][k] = dp[0][k - 1];
}
}
repp(l, h, 1) {
if (wh[l][0] == '.') {
dp[l][0] = dp[l - 1][0];
} else if (wh[l][0] == '#' && wh[l - 1][0] == '.') {
dp[l][0] = dp[l - 1][0] + 1;
} else {
dp[l][0] = dp[l - 1][0];
}
}
repp(a, w, 1) {
repp(s, h, 1) {
if (wh[a][s] == '.') {
dp[a][s] = min(dp[a - 1][s], dp[a][s - 1]);
} else {
if (wh[a - 1][s] == '#' && wh[a][s - 1] == '#') {
dp[a][s] = min(dp[a - 1][s], dp[a][s - 1]);
} else if (wh[a - 1][s] == '#' && wh[a][s - 1] == '.') {
dp[a][s] = min(dp[a - 1][s], dp[a][s - 1] + 1);
} else if (wh[a - 1][s] == '.' && wh[a][s - 1] == '#') {
dp[a][s] = min(dp[a - 1][s] + 1, dp[a][s - 1]);
} else {
dp[a][s] = min(dp[a - 1][s] + 1, dp[a][s - 1] + 1);
}
}
}
}
cout << dp[h - 1][w - 1] << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = m; i < (n); ++i)
using namespace std;
using ll = long long;
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> wh(h, vector<char>(w));
vector<vector<int>> dp(h, vector<int>(w));
rep(i, h) {
rep(j, w) { cin >> wh.at(i).at(j); }
}
if (wh[0][0] == '.') {
dp[0][0] = 0;
} else {
dp[0][0] = 1;
}
repp(k, w, 1) {
if (wh[0][k] == '.') {
dp[0][k] = dp[0][k - 1];
} else if (wh[0][k] == '#' && wh[0][k - 1] == '.') {
dp[0][k] = dp[0][k - 1] + 1;
} else {
dp[0][k] = dp[0][k - 1];
}
}
repp(l, h, 1) {
if (wh[l][0] == '.') {
dp[l][0] = dp[l - 1][0];
} else if (wh[l][0] == '#' && wh[l - 1][0] == '.') {
dp[l][0] = dp[l - 1][0] + 1;
} else {
dp[l][0] = dp[l - 1][0];
}
}
repp(a, h, 1) {
repp(s, w, 1) {
if (wh[a][s] == '.') {
dp[a][s] = min(dp[a - 1][s], dp[a][s - 1]);
} else {
if (wh[a - 1][s] == '#' && wh[a][s - 1] == '#') {
dp[a][s] = min(dp[a - 1][s], dp[a][s - 1]);
} else if (wh[a - 1][s] == '#' && wh[a][s - 1] == '.') {
dp[a][s] = min(dp[a - 1][s], dp[a][s - 1] + 1);
} else if (wh[a - 1][s] == '.' && wh[a][s - 1] == '#') {
dp[a][s] = min(dp[a - 1][s] + 1, dp[a][s - 1]);
} else {
dp[a][s] = min(dp[a - 1][s] + 1, dp[a][s - 1] + 1);
}
}
}
}
cout << dp[h - 1][w - 1] << endl;
} | replace | 37 | 39 | 37 | 39 | 0 | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;
const ll dx[] = {1, 0};
const ll dy[] = {0, 1};
int main() {
ll h, w;
cin >> h >> w;
vector<string> s(h);
for (auto &&e : s) {
cin >> e;
}
vector<vector<ll>> dp(h, vector<ll>(w, INF));
dp[0][0] = s[0][0] == '#' ? 1 : 0;
queue<pair<ll, ll>> nxt;
nxt.push({0, 0});
while (!nxt.empty()) {
ll x = nxt.front().first;
ll y = nxt.front().second;
nxt.pop();
for (int i = 0; i < 2; ++i) {
ll nx = x + dx[i];
ll ny = y + dy[i];
if (nx >= h || ny >= w)
continue;
dp[nx][ny] = min(dp[nx][ny], dp[x][y] + ((s[nx][ny] == '#') ? 1 : 0));
nxt.push({nx, ny});
}
}
// for (int i = 0; i < h; ++i)
// {
// for (int j = 0; j < w; ++j)
// {
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
cout << dp[h - 1][w - 1] << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;
const ll dx[] = {1, 0};
const ll dy[] = {0, 1};
int main() {
ll h, w;
cin >> h >> w;
vector<string> s(h);
for (auto &&e : s) {
cin >> e;
}
vector<vector<ll>> dp(h, vector<ll>(w, INF));
dp[0][0] = s[0][0] == '#' ? 1 : 0;
queue<pair<ll, ll>> nxt;
nxt.push({0, 0});
while (!nxt.empty()) {
ll x = nxt.front().first;
ll y = nxt.front().second;
nxt.pop();
for (int i = 0; i < 2; ++i) {
ll nx = x + dx[i];
ll ny = y + dy[i];
if (nx >= h || ny >= w)
continue;
if (dp[nx][ny] >
dp[x][y] + ((s[x][y] == '.' && s[nx][ny] == '#') ? 1 : 0)) {
dp[nx][ny] = dp[x][y] + ((s[x][y] == '.' && s[nx][ny] == '#') ? 1 : 0);
nxt.push({nx, ny});
}
}
}
// for (int i = 0; i < h; ++i)
// {
// for (int j = 0; j < w; ++j)
// {
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
cout << dp[h - 1][w - 1] << endl;
} | replace | 28 | 30 | 28 | 33 | TLE | |
p02735 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
bool CanGo(int y, int x, int R, int C) {
return y >= 0 && y < R && x >= 0 && x < C;
}
int ToBlock(vector<vector<char>> &Map, int y1, int x1, int y2, int x2) {
return Map[y1][x1] == '.' && Map[y2][x2] == '#' ? -1 : 0;
}
int TraceMaze(priority_queue<vector<int>> &Queue, int gy, int gx,
vector<vector<char>> &Map) {
vector<int> nowstate = Queue.top();
Queue.pop();
int R = Map.size();
int C = Map[0].size();
int step = nowstate[0];
int ny = nowstate[1];
int nx = nowstate[2];
if (ny == gy && nx == gx)
return -step;
if (Map[ny][nx] == '#' || Map[ny][nx] == '.') {
if (CanGo(ny + 1, nx, R, C))
Queue.push({step + ToBlock(Map, ny, nx, ny + 1, nx), ny + 1, nx});
if (CanGo(ny, nx + 1, R, C))
Queue.push({step + ToBlock(Map, ny, nx, ny, nx + 1), ny, nx + 1});
}
return -1;
}
int main() {
int H, W;
cin >> H >> W;
vector<vector<char>> Map(H, vector<char>(W, 0));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> Map[i][j];
}
}
int step = Map[0][0] == '#' ? -1 : 0;
priority_queue<vector<int>> Queue;
Queue.push({step, 0, 0});
int ans = -1;
while (!Queue.empty() && ans < 0)
ans = TraceMaze(Queue, H - 1, W - 1, Map);
cout << ans << endl;
}
| #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
bool CanGo(int y, int x, int R, int C) {
return y >= 0 && y < R && x >= 0 && x < C;
}
int ToBlock(vector<vector<char>> &Map, int y1, int x1, int y2, int x2) {
return Map[y1][x1] == '.' && Map[y2][x2] == '#' ? -1 : 0;
}
int TraceMaze(priority_queue<vector<int>> &Queue, int gy, int gx,
vector<vector<char>> &Map) {
vector<int> nowstate = Queue.top();
Queue.pop();
int R = Map.size();
int C = Map[0].size();
int step = nowstate[0];
int ny = nowstate[1];
int nx = nowstate[2];
if (ny == gy && nx == gx)
return -step;
if (Map[ny][nx] == '#' || Map[ny][nx] == '.') {
if (CanGo(ny + 1, nx, R, C))
Queue.push({step + ToBlock(Map, ny, nx, ny + 1, nx), ny + 1, nx});
if (CanGo(ny, nx + 1, R, C))
Queue.push({step + ToBlock(Map, ny, nx, ny, nx + 1), ny, nx + 1});
Map[ny][nx] = ' ';
}
return -1;
}
int main() {
int H, W;
cin >> H >> W;
vector<vector<char>> Map(H, vector<char>(W, 0));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> Map[i][j];
}
}
int step = Map[0][0] == '#' ? -1 : 0;
priority_queue<vector<int>> Queue;
Queue.push({step, 0, 0});
int ans = -1;
while (!Queue.empty() && ans < 0)
ans = TraceMaze(Queue, H - 1, W - 1, Map);
cout << ans << endl;
}
| insert | 32 | 32 | 32 | 33 | TLE | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define li long long int
#define rep(i, to) for (li i = 0; i < ((li)(to)); i++)
#define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++)
#define pb push_back
#define sz(v) ((li)(v).size())
#define bgn(v) ((v).begin())
#define eend(v) ((v).end())
#define allof(v) (v).begin(), (v).end()
#define dodp(v, n) memset(v, (li)n, sizeof(v))
#define bit(n) (1ll << (li)(n))
#define mp(a, b) make_pair(a, b)
#define rin rep(i, n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;
#define INF bit(60)
#define DBGP 1
#define idp if (DBGP)
#define F first
#define S second
#define p2(a, b) idp cout << a << "\t" << b << endl
#define p3(a, b, c) idp cout << a << "\t" << b << "\t" << c << endl
#define p4(a, b, c, d) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << endl
#define p5(a, b, c, d, e) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << endl
#define p6(a, b, c, d, e, f) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << endl
#define p7(a, b, c, d, e, f, g) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << endl
#define p8(a, b, c, d, e, f, g, h) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << endl
#define p9(a, b, c, d, e, f, g, h, i) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << endl
#define p10(a, b, c, d, e, f, g, h, i, j) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << "\t" << j << endl
#define foreach(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define p2p(x) idp p2((x).F, (x).S)
#define dump(x, n) \
idp { \
rep(i, n) { cout << x[i] << " "; } \
puts(""); \
}
#define dump2(x, n) \
idp { \
rep(i, n) { cout << "[" << x[i].F << " , " << x[i].S << "] "; } \
puts(""); \
}
#define dumpi(x) \
idp { \
foreach (it, x) { \
cout << (*it) << " "; \
} \
puts(""); \
}
#define dumpi2(x) \
idp { \
foreach (it, x) { \
cout << "[" << (it)->F << " , " << (it)->S << "] "; \
} \
puts(""); \
}
#define read2d(a, w, h) rep(i, h) rep(j, w) cin >> a[i][j]
#define dump2d(a, w, h) \
rep(i, h) { \
rep(j, w) cout << a[i][j] << " "; \
puts(""); \
}
typedef pair<li, li> PI;
li cost[111][111];
li h, w;
li dx[] = {1, 0};
li dy[] = {0, 1};
string board[111];
inline li dijkstra() {
priority_queue<pair<li, PI>> pq;
pq.push({0, {0, 0}});
while (!pq.empty()) {
PI now = pq.top().S;
li now_cost = -pq.top().F;
pq.pop();
if (now_cost >= cost[now.F][now.S]) {
continue;
}
cost[now.F][now.S] = now_cost;
if (now.F == h - 1 && now.S == w - 1) {
return now_cost;
}
rep(k, 4) {
PI next = {now.F + dy[k], now.S + dx[k]};
if (min(next.F, next.S) < 0 || next.F >= h || next.S >= w) {
continue;
}
if (board[next.F][next.S] != board[now.F][now.S]) {
pq.emplace(-now_cost - 1, next);
} else {
pq.emplace(-now_cost, next);
}
}
}
return INF;
}
int main() {
cin >> h >> w;
rep(i, h) { cin >> board[i]; }
rep(i, h) {
rep(j, w) { cost[i][j] = INF; }
}
li res = dijkstra();
if (res % 2 == 0) {
res /= 2;
if (board[0][0] == '#') {
++res;
}
} else {
res = (res + 1) / 2;
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define li long long int
#define rep(i, to) for (li i = 0; i < ((li)(to)); i++)
#define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++)
#define pb push_back
#define sz(v) ((li)(v).size())
#define bgn(v) ((v).begin())
#define eend(v) ((v).end())
#define allof(v) (v).begin(), (v).end()
#define dodp(v, n) memset(v, (li)n, sizeof(v))
#define bit(n) (1ll << (li)(n))
#define mp(a, b) make_pair(a, b)
#define rin rep(i, n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;
#define INF bit(60)
#define DBGP 1
#define idp if (DBGP)
#define F first
#define S second
#define p2(a, b) idp cout << a << "\t" << b << endl
#define p3(a, b, c) idp cout << a << "\t" << b << "\t" << c << endl
#define p4(a, b, c, d) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << endl
#define p5(a, b, c, d, e) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << endl
#define p6(a, b, c, d, e, f) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << endl
#define p7(a, b, c, d, e, f, g) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << endl
#define p8(a, b, c, d, e, f, g, h) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << endl
#define p9(a, b, c, d, e, f, g, h, i) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << endl
#define p10(a, b, c, d, e, f, g, h, i, j) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << "\t" << j << endl
#define foreach(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define p2p(x) idp p2((x).F, (x).S)
#define dump(x, n) \
idp { \
rep(i, n) { cout << x[i] << " "; } \
puts(""); \
}
#define dump2(x, n) \
idp { \
rep(i, n) { cout << "[" << x[i].F << " , " << x[i].S << "] "; } \
puts(""); \
}
#define dumpi(x) \
idp { \
foreach (it, x) { \
cout << (*it) << " "; \
} \
puts(""); \
}
#define dumpi2(x) \
idp { \
foreach (it, x) { \
cout << "[" << (it)->F << " , " << (it)->S << "] "; \
} \
puts(""); \
}
#define read2d(a, w, h) rep(i, h) rep(j, w) cin >> a[i][j]
#define dump2d(a, w, h) \
rep(i, h) { \
rep(j, w) cout << a[i][j] << " "; \
puts(""); \
}
typedef pair<li, li> PI;
li cost[111][111];
li h, w;
li dx[] = {1, 0};
li dy[] = {0, 1};
string board[111];
inline li dijkstra() {
priority_queue<pair<li, PI>> pq;
pq.push({0, {0, 0}});
while (!pq.empty()) {
PI now = pq.top().S;
li now_cost = -pq.top().F;
pq.pop();
if (now_cost >= cost[now.F][now.S]) {
continue;
}
cost[now.F][now.S] = now_cost;
if (now.F == h - 1 && now.S == w - 1) {
return now_cost;
}
rep(k, 2) {
PI next = {now.F + dy[k], now.S + dx[k]};
if (min(next.F, next.S) < 0 || next.F >= h || next.S >= w) {
continue;
}
if (board[next.F][next.S] != board[now.F][now.S]) {
pq.emplace(-now_cost - 1, next);
} else {
pq.emplace(-now_cost, next);
}
}
}
return INF;
}
int main() {
cin >> h >> w;
rep(i, h) { cin >> board[i]; }
rep(i, h) {
rep(j, w) { cost[i][j] = INF; }
}
li res = dijkstra();
if (res % 2 == 0) {
res /= 2;
if (board[0][0] == '#') {
++res;
}
} else {
res = (res + 1) / 2;
}
cout << res << endl;
return 0;
} | replace | 107 | 108 | 107 | 108 | 0 | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define DUMP(x) std::cerr << (#x) << " = " << (x) << "\n"
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define EREP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define RREP(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) REP(i, 0, n)
#define erep(i, n) EREP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)
#define ALL(r) r.begin(), r.end()
#define YES cout << "YES\n"
#define Yes cout << "Yes\n"
#define NO cout << "NO\n"
#define No cout << "No\n"
#define IMP cout << "IMPOSSIBLE\n"
#define Imp cout << "Impossible\n"
#define imp cout << "impossible\n"
#define M1 cout << "-1\n"
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
os << "{";
rep(i, v.size()) os << v[i] << (i == (int)v.size() - 1 ? "" : ", ");
os << "}";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &p) {
return (os << "(" << p.first << ", " << p.second << ")");
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &m) {
bool first = true;
os << "{";
for (const auto e : m) {
if (!first)
os << ", ";
os << "{" << e.first << ": " << e.second << "}";
first = false;
}
os << "}";
return os;
}
template <typename T> T dup(T x, T y) { return (x + y - 1) / y; };
template <typename A, size_t N, typename T>
inline void arrayFill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
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;
}
struct in {
const size_t n = 0;
in() = default;
in(size_t n) : n(n){};
template <typename T> operator T() {
T ret;
cin >> ret;
return ret;
}
template <typename T> operator vector<T>() {
assert(n != 0);
vector<T> ret(n);
for (T &x : ret) {
T tmp = in();
x = tmp;
}
return ret;
}
template <typename T, typename U> operator pair<T, U>() {
pair<T, U> ret;
ret.first = in();
ret.second = in();
return ret;
}
};
using ll = int64_t;
using vint = vector<int32_t>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pint = pair<int32_t, int32_t>;
using vpint = vector<pint>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using setint = set<int32_t>;
using setstr = set<string>;
using qint = queue<int32_t>;
using qpint = queue<pint>;
constexpr std::int32_t INF = 1001001001;
constexpr std::int64_t LINF = 1001001001001001001;
void Main() {
int h = in(), w = in();
vstr s = in(h);
vvint dp(h + 2, vint(w + 2, INF));
dp[0][0] = (s[0][0] == '#');
rep(i, h) rep(j, w) {
if (s[i][j] == '.') {
if (i + 1 < h) {
if (s[i + 1][j] == '.') {
chmin(dp[i + 1][j], dp[i][j]);
} else {
chmin(dp[i + 1][j], dp[i][j] + 1);
}
}
if (j + 1 < w) {
if (s[i][j + 1] == '.') {
chmin(dp[i][j + 1], dp[i][j]);
} else {
chmin(dp[i][j + 1], dp[i][j] + 1);
}
}
} else {
if (i + 1 < h) {
if (s[i + 1][j] == '.') {
chmin(dp[i + 1][j], dp[i][j]);
} else {
chmin(dp[i + 1][j], dp[i][j]);
}
}
if (j + 1 < w) {
if (s[i + 1][j] == '.') {
chmin(dp[i][j + 1], dp[i][j]);
} else {
chmin(dp[i][j + 1], dp[i][j]);
}
}
}
}
cout << dp[h - 1][w - 1] << endl;
}
signed main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define DUMP(x) std::cerr << (#x) << " = " << (x) << "\n"
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define EREP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define RREP(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) REP(i, 0, n)
#define erep(i, n) EREP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)
#define ALL(r) r.begin(), r.end()
#define YES cout << "YES\n"
#define Yes cout << "Yes\n"
#define NO cout << "NO\n"
#define No cout << "No\n"
#define IMP cout << "IMPOSSIBLE\n"
#define Imp cout << "Impossible\n"
#define imp cout << "impossible\n"
#define M1 cout << "-1\n"
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
os << "{";
rep(i, v.size()) os << v[i] << (i == (int)v.size() - 1 ? "" : ", ");
os << "}";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &p) {
return (os << "(" << p.first << ", " << p.second << ")");
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &m) {
bool first = true;
os << "{";
for (const auto e : m) {
if (!first)
os << ", ";
os << "{" << e.first << ": " << e.second << "}";
first = false;
}
os << "}";
return os;
}
template <typename T> T dup(T x, T y) { return (x + y - 1) / y; };
template <typename A, size_t N, typename T>
inline void arrayFill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
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;
}
struct in {
const size_t n = 0;
in() = default;
in(size_t n) : n(n){};
template <typename T> operator T() {
T ret;
cin >> ret;
return ret;
}
template <typename T> operator vector<T>() {
assert(n != 0);
vector<T> ret(n);
for (T &x : ret) {
T tmp = in();
x = tmp;
}
return ret;
}
template <typename T, typename U> operator pair<T, U>() {
pair<T, U> ret;
ret.first = in();
ret.second = in();
return ret;
}
};
using ll = int64_t;
using vint = vector<int32_t>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pint = pair<int32_t, int32_t>;
using vpint = vector<pint>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using setint = set<int32_t>;
using setstr = set<string>;
using qint = queue<int32_t>;
using qpint = queue<pint>;
constexpr std::int32_t INF = 1001001001;
constexpr std::int64_t LINF = 1001001001001001001;
void Main() {
int h = in(), w = in();
vstr s = in(h);
vvint dp(h + 2, vint(w + 2, INF));
dp[0][0] = (s[0][0] == '#');
rep(i, h) rep(j, w) {
if (s[i][j] == '.') {
if (i + 1 < h) {
if (s[i + 1][j] == '.') {
chmin(dp[i + 1][j], dp[i][j]);
} else {
chmin(dp[i + 1][j], dp[i][j] + 1);
}
}
if (j + 1 < w) {
if (s[i][j + 1] == '.') {
chmin(dp[i][j + 1], dp[i][j]);
} else {
chmin(dp[i][j + 1], dp[i][j] + 1);
}
}
} else {
if (i + 1 < h) {
if (s[i + 1][j] == '.') {
chmin(dp[i + 1][j], dp[i][j]);
} else {
chmin(dp[i + 1][j], dp[i][j]);
}
}
if (j + 1 < w) {
if (s[i][j + 1] == '.') {
chmin(dp[i][j + 1], dp[i][j]);
} else {
chmin(dp[i][j + 1], dp[i][j]);
}
}
}
}
cout << dp[h - 1][w - 1] << endl;
}
signed main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
}
| replace | 144 | 145 | 144 | 145 | -11 | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <time.h>
using namespace std;
typedef long long ll;
typedef pair<ll, pair<ll, ll>> ipair;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define f(i, n) for (int i = 0; i < n; i++)
#define F(i, a, b) for (int i = a; i <= b; i++)
//-------------------------CODE BEGINS HERE-------------------------------//
// clock_t tStart = clock();
ll mod = 998244353;
ll po(ll b, ll p) {
if (p == 0)
return 1LL;
if (p == 1)
return b;
ll t = po(b, p / 2);
t = (t * t) % mod;
return p % 2 ? (t * b) % mod : t;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int h, w;
cin >> h >> w;
char mat[h][w];
f(i, h) {
f(j, w) { cin >> mat[i][j]; }
}
int dp[h][w];
dp[0][0] = 0;
F(i, 1, h - 1) {
if (mat[i][0] != mat[i - 1][0])
dp[i][0] = dp[i - 1][0] + 1;
else
dp[i][0] = dp[i - 1][0];
}
F(i, 1, w - 1) {
if (mat[0][i] != mat[0][i - 1])
dp[0][i] = dp[0][i - 1] + 1;
else
dp[0][i] = dp[0][i - 1];
}
F(i, 1, h - 1) {
F(j, 1, w - 1) {
int p = INT_MAX;
if (mat[i][j - 1] == mat[i][j]) {
p = MIN(p, dp[i][j - 1]);
} else {
p = MIN(p, dp[i][j - 1] + 1);
}
if (mat[i - 1][j] == mat[i][j]) {
p = MIN(p, dp[i - 1][j]);
} else {
p = MIN(p, dp[i - 1][j] + 1);
}
dp[i][j] = p;
}
}
// f(i,h){
// f(j,w){
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
if (mat[0][0] == '.') {
cout << (dp[h - 1][w - 1] + 1) / 2 << endl;
} else {
cout << (dp[h - 1][w - 1]) / 2 + 1 << endl;
}
// printf("Time taken: %.2fs\n",(double)(clock()-tStart)/CLOCKS_PER_SEC);
return 0;
} | #include <bits/stdc++.h>
#include <time.h>
using namespace std;
typedef long long ll;
typedef pair<ll, pair<ll, ll>> ipair;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define f(i, n) for (int i = 0; i < n; i++)
#define F(i, a, b) for (int i = a; i <= b; i++)
//-------------------------CODE BEGINS HERE-------------------------------//
// clock_t tStart = clock();
ll mod = 998244353;
ll po(ll b, ll p) {
if (p == 0)
return 1LL;
if (p == 1)
return b;
ll t = po(b, p / 2);
t = (t * t) % mod;
return p % 2 ? (t * b) % mod : t;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int h, w;
cin >> h >> w;
char mat[h][w];
f(i, h) {
f(j, w) { cin >> mat[i][j]; }
}
int dp[h][w];
dp[0][0] = 0;
F(i, 1, h - 1) {
if (mat[i][0] != mat[i - 1][0])
dp[i][0] = dp[i - 1][0] + 1;
else
dp[i][0] = dp[i - 1][0];
}
F(i, 1, w - 1) {
if (mat[0][i] != mat[0][i - 1])
dp[0][i] = dp[0][i - 1] + 1;
else
dp[0][i] = dp[0][i - 1];
}
F(i, 1, h - 1) {
F(j, 1, w - 1) {
int p = INT_MAX;
if (mat[i][j - 1] == mat[i][j]) {
p = MIN(p, dp[i][j - 1]);
} else {
p = MIN(p, dp[i][j - 1] + 1);
}
if (mat[i - 1][j] == mat[i][j]) {
p = MIN(p, dp[i - 1][j]);
} else {
p = MIN(p, dp[i - 1][j] + 1);
}
dp[i][j] = p;
}
}
// f(i,h){
// f(j,w){
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
if (mat[0][0] == '.') {
cout << (dp[h - 1][w - 1] + 1) / 2 << endl;
} else {
cout << (dp[h - 1][w - 1]) / 2 + 1 << endl;
}
// printf("Time taken: %.2fs\n",(double)(clock()-tStart)/CLOCKS_PER_SEC);
return 0;
} | replace | 33 | 37 | 33 | 34 | -11 | |
p02735 | C++ | Time Limit Exceeded | // #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb(i) push_back(i)
#define pp() pop_back()
#define f first
#define s second
#define mk(l, r) make_pair(l, r)
#define all(vc) vc.begin(), vc.end()
#define bg(vc) vc.begin()
#define ed(vc) vc.end()
#define w(t) while (t--)
const ll MAX = 3e5 + 5;
const ll INF = 2e18;
int h, w;
int ans = INT_MAX;
void solve(string s[], int i, int j, int count) {
if (i == h - 1 && j == w - 1) {
ans = min(count, ans);
return;
}
if (i < h - 1) {
if (s[i + 1][j] == '#')
solve(s, i + 1, j, count + 1);
else
solve(s, i + 1, j, count);
}
if (j < w - 1) {
if (s[i][j + 1] == '#')
solve(s, i, j + 1, count + 1);
else
solve(s, i, j + 1, count);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
t = 1;
// cin>>t;
w(t) {
cin >> h >> w;
string s[h];
for (int i = 0; i < h; i++)
cin >> s[i];
solve(s, 0, 0, 0);
if (s[0][0] == '#')
ans++;
cout << ans << endl;
}
}
/*ll msb(ll x)
{
ll k=(ll)log2(x);
return (ll)(pow(2,k));
}
#define gc getchar_unlocked
void scanint(int &x)
{
register int c = gc();
x = 0;
int neg = 0;
for(;((c<48 || c>57) && c != '-');c = gc());
if(c=='-') {neg=1;c=gc();}
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg) x=-x;
}*/ | // #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb(i) push_back(i)
#define pp() pop_back()
#define f first
#define s second
#define mk(l, r) make_pair(l, r)
#define all(vc) vc.begin(), vc.end()
#define bg(vc) vc.begin()
#define ed(vc) vc.end()
#define w(t) while (t--)
const ll MAX = 3e5 + 5;
const ll INF = 2e18;
int h, w;
int ans = INT_MAX;
void solve(string s[], int i, int j, int count) {
if (i == h - 1 && j == w - 1) {
ans = min(count, ans);
return;
}
if (i < h - 1) {
if (s[i + 1][j] == '#')
solve(s, i + 1, j, count + 1);
else
solve(s, i + 1, j, count);
}
if (j < w - 1) {
if (s[i][j + 1] == '#')
solve(s, i, j + 1, count + 1);
else
solve(s, i, j + 1, count);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
t = 1;
// cin>>t;
w(t) {
cin >> h >> w;
string s[h];
for (int i = 0; i < h; i++)
cin >> s[i];
int dp[h][w];
memset(dp, 0, sizeof dp);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
int count1 = 0, count2 = 0;
int acth = 0, actw = 0;
if (s[i][j] == '#')
count1 = 1, count2 = 1;
if (i == 0 && j == 0)
dp[i][j] = count1;
else if (i - 1 >= 0 && j - 1 >= 0) {
if (s[i - 1][j] == '#')
count1 = 0;
if (s[i][j - 1] == '#')
count2 = 0;
dp[i][j] = min(dp[i - 1][j] + count1, dp[i][j - 1] + count2);
} else if (i - 1 >= 0) {
if (s[i - 1][j] == '#')
count1 = 0;
dp[i][j] = dp[i - 1][j] + count1;
} else if (j - 1 >= 0) {
if (s[i][j - 1] == '#')
count2 = 0;
dp[i][j] = dp[i][j - 1] + count2;
}
}
cout << dp[h - 1][w - 1] << endl;
}
}
/*ll msb(ll x)
{
ll k=(ll)log2(x);
return (ll)(pow(2,k));
}
#define gc getchar_unlocked
void scanint(int &x)
{
register int c = gc();
x = 0;
int neg = 0;
for(;((c<48 || c>57) && c != '-');c = gc());
if(c=='-') {neg=1;c=gc();}
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg) x=-x;
}*/ | replace | 48 | 52 | 48 | 77 | TLE | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define SZ(v) int((v).size())
#define ALL(vec) begin(vec), end(vec)
typedef long long i64;
template <typename T> inline bool uax(T &x, T y) {
return (y > x) ? x = y, true : false;
}
template <typename T> inline bool uin(T &x, T y) {
return (y < x) ? x = y, true : false;
}
#ifdef Rahul
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
#else
#define error(...) 42;
#endif
string to_string(char c) { return "'" + string(1, c) + "'"; }
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ": " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
bool f = false;
string r = "{";
for (auto x : v) {
if (f)
r += ", ";
r += to_string(x);
f = true;
}
return r += "}";
}
template <typename A> string to_string(vector<vector<A>> v) {
string r;
for (auto x : v)
r += "\n" + to_string(x);
return r;
}
int Nerr;
template <typename A> string to_string(A *p) {
return to_string(vector<A>(p, p + Nerr));
}
void err(istream_iterator<string>) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " =: " << to_string(a) << "; ";
err(++it, args...);
}
template <typename T> void kek(T ans) {
cout << ans << endl;
exit(0);
}
#define Lu(...) [&](auto &&u) { return __VA_ARGS__; }
#define Luv(...) [&](auto &&u, auto &&v) { return __VA_ARGS__; }
int const MOD = 1e9 + 7;
long long const INF = 1e18 + 42;
/***********************************************************************/
const int N = 128;
int a[N][N];
int b[N][N];
int n, m;
void dfs(int x, int y, int t, int val) {
if (x > n or y > m or b[x][y] != t)
return;
uin(a[x][y], val);
dfs(x + 1, y, t, val);
dfs(x, y + 1, t, val);
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
fill_n(a[0], N * N, MOD);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
string s;
cin >> s;
for (int j = 1; j <= m; ++j) {
b[i][j] = s[j - 1] == '#';
}
}
a[1][0] = a[0][1] = 0;
for (int x = 1; x <= n; ++x) {
for (int y = 1; y <= m; ++y) {
dfs(x, y, b[x][y], b[x][y] + min(a[x - 1][y], a[x][y - 1]));
}
}
kek(a[n][m]);
}
| #include <bits/stdc++.h>
using namespace std;
#define SZ(v) int((v).size())
#define ALL(vec) begin(vec), end(vec)
typedef long long i64;
template <typename T> inline bool uax(T &x, T y) {
return (y > x) ? x = y, true : false;
}
template <typename T> inline bool uin(T &x, T y) {
return (y < x) ? x = y, true : false;
}
#ifdef Rahul
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
#else
#define error(...) 42;
#endif
string to_string(char c) { return "'" + string(1, c) + "'"; }
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ": " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
bool f = false;
string r = "{";
for (auto x : v) {
if (f)
r += ", ";
r += to_string(x);
f = true;
}
return r += "}";
}
template <typename A> string to_string(vector<vector<A>> v) {
string r;
for (auto x : v)
r += "\n" + to_string(x);
return r;
}
int Nerr;
template <typename A> string to_string(A *p) {
return to_string(vector<A>(p, p + Nerr));
}
void err(istream_iterator<string>) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " =: " << to_string(a) << "; ";
err(++it, args...);
}
template <typename T> void kek(T ans) {
cout << ans << endl;
exit(0);
}
#define Lu(...) [&](auto &&u) { return __VA_ARGS__; }
#define Luv(...) [&](auto &&u, auto &&v) { return __VA_ARGS__; }
int const MOD = 1e9 + 7;
long long const INF = 1e18 + 42;
/***********************************************************************/
const int N = 128;
int a[N][N];
int b[N][N];
int n, m;
void dfs(int x, int y, int t, int val) {
if (x > n or y > m or b[x][y] != t)
return;
if (!uin(a[x][y], val))
return;
dfs(x + 1, y, t, val);
dfs(x, y + 1, t, val);
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
fill_n(a[0], N * N, MOD);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
string s;
cin >> s;
for (int j = 1; j <= m; ++j) {
b[i][j] = s[j - 1] == '#';
}
}
a[1][0] = a[0][1] = 0;
for (int x = 1; x <= n; ++x) {
for (int y = 1; y <= m; ++y) {
dfs(x, y, b[x][y], b[x][y] + min(a[x - 1][y], a[x][y - 1]));
}
}
kek(a[n][m]);
}
| replace | 74 | 75 | 74 | 76 | TLE | |
p02735 | C++ | Runtime Error | // LightOj Pass : qavajxyq
#include <bits/stdc++.h>
#define deb(x) cerr << (#x) << " : " << x << "\n"
#define ll long long int
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
ll powmod(ll a, ll b, ll mod = MOD) {
ll res = 1ll;
b %= (mod - 1ll), a %= mod;
while (b) {
if (b % 2 == 1)
res = (res * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("D:\\V S Code\\cpp\\competitiveProgramming\\Input.txt", "r", stdin);
freopen("D:\\V S Code\\cpp\\competitiveProgramming\\OPT.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
ll dp[n + 1][m + 1];
string arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
memset(dp, -1, sizeof(dp));
dp[0][0] = arr[0][0] == '#';
int cnt;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!i and !j)
continue;
dp[i][j] = INF;
if (j) {
if (arr[i][j - 1] == '.' and arr[i][j] == '#')
cnt = 1;
else
cnt = 0;
dp[i][j] = min(dp[i][j], dp[i][j - 1] + cnt);
}
if (i) {
if (arr[i - 1][j] == '.' and arr[i][j] == '#')
cnt = 1;
else
cnt = 0;
dp[i][j] = min(dp[i][j], dp[i - 1][j] + cnt);
}
}
}
cout << dp[n - 1][m - 1];
} | // LightOj Pass : qavajxyq
#include <bits/stdc++.h>
#define deb(x) cerr << (#x) << " : " << x << "\n"
#define ll long long int
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
ll powmod(ll a, ll b, ll mod = MOD) {
ll res = 1ll;
b %= (mod - 1ll), a %= mod;
while (b) {
if (b % 2 == 1)
res = (res * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
ll dp[n + 1][m + 1];
string arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
memset(dp, -1, sizeof(dp));
dp[0][0] = arr[0][0] == '#';
int cnt;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!i and !j)
continue;
dp[i][j] = INF;
if (j) {
if (arr[i][j - 1] == '.' and arr[i][j] == '#')
cnt = 1;
else
cnt = 0;
dp[i][j] = min(dp[i][j], dp[i][j - 1] + cnt);
}
if (i) {
if (arr[i - 1][j] == '.' and arr[i][j] == '#')
cnt = 1;
else
cnt = 0;
dp[i][j] = min(dp[i][j], dp[i - 1][j] + cnt);
}
}
}
cout << dp[n - 1][m - 1];
} | delete | 23 | 27 | 23 | 23 | -11 | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORS(i, a, b, c) for (int i = a; i < b; i += c)
#define FORN(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a - 1; i >= b; i--)
#define FORE(i, b) for (auto &i : b)
#define REP(i, b) FOR(i, 0, b)
#define REPN(i, b) FORN(i, 0, b)
#define SQ(i) (i * i)
#define ALL(a) a.begin(), a.end()
#define ALLA(a, n) a, a + n
#define SORT(a) sort(ALL(a))
#define SORTA(a, n) sort(ALLA(a, n))
#define REV(a) reverse(ALL(a))
#define REVA(a, n) reverse(ALLA(a, n))
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define IN(a, b) (a.find(b) != a.end())
#define BACK(a) \
a.back(); \
a.RB()
#define QBACK(a) \
a.top(); \
a.pop()
#define PRINT(a) \
FORE(i, a) cout << i << " "; \
cout << endl
#define PB push_back
#define RB pop_back
#define RF pop_front
#define INS insert
#define F first
#define S second
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define IO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<ll, ll> pll;
typedef queue<int> qi;
typedef queue<double> qd;
typedef queue<ll> qll;
typedef US<int> si;
typedef US<double> sd;
typedef US<ll> sll;
typedef vector<vi> mi;
typedef vector<vd> md;
typedef vector<vll> mll;
typedef vector<pi> vpi;
typedef vector<pd> vpd;
typedef vector<pll> vpll;
int main() {
IO;
int H, W;
cin >> H >> W;
vector<string> s(H);
REP(i, H) cin >> s[i];
mi DP(H, vi(W, 10000));
DP[0][0] = (s[0][0] == '#');
REP(i, H) {
REP(j, W) {
if (i)
DP[i][j] = DP[i - 1][j] + (s[i - 1][j] == '.' && s[i][j] == '#');
if (j)
MIN(DP[i][j], DP[i][j - 1] + (s[i][j - 1] == '.' && s[i][j] == '#'));
}
}
cout << DP[W - 1][H - 1] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORS(i, a, b, c) for (int i = a; i < b; i += c)
#define FORN(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a - 1; i >= b; i--)
#define FORE(i, b) for (auto &i : b)
#define REP(i, b) FOR(i, 0, b)
#define REPN(i, b) FORN(i, 0, b)
#define SQ(i) (i * i)
#define ALL(a) a.begin(), a.end()
#define ALLA(a, n) a, a + n
#define SORT(a) sort(ALL(a))
#define SORTA(a, n) sort(ALLA(a, n))
#define REV(a) reverse(ALL(a))
#define REVA(a, n) reverse(ALLA(a, n))
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define IN(a, b) (a.find(b) != a.end())
#define BACK(a) \
a.back(); \
a.RB()
#define QBACK(a) \
a.top(); \
a.pop()
#define PRINT(a) \
FORE(i, a) cout << i << " "; \
cout << endl
#define PB push_back
#define RB pop_back
#define RF pop_front
#define INS insert
#define F first
#define S second
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define IO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<ll, ll> pll;
typedef queue<int> qi;
typedef queue<double> qd;
typedef queue<ll> qll;
typedef US<int> si;
typedef US<double> sd;
typedef US<ll> sll;
typedef vector<vi> mi;
typedef vector<vd> md;
typedef vector<vll> mll;
typedef vector<pi> vpi;
typedef vector<pd> vpd;
typedef vector<pll> vpll;
int main() {
IO;
int H, W;
cin >> H >> W;
vector<string> s(H);
REP(i, H) cin >> s[i];
mi DP(H, vi(W, 10000));
DP[0][0] = (s[0][0] == '#');
REP(i, H) {
REP(j, W) {
if (i)
DP[i][j] = DP[i - 1][j] + (s[i - 1][j] == '.' && s[i][j] == '#');
if (j)
MIN(DP[i][j], DP[i][j - 1] + (s[i][j - 1] == '.' && s[i][j] == '#'));
}
}
cout << DP[H - 1][W - 1] << endl;
}
| replace | 82 | 83 | 82 | 83 | 0 | |
p02735 | C++ | Time Limit Exceeded | /*
This code has been written by MinakoKojima, feel free to ask me question. Blog:
http://www.shuizilong.com/house Template Date: 2015.10.12 Note: ...
*/
#pragma comment(linker, "/STACK:36777216")
// #pragma GCC optimize ("O2")
#define LOCAL
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <tr1/unordered_set>
// #include <tr1/unordered_map>
// #include <array>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define DWN(i, b, a) for (int i = b - 1; i >= a; --i)
#define REP_1(i, n) for (int i = 1; i <= n; ++i)
#define FOR_1(i, a, b) for (int i = a; i <= b; ++i)
#define DWN_1(i, b, a) for (int i = b; i >= a; --i)
#define REP_C(i, n) for (int n____ = n, i = 0; i < n____; ++i)
#define FOR_C(i, a, b) for (int b____ = b, i = a; i < b____; ++i)
#define DWN_C(i, b, a) for (int a____ = a, i = b - 1; i >= a____; --i)
#define REP_N(i, n) for (i = 0; i < n; ++i)
#define FOR_N(i, a, b) for (i = a; i < b; ++i)
#define DWN_N(i, b, a) for (i = b - 1; i >= a; --i)
#define REP_1_C(i, n) for (int n____ = n, i = 1; i <= n____; ++i)
#define FOR_1_C(i, a, b) for (int b____ = b, i = a; i <= b____; ++i)
#define DWN_1_C(i, b, a) for (int a____ = a, i = b; i >= a____; --i)
#define REP_1_N(i, n) for (i = 1; i <= n; ++i)
#define FOR_1_N(i, a, b) for (i = a; i <= b; ++i)
#define DWN_1_N(i, b, a) for (i = b; i >= a; --i)
#define REP_C_N(i, n) for (int n____ = (i = 0, n); i < n____; ++i)
#define FOR_C_N(i, a, b) for (int b____ = (i = 0, b); i < b____; ++i)
#define DWN_C_N(i, b, a) for (int a____ = (i = b - 1, a); i >= a____; --i)
#define REP_1_C_N(i, n) for (int n____ = (i = 1, n); i <= n____; ++i)
#define FOR_1_C_N(i, a, b) for (int b____ = (i = a, b); i <= b____; ++i)
#define DWN_1_C_N(i, b, a) for (int a____ = (i = b, a); i >= a____; --i)
#define ECH(it, A) \
for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++it)
#define rECH(it, A) \
for (__typeof((A).rbegin()) it = (A).rbegin(); it != (A).rend(); ++it)
#define REP_S(i, str) for (char *i = str; *i; ++i)
#define REP_L(i, hd, suc) for (int i = hd; i; i = suc[i])
#define REP_G(i, u) REP_L(i, hd[u], suc)
#define REP_SS(x, s) for (int x = s; x; x = (x - 1) & s)
#define DO(n) for (int ____n = n; ____n-- > 0;)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define REP_4(i, j, k, ii, n, m, l, nn) \
REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
#define REP_4_1(i, j, k, ii, n, m, l, nn) \
REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())
#define UBD(A, x) (upper_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int((A).size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define Ts *this
#define rTs return Ts
#define fi first
#define se second
#define re real()
#define im imag()
#define Rush for (int ____T = RD(); ____T--;)
#define Display(A, n, m) \
{ \
REP(i, n) { \
REP(j, m - 1) cout << A[i][j] << " "; \
cout << A[i][m - 1] << endl; \
} \
}
#define Display_1(A, n, m) \
{ \
REP_1(i, n) { \
REP_1(j, m - 1) cout << A[i][j] << " "; \
cout << A[i][m] << endl; \
} \
}
typedef long long LL;
// typedef long double DB;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
typedef set<int> SI;
typedef set<string> SS;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
template <class T> inline T &RD(T &);
template <class T> inline void OT(const T &);
// inline int RD(){int x; return RD(x);}
inline LL RD() {
LL x;
return RD(x);
}
inline DB &RF(DB &);
inline DB RF() {
DB x;
return RF(x);
}
inline char *RS(char *s);
inline char &RC(char &c);
inline char RC();
inline char &RC(char &c) {
scanf(" %c", &c);
return c;
}
inline char RC() {
char c;
return RC(c);
}
// inline char& RC(char &c){c = getchar(); return c;}
// inline char RC(){return getchar();}
template <class T> inline T &RDD(T &);
inline LL RDD() {
LL x;
return RDD(x);
}
template <class T0, class T1> inline T0 &RD(T0 &x0, T1 &x1) {
RD(x0), RD(x1);
return x0;
}
template <class T0, class T1, class T2> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) {
RD(x0), RD(x1), RD(x2);
return x0;
}
template <class T0, class T1, class T2, class T3>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) {
RD(x0), RD(x1), RD(x2), RD(x3);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);
return x0;
}
template <class T0, class T1> inline void OT(const T0 &x0, const T1 &x1) {
OT(x0), OT(x1);
}
template <class T0, class T1, class T2>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) {
OT(x0), OT(x1), OT(x2);
}
template <class T0, class T1, class T2, class T3>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) {
OT(x0), OT(x1), OT(x2), OT(x3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5, const T6 &x6) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);
}
inline char &RC(char &a, char &b) {
RC(a), RC(b);
return a;
}
inline char &RC(char &a, char &b, char &c) {
RC(a), RC(b), RC(c);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d) {
RC(a), RC(b), RC(c), RC(d);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e) {
RC(a), RC(b), RC(c), RC(d), RC(e);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g);
return a;
}
inline DB &RF(DB &a, DB &b) {
RF(a), RF(b);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c) {
RF(a), RF(b), RF(c);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d) {
RF(a), RF(b), RF(c), RF(d);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e) {
RF(a), RF(b), RF(c), RF(d), RF(e);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g);
return a;
}
inline void RS(char *s1, char *s2) { RS(s1), RS(s2); }
inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); }
template <class T0, class T1> inline T0 &RDD(T0 &a, T1 &b) {
RDD(a), RDD(b);
return a;
}
template <class T0, class T1, class T2> inline T1 &RDD(T0 &a, T1 &b, T2 &c) {
RDD(a), RDD(b), RDD(c);
return a;
}
template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); }
template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); }
template <class T> inline void CLR(T &A) { A.clear(); }
template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) {
RST(A0), RST(A1);
}
template <class T0, class T1, class T2>
inline void RST(T0 &A0, T1 &A1, T2 &A2) {
RST(A0), RST(A1), RST(A2);
}
template <class T0, class T1, class T2, class T3>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
RST(A0), RST(A1), RST(A2), RST(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);
}
template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) {
FLC(A0, x), FLC(A1, x);
}
template <class T0, class T1, class T2>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x);
}
template <class T0, class T1, class T2, class T3>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x),
FLC(A6, x);
}
template <class T> inline void CLR(priority_queue<T> &Q) {
while (!Q.empty())
Q.pop();
}
template <class T> inline void CLR(stack<T> &S) {
while (!S.empty())
S.pop();
}
template <class T> inline void CLR(queue<T> &Q) {
while (!Q.empty())
Q.pop();
}
template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) {
CLR(A0), CLR(A1);
}
template <class T0, class T1, class T2>
inline void CLR(T0 &A0, T1 &A1, T2 &A2) {
CLR(A0), CLR(A1), CLR(A2);
}
template <class T0, class T1, class T2, class T3>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);
}
template <class T> inline void CLR(T &A, int n) { REP(i, n) CLR(A[i]); }
template <class T> inline bool EPT(T &a) { return a.empty(); }
template <class T> inline T &SRT(T &A) {
sort(ALL(A));
return A;
}
template <class T, class C> inline T &SRT(T &A, C cmp) {
sort(ALL(A), cmp);
return A;
}
template <class T> inline T &RVS(T &A) {
reverse(ALL(A));
return A;
}
template <class T> inline T &UNQQ(T &A) {
A.resize(unique(ALL(A)) - A.begin());
return A;
}
template <class T> inline T &UNQ(T &A) {
SRT(A);
return UNQQ(A);
}
template <class T, class C> inline T &UNQ(T &A, C cmp) {
SRT(A, cmp);
return UNQQ(A);
}
//}
/** Constant List .. **/ //{
const int MOD = int(1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); // M_PI;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, 1, -1};
//}
/** Add On .. **/ //{
// <<= '0. Nichi Joo ., //{
template <class T> inline bool checkMin(T &a, const T b) {
return b < a ? a = b, 1 : 0;
}
template <class T> inline bool checkMax(T &a, const T b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class C> inline bool checkUpd(T &a, const T b, C c) {
return c(b, a) ? a = b, 1 : 0;
}
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
template <class T> inline T min(T a, T b, T c, T d, T e) {
return min(min(min(a, b), min(c, d)), e);
}
template <class T> inline T max(T a, T b, T c, T d, T e) {
return max(max(max(a, b), max(c, d)), e);
}
template <class T> inline T sqr(T a) { return a * a; }
template <class T> inline T cub(T a) { return a * a * a; }
template <class T> inline T ceil(T x, T y) { return (x - 1) / y + 1; }
template <class T> T abs(T x) { return x > 0 ? x : -x; }
inline int sgn(DB x) { return x < -EPS ? -1 : x > EPS; }
inline int sgn(DB x, DB y) { return sgn(x - y); }
inline DB cos(DB a, DB b, DB c) {
return (sqr(a) + sqr(b) - sqr(c)) / (2 * a * b);
}
inline DB cot(DB x) { return 1. / tan(x); };
inline DB sec(DB x) { return 1. / cos(x); };
inline DB csc(DB x) { return 1. / sin(x); };
//}
//}
/** I/O Accelerator Interface .. **/ //{
#define g (c = getchar())
#define d isdigit(g)
#define p x = x * 10 + c - '0'
#define n x = x * 10 + '0' - c
#define pp l /= 10, p
#define nn l /= 10, n
template <class T> inline T &RD(T &x) {
char c;
while (!d)
;
x = c - '0';
while (d)
p;
return x;
}
template <class T> inline T &RDD(T &x) {
char c;
while (g, c != '-' && !isdigit(c))
;
if (c == '-') {
x = '0' - g;
while (d)
n;
} else {
x = c - '0';
while (d)
p;
}
return x;
}
inline DB &RF(DB &x) {
// scanf("%lf", &x);
char c;
while (g, c != '-' && c != '.' && !isdigit(c))
;
if (c == '-')
if (g == '.') {
x = 0;
DB l = 1;
while (d)
nn;
x *= l;
} else {
x = '0' - c;
while (d)
n;
if (c == '.') {
DB l = 1;
while (d)
nn;
x *= l;
}
}
else if (c == '.') {
x = 0;
DB l = 1;
while (d)
pp;
x *= l;
} else {
x = c - '0';
while (d)
p;
if (c == '.') {
DB l = 1;
while (d)
pp;
x *= l;
}
}
return x;
}
#undef nn
#undef pp
#undef n
#undef p
#undef d
#undef g
inline char *RS(char *s) {
// gets(s);
scanf("%s", s);
return s;
}
LL last_ans;
int Case;
template <class T> inline void OT(const T &x) {
// printf("Case #%d: ", ++Case);
// printf("%lld\n", x);
// printf("%I64d\n", x);
// printf("%.9f\n", x);
printf("%d\n", x);
// cout << x << endl;
// last_ans = x;
}
//}/*
//..................................................................................................................................
//*/
const int N = int(1e2) + 9;
int D[N][N];
bool A[N][N];
int n, m;
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
RD(n, m);
REP_1(i, n) REP_1(j, m) { A[i][j] = RC() == '.'; }
FLC(D, 0x3f);
REP_1(i, n) REP_1(j, m) { // 0 2
if (i == 1 && j == 1) {
D[1][1] = !A[1][1] ? 1 : 0;
} else {
D[i][j] = min(D[i - 1][j] + (A[i - 1][j] != A[i][j]),
D[i][j - 1] + (A[i][j - 1] != A[i][j]));
}
// cout << D[i][j] << " ";
// if (j == m) cout << endl;
}
cout << (D[n][m] + 1) / 2 << endl;
}
| /*
This code has been written by MinakoKojima, feel free to ask me question. Blog:
http://www.shuizilong.com/house Template Date: 2015.10.12 Note: ...
*/
#pragma comment(linker, "/STACK:36777216")
// #pragma GCC optimize ("O2")
#define LOCAL
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <tr1/unordered_set>
// #include <tr1/unordered_map>
// #include <array>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define DWN(i, b, a) for (int i = b - 1; i >= a; --i)
#define REP_1(i, n) for (int i = 1; i <= n; ++i)
#define FOR_1(i, a, b) for (int i = a; i <= b; ++i)
#define DWN_1(i, b, a) for (int i = b; i >= a; --i)
#define REP_C(i, n) for (int n____ = n, i = 0; i < n____; ++i)
#define FOR_C(i, a, b) for (int b____ = b, i = a; i < b____; ++i)
#define DWN_C(i, b, a) for (int a____ = a, i = b - 1; i >= a____; --i)
#define REP_N(i, n) for (i = 0; i < n; ++i)
#define FOR_N(i, a, b) for (i = a; i < b; ++i)
#define DWN_N(i, b, a) for (i = b - 1; i >= a; --i)
#define REP_1_C(i, n) for (int n____ = n, i = 1; i <= n____; ++i)
#define FOR_1_C(i, a, b) for (int b____ = b, i = a; i <= b____; ++i)
#define DWN_1_C(i, b, a) for (int a____ = a, i = b; i >= a____; --i)
#define REP_1_N(i, n) for (i = 1; i <= n; ++i)
#define FOR_1_N(i, a, b) for (i = a; i <= b; ++i)
#define DWN_1_N(i, b, a) for (i = b; i >= a; --i)
#define REP_C_N(i, n) for (int n____ = (i = 0, n); i < n____; ++i)
#define FOR_C_N(i, a, b) for (int b____ = (i = 0, b); i < b____; ++i)
#define DWN_C_N(i, b, a) for (int a____ = (i = b - 1, a); i >= a____; --i)
#define REP_1_C_N(i, n) for (int n____ = (i = 1, n); i <= n____; ++i)
#define FOR_1_C_N(i, a, b) for (int b____ = (i = a, b); i <= b____; ++i)
#define DWN_1_C_N(i, b, a) for (int a____ = (i = b, a); i >= a____; --i)
#define ECH(it, A) \
for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++it)
#define rECH(it, A) \
for (__typeof((A).rbegin()) it = (A).rbegin(); it != (A).rend(); ++it)
#define REP_S(i, str) for (char *i = str; *i; ++i)
#define REP_L(i, hd, suc) for (int i = hd; i; i = suc[i])
#define REP_G(i, u) REP_L(i, hd[u], suc)
#define REP_SS(x, s) for (int x = s; x; x = (x - 1) & s)
#define DO(n) for (int ____n = n; ____n-- > 0;)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define REP_4(i, j, k, ii, n, m, l, nn) \
REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
#define REP_4_1(i, j, k, ii, n, m, l, nn) \
REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())
#define UBD(A, x) (upper_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int((A).size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define Ts *this
#define rTs return Ts
#define fi first
#define se second
#define re real()
#define im imag()
#define Rush for (int ____T = RD(); ____T--;)
#define Display(A, n, m) \
{ \
REP(i, n) { \
REP(j, m - 1) cout << A[i][j] << " "; \
cout << A[i][m - 1] << endl; \
} \
}
#define Display_1(A, n, m) \
{ \
REP_1(i, n) { \
REP_1(j, m - 1) cout << A[i][j] << " "; \
cout << A[i][m] << endl; \
} \
}
typedef long long LL;
// typedef long double DB;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
typedef set<int> SI;
typedef set<string> SS;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
template <class T> inline T &RD(T &);
template <class T> inline void OT(const T &);
// inline int RD(){int x; return RD(x);}
inline LL RD() {
LL x;
return RD(x);
}
inline DB &RF(DB &);
inline DB RF() {
DB x;
return RF(x);
}
inline char *RS(char *s);
inline char &RC(char &c);
inline char RC();
inline char &RC(char &c) {
scanf(" %c", &c);
return c;
}
inline char RC() {
char c;
return RC(c);
}
// inline char& RC(char &c){c = getchar(); return c;}
// inline char RC(){return getchar();}
template <class T> inline T &RDD(T &);
inline LL RDD() {
LL x;
return RDD(x);
}
template <class T0, class T1> inline T0 &RD(T0 &x0, T1 &x1) {
RD(x0), RD(x1);
return x0;
}
template <class T0, class T1, class T2> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) {
RD(x0), RD(x1), RD(x2);
return x0;
}
template <class T0, class T1, class T2, class T3>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) {
RD(x0), RD(x1), RD(x2), RD(x3);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);
return x0;
}
template <class T0, class T1> inline void OT(const T0 &x0, const T1 &x1) {
OT(x0), OT(x1);
}
template <class T0, class T1, class T2>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) {
OT(x0), OT(x1), OT(x2);
}
template <class T0, class T1, class T2, class T3>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) {
OT(x0), OT(x1), OT(x2), OT(x3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5, const T6 &x6) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);
}
inline char &RC(char &a, char &b) {
RC(a), RC(b);
return a;
}
inline char &RC(char &a, char &b, char &c) {
RC(a), RC(b), RC(c);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d) {
RC(a), RC(b), RC(c), RC(d);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e) {
RC(a), RC(b), RC(c), RC(d), RC(e);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g);
return a;
}
inline DB &RF(DB &a, DB &b) {
RF(a), RF(b);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c) {
RF(a), RF(b), RF(c);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d) {
RF(a), RF(b), RF(c), RF(d);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e) {
RF(a), RF(b), RF(c), RF(d), RF(e);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g);
return a;
}
inline void RS(char *s1, char *s2) { RS(s1), RS(s2); }
inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); }
template <class T0, class T1> inline T0 &RDD(T0 &a, T1 &b) {
RDD(a), RDD(b);
return a;
}
template <class T0, class T1, class T2> inline T1 &RDD(T0 &a, T1 &b, T2 &c) {
RDD(a), RDD(b), RDD(c);
return a;
}
template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); }
template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); }
template <class T> inline void CLR(T &A) { A.clear(); }
template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) {
RST(A0), RST(A1);
}
template <class T0, class T1, class T2>
inline void RST(T0 &A0, T1 &A1, T2 &A2) {
RST(A0), RST(A1), RST(A2);
}
template <class T0, class T1, class T2, class T3>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
RST(A0), RST(A1), RST(A2), RST(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);
}
template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) {
FLC(A0, x), FLC(A1, x);
}
template <class T0, class T1, class T2>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x);
}
template <class T0, class T1, class T2, class T3>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x),
FLC(A6, x);
}
template <class T> inline void CLR(priority_queue<T> &Q) {
while (!Q.empty())
Q.pop();
}
template <class T> inline void CLR(stack<T> &S) {
while (!S.empty())
S.pop();
}
template <class T> inline void CLR(queue<T> &Q) {
while (!Q.empty())
Q.pop();
}
template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) {
CLR(A0), CLR(A1);
}
template <class T0, class T1, class T2>
inline void CLR(T0 &A0, T1 &A1, T2 &A2) {
CLR(A0), CLR(A1), CLR(A2);
}
template <class T0, class T1, class T2, class T3>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);
}
template <class T> inline void CLR(T &A, int n) { REP(i, n) CLR(A[i]); }
template <class T> inline bool EPT(T &a) { return a.empty(); }
template <class T> inline T &SRT(T &A) {
sort(ALL(A));
return A;
}
template <class T, class C> inline T &SRT(T &A, C cmp) {
sort(ALL(A), cmp);
return A;
}
template <class T> inline T &RVS(T &A) {
reverse(ALL(A));
return A;
}
template <class T> inline T &UNQQ(T &A) {
A.resize(unique(ALL(A)) - A.begin());
return A;
}
template <class T> inline T &UNQ(T &A) {
SRT(A);
return UNQQ(A);
}
template <class T, class C> inline T &UNQ(T &A, C cmp) {
SRT(A, cmp);
return UNQQ(A);
}
//}
/** Constant List .. **/ //{
const int MOD = int(1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); // M_PI;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, 1, -1};
//}
/** Add On .. **/ //{
// <<= '0. Nichi Joo ., //{
template <class T> inline bool checkMin(T &a, const T b) {
return b < a ? a = b, 1 : 0;
}
template <class T> inline bool checkMax(T &a, const T b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class C> inline bool checkUpd(T &a, const T b, C c) {
return c(b, a) ? a = b, 1 : 0;
}
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
template <class T> inline T min(T a, T b, T c, T d, T e) {
return min(min(min(a, b), min(c, d)), e);
}
template <class T> inline T max(T a, T b, T c, T d, T e) {
return max(max(max(a, b), max(c, d)), e);
}
template <class T> inline T sqr(T a) { return a * a; }
template <class T> inline T cub(T a) { return a * a * a; }
template <class T> inline T ceil(T x, T y) { return (x - 1) / y + 1; }
template <class T> T abs(T x) { return x > 0 ? x : -x; }
inline int sgn(DB x) { return x < -EPS ? -1 : x > EPS; }
inline int sgn(DB x, DB y) { return sgn(x - y); }
inline DB cos(DB a, DB b, DB c) {
return (sqr(a) + sqr(b) - sqr(c)) / (2 * a * b);
}
inline DB cot(DB x) { return 1. / tan(x); };
inline DB sec(DB x) { return 1. / cos(x); };
inline DB csc(DB x) { return 1. / sin(x); };
//}
//}
/** I/O Accelerator Interface .. **/ //{
#define g (c = getchar())
#define d isdigit(g)
#define p x = x * 10 + c - '0'
#define n x = x * 10 + '0' - c
#define pp l /= 10, p
#define nn l /= 10, n
template <class T> inline T &RD(T &x) {
char c;
while (!d)
;
x = c - '0';
while (d)
p;
return x;
}
template <class T> inline T &RDD(T &x) {
char c;
while (g, c != '-' && !isdigit(c))
;
if (c == '-') {
x = '0' - g;
while (d)
n;
} else {
x = c - '0';
while (d)
p;
}
return x;
}
inline DB &RF(DB &x) {
// scanf("%lf", &x);
char c;
while (g, c != '-' && c != '.' && !isdigit(c))
;
if (c == '-')
if (g == '.') {
x = 0;
DB l = 1;
while (d)
nn;
x *= l;
} else {
x = '0' - c;
while (d)
n;
if (c == '.') {
DB l = 1;
while (d)
nn;
x *= l;
}
}
else if (c == '.') {
x = 0;
DB l = 1;
while (d)
pp;
x *= l;
} else {
x = c - '0';
while (d)
p;
if (c == '.') {
DB l = 1;
while (d)
pp;
x *= l;
}
}
return x;
}
#undef nn
#undef pp
#undef n
#undef p
#undef d
#undef g
inline char *RS(char *s) {
// gets(s);
scanf("%s", s);
return s;
}
LL last_ans;
int Case;
template <class T> inline void OT(const T &x) {
// printf("Case #%d: ", ++Case);
// printf("%lld\n", x);
// printf("%I64d\n", x);
// printf("%.9f\n", x);
printf("%d\n", x);
// cout << x << endl;
// last_ans = x;
}
//}/*
//..................................................................................................................................
//*/
const int N = int(1e2) + 9;
int D[N][N];
bool A[N][N];
int n, m;
int main() {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
RD(n, m);
REP_1(i, n) REP_1(j, m) { A[i][j] = RC() == '.'; }
FLC(D, 0x3f);
REP_1(i, n) REP_1(j, m) { // 0 2
if (i == 1 && j == 1) {
D[1][1] = !A[1][1] ? 1 : 0;
} else {
D[i][j] = min(D[i - 1][j] + (A[i - 1][j] != A[i][j]),
D[i][j - 1] + (A[i][j - 1] != A[i][j]));
}
// cout << D[i][j] << " ";
// if (j == m) cout << endl;
}
cout << (D[n][m] + 1) / 2 << endl;
}
| replace | 565 | 566 | 565 | 566 | TLE | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int h, w;
cin >> h >> w;
vector<string> s(h);
for (int i = 0; i < h; i++) {
cin >> s[i];
}
vector<vector<int>> dp(h, vector<int>(h, 1e5));
if (s[0][0] == '#')
dp[0][0] = 1;
else
dp[0][0] = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i + 1 < h && s[i + 1][j] == '#' && s[i][j] == '.')
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
else if (i + 1 < h)
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
if (j + 1 < w && s[i][j + 1] == '#' && s[i][j] == '.')
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1);
else if (j + 1 < w)
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j]);
}
}
cout << dp[h - 1][w - 1] << endl;
} | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int h, w;
cin >> h >> w;
vector<string> s(h);
for (int i = 0; i < h; i++) {
cin >> s[i];
}
vector<vector<int>> dp(h, vector<int>(w, 1e5));
if (s[0][0] == '#')
dp[0][0] = 1;
else
dp[0][0] = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i + 1 < h && s[i + 1][j] == '#' && s[i][j] == '.')
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
else if (i + 1 < h)
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
if (j + 1 < w && s[i][j + 1] == '#' && s[i][j] == '.')
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1);
else if (j + 1 < w)
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j]);
}
}
cout << dp[h - 1][w - 1] << endl;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p02735 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
int H, W;
cin >> H >> W;
vector<string> s(H);
for (int i = 0; i < H; i++)
cin >> s[i];
vector<vector<int>> dp(H, vector<int>(W));
if (s[0][0] == '#')
dp[0][0] = 1;
else
dp[0][0] = 0;
int ans;
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
ans = 20000;
if (i != 0) {
if (s[i - 1][j] == '.' && s[i][j] == '#')
ans = dp[i - 1][j] + 1;
else
ans = dp[i - 1][j];
}
if (j != 0) {
if (s[i][j - 1] == '.' && s[i][j] == '#')
ans = min(dp[i][j - 1] + 1, ans);
else
ans = min(dp[i][j - 1], ans);
}
if (i != 0 || j != 0) {
dp[i][j] = ans;
}
}
}
cout << dp[H - 1][W - 1] << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
int H, W;
cin >> H >> W;
vector<string> s(H);
for (int i = 0; i < H; i++)
cin >> s[i];
vector<vector<int>> dp(H, vector<int>(W));
if (s[0][0] == '#')
dp[0][0] = 1;
else
dp[0][0] = 0;
int ans;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
ans = 20000;
if (i != 0) {
if (s[i - 1][j] == '.' && s[i][j] == '#')
ans = dp[i - 1][j] + 1;
else
ans = dp[i - 1][j];
}
if (j != 0) {
if (s[i][j - 1] == '.' && s[i][j] == '#')
ans = min(dp[i][j - 1] + 1, ans);
else
ans = min(dp[i][j - 1], ans);
}
if (i != 0 || j != 0) {
dp[i][j] = ans;
}
}
}
cout << dp[H - 1][W - 1] << endl;
return 0;
}
| replace | 30 | 32 | 30 | 32 | 0 | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
typedef tuple<int, int, int> T;
void solve() {
int h, w;
cin >> h >> w;
vvi m(h, vi(w));
rep(i, h) {
string s;
cin >> s;
rep(j, w) if (s[j] == '#') m[i][j] = 1;
}
dumpv(m, h, w);
deque<T> q;
vvi used(h, vi(w, -1));
if (m[0][0]) {
q.push_back(T(0, 0, 1));
} else {
q.push_back(T(0, 0, 0));
}
while (!q.empty()) {
int i, j, c;
tie(i, j, c) = q.front();
q.pop_front();
if (used[i][j] != -1 && used[i][j] < c)
continue;
used[i][j] = c;
if (i < h - 1) {
if (m[i + 1][j] == m[i][j]) {
q.push_back(T(i + 1, j, c));
} else {
q.push_back(T(i + 1, j, c + 1));
}
}
if (j < w - 1) {
if (m[i][j + 1] == m[i][j]) {
q.push_back(T(i, j + 1, c));
} else {
q.push_back(T(i, j + 1, c + 1));
}
}
// dumpv(used, h, w);
}
cout << (used[h - 1][w - 1] + 1) / 2 << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
// freopen("temp.1", "r", stdin);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
typedef tuple<int, int, int> T;
void solve() {
int h, w;
cin >> h >> w;
vvi m(h, vi(w));
rep(i, h) {
string s;
cin >> s;
rep(j, w) if (s[j] == '#') m[i][j] = 1;
}
dumpv(m, h, w);
deque<T> q;
vvi used(h, vi(w, -1));
if (m[0][0]) {
q.push_back(T(0, 0, 1));
} else {
q.push_back(T(0, 0, 0));
}
while (!q.empty()) {
int i, j, c;
tie(i, j, c) = q.front();
q.pop_front();
if (used[i][j] != -1 && used[i][j] <= c)
continue;
used[i][j] = c;
if (i < h - 1) {
if (m[i + 1][j] == m[i][j]) {
q.push_back(T(i + 1, j, c));
} else {
q.push_back(T(i + 1, j, c + 1));
}
}
if (j < w - 1) {
if (m[i][j + 1] == m[i][j]) {
q.push_back(T(i, j + 1, c));
} else {
q.push_back(T(i, j + 1, c + 1));
}
}
// dumpv(used, h, w);
}
cout << (used[h - 1][w - 1] + 1) / 2 << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
// freopen("temp.1", "r", stdin);
solve();
return 0;
} | replace | 45 | 46 | 45 | 46 | TLE | |
p02735 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define endl '\n'
const int dx[2] = {+1, 0};
const int dy[2] = {0, +1};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int h, w;
cin >> h >> w;
vector<string> board(h);
for (int i = 0; i < h; ++i)
cin >> board[i];
vector<vector<int>> dist(h, vector<int>(w, h * w + 1));
dist[0][0] = 0;
deque<pair<int, int>> q;
q.push_back({0, 0});
while (!q.empty()) {
auto c = q.front();
q.pop_front();
int x = c.first, y = c.second, d = dist[x][y];
for (int k = 0; k < 4; ++k) {
int nx = x + dx[k], ny = y + dy[k];
if (0 <= nx && nx < h && 0 <= ny && ny < w) {
if (board[x][y] == board[nx][ny]) {
if (dist[nx][ny] > d) {
dist[nx][ny] = d;
q.push_front({nx, ny});
}
} else {
if (dist[nx][ny] > d + 1) {
dist[nx][ny] = d + 1;
q.push_back({nx, ny});
}
}
}
}
}
cout << (dist[h - 1][w - 1] + (board[0][0] == '#') +
(board[h - 1][w - 1] == '#') + 1) /
2
<< endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define endl '\n'
const int dx[2] = {+1, 0};
const int dy[2] = {0, +1};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int h, w;
cin >> h >> w;
vector<string> board(h);
for (int i = 0; i < h; ++i)
cin >> board[i];
vector<vector<int>> dist(h, vector<int>(w, h * w + 1));
dist[0][0] = 0;
deque<pair<int, int>> q;
q.push_back({0, 0});
while (!q.empty()) {
auto c = q.front();
q.pop_front();
int x = c.first, y = c.second, d = dist[x][y];
for (int k = 0; k < 2; ++k) {
int nx = x + dx[k], ny = y + dy[k];
if (0 <= nx && nx < h && 0 <= ny && ny < w) {
if (board[x][y] == board[nx][ny]) {
if (dist[nx][ny] > d) {
dist[nx][ny] = d;
q.push_front({nx, ny});
}
} else {
if (dist[nx][ny] > d + 1) {
dist[nx][ny] = d + 1;
q.push_back({nx, ny});
}
}
}
}
}
cout << (dist[h - 1][w - 1] + (board[0][0] == '#') +
(board[h - 1][w - 1] == '#') + 1) /
2
<< endl;
return 0;
} | replace | 43 | 44 | 43 | 44 | 0 | |
p02735 | C++ | Time Limit Exceeded | #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++)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int dx[2] = {0, 1};
int dy[2] = {1, 0};
char s[101][101];
int d[101][101]; // d[i][j] := (i,j)に行くのにかかる最小のコスト
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
rep(i, 101) rep(j, 101) d[i][j] = 1e9 + 7;
d[0][0] = 0;
int h, w;
cin >> h >> w;
rep(i, h) rep(j, w) cin >> s[i][j];
queue<pair<int, int>> q;
q.push({0, 0});
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
// 取り出した点が黒だったら黒の間移動しまくる
for (int i = 0; i < 2; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= h || ny >= w)
continue;
if (s[x][y] == '.') {
if (s[nx][ny] == '#') {
if (d[nx][ny] > d[x][y] + 1) {
d[nx][ny] = d[x][y] + 1;
q.push({nx, ny});
}
} else {
if (d[nx][ny] < d[x][y])
continue;
d[nx][ny] = d[x][y];
q.push({nx, ny});
}
} else {
if (d[nx][ny] > d[x][y]) {
d[nx][ny] = d[x][y];
q.push({nx, ny});
}
}
}
}
cout << d[h - 1][w - 1] + (s[0][0] == '#') << endl;
} | #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++)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int dx[2] = {0, 1};
int dy[2] = {1, 0};
char s[101][101];
int d[101][101]; // d[i][j] := (i,j)に行くのにかかる最小のコスト
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
rep(i, 101) rep(j, 101) d[i][j] = 1e9 + 7;
d[0][0] = 0;
int h, w;
cin >> h >> w;
rep(i, h) rep(j, w) cin >> s[i][j];
queue<pair<int, int>> q;
q.push({0, 0});
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
// 取り出した点が黒だったら黒の間移動しまくる
for (int i = 0; i < 2; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= h || ny >= w)
continue;
if (s[x][y] == '.') {
if (s[nx][ny] == '#') {
if (d[nx][ny] > d[x][y] + 1) {
d[nx][ny] = d[x][y] + 1;
q.push({nx, ny});
}
} else {
if (d[nx][ny] > d[x][y]) {
d[nx][ny] = d[x][y];
q.push({nx, ny});
}
}
} else {
if (d[nx][ny] > d[x][y]) {
d[nx][ny] = d[x][y];
q.push({nx, ny});
}
}
}
}
cout << d[h - 1][w - 1] + (s[0][0] == '#') << endl;
} | replace | 57 | 61 | 57 | 61 | TLE | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define INF (int)1e9
#define cBits(x) __builtin_popcount(x)
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) a / gcd(a, b) * b
#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 MAX3(a, b, c) max(a, max(b, c))
#define MIN3(a, b, c) min(a, min(b, c))
#define eachTest \
int t; \
cin >> t; \
while (t--) \
solve();
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
const int MOD = 1000000007;
void solve() { ; }
int main() {
fastio;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// eachTest
int h, w;
cin >> h >> w;
string board[h];
FOR(i, 0, h) { cin >> board[i]; }
int tbl[h][w];
// tbl[h][w] holds minimum number of blacks encountered when going from [i][j]
// to [h-1][w-1]
if (board[h - 1][w - 1] == '#') {
tbl[h - 1][w - 1] = 1;
} else {
tbl[h - 1][w - 1] = 0;
}
FORr(i, h - 2, 0) {
// [i][w-1]
tbl[i][w - 1] = tbl[i + 1][w - 1];
if (board[i][w - 1] != board[i + 1][w - 1] && board[i][w - 1] == '#') {
tbl[i][w - 1]++;
}
}
FORr(j, w - 2, 0) {
// [h-1][j]
tbl[h - 1][j] = tbl[h - 1][j + 1];
if (board[h - 1][j] != board[h - 1][j + 1] && board[h - 1][j] == '#') {
tbl[h - 1][j]++;
}
}
FORr(i, h - 2, 0) {
FORr(j, w - 2, 0) {
// tbl[i][j] = -1;
ll temph = -1;
ll tempv = -1;
if (board[i][j] == board[i + 1][j]) {
temph = tbl[i + 1][j];
} else if (board[i][j] == '#') {
temph = tbl[i + 1][j] + 1;
} else {
temph = tbl[i + 1][j];
}
if (board[i][j] == board[i][j + 1]) {
tempv = tbl[i][j + 1];
} else if (board[i][j] == '#') {
tempv = tbl[i][j + 1] + 1;
} else {
tempv = tbl[i][j + 1];
}
tbl[i][j] = min(temph, tempv);
}
}
// FOR(i,0,h) {
// FOR(j,0,w) {
// cout << tbl[i][j] << " ";
// }
// cout << "\n";
// }
cout << tbl[0][0] << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define INF (int)1e9
#define cBits(x) __builtin_popcount(x)
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) a / gcd(a, b) * b
#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 MAX3(a, b, c) max(a, max(b, c))
#define MIN3(a, b, c) min(a, min(b, c))
#define eachTest \
int t; \
cin >> t; \
while (t--) \
solve();
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
const int MOD = 1000000007;
void solve() { ; }
int main() {
// eachTest
int h, w;
cin >> h >> w;
string board[h];
FOR(i, 0, h) { cin >> board[i]; }
int tbl[h][w];
// tbl[h][w] holds minimum number of blacks encountered when going from [i][j]
// to [h-1][w-1]
if (board[h - 1][w - 1] == '#') {
tbl[h - 1][w - 1] = 1;
} else {
tbl[h - 1][w - 1] = 0;
}
FORr(i, h - 2, 0) {
// [i][w-1]
tbl[i][w - 1] = tbl[i + 1][w - 1];
if (board[i][w - 1] != board[i + 1][w - 1] && board[i][w - 1] == '#') {
tbl[i][w - 1]++;
}
}
FORr(j, w - 2, 0) {
// [h-1][j]
tbl[h - 1][j] = tbl[h - 1][j + 1];
if (board[h - 1][j] != board[h - 1][j + 1] && board[h - 1][j] == '#') {
tbl[h - 1][j]++;
}
}
FORr(i, h - 2, 0) {
FORr(j, w - 2, 0) {
// tbl[i][j] = -1;
ll temph = -1;
ll tempv = -1;
if (board[i][j] == board[i + 1][j]) {
temph = tbl[i + 1][j];
} else if (board[i][j] == '#') {
temph = tbl[i + 1][j] + 1;
} else {
temph = tbl[i + 1][j];
}
if (board[i][j] == board[i][j + 1]) {
tempv = tbl[i][j + 1];
} else if (board[i][j] == '#') {
tempv = tbl[i][j + 1] + 1;
} else {
tempv = tbl[i][j + 1];
}
tbl[i][j] = min(temph, tempv);
}
}
// FOR(i,0,h) {
// FOR(j,0,w) {
// cout << tbl[i][j] << " ";
// }
// cout << "\n";
// }
cout << tbl[0][0] << "\n";
return 0;
}
| delete | 30 | 36 | 30 | 30 | -11 | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<pll> vpl;
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#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 FOR(i, a, b) for (int i = (a); i < (b); i++)
#define PB push_back
#define EB emplace_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c) \
(sort(c.begin(), c.end()), \
c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dh[2] = {1, 0};
const int dw[2] = {0, 1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll h, w;
cin >> h >> w;
vector<string> s(h);
REP(i, h) cin >> s[i];
ll dist[h][w];
REP(i, h) REP(j, w) dist[i][j] = LLINF;
dist[0][0] = 0;
if (s[0][0] == '#')
dist[0][0]++;
deque<pair<pll, ll>> que;
que.push_back(make_pair(pll(0, 0), 0));
while (!que.empty()) {
pair<pll, ll> p = que.front();
que.pop_front();
pll point = p.FI;
ll cnt = p.SE;
if (cnt > dist[point.SE][point.FI])
continue;
REP(i, 2) {
if (point.FI + dw[i] >= w || point.SE + dh[i] >= h)
continue;
pll np = pll(point.FI + dw[i], point.SE + dh[i]);
pair<pll, ll> next = make_pair(np, dist[point.SE][point.FI]);
if (s[np.SE][np.FI] == '#' && s[point.SE][point.FI] == '.')
next.SE++;
dist[np.SE][np.FI] = min(dist[np.SE][np.FI], next.SE);
que.push_back(next);
}
}
cout << dist[h - 1][w - 1] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<pll> vpl;
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#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 FOR(i, a, b) for (int i = (a); i < (b); i++)
#define PB push_back
#define EB emplace_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c) \
(sort(c.begin(), c.end()), \
c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dh[2] = {1, 0};
const int dw[2] = {0, 1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll h, w;
cin >> h >> w;
vector<string> s(h);
REP(i, h) cin >> s[i];
ll dist[h][w];
REP(i, h) REP(j, w) dist[i][j] = LLINF;
dist[0][0] = 0;
if (s[0][0] == '#')
dist[0][0]++;
deque<pair<pll, ll>> que;
que.push_back(make_pair(pll(0, 0), 0));
while (!que.empty()) {
pair<pll, ll> p = que.front();
que.pop_front();
pll point = p.FI;
ll cnt = p.SE;
if (cnt > dist[point.SE][point.FI])
continue;
REP(i, 2) {
if (point.FI + dw[i] >= w || point.SE + dh[i] >= h)
continue;
pll np = pll(point.FI + dw[i], point.SE + dh[i]);
pair<pll, ll> next = make_pair(np, dist[point.SE][point.FI]);
if (s[np.SE][np.FI] == '#' && s[point.SE][point.FI] == '.')
next.SE++;
if (next.SE >= dist[np.SE][np.FI])
continue;
dist[np.SE][np.FI] = min(dist[np.SE][np.FI], next.SE);
que.push_back(next);
}
}
cout << dist[h - 1][w - 1] << endl;
}
| insert | 73 | 73 | 73 | 75 | TLE | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using graph = vector<vector<int>>;
using pii = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define all(x) (x).begin(), (x).end()
#define size(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define INF 1000000000000
#define M 10000007
#define MAXR 100000
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define FIND(x, y) find(all(x), y) != x.end()
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 (b < a) {
a = b;
return true;
}
return false;
}
struct uf {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
vector<int> rcount;
uf(int N) : par(N) { // 最初は全てが根であるとして初期化
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
template <int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0)
val += 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 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;
}
};
const int MOD = 1000000007;
using mint = Fp<MOD>;
const int dx[2] = {1, 0};
const int dy[2] = {0, 1};
bool flag = false;
int H, W, ans = 10000000;
vector<string> field;
int seen[510][510];
void dfs(int h, int w, int gh, int gw) {
rep(dir, 2) {
flag = false;
int nh = h + dy[dir];
int nw = w + dx[dir];
if (nh < 0 || nh >= H || nw < 0 || nw >= W)
continue;
if (field[h][w] != '#' && field[nh][nw] == '#')
flag = chmin(seen[nh][nw], seen[h][w] + 1);
else
flag = chmin(seen[nh][nw], seen[h][w]);
if (nh == gh && nw == gw)
continue;
dfs(nh, nw, gh, gw);
}
}
int main() {
cin >> H >> W;
field.resize(H);
rep(h, H) cin >> field[h];
int sh = 0, sw = 0, gh = H - 1, gw = W - 1;
rep(i, H) rep(j, W) seen[i][j] = 1000;
seen[0][0] = 0;
dfs(sh, sw, gh, gw);
if (field[0][0] == '#')
seen[gh][gw]++;
cout << seen[gh][gw] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using graph = vector<vector<int>>;
using pii = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define all(x) (x).begin(), (x).end()
#define size(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define INF 1000000000000
#define M 10000007
#define MAXR 100000
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define FIND(x, y) find(all(x), y) != x.end()
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 (b < a) {
a = b;
return true;
}
return false;
}
struct uf {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
vector<int> rcount;
uf(int N) : par(N) { // 最初は全てが根であるとして初期化
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
template <int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0)
val += 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 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;
}
};
const int MOD = 1000000007;
using mint = Fp<MOD>;
const int dx[2] = {1, 0};
const int dy[2] = {0, 1};
bool flag = false;
int H, W, ans = 10000000;
vector<string> field;
int seen[510][510];
void dfs(int h, int w, int gh, int gw) {
rep(dir, 2) {
flag = false;
int nh = h + dy[dir];
int nw = w + dx[dir];
if (nh < 0 || nh >= H || nw < 0 || nw >= W)
continue;
if (field[h][w] != '#' && field[nh][nw] == '#')
flag = chmin(seen[nh][nw], seen[h][w] + 1);
else
flag = chmin(seen[nh][nw], seen[h][w]);
if (nh == gh && nw == gw)
continue;
if (!flag)
continue;
dfs(nh, nw, gh, gw);
}
}
int main() {
cin >> H >> W;
field.resize(H);
rep(h, H) cin >> field[h];
int sh = 0, sw = 0, gh = H - 1, gw = W - 1;
rep(i, H) rep(j, W) seen[i][j] = 1000;
seen[0][0] = 0;
dfs(sh, sw, gh, gw);
if (field[0][0] == '#')
seen[gh][gw]++;
cout << seen[gh][gw] << endl;
}
| insert | 160 | 160 | 160 | 162 | TLE | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define MOD 1000000007
#define rep(i, n) for (ll(i) = 0LL; (i) < (ll)(n); (i)++)
#define rep2(i, s, e) for (ll(i) = (ll)(s); (i) < (ll)(e); (i)++)
#define repi(i, n) for (ll(i) = 0LL; (i) <= (ll)(n); (i)++)
#define repi2(i, s, e) for (ll(i) = (ll)(s); (i) <= (ll)(e); (i)++)
#define per(i, n) for (ll(i) = (ll)(n)-1LL; (i) >= 0LL; (i)--)
#define per2(i, s, e) for (ll(i) = (ll)(s)-1LL; (i) >= (ll)(e); (i)--)
#define peri(i, n) for (ll(i) = (ll)(n); (i) >= 0LL; (i)--)
#define peri2(i, s, e) for (ll(i) = (ll)(s); (i) >= (ll)(e); (i)--)
#define iter(i, it) for (auto &(i) : (it))
template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> m) {
cout << "(" << m.first << ", " << m.second << ")";
return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const map<T, U> m) {
ll c = 0;
cout << "{ ";
iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
cout << "}";
return s;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
cout << "{ ";
rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
cout << "}";
return s;
}
template <typename T> ostream &operator<<(ostream &s, const list<T> &v) {
ll c = 0;
cout << "{ ";
iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
cout << "}";
return s;
}
const pair<ll, ll> dir[] = {{1, 0}, {0, 1}};
int main(void) {
ll H, W;
vector<string> S;
vector<vector<ll>> dp;
cin >> H >> W;
S.resize(H);
dp.resize(H, vector<ll>(W, numeric_limits<ll>::max() - 1));
rep(h, H) cin >> S[h];
if (S[0][0] == '#')
dp[0][0] = 1LL;
else
dp[0][0] = 0LL;
rep(h, H) rep(w, W) {
rep(i, 2) {
ll x = w + dir[i].first;
ll y = h + dir[i].second;
if (x >= W || y >= H)
continue;
dp[x][y] = min(dp[x][y],
dp[h][w] + (S[x][y] == '#' && S[h][w] == '.' ? 1LL : 0LL));
}
}
cout << dp[H - 1][W - 1] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define MOD 1000000007
#define rep(i, n) for (ll(i) = 0LL; (i) < (ll)(n); (i)++)
#define rep2(i, s, e) for (ll(i) = (ll)(s); (i) < (ll)(e); (i)++)
#define repi(i, n) for (ll(i) = 0LL; (i) <= (ll)(n); (i)++)
#define repi2(i, s, e) for (ll(i) = (ll)(s); (i) <= (ll)(e); (i)++)
#define per(i, n) for (ll(i) = (ll)(n)-1LL; (i) >= 0LL; (i)--)
#define per2(i, s, e) for (ll(i) = (ll)(s)-1LL; (i) >= (ll)(e); (i)--)
#define peri(i, n) for (ll(i) = (ll)(n); (i) >= 0LL; (i)--)
#define peri2(i, s, e) for (ll(i) = (ll)(s); (i) >= (ll)(e); (i)--)
#define iter(i, it) for (auto &(i) : (it))
template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> m) {
cout << "(" << m.first << ", " << m.second << ")";
return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const map<T, U> m) {
ll c = 0;
cout << "{ ";
iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
cout << "}";
return s;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
cout << "{ ";
rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
cout << "}";
return s;
}
template <typename T> ostream &operator<<(ostream &s, const list<T> &v) {
ll c = 0;
cout << "{ ";
iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
cout << "}";
return s;
}
const pair<ll, ll> dir[] = {{1, 0}, {0, 1}};
int main(void) {
ll H, W;
vector<string> S;
vector<vector<ll>> dp;
cin >> H >> W;
S.resize(H);
dp.resize(H, vector<ll>(W, numeric_limits<ll>::max() - 1));
rep(h, H) cin >> S[h];
if (S[0][0] == '#')
dp[0][0] = 1LL;
else
dp[0][0] = 0LL;
rep(h, H) rep(w, W) {
rep(i, 2) {
ll x = w + dir[i].first;
ll y = h + dir[i].second;
if (x >= W || y >= H)
continue;
dp[y][x] = min(dp[y][x],
dp[h][w] + (S[y][x] == '#' && S[h][w] == '.' ? 1LL : 0LL));
}
}
cout << dp[H - 1][W - 1] << endl;
return 0;
}
| replace | 66 | 68 | 66 | 68 | 0 | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<string>> ve_ves;
#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 rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pub push_back
#define pob pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) max_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
char maze[105][105];
int N, M;
int sx, sy; // start
int gx, gy; // goal
int d[105][105]; // 各pointまでの最短距離
int dx[] = {0, 1};
int dy[] = {1, 0};
// startが大量にある場合
// https://atcoder.jp/contests/qupc2018/tasks/qupc2018_c
int bfs() {
queue<i_i> que;
rep(i, N) rep(j, M) d[i][j] = INF; // 初期化
que.push(i_i(sx, sy)); // que->start
d[sx][sy] = 0; // startは距離0
// queが空になるまでloop
while (que.size()) {
// queの先頭を取りだす
i_i p = que.front();
que.pop();
// getがgoalならend
if (p.fs == gx and p.sc == gy)
break;
rep(i, 2) {
int nx = p.fs + dx[i];
int ny = p.sc + dy[i];
if (0 <= nx and nx < N and 0 <= ny and ny < M) {
if (d[p.fs][p.sc] > d[nx][ny])
continue;
if (maze[p.fs][p.sc] == '#' and maze[nx][ny] == '.') {
if (d[p.fs][p.sc] < d[nx][ny]) {
d[nx][ny] = d[p.fs][p.sc];
que.push(i_i(nx, ny));
} else
continue;
} else if (maze[p.fs][p.sc] == '.' and maze[nx][ny] == '#') {
if (d[p.fs][p.sc] + 1 < d[nx][ny]) {
d[nx][ny] = d[p.fs][p.sc] + 1;
que.push(i_i(nx, ny));
} else
continue;
} else {
d[nx][ny] = d[p.fs][p.sc];
que.push(i_i(nx, ny));
}
// cout << nx << " " << ny << " " << d[nx][ny] << endl;
}
}
}
return d[gx][gy];
}
// AGC033A
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
rep(i, N) rep(j, M) { cin >> maze[i][j]; }
sx = 0;
sy = 0;
gx = N - 1;
gy = M - 1;
int ans = bfs();
if (maze[0][0] == '#')
ans++;
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<string>> ve_ves;
#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 rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pub push_back
#define pob pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) max_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
char maze[105][105];
int N, M;
int sx, sy; // start
int gx, gy; // goal
int d[105][105]; // 各pointまでの最短距離
int dx[] = {0, 1};
int dy[] = {1, 0};
// startが大量にある場合
// https://atcoder.jp/contests/qupc2018/tasks/qupc2018_c
int bfs() {
queue<i_i> que;
rep(i, N) rep(j, M) d[i][j] = INF; // 初期化
que.push(i_i(sx, sy)); // que->start
d[sx][sy] = 0; // startは距離0
// queが空になるまでloop
while (que.size()) {
// queの先頭を取りだす
i_i p = que.front();
que.pop();
// getがgoalならend
if (p.fs == gx and p.sc == gy)
break;
rep(i, 2) {
int nx = p.fs + dx[i];
int ny = p.sc + dy[i];
if (0 <= nx and nx < N and 0 <= ny and ny < M) {
if (d[p.fs][p.sc] >= d[nx][ny])
continue;
if (maze[p.fs][p.sc] == '#' and maze[nx][ny] == '.') {
if (d[p.fs][p.sc] < d[nx][ny]) {
d[nx][ny] = d[p.fs][p.sc];
que.push(i_i(nx, ny));
} else
continue;
} else if (maze[p.fs][p.sc] == '.' and maze[nx][ny] == '#') {
if (d[p.fs][p.sc] + 1 < d[nx][ny]) {
d[nx][ny] = d[p.fs][p.sc] + 1;
que.push(i_i(nx, ny));
} else
continue;
} else {
d[nx][ny] = d[p.fs][p.sc];
que.push(i_i(nx, ny));
}
// cout << nx << " " << ny << " " << d[nx][ny] << endl;
}
}
}
return d[gx][gy];
}
// AGC033A
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
rep(i, N) rep(j, M) { cin >> maze[i][j]; }
sx = 0;
sy = 0;
gx = N - 1;
gy = M - 1;
int ans = bfs();
if (maze[0][0] == '#')
ans++;
cout << ans << endl;
return 0;
} | replace | 63 | 64 | 63 | 64 | TLE | |
p02735 | C++ | Runtime Error | #pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define FAST \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define FIXED cout << fixed << setprecision(12)
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define graph vector<vector<int>>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define f first
#define s second
#define hashmap unordered_map
#define hashset unordered_set
#define eps 1e-9
#define mod 1000000007
#define inf 3000000000000000007ll
#define sz(a) signed(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#ifdef DEBUG
mt19937 gen(857204);
#else
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
template <class T, class U> inline void checkmin(T &x, U y) {
if (y < x)
x = y;
}
template <class T, class U> inline void checkmax(T &x, U y) {
if (y > x)
x = y;
}
template <class T, class U> inline bool ifmax(T &x, U y) {
if (y > x)
return x = y, true;
return false;
}
template <class T, class U> inline bool ifmin(T &x, U y) {
if (y < x)
return x = y, true;
return false;
}
template <class T> inline void sort(T &a) { sort(all(a)); }
template <class T> inline void rsort(T &a) { sort(rall(a)); }
template <class T> inline void reverse(T &a) { reverse(all(a)); }
template <class T, class U>
inline istream &operator>>(istream &str, pair<T, U> &p) {
return str >> p.f >> p.s;
}
template <class T> inline istream &operator>>(istream &str, vector<T> &a) {
for (auto &i : a)
str >> i;
return str;
}
template <class T> inline T sorted(T a) {
sort(a);
return a;
}
signed main() {
FAST;
FIXED;
int n, m;
cin >> n >> m;
vector<string> a(n);
cin >> a;
vector<vector<vector<pair<int, pii>>>> go(n,
vector<vector<pair<int, pii>>>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (a[i][j] == '.') {
if (i < n - 1) {
if (a[i + 1][j] == '#') {
go[i][j].pb({1, {i + 1, j}});
} else {
go[i][j].pb({0, {i + 1, j}});
}
}
if (j < m - 1) {
if (a[i][j + 1] == '#') {
go[i][j].pb({1, {i, j + 1}});
} else {
go[i][j].pb({0, {i, j + 1}});
}
}
} else {
if (i < n - 1)
go[i][j].pb({0, {i + 1, j}});
if (j < m - 1)
go[i][j].pb({0, {i, j + 1}});
}
}
for (auto &i : go)
for (auto &j : i)
sort(j);
deque<pii> que;
que.pb({0, 0});
vector<vector<int>> dist(n, vector<int>(m, mod));
dist[0][0] = 0;
while (sz(que)) {
auto f = que.front();
que.popf();
for (auto i : go[f.f][f.s])
if (ifmin(dist[i.s.f][i.s.s], dist[f.f][f.s] + i.f)) {
if (i.f == 0)
que.push_front(i.s);
else
que.push_back(i.s);
}
}
cout << dist[n - 1][m - 1] + (a[0][0] == '#');
#ifdef DEBUG
cerr << "Runtime is: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
return 0;
} | #pragma GCC optimize("O3", "unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define FAST \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define FIXED cout << fixed << setprecision(12)
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define graph vector<vector<int>>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define f first
#define s second
#define hashmap unordered_map
#define hashset unordered_set
#define eps 1e-9
#define mod 1000000007
#define inf 3000000000000000007ll
#define sz(a) signed(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#ifdef DEBUG
mt19937 gen(857204);
#else
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
template <class T, class U> inline void checkmin(T &x, U y) {
if (y < x)
x = y;
}
template <class T, class U> inline void checkmax(T &x, U y) {
if (y > x)
x = y;
}
template <class T, class U> inline bool ifmax(T &x, U y) {
if (y > x)
return x = y, true;
return false;
}
template <class T, class U> inline bool ifmin(T &x, U y) {
if (y < x)
return x = y, true;
return false;
}
template <class T> inline void sort(T &a) { sort(all(a)); }
template <class T> inline void rsort(T &a) { sort(rall(a)); }
template <class T> inline void reverse(T &a) { reverse(all(a)); }
template <class T, class U>
inline istream &operator>>(istream &str, pair<T, U> &p) {
return str >> p.f >> p.s;
}
template <class T> inline istream &operator>>(istream &str, vector<T> &a) {
for (auto &i : a)
str >> i;
return str;
}
template <class T> inline T sorted(T a) {
sort(a);
return a;
}
signed main() {
FAST;
FIXED;
int n, m;
cin >> n >> m;
vector<string> a(n);
cin >> a;
vector<vector<vector<pair<int, pii>>>> go(n,
vector<vector<pair<int, pii>>>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (a[i][j] == '.') {
if (i < n - 1) {
if (a[i + 1][j] == '#') {
go[i][j].pb({1, {i + 1, j}});
} else {
go[i][j].pb({0, {i + 1, j}});
}
}
if (j < m - 1) {
if (a[i][j + 1] == '#') {
go[i][j].pb({1, {i, j + 1}});
} else {
go[i][j].pb({0, {i, j + 1}});
}
}
} else {
if (i < n - 1)
go[i][j].pb({0, {i + 1, j}});
if (j < m - 1)
go[i][j].pb({0, {i, j + 1}});
}
}
for (auto &i : go)
for (auto &j : i)
sort(j);
deque<pii> que;
que.pb({0, 0});
vector<vector<int>> dist(n, vector<int>(m, mod));
dist[0][0] = 0;
while (sz(que)) {
auto f = que.front();
que.popf();
for (auto i : go[f.f][f.s])
if (ifmin(dist[i.s.f][i.s.s], dist[f.f][f.s] + i.f)) {
if (i.f == 0)
que.push_front(i.s);
else
que.push_back(i.s);
}
}
cout << dist[n - 1][m - 1] + (a[0][0] == '#');
#ifdef DEBUG
cerr << "Runtime is: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
return 0;
} | delete | 1 | 2 | 1 | 1 | 0 | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\n'
#define pl cout << endl;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int dp[105][105];
int main() {
#ifndef ONLINE_JUDGE
freopen("D:\\Ajay\\Codes\\in.txt", "r", stdin);
freopen("D:\\Ajay\\Codes\\out.txt", "w", stdout);
#endif
fast();
int r, c;
cin >> r >> c;
string s[105];
for (int i = 0; i < r; i++)
cin >> s[i];
if (s[0][0] == '#')
dp[0][0] = 1;
else
dp[0][0] = 0;
for (int i = 1; i < c; i++) {
if (s[0][i] == '.') {
dp[0][i] = dp[0][i - 1];
} else {
if (s[0][i - 1] == '#')
dp[0][i] = dp[0][i - 1];
else
dp[0][i] = dp[0][i - 1] + 1;
}
}
for (int i = 1; i < r; i++) {
if (s[i][0] == '.') {
dp[i][0] = dp[i - 1][0];
} else {
if (s[i - 1][0] == '#')
dp[i][0] = dp[i - 1][0];
else
dp[i][0] = dp[i - 1][0] + 1;
}
}
for (int i = 1; i < r; i++) {
for (int j = 1; j < c; j++) {
if (s[i][j] == '#') {
if (s[i - 1][j] == '#') {
if (s[i][j - 1] == '#') {
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]);
} else {
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1] + 1);
}
} else {
if (s[i][j - 1] == '#') {
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1]);
} else {
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
}
}
} else {
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]);
}
}
}
cout << dp[r - 1][c - 1];
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\n'
#define pl cout << endl;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int dp[105][105];
int main() {
fast();
int r, c;
cin >> r >> c;
string s[105];
for (int i = 0; i < r; i++)
cin >> s[i];
if (s[0][0] == '#')
dp[0][0] = 1;
else
dp[0][0] = 0;
for (int i = 1; i < c; i++) {
if (s[0][i] == '.') {
dp[0][i] = dp[0][i - 1];
} else {
if (s[0][i - 1] == '#')
dp[0][i] = dp[0][i - 1];
else
dp[0][i] = dp[0][i - 1] + 1;
}
}
for (int i = 1; i < r; i++) {
if (s[i][0] == '.') {
dp[i][0] = dp[i - 1][0];
} else {
if (s[i - 1][0] == '#')
dp[i][0] = dp[i - 1][0];
else
dp[i][0] = dp[i - 1][0] + 1;
}
}
for (int i = 1; i < r; i++) {
for (int j = 1; j < c; j++) {
if (s[i][j] == '#') {
if (s[i - 1][j] == '#') {
if (s[i][j - 1] == '#') {
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]);
} else {
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1] + 1);
}
} else {
if (s[i][j - 1] == '#') {
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1]);
} else {
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
}
}
} else {
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]);
}
}
}
cout << dp[r - 1][c - 1];
} | delete | 22 | 26 | 22 | 22 | 0 | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
int n, m;
char s[105][105];
int f[105][105][205];
bool vis[105][105][205];
int F(int x, int y, int i) {
if (x >= n || y >= m)
return 1e9;
int &ans = f[x][y][i];
if (vis[x][y][i])
return ans;
ans = 1e9;
int cur = 0;
if (s[x][y] ^ i) {
cur = 1;
i++;
}
if (x == n - 1 && y == m - 1) {
ans = cur;
return ans;
}
for (int ni = 0; ni <= i; ++ni) {
ans = min(ans, F(x + 1, y, ni));
ans = min(ans, F(x, y + 1, ni));
}
ans += cur;
// printf("F(%d %d %d) = %d\n", x, y, i, ans);
return ans;
}
int main() {
ios ::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> s[i];
for (int j = 0; j < m; ++j) {
if (s[i][j] == '#')
s[i][j] = 1;
else
s[i][j] = 0;
}
}
cout << F(0, 0, 0) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
int n, m;
char s[105][105];
int f[105][105][205];
bool vis[105][105][205];
int F(int x, int y, int i) {
if (x >= n || y >= m)
return 1e9;
int &ans = f[x][y][i];
if (vis[x][y][i])
return ans;
vis[x][y][i] = 1;
ans = 1e9;
int cur = 0;
if (s[x][y] ^ i) {
cur = 1;
i++;
}
if (x == n - 1 && y == m - 1) {
ans = cur;
return ans;
}
for (int ni = 0; ni <= i; ++ni) {
ans = min(ans, F(x + 1, y, ni));
ans = min(ans, F(x, y + 1, ni));
}
ans += cur;
// printf("F(%d %d %d) = %d\n", x, y, i, ans);
return ans;
}
int main() {
ios ::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> s[i];
for (int j = 0; j < m; ++j) {
if (s[i][j] == '#')
s[i][j] = 1;
else
s[i][j] = 0;
}
}
cout << F(0, 0, 0) << endl;
return 0;
} | insert | 18 | 18 | 18 | 19 | TLE | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;
typedef vector<vector<string>> vvs;
typedef vector<vector<char>> vvc;
typedef vector<vector<bool>> vvb;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define vrep(v, n) \
for (int i = 0; i < n; i++) { \
cin >> v.at(i); \
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define pb push_back
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
const int mod = 1000000007;
const int inf = 1e9;
#define PI 3.14159265369;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};
int tx[2] = {0, 1};
int ty[2] = {1, 0};
struct route {
int x, y, num;
};
int main() {
int h, w;
cin >> h >> w;
char maze[h][w];
rep(i, h) rep(j, w) cin >> maze[i][j];
int ch[h][w];
rep(i, h) rep(j, w) ch[i][j] = inf;
ch[0][0] = 0;
queue<route> q;
route e = {0, 0, 0};
q.push(e);
while (!q.empty()) {
route p = q.front();
q.pop();
int x = p.x;
int y = p.y;
int num = p.num;
if (ch[x][y] < num)
continue;
rep(i, 2) {
int nx = x + tx[i];
int ny = y + ty[i];
if (nx >= 0 && nx < h && ny >= 0 && ny < w) {
int j = num;
if (maze[x][y] != maze[nx][ny])
j++;
if (ch[nx][ny] > num) {
ch[nx][ny] = j;
route r = {nx, ny, j};
q.push(r);
}
}
}
}
cout << (ch[h - 1][w - 1] + 1) / 2 +
(maze[0][0] == '#' && maze[h - 1][w - 1] == '#' ? 1 : 0)
<< endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;
typedef vector<vector<string>> vvs;
typedef vector<vector<char>> vvc;
typedef vector<vector<bool>> vvb;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define vrep(v, n) \
for (int i = 0; i < n; i++) { \
cin >> v.at(i); \
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define pb push_back
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
const int mod = 1000000007;
const int inf = 1e9;
#define PI 3.14159265369;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};
int tx[2] = {0, 1};
int ty[2] = {1, 0};
struct route {
int x, y, num;
};
int main() {
int h, w;
cin >> h >> w;
char maze[h][w];
rep(i, h) rep(j, w) cin >> maze[i][j];
int ch[h][w];
rep(i, h) rep(j, w) ch[i][j] = inf;
ch[0][0] = 0;
queue<route> q;
route e = {0, 0, 0};
q.push(e);
while (!q.empty()) {
route p = q.front();
q.pop();
int x = p.x;
int y = p.y;
int num = p.num;
if (ch[x][y] < num)
continue;
rep(i, 2) {
int nx = x + tx[i];
int ny = y + ty[i];
if (nx >= 0 && nx < h && ny >= 0 && ny < w) {
int j = num;
if (maze[x][y] != maze[nx][ny])
j++;
if (ch[nx][ny] > j) {
ch[nx][ny] = j;
route r = {nx, ny, j};
q.push(r);
}
}
}
}
cout << (ch[h - 1][w - 1] + 1) / 2 +
(maze[0][0] == '#' && maze[h - 1][w - 1] == '#' ? 1 : 0)
<< endl;
} | replace | 81 | 82 | 81 | 82 | TLE | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << "\n"
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\n";
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(i, v.size()) cerr << " " << v[i]; \
cerr << "\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
vector<P> factorize(ll n) {
vector<P> result;
for (ll i = 2; i * i <= n; ++i) {
if (n % i == 0) {
result.pb({i, 0});
while (n % i == 0) {
n /= i;
result.back().second++;
}
}
}
if (n != 1) {
result.pb({n, 1});
}
return result;
}
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
SORT(ret);
return (ret);
}
const int mod = 1000000007;
struct mint {
ll x;
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; }
struct combination {
vector<mint> fact, ifact;
combination(ll n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (ll i = 1; i <= n; ++i) {
fact[i] = fact[i - 1] * i;
}
ifact[n] = fact[n].inv();
for (ll i = n; i >= 1; --i) {
ifact[i - 1] = ifact[i] * i;
}
}
mint operator()(ll n, ll k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
struct Z {
ll x;
ll y;
ll count;
// char before;
};
signed main() {
init();
ll H, W;
cin >> H >> W;
vector<string> S(H);
rep(i, H) cin >> S[i];
vvll ans(H, vll(W, INF));
queue<Z> q;
if (S[0][0] == '#') {
q.push({0, 0, 1});
} else {
q.push({0, 0, 0});
}
while (!q.empty()) {
Z z = q.front();
q.pop();
if (ans[z.x][z.y] < z.count)
continue;
ans[z.x][z.y] = min(ans[z.x][z.y], z.count);
if (S[z.x][z.y] == '#') {
if (z.y != W - 1) {
q.push({z.x, z.y + 1, z.count});
}
if (z.x != H - 1) {
q.push({z.x + 1, z.y, z.count});
}
continue;
}
if (z.y != W - 1) {
if (S[z.x][z.y] == S[z.x][z.y + 1]) {
q.push({z.x, z.y + 1, z.count});
} else {
q.push({z.x, z.y + 1, z.count + 1});
}
}
if (z.x != H - 1) {
if (S[z.x][z.y] == S[z.x + 1][z.y]) {
q.push({z.x + 1, z.y, z.count});
} else {
q.push({z.x + 1, z.y, z.count + 1});
}
}
}
COUT(ans[H - 1][W - 1]);
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << "\n"
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\n";
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(i, v.size()) cerr << " " << v[i]; \
cerr << "\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
vector<P> factorize(ll n) {
vector<P> result;
for (ll i = 2; i * i <= n; ++i) {
if (n % i == 0) {
result.pb({i, 0});
while (n % i == 0) {
n /= i;
result.back().second++;
}
}
}
if (n != 1) {
result.pb({n, 1});
}
return result;
}
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
SORT(ret);
return (ret);
}
const int mod = 1000000007;
struct mint {
ll x;
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; }
struct combination {
vector<mint> fact, ifact;
combination(ll n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (ll i = 1; i <= n; ++i) {
fact[i] = fact[i - 1] * i;
}
ifact[n] = fact[n].inv();
for (ll i = n; i >= 1; --i) {
ifact[i - 1] = ifact[i] * i;
}
}
mint operator()(ll n, ll k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
struct Z {
ll x;
ll y;
ll count;
// char before;
};
signed main() {
init();
ll H, W;
cin >> H >> W;
vector<string> S(H);
rep(i, H) cin >> S[i];
vvll ans(H, vll(W, INF));
queue<Z> q;
if (S[0][0] == '#') {
q.push({0, 0, 1});
} else {
q.push({0, 0, 0});
}
while (!q.empty()) {
Z z = q.front();
q.pop();
if (ans[z.x][z.y] <= z.count)
continue;
ans[z.x][z.y] = min(ans[z.x][z.y], z.count);
if (S[z.x][z.y] == '#') {
if (z.y != W - 1) {
q.push({z.x, z.y + 1, z.count});
}
if (z.x != H - 1) {
q.push({z.x + 1, z.y, z.count});
}
continue;
}
if (z.y != W - 1) {
if (S[z.x][z.y] == S[z.x][z.y + 1]) {
q.push({z.x, z.y + 1, z.count});
} else {
q.push({z.x, z.y + 1, z.count + 1});
}
}
if (z.x != H - 1) {
if (S[z.x][z.y] == S[z.x + 1][z.y]) {
q.push({z.x + 1, z.y, z.count});
} else {
q.push({z.x + 1, z.y, z.count + 1});
}
}
}
COUT(ans[H - 1][W - 1]);
return 0;
} | replace | 155 | 156 | 155 | 156 | TLE | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\n'
void pre() {}
void solve() {
ll n, m;
cin >> n >> m;
ll a[n + 1][m + 1];
memset(a, 0, sizeof a);
for (ll i = 1; i < n + 1; i++) {
for (ll j = 1; j < m + 1; j++) {
char ch;
cin >> ch;
if (ch == '#')
a[i][j] = 1;
}
}
ll dp[n + 1][m + 1];
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= m; j++) {
if (i == 1 and j == 1) {
dp[i][j] = a[i][j];
} else if (i == 1) {
dp[i][j] =
dp[i][j - 1] + (a[i][j] == 1 ? (a[i][j - 1] == 0 ? 1 : 0) : 0);
} else if (j == 1) {
dp[i][j] = dp[i - 1][j] + (a[i][j] == 1 ? a[i - 1][j] == 0 ? 1 : 0 : 0);
} else {
dp[i][j] = dp[i - 1][j] + (a[i][j] == 1 ? a[i - 1][j] == 0 ? 1 : 0 : 0);
dp[i][j] = min(dp[i][j],
dp[i][j - 1] +
(a[i][j] == 1 ? a[i][j - 1] == 0 ? 1ll : 0ll : 0ll));
}
}
}
// for(ll i=1;i<n+1;i++){
// for(ll j=1;j<m+1;j++){
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
cout << dp[n][m] << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input00.txt", "r", stdin);
#endif
IOS;
pre();
ll T = 1;
// cin>>T;
while (T--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\n'
void pre() {}
void solve() {
ll n, m;
cin >> n >> m;
ll a[n + 1][m + 1];
memset(a, 0, sizeof a);
for (ll i = 1; i < n + 1; i++) {
for (ll j = 1; j < m + 1; j++) {
char ch;
cin >> ch;
if (ch == '#')
a[i][j] = 1;
}
}
ll dp[n + 1][m + 1];
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= m; j++) {
if (i == 1 and j == 1) {
dp[i][j] = a[i][j];
} else if (i == 1) {
dp[i][j] =
dp[i][j - 1] + (a[i][j] == 1 ? (a[i][j - 1] == 0 ? 1 : 0) : 0);
} else if (j == 1) {
dp[i][j] = dp[i - 1][j] + (a[i][j] == 1 ? a[i - 1][j] == 0 ? 1 : 0 : 0);
} else {
dp[i][j] = dp[i - 1][j] + (a[i][j] == 1 ? a[i - 1][j] == 0 ? 1 : 0 : 0);
dp[i][j] = min(dp[i][j],
dp[i][j - 1] +
(a[i][j] == 1 ? a[i][j - 1] == 0 ? 1ll : 0ll : 0ll));
}
}
}
// for(ll i=1;i<n+1;i++){
// for(ll j=1;j<m+1;j++){
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
cout << dp[n][m] << endl;
}
int main() {
#ifdef ONLINE_JUDGE
freopen("input00.txt", "r", stdin);
#endif
IOS;
pre();
ll T = 1;
// cin>>T;
while (T--) {
solve();
}
return 0;
} | replace | 64 | 65 | 64 | 65 | -11 | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000000;
constexpr ll INF = 1e18;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;
int H, W;
vector<vector<char>> vec(110, vector<char>(110));
vector<vector<int>> ret(110, vector<int>(110, -1));
vector<int> dy = {0, 1};
vector<int> dx = {1, 0};
void dfs() {
if (vec.at(0).at(0) == '#') {
ret.at(0).at(0) = 1;
} else {
ret.at(0).at(0) = 0;
}
queue<P> que;
que.push(P(0, 0));
while (!(que.empty())) {
P p = que.front();
que.pop();
for (int i = 0; i < 2; i++) {
int ny = p.first + dy[i];
int nx = p.second + dx[i];
if (ny < H && nx < W) {
int cnt = ret[p.first][p.second];
if (vec[p.first][p.second] == '.' && vec[ny][nx] == '#') {
cnt++;
}
if (cnt <= ret[ny][nx] || ret[ny][nx] == -1) {
que.push(P(ny, nx));
ret[ny][nx] = cnt;
}
}
}
}
}
int main() {
cin >> H >> W;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> vec.at(i).at(j);
}
}
dfs();
cout << ret[H - 1][W - 1] << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000000;
constexpr ll INF = 1e18;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;
int H, W;
vector<vector<char>> vec(110, vector<char>(110));
vector<vector<int>> ret(110, vector<int>(110, -1));
vector<int> dy = {0, 1};
vector<int> dx = {1, 0};
void dfs() {
if (vec.at(0).at(0) == '#') {
ret.at(0).at(0) = 1;
} else {
ret.at(0).at(0) = 0;
}
queue<P> que;
que.push(P(0, 0));
while (!(que.empty())) {
P p = que.front();
que.pop();
for (int i = 0; i < 2; i++) {
int ny = p.first + dy[i];
int nx = p.second + dx[i];
if (ny < H && nx < W) {
int cnt = ret[p.first][p.second];
if (vec[p.first][p.second] == '.' && vec[ny][nx] == '#') {
cnt++;
}
if (cnt < ret[ny][nx] || ret[ny][nx] == -1) {
que.push(P(ny, nx));
ret[ny][nx] = cnt;
}
}
}
}
}
int main() {
cin >> H >> W;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> vec.at(i).at(j);
}
}
dfs();
cout << ret[H - 1][W - 1] << endl;
} | replace | 34 | 35 | 34 | 35 | TLE | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define MOD (1000000007)
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vi>
#define vvl vector<vl>
#define vvb vector<vb>
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define pb push_back
#define sz(s) (int)(s.size())
#define mp make_pair
#define all(a) (a).begin(), (a).end()
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll keta(ll n) {
string s = to_string(n);
ll num = s.size();
return num;
}
const ll INF = 1LL << 60;
struct Edge {
int to;
int weight;
Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
using P = pair<ll, int>;
class UnionFind {
public:
vi Parent, diff_weight;
UnionFind(int n) {
Parent = vi(n, -1);
diff_weight = vi(n, 0);
}
int root(int a) {
if (Parent[a] < 0)
return a;
int r = root(Parent[a]);
diff_weight[a] += diff_weight[Parent[a]];
return Parent[a] = r;
}
int weight(int a) {
root(a);
return diff_weight[a];
}
bool issame(int a, int b) { return root(a) == root(b); }
int size(int a) { return -Parent[root(a)]; }
bool merge(int a, int b, int w = 0) {
w += weight(a);
w -= weight(b);
a = root(a);
b = root(b);
if (a == b)
return false;
if (size(a) < size(b))
swap(a, b), w = -w;
Parent[a] += Parent[b];
Parent[b] = a;
diff_weight[b] = w;
return true;
}
int diff(int a, int b) { return weight(b) - weight(a); }
};
vi MP(string s) {
vi A(s.size() + 1);
A[0] = -1;
int j = -1;
rep(i, s.size()) {
while (j >= 0 && s[i] != s[j])
j = A[j];
j++;
A[i + 1] = j;
}
return A;
}
vi Manacher(string s) {
vi R(s.size());
int i = 0, j = 0;
while (i < s.size()) {
while (i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j])
++j;
R[i] = j;
int k = 1;
while (i - k >= 0 && i + k < s.size() && k + R[i - k] < j)
R[i + k] = R[i - k], k++;
i += k;
j -= k;
}
return R;
}
vi Z_algorithm(string &s) {
vi Z(s.size());
Z[0] = s.size();
int i = 1, j = 0;
while (i < s.size()) {
while (i + j < s.size() && s[j] == s[i + j])
j++;
Z[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < s.size() && k + Z[k] < j)
Z[i + k] = Z[k], ++k;
i += k;
j -= k;
}
return Z;
}
const int MAX = 1e6 + 1;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(ll n, ll k) {
if (n >= MAX) {
ll tmp = 1;
rep(i, k) {
tmp *= (n - i);
tmp %= MOD;
}
return tmp * finv[k] % MOD;
}
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll POW(ll x, ll n) {
ll ret = 1;
if (n < 0) { // n < 0 にも対応
n *= -1;
x = inv[x];
}
while (0 < n) {
if (n % 2 == 0) {
x = x * x % MOD;
n /= 2;
} else {
ret = ret * x % MOD;
n--;
}
}
return ret;
}
const int dh[2] = {1, 0};
const int dw[2] = {0, 1};
int main() {
int H, W;
cin >> H >> W;
vector<string> s(H);
rep(h, H) cin >> s[h];
int dist[H][W][2];
rep(h, H) rep(w, W) rep(i, 2) dist[h][w][i] = 1e9;
rep(i, 2) dist[0][0][i] = 0;
if (s[0][0] == '#') {
rep(i, 2) dist[0][0][i]++;
}
queue<pii> q;
q.push(mp(0, 0));
while (!q.empty()) {
pii v = q.front();
q.pop();
int h = v.first;
int w = v.second;
rep(i, 2) {
// 状態 dist[h][w][i] を配る
rep(dir, 2) {
int nh = h + dh[dir];
int nw = w + dw[dir];
if (nh < 0 || nh == H || nw < 0 || nw == W)
continue;
if (s[h][w] == '.' && s[nh][nw] == '#')
chmin(dist[nh][nw][dir], dist[h][w][i] + 1);
else if (s[h][w] == '#' && s[nh][nw] == '#' && i != dir) {
chmin(dist[nh][nw][dir], dist[h][w][i] + 1);
} else
chmin(dist[nh][nw][dir], dist[h][w][i]);
q.push(mp(nh, nw));
}
}
}
cout << min(dist[H - 1][W - 1][0], dist[H - 1][W - 1][1]) << endl;
}
| #include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define MOD (1000000007)
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vi>
#define vvl vector<vl>
#define vvb vector<vb>
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define pb push_back
#define sz(s) (int)(s.size())
#define mp make_pair
#define all(a) (a).begin(), (a).end()
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll keta(ll n) {
string s = to_string(n);
ll num = s.size();
return num;
}
const ll INF = 1LL << 60;
struct Edge {
int to;
int weight;
Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
using P = pair<ll, int>;
class UnionFind {
public:
vi Parent, diff_weight;
UnionFind(int n) {
Parent = vi(n, -1);
diff_weight = vi(n, 0);
}
int root(int a) {
if (Parent[a] < 0)
return a;
int r = root(Parent[a]);
diff_weight[a] += diff_weight[Parent[a]];
return Parent[a] = r;
}
int weight(int a) {
root(a);
return diff_weight[a];
}
bool issame(int a, int b) { return root(a) == root(b); }
int size(int a) { return -Parent[root(a)]; }
bool merge(int a, int b, int w = 0) {
w += weight(a);
w -= weight(b);
a = root(a);
b = root(b);
if (a == b)
return false;
if (size(a) < size(b))
swap(a, b), w = -w;
Parent[a] += Parent[b];
Parent[b] = a;
diff_weight[b] = w;
return true;
}
int diff(int a, int b) { return weight(b) - weight(a); }
};
vi MP(string s) {
vi A(s.size() + 1);
A[0] = -1;
int j = -1;
rep(i, s.size()) {
while (j >= 0 && s[i] != s[j])
j = A[j];
j++;
A[i + 1] = j;
}
return A;
}
vi Manacher(string s) {
vi R(s.size());
int i = 0, j = 0;
while (i < s.size()) {
while (i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j])
++j;
R[i] = j;
int k = 1;
while (i - k >= 0 && i + k < s.size() && k + R[i - k] < j)
R[i + k] = R[i - k], k++;
i += k;
j -= k;
}
return R;
}
vi Z_algorithm(string &s) {
vi Z(s.size());
Z[0] = s.size();
int i = 1, j = 0;
while (i < s.size()) {
while (i + j < s.size() && s[j] == s[i + j])
j++;
Z[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < s.size() && k + Z[k] < j)
Z[i + k] = Z[k], ++k;
i += k;
j -= k;
}
return Z;
}
const int MAX = 1e6 + 1;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(ll n, ll k) {
if (n >= MAX) {
ll tmp = 1;
rep(i, k) {
tmp *= (n - i);
tmp %= MOD;
}
return tmp * finv[k] % MOD;
}
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll POW(ll x, ll n) {
ll ret = 1;
if (n < 0) { // n < 0 にも対応
n *= -1;
x = inv[x];
}
while (0 < n) {
if (n % 2 == 0) {
x = x * x % MOD;
n /= 2;
} else {
ret = ret * x % MOD;
n--;
}
}
return ret;
}
const int dh[2] = {1, 0};
const int dw[2] = {0, 1};
int main() {
int H, W;
cin >> H >> W;
vector<string> s(H);
rep(h, H) cin >> s[h];
vvi dp(H, vi(W, 1e9));
dp[0][0] = 0;
if (s[0][0] == '#')
dp[0][0]++;
rep(h, H) rep(w, W) {
// dp[h][w] を dp[nh][nw] に配る
rep(dir, 2) {
int nh = h + dh[dir];
int nw = w + dw[dir];
// printf("nh:%d nw:%d\n", nh, nw);
if (nh == H || nw == W)
continue;
if (s[h][w] == '.' && s[nh][nw] == '#')
chmin(dp[nh][nw], dp[h][w] + 1);
else
chmin(dp[nh][nw], dp[h][w]);
}
}
// rep(h,H) rep(w,W) {
// if(w != W-1) printf("%d ", dp[h][w]);
// else printf("%d\n", dp[h][w]);
// }
cout << dp[H - 1][W - 1] << endl;
}
| replace | 219 | 254 | 219 | 245 | TLE | |
p02735 | C++ | Runtime Error | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int H, W;
int i, j;
cin >> H >> W;
vector<vector<int>> dp(H, vector<int>(W, 0));
vector<string> s(H);
for (auto &a : s)
cin >> a;
for (i = 0; i < H; i++) {
for (j = 0; j < W; j++) {
if (i == 0 && j == 0) {
dp[i][j] = (s[i][j] == '#');
} else if (i == 0) {
dp[i][j] = dp[i][j - 1] + (s[i][j] == '#' && s[i][j - 1] != '#');
} else if (j == 0) {
dp[i][j] = dp[i - 1][j] + (s[i][j] == '#' && s[i - 1][j] != '#');
} else {
dp[i][j] = min(dp[i][j - 1] + (s[i][j] == '#' && s[i][j - 1] != '#'),
dp[i - 1][j] + (s[i][j] == '#' && s[i - 1][j] != '#'));
}
}
}
cout << dp[H][W] << endl;
return 0;
} | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int H, W;
int i, j;
cin >> H >> W;
vector<vector<int>> dp(H, vector<int>(W, 0));
vector<string> s(H);
for (auto &a : s)
cin >> a;
for (i = 0; i < H; i++) {
for (j = 0; j < W; j++) {
if (i == 0 && j == 0) {
dp[i][j] = (s[i][j] == '#');
} else if (i == 0) {
dp[i][j] = dp[i][j - 1] + (s[i][j] == '#' && s[i][j - 1] != '#');
} else if (j == 0) {
dp[i][j] = dp[i - 1][j] + (s[i][j] == '#' && s[i - 1][j] != '#');
} else {
dp[i][j] = min(dp[i][j - 1] + (s[i][j] == '#' && s[i][j - 1] != '#'),
dp[i - 1][j] + (s[i][j] == '#' && s[i - 1][j] != '#'));
}
}
}
cout << dp[H - 1][W - 1] << endl;
return 0;
} | replace | 33 | 34 | 33 | 34 | -11 | |
p02735 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
char a[105][105];
int d[105][105];
int dx[] = {0, 1};
int dy[] = {1, 0};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int i = 0; i < 105; i++) {
for (int j = 0; j < 105; j++) {
d[i][j] = numeric_limits<int>::max();
}
}
deque<pair<int, int>> q;
q.push_back({1, 1});
d[1][1] = 0;
while (!q.empty()) {
int y, x;
y = q.front().first;
x = q.front().second;
// auto [y, x] = q.front();
q.pop_front();
for (int i = 0; i < 4; i++) {
int yy = y + dy[i];
int xx = x + dx[i];
if (yy < 1 || xx < 1 || yy > n || xx > m)
continue;
int dist = d[y][x] + (a[y][x] == a[yy][xx] ? 0 : 1);
if (dist < d[yy][xx]) {
if (a[yy][xx] == a[y][x])
q.push_front({yy, xx});
else
q.push_back({yy, xx});
d[yy][xx] = dist;
}
}
}
cout << d[n][m] / 2 + (a[1][1] == '#' || a[n][m] == '#') << endl;
} | #include <bits/stdc++.h>
using namespace std;
char a[105][105];
int d[105][105];
int dx[] = {0, 1};
int dy[] = {1, 0};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int i = 0; i < 105; i++) {
for (int j = 0; j < 105; j++) {
d[i][j] = numeric_limits<int>::max();
}
}
deque<pair<int, int>> q;
q.push_back({1, 1});
d[1][1] = 0;
while (!q.empty()) {
int y, x;
y = q.front().first;
x = q.front().second;
// auto [y, x] = q.front();
q.pop_front();
for (int i = 0; i < 2; i++) {
int yy = y + dy[i];
int xx = x + dx[i];
if (yy < 1 || xx < 1 || yy > n || xx > m)
continue;
int dist = d[y][x] + (a[y][x] == a[yy][xx] ? 0 : 1);
if (dist < d[yy][xx]) {
if (a[yy][xx] == a[y][x])
q.push_front({yy, xx});
else
q.push_back({yy, xx});
d[yy][xx] = dist;
}
}
}
cout << d[n][m] / 2 + (a[1][1] == '#' || a[n][m] == '#') << endl;
} | replace | 37 | 38 | 37 | 38 | 0 | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const string ln = "\n";
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;
int dy[] = {0, 1};
int dx[] = {1, 0};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int h, w;
cin >> h >> w;
vector<string> g(h);
for (int i = 0; i < h; i++) {
cin >> g[i];
}
queue<P> q;
q.push(P(0, 0));
int d[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
d[i][j] = INF;
}
}
d[0][0] = g[0][0] == '.' ? 0 : 1;
while (!q.empty()) {
P v = q.front();
q.pop();
int y = v.first;
int x = v.second;
for (int i = 0; i < 2; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < h && ny < w) {
int next = d[y][x] + (g[ny][nx] == '#' && g[y][x] == '.' ? 1 : 0);
d[ny][nx] = min(next, d[ny][nx]);
q.push(P(ny, nx));
}
}
}
cout << d[h - 1][w - 1] << ln;
return 0;
} | #include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const string ln = "\n";
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;
int dy[] = {0, 1};
int dx[] = {1, 0};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int h, w;
cin >> h >> w;
vector<string> g(h);
for (int i = 0; i < h; i++) {
cin >> g[i];
}
queue<P> q;
q.push(P(0, 0));
int d[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
d[i][j] = INF;
}
}
d[0][0] = g[0][0] == '.' ? 0 : 1;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
for (int i = 0; i < 2; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny < h && nx < w) {
int next = d[y][x] + (g[ny][nx] == '#' && g[y][x] == '.' ? 1 : 0);
d[ny][nx] = min(next, d[ny][nx]);
}
}
}
}
cout << d[h - 1][w - 1] << ln;
return 0;
} | replace | 35 | 51 | 35 | 44 | TLE | |
p02735 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cstring>
#include <functional>
#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 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 MOD (long long int)(1e9 + 7)
#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;
if (n <= 1e7) {
ret *= kaijo(n);
ret %= MOD;
ret *= gyaku_kaijo(r);
ret %= MOD;
ret *= gyaku_kaijo(n - r);
ret %= MOD;
} else {
rep(i, r) {
ret *= n - i;
ret %= MOD;
ret *= mpow(r - i, MOD - 2);
ret %= MOD;
}
}
return ret;
}
int main(void) {
fast_io cout << fixed << setprecision(15);
vector<string> S;
int h, w;
cin >> h >> w;
rep(i, h) {
string s;
cin >> s;
S.push_back(s);
}
int dp[102][102] = {};
rep(i, 102) {
rep(j, 102) { dp[i][j] = INF; }
}
if (S[0][0] == '#') {
dp[1][1] = 1;
} else {
dp[1][1] = 0;
}
reps(i, h) {
reps(j, w) {
dp[i][j] = min(
{dp[i][j],
dp[i - 1][j] + (S[i - 1 - 1][j - 1] == S[i - 1][j - 1] ? 0 : 1),
dp[i][j - 1] + (S[i - 1][j - 1 - 1] == S[i - 1][j - 1] ? 0 : 1)});
}
}
cout << (dp[h][w] + 1) / 2 << endl;
} | #include <algorithm>
#include <bitset>
#include <cstring>
#include <functional>
#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 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 MOD (long long int)(1e9 + 7)
#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;
if (n <= 1e7) {
ret *= kaijo(n);
ret %= MOD;
ret *= gyaku_kaijo(r);
ret %= MOD;
ret *= gyaku_kaijo(n - r);
ret %= MOD;
} else {
rep(i, r) {
ret *= n - i;
ret %= MOD;
ret *= mpow(r - i, MOD - 2);
ret %= MOD;
}
}
return ret;
}
int main(void) {
fast_io cout << fixed << setprecision(15);
vector<string> S;
int h, w;
cin >> h >> w;
rep(i, h) {
string s;
cin >> s;
S.push_back(s);
}
int dp[102][102] = {};
rep(i, 102) {
rep(j, 102) { dp[i][j] = INF; }
}
if (S[0][0] == '#') {
dp[1][1] = 1;
} else {
dp[1][1] = 0;
}
reps(i, h) {
reps(j, w) {
dp[i][j] =
min({dp[i][j],
dp[i - 1][j] +
(i >= 2 && S[i - 1 - 1][j - 1] == S[i - 1][j - 1] ? 0 : 1),
dp[i][j - 1] +
(j >= 2 && S[i - 1][j - 1 - 1] == S[i - 1][j - 1] ? 0 : 1)});
}
}
cout << (dp[h][w] + 1) / 2 << endl;
} | replace | 127 | 131 | 127 | 133 | -11 | |
p02735 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using ll = long long;
const int INF = 1e9;
int h, w;
vector<vector<int>> dp(100, vector<int>(100, INF));
string s[100];
int dx[2] = {1, 0};
int dy[2] = {0, 1};
int dfs(int i, int j) {
if (dp[i][j] != INF)
return dp[i][j];
int res = INF;
rep(k, 2) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < h && ny < w) {
if (s[i][j] == s[nx][ny])
res = min(dfs(nx, ny), res);
else
res = min(dfs(nx, ny) + 1, res);
}
}
return res;
}
int main() {
cin >> h >> w;
rep(i, h) cin >> s[i];
if (s[h - 1][w - 1] == '#')
dp[h - 1][w - 1] = 1;
else
dp[h - 1][w - 1] = 0;
int ans = dfs(0, 0);
if (s[0][0] == '#')
ans++;
cout << ans / 2 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using ll = long long;
const int INF = 1e9;
int h, w;
vector<vector<int>> dp(100, vector<int>(100, INF));
string s[100];
int dx[2] = {1, 0};
int dy[2] = {0, 1};
int dfs(int i, int j) {
if (dp[i][j] != INF)
return dp[i][j];
int res = INF;
rep(k, 2) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < h && ny < w) {
if (s[i][j] == s[nx][ny])
res = min(dfs(nx, ny), res);
else
res = min(dfs(nx, ny) + 1, res);
}
}
return dp[i][j] = res;
}
int main() {
cin >> h >> w;
rep(i, h) cin >> s[i];
if (s[h - 1][w - 1] == '#')
dp[h - 1][w - 1] = 1;
else
dp[h - 1][w - 1] = 0;
int ans = dfs(0, 0);
if (s[0][0] == '#')
ans++;
cout << ans / 2 << endl;
return 0;
}
| replace | 25 | 26 | 25 | 26 | TLE | |
p02736 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int N;
vector<int> pw2;
vector<int> A;
int ff(int a, int b) {
if (a == 1 && b == 2)
return 1;
if (a == 2 && b == 1)
return 1;
return a ^ b;
}
bool isok(int x, int key) {
// code here
if (pw2[x] < key)
return true;
else
return false;
}
int binsch(int key) {
// set value
int ng = N;
int ok = 0;
while (abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (isok(mid, key))
ok = mid;
else
ng = mid;
}
return pw2[ok];
}
int f(int d, int n) {
// cout << d<< " " << n << endl;
if (d == 1)
return A[n - 1];
int dd = binsch(d);
int a = f(d - dd, n);
int b = f(d - dd, n + dd);
return ff(a, b);
}
int main() {
cin >> N;
string S;
cin >> S;
A.resize(N);
rep(i, N) { A[i] = (int)(S[i] - '0'); }
int now = 1;
while (1) {
pw2.pb(now);
if (now >= 200000)
break;
now *= 2;
}
int ans;
if (N % 2 == 0) {
ans = f(N, 1);
} else {
int a = f(N - 1, 1);
int b = f(N - 1, 2);
ans = ff(a, b);
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int N;
vector<int> pw2;
vector<int> A;
int ff(int a, int b) {
if (a == 1 && b == 2)
return 1;
if (a == 2 && b == 1)
return 1;
return a ^ b;
}
bool isok(int x, int key) {
// code here
if (pw2[x] < key)
return true;
else
return false;
}
int binsch(int key) {
// set value
int ng = sz(pw2);
int ok = 0;
while (abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (isok(mid, key))
ok = mid;
else
ng = mid;
}
return pw2[ok];
}
int f(int d, int n) {
// cout << d<< " " << n << endl;
if (d == 1)
return A[n - 1];
int dd = binsch(d);
int a = f(d - dd, n);
int b = f(d - dd, n + dd);
return ff(a, b);
}
int main() {
cin >> N;
string S;
cin >> S;
A.resize(N);
rep(i, N) { A[i] = (int)(S[i] - '0'); }
int now = 1;
while (1) {
pw2.pb(now);
if (now >= 200000)
break;
now *= 2;
}
int ans;
if (N % 2 == 0) {
ans = f(N, 1);
} else {
int a = f(N - 1, 1);
int b = f(N - 1, 2);
ans = ff(a, b);
}
cout << ans << endl;
return 0;
}
| replace | 49 | 50 | 49 | 50 | 0 | |
p02736 | C++ | Runtime Error | // Author: Vamsi Krishna Reddy Satti
// With love for Competitive Programming!
#pragma GCC optimize("Ofast,unroll-loops,-ffloat-store")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
string to_string(const string &s) { return '"' + s + '"'; }
void debug_out() { cout << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cout << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 3
#endif
#define cin_exception cin.exceptions(cin.failbit);
#define cout_precision \
cout.setf(ios::fixed); \
cout.precision(15);
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
using ld = long double;
using vi = vector<int>;
using ll = int_fast64_t;
using ull = uint_fast64_t;
using vl = vector<ll>;
using vul = vector<ull>;
// -----------------------------------------------------------------------------
const int N = 1e6 + 5;
int n;
string s;
int a[N];
int cn[N];
void preprocess() {
for (int i = 1; i < N; ++i) {
if (i & 1)
continue;
cn[i] = cn[i >> 1] + 1;
}
for (int i = 1; i < N; ++i) {
cn[i] += cn[i - 1];
}
}
int ln_nCr(int n, int r) {
assert(n >= r);
return cn[n] - cn[n - r] - cn[r];
}
void solve() {
cin >> n >> s;
for (int i = 0; i < n; ++i) {
a[i] = s[i] - '1';
}
int k = 1;
while (1) {
int p = 0;
for (int i = 0; i < n; ++i) {
if (!(a[i] & 1))
continue;
p ^= (ln_nCr(n - 1, i) == 0);
}
if (p) {
cout << k << '\n';
return;
}
int oe = 0, te = 0;
for (int i = 0; i < n; ++i) {
if (a[i] == 1)
oe = 1;
if (a[i] == 2)
te = 1;
}
if (oe || (!te)) {
cout << 0 << '\n';
return;
}
for (int i = 0; i < n; ++i) {
a[i] >>= 1;
}
++k;
}
}
int main() {
fast_io;
cin_exception;
preprocess();
int t = 1;
// cin >> t;
for (int i = 1; i <= t; ++i) {
solve();
}
} | // Author: Vamsi Krishna Reddy Satti
// With love for Competitive Programming!
// #pragma GCC optimize ("Ofast,unroll-loops,-ffloat-store")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
string to_string(const string &s) { return '"' + s + '"'; }
void debug_out() { cout << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cout << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 3
#endif
#define cin_exception cin.exceptions(cin.failbit);
#define cout_precision \
cout.setf(ios::fixed); \
cout.precision(15);
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
using ld = long double;
using vi = vector<int>;
using ll = int_fast64_t;
using ull = uint_fast64_t;
using vl = vector<ll>;
using vul = vector<ull>;
// -----------------------------------------------------------------------------
const int N = 1e6 + 5;
int n;
string s;
int a[N];
int cn[N];
void preprocess() {
for (int i = 1; i < N; ++i) {
if (i & 1)
continue;
cn[i] = cn[i >> 1] + 1;
}
for (int i = 1; i < N; ++i) {
cn[i] += cn[i - 1];
}
}
int ln_nCr(int n, int r) {
assert(n >= r);
return cn[n] - cn[n - r] - cn[r];
}
void solve() {
cin >> n >> s;
for (int i = 0; i < n; ++i) {
a[i] = s[i] - '1';
}
int k = 1;
while (1) {
int p = 0;
for (int i = 0; i < n; ++i) {
if (!(a[i] & 1))
continue;
p ^= (ln_nCr(n - 1, i) == 0);
}
if (p) {
cout << k << '\n';
return;
}
int oe = 0, te = 0;
for (int i = 0; i < n; ++i) {
if (a[i] == 1)
oe = 1;
if (a[i] == 2)
te = 1;
}
if (oe || (!te)) {
cout << 0 << '\n';
return;
}
for (int i = 0; i < n; ++i) {
a[i] >>= 1;
}
++k;
}
}
int main() {
fast_io;
cin_exception;
preprocess();
int t = 1;
// cin >> t;
for (int i = 1; i <= t; ++i) {
solve();
}
} | replace | 3 | 5 | 3 | 5 | 0 | |
p02736 | C++ | Time Limit Exceeded | #include <algorithm>
#include <stdio.h>
int popcnt(int x) {
x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f);
x = (x & 0x00ff00ff) + ((x >> 8) & 0x00ff00ff);
x = (x & 0x0000ffff) + ((x >> 16) & 0x0000ffff);
return x;
}
int log2(int x) { return popcnt((x & -x) - 1); }
int n;
char a[1000100];
int solve(int div = 1) {
int mod = 0, one = 0, exp = 0;
for (int i = 0; i < n; i++) {
mod ^= (a[i] / div % 2) & (!(bool)exp);
one |= (a[i] / div) == 1;
exp += log2(n - i - 1) - log2(i + 1);
}
if (mod & 1)
return 1;
if (one)
return 0;
return solve(2) * 2;
}
int main() {
scanf("%d", &n);
scanf("%s", a);
for (int i = 0; i < n; i++)
a[i] = a[i] - '0' - 1;
printf("%d\n", solve());
} | #include <algorithm>
#include <stdio.h>
int popcnt(int x) {
x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f);
x = (x & 0x00ff00ff) + ((x >> 8) & 0x00ff00ff);
x = (x & 0x0000ffff) + ((x >> 16) & 0x0000ffff);
return x;
}
int log2(int x) { return popcnt((x & -x) - 1); }
int n;
char a[1000100];
int solve(int div = 1) {
int mod = 0, one = div == 2, exp = 0;
for (int i = 0; i < n; i++) {
mod ^= (a[i] / div % 2) & (!(bool)exp);
one |= (a[i] / div) == 1;
exp += log2(n - i - 1) - log2(i + 1);
}
if (mod & 1)
return 1;
if (one)
return 0;
return solve(2) * 2;
}
int main() {
scanf("%d", &n);
scanf("%s", a);
for (int i = 0; i < n; i++)
a[i] = a[i] - '0' - 1;
printf("%d\n", solve());
} | replace | 16 | 17 | 16 | 17 | TLE | |
p02736 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
template <typename T1, typename T2> inline void chmin(T1 &a, const T2 &b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, const T2 &b) {
if (a < b)
a = b;
}
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
/* do your best */
const long long MAXN = 1001024;
// a, b の最大公約数を返す O( log max(a, b) )
long long gcd(long long a, long long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// ax + by = 1 となるような (x, y) と gcd(a, b) を返す. gcd(a, b) = 1
// の時、解が存在する.
long long extgcd(long long a, long long b, long long &x, long long &y) {
long long d = a;
if (b != 0) {
d = extgcd(b, a, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
// 区間 [a, b) に存在する素数の個数を返す関数
long long prime[MAXN]; // [a,b) の素数のうち i 番目の素数
bool is_prime[MAXN]; // 整数 i が素数であるかどうか
bool is_prime_ab[MAXN]; // 整数 i+a が素数であるかどうか
long long sieve(long long n) {
long long res = 0;
fill(is_prime, is_prime + MAXN, true);
is_prime[0] = is_prime[1] = false; // 0 と 1 は素数ではない。
for (long long i = 2; i <= n; ++i) {
if (!is_prime[i])
continue;
prime[res++] = i;
for (long long j = 2 * i; j <= n; j += i)
is_prime[j] = false; // 素数 i の倍数は素数ではない (ふるい(篩)にかける)
}
return res;
}
/*
long long segment_sieve(long long a, long long b){
fill(is_prime, is_prime + MAXN, true);
fill(is_prime_ab, is_prime_ab + MAXN, true);
for(long long i = 2; i * i <= b - 1; i++) {
if(!is_prime[i]) continue;
for(long long j = 2 * i; j * j <= b - 1; j += i) is_prime[j] = false; //
素数 i で篩にかける for(long long j = a - a % i; j < b; j += i) { if(j < a)
continue; if(is_prime_ab[j-a]) is_prime_ab[j-a] = false; // 素数 i で篩にかける
}
}
long long res = 0;
for(long long i = a; i < b; i++) if(is_prime_ab[i - a]) prime[res++] = i;
return res;
}
*/
// ある整数の約数列挙
vector<long long> divisors(long long n) {
vector<long long> res;
for (long long i = 1; i * i <= n; ++i) {
if (n % i != 0)
continue;
res.push_back(i);
if (n / i == i)
continue; // 上の行で追加済み。
res.push_back(n / i);
}
sort(res.begin(), res.end());
return res;
}
// 素因数分解
map<long long, long long> prime_factors(long long n) {
map<long long, long long> res;
if (n == 1) { // n=1 の素因数分解は n^1
res[n] = 1;
return res;
}
for (long long i = 2, _n = n; i * i <= _n; ++i) {
while (n % i == 0) {
++res[i]; // 素数i^{res[i]}
n /= i;
}
}
if (n != 1)
res[n] = 1;
return res;
}
// 繰り返し 2 乗法
long long modpow(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
// 逆元を求める. a と m は互いに素であることが要請される.
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
long long fac[MAXN], finv[MAXN], inv[MAXN];
// 前処理 O(n)
void math_init() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (long long i = 2; i < MAXN; 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;
}
}
// 二項係数計算 O(1)
long long COM(long long n, long long k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// before your coding, you have to write a line "math_init()"
void naive(vector<int> a) {
int n = a.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
cerr << a[j] << " ";
}
cerr << endl;
for (int j = 0; j < n - i; j++) {
a[j] = (int)abs(a[j] - a[j + 1]);
}
}
}
int f(int a, int b) {
if (a > b)
swap(a, b);
if (a == b)
return 0;
if (a == 0 and b == 1)
return 1;
if (a == 0 and b == 2)
return 2;
if (a == 1 and b == 2)
return 1;
assert(false);
return -1;
}
int P(int n, int m) {
if (n == 0 and m == 0)
return 1;
if (n % 2 == 0 and m % 2 == 1)
return 0;
return P(n / 2, m / 2);
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
char c;
cin >> c;
a[i] = (int)(c - '0');
}
if (n == 2) {
cout << f(a[0], a[1]) << endl;
return 0;
}
// naive(a);
int magic = 1;
for (int i = 0; i < n; i++) {
// assert(i <= 100);
if (i >= 50) {
cout << 0 << endl;
return 0;
}
set<int> st;
for (int j = 0; j < n - i; j++) {
st.insert(a[j]);
}
// 0 を含んで 2 種類なら
if (st.size() == 2 and st.find(0) != st.end()) {
n = n - i;
for (int j = 0; j < n - i; j++) {
if (a[j] != 0) {
magic = a[j];
a[j] = 1;
}
}
break;
}
for (int j = 0; j < n - i; j++) {
a[j] = (int)abs(a[j] - a[j + 1]);
}
}
// cerr << "n = " << n << endl;
lint ans = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 0)
continue;
// n - 1 C i が奇数なら,奇数のまま
if (P(n - 1, i) != 0)
ans ^= 1;
}
// cerr << ans << " " << magic << endl;
cout << ans * magic << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
template <typename T1, typename T2> inline void chmin(T1 &a, const T2 &b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, const T2 &b) {
if (a < b)
a = b;
}
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
/* do your best */
const long long MAXN = 1001024;
// a, b の最大公約数を返す O( log max(a, b) )
long long gcd(long long a, long long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// ax + by = 1 となるような (x, y) と gcd(a, b) を返す. gcd(a, b) = 1
// の時、解が存在する.
long long extgcd(long long a, long long b, long long &x, long long &y) {
long long d = a;
if (b != 0) {
d = extgcd(b, a, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
// 区間 [a, b) に存在する素数の個数を返す関数
long long prime[MAXN]; // [a,b) の素数のうち i 番目の素数
bool is_prime[MAXN]; // 整数 i が素数であるかどうか
bool is_prime_ab[MAXN]; // 整数 i+a が素数であるかどうか
long long sieve(long long n) {
long long res = 0;
fill(is_prime, is_prime + MAXN, true);
is_prime[0] = is_prime[1] = false; // 0 と 1 は素数ではない。
for (long long i = 2; i <= n; ++i) {
if (!is_prime[i])
continue;
prime[res++] = i;
for (long long j = 2 * i; j <= n; j += i)
is_prime[j] = false; // 素数 i の倍数は素数ではない (ふるい(篩)にかける)
}
return res;
}
/*
long long segment_sieve(long long a, long long b){
fill(is_prime, is_prime + MAXN, true);
fill(is_prime_ab, is_prime_ab + MAXN, true);
for(long long i = 2; i * i <= b - 1; i++) {
if(!is_prime[i]) continue;
for(long long j = 2 * i; j * j <= b - 1; j += i) is_prime[j] = false; //
素数 i で篩にかける for(long long j = a - a % i; j < b; j += i) { if(j < a)
continue; if(is_prime_ab[j-a]) is_prime_ab[j-a] = false; // 素数 i で篩にかける
}
}
long long res = 0;
for(long long i = a; i < b; i++) if(is_prime_ab[i - a]) prime[res++] = i;
return res;
}
*/
// ある整数の約数列挙
vector<long long> divisors(long long n) {
vector<long long> res;
for (long long i = 1; i * i <= n; ++i) {
if (n % i != 0)
continue;
res.push_back(i);
if (n / i == i)
continue; // 上の行で追加済み。
res.push_back(n / i);
}
sort(res.begin(), res.end());
return res;
}
// 素因数分解
map<long long, long long> prime_factors(long long n) {
map<long long, long long> res;
if (n == 1) { // n=1 の素因数分解は n^1
res[n] = 1;
return res;
}
for (long long i = 2, _n = n; i * i <= _n; ++i) {
while (n % i == 0) {
++res[i]; // 素数i^{res[i]}
n /= i;
}
}
if (n != 1)
res[n] = 1;
return res;
}
// 繰り返し 2 乗法
long long modpow(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
// 逆元を求める. a と m は互いに素であることが要請される.
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
long long fac[MAXN], finv[MAXN], inv[MAXN];
// 前処理 O(n)
void math_init() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (long long i = 2; i < MAXN; 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;
}
}
// 二項係数計算 O(1)
long long COM(long long n, long long k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// before your coding, you have to write a line "math_init()"
void naive(vector<int> a) {
int n = a.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
cerr << a[j] << " ";
}
cerr << endl;
for (int j = 0; j < n - i; j++) {
a[j] = (int)abs(a[j] - a[j + 1]);
}
}
}
int f(int a, int b) { return (int)abs(a - b); }
int P(int n, int m) {
if (n == 0 and m == 0)
return 1;
if (n % 2 == 0 and m % 2 == 1)
return 0;
return P(n / 2, m / 2);
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
char c;
cin >> c;
a[i] = (int)(c - '0');
}
if (n == 2) {
cout << f(a[0], a[1]) << endl;
return 0;
}
// naive(a);
int magic = 1;
for (int i = 0; i < n; i++) {
// assert(i <= 100);
if (i >= 50) {
cout << 0 << endl;
return 0;
}
set<int> st;
for (int j = 0; j < n - i; j++) {
st.insert(a[j]);
}
// 0 を含んで 2 種類なら
if (st.size() == 2 and st.find(0) != st.end()) {
n = n - i;
for (int j = 0; j < n - i; j++) {
if (a[j] != 0) {
magic = a[j];
a[j] = 1;
}
}
break;
}
for (int j = 0; j < n - i; j++) {
a[j] = (int)abs(a[j] - a[j + 1]);
}
}
// cerr << "n = " << n << endl;
lint ans = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 0)
continue;
// n - 1 C i が奇数なら,奇数のまま
if (P(n - 1, i) != 0)
ans ^= 1;
}
// cerr << ans << " " << magic << endl;
cout << ans * magic << endl;
return 0;
}
| replace | 208 | 223 | 208 | 209 | 0 | |
p02736 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#ifdef LOCAL_DEBUG
#define DEBUG 1
#define CERR \
if (DEBUG) \
cerr
#define MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
#define pr(...) \
CERR << MACRO(__VA_ARGS__, pr10, pr9, pr8, pr7, pr6, pr5, pr4, pr3, pr2, \
pr1)(__VA_ARGS__) \
<< endl
#define pr1(a) (#a) << "=" << (a) << " "
#define pr2(a, b) pr1(a) << pr1(b)
#define pr3(a, b, c) pr1(a) << pr2(b, c)
#define pr4(a, b, c, d) pr1(a) << pr3(b, c, d)
#define pr5(a, b, c, d, e) pr1(a) << pr4(b, c, d, e)
#define pr6(a, b, c, d, e, f) pr1(a) << pr5(b, c, d, e, f)
#define pr7(a, b, c, d, e, f, g) pr1(a) << pr6(b, c, d, e, f, g)
#define pr8(a, b, c, d, e, f, g, h) pr1(a) << pr7(b, c, d, e, f, g, h)
#define pr9(a, b, c, d, e, f, g, h, i) pr1(a) << pr8(b, c, d, e, f, g, h, i)
#define pr10(a, b, c, d, e, f, g, h, i, j) \
pr1(a) << pr9(b, c, d, e, f, g, h, i, j)
#define prArr(a) \
{ \
CERR << (#a) << "={"; \
int i = 0; \
for (auto t : (a)) \
CERR << (i++ ? ", " : "") << t; \
CERR << "}" << endl; \
}
#else
#define DEBUG 0
#define pr(...)
#define prArr(a)
#endif
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL << 60) + 1e9; // ~ 1.15 * 1e18
const Int mod = (1e9) + 7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int, Int>;
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
template <class T1, class T2> ostream &operator<<(ostream &o, pair<T1, T2> p) {
return o << "(" << p.first << "," << p.second << ")";
}
template <class T1, class T2, class T3>
ostream &operator<<(ostream &o, tuple<T1, T2, T3> t) {
return o << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")";
}
template <class T1, class T2> istream &operator>>(istream &i, pair<T1, T2> &p) {
return i >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &o, vector<T> a) {
Int i = 0;
for (T t : a)
o << (i++ ? " " : "") << t;
return o;
}
template <class T> istream &operator>>(istream &i, vector<T> &a) {
for (T &t : a)
i >> t;
return i;
}
template <typename T> class CumulativeSum {
public:
Int n;
vector<T> sum;
vector<T> A;
Int added;
CumulativeSum() : n(-1), added(0) {}
CumulativeSum(Int n) : n(n), sum(n + 1), A(n + 1), added(0) {}
CumulativeSum(const vector<T> &B)
: n(B.size()), sum(n + 1), A(n + 1), added(0) {
for (Int i = 1; i <= n; i++)
sum[i] = sum[i - 1] + B[i - 1];
}
void apply() {
for (Int i = 1; i <= n; i++)
A[i] = A[i] + A[i - 1];
for (Int i = 1; i <= n; i++)
A[i] = A[i] + A[i - 1];
for (Int i = 1; i <= n; i++)
sum[i] = sum[i] + A[i - 1];
added = 0;
A.clear();
A.resize(n + 1);
}
//[l, r)にxを加算
void add(Int l, Int r, T x) {
added = 1;
assert(l <= r && 0 <= l && r <= n);
A[l] = A[l] + x;
A[r] = A[r] - x;
}
//[l, r)の和を得る
T get(Int l, Int r) {
assert(l <= r && 0 <= l && r <= n);
if (added)
apply();
return sum[r] - sum[l];
}
};
// INSERT ABOVE HERE
// 0 0 -> 0
// 1 1 -> 0
// 2 2 -> 0
// 0 1 -> 1
// 1 0 -> 1
// 1 2 -> 1
// 2 1 -> 1
// 0 2 -> 2
// 2 0 -> 2
CumulativeSum<Int> S;
Int nCr(Int n, Int r) {
if (S.n == -1) {
S = CumulativeSum<Int>(2e6);
for (Int i = 0; i < 2e6; i++) {
Int c = 0;
Int t = i;
while (t && t % 2 == 0)
t /= 2, c++;
S.add(i, i + 1, c);
}
}
if (n < r)
return 0;
if (r == 0)
return 1;
if (n == r)
return 1;
Int a = S.get(0, r + 1); // r!
Int b = S.get(n - r + 1, n + 1); // (n!) / (n-r)!
// pr(n, r, a, b);
if (b > a)
return 0;
return 1;
}
Int solve_(Int N, vector<Int> A, Int flag = 0) {
for (Int i = 0; i < N - 1; i++) {
if (i == 0 && flag)
pr(i, A);
vector<Int> nA;
for (Int i = 0; i < (Int)A.size() - 1; i++) {
nA.push_back(abs(A[i] - A[i + 1]));
}
A = nA;
if (flag)
pr(i, A);
}
assert(A.size() == 1);
return A[0];
}
Int solve2(Int N, vector<Int> A, Int flag) {
Int sum = 0;
for (Int i = 0; i < (Int)A.size(); i++) {
Int a = A[i];
Int num = nCr(((Int)A.size() - 1), i);
pr(a, num, A.size(), i);
if (a == 2)
sum ^= num % 2;
}
pr(sum);
return sum ? 2 : 0;
}
Int solve3(Int N, vector<Int> A, Int flag) {
Int sum = 0;
for (Int i = 0; i < (Int)A.size(); i++) {
Int a = A[i];
Int num = nCr(((Int)A.size() - 1), i);
pr(a, num, A.size(), i);
if (a == 3)
sum ^= num % 2;
}
pr(sum);
return sum ? 3 : 0;
}
Int solve(Int, vector<Int>, Int);
Int solve13(Int N, vector<Int> A, Int flag) {
if (A.size() == 1)
return A[0];
vector<Int> nA;
for (Int i = 0; i < (Int)A.size() - 1; i++) {
nA.push_back(abs(A[i] - A[i + 1]));
}
A = nA;
return solve(A.size(), A, flag);
}
Int solve(Int N, vector<Int> A, Int flag = 0) {
if (A == vector<Int>({3}))
return 3;
Int all02 = 1, all03 = 1, all13 = 1;
for (Int i = 0; i < N; i++)
if (!(A[i] == 0 || A[i] == 2))
all02 = 0;
for (Int i = 0; i < N; i++)
if (!(A[i] == 0 || A[i] == 3))
all03 = 0;
for (Int i = 0; i < N; i++)
if (!(A[i] == 1 || A[i] == 3))
all13 = 0;
Int one = 0, thr = 0;
for (Int i = 0; i < N; i++)
if (A[i] == 1)
one = 1;
for (Int i = 0; i < N; i++)
if (A[i] == 3)
thr = 1;
if (all02)
return solve2(N, A, flag);
if (all03)
return solve3(N, A, flag);
if (one && thr && all13)
return solve13(N, A, flag);
for (Int &a : A)
a %= 2;
if (A.size() == 1)
return A[0];
if (flag)
pr(A);
Int sum = 0;
for (Int i = 0; i < (Int)A.size(); i++) {
Int a = A[i];
Int num = nCr(((Int)A.size() - 1), i);
if (flag)
pr(a, num, A.size(), i);
sum ^= a * num % 2;
}
return sum;
}
void test() {
while (1) {
Int N = rand() % 5 + 1;
vector<Int> A(N);
for (Int &a : A)
a = rand() % 3 + 1;
Int a = solve_(N, A);
// if(a == 0 || a == 1) continue;
Int b = solve(N, A);
pr(N, A, a, b);
assert(a == b);
// return;
}
}
signed main() {
srand((unsigned)time(NULL));
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
// test();
Int N;
cin >> N;
string s;
cin >> s;
N = s.size();
vector<Int> A;
for (Int i = 0; i < N; i++)
A.push_back(s[i] - '0');
// Int ans = solve(N, A);
Int ans = solve_(N, A, 1);
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#ifdef LOCAL_DEBUG
#define DEBUG 1
#define CERR \
if (DEBUG) \
cerr
#define MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
#define pr(...) \
CERR << MACRO(__VA_ARGS__, pr10, pr9, pr8, pr7, pr6, pr5, pr4, pr3, pr2, \
pr1)(__VA_ARGS__) \
<< endl
#define pr1(a) (#a) << "=" << (a) << " "
#define pr2(a, b) pr1(a) << pr1(b)
#define pr3(a, b, c) pr1(a) << pr2(b, c)
#define pr4(a, b, c, d) pr1(a) << pr3(b, c, d)
#define pr5(a, b, c, d, e) pr1(a) << pr4(b, c, d, e)
#define pr6(a, b, c, d, e, f) pr1(a) << pr5(b, c, d, e, f)
#define pr7(a, b, c, d, e, f, g) pr1(a) << pr6(b, c, d, e, f, g)
#define pr8(a, b, c, d, e, f, g, h) pr1(a) << pr7(b, c, d, e, f, g, h)
#define pr9(a, b, c, d, e, f, g, h, i) pr1(a) << pr8(b, c, d, e, f, g, h, i)
#define pr10(a, b, c, d, e, f, g, h, i, j) \
pr1(a) << pr9(b, c, d, e, f, g, h, i, j)
#define prArr(a) \
{ \
CERR << (#a) << "={"; \
int i = 0; \
for (auto t : (a)) \
CERR << (i++ ? ", " : "") << t; \
CERR << "}" << endl; \
}
#else
#define DEBUG 0
#define pr(...)
#define prArr(a)
#endif
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL << 60) + 1e9; // ~ 1.15 * 1e18
const Int mod = (1e9) + 7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int, Int>;
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
template <class T1, class T2> ostream &operator<<(ostream &o, pair<T1, T2> p) {
return o << "(" << p.first << "," << p.second << ")";
}
template <class T1, class T2, class T3>
ostream &operator<<(ostream &o, tuple<T1, T2, T3> t) {
return o << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")";
}
template <class T1, class T2> istream &operator>>(istream &i, pair<T1, T2> &p) {
return i >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &o, vector<T> a) {
Int i = 0;
for (T t : a)
o << (i++ ? " " : "") << t;
return o;
}
template <class T> istream &operator>>(istream &i, vector<T> &a) {
for (T &t : a)
i >> t;
return i;
}
template <typename T> class CumulativeSum {
public:
Int n;
vector<T> sum;
vector<T> A;
Int added;
CumulativeSum() : n(-1), added(0) {}
CumulativeSum(Int n) : n(n), sum(n + 1), A(n + 1), added(0) {}
CumulativeSum(const vector<T> &B)
: n(B.size()), sum(n + 1), A(n + 1), added(0) {
for (Int i = 1; i <= n; i++)
sum[i] = sum[i - 1] + B[i - 1];
}
void apply() {
for (Int i = 1; i <= n; i++)
A[i] = A[i] + A[i - 1];
for (Int i = 1; i <= n; i++)
A[i] = A[i] + A[i - 1];
for (Int i = 1; i <= n; i++)
sum[i] = sum[i] + A[i - 1];
added = 0;
A.clear();
A.resize(n + 1);
}
//[l, r)にxを加算
void add(Int l, Int r, T x) {
added = 1;
assert(l <= r && 0 <= l && r <= n);
A[l] = A[l] + x;
A[r] = A[r] - x;
}
//[l, r)の和を得る
T get(Int l, Int r) {
assert(l <= r && 0 <= l && r <= n);
if (added)
apply();
return sum[r] - sum[l];
}
};
// INSERT ABOVE HERE
// 0 0 -> 0
// 1 1 -> 0
// 2 2 -> 0
// 0 1 -> 1
// 1 0 -> 1
// 1 2 -> 1
// 2 1 -> 1
// 0 2 -> 2
// 2 0 -> 2
CumulativeSum<Int> S;
Int nCr(Int n, Int r) {
if (S.n == -1) {
S = CumulativeSum<Int>(2e6);
for (Int i = 0; i < 2e6; i++) {
Int c = 0;
Int t = i;
while (t && t % 2 == 0)
t /= 2, c++;
S.add(i, i + 1, c);
}
}
if (n < r)
return 0;
if (r == 0)
return 1;
if (n == r)
return 1;
Int a = S.get(0, r + 1); // r!
Int b = S.get(n - r + 1, n + 1); // (n!) / (n-r)!
// pr(n, r, a, b);
if (b > a)
return 0;
return 1;
}
Int solve_(Int N, vector<Int> A, Int flag = 0) {
for (Int i = 0; i < N - 1; i++) {
if (i == 0 && flag)
pr(i, A);
vector<Int> nA;
for (Int i = 0; i < (Int)A.size() - 1; i++) {
nA.push_back(abs(A[i] - A[i + 1]));
}
A = nA;
if (flag)
pr(i, A);
}
assert(A.size() == 1);
return A[0];
}
Int solve2(Int N, vector<Int> A, Int flag) {
Int sum = 0;
for (Int i = 0; i < (Int)A.size(); i++) {
Int a = A[i];
Int num = nCr(((Int)A.size() - 1), i);
pr(a, num, A.size(), i);
if (a == 2)
sum ^= num % 2;
}
pr(sum);
return sum ? 2 : 0;
}
Int solve3(Int N, vector<Int> A, Int flag) {
Int sum = 0;
for (Int i = 0; i < (Int)A.size(); i++) {
Int a = A[i];
Int num = nCr(((Int)A.size() - 1), i);
pr(a, num, A.size(), i);
if (a == 3)
sum ^= num % 2;
}
pr(sum);
return sum ? 3 : 0;
}
Int solve(Int, vector<Int>, Int);
Int solve13(Int N, vector<Int> A, Int flag) {
if (A.size() == 1)
return A[0];
vector<Int> nA;
for (Int i = 0; i < (Int)A.size() - 1; i++) {
nA.push_back(abs(A[i] - A[i + 1]));
}
A = nA;
return solve(A.size(), A, flag);
}
Int solve(Int N, vector<Int> A, Int flag = 0) {
if (A == vector<Int>({3}))
return 3;
Int all02 = 1, all03 = 1, all13 = 1;
for (Int i = 0; i < N; i++)
if (!(A[i] == 0 || A[i] == 2))
all02 = 0;
for (Int i = 0; i < N; i++)
if (!(A[i] == 0 || A[i] == 3))
all03 = 0;
for (Int i = 0; i < N; i++)
if (!(A[i] == 1 || A[i] == 3))
all13 = 0;
Int one = 0, thr = 0;
for (Int i = 0; i < N; i++)
if (A[i] == 1)
one = 1;
for (Int i = 0; i < N; i++)
if (A[i] == 3)
thr = 1;
if (all02)
return solve2(N, A, flag);
if (all03)
return solve3(N, A, flag);
if (one && thr && all13)
return solve13(N, A, flag);
for (Int &a : A)
a %= 2;
if (A.size() == 1)
return A[0];
if (flag)
pr(A);
Int sum = 0;
for (Int i = 0; i < (Int)A.size(); i++) {
Int a = A[i];
Int num = nCr(((Int)A.size() - 1), i);
if (flag)
pr(a, num, A.size(), i);
sum ^= a * num % 2;
}
return sum;
}
void test() {
while (1) {
Int N = rand() % 5 + 1;
vector<Int> A(N);
for (Int &a : A)
a = rand() % 3 + 1;
Int a = solve_(N, A);
// if(a == 0 || a == 1) continue;
Int b = solve(N, A);
pr(N, A, a, b);
assert(a == b);
// return;
}
}
signed main() {
srand((unsigned)time(NULL));
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
// test();
Int N;
cin >> N;
string s;
cin >> s;
N = s.size();
vector<Int> A;
for (Int i = 0; i < N; i++)
A.push_back(s[i] - '0');
Int ans = solve(N, A);
// Int ans = solve_(N, A, 1);
cout << ans << endl;
return 0;
}
| replace | 281 | 283 | 281 | 283 | TLE | |
p02736 | C++ | Runtime Error | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
ll to, cost;
};
struct edge2 {
ll from, to, cost;
};
int dtwo[100005], N;
void d_init() {
dtwo[0] = dtwo[1] = 0;
FOR(i, 2, N + 1) {
int now_i = i;
int cnt = 0;
while (now_i % 2 == 0) {
cnt++;
now_i /= 2;
}
dtwo[i] = dtwo[i - 1] + cnt;
}
}
int COM_2(int N, int i) {
int k = dtwo[N] - (dtwo[i] + dtwo[N - i]);
if (k == 0) {
return 1;
} else {
return 0;
}
}
int main() {
cin >> N;
d_init();
string S;
cin >> S;
bool b = true;
rep(i, N) {
S[i] = S[i] - 1;
if (S[i] == '1') {
b = false;
}
}
ll ans = 0;
if (b) {
rep(i, N) {
if (S[i] == '2') {
S[i] = '1';
}
}
} else {
rep(i, N) {
if (S[i] == '2') {
S[i] = '0';
}
}
}
rep(i, N) {
if (S[i] == '1') {
ans += COM_2(N - 1, i);
ans %= 2;
}
}
if (b) {
ans *= 2;
}
cout << ans << endl;
} | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
ll to, cost;
};
struct edge2 {
ll from, to, cost;
};
int dtwo[1000005], N;
void d_init() {
dtwo[0] = dtwo[1] = 0;
FOR(i, 2, N + 1) {
int now_i = i;
int cnt = 0;
while (now_i % 2 == 0) {
cnt++;
now_i /= 2;
}
dtwo[i] = dtwo[i - 1] + cnt;
}
}
int COM_2(int N, int i) {
int k = dtwo[N] - (dtwo[i] + dtwo[N - i]);
if (k == 0) {
return 1;
} else {
return 0;
}
}
int main() {
cin >> N;
d_init();
string S;
cin >> S;
bool b = true;
rep(i, N) {
S[i] = S[i] - 1;
if (S[i] == '1') {
b = false;
}
}
ll ans = 0;
if (b) {
rep(i, N) {
if (S[i] == '2') {
S[i] = '1';
}
}
} else {
rep(i, N) {
if (S[i] == '2') {
S[i] = '0';
}
}
}
rep(i, N) {
if (S[i] == '1') {
ans += COM_2(N - 1, i);
ans %= 2;
}
}
if (b) {
ans *= 2;
}
cout << ans << endl;
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p02736 | C++ | Runtime Error | #include <bits/stdc++.h>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (lld i = a; i <= b; i++)
#define repr(i, a, b) for (lld i = a; i >= b; i--)
#define ar array
#define pb push_back
#define mp make_pair
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
lld n, m;
lld arr[300000];
string st;
int main() {
ios;
lld TESTS, q, a, s, b, r, k, c, p, h, w, d, x, y, z, xs, ys, t;
TESTS = 1;
// ans=0;
// cin>>TESTS;
// memset(dpt,0,sizeof(dpt));memset(dput,0,sizeof(dput));
// memset(dsu1,0,sizeof(dsu1));
while (TESTS--) {
bool cont = false;
cin >> n;
cin >> st;
rep(i, 0, n - 1) if (st[i] == '2') cont = true;
rep(i, 0, n - 1) {
if (cont) {
(st[i] == '1' || st[i] == '3') ? arr[i] = 1 : arr[i] = 0;
} else
(st[i] == '1') ? arr[i] = 1 : arr[i] = 0;
// cout<<arr[i]<<" ";
}
lld change = 0;
rep(i, 0, n - 1) if (arr[i] && ((n - 1) & i) == i) change ^= 1;
(change) ? ((cont) ? cout << 1 : cout << 2) : cout << 0;
}
return 0;
}
| #include <bits/stdc++.h>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (lld i = a; i <= b; i++)
#define repr(i, a, b) for (lld i = a; i >= b; i--)
#define ar array
#define pb push_back
#define mp make_pair
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
lld n, m;
lld arr[3000000];
string st;
int main() {
ios;
lld TESTS, q, a, s, b, r, k, c, p, h, w, d, x, y, z, xs, ys, t;
TESTS = 1;
// ans=0;
// cin>>TESTS;
// memset(dpt,0,sizeof(dpt));memset(dput,0,sizeof(dput));
// memset(dsu1,0,sizeof(dsu1));
while (TESTS--) {
bool cont = false;
cin >> n;
cin >> st;
rep(i, 0, n - 1) if (st[i] == '2') cont = true;
rep(i, 0, n - 1) {
if (cont) {
(st[i] == '1' || st[i] == '3') ? arr[i] = 1 : arr[i] = 0;
} else
(st[i] == '1') ? arr[i] = 1 : arr[i] = 0;
// cout<<arr[i]<<" ";
}
lld change = 0;
rep(i, 0, n - 1) if (arr[i] && ((n - 1) & i) == i) change ^= 1;
(change) ? ((cont) ? cout << 1 : cout << 2) : cout << 0;
}
return 0;
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p02736 | C++ | Runtime Error | /*
_____ _ _ _ _
|_ _| |__ ___ / \ _ __ ___| |__ _ _| |
| | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
| | | | | | __// ___ \| | | \__ \ | | | |_| | |
|_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define ppb pop_back
#define endl '\n'
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll, string>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define vs vector<string>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
/* For Debugging */
#define DEBUG cerr << "\n>>>I'm Here<<<\n" << endl;
#define display(x) \
trav(a, x) cout << a << " "; \
cout << endl;
#define what_is(x) cerr << #x << " is " << x << endl;
std::mt19937_64
rng(std::chrono::steady_clock::now().time_since_epoch().count());
#define ordered_set \
tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define TIME \
cerr << "\nTime elapsed: " << setprecision(5) \
<< 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define DECIMAL(n) \
cout << fixed; \
cout << setprecision(n);
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace __gnu_pbds;
using namespace std;
#define PI 3.141592653589793
#define N 100005
ll fact[N];
void solve() {
ll n;
cin >> n;
string s;
cin >> s;
string tmp = "";
rep(i, 1, n) {
ll ptt = abs(s[i] - s[i - 1]);
tmp += (char)(ptt + '0');
}
s = tmp;
// cout<<s<<endl;
n--;
fact[0] = 0;
ll num;
rep(i, 1, n + 1) {
num = i;
while (num != 0) {
num /= 2;
fact[i] += num;
}
}
bool f = 0;
ll ans = 0;
rep(i, 0, n) {
if (s[i] == '1') {
num = fact[n - 1] - fact[i] - fact[n - i - 1];
// cout<<num<<" ";
if (num == 0)
ans++;
f = 1;
}
}
// cout<<endl;
if (ans & 1) {
cout << 1 << endl;
return;
}
if (f) {
cout << 0 << endl;
return;
}
ans = 0;
rep(i, 0, n) {
if (s[i] == '2') {
num = fact[n - 1] - fact[i] - fact[n - i - 1];
if (num == 0)
ans++;
f = 1;
}
}
if (ans & 1) {
cout << 2 << endl;
} else {
cout << 0 << endl;
}
return;
}
int main() {
FAST int TESTS = 1;
// cin>>TESTS;
while (TESTS--) {
solve();
}
TIME return 0;
} | /*
_____ _ _ _ _
|_ _| |__ ___ / \ _ __ ___| |__ _ _| |
| | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
| | | | | | __// ___ \| | | \__ \ | | | |_| | |
|_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define ppb pop_back
#define endl '\n'
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll, string>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define vs vector<string>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
/* For Debugging */
#define DEBUG cerr << "\n>>>I'm Here<<<\n" << endl;
#define display(x) \
trav(a, x) cout << a << " "; \
cout << endl;
#define what_is(x) cerr << #x << " is " << x << endl;
std::mt19937_64
rng(std::chrono::steady_clock::now().time_since_epoch().count());
#define ordered_set \
tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define TIME \
cerr << "\nTime elapsed: " << setprecision(5) \
<< 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define DECIMAL(n) \
cout << fixed; \
cout << setprecision(n);
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace __gnu_pbds;
using namespace std;
#define PI 3.141592653589793
#define N 1000005
ll fact[N];
void solve() {
ll n;
cin >> n;
string s;
cin >> s;
string tmp = "";
rep(i, 1, n) {
ll ptt = abs(s[i] - s[i - 1]);
tmp += (char)(ptt + '0');
}
s = tmp;
// cout<<s<<endl;
n--;
fact[0] = 0;
ll num;
rep(i, 1, n + 1) {
num = i;
while (num != 0) {
num /= 2;
fact[i] += num;
}
}
bool f = 0;
ll ans = 0;
rep(i, 0, n) {
if (s[i] == '1') {
num = fact[n - 1] - fact[i] - fact[n - i - 1];
// cout<<num<<" ";
if (num == 0)
ans++;
f = 1;
}
}
// cout<<endl;
if (ans & 1) {
cout << 1 << endl;
return;
}
if (f) {
cout << 0 << endl;
return;
}
ans = 0;
rep(i, 0, n) {
if (s[i] == '2') {
num = fact[n - 1] - fact[i] - fact[n - i - 1];
if (num == 0)
ans++;
f = 1;
}
}
if (ans & 1) {
cout << 2 << endl;
} else {
cout << 0 << endl;
}
return;
}
int main() {
FAST int TESTS = 1;
// cin>>TESTS;
while (TESTS--) {
solve();
}
TIME return 0;
} | replace | 59 | 60 | 59 | 60 | 0 |
Time elapsed: 27.613ms
|
p02736 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
ll mod = 1e9 + 7;
const ld error = 1e-9;
const ld PI = acosl(-1); // const ld PI = acosl(-1)
#define FASTIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define eq(x, y) (fabs((x) - (y)) < error)
#define bt(i) (1LL << (i))
#define debug(x) cerr << #x << " = " << (x) << "\n"
#define hoise cerr << "hoise - " << __LINE__ << "\n"
#define tham getchar()
mt19937 rng((unsigned)chrono::system_clock::now()
.time_since_epoch()
.count()); // mt199937_64 for ll
inline ll MOD(ll x, ll m = mod) {
ll y = x % m;
return (y >= 0) ? y : y + m;
}
const int inf = 1e9;
const ll infl = 1e18 + 1;
const int nmax = 1e3 + 10;
///=========================================== template
///=======================================================
int arr[nmax];
int pairity(int n, int r) {
if ((~n) & r)
return 0;
else
return 1;
}
int main() {
FASTIO;
int n;
cin >> n;
n--;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
arr[i] = abs(s[i + 1] - s[i]);
}
// for(int i = 0; i<n; i++) debug(arr[i]);
int res = 0;
for (int i = 0; i < n; i++) {
res = ((res + pairity(n - 1, i) * arr[i]) & 1);
}
if (res == 1) {
cout << 1 << endl;
return 0;
}
for (int i = 0; i < n; i++) {
if (arr[i] == 1) {
cout << 0 << endl;
return 0;
}
}
for (int i = 0; i < n; i++) {
arr[i] /= 2;
}
res = 0;
for (int i = 0; i < n; i++) {
res = ((res + pairity(n - 1, i) * arr[i]) & 1);
}
if (res == 0)
cout << 0 << endl;
else
cout << 2 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
ll mod = 1e9 + 7;
const ld error = 1e-9;
const ld PI = acosl(-1); // const ld PI = acosl(-1)
#define FASTIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define eq(x, y) (fabs((x) - (y)) < error)
#define bt(i) (1LL << (i))
#define debug(x) cerr << #x << " = " << (x) << "\n"
#define hoise cerr << "hoise - " << __LINE__ << "\n"
#define tham getchar()
mt19937 rng((unsigned)chrono::system_clock::now()
.time_since_epoch()
.count()); // mt199937_64 for ll
inline ll MOD(ll x, ll m = mod) {
ll y = x % m;
return (y >= 0) ? y : y + m;
}
const int inf = 1e9;
const ll infl = 1e18 + 1;
const int nmax = 1e6 + 10;
///=========================================== template
///=======================================================
int arr[nmax];
int pairity(int n, int r) {
if ((~n) & r)
return 0;
else
return 1;
}
int main() {
FASTIO;
int n;
cin >> n;
n--;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
arr[i] = abs(s[i + 1] - s[i]);
}
// for(int i = 0; i<n; i++) debug(arr[i]);
int res = 0;
for (int i = 0; i < n; i++) {
res = ((res + pairity(n - 1, i) * arr[i]) & 1);
}
if (res == 1) {
cout << 1 << endl;
return 0;
}
for (int i = 0; i < n; i++) {
if (arr[i] == 1) {
cout << 0 << endl;
return 0;
}
}
for (int i = 0; i < n; i++) {
arr[i] /= 2;
}
res = 0;
for (int i = 0; i < n; i++) {
res = ((res + pairity(n - 1, i) * arr[i]) & 1);
}
if (res == 0)
cout << 0 << endl;
else
cout << 2 << endl;
return 0;
}
| replace | 31 | 32 | 31 | 32 | 0 | |
p02736 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200005, p = 998244353, inf = 0x3f3f3f3f;
int read() {
int f = 1, g = 0;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-')
f = -1;
for (; isdigit(ch); ch = getchar())
g = g * 10 + ch - '0';
return f * g;
}
int n, m, fl, ans, a[N], b[N];
char ch[N];
int calc(int x) {
int t = 0;
while ((x & 1) == 0)
x >>= 1, t++;
return t;
}
int C(int n, int m) { return (b[n] - b[m] - b[n - m]) ? 0 : 1; }
int main() {
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
n = read();
scanf(" %s", ch + 1);
for (int i = 1; i <= n; i++)
a[i] = ch[i] - '1';
fl = 2;
for (int i = 1; i <= n; i++)
if (a[i] == 1)
fl = 1;
if (fl == 1)
for (int i = 1; i <= n; i++)
a[i] &= 1;
else
for (int i = 1; i <= n; i++)
a[i] /= 2;
for (int i = 1; i <= n; i++)
b[i] = b[i - 1] + calc(i);
for (int i = 1; i <= n; i++)
ans = (ans + C(n - 1, i - 1) * a[i]) & 1;
printf("%d\n", ans * fl);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000005, p = 998244353, inf = 0x3f3f3f3f;
int read() {
int f = 1, g = 0;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-')
f = -1;
for (; isdigit(ch); ch = getchar())
g = g * 10 + ch - '0';
return f * g;
}
int n, m, fl, ans, a[N], b[N];
char ch[N];
int calc(int x) {
int t = 0;
while ((x & 1) == 0)
x >>= 1, t++;
return t;
}
int C(int n, int m) { return (b[n] - b[m] - b[n - m]) ? 0 : 1; }
int main() {
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
n = read();
scanf(" %s", ch + 1);
for (int i = 1; i <= n; i++)
a[i] = ch[i] - '1';
fl = 2;
for (int i = 1; i <= n; i++)
if (a[i] == 1)
fl = 1;
if (fl == 1)
for (int i = 1; i <= n; i++)
a[i] &= 1;
else
for (int i = 1; i <= n; i++)
a[i] /= 2;
for (int i = 1; i <= n; i++)
b[i] = b[i - 1] + calc(i);
for (int i = 1; i <= n; i++)
ans = (ans + C(n - 1, i - 1) * a[i]) & 1;
printf("%d\n", ans * fl);
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02736 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
string A;
cin >> N >> A;
vector<int> aa(N);
bool exist1 = false;
for (int i = 0; i < N; ++i) {
aa[i] = A[i] - '1';
if (aa[i] == 1)
exist1 = true;
}
vector<int> a(1, 0);
for (int i = 1; i < N; ++i) {
map<int, int> b;
for (int x : a) {
b[x] ^= 1;
b[x + 1] ^= 1;
}
a.clear();
for (auto p : b)
if (p.second)
a.push_back(p.first);
}
if (exist1) {
int r = 0;
for (auto i : a) {
r ^= aa[i] & 1;
}
cout << r << endl;
} else {
int r = 0;
for (auto i : a) {
r ^= aa[i];
}
cout << r << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
string A;
cin >> N >> A;
vector<int> aa(N);
bool exist1 = false;
for (int i = 0; i < N; ++i) {
aa[i] = A[i] - '1';
if (aa[i] == 1)
exist1 = true;
}
vector<int> a;
for (int i = 0; i < N; ++i) {
if ((i | (N - 1 - i)) == N - 1)
a.push_back(i);
}
if (exist1) {
int r = 0;
for (auto i : a) {
r ^= aa[i] & 1;
}
cout << r << endl;
} else {
int r = 0;
for (auto i : a) {
r ^= aa[i];
}
cout << r << endl;
}
}
| replace | 14 | 25 | 14 | 18 | TLE | |
p02736 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 1000005;
const int INF = 1001001001;
const int MOD = 1000000007;
vector<int> table;
void init(int N) {
table.resize(N);
for (int i = 1; i < N; ++i) {
int x = i, res = 0;
while (x > 0 && x % 2 == 0) {
x /= 2;
res++;
}
table[i] = table[i - 1] + res;
}
}
int f(vector<int> &V) {
int N = V.size(), res = 0;
rep(i, N) {
if (table[N - 1] == table[i] + table[N - 1 - i]) {
res += V[i];
}
}
if (res % 2)
return 1;
if (count(V.begin(), V.end(), 1) > 0)
return 0;
for (int &i : V)
i /= 2;
return 2 * f(V);
}
int main() {
int N;
string S;
cin >> N >> S;
vector<int> A(N);
rep(i, N) A[i] = S[i] - '1';
init(N);
cout << f(A) << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 1000005;
const int INF = 1001001001;
const int MOD = 1000000007;
vector<int> table;
void init(int N) {
table.resize(N);
for (int i = 1; i < N; ++i) {
int x = i, res = 0;
while (x > 0 && x % 2 == 0) {
x /= 2;
res++;
}
table[i] = table[i - 1] + res;
}
}
int f(vector<int> &V) {
int N = V.size(), res = 0;
rep(i, N) {
if (table[N - 1] == table[i] + table[N - 1 - i]) {
res += V[i];
}
}
if (res % 2)
return 1;
if (count(V.begin(), V.end(), 1) > 0)
return 0;
for (int &i : V)
i /= 2;
if (count(V.begin(), V.end(), V[0]) == N)
return 0;
return 2 * f(V);
}
int main() {
int N;
string S;
cin >> N >> S;
vector<int> A(N);
rep(i, N) A[i] = S[i] - '1';
init(N);
cout << f(A) << endl;
} | insert | 40 | 40 | 40 | 42 | TLE | |
p02737 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<vector<int>> E[3];
for (int i = 0; i < 3; i++) {
int M;
cin >> M;
E[i].resize(N);
for (int j = 0; j < M; j++) {
int a, b;
cin >> a >> b;
E[i][a - 1].push_back(b - 1);
E[i][b - 1].push_back(a - 1);
}
}
vector<int> G[3];
set<int> S;
for (int i = 0; i < 3; i++) {
G[i] = vector<int>(N);
for (int j = N - 1; j >= 0; j--) {
S.clear();
for (int e : E[i][j])
if (e > j)
S.insert(G[i][e]);
for (int k = 0;; k++)
if (S.count(k) == 0) {
G[i][j] = k;
break;
}
}
}
long long M = 998244353LL;
vector<long long> P10(N + 1);
P10[0] = 1;
for (int i = 1; i <= N; i++)
P10[i] = P10[i - 1] * (1000000000000000000 % M) % M;
vector<long long> GS[3];
int MG[3];
for (int i = 0; i < 3; i++) {
GS[i] = vector<long long>(N);
for (int j = 0; j < N; j++) {
GS[i][G[i][j]] += P10[j + 1];
GS[i][G[i][j]] %= M;
MG[i] = max(MG[i], G[i][j]);
}
}
long long ans = 0;
for (int i = 0; i <= MG[0]; i++)
for (int j = 0; j <= MG[1]; j++) {
int k = i ^ j;
if (k <= MG[2]) {
ans += GS[0][i] * GS[1][j] % M * GS[2][k] % M;
ans %= M;
}
}
cout << ans << endl;
}
| #include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<vector<int>> E[3];
for (int i = 0; i < 3; i++) {
int M;
cin >> M;
E[i].resize(N);
for (int j = 0; j < M; j++) {
int a, b;
cin >> a >> b;
E[i][a - 1].push_back(b - 1);
E[i][b - 1].push_back(a - 1);
}
}
vector<int> G[3];
set<int> S;
for (int i = 0; i < 3; i++) {
G[i] = vector<int>(N);
for (int j = N - 1; j >= 0; j--) {
S.clear();
for (int e : E[i][j])
if (e > j)
S.insert(G[i][e]);
for (int k = 0;; k++)
if (S.count(k) == 0) {
G[i][j] = k;
break;
}
}
}
long long M = 998244353LL;
vector<long long> P10(N + 1);
P10[0] = 1;
for (int i = 1; i <= N; i++)
P10[i] = P10[i - 1] * (1000000000000000000 % M) % M;
vector<long long> GS[3];
int MG[3] = {};
for (int i = 0; i < 3; i++) {
GS[i] = vector<long long>(N);
for (int j = 0; j < N; j++) {
GS[i][G[i][j]] += P10[j + 1];
GS[i][G[i][j]] %= M;
MG[i] = max(MG[i], G[i][j]);
}
}
long long ans = 0;
for (int i = 0; i <= MG[0]; i++)
for (int j = 0; j <= MG[1]; j++) {
int k = i ^ j;
if (k <= MG[2]) {
ans += GS[0][i] * GS[1][j] % M * GS[2][k] % M;
ans %= M;
}
}
cout << ans << endl;
}
| replace | 46 | 47 | 46 | 47 | TLE | |
p02737 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define REP(i, n) rep(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rs resize
#define DBG(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
const ld PI = acos(-1.0);
template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> int sz(const T &x) {
return (int)x.size(); // copy the ampersand(&)!
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
os << "\n[";
for (T &x : v)
os << x << ',';
return os << "]\n";
}
struct pairhash {
public:
template <typename T1, typename T2>
size_t operator()(const pair<T1, T2> &p) const {
size_t lhs = hash<T1>()(p.x);
size_t rhs = hash<T2>()(p.y);
return lhs ^ (rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2));
}
};
const ll MOD = 998244353;
const ll BASE = 1000000000000000000 % MOD;
// vector<vvi> adj;
vvi nim;
void run() {
ll N;
cin >> N;
ll maxNim = 0;
for (ll i = 0; i < 3; i++) {
vvi adj(N);
nim.pb(vi(N, 0));
ll M;
cin >> M;
for (ll j = 0; j < M; j++) {
ll a, b;
cin >> a >> b;
a--;
b--;
if (a > b)
swap(a, b);
adj[a].pb(b);
}
for (ll j = N - 1; j >= 0; j--) {
vector<bool> U(adj[j].size() + 2, false);
for (ll k : adj[j]) {
U[nim[i][k]] = true;
}
for (ll k = 0; k < U.size(); k++) {
if (!U[k]) {
// cout << j << ": " << k << endl;
nim[i][j] = k;
break;
}
}
maxNim = max(maxNim, nim[i][j] + 1);
}
}
vvi nimScore(3, vi(maxNim, 0));
for (ll i = 0; i < 3; i++) {
ll p = 1;
for (ll j = 0; j < N; j++) {
p *= BASE;
p %= MOD;
nimScore[i][nim[i][j]] += p;
nimScore[i][nim[i][j]] %= MOD;
}
}
ll res = 0;
for (ll i1 = 0; i1 < maxNim; i1++) {
for (ll i2 = 0; i2 < maxNim; i2++) {
if ((i1 ^ i2) < maxNim && (i1 ^ i2) >= 0) {
res += (nimScore[0][i1] * nimScore[1][i2]) % MOD * nimScore[2][i1 ^ i2];
res %= MOD;
}
}
}
// cout << maxNim << endl;
cout << res << endl;
// cout << nimScore[0][0] << ", " << nimScore[1][0] << ", " << nimScore[2][0]
// << endl;
}
signed main() {
// DON'T MIX "scanf" and "cin"!
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(20);
run();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define REP(i, n) rep(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rs resize
#define DBG(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
const ld PI = acos(-1.0);
template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> int sz(const T &x) {
return (int)x.size(); // copy the ampersand(&)!
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
os << "\n[";
for (T &x : v)
os << x << ',';
return os << "]\n";
}
struct pairhash {
public:
template <typename T1, typename T2>
size_t operator()(const pair<T1, T2> &p) const {
size_t lhs = hash<T1>()(p.x);
size_t rhs = hash<T2>()(p.y);
return lhs ^ (rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2));
}
};
const ll MOD = 998244353;
const ll BASE = 1000000000000000000 % MOD;
// vector<vvi> adj;
vvi nim;
void run() {
ll N;
cin >> N;
ll maxNim = 0;
for (ll i = 0; i < 3; i++) {
vvi adj(N);
nim.pb(vi(N, 0));
ll M;
cin >> M;
for (ll j = 0; j < M; j++) {
ll a, b;
cin >> a >> b;
a--;
b--;
if (a > b)
swap(a, b);
adj[a].pb(b);
}
for (ll j = N - 1; j >= 0; j--) {
vector<bool> U(adj[j].size() + 2, false);
for (ll k : adj[j]) {
if (nim[i][k] < U.size())
U[nim[i][k]] = true;
}
for (ll k = 0; k < U.size(); k++) {
if (!U[k]) {
// cout << j << ": " << k << endl;
nim[i][j] = k;
break;
}
}
maxNim = max(maxNim, nim[i][j] + 1);
}
}
vvi nimScore(3, vi(maxNim, 0));
for (ll i = 0; i < 3; i++) {
ll p = 1;
for (ll j = 0; j < N; j++) {
p *= BASE;
p %= MOD;
nimScore[i][nim[i][j]] += p;
nimScore[i][nim[i][j]] %= MOD;
}
}
ll res = 0;
for (ll i1 = 0; i1 < maxNim; i1++) {
for (ll i2 = 0; i2 < maxNim; i2++) {
if ((i1 ^ i2) < maxNim && (i1 ^ i2) >= 0) {
res += (nimScore[0][i1] * nimScore[1][i2]) % MOD * nimScore[2][i1 ^ i2];
res %= MOD;
}
}
}
// cout << maxNim << endl;
cout << res << endl;
// cout << nimScore[0][0] << ", " << nimScore[1][0] << ", " << nimScore[2][0]
// << endl;
}
signed main() {
// DON'T MIX "scanf" and "cin"!
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(20);
run();
return 0;
}
| replace | 72 | 73 | 72 | 74 | 0 | |
p02737 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int64 i = 0; i < (n); i++)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second
using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;
const double eps = 1e-10;
template <typename A, typename B> inline void chmin(A &a, B b) {
if (a > b)
a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
if (a < b)
a = b;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) {
u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) {
for (auto &e : u)
fill_v<T>(e, v...);
}
template <::std::uint_fast64_t mod> class ModInt {
private:
using value_type = ::std::uint_fast64_t;
value_type n;
public:
ModInt() : n(0) {}
ModInt(value_type n_) : n(n_ % mod) {}
ModInt(const ModInt &m) : n(m.n) {}
template <typename T> explicit operator T() const {
return static_cast<T>(n);
}
value_type get() const { return n; }
friend ::std::ostream &operator<<(::std::ostream &os, const ModInt<mod> &a) {
return os << a.n;
}
friend ::std::istream &operator>>(::std::istream &is, ModInt<mod> &a) {
value_type x;
is >> x;
a = ModInt<mod>(x);
return is;
}
bool operator==(const ModInt &m) const { return n == m.n; }
bool operator!=(const ModInt &m) const { return n != m.n; }
ModInt &operator*=(const ModInt &m) {
n = n * m.n % mod;
return *this;
}
ModInt pow(value_type b) const {
ModInt ans = 1, m = ModInt(*this);
while (b) {
if (b & 1)
ans *= m;
m *= m;
b >>= 1;
}
return ans;
}
ModInt inv() const { return (*this).pow(mod - 2); }
ModInt &operator+=(const ModInt &m) {
n += m.n;
n = (n < mod ? n : n - mod);
return *this;
}
ModInt &operator-=(const ModInt &m) {
n += mod - m.n;
n = (n < mod ? n : n - mod);
return *this;
}
ModInt &operator/=(const ModInt &m) {
*this *= m.inv();
return *this;
}
ModInt operator+(const ModInt &m) const { return ModInt(*this) += m; }
ModInt operator-(const ModInt &m) const { return ModInt(*this) -= m; }
ModInt operator*(const ModInt &m) const { return ModInt(*this) *= m; }
ModInt operator/(const ModInt &m) const { return ModInt(*this) /= m; }
ModInt &operator++() {
n += 1;
return *this;
}
ModInt &operator--() {
n -= 1;
return *this;
}
ModInt operator++(int) {
ModInt old(n);
n += 1;
return old;
}
ModInt operator--(int) {
ModInt old(n);
n -= 1;
return old;
}
ModInt operator-() const { return ModInt(mod - n); }
};
constexpr int64 mod = 998244353;
using Mint = ModInt<mod>;
int64 N, M[3];
vector<vector<int64>> G[3];
vector<int64> gr[3];
void calc(const vector<vector<int64>> &G, vector<int64> &gr) {
for (int64 i = N - 1; i >= 0; i--) {
set<int64> st;
for (auto &u : G[i]) {
if (gr[u] != -1)
st.insert(gr[u]);
}
int64 x = 0;
while (st.size() && *(st.begin()) == x) {
x++;
st.erase(st.begin());
}
gr[i] = x;
}
}
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
vector<Mint> grsum[3];
REP(i, 3) {
cin >> M[i];
G[i].resize(N);
gr[i].resize(N, -1);
REP(j, M[i]) {
int64 a, b;
cin >> a >> b;
a--;
b--;
G[i][a].push_back(b);
G[i][b].push_back(a);
}
calc(G[i], gr[i]);
grsum[i].resize(sqrt(M[i]) + 1, 0);
REP(j, N) { grsum[i][gr[i][j]] += Mint(INF_LL).pow(j + 1); }
}
vector<Mint> sum(M[2] + 1, 0);
REP(i, grsum[0].size()) {
REP(j, grsum[1].size()) {
if ((i ^ j) <= M[2])
sum[i ^ j] += grsum[0][i] * grsum[1][j];
}
}
Mint res = 0;
REP(i, grsum[2].size()) { res += grsum[2][i] * sum[i]; }
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int64 i = 0; i < (n); i++)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second
using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;
const double eps = 1e-10;
template <typename A, typename B> inline void chmin(A &a, B b) {
if (a > b)
a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
if (a < b)
a = b;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) {
u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) {
for (auto &e : u)
fill_v<T>(e, v...);
}
template <::std::uint_fast64_t mod> class ModInt {
private:
using value_type = ::std::uint_fast64_t;
value_type n;
public:
ModInt() : n(0) {}
ModInt(value_type n_) : n(n_ % mod) {}
ModInt(const ModInt &m) : n(m.n) {}
template <typename T> explicit operator T() const {
return static_cast<T>(n);
}
value_type get() const { return n; }
friend ::std::ostream &operator<<(::std::ostream &os, const ModInt<mod> &a) {
return os << a.n;
}
friend ::std::istream &operator>>(::std::istream &is, ModInt<mod> &a) {
value_type x;
is >> x;
a = ModInt<mod>(x);
return is;
}
bool operator==(const ModInt &m) const { return n == m.n; }
bool operator!=(const ModInt &m) const { return n != m.n; }
ModInt &operator*=(const ModInt &m) {
n = n * m.n % mod;
return *this;
}
ModInt pow(value_type b) const {
ModInt ans = 1, m = ModInt(*this);
while (b) {
if (b & 1)
ans *= m;
m *= m;
b >>= 1;
}
return ans;
}
ModInt inv() const { return (*this).pow(mod - 2); }
ModInt &operator+=(const ModInt &m) {
n += m.n;
n = (n < mod ? n : n - mod);
return *this;
}
ModInt &operator-=(const ModInt &m) {
n += mod - m.n;
n = (n < mod ? n : n - mod);
return *this;
}
ModInt &operator/=(const ModInt &m) {
*this *= m.inv();
return *this;
}
ModInt operator+(const ModInt &m) const { return ModInt(*this) += m; }
ModInt operator-(const ModInt &m) const { return ModInt(*this) -= m; }
ModInt operator*(const ModInt &m) const { return ModInt(*this) *= m; }
ModInt operator/(const ModInt &m) const { return ModInt(*this) /= m; }
ModInt &operator++() {
n += 1;
return *this;
}
ModInt &operator--() {
n -= 1;
return *this;
}
ModInt operator++(int) {
ModInt old(n);
n += 1;
return old;
}
ModInt operator--(int) {
ModInt old(n);
n -= 1;
return old;
}
ModInt operator-() const { return ModInt(mod - n); }
};
constexpr int64 mod = 998244353;
using Mint = ModInt<mod>;
int64 N, M[3];
vector<vector<int64>> G[3];
vector<int64> gr[3];
void calc(const vector<vector<int64>> &G, vector<int64> &gr) {
for (int64 i = N - 1; i >= 0; i--) {
set<int64> st;
for (auto &u : G[i]) {
if (gr[u] != -1)
st.insert(gr[u]);
}
int64 x = 0;
while (st.size() && *(st.begin()) == x) {
x++;
st.erase(st.begin());
}
gr[i] = x;
}
}
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
vector<Mint> grsum[3];
REP(i, 3) {
cin >> M[i];
G[i].resize(N);
gr[i].resize(N, -1);
REP(j, M[i]) {
int64 a, b;
cin >> a >> b;
a--;
b--;
G[i][a].push_back(b);
G[i][b].push_back(a);
}
calc(G[i], gr[i]);
grsum[i].resize(sqrt(2 * M[i]) + 1, 0);
REP(j, N) { grsum[i][gr[i][j]] += Mint(INF_LL).pow(j + 1); }
}
vector<Mint> sum(M[2] + 1, 0);
REP(i, grsum[0].size()) {
REP(j, grsum[1].size()) {
if ((i ^ j) <= M[2])
sum[i ^ j] += grsum[0][i] * grsum[1][j];
}
}
Mint res = 0;
REP(i, grsum[2].size()) { res += grsum[2][i] * sum[i]; }
cout << res << endl;
}
| replace | 171 | 172 | 171 | 172 | 0 | |
p02737 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
const int MaxN = 100000, MaxS = 350;
const int Mod = 998244353, base = ((long long)1e18) % Mod;
int N;
int Pw[MaxN + 5];
int Sg[3][MaxN + 5];
std::vector<int> Gr[3][MaxN + 5];
int Buc[3][MaxS + 5];
inline int add(int x, int y) { return (x += y) >= Mod ? x - Mod : x; }
inline int mul(int x, int y) { return 1LL * x * y % Mod; }
inline void inc(int &x, int y = 1) { x = add(x, y); }
void init() {
scanf("%d", &N);
Pw[0] = 1;
for (int i = 1; i <= N; ++i)
Pw[i] = mul(Pw[i - 1], base);
for (int k = 0; k < 3; ++k) {
int m;
scanf("%d", &m);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
if (u > v)
std::swap(u, v);
Gr[k][u].push_back(v);
}
}
}
void getSg(int k) {
static bool buc[MaxN + 5];
memset(buc, false, sizeof buc);
int mx = 0;
for (int u = N; u >= 1; --u) {
for (int i = 0; i <= mx; ++i)
buc[i] = false;
mx = 0;
for (int v : Gr[k][u]) {
buc[Sg[k][v]] = true;
mx = std::max(mx, Sg[k][v]);
}
for (int i = 0; i <= mx + 1; ++i)
if (buc[i] == false) {
Sg[k][u] = i;
break;
}
}
}
void solve() {
for (int k = 0; k < 3; ++k)
getSg(k);
for (int k = 0; k < 3; ++k)
for (int i = 1; i <= N; ++i)
inc(Buc[k][Sg[k][i]], Pw[i]);
int ans = 0;
for (int i = 0; i <= MaxS; ++i)
for (int j = 0; j <= MaxS; ++j) {
int k = i ^ j;
inc(ans, mul(mul(Buc[0][i], Buc[1][j]), Buc[2][k]));
}
printf("%d\n", ans);
}
int main() {
init();
solve();
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
const int MaxN = 100000, MaxS = 1000;
const int Mod = 998244353, base = ((long long)1e18) % Mod;
int N;
int Pw[MaxN + 5];
int Sg[3][MaxN + 5];
std::vector<int> Gr[3][MaxN + 5];
int Buc[3][MaxS + 5];
inline int add(int x, int y) { return (x += y) >= Mod ? x - Mod : x; }
inline int mul(int x, int y) { return 1LL * x * y % Mod; }
inline void inc(int &x, int y = 1) { x = add(x, y); }
void init() {
scanf("%d", &N);
Pw[0] = 1;
for (int i = 1; i <= N; ++i)
Pw[i] = mul(Pw[i - 1], base);
for (int k = 0; k < 3; ++k) {
int m;
scanf("%d", &m);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
if (u > v)
std::swap(u, v);
Gr[k][u].push_back(v);
}
}
}
void getSg(int k) {
static bool buc[MaxN + 5];
memset(buc, false, sizeof buc);
int mx = 0;
for (int u = N; u >= 1; --u) {
for (int i = 0; i <= mx; ++i)
buc[i] = false;
mx = 0;
for (int v : Gr[k][u]) {
buc[Sg[k][v]] = true;
mx = std::max(mx, Sg[k][v]);
}
for (int i = 0; i <= mx + 1; ++i)
if (buc[i] == false) {
Sg[k][u] = i;
break;
}
}
}
void solve() {
for (int k = 0; k < 3; ++k)
getSg(k);
for (int k = 0; k < 3; ++k)
for (int i = 1; i <= N; ++i)
inc(Buc[k][Sg[k][i]], Pw[i]);
int ans = 0;
for (int i = 0; i <= MaxS; ++i)
for (int j = 0; j <= MaxS; ++j) {
int k = i ^ j;
inc(ans, mul(mul(Buc[0][i], Buc[1][j]), Buc[2][k]));
}
printf("%d\n", ans);
}
int main() {
init();
solve();
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02737 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 10, mod = 998244353, lim = 1 << 10;
int n, m;
int ans[maxN + 1], g[maxN + 1], pw[maxN + 1];
int flag[maxN + 1], t[maxN + 1], s[maxN + 1];
bool vis[maxN + 1];
vector<int> nxt[maxN + 1];
inline int ADD(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int read() {
int num = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
num = (num << 3) + (num << 1) + (ch ^ 48), ch = getchar();
return num * f;
}
inline void dfs(int u) {
vis[u] = true;
for (int i = 0; i < nxt[u].size(); i++)
dfs(nxt[u][i]);
for (int i = 0; i < nxt[u].size(); i++)
flag[g[nxt[u][i]]] = u;
g[u] = 0;
while (flag[g[u]] == u)
g[u]++;
}
inline void work() {
m = read();
for (int i = 1; i <= n; i++)
vis[i] = false, nxt[i].clear();
for (int i = 0; i < lim; i++)
flag[i] = s[i] = 0;
for (int i = 1; i <= m; i++) {
int x = read(), y = read();
if (x > y)
swap(x, y);
nxt[x].push_back(y);
}
for (int i = 1; i <= n; i++)
if (!vis[i])
dfs(i);
for (int i = 1; i <= n; i++)
s[g[i]] = ADD(s[g[i]], pw[i]);
for (int i = 0; i < lim; i++)
t[i] = ans[i], ans[i] = 0;
for (int i = 0; i < lim; i++)
for (int j = 0; j < lim; j++)
ans[i ^ j] = ADD(ans[i ^ j], 1ll * t[i] * s[j] % mod);
}
int main() {
int base = 1;
for (int i = 1; i <= 18; i++)
base = 10ll * base % mod;
n = read();
pw[0] = 1;
for (int i = 1; i <= n; i++)
pw[i] = 1ll * base * pw[i - 1] % mod;
ans[0] = 1;
for (int i = 1; i <= 3; i++)
work();
printf("%d", ans[0]);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 10, mod = 998244353, lim = 1 << 10;
int n, m;
int ans[maxN + 1], g[maxN + 1], pw[maxN + 1];
int flag[maxN + 1], t[maxN + 1], s[maxN + 1];
bool vis[maxN + 1];
vector<int> nxt[maxN + 1];
inline int ADD(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int read() {
int num = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
num = (num << 3) + (num << 1) + (ch ^ 48), ch = getchar();
return num * f;
}
inline void dfs(int u) {
vis[u] = true;
for (int i = 0; i < nxt[u].size(); i++)
if (!vis[nxt[u][i]])
dfs(nxt[u][i]);
for (int i = 0; i < nxt[u].size(); i++)
flag[g[nxt[u][i]]] = u;
g[u] = 0;
while (flag[g[u]] == u)
g[u]++;
}
inline void work() {
m = read();
for (int i = 1; i <= n; i++)
vis[i] = false, nxt[i].clear();
for (int i = 0; i < lim; i++)
flag[i] = s[i] = 0;
for (int i = 1; i <= m; i++) {
int x = read(), y = read();
if (x > y)
swap(x, y);
nxt[x].push_back(y);
}
for (int i = 1; i <= n; i++)
if (!vis[i])
dfs(i);
for (int i = 1; i <= n; i++)
s[g[i]] = ADD(s[g[i]], pw[i]);
for (int i = 0; i < lim; i++)
t[i] = ans[i], ans[i] = 0;
for (int i = 0; i < lim; i++)
for (int j = 0; j < lim; j++)
ans[i ^ j] = ADD(ans[i ^ j], 1ll * t[i] * s[j] % mod);
}
int main() {
int base = 1;
for (int i = 1; i <= 18; i++)
base = 10ll * base % mod;
n = read();
pw[0] = 1;
for (int i = 1; i <= n; i++)
pw[i] = 1ll * base * pw[i - 1] % mod;
ans[0] = 1;
for (int i = 1; i <= 3; i++)
work();
printf("%d", ans[0]);
return 0;
}
| replace | 30 | 31 | 30 | 32 | TLE | |
p02737 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for (int i = 0; i < (n); i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const int mod = 998244353;
const llint inf = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-10;
template <class T, class U> bool chmin(T &a, U b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T, class U> bool chmax(T &a, U b) {
if (a < b) {
a = b;
return true;
}
return false;
}
llint gcd(llint a, llint b) {
if (a % b == 0) {
return b;
} else
return gcd(b, a % b);
}
llint lcm(llint a, llint b) {
if (a == 0) {
return b;
}
return a / gcd(a, b) * b;
}
template <class T> void SO(T &ve) { sort(ve.begin(), ve.end()); }
template <class T> void REV(T &ve) { reverse(ve.begin(), ve.end()); }
template <class T> llint LBI(const vector<T> &ar, T in) {
return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> llint UBI(const vector<T> &ar, T in) {
return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
int main(void) {
int i, j, n;
cin >> n;
llint S = 1000000000000000000LL % mod;
vector<llint> kei(n);
kei[0] = S;
for (i = 0; i < n - 1; i++) {
kei[i + 1] = kei[i] * S % mod;
}
int Lq = 1;
while (Lq * Lq < n) {
Lq++;
}
Lq *= 2;
Lq += 10;
llint L = 1;
while (L < Lq) {
L *= 2;
}
// cerr<<"L="<<L<<endl;
vector<llint> ans(L);
ans[0] = 1;
for (int kai = 0; kai < 3; kai++) {
int m;
cin >> m;
vector<vector<int>> go(n);
while (m--) {
int a, b;
cin >> a >> b;
a--;
b--;
if (a < b) {
swap(a, b);
}
go[b].pub(a);
}
vector<int> gru(n);
vector<int> hyo(L, mod);
vector<llint> gen(L, 0);
for (i = n - 1; i >= 0; i--) {
for (auto it : go[i]) {
hyo[gru[it]] = i;
}
int g = 0;
while (hyo[g] == i) {
g++;
}
gru[i] = g;
gen[g] += kei[i];
gen[g] %= mod;
}
vector<llint> dp(L, 0);
for (i = 0; i < L; i++) {
// cerr<<gen[i]<<endl;
for (j = 0; j < L; j++) {
dp[i ^ j] += ans[i] * gen[j];
dp[i ^ j] %= mod;
}
}
ans = dp;
}
cout << ans[0] << endl;
}
| #include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for (int i = 0; i < (n); i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const int mod = 998244353;
const llint inf = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-10;
template <class T, class U> bool chmin(T &a, U b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T, class U> bool chmax(T &a, U b) {
if (a < b) {
a = b;
return true;
}
return false;
}
llint gcd(llint a, llint b) {
if (a % b == 0) {
return b;
} else
return gcd(b, a % b);
}
llint lcm(llint a, llint b) {
if (a == 0) {
return b;
}
return a / gcd(a, b) * b;
}
template <class T> void SO(T &ve) { sort(ve.begin(), ve.end()); }
template <class T> void REV(T &ve) { reverse(ve.begin(), ve.end()); }
template <class T> llint LBI(const vector<T> &ar, T in) {
return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> llint UBI(const vector<T> &ar, T in) {
return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
int main(void) {
int i, j, n;
cin >> n;
llint S = 1000000000000000000LL % mod;
vector<llint> kei(n);
kei[0] = S;
for (i = 0; i < n - 1; i++) {
kei[i + 1] = kei[i] * S % mod;
}
llint L = 512;
// cerr<<"L="<<L<<endl;
vector<llint> ans(L);
ans[0] = 1;
for (int kai = 0; kai < 3; kai++) {
int m;
cin >> m;
vector<vector<int>> go(n);
while (m--) {
int a, b;
cin >> a >> b;
a--;
b--;
if (a < b) {
swap(a, b);
}
go[b].pub(a);
}
vector<int> gru(n);
vector<int> hyo(L, mod);
vector<llint> gen(L, 0);
for (i = n - 1; i >= 0; i--) {
for (auto it : go[i]) {
hyo[gru[it]] = i;
}
int g = 0;
while (hyo[g] == i) {
g++;
}
gru[i] = g;
gen[g] += kei[i];
gen[g] %= mod;
}
vector<llint> dp(L, 0);
for (i = 0; i < L; i++) {
// cerr<<gen[i]<<endl;
for (j = 0; j < L; j++) {
dp[i ^ j] += ans[i] * gen[j];
dp[i ^ j] %= mod;
}
}
ans = dp;
}
cout << ans[0] << endl;
}
| replace | 89 | 99 | 89 | 90 | 0 | |
p02737 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v) \
cerr << #v << ":"; \
for (int i = 0; i < v.size(); i++) \
cerr << " " << v[i]; \
cerr << endl;
#define DEBUG_MAT(v) \
cerr << #v << endl; \
for (int i = 0; i < v.size(); i++) { \
for (int j = 0; j < v[i].size(); j++) { \
cerr << v[i][j] << " "; \
} \
cerr << endl; \
}
typedef long long ll;
#define int ll
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
#define X first
#define Y second
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (n); i > 0; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(), c.end()
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
#define Sp(p) cout << setprecision(25) << fixed << p << endl;
// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
vi dx2 = {1, 1, 0, -1, -1, -1, 0, 1}, dy2 = {0, 1, 1, 1, 0, -1, -1, -1};
#define fio() \
cin.tie(0); \
ios::sync_with_stdio(false);
// const ll MOD = 1000000007;
const ll MOD = 998244353;
// #define mp make_pair
// #define endl '\n'
const int MAXN = 555555;
vl fact(MAXN);
vl rfact(MAXN);
ll mod_pow(ll x, ll p, ll M = MOD) {
if (p < 0) {
x = mod_pow(x, M - 2, M);
p = -p;
}
ll a = 1;
while (p) {
if (p % 2)
a = a * x % M;
x = x * x % M;
p /= 2;
}
return a;
}
ll mod_inverse(ll a, ll M = MOD) { return mod_pow(a, M - 2, M); }
void set_fact(ll n, ll M = MOD) {
fact[0] = fact[1] = rfact[0] = rfact[1] = 1;
for (ll i = 2; i <= n; i++) {
fact[i] = i * fact[i - 1] % M;
// rfact[i] = mod_inverse(fact[i], M);
}
}
// http://drken1215.hatenablog.com/entry/2018/06/08/210000
// n���傫��fact���v�Z�ł��Ȃ��Ƃ��̂ق��̌v�Z���@�ɂ��ď����Ă���
ll nCr(ll n, ll r, ll M = MOD) {
if (r > n)
return 0;
assert(fact[2] == 2);
ll ret = fact[n];
if (rfact[r] == 0) {
rfact[r] = mod_inverse(fact[r], M);
}
ret = (ret * rfact[r]) % M;
if (rfact[n - r] == 0) {
rfact[n - r] = mod_inverse(fact[n - r], M);
}
ret = (ret * rfact[n - r]) % M;
return ret;
}
ll nHr(ll n, ll r) { return nCr(n + r - 1, r); }
int n;
vector<vii> G(3);
vector<vii> num(3, vii(500));
void solve(int idx, vii G) {
vi g(n);
rrep(u, n) {
set<int> st;
for (int v : G[u]) {
if (v > u)
st.insert(g[v]);
}
rep(gl, 50000) {
if (not st.count(gl)) {
g[u] = gl;
num[idx][gl].push_back(u);
break;
}
}
}
}
signed main() {
cin >> n;
rep(i, 3) {
G[i].resize(n);
int m;
cin >> m;
rep(j, m) {
int u, v;
cin >> u >> v;
u--;
v--;
G[i][u].push_back(v);
G[i][v].push_back(u);
}
}
rep(i, 3) {
solve(i, G[i]);
// DEBUG(i);
// rep (j, 3) {
// rep (k, num[i][j].size()) {
// cout << num[i][j][k] << " ";
// }
// cout << endl;
// }
// cout << endl;
}
ll base = mod_pow(10, 18);
vll sum(3, vl(1000));
rep(i, 3) {
rep(j, 1000) {
for (int u : num[i][j]) {
(sum[i][j] += mod_pow(base, u + 1)) %= MOD;
}
}
}
ll ans = 0;
rep(x, 1000) {
rep(y, 1000) {
int z = x ^ y;
if (z >= 1000)
continue;
ll mul2 = sum[2][z];
ll mul1 = sum[1][y];
ll mul0 = sum[0][x];
(ans += mul0 * mul1 % MOD * mul2 % MOD) %= MOD;
// if (mul0 * mul1 % MOD * mul2 % MOD != 0) {
// DEBUG_VEC(vi({z, y, x}));
// DEBUG_VEC(vl({mul2, mul1, mul0}));
// }
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v) \
cerr << #v << ":"; \
for (int i = 0; i < v.size(); i++) \
cerr << " " << v[i]; \
cerr << endl;
#define DEBUG_MAT(v) \
cerr << #v << endl; \
for (int i = 0; i < v.size(); i++) { \
for (int j = 0; j < v[i].size(); j++) { \
cerr << v[i][j] << " "; \
} \
cerr << endl; \
}
typedef long long ll;
#define int ll
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
#define X first
#define Y second
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (n); i > 0; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(), c.end()
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
#define Sp(p) cout << setprecision(25) << fixed << p << endl;
// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
vi dx2 = {1, 1, 0, -1, -1, -1, 0, 1}, dy2 = {0, 1, 1, 1, 0, -1, -1, -1};
#define fio() \
cin.tie(0); \
ios::sync_with_stdio(false);
// const ll MOD = 1000000007;
const ll MOD = 998244353;
// #define mp make_pair
// #define endl '\n'
const int MAXN = 555555;
vl fact(MAXN);
vl rfact(MAXN);
ll mod_pow(ll x, ll p, ll M = MOD) {
if (p < 0) {
x = mod_pow(x, M - 2, M);
p = -p;
}
ll a = 1;
while (p) {
if (p % 2)
a = a * x % M;
x = x * x % M;
p /= 2;
}
return a;
}
ll mod_inverse(ll a, ll M = MOD) { return mod_pow(a, M - 2, M); }
void set_fact(ll n, ll M = MOD) {
fact[0] = fact[1] = rfact[0] = rfact[1] = 1;
for (ll i = 2; i <= n; i++) {
fact[i] = i * fact[i - 1] % M;
// rfact[i] = mod_inverse(fact[i], M);
}
}
// http://drken1215.hatenablog.com/entry/2018/06/08/210000
// n���傫��fact���v�Z�ł��Ȃ��Ƃ��̂ق��̌v�Z���@�ɂ��ď����Ă���
ll nCr(ll n, ll r, ll M = MOD) {
if (r > n)
return 0;
assert(fact[2] == 2);
ll ret = fact[n];
if (rfact[r] == 0) {
rfact[r] = mod_inverse(fact[r], M);
}
ret = (ret * rfact[r]) % M;
if (rfact[n - r] == 0) {
rfact[n - r] = mod_inverse(fact[n - r], M);
}
ret = (ret * rfact[n - r]) % M;
return ret;
}
ll nHr(ll n, ll r) { return nCr(n + r - 1, r); }
int n;
vector<vii> G(3);
vector<vii> num(3, vii(1000));
void solve(int idx, vii G) {
vi g(n);
rrep(u, n) {
set<int> st;
for (int v : G[u]) {
if (v > u)
st.insert(g[v]);
}
rep(gl, 50000) {
if (not st.count(gl)) {
g[u] = gl;
num[idx][gl].push_back(u);
break;
}
}
}
}
signed main() {
cin >> n;
rep(i, 3) {
G[i].resize(n);
int m;
cin >> m;
rep(j, m) {
int u, v;
cin >> u >> v;
u--;
v--;
G[i][u].push_back(v);
G[i][v].push_back(u);
}
}
rep(i, 3) {
solve(i, G[i]);
// DEBUG(i);
// rep (j, 3) {
// rep (k, num[i][j].size()) {
// cout << num[i][j][k] << " ";
// }
// cout << endl;
// }
// cout << endl;
}
ll base = mod_pow(10, 18);
vll sum(3, vl(1000));
rep(i, 3) {
rep(j, 1000) {
for (int u : num[i][j]) {
(sum[i][j] += mod_pow(base, u + 1)) %= MOD;
}
}
}
ll ans = 0;
rep(x, 1000) {
rep(y, 1000) {
int z = x ^ y;
if (z >= 1000)
continue;
ll mul2 = sum[2][z];
ll mul1 = sum[1][y];
ll mul0 = sum[0][x];
(ans += mul0 * mul1 % MOD * mul2 % MOD) %= MOD;
// if (mul0 * mul1 % MOD * mul2 % MOD != 0) {
// DEBUG_VEC(vi({z, y, x}));
// DEBUG_VEC(vl({mul2, mul1, mul0}));
// }
}
}
cout << ans << endl;
} | replace | 134 | 135 | 134 | 135 | -11 | |
p02737 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::bitset;
using std::deque;
using std::iterator;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;
using std::fill;
using std::ios_base;
using std::max_element;
using std::min_element;
using std::reverse;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;
using std::fixed;
using std::setprecision;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;
ll min(ll a, ll b) { return a < b ? a : b; }
ll min(int a, ll b) { return a < b ? a : b; }
ll min(ll a, int b) { return a < b ? a : b; }
ll min(int a, int b) { return a < b ? a : b; }
ll max(ll a, ll b) { return a > b ? a : b; }
ll max(int a, ll b) { return a > b ? a : b; }
ll max(ll a, int b) { return a > b ? a : b; }
ll max(int a, int b) { return a > b ? a : b; }
namespace MySpace {};
#define F(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long
const int N = 510200;
const long long MOD = 998244353;
const long long INF = 1e18;
int n, m;
int val[3][N];
vector<int> g[N];
long long inq(long long x, long long y) {
if (y == 0)
return 1;
long long l = inq(x, y / 2);
if (y % 2)
return l * l % MOD * x % MOD;
return l * l % MOD;
}
void run1(int id) {
for (int i = 0; i <= n; i++)
g[i].clear();
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
vector<int> gr(n + 1);
for (int i = n; i >= 1; i--) {
vector<int> cnt(g[i].size() + 2);
for (auto u : g[i]) {
if (i < u)
cnt[gr[u]]++;
}
for (int j = 0; j < g[i].size() + 2; j++) {
if (cnt[j] == 0) {
gr[i] = j;
val[id][j] = (val[id][j] + inq(INF % MOD, i)) % MOD;
break;
}
}
}
}
signed main() {
srand(time(NULL));
// ios_base::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
cin >> n;
run1(0);
run1(1);
run1(2);
long long ans = 0;
int M = 1000;
for (int x = 0; x < M; x++) {
for (int y = 0; y < M; y++) {
int z = x ^ y;
ans = (ans + val[0][x] * val[1][y] % MOD * val[2][z]) % MOD;
}
}
cout << ans;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::bitset;
using std::deque;
using std::iterator;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;
using std::fill;
using std::ios_base;
using std::max_element;
using std::min_element;
using std::reverse;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;
using std::fixed;
using std::setprecision;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;
ll min(ll a, ll b) { return a < b ? a : b; }
ll min(int a, ll b) { return a < b ? a : b; }
ll min(ll a, int b) { return a < b ? a : b; }
ll min(int a, int b) { return a < b ? a : b; }
ll max(ll a, ll b) { return a > b ? a : b; }
ll max(int a, ll b) { return a > b ? a : b; }
ll max(ll a, int b) { return a > b ? a : b; }
ll max(int a, int b) { return a > b ? a : b; }
namespace MySpace {};
#define F(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long
const int N = 510200;
const long long MOD = 998244353;
const long long INF = 1e18;
int n, m;
int val[3][N];
vector<int> g[N];
long long inq(long long x, long long y) {
if (y == 0)
return 1;
long long l = inq(x, y / 2);
if (y % 2)
return l * l % MOD * x % MOD;
return l * l % MOD;
}
void run1(int id) {
for (int i = 0; i <= n; i++)
g[i].clear();
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
vector<int> gr(n + 1);
for (int i = n; i >= 1; i--) {
vector<int> cnt(g[i].size() + 2);
for (auto u : g[i]) {
if (i < u && gr[u] < g[i].size() + 2)
cnt[gr[u]]++;
}
for (int j = 0; j < g[i].size() + 2; j++) {
if (cnt[j] == 0) {
gr[i] = j;
val[id][j] = (val[id][j] + inq(INF % MOD, i)) % MOD;
break;
}
}
}
}
signed main() {
srand(time(NULL));
// ios_base::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
cin >> n;
run1(0);
run1(1);
run1(2);
long long ans = 0;
int M = 1000;
for (int x = 0; x < M; x++) {
for (int y = 0; y < M; y++) {
int z = x ^ y;
ans = (ans + val[0][x] * val[1][y] % MOD * val[2][z]) % MOD;
}
}
cout << ans;
}
| replace | 108 | 109 | 108 | 109 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.