题目链接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. Java 异常处理的误区和经验总结

    Java 异常处理的误区和经验总结   1 本文着重介绍了 Java 异常选择和使用中的一些误区,希望各位读者能够熟练掌握异常处理的一些注意点和原则,注意总结和归纳.只有处理好了异常,才能提升开发人员 ...

  2. K8S集群 NOT READY的解决办法 1.13 错误信息:cni config uninitialized

    今天给同事 一个k8s 集群 出现not ready了 花了 40min 才搞定 这里记录一下 避免下载 再遇到了 不清楚. 错误现象:untime network not ready: Networ ...

  3. IIS 使用 HTTP重定向 修改 默认主页

    1. 被自己坑死了 多了一个 正斜杠 浪费了好长好长的时间 2. 需求 直接输入ip地址 总是 弹出来 iis 的 welcome的界面 感觉很low 想要修改一下 曾经用过 reflesh 来修改过 ...

  4. Java中List集合去除重复数据的四种方法

    1. 循环list中的所有元素然后删除重复   public static List removeDuplicate(List list) { for ( int i = 0 ; i < lis ...

  5. Mybatis Dao层注解及XML组合Dao的开发方式

    mybatis可以用xml进行数据操作,也可以在dao层用注解的方式,也可以采取xml和dao层接口组合使用的方法.显然 ,后者更加简单. 实体类Student   package com.zhao. ...

  6. @Param注解

    关于mybatis的@Param注解和参数 引用 https://www.cnblogs.com/whisper527/p/6568028.html 薇飘意 1,使用@Param注解 当以下面的方式进 ...

  7. DTW动态时间规整

    参考: https://blog.csdn.net/raym0ndkwan/article/details/45614813

  8. idea的pom.xml中提示dependency‘’not found

    今天下午在更新svn上的项目到本地,发现pom文件中的如下依赖的version一直标红,鼠标放上去显示“dependency not found.” 同时检查了Maven Projects中该项目引入 ...

  9. 记录SSM框架项目迁移SpringBoot框架-----pom.xml的迁移

    第一步:迁移pom.xml文件(去除spring相关的依赖) SSM中的pom: <project xmlns="http://maven.apache.org/POM/4.0.0&q ...

  10. Python:matplotlib绘制线条图

    线型图是学习matplotlib绘图的最基础案例.我们来看看具体过程:  下面我们将两条曲线绘制到一个图形里:   可以看到这种方式下,两个线条共用一个坐标轴,并且自动区分颜色. plot方法的核心是 ...