Poj 3993 not so flat after all (prime factor)

Source: Internet
Author: User

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 dimen1_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-dimen=space where each of the N axes is assigned a unique prime number. Each N-dimen=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 |-dimen1_space. the figure at the bottom strates 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-dimen1_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 882770 7920 0

Sample output

1. 3:42. 5:6

The question shows you two numbers A and B. Calculate the absolute values of the numbers of all prime factors of A and B, and the absolute values of the difference between the numbers of each prime factor are described in a complex way;

To solve the problem of prime numbers, first create a prime number table, and then calculate the prime factor. Then, calculate how many times a and B can be divided by this number.

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.