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-(欧拉筛+map+gcd+唯一分解定理)

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

  2. 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, ...

  3. Codeforces Round #520 (Div. 2) B. Math 唯一分解定理+贪心

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

  4. NOIP2009Hankson 的趣味题[唯一分解定理|暴力]

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

  5. UVA - 10375 Choose and divide[唯一分解定理]

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

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

    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 (唯一分解定理)

    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-(欧拉筛+唯一分解定理+组合数)

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

随机推荐

  1. linux获取内存、cpu、负载、网口流量、磁盘信息

    内存信息 / meminfo 返回dict   #!/usr/bin/env python   def memory_stat():   mem = {}   f = open("/proc ...

  2. 错误代码: 1231 - Variable 'sql_mode' can't be set to the value of 'NULL'

    错误代码: 1231 - Variable 'sql_mode' can't be set to the value of 'NULL' 错误代码: - Variable 'sql_mode' can ...

  3. Maven私服仓库类型

    1. 代理仓库(Proxy Repository) 顾名思义是代理第三方仓库的,如: maven-central nuget.org-proxy 版本策略(Version Policy): Relea ...

  4. jsp grid can not be used in this ('quirks') mode

    设置: <!--设置IE文档模式  --> <meta http-equiv="X-UA-Compatible" content="IE=9" ...

  5. leetcode1010

    class Solution: def numPairsDivisibleBy60(self, time: 'List[int]') -> int: sums = 0 s = {} n = le ...

  6. beego生成 api 项目 && api 文档

    目标 生成 api 项目,并且自动生成db的mapper.module等:同时生成api文档 操作步骤 1.生成 api 项目,并且自动生成db全表的映射 bee api [projectName] ...

  7. 机器学习进阶-人脸关键点检测 1.dlib.get_frontal_face_detector(构建人脸框位置检测器) 2.dlib.shape_predictor(绘制人脸关键点检测器) 3.cv2.convexHull(获得凸包位置信息)

    1.dlib.get_frontal_face_detector()  # 获得人脸框位置的检测器, detector(gray, 1) gray表示灰度图, 2.dlib.shape_predict ...

  8. python 阿狸的进阶之路(9)

    tcp传输: 传输需要ack回应,然后才清空缓存,服务端先起来. tcp流式协议,tcp的Nagle的优化算法,会将时间间隔短,数据量小的打包成一个,然后发送给对方,减少发送的次数. UDP协议: 不 ...

  9. java自定义抛出的异常Exception

    package com.zhanzhuang.exception; public class CustomizeException { public static void main(String[] ...

  10. 尚硅谷springboot学习11-占位符

    1.随机数 2.占位符获取之前配置的值,如果没有可以使用:指定默认值