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. 第九章:Javascript类和模块

    (过年了,祝大家新年好!) 第6章详细介绍了javascript对象,每个javascript对象都是一个属性集合,相互之间没有任何联系.在javascript中也可以定义对象的类,让每个对象都共享某 ...

  2. WRONGTYPE Operation against a key holding the wrong kind of value

    今天改动代码,一运行就跑错了,错误原因: 因为redis中已经存在了相同的key, 而且key对应的值类型并不是Set,而是SortSet(改动前):再调用smembers时,抛出此错误. 解决方法: ...

  3. logic标签用法

    logic  <logic:iterate> <% Dog dog1=new Dog(); dog1.setAge(2); dog1.setName("xiaoming& ...

  4. 【poj3177】 Redundant Paths

    http://poj.org/problem?id=3177 (题目链接) 题意 给出一个n个节点m条边的无向图,求最少连几条边使图中没有桥. Solution 我们可以发现,用最少的边使得图中没有桥 ...

  5. UOJ35 后缀数组(模板)

    #35. 后缀排序 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的位置.位置编号为 1 ...

  6. C/C++ 跨平台交叉编译、静态库/动态库编译、MinGW、Cygwin、CodeBlocks使用原理及链接参数选项

    目录 . 引言 . 交叉编译 . Cygwin简介 . 静态库编译及使用 . 动态库编译及使用 . MinGW简介 . CodeBlocks简介 0. 引言 UNIX是一个注册商标,是要满足一大堆条件 ...

  7. Syntax error, annotations are only available if source level is 1.5

    在项目上右键 -> Properties -> Java Compiler

  8. Exceptionless 本地部署

    免费开源分布式系统日志收集框架 Exceptionless 前两天看到了这篇文章,亲身体会了下,确实不错,按照官方的文档试了试本地部署,折腾一番后终于成功,记下心得在此,不敢独享. 本地部署官方wik ...

  9. WEB开发中的页面跳转方法总结

    PHP header()函数跳转 PHP的header()函数非常强大,其中在页面url跳转方面也调用简单,使用header()直接跳转到指定url页面,这时页面跳转是302重定向: $url = & ...

  10. Jquery+Ajax+Json的使用(微信答题实例)

    —————————————————————TP框架下的方法————————————————————————