UVALive 4225 Prime Bases 贪心
题目连接:
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2226
Descriptionww.co
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 + 12 + 4235
456 = 123 + 1235 + 22357
123456 = 123 + 6235 + 42357 + 1235711 + 4235711*13
Hint
题意
给你一个数,你需要拆成素数因子的形式
比如123 = 1 + 1*2+4*2*3*5
拆成n = a0 + a1* p0 + a2* p0* p1 + a3* p0* p1* p2 + ...
的形式
给你一个数,问你怎么拆
题解:
贪心去拆就好了让,素数乘积从大到小不断考虑
如果超过就减去就好了
然后不断贪
代码
#include<bits/stdc++.h>
using namespace std;const int MAXN = 30;
bool flag[MAXN];
vector<int> P;
void GetPrime_1()
{int i, j;memset(flag, false, sizeof(flag));for (i = 2; i < MAXN; i++)if (!flag[i]){P.push_back(i);for (j = i; j < MAXN; j += i)flag[j] = true;}
}
string get(int p)
{string k;while(p){k+=(char)(p%10+'0');p/=10;}reverse(k.begin(),k.end());return k;
}
int main()
{GetPrime_1();long long n;while(cin>>n){if(n==0)break;long long ans = 1;for(int i=0;i<P.size();i++)ans*=P[i];long long pre = n;stack<int> S;for(int i=P.size()-1;i>=0;i--){S.push(n/ans);n=n%ans;ans/=P[i];}printf("%lld =",pre);int first = 1;if(n){printf(" 1");first = 0;}for(int i=0;i<P.size();i++){if(S.top()==0){S.pop();continue;}if(!first)printf(" +");first=0;printf(" %d",S.top());S.pop();for(int j=0;j<=i;j++){printf("*%d",P[j]);}}printf("\n");}}
转载于:https://www.cnblogs.com/qscqesze/p/5152412.html
UVALive 4225 Prime Bases 贪心相关推荐
- ARC080F - Prime Flip(贪心,差分,二分图匹配)
ARC080F - Prime Flip Solution 差分,转化为每次可以翻转i,j(j−i∈oddprime)i,j(j-i\in odd\;prime)i,j(j−i∈oddprime). ...
- UESTC 2014 Summer Training #19
A.UVALive 6161 去迟了,队友已经开始写了,应该是个水题,贴个队友代码 #include<iostream> #include<cstdio> #include&l ...
- UVALive 8513 lovers 2017 西安区域赛 B 贪心+multiset
UVALive 8513 有2种人,每个人有自己的权值$A_i$ $B_i$ 当$A_i + B_i >=K$时 两个人可以配对 问最多多少人可以配对 解法 : 把$/{ A_i /}$ 排序 ...
- UVA12542 LA6149 Prime Substring【筛选法+贪心】
Given a string of digits, your task is to find the largest prime number which presents in that strin ...
- UVALive 4850 Installations 贪心
题目链接 题意 工程师要安装n个服务,其中服务Ji需要si单位的安装时间,截止时间为di.超时会有惩罚值,若实际完成时间为ci,则惩罚值为max{0,ci-di}.从0时刻开始执行任务,问惩罚值最大 ...
- I - Defeat the Enemy UVALive - 7146 二分 + 贪心
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 3959 Rectangular Polygons (排序贪心)
Rectangular Polygons 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/G Description In thi ...
- UVALive 6849 Landmine Cleaner(贪心、扫雷经典游戏)
题意: 给定N,M≤1000的数字雷图,试恢复原来的雷图,输入保证答案唯一给定N,M\le 1000的数字雷图, 试恢复原来的雷图, 输入保证答案唯一 有雷的格子显示3+9宫格雷数,无雷的格子显示0+ ...
- UVALive - 4850 Installations 贪心+枚举
题目大意:工程师要安装n个任务,其中第i个服务Ji需要si单位的安装时间,截止时间为di,如果在截止时间之前完成任务,不会有任何惩罚,否则惩罚值为任务完成时间与截止时间之差,你的任务是让惩罚值最大的两 ...
最新文章
- 安装server 2012 时提示输入的密码不满足网络或组管理员设置的密码复杂度
- PayPal 开发详解(六):下载paypal立即付款SDK 并编译打包
- linux目录详解i-node,i-node节点与链接
- 5种方式将机器学习带到Java、Python以及Go等编程语言
- 致我唯一的kiyoumi
- 测试Live Writer Beta2功能
- eclipse、MyEclipse实现批量改动文件编码
- paip.JS的调试--DOM元素的属性与事件绑定
- android开发需要那些Java基础
- GIMP教程 4 图层
- python 合并word并生成目录_使用Python制作WORD报告
- java uuid to long,生成long类型的UUID
- java闰年_java 判断闰年
- 优化算法|MOAVOA:一种新的多目标人工秃鹰优化算法(Matlab代码实现)
- 图片怎么转换成pdf格式?这几个方法帮你一键转换
- prefetch()
- YbtOj#20073. 「NOIP2020 模拟赛 B 组 Day6」钻石守卫
- linux 时间与bios,Linux操作系统时间与BIOS硬件时间
- 关于网站架构设计的知识
- wfilters小波滤波器