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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <tuple>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include "dzilib.h"

using namespace std;

struct VectorHash {
    size_t operator()(const std::vector<int>& v) const {
        std::hash<int> hasher;
        size_t seed = 0;
        for(int i : v) {
            seed ^= hasher(i) + 0x9e3779b9 + (seed<<6) + (seed>>2);
        }
        return seed;
    }
};

std::vector<int> eratosthenes(int n)
{
    std::vector<bool> prime(n + 1, true);
    std::vector<int> primes;
    prime[0] = prime[1] = false;
    for (int p = 2; p * p <= n; p++)
    {
        if (prime[p])
        {
            for (int i = p * p; i <= n; i += p)
            {
                prime[i] = false;
            }
        }
    }
    for (int p = 2; p <= n; p++)
    {
        if (prime[p])
        {
            primes.push_back(p);
        }
    }
    return primes;
}

std::vector<int> countDivisors(int n)
{
    auto primes = eratosthenes(static_cast<int>(std::sqrt(n)) + 1);
    std::vector<int> divisorsCount(n + 1, 1);

    for (int i = 2; i <= n; i++)
    {
        int num = i;
        for (auto prime : primes)
        {
            if (prime * prime > num)
            {
                break;
            }
            int power = 0;
            while (num % prime == 0)
            {
                num /= prime;
                power++;
            }
            divisorsCount[i] *= (power + 1);
        }
        if (num > 1)
        {
            divisorsCount[i] *= 2;
        }
    }

    return divisorsCount;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int t = GetT();
    int q = GetQ();
    long long c = GetC();
    long long n = GetN();

    const int dqSize = 17;
    int nPreprocess = n + dqSize + 2;

    auto divisors = countDivisors(nPreprocess + 1);
    std::unordered_map<std::vector<int>, int, VectorHash> map;
    map.reserve(n - dqSize + 1);

    std::vector<int> window(dqSize);

    for (int i = 0; i < dqSize; i++)
    {
        window[i] = divisors[i + 1];
    }

    for (int i = 1; i <= n; i++)
    {
        map[window] = i;
        std::rotate(window.begin(), window.begin() + 1, window.end());
        window[dqSize - 1] = divisors[i + dqSize];
    }

    while (t--)
    {
        std::vector<int> answers(dqSize);

        for (int i = 0; i < dqSize; i++)
        {
            answers[i] = Ask(i);
        }

        auto it = map.find(answers);
        if (it != map.end())
        {
            Answer(it->second);
        }
    }
    return 0;
}