1059. Prime Factors (25)解题报告
2016-10-30 17:04
423 查看
#define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <climits> #include <cstring> using namespace std; struct node { long long factor; int exp; }; int main(void) { queue<node> q; long long num, nu, max; scanf("%lld", &num); nu = num; max = sqrt(num); bool *prime = new bool[max + 1]; memset(prime, 0, sizeof(bool) * (max + 1)); for (long long i = 2; i < max + 1; i++) { for (long long j = 2; j*i < max + 1; j++) { prime[j*i] = true; } } long long i = 2; while (num > 1) { while (i < max + 1 && prime[i]) { i++; } if (i == max + 1) { break; } if (!(num % i)) { node n; n.factor = i; n.exp = 0; while (!(num % i)) { n.exp++; num /= i; } q.push(n); } i++; } if (i == max + 1 && num >1) { node n; n.factor = num; n.exp = 1; q.push(n); } if (!q.size()) { printf("%lld=%d", num, num); return 0; } node tmp = q.front(); q.pop(); printf("%lld=", nu); if (tmp.exp > 1) { printf("%lld^%d", tmp.factor, tmp.exp); } else { printf("%lld", tmp.factor); } while (!q.empty()) { tmp = q.front(); q.pop(); if (tmp.exp > 1) { printf("*%lld^%d", tmp.factor, tmp.exp); } else { printf("*%lld", tmp.factor); } } putchar('\n'); delete[] prime; return 0; }
相关文章推荐
- 剑指offer解题报告(Java版)——二叉树中和为某一值的路径 25
- 1063. Set Similarity (25)解题报告
- 1071. Speech Patterns (25)解题报告
- 1074. Reversing Linked List (25)解题报告
- 1089. Insert or Merge (25)解题报告
- 1097. Deduplication on a Linked List (25)解题报告
- Family Property (25)解题报告
- PAT (Advanced Level) 1002. A+B for Polynomials (25) 解题报告
- COGS 25. [NOIP2007] 守望者的逃离 解题报告
- 1082. Read Number in Chinese (25)解题报告
- 1085. Perfect Sequence (25)解题报告
- 1098. Insertion or Heap Sort (25)解题报告
- 1106. Lowest Price in Supply Chain (25)解题报告
- PAT (Advanced Level) 1003. Emergency (25) 解题报告
- CDOJ 25 点球大战(penalty) 解题报告
- 1066. Root of AVL Tree (25)解题报告
- 1083. List Grades (25)解题报告
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
- [BestCoder Round #25 1002]Harry And Magic Box 解题报告
- 1067. Sort with Swap(0,*) (25)解题报告