uva10791 uva10780(分解质因数)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1732
给定我们一个n, 要找到两个数的集合,使得这些书的最小公倍数(LCM)为n,由于有很多这样的集合,我们要选出总和最小的,而且也只要输出总和就行了
数的最大公倍数是怎么求的? 是每个质因数指数最大的那个相乘而来的。
通过最小公倍数的求法,我们可以看出最小公倍数取决于每个质因子在各个数中的最高次。
如果要总和最小,我们要把同一个质因数放到一个整数里面
因为a*b>a+b
比如12 = 3 * 2^2 a = 3, b = 2 a * b > a + b 即2*3 > 2 + 3
所以最终的结果是3 * 4 = 12, 此时和最小,为7
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/* */
int main()
{
LL n, i, m, k = ;
LL ans;
while (scanf("%lld", &n), n)
{
int nn = n;
ans = ;
m = sqrt(n) + 0.5;
int cnt = ;
for (i = ; i <= m; ++i)
{
if (n%i == )
{
int t = ;
while (n%i == )
{
t *= i;
n /= i;
}
ans += t;
cnt++;
}
}
if (n > )
{
ans += n;
cnt++;
}
if (cnt == )//这是只有单独一个数的情况
ans += ;
else if (cnt == )//这是n为1的情况
ans += ;
printf("Case %lld: %lld\n", k++,ans);
}
return ;
}
uva10780 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1721
给定m 和 n
求最大的x 使得 n % m^x ==0,
思路:将m分解质因数,相同的质因数合并, 那么可以得到 p1^a1 , p2^a2..pn^an , 我们只要求出n!中质因数pi 是pi^ai的多少次幂, 然后最小的那个次幂就是答案
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/* */
int a[ + ];
int main()
{
int t, n, m;
scanf("%d", &t);
for (int k = ; k <= t; ++k)
{
int ans = INF;
scanf("%d%d", &m, &n);
int limit = sqrt(m) + 0.5;
for (int i = ; i <= limit; ++i)
{
if (m%i == )
{
int cnt = ;
while (m%i == )
{
cnt++;
m /= i;
}
int tmp = ;
for (int j = ; j <= n; ++j)
{
int x = j;
while (x % i == )
{
x /= i;
tmp++;
}
}
ans = min(ans, tmp/cnt);
}
}
if (m > )
{
int tmp = ;
for (int j = ; j <= n; ++j)
{
int x = j;
while (x % m == )
{
x /= m;
tmp++;
}
}
ans = min(ans, tmp);
}
if (ans == || ans==INF)
printf("Case %d:\nImpossible to divide\n",k);
else
printf("Case %d:\n%d\n", k, ans);
}
return ;
}
uva10791 uva10780(分解质因数)的更多相关文章
- uva10780(分解质因数)
可以直接用高精度来暴力求. 也可以不用高精度: 把m分解质因数,记录每个因数和它的次数.然后计算每个因数在n的阶乘里出现了多少次,再把这个次数除以它在m中的次数,就是可能的k值.取最小的k. #inc ...
- java分解质因数
package test; import java.util.Scanner; public class Test19 { /** * 分析:对n进行分解质因数,应先找到一个最小的质数k * 最小 ...
- 程序设计入门——C语言 第6周编程练习 1 分解质因数(5分)
1 分解质因数(5分) 题目内容: 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数.比如,6可以被分解为2x3,而24可以被分解为2x2x2x3. ...
- 【python】将一个正整数分解质因数
def reduceNum(n): '''题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5''' print '{} = '.format(n), : print 'Pleas ...
- light oj 1236 分解质因数
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/H 题意:求满足1<=i<=j<=n ...
- 【基础数学】质数,约数,分解质因数,GCD,LCM
1.质数: 质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能整除以其他自然数(质数),换句话说就是该数除了1和它本身以外不再有其他的因数. 2.约数: 如 ...
- 将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m
给定两个数m,n,其中m是一个素数. 将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m. 输入 第一行是一个整数s(0<s<=100),表示测试数据的组数 随后 ...
- cdoj 1246 每周一题 拆拆拆~ 分解质因数
拆拆拆~ Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1246 Descri ...
- hdu 5428 The Factor 分解质因数
The Factor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest ...
随机推荐
- ios html5 设定PhoneGap开发环境
怎么样IOS平台搭建PhoneGap开发环境(PhoneGap2.5) (2013-03-13 14:44:51) 标签: c=blog&q=it&by=tag" targe ...
- OpenRisc-32-ORPSoC烧写外部spi flash
引言 经过前面的分析和介绍,我们对ORPSoC的启动过程(http://blog.csdn.net/rill_zhen/article/details/8855743)和 ORpSoC的debug子系 ...
- Typings实现智能
在Visual Studio Code中通过Typings实现智能提示功能 前言 我们知道在IDE中代码的智能提示几乎都是标配,虽然一些文本编辑器也有一些简单的提示,但这是通过代码片段提供的.功能 ...
- hdu1896之优先队列应用
Stones Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Sub ...
- DL动态载入框架技术文档
DL动态载入框架技术文档 DL技术交流群:215680213 1. Android apk动态载入机制的研究 2. Android apk动态载入机制的研究(二):资源载入和activity生命周期管 ...
- [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
先来说一下我们学校的网站: http://jwxt.sdu.edu.cn:7777/zhxt_bks/zhxt_bks.html 查询成绩需要登录,然后显示各学科成绩,但是只显示成绩而没有绩点,也就是 ...
- hdu1054(最小顶点覆盖)
传送门:Strategic Game 题意:用尽量少的顶点来覆盖所有的边. 分析:最小顶点覆盖裸题,最小顶点覆盖=最大匹配数(双向图)/2. #include <cstdio> #incl ...
- python实现人人网用户数据爬取及简单分析
这是之前做的一个小项目.这几天刚好整理了一些相关资料,顺便就在这里做一个梳理啦~ 简单来说这个项目实现了,登录人人网并爬取用户数据.并对用户数据进行分析挖掘,终于效果例如以下:1.存储人人网用户数据( ...
- MVC之文件上传1
MVC之文件上传 前言 这一节我们来讲讲在MVC中如何进行文件的上传,我们逐步深入,一起来看看. Upload File(一) 我们在默认创建的项目中的Home控制器下添加如下: public Act ...
- 怎样用Google APIs和Google的应用系统进行集成(1)----Google APIs简介
Google的应用系统提供了非常多的应用,比方 Google广告.Google 任务,Google 日历.Google blogger,Google Plus,Google 地图等等非常的多的应用,请 ...