Description
Input
Output
Sample Input
17 1 5 1 2 1 509 1 59 1 0
Sample Output
2 4 3 2 13 1 11 1 7 1 5 1 3 1 2 1
/*0ms,116KB*/ #include<cstdio> #include<cstring> #include<cmath> char s[24];///刚好 int base[6], index[6];///刚好 int main(void) { while (gets(s), s[0] != '0') { memset(base, 0, sizeof(base)); memset(index, 0, sizeof(index)); int temp = 0, count = 0; bool which = true; for (int i = 0; s[i]; ++i) { if (s[i] != ' ') temp = temp * 10 + s[i] - '0'; else { if (which) base[count] = temp; else index[count++] = temp; which = (which ? false : true); temp = 0; } } ///最后s[i] == '\0'退出 index[count++] = temp; int ans = 1; for (int i = 0; i < count; ++i) for (int j = 0; j < index[i]; ++j) ans *= base[i];///整数的幂运算最好用for代替! --ans; count = 0; int sqrtans = (int)sqrt((double)ans); for (int i = 2; i <= sqrtans; ++i) { if (ans % i == 0) { index[count] = 0; base[count] = i; while (ans % i == 0) { ++index[count]; ans /= i; } count++; } } if (ans > 1) ///无法分解 { base[count] = ans; index[count++] = 1; } ///逆序输出 for (int i = count - 1; i > 0; --i) printf("%d %d ", base[i], index[i]); printf("%d %d\n", base[0], index[0]); } return 0; }
先打表的方法:
/*16ms,128KB*/ #include<cstdio> #include<cstring> #include<cmath> char s[24];///刚好 int base[6], index[6], prime[43], c = 0; bool vis[192]; inline void create_prime() { for (int i = 2; i <= 191; ++i) if (!vis[i]) { prime[c++] = i; for (int j = i * i; j <= 191; j += i) vis[j] = true; } } int main(void) { create_prime(); while (gets(s), s[0] != '0') { memset(base, 0, sizeof(base)); memset(index, 0, sizeof(index)); int temp = 0, count = 0; bool which = true; for (int i = 0; s[i]; ++i) { if (s[i] != ' ') temp = temp * 10 + s[i] - '0'; else { if (which) base[count] = temp; else index[count++] = temp; which = (which ? false : true); temp = 0; } } ///最后s[i] == '\0'退出 index[count++] = temp; int ans = 1; for (int i = 0; i < count; ++i) for (int j = 0; j < index[i]; ++j) ans *= base[i];///整数的幂运算最好用for代替! --ans; count = 0; int sqrtans = (int)sqrt((double)ans); for (int i = 0; prime[i] <= sqrtans; ++i) { if (ans % prime[i] == 0) { index[count] = 0; base[count] = prime[i]; while (ans % prime[i] == 0) { ++index[count]; ans /= prime[i]; } count++; } } if (ans > 1) ///无法分解 { base[count] = ans; index[count++] = 1; } ///逆序输出 for (int i = count - 1; i > 0; --i) printf("%d %d ", base[i], index[i]); printf("%d %d\n", base[0], index[0]); } return 0; }