HDU 5505 - BestCoder Round #60 - GT and numbers
题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1002
思路 :
N有若干个质因子, N = a^b * c^d * e^f......
M也有若干个质因子, M = a^(b+k) * c(d+k1) * e^(f+k2)......
N能到达M的条件是它们的质因子必须完全相同
N每次可以乘上它的若干个质因子, 直到这个质因子的幂次等于M这个质因子的幂次
考虑这样一个事实, N乘上某个质因子的a次幂后, 新的N可以乘上该质因子的2a次幂
故对于每个质因子, N的次数每次平方, cnt++, 直到大于等于M这个质因子的次数, 取最大的cnt即可
要注意的是M的范围会爆long long 所以要开unsigned long long
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- typedef unsigned long long LL;
- const int MAXN = 1e6+;
- const int PRI_NUM = 2e6+;
- bool vis[PRI_NUM];
- int prime[MAXN];
- int factor_n[MAXN];
- int factor_m[MAXN];
- int prime_n[];
- int prime_m[];
- void Pre()
- {
- for(int i = ; i <= PRI_NUM; i++) {
- if(vis[i] == ) {
- for(int j = i+i; j <= PRI_NUM; j+=i) {
- vis[j] = ;
- }
- }
- }
- int cnt = ;
- for(int i = ; i <= PRI_NUM; i++) {
- if(vis[i] == ) {
- prime[cnt++] = i;
- }
- }
- }
- void Init()
- {
- memset(prime_n, , sizeof(prime_n));
- memset(prime_m, , sizeof(prime_m));
- }
- int main()
- {
- Pre();
- int t;
- int n;
- LL m;
- scanf("%d", &t);
- while(t--) {
- Init();
- scanf("%d %I64u", &n, &m);
- if(m == n) {
- printf("0\n");
- continue;
- }
- if(m % n || n <= ) {
- printf("-1\n");
- continue;
- }
- int cnt = ;
- for(int i = ; i < MAXN; i++) {
- if(n == ) break;
- if(n % prime[i] == ) {
- factor_n[cnt] = prime[i];
- while(n % prime[i] == ) {
- n /= prime[i];
- prime_n[cnt]++;
- }
- cnt++;
- }
- }
- if(n > ) {
- factor_n[cnt] = n;
- prime_n[cnt] = ;
- cnt++;
- }
- for(int i = ; i < cnt; i++) {
- while(m % factor_n[i] == ) {
- m /= factor_n[i];
- prime_m[i]++;
- }
- }
- if(m > ) {
- printf("-1\n");
- continue;
- }
- int ans = ;
- for(int i = ; i < cnt; i++) {
- int k = ;
- while(prime_n[i] < prime_m[i]) {
- prime_n[i] <<= ;
- k++;
- }
- if(k > ans) ans = k;
- }
- printf("%d\n", ans);
- }
- return ;
- }
另外看到一种很强的做法
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- typedef unsigned long long ULL;
- ULL Gcd(ULL a, ULL b)
- {
- ULL r;
- while(a % b) {
- r = a % b;
- a = b;
- b = r;
- }
- return b;
- }
- int main()
- {
- int t;
- ULL n, m;
- scanf("%d", &t);
- while(t--) {
- scanf("%I64u %I64u", &n, &m);
- int ans = ;
- bool flag = ;
- while(n != m) {
- if(m % n) {
- flag = ;
- break;
- }
- ULL temp = Gcd(m / n, n);
- if(temp == ) {
- flag = ;
- break;
- }
- n *= temp;
- ans++;
- }
- if(flag == ) {
- printf("%d\n", ans);
- }
- else printf("-1\n");
- }
- return ;
- }
HDU 5505 - BestCoder Round #60 - GT and numbers的更多相关文章
- HDU 5506 - BestCoder Round #60 - GT and set
题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1003 题意 : 给N集 ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- hdu 5643 BestCoder Round #75
King's Game Accepts: 249 Submissions: 671 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6 ...
- hdu 5641 BestCoder Round #75
King's Phone Accepts: 310 Submissions: 2980 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- BestCoder Round #60/HDU 5505 暴力数学
GT and numbers 问题描述 给出两个数NN和MM. NN每次可以乘上一个自己的因数变成新的NN. 求最初的NN到MM至少需要几步. 如果永远也到不了输出-1−1. 输入描述 第一行读入一个 ...
- HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树
zxa and leaf Problem Description zxa have an unrooted tree with n nodes, including (n−1) undirected ...
- HDU 5496 - BestCoder Round #58 - Beauty of Sequence
题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=637&pid=1002 思路 : 考 ...
- BestCoder Round #60 题解链接
题解 题目 1001 GT and sequence 注意先特判000的情况:如果读入的数据有000,那么去掉所有的000且最后答案和000取一个max. 剩下的正数显然全部乘起来比较优. 对于负数 ...
- BestCoder Round #60 1002
Problem Description You are given two numbers NNN and MMM. Every step you can get a new NNN in the w ...
随机推荐
- 二分图的最大匹配-hdu-3729-I'm Telling the Truth
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3729 题目意思: 有n个学生,老师询问每个学生的排名,每个学生都告诉了一个排名区间,求可能的最多的学 ...
- [ES6] Converting an array-like object into an Array with Array.from()
Array.from() lets you convert an "iterable" object (AKA an array-like object) to an array. ...
- Facebook的手游出海之道
对于不同的游戏公司,面临的同一个问题就是怎样让海外玩家能够一眼在App中发现你,成为你的新用户:不仅如此,怎样留住这些用户,让他们成为你游戏的忠实玩家也是让全部游戏开发商困扰的一个问题. w=580& ...
- 10 Questions To Make Programming Interviews Less Expensive--reference
Conducting Interview is not cheap and costs both time and money to a company. It take a lot of time ...
- webform 复杂点的服务器控件
1 , dropdownlist: 下拉框 属性items 列表集合, 里面的每一个元素是一个 listitem . 联动的时候注意要 设置属性 .Autopostback 为ture: 注注 ...
- mysql的distinct理解
select distinct id,name from route where update_time>=''; 上面的sql语句的逻辑是两条记录的id,name只要有一个不一样,就算不一样. ...
- Lesson 5: Typography in Product Design
Lesson 5: Typography in Product Design Article 1: Interactive Guide to Blog Typography 布局(Layout) 用空 ...
- c - 2/1, 3/2, 5/3, 8/5, 13/8...前20项的和
double pres(const int n) { ; //分子. ; //分母. ; double tmp; ; i <= n; i++) { sum += (numerator / den ...
- SQL数据库中把一个表中的数据复制到另一个表中
1.如果是整个表复制表达如下: insert into table1 select * from table2 2.如果是有选择性的复制数据表达如下: insert into table1(colu ...
- C#操作MYSQL遇到0000-00-00日期报错的原因
今天在做一个C#连接MYSQL数据库,并读取数据库的内容,遇到了0000-00-00日期转换报错:unable to convert MySQL date/time value to System.D ...