tgbaodeeptry's blog

By tgbaodeeptry, history, 3 years ago, In English

Hello guys,

Today, I am facing a serious problem, I get idea, but I don't is it possible to do it.

Assume that, I have a number n and I want to calculate count numbers that is co-prime to n and it is also in range [L, R]

I read totient Euler but I think it won't help me ...

Thanks so much for trying to understand my poor English. :(

Full text and comments »

  • Vote: I like it
  • +6
  • Vote: I do not like it

By tgbaodeeptry, history, 3 years ago, In English

Hello guys,

I'm trying to solve this task 1336A - Linova and Kingdom. I read its editorial and tried to understand it. But I still not understand idea behind it (why if we choose u to develop tourism, compared to choose it to develop industry, etc... )

This is editorial's link: https://codeforces.com/blog/entry/76099

And author's editorial about this task:

Spoiler

Thanks so much :D

Full text and comments »

  • Vote: I like it
  • -5
  • Vote: I do not like it

By tgbaodeeptry, history, 3 years ago, In English

Hello guys, after learning Segment Tree, I approach to learn segment tree with LAZY PROPAGATION I tried to implemented it by do this task increase V to all elements in range a to b and get sum of elements in this range. This is my code:

#include <bits/stdc++.h>
using namespace std;

struct Node {
    int val;
    int lazy;
};

struct Segment {
    int n;
    Node nodes[400001];

    Segment (int n) {
        this->n = n;

        for (int i = 0; i <= 4*n; i++) 
            nodes[i] = {0, 0};
    }

    void down(int id) {
        int t = nodes[id].lazy;

        nodes[id << 1].val += t;
        nodes[id << 1].lazy += t;
        nodes[id << 1 | 1].val += t;
        nodes[id << 1 | 1].lazy += t;

        nodes[id].lazy = 0;
    }

    void update(int id, int l, int r, int u, int v, int val) {
        if (l > v || r < u) 
            return;

        if (u <= l && r <= v) {
            nodes[id].val += val;
            nodes[id].lazy += val;
            return;
        }

        down(id);
        
        int m = (l + r) >> 1;
        update(id << 1, l, m, u, v, val);
        update(id << 1 | 1, m + 1, r, u, v, val);

        nodes[id].val = nodes[id << 1].val + nodes[id << 1 | 1].val;
    }

    int get(int id, int l, int r, int u, int v) {
        if (l > v || r < u) {
            return 0;
        }

        if (u <= l && r <= v) 
            return nodes[id].val;

        down(id);

        int m = (l + r) >> 1;
        return get(id << 1, l, m, u, v) + get(id << 1 | 1, m + 1, r, u, v);
    }
};

int main() {
    Segment seg(10);
    seg.update(1, 1, 10, 1, 5, 10);

    cout << seg.get(1, 1, 10, 1, 5) << endl;
}

And the answer after running is 10 (but correct answer is 50). Did I implement wrongly?

Thanks so much, guys :D

Full text and comments »

  • Vote: I like it
  • -18
  • Vote: I do not like it

By tgbaodeeptry, history, 3 years ago, In English

Hello guys, Today, I'm trying to code this problem: 545C - Дровосеки. I used DP to solve it and I have read problem's Editorial (https://codeforces.com/blog/entry/17982). And I knew that there still have another approach to solve it: Greedy.

Also this problem can be solved by the next greedy algoritm. Let's fell leftmost tree to the left (it always doesn't make an answer worse). After that, try to fell the next tree. If we can fell it to the left, let's do it (because it also always doesn't make an answer worse). If we can't, then try to fell it to the right. If it is possible, let's do it. Last step is correct because felling some tree to the right may only prevent the next tree's fallen. So we may "exchange" one tree to another without worsing an answer.

But I don't know, why if a tree can fall right we should do it? (I think there are some cases that it will make answer worse ( may be :( )

Thanks very much, guys

Full text and comments »

  • Vote: I like it
  • +8
  • Vote: I do not like it

By tgbaodeeptry, history, 3 years ago, In English

Assume I have 3 long long varables a, b and n. And them are approximately (max long long)( 2.10^18 ).

So, are there any ways that I can check if a + b is larger than n?

Thanks so much guys

Full text and comments »

  • Vote: I like it
  • +22
  • Vote: I do not like it

By tgbaodeeptry, history, 3 years ago, In English

Recently, I have been learning Dynamic Programming and I implemeted it:

int n, m, k;

bool can(int x, int y, int rem) {
    if (x > n || y > m || rem < 0)
        return false;

    if (x == n && y == m && rem == 0)
        return true;

    return can(x + 1, y, rem - y) || can(x, y + 1, rem - x); 
}

I want to save a 'state' contains: (x, y, rem) I tried to use map with tuple like: map<tuple<int, int, int>, bool> but syntax is not correct.

Thanks guy

Full text and comments »

  • Vote: I like it
  • +7
  • Vote: I do not like it