UVALive - 4225(贪心)
题目链接:https://vjudge.net/contest/244167#problem/F
题目:
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.
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.
#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(贪心)的更多相关文章
- UVALive 4225 Prime Bases 贪心
Prime Bases 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&a ...
- UVALive 4225 / HDU 2964 Prime Bases 贪心
Prime Bases Problem Description Given any integer base b >= 2, it is well known that every positi ...
- UVALive - 3266 (贪心) 田忌赛马
耳熟能详的故事,田忌赛马,第一行给出田忌的马的速度,第二行是齐王的马的速度,田忌赢一场得200,输一场失去200,平局不得也不失,问最后田忌最多能得多少钱? 都知道在故事里,田忌用下等马对上等马,中等 ...
- UVALive - 6434 (贪心)
题目链接:https://vjudge.net/problem/UVALive-6434 题意:给你n个数字,要你把这n个数字分成m组,每一组的消耗值定义为改组最大值和最小值之差,要求这m组的消耗值总 ...
- 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 ...
- 贪心 UVALive 6834 Shopping
题目传送门 /* 题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数 贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步 ...
- 贪心 UVALive 6832 Bit String Reordering
题目传送门 /* 贪心:按照0或1开头,若不符合,选择后面最近的进行交换.然后选取最少的交换次数 */ #include <cstdio> #include <algorithm&g ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- 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. ...
随机推荐
- java 工具
JClassLib 4.2 发布了,该版本支持 Java 7 和 Java 8 的类文件属性查看. JClassLib不但是一个字节码阅读器而且还包含一个类库允许开发者读取,修改,写入Java Cla ...
- node错误中间件处理 express类 带有路由操作
let express = require('express'); let app = new express(); let bodyParser = require('body-parser'); ...
- Oracle创建及调用存储过程
- 五句话搞定JavaScript作用域(ES5)
JavaScript的作用域一直以来是前端开发中比较难以理解的知识点,对于JavaScript的作用域主要记住几句话,走遍天下都不怕... 一.“JavaScript中无块级作用域” 在Java或C# ...
- Python Note1: Pycharm的安装与使用
前言 曾经学过一段时间python,虽然现在工作了主要使用C#和C++,但是觉得还是有必要在业余的时候学习学习python,提升下自己的知识面,毕竟技多不压身,加油吧! 安装与激活Pycharm 个人 ...
- mysql修改默认端口号后从windows命令行登录
mysql -u root -p -P 大写的P代表端口号,小写的p代表密码
- vue-cli3.0之vue.config.js的配置项(注解)
module.exports = { // 部署应用时的基本 URL baseUrl: process.env.NODE_ENV === 'production' ? '192.168.60.110: ...
- Could not render e, see the console.
错误截图: 解决: 在application.properties中开启swagger swagger2.enable=true
- const函数
1. const修饰成员函数:表示不可以修改成员变量 class test{ public: test(){ i_ = ; } int Get() const{ //i_ = 0; //error:不 ...
- Nginx 负载均衡一致性算法
一般Hash负载算法都是%算法 比如key-5 如果有5台服务器 那么5%5=0 那么请求将落在server 0 上,当有服务器宕机或者添加新服务器时,hash算法会引发大量路由更改,可能导致缓存大 ...