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. dubbo之服务容器

    服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源. 服务容器只是一个简单的Mai ...

  2. Docker方式安装QIIME 2

    # 下载最新版QIIME 2 docker pull qiime2/core:2017.7 # 测试是否安装成功 docker run -t -i -v $(pwd):/mnt/hgfs/2017 q ...

  3. react 中样式私有

    解决的问题,两个组件之间  有相同的class名,造成其中一个无法按预期的显示. import React, { Component } from 'react' import styles from ...

  4. zabbix部署-版本3.2.6

    172.18.237.14:一台主机上安装LAMP环境以及zabbix_server.zabbix_agentd 一.安装zibbix-server 1.环境要求 yum install mysql- ...

  5. 日常操作之如何打开windows注册表

    1.打开注册表:第一步按“win+R”或者点击开始菜单,找到运行,在运行输入框里面输入“regedit”.

  6. laravel 开发辅助工具

    laravel 开发辅助工具 配置 添加服务提供商 将下面这行添加至 config/app.php 文件 providers 数组中: 'providers' => [ ... App\Plug ...

  7. buf.writeInt16BE()函数详解

    buf.writeInt16BE(value, offset[, noAssert]) buf.writeInt16LE(value, offset[, noAssert]) value {Numbe ...

  8. xfce 安装文泉驿字体

    下载文泉驿字体 #拷贝字体到目录/usr/share/fonts/wqy#创建字体缓存 mkfontscale # 在当前目录下生成fonts.scale文件 mkfontdir # 在当前目录下生成 ...

  9. 【Python实践-8】和为S的两个数字

    (剑指offer)输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 思路:选定第一个数字,然后遍历后面的数字求和并与S比 ...

  10. java nio--采用Selector实现Socket通信

    server: /** * 选择器服务端 * Created by ascend on 2017/6/9 9:30. */ public class SelectorServer { // publi ...