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的更多相关文章

  1. UVALive 4225 Prime Bases 贪心

    Prime Bases 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&a ...

  2. UVALive 4225 / HDU 2964 Prime Bases 贪心

    Prime Bases Problem Description Given any integer base b >= 2, it is well known that every positi ...

  3. hdu 2964 Prime Bases(简单数学题)

    按照题意的要求逐渐求解: #include<stdio.h> #include<string.h> #include<algorithm> using namesp ...

  4. Java 素数 prime numbers-LeetCode 204

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  5. Prime Generator

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  6. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  7. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

  8. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  9. hdu 5901 count prime & code vs 3223 素数密度

    hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...

  10. 最小生成树 prime zoj1586

    题意:在n个星球,每2个星球之间的联通需要依靠一个网络适配器,每个星球喜欢的网络适配器的价钱不同,先给你一个n,然后n个数,代表第i个星球喜爱的网络适配器的价钱,然后给出一个矩阵M[i][j]代表第i ...

随机推荐

  1. mysql批量上传数据

    private object BlubckMysql(List<xiaoyao_blogs_pictureModel> list, string connect) { var sqllis ...

  2. Android Gradle配置

    解决问题 错误: Could not find the AndroidManifest.xml file, going up from path //打开app build.gradle文件加入以下代 ...

  3. 用Cython加速Python程序以及包装C程序简单测试

    用Cython加速Python程序 我没有拼错,就是Cython,C+Python=Cython! 我们来看看Cython的威力,先运行下边的程序: import time def fib(n): i ...

  4. (iOS)关于GCD死锁的问题

    - (void)viewDidLoad { [super viewDidLoad]; dispatch_sync(dispatch_get_main_queue(), ^{NSLog("); ...

  5. SQL Server 镜像

    sql server 2005镜像制作 以下是操作步骤:-- =========================================== -- 无论是主体服务器.镜像服务器, 还是见证服务 ...

  6. 安装AppManager

    http://www.searchvirtual.com.cn/showcontent_66884.htm

  7. QT完美转换特殊字符的大小写

    Util::ShowMessage(QString::fromUtf8("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØŒŠþÙÚÛÜÝŸ€")); Util::ShowMess ...

  8. xmu1214: 购物

    1214: 购物 Time Limit: 500 MS  Memory Limit: 64 MBSubmit: 10  Solved: 5[Submit][Status][Web Board] Des ...

  9. 如何使用ssh-keygen生成key

    ssh-keygen - 生成.管理和转换认证密钥 通常使用:[b]ssh-keygen -i -f 公密匙名>> authorized_keys[/b] 语法详细介绍 [code]ssh ...

  10. 读取excel出现空值

    表格里某列假如混杂了数字和字符就会有可能部分读取为空,连接Excel方法如下 string strConn = 'Provider=Microsoft.Jet.OLEDB.4.0;' 'Data So ...