题意:
给出n,m,每次操作可以选择n的一个因子乘以n,问最少多少次可以让n等于m,不能做到输出-1。
思路:
值得注意的是m的数据范围需要ull,这题用ll好像也可以照常运算。
先将n分解成素数乘积,得到n的素数分解之后用n的质因子除m,得到m中这个质因子的次数y,假设n中这个质因子的次数是x,那么y应该满足y>=x,否则无解。
得到之后计算x最少翻几翻能大于等于y,更新答案最大值。
代码:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
#define pb push_back
#define X first
#define Y second
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pii pair<int,int>
#define qclear(a) while(!a.empty())a.pop();
#define lowbit(x) (x&-x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define mst(a,b) memset(a,b,sizeof(a))
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout1(x) cout<<x<<endl
#define IOS std::ios::sync_with_stdio(false)
#define SRAND srand((unsigned int)(time(0)))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
using namespace std;
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;
const ll INFF=0x3f3f3f3f3f3f3f3f;
const ull mod=212370440130137957;
const double eps=1e-5;
const int maxn=1000005;
const int maxm=20005;
const int base=31;
int t;
ull n,m;
bool vis[maxn];
vector<int>prime;
void solve() {
mst(vis,0);
int lim=1000000;
for(int i=2;i<=lim;i++){
for(int j=i*2;j<=lim;j+=i){
vis[j]=1;
}
}
prime.clear();
for(int i=2;i<=lim;i++){
if(!vis[i]){
prime.pb(i);
}
}
cin>>t;
while(t--){
cin>>n>>m;
bool ok=1;
int ans=0;
for(int i=0;i<prime.size()&&prime[i]<=n;i++){
if(n%prime[i])continue;
int a=0,b=0;
while(n%prime[i]==0){
n/=prime[i];
a++;
}
while(m%prime[i]==0){
m/=prime[i];
b++;
}
if(a>b){
ok=0;
break;
}
int nowans=0;
while(a<b){
nowans++;
a*=2;
}
ans=max(nowans,ans);
}
if(n!=1){
if(m%n){
ok=0;
}else{
int a,b;
a=1;b=0;
while(m%n==0){
m/=n;
b++;
}
int nowans=0;
while(a<b){
nowans++;
a*=2;
}
ans=max(ans,nowans);
}
}
if(m!=1){
ok=0;
}
if(ok){
printf("%d\n",ans);
}else{
printf("-1\n");
}
}
return ;
}
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#else
// freopen("","r",stdin);
// freopen("","w",stdout);
#endif
solve();
return 0;
}