UVALive 4225 / HDU 2964 Prime Bases 贪心
Prime Bases
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.
456
123456
0
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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<vector>
using namespace std ;
typedef long long ll; const int N = + ;
const int mod = 1e9 + ;
ll n;
int a[] = {,,,,,,,,,,,,,};
ll ans[N];
ll sum[];
int main() {
sum[] = ;
for(int i = ; i <= ; i++) sum[i] = sum[i-] * a[i];
while(~scanf("%lld",&n)) {
if(!n) break;
int cool = ;
printf("%lld = ",n);
memset(ans,,sizeof(ans));
for(int i = ; i >= ; i--) {
if(n / sum[i]) {
ll tmp = n / sum[i];
n = n % sum[i];
ans[i] = tmp;
cool++;
}
}
int f = ;
if(n) printf(""),f = ;
for(int i = ; i <= ; i++) {
if(ans[i]) {
cool--;
if(f)printf(" + "),f=;
printf("%lld",ans[i]);
for(int j = ; j <= i; j++) printf("*%d",a[j]);
if(cool!=) printf(" + "); }
}
printf("\n");
}
return ;
}
UVALive 4225 / HDU 2964 Prime Bases 贪心的更多相关文章
- hdu 2964 Prime Bases(简单数学题)
按照题意的要求逐渐求解: #include<stdio.h> #include<string.h> #include<algorithm> using namesp ...
- UVALive 4225 Prime Bases 贪心
Prime Bases 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&a ...
- HDU 4442 Physical Examination(贪心)
HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- UVALive - 4225(贪心)
题目链接:https://vjudge.net/contest/244167#problem/F 题目: Given any integer base b ≥ 2, it is well known ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- HDU 1016 Prime Ring Problem
在刚刚写完代码的时候才发现我以前交过这道题,可是没有过. 后来因为不理解代码,于是也就不了了之了. 可说呢,那时的我哪知道什么DFS深搜的东西啊,而且对递归的理解也很肤浅. 这道题应该算HDU 261 ...
- HDU 5835 Danganronpa (贪心)
Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...
随机推荐
- 分享几个可用的rtsp, http測试url
rtsp://218.204.223.237:554/live/1/0547424F573B085C/gsfp90ef4k0a6iap.sdp rtsp://218.204.223.237:554/l ...
- 通过setSystemUiVisibility实现状态栏跟Activity之间的位置关系
曾经说到去除状态栏和标题栏总会用到动态代码的方式实现: getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , Windo ...
- [POJ 1316] 树上的询问
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1316 [算法] 点分治 由于边权较大,笔者在计算时使用了STL-set 注意当询问为 ...
- TCP/IP协议族简介
OSI网络分层介绍 网络结构的标准模型是OSI模型,由国际互联网标准化组织定义的网络分层模型.虽然目前没有完全按照这种模型实现的网络协议栈,但是学习这个模型对于我们理解网络协议还是很有帮助的. 1.O ...
- 关于懒加载中的self.和_
---恢复内容开始--- 在开发中,经常会用到懒加载,最常用的如加载一个数组 如图,在这个懒加载数组中有的地方用到了_array有的地方用到了self.array 原因是_array是直接访问,而se ...
- SQL语句之WITH AS
一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到. 其实就是把一大堆 ...
- Session会在浏览器关闭后消失吗?
转 http://blog.csdn.net/rongwenbin/article/details/51784310 Cookie的两种类型 在项目开发中我们时常将需要在客户端(浏览器)缓存的数 ...
- Centos7 minimal 系列之Nginx负载均衡搭建(四)
一.Nginx搭建请参考我的上篇文章 http://www.cnblogs.com/WJ--NET/p/8143899.html 二.在IIS上搭建2个网站 三.配置nginx 虚拟机和主机网络互通请 ...
- 嵌入式Linux基础知识
一.构建嵌入式开发环境 1.编译bootloader并烧写到板子中---uboot, 可以自己定制bootloader and logo 2.编译file system 并烧写--内嵌APP 3.编译 ...
- tp5控制器调用,方法调用
<?php //命名空间 namespace app\index\controller; use app\admin\controller\Deer; class User{ public ...