Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 1,901 Bytes
f7ba5f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

const int LIM = (int)1e9 + 1;
const bool READ_PRIMES_FROM_FILE = true;

vector<int> primes;

void load_primes() {
  primes.reserve(51000000);
  if (READ_PRIMES_FROM_FILE) {
    ifstream fin("primes.txt");
    for (int p; fin >> p;) {
      primes.push_back(p);
    }
  } else {
    bitset<LIM> sieve;
    int lim = sqrt(LIM);
    for (int i = 3; i < lim; i += 2) {
      if (!sieve[i] && i % 2 == 1) {
        for (int j = i * 2; j < LIM; j += i) {
          sieve.set(j);
        }
      }
    }
    primes.push_back(2);
    for (int i = 3; i <= LIM; i += 2) {
      if (!sieve[i]) {
        primes.push_back(i);
      }
    }
  }
}

// Returns k in the p^k term of the prime factorization of N!.
int legendre_exp(int p, int N) {
  int ans = 0;
  for (long long base = p; base <= N; base *= p) {
    ans += N / base;
  }
  return ans;
}

int powmod8(int p, int exp) {
  int ans = 1;
  for (exp %= 8; exp > 0; exp--) {
    ans = (ans * p) % 8;
  }
  return ans;
}

int N, K;

int solve() {
  bool is_one = true, is_two = true, two_exp_even = false;
  int mod8prod = 1;
  for (int p : primes) {
    if (p > N) {
      break;
    }
    int exp = legendre_exp(p, N) - legendre_exp(p, K);
    if (exp % 2 != 0) {
      is_one = false;
    }
    if (p % 4 == 3 && exp % 2 == 1) {
      is_two = false;
    }
    if (p == 2 && exp % 2 == 0) {
      two_exp_even = true;
    }
    if (p > 2) {
      mod8prod = (mod8prod * powmod8(p, exp)) % 8;
    }
  }
  bool is_three = !(two_exp_even && mod8prod == 7);
  return is_one ? 1 : is_two ? 2 : is_three ? 3 : 4;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  load_primes();
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {
    cin >> N >> K;
    cout << "Case #" << t << ": " << solve() << endl;
  }
  return 0;
}