hdu 2964按奇数权展开

来源:互联网 发布:国家经济数据 编辑:程序博客网 时间:2024/05/18 06:28

Prime Bases

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 422    Accepted Submission(s): 208


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

hdu  2964

找到的合适的奇数权开始,从大到小逐个计算其系数。

1、题目给出n = a0 + a1*p0 + a2*p0*p1 + a3*p0*p1*p2 + ... ,说明对于输入的每一个n都可分解完,这是前提。

这取决于每个base的特点:1、2、2*3、2*3*5、2*3*5*7、2*3*5*7*11...。比如n=123,它的最大base为2*3*5,我们用n%(2*3*5)得到到值一定小于7!利用反证法,如果大于7,那么最大的base最小也得是2*3*5*7啦!这就证明了开始的处理是正确的。那么,我们再假设计算到第k个base时是正确的。我们会有经过第k个base处理

后的数据一定小于第k个base,假设第k个base为2*3*5*7*11,处理后n=(2*3*5*7)*(0---11)。这就说明了第k-1个base的处理也一定是正确的!

#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#define N 16001#define ll long longusing namespace std;const int prime[]={2,3,5,7,11,13,17,19,23,29,31,37};ll data[15];int b[15];ll n;void init(){    int i,j;    memset(data,1,sizeof(data));    data[0]=2;    for(i=1;i<13;i++)    {      data[i]=data[i-1]*prime[i];    }}int work(){    int i,j,k,temp;    for(i=0;i<11;i++)    {        if(data[i]<=n&&data[i+1]>n)        {            temp=i;            break;        }    }    for(i=temp;i>=0;i--)    {      b[i]=n/data[i];      n=n%data[i];    }    return temp;}int main(){    int i,j,k;    init();    while(scanf("%I64d",&n)&&n)    {        printf("%I64d = ",n);        k=work();        int f=0;        if(n>0)        {            printf("%d",n);            f=1;        }        for(i=0;i<=k;i++)        {          if(b[i]!=0)          {              if(f!=0)              {                  printf(" + ");              }              else              f=1;              printf("%d",b[i]);              for(j=0;j<=i;j++)              printf("*%d",prime[j]);          }        }        printf("\n");    }    return 0;}


原创粉丝点击