hdoj 1299 Diophantus of Alexandria
hdoj 1299 Diophantus of Alexandria
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1299
题意:求 1/x + 1/y = 1/n (x <= y) 的组数。
思路:转化为一个数的因子个数。
因为x,y,z 都是整数,令 y = n+k (倒数和相等,x,y 明显大于 n),带入式子可得 x = n*n / k + n ;所以 x 的组数就与k相关了,只要 k 满足是 n*n 的约数,组数就 +1。假设 n = (p1^r1) * (p2^r2) * (p3^r3) * ... * (pn^rn),则 n 的约数个数为 (r1+1) * (r2+1) * ... * (rn+1), n * n 可分解为 n * n = (p1^2r1) * (p2^2r2) * … *(pn^2rn), 所以 n*n 的约数个数为 cnt = (2r1+1) * (2r2+1) * … * (2rn+1)。公式中的 p1,p2,……,pn 为素数。所以就转化为求素数的问题,这里用到线性筛法求 sqrt(n)内的素数。因为 x < y, 所以把结果除以2就得到答案。
代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std; typedef long int LL;
const int maxv = ;
int prime[], num[maxv]; void prim() //筛法求素数
{
int i, j, k = ;
for(i = ; i < maxv; ++i) num[i] = ;
prime[] = , num[] = ;
for(i = ; i < maxv; i += )
{
if(num[i]) prime[k++] = i;
for(j = ; (j<k && i*prime[j] < maxv); ++j)
{
num[i*prime[j]] = ;
if(i%prime[j] == ) break;
}
}
} int counter(int n) //计算约数个数
{
int cnt = , i, j, k = ;
int q;
i = (int)sqrt(n*1.0)+;
for(j = ; prime[j] <= i; ++j)
{
if(n % prime[j] == )
{
q = ;
while(n%prime[j] == ){
n = n/prime[j], q++;
}
cnt *= (*q+);
}
} if(n > )
cnt *= ;
return (cnt+)/;
}
int main()
{
int n;
int i = , cnt, t;
prim();
//freopen("hdoj1299.txt", "r", stdin);
cin >> t;
while(t--)
{
cin >> n;
cnt = counter(n);
cout<<"Scenario #" << i++ <<":\n" << cnt << endl << endl;
}
return ;
}
hdoj 1299 Diophantus of Alexandria的更多相关文章
- hdu 1299 Diophantus of Alexandria(数学题)
题目链接:hdu 1299 Diophantus of Alexandria 题意: 给你一个n,让你找1/x+1/y=1/n的方案数. 题解: 对于这种数学题,一般都变变形,找找规律,通过打表我们可 ...
- hdu 1299 Diophantus of Alexandria (数论)
Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- hdu 1299 Diophantus of Alexandria
1/x + 1/y = 1/n 1<=n<=10^9给你 n 求符合要求的x,y有多少对 x<=y// 首先 x>n 那么设 x=n+m 那么 1/y= 1/n - 1/(n+ ...
- 数学--数论--HDU 1299 +POJ 2917 Diophantus of Alexandria (因子个数函数+公式推导)
Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first ma ...
- Diophantus of Alexandria[HDU1299]
Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- hdu Diophantus of Alexandria(素数的筛选+分解)
Description Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of ...
- Diophantus of Alexandria
Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first ma ...
- Diophantus of Alexandria(唯一分解定理)
Diophantus of Alexandria was an Egypt mathematician living in Alexandria. He was one of the first ma ...
- hdu-1299 Diophantus of Alexandria(分解素因子)
思路: 因为x,y必须要大与n,那么将y设为(n+k);那么根据等式可求的x=(n2)/k+n;因为y为整数所以k要整除n*n; 那么符合上面等式的x,y的个数就变为求能被n*n整除的数k的个数,且k ...
随机推荐
- Java学习 · 初识 面向对象基础二
Package 为什么需要使用package a) 解决类重名的问题 b) 便于管理类 怎么使用package a) 类的第一句非注释性语句 b) 命名:域名倒着写,再加上模块名 注意 ...
- 什么是Spark
什么是Spark Apache Spark是一个开源集群运算框架, 相对于Hadoop的MapReduce会在运行完工作后将中介数据存放到磁盘中,Spark使用了存储器内运算技术,能在数据尚未写入硬盘 ...
- 技本功丨知否知否,Redux源码竟如此意味深长(下集)
上集回顾 Redux是如何使用的?首先再来回顾一下这个使用demo(谁让这段代码完整地展示了redux的使用) 如果有小伙伴对这段代码不是很理解的话,建议先去学习Redux的使用再来看这篇源码,这样更 ...
- LeetCode 144 ——二叉树的前序遍历
1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 将当前节点的数值加入到 data 中 递归得到其左子树的数据向量 temp,将 te ...
- POJ 3845 Fractal(计算几何の旋转缩放)
Description Fractals are really cool mathematical objects. They have a lot of interesting properties ...
- Python中除法:/和//
在Python中,除法有两种:/和//. X / Y 对于Python2.X来说,如果两个操作数都是整数,那么结果将向下取整(这个和C里面的不同,C里面是向0取整),也就是说,如果结果本来是-2.5, ...
- Alpha-2
前言 失心疯病源2 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 今天完成了那些任务 17:30~21:30 又测试了一些算法和代码,时间不能再拖下去了,要尽快进入代码阶段,决 ...
- LintCode-105.复制带随机指针的链表
复制带随机指针的链表 给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点. 返回一个深拷贝的链表. 挑战 可否使用O(1)的空间 标签 哈希表 链表 优步 code / ...
- 【week4】课堂Scrum站立会议
项目:连连看游戏 小组名称:天天向上(旁听) 小组成员:张政 张金生 李权 武致远 已完成任务 1.本项目采用c#. 2. 初步界面. 形成一个windows下的游戏界面,每个需要消除的方块是一个bu ...
- vue服务端渲染axios预取数据
首先是要参考vue服务端渲染教程:https://ssr.vuejs.org/zh/data.html. 本文主要代码均参考教程得来.基本原理如下,拷贝的原文教程. 为了解决这个问题,获取的数据需要位 ...