题目链接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就可以跳过。
 
附上代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int prime[]={,,,,,,,,};
int b[]={,,,,,,,,,};
int a[]; int main()
{
int n;
while(cin>>n&&n)
{
int x=n;
memset(a,,sizeof(a));
for(int i=;i>=;i--)
{
if(abs(x)>=b[i])
{
a[i]=x/b[i];
x=x%b[i];
}
}
printf("%d = ",n);
int flag=;
if(a[]!=)
{
cout<<"";
flag=;
}
for(int i=;i<=;i++)
{
if(a[i]!=)
{
if(flag) printf(" + ");
cout<<a[i];
for(int j=;j<i;j++)
printf("*%d",prime[j]);
flag=;
}
}
cout<<endl;
}
return ;
}

UVALive - 4225(贪心)的更多相关文章

  1. UVALive 4225 Prime Bases 贪心

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

  2. UVALive 4225 / HDU 2964 Prime Bases 贪心

    Prime Bases Problem Description Given any integer base b >= 2, it is well known that every positi ...

  3. UVALive - 3266 (贪心) 田忌赛马

    耳熟能详的故事,田忌赛马,第一行给出田忌的马的速度,第二行是齐王的马的速度,田忌赢一场得200,输一场失去200,平局不得也不失,问最后田忌最多能得多少钱? 都知道在故事里,田忌用下等马对上等马,中等 ...

  4. UVALive - 6434 (贪心)

    题目链接:https://vjudge.net/problem/UVALive-6434 题意:给你n个数字,要你把这n个数字分成m组,每一组的消耗值定义为改组最大值和最小值之差,要求这m组的消耗值总 ...

  5. Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+贪心][2016 EC-Final Problem D]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  6. 贪心 UVALive 6834 Shopping

    题目传送门 /* 题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数 贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步 ...

  7. 贪心 UVALive 6832 Bit String Reordering

    题目传送门 /* 贪心:按照0或1开头,若不符合,选择后面最近的进行交换.然后选取最少的交换次数 */ #include <cstdio> #include <algorithm&g ...

  8. UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  9. UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)

    Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...

随机推荐

  1. react双组件传值和传参

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. array_filter与array_map

    php数组array_filter函数和array_slice函数:<?php /* array_filter()用回调函数过滤数组中的单元 array_filter(array,functio ...

  3. [转帖]再次提醒Google Chrome用户应尽快升级浏览器到72.0.3626.121

    再次提醒Google Chrome用户应尽快升级浏览器到72.0.3626.121 转帖地址: https://www.cnbeta.com/articles/tech/825591.htm 国内离线 ...

  4. 剑指offer(16)栈的压入、弹出序列

    题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈 ...

  5. 剑指offer(5)

    题目: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解法: 一个栈专门用来存数,当需要输出数时,把所有数倒到第二个栈,当然,若此时第二个栈中已经有数了(之前倒 ...

  6. git上传本地代码到github

      1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小 ...

  7. mvc后台可以使前台弹出警告框的几种方法

    1,引用博主的 第一种方案:在jsp页面使用java代码实现. 后台处理代码:(把要提示的数据放到session中.) String message = ""; message = ...

  8. NFS配置与搭建

    参考: Linux下NFS服务器的搭建与配置 https://www.cnblogs.com/liuyisai/p/5992511.html http://blog.51cto.com/hongten ...

  9. 从Oracle数据库中查询前几个月数据时需要注意的一些问题

    在最近的一个项目中,有一个需求就是要查询数据库中前几个月的历史数据,但是由于自己考虑不全面造成了程序的bug,现在将这一块好好作一个总结,希望以后不再犯这种很低级的错误,首先贴出查询中用到的一个子函数 ...

  10. Java多线程之线程状态转换图

    说明:线程共包括以下5种状态.1. 新建状态(New)         : 线程对象被创建后,就进入了新建状态.例如,Thread thread = new Thread().2. 就绪状态(Runn ...