博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVALive 4225 Prime Bases 贪心
阅读量:7003 次
发布时间:2019-06-27

本文共 2432 字,大约阅读时间需要 8 分钟。

Prime Bases

题目连接:

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
using namespace std;const int MAXN = 30;bool flag[MAXN];vector
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
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

转载地址:http://cqevl.baihongyu.com/

你可能感兴趣的文章
《程序员的自我修养》读书总结
查看>>
JavaScript之声明提升
查看>>
深入剖析Window
查看>>
WebSocket的原理及运行机制
查看>>
您有一份AndroidX升级指南未领取
查看>>
前端调试入门
查看>>
Linux也有后悔药,五种方案快速恢复你的系统
查看>>
一份Cocoapods支持多个target
查看>>
Swift 中的 String 和 Substring 如何工作
查看>>
iOS 之 Protocol 协议 委托 代理 详解
查看>>
FMDB 使用方法
查看>>
Git使用笔记
查看>>
初识Scrapy框架+爬虫实战(7)-爬取链家网100页租房信息
查看>>
XXL-CRAWLER v1.1.0 发布了
查看>>
Spring Boot 中使用 Java API 调用 Elasticsearch
查看>>
HTTP/3 来啦,你还在等什么?赶紧了解一下
查看>>
一个合格的Webpack4配置工程师素养:第二部分
查看>>
利用 EasyWeChat 和 ChatterBot 简单搭建一个公众号「自动回复机器人」
查看>>
关于RxJava在业务上的一些思考
查看>>
MySQL8.0 新特性:Partial Update of LOB Column
查看>>