Description

Friend number are defined recursively as follows.
(1) numbers 1 and 2 are friend number;

(2) if a and b are friend numbers, so is ab+a+b;

(3) only the numbers defined in (1) and (2) are friend number.

Now your task is to judge whether an integer is a friend number.
 

Input

There are several lines in input, each line has a nunnegative integer a, 0<=a<=2^30.
 

Output

For the number a on each line of the input, if a is a friend number, output “YES!”, otherwise output “NO!”.
 

Sample Input

3
13121
12131
 

Sample Output

YES!
YES!
NO!

由题意,如果n = a+b+ab,a和b都是friend number

那么(n+1) = (a+1) * (b+1),

然后我记friend number叫做好数。

那么2和3是好数。

然后两个好数相乘也是好数。

由于其他数首先都是由2和3生出的,所以好数必然是2^k * 3^p。

接下来证明所有2^k * 3^p都是好数。

反证:

若2^k * 3^p不是好数,那么2^(k-1) * 3^p必然也不是好数,否则2^(k-1) * 3^p和2相乘会导致2^k * 3^p也是好数。

然后递降下来说明了3^p也不是好数。

同理说明了3不是好数。

矛盾!

所以所有2^k * 3^p都是好数。

于是判断好数只需要先把二因子除去,这里采用位运算优化。

然后除去3因子,判断最后结果是不是1。这里打表保存了3的所有指数密进行判断。

能判断好数了,自然能判断friend number了。不过需要对0进行特判。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#define LL long long using namespace std; const int maxn = <<; set <LL> s; void Init()
{
int now = ;
s.insert();
for (;;)
{
if (now > maxn)
break;
s.insert(now);
now = *now + ;
}
now = ;
for (;;)
{
if (now > maxn)
break;
s.insert(now);
now = *now + ;
} now = ;
for (;;)
{
if (now > maxn)
break;
s.insert(now);
now = *now + ;
}
} bool judge(LL n)
{
if (no.find(n) != no.end())
return false;
if (s.find(n) != s.end())
return true;
n++;
int len = sqrt(n);
for (int i = ; i <= len; ++i)
{
if (n % i)
continue;
if (judge(i-)&&judge(n/i-))
{
s.insert(n);
return true;
}
}
no.insert(n);
return false;
} int main()
{
//freopen("test.in", "r", stdin);
LL n;
Init();
while (scanf("%I64d", &n) != EOF)
{
if (judge(n))
printf("YES!\n");
else
printf("NO!\n");
}
return ;
}

ACM学习历程—HDU1719 Friend(数论)的更多相关文章

  1. ACM学习历程—HDU5668 Circle(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...

  2. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  3. ACM学习历程—HDU5666 Segment(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...

  4. ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...

  5. ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)

    http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...

  6. ACM学习历程—广东工业大学2016校赛决赛-网络赛F 我是好人4(数论)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=5 这个题目一看就是一道数论题,应该考虑使用容斥原理,这里对lcm进行容斥. ...

  7. ACM学习历程—HDU5637 Transform(数论 && 最短路)

    题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给两种操作,然后给你一个s,一个t,求s至少需要多少次操作到t. 考虑到第一种操作是将 ...

  8. ACM学习历程—BZOJ2956 模积和(数论)

    Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...

  9. ACM学习历程—SNNUOJ1132 余数之和(数论)

    Description F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n).其中%表示Mod,也就是余数.例如F(6) = 6 % 1 + 6 % ...

随机推荐

  1. js 组合键监听ctrl + enter

    $(window).keydown(function (event) { // 监听esc键退出全屏 if (event.keyCode == 27) { } // 监听 Ctrl + Enter 可 ...

  2. 说说我的web前端之路,分享些前端的好书(转)

    WEB前端研发工程师,在国内算是一个朝阳职业,这个领域没有学校的正规教育,大多数人都是靠自己自学成才.本文主要介绍自己从事web开发以来(从大二至今)看过的书籍和自己的成长过程,目的是给想了解Java ...

  3. ubantu 下 修改mysql 默认编码

    启动mysql后,以root登录mysql root@Eadgar-virtual-machine:~# mysql -uroot -proot mysql> show variables li ...

  4. 一、Silverlight中使用MVVM(一)——基础

    如果你不知道MVVM模式,我建议你先了解一下MVVM模式,至少要知道实现该模式的意图是什么. 那么我主要通过我认为是已经算是比较简单的例子进行讲解这个模式,当然后面我们会在这个例子的基础上一步一步的进 ...

  5. mnesia的脏读和事物读的测试

    在mnesia中,有脏读脏写等以及事物读写,它们的差异通过测试不难发现: 代码如下: -module(mnesia_read_test). -compile(export_all). -record( ...

  6. 【Android】百度地图自定义弹出窗口

    我们使用百度地图的时候,点击地图上的Marker,会弹出一个该地点详细信息的窗口,如下左图所示,有时候,我们希望自己定义这个弹出窗口的内容,或者,干脆用自己的数据来构造这样的弹出窗口,但是,在百度地图 ...

  7. Jquery和JS的区别

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Error -27728: Step download timeout (120 seconds)的解决方法(转)

    LR中超时问题解决方法 超时错误在LoadRunner录制Web协议脚本回放时超时经常出现. 现象1:Action.c(16): Error -27728: Step download timeout ...

  9. TCP的四种定时器简单记录

    TCP管理的4个不同的定时器: 1.重传定时器:用于当希望收到另一端的确认. 2.坚持定时器:使窗口大小信息保持不断流动. 3.保活定时器:检测TCP空闲连接的另一端何时崩溃或重启. 4.2MSL定时 ...

  10. yarn_action

    https://maprdocs.mapr.com/home/AdministratorGuide/ResourceAllocation-YARNContainer.html yarn.schedul ...