uva 10780Again Prime? No Time.(简单数论)

本文介绍了一个算法问题,即求解给定整数n的最大因子。通过质因数分解的方法找到最大的质因子,并计算该质因子在n!中的出现次数。文章提供了一段C++代码实现,用于处理多个测试案例。

Again Prime? No time.
Input: standard input
Output: standard output
Time Limit: 1 second


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 mdivides n! or a line "Impossible to divide" (without the quotes). Check the sample input and output format.

 

Sample Input

2
2 10
2 100

Sample Output

Case 1:
8
Case 2:
97

 

题意:给你m,n求最大的k使得是n!的因子;

思路:把m质因子分解为,设p为pi中最大的素数,,然后求n!中p的个数即为为b,即为答案(对于i,求出相应的b,最小的)

代码:

<pre name="code" class="cpp">#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<stdlib.h>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<set>
#define inf 0x3f3f3f3f
#define eps 1e-5
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
using namespace std;
int sum(int n,int p)
{
    int x=p,ans=0;
    while(n/p!=0)
    {
        ans+=n/p;
        p*=x;
    }
    return ans;
}
int main()
{
    int t,n,m,i,j;
    scanf("%d",&t);
    {
        for(i=1;i<=t;i++)
        {
            scanf("%d%d",&m,&n);
            printf("Case %d:\n",i);
            int ans=inf,pnum;
            for(j=2;m>1;j++)
            {
                pnum=0;
                while(m%j==0)
                {
                    pnum++;
                    m/=j;
                }
                if(pnum)
                {
                    int tt=sum(n,j)/pnum;
                    if(ans>tt)ans=tt;
                }
            }
            if(ans)
                printf("%d\n",ans);
            else
                printf("Impossible to divide\n");
        }
    }
    return 0;
}





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值