解題思路
若 l 不是 g 的倍數,立即無解。否則寫 a=gx、b=gy,則 xy=l/g=c 且 gcd(x,y)=1。
分解 c 為互異質數的完整冪 p^e。為保持互質,每個完整質數冪必須全放 x 或全放 y,不能拆開。
枚舉所有質數冪子集合乘積 x,y=c/x,檢查 g(x+y)≤T。每個子集合是一個有序組,空集合與全集都要枚舉;c=1僅有一組。
設 V=max(l/g)≤10^12,篩 O(10^6 log log10^6),每查詢試除至sqrt(c)內質數再 O(2^ω(c));空間 O(10^6+q+2^ω(c))。
Step-by-step animation
教學例子:g=2、l=12,c=6=2×3。
步驟 1 / 3
步驟 1
g=2、l=12,c=6=2×3。
2
3
- 狀態
- 兩個質數冪
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
const int V = 1000000;
vector<bool> ok(V + 1, true);
vector<int> pr;
for (int i = 2; i <= V; i++)
if (ok[i]) {
pr.push_back(i);
if (1LL * i * i <= V)
for (int j = i * i; j <= V; j += i)
ok[j] = false;
}
int q;
cin >> q;
while (q--) {
long long g, l, T;
cin >> g >> l >> T;
if (l % g) {
cout << 0 << '\n';
continue;
}
long long c = l / g, x = c;
vector<long long> powers;
for (long long p : pr) {
if (p * p > x)
break;
if (x % p == 0) {
long long z = 1;
while (x % p == 0) {
x /= p;
z *= p;
}
powers.push_back(z);
}
}
if (x > 1)
powers.push_back(x);
vector<long long> div = {1};
for (auto p : powers) {
int n = div.size();
for (int i = 0; i < n; i++)
div.push_back(div[i] * p);
}
int ans = 0;
for (auto a : div)
ans += g * (a + c / a) <= T;
cout << ans << '\n';
}
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。