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 % ...
随机推荐
- 转:office 2016最新安装及激活教程(KMS)
office 2016最新安装及激活教程(KMS)[亲测有效]!! win7激活教程 博主的一个朋友,咳咳……你们懂得,想装office,于是我就上网找了一下激活的方法,亲测有效,而且也没有什么广 ...
- 网络通信数据处理 Xbytestring类
PS_Xbytestring a byte string for store low level data type 文件夹[TOC] PS_Xbytestring 文件夹TOC base info ...
- Intellij IDEA如何不显示参数提示
刚安装了IDEA之后,调用方法的时候会提示方法中的参数,就像下面这样: 虽然IDEA也是好心,提示,但是劳资看着难受啊. 如果觉得不习惯,不想看参数名,可以用下图的方式取消.具体是: setting ...
- OpenSUSE 13.1上安装StrongSwan
结果: 1)iOS 7.1设备能够拨IPSec VPN到StrongSwan电脑上面来 - Connect to VPN 2)iOS 设备浏览器能够訪问StrongSwan VPN所在的内网地址服务器 ...
- IIS 配置错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。 HTTP 错误 500.19
因为 IIS 7 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改.运行命令行 %windir%\system32\inetsrv\appcmd unlock conf ...
- go web的简单服务器
1)简单web服务器: package main import ( "fmt" "net/http" ) func sayHelloName(w http.Re ...
- ADO.NET Data Service
关于ADO.NET Entity Framework部分的内容见ADO.NET Entity Framework(1-4) http://www.cnblogs.com/foundation/arch ...
- DBUtils使用详解
https://blog.csdn.net/samjustin1/article/details/52220423 https://www.cnblogs.com/CQY1183344265/p/58 ...
- Java泛型【转】
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: public class GenericTest { public static void main(String[] a ...
- 1355: [Baltic2009]Radio Transmission[循环节]
1355: [Baltic2009]Radio Transmission Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 796 Solved: 538 ...