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 = + ;
const int mod = 1e9 + ;
ll n;
int a[] = {,,,,,,,,,,,,,};
ll ans[N];
ll sum[];
int main() {
sum[] = ;
for(int i = ; i <= ; i++) sum[i] = sum[i-] * a[i];
while(~scanf("%lld",&n)) {
if(!n) break;
int cool = ;
printf("%lld = ",n);
memset(ans,,sizeof(ans));
for(int i = ; i >= ; i--) {
if(n / sum[i]) {
ll tmp = n / sum[i];
n = n % sum[i];
ans[i] = tmp;
cool++;
}
}
int f = ;
if(n) printf(""),f = ;
for(int i = ; i <= ; i++) {
if(ans[i]) {
cool--;
if(f)printf(" + "),f=;
printf("%lld",ans[i]);
for(int j = ; j <= i; j++) printf("*%d",a[j]);
if(cool!=) printf(" + "); }
}
printf("\n");
}
return ;
}

UVALive 4225 / HDU 2964 Prime Bases 贪心的更多相关文章

  1. hdu 2964 Prime Bases(简单数学题)

    按照题意的要求逐渐求解: #include<stdio.h> #include<string.h> #include<algorithm> using namesp ...

  2. UVALive 4225 Prime Bases 贪心

    Prime Bases 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&a ...

  3. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  4. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  5. UVALive - 4225(贪心)

    题目链接:https://vjudge.net/contest/244167#problem/F 题目: Given any integer base b ≥ 2, it is well known ...

  6. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  8. HDU 1016 Prime Ring Problem

    在刚刚写完代码的时候才发现我以前交过这道题,可是没有过. 后来因为不理解代码,于是也就不了了之了. 可说呢,那时的我哪知道什么DFS深搜的东西啊,而且对递归的理解也很肤浅. 这道题应该算HDU 261 ...

  9. HDU 5835 Danganronpa (贪心)

    Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...

随机推荐

  1. 鸟哥Linux私房菜知识点总结3到5章

    感觉自己对Linux的理解一直不够,所以近期翻看了一本<鸟哥的Linux私房菜>.这是一本基础的书,万丈高楼平地起,会的不多但能够学.这是我整理的一些知识点,尽管非常基础.希望和大家共同交 ...

  2. 为什么网络银行不支持GNU/Linux操作系统下的浏览器操作

    当年Linux没出时.银行就開始信息化建设了. 所为信息化,就是指用计算机工作了.服务客户了. 顺带着,慢慢的建server,连网(内部网).外网(网上银行) 这样下来, unix, dos, win ...

  3. 上机题目(中级)- 用小数形式输出指定符号出现的频率 (Java)

    题目例如以下:

  4. 精美viso制图(1)

    office组件中的viso是一款十分强大的绘图工具,在绘制流程图.结构框图时显得十分方便,这里将我自己绘制的一些viso图(大部分都是用在我自己的论文中的)与大家分享一把. 1.深度学习训练流程图 ...

  5. Android圆角Tag控件的另类实现

    一般的圆角标签控件都是用xml设置shape做实现.可是假设我们想要做一个更加强大通用的的圆角控件,不须要使用者去关心圆角,仅仅设置背景就能够了. 应该怎么实现呢?这个就须要把背景先设置成图片,然后再 ...

  6. Redis通过命令行进行配置

    redis 127.0.0.1:6379[1]> config set requirepass my_redis OK redis 127.0.0.1:6379[1]> config ge ...

  7. 使用dbms_metadata.get_ddl遇到ORA-31603

    建了一个外部表,想看看这个表的信息,一查就报错了: SQL> select dbms_metadata.get_ddl('TABLE','ext_case1') from dual; ERROR ...

  8. 9.自己实现linux中的tree

    运行效果: 代码: #include <stdio.h> #include <unistd.h> #include <string.h> #include < ...

  9. Android 多个APK共享数据

    Android给每个APK进程分配一个单独的用户空间,其manifest中的userid就是对应一个Linux用户(Android 系统是基于Linux)的.所以不同APK(用户)间互相访问数据默认是 ...

  10. VB.net 捕获项目全局异常

    在项目中添加如下代码:新建窗口来显示异常信息. Namespace My '全局错误处理,新的解决方案直接添加本ApplicationEvents.vb 到工程即可 '添加后还需要一个From用来显示 ...