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 ...
随机推荐
- Greenplum同步到Oracle
开发提出须要从Greenplum同步到Oracle的解决方式,写了个脚本用于定时调度处理. #!/bin/sh #copy_gp_2_ora.sh if [ $# -ne 1 ]; then ...
- C#控件系列--文本类控件
C#控件系列--文本类控件 文本类控件主要包含Label.LinkLabel.Button.TextBox以及RichTextBox. Label 功能 Label用来 ...
- TCP三次握手和Time-Wait状态
第一次握手:建立连接时.client发送syn包和一个随机序列号seq=x到server,并进入SYN_SEND状态,等待server进行确认. (syn,同 步序列编号). 第二次握手,server ...
- windows时间函数
介绍 我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执 行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记 ...
- [IDEs]Eclipse设置花括号样式
用惯Vistual Studio,在使用Eclipse时发现有很多东西还是挺不习惯,第一个就要解决花括号的样式 步骤: 1.Windows->Preferences->Java->C ...
- 飘逸的python - yield简明教程
发现还有非常多人对yield不理解,云里雾里,于是试着用文字表述. 仅仅要函数含有yield语句,它就返回一个生成器.所以我们与其把其看成函数定义,不如看作是生成器定义.函数用return返回,而生成 ...
- uva315(求割点数目)
传送门:Network 题意:给出一张无向图,求割点的个数. 分析:模板裸题,直接上模板. #include <cstdio> #include <cstring> #incl ...
- 在html中写python代码的语法和特点-----基于webpy的httpserver
在html文件里写python语法的内容,的注意事项: 1:python程序中的变量通过以下方法传入到html: 1:通过全局变量 :全局变量是不须要用$def with语法实现传递的,仅仅要定义了 ...
- Selenium执行测试脚本稳定性的一些经验分享交流
Selenium执行测试脚本稳定性的一些经验分享交流 公司的自动化WEB测试框架IATA已上线运行了一段时间,期间发现一些脚本稳定性的问题,与大家分享一下. CASE执行游览器:ie firefox ...
- Cocos2d-x v3.0正式版尝鲜体验【3】 Label文本标签
Cocos2d-x在新版本号中增加了新的Label API.和以往不同的是,2.x的版本号是通过三个不同的类来创建不同的文本标签,而如今是模仿着精灵的创建方式.一个类创建不同形式的文本,只是核心内容还 ...