Problem Description

You are given two numbers NNN and MMM.

Every step you can get a new NNN in the way that multiply NNN by a factor of NNN.

Work out how many steps can NNN be equal to MMM at least.

If N can't be to M forever,print −1-1−1.

Input

In the first line there is a number TTT.TTT is the test number.

In the next TTT lines there are two numbers NNN and MMM.

T≤1000T\leq1000T≤1000, 1≤N≤10000001\leq N \leq 10000001≤N≤1000000,1≤M≤2631 \leq M \leq 2^{63}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 23333这道题也是够了,按题意拍也是能A的,但是一开始的想法一直WA,感觉好坑取 循环取n,m的最大公约数t 然后n*n m/t更新,后来想有可能是n*n超了Long Long
AC代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef unsigned long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
unsigned int n,tt,cnt,k;
ll m;
int a[MAXN];
int prime[MAXN+];
void getPrime()
{
memset(prime,,sizeof(prime));
for(int i = ;i <= MAXN;i++)
{
if(!prime[i])prime[++prime[]] = i;
for(int j = ;j <= prime[] && prime[j] <= MAXN/i;j++)
{
prime[prime[j]*i] = ;
if(i % prime[j] == )break;
}
}
}
long long factor[][];
int fatCnt;
int getFactors(long long x)
{
fatCnt = ;
long long tmp = x;
for(int i = ; prime[i] <= tmp/prime[i];i++)
{
factor[fatCnt][] = ;
if(tmp % prime[i] == )
{
factor[fatCnt][] = prime[i];
while(tmp % prime[i] == )
{
factor[fatCnt][] ++;
tmp /= prime[i];
}
fatCnt++;
}
}
if(tmp != )
{
factor[fatCnt][] = tmp;
factor[fatCnt++][] = ;
}
return fatCnt;
} long long factor2[][];
int fatCnt2;
int getFactors2(long long x)
{
fatCnt2 = ;
long long tmp = x;
for(int i = ; prime[i] <= tmp/prime[i];i++)
{
factor2[fatCnt2][] = ;
if(tmp % prime[i] == )
{
factor2[fatCnt2][] = prime[i];
while(tmp % prime[i] == )
{
factor2[fatCnt2][] ++;
tmp /= prime[i];
}
fatCnt2++;
}
}
if(tmp != )
{
factor2[fatCnt2][] = tmp;
factor2[fatCnt2++][] = ;
}
return fatCnt2;
}
int main()
{
int i;
getPrime();
scanf("%d",&tt);
while(tt--)
{
scanf("%d %I64d",&n,&m);
getFactors(n);
bool flag=;
if(m<n)
{
printf("-1\n");
continue;
}
for(i=;i<fatCnt;i++)
{
int tot=;
while(m%factor[i][]==)
{
m/=factor[i][];
factor2[i][]=factor[i][];
factor2[i][]=tot++;
}
}
if(m!=)
{
flag=;
}
int Max=;
for(i=;i<fatCnt;i++)
{
int temp=factor[i][];
int tot=;
while(temp<factor2[i][])
{
temp<<=;
tot++;
}
Max=max(Max,tot);
}
if(!flag)
{
printf("-1\n");
}
else
{
printf("%d\n",Max);
}
}
}

AC

WA
#include<stdio.h>
#include<iostream>
using namespace std;
long long t,n,m;
int f,i,j;
int gcd(long long d,long long e)
{
if(e==) return d;
return gcd(e,d%e);
}
int main()
{
freopen("stdin.txt","r",stdin);
scanf("%d",&f);
for(i=;i<=f;i++)
{
scanf("%I64d %I64d",&n,&m);
if(m%n!=){cout<<"-1\n";continue;}
if(m==n){cout<<"0\n";continue;}
if(n==){cout<<"-1\n";continue;}
else
{
m/=n;
n*=n;
j=;
while()
{
if(m==){cout<<j<<endl;break;}
t=gcd(n,m);cout<<t<<"* ";
if(m!=&&t==){cout<<"-1\n";break;}
n*=n;
m/=t;
j++;
}
}
j=n=m=;
t=;
}
return ;
}

WA

BestCoder Round #60 1002的更多相关文章

  1. 暴力+降复杂度 BestCoder Round #39 1002 Mutiple

    题目传送门 /* 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 若没有,默认加0,nlogn复杂度: 我用暴力竟然水过去了:) */ #include <cst ...

  2. 矩阵快速幂---BestCoder Round#8 1002

    当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n)   f(n-1)] = [f(n ...

  3. 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

    题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...

  4. Manacher BestCoder Round #49 ($) 1002 Three Palindromes

    题目传送门 /* Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间 ...

  5. 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil

    题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...

  6. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  7. HDU 5505 - BestCoder Round #60 - GT and numbers

    题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1002 思路 : N有若 ...

  8. BestCoder Round #56 1002 Clarke and problem 1003 Clarke and puzzle (dp,二维bit或线段树)

    今天第二次做BC,不习惯hdu的oj,CE过2次... 1002 Clarke and problem 和Codeforces Round #319 (Div. 2) B Modulo Sum思路差不 ...

  9. BestCoder Round #92 1002 Count the Sheep —— 枚举+技巧

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1002 题解: 做题的时候只是想到 ...

随机推荐

  1. 15.C#回顾及匿名类型(八章8.1-8.5)

    今天的篇幅应该会很长,除了回顾前面学的一些,还有写一些关于匿名类型的相关知识,总体上对后续的学习很有帮助,学好了,后面更容易理解,不明白的,那就前面多翻几次,看多了总是会理解的.那么,进入正题吧. 自 ...

  2. 07.C#泛型的限制和可空类型的简单说明(三章3.5-四章4.1)

    自己在写文章的同时,也是在学习,对于书中的语句很多其实没有太好的理解,读一本书,要消化!!!三章都是讲泛型的,最后写一下泛型的限制,对于本章学习的完结,one end,one begin. 看下面的代 ...

  3. Android EditText使用详解

    一:新建HelloEditText工程 新建一个Hello world详细步骤可以参见 Android教程之三:第一个Android应用,HelloWorld 创建设置如下: Project name ...

  4. linux 查看服务器性能常用命令

    一.top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器   下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来 ...

  5. oracle练习题后15个

    31,32题更正: SQL> --31. 查询所有教师和同学的name.sex和birthday. SQL> select sname, ssex, sbirthday from stud ...

  6. shell中一维数组值得获取

    (1)数组的定义 root@tcx4440-03:~# a=(1 2 3 4) root@tcx4440-03:~# echo ${a[1]}2 root@tcx4440-03:~# a[0]=1ro ...

  7. spring 第一篇(1-1):让java开发变得更简单(下)

    切面(aspects)应用 DI能够让你的软件组件间保持松耦合,而面向切面编程(AOP)能够让你捕获到在整个应用中可重用的组件功能.在软件系统中,AOP通常被定义为提升关注点分离的一个技术.系统由很多 ...

  8. Treap实现山寨set

    treap插入.删除.查询时间复杂度均为O(logn) treap树中每个节点有两种权值:键值和该节点优先值 如果只看优先值,这棵树又是一个堆 treap有两种平衡方法:左旋&右旋 inser ...

  9. 由chrome剪贴板问题研究到了js模拟鼠标键盘事件

    写在前面 最近公司在搞浏览器兼容的事情,所有浏览器兼容的问题不得不一个人包了.下面来说一下今天遇到的一个问题吧 大家都知道IE下面如果要获得剪贴板里面的信息的话,代码应该如下所示 window.cli ...

  10. 类,抽象基类,接口类三者间的区别与联系(C++)

    结构上的区别: 普通类:数据+方法+实现 抽象类:数据+方法(一定包含虚方法n>=1)+部分方法的实现 接口类:方法(纯虚方法) http://www.cnblogs.com/Tris-wu/p ...