hdu2964-Prime Bases
http://acm.hdu.edu.cn/showproblem.php?pid=2964
题意,给你一个整数n,现在要你分解成 n = k1 * ( 2 * 3 * ....*x1 ) + k2 * ( 2 * 3 * .... *x2 ) + ........;其中后面均为素数,且是由最小的2递增相乘;
分析:首先打印素数表;然后使用数组a【】来储存到从第一个素数2到第几个素数乘积。找到第i个小于n,第i +1个大于n的数值 i ;
然后分解n.使用数组b【】来储存a【i】的个数;同理执行多次,知道将n分解完毕;
如下的这种输出格式比较好,大家可以参考下;
参照做的;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<iomanip> using namespace std;
const int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,39,41,43,47,51,53,57,59};
__int64 a[21];
int b[21],rem; void init( )
{
a[ 0 ] = 1 ;
for( int i =1 ; i < 21 ; ++i )
a[ i ] = a[ i - 1 ] * prime[ i - 1 ] ;
} void deal( int n )
{
int i ;
for( int i = 0 ; i < 21 ; ++i )
{
if( a[ i ] <= n && a[ i + 1 ] > n )
{
rem = i ;
break ;
}
}
memset( b , 0 ,sizeof( b ) ) ;
for( int i = rem ; i >=0 ; --i )
{
b[ i ] = n / a[ i ] ;
n %= a[ i ] ;
}
} void output( int n )
{
cout << n << " = " ;
for( int i = 0 ; i <= rem ; ++i )
{ if( b[ i ] )
{
cout << b[ i ] ;
for( int j = 0 ; j < i ; ++j )
cout << "*" << prime[ j ] ;
if( i != rem )
cout << " + " ;
}
}
cout << endl ;
} int main( )
{
init( ) ;
int n ;
while( cin >> n , n )
{
deal( n ) ;
output( n );
}
return 0 ;
}
hdu2964-Prime Bases的更多相关文章
- UVALive 4225 Prime Bases 贪心
Prime Bases 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&a ...
- UVALive 4225 / HDU 2964 Prime Bases 贪心
Prime Bases Problem Description Given any integer base b >= 2, it is well known that every positi ...
- hdu 2964 Prime Bases(简单数学题)
按照题意的要求逐渐求解: #include<stdio.h> #include<string.h> #include<algorithm> using namesp ...
- Java 素数 prime numbers-LeetCode 204
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Prime Generator
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- UVa 524 Prime Ring Problem(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- hdu 5901 count prime & code vs 3223 素数密度
hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...
- 最小生成树 prime zoj1586
题意:在n个星球,每2个星球之间的联通需要依靠一个网络适配器,每个星球喜欢的网络适配器的价钱不同,先给你一个n,然后n个数,代表第i个星球喜爱的网络适配器的价钱,然后给出一个矩阵M[i][j]代表第i ...
随机推荐
- Linux学习之linux目录
文件系统的类型 LINUX有四种基本文件系统类型:普通文件.目录文件.连接文件和特殊文件,可用file命令来识别. 普通文件:如文本文件.C语言元代码.SHELL脚本.二进制的可执行文件等,可用cat ...
- 机器学习算法实现(R&Python code)
Machine Learning Algorithms Machine Learning Algorithms (Python and R) 明天考试,今天就来简单写写机器学习的算法 Types Su ...
- eclipse注解快捷键
Search 功能:全局文件内容搜索快捷键: Ctrl + H -------------------------------------------------------------------- ...
- 如何重启MySQL服务,正确重启mysql
RedHat Linux (Fedora Core/Cent OS) 1.启动:/etc/init.d/mysqld start 2.停止:/etc/init.d/mysqld stop 3.重启:/ ...
- 解析Tensorflow官方PTB模型的demo
RNN 模型作为一个可以学习时间序列的模型被认为是深度学习中比较重要的一类模型.在Tensorflow的官方教程中,有两个与之相关的模型被实现出来.第一个模型是围绕着Zaremba的论文Recurre ...
- jasmine note
jasmine 简介 jasmine 是一个测试驱动开发(TDD)测试框架, 一个js测试框架,它不依赖于浏览器.dom或其他js框架 jasmine有十分简洁的语法 使用 从 这里 下载 stant ...
- IBM HeapAnalyzer
https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/W3b463571efc8_4f02_99af_3cbc0 ...
- Vim 实用技术,第 1 部分: 实用技巧(转)
原文链接:http://blog.jobbole.com/20604/ 0. Vim 简介 作为开源世界最重要的编辑器之一(另一个是 Emacs),Vim 以其强大的功能和可定制能力被众多开发者所喜爱 ...
- 随手写了一个linux服务端与window客户端的epoll程序,当做练习把。
linux服务端:监听链接,处理消息 #include <sys/socket.h> #include <sys/epoll.h> #include <n ...
- CodeForces 22C System Administrator
把v和2结点交换, 1和v连,其它点和v之间能够互相连. #include <iostream> #include <cstdlib> #include <cstring ...