Tuesday, 4 May 2021

UVA 10110 - Light, more light

In this problem there will be a number given.

we had to find if the n'th light is on or off? if a light is toggle odd number of time than it is on otherwise its off.

Observations: a number has odd number of factors if and only if it is a perfect square.

Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    ll n;
    while(1){
        cin>>n;
        if(n==0){
            break;
        }
        ///just has to check n is a perfect square or not? because all of the perfect square only
        ///has odd number of factors. and if number of factors is odd then it will be on.
        ll xx= sqrt(n);
        if(xx*xx==n){
            cout << "yes\n";
        }
        else cout << "no\n";
    }
    return 0;
}

No comments:

Post a Comment

If you have any doubts, let me know through comments

Monkey Banana Problem lightoj 1004

  In this problem we will check which adjacent cell will benefits the monkey best. For this, we will check all possible solution of this pro...