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

Автор fossil823656, история, 4 года назад, По-английски

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
  • Проголосовать: не нравится

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
4 года назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +1 Проголосовать: не нравится

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

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Might be overkill, but zero if/else statements.

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

Is !! also banned?

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

I'm pretty late but yeah.

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

    Thank you, pretty tricky solution.

  • »
    »
    4 года назад, скрыть # ^ |
    ← 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
»
4 года назад, скрыть # |
 
Проголосовать: нравится +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;
}
»
4 года назад, скрыть # |
 
Проголосовать: нравится +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;
}