质数的筛法+质因分解

Eratosthenes 筛法(O(N*log log N))
int m,pri[maxn];
bool v[maxn];
void primes(int n)
{
    memset(v,true,sizeof(v));
    m = 0;
    for(int i=2;i<=n;i++){
        if(v[i]){
            pri[m++] = i;
        }
        for(int j=i*i;j<=n;j+=i)
            v[j] = false;
    }
    for(int i=0;i<m;i++) cout<<pri[i]<<endl;
}
线性筛法 O(N)
int m,pri[maxn];
bool v[maxn];
void primes(int n)
{
    memset(v,true,sizeof(v));
    m = 0;
    for(int i=2;i<=n;i++){
        if(v[i])    pri[m++] = i;
        for(int j=0;j<m&&i*pri[j]<=n;j++){
            v[i*pri[j]] = 0;
            if(i%pri[j]==0)   break;
        }
    }
    for(int i=0;i<m;i++) cout<<pri[i]<<endl;
}
质因分解
int p[maxn],c[maxn];
void divide(int n)
{
    m = 0;
    for(int i=0;pri[i]<=n&&n!=1;i++){	//提前筛出质数
        if(n%pri[i]==0){
            p[++m] = pri[i],c[m] = 0;
            while(n%pri[i]==0){
                n/=pri[i];
                c[m]++;
            }
        }
    }
    if(n>1) p[++m] = 1,c[m] = 1;
    for(int i=1;i<=m;i++)
    	cout<<p[i]<<' '<<c[i]<<endl;
}
例题:POJ 1365 Prime Land
传送门:http://poj.org/problem?id=1365

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

题意

  给你一组质因分解的结果,让你求解 原数-1 的质因分解结果,并按照质因数由大到小输出。
  PS:5 1 2 1 == 51 * 21 = 10;10 - 1 == 9 == 32 = 3 2

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int maxn = 100010;
const ll lnf = 0x3f3f3f3f3f3f3f;
int m,pri[maxn];
bool v[maxn];
char cmd[maxn];
void primes()	//素数打表
{
    memset(v,true,sizeof(v));
    m = 0;
    for(int i=2;i<maxn;i++){
        if(v[i])    pri[m++] = i;
        for(int j=0;j<m&&i*pri[j]<maxn;j++){
            v[i*pri[j]] = 0;
            if(i%pri[j]==0)   break;
        }
    }
}
int ksm(int a,int b)
{
    int ans = 1;
    for(ll i=b;i;i>>=1){
        if(i&1) ans = ans * a;
        a *= a;
    }
    return ans;
}
int p[maxn],c[maxn];
void divide(int n)	//质因分解
{
    m = 0;
    for(int i=0;pri[i]<=n&&n!=1;i++){
        if(n%pri[i]==0){
            p[++m] = pri[i],c[m] = 0;
            while(n%pri[i]==0){
                n/=pri[i];
                c[m]++;
            }
        }
    }
    if(n>1) p[++m] = 1,c[m] = 1;
    for(int i=m;i>=1;i--){	//由大到小输出,注意格式
        if(i!=1)printf("%d %d ",p[i],c[i]);
        else printf("%d %d\n",p[i],c[i]);
    }
}
int main(void)
{
    int a,b,sum;
    primes();
    while(gets(cmd)){	
        if(cmd[0]=='0') break;
        int len = strlen(cmd);
        a = b = 0;
        sum = 1;
        for(int i=0;i<len;i++){
            if(cmd[i]!=' '){
                a = a*10 + (cmd[i]-'0');
            }
            else{
                i++;
                while(i<len&&cmd[i]!=' '){
                    b = b*10 + (cmd[i]-'0');
                    i++;
                }
                sum *= ksm(a,b);
                a = b = 0;
            }
        }
        --sum;
        divide(sum);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值