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 ...
随机推荐
- R与数据分析旧笔记(十六) 基于密度的方法:DBSCAN
基于密度的方法:DBSCAN 基于密度的方法:DBSCAN DBSCAN=Density-Based Spatial Clustering of Applications with Noise 本算法 ...
- ue中替换行
把替换的字符替换为^p 如:123,12,3,1, 在UE力把“,”替换未“^p”,就会替换为 1231231
- 深刻理解一句js
最近有点小懒,不想写东西,也不想看东西,看东西还老跑神,这是肿么了呢? 盯着显示器某一刻能把它看透,就是不想挪下眼睛 无意中看到了这行代码 [].forEach.call($$("*&quo ...
- Apache 开启 Https
1. 准备所需工具: 1) apache httpd2.4 浏览 2) Win32 OpenSSL v1.0.2d 浏览 2. 安装 2.1 安装Apache2.4服务 php环境搭建 浏览 2.2 ...
- MOQ
MOQ:(Minimum order Quantity) 最低订货数量 MOQ 即最小订购量(最小订单量) 对每个产品设定建议订单量是补货的方法之一.另外要注意订单的有效性,这是由供应商制定的 ...
- Jsoup代码解读之六-防御XSS攻击
Jsoup代码解读之八-防御XSS攻击 防御XSS攻击的一般原理 cleaner是Jsoup的重要功能之一,我们常用它来进行富文本输入中的XSS防御. 我们知道,XSS攻击的一般方式是,通过在页面输入 ...
- 在 Visual C++ 中开发自定义的绘图控件
本文讨论的重点介于两者 之间 — 公共控件赋予您想要的大部分功能,但控件的外观并不是您想要的.例如,列表视图控件提供在许多视图风格中显示数据列表的方式 — 小图标.大图标.列表和详细列表(报告).然而 ...
- 用auto_ptr类模板帮助动态内存管理
动态内存使用最多的是在C++应用程序的代码中.有过编程经验的程序员虽然都知道new操作符的使用一定要与delete匹配,在某些场合仍然可能有内存溢出.当异常被掷出时,程序的正常控制流程被改变,因此导致 ...
- 《Java4Android视频教程》学习笔记(三)
一:抽象类 接口 1.使用abstract修饰抽象类 抽象函数 2.一个类中有一个或者多个抽象函数,必须定义为抽象类 3.抽象类可以不含有抽象函数 4.抽象类不可以生成对象 tip:如果一个代码在语意 ...
- 破解win7开机密码,又不取消密码
Win7开机密码破解方法 1. 进入pe.(可用系统盘,或者u盘启动) 2.进入c:\windows\system32下 更改magnify.exe和cmd.exe的所有者为administrator ...