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≤2​63​​. 注意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 暴力数学的更多相关文章

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

  2. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  3. [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)

    BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, ...

  4. [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

    Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...

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

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

  6. HDU 5506 - BestCoder Round #60 - GT and set

    题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1003 题意 : 给N集 ...

  7. BestCoder Round #60 题解链接

    题解  题目 1001 GT and sequence 注意先特判000的情况:如果读入的数据有000,那么去掉所有的000且最后答案和000取一个max. 剩下的正数显然全部乘起来比较优. 对于负数 ...

  8. [BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)

    Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. 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 ...

随机推荐

  1. 隐藏win10任务栏输入法M图标

    在任务栏右键=>任务栏设置=>打开或关闭系统图标=>(关闭)输入指示

  2. Win7访问不了WINXP共享文件

    用win xp的机器可以访问,但用win 7的机器无法访问共享文件 提示:您没有权限访问.请与网络管理员联系请求访问权限 网上找了相应的资料 做了如下动作 1. 打开网上邻居→本地连接→属性里,“看是 ...

  3. Linux未来监控tracing框架——eBPF

    Linux未来监控tracing框架--eBPF eBPF源于早年间的成型于 BSD 之上的传统技术 BPF(Berkeley Packet Filter).BPF 的全称是 Berkeley Pac ...

  4. Spring Boot 与消息

    一.消息概述 在大多数应用中,可以通过消息服务中间件来提升系统的异步通信.扩展解耦和流量削峰等能力. 当消息发送者发送消息后,将由消息代理接管,消息代理保证消息传递到指定目的地. 消息队列主要有两种形 ...

  5. 运行jar包的命令

    windows下使用java -jar xxx.jar运行,linux下使用nohup java -jar xxx.jar & 如果想停止jar运行,ps -ef查看进程(进程多的话也可以加上 ...

  6. docker-ce安装官翻

    参考http://www.cnblogs.com/maple42/p/5868846.htmlhttp://blog.csdn.net/lizehua123/article/details/50601 ...

  7. LES on Wind turbine

  8. C. Day at the Beach

    codeforces 599c C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the be ...

  9. 恶补---bell数

    定义 bell数即一个集合划分的数目 示例 前几项的bell数列为 1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975 ,... 求值方法 1.bell ...

  10. 恶补数论(二) Baby-Step-Giant-Step 大步小步求离散模对数

    知识概述 好吧,我承认这是我初三寒假就听过的知识,然而我现在早就高一了(又是寒假,只不过我已经在省选了...) 额,这是求离散模对数的一种算法 也就是求满足方程a^x≡b(mod p)的最小的x(其中 ...