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. 【python】-- 类的装饰器方法、特殊成员方法

    装饰器方法 类的另外的特性,装饰器方法:静态方法(staticmethod).类方法(classmethod).属性方法(property) 一.静态方法 在方法名前加上@staticmethod装饰 ...

  2. java.time.ZonedDateTime

    Java的日期与时间 ZonedDateTime类是Java 8中日期时间功能里,用于表示带时区的日期与时间信息的类.可以用于表示一个真实事件的开始时间,如某火箭升空时间等等. ZonedDateTi ...

  3. java堆分析神器MAT

    Memory Analyzer(MAT) 基于Eclipse的软件 http://www.eclipse.org/mat/

  4. java线程 同步临界区:thinking in java4 21.3.5

    java线程 同步临界区:thinking in java4 21.3.5 thinking in java 4免费下载:http://download.csdn.net/detail/liangru ...

  5. mysql 自连接查询数据

    今天项目BOSS提了个sql语句需求,我听得稀里糊涂,没办法,一步步讨论.实践,最终搞定. 先上建表语句: /* Navicat MySQL Data Transfer Source Server : ...

  6. 编写你的第一个django应用程序4

    本教程上接教程3,我们将继续开发网页投票应用,本部分将主要关注简单的表单处理以及如何对代码进行优化 写一个简单的表单 让我们更新一下在上一个教程中编写的投票详细页面的模板(‘polls/detail. ...

  7. monokai-background

    foreground-color:f8f8f2 background-color:272822

  8. java程序实现Unicode码和中文互相转换

      根据前一篇的补充问题http://blog.csdn.net/fancylovejava/article/details/10142391 有了前一篇文章的了解,大概了解了unicode编码格式了 ...

  9. nc命令用法

    root@10.1.1.43:~# nc -h[v1.10-38]connect to somewhere: nc [-options] hostname port[s] [ports] ... li ...

  10. Mac安装使用kettle

    kettle是一个ETL工具 下载安装 https://jaist.dl.sourceforge.net/project/pentaho/Data Integration/ 选择版本后下载,并解压,如 ...