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. Modeling of Indoor Positioning Systems Based on Location Fingerprinting

    Kamol Kaemarungsi and Prashant Krishnamurthy Telecommunications Program School of Information Scienc ...

  2. linux c编程:管道

    2在前面介绍过,进程之间交换信息的唯一途径就是传送打开的文件.可以经由fork或者exec来传送.这一章将介绍新的进程共享方式 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都 ...

  3. 每天一个Linux命令(20)find命令_exec参数

    find命令的exec参数,用于find查找命令完成以后的后续操作.     (1)用法: 用法:  [find命令]  [-exec  其他命令 {} \;]     (2)功能: 功能:-exec ...

  4. Java+MySql图片数据保存

    之前一直没有做过涉及到图片存储的应用,最近要做的东东涉及到了这个点,就做了一个小的例子算是对图片存储的初试吧! 1.创建表: drop table if exists photo; CREATE TA ...

  5. mac iterm2 安装 lrzsz rz sz命令

    原文:https://blog.csdn.net/jack85986370/article/details/51382077 首先mac自带的终端是不支持lrzsz的,需要下载安装iterm2,下载地 ...

  6. mybatis学习(四)

    创建mybatis工程 工程目录: 具体步骤: 1.创建sqlMapConfig.xml文件,配置mybatis的运行环境,事物,数据源,加载mapper映射文件等. 2.创建po类(查询或者返回的属 ...

  7. PHP中有多态么

    PHP中有多态么 一.总结 一句话总结:封装是类的构建过程,php具有:php也具有继承的特性.唯独这个多态,php体现的十分模糊.原因是php是弱类型语言. php不具有像java那种清晰的多态,不 ...

  8. GridView设置多个DatakeyNames

    1.aspx页面GridView直接绑定DataKeyNames aspx设置: <asp:GridView ID="grvGrid" runat="server& ...

  9. 十三 Django框架,CSRF跨站请求伪造

     全局CSRF 如果要启用防止CSRF跨站请求伪造,就需要在中间件开启CSRF #中间件 MIDDLEWARE = [ 'django.middleware.security.SecurityMidd ...

  10. C#实现文件拖放并打开文件(使用ListBox)

    1.C#实现文件拖放并打开文件 (http://www.cnblogs.com/GaoHuhu/archive/2012/10/10/2717954.html)