B: Break Prime

题目描述

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

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

输入

多组输入

每行一个素数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
");
        else
        {
            int x1;
            x1 = (-3 + tt) / 6;
            printf("%d %d
",x1,x1+1);
            
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/-citywall123/p/10704268.html