PAT 1096. Consecutive Factors
Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 356*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
分析
这道题自己的解法超时了,于是看了别人的解法,最后输出的长度是不会超过12的,因为13的阶乘大于2^31了,并且可以知道如果长度大于等于2的话,那么第一个数是一定小于sqrt(n)的,否则乘积一定大于n.
附上别人的解析:
用暴力破解,一个个地尝试呀既然是递增连续的因子长度那么肯定是不重复的几个连着的数字相乘咯然后就想到了阶乘的概念对不对,首先题目说了n最大就2的31次方,后来我掐指一算就知道了2的31次方是介于12的阶乘和13的阶乘之间的大小的也就是说~~所求连续因子的长度再怎么长,也长不过12个数字那就从len=12开始,一直到len=1,看看能不能找到一个满足条件的序列咯
假如长度比1大那么至少得两个相邻的数相乘那么这两个数的乘积是不可能超过n的~【就是说如果不止一个因子相乘,那么所求因子序列的第一个数字不可能超过sqrt(n)】那就从start = 2开始(和从1开始相乘是一个意思啦= =乘不乘以1不都一样嘛= =)一直到start = sqrt(n),把start * (start+ 1)(start+2)…【嗯对一共要乘以len的长度个因为我们现在在尝试是否有连续的len长度的因子相乘满足条件】,乘完了的结果ans看看是不是n的约数【就是看看n%ans是不是等于0咯】,等于0就好办啦,说明已经找到这个最长的连续因子啦~快输出.然后直接return.耶耶耶.要是没找到,那说明什么呢。。说明它是个质数(就是说说明它因子只有他自己本身),那就输出长度为1,而且因子就是n本身咯
你可能会想。。既然len=1为什么要算?因为题目里说输出最小的连续因子序列,那么如果这个数不是质数,也就是说在检验len=1的时候发现了一个比这个数小的因子x,那个这个x就是最小的因子啦,就不能够是等到最后一句输出n为最小的因子啦,如果n是质数,那么小于sqrt(n)的限定情况下会保证它不找到n的本身这个数的,所以到最后要加上printf(“1\n%d”, n);哦,保证它是质数的时候也能被输出答案.
#include<iostream>
#include<math.h>
using namespace std;
int main(){
long long int n;
cin>>n;
long long int max=sqrt(n);
for(int len=12;len>=1;len--)
for(int start=2;start<=max;start++){
long long int ans=1;
for(int i=start;i-start<=len-1;i++)
ans*=i;
if(n%ans==0){
printf("%d\n%d",len,start);
for(int i=start+1;i-start<=len-1;i++)
printf("*%d",i);
return 0;
}
}
printf("1\n%d",n);
return 0;
}
PAT 1096. Consecutive Factors的更多相关文章
- PAT 1096 Consecutive Factors[难]
1096 Consecutive Factors (20 分) Among all the factors of a positive integer N, there may exist sever ...
- PAT甲级——1096 Consecutive Factors (数学题)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859 1096 Consecutive Factors ...
- PAT (Advanced Level) Practise - 1096. Consecutive Factors (20)
http://www.patest.cn/contests/pat-a-practise/1096 Among all the factors of a positive integer N, the ...
- PAT 甲级 1096 Consecutive Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...
- PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]
题目 Among all the factors of a positive integer N, there may exist several consecutive numbers. For e ...
- 1096. Consecutive Factors (20)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- PAT A1096 Consecutive Factors (20 分)——数字遍历
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- PAT (Advanced Level) 1096. Consecutive Factors (20)
如果是素数直接输出1与素数,否则枚举长度和起始数即可. #include<cstdio> #include<cstring> #include<cmath> #in ...
- PAT甲题题解-1096. Consecutive Factors(20)-(枚举)
题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可 ...
随机推荐
- ios14--购物车优化2
// // ViewController.m // 03-综合练习 // // Created by xiaomage on 15/12/28. // Copyright © 2015年 小码哥. A ...
- YTU 2706: 编写一个函数求最大的n值
2706: 编写一个函数求最大的n 值. 时间限制: 1 Sec 内存限制: 128 MB 提交: 341 解决: 132 题目描述 编写一个函数求满足以下条件的最大的n.:12+22+32+-+ ...
- WPF-使用面板控制内容布局,比较Canvas,WrapPanel,StackPanel,Grid,ScrollViewer
WPF-使用面板控制内容布局,比较Canvas,WrapPanel,StackPanel,Grid,ScrollViewer 分类: WPF2012-04-24 09:59 660人阅读 评论(0) ...
- 【POJ 3190】 Stall Reservations
[题目链接] http://poj.org/problem?id=3190 [算法] 将这些牛按开始吃草的时间排序 维护一个数组S,Si表示畜栏i进去的最后一头牛结束吃草的时间,对于每头牛,找任意一个 ...
- RDA 搜台
转载马斯特·李 流程: 将channel的读写回调在AL_FW_Init中注册 初始化datasaving部件,注册datasaving的回调,并建立DATASAVING_NvmStore_Threa ...
- bzoj1477 && exgcd学习笔记
exgcd 由于忘记了exgcd,这道题就没做出来... exgcd的用处是求ax+by=gcd(a,b)这样方程的解 大概是这个样子的 void ext_gcd(long long a, long ...
- MSP430 PIN 操作寄存器
1.P口端口寄存器: (1).PxDIR 输入/输出方向寄存器 (0:输入模式 1:输出模式) (2).PxIN 输入寄存器 输入寄存器是只读寄存器,用户不能对其写入,只能通过读取该寄 ...
- compileSdkVersion, minSdkVersion 和 targetSdkVersion的选择(copy)
英文原文:Picking your compileSdkVersion, minSdkVersion, and targetSdkVersion 作者:Ian Lake,Google Android ...
- IP V4 和 IP V6 初识
IP V4 是互联网协议的第四版 地址长度为32位,4字节,用十进制表示 格式为:A.B.C.D 最大的问题在于网络地址资源有限,严重制约了互联网的应用和发展 IP V6 是互联网协议的I ...
- js addeventlistener 刮刮贴
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...