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

来源:互联网 发布:c语言分布式服务框架 编辑:程序博客网 时间:2024/05/28 17:04

先贴上快速求质因子模板

#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
原创粉丝点击