使用异或运算避免申请大数组进行操作, a使用double类型保证计算精度, 异或运算规则(二进制)如下0 ^ 1 = 1, 1 ^ 1 = 0, 0 ^ 1 = 0,即一真为真, 全真为假, 因为题目上说最后只有一盏灯会亮, 所以其余的所有的灯均为偶数次操作, 异或运算的特点是偶数次运算相当于没有操作即 0 ^ 1 = 1, 1 ^ 1 = 0, 例子如下:(1 2 3)-->(1 2), 1的二进制为1, 2的二进制为10, 3的二进制为11, 对0按如上顺序进行异或运算即0 ^ 1 = 1, 1 ^ 10 = 11, 11 ^ 11 = 00, 00 ^ 1 = 01, 01 ^ 10 = 11, 11转化为十进制为3.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int data_count;
int result = 0;
cin >> data_count;
for (int i = 0; i < data_count; i++)
{
double a;
int t;
cin >> a >> t;
for (int j = 1; j <= t; j++)
{
const int temp = static_cast<int>(std::floor(j * a));;
result ^= temp;
}
}
cout << result;
}