题目链接:
GT and numbers
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
You are given two numbers N and M.
Every step you can get a new N in the way that multiply N by a factor of N.
Work out how many steps can N be equal to M at least.
If N can't be to M forever,print −1.
Input
In the first line there is a number T.T is the test number.
In the next T lines there are two numbers N and M.
T≤1000, 1≤N≤1000000,1≤M≤263.
Be careful to the range of M.
You'd better print the enter in the last line when you hack others.
You'd better not print space in the last of each line when you hack others.
Output
For each test case,output an answer.
Sample Input
3
1 1
1 2
2 4
Sample Output
-1
1
题意:
问一个数n,每次乘上它的一个因数,问最少多少次能变成m;
思路:
把n,m质因数分解,m如果能由n得来,那么分解后m的质因数与n的质因数相同,且每个质因数的个数不少于n;否则就不能由n得到;
最少的步数当然是对于每个质因数,贪心的去得到m中对应的质因数个数,最后答案就是所有质因数的个数变成m要求的个数的最大的那个;已经智障到不会写scanf了;
AC代码:
//#include <bits/stdc++.h> #include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef unsigned long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e6+;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
}
int prime[N];
struct node
{
int a[],m;
}po[N];
void Init()
{
for(int i=;i<N;i++)
{
po[i].m=;
}
for(int i=;i<N;i++)
{
if(!prime[i])
{
po[i].a[po[i].m++]=i;
for(int j=*i;j<N;j+=i)
{
po[j].a[po[j].m++]=i;
prime[j]=;
}
}
}
}
LL check(int x)
{
LL s=;
for(int i=;i<=x;i++)
{
s*=;
}
return s;
}
int getans(int x,int y)
{
int l=,r=(int)(log(y/x+)/log(2.0))+;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid)*x>=y)r=mid-;
else l=mid+;
}
return r+;
}
void solve(int n,LL m)
{
if(n==)
{
if(m==)printf("0\n");
else printf("-1\n");
}
else
{
int ans=;
int tempn=n;
LL tempm=m;
for(int i=;i<po[n].m;i++)
{
int num1=;
while(tempn%po[n].a[i]==)
{
tempn/=po[n].a[i];
num1++;
}
int num2=;
while(tempm%po[n].a[i]==)
{
tempm/=po[n].a[i];
num2++;
}
if(num2<num1)
{
printf("-1\n");
return ;
}
ans=max(ans,getans(num1,num2));
}
if(tempm>)printf("-1\n");
else printf("%d\n",ans);
}
}
int main()
{
Init();
int T;
scanf("%d",&T);
int n;
LL m;
while(T--)
{
read(n);
scanf("%I64u",&m);
solve(n,m);
}
return ;
}