题意
题意大概就是这样,输入的是一个数字因数分解之后的表示形式:
N=∏ki=1paii 其中 pi 是N 的质因子.
输入的数据 就是
pi
和
ai
然后求出的 N-1 的质因子分解表示,按质因子的倒序排列
SampleInput
17 1
5 1 2 1
509 1 59 1
0
SampleOutput
2 4
3 2
13 1 11 1 7 1 5 1 3 1 2 1
思路
就按着最简单的想法去做即可,不过需要注意 需要使用 long long
类型表示N。
另外输入的时候,由于并不确定因子的数量,因此需要以字符串的形式输入,然后再解析成数字;求积。
以及要求倒序输入, 可以用sort()
排序。。在这我还特意建了一个类,来排序,略费劲。
代码
/*Accepted 1038 C++ 1.4K 0'00.00" 1360K*/
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
char s[600];
long long res;
int a,b;
class Pri
{
public:
int p;int e;
};
bool cmp(Pri p1,Pri p2)
{
return p1.p>p2.p;
}
Pri pri[500];
int main()
{
while(gets(s)&&s[0]!='0')
{
res=1;
for(int i=0,k=0,h=0; i<strlen(s); i++)
{
if(s[i]!=' ')
{
h=h*10+s[i]-'0';
}
if(s[i]==' '||i==strlen(s)-1)
{
k++;
if(k==1) a=h;
else if(k==2)
{
b=h;
k=0;
for(int j=0;j<b;j++)
res*=a;
}
h=0;
}
}
int k=0;
res--;
for(long long i=2;i*i<=res;i++)
{
if(res%i==0)
{
a=0;
pri[k].p=i;
while(res%i==0)
a++,res/=i;
pri[k++].e=a;
}
}
if(res>1)
{
pri[k].p=res;
pri[k++].e=1;
}
sort(pri,pri+k,cmp);
for(int i=0;i<k;i++){
if(i>0)printf(" ");
printf("%d %d",pri[i].p,pri[i].e);
}
printf("\n");
}
}