HDU 2866-Special Prime(数论)

来源:互联网 发布:办公软件图标 编辑:程序博客网 时间:2024/06/18 05:09

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2866

题意:问你1到L中有多少个素数满足n^3 + p*n^2 = m^3(其中n,m为大于1的自然数)

题目思路:直接暴力n和m肯定是不现实的,呢就要从这个式子下手了,化简一下得到 n^2 *( n + p ) = m^3 
假设 n^2 和 n+p 之间有公共素因子 p , 那么 n+p = k*p , 即 n=p*(k-1),

带进去得到 p^3 * (k-1)^2 *k = m^3 , (k-1)^2*k 肯定是不能表示成某一个数的三次幂的,

所以假设不成立,所以 n^2 和 n+p 之间没有公共素因子 p ,
 那么可以假设n=x^3 , n+p=y^3 , 相减得到 p = y^3 - x^3 = (y-x) *(y^2+y*x+x^2) ,  p是素数,
所以 y-x=1 (这不用过多解释了吧,素数的因子只有1和P,右括号肯定是大于1的数了),
故 p =(x+1)^3 - x^3 = 3*x^2+3*x+1 , 暴力一下x,判断是否是素数就ok了。。。

#include<map>  #include<stack>  #include<queue>  #include<vector>  #include<math.h>  #include<stdio.h>  #include<iostream>  #include<string.h>  #include<stdlib.h>  #include<algorithm>  using namespace std;  typedef long long  ll;  #define inf 1000000000  #define mod 1000000007 #define  maxn  5000500#define  lowbit(x) (x&-x)  #define  eps 1e-10  int  a[maxn]={1,1};int  main(void){ll i,j,n,k=0,num;for(i=2;i<=maxn;i++){if(a[i])continue;for(j=i*i;j<=maxn;j+=i)a[j]=1;}while(scanf("%lld",&n)!=EOF){num=0;for(i=1;;i++){ll tmp=3*i*i+3*i+1;if(tmp>n)break;if(a[tmp]==0)num++;}if(num>0)printf("%lld\n",num);elseprintf("No Special Prime!\n");}return 0;}


原创粉丝点击