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 = 1
23 + 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");
} }

UVALive 4225 Prime Bases 贪心的更多相关文章

  1. UVALive 4225 / HDU 2964 Prime Bases 贪心

    Prime Bases Problem Description Given any integer base b >= 2, it is well known that every positi ...

  2. UVALive - 4225(贪心)

    题目链接:https://vjudge.net/contest/244167#problem/F 题目: Given any integer base b ≥ 2, it is well known ...

  3. UVALive 6912 Prime Switch 暴力枚举+贪心

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  4. UVALive 6912 Prime Switch 状压DP

    Prime Switch 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  5. hdu 2964 Prime Bases(简单数学题)

    按照题意的要求逐渐求解: #include<stdio.h> #include<string.h> #include<algorithm> using namesp ...

  6. UVALive 7464 Robots (贪心)

    Robots 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/K Description http://7xjob4.com1.z ...

  7. uvalive 3971 - Assemble(二分搜索 + 贪心)

    题目连接:3971 - Assemble 题目大意:有若干个零件, 每个零件给出的信息有种类, 名称, 价格, 质量,  现在给出一个金额, 要求在这个金额范围内, 将每个种类零件都买一个, 并且尽量 ...

  8. UVALive - 6912 Prime Switch (状压DP)

    题目链接:传送门 [题意]有n个灯,m个开关,灯的编号从1~n,每个开关上有一个质数,这个开关同时控制编号为这个质数的倍数的灯,问最多有多少灯打开. [分析]发现小于根号1000的质数有10个左右,然 ...

  9. UVALive 4867 Maximum Square 贪心

    E - Maximum Square Time Limit:4500MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. Storage Keepers

    题意: n个仓库,m个人申请看管仓库,一个人可以看管多个仓库,一个仓库只能被一个人看管,每个人都有一个能力值,他看管的仓库的安全度U是能力值/看管仓库数,安全线L是U中的最小值,有多少能力公司发多少工 ...

  2. erase() 返回的是删除此元素之后的下一个元素的迭代器 .xml

    pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...

  3. git初步使用

    git初步使用 主要目的:使用代码控制工具,练习使用git 1.创建新项目 网址如下: https://github.com/kellyseeme?tab=repositories 注意每个人使用的名 ...

  4. Inverse是hibernate双向关系中的基本概念。inverse的真正作用就是指定由哪一方来维护之间的关联关系。当一方中指定了“inverse=false”(默认),那么那一方就有责任负责之间的关联关系,说白了就是hibernate如何生成Sql来维护关联的记录

    <set name ='students' table="students_table" inverse='false'(默认不用写) > <key column ...

  5. CodeForces 622 A.Infinite Sequence

    A.Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN -摘自网络

    在Membership系列的最后一篇引入了ASP.NET Identity,看到大家对它还是挺感兴趣的,于是来一篇详解登录原理的文章.本文会涉及到Claims-based(基于声明)的认证,我们会详细 ...

  7. [转] 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文

    转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/9219333 首先得描述一下各个证书的定位,作用,这 ...

  8. mysql基础知识(5)--视图

    视图 单词:view 什么是视图: 视图可以看作是一个“临时存储的数据所构成的表”(非真实表),其实本质上只是一个select语句.只是将该select语句(通常比较复杂)进行一个“包装”,并设定了一 ...

  9. 第一百九十四天 how can I坚持

    该挺妈妈话的,不该买可乐,该熬点粥喝,肚子疼,救我. 好像每天都一样,每天都在重复.. 哎.. 对了,买了点花种子,铜钱草,牡丹.玫瑰.还买了棵小多肉. 还有,老妈把咸菜给邮过来了,有点期待啊. 连续 ...

  10. 第二百三十四天 how can I 坚持

    今天果然不负众望,下了一天的雪啊,挺好. 今天把花搞了下,都弄花盆里了,希望不会就这么挂掉.八千代,绿萝,还有小叶元宝. 中午喝了点酒,没感觉. 过两天气温就零下十多度了,该咋办啊,最怕冬天.家里现在 ...