• 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
  • 相关阅读:
    Laravel schema构建器列类型
    wkhtmltopdf docker + java(环境搭建及一些坑)
    dockerfile,仓库,私有仓库流程(转载)
    wkhtmltopdf参数详解及精讲使用方法(转载)
    传统前端项目中进行模块化编程并引入使用vue、elementui 前端
    vue3 + vuex4 实践 前端
    elementplus 原生开发 日期国际化语言 前端
    Vite2.0打包elementplus UI报错 前端
    vue3 Vetur报错:has no default export 组件没导出 前端
    windows版的HbuilderX连接iPad真机测试(uniapp)
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5838435.html
Copyright © 2020-2023  润新知