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 ...
随机推荐
- Could not resolve all files for configuration;Andriod在build.gradle添加compile files()报错
在build.gradle中添加个 compile files('libs/alipaySdk-20170922.jar') 就一直报这个错误 Error:Could not resolve all ...
- call与apply简单介绍
var pet={ word:'...', speak:function(say){ console.log(say+' '+this.word) } } //pet.speak('speak')// ...
- Java中对List集合的常用操作(转)
list中添加,获取,删除元素: list中是否包含某个元素: list中根据索引将元素数值改变(替换): list中查看(判断)元素的索引: 根据元素索引位置进行的判断: 利用list中索引位置重新 ...
- C# WPF DevExpress 图表控件之柱状图
说明:DevExpress版本是17.1.VS是2015. XAML: <!--#region 图表控件--> <dxc:ChartControl x:Name="char ...
- MySQL Developer
1.The mysql Client Program 2.Data Types 3.Joins 4.Subqueries 5.Views 6.StoredRoutine . 1.Client/Serv ...
- django配置https
1. pip install django-extensions pip install django-werkzeug-debugger-runserver pip install pyOpenSS ...
- python-单元测试unittest
目录: 1.unittest.TestCase中常用的断言方法 1.1 subTest子测试 1.2 套件测试 1.3 批量测试单个用例 2. 加载器 2.1加载器协议 2.2.执行器 TestRun ...
- 17.scrapy-splash安装-2
scrapy-splash是一个scrapy中支持的javascript渲染的工具. scrapy-splash安装分为两部分.一个是splash服务的安装,具体是通过docker,安装之后,会启动一 ...
- 33.纯 CSS 创作牛奶文字变换效果
原文地址:https://segmentfault.com/a/1190000015037234 感想:transform: translateY(50% & -50%); animatio ...
- GIFDecoder源码分析
源码见:ddxxll2008/gifdecoder_java run() public void run(){ if(in != null){ readStream(); }else if(gifDa ...