POJ 3993 Not So Flat After All

Not So Flat After All
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 356 Accepted: 151

Description

Any positive integer v can be written as p 1 a1*p 2 a2*...*p n an where p i is a prime number and ai ≥ 0. For example: 24 = 2 3*3 1
Pick any two prime numbers p 1 and p 2 where p 1 = p 2. Imagine a two dimensional plane where the powers of p 1 are plotted on the x-axis and the powers of p 2 on the y-axis. Now any number that can be written as p 1 a1*p 2 a2 can be plotted on this plane at location (x, y) = (a 1, a 2). The figure on the right shows few examples where p 1 = 3 and p 2 = 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 (a i ≥ 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  \subset 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

Source


题目意思,求两个数的 质因数种数和  与  每种质因数个数的差的和    的比

解题思路: 水题,快速求质因数。

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
using namespace std;

const int maxn=1000000;

vector <int> prime;
bool isprime[maxn+10];
int a,b,casen=0;

void ini(){
	for(int i=0;i<=maxn;i++) isprime[i]=true;
	isprime[0]=false;isprime[1]=false;
	for(int i=4;i<=maxn;i+=2) isprime[i]=false;
	for(int i=3;i<=maxn;i+=2){
		for(int j=i;j<=maxn/i;j+=2){
			isprime[i*j]=false;
		}
	}
	for(int i=2;i<=maxn;i++){
		if(isprime[i]) prime.push_back(i);
	}
	//for(int i=0;i<=100;i++) cout<<prime[i]<<endl;
	//cout<<prime.size()<<endl;
}

void computing(){
	int ans=0;
	map <int,int> mpa,mpb;
	set <int>::iterator it;
	set <int> mys;
	for(int i=0;i<prime.size();i++){
		while(a%prime[i]==0){
			mpa[prime[i]]++;
			a=a/prime[i];
			mys.insert(prime[i]);
		}
		if(a==1) break;
		if(isprime[a]){
			mpa[a]++;
			mys.insert(a);
			break;
		}
	}
	for(int i=0;i<prime.size();i++){
		while(b%prime[i]==0){
			mpb[prime[i]]++;
			b=b/prime[i];
			mys.insert(prime[i]);
		}
		if(b==1) break;
		if(isprime[b]){
			mpb[b]++;
			mys.insert(b);
			break;
		}
	}
	for(it=mys.begin();it!=mys.end();it++){
		ans+=abs(mpa[*it]-mpb[*it]);
	}
	cout<<casen<<". "<<mys.size()<<":"<<ans<<endl;
}

int main(){
	ini();
	while(cin>>a>>b && (a||b)){
		casen++;
		computing();
	}
	return 0;
}


转载于:https://www.cnblogs.com/toyking/p/3797380.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值