Codeforces 371BB. Fox Dividing Cheese
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.
The first line contains two space-separated integers a and b (1 ≤ a, b ≤ 109).
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.
15 20
3
14 8
-1
6 6
0
其实这就是一道数学题,题意可以看成“给两个数a,b,让它们除以2,3,5,最后相等,最少除几次”。
那么我们先求出他们的最大公约数c(因为要出的次数尽可能小),再看从原数到C分别用几步,或者能否除到C。
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<queue> #include<math.h> using namespace std; int a,b,ans; int maxyin; void chu(int a,int b) { int aa=a%b; if(aa==0) maxyin=b; else chu(b,aa); } int main() { cin>>a>>b; int c; if(b>a) { c=a; a=b; b=c; } if(a==b)//特判 { cout<<0; return 0; } if(a%b==0)//特潘 { c=a/b; while(c%5==0) c/=5,ans++; while(c%3==0) c/=3,ans++; while(c%2==0) c/=2,ans++; if(c==1) cout<<ans; else cout<<-1; return 0; } chu(a,b); c=a/maxyin;int d =b/maxyin; if(((c%5)&&(c%3)&&(c%2))||((d%5)&&(d%3)&&(d%2))) { cout<<-1; return 0; } ans=0; while(c%5==0) c/=5,ans++; while(c%3==0) c/=3,ans++; while(c%2==0) c/=2,ans++; while(d%5==0) d/=5,ans++; while(d%3==0) d/=3,ans++; while(d%2==0) d/=2,ans++; cout<<ans; return 0; }
来个好看的代码
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<queue> #include<math.h> using namespace std; int a,b; int a2,a3,a5,b2,b3,b5; int x; void gcd(int a,int b) { int aa=a%b; if(aa==0) {x=b;return ;} else gcd(b,aa); } int main() { cin>>a>>b; if(a==b) { cout<<0; return 0; } gcd(a,b); int m=a/x,n=b/x; while(m%3==0) m/=3,a3++; while(m%2==0) m/=2,a2++; while(m%5==0) m/=5,a5++; while(n%3==0) n/=3,b3++; while(n%2==0) n/=2,b2++; while(n%5==0) n/=5,b5++; if(m*n==1) { cout<<(a3+a2+a5+b2+b3+b5); return 0; } cout<<-1; return 0; }
下面是个搜索的
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<queue> #include<math.h> #include<vector> using namespace std; int a,b; struct ab{ int a;int b; int ans; }k; queue<ab>q; int f,ans; void bfs() { ab g,n; g=q.front();q.pop(); while(g.a!=g.b) { f=0; if(g.a>g.b) { n=g; if(g.a%2==0) { n=g; n.ans++; n.a=g.a/2; q.push(n); f=1; } if(g.a %3 ==0) { n=g; n.ans++; n.a=g.a/3; q.push(n); f=1; } if(g.a %5==0) { n=g; n.ans++; n.a=g.a/5; q.push(n); f=1; } }else { if(g.b%2==0) { n=g; n.ans++; n.b=g.b/2; q.push(n); f=1; } if(g.b %3 ==0) { n=g; n.ans++; n.b=g.b/3; q.push(n); f=1; } if(g.b %5==0) { n=g; n.ans++; n.b=g.b/5; q.push(n); f=1; } } g=q.front();ans=g.ans; q.pop(); if(!f) return ; } } int main( ) { cin>>k.a>>k.b; if(k.a==k.b) { cout<<0; return 0; } k.ans=0; q.push(k); bfs(); if(!f){ cout<<-1; return 0; }else cout<<ans; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App