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. jQuery-AJAX-格式

    function loadInfo(){    var domainName=$("input[name='domain-name']").val(); //域名    var c ...

  2. inux c编程:记录锁

    记录锁的功能:当一个进程正在读或修改文件的某个部分是,它可以阻止其他进程修改同一文件区.对于这个功能阐述我认为有三点要解释的: 记录锁不仅仅可以用来同步不同进程对同一文件的操作,还可以通过对同一文件加 ...

  3. Python socket server demo

    #coding:utf-8 from socket import * #开启ip和端口 ip_port = ("192.168.1.103",8088) print ip_port ...

  4. 使用documentFragment

    function insertHtml(range, val) { var doc = range.doc, frag = doc.createDocumentFragment(); K('@' + ...

  5. CenterFactory

    <?php /* 实例3 */ /* 抽象工厂 */ //青铜会员的打折商品 class BronzeRebateCommodity { //描述 public $desc = '青铜会员的打折 ...

  6. 动态创建selectjs 操作select和option

    1.动态创建select function createSelect(){ var mySelect = document.createElement("select"); myS ...

  7. 【leetcode刷题笔记】Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. Spring Cloud之统一fallback接口

    每个方法都配备一个fallback方法 不利于开发的 用类的方式 并且整个方法都是在同一个线程池里面的 主要对于client的修改: pom: <project xmlns="http ...

  9. Java -- 键盘输入 Scanner, BufferedReader。 系统相关System,Runtime。随机数 Randrom。日期操作Calendar

    1. Scanner 一个基于正则表达式的文本扫描器,他有多个构造函数,可以从文件,输入流和字符串中解析出基本类型值和字符串值. public class Main { public static v ...

  10. java反射(二)

    java的很多框架都是基于反射的.