B. Fox Dividing Cheese----GCD/因子问题

解析Codeforces B.FoxDividingCheese问题,通过计算两块奶酪重量的公约数来确定最少操作次数,使重量相等。涉及算法包括求最大公约数(GCD)及迭代去除可除因子。

B. Fox Dividing Cheese
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Two little greedy bears have found two pieces of cheese in the forest of weight a and b grams, correspondingly. The bears are so greedy that they are ready to fight for the larger piece. That's where the fox comes in and starts the dialog: "Little bears, wait a little, I want to make your pieces equal" "Come off it fox, how are you going to do that?", the curious bears asked. "It's easy", said the fox. "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths. I'll eat a little here and there and make the pieces equal".

The little bears realize that the fox's proposal contains a catch. But at the same time they realize that they can not make the two pieces equal themselves. So they agreed to her proposal, but on one condition: the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal.

Input

The first line contains two space-separated integers a and b (1 ≤ a, b ≤ 109).

Output

If the fox is lying to the little bears and it is impossible to make the pieces equal, print -1. Otherwise, print the required minimum number of operations. If the pieces of the cheese are initially equal, the required number is 0.

Examples
input
15 20
output
3
input
14 8
output
-1
input
6 6
output
0

题目链接:http://codeforces.com/contest/371/problem/B


题目的意思是给你两个数a,b,这两个数可以除以1/2,1/3,1/5,问最少变换多少次才能使两个数相等。

我猜的和gcd有关,没想到还真的有关系。

我们来看第一组示例,15=3*5,20=2*2*5,去掉公因数5,还剩下3个数,输出3即可,不能被2,3,5整除的输出-1.

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int c[4],d[4];
int main(){
    int a,b;
    scanf("%d%d",&a,&b);
    if(a==b){
        printf("0\n");
        return 0;
    }
    int gcd=__gcd(a,b);
    a/=gcd;
    b/=gcd;
    while(a%2==0){
        a/=2;
        c[0]++;
    }
    while(a%3==0){
        a/=3;
        c[1]++;
    }
    while(a%5==0){
        a/=5;
        c[2]++;
    }
    while(b%2==0){
        b/=2;
        d[0]++;
    }
    while(b%3==0){
        b/=3;
        d[1]++;
    }
    while(b%5==0){
        b/=5;
        d[2]++;
    }
    if(a!=b){
        printf("-1\n");
        return 0;
    }
    //for(int i=0;i<3;i++){
     //   printf("%d %d\n",c[i],d[i]);
    //}
    int ans=0;
    for(int i=0;i<3;i++){
        ans+=c[i];
        ans+=d[i];
    }
    printf("%d\n",ans);
}

所谓神题必有神解,回滚做法 http://blog.csdn.net/synapse7/article/details/21085153

内容概要:本文系统研究了构网型变流器的正负序阻抗解耦特性及其在弱电网环境下的稳定性表现,重点依托Matlab/Simulink仿真平台,构建了详细的阻抗数学模型,设计了解耦控制策略,并采用小信号扫频法进行频域辨识与稳定性验证。研究深入探讨了构网型变流器与传统跟网型逆变器在正负序阻抗特性上的本质差异,结合虚拟同步发电机(VSG)等先进控制技术,分析其在抑制宽频带振荡、削弱锁相环动态耦合等方面的优越性。文中不仅提供了完整的仿真模型与MATLAB代码实现,还整合了光伏、风电、储能、微电网等多类新能源系统的阻抗建模与稳定性分析资源,形成了一套面向新型电力系统稳定性的综合性技术资料体系,具有较强的科研复现与工程参考价值。; 适合人群:面向具备电力电子、电力系统自动化、新能源并网等专业背景的研究生、高校教师及工程技术人员,特别适用于从事阻抗建模、小干扰稳定性分析、宽频振荡机理研究以及撰写高水平学术论文的科研工作者。; 使用场景及目标:①掌握构网型变流器正负序阻抗建模与扫频辨识的仿真方法;②深入理解VSG等构网型控制在弱电网中提升稳定性的内在机理;③复现顶刊论文中的阻抗分析流程与稳定性判据应用;④利用提供的成熟模型与代码加速科研进程,支撑课题研究与学术成果产出。; 阅读建议:建议结合文中提供的Simulink模型与MATLAB代码,按照“理论建模—仿真搭建—扫频激励—频响提取—Nyquist判据分析”的完整流程进行实践操作,重点关注扫频信号的注入方式、频率范围设置及阻抗曲线的物理意义解读,并参考博士论文复现案例深化对复杂动态耦合问题的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值