HDU 5505——GT and numbers——————【素数】
2015-10-19 18:20
411 查看
GT and numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 683 Accepted Submission(s): 190
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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≤263.
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.
[align=left]Output[/align]
For each test case,output an answer.
[align=left]Sample Input[/align]
3
1 1
1 2
2 4
[align=left]Sample Output[/align]
0
-1
1
[align=left]Source[/align]
BestCoder Round #60
题目大意:
![](http://images2015.cnblogs.com/blog/729983/201510/729983-20151019181738599-2142320707.png)
解题思路:
![](http://images2015.cnblogs.com/blog/729983/201510/729983-20151019181944724-1738235358.png)
#include<stdio.h> #include<algorithm> #include<string.h> #include<iostream> #include<limits.h> using namespace std; typedef unsigned long long UINT; const int maxn = 1e6+2000; UINT prime[maxn]; void getprime(){ prime[0] = 1; prime[1] = 1; for(UINT i = 2; i*i <= maxn-10; i++){ if(!prime[i]) for(int j = i*i; j <= maxn-10 ;j += i){ prime[j] = 1; } } } int main(){ int T; getprime(); scanf("%d",&T); UINT a,b; while(T--){ scanf("%llu%llu",&a,&b); if(b < a){ puts("-1"); }else if(b == a){ puts("0"); }else{ if(a == 1 || b%a != 0){ puts("-1"); }else{ b /= a; int sum = 0; for(UINT i = 2; i <= maxn-100; i++){ if(prime[i]) continue; UINT tmp = 1 , times = 0; if(a % i != 0) continue; while(a % i == 0){ a /= i; tmp *= i; } while(b % tmp == 0){ b /= tmp; tmp *= tmp; times ++; } if(b % i == 0){ sum = times+1 > sum? times+1:sum; while( b % i == 0){ b /= i; } }else{ sum = times > sum? times:sum; } if(b == 1){ break; } } if(b == 1){ printf("%d\n",sum); }else{ puts("-1"); } } } } return 0; }
相关文章推荐
- UILabel顶端对齐
- exit()与_exit()函数的区别(Linux系统中)
- UNIX命令学习--(解压和压缩命令)
- 插入排序 - InsertionSort
- css+div学习笔记
- kali增加open***
- 工厂模式的enum实现
- 《Spring.3.x企业应用开发实战》--- 零碎笔记
- 匿名方法与Lambda表达式
- 匿名方法与Lambda表达式
- 各语言对浮点数的截取&四舍五入等操作
- JavaScript的Ajax数据请求与浏览器兼容
- SBT安装
- 学习prototypejs中的继承实现机制(二):让$super更像java中的super关键字
- CentOS-6.5系统基础优化附带优化脚本
- 利用HOOK保护记事本进程
- MFC对话框Dialog控件处理程序handler因为public修饰符导致无法访问
- Navicat_Premium连接Oracle_10g
- RocketMQ在windows上安装和开发使用
- 记录一次低级错误――secureCRT无法连接虚拟机