CF EDU 106 D - The Number of Pairs
D - The Number of Pairs
数论,线性筛
给定 求满足 的 对数 (当 时, 算两对)
记
因此
所以 是 的因子, 枚举 的因子
, 此时 为常数,记为
因为 与 互质,所以将 标准分解后, 有 种素因子,则 对数有 个(一种素因子要么在 , 要么在 )
但标准分解 太慢,设 为 有几种素因子,可以发现 有类似积性函数的递推关系:
因此可利用线性筛预处理出
注意 , 范围为 而非 , 要线性筛预处理到
CF EDU 106 D - The Number of Pairs
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 2e7 + 10;
int pr[N], f[N], cnt;
bool st[N];
ll qmi(ll a, ll b)
{
ll ans = 1;
while(b)
{
if (b & 1)
ans *= a;
b >>= 1;
a *= a;
}
return ans;
}
void get_primes(int n)
{
memset(st, true, sizeof st);
st[1] = false;
f[1] = 0;
for (int i = 2; i <= n; i++)
{
if (st[i])
{
pr[++cnt] = i;
f[i] = 1;
}
for (int j = 1; j <= cnt && pr[j] <= n / i; j++)
{
int p = pr[j];
st[i * p] = false;
if (i % p == 0)
{
f[i * p] = f[i];
break;
}
else
f[i * p] = f[i] + 1;
}
}
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
get_primes(N - 10);
int T;
cin >> T;
while(T--)
{
int c, d, x;
cin >> c >> d >> x;
vector<int> factor;
for (int i = 1; i <= x / i; i++)
{
if (x % i)
continue;
factor.push_back(i);
if (i != x / i)
factor.push_back(x / i);
}
ll ans = 0;
for (int y : factor)
{
if ((y + d) % c)
continue;
int t = (y + d) / c;
ans += qmi(2, f[t]);
}
cout << ans << endl;
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话