hdu5505-GT and numbers-(贪心+gcd+唯一分解定理)
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
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 .
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.
1 1
1 2
2 4
题意: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+唯一分解定理)的更多相关文章
- cf1047C-Enlarge GCD-(欧拉筛+map+gcd+唯一分解定理)
https://vjudge.net/problem/CodeForces-1047C 题意:有n个数,他们有个最大公约数设为maxxgcd,要删去一些数,使得剩下的数的gcd大于maxxgcd. 解 ...
- HDU-1492-The number of divisors(约数) about Humble Numbers -求因子总数+唯一分解定理的变形
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, ...
- Codeforces Round #520 (Div. 2) B. Math 唯一分解定理+贪心
题意:给出一个x 可以做两种操作 ①sqrt(x) 注意必须是完全平方数 ② x*=k (k为任意数) 问能达到的最小的x是多少 思路: 由题意以及 操作 应该联想到唯一分解定理 经过 ...
- NOIP2009Hankson 的趣味题[唯一分解定理|暴力]
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ1845Sumdiv(求所有因子和 + 唯一分解定理)
Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17387 Accepted: 4374 Descripti ...
- lightoj 1236 正整数唯一分解定理
A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB 6 ...
- POJ - 1845 G - Sumdiv (唯一分解定理)
Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S m ...
- hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)
GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
随机推荐
- Struts2学习:interceptor(拦截器)的使用
对于需要登陆验证.权限验证等功能的网站,每一次请求,每一个action都写一段验证的代码,未免显得冗余且不易维护.struts2提供了拦截器interceptor,为这些页面提供一个切面,或者说公共组 ...
- linux下mysql-5.6忘记root密码,重置root密码详细过程
在linux平台下使用mysql过程中忘记了root密码,对于运维和DBA来讲都是一件头疼的事情,下面来讲解下怎么进行重置mysql数据库root 密码: 1.首先停止mysql服务进程: 1 s ...
- SQL Server 合并行
select a.*,b.Organization_Name,c.User_Name sgry,c.renNum,d.User_Name fzr,e.pic_url from dbo.TB_ZYM_L ...
- 轻松解决oracle11g 空表不能exp导出的问题。
解决方法: 1插入一条数据(或者再删除),浪费时间,有时几百张表会累死的.2创建数据库之前使用代码: Sql代码 alter system set deferred_segment_creation ...
- redis的键命令
键的命令 查找键,参数支持正则 KEYS pattern 判断键是否存在,如果存在返回1,不存在返回0 EXISTS key [key ...] 查看键对应的value的类型 TYPE key 删除键 ...
- 4.安装mitmproxy问题处理
上次进行到安装 证书出现问题: 1.主要原因是:对应python版本不一致 需要3.6以上的版本 才能在cmd黑窗口执行 mitmdump命令 生成证书 如图: windows 版本 双击安装 mit ...
- 【学习】Python解决汉诺塔问题
参考文章:http://www.cnblogs.com/dmego/p/5965835.html 一句话:学程序不是目的,理解就好:写代码也不是必然,省事最好:拿也好,查也好,解决问题就好! ...
- 销售人员的分析,也可以用类似RFM的思路吗?
本文转自知乎 作者:接地气的陈老师 ————————————————————————————————————————————————————— 有同学问:“销售人员的分析,也可以用类似RFM的思路吗, ...
- 1. ibatis 查询的sql列存在相同的列名
如果SQL语句存在两个相同的查询列名,则映射时,取第一个列名的值进行映射 <?xml version="1.0" encoding="UTF-8" ?&g ...
- js代码执行过程
一:first-blood:任何程序执行的第一步总是会先检查有没有语法错误,如果有,则直接抛出语法错误,直接跳出, 二:second-blood:预编译,预编译呢会有四个执行过程,1:创建执行期上下文 ...