zoukankan      html  css  js  c++  java
  • 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
    " ,cnt);
            for(int i=0;i<cnt;i++)
            {
                if(i) printf("*");
                printf("%d",st + i);
            }
            printf("
    ");
        }
        else printf("1
    %lld
    ",n);
    }
     
  • 相关阅读:
    研究生毕业论文如何选题
    关于虚拟机中开vmnet1(host)后pc不能上网的问题解决
    ubuntu10.04安装x264库
    CentOS 5.5无法识别Atheros AR8151网卡问题解决
    dm365工作笔记20130731
    DAVINCI DM365-DM368开发攻略——U-BOOT-2010.12及UBL的移植
    SQL语法练习
    T-SQL查询语句常用优化技巧总结
    【转】Asp.net控件开发学习笔记整理篇
    【转】Asp.net控件开发学习笔记整理篇
  • 原文地址:https://www.cnblogs.com/HazelNut/p/8489969.html
Copyright © 2011-2022 走看看