GT and numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2772    Accepted Submission(s):
688

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≤2^63 .

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
0 -1 1

题意:n要变到m,每次乘一个n的因子数,求最少乘几次

坑1:m需要用无符号long long定义

坑2:n的因子会变,变多变大
用r表示需要乘的总数,择优,每次选r和n的gcd,这样每次乘得多,次数就少,同时更新r和n
如果遇到r!=1,gcd=1证明需要乘的数 包含 n没有的因子,退出。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
#define ll unsigned long long
const int p=;
const int maxx=1e6+;
ll n,m;///坑1:无符号long long才放得下
int step;
ll gcd(ll a,ll b)
{
if(b==) return a;
return gcd(b,a%b);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cin>>n>>m;
step=;
if(n==m) printf("0\n");
else if(n>m || m%n) printf("-1\n");
else
{
ll r=m/n;///r表示 n还要 乘 一个数 变成m
ll d;
bool flag=true;
while(r!=)
{
d=gcd(r,n);
if(d==)///出现这种情况,r!=1,和n没有共同因子,则表示m中含有n中不含有的质因子
{
flag=false;
break;
}
r=r/d;
n=n*d;///坑2:每次n会变大,因子会更新
step++;
}
if(flag)
cout<<step<<endl;
else printf("-1\n");
}
}
return ;
}

最新文章

  1. js 字符串拼接
  2. Google https服务被屏蔽
  3. 设计模式 - chain of Responsibility
  4. HDU3966-Aragorn&#39;s Story(树链剖分)
  5. Android studio插件安装
  6. show drop down menu within/from action bar
  7. IE兼容性标签和条件注释
  8. MVC-01 概述
  9. 十天学习PHP之第三天
  10. spring是什么,Spring能帮我们做什么
  11. Mac下CUDA开启及Tensorflow-gpu安装
  12. Windows 性能监视器的基本指标说明(CPU,内存,硬盘参数)
  13. JDK动态代理与CGLib动态代理相关问题
  14. SQL SELECT INTO
  15. D3开发中的资料整理
  16. hping网络安全工具的安装及使用
  17. pytorch GPU的程序kill后未释放内存
  18. 【jquery】$(document).ready() 与window.onload的区别
  19. 给zTree添加onSelect callback
  20. (转)MySQL的Grant命令

热门文章

  1. Solr——Windows下部署Solr7.5.0至jetty、Tomcat
  2. Apache服务器下phalcon项目报Mod-Rewrite is not enabled问题
  3. 变量,if.elif .else判断
  4. BZOJ4195 luoguP1955 NOI2015D1T1 程序自动分析
  5. [Unity动画]05.Entry &amp; Exit &amp; Any State
  6. 《算法》第四章部分程序 part 11
  7. Tomcat 之session 持久化1
  8. Python中的字符串方法
  9. 机器学习入门-文本特征-word2vec词向量模型 1.word2vec(进行word2vec映射编码)2.model.wv[&#39;sky&#39;]输出这个词的向量映射 3.model.wv.index2vec(输出经过映射的词名称)
  10. css-图片垂直居中