uva10780 - Again Prime? No time

uva10780 - Again Prime? No time

 

 

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 and n (0. 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

2
2 10
2 100

Sample Output

Case 1:
8
Case 2:
97

 

题目大意:给定mn,求一个最大正整数k,使得m^k能够被n!整除,变相问n!中有多少个m;

 

思路:开始想到的是从2-n遍历,把每个数中有多少的m都找出来,后来发现缺陷是m不是质数,例如4*96,就找不出有6,实际上应该有两个!正确的解法是分解质因数,然后找出有多少对质因数即可。当然要找的是对于每个质数的最小值.

code:
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
const int M=0x7fffffff;
const int N=10010;

bool ft[N];
int prime[N],t=0;
void init() //打素数表
{
memset(ft,true,sizeof(ft));
ft[0]=ft[1]=false;
for (int i=2;i<N;i++)
{
if (!ft[i]) continue;
prime[++t]=i;
for (int j=2*i;j<N;j+=i)
ft[j]=false;
}
}
int main()
{
init();
int T;
int m,n;
int i,j;
scanf("%d",&T);
for (int ca=1;ca<=T;ca++)
{
scanf("%d %d",&m,&n);
int ans=M,p=1;
while (m>1)
{
int ct=0;
while (m%prime[p]==0) //分解素数,从前往后找
{
ct++;
m/=prime[p];
}
if (ct==0){p++; continue;}
if (prime[p]>n){ans=-1; break;}
int nt=0;
for (i=prime[p];i<=n;i*=prime[p])
nt+=n/i;
ans=min(ans,nt/ct); //选出最小的素数比,也就是ans个素数对
}
printf("Case %d:\n",ca);
if (ans==-1) cout<<"Impossible to divide"<<endl;
else cout<<ans<<endl;
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值