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. What is the difference between iterations and epochs in Convolution neural networks?

    https://stats.stackexchange.com/questions/164876/tradeoff-batch-size-vs-number-of-iterations-to-trai ...

  2. php在web端播放amr语音(如微信语音)

    在使用微信JSSDK的上传下载语音接口时,发现一个问题: 下载的语音在iPhone上不能播放,测试了之后原因竟然是: 微信接口返回的音频内容是amr格式的,但iPhone不支持播放此类型格式. 那么转 ...

  3. [luogu3393]逃离僵尸岛

    [luogu3393]逃离僵尸岛 luogu 先把被禁止的点和新建的虚点n+1连0边 跑最短路,dis<=s的点价格为Q,否则为P, 再建图跑最短路 #define ll long long # ...

  4. rails数据验证

    @user1 = :name => "zhou" 与 @user2 = :name=> "ZHOU" 在为保存之前都有可能通过第一关validate ...

  5. 面向对象分析与设计(C++)课堂笔记

    第一次课: 对象是程序设计最基本的单元 对象:对象标识.属性.操作(对象标识又分为内部标识.外部标识) 三三制原则 继承:英文语义”is a kind of” 自动的拥有或隐含的复制 虚基类:解决多继 ...

  6. react项目中antd组件库的使用需要注意的问题

    antd是蚂蚁金服推出的ui组件库,给我们在react项目开发中提供了大大的便利.但在使用的过程中,或多或少的会遇到一些问题,毕竟,用的是别人的东西,就得遵守别人的规则嘛!官方文档:https://a ...

  7. JavaWeb -- 会话, Cookie 和 Session

    1. 会话 •Cookie是客户端技术,服务器把每个用户的数据以cookie的形式写给用户各自的浏览器.当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去.这样,web资源处理的就是 ...

  8. Mysql远程链接访问权限设置

    Host 'XXX' is not allowed to connect to this MySQL server 解决方案/如何开启MySQL的远程帐号 如何开启MySQL的远程帐号-1)首先以 r ...

  9. java: jsp:param中文乱码

    java: jsp:param中文乱码 假如a.jsp/b.jsp文件中 a.jsp代码: 需要加入:request.setCharacterEncoding("UTF-8")  ...

  10. Django-02

    知识预览 Ajax前戏:json Ajax简介 jquery实现的ajax JS实现的ajax 回到顶部 Ajax前戏:json 什么是json? 定义: JSON(JavaScript Object ...