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的更多相关文章

  1. hdu 1299 Diophantus of Alexandria(数学题)

    题目链接:hdu 1299 Diophantus of Alexandria 题意: 给你一个n,让你找1/x+1/y=1/n的方案数. 题解: 对于这种数学题,一般都变变形,找找规律,通过打表我们可 ...

  2. hdu 1299 Diophantus of Alexandria (数论)

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  3. 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+ ...

  4. 数学--数论--HDU 1299 +POJ 2917 Diophantus of Alexandria (因子个数函数+公式推导)

    Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first ma ...

  5. Diophantus of Alexandria[HDU1299]

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  6. hdu Diophantus of Alexandria(素数的筛选+分解)

    Description Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of ...

  7. Diophantus of Alexandria

    Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first ma ...

  8. Diophantus of Alexandria(唯一分解定理)

    Diophantus of Alexandria was an Egypt mathematician living in Alexandria. He was one of the first ma ...

  9. 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 ...

随机推荐

  1. python常用命令—ipython3环境下获取某个文件夹下的文件列表

    import os os.listdir('文件夹路径')

  2. docker容器学习笔记

    docker是通过内核虚拟化技术来提供容器的资源隔离与安全保障. docker组成: docker client.docker server.docker组件(镜像(image).容器(contain ...

  3. Ubuntu 常用软件推荐(QQ、微信、MATLAB等)及安装过程

    1. Wine QQ QQ 移植到 Linux 一直是一个比较头疼的问题,但我们日常交流.传输文件又离不开这个软件.在网上一番搜寻尝试后,发现最好的替代方案就是 Wine QQ,版本也还比较新,缺点是 ...

  4. 一道java笔试题

    输入一串用空格隔开的数字串,对于数字串的奇数位按升序排序,偶数位按降序排序. 示例输入: 4 6 2 3 6 7 8 1 处理过程: 奇数位:4 2 6 8 升序排序结果: 2 4 6 8 偶数位:6 ...

  5. C# Winform防止闪频和再次运行

    其实想实现只允许运行一个实例很简单,就是从program的入口函数入手.有两种情况: 第一种,用户运行第二个的时候给一个提示: using System; using System.Collectio ...

  6. react项目开发入门

    v16.2.0 在html头部引入react相关js文件 <!-- react核心库--><script src="../static/react/react.produc ...

  7. 统一日志系统 Log4Net/ExceptionLess

    一.   写在前面 本文Log4Net介绍了基础的方式,大数据量生产环境不能使用,中等日志量请日志单库. 希望爱技术的你不要错过exceptionless和ELK 第四节开始简单配置大牛们推荐的了Ex ...

  8. bootstrap 有些控件需要调用锚点,会与angular 路由 冲突

    最简单的方法 就是 在 #号前加/, 但有人说 在服务器上回失效,也不知道是什么原理.慎用 最靠谱的方法 就 是 使用bootstrap中的js控制控件, 比如轮播图的上一页 下一页,就可以在 ang ...

  9. byte字节的输入流 建议使用字节数组形式接受内容 因为当输入流是汉字时候 会超出字节的范围 出现无法读取的现象

    byte字节的输入流 建议使用字节数组形式接受内容 因为当输入流是汉字时候 会超出字节的范围 出现无法读取的现象

  10. BZOJ 1149 风铃(树形DP)

    题目描述的实际是一颗二叉树,对于每个结点,要么满叉,要么无叉. 对于一种无解的简单情况,我们搜一遍树找到最浅的叶子结点1和最深的叶子结点2,如果dep[1]<dep[2]-1,则显然无解. 所以 ...