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
  • 相关阅读:
    浅谈IO这件事
    tushare+pandas实现财经数据分析
    大话设计模式Python实现-解释器模式
    大话设计模式Python实现- 享元模式
    大话设计模式Python实现-中介者模式
    大话设计模式Python实现-职责链模式
    大话设计模式Python实现-命令模式
    大话设计模式Python实现-桥接模式
    java之AbstractStringBuilder类详解
    java之Class类详解
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5838435.html
Copyright © 2011-2022 走看看