BestCoder Round #60/HDU 5505 暴力数学
GT and numbers
给出两个数NN和MM。
NN每次可以乘上一个自己的因数变成新的NN。
求最初的NN到MM至少需要几步。
如果永远也到不了输出-1−1。
第一行读入一个数TT表示数据组数。
接下来TT行,每行两个数NN和MM。
T\leq1000T≤1000, 1\leq N \leq 10000001≤N≤1000000,1 \leq M \leq 2^{63}1≤M≤263. 注意M的范围。hack时建议输出最后一行的行末回车;每一行的结尾不要输出空格。
对于每组数据,输出一个数表示答案。
3
1 1
1 2
2 4
0
-1
1 题解:对n质数分解成x个指数因子,再对m进行整除到底,知道所得m为1为止,记录步数ans就是答案,否则-1;
///
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef unsigned long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define memfy(a) memset(a,-1,sizeof(a));
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--) #define inf 100000000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//***************************************
#define maxn 1000000+5
bool P[maxn];
int k;
int prime[maxn];
vector<int >V;
int H[maxn],S[maxn];
void init()
{
P[]=;
for(int i=;i<=;i++)
{
if(!P[i])
{
for(int j=i+i;j<=;j=j+i)
P[j]=;
}
}
k=;
for(int i=;i<=;i++)
{
if(!P[i])
prime[++k]=i;
}
}
__int64 get( __int64 x,__int64 y)
{
__int64 j=;
while(y<x)
{
x-=y;
j++;
y+=y;
}
if(j==)j++;
return j;
}
int main()
{
int T;
init();
scanf("%d",&T); while(T--)
{
V.clear();
ll n=read();
ll m=read();
if(n==m)
{
printf("0\n");
continue;
}
if(n>m)
{
printf("-1\n");
continue;
}
if(n==)
{
printf("-1\n");
continue;
}
if(m%n){
cout<<-<<endl;
continue;
}
ll tmp=m/n;//cout<<tmp<<endl;
for(int i=;i<=k;i++)
{
while(n%prime[i]==)
{
if(H[prime[i]]==)
V.push_back(prime[i]);
H[prime[i]]++;
n/=prime[i];
}if(prime[i]>n)break;
} // for(int i=0;i<V.size();i++)cout<<V[i]<<endl;
__int64 ans=;
for(int i=;i<V.size();i++)
{
if(tmp%V[i]==) {
ll jj=;
while(tmp%V[i]==)
{
tmp/=V[i];
jj++;
}
ans=max(ans,get(jj,H[V[i]]));
}
H[V[i]]=;
}
if(tmp!=){
printf("-1\n");
}
else printf("%I64d\n",ans); } return ;
}
代码
BestCoder Round #60/HDU 5505 暴力数学的更多相关文章
- Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, ...
- [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)
Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...
- HDU 5505 - BestCoder Round #60 - GT and numbers
题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1002 思路 : N有若 ...
- HDU 5506 - BestCoder Round #60 - GT and set
题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1003 题意 : 给N集 ...
- BestCoder Round #60 题解链接
题解 题目 1001 GT and sequence 注意先特判000的情况:如果读入的数据有000,那么去掉所有的000且最后答案和000取一个max. 剩下的正数显然全部乘起来比较优. 对于负数 ...
- [BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)
Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- BestCoder Round #60 1002
Problem Description You are given two numbers NNN and MMM. Every step you can get a new NNN in the w ...
随机推荐
- [JSOI2012]玄武密码 题解(AC自动机)
显然是AC自动机对吧 插入单词之后把文章在自动机上跑一遍,到达过的节点打上花火标记 之后检查一下每个单词有几个标记即可 可以把题目中的4个字母映射成abcd方便遍历 一定要记得把文章也映射啊! #in ...
- CAD绘制一个图象标记对象(com接口VB语言)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Spring框架系列(五)--面向切面AOP
背景: 当需要为多个不具有继承关系的对象引入一个公共行为,例如日志.权限验证.事务等功能时,如果使用OOP,需要为每个对象引入这些公共 行为.会产生大量重复代码,并且不利用维护.AOP就是为了解决这个 ...
- spring aop 方法增加日志记录
使用场景: 1:调用外部接口时需要记录出参和入参. 2:分布式系统之间,调用各个系统之间需要记录日志,一旦出现了问题也可以找得到元数据 一言不合,上代码: # 枚举类 package xxxxxxxx ...
- Deepin系统关于每次启动终端都要输入source /etc/profile的问题
关于每次启动终端都要输入source /etc/profile的问题 当我在Deepin系统中下载了node以及npm之后,我为了将node导入到系统文件,使用了以下命令sudo gedit ``/e ...
- HTML5定制全选列头
随着HTML5产品分支的不断深入使用,HTML5的需求也是越来越多,表格组件的使用也不例外,什么排序,分页,自动列宽等.最近有客户提出了如果让表格的列头加上全选的功能.细细分析其实就是两部分,表格的b ...
- ubuntu 常见的操作命令
原博客地址为:https://blog.csdn.net/qq_33421080/article/details/76551554 1.cd命令: cd:切换到当前用户根目录,默认[/home/用户名 ...
- 洛谷 2484 [SDOI2011]打地鼠
[题解] n^6的做法很好想,然而这样复杂度不对.. 然后我们可以发现R和C可以分开求,这样复杂度降到了n^4. 使用树状数组可以把复杂度降到n^3logn,可以顺利通过. #include<c ...
- c# 缓存!
做项目的时候获取所有城市的时候,发现每次去获取都花费了很多时间,所以用缓存方法让效率更高! 这是我做的例子,如下: public class CacheGetCity { /// <summar ...
- Apache Maven Cookbook(一)maven 使用命令创建、编译java项目
一.创建 使用命令创建项目分几步: 1.打开命令行窗口,比如cmd,把目录切换至想要创建项目地方. 2.执行如下命令: mvn archetype:generate -DgroupId=com.zua ...