所谓质因子分解是指将一个正整数n写成一个或多个质数的乘积形式。例如 6 = 2 x 3 , 8 = 2 x 2 x 3。
1、定义结构体
struct factor{
int x,cnt; // x为质因子,cnt为其数量
}fac[10];
模版
if( n % prime[i] == 0){
fac[num].x = prime[i];
fac[num].cnt = 0;
while(n % prime[i] == 0) {
fac[num].cnt++;
n /= prime[i];
}
num++;
}
如果在上面步骤结束后n仍然大于1,说明n有且仅有一个大于sqrt(n)的质因子,这时需要把这个质因子加入fac数组,并令其个数为1
if (n != 1){
fac[num].x = n;
fac[num++].cnt = 1;
}
例题1
PAT A1096 consecutive Factors
Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.
Input Specification:
Each input file contains one test case, which gives the integer N (1<N<231).
Output Specification:
For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]factor[2]…*factor[k], where the factors are listed in increasing order, and 1 is NOT included.
Sample Input:
630
Sample Output:
3
567
思路:
这题其实还挺好写的,如果能想到写法的话,不需要用质因子的模版,因为它是连续的,而且连乘的话,它的范围就不会超过其开根号,即2-根号n。遍历i = 2 ;i <= sqrt(n); i++;
再在里面来一个while(1)循环,当循环体 n % j != 0时 break;
记录最长ansLen = j - i + 1;
代码
#include<cstdio>
#include<cmath>
typedef long long LL;
int main(){
int n;
scanf("%d",&n);
LL sqr = (LL)sqrt(1.0*n),ansI = 0,ansLen = 0;
for(int i = 2; i <=sqr;i++){
LL temp = 1,j = i;
while(1){
temp *= j;
if (n % temp != 0)break;
if (j - i + 1 > ansLen){
ansI = i;
ansLen = j - i + 1;
}
j++;
}
}
if (ansLen == 0){
printf("1\n%lld",n);
}else{
printf("%lld\n",ansLen );
for(int i = ansI; i < ansI + ansLen;i++){
printf("%lld",i );
if (i != ansLen + ansI -1)printf("*");
}
}
return 0;
}
例题2
PAT A1059. Prime Factors(20)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N=p1k1×p2k2 ×⋯×pmkm.
Input Specification:
Each input file contains one test case which gives a positive integer N in the range of long int.
Output Specification:
Factor N in the format N = p1 k~1~*p~2~k2*…*pm ^km, where pI 's are prime factors of N in increasing order, and the exponent kI is the number of pI – hence when there is only one pI, kIis 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
思路:
直接套模版,需要注意的是for循环的判断条件是 i < pNUm && prime[i] <= sqr;
代码:
#include<cstdio>
#include<queue>
using namespace std;
struct mice
{
int R;
int weight;
}mouse[1010];
queue<int> q;
int main(){
int np,ng,order;
scanf("%d %d",&np,&ng);
for (int i = 0; i < np; ++i)
{
scanf("%d",&mouse[i].weight);
}
for (int i = 0; i < np; ++i)
{
scanf("%d",&order);
q.push(order);
}
int temp = np,group;
while(q.size()!=1){
if (temp%ng == 0)group = temp/ng;
else group = temp/ng + 1;
for(int i = 0; i < group;i++){
int k = q.front();
for(int j = 0; j < ng;j++){
if (i*ng + j >= temp) break;
int front = q.front();
if (mouse[front].weight > mouse[k].weight){
k = front;
}
mouse[front].R = group + 1;
q.pop();
}
q.push(k);
}
temp = group;
}
mouse[q.front()].R = 1;
for(int i = 0; i < np;i++){
printf("%d\n",mouse[i].R );
if (i < np -1) printf(" ");
}
return 0;
}