1059 Prime Factors (25分) + 1096 Consecutive Factors (20分)

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 = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​.

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 = p​1​​^k​1​​*p​2​​^k​2​​**p​m​​^k​m​​, where p​i​​'s are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ -- hence when there is only one p​i​​, k​i​​ 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<2​31​​).

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;
}

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值