hdu5505-GT and numbers-(贪心+gcd+唯一分解定理)

时间:2022-09-26 07:59:43

GT and numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2772    Accepted Submission(s):
688

Problem Description
You are given two numbers N and M.

Every step you can get a new N in the way that multiply N by a factor of N .

Work out how many steps can N be equal to M at least.

If N can't be to M forever,print −1 .

 
Input
In the first line there is a number T. T is the test number.

In the next T lines there are two numbers N and M .

T≤1000 , 1≤N≤1000000 ,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

题意:n要变到m,每次乘一个n的因子数,求最少乘几次

坑1:m需要用无符号long long定义

坑2:n的因子会变,变多变大
用r表示需要乘的总数,择优,每次选r和n的gcd,这样每次乘得多,次数就少,同时更新r和n
如果遇到r!=1,gcd=1证明需要乘的数 包含 n没有的因子,退出。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
#define ll unsigned long long
const int p=;
const int maxx=1e6+;
ll n,m;///坑1:无符号long long才放得下
int step;
ll gcd(ll a,ll b)
{
if(b==) return a;
return gcd(b,a%b);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cin>>n>>m;
step=;
if(n==m) printf("0\n");
else if(n>m || m%n) printf("-1\n");
else
{
ll r=m/n;///r表示 n还要 乘 一个数 变成m
ll d;
bool flag=true;
while(r!=)
{
d=gcd(r,n);
if(d==)///出现这种情况,r!=1,和n没有共同因子,则表示m中含有n中不含有的质因子
{
flag=false;
break;
}
r=r/d;
n=n*d;///坑2:每次n会变大,因子会更新
step++;
}
if(flag)
cout<<step<<endl;
else printf("-1\n");
}
}
return ;
}

hdu5505-GT and numbers-(贪心+gcd+唯一分解定理)的更多相关文章

  1. cf1047C-Enlarge GCD-(欧拉筛&plus;map&plus;gcd&plus;唯一分解定理)

    https://vjudge.net/problem/CodeForces-1047C 题意:有n个数,他们有个最大公约数设为maxxgcd,要删去一些数,使得剩下的数的gcd大于maxxgcd. 解 ...

  2. HDU-1492-The number of divisors&lpar;约数&rpar; about Humble Numbers -求因子总数&plus;唯一分解定理的变形

    A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, ...

  3. Codeforces Round &num;520 &lpar;Div&period; 2&rpar; B&period; Math 唯一分解定理&plus;贪心

    题意:给出一个x 可以做两种操作  ①sqrt(x)  注意必须是完全平方数  ② x*=k  (k为任意数)  问能达到的最小的x是多少 思路: 由题意以及 操作  应该联想到唯一分解定理   经过 ...

  4. NOIP2009Hankson 的趣味题&lbrack;唯一分解定理&vert;暴力&rsqb;

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...

  5. UVA - 10375 Choose and divide&lbrack;唯一分解定理&rsqb;

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  6. POJ1845Sumdiv(求所有因子和 &plus; 唯一分解定理)

    Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17387   Accepted: 4374 Descripti ...

  7. lightoj 1236 正整数唯一分解定理

    A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     6 ...

  8. POJ - 1845 G - Sumdiv &lpar;唯一分解定理&rpar;

    Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S m ...

  9. hdu4497-GCD and LCM-&lpar;欧拉筛&plus;唯一分解定理&plus;组合数&rpar;

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

随机推荐

  1. java 中的一个项目如何做到访问另一个项目的一个方法 或者 页面

    两种方法:1.将一个项目打成jar包,第二个项目进行导入该jar包,就可以使用第一个项目里的类方法属性等2.将第一个项目发布出去,然后第二个项目调用,所谓发布出去就是开发远程接口,允许其他人调用.

  2. 比特币钱包应用breadwallet源码

    breadwallet是一款安全.可靠和便捷的比特币钱包,可使用户免于恶意软件和其他应用中常见的安全问题的骚扰,充分利用了iOS提供的安全功能,包括AES硬件加密.app沙盒和数据保护.代码签名以及k ...

  3. &lbrack;leetcode&rsqb;&lowbar;Best Time to Buy and Sell Stock I &amp&semi;&amp&semi; II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  4. Android的ADT内容助手快捷方式设置

    请注明出处:http://www.cnblogs.com/killerlegend/p/3550019.html  Written By KillerLegend 先将Word Completion的 ...

  5. error&colon; Error&colon; No resource found that matches the given name &lpar;at &&num;39&semi;layout&lowbar;above&&num;39&semi; with value &&num;39&semi;&commat;id&sol;btnLayout&&num;39&semi;&rpar;&period;

    今天在练习fragment碎片的时候,进行界面布局的时候出现了这个问题. 后来解决后发现原因很简单:就是因为在布局xml文件中,引用ID和声明ID的顺序必须保证声明在前,引用在后.和布局的顺序无关. ...

  6. AIX topas命令详解

    本文转载于:AIX topas命令详解 topas命令默认2秒更新一次 一.topas命令以区域形式表现系统各项指标性能,如下图: 1. CPU:反应CPU性能区域,如果有多个 CPU,按 c 键两次 ...

  7. activemq下activemq&period;bat不能启动

    今天下载了一个apache-activemq-5.5.0-bin.rar解压缩后双击/bin目录下的activemq.bat批处理文件发现启动窗口一闪而过无法启动,最后找到原因是因为在环境变量-系统变 ...

  8. Open-Falcon第三步安装Agent (小米开源互联网企业级监控系统)

    安装Agent 每台机器上,都需要部署agent,agent会自动采集预先定义的各种采集项,每隔60秒,push到transfer. cd $WORKSPACE/agent/ mv cfg.examp ...

  9. 数字图像处理的Matlab实现(1)—绪论

    第1章 绪论 1.1 什么是数字图像处理 一幅图像可以定义为一个二维函数\(f(x,y)\),这里的\(x\)和\(y\)是空间坐标,而在任意坐标\((x,y)\)处的幅度\(f\)被称为这一坐标位置 ...

  10. LDA-线性判别分析(二)Two-classes 情形的数学推导

    本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...