CF1750D Count GCD
Problem
多组数据。
每组数据给定两个整数
Input
第一行输入
每组数据第一行是两个整数
每组数据第二行输入
Output
输出
Sample
Input 1
5
3 5
4 2 1
2 1
1 1
5 50
2 3 5 2 3
4 1000000000
60 30 1 1
2 1000000000
1000000000 2
Output 1
3
1
0
595458194
200000000
Solution
首先对于
因为
而
考虑容斥,我们令
由于
而对于
代码:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int kmax = 2e5 + 3;
const int Mod = 998244353;
int t, n, m;
int b[kmax];
int p[kmax], pc;
long long res;
long long Calc(int x, int y) { // 容斥
pc = 0;
int j = y;
for (int i = 2; i * i <= y; i++) { // 筛质因子
if (j % i) continue;
p[++pc] = i;
for (; j % i == 0; j /= i) {
}
}
if (j > 1) p[++pc] = j;
long long res = 0;
for (int i = 0; i < 1 << pc; i++) { // 枚举状态
int op = 1;
long long v = 1;
for (int j = 0; j < pc; j++) {
if (i & 1 << j) {
op *= -1;
v *= p[j + 1];
}
}
res += op * x / v;
}
return res;
}
void Solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
res = 1;
for (int i = 2; i <= n; i++) {
if (b[i - 1] % b[i]) { // 无解
cout << 0 << '\n';
return;
}
res = res * Calc(m / b[i], b[i - 1] / b[i]) % Mod; // 统计答案
}
cout << res << '\n';
}
int main() {
cin >> t;
while (t--) {
Solve();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】