The final state of the last bulb is determined by the number of factors of n. Only the number which is the square of a number has an odd number of factors, thus it has a state of "yes".
Code:
- /***************************************************************************
- * Copyright (C) 2008 by Liu Kaipeng *
- * LiuKaipeng at gmail dot com *
- ***************************************************************************/
- /* @JUDGE_ID 00000 10110 C++ "Light, More Light" */
- #include <algorithm>
- #include <cmath>
- #include <cstdio>
- #include <cstring>
- #include <deque>
- #include <fstream>
- #include <iostream>
- #include <list>
- #include <map>
- #include <queue>
- #include <set>
- #include <stack>
- #include <string>
- #include <vector>
- using namespace std;
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
- freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
- #endif
- /* The final state of the last bulb is determined by the number of factors
- of n. Only the number which is the square of a number has odd number of
- factors, thus it has a state of "yes". */
- for (unsigned int n; cin >> n && n != 0; ) {
- unsigned int r = sqrt(n);
- if (r * r == n) cout << "yes/n";
- else cout << "no/n";
- }
- return 0;
- }