Prime Bases

来源:互联网 发布:java scanner语句用法 编辑:程序博客网 时间:2024/05/21 10:05
Problem Description
Given any integer base b >= 2, it is well known that every positive integer n can be uniquely represented in base b. That is, we can write

n = a0 + a1*b + a2*b*b + a3*b*b*b + ... 

where the coefficients a0, a1, a2, a3, ... are between 0 and b-1 (inclusive).

What is less well known is that if p0, p1, p2, ... are the first primes (starting from 2, 3, 5, ...), every positive integer n can be represented uniquely in the "mixed" bases as:

n = a0 + a1*p0 + a2*p0*p1 + a3*p0*p1*p2 + ... 

where each coefficient ai is between 0 and pi-1 (inclusive). Notice that, for example, a3 is between 0 and p3-1, even though p3 may not be needed explicitly to represent the integer n.

Given a positive integer n, you are asked to write n in the representation above. Do not use more primes than it is needed to represent n, and omit all terms in which the coefficient is 0.
 

Input
Each line of input consists of a single positive 32-bit signed integer. The end of input is indicated by a line containing the integer 0.
 

Output
For each integer, print the integer, followed by a space, an equal sign, and a space, followed by the mixed base representation of the integer in the format shown below. The terms should be separated by a space, a plus sign, and a space. The output for each integer should appear on its own line.
 

Sample Input
1234561234560
 

Sample Output
123 = 1 + 1*2 + 4*2*3*5456 = 1*2*3 + 1*2*3*5 + 2*2*3*5*7123456 = 1*2*3 + 6*2*3*5 + 4*2*3*5*7 + 1*2*3*5*7*11 + 4*2*3*5*7*11*13
 
#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<iostream>#include<set>#include<map>#include<list>#include<queue>#include<stack>#include<vector>#include<string>#include<algorithm>using namespace std;#define LL long longLL a[12][2] = { { 1, 1 }, { 2, 2 }, { 3, 6 }, { 5, 30 }, { 7, 210 }, { 11, 2310 }, { 13, 30030 }, { 17, 510510 }, { 19, 9699690 }, { 23, 223092870 }, { 29, 6469693230 }, { 31, 200560490130 } };int main(){LL n;while (scanf("%lld", &n), n){int b[12];LL m = n;memset(b, 0, sizeof b);for (int i = 10; i >= 0; i--){b[i] = n / a[i][1];n = n%a[i][1];}int flag = 1;printf("%lld = ", m);for (int i = 0; i < 12; i++){if (b[i]&&flag){printf("%d", b[i]);for (int j = 1; j <= i; j++){printf("*%d", a[j][0]);}flag = 0;}else if (b[i]){printf(" + %d", b[i]);for (int j = 1; j <= i; j++){printf("*%d", a[j][0]);}}}printf("\n");}return 0;}


0 0