Advertisement
Guest User

Untitled

a guest
Sep 29th, 2014
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. const int Maxn = 100;
  8. const int Maxd = 10;
  9.  
  10. bool nprime[Maxn];
  11. bool mp[Maxn][Maxn];
  12. int t;
  13. ll a, b;
  14. int res;
  15.  
  16. int Gcd(int x, int y) { return x? Gcd(y % x, x): y; }
  17.  
  18. void Go(ll num, int lst)
  19. {
  20.     if (num > b) return;
  21.     if (a <= num) res++;
  22.     int curl = lst % 10 * 10;
  23.     for (int i = 0; i < Maxd; i++, curl++)
  24.         if (nprime[curl] && mp[lst][curl])
  25.             Go(10ll * num + i, curl);
  26. }
  27.  
  28. int main()
  29. {
  30.     nprime[0] = nprime[1] = true;
  31.     for (int i = 2; i < Maxn; i++) if (!nprime[i])
  32.         for (int j = i + i; j < Maxn; j += i) nprime[j] = true;
  33.     for (int i = 0; i < Maxn; i++)
  34.         for (int j = 0; j < Maxn; j++)
  35.             mp[i][j] = Gcd(min(i, j), max(i, j)) == 1;
  36.     scanf("%d", &t);
  37.     for (int tc = 1; tc <= t; tc++) {
  38.         scanf("%I64d %I64d", &a, &b);
  39.         res = 0;
  40.         for (int i = 10; i < Maxn; i++) if (nprime[i])
  41.             Go(i, i);
  42.         printf("Case #%d: %d\n", tc, res);
  43.     }
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement