码迷,mamicode.com
首页 > 其他好文 > 详细

poj3993Not So Flat After All(筛法素数+分解质因子)

时间:2014-08-07 09:46:49      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   

题目链接:

题意:

题意是给出两个数字,然后有由一分解定理得,每个数可以分解成若干质因数的乘积,这样就可以在一个n维的坐标系下表示出这个点。。。比如给出50和24  
因为24=2^3*3^1*5^0  而50=2^1*3^0*5^2那么这两个点就可以在一个3维德坐标系下表示出这两个点。。24=(3,1,0)  50=(1,0,2)  那么共同拥有的维度就是3  而两个点在n维坐标系下的距离就是|3-1|+|1-0|+|0-2|=5,这样题意完全理解。。。

思路:
先筛出10000内的素数,我最开始天真的以为筛出sqrt(10000)的素数即可,最后忽然想到一个接近100000的素数怎么办??然后改了就ac了。。言归正传,然后对这些素数从前向后扫描,如果两个中有一个能够扫描,那么维度就加1,最后在分解质因数。。最后统计即可。。这样这个问题得到圆满解决。。。

题目:
Not So Flat After All
Time Limit: 1000MS   Memory Limit: 65536K
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 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. 
bubuko.com,布布扣

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 bubuko.com,布布扣 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. 
bubuko.com,布布扣

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

Source



代码为:

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


poj3993Not So Flat After All(筛法素数+分解质因子),布布扣,bubuko.com

poj3993Not So Flat After All(筛法素数+分解质因子)

标签:des   style   http   color   os   io   strong   for   

原文地址:http://blog.csdn.net/u014303647/article/details/38413005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!