UVA-10791 数学

UVA-10791

题意:

输入n (1<=n<2^31) 求至少两个正整数使得他们的lcm等于n并且他们的和最小,输出最小和

代码:

// a*b=lcm*gcd => a=lcm*gcd/b; a+b=b+lcm*gcd/b 显然gcd越小a+b就越小,即gcd=1,所以几个数
// 互质时他们的和最小。求n的所有的质因子求和。
// 题目要求至少两个数所以要注意n==1和n只有一种质因子的情况。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
ll solve(ll n){
    ll nn=n,ans=0;
    int m=sqrt(n+0.5),cnt=0;
    for(int i=2;i<=m;i++){
        if(n==1) break;
        if(n%i==0){
            cnt++;
            ll tmp=1;
            while(n%i==0){
                tmp*=i;
                n/=i;
            }
            ans+=tmp;
        }
    }
    if(n>1) ans+=n;
    if(cnt==1&&n==1) ans++;//只有一种素因子的情况
    if(n==nn) ans=n+1;//n是素数的情况
    return ans;
}
int main()
{
    ll n;
    int cas=0;
    while(scanf("%lld",&n)==1&&n){
        ll ans=solve(n);
        if(n==1) ans=2;
        printf("Case %d: %lld\n",++cas,ans);
    }
    return 0;
}
时间: 2024-08-01 21:41:58

UVA-10791 数学的相关文章

uva 10791 Minimum Sum LCM ( 唯一分解定理 )

使用唯一分解定理的时候不一定要打出素数表,这句话是相对上一篇来讲的.做这道题目之前我对唯一分解定理方法的理解不完全. 现在多想到了一些 唯一分解,将当前需要分解的n用因子将其分解表达.需要试因子. 因子的枚举应该是从2开始(从1开始没有意义),当当前数字n可以整除当前因子i时,就使其不断除以i,直到不能整除. 这个步骤实际上已经在根本上避免了出现像4.6这种因子在唯一分解式中的出现--之前的因子2和3已经将其代替了.所以可证明唯一分解时并不一定需要构造素数表 针对本题来说,最小公倍数的最小和,有

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

UVA - 10791 - Minimum Sum LCM (数论相关!)

题目链接:Minimum Sum LCM UVA - 10791 Minimum Sum LCM Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu SubmitStatus Description  Minimum Sum LCM  LCM (Least Common Multiple) of a set of integers is defined as the minimum number, whic

UVA 11889-Benefit(数学_快速枚举因子)

Recently Yaghoub is playing a new trick to sell some more. When somebody gives him A Tomans, he who never has appropriate changes, asks for B Tomans such that lowest common multiple of A and B equals to C and he will pay back a round bill. Or otherwi

UVA 10791

题目链接:https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1732 hint:素因子分解 #include <iostream> #include <cstdio> #include <cmath> using namespace std; typedef long long LL; int main() { int m,ss,flag,

UVA 10127- Ones 数学

Given any integer 0 ≤ n ≤ 10000 not divisibleby 2 or 5, some multiple of n is a number whichin decimal notation is a sequence of 1’s. Howmany digits are in the smallest such a multipleof n?InputA ?le of integers at one integer per line.OutputEach out

uva 10791 Minimum Sum LCM

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma

UVA - 10791 Minimum Sum LCM(最小公倍数的最小和)

题意:输入整数n(1<=n<231),求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小.输出最小的和. 分析: 1.将n分解为a1p1*a2p2--,每个aipi作为一个单独的整数时最优. 2.n为1时,len==0:n为素数时,len==1. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #includ

Minimum Sum LCM(uva 10791)

题意(就是因为读错题意而wa了一次):给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 例如12,是1和12的最小公倍数,是3和4的最小公倍数,是1,2,3,4,6,12的最小公倍数,是12和12的最小公倍数……………… 那么找出一个序列,使他们的和最小,上面的例子中,他们的和分别为13,7,28,24……显然最小和为7 /* 我们很容易可以发现,将n唯一分解之后,把所有质因数乘以次数加起来就行了.比如:12=2^2*3^1,那么ans=2^2

uva 568(数学)

题解:从1開始乘到n,由于结果仅仅要最后一位.所以每乘完一次,仅仅要保留后5位(少了值会不准确,刚開始仅仅保留了一位.结果到15就错了,保留多了int会溢出,比方3125就会出错) 和下一个数相乘,接着保留5位,注意5位没有后导零,最后取5位中最后一个不是零的就能够了. #include <iostream> #include <cstdio> using namespace std; int main() { int n; long int temp; while (scanf(