UVA10780 - Again Prime? No Time.(分解质因子)

题目链接

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

思路:m^k等于m的所有质因子的k次方的和,所以只要找到m中的质因子在n!中所能得到的最小的次方,就是k的值。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;

int n, m;

int main() {
    int cas;
    int t = 1;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d%d", &m, &n);
        printf("Case %d:\n", t++);
        int k = 2;
        int ans = INF;
        while (m != 1) {
            int cnt = 0;
            while (m % k == 0) {
                m /= k;
                cnt++;
            }
            if (cnt) {
                int a = 0;
                for (int i = 0; i <= n; i += k) {
                    if (i % k == 0 && i != 0) {
                        int temp = i;
                        while (temp % k == 0) {
                            a++;
                            temp /= k;
                        }
                    }
                }
                a = a / cnt;
                ans = min(ans, a);
            }
            k++;
        }
        if (ans)
            printf("%d\n", ans);
        else
            printf("Impossible to divide\n");
    } 

    return 0;
}

时间: 2024-07-31 21:52:59

UVA10780 - Again Prime? No Time.(分解质因子)的相关文章

分解质因子(个人模版)

分解质因子: 1 memset(prime,0,sizeof(prime)); 2 memset(num,0,sizeof(num)); 3 for(int i=2;i<=5000005;i++) 4 { 5 if(prime[i]==0) 6 { 7 for(int j=i;j<=5000005;j+=i) 8 { 9 int temp=j; 10 while(temp%i==0) 11 { 12 num[j]++; 13 temp/=i; 14 } 15 prime[i]=1; 16 }

poj3993Not So Flat After All(筛法素数+分解质因子)

题目链接: 啊哈哈,点我点我 题意: 题意是给出两个数字,然后有由一分解定理得,每个数可以分解成若干质因数的乘积,这样就可以在一个n维的坐标系下表示出这个点...比如给出50和24 因为24=2^3*3^1*5^0  而50=2^1*3^0*5^2那么这两个点就可以在一个3维德坐标系下表示出这两个点..24=(3,1,0)  50=(1,0,2)  那么共同拥有的维度就是3  而两个点在n维坐标系下的距离就是|3-1|+|1-0|+|0-2|=5,这样题意完全理解... 思路: 先筛出10000

HDU 3641 Treasure Hunting (二分+分解质因子)

题目链接:HDU 3641 Treasure Hunting 题意:求X!%M==0中最小的的X.其中M=a1^b1*a2^b2*a3^b3.... 思路:求余为0想到整除,即分母的因子构成的集合是分子的因子构成的集合的子集.因子又想到,任何一个整数都可以分解成若干个素数相乘. 注意:题目数据很大,查到答案时二分. AC代码: #include<stdio.h> #include<string.h> #define ll __int64 bool Prime[210]; ll nu

UVA10892 - LCM Cardinality(分解质因子)

题目链接 题意:输入正整数n,统计有多少对正整数a <= b,满足lcm(a, b) = n. 思路:分解质因子,然后直接暴力求出对数 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; const int MA

HDU 4497 GCD and LCM(分解质因子+排列组合)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满足要求的(x, y, z)有多少组,并且要考虑顺序. 思路:如果L%G != 0显然不存在这样的(x, y, z),相反肯定存在.具体做法就是将L/G分解质因子,得到:L/G = P1^t1 * P2^t2 * ... * Pk^tk,我们来考虑任意一个因子Pi^ti,此时(x/G, y/G, z/

POJ 1730 Perfect Pth Powers (枚举||分解质因子)

Perfect Pth Powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16638   Accepted: 3771 Description We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More g

分解质因子问题

题目大意:有t(1<=t<=104)个数arr[1],arr[2]-.arr[t],设每个数是n(2<=n<=109),任务是将这个n的质因子分解出来,包括重复的质因子,时限是1000MS..比如n=18,而18=2*3*3,所以输出的结果就是2 3 3. n的范围是[2,109],很容易想到n的质因子的范围是[2,sqrt(n)],所以可以预处理出[2,sqrt(109)]的所有素数(注:sqrt()就是求解一个数的平方根),这个范围内的素数大概只有3千多个,拿这个n跟所有的素数

BNU 13259.Story of Tomisu Ghost 分解质因子

Story of Tomisu Ghost It is now 2150 AD and problem-setters are having a horrified time as the ghost of a problem-setter from the past, Mr. Tomisu, is frequently disturbing them. As always is the case in most common ghost stories, Mr. Tomisu has an u

HDU 4135 Co-prime (容斥+分解质因子)

<题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. 解题分析: 将求区间[A,B]与N互质的数转化成求[1,B] 区间与N互质的个数  -  [1,A-1]中与N互质的个数.同时,因为直接求区间内与N互质的数不好求,我们从反面入手,求出与N不互质的数,借鉴埃筛的思想,我们先求出N的所有质因子,然后将这些质因子在区间内倍数的个数全部求出(即与N不互质的数),再用