POJ 3993 Not So Flat After All (快速求质因子)

先贴上快速求质因子模板

#include <iostream>
#include <vector>
using namespace std;

void get(int n, vector<int>&a){
	a.clear();
	// i小于等于sqrt(n)的原因:
	// 对于任意的n来说,它的质因子比sqrt(n)大的最多只有一个
	for (int i = 2; i*i < n; i++){
		while (n%i == 0){
			a.push_back(i);
			n /= i;
		}
	}
	// 把比sqrt(n)大的因子添加进去
	if (n>1)a.push_back(n);
}

这道题题意很简单,直接贴代码

#include <iostream>
#include <set>
using namespace std;

set<int>sum;

void get(int x){
	for (int i = 2; i*i <= x; i++){
		while (x%i == 0){
			sum.insert(i);
			x /= i;
		}
	}
	if (x > 1){
		sum.insert(x);
	}
}

int main()
{
	int n, m, kase = 1;
	while (cin >> n >> m&&n&&m){
		sum.clear();
		get(n);
		get(m);
		int ans = 0;
		set<int>::iterator it = sum.begin();
		for (; it != sum.end(); it++){
			int a = 0, b = 0;
			int temp = *it;
			while (n%temp == 0)a++, n /= temp;
			while (m%temp == 0)b++, m /= temp;
			ans += (a > b ? a - b : b - a);
		}
		cout << kase++ << ". " << sum.size() << ":" << ans << endl;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值