3 13121 12131
YES! YES! NO!
首先 1和2都是friend number
ab+a+b = (a+1)(b+1)-1; 设n是friend number 所以 n = (a+1)(b+1)-1; 但是a,b不一定只是1和2.也可以是(a+1)(b+1)-1,仔细推导一下就可知道friend number 一定是
(a+1)n次方和(b+1)m次方的乘积-1,(又因为1, 2是friend number)故 friend number 一定是2的n次方和3的m次方的乘积-1, 我们只需要令给出的n+1, 在对2和3除,看结果是否为1即可
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <vector> using namespace std; const int M = 105; #define LL __int64 int main(){ LL n; while(scanf("%I64d", &n) == 1){ if(n == 0){ printf("NO!\n"); continue; } n += 1; while(n%2 == 0) n /= 2; while(n%3 == 0) n /= 3; if(n == 1) printf("YES!\n"); else printf("NO!\n"); } return 0; }
原文:http://blog.csdn.net/shengweisong/article/details/44539323