fork download
  1. ///=======================================//
  2. /// Author : Maruf Tuhin //
  3. /// School : CUET CSE 11 //
  4. /// TopCoder : the_redback //
  5. /// Codeforces : maruf.2hin //
  6. /// UVA : Redback //
  7. /// http://w...content-available-to-author-only...b.com/maruf.2hin //
  8. ///=======================================//
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <cstdlib>
  12. #include <cmath>
  13. #include <algorithm>
  14. #include <iostream>
  15. #include <string>
  16. #include <vector>
  17. using namespace std;
  18. #define MAX 50001
  19. unsigned long pr[(MAX>>5) +1];
  20.  
  21. void sieve(int n)
  22. {
  23. long i,j,k,l;
  24. pr[0]=pr[0]|1<<1;
  25.  
  26. for(i=4; i<=n; i+=2)
  27. pr[i>>6]=pr[i>>5]|1<<(i&31);
  28.  
  29. for(i=3; i*i<=n; i+=2)
  30. {
  31. if(!(pr[i>>5]&1<<(i&31)))
  32. {
  33. for(j=i*i; j<=n; j+=2*i)
  34. pr[j>>5]=pr[j>>5]|1<<(j&31);
  35. }
  36. }
  37. }
  38.  
  39. main()
  40. {
  41. vector<int>v;
  42. sieve(50000);
  43. long long n,m;
  44. long long k;
  45. long long i;
  46. int j,t;
  47. while(scanf("%lld",&m)==1)
  48. {
  49. if(m==0)
  50. return 0;
  51. if(m<0)
  52. {
  53. n=m*-1;
  54. }
  55. else
  56. n=m;
  57. k=n;
  58. for(i=2; i*i<=n; i++)
  59. {
  60. while(pr[i>>5]&1<<(i&31))
  61. {
  62. i++;
  63. }
  64. while(k%i==0)
  65. {
  66. k/=i;
  67. v.push_back(i);
  68. }
  69. if(k==1)
  70. break;
  71. }
  72. printf("%lld = ",m);
  73. t=0;
  74. if(m<0)
  75. {
  76. printf("-1");
  77. t=1;
  78. }
  79. for(i=0; i<v.size(); i++)
  80. {
  81. if(t!=0)
  82. printf(" x ");
  83. printf("%d",v[i]);
  84. t=1;
  85. }
  86. if(k!=1)
  87. {
  88. if(t!=0)
  89. printf(" x ");
  90. printf("%lld",k);
  91. }
  92. printf("\n");
  93. v.clear();
  94. }
  95. return 0;
  96. }
  97.  
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
Standard output is empty