Luogu brush questions C++ language | P1161 Turn on the light

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

On an infinitely long road, there is a row of infinitely long street lights, numbered 1, 2, 3, 4, ....

Each light has only two possible states, on or off. If you press the switch of a certain light, the state of this light will change. If it was on, it will become off. If it was off, it will become on.

In the beginning, all the lights were off. Xiaoming can perform the following operations each time:

Specify two numbers, a , t ( a  is a real number, t  is a positive integer). Press the lights numbered ⌊ a ⌋, ⌊2× a ⌋, ⌊3× a ⌋,…,⌊ t × a ⌋ once each. where ⌊ k ⌋ represents the integer part of the real number  k  .

After Xiao Ming performed  n  operations, Xiao Ming suddenly found that only one light was on at this time. Xiao Ming wanted to know the number of this light, but this light was too far away from Xiao Ming, and Xiao Ming couldn’t see the number. .

Fortunately, Xiao Ming still remembers the previous  n  operations. So Xiao Ming found you, can you help him calculate the number of the light that is on?

【enter】

The first line is a positive integer  n , representing  n  operations.

Next there are  n  lines, each with two numbers, ai , ti . Among them  , ai  is a real number, there must be 6 digits after the decimal point, and ti  is a positive integer.

【Output】

Only one positive integer, the number of the light that was on.

【Input example】

3 1.618034 13 2.618034 7 1.000000 21

【Example of output】

20

【Code Explanation】

#include <bits/stdc++.h>
using namespace std;
int b[2000005]={0};

int main()
{
    int n,t;
    double a;
    cin >> n;
    for (int i=1; i<=n; i++) {
        cin >> a >> t;
        for (int j=1; j<=t; j++) {
            int temp = a*j;
            b[temp] = 1 - b[temp];
        }
    }
    for (int i=1; i<=2000000; i++) {
        if (b[i]==1) {
            cout << i;
            break;
        }
    }
    return 0;
}

【operation result】

3
1.618034 13
2.618034 7
1.000000 21
20

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132676236