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 ...
随机推荐
- 利用maven进行项目管理
下面为maven项目管理的一个结构 首先pom是路径文件,我们在编译或是运行程序时调用到jdk或一些自己写的jar包时会需要指明物理路径,这里的pom是一样的道理,同时在maven的管理下多出来了一些 ...
- mybatis 枚举类型使用
一.首先定义接口,提供获取数据库存取的值得方法,如下: public interface BaseEnum { int getCode(); } 二.定义mybatis的typeHandler扩展类, ...
- [leetcode-783-Minimum Distance Between BST Nodes]
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)
Description The stable marriage problem consists of matching members of two different sets according ...
- Thunder团队第六周 - Scrum会议2
Scrum会议2 小组名称:Thunder 项目名称:i阅app Scrum Master:宋雨 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...
- android仿win8
在 eoe上偶然发现已经有人实现了这个功能的源码(地址:http://www.eoeandroid.com /forum.php?mod=viewthread&tid=327557),马上下载 ...
- phpshell提权
实际操作中可以在webshell用udf.dll提权,用函数的上传文件功能上传文件到启动目录,再用shut函数重起系统.(目前没成功过,有 机会本地测试一下,先记录在这了).如果是英文版的系统,启动目 ...
- C++ 普通函数和虚函数调用的区别
引出:写个类A,声明类A指针指向NULL,调用类A的方法会有什么后果,编译通过吗,运行会通过吗? #include<stdio.h> #include<iostream> us ...
- xpath教程三---逐层检索和全局检索
本节主要介绍用xpath来描述html的层级关系 主要使用到的知识点如下: 单独的一个点 .,表示当前位置 两个点 ..,表示上一级父标签的位置 单独的一个斜杠 /,表示只检索下面一级 单独的两个斜杠 ...
- cacti 添加redis监控(远程服务器)
监控主机 192.168.24.69 ,以下用A表示 被监控主机 192.168.24.79,以下用B标识 记得在A服务器的cacti中导入监控mysql的templates文件 拷贝ss_get ...