Prime Land
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3552   Accepted: 1609

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 思路:直接模拟
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
int prime[MAXN],len;
bool isPrime[MAXN];
void init()
{
memset(isPrime,true,sizeof(isPrime));
for(int i=;i<MAXN;i++)
{
if(isPrime[i])
{
prime[len++]=i;
for(int j=i+i;j<MAXN;j+=i)
isPrime[j]=false;
}
}
}
int counter[MAXN];
long long npow(int x,int n)
{
long long res=;
while(n>)
{
if(n&)
res*=x;
x*=x;
n>>=;
}
return res;
}
int main()
{
int p,e;
char op;
init();
while(true)
{
memset(counter,,sizeof(counter));
scanf("%d%*c",&p);
if(p==) break;
scanf("%d%c",&e,&op);
long long mul=;
mul*=npow(p,e);
while(op!='\n')
{
scanf("%d%*c%d%c",&p,&e,&op);
mul*=npow(p,e);
}
mul--;
int l=;
while(mul!=)
{
while(mul%prime[l]==)
{
counter[prime[l]]++;
mul/=prime[l];
}
l++;
}
int _stack[MAXN],top=;
for(int i=;i<len;i++)
if(counter[prime[i]]!=)
_stack[top++]=prime[i]; for(int i=top-;i>=;i--)
printf("%d %d ",_stack[i],counter[_stack[i]]);
printf("%d %d\n",_stack[],counter[_stack[]]);
} return ;
}
												

POJ1365:素数的更多相关文章

  1. POJ1365 Prime Land【质因数分解】【素数】【水题】

    题目链接: http://poj.org/problem?id=1365 题目大意: 告诉你一个数的质因数x的全部底数pi和幂ei.输出x-1的质因数的全部底数和幂 解题思路: 这道题不难.可是题意特 ...

  2. Help Hanzo (素数筛+区间枚举)

    Help Hanzo 题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000).     (全题在文末) 题解: a~b枚举必定TLE,普通打表MLE,真是头疼 ...

  3. Java 素数 prime numbers-LeetCode 204

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

  4. 求解第N个素数

    任务 求解第 10,0000.100,0000.1000,0000 ... 个素数(要求精确解). 想法 Sieve of Eratosthenes 学习初等数论的时候曾经学过埃拉托斯特尼筛法(Sie ...

  5. 使用BitArray判断素数

    首先显示1024范围内的所有素数,然后显示输入的数是否是素数.1024 是代码中计算的素数的范围,可以修改.计算平方根,是为了确定一个基数的范围.1024的平方根是32,两个超过32 的数相乘,肯定大 ...

  6. 查找素数Eratosthenes筛法的mpi程序

    思路: 只保留奇数 (1)由输入的整数n确定存储奇数(不包括1)的数组大小: n=(n%2==0)?(n/2-1):((n-1)/2);//n为存储奇数的数组大小,不包括基数1 (2)由数组大小n.进 ...

  7. Openjudge 1.13-23:区间内的真素数(每日一水)

    总时间限制:  1000ms 内存限制:  65536kB 描述 找出正整数 M 和 N 之间(N 不小于 M)的所有真素数.真素数的定义:如果一个正整数 P 为素数,且其反序也为素数,那么 P 就为 ...

  8. java语言 打印素数实例

    //根据定义判断素数---循环n-1次,当n很大时循环n次 public static void main(String[] args) {        // TODO Auto-generated ...

  9. 埃拉托色尼筛法(Sieve of Eratosthenes)求素数。

    埃拉托色尼筛法(Sieve of Eratosthenes)是一种用来求所有小于N的素数的方法.从建立一个整数2~N的表着手,寻找i? 的整数,编程实现此算法,并讨论运算时间. 由于是通过删除来实现, ...

随机推荐

  1. Bytecode Visualizer 一个好用的class字节码查看工具

    http://www.drgarbage.com/howto/install/   eclipse插件,效果如下:

  2. liferay 指定默认首页

    1.登录liferay后,点击控制面板-->设置--> portal设置 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmVuZ3hpbmdf ...

  3. python实例3-天气小模块

    调用中国天气的一小段代码,抓取 #! /usr/bin/python # coding = utf-8 # ToDo: get weather info from weather.com.cn # A ...

  4. python学习-3.一些常用模块用法

    一.time.datetime 时间戳转化为元组 1 >>> time.localtime() 2 time.struct_time(tm_year=2016, tm_mon=8, ...

  5. 2018年长沙理工大学第十三届程序设计竞赛 E 小木乃伊到我家 【最短路】

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 AA的欧尼酱qwb是个考古学家,有一天qwb发 ...

  6. IOS UI Frame 相对位置调整 与优化方法 Height Width X Y 调整

    不使用xib ,纯代码开发的过程中,动态UI  需要改对象的大小位置 反复使用CGRectMake 去 setFrame  非常低效耗时,而且 牵一发动全身. 以下整理出几个方法,方便动态布局 1.s ...

  7. 广义表(C++实现)

    广义表是非线性结构,其定义是递归的. 以下给出几种简单的广义表模型: 由上图我们可以看到,广义表的节点类型无非head.value.sub三种,这里设置枚举类型,利用枚举变量来记录每个节点的类型: e ...

  8. servlet实现验证码

    (博客内容来自:jsp使用servlet实现验证码,以下内容只是不才对原博客的摘抄以便以后翻阅使用) 一.原理: 验证码作为一个图片,在页面中为“画”出来的,它是如何画出来的呢? <生成图片&g ...

  9. Stanford Log-linear Part-Of-Speech Tagger标记含义

    Stanford Log-linear Part-Of-Speech Tagger标记含义 英文词性标记名称缩写的含义: 使用的是宾州树库的tag集合,具体含义和举例如下表: Tag Descript ...

  10. assembly打包插件引发的自定义spring标签找不到声明的错误

    异常信息:通配符的匹配很全面, 但无法找到元素 的声明. 报的异常信息是关于我们使用的一个自定义的spring标签,这个异常通常的原因可能是读取不到自定义标签的映射. 到META-INF目录下找一下是 ...