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 ...
随机推荐
- day7_python学习笔记_chapter9_文件
1. open(), file(), 作用完全相同 2. 语法: file_object = open(file_name, access_mode='r', buffering='-1') acce ...
- 去掉xcode编译warning:ld: warning: directory not found for option '
tyle="margin:20px 0px 0px; font-size:14px; line-height:26px; font-family:Arial; text-align:left ...
- asp.net 多站点共享StateServer Session
<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" ...
- js动态创建样式: style 和 link
js动态创建样式: style 和 link ie6 不能 document.createElement('style') 然后append到head标签里.所以就找到这样个好文章 有很多提供动态创建 ...
- Developing your first FNC custom control
Friday, May 13, 2016 Some weeks ago, we released the TMS FNC UI Pack, a set of Framework Neutral Com ...
- [置顶] ※数据结构※→☆线性表结构(list)☆============单向链表结构(list single)(二)
单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始:链表是使用指针进行构造的列表:又称为结点列表,因为链表是由一个个结点组装起来的:其中每个结点都有指 ...
- android技术牛人的博客[转]
Android+JNI调用–文件操作 开发环境:Windows xp sp3 +MyEclipse 8.6+android2.3.3+jdk1.6+android-ndk-r6b JNI概述: ...
- ubuntu 14.04设备flash媒体播放器
今天是2014年4一个月19日本,就在两天前公布 ubuntu 14.04版本号, 以今天的优势是星期六,西安小雨,所以折腾linux. 我是个有点linux基础的小白.说是有些基础是由于以前在上大学 ...
- vmware虚拟机上linux操作系统进行tty1~tty6切换方法和具体步骤
vmware虚拟机上linux操作系统怎样进行tty1~tty6切换? 现象: Linux的终端机(文字)界面与图形界面间的切换热键为: 进入终端机也就是字符界面(tty1-tty6):[Ctrl] ...
- Uploadif稍做扩展使用
文章出自Uploadify扩展配置使用http://www.wuyinweb.com/doc/52/57.aspx 在做项目中涉及多文件上传,经过筛选,选择了Uploaidify,但还涉及一个问题,就 ...