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 ...
随机推荐
- Win32环境下的程序崩溃异常定位
1 案例描述 作为Windows程序员,平时最担心见到的事情可能就是程序发生了崩溃(异常),这时Windows会提示该程序执行了非法操作,即将关闭.请与您的供应商联系.呵呵,这句微软的“名 ...
- PHP从数据库获取的下拉树
<?php include "config.php"; include "mysql.php"; $db = new Mysql('test'); //几 ...
- [ExtJS5学习笔记]第第二十四次 Extjs5形式上gridpanel或表单数据后台传输remoteFilter设定
本文地址:http://blog.csdn.net/sushengmiyan/article/details/39667533 官方文档:http://docs.sencha.com/extjs/5. ...
- 9月mob(ShareSDK)活动预告,这个秋天非常热
9月秋天来临,广州的天气依旧非常热,广州的活动氛围更热~ 先有GMGC B2B对接会在广州创新谷,再有上方网TFC全球移动游戏开发人员大会来袭,游戏圈的火越烧越旺,成都GMGDC全球移动游戏开发人员大 ...
- SystemParametersInfo API学习(128个中文参数解释,215个实际值)
uiAction:该参数指定要查询或设置的系统级参数.其取值如下:SPI_GETACCESSTIMEOUT:检索与可访问特性相关联的超时段的信息,PvParam参数必须指向某个ACCESSTIMEOU ...
- openwrt 3g模块上网
硬件环境: 开发板为RT5053F 3G模块为中兴 MC2176 电信版 以下是操作步骤 加入VID .PID VID . PID 的获取方法是 将设备插入电脑在linux下执行 ...
- nginx & flup & django & python3.x @ window7配置备忘录
最近考虑原Prism建筑(非职业.半专业人士认为C/S建筑)至B/S迁移,主要是由于部署问题,包括两个因素:已经做,虽然一键安装和部署的一个因素,心存顾虑,虽然我一再声明这是一个绿色软件.还有一个因素 ...
- A Game of Thrones(17) - Bran
It seemed as though he had been falling for years. Fly, a voice whispered in the darkness, but Bran ...
- make 2>&1 | tee log.txt之小析
前言 接触过linux的人,或多或少都会了解一点make 2>&1 | tee log.txt这个命令. 1. make是什么? make是linux下一个非常强大的命令,简单点就是你要 ...
- csdn肿么了,这两天写的博文都是待审核
昨天早上8点写了一篇博文,然后点击发表,结果系统显示"待审核".于是仅仅好qq联系csdn的客服,等到9点时候,csdn的客服上线了,然后回复说是链接达到5个以上须要审核,于是回到 ...