题目描述

       给定一个素数,试判断能否将该素数写为b3a3

的形式,a,b皆为非负整数。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

输入

多组输入

每行一个素数P (2P1015)

 

输出

若可以分解输出a,b(a<b)

,不能输出-1 -1;

 

样例输入

19

样例输出

2 3


题解:用公式降幂:a3-b3=(a-b)(a2+b2+ab);
因为a3-b3=素数,所以a-b=1;
所以解a2+b2+ab=p就行
注意:用cin,cout会超时
#include <iostream>
#include<stdio.h>
#include <math.h>
#include <string.h>
#define ll long long
using namespace std;
int main()
{
    ll p, st;
    double t;
    while (~scanf("%lld", &p))
    {
        st = 9 - 4 * 3 * (1 - p);
        t = sqrt(st);
        int tt;
        tt=(int)t;
        if(t!=tt)
            printf("-1 -1\n");
        else
        {
            int x1;
            x1 = (-3 + tt) / 6;
            printf("%d %d\n",x1,x1+1);
            
        }
    }
    return 0;
}

 


扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄