Prime Land
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3590   Accepted: 1623

Description

Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,... denote the increasing sequence of all prime numbers. We know that x > 1 can be represented in only one way in the form of product of powers of prime factors. This implies that there is an integer kx and uniquely determined integers ekx, ekx-1, ..., e1, e0, (ekx > 0), that  The sequence

(ekx, ekx-1, ... ,e1, e0)

is considered to be the representation of x in prime base number system.

It is really true that all numerical calculations in prime base number system can seem to us a little bit unusual, or even hard. In fact, the children in Prime Land learn to add to subtract numbers several years. On the other hand, multiplication and division is very simple.

Recently, somebody has returned from a holiday in the Computer Land where small smart things called computers have been used. It has turned out that they could be used to make addition and subtraction in prime base number system much easier. It has been decided to make an experiment and let a computer to do the operation ``minus one''.

Help people in the Prime Land and write a corresponding program.

For practical reasons we will write here the prime base representation as a sequence of such pi and ei from the prime base representation above for which ei > 0. We will keep decreasing order with regard to pi.

Input

The input consists of lines (at least one) each of which except the last contains prime base representation of just one positive integer greater than 2 and less or equal 32767. All numbers in the line are separated by one space. The last line contains number 0.

Output

The output contains one line for each but the last line of the input. If x is a positive integer contained in a line of the input, the line in the output will contain x - 1 in prime base representation. All numbers in the line are separated by one space. There is no line in the output corresponding to the last ``null'' line of the input.

Sample Input

17 1
5 1 2 1
509 1 59 1
0

Sample Output

2 4
3 2
13 1 11 1 7 1 5 1 3 1 2 1 举例理解题意:例2:将 进行质因数分解,结果为3^2。
思路:筛选素数打表,由小到大分解。
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN=;
bool isPrime[MAXN];
int prime[MAXN],top;
void sieve()
{
memset(isPrime,true,sizeof(isPrime));
isPrime[]=false;
isPrime[]=false;
for(int i=;i<MAXN;i++)
{
if(isPrime[i])
{
prime[top++]=i;
for(int j=i+i;j<MAXN;j+=i)
{
isPrime[j]=false;
}
}
}
}
int multiply(int x,int n)
{
int mul=;
for(int i=;i<=n;i++)
{
mul*=x;
}
return mul;
}
int methods[MAXN];
void solve(int x)
{
memset(methods,,sizeof(methods));
int l=;
while(x!=)
{
if(x%prime[l]==)
{
methods[prime[l]]++;
x/=prime[l];
}
else l++;
}
}
int main()
{
int x,n;
sieve();
while(cin>>x&&x!=)
{
int sum=;
cin>>n;
sum*=multiply(x,n);
while(cin.get()!='\n')
{
cin>>x>>n;
sum*=multiply(x,n);
}
sum--;
solve(sum);
for(int i=top;i>=;i--)
{
if(methods[prime[i]]!=)
{
cout<<prime[i]<<" "<<methods[prime[i]]<<" ";
}
}
cout<<endl;
}
return ;
}

POJ1365:质因数分解的更多相关文章

  1. POJ1365 - Prime Land(质因数分解)

    题目大意 给定一个数的质因子表达式,要求你计算机它的值,并减一,再对这个值进行质因数分解,输出表达式 题解 预处理一下,线性筛法筛下素数,然后求出值来之后再用筛选出的素数去分解....其实主要就是字符 ...

  2. 求n!质因数分解之后素数a的个数

    n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do ...

  3. AC日记——质因数分解 1.5 43

    43:质因数分解 总时间限制:  1000ms 内存限制:  65536kB 描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n. 对于60% ...

  4. 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛

    4514: [Sdoi2016]数字配对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 726  Solved: 309[Submit][Status ...

  5. 整数分解 && 质因数分解

    输入整数(0-30)分解成所有整数之和.每四行换行一次. 一种方法是通过深度优先枚举出解.通过递归的方式来实现. #include <stdio.h> #include <strin ...

  6. algorithm@ 大素数判定和大整数质因数分解

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  7. 数学概念——J - 数论,质因数分解

    J - 数论,质因数分解 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  8. hdu1405 第六周J题(质因数分解)

    J - 数论,质因数分解 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Desc ...

  9. POj3421 X-factor Chains(质因数分解+排列组合)

    POj3421X-factor Chains 一开始没读懂题意,不太明白 Xi | Xi+1 where a | b means a perfectly divides into b的意思,后来才发现 ...

随机推荐

  1. 在VS2017环境中编译libxml2库

    libxml2库编译 1.下载libxml2,官网是:http://www.xmlsoft.org/downloads.html, 我下载的版本是:libxml2-sources-2.9.7.tar. ...

  2. Django在不启动server的情况下调用方法

    from django.conf import settingsfrom django import template settings.configure() a = template.Templa ...

  3. mongodb 的注意点

    昨天同事安装mongodb遇到了些问题,问了下我,后拉发现都是些细节没注意(讲道理这应该是很简单,一顿操作就ok的事情) 首先,下载 mongo包, 然后 ,解压安装, 启动之. 问题就出现在他后台启 ...

  4. JVM性能优化, Part 1 ―― JVM简介

    JVM性能优化这些列文章共分为5章,是ImportNew上面翻译自Javaworld: 第1章:JVM技术概览 第2章:编译器 第3章:垃圾回收 第4章:并发垃圾回收 第5章:可伸缩性 众所周知,Ja ...

  5. Linux里AWK中split函数的用法

    跟java里的split函数的用法是很相像的,举例如下: The awk function split(s,a,sep) splits a string s into an awk array a u ...

  6. python基础14 ---函数模块5(模块和包)

    模块与包 一.模块 1.模块是怎么诞生的. 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到 不同的文 ...

  7. shell基础part2

    shell基础 一.bash中的变量 1.变量的定义:变量是计算机的内存单元,其中存放的值是可以改变的. 2.变量的设定规则:变量名不能以数字开头:变量的等号两边不能有空格,变量的值如果想有空格必须用 ...

  8. Redis缓存全自动安装shell脚本

    我只是把命令放到shell文件中了,方便安装,代码如下: #!/bin/bash # shell的执行选项: # -n 只读取shell脚本,但不实际执行 # -x 进入跟踪方式,显示所执行的每一条命 ...

  9. Docker 数据管理-Volumes

    Volumes是Docker最为推荐的数据持久化方法. Volumes have several advantages over bind mounts: Volumes are easier to ...

  10. 添加linux开机启动任务

    对于系统里面设置的开机启动程序 先来看一个例子nginx启动脚本 #!/bin/sh ### BEGIN INIT INFO # Provides: nginx # Required-Start: $ ...