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 ...
随机推荐
- Cocos2d-x学习资源集锦+有奖抽楼活动
大家好,事实上我是这个游戏开发版块的新任版主之中的一个,可能大家的焦点都在candycat1992女版主身上,所以我认为我应该冒个泡. 俗话说,新版主上任,三把"水"(是你自己说的 ...
- <LeetCode OJ> 100. Same Tree
100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...
- USACO 1.4 Mother's Milk
Mother's Milk Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numb ...
- Python笔记(三)
# -*- coding:utf-8 -*- # 运算符 a,b=10,20 # 算术运算符:包括+.-.*./.%.**.//运算 print "********************1 ...
- 关于Android对话框简单实用方法总结
要显示一个对话框,首先需要在xx.xml下添加一个Button按钮,并添加一个对应id. 单次点击事件对话框: button.setOnClickListener(new View.OnClickLi ...
- PHP魔术方法__clone()篇
PHP中定义了一个内置方法__clone()来调整兑现的克隆行为: 当一个对象被克隆的时候会自动执行__clone()方法,而复制的对象可以在其方法内进行调整 header('Content-type ...
- day07 分支,循环
目录 if(分支) if的语法 if...else... if...elif...else if的嵌套 for循环 for-else 语句 for循环的嵌套(重要) range介绍 while循环 w ...
- EL表达式的作用与限制条件
限制条件 只能访问域对象的数据 用法 访问基本数据类型 首先把数据保存在域对象中 pagecontext.setAttribute("name","eric") ...
- java开发移动端之spring的restful风格定义
https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/index.html
- python链接mysql数据库
1.安装pycharm python3.6 pip 在windows+R cmd where pip pip install mysql-client 如何看自已mysql-client有没有 ...