洛谷P1161 开灯 题解 位运算(异或的应用)

题目链接:https://www.luogu.com.cn/problem/P1161

解题思路:
因为只有一盏灯是开着的,说明除了这盏灯被操作过奇数次以外,别的灯都被操作过偶数次(偶数包括0)。

所以我们只需要对所有的数进行异或操作,异或和就是我们的答案。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int n, t, ans;
double a;
int main() {
    scanf("%d", &n);
    while (n --) {
        scanf("%lf%d", &a, &t);
        for (int i = 1; i <= t; i ++) ans ^= (int) (a * i);
    }
    printf("%d\n", ans);
    return 0;
}
posted @ 2020-05-06 12:29  quanjun  阅读(314)  评论(0编辑  收藏  举报