1059 Prime Factors (25分)
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^
k1*
p2^
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, ki is 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
求一个数的质因子分解,
首先,这个long int 我想着应该比int范围大很多,其实不然。这点影响我的思路。
int型数据范围内的质数开一个100000的数组来存就行。
我参考了算法笔记。有一个很重要的结论:
一个数的质因子要么全在[2,sqrt(n)]之间;要么有一个大于sqrt(n),其余的全在[2,sqrt(n)]之间。
先在2到sqrt之间遍历,如果能被整除,就n/=这个质因子,如果n最后不为1,说明此时的n也是一个质因子
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
const int r=100005;
vector<int> p;
int n,cnt;
bool prime[r];
struct node{
int f;
int cnt;
};
vector<node> s;
void init(){
int i,j;
fill(prime,prime+r,true);
prime[0]=prime[1]=false;
for(i=2;i<100005;i++){
if(prime[i]){
p.push_back(i);
}
for(j=2*i;j<100005;j+=i){
prime[j]=false;
}
}
}
int main(){
int i,j,t,len;
node temp;
scanf("%d",&n);
t=n;
init();
if(n==1){
printf("1=1\n");
}
else{
len=p.size();
int sqt=(int)(1.0*sqrt(n));
for(i=0;i<len&&p[i]<=sqt;i++){
if(n%p[i]==0){
temp.f=p[i];
temp.cnt=1;
n/=p[i];
while(n%p[i]==0){
temp.cnt++;
n/=p[i];
}
s.push_back(temp);
temp.f=0;
temp.cnt=0;
}
if(n==1){
break;
}
}
if(n!=1){
temp.f=n;
temp.cnt=1;
s.push_back(temp);
}
printf("%d=",t);
for(i=0;i<s.size();i++){
if(s[i].cnt!=1){
printf("%d^%d",s[i].f,s[i].cnt);
}
else{
printf("%d",s[i].f);
}
if(i<s.size()-1)
printf("*");
}
}
return 0;
}
1096 Consecutive Factors (20分)
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
5*6*7
第二题,就是暴力求连续因数子串,从2到sqrt遍历,找最长的数字子串。
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
long long i,j,n,ansI=0,ansLen=0,sqt;
cin>>n;
sqt=(long long)(sqrt(n)*1.0);
for(i=2;i<=sqt;i++){
long long t=1;
j=i;
while(1){
t*=j;
if(n%t!=0){
break;
}
if(j-i+1>ansLen){
ansI=i;
ansLen=j-i+1;
}
j++;
// cout8<<ansI<<" "<<ansLen<<endl;
}
}
if(ansLen==0){
cout<<1<<endl<<n;
}
else{
cout<<ansLen<<endl;
for(i=ansI;i<ansI+ansLen;i++){
cout<<i;
if(i<ansI+ansLen-1)
cout<<"*";
}
}
return 0;
}