PAT 天梯赛练习集 L1-006. 连续因子

题目链接:https://www.patest.cn/contests/gplt/L1-006

一个正整数N的因子中可能存在若干连续的数字。例如630可以分解为3*5*6*7,其中5、6、7就是3个连续的数字。给定任一正整数N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。

输入格式:

输入在一行中给出一个正整数N(1<N<231)。

输出格式:

首先在第1行输出最长连续因子的个数;然后在第2行中按“因子1*因子2*……*因子k”的格式输出最小的连续因子序列,其中因子按递增顺序输出,1不算在内。

输入样例:

630

输出样例:

3
5*6*7

连续因子相乘小于231最大为13!,即连续因子串长度最大为13,预处理把sqrt(231)的范围内的长度为1-13的连续数字相乘得到的积存在map中,然后遍历map,找到能被输入的数整除的包含因子个数最多的、起始位置最小的数,就是答案了。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
using namespace std;
typedef long long ll;

const ll lim = (1LL << 31);
map <ll,pair<int,int> > mp;

int main()
{
    ll mid = sqrt(lim);
    for(int i=1;i<=13;i++)
        for(ll s=2;s<mid;s++)
        {
            ll tmp = s;
            for(ll j=1;j<i;j++)
                tmp *= (s + j);
            if(tmp > lim) break;
            mp[tmp] = make_pair(i,s);
        }
    ll tp = 0; int cnt = 0, st = 0;
    ll n;
    scanf("%lld",&n);
    for(map<ll,pair<int,int> >::iterator it = mp.begin();it != mp.end(); it++)
    {
        if(n % it->first == 0)
        {
            if((it -> second).first > cnt)
            {
                tp = it -> first;
                cnt = (it -> second).first;
                st = (it->second).second;
            }else if((it->second).first == cnt)
            {
                if((it->second).second < st)
                {
                    tp = it -> first;
                    cnt = (it -> second).first;
                    st = (it->second).second;
                }
            }
        }
    }
    if(tp)
    {
        printf("%d\n" ,cnt);
        for(int i=0;i<cnt;i++)
        {
            if(i) printf("*");
            printf("%d",st + i);
        }
        printf("\n");
    }
    else printf("1\n%lld\n",n);
}

 

 

转载于:https://www.cnblogs.com/HazelNut/p/8489969.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值