zoukankan      html  css  js  c++  java
  • UVa10780

    10780 Again Prime? No time.
    The problem statement is very easy. Given a number n you have to determine the largest power of m,
    not necessarily prime, that divides n!.
    Input
    The input file consists of several test cases. The first line in the file is the number of cases to handle.
    The following lines are the cases each of which contains two integers m (1 < m < 5000) and n
    (0 < n < 10000). The integers are separated by an space. There will be no invalid cases given
    and there are not more that 500 test cases.
    Output
    For each case in the input, print the case number and result in separate lines. The result is either an
    integer if m divides n! or a line ‘Impossible to divide’ (without the quotes). Check the sample input
    and output format.
    Sample Input
    22
    10
    2 100
    Sample Output
    Case 1:
    8
    Case 2:
    97

    题意:
           输入两正整数m、n(1<m<5000、0<n<10000)求最大的正整数k使得m^k是n!的约数。如果无解则输出“Impossible to divide”。

    分析:

           枚举m的每个素因子i的幂次p以及n!这一素因子(i)的幂次p2,计算它们的比值res =(int)(p2/p),每枚举一个素因子就更新一次res值。如果res值最终大于零,则那个值就是答案,否则输出“无解”。计算某个素因子i的幂次时,对于一个数m,只需要不断除以i直到除尽i为止;而对于一个正整数n!,则只需要根据公式:n/i + n/(i^2) + n/(i^3) + …计算即可。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cstdio>
     5 using namespace std;
     6 const int inf = 0x3f3f3f3f;
     7 int main(){
     8     int t,n,m,cas = 1;
     9     scanf("%d",&t);
    10     while(t--){
    11         scanf("%d%d",&m,&n);
    12         int i = 2,ans = inf;
    13         while(m != 1){
    14             int p = 0;
    15             while(m % i == 0) m /= i,p++;
    16             if(p){
    17                 int num = n,p2 = 0;
    18                 while(num) p2 += num / i,num /= i;
    19                 ans = min(ans,p2 / p);
    20             }
    21             i++;
    22         }
    23         printf("Case %d:
    ",cas++);
    24         if(ans) printf("%d
    ",ans);
    25         else printf("Impossible to divide
    ");
    26     }
    27     return 0;
    28 }
    View Code
  • 相关阅读:
    linq获取最大ID值
    asp:MultiView选项卡控件,可以用来选择性的显示需要的部门
    TFS修改工作区映射区
    怎么解决javascript小数相减会出现一长串的小数位数?
    (转)向页面动态载入用户控件和自定义控件的方法(谨记)
    (转)工作经验到底是个什么东东?工作经验从哪里来?
    hdu 5441 travel 离线+带权并查集
    hdu 5438 Ponds dfs
    hdu 5437 Alisha’s Party 模拟 优先队列
    CF 500 B. New Year Permutation 并查集
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5838435.html
Copyright © 2011-2022 走看看