POJ1365:质因数分解

Prime Land

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3590   Accepted: 1623

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

举例理解题意:例2:将 进行质因数分解,结果为3^2。思路:筛选素数打表,由小到大分解。
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN=33000;
bool isPrime[MAXN];
int prime[MAXN],top;
void sieve()
{
    memset(isPrime,true,sizeof(isPrime));
    isPrime[0]=false;
    isPrime[1]=false;
    for(int i=2;i<MAXN;i++)
    {
        if(isPrime[i])
        {
            prime[top++]=i;
            for(int j=i+i;j<MAXN;j+=i)
            {
                isPrime[j]=false;
            }
        }
    }
}
int multiply(int x,int n)
{
    int mul=1;
    for(int i=1;i<=n;i++)
    {
        mul*=x;
    }
    return mul;
}
int methods[MAXN];
void solve(int x)
{
    memset(methods,0,sizeof(methods));
    int l=0;
    while(x!=1)
    {
        if(x%prime[l]==0)
        {
            methods[prime[l]]++;
            x/=prime[l];
        }
        else l++;
    }
}
int main()
{
    int x,n;
    sieve();
    while(cin>>x&&x!=0)
    {
        int sum=1;
        cin>>n;
        sum*=multiply(x,n);
        while(cin.get()!=‘\n‘)
        {
            cin>>x>>n;
            sum*=multiply(x,n);
        }
        sum--;
        solve(sum);
        for(int i=top;i>=0;i--)
        {
            if(methods[prime[i]]!=0)
            {
                cout<<prime[i]<<" "<<methods[prime[i]]<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}
时间: 2024-11-05 15:54:14

POJ1365:质因数分解的相关文章

【BZOJ2227】【ZJOI2011】看电影 [组合数学][质因数分解]

看电影 Time Limit: 10 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description 到了难得的假期,小白班上组织大家去看电影.但由于假期里看电影的人太多,很难做到让全班看上同一场电影,最后大家在一个偏僻的小胡同里找到了一家电影院.但这家电影院分配座位的方式很特殊,具体方式如下: 1. 电影院的座位共有K个,并被标号为1…K,每个人买完票后会被随机指定一个座位,具体来说是从1…K中等可能的随机选取一个正整数,设其为L.

Codevs 1313 质因数分解

1313 质因数分解 题目描述 Description 已知正整数 n是两个不同的质数的乘积,试求出较大的那个质数 . 输入描述 Input Description 输入只有一行,包含一个正整数 n. 输出描述 Output Description 输出只有一行,包含一个正整数p,即较大的那个质数. 样例输入 Sample Input 21 样例输出 Sample Output 7 #include<iostream> #include<cstdio> #include<cm

HDU 3988 n!质因数分解

Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2324    Accepted Submission(s): 569 Problem Description iSea is tired of writing the story of Harry Potter, so, l

HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a <= b <= 100000, c=1, c <= d <= 100000, 0 <= k <= 100000) 思路:因为x与y的最大公约数为k,所以xx=x/k与yy=y/k一定互质.要从a/k和b/k之中选择互质的数,枚举1~b/k,当选择的yy小于等于a/k时,可以

求n!质因数分解之后素数a的个数

n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do { n/=m; w+=n; }while(n);

3164 质因数分解

3164 质因数分解 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description (多数据)给出t个数,求出它的质因子个数. 数据没坑,难度降低. 输入描述 Input Description 第一行 t 之后t行 数据 输出描述 Output Description t行 分解后结果(质因子个数) 样例输入 Sample Input 2 11 6 样例输出 Sample Output 1 2 数据范围及提示 Data

莫比乌斯函数-质因数分解

1240 莫比乌斯函数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.(据说,高斯(Gauss)比莫比乌斯早三十年就曾考虑过这个函数). 具体定义如下: 如果一个数包含平方因子,那么miu(n) = 0.例如:miu(4), miu(12), miu(18) = 0. 如果一个数不包含平方因子,并且有k个不同的质因子,那么miu(n)

质因数分解(0)&lt;P2012_1&gt;

质因数分解 (prime.cpp/c/pas) [问题描述] 已知正整数n是两个不同的质数的乘积,试求出较大的那个质数. [输入] 输入文件名为prime.in. 输入只有一行,包含一个正整数n. [输出] 输出文件名为prime.out. 输出只有一行,包含一个正整数p,即较大的那个质数. [数据范围] 对于60%的数据,6 ≤ n ≤ 1000. 对于100%的数据,6 ≤ n ≤ 2*109.

51nod 1240 莫比乌斯函数 (质因数分解)

1240 莫比乌斯函数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 取消关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.(据说,高斯(Gauss)比莫比乌斯早三十年就曾考虑过这个函数). 具体定义如下: 如果一个数包含平方因子,那么miu(n) = 0.例如:miu(4), miu(12), miu(18) = 0. 如果一个数不包含平方因子,并且有k个不同的质因