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. CNN 手写数字识别

    1. 知识点准备 在了解 CNN 网络神经之前有两个概念要理解,第一是二维图像上卷积的概念,第二是 pooling 的概念. a. 卷积 关于卷积的概念和细节可以参考这里,卷积运算有两个非常重要特性, ...

  2. AngularJS - 服务简介

    服务是AngularJS中非常重要的一个概念,虽然我们有了控制器,但考虑到其生命实在脆弱,我们需要用到服务. 起初用service时,我便把service和factory()理所当然地关联起来了. 确 ...

  3. OC基础--成员变量的封装

    一.封装的作用: 1.重用 2.不必关心具体的实现 3.面向对象三大特征之一 4.具有安全性 二.OC中成员变量的命名规范以及注意事项 1.命名规范--.成员变量都以下划线“_”开头 1)为了跟get ...

  4. Freemarker-标签使用

     FreeMarker标签使用  FreeMarker模板文件主要有4个部分组成 1.文本,直接输出的部分 2.注释,即<#--...-->格式不会输出 3.插值(Interpolatio ...

  5. Java-httpClient警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

    使用HttpClient,总是报出“Going to buffer response body of large or unknown size. Using getResponseBodyAsStr ...

  6. 【UVA 11401】Triangle Counting

    题 题意 求1到n长度的n根棍子(3≤n≤1000000)能组成多少不同三角形. 分析 我看大家的递推公式都是 a[i]=a[i-1]+ ((i-1)*(i-2)/2-(i-1)/2)/2; 以i 为 ...

  7. GCC、Makefile编程学习

    相关学习资料 http://gcc.gnu.org/ https://gcc.gnu.org/onlinedocs/ http://zh.wikipedia.org/zh/GCC http://blo ...

  8. 取出list的数组元素

    java中将list中的一维数组中的元素取出需要2步.第一步:获取list的迭代器,将数组从迭代器中遍历取出:第二部:对取出的数组进行遍历,取出数组中存储的元素.java的list集合中只能存储引用型 ...

  9. easyUI框架之学习3--表格datagrid

    @model MVCEasyUI.Models.Sale.PageResult<MVCEasyUI.Models.Sale.Order> @{ ViewBag.Title = " ...

  10. ThinkPad紧凑型蓝牙键盘(0B47189)鼠标滚轮用法,F1到F12功能键的功能切换以及其他技巧

    入手小红点蓝牙键盘(ThinkPad Compact Bluetooth),手感极佳,小红点特别适合程序员工作,双手无需离开键盘就可以操作鼠标,完全解决肩部.腕部疲劳酸痛问题,程序员健康的大福音! 使 ...