fork download
  1. #include<stdio.h>
  2. //#include<math.h>
  3. #define true 1
  4. #define false 0
  5. typedef unsigned long long ulong;
  6. int prime[1000000];
  7.  
  8.  
  9. ulong power(ulong a,int n){
  10. if(n==0)
  11. return 1;
  12. if(n&1){
  13. ulong p=power(a,n>>1);
  14. return a*p*p;
  15. }
  16. else
  17. {
  18. ulong p=power(a,n>>1);
  19. return p*p;
  20. }
  21. }
  22.  
  23. int returnIndex(int i,ulong l){
  24. if(i>l)
  25. return 2;
  26. int j;
  27. for(j=2;j<100;j++){
  28. if(!prime[j-1]){
  29. if(power(i,j-1)>=l)
  30. {
  31. return j;
  32. }
  33. }
  34. }
  35. }
  36. void sieve(){
  37. int i;
  38. for(i=2;i<=1000;i++){
  39. int p=1-1%i;
  40. if(i*i>p)
  41. p=i*i;
  42.  
  43. if(p==i || p<1)
  44. p+=i;
  45.  
  46. while(p<=1000000){
  47. prime[p-1]=true;
  48. p+=i;
  49. }
  50. }
  51. }
  52. int main(){
  53. int t;
  54. scanf("%d",&t);
  55. sieve();
  56. while(t-->0){
  57. ulong l,r;
  58. scanf("%llu",&l);
  59. scanf("%llu",&r);
  60. int size=r-l+1;
  61. int input[size];
  62. // printf("Here I Am\n");
  63. // printf("Here I Am\n");
  64. int i,j;
  65. for(i=0;i<size;i++)
  66. input[i]=1;
  67. //mark all prime powers of primes...
  68. for(i=2;i<=r && i<1000000;i++){
  69. if(!prime[i-1]){
  70. int rem = l%i;
  71. int index=0;
  72. if(rem!=0)
  73. index = i-rem;
  74. for(j=index;j<size;j+=i){
  75. if((j+l)==i)
  76. {input[j]=1;
  77. //printf("applying 1 on j=%llu for i=%d\n",j+l,i);
  78. }
  79. else{
  80. // printf("unmarking %llu for i=%d\n",j+l,i);
  81. input[j]=0;
  82. }
  83. }
  84. }
  85. }
  86. //printf("prime powers successfully done.\n");
  87. // for(i=0;i<size;i++)
  88. // printf("mark=%d i=%d value=%llu\n",input[i],i,i+l);
  89. for(i=2;i<=r && i<1000000;i++){
  90. if(!prime[i-1]){
  91. int ret = returnIndex(i,l);
  92. for(j=ret;j>0;j++){
  93. if(!prime[j-1]){
  94. ulong temp=power(i,j-1);
  95. if(temp>r){
  96. break;
  97. }
  98. else{
  99. input[temp-l]=1;
  100. //printf("marking it back %llu for i=%d\n",temp,i);
  101. }
  102. }
  103. }
  104. }
  105. }
  106. int count=0;
  107. if(l==1)
  108. input[0]=0;
  109.  
  110. for(i=0;i<size;i++){
  111. if(input[i]){
  112. //printf("%llu\n",i+l);
  113. count++;
  114. }
  115. }
  116. printf("%d\n",count);
  117. }
  118. }
Success #stdin #stdout 2.74s 11040KB
stdin
50
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
1 1000000
999999000000 1000000000000
stdout
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400
78687
36400