• UVALive


    题目链接https://vjudge.net/contest/244167#problem/F

    题目

    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
     
    题目大意:意思就是给你一个有符号int整数,让你拆成 n = a0 + a1 ∗p0 + a2 ∗p0 ∗p1 + a3 ∗p0 ∗p1 ∗p2 + ...这种形式,其中p0,p1,p2……,分别表示素数2 3 5……,输出见样例。
     
    解题思路:在计蒜客做过一题和这很类似的题,就是拆成上面那种形式,只不过改了下现在拆成下面这种形式,比赛的时候竟然都没去看。。。原理都差不多,就是贪心从最大的开始拆,能拆多少就拆多少,有多余的就是用小的来拆。不断的除和取模就OK了,因为int有符号整型太小了,好像不超过32767,我是先写个程序计算出了2,2*3,2*3*5……乘到9个或者10个就行了,这时候已经远远大于那个范围了,然后就是贪心了。输出的时候注意下就是了,系数为0就可以跳过。
     
    附上代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 using namespace std;
     6 int prime[15]={2,3,5,7,11,13,17,19,23};
     7 int b[15]={1,2,6,30,210,2310,30030,510510,9699690,223092870};
     8 int a[15];
     9 
    10 int main()
    11 {
    12     int n;
    13     while(cin>>n&&n)
    14     {
    15         int x=n;
    16         memset(a,0,sizeof(a));
    17         for(int i=9;i>=0;i--)
    18         {
    19             if(abs(x)>=b[i])
    20             {
    21                 a[i]=x/b[i];
    22                 x=x%b[i];
    23             }
    24         }
    25         printf("%d = ",n);
    26         int flag=0;
    27         if(a[0]!=0)
    28         {
    29             cout<<"1";
    30             flag=1;
    31         }
    32         for(int i=1;i<=9;i++)
    33         {
    34             if(a[i]!=0)
    35             {
    36                 if(flag) printf(" + ");
    37                 cout<<a[i];
    38                 for(int j=0;j<i;j++)
    39                     printf("*%d",prime[j]);
    40                 flag=1;
    41             }
    42         }
    43         cout<<endl;
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    js 注意
    JS学习大作业-Excel
    js继承
    转载:margin外边距合并问题以及解决方式
    CSS属性选择器和部分伪类
    HTML使用CSS样式的方法
    link元素 rel src href属性
    【2020.01.06】SDN大作业
    【2019.12.11】SDN上机第7次作业
    【2019.12.04】SDN上机第6次作业
  • 原文地址:https://www.cnblogs.com/zjl192628928/p/9430546.html
Copyright © 2020-2023  润新知