您的位置:首页 > 大数据 > 人工智能

UVa 10780 - Again Prime? No time (质因式分解)

2014-08-26 08:12 411 查看
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 dividesn!.

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

Problem setter: Anupam Bhattacharjee, CSE, BUET

Thanks to Shabuj for checking and Adrian for alternate solution.


"~~ Algorithms are the rhythms of Computer Science ~~"

题意:
输入两个正整数nm,求最大的正整数k使得m^k是n!的约数

质因数分解
n!可以分解为2^x1 * 3^x2 * 5^x3 * ... * prime[i]^xi
m同理
m^k要为n!的约数,m^2就是m的分解中的每个指数加倍 并且m^k的分解相应的指数不能超过n!的指数

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 10000 + 20;
int Am[maxn], An[maxn];
int prim[maxn], primNum;
int vis[maxn];

int getPrimeTable(int n) {
primNum = 0;
memset(vis, 0, sizeof(vis));
for(int i=2; i<=n; i++) if(!vis[i]) {
for(int j=i; j<=n; j+=i) {
vis[j] = 1;
}
prim[primNum++] = i;
}
return primNum;
}

void get(int * A, int x) {
for(int i=0; i<primNum && x > 1; i++) if(x % prim[i] == 0){
while(x % prim[i] == 0) {
A[i]++;
x /= prim[i];
}
}
}

int main() {
int T;

getPrimeTable(maxn-1);
scanf("%d", &T);
for(int kase=1; kase<=T; kase++) {
int m, n;
scanf("%d%d", &m, &n);
memset(Am, 0, sizeof(Am));
memset(An, 0, sizeof(An));
get(Am, m);
for(int i=2; i<=n; i++) {
get(An, i);
}
int ans = INF;
for(int i=0; i<primNum; i++) {
if(Am[i] > An[i]) {
ans = -1;
break;
} else if(Am[i] > 0) {
ans = min(ans, An[i]/Am[i]);
}
}
printf("Case %d:\n", kase);
if(ans == -1) puts("Impossible to divide");
else printf("%d\n", ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: