题意:有一个长度为n的数组 a,对于每个1 ≤ i ≤ n, 1 ≤ ai ≤ m。构造出一个数组 b 使对于每个 1 ≤ i ≤ n, 1 ≤ bi ≤ m 并且 gcd(b1,b2,b3,...,bi)= ai.求数组b的数量。
思路:首先有答案必须要保证对于每个2 ≤ i ≤ n,前面的gcd必须是当前ai的倍数,也就是 ai-1 mod ai 等于 0(前面的gcd的值等于 ai - 1)。然后考虑每个 bi,bi 的因子里必须要有ai,如果不考虑 ai - 1对当前 bi 的影响,bi 可以取的值的数量是 m / ai,但是 ai- 1 和当前bi的最大公约数是ai,也就是说明 ai - 1 和 bi 除了 ai 这个公共因子外,没有其他的公共质因子。所以 bi 要在可以取的值中去掉 ai-1 / ai 的因子的倍数,去掉的时候要用容斥原理。
代码:
#include<bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 2e5 + 10, P = 1e9 + 7, mod = 998244353;
ll a[N];
void solve() {
ll n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
ll ans = 1;
for (int i = 2; i <= n; i++) {
if(a[i - 1] % a[i]) {
ans = 0;
break;
}
vector<ll> v;
ll x = a[i - 1] / a[i];
for (int j = 2; j * j <= x; j++) {
if(x % j == 0) {
v.pb(j);
while(x % j == 0) x /= j;
}
}
if(x > 1) v.pb(x);
int sz = v.size();
ll res = m / a[i];
ll r = res;
for (int j = 1; j < (1 << sz); j++) {
int x = j;
ll fac = 1, cnt = 0, now = 0;
while(x) {
if(x & 1) cnt++, fac *= v[now];
now++;
x >>= 1;
}
if(cnt & 1) res -= r / fac;
else res += r / fac;
}
ans = ans * res % mod;
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}