Изменения рейтингов за последние раунды временно удалены. Скоро они будут возвращены. ×

Блог пользователя Madiyar

Автор Madiyar, история, 9 лет назад, По-английски

Small brainteaser. Do you know that function below doesn't work in some cases.
Try to attack it.

#include <iostream>

using namespace std;

void swap(int &a, int &b){ 
	a = a + b; 
	b = a - b; 
	a = a - b;
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • +55
  • Проголосовать: не нравится

Автор Madiyar, 9 лет назад, По-английски

Hope this will helpful for someone.

Never repeat my mistake. Never start name of global variable with an underscore.

For example Accepted code in Codechef.

The same code gives Runtime error with addition:#define where _end, i.e by renaming where variable to _end.

Again, the same code gives Wrong Answer with addition: #define size _end, i.e by renaming another variable "size" to _end.

In my point of view, variables beginning with an underscore are reserved in the global namespaces.

Can somebody better explain what is happening here? I tried in some problems in Codeforces, but couldn't trigger it.

Полный текст и комментарии »

Теги c++
  • Проголосовать: нравится
  • +43
  • Проголосовать: не нравится

Автор Madiyar, 9 лет назад, По-английски

My another blog hopefully will help to prevent from bugs.

Try to guess the output without using compiler or IDE. Please hide your answers to not spoil.

1.

#include <iostream>
using namespace std;

int main() {
    int a = 020;
    short b = 2;
    cout << a - b << endl;
    return 0;
}

2.

#include <iostream>
using namespace std;

int main() {
    bool ok = true;
    // Try to guess with if condition  \
    if (!ok && true) 
        cout << "I am ok" << endl;
    return 0;
}

3.

#include <iostream>
using namespace std;

int main() {
    unsigned a = 0;
    int b = 2;
    if (a + b >= -2) 
        cout << a + b << ">=" << -2 << endl;
    else
        cout << a + b << "<" << -2 << endl;
    return 0;
}

4.

#include <iostream>
using namespace std;

int main() {
    int  a = 0, b = 1;
    if (a&b==0) {
        cout << (a&b) << "=" << 0 << endl;
    } else {
        cout << (a&b) << "!=" << 0 << endl;
    }
    return 0;
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • +96
  • Проголосовать: не нравится

Автор Madiyar, 9 лет назад, По-английски

I use python a lot. So, by accidentally I can type this code and compiler won't give me a error:

#include <iostream>
using namespace std;

int main() {
	cout << (1 <= 2 <= 1);
}

In python, and some other languages, the expression a <= b <= c is equivalent to a <= b and b <= c. But in c++, this code produces 1 (true).

Why it compiles successfully? And why it prints 1?

Hope this blog will prevent someone from possible future mistakes.

Полный текст и комментарии »

Теги c++
  • Проголосовать: нравится
  • +73
  • Проголосовать: не нравится

Автор Madiyar, 10 лет назад, По-английски

Single Round Match 630 will be held at 05:00 Fri Moscow time.

Let's discuss here problems after the contest.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +79
  • Проголосовать: не нравится

Автор Madiyar, 10 лет назад, перевод, По-русски

I hope this post will be helpful for someone :).

I use a lot standard c++ gcd function (__gcd(x, y)).
But today, I learnt that on some compiler __gcd(0, 0) gives exception. (Maybe because 0 is divisible by any number?! )

Note for myself and everybody: While using __gcd we must carefully handle (0, 0) case or write own gcd.

upd: riadwaw noted below that we must be careful also with case __gcd(x, 0).

Полный текст и комментарии »

  • Проголосовать: нравится
  • +79
  • Проголосовать: не нравится

Автор Madiyar, 10 лет назад, перевод, По-русски

Hello

I am trying to solve this problem from hackerrank.

In short, there is a tournament graph, which is directed complete graph, i.e for every (u, v), where u < v there is an edge either from u to v, or from v to u.
We know scores of each player (i.e out degrees of each vertex), but some scores might be erased. Instead of erased scores we can put any scores, so that for these scores should exist a tournament graph. How many possible variants exists?

My thoughts (which can help to solve this problem):

To check (s[1], s[2], .., s[n]) sequence is out degrees of tournament graph, must hold:

if we sort sequence, so that s[1] <= s[2] <= .. <= s[n]

1.

2., for every k = 1 .. n-1

I though to solve this problem using dynamic problem and these properties, but it was wrong.

So How to solve this problem?

Полный текст и комментарии »

  • Проголосовать: нравится
  • +14
  • Проголосовать: не нравится

Автор Madiyar, 10 лет назад, По-английски

While solving Andrew Stankevich Contest 32, Problem K, I noticed that __builtin_popcount for long long doesn't work properly, I spent a lot of time to find my mistake, but when I wrote __builtin_popcount by myself it accepted.

__builtin_popcount defined only for int?

Полный текст и комментарии »

  • Проголосовать: нравится
  • +31
  • Проголосовать: не нравится

Автор Madiyar, 10 лет назад, По-русски

In my Facebook page I noticed e-maxx.ru in advertisement section,

e-maxx

Who else have e-maxx in advertisement section?

Полный текст и комментарии »

  • Проголосовать: нравится
  • +63
  • Проголосовать: не нравится

Автор Madiyar, 11 лет назад, По-русски

Hello

I solved this task, but I have a question.

First of all I submitted this code

TL : 0.04 Memory : 1672

Then I resubmitted this code but without #include <iostream>

TL: 0.04 but Memory : 1088

Question: For what spends memory in iostream?

Полный текст и комментарии »

  • Проголосовать: нравится
  • +11
  • Проголосовать: не нравится

Автор Madiyar, 11 лет назад, По-русски

Как решить такую задачу на матожидание lightoj 1284

Тупое решение такое : матожидание линейна, поэтому для каждой клетки можно посчитать отдельно. Используя формулу Бернули , матожидание для одной клетки найдем. А ответ для задачи это сумма всех ячеек.код

UPD: ACCEPTED. Решил за O(X * Y * Z * log(k) * T) . Мне кажеться не должно было проходит из за double. Для одной клетки считаем матожидание с помощью возведение матрицу в степень.code

Задача : ~~~~~ You are given a 3D grid, which has dimensions X, Y and Z. Each of the X x Y x Z cells contains a light. Initially all lights are off. You will have K turns. In each of the K turns,

  1. You select a cell A randomly from the grid,
  2. You select a cell B randomly from the grid and
  3. Toggle the states of all the bulbs bounded by cell A and cell B, i.e. make all the ON lights OFF and make all the OFF lights ON which are bounded by A and B. To be clear, consider cell A is (x1, y1, z1) and cell B is (x2, y2, z2). Then you have to toggle all the bulbs in grid cell (x, y, z) where min(x1, x2) ≤ x ≤ max(x1, x2), min(y1, y2) ≤ y ≤ max(y1, y2) and min(z1, z2) ≤ z ≤ max(z1, z2).

Your task is to find the expected number of lights to be ON after K turns. Input Input starts with an integer T (≤ 50), denoting the number of test cases. Each case starts with a line containing four integers X, Y, Z (1 ≤ X, Y, Z ≤ 100) and K (0 ≤ K ≤ 10000). ~~~~~

Полный текст и комментарии »

  • Проголосовать: нравится
  • +7
  • Проголосовать: не нравится

Автор Madiyar, 12 лет назад, По-русски

Задача с acm.mipt.ru, 120 Range Query. Сделал за O(nlog^2(n)) но получаю time limit, есть ли другое решение или надо оптимизировать?
Я сделал с помощью дерево отрезков, внутри каждой вершины хранил декартку , но получаю time limit :(.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +12
  • Проголосовать: не нравится

Автор Madiyar, 12 лет назад, По-русски
  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится

Автор Madiyar, 13 лет назад, По-русски
Can you give me some advices, which math books are usefull for programming ?

Полный текст и комментарии »

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

Автор Madiyar, 13 лет назад, По-русски

http://acm.sgu.ru/problem.php?contest=0&problem=206


please , Help to solve this task

I reduced this task to linear programming, and linear programming to dual 

w[j] >= w[i] (where  j = n .. m  and i = 1 .. n-1 ) this is the least condition for minimality

and We must find x[i] (where i = 1..m) ,such that x[1] + x[2] + .. + x[m] is minimal

and conditions  are: 
w[j]+x[j] >= w[i] - x[i]

This equation is linear programming,  and dual form of this equation is the optimal match problem

so we can find sum of x[1]+x[2]+...+x[m], and how to find x[i] ?


 

Полный текст и комментарии »

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится