题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1719
题目翻译:
(1) 1, 2是朋友数
(2) a,b是朋友数,则a*b + a + b也是朋友数
给出一个数N,求它是否是朋友数。
a*b+a+b = (a+1)*(b+1)-1,最开始的两个朋友数是1,2,则下一个朋友数是2*3-1=5
1 2 5. 下一个朋友数是2*6-1 = 11,从公式可以知道 ,对于一个数N,如果N+1,可以除2或除3
尽,则它就是朋友数,否则它不是朋友数,注意0也不是朋友数。
#include <iostream>
#include <stdio.h>
using namespace std;
const int maxn = 50000;
int main() {
int N;
while(~scanf("%d",&N)) {
if(N == 0) {
printf("NO!\n");
}
else {
int temp = N+1;
while(temp%2==0 || temp%3==0) {
if(temp%2 == 0) {
temp = temp/2;
}
else {
temp = temp/3;
}
}
if(temp == 1) {
printf("YES!\n");
}
else {
printf("NO!\n");
}
}
}
return 0;
}