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. IOS之UITableView——怎样刷新父页面的Cell

    问题:评论数同步 在社交相关的项目中常常有这种主页面,主列表的Cell中有赞数.评论数,详情页顶部也是相同的一个Cell,下部有评论列表,评论添加或降低.详情页的评论数随之改变,返回主列表,主列表的相 ...

  2. 0x27 A*

    终于完全了解A*到底是什么玩意儿了 对于当前的决策,选取当前花费+预估花费最小来拓展. 因为假如预估出现失误,那么很可能就会延伸到一个错误的决策点,而这个决策点偏偏就是ed,而由于预估失误,其他点的当 ...

  3. poj--2631--Roads in the North(树的直径 裸模板)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2389   Accepted: 117 ...

  4. springmvc-mvc:resource标签使用

    转自:http://www.cnblogs.com/gzulmc/p/6746174.html <!-- 配置静态资源 --><mvc:resources location=&quo ...

  5. Centos7 minimal 系列之Nginx负载均衡搭建(四)

    一.Nginx搭建请参考我的上篇文章 http://www.cnblogs.com/WJ--NET/p/8143899.html 二.在IIS上搭建2个网站 三.配置nginx 虚拟机和主机网络互通请 ...

  6. js通过经纬度计算两点之间的距离

    最近这几天在做地图的时候,获取到目的地经纬度和当前所在位置的经纬度,通过这几个参数,用js代码就能获取到这两点之间的直线距离: function (lat1, lng1, lat2, lng2) { ...

  7. Android 制作类似支付圆圈和打钩界面ProgressWheel

    首先要说明的是,制作圆圈旋转的效果并不是博主做的,是参照了github上的一个代码,只是在上面添加了修改,对其优化并增加了一个打钩的动画. 先来看下效果,1+的手机获取root权限真是难,没法录屏,只 ...

  8. SQL Server中怎样可以从SELECT语句的结果集中删除重复行

    首先要分析出现重复记录的原因,是不是有一些where条件没有加上,把该加的条件都加上如果还有结果集重复,考虑以下方法去重: 结果集中去除重复行可以使用函数[distinct]也可以使用分组语句[gro ...

  9. solarwind之network Atlas

    1.  连接密码为空,连接到Orion     2.  连接后如下图   3.  直接拖动节点即可进行绘制地图   4.  查看它的相关属性          

  10. hdu 1754 I Hate It【线段树】

    维护一个最大值 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...