ACM学习历程—HDU1719 Friend(数论)
Description
(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
Output
Sample Input
Sample Output
由题意,如果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(数论)的更多相关文章
- ACM学习历程—HDU5668 Circle(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- ACM学习历程—HDU5666 Segment(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...
- ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...
- ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)
http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...
- ACM学习历程—广东工业大学2016校赛决赛-网络赛F 我是好人4(数论)
题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=5 这个题目一看就是一道数论题,应该考虑使用容斥原理,这里对lcm进行容斥. ...
- ACM学习历程—HDU5637 Transform(数论 && 最短路)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给两种操作,然后给你一个s,一个t,求s至少需要多少次操作到t. 考虑到第一种操作是将 ...
- ACM学习历程—BZOJ2956 模积和(数论)
Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...
- ACM学习历程—SNNUOJ1132 余数之和(数论)
Description F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n).其中%表示Mod,也就是余数.例如F(6) = 6 % 1 + 6 % ...
随机推荐
- [读书笔记] learn python the hard way书中 有关powershell 的一些小问题
ex46中,创建自己的python, 当你激活环境时 .\.venvs\lpthw\ Scripts\activate 会报一个错误 此时需要以管理员身份运行PowerShell,(当前的PS不用关 ...
- Java 调用OPENOFFIC 转换文档类型
public static void office2PDF(String sourceFile, String destFile) { try { File inputFile = new File( ...
- 你要的最后一个字符就在下面这个字符串里,这个字符是下面整个字符串中第一个只出现一次的字符。(比如,串是abaccdeff,那么正确字符就是b了)
include "stdafx.h" #include<iostream> #include<string> using namespace std; in ...
- nginx教程1:location 匹配规则
worker_process # 表示工作进程的数量,一般设置为cpu的核数 worker_connections # 表示每个工作进程的最大连接数 server{} # 块定义了虚拟主机 liste ...
- ios -- 极光推送《3》 pod 方法
iOS SDK 集成指南 SDK说明 适用版本 本文匹配的 SDK版本:r2.1.5 以后.查看最近更新了解最新的SDK更新情况.使用Xcode 6及以上版本可以使用新版Push SDK,Xcode ...
- 搭建sftp服务+nginx代理
在公司,经常会用到sftp服务,比如两个公司对接生产项目,其中一方,要在sftp上上传pdf文件,另一方公司要在sftp服务器上用nginx代理直接下载pdf文件.下面就说说我在实际中应用到的sftp ...
- share初始化
要看懂share先看与map的交互以及跨地图的交互 share初始化 void Share::ShareInit(I_DataLayer* data_layer) { // 加载xls表 if(!Lo ...
- Open-sourcing LogDevice, a distributed data store for sequential data
https://logdevice.io/blog/2018/09/12/open-sourcing-announcement.html September 12, 2018 We are exc ...
- python cookbook第三版学习笔记十一:类和对象(二)调用父类的方法
在子类中调用父类的方法,可以下面的A.spam(self)的方法. class A(object): def spam(self): print 'A.spam' class ...
- HttpWebRequest的timeout和ReadWriteTimeout(转载)
公司[1]一牛人看我的代码,说我设置的timeout有误,还应该设置ReadWriteTimeout.本人很不服,于是上网查看了相关说明. HttpWebRequest httpWebRequest ...