P8926 题解
shinzAnmono · · 题解
一看题目,最大公约数,最小公倍数……
一言不合推式子:
令
我们知道,两个数互质的充要条件是两个数没有共同的质因数,对于
上面的情况只是当
我们使用线性筛预处理出
本题的空间可能有一点小,需要使用 bitset 进行优化。
附代码:
#include<iostream>
#include<algorithm>
#include<bitset>
#include<vector>
using ll = long long;
const int mod = 1e9 + 7;
const int sz = 1e8 + 10;
ll qpow(ll base, ll exp) {
ll ans = 1;
while (exp) {
if (exp & 1) ans = ans * base % mod;
base = base * base % mod, exp >>= 1;
}
return ans;
}
std::bitset<sz> vis;
std::vector<ll> primes;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
int n = 1e8;
for (int i = 2; i <= n; i++) {
if (!vis[i])
primes.push_back(i);
for (int j = 0; j < primes.size() && i * primes[j] <= n; j++) {
vis[i * primes[j]] = 1;
if (i % primes[j] == 0)
break;
}
}
while (t--) {
ll k, p, q, ans = 0;
std::cin >> k >> p >> q;
if (k == 1) {
std::cout << q - p + 1 << "\n";
continue;
}
for (ll p: primes) {
if (p * p > k) break;
if (k % p == 0) {
ans++;
while (k % p == 0) k /= p;
}
}
if (k > 1) ans++;
std::cout << qpow(2, ans) * (q - p + 1) % mod << "\n";
}
return 0;
}