zoukankan      html  css  js  c++  java
  • UVA 10780 Again Prime No Time.(数学)

    给定两个整数m和n,求最大的k使得m^k是n!的约数

    对m质因子分解,然后使用勒让德定理求得n!包含的质数p的阶数,min(b[i] / a[i])即为结果k, 若为0无解

    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<vector>
    #include<cmath>
    #include<utility>
    using namespace std;
    typedef long long LL;
    const int N = 5008, INF = 0x3F3F3F3F;
    #define MS(a, num) memset(a, num, sizeof(a))
    #define PB(A) push_back(A)
    #define FOR(i, n) for(int i = 0; i < n; i++)
    int p[N], a[N], b[N];
    
    int lp(int n, int p){
    	int ans = 0;
    	for(int i = p; i <= n; i*= p){
    		ans += n / i;
    	}
    	return ans;
    }
    int solve(int m, int n){
    	memset(a, 0, sizeof(a));
    	memset(b, 0, sizeof(b));
    	int tot = 0;
    	for(int i = 2; i * i <= m; i++){
    		if(m % i == 0){
    			p[tot] = i;
    			while(m % i == 0){
    				a[tot]++;
    				m /= i;
    			}
    			tot++;
    		}
    	}
    	if(m > 1){
    		p[tot] = m;
    		a[tot++] = 1;
    	}
    	int ans = INF;
    	for(int i = 0; i < tot; i++){
    		b[i] = lp(n, p[i]);
    		if(b[i] < a[i]){
    			return -1;
    		}
    		ans = min(ans, b[i] / a[i]);
    	}
    	return ans;
    
    }
    int main(){
        int t, m, n;
        cin >>t;
        for(int cas = 1; cas <= t; cas++){
        	scanf("%d %d", &m, &n);
        	int ans = solve(m, n);
        	printf("Case %d:
    ", cas);
        	if(ans == -1){
        		printf("Impossible to divide
    ");
        	}else{
        		printf("%d
    ", ans);
        	}
    
        }
        return 0;
    }
    

      

  • 相关阅读:
    ubuntu18.04 复制或剪切某文件夹下的前x个文件到另一个文件夹下
    VOC2012数据集提取自己需要的类的图片和对应的xml标签
    python面试题
    vi命令使用
    缓冲(Buffer)和缓存(Cache)区别和联系
    cpu相关概念
    pycharm
    jmeter分布式部署
    LeetCode#67 Add Binary
    LeetCode#70 Climbing Stairs
  • 原文地址:https://www.cnblogs.com/IMGavin/p/5977888.html
Copyright © 2011-2022 走看看