poj 1365 Prime Land
一开始被输入格式卡住了,水题一道。。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<algorithm> 8 #include<map> 9 #include<iomanip> 10 #include<climits> 11 #include<string.h> 12 #include<numeric> 13 #include<cmath> 14 #include<stdlib.h> 15 #include<vector> 16 #include<stack> 17 #include<set> 18 #define INF 1e7 19 #define MAXN 100010 20 #define maxn 1000010 21 #define Mod 1000007 22 #define N 1010 23 using namespace std; 24 typedef long long LL; 25 26 int prime[32000]; 27 int vis[32000]; 28 void run() 29 { 30 int k = 1; 31 for (int i = 2; i <= 32000; ++i) 32 if (!vis[i]) { 33 prime[k++] = i; 34 for (int j = i*i; j <= 32000; j += i) 35 vis[j] = 1; 36 } 37 /*for (int i = 1; i < k; ++i) 38 cout << prime[i] << " "; 39 cout << endl;*/ 40 } 41 42 int n, m; 43 int p[33000], e[33000]; 44 int index, flag, cnt; 45 int main() 46 { 47 run(); 48 while (1) { 49 index = 0; 50 flag = 0; 51 while (1) { 52 cin >> n; 53 if (n == 0) { 54 flag = 1; 55 break; 56 } 57 cin >> m; 58 p[index] = n, e[index++] = m; 59 char ch = getchar(); 60 if (ch == '\n') break; 61 } 62 if (flag == 1) break; 63 LL num = 1; 64 for (int i = 0; i < index; ++i) { 65 num *= pow(p[i], e[i]); 66 } 67 num--; 68 int k = 2; 69 cnt = 0; 70 memset(p, 0, sizeof(p)); 71 memset(e, 0, sizeof(e)); 72 while (num != 1) { 73 while (num % k == 0) { 74 p[cnt] = k; 75 num /= k; 76 e[cnt]++; 77 } 78 k++; 79 cnt++; 80 } 81 for (int i = cnt - 1; i >= 0; --i) if (p[i]){ 82 if (i != cnt - 1) cout << " "; 83 cout << p[i] << " " << e[i]; 84 85 } 86 puts(""); 87 } 88 return 0; 89 }