PC/UVa:110701/10110
可以不用模拟每一次按了哪些灯的开关,因为第1次和第n次的操作使得最后一盏灯恢复到初始状态,所以统计n的因子个数,根据奇偶判断。
第一次统计因子个数的时候,先判断除数能不能整除被除数,然后根据除数和商的大小关系增加因子个数,然后就超时了,因为这几乎会试除到n-1。
第二次提交改成了先判断除数和商的关系,uDebug上的第一个测试用例的速度明显提高了,但是依然超时了。
然后既然因子都是成对出现的,那只要n不是某个数的平方,就肯定是关着的。 ???
#include <iostream>
using namespace std;
int cntFactors(unsigned int n)
{
int cnt = 0;
unsigned int q;
cnt += 2;
for (unsigned int i = 1;; i++)
{
if (n % i == 0){
q = n / i;
if (q * i == n){
if (q > i) cnt += 2;
else if (q == i){
cnt += 1;
break;
}
else break;
}
}
}
return cnt;
}
int cntFactors2(unsigned int n)
{
int cnt = 0;
unsigned int q;
if ((n & 0x1) == 0){
cnt += 2;
for (unsigned int i = 2;; i += 2)
{
q = n / i;
if (q < i) break;
if (q * i == n){
if (q > i) cnt += 2;
else if (q == i){
cnt += 1;
break;
}
else break;
}
}
}
else{
for (unsigned int i = 1;; i += 2)
{
q = n / i;
if (q < i) break;
if (q * i == n){
if (q > i) cnt += 2;
else if (q == i){
cnt += 1;
break;
}
else break;
}
}
}
return cnt;
}
int cntFactors3(unsigned long long n)
{
unsigned long long product;
int cnt;
for (unsigned long long i = 0;; i++)
{
product = i * i;
if (product == n) return 1;
else if (product > n) return 0;
}
}
int main()
{
unsigned int n = 0;
while (cin >> n){
if (n == 0) break;
int cnt = cntFactors3(n);
if (cnt == 0) cout << "no" << endl;
else cout << "yes" << endl;
}
return 0;
}
/*
3
6241
8191
0
*/
229

被折叠的 条评论
为什么被折叠?



