Hello codeforces! Can someone give me the solution for this problem please.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Hello codeforces! Can someone give me the solution for this problem please.
Hi. I've been solving this problem in USACO named snakes. when I want to submit this is what I write for input output stuff:
freopen("snakes.in", "r", stdin); freopen("snakes.out", "w", stdout);
ans this model has always worked and I don't know whats wrong with this problem. and this is what usaco gives me when submitted:
` Runtime error or memory limit exceeded on sample input case -- details below Your output file snakes.out: [File missing!]
The correct answer: 3`
and this is exact code for the problem(if you want to check):
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
using namespace std;
typedef long long ll;
const int N = 410, inf = (int) 2e9;
int dp[N][N][N];
int dp_min[N][N];
int a[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
freopen("snakes.in", "r", stdin);
freopen("snakes.out", "w", stdout);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k <= m; k++) {
dp[i][j][k] = -1;
dp_min[i][k] = inf;
}
}
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= m; j++) {
if (a[i] < a[0]) {
continue;
}
dp[0][i][j] = a[i] - a[0];
dp_min[0][j] = min(dp_min[0][j], dp[0][i][j]);
}
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k <= m; k++) {
if (a[j] < a[i]) {
continue;
}
if (k == 0) {
if (dp[i - 1][j][k] == -1) {
dp[i][j][k] = -1;
} else {
dp[i][j][k] = dp[i - 1][j][k] + a[j] - a[i];
dp_min[i][k] = min(dp_min[i][k], dp[i][j][k]);
}
continue;
}
if (dp[i - 1][j][k] != -1) {
dp[i][j][k] = min(dp[i - 1][j][k] + a[j] - a[i], dp_min[i - 1][k - 1] + a[j] - a[i]);
} else {
dp[i][j][k] = dp_min[i - 1][k - 1] + a[j] - a[i];
}
dp_min[i][k] = min(dp_min[i][k], dp[i][j][k]);
}
}
}
/*
debug("dp");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k <= m; k++) {
debug(i);
debug(j);
debug(k);
debug(dp[i][j][k]);
}
}
}
debug("dp_min");
for (int i = 0; i < n; i++) {
for (int k = 0; k <= m; k++) {
debug(i);
debug(k);
debug(dp_min[i][k]);
}
}
*/
cout << dp_min[n - 1][m] << '\n';
return 0;
}
PLEASE HELP!
This code gives me compilation error:
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << 2 ^ 3;
return 0;
}
But when instead of 2 ^ 3 I write (2 ^ 3) it becomes OK. Does anybody know why?
Hi everybody and Hope you are doing well. I am thinking in these quarantine days it’s maybe not easy for programmers to code all day. For sure they take a break and do something else. I wanted to know what do you do when you are not programming and what are your hobbies. For example mine are playing video games, playing chess, listening to music, watching youtube and of course I love playing ping pong but it’s kinda impossible these days:( . Tell me what do you do in the comments!
Hi. I wanted know is there a valid mapping between codejam problems difficulties and codeforces problems difficulties. I mean for example can we say that the final round of code jam problems are in the range of [3000, 3500] or the qualification round is about 1800 or something like that?
few days ago I wrote a blog asking about if vectors can cause you TLE. and now I have faced such problem in solving this problem. and now I show you the codes that their solution is totally the same but the first one gets AC and the other one gets TLE. I only changed the vectors of the second one to arrays and it passed. please tell me if I'm wrong and TLE was for smth else.
AC solution:
//\\//\\ * * * //\\// ||
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
using namespace std;
typedef long long ll;
int md;
inline void add(int& a, int b) {
a += b;
if (a >= md) {
a -= md;
}
}
inline int mul(int a, int b) {
return (int) ((ll) a * b % md);
}
const int N = (int) 1e4 + 10;
const int M = 2000;
bool is_prime[N];
int dp[N][M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
freopen("exercise.in", "r", stdin);
freopen("exercise.out", "w", stdout);
int n;
cin >> n >> md;
for (int i = 0; i <= n; i++) {
is_prime[i] = true;
}
is_prime[1] = is_prime[0] = false;
for (int i = 2; i <= n; i++) {
if (!is_prime[i]) {
continue;
}
for (int j = i + i; j <= n; j += i) {
is_prime[j] = false;
}
}
vector<int> p;
for (int i = 0; i <= n; i++) {
if (is_prime[i]) {
p.push_back(i);
}
}
int m = (int) p.size();
for (int i = 0; i <= m - 1; i++) {
dp[0][i] = 0;
dp[1][i] = 0;
}
for (int i = 2; i <= n; i++) {
int x = 2;
while (x <= i) {
x *= 2;
}
x /= 2;
if (x == i) {
add(dp[i][0], x);
}
for (int j = 1; j < m; j++) {
add(dp[i][j], dp[i][j - 1]);
int y = p[j];
while (y <= i) {
if (y == i) {
add(dp[i][j], y);
break;
}
add(dp[i][j], mul(dp[i - y][j - 1], y));
y *= p[j];
}
}
}
int ans = 0;
for (int i = 0; i <= n; i++) {
add(ans, dp[i][m - 1]);
}
add(ans, 1);
cout << ans << '\n';
return 0;
}
TLE solution:
/**
@@@@@@@ @@@@ @@@@ @@@@ @@@@ @
@ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @@@@ @@@@ @@@@ @ @ @
**/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define ld long double
#define str string
#define pi pair
#define tup tuple
#define vec vector
#define temp template
#define tn typename
#define ff first
#define ss second
#define SZ(x) (ll) x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rsz resize
#define ins insert
#define fr front
#define bc back
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define mk make_pair
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define ROF(i, a, b) for (ll i = (a); i >= (b); i--)
#define trav(a, x) for (auto &a : x)
#define popcount __builtin_popcountll
#define ctz __builtin_ctzll
#define clz __builtin_clzll
#define gcd __gcd
#define max(a, b) max((ll) a, (ll) b)
#define min(a, b) min((ll) a, (ll) b)
// DEBUG
#define ts to_string
temp<tn A, tn B> str ts(pi<A, B> p);
temp<tn A, tn B, tn C> str ts(tup<A, B, C> p);
str ts(const str& s) { return '"' + s + '"'; }
str ts(const char* s) { return ts((str) s); }
str ts(bool b) { return (b ? "true" : "false"); }
str ts(vec<bool> v) { bool f = true; str res = "{";
FOR(i, 0, static_cast<ll>(v.size()) - 1)
{ if (!f) res += ", "; res += ts(v[i]);
f = false; } res += "}"; return res; }
temp<size_t N> str ts(bitset<N> v) { str res = "";
trav(a, v) { res += (char) ('0' + a); } return res; }
temp<tn A> str ts(A v) { bool f = true; str res = "{";
trav(x, v) { if (!f) res += ", "; res += ts(x);
f = false; } res += "}"; return res; }
temp<tn A, tn B> str ts(pi<A, B> p) {
return "(" + ts(p.ff) + ", " + ts(p.ss) + ")"; }
temp<tn A, tn B, tn C> str ts(tup<A, B, C> t) {
return "(" + ts(get<0>(t)) + ", " + ts(get<1>(t)) + ", " + ts(get<2>(t)) + ")"; }
void DBG() { cerr << endl; }
temp<tn H, tn... T> void DBG(H h, T... t) { cerr << " " << ts(h); DBG(t...); }
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", DBG(__VA_ARGS__);
#else
#define dbg(...) 42
#endif
// INPUT AND OUTPUT
temp<tn T> void re(T& x) { cin >> x; };
temp<tn H, tn... T> void re(H& h, T&... t) { re(h); re(t...); }
temp<tn T> void pr(T x) { cout << x; };
temp<tn H, tn... T> void pr(H h, T... t) { pr(h); pr(t...); }
temp<tn T> void pri(T x) { cout << x; cout << endl; }; // used for interactives
temp<tn H, tn... T> void pri(H h, T... t) { pri(h); pri(t...); }
/**
* Name: Modular operations
* Description:
* used for doing modular operations
**/
ll md;
ll nrm(ll x) { while (x < (ll) 0) x += md; x %= md; return x; }
ll sum() { return (ll) 0; }
temp <tn H, tn... T> ll sum(H h, T... t) { return nrm(nrm(h) + sum(t...)); }
ll mul() { return (ll) 1; }
temp <tn H, tn... T> ll mul(H h, T... t) { return nrm(nrm(h) * mul(t...)); }
ll power(ll a, ll b) {
ll res = 1;
while (b) { if (b & 1) res = mul(res, a); a = mul(a, a); b >>= 1; }
return res;
}
ll inv(ll x) { return power(x, md - 2); }
// pay attention to md's value first!
// use normalized form of modular integers for output!
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
freopen("exercise.in", "r", stdin);
freopen("exercise.out", "w", stdout);
ll n;
re(n, md);
vec<ll> is_prime(n + 1, true);
is_prime[1] = is_prime[0] = false;
FOR(i, 2, n) {
if (!is_prime[i]) {
continue;
}
for (ll j = i + i; j <= n; j += i) {
is_prime[j] = false;
}
}
vec<ll> p;
FOR(i, 0, n) {
if (is_prime[i]) {
p.pb(i);
}
}
// dbg(p);
ll m = SZ(p);
vec<vec<ll>> dp(n + 1, vec<ll>(m));
FOR(i, 0, m - 1) {
dp[0][i] = 0;
dp[1][i] = 0;
}
FOR(i, 2, n) {
ll x = 2;
while (x <= i) {
x *= 2;
}
x /= 2;
if (x == i) {
dp[i][0] = sum(dp[i][0], x);
}
FOR(j, 1, m - 1) {
dp[i][j] = sum(dp[i][j], dp[i][j - 1]);
ll y = p[j];
while (y <= i) {
if (y == i) {
dp[i][j] = sum(dp[i][j], y);
break;
}
dp[i][j] = sum(dp[i][j], mul(dp[i - y][j - 1], y));
y *= p[j];
}
}
}
// dbg(dp);
ll ans = 0;
FOR(i, 0, n) {
ans = sum(ans, dp[i][m - 1]);
}
ans = sum(ans, 1);
pr(ans, '\n');
return 0;
}
hi. I wanted to know some basic information about vectors. I really like vectors more than arrays as they are easier to use. But the only problem is about the time. It rarely happens that vectors cause you TLE and when you change them to array it gets accepted. So I wanted to know what those cases exactly are and in witch problems should I start coding using arrays. Will be happy to hear your opinion if you have faced my problems and know how to fix them. Thanks!
Hi. Ive been thinking that all the tasks we are killing our selves to solve to get rating are authored by someone. And I got interested to know if there are any special rules or bounds for making a new task. Or maybe somebody can just start from any structure (for example a directed graph or two prime numbers) and play with it and randomly build a task from it? Or do we have a set of classic problems that all new problems are based on them? Will be happy to know your opinion!
https://www.spoj.com/problems/DQUERY/
can anybody give me a fenwick tree approach for this problem?
Thanks.
It happens a lot to me and for sure to others to write 200 lines of code and then receive a WA. then I say ah **** who is going to debug it... and it takes an hour for me to debug the code. what to you do in these moments? which part of code is more likely to have typo and which parts should I check in order to find the bug quicker? thanks for your helps.
Ive seen a lot of people use this structure in their code:
(Part of code that works locally)
(The previous part doesn't work in online mode)
instead of LOCAL some other strings can be written too. What I want to know is how to make this structure work. how to make ifdef LOCAL work?
btw Im using devc++.
thanks in advance for helping.
hi. I have two problems in devc++:
I have set the tabsize 2. but when submitting on codeforces the visible code is a disaster. tabsize 2 has changed and the code is ruined. How can I fix it?
I have built a library for famous algorithms/data structures but don't know how to insert the prewritten code fast. Does anybody know how to do that?
Thanks in advance for helping.
Name |
---|