题目链接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就可以跳过。
 
附上代码:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. using namespace std;
  6. int prime[]={,,,,,,,,};
  7. int b[]={,,,,,,,,,};
  8. int a[];
  9.  
  10. int main()
  11. {
  12. int n;
  13. while(cin>>n&&n)
  14. {
  15. int x=n;
  16. memset(a,,sizeof(a));
  17. for(int i=;i>=;i--)
  18. {
  19. if(abs(x)>=b[i])
  20. {
  21. a[i]=x/b[i];
  22. x=x%b[i];
  23. }
  24. }
  25. printf("%d = ",n);
  26. int flag=;
  27. if(a[]!=)
  28. {
  29. cout<<"";
  30. flag=;
  31. }
  32. for(int i=;i<=;i++)
  33. {
  34. if(a[i]!=)
  35. {
  36. if(flag) printf(" + ");
  37. cout<<a[i];
  38. for(int j=;j<i;j++)
  39. printf("*%d",prime[j]);
  40. flag=;
  41. }
  42. }
  43. cout<<endl;
  44. }
  45. return ;
  46. }

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. Velocity中判断是否为空

    方法一: 使用 #ifnull() 或 #ifnotnull() eg:#ifnull ($foo) 要使用这个特性必须在velocity.properties文件中加入: userdirective ...

  2. Java 简单的登录验证码

    1 验证码的作用 验证码是为了区分人与机器,如果没有验证码机制,web网站或者应用会遇到很多问题,具体如下: ① 网站容易被暴力登录攻破密码,可以制作一个自动程序不断的尝试登录,密码很容易被破解,系统 ...

  3. dart正则

    1.前言 API中对于正则表达式的注释是:正则表达式的规范和语义与JavaScript相同详细的规范可以参考:http://ecma-international.org/ecma-262/5.1/#s ...

  4. Hbase存储流程

  5. python学习笔记(10)--组合数据类型(字典类型)

    理解映射: 映射是一种键(索引)和值(数据)的对应.字典是键值对的集合,键值之间无序.用大括号表示{},和dict()创建,键值对用冒号:表示. {键:值,键:值,键:值} >>> ...

  6. StatefulSet

    StatefulSet: 1.稳点且唯一的网络标识符 2.稳点且持久的存储 3.有序.平滑的部署和扩展 4.有序.平滑的删除和终止 5.有序的滚动更新 三个组件组成:headless(无头服务)    ...

  7. 微信小程序wxml無法實現頁面跳轉的問題

    wxml的 navigator的url設置后無法跳轉? 檢查要跳轉的頁面是否是在APP.json的tabBar里註冊過,如果是tabBar頁面是不能用wx.navigateTo和wx.Redirect ...

  8. vue之v-for使用说明

    demo.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...

  9. CSS遮罩效果和毛玻璃效果

    前面的话 本文将详细介绍CSS遮罩效果和毛玻璃效果 遮罩效果 普通遮罩 一般地,处理全屏遮罩的方法是使用额外标签 <style>.overlay{ position:fixed; top: ...

  10. Web API 2 使用Entity Framework Part 1.

    创建项目 打开Visual Studio,选择ASP.NET Web Application,项目名称BookService并点击OK. 选择Web API 模板 如果你想使用Azure App Se ...