• 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

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <queue>
     7 #include <stack>
     8 #include <vector>
     9 #include <map>
    10 #include <set>
    11 #include <string>
    12 #include <math.h>
    13 using namespace std;
    14 #pragma warning(disable:4996)
    15 typedef long long LL;                   
    16 const int INF = 1<<30;
    17 /*
    18 
    19 */
    20 int main()
    21 {
    22     LL n, i, m, k = 1;
    23     LL ans;
    24     while (scanf("%lld", &n), n)
    25     {
    26         int nn = n;
    27         ans = 0;
    28         m = sqrt(n) + 0.5;
    29         int cnt = 0;
    30         for (i = 2; i <= m; ++i)
    31         {
    32             if (n%i == 0)
    33             {
    34                 int t = 1;
    35                 while (n%i == 0)
    36                 {
    37                     t *= i;
    38                     n /= i;
    39                 }
    40                 ans += t;
    41                 cnt++;
    42             }
    43         }
    44         if (n > 1)
    45         {
    46             ans += n;
    47             cnt++;
    48         }
    49         if (cnt == 1)//这是只有单独一个数的情况
    50             ans += 1;
    51         else if (cnt == 0)//这是n为1的情况
    52             ans += 2;
    53         printf("Case %lld: %lld
    ", k++,ans);
    54     }
    55     return 0;
    56 }
    View Code

    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的多少次幂, 然后最小的那个次幂就是答案

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <queue>
     7 #include <stack>
     8 #include <vector>
     9 #include <map>
    10 #include <set>
    11 #include <string>
    12 #include <math.h>
    13 using namespace std;
    14 #pragma warning(disable:4996)
    15 typedef long long LL;                   
    16 const int INF = 1<<30;
    17 /*
    18 
    19 */
    20 int a[10000 + 10];
    21 int main()
    22 {
    23     int t, n, m;
    24     scanf("%d", &t);
    25     for (int k = 1; k <= t; ++k)
    26     {
    27         int ans = INF;
    28         scanf("%d%d", &m, &n);
    29         int limit = sqrt(m) + 0.5;
    30         for (int i = 2; i <= limit; ++i)
    31         {
    32             if (m%i == 0)
    33             {
    34                 int cnt = 0;
    35                 while (m%i == 0)
    36                 {
    37                     cnt++;
    38                     m /= i;
    39                 }
    40                 int tmp = 0;
    41                 for (int j = 2; j <= n; ++j)
    42                 {
    43                     int x = j;
    44                     while (x % i == 0)
    45                     {
    46                         x /= i;
    47                         tmp++;
    48                     }
    49                 }
    50                 ans = min(ans, tmp/cnt);
    51             }
    52         }
    53         if (m > 1)
    54         {
    55             int tmp = 0;
    56             for (int j = 2; j <= n; ++j)
    57             {
    58                 int x = j;
    59                 while (x % m == 0)
    60                 {
    61                     x /= m;
    62                     tmp++;
    63                 }
    64             }
    65             ans = min(ans, tmp);
    66         }
    67         if (ans == 0 || ans==INF)
    68             printf("Case %d:
    Impossible to divide
    ",k);
    69         else
    70             printf("Case %d:
    %d
    ", k, ans);
    71     }
    72     return 0;
    73 }
    View Code
  • 相关阅读:
    solr源码解读(转)
    solr安装配置
    HTML转义字符
    JAVA:在0-99间产生100个不重复的随机数
    JS中的$符号
    使用Emacs敲出UML,PlantUML快速指南
    operator 安装
    package handler
    shell 条件判断if
    libvirtError: internal error: No more available PCI slots
  • 原文地址:https://www.cnblogs.com/justPassBy/p/4491015.html
Copyright © 2020-2023  润新知