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

Автор tredsused70, история, 15 месяцев назад, По-английски

Hi, I was solving this problem a few minutes ago, and the following problem is a subproblem of that one.

Given integer $$$n$$$, $$$ n \ge 3$$$, and array $$$a$$$ consisting of $$$n - 1$$$ integers, all of them are equal to some value $$$b$$$. Later, some integer $$$c$$$, such that $$$c \ne b$$$ is selected and inserted into the array $$$a$$$ at random position. By given $$$n$$$ and $$$a$$$ find $$$c$$$.

This problem itself isn't so difficult, but with a certain restriction I couldn't solve it. The restriction is folowing: you can't use $$$if$$$ and ternary operators in any way.

I came up with the solution, that calls $$$if$$$ only one time.

my solution

Can you solve this problem with the given restriction?

UPD: In the comments there is one of the solutions, and I've also came up with one.

my final solution

BledDest's solution is shorter, but mine is easier to understand, at least for me, this is just

spoiler

.

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

»
15 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by tredsused70 (previous revision, new revision, compare).

»
15 месяцев назад, # |
Rev. 3   Проголосовать: нравится +1 Проголосовать: не нравится

Sort any three elements, then let $$$x$$$ second element. $$$c=x\oplus\sum_aa\oplus x$$$

  • »
    »
    15 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I think this will find $$$b$$$, not $$$c$$$

  • »
    »
    15 месяцев назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks for the answer, but I can't really accept it. First, sorting has $$$if$$$ in itself, and second, this approach finds b, and not c. After edit it finds c, but still sorting requires $$$if$$$.

»
15 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Might be overkill, but zero if/else statements.

code
»
15 месяцев назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Is !! also banned?

If not, then this is the solution
  • »
    »
    15 месяцев назад, # ^ |
      Проголосовать: нравится +16 Проголосовать: не нравится
    Simpler version without `!!`
»
15 месяцев назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

I'm pretty late but yeah.

Spoiler
  • »
    »
    15 месяцев назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

    Thank you, pretty tricky solution.

  • »
    »
    15 месяцев назад, # ^ |
    Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

    Ooops looks like my solution is wrong when $$$n$$$ is even and $$$a_1=c$$$, for example:

    4

    1 2 1 1

    I guess it doesn't matter anymore, but here is corrected version:

    Spoiler
»
15 месяцев назад, # |
  Проголосовать: нравится +44 Проголосовать: не нравится

Another solution:

int main() {
    int n;
    cin >> n;
    vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i];
    int b = (a[0] & a[1]) | (a[1] & a[2]) | (a[0] & a[2]);
    int c = b;
    for (int i = 0; i < n; i++) c ^= (a[i] ^ b);
    cout << c << endl;
    return 0;
}
  • »
    »
    15 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Very cool. I like this solution a lot. It is very simple to understand, even for me, and very slick as well.

»
15 месяцев назад, # |
  Проголосовать: нравится +12 Проголосовать: не нравится

The problem is you can't really say if something is an if.

(flag ? x : y) is equivalent to x * flag + y * !flag, for example.

int main() {
    int n;
    cin >> n;
    vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i];
    int res = 0; for (int x : a) res ^= x;
    res ^= ((n & 1) ^ 1) * ((a[0] == a[1]) * a[0] ^ (a[0] == a[2]) * a[0] ^ (a[1] == a[2]) * a[1]);
    cout << res << endl;
    return 0;
}