Codeforces 371B Fox Dividing Cheese(简单数论)

题目链接 Fox Dividing Cheese

思路:求出两个数a和b的最大公约数g,然后求出a/g,b/g,分别记为c和d。

   然后考虑c和d,若c或d中存在不为2,3,5的质因子,则直接输出-1(根据题目要求)

           计算出c = (2 ^ a2) * (3 ^ a3) * (5 ^ a5)      d = (2 ^ b2) * (3 ^ b3) * (5 ^ b5)

   那么答案就是a2 + a3 + a5 + b2 + b3 + b5

#include <bits/stdc++.h>

using namespace std;

int a, b;
int n, m, x;
int a2, a3, a5, b2, b3, b5;

int gcd(int a, int b){
    return b == 0 ? a : gcd(b, a % b);
}

int main(){

    scanf("%d%d", &n, &m);
    if (n == m){puts("0"); return 0;}
    x = gcd(n, m);

    a = n / x, b = m / x;
    while (a % 2 == 0) { a /= 2, ++a2;}
    while (a % 3 == 0) { a /= 3, ++a3;}
    while (a % 5 == 0) { a /= 5, ++a5;}

    while (b % 2 == 0) { b /= 2, ++b2;}
    while (b % 3 == 0) { b /= 3, ++b3;}
    while (b % 5 == 0) { b /= 5, ++b5;}

    if (a > 1 || b > 1){
        puts("-1");
        return 0;
    }
    
    printf("%d\n", a2 + a3 + a5 + b2 + b3 + b5);
    return 0;

}

 

转载于:https://www.cnblogs.com/cxhscst2/p/6359056.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值