HDU 5505——GT and numbers——————【素数】

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

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≤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.

Output

For each test case,output an answer.

Sample Input

3

1 1

1 2

2 4

Sample Output

0

-1

1

Source

BestCoder Round #60

题目大意:

解题思路:

#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;
}

  

时间: 2024-12-17 12:59:41

HDU 5505——GT and numbers——————【素数】的相关文章

HDU 5505 GT and numbers(GCD魔法)

题目链接:点击打开链接 题意:给两个数n和m, n每次乘以它的因子变成一个新的值, 求最少乘几次可以变成m. 思路:每次乘以的整数v有两个要求:1.它是n的因子:2.它要尽量大. 又因为如果n能最终到达m,一定是乘以n的k倍, 所以只要n能被m整除, 那么每次取gcd(n, m/n)就行了. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #i

hdu how many prime numbers 筛选法求素数

/* * hdu How many prime numbers * date 2014/5/13 * state AC */ #include <iostream> #include <cmath> #include <cstdio> using namespace std; bool isPrime(int x) { int sqr=sqrt(x*1.0); for(int i=2;i<=sqr;i++) { if(x%i==0)return false; }

筛法暴力打表 --- hdu : 12876 Quite Good Numbers

Quite Good Numbers Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 77, Accepted users: 57 Problem 12876 : No special judgement Problem description A "perfect" number is an integer that is equal to the sum

HDU 4228 Flooring Tiles 反素数

推出了结论,万万没想到最后用搜索.. 还想dp来着.. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <math.h> #include <set> #include <vector> #include <map> using namespace std; #define ll lon

HDU 4228 Flooring Tiles 反素数的应用

给你一个数N,找出一个最小的可以拆分成N种乘积表达形式的数x 比如N=2,6可以拆成2x3或者1x6两种,但不是最小的,最小的是4可以拆成1x4,2x2两种 首先可以肯定的是x必然有N*2或者是N*2-1(完全平方的情况)个约数 利用求反素数的过程求出约数为N*2和N*2-1个的最小的数 #include <cstdio> #include <sstream> #include <fstream> #include <cstring> #include &l

HDU 3641 Treasure Hunting (素数拆分)

题意:有N个ai,bi,M=a1^b1*a2^b2*a3^b3-*an^bn ,求最小的 x 使得 x! % M ==0. 思路:把M分成多个素数相乘,num[i] 记录素数 i 的个数,然后二分找到x,若 x! 中所有 i 的个数满足>=num[i] 即为答案. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #in

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won't exceed 32-

HDU 5108Alexandra and Prime Numbers(大素数)

Problem Description Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given an positive integer N, judge whether N is prime. The problem above is quite easy, so Alexandra gave him a new task: Given

hdu 5272 Dylans loves numbers 水题

Dylans loves numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5272 Description Dylans是谁?你可以在 UOJ 和 Codeforces上看到他.在BestCoder里,他有另外一个ID:s1451900.今天的题目都和他有关哦.Dylans得到了一个数N.他想知道N的二进制中有几组1.如果两个1之间有若干个(至少一个)0