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. AOP 切面编程

    简介 如果你很熟悉面向方面编程(AOP),你就会知道给代码增加“切面”可以使代码更清晰并且具有可维护性.但是AOP通常都依赖于第三方类库或者硬编码的.net特性来工作.虽然这些实现方式的好处大于它们的 ...

  2. 张孝祥Java高新技术汇总

    一.自动装箱和拆箱: 在Java中有8种基本数据类型:byte,short,int,long,float,double,char,boolean.而基本数据类型不是对象,这时人们给他们定义了包装类,使 ...

  3. 插入数据,返回最新id

    最简单的方法就是在查询之后select @@indentity. sql代码: INSERT INTO table_name (.....) VALUES(......)  SELECT @@IDEN ...

  4. css样式写一个三角形

    <style> .test{ border-color:transparent #abcdef transparent transparent; border-style:solid; b ...

  5. 1.PHP 教程_PHP 简介

    PHP是服务器端脚本语言. 在学习之前,您需要对以下知识有基本的了解: HTML css PHP是什么? PHP代表PHP:Hypertext Preprocessor PHP是一种使用广泛的开源的脚 ...

  6. Arduino Micro USB库

    USBCore.cpp #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_co ...

  7. Spring Boot的一个测试用例

    package tk.mybatis.springboot.mapper; import org.junit.Assert; import org.junit.Test; import org.jun ...

  8. Java泛型的一点用法(转)

    1.一个优秀的泛型,建议不要这样写public static <K, V> Map<K, V> getMap(String source, String firstSplit, ...

  9. Android实现左右滑动指引效果

    本文介绍Android中实现左右滑动的指引效果. 关于左右滑动效果,我在以前的一篇博文中提到过,有兴趣的朋友可以查看:http://www.cnblogs.com/hanyonglu/archive/ ...

  10. SqlCacheDependency的使用

    最近项目需要几秒就获取一次数据,查数据库服务器压力会很大,因此用缓存技术来缓解服务器压力. 使用SqlCacheDependency采用轮询的方式来获取缓存,SqlDependency查询通知的方式来 ...