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 ...
随机推荐
- 5.安装hbase
下载安装包并解压设置hbase环境变量配置hbase-site.xml启动hbase检测hbase启动情况测试hbase shell 下载安装包并解压 https://mirrors.tuna.tsi ...
- BZOJ 3166 HEOI2013 ALO 可持久化trie+st表
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3166(洛谷上也有) 题意概述: 给出一个序列,对于一个区间,其权值为区间中的次大值亦或区 ...
- 使用HTML5制作loading图
昨天发了一篇使用HTML5 canvas写的时钟的文章,今天发一篇关于使用HTML5制作loading图的文章. <!DOCTYPE html> <html> <head ...
- 使用libpcab抓包&处理包
#include <stdio.h> #include <stdlib.h> #include <strings.h> #include <string.h& ...
- “Hello World!”团队第三周召开的第五次会议
一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码地址 一.会议时间 2017年10月31日 11:45-12:17 二.会议地点: ...
- win7 个人电脑 IIS7服务器(web服务器) 同一局域网下均可访问本机网页
建立web服务器: 1.控制面板-->程序-->打开或关闭windows功能-->internet信息服务全部打钩,确定即可. 访问网页: 1.C:\inetpub\wwwroot\ ...
- c#和.net区别
.net 包含两大部分:.net framework类库和公共语言运行库(CLR) .net framework类库,就是微软工程师写好的各种功能类,例如math类. 公共语言运行库:1.与操作系统进 ...
- 有关c#的学习笔记整理与心得
[ 塔 · 第 一 条 约 定 ] 整理c#:Array Arraylist List Hashtable Dictionary Stack Queue等 Array 的容量是固定的,而 ArrayL ...
- Python自定义包在linux服务器导入错误的解决办法
在本地机器上跑python代码,自己定义的文件进行导包运行是没有问题,但是放到linux服务器上的时候就会提示 ImportError:No module named xxxx(要导入的文件包名) 在 ...
- go的IO函数,整理下最基本的IO处理函数,工欲善其事必先利其器
bufio.NewScanner()函数是一行一行地读,但是对/proc/函数,这里不是个好方法,最好是把所有的数据一次读完,然后再去读,有没有这样读的接口呢?把所有数据都读入到内存中然后再通过通过搜 ...