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. Django之信息聚合

    feeds.py #coding:utf-8 __author__ = 'similarface' from django.contrib.syndication.views import Feed ...

  2. Unix下C语言开发工具

    在Unix下进行C语言开发有一套工具,主要有: 编译器:cc,gcc,clang SSH登录工具:putty,xshell就不说了,尼玛还有后门 make工具:跟Maven一样是项目构建工具,这个使用 ...

  3. sigar 监控服务器硬件信息

    转载 http://www.cnblogs.com/jifeng/archive/2012/05/16/2503519.html 通过使用第三方开源jar包sigar.jar我们可以获得本地的信息 1 ...

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

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

  5. iOS 应用发布

    本文转载至  http://blog.csdn.net/ysy441088327/article/details/7833579 苹果为广大的开发者提供了一个很好的应用生态环境 参考资料: 1:如何向 ...

  6. Hadoop实战-Flume之Source multiplexing(十五)

    a1.sources = r1 a1.sinks = k1 k2 a1.channels = c1 c2 # Describe/configure the source a1.sources.r1.t ...

  7. DAICO模式到底是什么?

    自从V神提出DAICO以来,已经有项目围绕DAICO模式落地,但是DAICO到底是什么呢? 所谓DAIC0,其实就是DAO+IC0. DAO是DistributedAutonomous Organiz ...

  8. Android databinding 开发参考

    1,android studio添加databinding依赖需要注意事项: http://www.zhihu.com/question/33538477?sort=created 2, databi ...

  9. Java中的内存泄漏

    [转]介绍Java中的内存泄漏 1. 什么是内存泄漏? 内存泄漏的定义:对象已经没有被应用程序使用,但是垃圾回收器没办法移除它们,因为还在被引用着. 要想理解这个定义,我们需要先了解一下对象在内存中的 ...

  10. LightOJ - 1079 Just another Robbery —— 概率、背包

    题目链接:https://vjudge.net/problem/LightOJ-1079 1079 - Just another Robbery    PDF (English) Statistics ...