题目链接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 工具

    JClassLib 4.2 发布了,该版本支持 Java 7 和 Java 8 的类文件属性查看. JClassLib不但是一个字节码阅读器而且还包含一个类库允许开发者读取,修改,写入Java Cla ...

  2. node错误中间件处理 express类 带有路由操作

    let express = require('express'); let app = new express(); let bodyParser = require('body-parser'); ...

  3. Oracle创建及调用存储过程

  4. 五句话搞定JavaScript作用域(ES5)

    JavaScript的作用域一直以来是前端开发中比较难以理解的知识点,对于JavaScript的作用域主要记住几句话,走遍天下都不怕... 一.“JavaScript中无块级作用域” 在Java或C# ...

  5. Python Note1: Pycharm的安装与使用

    前言 曾经学过一段时间python,虽然现在工作了主要使用C#和C++,但是觉得还是有必要在业余的时候学习学习python,提升下自己的知识面,毕竟技多不压身,加油吧! 安装与激活Pycharm 个人 ...

  6. mysql修改默认端口号后从windows命令行登录

    mysql -u root -p -P 大写的P代表端口号,小写的p代表密码

  7. vue-cli3.0之vue.config.js的配置项(注解)

    module.exports = { // 部署应用时的基本 URL baseUrl: process.env.NODE_ENV === 'production' ? '192.168.60.110: ...

  8. Could not render e, see the console.

    错误截图: 解决: 在application.properties中开启swagger swagger2.enable=true

  9. const函数

    1. const修饰成员函数:表示不可以修改成员变量 class test{ public: test(){ i_ = ; } int Get() const{ //i_ = 0; //error:不 ...

  10. Nginx 负载均衡一致性算法

    一般Hash负载算法都是%算法 比如key-5 如果有5台服务器 那么5%5=0  那么请求将落在server 0 上,当有服务器宕机或者添加新服务器时,hash算法会引发大量路由更改,可能导致缓存大 ...