|
| 1 | +#include<bits/stdc++.h> |
| 2 | + |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +#define ms(x,v) memset(x,(v), sizeof(x)) |
| 6 | +#define msn(x,v,n) memset(x,(v),sizeof(x[0]) * n) |
| 7 | +#define INF 0x3f3f3f3f |
| 8 | +#define random(a,b) (rand() % ((b) -(a)) +(a)) |
| 9 | + |
| 10 | +typedef long long LL; |
| 11 | +typedef pair<int,int> PII; |
| 12 | + |
| 13 | +const int MAXN = 1e6+10; |
| 14 | + |
| 15 | +namespace Prime{ |
| 16 | + LL power_mod(LL x,LL n,LL mod){ |
| 17 | + LL ret =1; |
| 18 | + while(n){ |
| 19 | + if(n & 1) ret = x * ret % mod; |
| 20 | + x = x * x % mod; |
| 21 | + n >>=1; |
| 22 | + } |
| 23 | + return ret; |
| 24 | + } |
| 25 | + LL mulmod(LL x,LL y,LL n){ |
| 26 | + return x * y % n; |
| 27 | + } |
| 28 | + bool witness(LL a,LL n,LL u,LL t){ |
| 29 | + LL x0 = power_mod(a,u,n),x1; |
| 30 | + for(int i=1 ;i<=t ; ++i){ |
| 31 | + x1 = mulmod(x0,x0,n); |
| 32 | + if(x1==1 && x0!=1 && x0!=n-1) |
| 33 | + return false; |
| 34 | + x0 = x1; |
| 35 | + } |
| 36 | + if(x1 !=1)return false; |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + bool miller_rabin(LL n, int times = 20){ |
| 41 | + if(n<2)return false; |
| 42 | + if(n==2)return true; |
| 43 | + if(!(n&1))return false; |
| 44 | + LL u = n-1,t =0; |
| 45 | + while (u%2==0) { |
| 46 | + t++;u>>=1; |
| 47 | + } |
| 48 | + while (times--) { |
| 49 | + LL a = random(1,n-1); |
| 50 | + //if(a == 0)std::cout << a << " "<<n<< " "<<u<<" " << t<<'\n'; |
| 51 | + if(!witness(a,n,u,t))return false; |
| 52 | + } |
| 53 | + return true; |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +int start; |
| 58 | +inline bool check_prime(int x){ |
| 59 | + return x == 1 || Prime::miller_rabin(x); |
| 60 | +} |
| 61 | + |
| 62 | +int check(int x){ |
| 63 | + int cnt =0; |
| 64 | + while(x %2 ==0){ |
| 65 | + x /=2; |
| 66 | + cnt ++; |
| 67 | + if(cnt >=4)break; |
| 68 | + } |
| 69 | + if(cnt ==4)return 0; |
| 70 | + else if(cnt ==3) return x ==1; |
| 71 | + else if(cnt ==2) return check_prime(x); |
| 72 | + else if(cnt == 1)return 1; |
| 73 | + else return check_prime(x); |
| 74 | + |
| 75 | +} |
| 76 | +void solve(){ |
| 77 | + LL l, r; |
| 78 | + cin >> l >> r; |
| 79 | + int ans =0; |
| 80 | + |
| 81 | + for(int i = l ; i<=r ; ++i){ |
| 82 | + ans += check(i); |
| 83 | + } |
| 84 | + cout << " " << ans; |
| 85 | +} |
| 86 | + |
| 87 | +int main(int argc, char const *argv[]) |
| 88 | +{ |
| 89 | + |
| 90 | + ios :: sync_with_stdio(0); |
| 91 | + cin.tie(0); |
| 92 | + // cout.tie(0); |
| 93 | + std::cout.precision(8); |
| 94 | + std::cout.setf( std::ios::fixed, std:: ios::floatfield ); |
| 95 | + int T; |
| 96 | + cin >> T; |
| 97 | + |
| 98 | + for(int kase =1; kase <= T ; ++kase) |
| 99 | + { |
| 100 | + cout << "Case #"<<kase << ":"; |
| 101 | + |
| 102 | + solve(); |
| 103 | + cout << '\n'; |
| 104 | + } |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
0 commit comments