POJ1365_Prime Land【质因数分解】【素数】【水题】

Prime Land

Time Limit: 1000MS
Memory Limit: 10000K

Total Submissions: 3086
Accepted: 1416

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

Source

Central Europe 1997

题目大意:告诉你一个数的质因数x的所有底数pi和幂ei,输出x-1的质因数的所有底数和幂

思路:这道题不难,但是题意特别不好理解,对于我这种英文渣的人,愣是一个小时没看明白

关于题意举例说明吧

例如 509 1 59 1

x = 509^1 * 59^1 = 30031

x-1 = 30030

则答案13 1 11 1 7 1 5 1 3 1 2 1 就是 x-1 = 13^1 * 11^1 * 7^1 * 5^1 *3^1 *2^1 = 30031

那么直接按着题意暴力解决就行了。。。。。

#include<stdio.h>
#include<string.h>
#include<math.h>
/*
pow函数说明
原型:extern float pow(float x, float y);
用法:#include <math.h>
功能:计算x的y次幂。
说明:x应大于零,返回幂指数的结果。
*/
double p[110],e[110];
int Prime[35000],E[35000];

void IsPrime()
{
    Prime[0] = Prime[1] = 0;
    for(int i = 2; i <= 35000; i++)
    {
        Prime[i] = 1;
    }
    for(int i = 2; i <= 35000; i++)
    {
        for(int j = i+i; j <= 35000; j+=i)
        {
            Prime[j] = 0;
        }
    }
}
int main()
{
    int count,sign;
    IsPrime();
//    for(int i = 2; i <= 35000; i++)
//        if(Prime[i])
//            printf("%d ",i);
    while(1)
    {
        count = 0,sign = 0;
        memset(p,0,sizeof(p));
        memset(e,0,sizeof(e));
        memset(E,0,sizeof(E));

        while(1)
        {
            scanf("%lf",&p[count]);
            if(p[count] == 0)
            {
                sign = 1;
                 break;
            }
            scanf("%lf",&e[count]);
            count++;
            char c = getchar();
            if(c=='\n')
                break;
        }

        if(sign == 1)
            break;

        double num = 1;
        for(int i = 0; i < count; i++)
            num *= pow(p[i],e[i]);

        int sum = (int)num - 1;
//        printf("%d\n",sum);
        int flag = 0,pos = 2;
        for(int i = 2; i <= 32767; i++)
        {
            if(sum == 1)
                break;
            if(Prime[i])
            {
                while(sum % i == 0)
                {
                    E[i]++;
                    sum /= i;
                    if(flag == 0)
                    {
                        flag = 1;
                        pos = i;
                    }
                }
            }
        }

        for(int i = 32767; i>= 2; i--)
        {
            if(E[i]!=0 && i!=pos)
                printf("%d %d ",i,E[i]);
            else if(E[i]!=0 && i==pos)
            {
                printf("%d %d\n",i,E[i]);
                break;
            }
        }
    }

    return 0;
}
时间: 2024-08-07 00:08:26

POJ1365_Prime Land【质因数分解】【素数】【水题】的相关文章

HDU ACM 2521 反素数 水题+因子打表

分析:水题,不解释. #include<iostream> using namespace std; int cnt[6000]; void init() //打表 { int i,j; memset(cnt,0,sizeof(cnt)); cnt[1]=1; //1只有他本身 for(i=2;i<=5005;i++) { cnt[i]+=2; //1和他本身 for(j=2;j<=i/2;j++) if(i%j==0) cnt[i]++; } } int main() { int

素数三元组(南阳oj1156)(素数水题)

素数三元组 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 相邻三个奇数都是素数是一种非常少见的情形,也就是三个奇数p-2, p, p+2都是素数,这样就形成了一个素数三元组.请找出三个数都不超过n的所有这样的素数三元组. 输入 输入多组数据,每组测试数据为一个正整数n,n <= 5000000. 输出 输出大小不超过n的所有的素数三元组,每行按照从小到大的顺序输出一个三元组中的三个数,两个数之间用空格间隔.如果不存在这样的素数三元组,请输出"No tripl

HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题目解析: Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. 题目又说a==c==1,所以就是求[1,b]与[1,d]中gcd等于k的个数,因为若gcd(x,y)==z,那么gcd(x/z,y/z)==1,又因为不是z的倍数的肯定不是,所以不是z的倍数的可以直接去

poj 1365 Prime Land 质因数分解

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int primeNUM[] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,

POJ 1365(质因数分级+素数打表)

Prime Land Time Limit: 1000ms                                Memory Limit: 10000KB 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 incr

SDNU 1427.分解质因数(水题)

Description 问题描述 求出区间[a,b]中所有整数的质因数分解. 输入格式 输入两个整数a,b. 输出格式 每行输出一个数的分解,形如k=a1*a2*a3...(a1< =a2< =a3...,k也是从小到大的)(具体可看样例) 样例输入 3  10 样例输出 3=3 4=2*2 5=5 6=2*3 7=7 8=2*2*2 9=3*3 10=2*5 提示 先筛出所有素数,然后再分解. 数据规模和约定 2< =a< =b< =10000 Input Output S

POJ2262_Goldbach&#39;s Conjecture【素数判断】【水题】

Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38024 Accepted: 14624 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:

POJ2909_Goldbach&#39;s Conjecture【素数判断】【水题】

Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10116 Accepted: 5973 Description For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2 This c

水仙花数&amp;素数&amp;质因数分解的C语言实现

最近,我翻了一下之前的C语言教材,看了三个有意思的小程序,包括:寻找"水仙花数".判断某数是否为素数.对一个数进行质因数分解.我想把这三个东西放到一个程序中,便写下了此文. 算法步骤 1. 寻找"水仙花数". "水仙花数"是指一个三位数,其各位数字的立方和等于该数本身.例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方. 2. 判断某数是否为素数. 素数是指只能被1和它本身整除的数,判断一个数是否为

蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)

算法训练 Torry的困惑(基本型) 时间限制:1.0s   内存限制:512.0MB 问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7--这样的数叫做质数.Torry突然想到一个问题,前10.100.1000.10000--个质数的乘积是多少呢?他把这个问题告诉老师.老师愣住了,一时回答不出来.于是Torry求助于会编程的你,请你算出前n个质数的乘积.不过,考虑到你才接触编程不久,Torry只要你算出这个数模上50000的值. 输入格式 仅包含一个正整数n,其中n<=100