题目链接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. Latex常用

    插入罗马数字 \newcommand{\RNum}[1]{\uppercase\expandafter{\romannumeral #1\relax}} 然后在正文里面就可以用\RNum{}来添加罗马 ...

  2. Vue 中提示报错 handlers[i].call is not a function解决方法

    Vue 中提示警告 TypeError: handlers[i].call is not a function at callHook (vue.esm.js?a026:2921) at Object ...

  3. Codeforces 1154F Shovels Shop

    题目链接:http://codeforces.com/problemset/problem/1154/F 题目大意: 商店有n把铲子,欲购k把,现有m种优惠,每种优惠可使用多次,每种优惠(x, y)表 ...

  4. springboot的几种启动方式

    一:IDE 运行Application这个类的main方法 二:在springboot的应用的根目录下运行mvn spring-boot:run 三:使用mvn install 生成jar后运行 先到 ...

  5. JMeter 连接 sql server

    1.安装驱动 http://www.microsoft.com/zh-CN/download/details.aspx?id=11774 下载后解压后复制sqljdbc.jar到 “jmeter的安装 ...

  6. 初识Anrdiod SDK

    概念 SDK:(software development kit)软件开发工具包.被软件开发工程师用于为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件的开发工具的集合. 因此,Android ...

  7. 【python练习题】程序9

    #题目:暂停一秒输出. import time for i in range(5): print (i) time.sleep(1)

  8. RBS SharePoint 2010 Server.wmv

    视频地址: https://www.youtube.com/watch?v=DXi2er514iA&feature=youtu.be

  9. OneinStack——PHP多版本共存

    前言 我事先安装的是LNMP环境,PHP版本为7.2,但是现在环境需要一个PHP5.6,所以就准备安装个上版本,顺带写个安装教程,写完后我发现了原来有直接安装的命令!所以后面的内容大家可以忽略了!从配 ...

  10. 洛谷P2678跳石头题解

    题目 这个题也是一个很经典的题了.其主要思想也是二分答案,原因就是题目中只要出现最大值最小或最小值最大,这种描述十有八九就是二分答案. 这个题原题也是让我们求最短的跳跃距离的最大值. 显而易见,最大值 ...