蒟蒻代码
#include<iostream>
#include<queue>
#include<cmath>
using namespace std;
int main(void) {
long long int T;
cin >> T;
queue<long long int> Q;
for (int i = 0; i < T; ++i) {
long long int n, p, x, y;
cin >> n >> p >> x >> y;
if (x != y) {
Q.push(0);
for (int j = 0; j < n; ++j) {
int temp = Q.size();
while (temp--) {
int data = Q.front();
if ((data + x) % p) {
Q.push(data + x);
}
if ((data + y) % p) {
Q.push(data + y);
}
Q.pop();
}
}
long long int result = Q.size() % (long long int)(pow(10, 9) + 7);
cout << result << endl;
}
else {
int temp = 0;
for (int j = 1; j <= n; ++j) {
if (!((j * x) % p)) {
cout << "0" << endl;
break;
}
else {
++temp;
}
}
if (temp == n)
cout << "1" << endl;
}
}
return 0;
}