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,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可 ...
随机推荐
- 【bzoj1196】[HNOI2006]公路修建问题
二分答案 验证有一种贪心的思想,就是如果这条路的c1比二分的值还小,那就要果断选择一级公路. 搜过一遍后,如果可供选择的一级公路小于k,就可以直接返回否了. 接下来继续选择,如果可以选到n-1条路,就 ...
- 【bzoj1002】 [FJOI2007]轮状病毒DP
递推+环状特殊处理+高精度 #include<algorithm> #include<iostream> #include<cstdlib> #include& ...
- python 执行shell
一.import os ex: 1.os.system('ls') ----并不能得到返回值 2.output = os.popen('ls') res = output.read() ----能得到 ...
- Solution:Cannot pull with rebase: You have unstaged changes in Github
You can do this to work around using following steps 1. stash your changes with: git stash 2. pull f ...
- JZOJ 1003 [ 东莞市选 2007 ] 拦截导弹 —— 递推
题目:https://jzoj.net/senior/#main/show/1003 n^2 的话递推就可以啦. 代码如下: #include<iostream> #include< ...
- bzoj4889
http://www.lydsy.com/JudgeOnline/problem.php?id=4889 人傻常数大 bzoj上跑不过 洛谷上能过两到三个点 我写的是树套树啊 怎么跑的比分块还慢 每次 ...
- Datatable转换为Json 的方法
/// <summary> /// Datatable转换为Json /// </summary> /// <param n ...
- springmvc的jar包
<!-- spring框架的的组件构成(springFramework)--> 一.核心部分Core Container 1.1 spring-core,spring-beans 提供控 ...
- [App Store Connect帮助]一、 App Store Connect 使用入门(4)iOS 版 App Store Connect
通过 iOS 版 App Store Connect,您可以在移动设备上查看销售数据.App 元数据和顾客评论.您还可以检查 App 状态.发布您 App 的新版本并回应“Resolution Cen ...
- DB2锁表或超时解决方案
DB2锁表或超时 一.场景 对数据表进行更新(查询没问题),错误提示如下: SQLCODE=-911, SQLSTATE=40001, DRIVER=3.63.75SQL0911N The curre ...