zoukankan      html  css  js  c++  java
  • UVALive 4225 / HDU 2964 Prime Bases 贪心

    Prime Bases

    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
     
    题意: 
      给你一个数:让你表示成 连续素数 下 乘系数  形式,列如  连续 素数 a*2*3*5  
      且满足a <= 5后面这个素数(7)-1
    题解:
      n的范围是32位
      我们打个表知道最多就是13位连续素数相乘
      我们从最大的开始,看n是否大于它,大于的话求出系数,记录答案,否则 向下接着如此判断
      注意边界
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include<vector>
    using namespace std ;
    typedef long long ll;
    
    const int  N = 100000 + 10;
    const int mod = 1e9 + 7;
    ll n;
    int a[20] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43};
    ll ans[N];
    ll sum[50];
    int main() {
        sum[0] = 2;
        for(int i = 1; i <= 13; i++) sum[i] = sum[i-1] * a[i];
        while(~scanf("%lld",&n))  {
            if(!n) break;
            int cool = 0;
            printf("%lld = ",n);
            memset(ans,0,sizeof(ans));
            for(int i = 13; i >= 0; i--) {
                if(n / sum[i]) {
                   ll  tmp = n / sum[i];
                    n = n % sum[i];
                    ans[i] = tmp;
                    cool++;
                }
            }
            int f = 0;
            if(n) printf("1"),f = 1;
            for(int i = 0; i <= 13; i++) {
                if(ans[i]) {
                    cool--;
                    if(f)printf(" + "),f=0;
                    printf("%lld",ans[i]);
                    for(int j = 0; j <= i; j++) printf("*%d",a[j]);
                    if(cool!=0) printf(" + ");
    
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    Spring Boot 缓存技术:Spring Boot
    Java基础之Iterable接口
    使用sqlyog连接 Mysql 出现1251错误
    IDEA更改主题插件——Material Theme UI详解
    免安装版的Mysql
    使用Nexus搭建Maven私服
    Spring之注解注入bean
    Idea springboot 配置热部署
    Spring Boot 异常处理与单元测试
    Ubuntu20.04在线安装VMware-Tools
  • 原文地址:https://www.cnblogs.com/zxhl/p/5152623.html
Copyright © 2011-2022 走看看