Minimum Sum LCM(uva10791+和最小的LCM+推理)

L - Minimum Sum LCM

Time
Limit:
3000MS     Memory Limit:0KB     64bit
IO Format:
%lld & %llu

Submit Status Practice UVA
10791

题意:输入正整数n,<注意n=2^31-1是素数,结果是2^31已经超int,用long long,>找至少两个数,使得他们的LCM为n且要输出最小的和;

思路:既然LCM是n,那么一定是n的质因子组成的数,又要使和最小,那么就是ans+=[质因子]^[个数]+...;

之前我一直超时,感觉都无语了。

转载请注明出处:寻找&星空の孩子

题目链接:UVA
10791

也欢迎来我开的专题刷题。哈哈http://acm.hust.edu.cn/vjudge/contest/view.action?cid=77956#overview

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define LL long long

LL n,sum;

inline LL divisor(LL x)
{
    int t=0,cnt;
    LL tp;
    for(int i=2; i<=sqrt(n); i++)
    {
        cnt=0;
        tp=1;
        if(x%i==0&&i!=n)
        {
            while(x)
            {
                if(x%i==0)
                {
                    cnt++;
                    x=x/i;
                    tp=tp*i;
                }
                else {sum+=tp;break;}
            }
            t++;
        }
        if(!x) break;
    }
    if(x>1){sum+=x;t++;}
 //   printf("sum=%lld\n",sum);
    return t;
}

int main()
{

    int ca=1;
    while(scanf("%lld",&n),n)
    {
        sum=0;
        LL m=divisor(n);
        if(sum==0||m==1)sum=n+1;
        printf("Case %d: %lld\n",ca++,sum);
    }
    return 0;
}

超时代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long

LL n,sum;

inline LL divisor(LL x)
{
    int t=0,cnt;
    LL tp;
    for(int i=2; i<=x; i++)//这么写,就超时 了。。。。。
    {
        cnt=0;
        tp=1;
        if(x%i==0&&i!=n)
        {
            while(x)
            {
                if(x%i==0)
                {
                    cnt++;
                    x=x/i;
                    tp=tp*i;
                }
                else {sum+=tp;break;}
            }
            t++;
        }
        if(!x) break;
    }
    return t;
}

int main()
{

    int ca=1;
    while(scanf("%lld",&n),n)
    {
        sum=0;
        LL m=divisor(n);
        if(sum==0||m==1)sum=n+1;
        printf("Case %d: %lld\n",ca++,sum);
    }
    return 0;
}
时间: 2024-12-18 13:48:11

Minimum Sum LCM(uva10791+和最小的LCM+推理)的相关文章

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 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

Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】

Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 623  Accepted: 178  Special Judge: No Description There are n numbers A[1] , A[2] .... A[n], you can select m numbers of it A[B[1]] , A[B[2]] ... A[B[m]]  ( 1 <= B[

Minimum Sum(思维)

Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB    Total Submit: 563  Accepted: 156  Special Judge: No Description There are n numbers A[1] , A[2] .... A[n], you can select m numbers of it A[B[1]] , A[B[2]] ... A[B[m]]  ( 1 <= B

hdu 3473 Minimum Sum 再来一波划分树,对划分树累觉不爱。

Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3084    Accepted Submission(s): 710 Problem Description You are given N positive integers, denoted as x0, x1 ... xN-1. Then give you

HDU 3473 Minimum Sum

Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2667    Accepted Submission(s): 609 Problem Description You are given N positive integers, denoted as x0, x1 ... xN-1. Then give you

[email&#160;protected] Minimum sum partition (Dynamic Programming)

http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum. Input: The first line contains an i

hdu 3473 Minimum Sum(划分树-sum操作)

划分树.只是考虑求当前区间大于第k值的值得和,和小于第k值的和.显然可以在查询的时候直接搞出来.sum[d][i]表示第d层子区间l,r种l-i的和.写错了一个下标,检查了半辈子... #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue&g