POJ 3993 Not So Flat After All(质因数)

Not So Flat After All

Description

Any positive integer v can be written as p1a1*p2a2*...*pnan where pi is a prime number and
ai ≥ 0. For example: 24 = 23*31.

Pick any two prime numbers p1 and p2 where p1 = p2. Imagine a two dimensional plane where the powers of p1 are plotted on the x-axis and the powers of p2 on the y-axis. Now any number that can
be written as p1a1*p2a2 can be plotted on this plane at location (x, y) = (a1, a2). The figure on the right shows few examples where p1 = 3 and p2 =
2.

This idea can be extended for any N-Dimensional space where each of the N axes is assigned a unique prime number. Each N-Dimensional space has a unique set of primes.

We call such set the Space Identification Set or S for short. |S| (the ordinal of S) is N.

Any number that can be expressed as a multiplication of pi ∈ S (each raised to a power (ai ≥ 0) can be plotted in this |S|-Dimensional space. The figure at the bottom illustrates this idea for N = 3 and S = {2, 3, 7}. Needless to say, any number
that can be plotted on space A can also be plotted on space B as long as SA  SB.

We define the distance between any two points in a given N-Dimensional space to be the sum of units traveled to get from one point to the other while following the grid lines (i.e. movement is always parallel to one of the axes.) For example, in the figure
below, the distance between 168 and 882 is 4.

Given two positive integers, write a program that determines the minimum ordinal of a space where both numbers can be plotted in. The program also determines the distance between these two integers in that space.

Input

Your program will be tested on one or more test cases. Each test case is specified on a line with two positive integers (0 < A,B < 1, 000, 000) where A * B > 1.

The last line is made of two zeros.

Output

For each test case, print the following line:

k. X:D

Where k is the test case number (starting at one,) X is the minimum ordinal needed in a space that both A and B can be plotted in. D is the distance between these two points.

Sample Input

168 882
770 792
0 0

Sample Output

1. 3:4
2. 5:6

题意  给你两个数a,b   求a,b所有的质因数个数  和每个质因数个数的差的绝对值的和  被描述得好复杂  理解了就是个水题;

素数问题就先打个素数表吧  然后能被整除的就是质因数了  然后统计a,b分别能被这个数整除多少次

#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
const int N = 1000001;
int p[N], ans[N], vis[N], a, b;
void initPrime()
{
    int num = 0, m = sqrt (N + 0.5);
    for (int i = 2; i <= m; ++i)
        if (vis[i] == 0)
            for (int j = i * i; j <= N; j += i) vis[j] = 1;
    for (int i = 2; i <= N; ++i)
        if (vis[i] == 0) p[++num] = i;
}

int main()
{
    initPrime();
    int cas=0;
    while (scanf ("%d%d", &a, &b),a)
    {
        int cnt = 0, ans = 0, ca, cb;
        for (int i = 1; a > 1 || b > 1; ++i)
            if (a % p[i] == 0 || b % p[i] == 0)
            {
                ++cnt, ca = cb = 0;
                while (a % p[i] == 0) a /= p[i], ++ca;
                while (b % p[i] == 0) b /= p[i], ++cb;
                ans += (ca > cb ? ca - cb : cb - ca);
            }
        printf ("%d. %d:%d\n", ++cas,cnt, ans);
    }
    return 0;
}

POJ 3993 Not So Flat After All(质因数)

时间: 2024-10-07 11:36:18

POJ 3993 Not So Flat After All(质因数)的相关文章

POJ 1142 Smith Numbers(分治法+质因数分解)

http://poj.org/problem?id=1142 题意: 给出一个数n,求大于n的最小数,它满足各位数相加等于该数分解质因数的各位相加. 思路:直接暴力. 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <cmath> 7 using nam

POJ 3993 / HDU 3353

题意:没看懂,实验室的队友们6翻了. 大体就是,打一个素数表,然后把这个数分解了.最后问的是有多少个不同的指数,和两个数之间的距离,也就是指数之差的和. 代码: #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; const int maxn=1123456; int prime[maxn]; int vis[ma

从“n!末尾有多少个0”谈起

在学习循环控制结构的时候,我们经常会看到这样一道例题或习题.问n!末尾有多少个0?POJ 1401就是这样的一道题. [例1]Factorial (POJ 1401). Description The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the

POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10309   Accepted: 3566 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are a

数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the

POJ 1845 Sumdiv#质因数分解+二分

题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想:选定一个要进行比较的目标,在区间[l,r]之间不断二分,直到取到与目标相等的值. #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll

POJ 2429 long long 质因数分解

GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16206   Accepted: 3008 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a

POJ 1091 跳蚤(分解质因数 + 容斥 + 大数)

跳蚤 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8910   Accepted: 2676 Description Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最后一个是M,而前N个数都不超过M,卡片上允许有相同的数字.跳蚤每次可以从卡片上任意选择一个自然数S,然后向左,或向

POJ 1181 大整数是否为素数以及求大整数的质因数-数论-(Miller_rabin+Pollard_rho)

题意:求一个整数是否是素数,如果不是,则输出它最小的质因数. 分析: 判断一个大整数是否为素数用Miller_rabin算法,求一个大整数的所有质因数用Pollard_rho算法.这题就是直接套模板. 另外这里的gcd和pow_mod不能用一般的方式,T了.代码里我注释掉的就是T了的写法. 代码: #include<iostream> #include<cmath> #include<ctime> #include<cstdio> #include<a