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. 2014秋C++ 第7周项目 数据类型和表达式

    课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在云学堂"贺老师课堂"同步展示,使用的帐号请到课 ...

  2. COCOS学习笔记--Cocod2dx内存管理(三)-Coco2d-x内存执行原理

    通过上两篇博客.我们对Cocos引用计数和Ref类.PoolManager类以及AutoreleasePool类已有所了解,那么接下来就通过举栗子来进一步看看Coco2d-x内存执行原理是如何的. / ...

  3. Struts2值栈的相关操作

    import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import ...

  4. Qt容器类的对象模型及应用(线性结构篇:对于QList来说,sharable默认是false的,但对于接下来讲的QVector来说,sharable默认是true)

    用Qt做过项目开发的人,肯定使用过诸如QList.QVector.QLinkList这样的模板容器类,它们虽然名字长的不同,但使用方法都大致相同, 因为其使用方法都大体相同,很多人可能随便拿一个容器类 ...

  5. 0x11 栈

    这个不难吧,算是常识了..毕竟也是刷过USACO的人 对顶栈这东西前几天才遇到过,好像和在线求中位数那东西放一起了吧 单调栈倒是没什么...贴个代码算了.一开始有点蠢的每个位置算,后来发现出栈再算就行 ...

  6. 将字符串序列化Object格式

    using Newtonsoft.Json; 首先引用  Newtonsoft.Json; 定义一个字符串 string str = "[{'ID':8.0,'PAGEID':201.0,' ...

  7. AMD cpu 下 Pytorch 多卡并行卡死问题解决

    dataparallel not working on nvidia gpus and amd cpus   https://github.com/pytorch/pytorch/issues/130 ...

  8. MSSQL执行大脚本文件时,提示“内存不足”的解决办法

    导出了一个脚本文件,将近900M,回来往sql studio一丢,报了个内存不足,然后就有了此文.. 问题描述: 当客户服务器不允许直接备份时,往往通过导出数据库脚本的方式来部署-还原数据库, 但是当 ...

  9. 通过配置host,自定义域名让本地访问

    最近服务器这块一直由我来维护,我们开发的项目很多域名根本没有解析,而是仅仅配置了host,就可以本地访问服务器了.感觉很有意思,于是乎,打算试一试.结果弄了许久,最后第二天才解决了.把这个艰辛的旅程记 ...

  10. Jq自定义的方法绑定树结构

    1.先上效果图  (借鉴博客) 2.这边不做样式的只做结构 function toTreeData(data) { var pos = {}; var tree = []; var i = 0; wh ...