HDU - 2866- Special Prime【 数论 】题解

傷城~ 2021-07-25 00:10 467阅读 0赞

目录

      • 1.题目
      • 2.代码

1.题目

Give you a prime number p, if you could find some natural number (0 is not inclusive) n and m, satisfy the following expression:
在这里插入图片描述
We call this p a “Special Prime”.
AekdyCoin want you to tell him the number of the “Special Prime” that no larger than L.
For example:
If L =20
1^3 + 71^2 = 2^3
8^3 + 19
8^2 = 12^3
That is to say the prime number 7, 19 are two “Special Primes”.
Input
The input consists of several test cases.
Every case has only one integer indicating L.(1<=L<=10^6)
Output
For each case, you should output a single line indicate the number of “Special Prime” that no larger than L. If you can’t find such “Special Prime”, just output “No Special Prime!”
Sample Input
7
777
Sample Output
1
10

2.代码

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. using namespace std;
  5. const int maxn=1e6+10;
  6. int prime[maxn],ans[maxn];
  7. void isprime()
  8. {
  9. prime[0]=1;
  10. prime[1]=1;
  11. for(int i=2;i<maxn;i++)
  12. {
  13. if(prime[i]==0)
  14. {
  15. for(int j=i+i;j<maxn;j+=i)
  16. prime[j]=1;
  17. }
  18. }
  19. }
  20. bool judge(int p)//判断12*P-3是否是平方数
  21. {
  22. int m=12*p-3;
  23. int k=(int)sqrt(m);
  24. return k*k==m;
  25. }
  26. void table()
  27. {
  28. for(int i=1;i<maxn;i++)
  29. {
  30. if(!prime[i]&&judge(i))
  31. ans[i]++;
  32. ans[i]+=ans[i-1];
  33. }
  34. }
  35. int main()
  36. {
  37. isprime();
  38. table();
  39. int l;
  40. while(cin>>l)
  41. {
  42. if(ans[l])
  43. cout<<ans[l]<<endl;
  44. else
  45. cout<<"No Special Prime!"<<endl;
  46. }
  47. return 0;
  48. }

发表评论

表情:
评论列表 (有 0 条评论,467人围观)

还没有评论,来说两句吧...

相关阅读