HDU-1850 Being a Good Boy in Spring Festival(HDU-1850 Being a Good Boy in Spring Festival)

Being a Good Boy in Spring Festival

尼姆博弈

求出异或和之后,\(sum \oplus num[i]\) 就是除去当前值,其他数字的异或和,为了使整体异或为 0,所以就要让此时的 \(num[i]\) 变成 \(sum \oplus num[i]\),因此,如果 \(num[i] > sum \oplus num[i]\) 则改变这个 \(num[i]\) 可行,等于的时候相当于没拿,所以不可行

tips: \(\oplus\) 的优先级比 \(>\) 低

#include <iostream>
using namespace std;
const int maxn = 110;
int num[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    while(cin >> n && n)
    {
        for(int i=0; i<n; i++) cin >> num[i];
        int sum = 0;
        for(int i=0; i<n; i++) sum ^= num[i];
        int ans = 0;
        for(int i=0; i<n; i++) if(num[i] > (sum ^ num[i])) ans++;
        if(sum == 0) ans = 0;
        cout << ans << endl;
    }
    return 0;
}
————————

Being a Good Boy in Spring Festival

Nimm Game

After the XOR sum is obtained, \ (sum \ oplus num [i] \) is to remove the current value, and the XOR sum of other numbers. In order to make the overall XOR 0, it is necessary to make the current \ (Num [i] \) become \ (sum \ oplus num [i] \). Therefore, if \ (Num [i] & gt; sum \ oplus num [i] \) is changed, it is feasible to change this \ (Num [i] \) when it is equal to zero, so it is not feasible

Tips: \ (\ oplus \) has a lower priority than \ (& gt; \)

#include <iostream>
using namespace std;
const int maxn = 110;
int num[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    while(cin >> n && n)
    {
        for(int i=0; i<n; i++) cin >> num[i];
        int sum = 0;
        for(int i=0; i<n; i++) sum ^= num[i];
        int ans = 0;
        for(int i=0; i<n; i++) if(num[i] > (sum ^ num[i])) ans++;
        if(sum == 0) ans = 0;
        cout << ans << endl;
    }
    return 0;
}