PAT (Advanced Level) 1096. Consecutive Factors (20)

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1096. Consecutive Factors (20)相关的知识,希望对你有一定的参考价值。

如果是素数直接输出1与素数,否则枚举长度和起始数即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

long long n;

bool prime(long long x)
{
    if(x==1) return 0;
    if(x==2) return 1;
    for(long long i=2; i*i<=x; i++)
    {
        if(x%i==0) return 0;
    }
    return 1;
}

int main()
{
    scanf("%lld",&n);
    int f=0;

    if(prime(n))
    {
        printf("1\n");
        printf("%lld\n",n);
    }
    else
    {
        for(int len=10; len>=1; len--)
        {
            long long num=1;
            for(long long i=2;; i++)
            {
                if(i==2)
                {
                    for(long long j=i; j<=i+(long long)len-1; j++)
                    {
                        num=num*j;
                        if(num>n) break;
                    }
                }
                else num=num/(i-1)*(i+len-1);

                if(num>n) break;
                if(n%num==0)
                {
                    f=1;
                    printf("%d\n",len);
                    for(long long j=i; j<=i+len-1; j++)
                    {
                        printf("%lld",j);
                        if(j<i+len-1) printf("*");
                    }
                    printf("\n");
                    break;
                }
            }
            if(f==1) break;
        }
    }
    return 0;
}

 

以上是关于PAT (Advanced Level) 1096. Consecutive Factors (20)的主要内容,如果未能解决你的问题,请参考以下文章

PAT (Advanced Level) 1096. Consecutive Factors (20)

PAT Advanced 1096 Consecutive Factors (20分)

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079