hdu 2964 Prime Bases

Prime Bases

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


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
  
  
123 456 123456 0

Sample Output
  
  
123 = 1 + 1*2 + 4*2*3*5 456 = 1*2*3 + 1*2*3*5 + 2*2*3*5*7 123456 = 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

Source

Recommend
gaojie
     题目不难。关键是输出格式控制。
#include <stdio.h>
#include<string.h>
int a[10]={1,2,3,5,7,11,13,17,19,23};//经估算。十位就够了。多了就溢出了
int b[10],num[10];

int main()
{
    int i,j,n,m,flag;
    b[0]=1;
    for(i=1;i<10;i++)
        b[i]=b[i-1]*a[i];//b[i]存前i个素数的乘积
        //for(i=0;i<=9;i++)
         //   printf("%d ",b[i]);
    while(scanf("%d",&n),n)
    {
        memset(num,0,sizeof num);
        m=n;
        flag=0;//flag用于判断是不是第一个数
        for(i=9;i>=0;i--)//从后往前计算
        {
            if(m>=b[i])
            {
                num[i]=m/b[i];//num[i]用于记录系数
                m%=b[i];//剩下的相同处理
            }
        }
        printf("%d =",n);
        for(i=0;i<=9;i++)
        {
            if(num[i]!=0)
            {
                if(flag!=0)
                printf(" +");
                printf(" %d",num[i]);
                for(j=1;j<=i;j++)
                    printf("*%d",a[j]);
                flag++;
            }
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值