TJU 1038 素数表

1038.    Prime Land
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 785    Accepted Runs: 395     Multiple test files



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 x = p(ekx,kx)*p(ekx-1,kx-1)*...*p(e1,1)*p(e0,0). 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

题目大意就是 输入一个数X的质因子和质因子的次数,然后让我们求出X-1的质因子和质因子的次数,直接暴力法。

#include"stdio.h"
#include"string.h"
#include"sstream"
#include"iostream"
#include"cmath"
#include"vector"
#include"algorithm"
using namespace std;
int prime[100000];
int a[1000];
void list()
{
    prime[0]=prime[1]=1;
    for(int i=2;i*i<=100000;i++)
        if(!prime[i])
        for(int j=i*i;j<=100000;j+=i)
        if(!prime[j]) prime[j]=1;
}
int StrToDouble(char* s)
{
    memset(a,0,sizeof(a));
    int pos=0;
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]==32)
        {
            pos++;
        }
        else{
            a[pos]=a[pos]*10+s[i]-48;
        }
    }
    return pos;// 返回空格个数 = N-1
}
int main()
{

    list();  //初始化素数表
    /*
    for(int i=1;i<=100;i++)
        if(!prime[i]) printf("%d\n",i);
    */

    //freopen("a.txt","r",stdin);
    char s[1000];
    while(gets(s))
    {
        //cout<<"pow(13,1)="<<pow(13,1)<<endl;
        if(strcmp(s,"0")==0) break;
        int n=StrToDouble(s);
        double number=1;  //不能用long long有精度误差
        vector<double>base;   // 使用int 在pow会有精度误差
        vector<double>power;
        for(int i=0;i<n+1;i+=2) //输入数据最后一个是没有空格的
        {
            //cout<<a[i]<<" ";
            number*=pow(a[i],a[i+1]);
        }
        number--;  //所求的数是number-1
        //cout<<number<<endl;
        for(int i=2;i<=number;i++)  //找出所有的质因子
        {
            if(!prime[i]&&(long long)number%i==0)
                base.push_back(i);
        }
        /*
        for(vector<double>::iterator p=base.begin();p!=base.end();p++)  //质因子
            cout<<*p<<" ";
        cout<<endl;
        */
        reverse(base.begin(),base.end()); //从大到小排放
        /*
        for(vector<double>::iterator p=base.begin();p!=base.end();p++) //质因子
            cout<<*p<<" ";
        cout<<endl;
        */
        //cout<<"(long long)number="<<(long long)number<<endl;
        for(vector<double>::iterator p=base.begin();p!=base.end();p++)
        {
            for(int i=1;;) //次数逐渐增加 直到不能被整除
            {
                double temp=pow(*p,i);
                //cout<<"*p="<<*p<<" "<<"i="<<i<<" "<<"temp:"<<temp<<endl;
                if((long long)number%(long long)temp==0)
                {
                    ++i;
                }
                else
                {
                    --i;
                    power.push_back(i);
                    break;
                }
            }

        }

        int first=1;
        vector<double>::iterator p=base.begin(),q=power.begin();
        for(;p!=base.end();p++,q++)
        {
            if(first) first=0;
            else cout<<" ";
            cout<<*p<<" "<<*q;
        }

        cout<<endl;

    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值