Poj3993not so flat after all (sieve Prime Number + factorization prime factor)

Source: Internet
Author: User
Question link: Ah, haha, click me
Question: two numbers are given, which are obtained by the decomposition theorem. Each number can be divided into the product of several prime factors, in this way, the point can be expressed in an n-dimensional coordinate system... For example, 50 and 24
Because 24 = 2 ^ 3*3 ^ 1*5 ^ 0 and 50 = 2 ^ 1*3 ^ 0*5 ^ 2, the two points can be in a 3-dimensional coordinate system. indicates the two points .. 24 = (3, 1, 0) 50 = (1, 0, 2) the common dimension is 3, and the distance between the two points in the n-dimensional coordinate system is | 3-1 | + | 1-0 | + | 0-2 | = 5, so that the meaning of the question is completely understood... Ideas:
First, I had to screen out the prime number in 10000. At first, I thought that I could just screen out the prime number of SQRT (10000). Finally, I suddenly thought of a prime number close to 100000. What should I do ?? Then the AC is changed .. Back to the original, and then scan the prime numbers from the front to the back. If one of the two values can be scanned, add 1 to the dimension, and finally decompose the prime factor .. The final statistics are enough .. In this way, the problem is solved successfully... Question: Not so flat after all
Time limit:1000 ms   Memory limit:65536 K
Total submissions:572   Accepted:198

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

Source

Anarc 2009

Code:
#include<cstdio>#include<cmath>#include<iostream>#include<cstring>#include<algorithm>#include<cstdlib>using namespace std;const int maxn=1000000+10;int prime[maxn],vis[maxn],num[2][maxn];int cal;int init(){    int c=0,m;    memset(vis,0,sizeof(vis));    m=sqrt(maxn+0.5);    for(int i=2;i<=m;i++)    {       if(!vis[i])       {           for(int j=i*i;j<=maxn;j+=i)              vis[j]=1;       }    }    for(int i=2;i<=maxn;i++)    {        if(!vis[i])            prime[++c]=i;    }    return c;}void solve(int ith,int x){   for(int i=1;i<=cal;i++)      num[ith][i]=0;   for(int i=1;i<=cal&&x>=prime[i];i++)   {       while(x%prime[i]==0)       {           num[ith][i]++;           x=x/prime[i];       }       if(x==1) break;   }}int main(){    int cas=1,a,b,ans,max_ans;    cal=init();    while(~scanf("%d%d",&a,&b))    {        max_ans=ans=0;        if(a==0&&b==0)  return 0;        for(int i=1;i<=cal;i++)        {           if(a%prime[i]==0||b%prime[i]==0)             ans++;        }        solve(0,a);        solve(1,b);        for(int i=1;i<=cal;i++)           max_ans=max_ans+abs(num[0][i]-num[1][i]);        printf("%d. %d:%d\n",cas++,ans,max_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.