[原创]UVA 10780 Again Prime? No Time. [质因子分解]【数论】

2016-07-02 16:42:00 Tabris_ 阅读数:328


博客爬取于2020-06-14 22:44:16
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/51811570


题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=120197#problem/H


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


题目大意 : 就是N!%m^k=0; 给你n和m 求最大的k;

题解 : 其实很简单 就是把N!与M 质因子分解后
Pi表示第i个质数的贡献是多少
像这样
8 = 2^3 ,Pi=3;

N! ~~ Pn1…Pn2…Pn3…Pn4…Pn5…
M ~~ Pm1…Pm2…Pm3…Pm4…Pm5…

然后找出Pni/Pmi 的最小值 就是我们要求的答案了

附本题代码 170ms


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <limits.h>
# include <malloc.h>
# include <ctype.h>
# include <math.h>
# include <string>
# include <iostream>
# include <algorithm>
# include <map>
# include <vector>
# include <set>

using namespace std;

# define LL long long int
# define _LL __int64

LL prime[2000];
LL Is_or[10101];

void Prime()
{
int n = 10010 ,k = 0;
memset(prime,0,sizeof(prime));
memset(Is_or,1,sizeof(Is_or));

Is_or[0]=Is_or[1]=0;

for(int i=2; i<n; i++)
if(Is_or[i])
{
prime[k++]=i;
for(int j=i+i; j<n; j+=i)
Is_or[j]=0;
}
return ;
}

int z[10010][1300];

void init()
{
memset(z,0,sizeof(z));

int temp,k;

for(int i=1; i<10000; i++)
{
temp=i;
k=0;
while(prime[k]<=temp&&prime[k]!=0)
{
while(temp%prime[k]==0)
{
temp/=prime[k];
z[i][k]++;
}
k++;
}
}

for(int j=0; j<1300; j++)
{
for(int i=2; i<10010; i++)
{
z[i][j]+=z[i-1][j];
}
}

return ;
}

int mm[1300];

int solve(int temp)
{
memset(mm,0,sizeof(mm));

int k=0;
while(prime[k]<=temp&&prime[k]!=0)
{
while(temp%prime[k]==0)
{
temp/=prime[k];
mm[k]++;
}
k++;
}

return k;
}


int main()
{
Prime();
init();

int t,p=0;
scanf("%d",&t);
while(t--)
{
int n,m;

int sum=10000;

scanf("%d%d",&m,&n);

int num=solve(m);

for(int i=0; i<num; i++)

if(mm[i])
sum=min(sum,z[n][i]/mm[i]);

printf("Case %d:\n",++p);

if(sum)
printf("%d\n",sum);
else
printf("Impossible to divide\n");
}
return 0;
}