UVA10780 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
2
2 10
2 100
Sample Output
Case 1:
8
Case 2:
97
问题链接:UVA10780 Again Prime? No Time.
问题简述:(略)
问题分析:
给定m,n,求最大的k使得m^k是n!的因子。
用筛选法进行素数打表,然后暴力一下。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA10780 Again Prime? No Time. */
#include <bits/stdc++.h>
using namespace std;
const int SUP = 0x7FFFFFFF;
const int N = 10000;
const int SQRTN = sqrt((double) N);
bool prime[N + 1];
int primek[N / 7], pcnt;
// Eratosthenes筛选法
void esieve(void)
{
memset(prime, true, sizeof(prime));
pcnt = 0;
prime[0] = prime[1] = false;
for(int i = 2; i <= SQRTN; i++) {
if(prime[i]) {
primek[pcnt++] = i;
for(int j = i * i; j <= N; j += i) //筛选
prime[j] = false;
}
}
for(int i = SQRTN + 1; i <= N; i++)
if(prime[i])
primek[pcnt++] = i;
}
int main()
{
esieve();
int t, caseno = 0, n, m;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &m, &n);
int mink = SUP;
for (int i = 0; m > 1 && i < pcnt; i++) {
int cnt = 0;
while (m % primek[i] == 0) {
m /= primek[i];
cnt++;
}
if (cnt) {
int now = n, sum = 0;
while (now) {
now /= primek[i];
sum += now;
}
sum /= cnt;
mink = min(mink, sum);
}
}
printf("Case %d:\n", ++caseno);
if (mink && mink != SUP)
printf("%d\n", mink);
else
printf("Impossible to divide\n");
}
return 0;
}
浙公网安备 33010602011771号