Oppa Funcan Style Remastered

Surely you have seen insane videos by South Korean rapper PSY, such as “Gangnam Style”, “Gentleman” and “Daddy”. You might also hear that PSY has been recording video “Oppa Funcan Style” two years ago (unfortunately we couldn’t find it on the internet). We will remind you what this hit looked like (you can find original description here):

On the ground there are $n$ platforms, which are numbered with integers from $1$ to $n$, on $i$-th platform there is a dancer with number $i$. Further, every second all the dancers standing on the platform with number $i$ jump to the platform with the number $f(i)$. The moving rule $f$ is selected in advance and is not changed throughout the clip.

The duration of the clip was $k$ seconds and the rule $f$ was chosen in such a way that after $k$ seconds all dancers were in their initial positions (i.e. the $i$-th dancer stood on the platform with the number $i$). That allowed to loop the clip and collect even more likes.

PSY knows that enhanced versions of old artworks become more and more popular every day. So he decided to release a remastered-version of his video.

In his case “enhanced version” means even more insanity, so the number of platforms can be up to $10^{18}$! But the video director said that if some dancer stays on the same platform all the time, then the viewer will get bored and will turn off the video immediately. Therefore, for all $x$ from $1$ to $n$ $f(x) \neq x$ must hold.

Big part of classic video’s success was in that looping, so in the remastered version all dancers should return to their initial positions in the end of the clip as well.

PSY hasn’t decided on the exact number of platforms and video duration yet, so he asks you to check if there is a good rule $f$ for different options.

Input

In the first line of input there is one integer $t$ ($1 \le t \le 10^{4}$) — the number of options for $n$ and $k$ to check.

In the next $t$ lines the options are given: each option is described with two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^{15}$) — the number of dancers and the duration in seconds.

It is guaranteed that the number of different values of $k$ in one test is not greater than $50$.

Output

Print $t$ lines. If the $i$-th option of the video is feasible, print “YES” (without quotes) in $i$-th line, otherwise print “NO” (without quotes).

Example

input

3
7 7
3 8
5 6

output

YES
NO
YES

Solution:

#include <bits/stdc++.h>

using namespace std;

long long mulmod(long long a, long long b, long long c) {
long long sign = 1;
if (a < 0) {
    a = -a;
    sign = -sign;
  }
  if (b < 0) {
    b = -b;
    sign = -sign;
  }
  a %= c;
  b %= c;
  long long res = 0;
  while (b > 0) {
if (b & 1) {
res = (res + a) % c;
}
a = (a + a) % c;
b >>= 1;
}
if (sign == -1) {
res = (-res) % c;
}
return res;
}

template<typename T>
T extgcd(T a, T b, T &x, T &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
T p = b / a;
T g = extgcd(b - p * a, a, y, x);
x -= p * y;
return g;
}

template<typename T>
bool diophantine(T a, T b, T c, T &x, T &y, T &g) {
if (a == 0 && b == 0) {
if (c == 0) {
x = y = g = 0;
return true;
}
return false;
}
if (a == 0) {
if (c % b == 0) {
x = 0;
y = c / b;
g = abs(b);
return true;
}
return false;
}
if (b == 0) {
if (c % a == 0) {
x = c / a;
y = 0;
g = abs(a);
return true;
}
return false;
}
g = extgcd(a, b, x, y);
if (c % g != 0) {
return false;
}
T dx = c / a;
c -= dx * a;
T dy = c / b;
c -= dy * b;
x = dx + mulmod(x, c / g, b);
y = dy + mulmod(y, c / g, a);
g = abs(g);
return true;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
const int MAX = (int) (sqrt(1e15) + 1e3);
vector<bool> is_prime(MAX, true);
for (int i = 2; i * i < MAX; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j < MAX; j += i) {
        is_prime[j] = false;
      }
    }
  }
  vector<int> primes;
for (int i = 2; i < MAX; i++) {
    if (is_prime[i]) {
      primes.push_back(i);
    }
  }
  int sz = (int) primes.size();
  int tt;
  cin >> tt;
vector<long long> ns(tt), ks(tt);
map<long long,vector<int>> mapik;
vector<int> res(tt, 0);
for (int i = 0; i < tt; i++) {
    cin >> ns[i] >> ks[i];
mapik[ks[i]].push_back(i);
}
for (auto &p : mapik) {
long long k = p.first;
vector<long long> d;
{
long long tmp = k;
for (int it = 0; it < sz && (long long) primes[it] * primes[it] <= tmp; it++) {
        if (tmp % primes[it] == 0) {
          d.push_back(primes[it]);
          while (tmp % primes[it] == 0) {
            tmp /= primes[it];
          }
        }
      }
      if (tmp > 1) {
d.push_back(tmp);
}
}
if (d.size() == 0) {
continue;
}
if (d.size() == 1) {
for (int i : p.second) {
res[i] = (ns[i] % d[0] == 0);
}
continue;
}
if (d.size() == 2) {
for (int i : p.second) {
long long x, y, g;
if (diophantine(d[0], d[1], ns[i], x, y, g)) {
if (x >= 0 && y < 0) {
            long long can_subtr = x / d[1];
            long long need_add = ((-y) + d[0] - 1) / d[0];
            if (can_subtr >= need_add) {
y = 0;
}
}
if (x < 0 && y >= 0) {
long long can_subtr = y / d[0];
long long need_add = ((-x) + d[1] - 1) / d[1];
if (can_subtr >= need_add) {
x = 0;
}
}
res[i] = (x >= 0 && y >= 0);
}
}
continue;
}
const long long inf = (long long) 2e18;
vector<long long> dist(d[0], inf);
priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > s;
dist[0] = 0;
s.emplace(dist[0], 0);
while (!s.empty()) {
long long expected = s.top().first;
int i = s.top().second;
s.pop();
if (dist[i] != expected) {
continue;
}
for (int it = 1; it < (int) d.size(); it++) {
        int to = (int) ((i + d[it]) % d[0]);
        if (dist[i] + d[it] < dist[to]) {
          dist[to] = dist[i] + d[it];
          s.emplace(dist[to], to);
        }
      }
    }
    for (int i : p.second) {
      res[i] = (ns[i] >= dist[ns[i] % d[0]]);
}
}
for (int i = 0; i < tt; i++) {
    cout << (res[i] ? "YES" : "NO") << '\n';
  }
  return 0;
}