Advertisement
a53

pdigit

a53
Feb 25th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. # include <fstream>
  2. # include <bitset>
  3. # define N_Max 5000050
  4. using namespace std;
  5. ifstream f("pdigit.in");
  6. ofstream g("pdigit.out");
  7.  
  8. bitset <N_Max+1>ciur;
  9. int p[360001];
  10. int s,k,n,c,i,a,b,nr,j,m,x,y,z;
  11. void ciurerathos(int N)
  12. {
  13. int i,j,M;
  14. p[1]=1;N+=50;
  15. for(i=2;i<=N;++i)
  16. {
  17. if(ciur[i]==0)
  18. {
  19. p[++nr]=i;M=N/i;
  20. for(j=2;j<=M;++j)
  21. ciur[i*j]=1;
  22. }
  23. }
  24. }
  25.  
  26. int cifracontrol(int a,int b)
  27. {
  28. int z=a,s,P,x;
  29. while(z>=b)
  30. {
  31. ///calculam suma
  32. s=0;
  33. while(z>0)
  34. {
  35. if(z%10>=b)
  36. return -1;
  37. s+=z%10;
  38. z/=10;
  39. }
  40. ///trec numarul in baza b
  41. P=1;x=0;
  42. while(s>0)
  43. {
  44. x+=(s%b)*P;
  45. P*=10;
  46. s/=b;
  47. }
  48. z=x;
  49. }
  50. return z;
  51. }
  52.  
  53. int main()
  54. {
  55. f>>x>>y;
  56. ciurerathos(y);
  57. ///caut primul numar prim care are cifra de control = c >=a
  58. i=1;j=nr;
  59. while(i<=j)
  60. {
  61. m=(i+j)>>1;
  62. if(x<=p[m])
  63. j=m-1;
  64. else
  65. i=m+1;
  66. }
  67. a=p[j+1];
  68. g<<a<<'\n';
  69. ///determin baza b
  70. b=0;z=a;
  71. while(z>0)
  72. {
  73. c=z%10;
  74. if(c>b)
  75. b=c;
  76. z/=10;
  77. }
  78. ++b;
  79. g<<b<<'\n';
  80. ///determin cifra de control
  81. c=cifracontrol(a,b);
  82. g<<c<< '\n';
  83. ///determin numerele prime din intervalul [x,y]
  84. i=j+2;k=1;
  85. while(p[i]<=y)
  86. {
  87. if(cifracontrol(p[i],b)==c)
  88. ++k;
  89. ++i;
  90. }
  91. g<<k<<'\n';
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement