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 ...
随机推荐
- 修改之前某次commit日志和内容
如果需要撤销最近一次提交的代码 已经commit,没有submit状态:可以使用git reset --hard HEAD^ 比如之前已经提交了五个patch,但是需要修改第三个. 第一步: 将修改的 ...
- linux配置防火墙,开启端口
Centos7,配置防火墙,开启端口 1.查看已开放的端口(默认不开放任何端口) firewall-cmd --list-ports 2.开启80端口 firewall-cmd --zone=publ ...
- java中的排序(自定义数据排序)--使用Collections的sort方法
排序:将一组数据按相应的规则 排列 顺序 1.规则: 基本数据类型:日常的大小排序. 引用类型: 内置引用类型(String,Integer..),内部已经指定规则,直接使用即可.---- ...
- hping安装过程
转载: http://www.safecdn.cn/website-announcement/2018/12/hping-install/97.html Hping的主要功能有: 测试防火墙实用的 ...
- Apache-Axis小结
以前用过axis, 不过好久不弄, 有忘记了.很多很多东西放在收藏夹里面, 但是长时间不去看,结果就是还是不熟悉!现在再简单总结一下吧. Axis开发服务器端webservice其实很简单. 1 下载 ...
- Cannot run Eclipse; JVM terminated. Exit code=13
在myeclipse 上运行好好的, 在 eclipse 上就运行不了了. 运行eclipse.exe 就出现: Cannot run Eclipse; JVM terminated. Exit co ...
- Angular.js实现分页
一.编写angularJS实现的分页js(网上搜)和样式表(pagination),并在页面引入 二.编写变量和方法 //分页控件控制 $scope.paginationConf={ currentP ...
- Android将Log写入文件
为什么要将Log写入文件 运行应用程序的时候,大多数是不会连接着IDE的: 而当应用程序崩溃时,我们需要收集复现步骤,在设备上复现,并进行Debug: 而由于Android手机的多样性,有些问题是某个 ...
- scrapy之parallel
Limiting Parallelism jcalderone May 22nd, 2006 This blog has moved! Read this post and its comments ...
- CustomJSProperties珍藏版。目的是减少客户端的代码数量,但是又能将服务器数据传输给客户端。关键是:数据是实时更新的!!!!
[这是帮助文档的说明] CustomJSProperties EventThe CustomJSProperties event fires each time a control callback ...